2 * Copyright © 2012 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 * \file lower_ubo_reference.cpp
27 * IR lower pass to replace dereferences of variables in a uniform
28 * buffer object with usage of ir_binop_ubo_load expressions, each of
29 * which can read data up to the size of a vec4.
31 * This relieves drivers of the responsibility to deal with tricky UBO
32 * layout issues like std140 structures and row_major matrices on
37 #include "ir_builder.h"
38 #include "ir_rvalue_visitor.h"
39 #include "main/macros.h"
41 using namespace ir_builder
;
44 class lower_ubo_reference_visitor
: public ir_rvalue_enter_visitor
{
46 lower_ubo_reference_visitor(struct gl_shader
*shader
)
51 void handle_rvalue(ir_rvalue
**rvalue
);
52 void emit_ubo_loads(ir_dereference
*deref
, ir_variable
*base_offset
,
53 unsigned int deref_offset
);
54 ir_expression
*ubo_load(const struct glsl_type
*type
,
58 struct gl_shader
*shader
;
59 struct gl_uniform_buffer_variable
*ubo_var
;
60 unsigned uniform_block
;
64 static inline unsigned int
65 align(unsigned int a
, unsigned int align
)
67 return (a
+ align
- 1) / align
* align
;
71 lower_ubo_reference_visitor::handle_rvalue(ir_rvalue
**rvalue
)
76 ir_dereference
*deref
= (*rvalue
)->as_dereference();
80 ir_variable
*var
= deref
->variable_referenced();
81 if (!var
|| var
->uniform_block
== -1)
84 mem_ctx
= ralloc_parent(*rvalue
);
85 uniform_block
= var
->uniform_block
;
86 struct gl_uniform_block
*block
= &shader
->UniformBlocks
[uniform_block
];
87 this->ubo_var
= &block
->Uniforms
[var
->location
];
88 ir_rvalue
*offset
= new(mem_ctx
) ir_constant(0u);
89 unsigned const_offset
= 0;
90 bool row_major
= ubo_var
->RowMajor
;
92 /* Calculate the offset to the start of the region of the UBO
93 * dereferenced by *rvalue. This may be a variable offset if an
94 * array dereference has a variable index.
97 switch (deref
->ir_type
) {
98 case ir_type_dereference_variable
: {
99 const_offset
+= ubo_var
->Offset
;
104 case ir_type_dereference_array
: {
105 ir_dereference_array
*deref_array
= (ir_dereference_array
*)deref
;
106 unsigned array_stride
;
107 if (deref_array
->array
->type
->is_matrix() && row_major
) {
108 /* When loading a vector out of a row major matrix, the
109 * step between the columns (vectors) is the size of a
110 * float, while the step between the rows (elements of a
111 * vector) is handled below in emit_ubo_loads.
115 array_stride
= deref_array
->type
->std140_size(row_major
);
116 array_stride
= align(array_stride
, 16);
119 ir_constant
*const_index
= deref_array
->array_index
->as_constant();
121 const_offset
+= array_stride
* const_index
->value
.i
[0];
124 mul(deref_array
->array_index
,
125 new(mem_ctx
) ir_constant(array_stride
)));
127 deref
= deref_array
->array
->as_dereference();
131 case ir_type_dereference_record
: {
132 ir_dereference_record
*deref_record
= (ir_dereference_record
*)deref
;
133 const glsl_type
*struct_type
= deref_record
->record
->type
;
134 unsigned intra_struct_offset
= 0;
136 unsigned max_field_align
= 16;
137 for (unsigned int i
= 0; i
< struct_type
->length
; i
++) {
138 const glsl_type
*type
= struct_type
->fields
.structure
[i
].type
;
139 unsigned field_align
= type
->std140_base_alignment(row_major
);
140 max_field_align
= MAX2(field_align
, max_field_align
);
141 intra_struct_offset
= align(intra_struct_offset
, field_align
);
143 if (strcmp(struct_type
->fields
.structure
[i
].name
,
144 deref_record
->field
) == 0)
146 intra_struct_offset
+= type
->std140_size(row_major
);
149 const_offset
= align(const_offset
, max_field_align
);
150 const_offset
+= intra_struct_offset
;
152 deref
= deref_record
->record
->as_dereference();
156 assert(!"not reached");
162 /* Now that we've calculated the offset to the start of the
163 * dereference, walk over the type and emit loads into a temporary.
165 const glsl_type
*type
= (*rvalue
)->type
;
166 ir_variable
*load_var
= new(mem_ctx
) ir_variable(type
,
169 base_ir
->insert_before(load_var
);
171 ir_variable
*load_offset
= new(mem_ctx
) ir_variable(glsl_type::uint_type
,
172 "ubo_load_temp_offset",
174 base_ir
->insert_before(load_offset
);
175 base_ir
->insert_before(assign(load_offset
, offset
));
177 deref
= new(mem_ctx
) ir_dereference_variable(load_var
);
178 emit_ubo_loads(deref
, load_offset
, const_offset
);
185 lower_ubo_reference_visitor::ubo_load(const glsl_type
*type
,
189 ir_expression(ir_binop_ubo_load
,
191 new(mem_ctx
) ir_constant(this->uniform_block
),
197 * Takes LHS and emits a series of assignments into its components
198 * from the UBO variable at variable_offset + deref_offset.
200 * Recursively calls itself to break the deref down to the point that
201 * the ir_binop_ubo_load expressions generated are contiguous scalars
205 lower_ubo_reference_visitor::emit_ubo_loads(ir_dereference
*deref
,
206 ir_variable
*base_offset
,
207 unsigned int deref_offset
)
209 if (deref
->type
->is_record()) {
210 unsigned int field_offset
= 0;
212 for (unsigned i
= 0; i
< deref
->type
->length
; i
++) {
213 const struct glsl_struct_field
*field
=
214 &deref
->type
->fields
.structure
[i
];
215 ir_dereference
*field_deref
=
216 new(mem_ctx
) ir_dereference_record(deref
->clone(mem_ctx
, NULL
),
221 field
->type
->std140_base_alignment(ubo_var
->RowMajor
));
223 emit_ubo_loads(field_deref
, base_offset
, deref_offset
+ field_offset
);
225 field_offset
+= field
->type
->std140_size(ubo_var
->RowMajor
);
230 if (deref
->type
->is_array()) {
231 unsigned array_stride
=
232 align(deref
->type
->fields
.array
->std140_size(ubo_var
->RowMajor
), 16);
234 for (unsigned i
= 0; i
< deref
->type
->length
; i
++) {
235 ir_constant
*element
= new(mem_ctx
) ir_constant(i
);
236 ir_dereference
*element_deref
=
237 new(mem_ctx
) ir_dereference_array(deref
->clone(mem_ctx
, NULL
),
239 emit_ubo_loads(element_deref
, base_offset
,
240 deref_offset
+ i
* array_stride
);
245 if (deref
->type
->is_matrix()) {
246 for (unsigned i
= 0; i
< deref
->type
->matrix_columns
; i
++) {
247 ir_constant
*col
= new(mem_ctx
) ir_constant(i
);
248 ir_dereference
*col_deref
=
249 new(mem_ctx
) ir_dereference_array(deref
->clone(mem_ctx
, NULL
),
252 /* std140 always rounds the stride of arrays (and matrices)
253 * to a vec4, so matrices are always 16 between columns/rows.
255 emit_ubo_loads(col_deref
, base_offset
, deref_offset
+ i
* 16);
260 assert(deref
->type
->is_scalar() ||
261 deref
->type
->is_vector());
263 if (!ubo_var
->RowMajor
) {
264 ir_rvalue
*offset
= add(base_offset
,
265 new(mem_ctx
) ir_constant(deref_offset
));
266 base_ir
->insert_before(assign(deref
->clone(mem_ctx
, NULL
),
267 ubo_load(deref
->type
, offset
)));
269 /* We're dereffing a column out of a row-major matrix, so we
270 * gather the vector from each stored row.
272 assert(deref
->type
->base_type
== GLSL_TYPE_FLOAT
);
273 /* Matrices, row_major or not, are stored as if they were
274 * arrays of vectors of the appropriate size in std140.
275 * Arrays have their strides rounded up to a vec4, so the
276 * matrix stride is always 16.
278 unsigned matrix_stride
= 16;
280 for (unsigned i
= 0; i
< deref
->type
->vector_elements
; i
++) {
281 ir_rvalue
*chan
= new(mem_ctx
) ir_constant((int)i
);
282 ir_dereference
*deref_chan
=
283 new(mem_ctx
) ir_dereference_array(deref
->clone(mem_ctx
, NULL
),
286 ir_rvalue
*chan_offset
=
288 new(mem_ctx
) ir_constant(deref_offset
+ i
* matrix_stride
));
290 base_ir
->insert_before(assign(deref_chan
,
291 ubo_load(glsl_type::float_type
,
297 } /* unnamed namespace */
300 lower_ubo_reference(struct gl_shader
*shader
, exec_list
*instructions
)
302 lower_ubo_reference_visitor
v(shader
);
304 /* Loop over the instructions lowering references, because we take
305 * a deref of a UBO array using a UBO dereference as the index will
306 * produce a collection of instructions all of which have cloned
307 * UBO dereferences for that array index.
311 visit_list_elements(&v
, instructions
);
312 } while (v
.progress
);