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 *,
72 assert(type
->is_record());
73 this->offset
= glsl_align(
74 this->offset
, type
->std140_base_alignment(row_major
));
77 virtual void leave_record(const glsl_type
*type
, const char *,
79 assert(type
->is_record());
81 /* If this is the last field of a structure, apply rule #9. The
82 * GL_ARB_uniform_buffer_object spec says:
84 * "The structure may have padding at the end; the base offset of
85 * the member following the sub-structure is rounded up to the next
86 * multiple of the base alignment of the structure."
88 this->offset
= glsl_align(
89 this->offset
, type
->std140_base_alignment(row_major
));
92 virtual void visit_field(const glsl_type
*type
, const char *name
,
93 bool row_major
, const glsl_type
*,
94 bool /* last_field */)
96 assert(this->index
< this->num_variables
);
98 gl_uniform_buffer_variable
*v
= &this->variables
[this->index
++];
100 v
->Name
= ralloc_strdup(mem_ctx
, name
);
102 v
->RowMajor
= type
->without_array()->is_matrix() && row_major
;
104 if (this->is_array_instance
) {
105 v
->IndexName
= ralloc_strdup(mem_ctx
, name
);
107 char *open_bracket
= strchr(v
->IndexName
, '[');
108 assert(open_bracket
!= NULL
);
110 char *close_bracket
= strchr(open_bracket
, ']');
111 assert(close_bracket
!= NULL
);
113 /* Length of the tail without the ']' but with the NUL.
115 unsigned len
= strlen(close_bracket
+ 1) + 1;
117 memmove(open_bracket
, close_bracket
+ 1, len
);
119 v
->IndexName
= v
->Name
;
122 unsigned alignment
= 0;
125 if (v
->Type
->interface_packing
== GLSL_INTERFACE_PACKING_STD430
) {
126 alignment
= type
->std430_base_alignment(v
->RowMajor
);
127 size
= type
->std430_size(v
->RowMajor
);
129 alignment
= type
->std140_base_alignment(v
->RowMajor
);
130 size
= type
->std140_size(v
->RowMajor
);
133 this->offset
= glsl_align(this->offset
, alignment
);
134 v
->Offset
= this->offset
;
136 this->offset
+= size
;
138 /* From the GL_ARB_uniform_buffer_object spec:
140 * "For uniform blocks laid out according to [std140] rules, the
141 * minimum buffer object size returned by the
142 * UNIFORM_BLOCK_DATA_SIZE query is derived by taking the offset of
143 * the last basic machine unit consumed by the last uniform of the
144 * uniform block (including any end-of-array or end-of-structure
145 * padding), adding one, and rounding up to the next multiple of
146 * the base alignment required for a vec4."
148 this->buffer_size
= glsl_align(this->offset
, 16);
152 class count_block_size
: public program_resource_visitor
{
154 count_block_size() : num_active_uniforms(0)
159 unsigned num_active_uniforms
;
162 virtual void visit_field(const glsl_type
*type
, const char *name
,
168 this->num_active_uniforms
++;
172 } /* anonymous namespace */
175 const glsl_type
*type
;
176 bool has_instance_name
;
180 link_uniform_blocks(void *mem_ctx
,
181 struct gl_shader_program
*prog
,
182 struct gl_shader
**shader_list
,
183 unsigned num_shaders
,
184 struct gl_uniform_block
**blocks_ret
)
186 /* This hash table will track all of the uniform blocks that have been
187 * encountered. Since blocks with the same block-name must be the same,
188 * the hash is organized by block-name.
190 struct hash_table
*block_hash
=
191 _mesa_hash_table_create(mem_ctx
, _mesa_key_hash_string
,
192 _mesa_key_string_equal
);
194 if (block_hash
== NULL
) {
195 _mesa_error_no_memory(__func__
);
196 linker_error(prog
, "out of memory\n");
200 /* Determine which uniform blocks are active.
202 link_uniform_block_active_visitor
v(mem_ctx
, block_hash
, prog
);
203 for (unsigned i
= 0; i
< num_shaders
; i
++) {
204 visit_list_elements(&v
, shader_list
[i
]->ir
);
207 /* Count the number of active uniform blocks. Count the total number of
208 * active slots in those uniform blocks.
210 unsigned num_blocks
= 0;
211 unsigned num_variables
= 0;
212 count_block_size block_size
;
213 struct hash_entry
*entry
;
215 hash_table_foreach (block_hash
, entry
) {
216 const struct link_uniform_block_active
*const b
=
217 (const struct link_uniform_block_active
*) entry
->data
;
219 const glsl_type
*const block_type
=
220 b
->type
->is_array() ? b
->type
->fields
.array
: b
->type
;
222 assert((b
->num_array_elements
> 0) == b
->type
->is_array());
224 block_size
.num_active_uniforms
= 0;
225 block_size
.process(block_type
, "");
227 if (b
->num_array_elements
> 0) {
228 num_blocks
+= b
->num_array_elements
;
229 num_variables
+= b
->num_array_elements
230 * block_size
.num_active_uniforms
;
233 num_variables
+= block_size
.num_active_uniforms
;
238 if (num_blocks
== 0) {
239 assert(num_variables
== 0);
240 _mesa_hash_table_destroy(block_hash
, NULL
);
244 assert(num_variables
!= 0);
246 /* Allocate storage to hold all of the informatation related to uniform
247 * blocks that can be queried through the API.
249 gl_uniform_block
*blocks
=
250 ralloc_array(mem_ctx
, gl_uniform_block
, num_blocks
);
251 gl_uniform_buffer_variable
*variables
=
252 ralloc_array(blocks
, gl_uniform_buffer_variable
, num_variables
);
254 /* Add each variable from each uniform block to the API tracking
258 ubo_visitor
parcel(blocks
, variables
, num_variables
);
260 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_STD140
)
261 == unsigned(ubo_packing_std140
));
262 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_SHARED
)
263 == unsigned(ubo_packing_shared
));
264 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_PACKED
)
265 == unsigned(ubo_packing_packed
));
266 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_STD430
)
267 == unsigned(ubo_packing_std430
));
269 hash_table_foreach (block_hash
, entry
) {
270 const struct link_uniform_block_active
*const b
=
271 (const struct link_uniform_block_active
*) entry
->data
;
272 const glsl_type
*block_type
= b
->type
;
274 if (b
->num_array_elements
> 0) {
275 const char *const name
= block_type
->fields
.array
->name
;
277 assert(b
->has_instance_name
);
278 for (unsigned j
= 0; j
< b
->num_array_elements
; j
++) {
279 blocks
[i
].Name
= ralloc_asprintf(blocks
, "%s[%u]", name
,
280 b
->array_elements
[j
]);
281 blocks
[i
].Uniforms
= &variables
[parcel
.index
];
283 /* The GL_ARB_shading_language_420pack spec says:
285 * "If the binding identifier is used with a uniform block
286 * instanced as an array then the first element of the array
287 * takes the specified block binding and each subsequent
288 * element takes the next consecutive uniform block binding
291 blocks
[i
].Binding
= (b
->has_binding
) ? b
->binding
+ j
: 0;
293 blocks
[i
].UniformBufferSize
= 0;
295 gl_uniform_block_packing(block_type
->interface_packing
);
297 parcel
.process(block_type
->fields
.array
,
300 blocks
[i
].UniformBufferSize
= parcel
.buffer_size
;
302 blocks
[i
].NumUniforms
=
303 (unsigned)(ptrdiff_t)(&variables
[parcel
.index
] - blocks
[i
].Uniforms
);
305 blocks
[i
].IsShaderStorage
= b
->is_shader_storage
;
310 blocks
[i
].Name
= ralloc_strdup(blocks
, block_type
->name
);
311 blocks
[i
].Uniforms
= &variables
[parcel
.index
];
312 blocks
[i
].Binding
= (b
->has_binding
) ? b
->binding
: 0;
313 blocks
[i
].UniformBufferSize
= 0;
315 gl_uniform_block_packing(block_type
->interface_packing
);
317 parcel
.process(block_type
,
318 b
->has_instance_name
? block_type
->name
: "");
320 blocks
[i
].UniformBufferSize
= parcel
.buffer_size
;
322 blocks
[i
].NumUniforms
=
323 (unsigned)(ptrdiff_t)(&variables
[parcel
.index
] - blocks
[i
].Uniforms
);
325 blocks
[i
].IsShaderStorage
= b
->is_shader_storage
;
331 assert(parcel
.index
== num_variables
);
333 _mesa_hash_table_destroy(block_hash
, NULL
);
335 *blocks_ret
= blocks
;
340 link_uniform_blocks_are_compatible(const gl_uniform_block
*a
,
341 const gl_uniform_block
*b
)
343 assert(strcmp(a
->Name
, b
->Name
) == 0);
345 /* Page 35 (page 42 of the PDF) in section 4.3.7 of the GLSL 1.50 spec says:
347 * "Matched block names within an interface (as defined above) must
348 * match in terms of having the same number of declarations with the
349 * same sequence of types and the same sequence of member names, as
350 * well as having the same member-wise layout qualification....if a
351 * matching block is declared as an array, then the array sizes must
352 * also match... Any mismatch will generate a link error."
354 * Arrays are not yet supported, so there is no check for that.
356 if (a
->NumUniforms
!= b
->NumUniforms
)
359 if (a
->_Packing
!= b
->_Packing
)
362 for (unsigned i
= 0; i
< a
->NumUniforms
; i
++) {
363 if (strcmp(a
->Uniforms
[i
].Name
, b
->Uniforms
[i
].Name
) != 0)
366 if (a
->Uniforms
[i
].Type
!= b
->Uniforms
[i
].Type
)
369 if (a
->Uniforms
[i
].RowMajor
!= b
->Uniforms
[i
].RowMajor
)