nir: add rcp(w) lowering for gl_FragCoord
authorAndreas Baierl <ichgeh@imkreisrum.de>
Fri, 26 Apr 2019 13:01:43 +0000 (15:01 +0200)
committerQiang Yu <yuq825@gmail.com>
Mon, 29 Apr 2019 02:46:44 +0000 (02:46 +0000)
On some hardware (e.g. Mali400) the shader needs to apply some
transformations for correct gl_FragCoord handling. The lowering
actions look like the following in pseudocode:
   gl_FragCoord.xyz = gl_FragCoord_orig.xyz
   gl_FragCoord.w = 1.0 / gl_FragCoord_orig.w

Add this lowering as a nir pass in preparation for using it in the driver.

Signed-off-by: Andreas Baierl <ichgeh@imkreisrum.de>
Reviewed-by: Qiang Yu <yuq825@gmail.com>
Reviewed-by: Eric Anholt <eric@anholt.net>
src/compiler/Makefile.sources
src/compiler/nir/meson.build
src/compiler/nir/nir.h
src/compiler/nir/nir_lower_fragcoord_wtrans.c [new file with mode: 0644]

index 35dd7ff1abe07774ed06659ccc292cb227f2407a..a8e5f6280db6dbab883567d795f2c061150d472a 100644 (file)
@@ -243,6 +243,7 @@ NIR_FILES = \
        nir/nir_lower_constant_initializers.c \
        nir/nir_lower_double_ops.c \
        nir/nir_lower_drawpixels.c \
+       nir/nir_lower_fragcoord_wtrans.c \
        nir/nir_lower_frexp.c \
        nir/nir_lower_global_vars_to_local.c \
        nir/nir_lower_gs_intrinsics.c \
index d6e7b2cc1674335a62ef9f7256ec930bc9549e97..625f1508850f7478150fe054e05cbccce8846b6e 100644 (file)
@@ -123,6 +123,7 @@ files_libnir = files(
   'nir_lower_constant_initializers.c',
   'nir_lower_double_ops.c',
   'nir_lower_drawpixels.c',
+  'nir_lower_fragcoord_wtrans.c',
   'nir_lower_frexp.c',
   'nir_lower_global_vars_to_local.c',
   'nir_lower_gs_intrinsics.c',
index e878a63409d5ed4e4fae164c819bbdcdfdbae014..0ba90caad38eec56aa27e83ad75a19bbcd848d13 100644 (file)
@@ -3159,6 +3159,7 @@ void nir_lower_io_to_scalar(nir_shader *shader, nir_variable_mode mask);
 void nir_lower_io_to_scalar_early(nir_shader *shader, nir_variable_mode mask);
 bool nir_lower_io_to_vector(nir_shader *shader, nir_variable_mode mask);
 
+void nir_lower_fragcoord_wtrans(nir_shader *shader);
 void nir_lower_viewport_transform(nir_shader *shader);
 bool nir_lower_uniforms_to_ubo(nir_shader *shader, int multiplier);
 
diff --git a/src/compiler/nir/nir_lower_fragcoord_wtrans.c b/src/compiler/nir/nir_lower_fragcoord_wtrans.c
new file mode 100644 (file)
index 0000000..b8c0b0b
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2019 Andreas Baierl
+ *
+ * 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 "nir.h"
+#include "nir_builder.h"
+
+/* Lower gl_FragCoord and transform the w component
+ * according to the following pseudocode:
+ *
+ *    gl_FragCoord.xyz = gl_FragCoord_orig.xyz
+ *    gl_FragCoord.w = 1.0 / gl_FragCoord_orig.w
+ *
+ * To trigger the transformation, gl_FragCoord currently has to be treated
+ * as a system value with PIPE_CAP_TGSI_FS_POSITION_IS_SYSVAL enabled.
+ */
+
+static void
+lower_fragcoord_wtrans(nir_builder *b, nir_intrinsic_instr *intr)
+{
+   assert(intr->dest.is_ssa);
+
+   b->cursor = nir_before_instr(&intr->instr);
+
+   nir_ssa_def *fragcoord_in = nir_load_frag_coord(b);
+   nir_ssa_def *w_rcp = nir_frcp(b, nir_channel(b, fragcoord_in, 3));
+   nir_ssa_def *fragcoord_wtrans = nir_vec4(b,
+                                            nir_channel(b, fragcoord_in, 0),
+                                            nir_channel(b, fragcoord_in, 1),
+                                            nir_channel(b, fragcoord_in, 2),
+                                            w_rcp);
+   nir_ssa_def_rewrite_uses(&intr->dest.ssa,
+                            nir_src_for_ssa(fragcoord_wtrans));
+}
+
+void
+nir_lower_fragcoord_wtrans(nir_shader *shader)
+{
+   assert(shader->info.stage == MESA_SHADER_FRAGMENT);
+
+   nir_foreach_function(func, shader) {
+      if (!func->impl)
+         continue;
+
+      nir_builder b;
+      nir_builder_init(&b, func->impl);
+      nir_foreach_block(block, func->impl) {
+         nir_foreach_instr_safe(instr, block) {
+            if (instr->type != nir_instr_type_intrinsic)
+               continue;
+
+            nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
+            if (intr->intrinsic != nir_intrinsic_load_frag_coord)
+               continue;
+
+            lower_fragcoord_wtrans(&b, intr);
+         }
+      }
+      nir_metadata_preserve(func->impl, nir_metadata_block_index |
+                            nir_metadata_dominance);
+   }
+}