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 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
));
70 this->offset
= glsl_align(
71 this->offset
, type
->std140_base_alignment(row_major
));
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());
78 /* If this is the last field of a structure, apply rule #9. The
79 * GL_ARB_uniform_buffer_object spec says:
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."
85 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
86 this->offset
= glsl_align(
87 this->offset
, type
->std430_base_alignment(row_major
));
89 this->offset
= glsl_align(
90 this->offset
, type
->std140_base_alignment(row_major
));
93 virtual void set_buffer_offset(unsigned offset
)
95 this->offset
= offset
;
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
,
103 assert(this->index
< this->num_variables
);
105 gl_uniform_buffer_variable
*v
= &this->variables
[this->index
++];
107 v
->Name
= ralloc_strdup(mem_ctx
, name
);
109 v
->RowMajor
= type
->without_array()->is_matrix() && row_major
;
111 if (this->is_array_instance
) {
112 v
->IndexName
= ralloc_strdup(mem_ctx
, name
);
114 char *open_bracket
= strchr(v
->IndexName
, '[');
115 assert(open_bracket
!= NULL
);
117 char *close_bracket
= strchr(open_bracket
, '.') - 1;
118 assert(close_bracket
!= NULL
);
120 /* Length of the tail without the ']' but with the NUL.
122 unsigned len
= strlen(close_bracket
+ 1) + 1;
124 memmove(open_bracket
, close_bracket
+ 1, len
);
126 v
->IndexName
= v
->Name
;
129 unsigned alignment
= 0;
132 /* From ARB_program_interface_query:
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."
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).
142 const glsl_type
*type_for_size
= type
;
143 if (type
->is_unsized_array()) {
145 type_for_size
= type
->without_array();
148 if (packing
== GLSL_INTERFACE_PACKING_STD430
) {
149 alignment
= type
->std430_base_alignment(v
->RowMajor
);
150 size
= type_for_size
->std430_size(v
->RowMajor
);
152 alignment
= type
->std140_base_alignment(v
->RowMajor
);
153 size
= type_for_size
->std140_size(v
->RowMajor
);
156 this->offset
= glsl_align(this->offset
, alignment
);
157 v
->Offset
= this->offset
;
159 this->offset
+= size
;
161 /* From the GL_ARB_uniform_buffer_object spec:
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."
171 this->buffer_size
= glsl_align(this->offset
, 16);
175 class count_block_size
: public program_resource_visitor
{
177 count_block_size() : num_active_uniforms(0)
182 unsigned num_active_uniforms
;
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 */)
192 this->num_active_uniforms
++;
196 } /* anonymous namespace */
199 const glsl_type
*type
;
200 bool has_instance_name
;
203 static void process_block_array_leaf(char **name
, gl_uniform_block
*blocks
,
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
);
215 * \param first_index Value of \c block_index for the first element of the
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
)
227 for (unsigned j
= 0; j
< ub_array
->num_array_elements
; j
++) {
228 size_t new_length
= name_length
;
230 /* Append the subscript to the current variable name */
231 ralloc_asprintf_rewrite_tail(name
, &new_length
, "[%u]",
232 ub_array
->array_elements
[j
]);
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
);
239 process_block_array_leaf(name
, blocks
,
240 parcel
, variables
, b
, block_index
,
241 binding_offset
, *block_index
- first_index
,
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
)
256 unsigned i
= *block_index
;
257 const glsl_type
*type
= b
->type
->without_array();
259 blocks
[i
].Name
= ralloc_strdup(blocks
, *name
);
260 blocks
[i
].Uniforms
= &variables
[(*parcel
).index
];
262 /* The GL_ARB_shading_language_420pack spec says:
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."
269 blocks
[i
].Binding
= (b
->has_binding
) ? b
->binding
+ *binding_offset
: 0;
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
;
276 parcel
->process(type
, blocks
[i
].Name
);
278 blocks
[i
].UniformBufferSize
= parcel
->buffer_size
;
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)",
287 ctx
->Const
.MaxShaderStorageBlockSize
);
289 blocks
[i
].NumUniforms
=
290 (unsigned)(ptrdiff_t)(&variables
[parcel
->index
] - blocks
[i
].Uniforms
);
292 *block_index
= *block_index
+ 1;
293 *binding_offset
= *binding_offset
+ 1;
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.
299 static const glsl_type
*
300 resize_block_array(const glsl_type
*type
,
301 struct uniform_block_array_elements
*ub_array
)
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
);
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
;
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
)
326 if (num_blocks
== 0) {
327 assert(num_variables
== 0);
331 assert(num_variables
!= 0);
333 /* Allocate storage to hold all of the information related to uniform
334 * blocks that can be queried through the API.
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
);
340 /* Add each variable from each uniform block to the API tracking
343 ubo_visitor
parcel(blocks
, variables
, num_variables
, prog
);
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
));
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
;
361 if ((create_ubo_blocks
&& !b
->is_shader_storage
) ||
362 (!create_ubo_blocks
&& b
->is_shader_storage
)) {
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
);
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
,
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;
381 gl_uniform_block_packing(block_type
->interface_packing
);
382 blocks
[i
]._RowMajor
= block_type
->get_interface_row_major();
384 parcel
.process(block_type
,
385 b
->has_instance_name
? block_type
->name
: "");
387 blocks
[i
].UniformBufferSize
= parcel
.buffer_size
;
389 /* Check SSBO size is lower than maximum supported size for SSBO
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
);
398 blocks
[i
].NumUniforms
= (unsigned)(ptrdiff_t)
399 (&variables
[parcel
.index
] - blocks
[i
].Uniforms
);
407 assert(parcel
.index
== num_variables
);
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
)
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.
424 struct hash_table
*block_hash
=
425 _mesa_hash_table_create(mem_ctx
, _mesa_key_hash_string
,
426 _mesa_key_string_equal
);
428 if (block_hash
== NULL
) {
429 _mesa_error_no_memory(__func__
);
430 linker_error(prog
, "out of memory\n");
434 /* Determine which uniform blocks are active.
436 link_uniform_block_active_visitor
v(mem_ctx
, block_hash
, prog
);
437 visit_list_elements(&v
, shader
->ir
);
439 /* Count the number of active uniform blocks. Count the total number of
440 * active slots in those uniform blocks.
442 unsigned num_ubo_variables
= 0;
443 unsigned num_ssbo_variables
= 0;
444 count_block_size block_size
;
445 struct hash_entry
*entry
;
447 hash_table_foreach (block_hash
, entry
) {
448 struct link_uniform_block_active
*const b
=
449 (struct link_uniform_block_active
*) entry
->data
;
451 assert((b
->array
!= NULL
) == b
->type
->is_array());
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
;
460 block_size
.num_active_uniforms
= 0;
461 block_size
.process(b
->type
->without_array(), "");
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
;
469 *num_ubo_blocks
+= aoa_size
;
470 num_ubo_variables
+= aoa_size
* block_size
.num_active_uniforms
;
473 if (b
->is_shader_storage
) {
474 (*num_ssbo_blocks
)++;
475 num_ssbo_variables
+= block_size
.num_active_uniforms
;
478 num_ubo_variables
+= block_size
.num_active_uniforms
;
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);
489 _mesa_hash_table_destroy(block_hash
, NULL
);
493 link_uniform_blocks_are_compatible(const gl_uniform_block
*a
,
494 const gl_uniform_block
*b
)
496 assert(strcmp(a
->Name
, b
->Name
) == 0);
498 /* Page 35 (page 42 of the PDF) in section 4.3.7 of the GLSL 1.50 spec says:
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."
507 * Arrays are not yet supported, so there is no check for that.
509 if (a
->NumUniforms
!= b
->NumUniforms
)
512 if (a
->_Packing
!= b
->_Packing
)
515 if (a
->_RowMajor
!= b
->_RowMajor
)
518 for (unsigned i
= 0; i
< a
->NumUniforms
; i
++) {
519 if (strcmp(a
->Uniforms
[i
].Name
, b
->Uniforms
[i
].Name
) != 0)
522 if (a
->Uniforms
[i
].Type
!= b
->Uniforms
[i
].Type
)
525 if (a
->Uniforms
[i
].RowMajor
!= b
->Uniforms
[i
].RowMajor
)
533 * Merges a uniform block into an array of uniform blocks that may or
534 * may not already contain a copy of it.
536 * Returns the index of the new block in the array.
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
)
544 for (unsigned int i
= 0; i
< *num_linked_blocks
; i
++) {
545 struct gl_uniform_block
*old_block
= &(*linked_blocks
)[i
];
547 if (strcmp(old_block
->Name
, new_block
->Name
) == 0)
548 return link_uniform_blocks_are_compatible(old_block
, new_block
)
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
];
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
);
563 memcpy(linked_block
->Uniforms
,
565 sizeof(*linked_block
->Uniforms
) * linked_block
->NumUniforms
);
567 linked_block
->Name
= ralloc_strdup(*linked_blocks
, linked_block
->Name
);
569 for (unsigned int i
= 0; i
< linked_block
->NumUniforms
; i
++) {
570 struct gl_uniform_buffer_variable
*ubo_var
=
571 &linked_block
->Uniforms
[i
];
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
;
577 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
578 ubo_var
->IndexName
= ralloc_strdup(*linked_blocks
, ubo_var
->IndexName
);
582 return linked_block_index
;