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