glsl: Fix wonkey indentation left from previous commit
[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 struct gl_context *ctx,
210 struct gl_shader_program *prog);
211
212 static void
213 process_block_array(struct uniform_block_array_elements *ub_array, char **name,
214 size_t name_length, gl_uniform_block *blocks,
215 ubo_visitor *parcel, gl_uniform_buffer_variable *variables,
216 const struct link_uniform_block_active *const b,
217 unsigned *block_index, unsigned *binding_offset,
218 struct gl_context *ctx, struct gl_shader_program *prog)
219 {
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 if (ub_array->array) {
228 process_block_array(ub_array->array, name, new_length, blocks,
229 parcel, variables, b, block_index,
230 binding_offset, ctx, prog);
231 } else {
232 process_block_array_leaf(name, blocks,
233 parcel, variables, b, block_index,
234 binding_offset, ctx, prog);
235 }
236 }
237 }
238
239 static void
240 process_block_array_leaf(char **name,
241 gl_uniform_block *blocks,
242 ubo_visitor *parcel, gl_uniform_buffer_variable *variables,
243 const struct link_uniform_block_active *const b,
244 unsigned *block_index, unsigned *binding_offset,
245 struct gl_context *ctx, struct gl_shader_program *prog)
246 {
247 unsigned i = *block_index;
248 const glsl_type *type = b->type->without_array();
249
250 blocks[i].Name = ralloc_strdup(blocks, *name);
251 blocks[i].Uniforms = &variables[(*parcel).index];
252
253 /* The GL_ARB_shading_language_420pack spec says:
254 *
255 * "If the binding identifier is used with a uniform block instanced as
256 * an array then the first element of the array takes the specified
257 * block binding and each subsequent element takes the next consecutive
258 * uniform block binding point."
259 */
260 blocks[i].Binding = (b->has_binding) ? b->binding + *binding_offset : 0;
261
262 blocks[i].UniformBufferSize = 0;
263 blocks[i]._Packing = gl_uniform_block_packing(type->interface_packing);
264 blocks[i]._RowMajor = type->get_interface_row_major();
265
266 parcel->process(type, blocks[i].Name);
267
268 blocks[i].UniformBufferSize = parcel->buffer_size;
269
270 /* Check SSBO size is lower than maximum supported size for SSBO */
271 if (b->is_shader_storage &&
272 parcel->buffer_size > ctx->Const.MaxShaderStorageBlockSize) {
273 linker_error(prog, "shader storage block `%s' has size %d, "
274 "which is larger than than the maximum allowed (%d)",
275 b->type->name,
276 parcel->buffer_size,
277 ctx->Const.MaxShaderStorageBlockSize);
278 }
279 blocks[i].NumUniforms =
280 (unsigned)(ptrdiff_t)(&variables[parcel->index] - blocks[i].Uniforms);
281
282 *block_index = *block_index + 1;
283 *binding_offset = *binding_offset + 1;
284 }
285
286 /* This function resizes the array types of the block so that later we can use
287 * this new size to correctly calculate the offest for indirect indexing.
288 */
289 static const glsl_type *
290 resize_block_array(const glsl_type *type,
291 struct uniform_block_array_elements *ub_array)
292 {
293 if (type->is_array()) {
294 struct uniform_block_array_elements *child_array =
295 type->fields.array->is_array() ? ub_array->array : NULL;
296 const glsl_type *new_child_type =
297 resize_block_array(type->fields.array, child_array);
298
299 const glsl_type *new_type =
300 glsl_type::get_array_instance(new_child_type,
301 ub_array->num_array_elements);
302 ub_array->ir->array->type = new_type;
303 return new_type;
304 } else {
305 return type;
306 }
307 }
308
309 static void
310 create_buffer_blocks(void *mem_ctx, struct gl_context *ctx,
311 struct gl_shader_program *prog,
312 struct gl_uniform_block **out_blks, unsigned num_blocks,
313 struct hash_table *block_hash, unsigned num_variables,
314 bool create_ubo_blocks)
315 {
316 if (num_blocks == 0) {
317 assert(num_variables == 0);
318 return;
319 }
320
321 assert(num_variables != 0);
322
323 /* Allocate storage to hold all of the information related to uniform
324 * blocks that can be queried through the API.
325 */
326 struct gl_uniform_block *blocks = rzalloc_array(mem_ctx, gl_uniform_block, num_blocks);
327 gl_uniform_buffer_variable *variables =
328 ralloc_array(blocks, gl_uniform_buffer_variable, num_variables);
329
330 /* Add each variable from each uniform block to the API tracking
331 * structures.
332 */
333 ubo_visitor parcel(blocks, variables, num_variables, prog);
334
335 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_STD140)
336 == unsigned(ubo_packing_std140));
337 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_SHARED)
338 == unsigned(ubo_packing_shared));
339 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_PACKED)
340 == unsigned(ubo_packing_packed));
341 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_STD430)
342 == unsigned(ubo_packing_std430));
343
344 unsigned i = 0;
345 struct hash_entry *entry;
346 hash_table_foreach (block_hash, entry) {
347 const struct link_uniform_block_active *const b =
348 (const struct link_uniform_block_active *) entry->data;
349 const glsl_type *block_type = b->type;
350
351 if ((create_ubo_blocks && !b->is_shader_storage) ||
352 (!create_ubo_blocks && b->is_shader_storage)) {
353
354 if (b->array != NULL) {
355 unsigned binding_offset = 0;
356 char *name = ralloc_strdup(NULL,
357 block_type->without_array()->name);
358 size_t name_length = strlen(name);
359
360 assert(b->has_instance_name);
361 process_block_array(b->array, &name, name_length, blocks, &parcel,
362 variables, b, &i, &binding_offset, ctx, prog);
363 ralloc_free(name);
364 } else {
365 blocks[i].Name = ralloc_strdup(blocks, block_type->name);
366 blocks[i].Uniforms = &variables[parcel.index];
367 blocks[i].Binding = (b->has_binding) ? b->binding : 0;
368 blocks[i].UniformBufferSize = 0;
369 blocks[i]._Packing =
370 gl_uniform_block_packing(block_type->interface_packing);
371 blocks[i]._RowMajor = block_type->get_interface_row_major();
372
373 parcel.process(block_type,
374 b->has_instance_name ? block_type->name : "");
375
376 blocks[i].UniformBufferSize = parcel.buffer_size;
377
378 /* Check SSBO size is lower than maximum supported size for SSBO
379 */
380 if (b->is_shader_storage &&
381 parcel.buffer_size > ctx->Const.MaxShaderStorageBlockSize) {
382 linker_error(prog, "shader storage block `%s' has size %d, "
383 "which is larger than than the maximum allowed (%d)",
384 block_type->name, parcel.buffer_size,
385 ctx->Const.MaxShaderStorageBlockSize);
386 }
387 blocks[i].NumUniforms = (unsigned)(ptrdiff_t)
388 (&variables[parcel.index] - blocks[i].Uniforms);
389 i++;
390 }
391 }
392 }
393
394 *out_blks = blocks;
395
396 assert(parcel.index == num_variables);
397 }
398
399 void
400 link_uniform_blocks(void *mem_ctx,
401 struct gl_context *ctx,
402 struct gl_shader_program *prog,
403 struct gl_linked_shader *shader,
404 struct gl_uniform_block **ubo_blocks,
405 unsigned *num_ubo_blocks,
406 struct gl_uniform_block **ssbo_blocks,
407 unsigned *num_ssbo_blocks)
408 {
409 /* This hash table will track all of the uniform blocks that have been
410 * encountered. Since blocks with the same block-name must be the same,
411 * the hash is organized by block-name.
412 */
413 struct hash_table *block_hash =
414 _mesa_hash_table_create(mem_ctx, _mesa_key_hash_string,
415 _mesa_key_string_equal);
416
417 if (block_hash == NULL) {
418 _mesa_error_no_memory(__func__);
419 linker_error(prog, "out of memory\n");
420 return;
421 }
422
423 /* Determine which uniform blocks are active.
424 */
425 link_uniform_block_active_visitor v(mem_ctx, block_hash, prog);
426 visit_list_elements(&v, shader->ir);
427
428 /* Count the number of active uniform blocks. Count the total number of
429 * active slots in those uniform blocks.
430 */
431 unsigned num_ubo_variables = 0;
432 unsigned num_ssbo_variables = 0;
433 count_block_size block_size;
434 struct hash_entry *entry;
435
436 hash_table_foreach (block_hash, entry) {
437 struct link_uniform_block_active *const b =
438 (struct link_uniform_block_active *) entry->data;
439
440 assert((b->array != NULL) == b->type->is_array());
441
442 if (b->array != NULL &&
443 (b->type->without_array()->interface_packing ==
444 GLSL_INTERFACE_PACKING_PACKED)) {
445 b->type = resize_block_array(b->type, b->array);
446 b->var->type = b->type;
447 }
448
449 block_size.num_active_uniforms = 0;
450 block_size.process(b->type->without_array(), "");
451
452 if (b->array != NULL) {
453 unsigned aoa_size = b->type->arrays_of_arrays_size();
454 if (b->is_shader_storage) {
455 *num_ssbo_blocks += aoa_size;
456 num_ssbo_variables += aoa_size * block_size.num_active_uniforms;
457 } else {
458 *num_ubo_blocks += aoa_size;
459 num_ubo_variables += aoa_size * block_size.num_active_uniforms;
460 }
461 } else {
462 if (b->is_shader_storage) {
463 (*num_ssbo_blocks)++;
464 num_ssbo_variables += block_size.num_active_uniforms;
465 } else {
466 (*num_ubo_blocks)++;
467 num_ubo_variables += block_size.num_active_uniforms;
468 }
469 }
470
471 }
472
473 create_buffer_blocks(mem_ctx, ctx, prog, ubo_blocks, *num_ubo_blocks,
474 block_hash, num_ubo_variables, true);
475 create_buffer_blocks(mem_ctx, ctx, prog, ssbo_blocks, *num_ssbo_blocks,
476 block_hash, num_ssbo_variables, false);
477
478 _mesa_hash_table_destroy(block_hash, NULL);
479 }
480
481 static bool
482 link_uniform_blocks_are_compatible(const gl_uniform_block *a,
483 const gl_uniform_block *b)
484 {
485 assert(strcmp(a->Name, b->Name) == 0);
486
487 /* Page 35 (page 42 of the PDF) in section 4.3.7 of the GLSL 1.50 spec says:
488 *
489 * "Matched block names within an interface (as defined above) must
490 * match in terms of having the same number of declarations with the
491 * same sequence of types and the same sequence of member names, as
492 * well as having the same member-wise layout qualification....if a
493 * matching block is declared as an array, then the array sizes must
494 * also match... Any mismatch will generate a link error."
495 *
496 * Arrays are not yet supported, so there is no check for that.
497 */
498 if (a->NumUniforms != b->NumUniforms)
499 return false;
500
501 if (a->_Packing != b->_Packing)
502 return false;
503
504 if (a->_RowMajor != b->_RowMajor)
505 return false;
506
507 for (unsigned i = 0; i < a->NumUniforms; i++) {
508 if (strcmp(a->Uniforms[i].Name, b->Uniforms[i].Name) != 0)
509 return false;
510
511 if (a->Uniforms[i].Type != b->Uniforms[i].Type)
512 return false;
513
514 if (a->Uniforms[i].RowMajor != b->Uniforms[i].RowMajor)
515 return false;
516 }
517
518 return true;
519 }
520
521 /**
522 * Merges a uniform block into an array of uniform blocks that may or
523 * may not already contain a copy of it.
524 *
525 * Returns the index of the new block in the array.
526 */
527 int
528 link_cross_validate_uniform_block(void *mem_ctx,
529 struct gl_uniform_block **linked_blocks,
530 unsigned int *num_linked_blocks,
531 struct gl_uniform_block *new_block)
532 {
533 for (unsigned int i = 0; i < *num_linked_blocks; i++) {
534 struct gl_uniform_block *old_block = &(*linked_blocks)[i];
535
536 if (strcmp(old_block->Name, new_block->Name) == 0)
537 return link_uniform_blocks_are_compatible(old_block, new_block)
538 ? i : -1;
539 }
540
541 *linked_blocks = reralloc(mem_ctx, *linked_blocks,
542 struct gl_uniform_block,
543 *num_linked_blocks + 1);
544 int linked_block_index = (*num_linked_blocks)++;
545 struct gl_uniform_block *linked_block = &(*linked_blocks)[linked_block_index];
546
547 memcpy(linked_block, new_block, sizeof(*new_block));
548 linked_block->Uniforms = ralloc_array(*linked_blocks,
549 struct gl_uniform_buffer_variable,
550 linked_block->NumUniforms);
551
552 memcpy(linked_block->Uniforms,
553 new_block->Uniforms,
554 sizeof(*linked_block->Uniforms) * linked_block->NumUniforms);
555
556 linked_block->Name = ralloc_strdup(*linked_blocks, linked_block->Name);
557
558 for (unsigned int i = 0; i < linked_block->NumUniforms; i++) {
559 struct gl_uniform_buffer_variable *ubo_var =
560 &linked_block->Uniforms[i];
561
562 if (ubo_var->Name == ubo_var->IndexName) {
563 ubo_var->Name = ralloc_strdup(*linked_blocks, ubo_var->Name);
564 ubo_var->IndexName = ubo_var->Name;
565 } else {
566 ubo_var->Name = ralloc_strdup(*linked_blocks, ubo_var->Name);
567 ubo_var->IndexName = ralloc_strdup(*linked_blocks, ubo_var->IndexName);
568 }
569 }
570
571 return linked_block_index;
572 }