glsl: Translate the AST for uniform blocks into some IR structures.
authorEric Anholt <eric@anholt.net>
Fri, 27 Apr 2012 01:21:43 +0000 (18:21 -0700)
committerEric Anholt <eric@anholt.net>
Fri, 20 Jul 2012 17:43:19 +0000 (10:43 -0700)
We're going to need this structure to cross-validate the uniform
blocks between shader stages, since unused ir_variables might get
dropped.  It's also the place we store the RowMajor qualifier, which
is not part of the GLSL type (since that would cause a bunch of type
equality checks to fail).

Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
src/glsl/ast_to_hir.cpp
src/glsl/glsl_parser_extras.h
src/glsl/ir.cpp
src/glsl/ir.h
src/glsl/ir_clone.cpp
src/mesa/main/mtypes.h

index 8c739653fbe02fef996d99c52f2c58b84dadd276..1c54991cf7423f4164b6dd69de4846a4c2c907ed 100644 (file)
@@ -3994,6 +3994,25 @@ ast_struct_specifier::hir(exec_list *instructions,
    return NULL;
 }
 
+static struct gl_uniform_block *
+get_next_uniform_block(struct _mesa_glsl_parse_state *state)
+{
+   if (state->num_uniform_blocks >= state->uniform_block_array_size) {
+      state->uniform_block_array_size *= 2;
+      if (state->uniform_block_array_size <= 4)
+        state->uniform_block_array_size = 4;
+
+      state->uniform_blocks = reralloc(state,
+                                      state->uniform_blocks,
+                                      struct gl_uniform_block,
+                                      state->uniform_block_array_size);
+   }
+
+   memset(&state->uniform_blocks[state->num_uniform_blocks],
+         0, sizeof(*state->uniform_blocks));
+   return &state->uniform_blocks[state->num_uniform_blocks++];
+}
+
 ir_rvalue *
 ast_uniform_block::hir(exec_list *instructions,
                       struct _mesa_glsl_parse_state *state)
