freedreno: Convert load_barycentric_at_sample to the NIR lowering helper.
authorEric Anholt <eric@anholt.net>
Tue, 16 Jul 2019 18:15:15 +0000 (11:15 -0700)
committerEric Anholt <eric@anholt.net>
Thu, 18 Jul 2019 18:28:56 +0000 (11:28 -0700)
Cuts out a ton of boilerplate.

Reviewed-by: Kristian H. Kristensen <hoegsberg@google.com>
src/freedreno/ir3/ir3_nir_lower_load_barycentric_at_sample.c

index e27f4822e2c2c376a23b520c28ad74cbb45ce234..ef8d7eed025eedd258aa2c45d300b77ba0fe729c 100644 (file)
@@ -46,7 +46,7 @@ load_sample_pos(nir_builder *b, nir_ssa_def *samp_id)
        return &load_sp->dest.ssa;
 }
 
-static void
+static nir_ssa_def *
 lower_load_barycentric_at_sample(nir_builder *b, nir_intrinsic_instr *intr)
 {
        nir_ssa_def *pos = load_sample_pos(b, intr->src[0].ssa);
@@ -60,11 +60,10 @@ lower_load_barycentric_at_sample(nir_builder *b, nir_intrinsic_instr *intr)
                        &load_bary_at_offset->dest, 2, 32, NULL);
        nir_builder_instr_insert(b, &load_bary_at_offset->instr);
 
-       nir_ssa_def_rewrite_uses(&intr->dest.ssa,
-                       nir_src_for_ssa(&load_bary_at_offset->dest.ssa));
+       return &load_bary_at_offset->dest.ssa;
 }
 
-static void
+static nir_ssa_def *
 lower_load_sample_pos(nir_builder *b, nir_intrinsic_instr *intr)
 {
        nir_ssa_def *pos = load_sample_pos(b, nir_load_sample_id(b));
@@ -75,56 +74,39 @@ lower_load_sample_pos(nir_builder *b, nir_intrinsic_instr *intr)
         * for example.
         */
        nir_ssa_def *half = nir_imm_float(b, 0.5);
-       pos = nir_fadd(b, pos, nir_vec2(b, half, half));
+       return nir_fadd(b, pos, nir_vec2(b, half, half));
+}
+
+static nir_ssa_def *
+ir3_nir_lower_load_barycentric_at_sample_instr(nir_builder *b,
+               nir_instr *instr, void *data)
+{
+       nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
 
-       nir_ssa_def_rewrite_uses(&intr->dest.ssa, nir_src_for_ssa(pos));
+       if (intr->intrinsic == nir_intrinsic_load_sample_pos)
+               return lower_load_sample_pos(b, intr);
+       else
+               return lower_load_barycentric_at_sample(b, intr);
+}
+
+static bool
+ir3_nir_lower_load_barycentric_at_sample_filter(const nir_instr *instr,
+               const void *data)
+{
+       if (instr->type != nir_instr_type_intrinsic)
+               return false;
+       nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
+       return (intr->intrinsic == nir_intrinsic_load_barycentric_at_sample ||
+                       intr->intrinsic == nir_intrinsic_load_sample_pos);
 }
 
 bool
 ir3_nir_lower_load_barycentric_at_sample(nir_shader *shader)
 {
-       bool progress = false;
-
        debug_assert(shader->info.stage == MESA_SHADER_FRAGMENT);
 
-       nir_foreach_function (function, shader) {
-               if (!function->impl)
-                       continue;
-
-               nir_builder b;
-               nir_builder_init(&b, function->impl);
-
-               nir_foreach_block (block, function->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_barycentric_at_sample &&
-                                               intr->intrinsic != nir_intrinsic_load_sample_pos)
-                                       continue;
-
-                               debug_assert(intr->dest.is_ssa);
-
-                               b.cursor = nir_before_instr(instr);
-
-                               if (intr->intrinsic == nir_intrinsic_load_sample_pos) {
-                                       lower_load_sample_pos(&b, intr);
-                               } else {
-                                       debug_assert(intr->src[0].is_ssa);
-                                       lower_load_barycentric_at_sample(&b, intr);
-                               }
-
-                               progress = true;
-                       }
-               }
-
-               if (progress) {
-                       nir_metadata_preserve(function->impl,
-                               nir_metadata_block_index | nir_metadata_dominance);
-               }
-       }
-
-       return progress;
+       return nir_shader_lower_instructions(shader,
+                       ir3_nir_lower_load_barycentric_at_sample_filter,
+                       ir3_nir_lower_load_barycentric_at_sample_instr,
+                       NULL);
 }