glsl: Translate the AST for uniform blocks into some IR structures.

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>
This commit is contained in:
Eric Anholt 2012-04-26 18:21:43 -07:00
parent f7561e8ecd
commit b3c093c79c
6 changed files with 103 additions and 1 deletions

View 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);
}

View 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;

View 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;

View 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.
*/

View 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;

View 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.