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