Mesa (master): compiler/nir: rewrite lower_fragcoord_wtrans to use nir_lower_instructions

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Mon Aug 17 12:59:41 UTC 2020


Module: Mesa
Branch: master
Commit: 7aaddf1a342944743f65ab0c55df46f2c7d3c17f
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=7aaddf1a342944743f65ab0c55df46f2c7d3c17f

Author: Gert Wollny <gert.wollny at collabora.com>
Date:   Mon Jul  6 09:38:01 2020 +0200

compiler/nir: rewrite lower_fragcoord_wtrans to use nir_lower_instructions

This compacts the code and makes it easier to extend.

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

---

 src/compiler/nir/nir.h                        |  2 +-
 src/compiler/nir/nir_lower_fragcoord_wtrans.c | 57 +++++++++++----------------
 2 files changed, 24 insertions(+), 35 deletions(-)

diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
index d538954ffea..4f3df940c99 100644
--- a/src/compiler/nir/nir.h
+++ b/src/compiler/nir/nir.h
@@ -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);
 
diff --git a/src/compiler/nir/nir_lower_fragcoord_wtrans.c b/src/compiler/nir/nir_lower_fragcoord_wtrans.c
index b8c0b0b24e5..f469b955396 100644
--- a/src/compiler/nir/nir_lower_fragcoord_wtrans.c
+++ b/src/compiler/nir/nir_lower_fragcoord_wtrans.c
@@ -34,48 +34,37 @@
  * 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);
-   }
 }



More information about the mesa-commit mailing list