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/shader_types.h"
32
#include "main/consts_exts.h"
36
class ubo_visitor : public program_resource_visitor {
38
ubo_visitor(void *mem_ctx, gl_uniform_buffer_variable *variables,
39
unsigned num_variables, struct gl_shader_program *prog,
40
bool use_std430_as_default)
41
: index(0), offset(0), buffer_size(0), variables(variables),
42
num_variables(num_variables), mem_ctx(mem_ctx),
43
is_array_instance(false), prog(prog),
44
use_std430_as_default(use_std430_as_default)
49
void process(const glsl_type *type, const char *name)
52
this->buffer_size = 0;
53
this->is_array_instance = strchr(name, ']') != NULL;
54
this->program_resource_visitor::process(type, name,
55
use_std430_as_default);
61
gl_uniform_buffer_variable *variables;
62
unsigned num_variables;
64
bool is_array_instance;
65
struct gl_shader_program *prog;
68
virtual void enter_record(const glsl_type *type, const char *,
70
const enum glsl_interface_packing packing)
72
assert(type->is_struct());
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 *,
83
const enum glsl_interface_packing packing)
85
assert(type->is_struct());
87
/* If this is the last field of a structure, apply rule #9. The
88
* ARB_uniform_buffer_object spec says:
90
* The structure may have padding at the end; the base offset of the
91
* 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
/* The ARB_program_interface_query spec says:
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
148
* calculate its size. We don't need to check if the unsized array is
149
* the last member of a shader storage block (that check was already
150
* done by the parser).
152
const glsl_type *type_for_size = type;
153
if (type->is_unsized_array()) {
155
linker_error(prog, "unsized array `%s' definition: "
156
"only last member of a shader storage block "
157
"can be defined as unsized array",
161
type_for_size = type->without_array();
164
if (packing == GLSL_INTERFACE_PACKING_STD430) {
165
alignment = type->std430_base_alignment(v->RowMajor);
166
size = type_for_size->std430_size(v->RowMajor);
168
alignment = type->std140_base_alignment(v->RowMajor);
169
size = type_for_size->std140_size(v->RowMajor);
172
this->offset = glsl_align(this->offset, alignment);
173
v->Offset = this->offset;
175
this->offset += size;
177
/* The ARB_uniform_buffer_object spec says:
179
* For uniform blocks laid out according to [std140] rules, the
180
* minimum buffer object size returned by the UNIFORM_BLOCK_DATA_SIZE
181
* query is derived by taking the offset of the last basic machine
182
* unit consumed by the last uniform of the uniform block (including
183
* any end-of-array or end-of-structure padding), adding one, and
184
* rounding up to the next multiple of the base alignment required
187
this->buffer_size = glsl_align(this->offset, 16);
190
bool use_std430_as_default;
193
class count_block_size : public program_resource_visitor {
195
count_block_size() : num_active_uniforms(0)
200
unsigned num_active_uniforms;
203
virtual void visit_field(const glsl_type * /* type */,
204
const char * /* name */,
205
bool /* row_major */,
206
const glsl_type * /* record_type */,
207
const enum glsl_interface_packing,
208
bool /* last_field */)
210
this->num_active_uniforms++;
214
} /* anonymous namespace */
217
const glsl_type *type;
218
bool has_instance_name;
221
static void process_block_array_leaf(const char *name, gl_uniform_block *blocks,
223
gl_uniform_buffer_variable *variables,
224
const struct link_uniform_block_active *const b,
225
unsigned *block_index,
226
unsigned binding_offset,
227
unsigned linearized_index,
228
const struct gl_constants *consts,
229
struct gl_shader_program *prog);
233
* \param first_index Value of \c block_index for the first element of the
237
process_block_array(struct uniform_block_array_elements *ub_array, char **name,
238
size_t name_length, gl_uniform_block *blocks,
239
ubo_visitor *parcel, gl_uniform_buffer_variable *variables,
240
const struct link_uniform_block_active *const b,
241
unsigned *block_index, unsigned binding_offset,
242
const struct gl_constants *consts,
243
struct gl_shader_program *prog,
244
unsigned first_index)
246
for (unsigned j = 0; j < ub_array->num_array_elements; j++) {
247
size_t new_length = name_length;
249
unsigned int element_idx = ub_array->array_elements[j];
250
/* Append the subscript to the current variable name */
251
ralloc_asprintf_rewrite_tail(name, &new_length, "[%u]", element_idx);
253
if (ub_array->array) {
254
unsigned binding_stride = binding_offset + (element_idx *
255
ub_array->array->aoa_size);
256
process_block_array(ub_array->array, name, new_length, blocks,
257
parcel, variables, b, block_index,
258
binding_stride, consts, prog, first_index);
260
process_block_array_leaf(*name, blocks,
261
parcel, variables, b, block_index,
262
binding_offset + element_idx,
263
*block_index - first_index, consts, prog);
269
process_block_array_leaf(const char *name,
270
gl_uniform_block *blocks,
271
ubo_visitor *parcel, gl_uniform_buffer_variable *variables,
272
const struct link_uniform_block_active *const b,
273
unsigned *block_index, unsigned binding_offset,
274
unsigned linearized_index,
275
const struct gl_constants *consts,
276
struct gl_shader_program *prog)
278
unsigned i = *block_index;
279
const glsl_type *type = b->type->without_array();
281
blocks[i].name.string = ralloc_strdup(blocks, name);
282
resource_name_updated(&blocks[i].name);
283
blocks[i].Uniforms = &variables[(*parcel).index];
285
/* The ARB_shading_language_420pack spec says:
287
* If the binding identifier is used with a uniform block instanced as
288
* an array then the first element of the array takes the specified
289
* block binding and each subsequent element takes the next consecutive
290
* uniform block binding point.
292
blocks[i].Binding = (b->has_binding) ? b->binding + binding_offset : 0;
294
blocks[i].UniformBufferSize = 0;
295
blocks[i]._Packing = glsl_interface_packing(type->interface_packing);
296
blocks[i]._RowMajor = type->get_interface_row_major();
297
blocks[i].linearized_array_index = linearized_index;
299
parcel->process(type, b->has_instance_name ? blocks[i].name.string : "");
301
blocks[i].UniformBufferSize = parcel->buffer_size;
303
/* Check SSBO size is lower than maximum supported size for SSBO */
304
if (b->is_shader_storage &&
305
parcel->buffer_size > consts->MaxShaderStorageBlockSize) {
306
linker_error(prog, "shader storage block `%s' has size %d, "
307
"which is larger than the maximum allowed (%d)",
310
consts->MaxShaderStorageBlockSize);
312
blocks[i].NumUniforms =
313
(unsigned)(ptrdiff_t)(&variables[parcel->index] - blocks[i].Uniforms);
315
*block_index = *block_index + 1;
318
/* This function resizes the array types of the block so that later we can use
319
* this new size to correctly calculate the offest for indirect indexing.
321
static const glsl_type *
322
resize_block_array(const glsl_type *type,
323
struct uniform_block_array_elements *ub_array)
325
if (type->is_array()) {
326
struct uniform_block_array_elements *child_array =
327
type->fields.array->is_array() ? ub_array->array : NULL;
328
const glsl_type *new_child_type =
329
resize_block_array(type->fields.array, child_array);
331
const glsl_type *new_type =
332
glsl_type::get_array_instance(new_child_type,
333
ub_array->num_array_elements);
334
ub_array->ir->array->type = new_type;
342
create_buffer_blocks(void *mem_ctx, const struct gl_constants *consts,
343
struct gl_shader_program *prog,
344
struct gl_uniform_block **out_blks, unsigned num_blocks,
345
struct hash_table *block_hash, unsigned num_variables,
346
bool create_ubo_blocks)
348
if (num_blocks == 0) {
349
assert(num_variables == 0);
353
assert(num_variables != 0);
355
/* Allocate storage to hold all of the information related to uniform
356
* blocks that can be queried through the API.
358
struct gl_uniform_block *blocks =
359
rzalloc_array(mem_ctx, gl_uniform_block, num_blocks);
360
gl_uniform_buffer_variable *variables =
361
ralloc_array(blocks, gl_uniform_buffer_variable, num_variables);
363
/* Add each variable from each uniform block to the API tracking
366
ubo_visitor parcel(blocks, variables, num_variables, prog,
367
consts->UseSTD430AsDefaultPacking);
370
hash_table_foreach (block_hash, entry) {
371
const struct link_uniform_block_active *const b =
372
(const struct link_uniform_block_active *) entry->data;
373
const glsl_type *block_type = b->type;
375
if ((create_ubo_blocks && !b->is_shader_storage) ||
376
(!create_ubo_blocks && b->is_shader_storage)) {
378
if (b->array != NULL) {
379
char *name = ralloc_strdup(NULL,
380
block_type->without_array()->name);
381
size_t name_length = strlen(name);
383
assert(b->has_instance_name);
384
process_block_array(b->array, &name, name_length, blocks, &parcel,
385
variables, b, &i, 0, consts, prog,
389
process_block_array_leaf(block_type->name, blocks, &parcel,
398
assert(parcel.index == num_variables);
402
link_uniform_blocks(void *mem_ctx,
403
const struct gl_constants *consts,
404
struct gl_shader_program *prog,
405
struct gl_linked_shader *shader,
406
struct gl_uniform_block **ubo_blocks,
407
unsigned *num_ubo_blocks,
408
struct gl_uniform_block **ssbo_blocks,
409
unsigned *num_ssbo_blocks)
411
/* This hash table will track all of the uniform blocks that have been
412
* encountered. Since blocks with the same block-name must be the same,
413
* the hash is organized by block-name.
415
struct hash_table *block_hash =
416
_mesa_hash_table_create(mem_ctx, _mesa_hash_string,
417
_mesa_key_string_equal);
419
if (block_hash == NULL) {
420
_mesa_error_no_memory(__func__);
421
linker_error(prog, "out of memory\n");
425
/* Determine which uniform blocks are active. */
426
link_uniform_block_active_visitor v(mem_ctx, block_hash, prog);
427
visit_list_elements(&v, shader->ir);
429
/* Count the number of active uniform blocks. Count the total number of
430
* active slots in those uniform blocks.
432
unsigned num_ubo_variables = 0;
433
unsigned num_ssbo_variables = 0;
434
count_block_size block_size;
436
hash_table_foreach (block_hash, entry) {
437
struct link_uniform_block_active *const b =
438
(struct link_uniform_block_active *) entry->data;
440
assert((b->array != NULL) == b->type->is_array());
442
if (b->array != NULL &&
443
(b->type->without_array()->interface_packing ==
444
GLSL_INTERFACE_PACKING_PACKED)) {
445
b->type = resize_block_array(b->type, b->array);
446
b->var->type = b->type;
447
b->var->data.max_array_access = b->type->length - 1;
450
block_size.num_active_uniforms = 0;
451
block_size.process(b->type->without_array(), "",
452
consts->UseSTD430AsDefaultPacking);
454
if (b->array != NULL) {
455
unsigned aoa_size = b->type->arrays_of_arrays_size();
456
if (b->is_shader_storage) {
457
*num_ssbo_blocks += aoa_size;
458
num_ssbo_variables += aoa_size * block_size.num_active_uniforms;
460
*num_ubo_blocks += aoa_size;
461
num_ubo_variables += aoa_size * block_size.num_active_uniforms;
464
if (b->is_shader_storage) {
465
(*num_ssbo_blocks)++;
466
num_ssbo_variables += block_size.num_active_uniforms;
469
num_ubo_variables += block_size.num_active_uniforms;
475
create_buffer_blocks(mem_ctx, consts, prog, ubo_blocks, *num_ubo_blocks,
476
block_hash, num_ubo_variables, true);
477
create_buffer_blocks(mem_ctx, consts, prog, ssbo_blocks, *num_ssbo_blocks,
478
block_hash, num_ssbo_variables, false);
480
_mesa_hash_table_destroy(block_hash, NULL);
484
link_uniform_blocks_are_compatible(const gl_uniform_block *a,
485
const gl_uniform_block *b)
487
assert(strcmp(a->name.string, b->name.string) == 0);
489
/* Page 35 (page 42 of the PDF) in section 4.3.7 of the GLSL 1.50 spec says:
491
* Matched block names within an interface (as defined above) must match
492
* in terms of having the same number of declarations with the same
493
* sequence of types and the same sequence of member names, as well as
494
* having the same member-wise layout qualification....if a matching
495
* block is declared as an array, then the array sizes must also
496
* match... Any mismatch will generate a link error.
498
* Arrays are not yet supported, so there is no check for that.
500
if (a->NumUniforms != b->NumUniforms)
503
if (a->_Packing != b->_Packing)
506
if (a->_RowMajor != b->_RowMajor)
509
if (a->Binding != b->Binding)
512
for (unsigned i = 0; i < a->NumUniforms; i++) {
513
if (strcmp(a->Uniforms[i].Name, b->Uniforms[i].Name) != 0)
516
if (a->Uniforms[i].Type != b->Uniforms[i].Type)
519
if (a->Uniforms[i].RowMajor != b->Uniforms[i].RowMajor)
527
* Merges a uniform block into an array of uniform blocks that may or
528
* may not already contain a copy of it.
530
* Returns the index of the new block in the array.
533
link_cross_validate_uniform_block(void *mem_ctx,
534
struct gl_uniform_block **linked_blocks,
535
unsigned int *num_linked_blocks,
536
struct gl_uniform_block *new_block)
538
for (unsigned int i = 0; i < *num_linked_blocks; i++) {
539
struct gl_uniform_block *old_block = &(*linked_blocks)[i];
541
if (strcmp(old_block->name.string, new_block->name.string) == 0)
542
return link_uniform_blocks_are_compatible(old_block, new_block)
546
*linked_blocks = reralloc(mem_ctx, *linked_blocks,
547
struct gl_uniform_block,
548
*num_linked_blocks + 1);
549
int linked_block_index = (*num_linked_blocks)++;
550
struct gl_uniform_block *linked_block = &(*linked_blocks)[linked_block_index];
552
memcpy(linked_block, new_block, sizeof(*new_block));
553
linked_block->Uniforms = ralloc_array(*linked_blocks,
554
struct gl_uniform_buffer_variable,
555
linked_block->NumUniforms);
557
memcpy(linked_block->Uniforms,
559
sizeof(*linked_block->Uniforms) * linked_block->NumUniforms);
561
linked_block->name.string = ralloc_strdup(*linked_blocks, linked_block->name.string);
562
resource_name_updated(&linked_block->name);
564
for (unsigned int i = 0; i < linked_block->NumUniforms; i++) {
565
struct gl_uniform_buffer_variable *ubo_var =
566
&linked_block->Uniforms[i];
568
if (ubo_var->Name == ubo_var->IndexName) {
569
ubo_var->Name = ralloc_strdup(*linked_blocks, ubo_var->Name);
570
ubo_var->IndexName = ubo_var->Name;
572
ubo_var->Name = ralloc_strdup(*linked_blocks, ubo_var->Name);
573
ubo_var->IndexName = ralloc_strdup(*linked_blocks, ubo_var->IndexName);
577
return linked_block_index;