Mesa (main): i915g: Use the devmaster quadratic approximation for sin/cos.

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Mon Aug 9 04:51:47 UTC 2021


Module: Mesa
Branch: main
Commit: 01e9824997dcd6e361e5208826d8a997f41c8b7b
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=01e9824997dcd6e361e5208826d8a997f41c8b7b

Author: Emma Anholt <emma at anholt.net>
Date:   Thu Aug  5 15:41:04 2021 -0700

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>

---

 .../drivers/i915/ci/deqp-i915-g33-fails.txt        | 42 -----------
 .../drivers/i915/ci/piglit-i915-g33-fails.txt      | 15 ----
 src/gallium/drivers/i915/ci/traces-i915.yml        |  8 +--
 src/gallium/drivers/i915/i915_fpc.h                |  4 ++
 src/gallium/drivers/i915/i915_fpc_translate.c      | 77 --------------------
 src/gallium/drivers/i915/i915_nir.c                | 81 ++++++++++++++++++++++
 src/gallium/drivers/i915/i915_state.c              |  6 +-
 src/gallium/drivers/i915/meson.build               |  1 +
 8 files changed, 95 insertions(+), 139 deletions(-)

diff --git a/src/gallium/drivers/i915/ci/deqp-i915-g33-fails.txt b/src/gallium/drivers/i915/ci/deqp-i915-g33-fails.txt
index 5c598cefcc9..d1c8167897b 100644
--- a/src/gallium/drivers/i915/ci/deqp-i915-g33-fails.txt
+++ b/src/gallium/drivers/i915/ci/deqp-i915-g33-fails.txt
@@ -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
diff --git a/src/gallium/drivers/i915/ci/piglit-i915-g33-fails.txt b/src/gallium/drivers/i915/ci/piglit-i915-g33-fails.txt
index bcd914389df..d78dd68938f 100644
--- a/src/gallium/drivers/i915/ci/piglit-i915-g33-fails.txt
+++ b/src/gallium/drivers/i915/ci/piglit-i915-g33-fails.txt
@@ -1,5 +1,4 @@
 shaders at glsl-bug-110796,Fail
-shaders at glsl-cos,Fail
 shaders at glsl-derivs,Fail
 shaders at glsl-fs-loop,Fail
 shaders at glsl-fs-loop-nested,Fail
@@ -7,7 +6,6 @@ shaders at glsl-fs-raytrace-bug27060,Fail
 shaders at glsl-fwidth,Fail
 shaders at glsl-max-varyings,Fail
 shaders at glsl-max-varyings >max_varying_components,Fail
-shaders at glsl-sin,Fail
 shaders at glsl-uniform-interstage-limits@520 vs- 1 fs,Fail
 shaders at glsl-uniform-interstage-limits@subdivide 5,Fail
 shaders at glsl-uniform-interstage-limits@subdivide 5- statechanges,Fail
@@ -463,10 +461,6 @@ spec at ext_texture_srgb@texwrap formats-s3tc bordercolor at GL_COMPRESSED_SRGB_ALPHA-
 spec at ext_texture_srgb@texwrap formats-s3tc bordercolor at GL_COMPRESSED_SRGB_S3TC_DXT1_EXT- border color only,Fail
 spec at glsl-1.10@execution at built-in-functions@fs-atan-vec3-vec3,Fail
 spec at glsl-1.10@execution at built-in-functions@fs-atan-vec4-vec4,Fail
-spec at glsl-1.10@execution at built-in-functions@fs-cos-float,Fail
-spec at glsl-1.10@execution at built-in-functions@fs-cos-vec2,Fail
-spec at glsl-1.10@execution at built-in-functions@fs-cos-vec3,Fail
-spec at glsl-1.10@execution at built-in-functions@fs-cos-vec4,Fail
 spec at glsl-1.10@execution at built-in-functions@fs-degrees-float,Fail
 spec at glsl-1.10@execution at built-in-functions@fs-degrees-vec2,Fail
 spec at glsl-1.10@execution at built-in-functions@fs-degrees-vec3,Fail
