[Mesa-dev] [PATCH 4/4] gallium/ttn: add UBO support

Rob Clark robdclark at gmail.com
Sun Apr 12 16:01:38 PDT 2015


From: Rob Clark <robclark at freedesktop.org>

Signed-off-by: Rob Clark <robclark at freedesktop.org>
---
 src/gallium/auxiliary/nir/tgsi_to_nir.c | 63 ++++++++++++++++++++++++++++-----
 1 file changed, 54 insertions(+), 9 deletions(-)

diff --git a/src/gallium/auxiliary/nir/tgsi_to_nir.c b/src/gallium/auxiliary/nir/tgsi_to_nir.c
index 610e5d9..bdbbe16 100644
--- a/src/gallium/auxiliary/nir/tgsi_to_nir.c
+++ b/src/gallium/auxiliary/nir/tgsi_to_nir.c
@@ -163,6 +163,10 @@ ttn_emit_declaration(struct ttn_compile *c)
              file == TGSI_FILE_OUTPUT ||
              file == TGSI_FILE_CONSTANT);
 
+      /* nothing to do for UBOs: */
+      if ((file == TGSI_FILE_CONSTANT) && decl->Declaration.Dimension)
+         return;
+
       var = rzalloc(b->shader, nir_variable);
       var->data.driver_location = decl->Range.First;
 
