i915g: Use the devmaster quadratic approximation for sin/cos.

11 instructions, but now processes up to 4 channels at once (since TGSI
splits to scalar for these math ops) while being higher accuracy.
Previously we used 6 instructions per channel, but it didn't look like a
sine wave.  i915c managed it in 9 instructions per scalar channel, thanks
to avoiding an extra mov we do for the fabs (should be fixable), and
avoiding an extra MUL (maybe just needs reassociation of our immediates?).
But, the ALU count win from doing 4 channels at once will be way more
important for making sure that programs compile than those 2 ALU ops, plus
now we do it in NIR instead of assembly.

Closes: #4981
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/12250>
This commit is contained in:
Emma Anholt
2021-08-05 15:41:04 -07:00
committed by Marge Bot
parent f800b9182b
commit 01e9824997
8 changed files with 95 additions and 139 deletions

View File

@@ -517,65 +517,24 @@ dEQP-GLES2.functional.shaders.loops.while_uniform_iterations.single_iteration_fr
dEQP-GLES2.functional.shaders.loops.while_uniform_iterations.single_statement_fragment,Fail
dEQP-GLES2.functional.shaders.loops.while_uniform_iterations.unconditional_continue_fragment,Fail
dEQP-GLES2.functional.shaders.loops.while_uniform_iterations.vector_counter_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.cos.highp_float_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.cos.highp_vec2_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.cos.highp_vec3_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.cos.highp_vec4_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.cos.mediump_float_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.cos.mediump_vec2_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.cos.mediump_vec3_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.cos.mediump_vec4_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.sin.highp_float_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.sin.highp_vec2_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.sin.highp_vec3_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.sin.highp_vec4_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.sin.mediump_float_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.sin.mediump_vec2_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.sin.mediump_vec3_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.sin.mediump_vec4_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.tan.highp_float_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.tan.highp_vec2_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.tan.highp_vec3_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.tan.highp_vec4_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.tan.lowp_float_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.tan.lowp_vec2_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.tan.lowp_vec3_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.tan.lowp_vec4_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.tan.mediump_float_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.tan.mediump_vec2_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.tan.mediump_vec3_fragment,Fail
dEQP-GLES2.functional.shaders.operator.angle_and_trigonometry.tan.mediump_vec4_fragment,Fail
dEQP-GLES2.functional.shaders.random.all_features.fragment.22,Fail
dEQP-GLES2.functional.shaders.random.all_features.fragment.32,Fail
dEQP-GLES2.functional.shaders.random.all_features.fragment.34,Fail
dEQP-GLES2.functional.shaders.random.all_features.fragment.38,Fail
dEQP-GLES2.functional.shaders.random.all_features.fragment.45,Fail
dEQP-GLES2.functional.shaders.random.all_features.fragment.49,Fail
dEQP-GLES2.functional.shaders.random.all_features.fragment.5,Fail
dEQP-GLES2.functional.shaders.random.all_features.fragment.53,Fail
dEQP-GLES2.functional.shaders.random.all_features.fragment.63,Fail
dEQP-GLES2.functional.shaders.random.all_features.fragment.78,Fail
dEQP-GLES2.functional.shaders.random.all_features.fragment.80,Fail
dEQP-GLES2.functional.shaders.random.all_features.fragment.86,Fail
dEQP-GLES2.functional.shaders.random.all_features.fragment.89,Fail
dEQP-GLES2.functional.shaders.random.all_features.fragment.93,Fail
dEQP-GLES2.functional.shaders.random.all_features.fragment.97,Fail
dEQP-GLES2.functional.shaders.random.trigonometric.fragment.0,Fail
dEQP-GLES2.functional.shaders.random.trigonometric.fragment.1,Fail
dEQP-GLES2.functional.shaders.random.trigonometric.fragment.12,Fail
dEQP-GLES2.functional.shaders.random.trigonometric.fragment.15,Fail
dEQP-GLES2.functional.shaders.random.trigonometric.fragment.20,Fail
dEQP-GLES2.functional.shaders.random.trigonometric.fragment.26,Fail
dEQP-GLES2.functional.shaders.random.trigonometric.fragment.28,Fail
dEQP-GLES2.functional.shaders.random.trigonometric.fragment.4,Fail
dEQP-GLES2.functional.shaders.random.trigonometric.fragment.42,Fail
dEQP-GLES2.functional.shaders.random.trigonometric.fragment.45,Fail
dEQP-GLES2.functional.shaders.random.trigonometric.fragment.48,Fail
dEQP-GLES2.functional.shaders.random.trigonometric.fragment.52,Fail
dEQP-GLES2.functional.shaders.random.trigonometric.fragment.63,Fail
dEQP-GLES2.functional.shaders.random.trigonometric.fragment.7,Fail
dEQP-GLES2.functional.shaders.random.trigonometric.fragment.81,Fail
dEQP-GLES2.functional.shaders.random.trigonometric.fragment.92,Fail
dEQP-GLES2.functional.shaders.return.return_in_dynamic_loop_dynamic_fragment,Fail
dEQP-GLES2.functional.shaders.return.return_in_dynamic_loop_never_fragment,Fail
dEQP-GLES2.functional.shaders.return.return_in_static_loop_dynamic_fragment,Fail
@@ -670,7 +629,6 @@ KHR-GLES2.core.internalformat.texture2d.depth_component_unsigned_short_depth_com
KHR-GLES2.shaders.aggressive_optimizations.cos_float_frag,Fail
KHR-GLES2.shaders.aggressive_optimizations.cos_vec2_frag,Fail
KHR-GLES2.shaders.aggressive_optimizations.cos_vec3_frag,Fail
KHR-GLES2.shaders.aggressive_optimizations.sin_float_frag,Fail
KHR-GLES2.shaders.aggressive_optimizations.sin_vec2_frag,Fail
KHR-GLES2.shaders.aggressive_optimizations.sin_vec3_frag,Fail
KHR-GLES2.texture_3d.copy_sub_image.negative,Fail

