[Mesa-dev] [PATCH 153/133] nir/tex_instr: Add a nir_tex_src struct and

Jason Ekstrand jason at jlekstrand.net
Fri Jan 9 20:04:48 PST 2015


This solves a number of problems.  First is the ability to change the
number of sources that a texture instruction has.  Second, it solves the
delema that may occur if a texture instruction has more than 4 sources.
---
 src/glsl/nir/glsl_to_nir.cpp             | 36 ++++++++++++++++----------------
 src/glsl/nir/nir.c                       |  5 +++--
 src/glsl/nir/nir.h                       | 22 +++++++++++--------
 src/glsl/nir/nir_lower_samplers.cpp      | 11 +++++++---
 src/glsl/nir/nir_print.c                 |  4 ++--
 src/glsl/nir/nir_validate.c              |  4 ++--
 src/mesa/drivers/dri/i965/brw_fs_nir.cpp |  4 ++--
 7 files changed, 48 insertions(+), 38 deletions(-)

diff --git a/src/glsl/nir/glsl_to_nir.cpp b/src/glsl/nir/glsl_to_nir.cpp
index a84a06f..744c404 100644
--- a/src/glsl/nir/glsl_to_nir.cpp
+++ b/src/glsl/nir/glsl_to_nir.cpp
@@ -1667,20 +1667,20 @@ nir_visitor::visit(ir_texture *ir)
 
    if (ir->coordinate != NULL) {
       instr->coord_components = ir->coordinate->type->vector_elements;
-      instr->src[src_number] = evaluate_rvalue(ir->coordinate);
-      instr->src_type[src_number] = nir_tex_src_coord;
+      instr->src[src_number].src = evaluate_rvalue(ir->coordinate);
+      instr->src[src_number].src_type = nir_tex_src_coord;
       src_number++;
    }
 
    if (ir->projector != NULL) {
-      instr->src[src_number] = evaluate_rvalue(ir->projector);
-      instr->src_type[src_number] = nir_tex_src_projector;
+      instr->src[src_number].src = evaluate_rvalue(ir->projector);
+      instr->src[src_number].src_type = nir_tex_src_projector;
       src_number++;
    }
 
    if (ir->shadow_comparitor != NULL) {
-      instr->src[src_number] = evaluate_rvalue(ir->shadow_comparitor);
-      instr->src_type[src_number] = nir_tex_src_comparitor;
+      instr->src[src_number].src = evaluate_rvalue(ir->shadow_comparitor);
+      instr->src[src_number].src_type = nir_tex_src_comparitor;
       src_number++;
    }
 
