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

Rob Clark robdclark at gmail.com
Wed Apr 15 14:28:13 PDT 2015


From: Rob Clark <robclark at freedesktop.org>

v2: move ishl into ttn (instead of driver backend) to keep the units
    consistent between immediate and indirect offsets

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

diff --git a/src/gallium/auxiliary/nir/tgsi_to_nir.c b/src/gallium/auxiliary/nir/tgsi_to_nir.c
index b6123e0..6c8d3fc 100644
--- a/src/gallium/auxiliary/nir/tgsi_to_nir.c
+++ b/src/gallium/auxiliary/nir/tgsi_to_nir.c
@@ -162,6 +162,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;
 
@@ -286,7 +290,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;
@@ -314,15 +320,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: {
@@ -330,6 +339,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;
@@ -361,15 +373,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");
@@ -381,8 +402,28 @@ 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 {
+            /* UBOs start at index 1 in TGSI: */
+            load->src[srcn] =
+               nir_src_for_ssa(nir_imm_int(b, dim->Index - 1));
+         }
+         srcn++;
+      }
       if (indirect) {
-         load->src[0] = ttn_src_for_indirect(c, indirect);
+         load->src[srcn] = ttn_src_for_indirect(c, indirect);
+         if (dim) {
+            assert(load->src[srcn].is_ssa);
+            /* we also need to covert vec4 to byte here too: */
+            load->src[srcn] =
+               nir_src_for_ssa(nir_ishl(b, load->src[srcn].ssa,
+                                        nir_imm_int(b, 4)));
+         }
+         srcn++;
       }
       nir_ssa_dest_init(&load->instr, &load->dest, 4, NULL);
       nir_instr_insert_after_cf_list(b->cf_node_list, &load->instr);
@@ -409,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);
    return nir_src_for_ssa(nir_imov_alu(b, src, 1));
 }
 
@@ -524,11 +566,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;
@@ -1122,7 +1173,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;
@@ -1646,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