@@ -628,10 +622,6 @@ spec at glsl-1.10@execution at built-in-functions@fs-reflect-vec4-vec4,Fail
 spec at glsl-1.10@execution at built-in-functions@fs-refract-vec2-vec2-float,Fail
 spec at glsl-1.10@execution at built-in-functions@fs-refract-vec3-vec3-float,Fail
 spec at glsl-1.10@execution at built-in-functions@fs-refract-vec4-vec4-float,Fail
-spec at glsl-1.10@execution at built-in-functions@fs-sin-float,Fail
-spec at glsl-1.10@execution at built-in-functions@fs-sin-vec2,Fail
-spec at glsl-1.10@execution at built-in-functions@fs-sin-vec3,Fail
-spec at glsl-1.10@execution at built-in-functions@fs-sin-vec4,Fail
 spec at glsl-1.10@execution at built-in-functions@fs-smoothstep-float-float-float,Fail
 spec at glsl-1.10@execution at built-in-functions@fs-smoothstep-float-float-vec2,Fail
 spec at glsl-1.10@execution at built-in-functions@fs-smoothstep-float-float-vec3,Fail
@@ -639,12 +629,7 @@ spec at glsl-1.10@execution at built-in-functions@fs-smoothstep-float-float-vec4,Fail
 spec at glsl-1.10@execution at built-in-functions@fs-smoothstep-vec2-vec2-vec2,Fail
 spec at glsl-1.10@execution at built-in-functions@fs-smoothstep-vec3-vec3-vec3,Fail
 spec at glsl-1.10@execution at built-in-functions@fs-smoothstep-vec4-vec4-vec4,Fail
-spec at glsl-1.10@execution at built-in-functions@fs-tan-float,Fail
-spec at glsl-1.10@execution at built-in-functions@fs-tan-vec2,Fail
-spec at glsl-1.10@execution at built-in-functions@fs-tan-vec3,Fail
-spec at glsl-1.10@execution at built-in-functions@fs-tan-vec4,Fail
 spec at glsl-1.10@execution at builtins@glsl-fs-atan-3,Fail
-spec at glsl-1.10@execution at builtins@glsl-fs-tan-1,Fail
 spec at glsl-1.10@execution at copy-propagation@glsl-copy-propagation-loop-1,Fail
 spec at glsl-1.10@execution at copy-propagation@glsl-copy-propagation-loop-2,Fail
 spec at glsl-1.10@execution at derivatives@glsl-derivs-abs,Fail
diff --git a/src/gallium/drivers/i915/ci/traces-i915.yml b/src/gallium/drivers/i915/ci/traces-i915.yml
index 741df0b16b5..f35d482f752 100644
--- a/src/gallium/drivers/i915/ci/traces-i915.yml
+++ b/src/gallium/drivers/i915/ci/traces-i915.yml
@@ -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
diff --git a/src/gallium/drivers/i915/i915_fpc.h b/src/gallium/drivers/i915/i915_fpc.h
index 6ff372d503b..4c8c77e299f 100644
--- a/src/gallium/drivers/i915/i915_fpc.h
+++ b/src/gallium/drivers/i915/i915_fpc.h
@@ -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
diff --git a/src/gallium/drivers/i915/i915_fpc_translate.c b/src/gallium/drivers/i915/i915_fpc_translate.c
index 0da91a17ee7..6ca95ffbbf9 100644
--- a/src/gallium/drivers/i915/i915_fpc_translate.c
+++ b/src/gallium/drivers/i915/i915_fpc_translate.c
@@ -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);
diff --git a/src/gallium/drivers/i915/i915_nir.c b/src/gallium/drivers/i915/i915_nir.c
new file mode 100644
index 00000000000..fc43139fcec
--- /dev/null
+++ b/src/gallium/drivers/i915/i915_nir.c
@@ -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);
+}
diff --git a/src/gallium/drivers/i915/i915_state.c b/src/gallium/drivers/i915/i915_state.c
index 2eb69690731..947a67c149c 100644
--- a/src/gallium/drivers/i915/i915_state.c
+++ b/src/gallium/drivers/i915/i915_state.c
@@ -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 */
diff --git a/src/gallium/drivers/i915/meson.build b/src/gallium/drivers/i915/meson.build
index cb17f845c4b..72434cd4e5f 100644
--- a/src/gallium/drivers/i915/meson.build
+++ b/src/gallium/drivers/i915/meson.build
@@ -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',



More information about the mesa-commit mailing list