View File

@@ -1,5 +1,4 @@
shaders@glsl-bug-110796,Fail
shaders@glsl-cos,Fail
shaders@glsl-derivs,Fail
shaders@glsl-fs-loop,Fail
shaders@glsl-fs-loop-nested,Fail
@@ -7,7 +6,6 @@ shaders@glsl-fs-raytrace-bug27060,Fail
shaders@glsl-fwidth,Fail
shaders@glsl-max-varyings,Fail
shaders@glsl-max-varyings >max_varying_components,Fail
shaders@glsl-sin,Fail
shaders@glsl-uniform-interstage-limits@520 vs- 1 fs,Fail
shaders@glsl-uniform-interstage-limits@subdivide 5,Fail
shaders@glsl-uniform-interstage-limits@subdivide 5- statechanges,Fail
@@ -463,10 +461,6 @@ spec@ext_texture_srgb@texwrap formats-s3tc bordercolor@GL_COMPRESSED_SRGB_ALPHA-
spec@ext_texture_srgb@texwrap formats-s3tc bordercolor@GL_COMPRESSED_SRGB_S3TC_DXT1_EXT- border color only,Fail
spec@glsl-1.10@execution@built-in-functions@fs-atan-vec3-vec3,Fail
spec@glsl-1.10@execution@built-in-functions@fs-atan-vec4-vec4,Fail
spec@glsl-1.10@execution@built-in-functions@fs-cos-float,Fail
spec@glsl-1.10@execution@built-in-functions@fs-cos-vec2,Fail
spec@glsl-1.10@execution@built-in-functions@fs-cos-vec3,Fail
spec@glsl-1.10@execution@built-in-functions@fs-cos-vec4,Fail
spec@glsl-1.10@execution@built-in-functions@fs-degrees-float,Fail
spec@glsl-1.10@execution@built-in-functions@fs-degrees-vec2,Fail
spec@glsl-1.10@execution@built-in-functions@fs-degrees-vec3,Fail
@@ -628,10 +622,6 @@ spec@glsl-1.10@execution@built-in-functions@fs-reflect-vec4-vec4,Fail
spec@glsl-1.10@execution@built-in-functions@fs-refract-vec2-vec2-float,Fail
spec@glsl-1.10@execution@built-in-functions@fs-refract-vec3-vec3-float,Fail
spec@glsl-1.10@execution@built-in-functions@fs-refract-vec4-vec4-float,Fail
spec@glsl-1.10@execution@built-in-functions@fs-sin-float,Fail
spec@glsl-1.10@execution@built-in-functions@fs-sin-vec2,Fail
spec@glsl-1.10@execution@built-in-functions@fs-sin-vec3,Fail
spec@glsl-1.10@execution@built-in-functions@fs-sin-vec4,Fail
spec@glsl-1.10@execution@built-in-functions@fs-smoothstep-float-float-float,Fail
spec@glsl-1.10@execution@built-in-functions@fs-smoothstep-float-float-vec2,Fail
spec@glsl-1.10@execution@built-in-functions@fs-smoothstep-float-float-vec3,Fail
@@ -639,12 +629,7 @@ spec@glsl-1.10@execution@built-in-functions@fs-smoothstep-float-float-vec4,Fail
spec@glsl-1.10@execution@built-in-functions@fs-smoothstep-vec2-vec2-vec2,Fail
spec@glsl-1.10@execution@built-in-functions@fs-smoothstep-vec3-vec3-vec3,Fail
spec@glsl-1.10@execution@built-in-functions@fs-smoothstep-vec4-vec4-vec4,Fail
spec@glsl-1.10@execution@built-in-functions@fs-tan-float,Fail
spec@glsl-1.10@execution@built-in-functions@fs-tan-vec2,Fail
spec@glsl-1.10@execution@built-in-functions@fs-tan-vec3,Fail
spec@glsl-1.10@execution@built-in-functions@fs-tan-vec4,Fail
spec@glsl-1.10@execution@builtins@glsl-fs-atan-3,Fail
spec@glsl-1.10@execution@builtins@glsl-fs-tan-1,Fail
spec@glsl-1.10@execution@copy-propagation@glsl-copy-propagation-loop-1,Fail
spec@glsl-1.10@execution@copy-propagation@glsl-copy-propagation-loop-2,Fail
spec@glsl-1.10@execution@derivatives@glsl-derivs-abs,Fail

