2 * Copyright © 2012 Intel Corporation
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:
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
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.
24 #include "main/core.h"
27 #include "ir_uniform.h"
28 #include "link_uniform_block_active_visitor.h"
29 #include "util/hash_table.h"
34 class ubo_visitor
: public program_resource_visitor
{
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),
45 void process(const glsl_type
*type
, const char *name
)
48 this->buffer_size
= 0;
49 this->is_array_instance
= strchr(name
, ']') != NULL
;
50 this->program_resource_visitor::process(type
, name
);
56 gl_uniform_buffer_variable
*variables
;
57 unsigned num_variables
;
59 bool is_array_instance
;
60 struct gl_shader_program
*prog
;
63 virtual void visit_field(const glsl_type
*type
, const char *name
,
69 assert(!"Should not get here.");
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
));
79 this->offset
= glsl_align(
80 this->offset
, type
->std140_base_alignment(row_major
));
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());
87 /* If this is the last field of a structure, apply rule #9. The
88 * GL_ARB_uniform_buffer_object spec says:
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."
94 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
95 this->offset
= glsl_align(
96 this->offset
, type
->std430_base_alignment(row_major
));
98 this->offset
= glsl_align(
99 this->offset
, type
->std140_base_alignment(row_major
));
102 virtual void set_buffer_offset(unsigned offset
)
104 this->offset
= offset
;
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
,
112 assert(this->index
< this->num_variables
);
114 gl_uniform_buffer_variable
*v
= &this->variables
[this->index
++];
116 v
->Name
= ralloc_strdup(mem_ctx
, name
);
118 v
->RowMajor
= type
->without_array()->is_matrix() && row_major
;
120 if (this->is_array_instance
) {
121 v
->IndexName
= ralloc_strdup(mem_ctx
, name
);
123 char *open_bracket
= strchr(v
->IndexName
, '[');
124 assert(open_bracket
!= NULL
);
126 char *close_bracket
= strchr(open_bracket
, '.') - 1;
127 assert(close_bracket
!= NULL
);
129 /* Length of the tail without the ']' but with the NUL.
131 unsigned len
= strlen(close_bracket
+ 1) + 1;
133 memmove(open_bracket
, close_bracket
+ 1, len
);
135 v
->IndexName
= v
->Name
;
138 unsigned alignment
= 0;
141 /* From ARB_program_interface_query:
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."
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).
151 const glsl_type
*type_for_size
= type
;
152 if (type
->is_unsized_array()) {
154 linker_error(prog
, "unsized array `%s' definition: "
155 "only last member of a shader storage block "
156 "can be defined as unsized array",
160 type_for_size
= type
->without_array();
163 if (packing
== GLSL_INTERFACE_PACKING_STD430
) {
164 alignment
= type
->std430_base_alignment(v
->RowMajor
);
165 size
= type_for_size
->std430_size(v
->RowMajor
);
167 alignment
= type
->std140_base_alignment(v
->RowMajor
);
168 size
= type_for_size
->std140_size(v
->RowMajor
);
171 this->offset
= glsl_align(this->offset
, alignment
);
172 v
->Offset
= this->offset
;
174 this->offset
+= size
;
176 /* From the GL_ARB_uniform_buffer_object spec:
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."
186 this->buffer_size
= glsl_align(this->offset
, 16);
190 class count_block_size
: public program_resource_visitor
{
192 count_block_size() : num_active_uniforms(0)
197 unsigned num_active_uniforms
;
200 virtual void visit_field(const glsl_type
*type
, const char *name
,
206 this->num_active_uniforms
++;
210 } /* anonymous namespace */
213 const glsl_type
*type
;
214 bool has_instance_name
;
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
)
226 for (unsigned j
= 0; j
< ub_array
->num_array_elements
; j
++) {
227 size_t new_length
= name_length
;
229 /* Append the subscript to the current variable name */
230 ralloc_asprintf_rewrite_tail(name
, &new_length
, "[%u]",
231 ub_array
->array_elements
[j
]);
233 process_block_array(ub_array
->array
, name
, new_length
, blocks
,
234 parcel
, variables
, b
, block_index
,
235 binding_offset
, ctx
, prog
);
238 unsigned i
= *block_index
;
239 const glsl_type
*type
= b
->type
->without_array();
241 blocks
[i
].Name
= ralloc_strdup(blocks
, *name
);
242 blocks
[i
].Uniforms
= &variables
[(*parcel
).index
];
244 /* The GL_ARB_shading_language_420pack spec says:
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
252 blocks
[i
].Binding
= (b
->has_binding
) ? b
->binding
+ *binding_offset
: 0;
254 blocks
[i
].UniformBufferSize
= 0;
255 blocks
[i
]._Packing
= gl_uniform_block_packing(type
->interface_packing
);
257 parcel
->process(type
, blocks
[i
].Name
);
259 blocks
[i
].UniformBufferSize
= parcel
->buffer_size
;
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)",
268 ctx
->Const
.MaxShaderStorageBlockSize
);
270 blocks
[i
].NumUniforms
=
271 (unsigned)(ptrdiff_t)(&variables
[parcel
->index
] - blocks
[i
].Uniforms
);
273 *block_index
= *block_index
+ 1;
274 *binding_offset
= *binding_offset
+ 1;
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.
281 static const glsl_type
*
282 resize_block_array(const glsl_type
*type
,
283 struct uniform_block_array_elements
*ub_array
)
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
);
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
;
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
)
308 if (num_blocks
== 0) {
309 assert(num_variables
== 0);
313 assert(num_variables
!= 0);
315 /* Allocate storage to hold all of the information related to uniform
316 * blocks that can be queried through the API.
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
);
322 /* Add each variable from each uniform block to the API tracking
325 ubo_visitor
parcel(blocks
, variables
, num_variables
, prog
);
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
));
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
;
343 if ((create_ubo_blocks
&& !b
->is_shader_storage
) ||
344 (!create_ubo_blocks
&& b
->is_shader_storage
)) {
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
);
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
);
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;
362 gl_uniform_block_packing(block_type
->interface_packing
);
364 parcel
.process(block_type
,
365 b
->has_instance_name
? block_type
->name
: "");
367 blocks
[i
].UniformBufferSize
= parcel
.buffer_size
;
369 /* Check SSBO size is lower than maximum supported size for SSBO
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
);
378 blocks
[i
].NumUniforms
= (unsigned)(ptrdiff_t)
379 (&variables
[parcel
.index
] - blocks
[i
].Uniforms
);
387 assert(parcel
.index
== num_variables
);
391 link_uniform_blocks(void *mem_ctx
,
392 struct gl_context
*ctx
,
393 struct gl_shader_program
*prog
,
394 struct gl_shader
**shader_list
,
395 unsigned num_shaders
,
396 struct gl_uniform_block
**ubo_blocks
,
397 unsigned *num_ubo_blocks
,
398 struct gl_uniform_block
**ssbo_blocks
,
399 unsigned *num_ssbo_blocks
)
401 /* This hash table will track all of the uniform blocks that have been
402 * encountered. Since blocks with the same block-name must be the same,
403 * the hash is organized by block-name.
405 struct hash_table
*block_hash
=
406 _mesa_hash_table_create(mem_ctx
, _mesa_key_hash_string
,
407 _mesa_key_string_equal
);
409 if (block_hash
== NULL
) {
410 _mesa_error_no_memory(__func__
);
411 linker_error(prog
, "out of memory\n");
415 /* Determine which uniform blocks are active.
417 link_uniform_block_active_visitor
v(mem_ctx
, block_hash
, prog
);
418 for (unsigned i
= 0; i
< num_shaders
; i
++) {
419 visit_list_elements(&v
, shader_list
[i
]->ir
);
422 /* Count the number of active uniform blocks. Count the total number of
423 * active slots in those uniform blocks.
425 unsigned num_ubo_variables
= 0;
426 unsigned num_ssbo_variables
= 0;
427 count_block_size block_size
;
428 struct hash_entry
*entry
;
430 hash_table_foreach (block_hash
, entry
) {
431 struct link_uniform_block_active
*const b
=
432 (struct link_uniform_block_active
*) entry
->data
;
434 assert((b
->array
!= NULL
) == b
->type
->is_array());
436 if (b
->array
!= NULL
&&
437 (b
->type
->without_array()->interface_packing
==
438 GLSL_INTERFACE_PACKING_PACKED
)) {
439 b
->type
= resize_block_array(b
->type
, b
->array
);
440 b
->var
->type
= b
->type
;
443 block_size
.num_active_uniforms
= 0;
444 block_size
.process(b
->type
->without_array(), "");
446 if (b
->array
!= NULL
) {
447 unsigned aoa_size
= b
->type
->arrays_of_arrays_size();
448 if (b
->is_shader_storage
) {
449 *num_ssbo_blocks
+= aoa_size
;
450 num_ssbo_variables
+= aoa_size
* block_size
.num_active_uniforms
;
452 *num_ubo_blocks
+= aoa_size
;
453 num_ubo_variables
+= aoa_size
* block_size
.num_active_uniforms
;
456 if (b
->is_shader_storage
) {
457 (*num_ssbo_blocks
)++;
458 num_ssbo_variables
+= block_size
.num_active_uniforms
;
461 num_ubo_variables
+= block_size
.num_active_uniforms
;
467 create_buffer_blocks(mem_ctx
, ctx
, prog
, ubo_blocks
, *num_ubo_blocks
,
468 block_hash
, num_ubo_variables
, true);
469 create_buffer_blocks(mem_ctx
, ctx
, prog
, ssbo_blocks
, *num_ssbo_blocks
,
470 block_hash
, num_ssbo_variables
, false);
472 _mesa_hash_table_destroy(block_hash
, NULL
);
476 link_uniform_blocks_are_compatible(const gl_uniform_block
*a
,
477 const gl_uniform_block
*b
)
479 assert(strcmp(a
->Name
, b
->Name
) == 0);
481 /* Page 35 (page 42 of the PDF) in section 4.3.7 of the GLSL 1.50 spec says:
483 * "Matched block names within an interface (as defined above) must
484 * match in terms of having the same number of declarations with the
485 * same sequence of types and the same sequence of member names, as
486 * well as having the same member-wise layout qualification....if a
487 * matching block is declared as an array, then the array sizes must
488 * also match... Any mismatch will generate a link error."
490 * Arrays are not yet supported, so there is no check for that.
492 if (a
->NumUniforms
!= b
->NumUniforms
)
495 if (a
->_Packing
!= b
->_Packing
)
498 for (unsigned i
= 0; i
< a
->NumUniforms
; i
++) {
499 if (strcmp(a
->Uniforms
[i
].Name
, b
->Uniforms
[i
].Name
) != 0)
502 if (a
->Uniforms
[i
].Type
!= b
->Uniforms
[i
].Type
)
505 if (a
->Uniforms
[i
].RowMajor
!= b
->Uniforms
[i
].RowMajor
)
513 * Merges a uniform block into an array of uniform blocks that may or
514 * may not already contain a copy of it.
516 * Returns the index of the new block in the array.
519 link_cross_validate_uniform_block(void *mem_ctx
,
520 struct gl_uniform_block
**linked_blocks
,
521 unsigned int *num_linked_blocks
,
522 struct gl_uniform_block
*new_block
)
524 for (unsigned int i
= 0; i
< *num_linked_blocks
; i
++) {
525 struct gl_uniform_block
*old_block
= &(*linked_blocks
)[i
];
527 if (strcmp(old_block
->Name
, new_block
->Name
) == 0)
528 return link_uniform_blocks_are_compatible(old_block
, new_block
)
532 *linked_blocks
= reralloc(mem_ctx
, *linked_blocks
,
533 struct gl_uniform_block
,
534 *num_linked_blocks
+ 1);
535 int linked_block_index
= (*num_linked_blocks
)++;
536 struct gl_uniform_block
*linked_block
= &(*linked_blocks
)[linked_block_index
];
538 memcpy(linked_block
, new_block
, sizeof(*new_block
));
539 linked_block
->Uniforms
= ralloc_array(*linked_blocks
,
540 struct gl_uniform_buffer_variable
,
541 linked_block
->NumUniforms
);
543 memcpy(linked_block
->Uniforms
,
545 sizeof(*linked_block
->Uniforms
) * linked_block
->NumUniforms
);
547 linked_block
->Name
= ralloc_strdup(*linked_blocks
, linked_block
->Name
);
549 for (unsigned int i
= 0; i
< linked_block
->NumUniforms
; i
++) {
550 struct gl_uniform_buffer_variable
*ubo_var
=
551 &linked_block
->Uniforms
[i
];
553 if (ubo_var
->Name
== ubo_var
->IndexName
) {
554 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
555 ubo_var
->IndexName
= ubo_var
->Name
;
557 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
558 ubo_var
->IndexName
= ralloc_strdup(*linked_blocks
, ubo_var
->IndexName
);
562 return linked_block_index
;