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
)
38 : index(0), offset(0), buffer_size(0), variables(variables
),
39 num_variables(num_variables
), mem_ctx(mem_ctx
), is_array_instance(false)
44 void process(const glsl_type
*type
, const char *name
)
47 this->buffer_size
= 0;
48 this->is_array_instance
= strchr(name
, ']') != NULL
;
49 this->program_resource_visitor::process(type
, name
);
55 gl_uniform_buffer_variable
*variables
;
56 unsigned num_variables
;
58 bool is_array_instance
;
61 virtual void visit_field(const glsl_type
*type
, const char *name
,
67 assert(!"Should not get here.");
70 virtual void enter_record(const glsl_type
*type
, const char *,
71 bool row_major
, const unsigned packing
) {
72 assert(type
->is_record());
73 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
74 this->offset
= glsl_align(
75 this->offset
, type
->std430_base_alignment(row_major
));
77 this->offset
= glsl_align(
78 this->offset
, type
->std140_base_alignment(row_major
));
81 virtual void leave_record(const glsl_type
*type
, const char *,
82 bool row_major
, const unsigned packing
) {
83 assert(type
->is_record());
85 /* If this is the last field of a structure, apply rule #9. The
86 * GL_ARB_uniform_buffer_object spec says:
88 * "The structure may have padding at the end; the base offset of
89 * the member following the sub-structure is rounded up to the next
90 * multiple of the base alignment of the structure."
92 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
93 this->offset
= glsl_align(
94 this->offset
, type
->std430_base_alignment(row_major
));
96 this->offset
= glsl_align(
97 this->offset
, type
->std140_base_alignment(row_major
));
100 virtual void visit_field(const glsl_type
*type
, const char *name
,
101 bool row_major
, const glsl_type
*,
102 const unsigned packing
,
103 bool /* last_field */)
105 assert(this->index
< this->num_variables
);
107 gl_uniform_buffer_variable
*v
= &this->variables
[this->index
++];
109 v
->Name
= ralloc_strdup(mem_ctx
, name
);
111 v
->RowMajor
= type
->without_array()->is_matrix() && row_major
;
113 if (this->is_array_instance
) {
114 v
->IndexName
= ralloc_strdup(mem_ctx
, name
);
116 char *open_bracket
= strchr(v
->IndexName
, '[');
117 assert(open_bracket
!= NULL
);
119 char *close_bracket
= strchr(open_bracket
, '.') - 1;
120 assert(close_bracket
!= NULL
);
122 /* Length of the tail without the ']' but with the NUL.
124 unsigned len
= strlen(close_bracket
+ 1) + 1;
126 memmove(open_bracket
, close_bracket
+ 1, len
);
128 v
->IndexName
= v
->Name
;
131 unsigned alignment
= 0;
134 if (packing
== GLSL_INTERFACE_PACKING_STD430
) {
135 alignment
= type
->std430_base_alignment(v
->RowMajor
);
136 size
= type
->std430_size(v
->RowMajor
);
138 alignment
= type
->std140_base_alignment(v
->RowMajor
);
139 size
= type
->std140_size(v
->RowMajor
);
142 this->offset
= glsl_align(this->offset
, alignment
);
143 v
->Offset
= this->offset
;
145 this->offset
+= size
;
147 /* From the GL_ARB_uniform_buffer_object spec:
149 * "For uniform blocks laid out according to [std140] rules, the
150 * minimum buffer object size returned by the
151 * UNIFORM_BLOCK_DATA_SIZE query is derived by taking the offset of
152 * the last basic machine unit consumed by the last uniform of the
153 * uniform block (including any end-of-array or end-of-structure
154 * padding), adding one, and rounding up to the next multiple of
155 * the base alignment required for a vec4."
157 this->buffer_size
= glsl_align(this->offset
, 16);
161 class count_block_size
: public program_resource_visitor
{
163 count_block_size() : num_active_uniforms(0)
168 unsigned num_active_uniforms
;
171 virtual void visit_field(const glsl_type
*type
, const char *name
,
177 this->num_active_uniforms
++;
181 } /* anonymous namespace */
184 const glsl_type
*type
;
185 bool has_instance_name
;
189 process_block_array(struct uniform_block_array_elements
*ub_array
, char **name
,
190 size_t name_length
, gl_uniform_block
*blocks
,
191 ubo_visitor
*parcel
, gl_uniform_buffer_variable
*variables
,
192 const struct link_uniform_block_active
*const b
,
193 unsigned *block_index
, unsigned *binding_offset
,
194 struct gl_context
*ctx
, struct gl_shader_program
*prog
)
197 for (unsigned j
= 0; j
< ub_array
->num_array_elements
; j
++) {
198 size_t new_length
= name_length
;
200 /* Append the subscript to the current variable name */
201 ralloc_asprintf_rewrite_tail(name
, &new_length
, "[%u]",
202 ub_array
->array_elements
[j
]);
204 process_block_array(ub_array
->array
, name
, new_length
, blocks
,
205 parcel
, variables
, b
, block_index
,
206 binding_offset
, ctx
, prog
);
209 unsigned i
= *block_index
;
210 const glsl_type
*type
= b
->type
->without_array();
212 blocks
[i
].Name
= ralloc_strdup(blocks
, *name
);
213 blocks
[i
].Uniforms
= &variables
[(*parcel
).index
];
215 /* The GL_ARB_shading_language_420pack spec says:
217 * "If the binding identifier is used with a uniform block
218 * instanced as an array then the first element of the array
219 * takes the specified block binding and each subsequent
220 * element takes the next consecutive uniform block binding
223 blocks
[i
].Binding
= (b
->has_binding
) ? b
->binding
+ *binding_offset
: 0;
225 blocks
[i
].UniformBufferSize
= 0;
226 blocks
[i
]._Packing
= gl_uniform_block_packing(type
->interface_packing
);
228 parcel
->process(type
, blocks
[i
].Name
);
230 blocks
[i
].UniformBufferSize
= parcel
->buffer_size
;
232 /* Check SSBO size is lower than maximum supported size for SSBO */
233 if (b
->is_shader_storage
&&
234 parcel
->buffer_size
> ctx
->Const
.MaxShaderStorageBlockSize
) {
235 linker_error(prog
, "shader storage block `%s' has size %d, "
236 "which is larger than than the maximum allowed (%d)",
239 ctx
->Const
.MaxShaderStorageBlockSize
);
241 blocks
[i
].NumUniforms
=
242 (unsigned)(ptrdiff_t)(&variables
[parcel
->index
] - blocks
[i
].Uniforms
);
243 blocks
[i
].IsShaderStorage
= b
->is_shader_storage
;
245 *block_index
= *block_index
+ 1;
246 *binding_offset
= *binding_offset
+ 1;
250 /* This function resizes the array types of the block so that later we can use
251 * this new size to correctly calculate the offest for indirect indexing.
254 resize_block_array(const glsl_type
*type
,
255 struct uniform_block_array_elements
*ub_array
)
257 if (type
->is_array()) {
258 struct uniform_block_array_elements
*child_array
=
259 type
->fields
.array
->is_array() ? ub_array
->array
: NULL
;
260 const glsl_type
*new_child_type
=
261 resize_block_array(type
->fields
.array
, child_array
);
263 const glsl_type
*new_type
=
264 glsl_type::get_array_instance(new_child_type
,
265 ub_array
->num_array_elements
);
266 ub_array
->ir
->array
->type
= new_type
;
274 link_uniform_blocks(void *mem_ctx
,
275 struct gl_context
*ctx
,
276 struct gl_shader_program
*prog
,
277 struct gl_shader
**shader_list
,
278 unsigned num_shaders
,
279 struct gl_uniform_block
**blocks_ret
)
281 /* This hash table will track all of the uniform blocks that have been
282 * encountered. Since blocks with the same block-name must be the same,
283 * the hash is organized by block-name.
285 struct hash_table
*block_hash
=
286 _mesa_hash_table_create(mem_ctx
, _mesa_key_hash_string
,
287 _mesa_key_string_equal
);
289 if (block_hash
== NULL
) {
290 _mesa_error_no_memory(__func__
);
291 linker_error(prog
, "out of memory\n");
295 /* Determine which uniform blocks are active.
297 link_uniform_block_active_visitor
v(mem_ctx
, block_hash
, prog
);
298 for (unsigned i
= 0; i
< num_shaders
; i
++) {
299 visit_list_elements(&v
, shader_list
[i
]->ir
);
302 /* Count the number of active uniform blocks. Count the total number of
303 * active slots in those uniform blocks.
305 unsigned num_blocks
= 0;
306 unsigned num_variables
= 0;
307 count_block_size block_size
;
308 struct hash_entry
*entry
;
310 hash_table_foreach (block_hash
, entry
) {
311 struct link_uniform_block_active
*const b
=
312 (struct link_uniform_block_active
*) entry
->data
;
314 assert((b
->array
!= NULL
) == b
->type
->is_array());
316 if (b
->array
!= NULL
&&
317 (b
->type
->without_array()->interface_packing
==
318 GLSL_INTERFACE_PACKING_PACKED
)) {
319 b
->type
= resize_block_array(b
->type
, b
->array
);
320 b
->var
->type
= b
->type
;
323 block_size
.num_active_uniforms
= 0;
324 block_size
.process(b
->type
->without_array(), "");
326 if (b
->array
!= NULL
) {
327 unsigned aoa_size
= b
->type
->arrays_of_arrays_size();
328 num_blocks
+= aoa_size
;
329 num_variables
+= aoa_size
* block_size
.num_active_uniforms
;
332 num_variables
+= block_size
.num_active_uniforms
;
337 if (num_blocks
== 0) {
338 assert(num_variables
== 0);
339 _mesa_hash_table_destroy(block_hash
, NULL
);
343 assert(num_variables
!= 0);
345 /* Allocate storage to hold all of the informatation related to uniform
346 * blocks that can be queried through the API.
348 gl_uniform_block
*blocks
=
349 ralloc_array(mem_ctx
, gl_uniform_block
, num_blocks
);
350 gl_uniform_buffer_variable
*variables
=
351 ralloc_array(blocks
, gl_uniform_buffer_variable
, num_variables
);
353 /* Add each variable from each uniform block to the API tracking
357 ubo_visitor
parcel(blocks
, variables
, num_variables
);
359 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_STD140
)
360 == unsigned(ubo_packing_std140
));
361 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_SHARED
)
362 == unsigned(ubo_packing_shared
));
363 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_PACKED
)
364 == unsigned(ubo_packing_packed
));
365 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_STD430
)
366 == unsigned(ubo_packing_std430
));
368 hash_table_foreach (block_hash
, entry
) {
369 const struct link_uniform_block_active
*const b
=
370 (const struct link_uniform_block_active
*) entry
->data
;
371 const glsl_type
*block_type
= b
->type
;
373 if (b
->array
!= NULL
) {
374 unsigned binding_offset
= 0;
375 char *name
= ralloc_strdup(NULL
, block_type
->without_array()->name
);
376 size_t name_length
= strlen(name
);
378 assert(b
->has_instance_name
);
379 process_block_array(b
->array
, &name
, name_length
, blocks
, &parcel
,
380 variables
, b
, &i
, &binding_offset
, ctx
, prog
);
383 blocks
[i
].Name
= ralloc_strdup(blocks
, block_type
->name
);
384 blocks
[i
].Uniforms
= &variables
[parcel
.index
];
385 blocks
[i
].Binding
= (b
->has_binding
) ? b
->binding
: 0;
386 blocks
[i
].UniformBufferSize
= 0;
388 gl_uniform_block_packing(block_type
->interface_packing
);
390 parcel
.process(block_type
,
391 b
->has_instance_name
? block_type
->name
: "");
393 blocks
[i
].UniformBufferSize
= parcel
.buffer_size
;
395 /* Check SSBO size is lower than maximum supported size for SSBO */
396 if (b
->is_shader_storage
&&
397 parcel
.buffer_size
> ctx
->Const
.MaxShaderStorageBlockSize
) {
398 linker_error(prog
, "shader storage block `%s' has size %d, "
399 "which is larger than than the maximum allowed (%d)",
402 ctx
->Const
.MaxShaderStorageBlockSize
);
404 blocks
[i
].NumUniforms
=
405 (unsigned)(ptrdiff_t)(&variables
[parcel
.index
] - blocks
[i
].Uniforms
);
407 blocks
[i
].IsShaderStorage
= b
->is_shader_storage
;
413 assert(parcel
.index
== num_variables
);
415 _mesa_hash_table_destroy(block_hash
, NULL
);
417 *blocks_ret
= blocks
;
422 link_uniform_blocks_are_compatible(const gl_uniform_block
*a
,
423 const gl_uniform_block
*b
)
425 assert(strcmp(a
->Name
, b
->Name
) == 0);
427 /* Page 35 (page 42 of the PDF) in section 4.3.7 of the GLSL 1.50 spec says:
429 * "Matched block names within an interface (as defined above) must
430 * match in terms of having the same number of declarations with the
431 * same sequence of types and the same sequence of member names, as
432 * well as having the same member-wise layout qualification....if a
433 * matching block is declared as an array, then the array sizes must
434 * also match... Any mismatch will generate a link error."
436 * Arrays are not yet supported, so there is no check for that.
438 if (a
->NumUniforms
!= b
->NumUniforms
)
441 if (a
->_Packing
!= b
->_Packing
)
444 for (unsigned i
= 0; i
< a
->NumUniforms
; i
++) {
445 if (strcmp(a
->Uniforms
[i
].Name
, b
->Uniforms
[i
].Name
) != 0)
448 if (a
->Uniforms
[i
].Type
!= b
->Uniforms
[i
].Type
)
451 if (a
->Uniforms
[i
].RowMajor
!= b
->Uniforms
[i
].RowMajor
)