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

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

index 37a3dcb26f809d0ad496baeb4a1324836e2efcc7..6650a285b4776f1b32aa2cbe315d51620d28b02a 100644 (file)
  * direct texture calls.
  */
 
-static bool
-lower_tg4(nir_block *block, nir_builder *b, void *mem_ctx)
+static nir_ssa_def *
+ir3_nir_lower_tg4_to_tex_instr(nir_builder *b, nir_instr *instr, void *data)
 {
-       bool progress = false;
-
+       nir_tex_instr *tg4 = nir_instr_as_tex(instr);
        static const int offsets[3][2] = { {0, 1}, {1, 1}, {1, 0} };
 
-       nir_foreach_instr_safe(instr, block) {
-               if (instr->type != nir_instr_type_tex)
-                       continue;
-
-               nir_tex_instr *tg4 = (nir_tex_instr *)instr;
-
-               if (tg4->op != nir_texop_tg4)
-                       continue;
-
-               b->cursor = nir_before_instr(&tg4->instr);
-
-               nir_ssa_def *results[4];
-               int offset_index = nir_tex_instr_src_index(tg4, nir_tex_src_offset);
-               for (int i = 0; i < 4; i++) {
-                       int num_srcs = tg4->num_srcs + 1 /* lod */;
-                       if (offset_index < 0 && i < 3)
-                               num_srcs++;
-
-                       nir_tex_instr *tex = nir_tex_instr_create(b->shader, num_srcs);
-                       tex->op = nir_texop_txl;
-                       tex->sampler_dim = tg4->sampler_dim;
-                       tex->coord_components = tg4->coord_components;
-                       tex->is_array = tg4->is_array;
-                       tex->is_shadow = tg4->is_shadow;
-                       tex->is_new_style_shadow = tg4->is_new_style_shadow;
-                       tex->texture_index = tg4->texture_index;
-                       tex->sampler_index = tg4->sampler_index;
-                       tex->dest_type = tg4->dest_type;
-
-                       for (int j = 0; j < tg4->num_srcs; j++) {
-                               nir_src_copy(&tex->src[j].src, &tg4->src[j].src, tex);
-                               tex->src[j].src_type = tg4->src[j].src_type;
-                       }
-                       if (i != 3) {
-                               nir_ssa_def *offset =
-                                       nir_vec2(b, nir_imm_int(b, offsets[i][0]),
-                                                        nir_imm_int(b, offsets[i][1]));
-                               if (offset_index < 0) {
-                                       tex->src[tg4->num_srcs].src = nir_src_for_ssa(offset);
-                                       tex->src[tg4->num_srcs].src_type = nir_tex_src_offset;
-                               } else {
-                                       assert(nir_tex_instr_src_size(tex, offset_index) == 2);
-                                       nir_ssa_def *orig = nir_ssa_for_src(
-                                                       b, tex->src[offset_index].src, 2);
-                                       tex->src[offset_index].src =
-                                               nir_src_for_ssa(nir_iadd(b, orig, offset));
-                               }
+       nir_ssa_def *results[4];
+       int offset_index = nir_tex_instr_src_index(tg4, nir_tex_src_offset);
+       for (int i = 0; i < 4; i++) {
+               int num_srcs = tg4->num_srcs + 1 /* lod */;
+               if (offset_index < 0 && i < 3)
+                       num_srcs++;
+
+               nir_tex_instr *tex = nir_tex_instr_create(b->shader, num_srcs);
+               tex->op = nir_texop_txl;
+               tex->sampler_dim = tg4->sampler_dim;
+               tex->coord_components = tg4->coord_components;
+               tex->is_array = tg4->is_array;
+               tex->is_shadow = tg4->is_shadow;
+               tex->is_new_style_shadow = tg4->is_new_style_shadow;
+               tex->texture_index = tg4->texture_index;
+               tex->sampler_index = tg4->sampler_index;
+               tex->dest_type = tg4->dest_type;
+
+               for (int j = 0; j < tg4->num_srcs; j++) {
+                       nir_src_copy(&tex->src[j].src, &tg4->src[j].src, tex);
+                       tex->src[j].src_type = tg4->src[j].src_type;
+               }
+               if (i != 3) {
+                       nir_ssa_def *offset =
+                               nir_vec2(b, nir_imm_int(b, offsets[i][0]),
+                                               nir_imm_int(b, offsets[i][1]));
+                       if (offset_index < 0) {
+                               tex->src[tg4->num_srcs].src = nir_src_for_ssa(offset);
+                               tex->src[tg4->num_srcs].src_type = nir_tex_src_offset;
+                       } else {
+                               assert(nir_tex_instr_src_size(tex, offset_index) == 2);
+                               nir_ssa_def *orig = nir_ssa_for_src(
+                                       b, tex->src[offset_index].src, 2);
+                               tex->src[offset_index].src =
+                                       nir_src_for_ssa(nir_iadd(b, orig, offset));
                        }
-                       tex->src[num_srcs - 1].src = nir_src_for_ssa(nir_imm_float(b, 0));
-                       tex->src[num_srcs - 1].src_type = nir_tex_src_lod;
-
-                       nir_ssa_dest_init(&tex->instr, &tex->dest,
-                                                         nir_tex_instr_dest_size(tex), 32, NULL);
-                       nir_builder_instr_insert(b, &tex->instr);
-
-                       results[i] = nir_channel(b, &tex->dest.ssa, tg4->component);
                }
+               tex->src[num_srcs - 1].src = nir_src_for_ssa(nir_imm_float(b, 0));
+               tex->src[num_srcs - 1].src_type = nir_tex_src_lod;
 
-               nir_ssa_def *result = nir_vec4(b, results[0], results[1], results[2], results[3]);
-               nir_ssa_def_rewrite_uses(&tg4->dest.ssa, nir_src_for_ssa(result));
-
-               nir_instr_remove(&tg4->instr);
+               nir_ssa_dest_init(&tex->instr, &tex->dest,
+                               nir_tex_instr_dest_size(tex), 32, NULL);
+               nir_builder_instr_insert(b, &tex->instr);
 
-               progress = true;
+               results[i] = nir_channel(b, &tex->dest.ssa, tg4->component);
        }
 
-       return progress;
+       return nir_vec(b, results, 4);
 }
 
 static bool
-lower_tg4_func(nir_function_impl *impl)
+ir3_nir_lower_tg4_to_tex_filter(const nir_instr *instr, const void *data)
 {
-       void *mem_ctx = ralloc_parent(impl);
-       nir_builder b;
-       nir_builder_init(&b, impl);
-
-       bool progress = false;
-       nir_foreach_block_safe(block, impl) {
-               progress |= lower_tg4(block, &b, mem_ctx);
-       }
-
-       if (progress)
-               nir_metadata_preserve(impl, nir_metadata_block_index |
-                                                                       nir_metadata_dominance);
-
-       return progress;
+       return (instr->type == nir_instr_type_tex &&
+                       nir_instr_as_tex(instr)->op == nir_texop_tg4);
 }
 
 bool
 ir3_nir_lower_tg4_to_tex(nir_shader *shader)
 {
-       bool progress = false;
-
-       nir_foreach_function(function, shader) {
-               if (function->impl)
-                       progress |= lower_tg4_func(function->impl);
-       }
-
-       return progress;
+       return nir_shader_lower_instructions(shader,
+                       ir3_nir_lower_tg4_to_tex_filter,
+                       ir3_nir_lower_tg4_to_tex_instr, NULL);
 }