d5d30bb0a0dd39497084fc42e65a8865197e5099
[mesa.git] / src / glsl / link_uniform_blocks.cpp
1 /*
2 * Copyright © 2012 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 #include "main/core.h"
25 #include "ir.h"
26 #include "linker.h"
27 #include "ir_uniform.h"
28 #include "link_uniform_block_active_visitor.h"
29 #include "util/hash_table.h"
30 #include "program.h"
31
32 namespace {
33
34 class ubo_visitor : public program_resource_visitor {
35 public:
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)
40 {
41 /* empty */
42 }
43
44 void process(const glsl_type *type, const char *name)
45 {
46 this->offset = 0;
47 this->buffer_size = 0;
48 this->is_array_instance = strchr(name, ']') != NULL;
49 this->program_resource_visitor::process(type, name);
50 }
51
52 unsigned index;
53 unsigned offset;
54 unsigned buffer_size;
55 gl_uniform_buffer_variable *variables;
56 unsigned num_variables;
57 void *mem_ctx;
58 bool is_array_instance;
59
60 private:
61 virtual void visit_field(const glsl_type *type, const char *name,
62 bool row_major)
63 {
64 (void) type;
65 (void) name;
66 (void) row_major;
67 assert(!"Should not get here.");
68 }
69
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));
76 else
77 this->offset = glsl_align(
78 this->offset, type->std140_base_alignment(row_major));
79 }
80
81 virtual void leave_record(const glsl_type *type, const char *,
82 bool row_major, const unsigned packing) {
83 assert(type->is_record());
84
85 /* If this is the last field of a structure, apply rule #9. The
86 * GL_ARB_uniform_buffer_object spec says:
87 *
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."
91 */
92 if (packing == GLSL_INTERFACE_PACKING_STD430)
93 this->offset = glsl_align(
94 this->offset, type->std430_base_alignment(row_major));
95 else
96 this->offset = glsl_align(
97 this->offset, type->std140_base_alignment(row_major));
98 }
99
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)
104 {
105 assert(this->index < this->num_variables);
106
107 gl_uniform_buffer_variable *v = &this->variables[this->index++];
108
109 v->Name = ralloc_strdup(mem_ctx, name);
110 v->Type = type;
111 v->RowMajor = type->without_array()->is_matrix() && row_major;
112
113 if (this->is_array_instance) {
114 v->IndexName = ralloc_strdup(mem_ctx, name);
115
116 char *open_bracket = strchr(v->IndexName, '[');
117 assert(open_bracket != NULL);
118
119 char *close_bracket = strchr(open_bracket, '.') - 1;
120 assert(close_bracket != NULL);
121
122 /* Length of the tail without the ']' but with the NUL.
123 */
124 unsigned len = strlen(close_bracket + 1) + 1;
125
126 memmove(open_bracket, close_bracket + 1, len);
127 } else {
128 v->IndexName = v->Name;
129 }
130
131 unsigned alignment = 0;
132 unsigned size = 0;
133
134 /* From ARB_program_interface_query:
135 *
136 * "If the final member of an active shader storage block is array
137 * with no declared size, the minimum buffer size is computed
138 * assuming the array was declared as an array with one element."
139 *
140 * For that reason, we use the base type of the unsized array to calculate
141 * its size. We don't need to check if the unsized array is the last member
142 * of a shader storage block (that check was already done by the parser).
143 */
144 const glsl_type *type_for_size = type;
145 if (type->is_unsized_array()) {
146 assert(last_field);
147 type_for_size = type->without_array();
148 }
149
150 if (packing == GLSL_INTERFACE_PACKING_STD430) {
151 alignment = type->std430_base_alignment(v->RowMajor);
152 size = type_for_size->std430_size(v->RowMajor);
153 } else {
154 alignment = type->std140_base_alignment(v->RowMajor);
155 size = type_for_size->std140_size(v->RowMajor);
156 }
157
158 this->offset = glsl_align(this->offset, alignment);
159 v->Offset = this->offset;
160
161 this->offset += size;
162
163 /* From the GL_ARB_uniform_buffer_object spec:
164 *
165 * "For uniform blocks laid out according to [std140] rules, the
166 * minimum buffer object size returned by the
167 * UNIFORM_BLOCK_DATA_SIZE query is derived by taking the offset of
168 * the last basic machine unit consumed by the last uniform of the
169 * uniform block (including any end-of-array or end-of-structure
170 * padding), adding one, and rounding up to the next multiple of
171 * the base alignment required for a vec4."
172 */
173 this->buffer_size = glsl_align(this->offset, 16);
174 }
175 };
176
177 class count_block_size : public program_resource_visitor {
178 public:
179 count_block_size() : num_active_uniforms(0)
180 {
181 /* empty */
182 }
183
184 unsigned num_active_uniforms;
185
186 private:
187 virtual void visit_field(const glsl_type *type, const char *name,
188 bool row_major)
189 {
190 (void) type;
191 (void) name;
192 (void) row_major;
193 this->num_active_uniforms++;
194 }
195 };
196
197 } /* anonymous namespace */
198
199 struct block {
200 const glsl_type *type;
201 bool has_instance_name;
202 };
203
204 static void
205 process_block_array(struct uniform_block_array_elements *ub_array, char **name,
206 size_t name_length, gl_uniform_block *blocks,
207 ubo_visitor *parcel, gl_uniform_buffer_variable *variables,
208 const struct link_uniform_block_active *const b,
209 unsigned *block_index, unsigned *binding_offset,
210 struct gl_context *ctx, struct gl_shader_program *prog)
211 {
212 if (ub_array) {
213 for (unsigned j = 0; j < ub_array->num_array_elements; j++) {
214 size_t new_length = name_length;
215
216 /* Append the subscript to the current variable name */
217 ralloc_asprintf_rewrite_tail(name, &new_length, "[%u]",
218 ub_array->array_elements[j]);
219
220 process_block_array(ub_array->array, name, new_length, blocks,
221 parcel, variables, b, block_index,
222 binding_offset, ctx, prog);
223 }
224 } else {
225 unsigned i = *block_index;
226 const glsl_type *type = b->type->without_array();
227
228 blocks[i].Name = ralloc_strdup(blocks, *name);
229 blocks[i].Uniforms = &variables[(*parcel).index];
230
231 /* The GL_ARB_shading_language_420pack spec says:
232 *
233 * "If the binding identifier is used with a uniform block
234 * instanced as an array then the first element of the array
235 * takes the specified block binding and each subsequent
236 * element takes the next consecutive uniform block binding
237 * point."
238 */
239 blocks[i].Binding = (b->has_binding) ? b->binding + *binding_offset : 0;
240
241 blocks[i].UniformBufferSize = 0;
242 blocks[i]._Packing = gl_uniform_block_packing(type->interface_packing);
243
244 parcel->process(type, blocks[i].Name);
245
246 blocks[i].UniformBufferSize = parcel->buffer_size;
247
248 /* Check SSBO size is lower than maximum supported size for SSBO */
249 if (b->is_shader_storage &&
250 parcel->buffer_size > ctx->Const.MaxShaderStorageBlockSize) {
251 linker_error(prog, "shader storage block `%s' has size %d, "
252 "which is larger than than the maximum allowed (%d)",
253 b->type->name,
254 parcel->buffer_size,
255 ctx->Const.MaxShaderStorageBlockSize);
256 }
257 blocks[i].NumUniforms =
258 (unsigned)(ptrdiff_t)(&variables[parcel->index] - blocks[i].Uniforms);
259 blocks[i].IsShaderStorage = b->is_shader_storage;
260
261 *block_index = *block_index + 1;
262 *binding_offset = *binding_offset + 1;
263 }
264 }
265
266 /* This function resizes the array types of the block so that later we can use
267 * this new size to correctly calculate the offest for indirect indexing.
268 */
269 const glsl_type *
270 resize_block_array(const glsl_type *type,
271 struct uniform_block_array_elements *ub_array)
272 {
273 if (type->is_array()) {
274 struct uniform_block_array_elements *child_array =
275 type->fields.array->is_array() ? ub_array->array : NULL;
276 const glsl_type *new_child_type =
277 resize_block_array(type->fields.array, child_array);
278
279 const glsl_type *new_type =
280 glsl_type::get_array_instance(new_child_type,
281 ub_array->num_array_elements);
282 ub_array->ir->array->type = new_type;
283 return new_type;
284 } else {
285 return type;
286 }
287 }
288
289 unsigned
290 link_uniform_blocks(void *mem_ctx,
291 struct gl_context *ctx,
292 struct gl_shader_program *prog,
293 struct gl_shader **shader_list,
294 unsigned num_shaders,
295 struct gl_uniform_block **blocks_ret)
296 {
297 /* This hash table will track all of the uniform blocks that have been
298 * encountered. Since blocks with the same block-name must be the same,
299 * the hash is organized by block-name.
300 */
301 struct hash_table *block_hash =
302 _mesa_hash_table_create(mem_ctx, _mesa_key_hash_string,
303 _mesa_key_string_equal);
304
305 if (block_hash == NULL) {
306 _mesa_error_no_memory(__func__);
307 linker_error(prog, "out of memory\n");
308 return 0;
309 }
310
311 /* Determine which uniform blocks are active.
312 */
313 link_uniform_block_active_visitor v(mem_ctx, block_hash, prog);
314 for (unsigned i = 0; i < num_shaders; i++) {
315 visit_list_elements(&v, shader_list[i]->ir);
316 }
317
318 /* Count the number of active uniform blocks. Count the total number of
319 * active slots in those uniform blocks.
320 */
321 unsigned num_blocks = 0;
322 unsigned num_variables = 0;
323 count_block_size block_size;
324 struct hash_entry *entry;
325
326 hash_table_foreach (block_hash, entry) {
327 struct link_uniform_block_active *const b =
328 (struct link_uniform_block_active *) entry->data;
329
330 assert((b->array != NULL) == b->type->is_array());
331
332 if (b->array != NULL &&
333 (b->type->without_array()->interface_packing ==
334 GLSL_INTERFACE_PACKING_PACKED)) {
335 b->type = resize_block_array(b->type, b->array);
336 b->var->type = b->type;
337 }
338
339 block_size.num_active_uniforms = 0;
340 block_size.process(b->type->without_array(), "");
341
342 if (b->array != NULL) {
343 unsigned aoa_size = b->type->arrays_of_arrays_size();
344 num_blocks += aoa_size;
345 num_variables += aoa_size * block_size.num_active_uniforms;
346 } else {
347 num_blocks++;
348 num_variables += block_size.num_active_uniforms;
349 }
350
351 }
352
353 if (num_blocks == 0) {
354 assert(num_variables == 0);
355 _mesa_hash_table_destroy(block_hash, NULL);
356 return 0;
357 }
358
359 assert(num_variables != 0);
360
361 /* Allocate storage to hold all of the informatation related to uniform
362 * blocks that can be queried through the API.
363 */
364 gl_uniform_block *blocks =
365 ralloc_array(mem_ctx, gl_uniform_block, num_blocks);
366 gl_uniform_buffer_variable *variables =
367 ralloc_array(blocks, gl_uniform_buffer_variable, num_variables);
368
369 /* Add each variable from each uniform block to the API tracking
370 * structures.
371 */
372 unsigned i = 0;
373 ubo_visitor parcel(blocks, variables, num_variables);
374
375 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_STD140)
376 == unsigned(ubo_packing_std140));
377 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_SHARED)
378 == unsigned(ubo_packing_shared));
379 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_PACKED)
380 == unsigned(ubo_packing_packed));
381 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_STD430)
382 == unsigned(ubo_packing_std430));
383
384 hash_table_foreach (block_hash, entry) {
385 const struct link_uniform_block_active *const b =
386 (const struct link_uniform_block_active *) entry->data;
387 const glsl_type *block_type = b->type;
388
389 if (b->array != NULL) {
390 unsigned binding_offset = 0;
391 char *name = ralloc_strdup(NULL, block_type->without_array()->name);
392 size_t name_length = strlen(name);
393
394 assert(b->has_instance_name);
395 process_block_array(b->array, &name, name_length, blocks, &parcel,
396 variables, b, &i, &binding_offset, ctx, prog);
397 ralloc_free(name);
398 } else {
399 blocks[i].Name = ralloc_strdup(blocks, block_type->name);
400 blocks[i].Uniforms = &variables[parcel.index];
401 blocks[i].Binding = (b->has_binding) ? b->binding : 0;
402 blocks[i].UniformBufferSize = 0;
403 blocks[i]._Packing =
404 gl_uniform_block_packing(block_type->interface_packing);
405
406 parcel.process(block_type,
407 b->has_instance_name ? block_type->name : "");
408
409 blocks[i].UniformBufferSize = parcel.buffer_size;
410
411 /* Check SSBO size is lower than maximum supported size for SSBO */
412 if (b->is_shader_storage &&
413 parcel.buffer_size > ctx->Const.MaxShaderStorageBlockSize) {
414 linker_error(prog, "shader storage block `%s' has size %d, "
415 "which is larger than than the maximum allowed (%d)",
416 block_type->name,
417 parcel.buffer_size,
418 ctx->Const.MaxShaderStorageBlockSize);
419 }
420 blocks[i].NumUniforms =
421 (unsigned)(ptrdiff_t)(&variables[parcel.index] - blocks[i].Uniforms);
422
423 blocks[i].IsShaderStorage = b->is_shader_storage;
424
425 i++;
426 }
427 }
428
429 assert(parcel.index == num_variables);
430
431 _mesa_hash_table_destroy(block_hash, NULL);
432
433 *blocks_ret = blocks;
434 return num_blocks;
435 }
436
437 bool
438 link_uniform_blocks_are_compatible(const gl_uniform_block *a,
439 const gl_uniform_block *b)
440 {
441 assert(strcmp(a->Name, b->Name) == 0);
442
443 /* Page 35 (page 42 of the PDF) in section 4.3.7 of the GLSL 1.50 spec says:
444 *
445 * "Matched block names within an interface (as defined above) must
446 * match in terms of having the same number of declarations with the
447 * same sequence of types and the same sequence of member names, as
448 * well as having the same member-wise layout qualification....if a
449 * matching block is declared as an array, then the array sizes must
450 * also match... Any mismatch will generate a link error."
451 *
452 * Arrays are not yet supported, so there is no check for that.
453 */
454 if (a->NumUniforms != b->NumUniforms)
455 return false;
456
457 if (a->_Packing != b->_Packing)
458 return false;
459
460 for (unsigned i = 0; i < a->NumUniforms; i++) {
461 if (strcmp(a->Uniforms[i].Name, b->Uniforms[i].Name) != 0)
462 return false;
463
464 if (a->Uniforms[i].Type != b->Uniforms[i].Type)
465 return false;
466
467 if (a->Uniforms[i].RowMajor != b->Uniforms[i].RowMajor)
468 return false;
469 }
470
471 return true;
472 }