@@ -4002,11 +4021,59 @@ ast_uniform_block::hir(exec_list *instructions,
     * need to turn those into ir_variables with an association
     * with this uniform block.
     */
+   struct gl_uniform_block *ubo = get_next_uniform_block(state);
+   ubo->Name = ralloc_strdup(state->uniform_blocks, this->block_name);
+
+   unsigned int num_variables = 0;
+   foreach_list_typed(ast_declarator_list, decl_list, link, &declarations) {
+      foreach_list_const(node, &decl_list->declarations) {
+        num_variables++;
+      }
+   }
+
+   bool block_row_major = this->layout.flags.q.row_major;
+
+   ubo->Uniforms = rzalloc_array(state->uniform_blocks,
+                                struct gl_uniform_buffer_variable,
+                                num_variables);
+
    foreach_list_typed(ast_declarator_list, decl_list, link, &declarations) {
       exec_list declared_variables;
 
       decl_list->hir(&declared_variables, state);
 
+      foreach_list_const(node, &declared_variables) {
+        struct ir_variable *var = (ir_variable *)node;
+
+        struct gl_uniform_buffer_variable *ubo_var =
+           &ubo->Uniforms[ubo->NumUniforms++];
+
+        var->uniform_block = ubo - state->uniform_blocks;
+
+        ubo_var->Name = ralloc_strdup(state->uniform_blocks, var->name);
+        ubo_var->Type = var->type;
+        ubo_var->Buffer = ubo - state->uniform_blocks;
+        ubo_var->Offset = 0; /* Assigned at link time. */
+        ubo_var->RowMajor = block_row_major;
+        if (decl_list->type->qualifier.flags.q.row_major)
+           ubo_var->RowMajor = true;
+        else if (decl_list->type->qualifier.flags.q.column_major)
+           ubo_var->RowMajor = false;
+
+        /* From the GL_ARB_uniform_buffer_object spec:
+         *
+         *     "Sampler types are not allowed inside of uniform
+         *      blocks. All other types, arrays, and structures
+         *      allowed for uniforms are allowed within a uniform
+         *      block."
+         */
+        if (var->type->contains_sampler()) {
+           YYLTYPE loc = decl_list->get_location();
+           _mesa_glsl_error(&loc, state,
+                            "Uniform in non-default uniform block contains sampler\n");
+        }
+      }
+
       instructions->append_list(&declared_variables);
    }
 
index d97217ef943ecfb16e733067e8af6326c17d02e4..fc5d11cb97c20eb2780c12e121fce5e83ddd0528 100644 (file)
@@ -82,6 +82,10 @@ struct _mesa_glsl_parse_state {
    exec_list translation_unit;
    glsl_symbol_table *symbols;
 
+   unsigned num_uniform_blocks;
+   unsigned uniform_block_array_size;
+   struct gl_uniform_block *uniform_blocks;
+
    bool es_shader;
    unsigned language_version;
    const char *version_string;
index 1c9eec6e2d19b5e187b82209e827c4837a086a74..b0e38d820e6b9dd6bb721a3a831ec7551fb0142e 100644 (file)
@@ -1491,6 +1491,7 @@ ir_variable::ir_variable(const struct glsl_type *type, const char *name,
    this->explicit_location = false;
    this->has_initializer = false;
    this->location = -1;
+   this->uniform_block = -1;
    this->warn_extension = NULL;
    this->constant_value = NULL;
    this->constant_initializer = NULL;
index 9bbf3b7efae31202ed14c84b0cab7e4478833aa7..8506f604058293e4d652d3f3e96c16e13d17d314 100644 (file)
@@ -453,7 +453,8 @@ public:
     *   - Vertex shader output: one of the values from \c gl_vert_result.
     *   - Fragment shader input: one of the values from \c gl_frag_attrib.
     *   - Fragment shader output: one of the values from \c gl_frag_result.
-    *   - Uniforms: Per-stage uniform slot number.
+    *   - Uniforms: Per-stage uniform slot number for default uniform block.
+    *   - Uniforms: Index within the uniform block definition for UBO members.
     *   - Other: This field is not currently used.
     *
     * If the variable is a uniform, shader input, or shader output, and the
@@ -461,6 +462,16 @@ public:
     */
    int location;
 
+   /**
+    * Uniform block number for uniforms.
+    *
+    * This index is into the shader's list of uniform blocks, not the
+    * linked program's merged list.
+    *
+    * If the variable is not in a uniform block, the value will be -1.
+    */
+   int uniform_block;
+
    /**
     * output index for dual source blending.
     */
index 591fe7b7756780f7b6701ae1f4b2efc974dfd42f..4314efa76d402e12ed3ed293521b126da2d894fc 100644 (file)
@@ -54,6 +54,7 @@ ir_variable::clone(void *mem_ctx, struct hash_table *ht) const
    var->interpolation = this->interpolation;
    var->location = this->location;
    var->index = this->index;
+   var->uniform_block = this->uniform_block;
    var->warn_extension = this->warn_extension;
    var->origin_upper_left = this->origin_upper_left;
    var->pixel_center_integer = this->pixel_center_integer;
index 4769e106631a054fe1be38f885635d82e1ea4e6c..b39a8dcd75bf83fc4408a61c45fcfcb64d796ace 100644 (file)
@@ -2237,6 +2237,24 @@ typedef enum
    MESA_SHADER_TYPES = 3
 } gl_shader_type;
 
+struct gl_uniform_buffer_variable
+{
+   char *Name;
+   const struct glsl_type *Type;
+   unsigned int Buffer;
+   unsigned int Offset;
+   GLboolean RowMajor;
+};
+
+struct gl_uniform_block
+{
+   /** Declared name of the uniform block */
+   char *Name;
+
+   /** Array of supplemental information about UBO ir_variables. */
+   struct gl_uniform_buffer_variable *Uniforms;
+   GLuint NumUniforms;
+};
 
 /**
  * A GLSL program object.