View File

@@ -27,7 +27,7 @@ traces:
# The cel shading is pretty chunky, but maybe that's just precision stuff.
expectations:
- device: i915-g33
checksum: 86094ef6a40bfe48d4b7d66871af97a5
checksum: 2258848deb4052a3c19145fe524e2805
# Requires GLSL 1.30
#- path: humus/DynamicBranching3.trace
# expectations:
@@ -41,7 +41,7 @@ traces:
- path: humus/Portals.trace
expectations:
- device: i915-g33
checksum: 46a5f3c04ef40f275e1d330c2ac965c8
checksum: 9306a8d9f37d83a327759319c46f7cae
# The shadow raytracing shader fails to compile
#- path: humus/RaytracedShadows.trace
# expectations:
@@ -55,9 +55,9 @@ traces:
- path: neverball/neverball.trace
expectations:
- device: i915-g33
checksum: e621fd97320f24ea4451565390f9b1ac
checksum: 344479e929ad4bc7f7316b5e574b0131
- path: valve/counterstrike-v2.trace
# Tree foliage rendering is extra aliased.
expectations:
- device: i915-g33
checksum: 4a693fcd3b2fbe388ba847ca3a93f079
checksum: 66a18e91b95674a3a1d227ed5681257d

View File

@@ -35,6 +35,8 @@
#include "tgsi/tgsi_parse.h"
struct nir_shader;
#define I915_PROGRAM_SIZE 192
/* Use those indices for pos/face routing, must be >= num of inputs */
@@ -289,4 +291,6 @@ extern void i915_optimize_free(struct i915_token_list *tokens);
extern uint32_t i915_num_coords(uint32_t tex);
extern bool i915_nir_lower_sincos(struct nir_shader *s);
#endif

View File

