compiler/nir: rewrite lower_fragcoord_wtrans to use nir_lower_instructions
authorGert Wollny <gert.wollny@collabora.com>
Mon, 6 Jul 2020 07:38:01 +0000 (09:38 +0200)
committerMarge Bot <eric+marge@anholt.net>
Mon, 17 Aug 2020 12:38:05 +0000 (12:38 +0000)
This compacts the code and makes it easier to extend.

Signed-off-by: Gert Wollny <gert.wollny@collabora.com>
Reviewed-by: Andreas Baierl <ichgeh@imkreisrum.de>
Reviewed-by: Eric Anholt <eric@anholt.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6220>

src/compiler/nir/nir.h
src/compiler/nir/nir_lower_fragcoord_wtrans.c

index d538954ffea1c3fe1195ae10d0c791e58f1608a5..4f3df940c998f8149c1620bb1a36c7dc36a4bcf4 100644 (file)
@@ -4222,7 +4222,7 @@ 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);
 
 bool nir_lower_fragcolor(nir_shader *shader);
-void nir_lower_fragcoord_wtrans(nir_shader *shader);
+bool 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);
 
index b8c0b0b24e5b9bf2141bafae049e2164274376ef..f469b95539635188fc0621f472fd5306821ca656 100644 (file)
  * 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)
+static bool
+lower_fragcoord_wtrans_filter(const nir_instr *instr, UNUSED const void *_options)
 {
-   assert(intr->dest.is_ssa);
+   if (instr->type != nir_instr_type_intrinsic)
+      return false;
 
-   b->cursor = nir_before_instr(&intr->instr);
+   nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
+   return intr->intrinsic == nir_intrinsic_load_frag_coord;
+}
+
+static nir_ssa_def *
+lower_fragcoord_wtrans_impl(nir_builder *b, nir_instr *instr,
+                            UNUSED void *_options)
+{
+   nir_intrinsic_instr *intr = nir_instr_as_intrinsic(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));
+   return nir_vec4(b,
+                   nir_channel(b, &intr->dest.ssa, 0),
+                   nir_channel(b, &intr->dest.ssa, 1),
+                   nir_channel(b, &intr->dest.ssa, 2),
+                   nir_frcp(b, nir_channel(b, &intr->dest.ssa, 3)));
 }
 
-void
+bool
 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;
+   return nir_shader_lower_instructions(shader,
+                                        lower_fragcoord_wtrans_filter,
+                                        lower_fragcoord_wtrans_impl,
+                                        NULL);
 
-            lower_fragcoord_wtrans(&b, intr);
-         }
-      }
-      nir_metadata_preserve(func->impl, nir_metadata_block_index |
-                            nir_metadata_dominance);
-   }
 }