glsl: Track the linearized array index for each UBO instance array element
authorIan Romanick <ian.d.romanick@intel.com>
Mon, 12 Dec 2016 20:24:18 +0000 (12:24 -0800)
committerIan Romanick <ian.d.romanick@intel.com>
Mon, 19 Dec 2016 23:55:37 +0000 (15:55 -0800)
v2: Set linearizer_array_index in process_block_array_leaf.  Suggested
by Timothy.

Signed-off-by: Ian Romanick <ian.d.romanick@intel.com>
Cc: mesa-stable@lists.freedesktop.org
Reviewed-by: Timothy Arceri <timothy.arceri@collabora.com>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
src/compiler/glsl/link_uniform_blocks.cpp
src/mesa/main/mtypes.h

index 41b26e72d2d3b5c8e09427ac6c6b7aeab07b24e5..e952c7a1bffebb7dbc97bb72e280ffeff804df95 100644 (file)
@@ -206,16 +206,23 @@ static void process_block_array_leaf(char **name, gl_uniform_block *blocks,
                                      const struct link_uniform_block_active *const b,
                                      unsigned *block_index,
                                      unsigned *binding_offset,
+                                     unsigned linearized_index,
                                      struct gl_context *ctx,
                                      struct gl_shader_program *prog);
 
+/**
+ *
+ * \param first_index Value of \c block_index for the first element of the
+ *                    array.
+ */
 static void
 process_block_array(struct uniform_block_array_elements *ub_array, char **name,
                     size_t name_length, gl_uniform_block *blocks,
                     ubo_visitor *parcel, gl_uniform_buffer_variable *variables,
                     const struct link_uniform_block_active *const b,
                     unsigned *block_index, unsigned *binding_offset,
-                    struct gl_context *ctx, struct gl_shader_program *prog)
+                    struct gl_context *ctx, struct gl_shader_program *prog,
+                    unsigned first_index)
 {
    for (unsigned j = 0; j < ub_array->num_array_elements; j++) {
       size_t new_length = name_length;
@@ -227,11 +234,12 @@ process_block_array(struct uniform_block_array_elements *ub_array, char **name,
       if (ub_array->array) {
          process_block_array(ub_array->array, name, new_length, blocks,
                              parcel, variables, b, block_index,
-                             binding_offset, ctx, prog);
+                             binding_offset, ctx, prog, first_index);
       } else {
          process_block_array_leaf(name, blocks,
                                   parcel, variables, b, block_index,
-                                  binding_offset, ctx, prog);
+                                  binding_offset, *block_index - first_index,
+                                  ctx, prog);
       }
    }
 }
@@ -242,6 +250,7 @@ process_block_array_leaf(char **name,
                          ubo_visitor *parcel, gl_uniform_buffer_variable *variables,
                          const struct link_uniform_block_active *const b,
                          unsigned *block_index, unsigned *binding_offset,
+                         unsigned linearized_index,
                          struct gl_context *ctx, struct gl_shader_program *prog)
 {
    unsigned i = *block_index;
@@ -262,6 +271,7 @@ process_block_array_leaf(char **name,
    blocks[i].UniformBufferSize = 0;
    blocks[i]._Packing = gl_uniform_block_packing(type->interface_packing);
    blocks[i]._RowMajor = type->get_interface_row_major();
+   blocks[i].linearized_array_index = linearized_index;
 
    parcel->process(type, blocks[i].Name);
 
@@ -359,7 +369,8 @@ create_buffer_blocks(void *mem_ctx, struct gl_context *ctx,
 
             assert(b->has_instance_name);
             process_block_array(b->array, &name, name_length, blocks, &parcel,
-                                variables, b, &i, &binding_offset, ctx, prog);
+                                variables, b, &i, &binding_offset, ctx, prog,
+                                i);
             ralloc_free(name);
          } else {
             blocks[i].Name = ralloc_strdup(blocks, block_type->name);
index 36d48e2115ded4b58ada421f662036234988dc13..ac4cac0e498e1f8e4fb2ee4941f68d4ea300b345 100644 (file)
@@ -2492,6 +2492,21 @@ struct gl_uniform_block
    /** Stages that reference this block */
    uint8_t stageref;
 
+   /**
+    * Linearized array index for uniform block instance arrays
+    *
+    * Given a uniform block instance array declared with size
+    * blk[s_0][s_1]..[s_m], the block referenced by blk[i_0][i_1]..[i_m] will
+    * have the linearized array index
+    *
+    *           m-1       m
+    *     i_m + ∑   i_j * ∏     s_k
+    *           j=0       k=j+1
+    *
+    * For a uniform block instance that is not an array, this is always 0.
+    */
+   uint8_t linearized_array_index;
+
    /**
     * Layout specified in the shader
     *