@@ -1693,16 +1693,16 @@ nir_visitor::visit(ir_texture *ir)
          for (unsigned i = 0; i < const_offset->type->vector_elements; i++)
             instr->const_offset[i] = const_offset->value.i[i];
       } else {
-         instr->src[src_number] = evaluate_rvalue(ir->offset);
-         instr->src_type[src_number] = nir_tex_src_offset;
+         instr->src[src_number].src = evaluate_rvalue(ir->offset);
+         instr->src[src_number].src_type = nir_tex_src_offset;
          src_number++;
       }
    }
 
    switch (ir->op) {
    case ir_txb:
-      instr->src[src_number] = evaluate_rvalue(ir->lod_info.bias);
-      instr->src_type[src_number] = nir_tex_src_bias;
+      instr->src[src_number].src = evaluate_rvalue(ir->lod_info.bias);
+      instr->src[src_number].src_type = nir_tex_src_bias;
       src_number++;
       break;
 
@@ -1710,24 +1710,24 @@ nir_visitor::visit(ir_texture *ir)
    case ir_txf:
    case ir_txs:
       if (ir->lod_info.lod != NULL) {
-         instr->src[src_number] = evaluate_rvalue(ir->lod_info.lod);
-         instr->src_type[src_number] = nir_tex_src_lod;
+         instr->src[src_number].src = evaluate_rvalue(ir->lod_info.lod);
+         instr->src[src_number].src_type = nir_tex_src_lod;
          src_number++;
       }
       break;
 
    case ir_txd:
-      instr->src[src_number] = evaluate_rvalue(ir->lod_info.grad.dPdx);
-      instr->src_type[src_number] = nir_tex_src_ddx;
+      instr->src[src_number].src = evaluate_rvalue(ir->lod_info.grad.dPdx);
+      instr->src[src_number].src_type = nir_tex_src_ddx;
       src_number++;
-      instr->src[src_number] = evaluate_rvalue(ir->lod_info.grad.dPdy);
-      instr->src_type[src_number] = nir_tex_src_ddy;
+      instr->src[src_number].src = evaluate_rvalue(ir->lod_info.grad.dPdy);
+      instr->src[src_number].src_type = nir_tex_src_ddy;
       src_number++;
       break;
 
    case ir_txf_ms:
-      instr->src[src_number] = evaluate_rvalue(ir->lod_info.sample_index);
-      instr->src_type[src_number] = nir_tex_src_ms_index;
+      instr->src[src_number].src = evaluate_rvalue(ir->lod_info.sample_index);
+      instr->src[src_number].src_type = nir_tex_src_ms_index;
       src_number++;
       break;
 
diff --git a/src/glsl/nir/nir.c b/src/glsl/nir/nir.c
index d8560c2..6239365 100644
--- a/src/glsl/nir/nir.c
+++ b/src/glsl/nir/nir.c
@@ -444,8 +444,9 @@ nir_tex_instr_create(void *mem_ctx, unsigned num_srcs)
    dest_init(&instr->dest);
 
    instr->num_srcs = num_srcs;
-   for (unsigned i = 0; i < 4; i++)
-      src_init(&instr->src[i]);
+   instr->src = ralloc_array(mem_ctx, nir_tex_src, num_srcs);
+   for (unsigned i = 0; i < num_srcs; i++)
+      src_init(&instr->src[i].src);
 
    instr->sampler_index = 0;
    instr->sampler_array_size = 0;
diff --git a/src/glsl/nir/nir.h b/src/glsl/nir/nir.h
index 93023d3..26d99ee 100644
--- a/src/glsl/nir/nir.h
+++ b/src/glsl/nir/nir.h
@@ -823,7 +823,12 @@ typedef enum {
    nir_tex_src_ddy,
    nir_tex_src_sampler_offset, /* < dynamically uniform indirect offset */
    nir_num_texinput_types
-} nir_texinput_type;
+} nir_tex_src_type;
+
+typedef struct {
+   nir_src src;
+   nir_tex_src_type src_type;
+} nir_tex_src;
 
 typedef enum {
    nir_texop_tex,                /**< Regular texture look-up */
@@ -846,8 +851,7 @@ typedef struct {
 
    nir_texop op;
    nir_dest dest;
-   nir_src src[4];
-   nir_texinput_type src_type[4];
+   nir_tex_src *src;
    unsigned num_srcs, coord_components;
    bool is_array, is_shadow;
 
@@ -917,13 +921,13 @@ nir_tex_instr_dest_size(nir_tex_instr *instr)
 static inline unsigned
 nir_tex_instr_src_size(nir_tex_instr *instr, unsigned src)
 {
-   if (instr->src_type[src] == nir_tex_src_coord)
+   if (instr->src[src].src_type == nir_tex_src_coord)
       return instr->coord_components;
 
 
-   if (instr->src_type[src] == nir_tex_src_offset ||
-       instr->src_type[src] == nir_tex_src_ddx ||
-       instr->src_type[src] == nir_tex_src_ddy) {
+   if (instr->src[src].src_type == nir_tex_src_offset ||
+       instr->src[src].src_type == nir_tex_src_ddx ||
+       instr->src[src].src_type == nir_tex_src_ddy) {
       if (instr->is_array)
          return instr->coord_components - 1;
       else
@@ -934,10 +938,10 @@ nir_tex_instr_src_size(nir_tex_instr *instr, unsigned src)
 }
 
 static inline int
-nir_tex_instr_src_index(nir_tex_instr *instr, nir_texinput_type type)
+nir_tex_instr_src_index(nir_tex_instr *instr, nir_tex_src_type type)
 {
    for (unsigned i = 0; i < instr->num_srcs; i++)
-      if (instr->src_type[i] == type)
+      if (instr->src[i].src_type == type)
          return (int) i;
 
    return -1;
diff --git a/src/glsl/nir/nir_lower_samplers.cpp b/src/glsl/nir/nir_lower_samplers.cpp
index 5e90a4c..34b1a2d 100644
--- a/src/glsl/nir/nir_lower_samplers.cpp
+++ b/src/glsl/nir/nir_lower_samplers.cpp
@@ -96,11 +96,16 @@ lower_sampler(nir_tex_instr *instr, struct gl_shader_program *shader_program,
 
             assert(instr->num_srcs < 4);
 
-            nir_instr_rewrite_src(&instr->instr, &instr->src[instr->num_srcs],
-                                  nir_src_copy(deref_array->indirect, mem_ctx));
-            instr->src_type[instr->num_srcs] = nir_tex_src_sampler_offset;
+            instr->src = reralloc(mem_ctx, instr->src, nir_tex_src,
+                                  instr->num_srcs + 1);
+            memset(&instr->src[instr->num_srcs], 0, sizeof *instr->src);
+            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].src,
+                                  nir_src_copy(deref_array->indirect, mem_ctx));
+
             instr->sampler_array_size = glsl_get_length(deref->type);
 
             nir_src empty;
diff --git a/src/glsl/nir/nir_print.c b/src/glsl/nir/nir_print.c
index 502da38..ac1bb92 100644
--- a/src/glsl/nir/nir_print.c
+++ b/src/glsl/nir/nir_print.c
@@ -403,11 +403,11 @@ print_tex_instr(nir_tex_instr *instr, print_var_state *state, FILE *fp)
    }
 
    for (unsigned i = 0; i < instr->num_srcs; i++) {
-      print_src(&instr->src[i], fp);
+      print_src(&instr->src[i].src, fp);
 
       fprintf(fp, " ");
 
-      switch(instr->src_type[i]) {
+      switch(instr->src[i].src_type) {
       case nir_tex_src_coord:
          fprintf(fp, "(coord)");
          break;
diff --git a/src/glsl/nir/nir_validate.c b/src/glsl/nir/nir_validate.c
index 5b5a7fc..a5ca3bd 100644
--- a/src/glsl/nir/nir_validate.c
+++ b/src/glsl/nir/nir_validate.c
@@ -393,8 +393,8 @@ validate_tex_instr(nir_tex_instr *instr, validate_state *state)
       src_type_seen[i] = false;
 
    for (unsigned i = 0; i < instr->num_srcs; i++) {
-      assert(!src_type_seen[instr->src_type[i]]);
-      src_type_seen[instr->src_type[i]] = true;
+      assert(!src_type_seen[instr->src[i].src_type]);
+      src_type_seen[instr->src[i].src_type] = true;
       validate_src(&instr->src[i], state);
    }
 
diff --git a/src/mesa/drivers/dri/i965/brw_fs_nir.cpp b/src/mesa/drivers/dri/i965/brw_fs_nir.cpp
index 845e47f..a520a58 100644
--- a/src/mesa/drivers/dri/i965/brw_fs_nir.cpp
+++ b/src/mesa/drivers/dri/i965/brw_fs_nir.cpp
@@ -1607,8 +1607,8 @@ fs_visitor::nir_emit_texture(nir_tex_instr *instr)
    fs_reg coordinate, shadow_comparitor, lod, lod2, sample_index, mcs, offset;
 
    for (unsigned i = 0; i < instr->num_srcs; i++) {
-      fs_reg src = get_nir_src(instr->src[i]);
-      switch (instr->src_type[i]) {
+      fs_reg src = get_nir_src(instr->src[i].src);
+      switch (instr->src[i].src_type) {
       case nir_tex_src_bias:
          lod = retype(src, BRW_REGISTER_TYPE_F);
          break;
-- 
2.2.0



More information about the mesa-dev mailing list