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.
26 #include "ir_uniform.h"
27 #include "link_uniform_block_active_visitor.h"
28 #include "util/hash_table.h"
30 #include "main/errors.h"
31 #include "main/mtypes.h"
35 class ubo_visitor
: public program_resource_visitor
{
37 ubo_visitor(void *mem_ctx
, gl_uniform_buffer_variable
*variables
,
38 unsigned num_variables
, struct gl_shader_program
*prog
,
39 bool use_std430_as_default
)
40 : index(0), offset(0), buffer_size(0), variables(variables
),
41 num_variables(num_variables
), mem_ctx(mem_ctx
),
42 is_array_instance(false), prog(prog
),
43 use_std430_as_default(use_std430_as_default
)
48 void process(const glsl_type
*type
, const char *name
)
51 this->buffer_size
= 0;
52 this->is_array_instance
= strchr(name
, ']') != NULL
;
53 this->program_resource_visitor::process(type
, name
,
54 use_std430_as_default
);
60 gl_uniform_buffer_variable
*variables
;
61 unsigned num_variables
;
63 bool is_array_instance
;
64 struct gl_shader_program
*prog
;
67 virtual void enter_record(const glsl_type
*type
, const char *,
69 const enum glsl_interface_packing packing
)
71 assert(type
->is_record());
72 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
73 this->offset
= glsl_align(
74 this->offset
, type
->std430_base_alignment(row_major
));
76 this->offset
= glsl_align(
77 this->offset
, type
->std140_base_alignment(row_major
));
80 virtual void leave_record(const glsl_type
*type
, const char *,
82 const enum glsl_interface_packing packing
)
84 assert(type
->is_record());
86 /* If this is the last field of a structure, apply rule #9. The
87 * ARB_uniform_buffer_object spec says:
89 * The structure may have padding at the end; the base offset of the
90 * member following the sub-structure is rounded up to the next
91 * multiple of the base alignment of the structure.
93 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
94 this->offset
= glsl_align(
95 this->offset
, type
->std430_base_alignment(row_major
));
97 this->offset
= glsl_align(
98 this->offset
, type
->std140_base_alignment(row_major
));
101 virtual void set_buffer_offset(unsigned offset
)
103 this->offset
= offset
;
106 virtual void visit_field(const glsl_type
*type
, const char *name
,
107 bool row_major
, const glsl_type
*,
108 const enum glsl_interface_packing packing
,
111 assert(this->index
< this->num_variables
);
113 gl_uniform_buffer_variable
*v
= &this->variables
[this->index
++];
115 v
->Name
= ralloc_strdup(mem_ctx
, name
);
117 v
->RowMajor
= type
->without_array()->is_matrix() && row_major
;
119 if (this->is_array_instance
) {
120 v
->IndexName
= ralloc_strdup(mem_ctx
, name
);
122 char *open_bracket
= strchr(v
->IndexName
, '[');
123 assert(open_bracket
!= NULL
);
125 char *close_bracket
= strchr(open_bracket
, '.') - 1;
126 assert(close_bracket
!= NULL
);
128 /* Length of the tail without the ']' but with the NUL.
130 unsigned len
= strlen(close_bracket
+ 1) + 1;
132 memmove(open_bracket
, close_bracket
+ 1, len
);
134 v
->IndexName
= v
->Name
;
137 unsigned alignment
= 0;
140 /* The ARB_program_interface_query spec says:
142 * If the final member of an active shader storage block is array
143 * with no declared size, the minimum buffer size is computed
144 * assuming the array was declared as an array with one element.
146 * For that reason, we use the base type of the unsized array to
147 * calculate its size. We don't need to check if the unsized array is
148 * the last member of a shader storage block (that check was already
149 * 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 /* The ARB_uniform_buffer_object spec says:
178 * For uniform blocks laid out according to [std140] rules, the
179 * minimum buffer object size returned by the UNIFORM_BLOCK_DATA_SIZE
180 * query is derived by taking the offset of the last basic machine
181 * unit consumed by the last uniform of the uniform block (including
182 * any end-of-array or end-of-structure padding), adding one, and
183 * rounding up to the next multiple of the base alignment required
186 this->buffer_size
= glsl_align(this->offset
, 16);
189 bool use_std430_as_default
;
192 class count_block_size
: public program_resource_visitor
{
194 count_block_size() : num_active_uniforms(0)
199 unsigned num_active_uniforms
;
202 virtual void visit_field(const glsl_type
* /* type */,
203 const char * /* name */,
204 bool /* row_major */,
205 const glsl_type
* /* record_type */,
206 const enum glsl_interface_packing
,
207 bool /* last_field */)
209 this->num_active_uniforms
++;
213 } /* anonymous namespace */
216 const glsl_type
*type
;
217 bool has_instance_name
;
220 static void process_block_array_leaf(const char *name
, gl_uniform_block
*blocks
,
222 gl_uniform_buffer_variable
*variables
,
223 const struct link_uniform_block_active
*const b
,
224 unsigned *block_index
,
225 unsigned *binding_offset
,
226 unsigned linearized_index
,
227 struct gl_context
*ctx
,
228 struct gl_shader_program
*prog
);
232 * \param first_index Value of \c block_index for the first element of the
236 process_block_array(struct uniform_block_array_elements
*ub_array
, char **name
,
237 size_t name_length
, gl_uniform_block
*blocks
,
238 ubo_visitor
*parcel
, gl_uniform_buffer_variable
*variables
,
239 const struct link_uniform_block_active
*const b
,
240 unsigned *block_index
, unsigned *binding_offset
,
241 struct gl_context
*ctx
, struct gl_shader_program
*prog
,
242 unsigned first_index
)
244 for (unsigned j
= 0; j
< ub_array
->num_array_elements
; j
++) {
245 size_t new_length
= name_length
;
247 /* Append the subscript to the current variable name */
248 ralloc_asprintf_rewrite_tail(name
, &new_length
, "[%u]",
249 ub_array
->array_elements
[j
]);
251 if (ub_array
->array
) {
252 process_block_array(ub_array
->array
, name
, new_length
, blocks
,
253 parcel
, variables
, b
, block_index
,
254 binding_offset
, ctx
, prog
, first_index
);
256 process_block_array_leaf(*name
, blocks
,
257 parcel
, variables
, b
, block_index
,
258 binding_offset
, *block_index
- first_index
,
265 process_block_array_leaf(const char *name
,
266 gl_uniform_block
*blocks
,
267 ubo_visitor
*parcel
, gl_uniform_buffer_variable
*variables
,
268 const struct link_uniform_block_active
*const b
,
269 unsigned *block_index
, unsigned *binding_offset
,
270 unsigned linearized_index
,
271 struct gl_context
*ctx
, struct gl_shader_program
*prog
)
273 unsigned i
= *block_index
;
274 const glsl_type
*type
= b
->type
->without_array();
276 blocks
[i
].Name
= ralloc_strdup(blocks
, name
);
277 blocks
[i
].Uniforms
= &variables
[(*parcel
).index
];
279 /* The ARB_shading_language_420pack spec says:
281 * If the binding identifier is used with a uniform block instanced as
282 * an array then the first element of the array takes the specified
283 * block binding and each subsequent element takes the next consecutive
284 * uniform block binding point.
286 blocks
[i
].Binding
= (b
->has_binding
) ? b
->binding
+ *binding_offset
: 0;
288 blocks
[i
].UniformBufferSize
= 0;
289 blocks
[i
]._Packing
= glsl_interface_packing(type
->interface_packing
);
290 blocks
[i
]._RowMajor
= type
->get_interface_row_major();
291 blocks
[i
].linearized_array_index
= linearized_index
;
293 parcel
->process(type
, b
->has_instance_name
? blocks
[i
].Name
: "");
295 blocks
[i
].UniformBufferSize
= parcel
->buffer_size
;
297 /* Check SSBO size is lower than maximum supported size for SSBO */
298 if (b
->is_shader_storage
&&
299 parcel
->buffer_size
> ctx
->Const
.MaxShaderStorageBlockSize
) {
300 linker_error(prog
, "shader storage block `%s' has size %d, "
301 "which is larger than the maximum allowed (%d)",
304 ctx
->Const
.MaxShaderStorageBlockSize
);
306 blocks
[i
].NumUniforms
=
307 (unsigned)(ptrdiff_t)(&variables
[parcel
->index
] - blocks
[i
].Uniforms
);
309 *block_index
= *block_index
+ 1;
310 *binding_offset
= *binding_offset
+ 1;
313 /* This function resizes the array types of the block so that later we can use
314 * this new size to correctly calculate the offest for indirect indexing.
316 static const glsl_type
*
317 resize_block_array(const glsl_type
*type
,
318 struct uniform_block_array_elements
*ub_array
)
320 if (type
->is_array()) {
321 struct uniform_block_array_elements
*child_array
=
322 type
->fields
.array
->is_array() ? ub_array
->array
: NULL
;
323 const glsl_type
*new_child_type
=
324 resize_block_array(type
->fields
.array
, child_array
);
326 const glsl_type
*new_type
=
327 glsl_type::get_array_instance(new_child_type
,
328 ub_array
->num_array_elements
);
329 ub_array
->ir
->array
->type
= new_type
;
337 create_buffer_blocks(void *mem_ctx
, struct gl_context
*ctx
,
338 struct gl_shader_program
*prog
,
339 struct gl_uniform_block
**out_blks
, unsigned num_blocks
,
340 struct hash_table
*block_hash
, unsigned num_variables
,
341 bool create_ubo_blocks
)
343 if (num_blocks
== 0) {
344 assert(num_variables
== 0);
348 assert(num_variables
!= 0);
350 /* Allocate storage to hold all of the information related to uniform
351 * blocks that can be queried through the API.
353 struct gl_uniform_block
*blocks
=
354 rzalloc_array(mem_ctx
, gl_uniform_block
, num_blocks
);
355 gl_uniform_buffer_variable
*variables
=
356 ralloc_array(blocks
, gl_uniform_buffer_variable
, num_variables
);
358 /* Add each variable from each uniform block to the API tracking
361 ubo_visitor
parcel(blocks
, variables
, num_variables
, prog
,
362 ctx
->Const
.UseSTD430AsDefaultPacking
);
365 struct hash_entry
*entry
;
366 hash_table_foreach (block_hash
, entry
) {
367 const struct link_uniform_block_active
*const b
=
368 (const struct link_uniform_block_active
*) entry
->data
;
369 const glsl_type
*block_type
= b
->type
;
371 if ((create_ubo_blocks
&& !b
->is_shader_storage
) ||
372 (!create_ubo_blocks
&& b
->is_shader_storage
)) {
374 unsigned binding_offset
= 0;
375 if (b
->array
!= NULL
) {
376 char *name
= ralloc_strdup(NULL
,
377 block_type
->without_array()->name
);
378 size_t name_length
= strlen(name
);
380 assert(b
->has_instance_name
);
381 process_block_array(b
->array
, &name
, name_length
, blocks
, &parcel
,
382 variables
, b
, &i
, &binding_offset
, ctx
, prog
,
386 process_block_array_leaf(block_type
->name
, blocks
, &parcel
,
387 variables
, b
, &i
, &binding_offset
,
395 assert(parcel
.index
== num_variables
);
399 link_uniform_blocks(void *mem_ctx
,
400 struct gl_context
*ctx
,
401 struct gl_shader_program
*prog
,
402 struct gl_linked_shader
*shader
,
403 struct gl_uniform_block
**ubo_blocks
,
404 unsigned *num_ubo_blocks
,
405 struct gl_uniform_block
**ssbo_blocks
,
406 unsigned *num_ssbo_blocks
)
408 /* This hash table will track all of the uniform blocks that have been
409 * encountered. Since blocks with the same block-name must be the same,
410 * the hash is organized by block-name.
412 struct hash_table
*block_hash
=
413 _mesa_hash_table_create(mem_ctx
, _mesa_key_hash_string
,
414 _mesa_key_string_equal
);
416 if (block_hash
== NULL
) {
417 _mesa_error_no_memory(__func__
);
418 linker_error(prog
, "out of memory\n");
422 /* Determine which uniform blocks are active. */
423 link_uniform_block_active_visitor
v(mem_ctx
, block_hash
, prog
);
424 visit_list_elements(&v
, shader
->ir
);
426 /* Count the number of active uniform blocks. Count the total number of
427 * active slots in those uniform blocks.
429 unsigned num_ubo_variables
= 0;
430 unsigned num_ssbo_variables
= 0;
431 count_block_size block_size
;
432 struct hash_entry
*entry
;
434 hash_table_foreach (block_hash
, entry
) {
435 struct link_uniform_block_active
*const b
=
436 (struct link_uniform_block_active
*) entry
->data
;
438 assert((b
->array
!= NULL
) == b
->type
->is_array());
440 if (b
->array
!= NULL
&&
441 (b
->type
->without_array()->interface_packing
==
442 GLSL_INTERFACE_PACKING_PACKED
)) {
443 b
->type
= resize_block_array(b
->type
, b
->array
);
444 b
->var
->type
= b
->type
;
447 block_size
.num_active_uniforms
= 0;
448 block_size
.process(b
->type
->without_array(), "",
449 ctx
->Const
.UseSTD430AsDefaultPacking
);
451 if (b
->array
!= NULL
) {
452 unsigned aoa_size
= b
->type
->arrays_of_arrays_size();
453 if (b
->is_shader_storage
) {
454 *num_ssbo_blocks
+= aoa_size
;
455 num_ssbo_variables
+= aoa_size
* block_size
.num_active_uniforms
;
457 *num_ubo_blocks
+= aoa_size
;
458 num_ubo_variables
+= aoa_size
* block_size
.num_active_uniforms
;
461 if (b
->is_shader_storage
) {
462 (*num_ssbo_blocks
)++;
463 num_ssbo_variables
+= block_size
.num_active_uniforms
;
466 num_ubo_variables
+= block_size
.num_active_uniforms
;
472 create_buffer_blocks(mem_ctx
, ctx
, prog
, ubo_blocks
, *num_ubo_blocks
,
473 block_hash
, num_ubo_variables
, true);
474 create_buffer_blocks(mem_ctx
, ctx
, prog
, ssbo_blocks
, *num_ssbo_blocks
,
475 block_hash
, num_ssbo_variables
, false);
477 _mesa_hash_table_destroy(block_hash
, NULL
);
481 link_uniform_blocks_are_compatible(const gl_uniform_block
*a
,
482 const gl_uniform_block
*b
)
484 assert(strcmp(a
->Name
, b
->Name
) == 0);
486 /* Page 35 (page 42 of the PDF) in section 4.3.7 of the GLSL 1.50 spec says:
488 * Matched block names within an interface (as defined above) must match
489 * in terms of having the same number of declarations with the same
490 * sequence of types and the same sequence of member names, as well as
491 * having the same member-wise layout qualification....if a matching
492 * block is declared as an array, then the array sizes must also
493 * match... Any mismatch will generate a link error.
495 * Arrays are not yet supported, so there is no check for that.
497 if (a
->NumUniforms
!= b
->NumUniforms
)
500 if (a
->_Packing
!= b
->_Packing
)
503 if (a
->_RowMajor
!= b
->_RowMajor
)
506 if (a
->Binding
!= b
->Binding
)
509 for (unsigned i
= 0; i
< a
->NumUniforms
; i
++) {
510 if (strcmp(a
->Uniforms
[i
].Name
, b
->Uniforms
[i
].Name
) != 0)
513 if (a
->Uniforms
[i
].Type
!= b
->Uniforms
[i
].Type
)
516 if (a
->Uniforms
[i
].RowMajor
!= b
->Uniforms
[i
].RowMajor
)
524 * Merges a uniform block into an array of uniform blocks that may or
525 * may not already contain a copy of it.
527 * Returns the index of the new block in the array.
530 link_cross_validate_uniform_block(void *mem_ctx
,
531 struct gl_uniform_block
**linked_blocks
,
532 unsigned int *num_linked_blocks
,
533 struct gl_uniform_block
*new_block
)
535 for (unsigned int i
= 0; i
< *num_linked_blocks
; i
++) {
536 struct gl_uniform_block
*old_block
= &(*linked_blocks
)[i
];
538 if (strcmp(old_block
->Name
, new_block
->Name
) == 0)
539 return link_uniform_blocks_are_compatible(old_block
, new_block
)
543 *linked_blocks
= reralloc(mem_ctx
, *linked_blocks
,
544 struct gl_uniform_block
,
545 *num_linked_blocks
+ 1);
546 int linked_block_index
= (*num_linked_blocks
)++;
547 struct gl_uniform_block
*linked_block
= &(*linked_blocks
)[linked_block_index
];
549 memcpy(linked_block
, new_block
, sizeof(*new_block
));
550 linked_block
->Uniforms
= ralloc_array(*linked_blocks
,
551 struct gl_uniform_buffer_variable
,
552 linked_block
->NumUniforms
);
554 memcpy(linked_block
->Uniforms
,
556 sizeof(*linked_block
->Uniforms
) * linked_block
->NumUniforms
);
558 linked_block
->Name
= ralloc_strdup(*linked_blocks
, linked_block
->Name
);
560 for (unsigned int i
= 0; i
< linked_block
->NumUniforms
; i
++) {
561 struct gl_uniform_buffer_variable
*ubo_var
=
562 &linked_block
->Uniforms
[i
];
564 if (ubo_var
->Name
== ubo_var
->IndexName
) {
565 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
566 ubo_var
->IndexName
= ubo_var
->Name
;
568 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
569 ubo_var
->IndexName
= ralloc_strdup(*linked_blocks
, ubo_var
->IndexName
);
573 return linked_block_index
;