nir/tex_instr: Add a nir_tex_src struct and dynamically allocate the src array
authorJason Ekstrand <jason.ekstrand@intel.com>
Sat, 10 Jan 2015 04:01:13 +0000 (20:01 -0800)
committerJason Ekstrand <jason.ekstrand@intel.com>
Thu, 15 Jan 2015 15:20:24 +0000 (07:20 -0800)
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.

Reviewed-by: Connor Abbott <cwabbott0@gmail.com>
src/glsl/nir/glsl_to_nir.cpp
src/glsl/nir/nir.c
src/glsl/nir/nir.h
src/glsl/nir/nir_lower_samplers.cpp
src/glsl/nir/nir_print.c
src/glsl/nir/nir_validate.c
src/mesa/drivers/dri/i965/brw_fs_nir.cpp

index 372f219f4f46096aec2714c6c323e7fa24de5d4e..6ac830e44d9fb729e3cb6163a36c7e30cc4d7d24 100644 (file)
@@ -1662,20 +1662,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++;
    }
 
@@ -1688,16 +1688,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;
 
@@ -1705,24 +1705,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;
 
index ad86a6ea945d72261543529f215a7de8d526d547..da9b7a1d3b6558c166c9f65f0415dba24fc591b7 100644 (file)
@@ -447,8 +447,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;
index 1daf53fa44c6b0827c84aad2e73adbadc3571dd7..e797ce24978e11ff9823e32359f78f329d68259d 100644 (file)
@@ -822,8 +822,13 @@ typedef enum {
    nir_tex_src_ddx,
    nir_tex_src_ddy,
    nir_tex_src_sampler_offset, /* < dynamically uniform indirect offset */
-   nir_num_texinput_types
-} nir_texinput_type;
+   nir_num_tex_src_types
+} 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;
index 5e90a4ce82e6d6456e1bdc7977f772febaec8704..99f31fc028197176eef96cd6d09bcd86549f7022 100644 (file)
@@ -94,12 +94,15 @@ lower_sampler(nir_tex_instr *instr, struct gl_shader_program *shader_program,
          case nir_deref_array_type_indirect: {
             assert(!has_indirect);
 
-            assert(instr->num_srcs < 4);
+            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],
+            nir_instr_rewrite_src(&instr->instr,
+                                  &instr->src[instr->num_srcs - 1].src,
                                   nir_src_copy(deref_array->indirect, mem_ctx));
-            instr->src_type[instr->num_srcs] = nir_tex_src_sampler_offset;
-            instr->num_srcs++;
 
             instr->sampler_array_size = glsl_get_length(deref->type);
 
index 7cf2e0b25081ac8034dc0105ce47e49e4d8a7f12..fe5ceeaf035944345542e844e3f69503eec55f7a 100644 (file)
@@ -402,11 +402,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;
index d2ce021196ea2e565d242fd993b7c4f22fe65070..0d267baa21f563967291a36ef4b137609a1a5c03 100644 (file)
@@ -381,13 +381,13 @@ validate_tex_instr(nir_tex_instr *instr, validate_state *state)
 {
    validate_dest(&instr->dest, state);
 
-   bool src_type_seen[nir_num_texinput_types];
-   for (unsigned i = 0; i < nir_num_texinput_types; i++)
+   bool src_type_seen[nir_num_tex_src_types];
+   for (unsigned i = 0; i < nir_num_tex_src_types; i++)
       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);
    }
 
index ee2f80e7f355c24c4820783f87a46a36008313f5..4c91a6edc44850e03faf93806508dc4df1f52e83 100644 (file)
@@ -1625,8 +1625,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;