diff options
author | marha <marha@users.sourceforge.net> | 2012-08-13 10:09:30 +0200 |
---|---|---|
committer | marha <marha@users.sourceforge.net> | 2012-08-13 10:09:30 +0200 |
commit | 9ddf44af81782451cee798e06749ce3067a14a41 (patch) | |
tree | f84b06f6897929113f080d8e505621fa6bf73fb9 /mesalib/src/glsl | |
parent | f8e35ebbe71eed74ccf68af8ccda4182f1edc7f0 (diff) | |
download | vcxsrv-9ddf44af81782451cee798e06749ce3067a14a41.tar.gz vcxsrv-9ddf44af81782451cee798e06749ce3067a14a41.tar.bz2 vcxsrv-9ddf44af81782451cee798e06749ce3067a14a41.zip |
mesa pixman xkeyboard-config xserver git update 13 Aug 2012
Diffstat (limited to 'mesalib/src/glsl')
-rw-r--r-- | mesalib/src/glsl/Makefile.sources | 1 | ||||
-rw-r--r-- | mesalib/src/glsl/README | 1 | ||||
-rw-r--r-- | mesalib/src/glsl/ast_to_hir.cpp | 14 | ||||
-rw-r--r-- | mesalib/src/glsl/glsl_parser.yy | 20 | ||||
-rw-r--r-- | mesalib/src/glsl/glsl_types.cpp | 13 | ||||
-rw-r--r-- | mesalib/src/glsl/ir.cpp | 1 | ||||
-rw-r--r-- | mesalib/src/glsl/ir.h | 10 | ||||
-rw-r--r-- | mesalib/src/glsl/ir_optimization.h | 1 | ||||
-rw-r--r-- | mesalib/src/glsl/ir_rvalue_visitor.cpp | 127 | ||||
-rw-r--r-- | mesalib/src/glsl/ir_rvalue_visitor.h | 31 | ||||
-rw-r--r-- | mesalib/src/glsl/ir_validate.cpp | 7 | ||||
-rw-r--r-- | mesalib/src/glsl/link_uniforms.cpp | 30 | ||||
-rw-r--r-- | mesalib/src/glsl/lower_ubo_reference.cpp | 313 |
13 files changed, 543 insertions, 26 deletions
diff --git a/mesalib/src/glsl/Makefile.sources b/mesalib/src/glsl/Makefile.sources index f2743f750..765f06a27 100644 --- a/mesalib/src/glsl/Makefile.sources +++ b/mesalib/src/glsl/Makefile.sources @@ -66,6 +66,7 @@ LIBGLSL_CXX_FILES = \ $(GLSL_SRCDIR)/lower_vec_index_to_swizzle.cpp \ $(GLSL_SRCDIR)/lower_vector.cpp \ $(GLSL_SRCDIR)/lower_output_reads.cpp \ + $(GLSL_SRCDIR)/lower_ubo_reference.cpp \ $(GLSL_SRCDIR)/opt_algebraic.cpp \ $(GLSL_SRCDIR)/opt_array_splitting.cpp \ $(GLSL_SRCDIR)/opt_constant_folding.cpp \ diff --git a/mesalib/src/glsl/README b/mesalib/src/glsl/README index dd80a53d4..0a0afccdc 100644 --- a/mesalib/src/glsl/README +++ b/mesalib/src/glsl/README @@ -177,7 +177,6 @@ ir_unop_fract was added. The following areas need updating to add a new expression type: ir.h (new enum) -ir.cpp:get_num_operands() (used for ir_reader) ir.cpp:operator_strs (used for ir_reader) ir_constant_expression.cpp (you probably want to be able to constant fold) ir_validate.cpp (check users have the right types) diff --git a/mesalib/src/glsl/ast_to_hir.cpp b/mesalib/src/glsl/ast_to_hir.cpp index 1c54991cf..02fe66b60 100644 --- a/mesalib/src/glsl/ast_to_hir.cpp +++ b/mesalib/src/glsl/ast_to_hir.cpp @@ -4054,11 +4054,15 @@ ast_uniform_block::hir(exec_list *instructions, 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; + + if (var->type->is_matrix() || + (var->type->is_array() && var->type->fields.array->is_matrix())) { + 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: * diff --git a/mesalib/src/glsl/glsl_parser.yy b/mesalib/src/glsl/glsl_parser.yy index 04c64f096..ee6a67288 100644 --- a/mesalib/src/glsl/glsl_parser.yy +++ b/mesalib/src/glsl/glsl_parser.yy @@ -1929,6 +1929,16 @@ uniform_block: void *ctx = state; $$ = new(ctx) ast_uniform_block(*state->default_uniform_qualifier, $2, $4); + + if (!state->ARB_uniform_buffer_object_enable) { + _mesa_glsl_error(& @1, state, + "#version 140 / GL_ARB_uniform_buffer_object " + "required for defining uniform blocks\n"); + } else if (state->ARB_uniform_buffer_object_warn) { + _mesa_glsl_warning(& @1, state, + "#version 140 / GL_ARB_uniform_buffer_object " + "required for defining uniform blocks\n"); + } } | layout_qualifier UNIFORM NEW_IDENTIFIER '{' member_list '}' ';' { @@ -1939,6 +1949,16 @@ uniform_block: YYERROR; } $$ = new(ctx) ast_uniform_block(qual, $3, $5); + + if (!state->ARB_uniform_buffer_object_enable) { + _mesa_glsl_error(& @1, state, + "#version 140 / GL_ARB_uniform_buffer_object " + "required for defining uniform blocks\n"); + } else if (state->ARB_uniform_buffer_object_warn) { + _mesa_glsl_warning(& @1, state, + "#version 140 / GL_ARB_uniform_buffer_object " + "required for defining uniform blocks\n"); + } } ; diff --git a/mesalib/src/glsl/glsl_types.cpp b/mesalib/src/glsl/glsl_types.cpp index 3d7866058..2aa51f0b3 100644 --- a/mesalib/src/glsl/glsl_types.cpp +++ b/mesalib/src/glsl/glsl_types.cpp @@ -694,14 +694,19 @@ glsl_type::std140_base_alignment(bool row_major) const * row vectors with <C> components each, according to rule (4). */ if (this->is_matrix()) { - const struct glsl_type *vec_type; + const struct glsl_type *vec_type, *array_type; + int c = this->matrix_columns; + int r = this->vector_elements; + if (row_major) { - vec_type = get_instance(GLSL_TYPE_FLOAT, this->vector_elements, 1); + vec_type = get_instance(GLSL_TYPE_FLOAT, c, 1); + array_type = glsl_type::get_array_instance(vec_type, r); } else { - vec_type = get_instance(GLSL_TYPE_FLOAT, this->matrix_columns, 1); + vec_type = get_instance(GLSL_TYPE_FLOAT, r, 1); + array_type = glsl_type::get_array_instance(vec_type, c); } - return vec_type->std140_base_alignment(false); + return array_type->std140_base_alignment(false); } /* (9) If the member is a structure, the base alignment of the diff --git a/mesalib/src/glsl/ir.cpp b/mesalib/src/glsl/ir.cpp index b0e38d820..f59cdd29a 100644 --- a/mesalib/src/glsl/ir.cpp +++ b/mesalib/src/glsl/ir.cpp @@ -480,6 +480,7 @@ static const char *const operator_strs[] = { "min", "max", "pow", + "ubo_load", "vector", }; diff --git a/mesalib/src/glsl/ir.h b/mesalib/src/glsl/ir.h index f019837d5..89c516c87 100644 --- a/mesalib/src/glsl/ir.h +++ b/mesalib/src/glsl/ir.h @@ -1018,9 +1018,17 @@ enum ir_expression_operation { ir_binop_pow, /** + * Load a value the size of a given GLSL type from a uniform block. + * + * operand0 is the ir_constant uniform block index in the linked shader. + * operand1 is a byte offset within the uniform block. + */ + ir_binop_ubo_load, + + /** * A sentinel marking the last of the binary operations. */ - ir_last_binop = ir_binop_pow, + ir_last_binop = ir_binop_ubo_load, ir_quadop_vector, diff --git a/mesalib/src/glsl/ir_optimization.h b/mesalib/src/glsl/ir_optimization.h index c435d7717..2220d511e 100644 --- a/mesalib/src/glsl/ir_optimization.h +++ b/mesalib/src/glsl/ir_optimization.h @@ -74,6 +74,7 @@ bool lower_variable_index_to_cond_assign(exec_list *instructions, bool lower_quadop_vector(exec_list *instructions, bool dont_lower_swz); bool lower_clip_distance(exec_list *instructions); void lower_output_reads(exec_list *instructions); +void lower_ubo_reference(struct gl_shader *shader, exec_list *instructions); bool optimize_redundant_jumps(exec_list *instructions); bool optimize_split_arrays(exec_list *instructions, bool linked); diff --git a/mesalib/src/glsl/ir_rvalue_visitor.cpp b/mesalib/src/glsl/ir_rvalue_visitor.cpp index 193bcd2d7..b34a419e8 100644 --- a/mesalib/src/glsl/ir_rvalue_visitor.cpp +++ b/mesalib/src/glsl/ir_rvalue_visitor.cpp @@ -36,7 +36,7 @@ #include "glsl_types.h" ir_visitor_status -ir_rvalue_visitor::visit_leave(ir_expression *ir) +ir_rvalue_base_visitor::rvalue_visit(ir_expression *ir) { unsigned int operand; @@ -48,7 +48,7 @@ ir_rvalue_visitor::visit_leave(ir_expression *ir) } ir_visitor_status -ir_rvalue_visitor::visit_leave(ir_texture *ir) +ir_rvalue_base_visitor::rvalue_visit(ir_texture *ir) { handle_rvalue(&ir->coordinate); handle_rvalue(&ir->projector); @@ -76,14 +76,14 @@ ir_rvalue_visitor::visit_leave(ir_texture *ir) } ir_visitor_status -ir_rvalue_visitor::visit_leave(ir_swizzle *ir) +ir_rvalue_base_visitor::rvalue_visit(ir_swizzle *ir) { handle_rvalue(&ir->val); return visit_continue; } ir_visitor_status -ir_rvalue_visitor::visit_leave(ir_dereference_array *ir) +ir_rvalue_base_visitor::rvalue_visit(ir_dereference_array *ir) { /* The array index is not the target of the assignment, so clear the * 'in_assignee' flag. Restore it after returning from the array index. @@ -98,14 +98,14 @@ ir_rvalue_visitor::visit_leave(ir_dereference_array *ir) } ir_visitor_status -ir_rvalue_visitor::visit_leave(ir_dereference_record *ir) +ir_rvalue_base_visitor::rvalue_visit(ir_dereference_record *ir) { handle_rvalue(&ir->record); return visit_continue; } ir_visitor_status -ir_rvalue_visitor::visit_leave(ir_assignment *ir) +ir_rvalue_base_visitor::rvalue_visit(ir_assignment *ir) { handle_rvalue(&ir->rhs); handle_rvalue(&ir->condition); @@ -114,7 +114,7 @@ ir_rvalue_visitor::visit_leave(ir_assignment *ir) } ir_visitor_status -ir_rvalue_visitor::visit_leave(ir_call *ir) +ir_rvalue_base_visitor::rvalue_visit(ir_call *ir) { foreach_iter(exec_list_iterator, iter, *ir) { ir_rvalue *param = (ir_rvalue *)iter.get(); @@ -129,15 +129,124 @@ ir_rvalue_visitor::visit_leave(ir_call *ir) } ir_visitor_status -ir_rvalue_visitor::visit_leave(ir_return *ir) +ir_rvalue_base_visitor::rvalue_visit(ir_return *ir) { handle_rvalue(&ir->value);; return visit_continue; } ir_visitor_status -ir_rvalue_visitor::visit_leave(ir_if *ir) +ir_rvalue_base_visitor::rvalue_visit(ir_if *ir) { handle_rvalue(&ir->condition); return visit_continue; } + + +ir_visitor_status +ir_rvalue_visitor::visit_leave(ir_expression *ir) +{ + return rvalue_visit(ir); +} + +ir_visitor_status +ir_rvalue_visitor::visit_leave(ir_texture *ir) +{ + return rvalue_visit(ir); +} + +ir_visitor_status +ir_rvalue_visitor::visit_leave(ir_swizzle *ir) +{ + return rvalue_visit(ir); +} + +ir_visitor_status +ir_rvalue_visitor::visit_leave(ir_dereference_array *ir) +{ + return rvalue_visit(ir); +} + +ir_visitor_status +ir_rvalue_visitor::visit_leave(ir_dereference_record *ir) +{ + return rvalue_visit(ir); +} + +ir_visitor_status +ir_rvalue_visitor::visit_leave(ir_assignment *ir) +{ + return rvalue_visit(ir); +} + +ir_visitor_status +ir_rvalue_visitor::visit_leave(ir_call *ir) +{ + return rvalue_visit(ir); +} + +ir_visitor_status +ir_rvalue_visitor::visit_leave(ir_return *ir) +{ + return rvalue_visit(ir); +} + +ir_visitor_status +ir_rvalue_visitor::visit_leave(ir_if *ir) +{ + return rvalue_visit(ir); +} + +ir_visitor_status +ir_rvalue_enter_visitor::visit_enter(ir_expression *ir) +{ + return rvalue_visit(ir); +} + +ir_visitor_status +ir_rvalue_enter_visitor::visit_enter(ir_texture *ir) +{ + return rvalue_visit(ir); +} + +ir_visitor_status +ir_rvalue_enter_visitor::visit_enter(ir_swizzle *ir) +{ + return rvalue_visit(ir); +} + +ir_visitor_status +ir_rvalue_enter_visitor::visit_enter(ir_dereference_array *ir) +{ + return rvalue_visit(ir); +} + +ir_visitor_status +ir_rvalue_enter_visitor::visit_enter(ir_dereference_record *ir) +{ + return rvalue_visit(ir); +} + +ir_visitor_status +ir_rvalue_enter_visitor::visit_enter(ir_assignment *ir) +{ + return rvalue_visit(ir); +} + +ir_visitor_status +ir_rvalue_enter_visitor::visit_enter(ir_call *ir) +{ + return rvalue_visit(ir); +} + +ir_visitor_status +ir_rvalue_enter_visitor::visit_enter(ir_return *ir) +{ + return rvalue_visit(ir); +} + +ir_visitor_status +ir_rvalue_enter_visitor::visit_enter(ir_if *ir) +{ + return rvalue_visit(ir); +} diff --git a/mesalib/src/glsl/ir_rvalue_visitor.h b/mesalib/src/glsl/ir_rvalue_visitor.h index 31a56beb9..2179fa5a8 100644 --- a/mesalib/src/glsl/ir_rvalue_visitor.h +++ b/mesalib/src/glsl/ir_rvalue_visitor.h @@ -30,7 +30,22 @@ * a pointer to each rvalue in the tree. */ -class ir_rvalue_visitor : public ir_hierarchical_visitor { +class ir_rvalue_base_visitor : public ir_hierarchical_visitor { +public: + ir_visitor_status rvalue_visit(ir_assignment *); + ir_visitor_status rvalue_visit(ir_call *); + ir_visitor_status rvalue_visit(ir_dereference_array *); + ir_visitor_status rvalue_visit(ir_dereference_record *); + ir_visitor_status rvalue_visit(ir_expression *); + ir_visitor_status rvalue_visit(ir_if *); + ir_visitor_status rvalue_visit(ir_return *); + ir_visitor_status rvalue_visit(ir_swizzle *); + ir_visitor_status rvalue_visit(ir_texture *); + + virtual void handle_rvalue(ir_rvalue **rvalue) = 0; +}; + +class ir_rvalue_visitor : public ir_rvalue_base_visitor { public: virtual ir_visitor_status visit_leave(ir_assignment *); @@ -42,6 +57,18 @@ public: virtual ir_visitor_status visit_leave(ir_return *); virtual ir_visitor_status visit_leave(ir_swizzle *); virtual ir_visitor_status visit_leave(ir_texture *); +}; - virtual void handle_rvalue(ir_rvalue **rvalue) = 0; +class ir_rvalue_enter_visitor : public ir_rvalue_base_visitor { +public: + + virtual ir_visitor_status visit_enter(ir_assignment *); + virtual ir_visitor_status visit_enter(ir_call *); + virtual ir_visitor_status visit_enter(ir_dereference_array *); + virtual ir_visitor_status visit_enter(ir_dereference_record *); + virtual ir_visitor_status visit_enter(ir_expression *); + virtual ir_visitor_status visit_enter(ir_if *); + virtual ir_visitor_status visit_enter(ir_return *); + virtual ir_visitor_status visit_enter(ir_swizzle *); + virtual ir_visitor_status visit_enter(ir_texture *); }; diff --git a/mesalib/src/glsl/ir_validate.cpp b/mesalib/src/glsl/ir_validate.cpp index 191d39831..af0b5768a 100644 --- a/mesalib/src/glsl/ir_validate.cpp +++ b/mesalib/src/glsl/ir_validate.cpp @@ -423,6 +423,13 @@ ir_validate::visit_leave(ir_expression *ir) assert(ir->operands[0]->type == ir->operands[1]->type); break; + case ir_binop_ubo_load: + assert(ir->operands[0]->as_constant()); + assert(ir->operands[0]->type == glsl_type::uint_type); + + assert(ir->operands[1]->type == glsl_type::uint_type); + break; + case ir_quadop_vector: /* The vector operator collects some number of scalars and generates a * vector from them. diff --git a/mesalib/src/glsl/link_uniforms.cpp b/mesalib/src/glsl/link_uniforms.cpp index 1baa46c5d..25dc1d7dc 100644 --- a/mesalib/src/glsl/link_uniforms.cpp +++ b/mesalib/src/glsl/link_uniforms.cpp @@ -224,14 +224,24 @@ public: } void set_and_process(struct gl_shader_program *prog, + struct gl_shader *shader, ir_variable *var) { ubo_var = NULL; if (var->uniform_block != -1) { struct gl_uniform_block *block = - &prog->UniformBlocks[var->uniform_block]; + &shader->UniformBlocks[var->uniform_block]; + + ubo_block_index = -1; + for (unsigned i = 0; i < prog->NumUniformBlocks; i++) { + if (!strcmp(prog->UniformBlocks[i].Name, + shader->UniformBlocks[var->uniform_block].Name)) { + ubo_block_index = i; + break; + } + } + assert(ubo_block_index != -1); - ubo_block_index = var->uniform_block; ubo_var_index = var->location; ubo_var = &block->Uniforms[var->location]; ubo_byte_offset = ubo_var->Offset; @@ -490,7 +500,19 @@ link_assign_uniform_block_offsets(struct gl_shader *shader) ubo_var->Offset = offset; offset += size; } - block->UniformBufferSize = offset; + + /* From the GL_ARB_uniform_buffer_object spec: + * + * "For uniform blocks laid out according to [std140] rules, + * the minimum buffer object size returned by the + * UNIFORM_BLOCK_DATA_SIZE query is derived by taking the + * offset of the last basic machine unit consumed by the + * last uniform of the uniform block (including any + * end-of-array or end-of-structure padding), adding one, + * and rounding up to the next multiple of the base + * alignment required for a vec4." + */ + block->UniformBufferSize = align(offset, 16); } } @@ -598,7 +620,7 @@ link_assign_uniform_locations(struct gl_shader_program *prog) if (strncmp("gl_", var->name, 3) == 0) continue; - parcel.set_and_process(prog, var); + parcel.set_and_process(prog, prog->_LinkedShaders[i], var); } prog->_LinkedShaders[i]->active_samplers = parcel.shader_samplers_used; diff --git a/mesalib/src/glsl/lower_ubo_reference.cpp b/mesalib/src/glsl/lower_ubo_reference.cpp new file mode 100644 index 000000000..e8d2c4742 --- /dev/null +++ b/mesalib/src/glsl/lower_ubo_reference.cpp @@ -0,0 +1,313 @@ +/* + * Copyright © 2012 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +/** + * \file lower_ubo_reference.cpp + * + * IR lower pass to replace dereferences of variables in a uniform + * buffer object with usage of ir_binop_ubo_load expressions, each of + * which can read data up to the size of a vec4. + * + * This relieves drivers of the responsibility to deal with tricky UBO + * layout issues like std140 structures and row_major matrices on + * their own. + */ + +#include "ir.h" +#include "ir_builder.h" +#include "ir_rvalue_visitor.h" +#include "main/macros.h" + +using namespace ir_builder; + +namespace { +class lower_ubo_reference_visitor : public ir_rvalue_enter_visitor { +public: + lower_ubo_reference_visitor(struct gl_shader *shader) + : shader(shader) + { + } + + void handle_rvalue(ir_rvalue **rvalue); + void emit_ubo_loads(ir_dereference *deref, ir_variable *base_offset, + unsigned int deref_offset); + ir_expression *ubo_load(const struct glsl_type *type, + ir_rvalue *offset); + + void *mem_ctx; + struct gl_shader *shader; + struct gl_uniform_buffer_variable *ubo_var; + unsigned uniform_block; + bool progress; +}; + +static inline unsigned int +align(unsigned int a, unsigned int align) +{ + return (a + align - 1) / align * align; +} + +void +lower_ubo_reference_visitor::handle_rvalue(ir_rvalue **rvalue) +{ + if (!*rvalue) + return; + + ir_dereference *deref = (*rvalue)->as_dereference(); + if (!deref) + return; + + ir_variable *var = deref->variable_referenced(); + if (!var || var->uniform_block == -1) + return; + + mem_ctx = ralloc_parent(*rvalue); + uniform_block = var->uniform_block; + struct gl_uniform_block *block = &shader->UniformBlocks[uniform_block]; + this->ubo_var = &block->Uniforms[var->location]; + ir_rvalue *offset = new(mem_ctx) ir_constant(0u); + unsigned const_offset = 0; + bool row_major = ubo_var->RowMajor; + + /* Calculate the offset to the start of the region of the UBO + * dereferenced by *rvalue. This may be a variable offset if an + * array dereference has a variable index. + */ + while (deref) { + switch (deref->ir_type) { + case ir_type_dereference_variable: { + const_offset += ubo_var->Offset; + deref = NULL; + break; + } + + case ir_type_dereference_array: { + ir_dereference_array *deref_array = (ir_dereference_array *)deref; + unsigned array_stride; + if (deref_array->array->type->is_matrix() && row_major) { + /* When loading a vector out of a row major matrix, the + * step between the columns (vectors) is the size of a + * float, while the step between the rows (elements of a + * vector) is handled below in emit_ubo_loads. + */ + array_stride = 4; + } else { + array_stride = deref_array->type->std140_size(row_major); + array_stride = align(array_stride, 16); + } + + ir_constant *const_index = deref_array->array_index->as_constant(); + if (const_index) { + const_offset += array_stride * const_index->value.i[0]; + } else { + offset = add(offset, + mul(deref_array->array_index, + new(mem_ctx) ir_constant(array_stride))); + } + deref = deref_array->array->as_dereference(); + break; + } + + case ir_type_dereference_record: { + ir_dereference_record *deref_record = (ir_dereference_record *)deref; + const glsl_type *struct_type = deref_record->record->type; + unsigned intra_struct_offset = 0; + + unsigned max_field_align = 16; + for (unsigned int i = 0; i < struct_type->length; i++) { + const glsl_type *type = struct_type->fields.structure[i].type; + unsigned field_align = type->std140_base_alignment(row_major); + max_field_align = MAX2(field_align, max_field_align); + intra_struct_offset = align(intra_struct_offset, field_align); + + if (strcmp(struct_type->fields.structure[i].name, + deref_record->field) == 0) + break; + intra_struct_offset += type->std140_size(row_major); + } + + const_offset = align(const_offset, max_field_align); + const_offset += intra_struct_offset; + + deref = deref_record->record->as_dereference(); + break; + } + default: + assert(!"not reached"); + deref = NULL; + break; + } + } + + /* Now that we've calculated the offset to the start of the + * dereference, walk over the type and emit loads into a temporary. + */ + const glsl_type *type = (*rvalue)->type; + ir_variable *load_var = new(mem_ctx) ir_variable(type, + "ubo_load_temp", + ir_var_temporary); + base_ir->insert_before(load_var); + + ir_variable *load_offset = new(mem_ctx) ir_variable(glsl_type::uint_type, + "ubo_load_temp_offset", + ir_var_temporary); + base_ir->insert_before(load_offset); + base_ir->insert_before(assign(load_offset, offset)); + + deref = new(mem_ctx) ir_dereference_variable(load_var); + emit_ubo_loads(deref, load_offset, const_offset); + *rvalue = deref; + + progress = true; +} + +ir_expression * +lower_ubo_reference_visitor::ubo_load(const glsl_type *type, + ir_rvalue *offset) +{ + return new(mem_ctx) + ir_expression(ir_binop_ubo_load, + type, + new(mem_ctx) ir_constant(this->uniform_block), + offset); + +} + +/** + * Takes LHS and emits a series of assignments into its components + * from the UBO variable at variable_offset + deref_offset. + * + * Recursively calls itself to break the deref down to the point that + * the ir_binop_ubo_load expressions generated are contiguous scalars + * or vectors. + */ +void +lower_ubo_reference_visitor::emit_ubo_loads(ir_dereference *deref, + ir_variable *base_offset, + unsigned int deref_offset) +{ + if (deref->type->is_record()) { + unsigned int field_offset = 0; + + for (unsigned i = 0; i < deref->type->length; i++) { + const struct glsl_struct_field *field = + &deref->type->fields.structure[i]; + ir_dereference *field_deref = + new(mem_ctx) ir_dereference_record(deref->clone(mem_ctx, NULL), + field->name); + + field_offset = + align(field_offset, + field->type->std140_base_alignment(ubo_var->RowMajor)); + + emit_ubo_loads(field_deref, base_offset, deref_offset + field_offset); + + field_offset += field->type->std140_size(ubo_var->RowMajor); + } + return; + } + + if (deref->type->is_array()) { + unsigned array_stride = + align(deref->type->fields.array->std140_size(ubo_var->RowMajor), 16); + + for (unsigned i = 0; i < deref->type->length; i++) { + ir_constant *element = new(mem_ctx) ir_constant(i); + ir_dereference *element_deref = + new(mem_ctx) ir_dereference_array(deref->clone(mem_ctx, NULL), + element); + emit_ubo_loads(element_deref, base_offset, + deref_offset + i * array_stride); + } + return; + } + + if (deref->type->is_matrix()) { + for (unsigned i = 0; i < deref->type->matrix_columns; i++) { + ir_constant *col = new(mem_ctx) ir_constant(i); + ir_dereference *col_deref = + new(mem_ctx) ir_dereference_array(deref->clone(mem_ctx, NULL), + col); + + /* std140 always rounds the stride of arrays (and matrices) + * to a vec4, so matrices are always 16 between columns/rows. + */ + emit_ubo_loads(col_deref, base_offset, deref_offset + i * 16); + } + return; + } + + assert(deref->type->is_scalar() || + deref->type->is_vector()); + + if (!ubo_var->RowMajor) { + ir_rvalue *offset = add(base_offset, + new(mem_ctx) ir_constant(deref_offset)); + base_ir->insert_before(assign(deref->clone(mem_ctx, NULL), + ubo_load(deref->type, offset))); + } else { + /* We're dereffing a column out of a row-major matrix, so we + * gather the vector from each stored row. + */ + assert(deref->type->base_type == GLSL_TYPE_FLOAT); + /* Matrices, row_major or not, are stored as if they were + * arrays of vectors of the appropriate size in std140. + * Arrays have their strides rounded up to a vec4, so the + * matrix stride is always 16. + */ + unsigned matrix_stride = 16; + + for (unsigned i = 0; i < deref->type->vector_elements; i++) { + ir_rvalue *chan = new(mem_ctx) ir_constant((int)i); + ir_dereference *deref_chan = + new(mem_ctx) ir_dereference_array(deref->clone(mem_ctx, NULL), + chan); + + ir_rvalue *chan_offset = + add(base_offset, + new(mem_ctx) ir_constant(deref_offset + i * matrix_stride)); + + base_ir->insert_before(assign(deref_chan, + ubo_load(glsl_type::float_type, + chan_offset))); + } + } +} + +} /* unnamed namespace */ + +void +lower_ubo_reference(struct gl_shader *shader, exec_list *instructions) +{ + lower_ubo_reference_visitor v(shader); + + /* Loop over the instructions lowering references, because we take + * a deref of a UBO array using a UBO dereference as the index will + * produce a collection of instructions all of which have cloned + * UBO dereferences for that array index. + */ + do { + v.progress = false; + visit_list_elements(&v, instructions); + } while (v.progress); +} |