8d13ec181923c8b46d5e09526212863d1340065d
[mesa.git] / src / glsl / lower_ubo_reference.cpp
1 /*
2 * Copyright © 2012 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 /**
25 * \file lower_ubo_reference.cpp
26 *
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.
30 *
31 * This relieves drivers of the responsibility to deal with tricky UBO
32 * layout issues like std140 structures and row_major matrices on
33 * their own.
34 */
35
36 #include "ir.h"
37 #include "ir_builder.h"
38 #include "ir_rvalue_visitor.h"
39 #include "main/macros.h"
40
41 using namespace ir_builder;
42
43 namespace {
44 class lower_ubo_reference_visitor : public ir_rvalue_enter_visitor {
45 public:
46 lower_ubo_reference_visitor(struct gl_shader *shader)
47 : shader(shader)
48 {
49 }
50
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,
55 ir_rvalue *offset);
56
57 void *mem_ctx;
58 struct gl_shader *shader;
59 struct gl_uniform_buffer_variable *ubo_var;
60 unsigned uniform_block;
61 bool progress;
62 };
63
64 void
65 lower_ubo_reference_visitor::handle_rvalue(ir_rvalue **rvalue)
66 {
67 if (!*rvalue)
68 return;
69
70 ir_dereference *deref = (*rvalue)->as_dereference();
71 if (!deref)
72 return;
73
74 ir_variable *var = deref->variable_referenced();
75 if (!var || !var->is_in_uniform_block())
76 return;
77
78 mem_ctx = ralloc_parent(*rvalue);
79 uniform_block = var->uniform_block;
80 struct gl_uniform_block *block = &shader->UniformBlocks[uniform_block];
81 this->ubo_var = &block->Uniforms[var->location];
82 ir_rvalue *offset = new(mem_ctx) ir_constant(0u);
83 unsigned const_offset = 0;
84 bool row_major = ubo_var->RowMajor;
85
86 /* Calculate the offset to the start of the region of the UBO
87 * dereferenced by *rvalue. This may be a variable offset if an
88 * array dereference has a variable index.
89 */
90 while (deref) {
91 switch (deref->ir_type) {
92 case ir_type_dereference_variable: {
93 const_offset += ubo_var->Offset;
94 deref = NULL;
95 break;
96 }
97
98 case ir_type_dereference_array: {
99 ir_dereference_array *deref_array = (ir_dereference_array *)deref;
100 unsigned array_stride;
101 if (deref_array->array->type->is_matrix() && row_major) {
102 /* When loading a vector out of a row major matrix, the
103 * step between the columns (vectors) is the size of a
104 * float, while the step between the rows (elements of a
105 * vector) is handled below in emit_ubo_loads.
106 */
107 array_stride = 4;
108 } else {
109 array_stride = deref_array->type->std140_size(row_major);
110 array_stride = glsl_align(array_stride, 16);
111 }
112
113 ir_constant *const_index = deref_array->array_index->as_constant();
114 if (const_index) {
115 const_offset += array_stride * const_index->value.i[0];
116 } else {
117 offset = add(offset,
118 mul(deref_array->array_index,
119 new(mem_ctx) ir_constant(array_stride)));
120 }
121 deref = deref_array->array->as_dereference();
122 break;
123 }
124
125 case ir_type_dereference_record: {
126 ir_dereference_record *deref_record = (ir_dereference_record *)deref;
127 const glsl_type *struct_type = deref_record->record->type;
128 unsigned intra_struct_offset = 0;
129
130 unsigned max_field_align = 16;
131 for (unsigned int i = 0; i < struct_type->length; i++) {
132 const glsl_type *type = struct_type->fields.structure[i].type;
133 unsigned field_align = type->std140_base_alignment(row_major);
134 max_field_align = MAX2(field_align, max_field_align);
135 intra_struct_offset = glsl_align(intra_struct_offset, field_align);
136
137 if (strcmp(struct_type->fields.structure[i].name,
138 deref_record->field) == 0)
139 break;
140 intra_struct_offset += type->std140_size(row_major);
141 }
142
143 const_offset = glsl_align(const_offset, max_field_align);
144 const_offset += intra_struct_offset;
145
146 deref = deref_record->record->as_dereference();
147 break;
148 }
149 default:
150 assert(!"not reached");
151 deref = NULL;
152 break;
153 }
154 }
155
156 /* Now that we've calculated the offset to the start of the
157 * dereference, walk over the type and emit loads into a temporary.
158 */
159 const glsl_type *type = (*rvalue)->type;
160 ir_variable *load_var = new(mem_ctx) ir_variable(type,
161 "ubo_load_temp",
162 ir_var_temporary);
163 base_ir->insert_before(load_var);
164
165 ir_variable *load_offset = new(mem_ctx) ir_variable(glsl_type::uint_type,
166 "ubo_load_temp_offset",
167 ir_var_temporary);
168 base_ir->insert_before(load_offset);
169 base_ir->insert_before(assign(load_offset, offset));
170
171 deref = new(mem_ctx) ir_dereference_variable(load_var);
172 emit_ubo_loads(deref, load_offset, const_offset);
173 *rvalue = deref;
174
175 progress = true;
176 }
177
178 ir_expression *
179 lower_ubo_reference_visitor::ubo_load(const glsl_type *type,
180 ir_rvalue *offset)
181 {
182 return new(mem_ctx)
183 ir_expression(ir_binop_ubo_load,
184 type,
185 new(mem_ctx) ir_constant(this->uniform_block),
186 offset);
187
188 }
189
190 /**
191 * Takes LHS and emits a series of assignments into its components
192 * from the UBO variable at variable_offset + deref_offset.
193 *
194 * Recursively calls itself to break the deref down to the point that
195 * the ir_binop_ubo_load expressions generated are contiguous scalars
196 * or vectors.
197 */
198 void
199 lower_ubo_reference_visitor::emit_ubo_loads(ir_dereference *deref,
200 ir_variable *base_offset,
201 unsigned int deref_offset)
202 {
203 if (deref->type->is_record()) {
204 unsigned int field_offset = 0;
205
206 for (unsigned i = 0; i < deref->type->length; i++) {
207 const struct glsl_struct_field *field =
208 &deref->type->fields.structure[i];
209 ir_dereference *field_deref =
210 new(mem_ctx) ir_dereference_record(deref->clone(mem_ctx, NULL),
211 field->name);
212
213 field_offset =
214 glsl_align(field_offset,
215 field->type->std140_base_alignment(ubo_var->RowMajor));
216
217 emit_ubo_loads(field_deref, base_offset, deref_offset + field_offset);
218
219 field_offset += field->type->std140_size(ubo_var->RowMajor);
220 }
221 return;
222 }
223
224 if (deref->type->is_array()) {
225 unsigned array_stride =
226 glsl_align(deref->type->fields.array->std140_size(ubo_var->RowMajor),
227 16);
228
229 for (unsigned i = 0; i < deref->type->length; i++) {
230 ir_constant *element = new(mem_ctx) ir_constant(i);
231 ir_dereference *element_deref =
232 new(mem_ctx) ir_dereference_array(deref->clone(mem_ctx, NULL),
233 element);
234 emit_ubo_loads(element_deref, base_offset,
235 deref_offset + i * array_stride);
236 }
237 return;
238 }
239
240 if (deref->type->is_matrix()) {
241 for (unsigned i = 0; i < deref->type->matrix_columns; i++) {
242 ir_constant *col = new(mem_ctx) ir_constant(i);
243 ir_dereference *col_deref =
244 new(mem_ctx) ir_dereference_array(deref->clone(mem_ctx, NULL),
245 col);
246
247 /* std140 always rounds the stride of arrays (and matrices)
248 * to a vec4, so matrices are always 16 between columns/rows.
249 */
250 emit_ubo_loads(col_deref, base_offset, deref_offset + i * 16);
251 }
252 return;
253 }
254
255 assert(deref->type->is_scalar() ||
256 deref->type->is_vector());
257
258 if (!ubo_var->RowMajor) {
259 ir_rvalue *offset = add(base_offset,
260 new(mem_ctx) ir_constant(deref_offset));
261 base_ir->insert_before(assign(deref->clone(mem_ctx, NULL),
262 ubo_load(deref->type, offset)));
263 } else {
264 /* We're dereffing a column out of a row-major matrix, so we
265 * gather the vector from each stored row.
266 */
267 assert(deref->type->base_type == GLSL_TYPE_FLOAT);
268 /* Matrices, row_major or not, are stored as if they were
269 * arrays of vectors of the appropriate size in std140.
270 * Arrays have their strides rounded up to a vec4, so the
271 * matrix stride is always 16.
272 */
273 unsigned matrix_stride = 16;
274
275 for (unsigned i = 0; i < deref->type->vector_elements; i++) {
276 ir_rvalue *chan = new(mem_ctx) ir_constant((int)i);
277 ir_dereference *deref_chan =
278 new(mem_ctx) ir_dereference_array(deref->clone(mem_ctx, NULL),
279 chan);
280
281 ir_rvalue *chan_offset =
282 add(base_offset,
283 new(mem_ctx) ir_constant(deref_offset + i * matrix_stride));
284
285 base_ir->insert_before(assign(deref_chan,
286 ubo_load(glsl_type::float_type,
287 chan_offset)));
288 }
289 }
290 }
291
292 } /* unnamed namespace */
293
294 void
295 lower_ubo_reference(struct gl_shader *shader, exec_list *instructions)
296 {
297 lower_ubo_reference_visitor v(shader);
298
299 /* Loop over the instructions lowering references, because we take
300 * a deref of a UBO array using a UBO dereference as the index will
301 * produce a collection of instructions all of which have cloned
302 * UBO dereferences for that array index.
303 */
304 do {
305 v.progress = false;
306 visit_list_elements(&v, instructions);
307 } while (v.progress);
308 }