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