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