glsl: Indirect array indexing on non-last SSBO member must fail compilation
[mesa.git] / src / compiler / glsl / link_uniform_blocks.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 #include "main/core.h"
25 #include "ir.h"
26 #include "linker.h"
27 #include "ir_uniform.h"
28 #include "link_uniform_block_active_visitor.h"
29 #include "util/hash_table.h"
30 #include "program.h"
31
32 namespace {
33
34 class ubo_visitor : public program_resource_visitor {
35 public:
36 ubo_visitor(void *mem_ctx, gl_uniform_buffer_variable *variables,
37 unsigned num_variables, struct gl_shader_program *prog)
38 : index(0), offset(0), buffer_size(0), variables(variables),
39 num_variables(num_variables), mem_ctx(mem_ctx), is_array_instance(false),
40 prog(prog)
41 {
42 /* empty */
43 }
44
45 void process(const glsl_type *type, const char *name)
46 {
47 this->offset = 0;
48 this->buffer_size = 0;
49 this->is_array_instance = strchr(name, ']') != NULL;
50 this->program_resource_visitor::process(type, name);
51 }
52
53 unsigned index;
54 unsigned offset;
55 unsigned buffer_size;
56 gl_uniform_buffer_variable *variables;
57 unsigned num_variables;
58 void *mem_ctx;
59 bool is_array_instance;
60 struct gl_shader_program *prog;
61
62 private:
63 virtual void visit_field(const glsl_type *type, const char *name,
64 bool row_major)
65 {
66 (void) type;
67 (void) name;
68 (void) row_major;
69 assert(!"Should not get here.");
70 }
71
72 virtual void enter_record(const glsl_type *type, const char *,
73 bool row_major, const enum glsl_interface_packing packing) {
74 assert(type->is_record());
75 if (packing == GLSL_INTERFACE_PACKING_STD430)
76 this->offset = glsl_align(
77 this->offset, type->std430_base_alignment(row_major));
78 else
79 this->offset = glsl_align(
80 this->offset, type->std140_base_alignment(row_major));
81 }
82
83 virtual void leave_record(const glsl_type *type, const char *,
84 bool row_major, const enum glsl_interface_packing packing) {
85 assert(type->is_record());
86
87 /* If this is the last field of a structure, apply rule #9. The
88 * GL_ARB_uniform_buffer_object spec says:
89 *
90 * "The structure may have padding at the end; the base offset of
91 * the member following the sub-structure is rounded up to the next
92 * multiple of the base alignment of the structure."
93 */
94 if (packing == GLSL_INTERFACE_PACKING_STD430)
95 this->offset = glsl_align(
96 this->offset, type->std430_base_alignment(row_major));
97 else
98 this->offset = glsl_align(
99 this->offset, type->std140_base_alignment(row_major));
100 }
101
102 virtual void set_buffer_offset(unsigned offset)
103 {
104 this->offset = offset;
105 }
106
107 virtual void visit_field(const glsl_type *type, const char *name,
108 bool row_major, const glsl_type *,
109 const enum glsl_interface_packing packing,
110 bool last_field)
111 {
112 assert(this->index < this->num_variables);
113
114 gl_uniform_buffer_variable *v = &this->variables[this->index++];
115
116 v->Name = ralloc_strdup(mem_ctx, name);
117 v->Type = type;
118 v->RowMajor = type->without_array()->is_matrix() && row_major;
119
120 if (this->is_array_instance) {
121 v->IndexName = ralloc_strdup(mem_ctx, name);
122
123 char *open_bracket = strchr(v->IndexName, '[');
124 assert(open_bracket != NULL);
125
126 char *close_bracket = strchr(open_bracket, '.') - 1;
127 assert(close_bracket != NULL);
128
129 /* Length of the tail without the ']' but with the NUL.
130 */
131 unsigned len = strlen(close_bracket + 1) + 1;
132
133 memmove(open_bracket, close_bracket + 1, len);
134 } else {
135 v->IndexName = v->Name;
136 }
137
138 unsigned alignment = 0;
139 unsigned size = 0;
140
141 /* From ARB_program_interface_query:
142 *
143 * "If the final member of an active shader storage block is array
144 * with no declared size, the minimum buffer size is computed
145 * assuming the array was declared as an array with one element."
146 *
147 * For that reason, we use the base type of the unsized array to calculate
148 * its size. We don't need to check if the unsized array is the last member
149 * of a shader storage block (that check was already done by the parser).
150 */
151 const glsl_type *type_for_size = type;
152 if (type->is_unsized_array()) {
153 assert(last_field);
154 type_for_size = type->without_array();
155 }
156
157 if (packing == GLSL_INTERFACE_PACKING_STD430) {
158 alignment = type->std430_base_alignment(v->RowMajor);
159 size = type_for_size->std430_size(v->RowMajor);
160 } else {
161 alignment = type->std140_base_alignment(v->RowMajor);
162 size = type_for_size->std140_size(v->RowMajor);
163 }
164
165 this->offset = glsl_align(this->offset, alignment);
166 v->Offset = this->offset;
167
168 this->offset += size;
169
170 /* From the GL_ARB_uniform_buffer_object spec:
171 *
172 * "For uniform blocks laid out according to [std140] rules, the
173 * minimum buffer object size returned by the
174 * UNIFORM_BLOCK_DATA_SIZE query is derived by taking the offset of
175 * the last basic machine unit consumed by the last uniform of the
176 * uniform block (including any end-of-array or end-of-structure
177 * padding), adding one, and rounding up to the next multiple of
178 * the base alignment required for a vec4."
179 */
180 this->buffer_size = glsl_align(this->offset, 16);
181 }
182 };
183
184 class count_block_size : public program_resource_visitor {
185 public:
186 count_block_size() : num_active_uniforms(0)
187 {
188 /* empty */
189 }
190
191 unsigned num_active_uniforms;
192
193 private:
194 virtual void visit_field(const glsl_type *type, const char *name,
195 bool row_major)
196 {
197 (void) type;
198 (void) name;
199 (void) row_major;
200 this->num_active_uniforms++;
201 }
202 };
203
204 } /* anonymous namespace */
205
206 struct block {
207 const glsl_type *type;
208 bool has_instance_name;
209 };
210
211 static void
212 process_block_array(struct uniform_block_array_elements *ub_array, char **name,
213 size_t name_length, gl_uniform_block *blocks,
214 ubo_visitor *parcel, gl_uniform_buffer_variable *variables,
215 const struct link_uniform_block_active *const b,
216 unsigned *block_index, unsigned *binding_offset,
217 struct gl_context *ctx, struct gl_shader_program *prog)
218 {
219 if (ub_array) {
220 for (unsigned j = 0; j < ub_array->num_array_elements; j++) {
221 size_t new_length = name_length;
222
223 /* Append the subscript to the current variable name */
224 ralloc_asprintf_rewrite_tail(name, &new_length, "[%u]",
225 ub_array->array_elements[j]);
226
227 process_block_array(ub_array->array, name, new_length, blocks,
228 parcel, variables, b, block_index,
229 binding_offset, ctx, prog);
230 }
231 } else {
232 unsigned i = *block_index;
233 const glsl_type *type = b->type->without_array();
234
235 blocks[i].Name = ralloc_strdup(blocks, *name);
236 blocks[i].Uniforms = &variables[(*parcel).index];
237
238 /* The GL_ARB_shading_language_420pack spec says:
239 *
240 * "If the binding identifier is used with a uniform block
241 * instanced as an array then the first element of the array
242 * takes the specified block binding and each subsequent
243 * element takes the next consecutive uniform block binding
244 * point."
245 */
246 blocks[i].Binding = (b->has_binding) ? b->binding + *binding_offset : 0;
247
248 blocks[i].UniformBufferSize = 0;
249 blocks[i]._Packing = gl_uniform_block_packing(type->interface_packing);
250
251 parcel->process(type, blocks[i].Name);
252
253 blocks[i].UniformBufferSize = parcel->buffer_size;
254
255 /* Check SSBO size is lower than maximum supported size for SSBO */
256 if (b->is_shader_storage &&
257 parcel->buffer_size > ctx->Const.MaxShaderStorageBlockSize) {
258 linker_error(prog, "shader storage block `%s' has size %d, "
259 "which is larger than than the maximum allowed (%d)",
260 b->type->name,
261 parcel->buffer_size,
262 ctx->Const.MaxShaderStorageBlockSize);
263 }
264 blocks[i].NumUniforms =
265 (unsigned)(ptrdiff_t)(&variables[parcel->index] - blocks[i].Uniforms);
266
267 *block_index = *block_index + 1;
268 *binding_offset = *binding_offset + 1;
269 }
270 }
271
272 /* This function resizes the array types of the block so that later we can use
273 * this new size to correctly calculate the offest for indirect indexing.
274 */
275 static const glsl_type *
276 resize_block_array(const glsl_type *type,
277 struct uniform_block_array_elements *ub_array)
278 {
279 if (type->is_array()) {
280 struct uniform_block_array_elements *child_array =
281 type->fields.array->is_array() ? ub_array->array : NULL;
282 const glsl_type *new_child_type =
283 resize_block_array(type->fields.array, child_array);
284
285 const glsl_type *new_type =
286 glsl_type::get_array_instance(new_child_type,
287 ub_array->num_array_elements);
288 ub_array->ir->array->type = new_type;
289 return new_type;
290 } else {
291 return type;
292 }
293 }
294
295 static void
296 create_buffer_blocks(void *mem_ctx, struct gl_context *ctx,
297 struct gl_shader_program *prog,
298 struct gl_uniform_block **out_blks, unsigned num_blocks,
299 struct hash_table *block_hash, unsigned num_variables,
300 bool create_ubo_blocks)
301 {
302 if (num_blocks == 0) {
303 assert(num_variables == 0);
304 return;
305 }
306
307 assert(num_variables != 0);
308
309 /* Allocate storage to hold all of the information related to uniform
310 * blocks that can be queried through the API.
311 */
312 struct gl_uniform_block *blocks = ralloc_array(mem_ctx, gl_uniform_block, num_blocks);
313 gl_uniform_buffer_variable *variables =
314 ralloc_array(blocks, gl_uniform_buffer_variable, num_variables);
315
316 /* Add each variable from each uniform block to the API tracking
317 * structures.
318 */
319 ubo_visitor parcel(blocks, variables, num_variables, prog);
320
321 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_STD140)
322 == unsigned(ubo_packing_std140));
323 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_SHARED)
324 == unsigned(ubo_packing_shared));
325 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_PACKED)
326 == unsigned(ubo_packing_packed));
327 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_STD430)
328 == unsigned(ubo_packing_std430));
329
330 unsigned i = 0;
331 struct hash_entry *entry;
332 hash_table_foreach (block_hash, entry) {
333 const struct link_uniform_block_active *const b =
334 (const struct link_uniform_block_active *) entry->data;
335 const glsl_type *block_type = b->type;
336
337 if ((create_ubo_blocks && !b->is_shader_storage) ||
338 (!create_ubo_blocks && b->is_shader_storage)) {
339
340 if (b->array != NULL) {
341 unsigned binding_offset = 0;
342 char *name = ralloc_strdup(NULL,
343 block_type->without_array()->name);
344 size_t name_length = strlen(name);
345
346 assert(b->has_instance_name);
347 process_block_array(b->array, &name, name_length, blocks, &parcel,
348 variables, b, &i, &binding_offset, ctx, prog);
349 ralloc_free(name);
350 } else {
351 blocks[i].Name = ralloc_strdup(blocks, block_type->name);
352 blocks[i].Uniforms = &variables[parcel.index];
353 blocks[i].Binding = (b->has_binding) ? b->binding : 0;
354 blocks[i].UniformBufferSize = 0;
355 blocks[i]._Packing =
356 gl_uniform_block_packing(block_type->interface_packing);
357
358 parcel.process(block_type,
359 b->has_instance_name ? block_type->name : "");
360
361 blocks[i].UniformBufferSize = parcel.buffer_size;
362
363 /* Check SSBO size is lower than maximum supported size for SSBO
364 */
365 if (b->is_shader_storage &&
366 parcel.buffer_size > ctx->Const.MaxShaderStorageBlockSize) {
367 linker_error(prog, "shader storage block `%s' has size %d, "
368 "which is larger than than the maximum allowed (%d)",
369 block_type->name, parcel.buffer_size,
370 ctx->Const.MaxShaderStorageBlockSize);
371 }
372 blocks[i].NumUniforms = (unsigned)(ptrdiff_t)
373 (&variables[parcel.index] - blocks[i].Uniforms);
374 i++;
375 }
376 }
377 }
378
379 *out_blks = blocks;
380
381 assert(parcel.index == num_variables);
382 }
383
384 void
385 link_uniform_blocks(void *mem_ctx,
386 struct gl_context *ctx,
387 struct gl_shader_program *prog,
388 struct gl_linked_shader *shader,
389 struct gl_uniform_block **ubo_blocks,
390 unsigned *num_ubo_blocks,
391 struct gl_uniform_block **ssbo_blocks,
392 unsigned *num_ssbo_blocks)
393 {
394 /* This hash table will track all of the uniform blocks that have been
395 * encountered. Since blocks with the same block-name must be the same,
396 * the hash is organized by block-name.
397 */
398 struct hash_table *block_hash =
399 _mesa_hash_table_create(mem_ctx, _mesa_key_hash_string,
400 _mesa_key_string_equal);
401
402 if (block_hash == NULL) {
403 _mesa_error_no_memory(__func__);
404 linker_error(prog, "out of memory\n");
405 return;
406 }
407
408 /* Determine which uniform blocks are active.
409 */
410 link_uniform_block_active_visitor v(mem_ctx, block_hash, prog);
411 visit_list_elements(&v, shader->ir);
412
413 /* Count the number of active uniform blocks. Count the total number of
414 * active slots in those uniform blocks.
415 */
416 unsigned num_ubo_variables = 0;
417 unsigned num_ssbo_variables = 0;
418 count_block_size block_size;
419 struct hash_entry *entry;
420
421 hash_table_foreach (block_hash, entry) {
422 struct link_uniform_block_active *const b =
423 (struct link_uniform_block_active *) entry->data;
424
425 assert((b->array != NULL) == b->type->is_array());
426
427 if (b->array != NULL &&
428 (b->type->without_array()->interface_packing ==
429 GLSL_INTERFACE_PACKING_PACKED)) {
430 b->type = resize_block_array(b->type, b->array);
431 b->var->type = b->type;
432 }
433
434 block_size.num_active_uniforms = 0;
435 block_size.process(b->type->without_array(), "");
436
437 if (b->array != NULL) {
438 unsigned aoa_size = b->type->arrays_of_arrays_size();
439 if (b->is_shader_storage) {
440 *num_ssbo_blocks += aoa_size;
441 num_ssbo_variables += aoa_size * block_size.num_active_uniforms;
442 } else {
443 *num_ubo_blocks += aoa_size;
444 num_ubo_variables += aoa_size * block_size.num_active_uniforms;
445 }
446 } else {
447 if (b->is_shader_storage) {
448 (*num_ssbo_blocks)++;
449 num_ssbo_variables += block_size.num_active_uniforms;
450 } else {
451 (*num_ubo_blocks)++;
452 num_ubo_variables += block_size.num_active_uniforms;
453 }
454 }
455
456 }
457
458 create_buffer_blocks(mem_ctx, ctx, prog, ubo_blocks, *num_ubo_blocks,
459 block_hash, num_ubo_variables, true);
460 create_buffer_blocks(mem_ctx, ctx, prog, ssbo_blocks, *num_ssbo_blocks,
461 block_hash, num_ssbo_variables, false);
462
463 _mesa_hash_table_destroy(block_hash, NULL);
464 }
465
466 static bool
467 link_uniform_blocks_are_compatible(const gl_uniform_block *a,
468 const gl_uniform_block *b)
469 {
470 assert(strcmp(a->Name, b->Name) == 0);
471
472 /* Page 35 (page 42 of the PDF) in section 4.3.7 of the GLSL 1.50 spec says:
473 *
474 * "Matched block names within an interface (as defined above) must
475 * match in terms of having the same number of declarations with the
476 * same sequence of types and the same sequence of member names, as
477 * well as having the same member-wise layout qualification....if a
478 * matching block is declared as an array, then the array sizes must
479 * also match... Any mismatch will generate a link error."
480 *
481 * Arrays are not yet supported, so there is no check for that.
482 */
483 if (a->NumUniforms != b->NumUniforms)
484 return false;
485
486 if (a->_Packing != b->_Packing)
487 return false;
488
489 for (unsigned i = 0; i < a->NumUniforms; i++) {
490 if (strcmp(a->Uniforms[i].Name, b->Uniforms[i].Name) != 0)
491 return false;
492
493 if (a->Uniforms[i].Type != b->Uniforms[i].Type)
494 return false;
495
496 if (a->Uniforms[i].RowMajor != b->Uniforms[i].RowMajor)
497 return false;
498 }
499
500 return true;
501 }
502
503 /**
504 * Merges a uniform block into an array of uniform blocks that may or
505 * may not already contain a copy of it.
506 *
507 * Returns the index of the new block in the array.
508 */
509 int
510 link_cross_validate_uniform_block(void *mem_ctx,
511 struct gl_uniform_block **linked_blocks,
512 unsigned int *num_linked_blocks,
513 struct gl_uniform_block *new_block)
514 {
515 for (unsigned int i = 0; i < *num_linked_blocks; i++) {
516 struct gl_uniform_block *old_block = &(*linked_blocks)[i];
517
518 if (strcmp(old_block->Name, new_block->Name) == 0)
519 return link_uniform_blocks_are_compatible(old_block, new_block)
520 ? i : -1;
521 }
522
523 *linked_blocks = reralloc(mem_ctx, *linked_blocks,
524 struct gl_uniform_block,
525 *num_linked_blocks + 1);
526 int linked_block_index = (*num_linked_blocks)++;
527 struct gl_uniform_block *linked_block = &(*linked_blocks)[linked_block_index];
528
529 memcpy(linked_block, new_block, sizeof(*new_block));
530 linked_block->Uniforms = ralloc_array(*linked_blocks,
531 struct gl_uniform_buffer_variable,
532 linked_block->NumUniforms);
533
534 memcpy(linked_block->Uniforms,
535 new_block->Uniforms,
536 sizeof(*linked_block->Uniforms) * linked_block->NumUniforms);
537
538 linked_block->Name = ralloc_strdup(*linked_blocks, linked_block->Name);
539
540 for (unsigned int i = 0; i < linked_block->NumUniforms; i++) {
541 struct gl_uniform_buffer_variable *ubo_var =
542 &linked_block->Uniforms[i];
543
544 if (ubo_var->Name == ubo_var->IndexName) {
545 ubo_var->Name = ralloc_strdup(*linked_blocks, ubo_var->Name);
546 ubo_var->IndexName = ubo_var->Name;
547 } else {
548 ubo_var->Name = ralloc_strdup(*linked_blocks, ubo_var->Name);
549 ubo_var->IndexName = ralloc_strdup(*linked_blocks, ubo_var->IndexName);
550 }
551 }
552
553 return linked_block_index;
554 }