@@ -65,19 +65,6 @@ static unsigned passthrough_program[] = {
(SRC_ONE << A1_SRC0_CHANNEL_W_SHIFT)),
0};
/* 2*pi, -(2*pi)^3/3!, (2*pi)^5/5!, -(2*pi)^7/7! */
static const float sin_constants[4] = {
2.0 * M_PI, -8.0f * M_PI *M_PI *M_PI / (3 * 2 * 1),
32.0f * M_PI *M_PI *M_PI *M_PI *M_PI / (5 * 4 * 3 * 2 * 1),
-128.0f * M_PI *M_PI *M_PI *M_PI *M_PI *M_PI *M_PI /
(7 * 6 * 5 * 4 * 3 * 2 * 1)};
/* 1, -(2*pi)^2/2!, (2*pi)^4/4!, -(2*pi)^6/6! */
static const float cos_constants[4] = {
1.0, -4.0f * M_PI *M_PI / (2 * 1),
16.0f * M_PI *M_PI *M_PI *M_PI / (4 * 3 * 2 * 1),
-64.0f * M_PI *M_PI *M_PI *M_PI *M_PI *M_PI / (6 * 5 * 4 * 3 * 2 * 1)};
/**
* component-wise negation of ureg
*/
@@ -461,38 +448,6 @@ i915_translate_instruction(struct i915_fp_compile *p,
src1); /* NOTE: order of src2, src1 */
break;
case TGSI_OPCODE_COS:
src0 = src_vector(p, &inst->Src[0], fs);
tmp = i915_get_utemp(p);
i915_emit_arith(p, A0_MUL, tmp, A0_DEST_CHANNEL_X, 0, src0,
i915_emit_const1f(p, 1.0f / (float)(M_PI * 2.0)), 0);
i915_emit_arith(p, A0_MOD, tmp, A0_DEST_CHANNEL_X, 0, tmp, 0, 0);
/*
* t0.xy = MUL x.xx11, x.x111 ; x^2, x, 1, 1
* t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, 1
* t0 = MUL t0.xxz1 t0.z111 ; x^6 x^4 x^2 1
* result = DP4 t0, cos_constants
*/
i915_emit_arith(p, A0_MUL, tmp, A0_DEST_CHANNEL_XY, 0,
swizzle(tmp, X, X, ONE, ONE),
swizzle(tmp, X, ONE, ONE, ONE), 0);
i915_emit_arith(p, A0_MUL, tmp, A0_DEST_CHANNEL_XYZ, 0,
swizzle(tmp, X, Y, X, ONE), swizzle(tmp, X, X, ONE, ONE),
0);
i915_emit_arith(p, A0_MUL, tmp, A0_DEST_CHANNEL_XYZ, 0,
swizzle(tmp, X, X, Z, ONE),
swizzle(tmp, Z, ONE, ONE, ONE), 0);
i915_emit_arith(p, A0_DP4, get_result_vector(p, &inst->Dst[0]),
get_result_flags(inst), 0, swizzle(tmp, ONE, Z, Y, X),
i915_emit_const4fv(p, cos_constants), 0);
break;
case TGSI_OPCODE_DDX:
case TGSI_OPCODE_DDY:
/* XXX We just output 0 here */
@@ -721,38 +676,6 @@ i915_translate_instruction(struct i915_fp_compile *p,
emit_simple_arith(p, inst, A0_SGE, 2, fs);
break;
case TGSI_OPCODE_SIN:
src0 = src_vector(p, &inst->Src[0], fs);
tmp = i915_get_utemp(p);
i915_emit_arith(p, A0_MUL, tmp, A0_DEST_CHANNEL_X, 0, src0,
i915_emit_const1f(p, 1.0f / (float)(M_PI * 2.0)), 0);
i915_emit_arith(p, A0_MOD, tmp, A0_DEST_CHANNEL_X, 0, tmp, 0, 0);
/*
* t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1
* t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, x
* t1 = MUL t0.xyyw t0.yz11 ; x^7 x^5 x^3 x
* result = DP4 t1.wzyx, sin_constants
*/
i915_emit_arith(p, A0_MUL, tmp, A0_DEST_CHANNEL_XY, 0,
swizzle(tmp, X, X, ONE, ONE),
swizzle(tmp, X, ONE, ONE, ONE), 0);
i915_emit_arith(p, A0_MUL, tmp, A0_DEST_CHANNEL_ALL, 0,
swizzle(tmp, X, Y, X, Y), swizzle(tmp, X, X, ONE, ONE),
0);
i915_emit_arith(p, A0_MUL, tmp, A0_DEST_CHANNEL_ALL, 0,
swizzle(tmp, X, Y, Y, W), swizzle(tmp, X, Z, ONE, ONE),
0);
i915_emit_arith(p, A0_DP4, get_result_vector(p, &inst->Dst[0]),
get_result_flags(inst), 0, swizzle(tmp, W, Z, Y, X),
i915_emit_const4fv(p, sin_constants), 0);
break;
case TGSI_OPCODE_SLE:
/* like SGE, but swap reg0, reg1 */
emit_simple_arith_swap2(p, inst, A0_SGE, 2, fs);

View File

@@ -0,0 +1,81 @@
/*
* Copyright © 2021 Emma Anholt
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "compiler/nir/nir.h"
#include "compiler/nir/nir_builder.h"
#include "i915_fpc.h"
static bool
i915_sincos_filter(const nir_instr *instr, const void *data)
{
if (instr->type != nir_instr_type_alu)
return false;
switch (nir_instr_as_alu(instr)->op) {
case nir_op_fcos:
case nir_op_fsin:
return true;
default:
return false;
}
}
/* Compute sin using a quadratic and quartic. It gives continuity
* that repeating the Taylor series lacks every 2*pi, and has
* reduced error.
*
* The idea was described at:
* https://web.archive.org/web/20100613230051/http://www.devmaster.net/forums/showthread.php?t=5784
*/
static nir_ssa_def *
i915_sincos_lower(nir_builder *b, nir_instr *instr, void *data)
{
nir_alu_instr *alu = nir_instr_as_alu(instr);
nir_ssa_def *x = nir_ssa_for_alu_src(b, alu, 0);
/* Reduce range from repeating about [-pi,pi] to [-1,1] */
x = nir_fmul_imm(b, x, M_1_PI / 2.0);
if (alu->op == nir_op_fsin)
x = nir_fadd_imm(b, x, 0.5);
else
x = nir_fadd_imm(b, x, 0.75);
x = nir_ffract(b, x);
x = nir_fadd_imm(b, nir_fmul_imm(b, x, 2.0), -1.0);
nir_ssa_def *x_absx = nir_fmul(b, x, nir_fabs(b, x));
/* y is the first approximation of the result. */
nir_ssa_def *y =
nir_fadd(b, nir_fmul_imm(b, x, 4.0), nir_fmul_imm(b, x_absx, -4.0));
/* improve the accuracy. */
nir_ssa_def *y_absy = nir_fmul(b, y, nir_fabs(b, y));
return nir_fadd(b, nir_fmul_imm(b, nir_fsub(b, y_absy, y), 0.225), y);
}
bool
i915_nir_lower_sincos(nir_shader *s)
{
return nir_shader_lower_instructions(s, i915_sincos_filter,
i915_sincos_lower, NULL);
}

View File

@@ -546,7 +546,11 @@ i915_create_fs_state(struct pipe_context *pipe,
ifs->draw_data = draw_create_fragment_shader(i915->draw, templ);
if (templ->type == PIPE_SHADER_IR_NIR) {
ifs->state.tokens = nir_to_tgsi(templ->ir.nir, pipe->screen);
nir_shader *s = templ->ir.nir;
NIR_PASS_V(s, i915_nir_lower_sincos);
ifs->state.tokens = nir_to_tgsi(s, pipe->screen);
} else {
assert(templ->type == PIPE_SHADER_IR_TGSI);
/* we need to keep a local copy of the tokens */

View File

@@ -35,6 +35,7 @@ files_i915 = files(
'i915_fpc.h',
'i915_fpc_optimize.c',
'i915_fpc_translate.c',
'i915_nir.c',
'i915_prim_emit.c',
'i915_prim_vbuf.c',
'i915_public.h',