nir: Add a helper for adding texture instruction sources
authorJason Ekstrand <jason.ekstrand@intel.com>
Mon, 16 Oct 2017 15:50:23 +0000 (08:50 -0700)
committerJason Ekstrand <jason.ekstrand@intel.com>
Tue, 17 Oct 2017 14:36:00 +0000 (07:36 -0700)
Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com>
src/compiler/nir/nir.c
src/compiler/nir/nir.h
src/compiler/nir/nir_lower_samplers.c
src/intel/vulkan/anv_nir_apply_pipeline_layout.c

index afd4d1a723681c43282e1ed0c021cc5c3efc2b57..5bc07b7e506935b170795179b4cf6cd8a9aaf7e3 100644 (file)
@@ -541,6 +541,28 @@ nir_tex_instr_create(nir_shader *shader, unsigned num_srcs)
    return instr;
 }
 
+void
+nir_tex_instr_add_src(nir_tex_instr *tex,
+                      nir_tex_src_type src_type,
+                      nir_src src)
+{
+   nir_tex_src *new_srcs = rzalloc_array(tex, nir_tex_src,
+                                         tex->num_srcs + 1);
+
+   for (unsigned i = 0; i < tex->num_srcs; i++) {
+      new_srcs[i].src_type = tex->src[i].src_type;
+      nir_instr_move_src(&tex->instr, &new_srcs[i].src,
+                         &tex->src[i].src);
+   }
+
+   ralloc_free(tex->src);
+   tex->src = new_srcs;
+
+   tex->src[tex->num_srcs].src_type = src_type;
+   nir_instr_rewrite_src(&tex->instr, &tex->src[tex->num_srcs].src, src);
+   tex->num_srcs++;
+}
+
 void
 nir_tex_instr_remove_src(nir_tex_instr *tex, unsigned src_idx)
 {
index bd6035e1f90ee4368bd40741ec733e4f5402a3d0..70c23c2db99cf764c43c94569f3b02f9b813fd27 100644 (file)
@@ -1379,6 +1379,10 @@ nir_tex_instr_src_index(const nir_tex_instr *instr, nir_tex_src_type type)
    return -1;
 }
 
+void nir_tex_instr_add_src(nir_tex_instr *tex,
+                           nir_tex_src_type src_type,
+                           nir_src src);
+
 void nir_tex_instr_remove_src(nir_tex_instr *tex, unsigned src_idx);
 
 typedef struct {
index 0c4e91b000fac1968fb78fc518b567802d753c30..f75fb1afe886f433c97d45e9a44aecf582f1575d 100644 (file)
@@ -109,32 +109,9 @@ lower_sampler(nir_tex_instr *instr, const struct gl_shader_program *shader_progr
       assert(array_elements >= 1);
       indirect = nir_umin(b, indirect, nir_imm_int(b, array_elements - 1));
 
-      /* First, we have to resize the array of texture sources */
-      nir_tex_src *new_srcs = rzalloc_array(instr, nir_tex_src,
-                                            instr->num_srcs + 2);
-
-      for (unsigned i = 0; i < instr->num_srcs; i++) {
-         new_srcs[i].src_type = instr->src[i].src_type;
-         nir_instr_move_src(&instr->instr, &new_srcs[i].src,
-                            &instr->src[i].src);
-      }
-
-      ralloc_free(instr->src);
-      instr->src = new_srcs;
-
-      /* Now we can go ahead and move the source over to being a
-       * first-class texture source.
-       */
-      instr->src[instr->num_srcs].src_type = nir_tex_src_texture_offset;
-      instr->num_srcs++;
-      nir_instr_rewrite_src(&instr->instr,
-                            &instr->src[instr->num_srcs - 1].src,
+      nir_tex_instr_add_src(instr, nir_tex_src_texture_offset,
                             nir_src_for_ssa(indirect));
-
-      instr->src[instr->num_srcs].src_type = nir_tex_src_sampler_offset;
-      instr->num_srcs++;
-      nir_instr_rewrite_src(&instr->instr,
-                            &instr->src[instr->num_srcs - 1].src,
+      nir_tex_instr_add_src(instr, nir_tex_src_sampler_offset,
                             nir_src_for_ssa(indirect));
 
       instr->texture_array_size = array_elements;
index 26e7dccc79da0509fb08d84ca0d3cc458e2dcaa9..1c8651333b31ea6159b9be99a4b99f9d3a59d70d 100644 (file)
@@ -157,24 +157,7 @@ lower_tex_deref(nir_tex_instr *tex, nir_deref_var *deref,
          if (state->add_bounds_checks)
             index = nir_umin(b, index, nir_imm_int(b, array_size - 1));
 
-         nir_tex_src *new_srcs = rzalloc_array(tex, nir_tex_src,
-                                               tex->num_srcs + 1);
-
-         for (unsigned i = 0; i < tex->num_srcs; i++) {
-            new_srcs[i].src_type = tex->src[i].src_type;
-            nir_instr_move_src(&tex->instr, &new_srcs[i].src, &tex->src[i].src);
-         }
-
-         ralloc_free(tex->src);
-         tex->src = new_srcs;
-
-         /* Now we can go ahead and move the source over to being a
-          * first-class texture source.
-          */
-         tex->src[tex->num_srcs].src_type = src_type;
-         nir_instr_rewrite_src(&tex->instr, &tex->src[tex->num_srcs].src,
-                               nir_src_for_ssa(index));
-         tex->num_srcs++;
+         nir_tex_instr_add_src(tex, src_type, nir_src_for_ssa(index));
       } else {
          *const_index += MIN2(deref_array->base_offset, array_size - 1);
       }