glsl: Correctly load columns of a row-major matrix
[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 ir_rvalue *uniform_block;
61 bool progress;
62 };
63
64 /**
65 * Determine the name of the interface block field
66 *
67 * This is the name of the specific member as it would appear in the
68 * \c gl_uniform_buffer_variable::Name field in the shader's
69 * \c UniformBlocks array.
70 */
71 static const char *
72 interface_field_name(void *mem_ctx, char *base_name, ir_dereference *d,
73 ir_rvalue **nonconst_block_index)
74 {
75 ir_rvalue *previous_index = NULL;
76 *nonconst_block_index = NULL;
77
78 while (d != NULL) {
79 switch (d->ir_type) {
80 case ir_type_dereference_variable: {
81 ir_dereference_variable *v = (ir_dereference_variable *) d;
82 if (previous_index
83 && v->var->is_interface_instance()
84 && v->var->type->is_array()) {
85
86 ir_constant *const_index = previous_index->as_constant();
87 if (!const_index) {
88 *nonconst_block_index = previous_index;
89 return ralloc_asprintf(mem_ctx, "%s[0]", base_name);
90 } else {
91 return ralloc_asprintf(mem_ctx,
92 "%s[%d]",
93 base_name,
94 const_index->get_uint_component(0));
95 }
96 } else {
97 return base_name;
98 }
99
100 break;
101 }
102
103 case ir_type_dereference_record: {
104 ir_dereference_record *r = (ir_dereference_record *) d;
105
106 d = r->record->as_dereference();
107 break;
108 }
109
110 case ir_type_dereference_array: {
111 ir_dereference_array *a = (ir_dereference_array *) d;
112
113 d = a->array->as_dereference();
114 previous_index = a->array_index;
115
116 break;
117 }
118
119 default:
120 assert(!"Should not get here.");
121 break;
122 }
123 }
124
125 assert(!"Should not get here.");
126 return NULL;
127 }
128
129 void
130 lower_ubo_reference_visitor::handle_rvalue(ir_rvalue **rvalue)
131 {
132 if (!*rvalue)
133 return;
134
135 ir_dereference *deref = (*rvalue)->as_dereference();
136 if (!deref)
137 return;
138
139 ir_variable *var = deref->variable_referenced();
140 if (!var || !var->is_in_uniform_block())
141 return;
142
143 mem_ctx = ralloc_parent(*rvalue);
144
145 ir_rvalue *nonconst_block_index;
146 const char *const field_name =
147 interface_field_name(mem_ctx, (char *) var->get_interface_type()->name,
148 deref, &nonconst_block_index);
149
150 this->uniform_block = NULL;
151 for (unsigned i = 0; i < shader->NumUniformBlocks; i++) {
152 if (strcmp(field_name, shader->UniformBlocks[i].Name) == 0) {
153
154 ir_constant *index = new(mem_ctx) ir_constant(i);
155
156 if (nonconst_block_index) {
157 if (nonconst_block_index->type != glsl_type::uint_type)
158 nonconst_block_index = i2u(nonconst_block_index);
159 this->uniform_block = add(nonconst_block_index, index);
160 } else {
161 this->uniform_block = index;
162 }
163
164 struct gl_uniform_block *block = &shader->UniformBlocks[i];
165
166 this->ubo_var = var->is_interface_instance()
167 ? &block->Uniforms[0] : &block->Uniforms[var->data.location];
168
169 break;
170 }
171 }
172
173 assert(this->uniform_block);
174
175 ir_rvalue *offset = new(mem_ctx) ir_constant(0u);
176 unsigned const_offset = 0;
177 bool row_major = ubo_var->RowMajor;
178
179 /* Calculate the offset to the start of the region of the UBO
180 * dereferenced by *rvalue. This may be a variable offset if an
181 * array dereference has a variable index.
182 */
183 while (deref) {
184 switch (deref->ir_type) {
185 case ir_type_dereference_variable: {
186 const_offset += ubo_var->Offset;
187 deref = NULL;
188 break;
189 }
190
191 case ir_type_dereference_array: {
192 ir_dereference_array *deref_array = (ir_dereference_array *)deref;
193 unsigned array_stride;
194 if (deref_array->array->type->is_matrix() && row_major) {
195 /* When loading a vector out of a row major matrix, the
196 * step between the columns (vectors) is the size of a
197 * float, while the step between the rows (elements of a
198 * vector) is handled below in emit_ubo_loads.
199 */
200 array_stride = 4;
201 } else if (deref_array->type->is_interface()) {
202 /* We're processing an array dereference of an interface instance
203 * array. The thing being dereferenced *must* be a variable
204 * dereference because intefaces cannot be embedded an other
205 * types. In terms of calculating the offsets for the lowering
206 * pass, we don't care about the array index. All elements of an
207 * interface instance array will have the same offsets relative to
208 * the base of the block that backs them.
209 */
210 assert(deref_array->array->as_dereference_variable());
211 deref = deref_array->array->as_dereference();
212 break;
213 } else {
214 array_stride = deref_array->type->std140_size(row_major);
215 array_stride = glsl_align(array_stride, 16);
216 }
217
218 ir_rvalue *array_index = deref_array->array_index;
219 if (array_index->type->base_type == GLSL_TYPE_INT)
220 array_index = i2u(array_index);
221
222 ir_constant *const_index =
223 array_index->constant_expression_value(NULL);
224 if (const_index) {
225 const_offset += array_stride * const_index->value.u[0];
226 } else {
227 offset = add(offset,
228 mul(array_index,
229 new(mem_ctx) ir_constant(array_stride)));
230 }
231 deref = deref_array->array->as_dereference();
232 break;
233 }
234
235 case ir_type_dereference_record: {
236 ir_dereference_record *deref_record = (ir_dereference_record *)deref;
237 const glsl_type *struct_type = deref_record->record->type;
238 unsigned intra_struct_offset = 0;
239
240 unsigned max_field_align = 16;
241 for (unsigned int i = 0; i < struct_type->length; i++) {
242 const glsl_type *type = struct_type->fields.structure[i].type;
243 unsigned field_align = type->std140_base_alignment(row_major);
244 max_field_align = MAX2(field_align, max_field_align);
245 intra_struct_offset = glsl_align(intra_struct_offset, field_align);
246
247 if (strcmp(struct_type->fields.structure[i].name,
248 deref_record->field) == 0)
249 break;
250 intra_struct_offset += type->std140_size(row_major);
251 }
252
253 const_offset = glsl_align(const_offset, max_field_align);
254 const_offset += intra_struct_offset;
255
256 deref = deref_record->record->as_dereference();
257 break;
258 }
259 default:
260 assert(!"not reached");
261 deref = NULL;
262 break;
263 }
264 }
265
266 /* Now that we've calculated the offset to the start of the
267 * dereference, walk over the type and emit loads into a temporary.
268 */
269 const glsl_type *type = (*rvalue)->type;
270 ir_variable *load_var = new(mem_ctx) ir_variable(type,
271 "ubo_load_temp",
272 ir_var_temporary);
273 base_ir->insert_before(load_var);
274
275 ir_variable *load_offset = new(mem_ctx) ir_variable(glsl_type::uint_type,
276 "ubo_load_temp_offset",
277 ir_var_temporary);
278 base_ir->insert_before(load_offset);
279 base_ir->insert_before(assign(load_offset, offset));
280
281 deref = new(mem_ctx) ir_dereference_variable(load_var);
282 emit_ubo_loads(deref, load_offset, const_offset);
283 *rvalue = deref;
284
285 progress = true;
286 }
287
288 ir_expression *
289 lower_ubo_reference_visitor::ubo_load(const glsl_type *type,
290 ir_rvalue *offset)
291 {
292 ir_rvalue *block_ref = this->uniform_block->clone(mem_ctx, NULL);
293 return new(mem_ctx)
294 ir_expression(ir_binop_ubo_load,
295 type,
296 block_ref,
297 offset);
298
299 }
300
301 /**
302 * Takes LHS and emits a series of assignments into its components
303 * from the UBO variable at variable_offset + deref_offset.
304 *
305 * Recursively calls itself to break the deref down to the point that
306 * the ir_binop_ubo_load expressions generated are contiguous scalars
307 * or vectors.
308 */
309 void
310 lower_ubo_reference_visitor::emit_ubo_loads(ir_dereference *deref,
311 ir_variable *base_offset,
312 unsigned int deref_offset)
313 {
314 if (deref->type->is_record()) {
315 unsigned int field_offset = 0;
316
317 for (unsigned i = 0; i < deref->type->length; i++) {
318 const struct glsl_struct_field *field =
319 &deref->type->fields.structure[i];
320 ir_dereference *field_deref =
321 new(mem_ctx) ir_dereference_record(deref->clone(mem_ctx, NULL),
322 field->name);
323
324 field_offset =
325 glsl_align(field_offset,
326 field->type->std140_base_alignment(ubo_var->RowMajor));
327
328 emit_ubo_loads(field_deref, base_offset, deref_offset + field_offset);
329
330 field_offset += field->type->std140_size(ubo_var->RowMajor);
331 }
332 return;
333 }
334
335 if (deref->type->is_array()) {
336 unsigned array_stride =
337 glsl_align(deref->type->fields.array->std140_size(ubo_var->RowMajor),
338 16);
339
340 for (unsigned i = 0; i < deref->type->length; i++) {
341 ir_constant *element = new(mem_ctx) ir_constant(i);
342 ir_dereference *element_deref =
343 new(mem_ctx) ir_dereference_array(deref->clone(mem_ctx, NULL),
344 element);
345 emit_ubo_loads(element_deref, base_offset,
346 deref_offset + i * array_stride);
347 }
348 return;
349 }
350
351 if (deref->type->is_matrix()) {
352 for (unsigned i = 0; i < deref->type->matrix_columns; i++) {
353 ir_constant *col = new(mem_ctx) ir_constant(i);
354 ir_dereference *col_deref =
355 new(mem_ctx) ir_dereference_array(deref->clone(mem_ctx, NULL),
356 col);
357
358 if (ubo_var->RowMajor) {
359 /* For a row-major matrix, the next column starts at the next
360 * element.
361 */
362 emit_ubo_loads(col_deref, base_offset, deref_offset + i * 4);
363 } else {
364 /* std140 always rounds the stride of arrays (and matrices) to a
365 * vec4, so matrices are always 16 between columns/rows.
366 */
367 emit_ubo_loads(col_deref, base_offset, deref_offset + i * 16);
368 }
369 }
370 return;
371 }
372
373 assert(deref->type->is_scalar() ||
374 deref->type->is_vector());
375
376 if (!ubo_var->RowMajor) {
377 ir_rvalue *offset = add(base_offset,
378 new(mem_ctx) ir_constant(deref_offset));
379 base_ir->insert_before(assign(deref->clone(mem_ctx, NULL),
380 ubo_load(deref->type, offset)));
381 } else {
382 /* We're dereffing a column out of a row-major matrix, so we
383 * gather the vector from each stored row.
384 */
385 assert(deref->type->base_type == GLSL_TYPE_FLOAT);
386 /* Matrices, row_major or not, are stored as if they were
387 * arrays of vectors of the appropriate size in std140.
388 * Arrays have their strides rounded up to a vec4, so the
389 * matrix stride is always 16.
390 */
391 unsigned matrix_stride = 16;
392
393 for (unsigned i = 0; i < deref->type->vector_elements; i++) {
394 ir_rvalue *chan_offset =
395 add(base_offset,
396 new(mem_ctx) ir_constant(deref_offset + i * matrix_stride));
397
398 base_ir->insert_before(assign(deref->clone(mem_ctx, NULL),
399 ubo_load(glsl_type::float_type,
400 chan_offset),
401 (1U << i)));
402 }
403 }
404 }
405
406 } /* unnamed namespace */
407
408 void
409 lower_ubo_reference(struct gl_shader *shader, exec_list *instructions)
410 {
411 lower_ubo_reference_visitor v(shader);
412
413 /* Loop over the instructions lowering references, because we take
414 * a deref of a UBO array using a UBO dereference as the index will
415 * produce a collection of instructions all of which have cloned
416 * UBO dereferences for that array index.
417 */
418 do {
419 v.progress = false;
420 visit_list_elements(&v, instructions);
421 } while (v.progress);
422 }