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