glsl/lower_ubo_reference: lower UBOs and SSBOs to separate index spaces
[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 #include "glsl_parser_extras.h"
41
42 using namespace ir_builder;
43
44 /**
45 * Determine if a thing being dereferenced is row-major
46 *
47 * There is some trickery here.
48 *
49 * If the thing being dereferenced is a member of uniform block \b without an
50 * instance name, then the name of the \c ir_variable is the field name of an
51 * interface type. If this field is row-major, then the thing referenced is
52 * row-major.
53 *
54 * If the thing being dereferenced is a member of uniform block \b with an
55 * instance name, then the last dereference in the tree will be an
56 * \c ir_dereference_record. If that record field is row-major, then the
57 * thing referenced is row-major.
58 */
59 static bool
60 is_dereferenced_thing_row_major(const ir_rvalue *deref)
61 {
62 bool matrix = false;
63 const ir_rvalue *ir = deref;
64
65 while (true) {
66 matrix = matrix || ir->type->without_array()->is_matrix();
67
68 switch (ir->ir_type) {
69 case ir_type_dereference_array: {
70 const ir_dereference_array *const array_deref =
71 (const ir_dereference_array *) ir;
72
73 ir = array_deref->array;
74 break;
75 }
76
77 case ir_type_dereference_record: {
78 const ir_dereference_record *const record_deref =
79 (const ir_dereference_record *) ir;
80
81 ir = record_deref->record;
82
83 const int idx = ir->type->field_index(record_deref->field);
84 assert(idx >= 0);
85
86 const enum glsl_matrix_layout matrix_layout =
87 glsl_matrix_layout(ir->type->fields.structure[idx].matrix_layout);
88
89 switch (matrix_layout) {
90 case GLSL_MATRIX_LAYOUT_INHERITED:
91 break;
92 case GLSL_MATRIX_LAYOUT_COLUMN_MAJOR:
93 return false;
94 case GLSL_MATRIX_LAYOUT_ROW_MAJOR:
95 return matrix || deref->type->without_array()->is_record();
96 }
97
98 break;
99 }
100
101 case ir_type_dereference_variable: {
102 const ir_dereference_variable *const var_deref =
103 (const ir_dereference_variable *) ir;
104
105 const enum glsl_matrix_layout matrix_layout =
106 glsl_matrix_layout(var_deref->var->data.matrix_layout);
107
108 switch (matrix_layout) {
109 case GLSL_MATRIX_LAYOUT_INHERITED:
110 assert(!matrix);
111 return false;
112 case GLSL_MATRIX_LAYOUT_COLUMN_MAJOR:
113 return false;
114 case GLSL_MATRIX_LAYOUT_ROW_MAJOR:
115 return matrix || deref->type->without_array()->is_record();
116 }
117
118 unreachable("invalid matrix layout");
119 break;
120 }
121
122 default:
123 return false;
124 }
125 }
126
127 /* The tree must have ended with a dereference that wasn't an
128 * ir_dereference_variable. That is invalid, and it should be impossible.
129 */
130 unreachable("invalid dereference tree");
131 return false;
132 }
133
134 namespace {
135 class lower_ubo_reference_visitor : public ir_rvalue_enter_visitor {
136 public:
137 lower_ubo_reference_visitor(struct gl_shader *shader)
138 : shader(shader)
139 {
140 }
141
142 void handle_rvalue(ir_rvalue **rvalue);
143 ir_visitor_status visit_enter(ir_assignment *ir);
144
145 void setup_for_load_or_store(ir_variable *var,
146 ir_rvalue *deref,
147 ir_rvalue **offset,
148 unsigned *const_offset,
149 bool *row_major,
150 int *matrix_columns,
151 unsigned packing);
152 ir_expression *ubo_load(const struct glsl_type *type,
153 ir_rvalue *offset);
154 ir_call *ssbo_load(const struct glsl_type *type,
155 ir_rvalue *offset);
156
157 void check_for_ssbo_store(ir_assignment *ir);
158 void write_to_memory(ir_dereference *deref,
159 ir_variable *var,
160 ir_variable *write_var,
161 unsigned write_mask);
162 ir_call *ssbo_store(ir_rvalue *deref, ir_rvalue *offset,
163 unsigned write_mask);
164
165 void emit_access(bool is_write, ir_dereference *deref,
166 ir_variable *base_offset, unsigned int deref_offset,
167 bool row_major, int matrix_columns,
168 unsigned packing, unsigned write_mask);
169
170 ir_visitor_status visit_enter(class ir_expression *);
171 ir_expression *calculate_ssbo_unsized_array_length(ir_expression *expr);
172 void check_ssbo_unsized_array_length_expression(class ir_expression *);
173 void check_ssbo_unsized_array_length_assignment(ir_assignment *ir);
174
175 ir_expression *process_ssbo_unsized_array_length(ir_rvalue **,
176 ir_dereference *,
177 ir_variable *);
178 ir_expression *emit_ssbo_get_buffer_size();
179
180 unsigned calculate_unsized_array_stride(ir_dereference *deref,
181 unsigned packing);
182
183 ir_call *lower_ssbo_atomic_intrinsic(ir_call *ir);
184 ir_call *check_for_ssbo_atomic_intrinsic(ir_call *ir);
185 ir_visitor_status visit_enter(ir_call *ir);
186
187 void *mem_ctx;
188 struct gl_shader *shader;
189 struct gl_uniform_buffer_variable *ubo_var;
190 ir_rvalue *uniform_block;
191 bool progress;
192 bool is_shader_storage;
193 };
194
195 /**
196 * Determine the name of the interface block field
197 *
198 * This is the name of the specific member as it would appear in the
199 * \c gl_uniform_buffer_variable::Name field in the shader's
200 * \c UniformBlocks array.
201 */
202 static const char *
203 interface_field_name(void *mem_ctx, char *base_name, ir_rvalue *d,
204 ir_rvalue **nonconst_block_index)
205 {
206 ir_rvalue *previous_index = NULL;
207 *nonconst_block_index = NULL;
208
209 while (d != NULL) {
210 switch (d->ir_type) {
211 case ir_type_dereference_variable: {
212 ir_dereference_variable *v = (ir_dereference_variable *) d;
213 if (previous_index
214 && v->var->is_interface_instance()
215 && v->var->type->is_array()) {
216
217 ir_constant *const_index = previous_index->as_constant();
218 if (!const_index) {
219 *nonconst_block_index = previous_index;
220 return ralloc_asprintf(mem_ctx, "%s[0]", base_name);
221 } else {
222 return ralloc_asprintf(mem_ctx,
223 "%s[%d]",
224 base_name,
225 const_index->get_uint_component(0));
226 }
227 } else {
228 return base_name;
229 }
230
231 break;
232 }
233
234 case ir_type_dereference_record: {
235 ir_dereference_record *r = (ir_dereference_record *) d;
236
237 d = r->record->as_dereference();
238 break;
239 }
240
241 case ir_type_dereference_array: {
242 ir_dereference_array *a = (ir_dereference_array *) d;
243
244 d = a->array->as_dereference();
245 previous_index = a->array_index;
246
247 break;
248 }
249 case ir_type_swizzle: {
250 ir_swizzle *s = (ir_swizzle *) d;
251
252 d = s->val->as_dereference();
253 break;
254 }
255 default:
256 assert(!"Should not get here.");
257 break;
258 }
259 }
260
261 assert(!"Should not get here.");
262 return NULL;
263 }
264
265 void
266 lower_ubo_reference_visitor::setup_for_load_or_store(ir_variable *var,
267 ir_rvalue *deref,
268 ir_rvalue **offset,
269 unsigned *const_offset,
270 bool *row_major,
271 int *matrix_columns,
272 unsigned packing)
273 {
274 /* Determine the name of the interface block */
275 ir_rvalue *nonconst_block_index;
276 const char *const field_name =
277 interface_field_name(mem_ctx, (char *) var->get_interface_type()->name,
278 deref, &nonconst_block_index);
279
280 /* Locate the block by interface name */
281 this->is_shader_storage = var->is_in_shader_storage_block();
282 unsigned num_blocks;
283 struct gl_uniform_block **blocks;
284 if (this->is_shader_storage) {
285 num_blocks = shader->NumShaderStorageBlocks;
286 blocks = shader->ShaderStorageBlocks;
287 } else {
288 num_blocks = shader->NumUniformBlocks;
289 blocks = shader->UniformBlocks;
290 }
291 this->uniform_block = NULL;
292 for (unsigned i = 0; i < num_blocks; i++) {
293 if (strcmp(field_name, blocks[i]->Name) == 0) {
294
295 ir_constant *index = new(mem_ctx) ir_constant(i);
296
297 if (nonconst_block_index) {
298 if (nonconst_block_index->type != glsl_type::uint_type)
299 nonconst_block_index = i2u(nonconst_block_index);
300 this->uniform_block = add(nonconst_block_index, index);
301 } else {
302 this->uniform_block = index;
303 }
304
305 this->ubo_var = var->is_interface_instance()
306 ? &blocks[i]->Uniforms[0] : &blocks[i]->Uniforms[var->data.location];
307
308 break;
309 }
310 }
311
312 assert(this->uniform_block);
313
314 *offset = new(mem_ctx) ir_constant(0u);
315 *const_offset = 0;
316 *row_major = is_dereferenced_thing_row_major(deref);
317 *matrix_columns = 1;
318
319 /* Calculate the offset to the start of the region of the UBO
320 * dereferenced by *rvalue. This may be a variable offset if an
321 * array dereference has a variable index.
322 */
323 while (deref) {
324 switch (deref->ir_type) {
325 case ir_type_dereference_variable: {
326 *const_offset += ubo_var->Offset;
327 deref = NULL;
328 break;
329 }
330
331 case ir_type_dereference_array: {
332 ir_dereference_array *deref_array = (ir_dereference_array *) deref;
333 unsigned array_stride;
334 if (deref_array->array->type->is_matrix() && *row_major) {
335 /* When loading a vector out of a row major matrix, the
336 * step between the columns (vectors) is the size of a
337 * float, while the step between the rows (elements of a
338 * vector) is handled below in emit_ubo_loads.
339 */
340 array_stride = 4;
341 if (deref_array->array->type->is_double())
342 array_stride *= 2;
343 *matrix_columns = deref_array->array->type->matrix_columns;
344 } else if (deref_array->type->is_interface()) {
345 /* We're processing an array dereference of an interface instance
346 * array. The thing being dereferenced *must* be a variable
347 * dereference because interfaces cannot be embedded in other
348 * types. In terms of calculating the offsets for the lowering
349 * pass, we don't care about the array index. All elements of an
350 * interface instance array will have the same offsets relative to
351 * the base of the block that backs them.
352 */
353 assert(deref_array->array->as_dereference_variable());
354 deref = deref_array->array->as_dereference();
355 break;
356 } else {
357 /* Whether or not the field is row-major (because it might be a
358 * bvec2 or something) does not affect the array itself. We need
359 * to know whether an array element in its entirety is row-major.
360 */
361 const bool array_row_major =
362 is_dereferenced_thing_row_major(deref_array);
363
364 /* The array type will give the correct interface packing
365 * information
366 */
367 if (packing == GLSL_INTERFACE_PACKING_STD430) {
368 array_stride = deref_array->type->std430_array_stride(array_row_major);
369 } else {
370 array_stride = deref_array->type->std140_size(array_row_major);
371 array_stride = glsl_align(array_stride, 16);
372 }
373 }
374
375 ir_rvalue *array_index = deref_array->array_index;
376 if (array_index->type->base_type == GLSL_TYPE_INT)
377 array_index = i2u(array_index);
378
379 ir_constant *const_index =
380 array_index->constant_expression_value(NULL);
381 if (const_index) {
382 *const_offset += array_stride * const_index->value.u[0];
383 } else {
384 *offset = add(*offset,
385 mul(array_index,
386 new(mem_ctx) ir_constant(array_stride)));
387 }
388 deref = deref_array->array->as_dereference();
389 break;
390 }
391
392 case ir_type_dereference_record: {
393 ir_dereference_record *deref_record = (ir_dereference_record *) deref;
394 const glsl_type *struct_type = deref_record->record->type;
395 unsigned intra_struct_offset = 0;
396
397 for (unsigned int i = 0; i < struct_type->length; i++) {
398 const glsl_type *type = struct_type->fields.structure[i].type;
399
400 ir_dereference_record *field_deref = new(mem_ctx)
401 ir_dereference_record(deref_record->record,
402 struct_type->fields.structure[i].name);
403 const bool field_row_major =
404 is_dereferenced_thing_row_major(field_deref);
405
406 ralloc_free(field_deref);
407
408 unsigned field_align = 0;
409
410 if (packing == GLSL_INTERFACE_PACKING_STD430)
411 field_align = type->std430_base_alignment(field_row_major);
412 else
413 field_align = type->std140_base_alignment(field_row_major);
414
415 intra_struct_offset = glsl_align(intra_struct_offset, field_align);
416
417 if (strcmp(struct_type->fields.structure[i].name,
418 deref_record->field) == 0)
419 break;
420
421 if (packing == GLSL_INTERFACE_PACKING_STD430)
422 intra_struct_offset += type->std430_size(field_row_major);
423 else
424 intra_struct_offset += type->std140_size(field_row_major);
425
426 /* If the field just examined was itself a structure, apply rule
427 * #9:
428 *
429 * "The structure may have padding at the end; the base offset
430 * of the member following the sub-structure is rounded up to
431 * the next multiple of the base alignment of the structure."
432 */
433 if (type->without_array()->is_record()) {
434 intra_struct_offset = glsl_align(intra_struct_offset,
435 field_align);
436
437 }
438 }
439
440 *const_offset += intra_struct_offset;
441 deref = deref_record->record->as_dereference();
442 break;
443 }
444
445 case ir_type_swizzle: {
446 ir_swizzle *deref_swizzle = (ir_swizzle *) deref;
447
448 assert(deref_swizzle->mask.num_components == 1);
449
450 *const_offset += deref_swizzle->mask.x * sizeof(int);
451 deref = deref_swizzle->val->as_dereference();
452 break;
453 }
454
455 default:
456 assert(!"not reached");
457 deref = NULL;
458 break;
459 }
460 }
461 }
462
463 void
464 lower_ubo_reference_visitor::handle_rvalue(ir_rvalue **rvalue)
465 {
466 if (!*rvalue)
467 return;
468
469 ir_dereference *deref = (*rvalue)->as_dereference();
470 if (!deref)
471 return;
472
473 ir_variable *var = deref->variable_referenced();
474 if (!var || !var->is_in_buffer_block())
475 return;
476
477 mem_ctx = ralloc_parent(shader->ir);
478
479 ir_rvalue *offset = NULL;
480 unsigned const_offset;
481 bool row_major;
482 int matrix_columns;
483 unsigned packing = var->get_interface_type()->interface_packing;
484
485 /* Compute the offset to the start if the dereference as well as other
486 * information we need to configure the write
487 */
488 setup_for_load_or_store(var, deref,
489 &offset, &const_offset,
490 &row_major, &matrix_columns,
491 packing);
492 assert(offset);
493
494 /* Now that we've calculated the offset to the start of the
495 * dereference, walk over the type and emit loads into a temporary.
496 */
497 const glsl_type *type = (*rvalue)->type;
498 ir_variable *load_var = new(mem_ctx) ir_variable(type,
499 "ubo_load_temp",
500 ir_var_temporary);
501 base_ir->insert_before(load_var);
502
503 ir_variable *load_offset = new(mem_ctx) ir_variable(glsl_type::uint_type,
504 "ubo_load_temp_offset",
505 ir_var_temporary);
506 base_ir->insert_before(load_offset);
507 base_ir->insert_before(assign(load_offset, offset));
508
509 deref = new(mem_ctx) ir_dereference_variable(load_var);
510 emit_access(false, deref, load_offset, const_offset,
511 row_major, matrix_columns, packing, 0);
512 *rvalue = deref;
513
514 progress = true;
515 }
516
517 ir_expression *
518 lower_ubo_reference_visitor::ubo_load(const glsl_type *type,
519 ir_rvalue *offset)
520 {
521 ir_rvalue *block_ref = this->uniform_block->clone(mem_ctx, NULL);
522 return new(mem_ctx)
523 ir_expression(ir_binop_ubo_load,
524 type,
525 block_ref,
526 offset);
527
528 }
529
530 static bool
531 shader_storage_buffer_object(const _mesa_glsl_parse_state *state)
532 {
533 return state->ARB_shader_storage_buffer_object_enable;
534 }
535
536 ir_call *
537 lower_ubo_reference_visitor::ssbo_store(ir_rvalue *deref,
538 ir_rvalue *offset,
539 unsigned write_mask)
540 {
541 exec_list sig_params;
542
543 ir_variable *block_ref = new(mem_ctx)
544 ir_variable(glsl_type::uint_type, "block_ref" , ir_var_function_in);
545 sig_params.push_tail(block_ref);
546
547 ir_variable *offset_ref = new(mem_ctx)
548 ir_variable(glsl_type::uint_type, "offset" , ir_var_function_in);
549 sig_params.push_tail(offset_ref);
550
551 ir_variable *val_ref = new(mem_ctx)
552 ir_variable(deref->type, "value" , ir_var_function_in);
553 sig_params.push_tail(val_ref);
554
555 ir_variable *writemask_ref = new(mem_ctx)
556 ir_variable(glsl_type::uint_type, "write_mask" , ir_var_function_in);
557 sig_params.push_tail(writemask_ref);
558
559 ir_function_signature *sig = new(mem_ctx)
560 ir_function_signature(glsl_type::void_type, shader_storage_buffer_object);
561 assert(sig);
562 sig->replace_parameters(&sig_params);
563 sig->is_intrinsic = true;
564
565 ir_function *f = new(mem_ctx) ir_function("__intrinsic_store_ssbo");
566 f->add_signature(sig);
567
568 exec_list call_params;
569 call_params.push_tail(this->uniform_block->clone(mem_ctx, NULL));
570 call_params.push_tail(offset->clone(mem_ctx, NULL));
571 call_params.push_tail(deref->clone(mem_ctx, NULL));
572 call_params.push_tail(new(mem_ctx) ir_constant(write_mask));
573 return new(mem_ctx) ir_call(sig, NULL, &call_params);
574 }
575
576 ir_call *
577 lower_ubo_reference_visitor::ssbo_load(const struct glsl_type *type,
578 ir_rvalue *offset)
579 {
580 exec_list sig_params;
581
582 ir_variable *block_ref = new(mem_ctx)
583 ir_variable(glsl_type::uint_type, "block_ref" , ir_var_function_in);
584 sig_params.push_tail(block_ref);
585
586 ir_variable *offset_ref = new(mem_ctx)
587 ir_variable(glsl_type::uint_type, "offset_ref" , ir_var_function_in);
588 sig_params.push_tail(offset_ref);
589
590 ir_function_signature *sig =
591 new(mem_ctx) ir_function_signature(type, shader_storage_buffer_object);
592 assert(sig);
593 sig->replace_parameters(&sig_params);
594 sig->is_intrinsic = true;
595
596 ir_function *f = new(mem_ctx) ir_function("__intrinsic_load_ssbo");
597 f->add_signature(sig);
598
599 ir_variable *result = new(mem_ctx)
600 ir_variable(type, "ssbo_load_result", ir_var_temporary);
601 base_ir->insert_before(result);
602 ir_dereference_variable *deref_result = new(mem_ctx)
603 ir_dereference_variable(result);
604
605 exec_list call_params;
606 call_params.push_tail(this->uniform_block->clone(mem_ctx, NULL));
607 call_params.push_tail(offset->clone(mem_ctx, NULL));
608
609 return new(mem_ctx) ir_call(sig, deref_result, &call_params);
610 }
611
612 static inline int
613 writemask_for_size(unsigned n)
614 {
615 return ((1 << n) - 1);
616 }
617
618 /**
619 * Takes a deref and recursively calls itself to break the deref down to the
620 * point that the reads or writes generated are contiguous scalars or vectors.
621 */
622 void
623 lower_ubo_reference_visitor::emit_access(bool is_write,
624 ir_dereference *deref,
625 ir_variable *base_offset,
626 unsigned int deref_offset,
627 bool row_major,
628 int matrix_columns,
629 unsigned packing,
630 unsigned write_mask)
631 {
632 if (deref->type->is_record()) {
633 unsigned int field_offset = 0;
634
635 for (unsigned i = 0; i < deref->type->length; i++) {
636 const struct glsl_struct_field *field =
637 &deref->type->fields.structure[i];
638 ir_dereference *field_deref =
639 new(mem_ctx) ir_dereference_record(deref->clone(mem_ctx, NULL),
640 field->name);
641
642 field_offset =
643 glsl_align(field_offset,
644 field->type->std140_base_alignment(row_major));
645
646 emit_access(is_write, field_deref, base_offset,
647 deref_offset + field_offset,
648 row_major, 1, packing,
649 writemask_for_size(field_deref->type->vector_elements));
650
651 field_offset += field->type->std140_size(row_major);
652 }
653 return;
654 }
655
656 if (deref->type->is_array()) {
657 unsigned array_stride = packing == GLSL_INTERFACE_PACKING_STD430 ?
658 deref->type->fields.array->std430_array_stride(row_major) :
659 glsl_align(deref->type->fields.array->std140_size(row_major), 16);
660
661 for (unsigned i = 0; i < deref->type->length; i++) {
662 ir_constant *element = new(mem_ctx) ir_constant(i);
663 ir_dereference *element_deref =
664 new(mem_ctx) ir_dereference_array(deref->clone(mem_ctx, NULL),
665 element);
666 emit_access(is_write, element_deref, base_offset,
667 deref_offset + i * array_stride,
668 row_major, 1, packing,
669 writemask_for_size(element_deref->type->vector_elements));
670 }
671 return;
672 }
673
674 if (deref->type->is_matrix()) {
675 for (unsigned i = 0; i < deref->type->matrix_columns; i++) {
676 ir_constant *col = new(mem_ctx) ir_constant(i);
677 ir_dereference *col_deref =
678 new(mem_ctx) ir_dereference_array(deref->clone(mem_ctx, NULL), col);
679
680 if (row_major) {
681 /* For a row-major matrix, the next column starts at the next
682 * element.
683 */
684 int size_mul = deref->type->is_double() ? 8 : 4;
685 emit_access(is_write, col_deref, base_offset,
686 deref_offset + i * size_mul,
687 row_major, deref->type->matrix_columns, packing,
688 writemask_for_size(col_deref->type->vector_elements));
689 } else {
690 int size_mul;
691
692 /* std430 doesn't round up vec2 size to a vec4 size */
693 if (packing == GLSL_INTERFACE_PACKING_STD430 &&
694 deref->type->vector_elements == 2 &&
695 !deref->type->is_double()) {
696 size_mul = 8;
697 } else {
698 /* std140 always rounds the stride of arrays (and matrices) to a
699 * vec4, so matrices are always 16 between columns/rows. With
700 * doubles, they will be 32 apart when there are more than 2 rows.
701 *
702 * For both std140 and std430, if the member is a
703 * three-'component vector with components consuming N basic
704 * machine units, the base alignment is 4N. For vec4, base
705 * alignment is 4N.
706 */
707 size_mul = (deref->type->is_double() &&
708 deref->type->vector_elements > 2) ? 32 : 16;
709 }
710
711 emit_access(is_write, col_deref, base_offset,
712 deref_offset + i * size_mul,
713 row_major, deref->type->matrix_columns, packing,
714 writemask_for_size(col_deref->type->vector_elements));
715 }
716 }
717 return;
718 }
719
720 assert(deref->type->is_scalar() || deref->type->is_vector());
721
722 if (!row_major) {
723 ir_rvalue *offset =
724 add(base_offset, new(mem_ctx) ir_constant(deref_offset));
725 if (is_write)
726 base_ir->insert_after(ssbo_store(deref, offset, write_mask));
727 else {
728 if (!this->is_shader_storage) {
729 base_ir->insert_before(assign(deref->clone(mem_ctx, NULL),
730 ubo_load(deref->type, offset)));
731 } else {
732 ir_call *load_ssbo = ssbo_load(deref->type, offset);
733 base_ir->insert_before(load_ssbo);
734 ir_rvalue *value = load_ssbo->return_deref->as_rvalue()->clone(mem_ctx, NULL);
735 base_ir->insert_before(assign(deref->clone(mem_ctx, NULL), value));
736 }
737 }
738 } else {
739 unsigned N = deref->type->is_double() ? 8 : 4;
740
741 /* We're dereffing a column out of a row-major matrix, so we
742 * gather the vector from each stored row.
743 */
744 assert(deref->type->base_type == GLSL_TYPE_FLOAT ||
745 deref->type->base_type == GLSL_TYPE_DOUBLE);
746 /* Matrices, row_major or not, are stored as if they were
747 * arrays of vectors of the appropriate size in std140.
748 * Arrays have their strides rounded up to a vec4, so the
749 * matrix stride is always 16. However a double matrix may either be 16
750 * or 32 depending on the number of columns.
751 */
752 assert(matrix_columns <= 4);
753 unsigned matrix_stride = 0;
754 /* Matrix stride for std430 mat2xY matrices are not rounded up to
755 * vec4 size. From OpenGL 4.3 spec, section 7.6.2.2 "Standard Uniform
756 * Block Layout":
757 *
758 * "2. If the member is a two- or four-component vector with components
759 * consuming N basic machine units, the base alignment is 2N or 4N,
760 * respectively." [...]
761 * "4. If the member is an array of scalars or vectors, the base alignment
762 * and array stride are set to match the base alignment of a single array
763 * element, according to rules (1), (2), and (3), and rounded up to the
764 * base alignment of a vec4." [...]
765 * "7. If the member is a row-major matrix with C columns and R rows, the
766 * matrix is stored identically to an array of R row vectors with C
767 * components each, according to rule (4)." [...]
768 * "When using the std430 storage layout, shader storage blocks will be
769 * laid out in buffer storage identically to uniform and shader storage
770 * blocks using the std140 layout, except that the base alignment and
771 * stride of arrays of scalars and vectors in rule 4 and of structures in
772 * rule 9 are not rounded up a multiple of the base alignment of a vec4."
773 */
774 if (packing == GLSL_INTERFACE_PACKING_STD430 && matrix_columns == 2)
775 matrix_stride = 2 * N;
776 else
777 matrix_stride = glsl_align(matrix_columns * N, 16);
778
779 const glsl_type *deref_type = deref->type->base_type == GLSL_TYPE_FLOAT ?
780 glsl_type::float_type : glsl_type::double_type;
781
782 for (unsigned i = 0; i < deref->type->vector_elements; i++) {
783 ir_rvalue *chan_offset =
784 add(base_offset,
785 new(mem_ctx) ir_constant(deref_offset + i * matrix_stride));
786 if (is_write) {
787 /* If the component is not in the writemask, then don't
788 * store any value.
789 */
790 if (!((1 << i) & write_mask))
791 continue;
792
793 base_ir->insert_after(ssbo_store(swizzle(deref, i, 1), chan_offset, 1));
794 } else {
795 if (!this->is_shader_storage) {
796 base_ir->insert_before(assign(deref->clone(mem_ctx, NULL),
797 ubo_load(deref_type, chan_offset),
798 (1U << i)));
799 } else {
800 ir_call *load_ssbo = ssbo_load(deref_type, chan_offset);
801 base_ir->insert_before(load_ssbo);
802 ir_rvalue *value = load_ssbo->return_deref->as_rvalue()->clone(mem_ctx, NULL);
803 base_ir->insert_before(assign(deref->clone(mem_ctx, NULL),
804 value,
805 (1U << i)));
806 }
807 }
808 }
809 }
810 }
811
812 void
813 lower_ubo_reference_visitor::write_to_memory(ir_dereference *deref,
814 ir_variable *var,
815 ir_variable *write_var,
816 unsigned write_mask)
817 {
818 ir_rvalue *offset = NULL;
819 unsigned const_offset;
820 bool row_major;
821 int matrix_columns;
822 unsigned packing = var->get_interface_type()->interface_packing;
823
824 /* Compute the offset to the start if the dereference as well as other
825 * information we need to configure the write
826 */
827 setup_for_load_or_store(var, deref,
828 &offset, &const_offset,
829 &row_major, &matrix_columns,
830 packing);
831 assert(offset);
832
833 /* Now emit writes from the temporary to memory */
834 ir_variable *write_offset =
835 new(mem_ctx) ir_variable(glsl_type::uint_type,
836 "ssbo_store_temp_offset",
837 ir_var_temporary);
838
839 base_ir->insert_before(write_offset);
840 base_ir->insert_before(assign(write_offset, offset));
841
842 deref = new(mem_ctx) ir_dereference_variable(write_var);
843 emit_access(true, deref, write_offset, const_offset,
844 row_major, matrix_columns, packing, write_mask);
845 }
846
847 ir_visitor_status
848 lower_ubo_reference_visitor::visit_enter(ir_expression *ir)
849 {
850 check_ssbo_unsized_array_length_expression(ir);
851 return rvalue_visit(ir);
852 }
853
854 ir_expression *
855 lower_ubo_reference_visitor::calculate_ssbo_unsized_array_length(ir_expression *expr)
856 {
857 if (expr->operation !=
858 ir_expression_operation(ir_unop_ssbo_unsized_array_length))
859 return NULL;
860
861 ir_rvalue *rvalue = expr->operands[0]->as_rvalue();
862 if (!rvalue ||
863 !rvalue->type->is_array() || !rvalue->type->is_unsized_array())
864 return NULL;
865
866 ir_dereference *deref = expr->operands[0]->as_dereference();
867 if (!deref)
868 return NULL;
869
870 ir_variable *var = expr->operands[0]->variable_referenced();
871 if (!var || !var->is_in_shader_storage_block())
872 return NULL;
873 return process_ssbo_unsized_array_length(&rvalue, deref, var);
874 }
875
876 void
877 lower_ubo_reference_visitor::check_ssbo_unsized_array_length_expression(ir_expression *ir)
878 {
879 if (ir->operation ==
880 ir_expression_operation(ir_unop_ssbo_unsized_array_length)) {
881 /* Don't replace this unop if it is found alone. It is going to be
882 * removed by the optimization passes or replaced if it is part of
883 * an ir_assignment or another ir_expression.
884 */
885 return;
886 }
887
888 for (unsigned i = 0; i < ir->get_num_operands(); i++) {
889 if (ir->operands[i]->ir_type != ir_type_expression)
890 continue;
891 ir_expression *expr = (ir_expression *) ir->operands[i];
892 ir_expression *temp = calculate_ssbo_unsized_array_length(expr);
893 if (!temp)
894 continue;
895
896 delete expr;
897 ir->operands[i] = temp;
898 }
899 }
900
901 void
902 lower_ubo_reference_visitor::check_ssbo_unsized_array_length_assignment(ir_assignment *ir)
903 {
904 if (!ir->rhs || ir->rhs->ir_type != ir_type_expression)
905 return;
906
907 ir_expression *expr = (ir_expression *) ir->rhs;
908 ir_expression *temp = calculate_ssbo_unsized_array_length(expr);
909 if (!temp)
910 return;
911
912 delete expr;
913 ir->rhs = temp;
914 return;
915 }
916
917 ir_expression *
918 lower_ubo_reference_visitor::emit_ssbo_get_buffer_size()
919 {
920 ir_rvalue *block_ref = this->uniform_block->clone(mem_ctx, NULL);
921 return new(mem_ctx) ir_expression(ir_unop_get_buffer_size,
922 glsl_type::int_type,
923 block_ref);
924 }
925
926 unsigned
927 lower_ubo_reference_visitor::calculate_unsized_array_stride(ir_dereference *deref,
928 unsigned packing)
929 {
930 unsigned array_stride = 0;
931
932 switch (deref->ir_type) {
933 case ir_type_dereference_variable:
934 {
935 ir_dereference_variable *deref_var = (ir_dereference_variable *)deref;
936 const struct glsl_type *unsized_array_type = NULL;
937 /* An unsized array can be sized by other lowering passes, so pick
938 * the first field of the array which has the data type of the unsized
939 * array.
940 */
941 unsized_array_type = deref_var->var->type->fields.array;
942
943 /* Whether or not the field is row-major (because it might be a
944 * bvec2 or something) does not affect the array itself. We need
945 * to know whether an array element in its entirety is row-major.
946 */
947 const bool array_row_major =
948 is_dereferenced_thing_row_major(deref_var);
949
950 if (packing == GLSL_INTERFACE_PACKING_STD430) {
951 array_stride = unsized_array_type->std430_array_stride(array_row_major);
952 } else {
953 array_stride = unsized_array_type->std140_size(array_row_major);
954 array_stride = glsl_align(array_stride, 16);
955 }
956 break;
957 }
958 case ir_type_dereference_record:
959 {
960 ir_dereference_record *deref_record = (ir_dereference_record *) deref;
961 ir_dereference *interface_deref =
962 deref_record->record->as_dereference();
963 assert(interface_deref != NULL);
964 const struct glsl_type *interface_type = interface_deref->type;
965 unsigned record_length = interface_type->length;
966 /* Unsized array is always the last element of the interface */
967 const struct glsl_type *unsized_array_type =
968 interface_type->fields.structure[record_length - 1].type->fields.array;
969
970 const bool array_row_major =
971 is_dereferenced_thing_row_major(deref_record);
972
973 if (packing == GLSL_INTERFACE_PACKING_STD430) {
974 array_stride = unsized_array_type->std430_array_stride(array_row_major);
975 } else {
976 array_stride = unsized_array_type->std140_size(array_row_major);
977 array_stride = glsl_align(array_stride, 16);
978 }
979 break;
980 }
981 default:
982 unreachable("Unsupported dereference type");
983 }
984 return array_stride;
985 }
986
987 ir_expression *
988 lower_ubo_reference_visitor::process_ssbo_unsized_array_length(ir_rvalue **rvalue,
989 ir_dereference *deref,
990 ir_variable *var)
991 {
992 mem_ctx = ralloc_parent(*rvalue);
993
994 ir_rvalue *base_offset = NULL;
995 unsigned const_offset;
996 bool row_major;
997 int matrix_columns;
998 unsigned packing = var->get_interface_type()->interface_packing;
999 int unsized_array_stride = calculate_unsized_array_stride(deref, packing);
1000
1001 /* Compute the offset to the start if the dereference as well as other
1002 * information we need to calculate the length.
1003 */
1004 setup_for_load_or_store(var, deref,
1005 &base_offset, &const_offset,
1006 &row_major, &matrix_columns,
1007 packing);
1008 /* array.length() =
1009 * max((buffer_object_size - offset_of_array) / stride_of_array, 0)
1010 */
1011 ir_expression *buffer_size = emit_ssbo_get_buffer_size();
1012
1013 ir_expression *offset_of_array = new(mem_ctx)
1014 ir_expression(ir_binop_add, base_offset,
1015 new(mem_ctx) ir_constant(const_offset));
1016 ir_expression *offset_of_array_int = new(mem_ctx)
1017 ir_expression(ir_unop_u2i, offset_of_array);
1018
1019 ir_expression *sub = new(mem_ctx)
1020 ir_expression(ir_binop_sub, buffer_size, offset_of_array_int);
1021 ir_expression *div = new(mem_ctx)
1022 ir_expression(ir_binop_div, sub,
1023 new(mem_ctx) ir_constant(unsized_array_stride));
1024 ir_expression *max = new(mem_ctx)
1025 ir_expression(ir_binop_max, div, new(mem_ctx) ir_constant(0));
1026
1027 return max;
1028 }
1029
1030 void
1031 lower_ubo_reference_visitor::check_for_ssbo_store(ir_assignment *ir)
1032 {
1033 if (!ir || !ir->lhs)
1034 return;
1035
1036 ir_rvalue *rvalue = ir->lhs->as_rvalue();
1037 if (!rvalue)
1038 return;
1039
1040 ir_dereference *deref = ir->lhs->as_dereference();
1041 if (!deref)
1042 return;
1043
1044 ir_variable *var = ir->lhs->variable_referenced();
1045 if (!var || !var->is_in_buffer_block())
1046 return;
1047
1048 /* We have a write to a buffer variable, so declare a temporary and rewrite
1049 * the assignment so that the temporary is the LHS.
1050 */
1051 mem_ctx = ralloc_parent(shader->ir);
1052
1053 const glsl_type *type = rvalue->type;
1054 ir_variable *write_var = new(mem_ctx) ir_variable(type,
1055 "ssbo_store_temp",
1056 ir_var_temporary);
1057 base_ir->insert_before(write_var);
1058 ir->lhs = new(mem_ctx) ir_dereference_variable(write_var);
1059
1060 /* Now we have to write the value assigned to the temporary back to memory */
1061 write_to_memory(deref, var, write_var, ir->write_mask);
1062 progress = true;
1063 }
1064
1065
1066 ir_visitor_status
1067 lower_ubo_reference_visitor::visit_enter(ir_assignment *ir)
1068 {
1069 check_ssbo_unsized_array_length_assignment(ir);
1070 check_for_ssbo_store(ir);
1071 return rvalue_visit(ir);
1072 }
1073
1074 /* Lowers the intrinsic call to a new internal intrinsic that swaps the
1075 * access to the buffer variable in the first parameter by an offset
1076 * and block index. This involves creating the new internal intrinsic
1077 * (i.e. the new function signature).
1078 */
1079 ir_call *
1080 lower_ubo_reference_visitor::lower_ssbo_atomic_intrinsic(ir_call *ir)
1081 {
1082 /* SSBO atomics usually have 2 parameters, the buffer variable and an
1083 * integer argument. The exception is CompSwap, that has an additional
1084 * integer parameter.
1085 */
1086 int param_count = ir->actual_parameters.length();
1087 assert(param_count == 2 || param_count == 3);
1088
1089 /* First argument must be a scalar integer buffer variable */
1090 exec_node *param = ir->actual_parameters.get_head();
1091 ir_instruction *inst = (ir_instruction *) param;
1092 assert(inst->ir_type == ir_type_dereference_variable ||
1093 inst->ir_type == ir_type_dereference_array ||
1094 inst->ir_type == ir_type_dereference_record ||
1095 inst->ir_type == ir_type_swizzle);
1096
1097 ir_rvalue *deref = (ir_rvalue *) inst;
1098 assert(deref->type->is_scalar() && deref->type->is_integer());
1099
1100 ir_variable *var = deref->variable_referenced();
1101 assert(var);
1102
1103 /* Compute the offset to the start if the dereference and the
1104 * block index
1105 */
1106 mem_ctx = ralloc_parent(shader->ir);
1107
1108 ir_rvalue *offset = NULL;
1109 unsigned const_offset;
1110 bool row_major;
1111 int matrix_columns;
1112 unsigned packing = var->get_interface_type()->interface_packing;
1113
1114 setup_for_load_or_store(var, deref,
1115 &offset, &const_offset,
1116 &row_major, &matrix_columns,
1117 packing);
1118 assert(offset);
1119 assert(!row_major);
1120 assert(matrix_columns == 1);
1121
1122 ir_rvalue *deref_offset =
1123 add(offset, new(mem_ctx) ir_constant(const_offset));
1124 ir_rvalue *block_index = this->uniform_block->clone(mem_ctx, NULL);
1125
1126 /* Create the new internal function signature that will take a block
1127 * index and offset instead of a buffer variable
1128 */
1129 exec_list sig_params;
1130 ir_variable *sig_param = new(mem_ctx)
1131 ir_variable(glsl_type::uint_type, "block_ref" , ir_var_function_in);
1132 sig_params.push_tail(sig_param);
1133
1134 sig_param = new(mem_ctx)
1135 ir_variable(glsl_type::uint_type, "offset" , ir_var_function_in);
1136 sig_params.push_tail(sig_param);
1137
1138 const glsl_type *type = deref->type->base_type == GLSL_TYPE_INT ?
1139 glsl_type::int_type : glsl_type::uint_type;
1140 sig_param = new(mem_ctx)
1141 ir_variable(type, "data1", ir_var_function_in);
1142 sig_params.push_tail(sig_param);
1143
1144 if (param_count == 3) {
1145 sig_param = new(mem_ctx)
1146 ir_variable(type, "data2", ir_var_function_in);
1147 sig_params.push_tail(sig_param);
1148 }
1149
1150 ir_function_signature *sig =
1151 new(mem_ctx) ir_function_signature(deref->type,
1152 shader_storage_buffer_object);
1153 assert(sig);
1154 sig->replace_parameters(&sig_params);
1155 sig->is_intrinsic = true;
1156
1157 char func_name[64];
1158 sprintf(func_name, "%s_internal", ir->callee_name());
1159 ir_function *f = new(mem_ctx) ir_function(func_name);
1160 f->add_signature(sig);
1161
1162 /* Now, create the call to the internal intrinsic */
1163 exec_list call_params;
1164 call_params.push_tail(block_index);
1165 call_params.push_tail(deref_offset);
1166 param = ir->actual_parameters.get_head()->get_next();
1167 ir_rvalue *param_as_rvalue = ((ir_instruction *) param)->as_rvalue();
1168 call_params.push_tail(param_as_rvalue->clone(mem_ctx, NULL));
1169 if (param_count == 3) {
1170 param = param->get_next();
1171 param_as_rvalue = ((ir_instruction *) param)->as_rvalue();
1172 call_params.push_tail(param_as_rvalue->clone(mem_ctx, NULL));
1173 }
1174 ir_dereference_variable *return_deref =
1175 ir->return_deref->clone(mem_ctx, NULL);
1176 return new(mem_ctx) ir_call(sig, return_deref, &call_params);
1177 }
1178
1179 ir_call *
1180 lower_ubo_reference_visitor::check_for_ssbo_atomic_intrinsic(ir_call *ir)
1181 {
1182 const char *callee = ir->callee_name();
1183 if (!strcmp("__intrinsic_ssbo_atomic_add", callee) ||
1184 !strcmp("__intrinsic_ssbo_atomic_min", callee) ||
1185 !strcmp("__intrinsic_ssbo_atomic_max", callee) ||
1186 !strcmp("__intrinsic_ssbo_atomic_and", callee) ||
1187 !strcmp("__intrinsic_ssbo_atomic_or", callee) ||
1188 !strcmp("__intrinsic_ssbo_atomic_xor", callee) ||
1189 !strcmp("__intrinsic_ssbo_atomic_exchange", callee) ||
1190 !strcmp("__intrinsic_ssbo_atomic_comp_swap", callee)) {
1191 return lower_ssbo_atomic_intrinsic(ir);
1192 }
1193
1194 return ir;
1195 }
1196
1197
1198 ir_visitor_status
1199 lower_ubo_reference_visitor::visit_enter(ir_call *ir)
1200 {
1201 ir_call *new_ir = check_for_ssbo_atomic_intrinsic(ir);
1202 if (new_ir != ir) {
1203 progress = true;
1204 base_ir->replace_with(new_ir);
1205 return visit_continue_with_parent;
1206 }
1207
1208 return rvalue_visit(ir);
1209 }
1210
1211
1212 } /* unnamed namespace */
1213
1214 void
1215 lower_ubo_reference(struct gl_shader *shader, exec_list *instructions)
1216 {
1217 lower_ubo_reference_visitor v(shader);
1218
1219 /* Loop over the instructions lowering references, because we take
1220 * a deref of a UBO array using a UBO dereference as the index will
1221 * produce a collection of instructions all of which have cloned
1222 * UBO dereferences for that array index.
1223 */
1224 do {
1225 v.progress = false;
1226 visit_list_elements(&v, instructions);
1227 } while (v.progress);
1228 }