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