@@ -287,7 +291,9 @@ ttn_array_deref(struct ttn_compile *c, nir_intrinsic_instr *instr,
 
 static nir_src
 ttn_src_for_file_and_index(struct ttn_compile *c, unsigned file, unsigned index,
-                           struct tgsi_ind_register *indirect)
+                           struct tgsi_ind_register *indirect,
+                           struct tgsi_dimension *dim,
+                           struct tgsi_ind_register *dimind)
 {
    nir_builder *b = &c->build;
    nir_src src;
@@ -315,15 +321,18 @@ ttn_src_for_file_and_index(struct ttn_compile *c, unsigned file, unsigned index,
          assert(!indirect);
          src.reg.reg = c->temp_regs[index].reg;
       }
+      assert(!dim);
       break;
 
    case TGSI_FILE_ADDRESS:
       src.reg.reg = c->addr_reg;
+      assert(!dim);
       break;
 
    case TGSI_FILE_IMMEDIATE:
       src = nir_src_for_ssa(c->imm_defs[index]);
       assert(!indirect);
+      assert(!dim);
       break;
 
    case TGSI_FILE_SYSTEM_VALUE: {
@@ -331,6 +340,9 @@ ttn_src_for_file_and_index(struct ttn_compile *c, unsigned file, unsigned index,
       nir_intrinsic_op op;
       unsigned ncomp = 1;
 
+      assert(!indirect);
+      assert(!dim);
+
       switch (c->scan->system_value_semantic_name[index]) {
       case TGSI_SEMANTIC_VERTEXID_NOBASE:
          op = nir_intrinsic_load_vertex_id_zero_base;
@@ -362,15 +374,24 @@ ttn_src_for_file_and_index(struct ttn_compile *c, unsigned file, unsigned index,
    case TGSI_FILE_CONSTANT: {
       nir_intrinsic_instr *load;
       nir_intrinsic_op op;
+      unsigned srcn = 0;
 
       switch (file) {
       case TGSI_FILE_INPUT:
          op = indirect ? nir_intrinsic_load_input_indirect :
                          nir_intrinsic_load_input;
+         assert(!dim);
          break;
       case TGSI_FILE_CONSTANT:
-         op = indirect ? nir_intrinsic_load_uniform_indirect :
-                         nir_intrinsic_load_uniform;
+         if (dim) {
+            op = indirect ? nir_intrinsic_load_ubo_indirect :
+                            nir_intrinsic_load_ubo;
+            /* convert index from vec4 to byte: */
+            index *= 16;
+         } else {
+            op = indirect ? nir_intrinsic_load_uniform_indirect :
+                            nir_intrinsic_load_uniform;
+         }
          break;
       default:
          unreachable("No other load files supported");
@@ -382,13 +403,27 @@ ttn_src_for_file_and_index(struct ttn_compile *c, unsigned file, unsigned index,
       load->num_components = 4;
       load->const_index[0] = index;
       load->const_index[1] = 1;
+      if (dim) {
+         if (dimind) {
+            load->src[srcn++] =
+               ttn_src_for_file_and_index(c, dimind->File, dimind->Index,
+                                          NULL, NULL, NULL);
+         } else {
+            nir_load_const_instr *load_const;
+            load_const = nir_load_const_instr_create(b->shader, 1);
+            /* UBOs start at index 1: */
+            load_const->value.u[0] = dim->Index - 1;
+            load->src[srcn++] = nir_src_for_ssa(&load_const->def);
+            nir_instr_insert_after_cf_list(b->cf_node_list, &load_const->instr);
+         }
+      }
       if (indirect) {
          nir_alu_src indirect_address;
          memset(&indirect_address, 0, sizeof(indirect_address));
          indirect_address.src = nir_src_for_reg(c->addr_reg);
          for (int i = 0; i < 4; i++)
             indirect_address.swizzle[i] = indirect->Swizzle;
-         load->src[0] = nir_src_for_ssa(nir_imov_alu(b, indirect_address, 1));
+         load->src[srcn++] = nir_src_for_ssa(nir_imov_alu(b, indirect_address, 1));
       }
       nir_ssa_dest_init(&load->instr, &load->dest, 4, NULL);
       nir_instr_insert_after_cf_list(b->cf_node_list, &load->instr);
@@ -415,7 +450,8 @@ ttn_src_for_indirect(struct ttn_compile *c, struct tgsi_ind_register *indirect)
       src.swizzle[i] = indirect->Swizzle;
    src.src = ttn_src_for_file_and_index(c,
                                         indirect->File,
-                                        indirect->Index, NULL);
+                                        indirect->Index,
+                                        NULL, NULL, NULL);
    nir_src *result = ralloc(b->shader, nir_src);
    *result = nir_src_for_ssa(nir_imov_alu(b, src, 1));
    return result;
@@ -529,11 +565,20 @@ ttn_get_src(struct ttn_compile *c, struct tgsi_full_src_register *tgsi_fsrc)
       assert(!tgsi_src->Indirect);
       return NULL;
    } else {
+      struct tgsi_ind_register *ind = NULL;
+      struct tgsi_dimension *dim = NULL;
+      struct tgsi_ind_register *dimind = NULL;
+      if (tgsi_src->Indirect)
+         ind = &tgsi_fsrc->Indirect;
+      if (tgsi_src->Dimension) {
+         dim = &tgsi_fsrc->Dimension;
+         if (dim->Indirect)
+            dimind = &tgsi_fsrc->DimIndirect;
+      }
       src.src = ttn_src_for_file_and_index(c,
                                            tgsi_src->File,
                                            tgsi_src->Index,
-                                           (tgsi_src->Indirect ?
-                                            &tgsi_fsrc->Indirect : NULL));
+                                           ind, dim, dimind);
    }
 
    src.swizzle[0] = tgsi_src->SwizzleX;
@@ -1127,7 +1172,7 @@ ttn_tex(struct ttn_compile *c, nir_alu_dest dest, nir_ssa_def **src)
       src.src = ttn_src_for_file_and_index(c,
                                            tex_offset->File,
                                            tex_offset->Index,
-                                           NULL);
+                                           NULL, NULL, NULL);
 
       src.swizzle[0] = tex_offset->SwizzleX;
       src.swizzle[1] = tex_offset->SwizzleY;
@@ -1652,7 +1697,7 @@ tgsi_to_nir(const void *tgsi_tokens,
    c->scan = &scan;
 
    s->num_inputs = scan.file_max[TGSI_FILE_INPUT] + 1;
-   s->num_uniforms = scan.file_max[TGSI_FILE_CONSTANT] + 1;
+   s->num_uniforms = scan.const_file_max[0] + 1;
    s->num_outputs = scan.file_max[TGSI_FILE_OUTPUT] + 1;
 
    c->output_regs = rzalloc_array(c, struct ttn_reg_info,
-- 
2.1.0



More information about the mesa-dev mailing list