1 /*
2 * Copyright © 2011 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 "ir.h"
25 #include "linker.h"
26 #include "ir_uniform.h"
27 #include "glsl_symbol_table.h"
28 #include "program.h"
29 #include "string_to_uint_map.h"
30 #include "ir_array_refcount.h"
31
32 #include "main/shader_types.h"
33 #include "main/consts_exts.h"
34 #include "util/strndup.h"
35
36 /**
37 * \file link_uniforms.cpp
38 * Assign locations for GLSL uniforms.
39 *
40 * \author Ian Romanick <ian.d.romanick@intel.com>
41 */
42
43 /**
44 * Used by linker to indicate uniforms that have no location set.
45 */
46 #define UNMAPPED_UNIFORM_LOC ~0u
47
48 static char*
get_top_level_name(const char * name)49 get_top_level_name(const char *name)
50 {
51 const char *first_dot = strchr(name, '.');
52 const char *first_square_bracket = strchr(name, '[');
53 int name_size = 0;
54
55 /* The ARB_program_interface_query spec says:
56 *
57 * "For the property TOP_LEVEL_ARRAY_SIZE, a single integer identifying
58 * the number of active array elements of the top-level shader storage
59 * block member containing to the active variable is written to
60 * <params>. If the top-level block member is not declared as an
61 * array, the value one is written to <params>. If the top-level block
62 * member is an array with no declared size, the value zero is written
63 * to <params>."
64 */
65
66 /* The buffer variable is on top level.*/
67 if (!first_square_bracket && !first_dot)
68 name_size = strlen(name);
69 else if ((!first_square_bracket ||
70 (first_dot && first_dot < first_square_bracket)))
71 name_size = first_dot - name;
72 else
73 name_size = first_square_bracket - name;
74
75 return strndup(name, name_size);
76 }
77
78 static char*
get_var_name(const char * name)79 get_var_name(const char *name)
80 {
81 const char *first_dot = strchr(name, '.');
82
83 if (!first_dot)
84 return strdup(name);
85
86 return strndup(first_dot+1, strlen(first_dot) - 1);
87 }
88
89 static bool
is_top_level_shader_storage_block_member(const char * name,const char * interface_name,const char * field_name)90 is_top_level_shader_storage_block_member(const char* name,
91 const char* interface_name,
92 const char* field_name)
93 {
94 bool result = false;
95
96 /* If the given variable is already a top-level shader storage
97 * block member, then return array_size = 1.
98 * We could have two possibilities: if we have an instanced
99 * shader storage block or not instanced.
100 *
101 * For the first, we check create a name as it was in top level and
102 * compare it with the real name. If they are the same, then
103 * the variable is already at top-level.
104 *
105 * Full instanced name is: interface name + '.' + var name +
106 * NULL character
107 */
108 int name_length = strlen(interface_name) + 1 + strlen(field_name) + 1;
109 char *full_instanced_name = (char *) calloc(name_length, sizeof(char));
110 if (!full_instanced_name) {
111 fprintf(stderr, "%s: Cannot allocate space for name\n", __func__);
112 return false;
113 }
114
115 snprintf(full_instanced_name, name_length, "%s.%s",
116 interface_name, field_name);
117
118 /* Check if its top-level shader storage block member of an
119 * instanced interface block, or of a unnamed interface block.
120 */
121 if (strcmp(name, full_instanced_name) == 0 ||
122 strcmp(name, field_name) == 0)
123 result = true;
124
125 free(full_instanced_name);
126 return result;
127 }
128
129 static int
get_array_size(struct gl_uniform_storage * uni,const glsl_struct_field * field,char * interface_name,char * var_name)130 get_array_size(struct gl_uniform_storage *uni, const glsl_struct_field *field,
131 char *interface_name, char *var_name)
132 {
133 /* The ARB_program_interface_query spec says:
134 *
135 * "For the property TOP_LEVEL_ARRAY_SIZE, a single integer identifying
136 * the number of active array elements of the top-level shader storage
137 * block member containing to the active variable is written to
138 * <params>. If the top-level block member is not declared as an
139 * array, the value one is written to <params>. If the top-level block
140 * member is an array with no declared size, the value zero is written
141 * to <params>."
142 */
143 if (is_top_level_shader_storage_block_member(uni->name.string,
144 interface_name,
145 var_name))
146 return 1;
147 else if (field->type->is_array())
148 return field->type->length;
149
150 return 1;
151 }
152
153 static int
get_array_stride(struct gl_uniform_storage * uni,const glsl_type * iface,const glsl_struct_field * field,char * interface_name,char * var_name,bool use_std430_as_default)154 get_array_stride(struct gl_uniform_storage *uni, const glsl_type *iface,
155 const glsl_struct_field *field, char *interface_name,
156 char *var_name, bool use_std430_as_default)
157 {
158 /* The ARB_program_interface_query spec says:
159 *
160 * "For the property TOP_LEVEL_ARRAY_STRIDE, a single integer
161 * identifying the stride between array elements of the top-level
162 * shader storage block member containing the active variable is
163 * written to <params>. For top-level block members declared as
164 * arrays, the value written is the difference, in basic machine units,
165 * between the offsets of the active variable for consecutive elements
166 * in the top-level array. For top-level block members not declared as
167 * an array, zero is written to <params>."
168 */
169 if (field->type->is_array()) {
170 const enum glsl_matrix_layout matrix_layout =
171 glsl_matrix_layout(field->matrix_layout);
172 bool row_major = matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR;
173 const glsl_type *array_type = field->type->fields.array;
174
175 if (is_top_level_shader_storage_block_member(uni->name.string,
176 interface_name,
177 var_name))
178 return 0;
179
180 if (GLSL_INTERFACE_PACKING_STD140 ==
181 iface->get_internal_ifc_packing(use_std430_as_default)) {
182 if (array_type->is_struct() || array_type->is_array())
183 return glsl_align(array_type->std140_size(row_major), 16);
184 else
185 return MAX2(array_type->std140_base_alignment(row_major), 16);
186 } else {
187 return array_type->std430_array_stride(row_major);
188 }
189 }
190 return 0;
191 }
192
193 static void
calculate_array_size_and_stride(struct gl_shader_program * shProg,struct gl_uniform_storage * uni,bool use_std430_as_default)194 calculate_array_size_and_stride(struct gl_shader_program *shProg,
195 struct gl_uniform_storage *uni,
196 bool use_std430_as_default)
197 {
198 if (!uni->is_shader_storage)
199 return;
200
201 int block_index = uni->block_index;
202 int array_size = -1;
203 int array_stride = -1;
204 char *var_name = get_top_level_name(uni->name.string);
205 char *interface_name =
206 get_top_level_name(uni->is_shader_storage ?
207 shProg->data->ShaderStorageBlocks[block_index].name.string :
208 shProg->data->UniformBlocks[block_index].name.string);
209
210 if (strcmp(var_name, interface_name) == 0) {
211 /* Deal with instanced array of SSBOs */
212 char *temp_name = get_var_name(uni->name.string);
213 if (!temp_name) {
214 linker_error(shProg, "Out of memory during linking.\n");
215 goto write_top_level_array_size_and_stride;
216 }
217 free(var_name);
218 var_name = get_top_level_name(temp_name);
219 free(temp_name);
220 if (!var_name) {
221 linker_error(shProg, "Out of memory during linking.\n");
222 goto write_top_level_array_size_and_stride;
223 }
224 }
225
226 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
227 const gl_linked_shader *sh = shProg->_LinkedShaders[i];
228 if (sh == NULL)
229 continue;
230
231 foreach_in_list(ir_instruction, node, sh->ir) {
232 ir_variable *var = node->as_variable();
233 if (!var || !var->get_interface_type() ||
234 var->data.mode != ir_var_shader_storage)
235 continue;
236
237 const glsl_type *iface = var->get_interface_type();
238
239 if (strcmp(interface_name, iface->name) != 0)
240 continue;
241
242 for (unsigned i = 0; i < iface->length; i++) {
243 const glsl_struct_field *field = &iface->fields.structure[i];
244 if (strcmp(field->name, var_name) != 0)
245 continue;
246
247 array_stride = get_array_stride(uni, iface, field, interface_name,
248 var_name, use_std430_as_default);
249 array_size = get_array_size(uni, field, interface_name, var_name);
250 goto write_top_level_array_size_and_stride;
251 }
252 }
253 }
254 write_top_level_array_size_and_stride:
255 free(interface_name);
256 free(var_name);
257 uni->top_level_array_stride = array_stride;
258 uni->top_level_array_size = array_size;
259 }
260
261 void
process(const glsl_type * type,const char * name,bool use_std430_as_default)262 program_resource_visitor::process(const glsl_type *type, const char *name,
263 bool use_std430_as_default)
264 {
265 assert(type->without_array()->is_struct()
266 || type->without_array()->is_interface());
267
268 unsigned record_array_count = 1;
269 char *name_copy = ralloc_strdup(NULL, name);
270
271 enum glsl_interface_packing packing =
272 type->get_internal_ifc_packing(use_std430_as_default);
273
274 recursion(type, &name_copy, strlen(name), false, NULL, packing, false,
275 record_array_count, NULL);
276 ralloc_free(name_copy);
277 }
278
279 void
process(ir_variable * var,bool use_std430_as_default)280 program_resource_visitor::process(ir_variable *var, bool use_std430_as_default)
281 {
282 const glsl_type *t =
283 var->data.from_named_ifc_block ? var->get_interface_type() : var->type;
284 process(var, t, use_std430_as_default);
285 }
286
287 void
process(ir_variable * var,const glsl_type * var_type,bool use_std430_as_default)288 program_resource_visitor::process(ir_variable *var, const glsl_type *var_type,
289 bool use_std430_as_default)
290 {
291 unsigned record_array_count = 1;
292 const bool row_major =
293 var->data.matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR;
294
295 enum glsl_interface_packing packing = var->get_interface_type() ?
296 var->get_interface_type()->
297 get_internal_ifc_packing(use_std430_as_default) :
298 var->type->get_internal_ifc_packing(use_std430_as_default);
299
300 const glsl_type *t = var_type;
301 const glsl_type *t_without_array = t->without_array();
302
303 /* false is always passed for the row_major parameter to the other
304 * processing functions because no information is available to do
305 * otherwise. See the warning in linker.h.
306 */
307 if (t_without_array->is_struct() ||
308 (t->is_array() && t->fields.array->is_array())) {
309 char *name = ralloc_strdup(NULL, var->name);
310 recursion(var->type, &name, strlen(name), row_major, NULL, packing,
311 false, record_array_count, NULL);
312 ralloc_free(name);
313 } else if (t_without_array->is_interface()) {
314 char *name = ralloc_strdup(NULL, t_without_array->name);
315 const glsl_struct_field *ifc_member = var->data.from_named_ifc_block ?
316 &t_without_array->
317 fields.structure[t_without_array->field_index(var->name)] : NULL;
318
319 recursion(t, &name, strlen(name), row_major, NULL, packing,
320 false, record_array_count, ifc_member);
321 ralloc_free(name);
322 } else {
323 this->set_record_array_count(record_array_count);
324 this->visit_field(t, var->name, row_major, NULL, packing, false);
325 }
326 }
327
328 void
recursion(const glsl_type * t,char ** name,size_t name_length,bool row_major,const glsl_type * record_type,const enum glsl_interface_packing packing,bool last_field,unsigned record_array_count,const glsl_struct_field * named_ifc_member)329 program_resource_visitor::recursion(const glsl_type *t, char **name,
330 size_t name_length, bool row_major,
331 const glsl_type *record_type,
332 const enum glsl_interface_packing packing,
333 bool last_field,
334 unsigned record_array_count,
335 const glsl_struct_field *named_ifc_member)
336 {
337 /* Records need to have each field processed individually.
338 *
339 * Arrays of records need to have each array element processed
340 * individually, then each field of the resulting array elements processed
341 * individually.
342 */
343 if (t->is_interface() && named_ifc_member) {
344 ralloc_asprintf_rewrite_tail(name, &name_length, ".%s",
345 named_ifc_member->name);
346 recursion(named_ifc_member->type, name, name_length, row_major, NULL,
347 packing, false, record_array_count, NULL);
348 } else if (t->is_struct() || t->is_interface()) {
349 if (record_type == NULL && t->is_struct())
350 record_type = t;
351
352 if (t->is_struct())
353 this->enter_record(t, *name, row_major, packing);
354
355 for (unsigned i = 0; i < t->length; i++) {
356 const char *field = t->fields.structure[i].name;
357 size_t new_length = name_length;
358
359 if (t->is_interface() && t->fields.structure[i].offset != -1)
360 this->set_buffer_offset(t->fields.structure[i].offset);
361
362 /* Append '.field' to the current variable name. */
363 if (name_length == 0) {
364 ralloc_asprintf_rewrite_tail(name, &new_length, "%s", field);
365 } else {
366 ralloc_asprintf_rewrite_tail(name, &new_length, ".%s", field);
367 }
368
369 /* The layout of structures at the top level of the block is set
370 * during parsing. For matrices contained in multiple levels of
371 * structures in the block, the inner structures have no layout.
372 * These cases must potentially inherit the layout from the outer
373 * levels.
374 */
375 bool field_row_major = row_major;
376 const enum glsl_matrix_layout matrix_layout =
377 glsl_matrix_layout(t->fields.structure[i].matrix_layout);
378 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
379 field_row_major = true;
380 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
381 field_row_major = false;
382 }
383
384 recursion(t->fields.structure[i].type, name, new_length,
385 field_row_major,
386 record_type,
387 packing,
388 (i + 1) == t->length, record_array_count, NULL);
389
390 /* Only the first leaf-field of the record gets called with the
391 * record type pointer.
392 */
393 record_type = NULL;
394 }
395
396 if (t->is_struct()) {
397 (*name)[name_length] = '\0';
398 this->leave_record(t, *name, row_major, packing);
399 }
400 } else if (t->without_array()->is_struct() ||
401 t->without_array()->is_interface() ||
402 (t->is_array() && t->fields.array->is_array())) {
403 if (record_type == NULL && t->fields.array->is_struct())
404 record_type = t->fields.array;
405
406 unsigned length = t->length;
407
408 /* Shader storage block unsized arrays: add subscript [0] to variable
409 * names.
410 */
411 if (t->is_unsized_array())
412 length = 1;
413
414 record_array_count *= length;
415
416 for (unsigned i = 0; i < length; i++) {
417 size_t new_length = name_length;
418
419 /* Append the subscript to the current variable name */
420 ralloc_asprintf_rewrite_tail(name, &new_length, "[%u]", i);
421
422 recursion(t->fields.array, name, new_length, row_major,
423 record_type,
424 packing,
425 (i + 1) == t->length, record_array_count,
426 named_ifc_member);
427
428 /* Only the first leaf-field of the record gets called with the
429 * record type pointer.
430 */
431 record_type = NULL;
432 }
433 } else {
434 this->set_record_array_count(record_array_count);
435 this->visit_field(t, *name, row_major, record_type, packing, last_field);
436 }
437 }
438
439 void
enter_record(const glsl_type *,const char *,bool,const enum glsl_interface_packing)440 program_resource_visitor::enter_record(const glsl_type *, const char *, bool,
441 const enum glsl_interface_packing)
442 {
443 }
444
445 void
leave_record(const glsl_type *,const char *,bool,const enum glsl_interface_packing)446 program_resource_visitor::leave_record(const glsl_type *, const char *, bool,
447 const enum glsl_interface_packing)
448 {
449 }
450
451 void
set_buffer_offset(unsigned)452 program_resource_visitor::set_buffer_offset(unsigned)
453 {
454 }
455
456 void
set_record_array_count(unsigned)457 program_resource_visitor::set_record_array_count(unsigned)
458 {
459 }
460
461 namespace {
462
463 /**
464 * Class to help calculate the storage requirements for a set of uniforms
465 *
466 * As uniforms are added to the active set the number of active uniforms and
467 * the storage requirements for those uniforms are accumulated. The active
468 * uniforms are added to the hash table supplied to the constructor.
469 *
470 * If the same uniform is added multiple times (i.e., once for each shader
471 * target), it will only be accounted once.
472 */
473 class count_uniform_size : public program_resource_visitor {
474 public:
count_uniform_size(struct string_to_uint_map * map,struct string_to_uint_map * hidden_map,bool use_std430_as_default)475 count_uniform_size(struct string_to_uint_map *map,
476 struct string_to_uint_map *hidden_map,
477 bool use_std430_as_default)
478 : num_active_uniforms(0), num_hidden_uniforms(0), num_values(0),
479 num_shader_samplers(0), num_shader_images(0),
480 num_shader_uniform_components(0), num_shader_subroutines(0),
481 is_buffer_block(false), is_shader_storage(false), map(map),
482 hidden_map(hidden_map), current_var(NULL),
483 use_std430_as_default(use_std430_as_default)
484 {
485 /* empty */
486 }
487
start_shader()488 void start_shader()
489 {
490 this->num_shader_samplers = 0;
491 this->num_shader_images = 0;
492 this->num_shader_uniform_components = 0;
493 this->num_shader_subroutines = 0;
494 }
495
process(ir_variable * var)496 void process(ir_variable *var)
497 {
498 this->current_var = var;
499 this->is_buffer_block = var->is_in_buffer_block();
500 this->is_shader_storage = var->is_in_shader_storage_block();
501 if (var->is_interface_instance())
502 program_resource_visitor::process(var->get_interface_type(),
503 var->get_interface_type()->name,
504 use_std430_as_default);
505 else
506 program_resource_visitor::process(var, use_std430_as_default);
507 }
508
509 /**
510 * Total number of active uniforms counted
511 */
512 unsigned num_active_uniforms;
513
514 unsigned num_hidden_uniforms;
515
516 /**
517 * Number of data values required to back the storage for the active uniforms
518 */
519 unsigned num_values;
520
521 /**
522 * Number of samplers used
523 */
524 unsigned num_shader_samplers;
525
526 /**
527 * Number of images used
528 */
529 unsigned num_shader_images;
530
531 /**
532 * Number of uniforms used in the current shader
533 */
534 unsigned num_shader_uniform_components;
535
536 /**
537 * Number of subroutine uniforms used
538 */
539 unsigned num_shader_subroutines;
540
541 bool is_buffer_block;
542 bool is_shader_storage;
543
544 struct string_to_uint_map *map;
545
546 private:
visit_field(const glsl_type * type,const char * name,bool,const glsl_type *,const enum glsl_interface_packing,bool)547 virtual void visit_field(const glsl_type *type, const char *name,
548 bool /* row_major */,
549 const glsl_type * /* record_type */,
550 const enum glsl_interface_packing,
551 bool /* last_field */)
552 {
553 assert(!type->without_array()->is_struct());
554 assert(!type->without_array()->is_interface());
555 assert(!(type->is_array() && type->fields.array->is_array()));
556
557 /* Count the number of samplers regardless of whether the uniform is
558 * already in the hash table. The hash table prevents adding the same
559 * uniform for multiple shader targets, but in this case we want to
560 * count it for each shader target.
561 */
562 const unsigned values = type->component_slots();
563 if (type->contains_subroutine()) {
564 this->num_shader_subroutines += values;
565 } else if (type->contains_sampler() && !current_var->data.bindless) {
566 /* Samplers (bound or bindless) are counted as two components as
567 * specified by ARB_bindless_texture. */
568 this->num_shader_samplers += values / 2;
569 } else if (type->contains_image() && !current_var->data.bindless) {
570 /* Images (bound or bindless) are counted as two components as
571 * specified by ARB_bindless_texture. */
572 this->num_shader_images += values / 2;
573
574 /* As drivers are likely to represent image uniforms as
575 * scalar indices, count them against the limit of uniform
576 * components in the default block. The spec allows image
577 * uniforms to use up no more than one scalar slot.
578 */
579 if (!is_shader_storage)
580 this->num_shader_uniform_components += values;
581 } else {
582 /* Accumulate the total number of uniform slots used by this shader.
583 * Note that samplers do not count against this limit because they
584 * don't use any storage on current hardware.
585 */
586 if (!is_buffer_block)
587 this->num_shader_uniform_components += values;
588 }
589
590 /* If the uniform is already in the map, there's nothing more to do.
591 */
592 unsigned id;
593 if (this->map->get(id, name))
594 return;
595
596 if (this->current_var->data.how_declared == ir_var_hidden) {
597 this->hidden_map->put(this->num_hidden_uniforms, name);
598 this->num_hidden_uniforms++;
599 } else {
600 this->map->put(this->num_active_uniforms-this->num_hidden_uniforms,
601 name);
602 }
603
604 /* Each leaf uniform occupies one entry in the list of active
605 * uniforms.
606 */
607 this->num_active_uniforms++;
608
609 if(!is_gl_identifier(name) && !is_shader_storage && !is_buffer_block)
610 this->num_values += values;
611 }
612
613 struct string_to_uint_map *hidden_map;
614
615 /**
616 * Current variable being processed.
617 */
618 ir_variable *current_var;
619
620 bool use_std430_as_default;
621 };
622
623 } /* anonymous namespace */
624
625 unsigned
link_calculate_matrix_stride(const glsl_type * matrix,bool row_major,enum glsl_interface_packing packing)626 link_calculate_matrix_stride(const glsl_type *matrix, bool row_major,
627 enum glsl_interface_packing packing)
628 {
629 const unsigned N = matrix->is_double() ? 8 : 4;
630 const unsigned items =
631 row_major ? matrix->matrix_columns : matrix->vector_elements;
632
633 assert(items <= 4);
634
635 /* Matrix stride for std430 mat2xY matrices are not rounded up to
636 * vec4 size.
637 *
638 * Section 7.6.2.2 "Standard Uniform Block Layout" of the OpenGL 4.3 spec
639 * says:
640 *
641 * 2. If the member is a two- or four-component vector with components
642 * consuming N basic machine units, the base alignment is 2N or 4N,
643 * respectively.
644 * ...
645 * 4. If the member is an array of scalars or vectors, the base
646 * alignment and array stride are set to match the base alignment of
647 * a single array element, according to rules (1), (2), and (3), and
648 * rounded up to the base alignment of a vec4.
649 * ...
650 * 7. If the member is a row-major matrix with C columns and R rows, the
651 * matrix is stored identically to an array of R row vectors with C
652 * components each, according to rule (4).
653 * ...
654 *
655 * When using the std430 storage layout, shader storage blocks will be
656 * laid out in buffer storage identically to uniform and shader storage
657 * blocks using the std140 layout, except that the base alignment and
658 * stride of arrays of scalars and vectors in rule 4 and of structures
659 * in rule 9 are not rounded up a multiple of the base alignment of a
660 * vec4.
661 */
662 return packing == GLSL_INTERFACE_PACKING_STD430
663 ? (items < 3 ? items * N : glsl_align(items * N, 16))
664 : glsl_align(items * N, 16);
665 }
666
667 /**
668 * Class to help parcel out pieces of backing storage to uniforms
669 *
670 * Each uniform processed has some range of the \c gl_constant_value
671 * structures associated with it. The association is done by finding
672 * the uniform in the \c string_to_uint_map and using the value from
673 * the map to connect that slot in the \c gl_uniform_storage table
674 * with the next available slot in the \c gl_constant_value array.
675 *
676 * \warning
677 * This class assumes that every uniform that will be processed is
678 * already in the \c string_to_uint_map. In addition, it assumes that
679 * the \c gl_uniform_storage and \c gl_constant_value arrays are "big
680 * enough."
681 */
682 class parcel_out_uniform_storage : public program_resource_visitor {
683 public:
parcel_out_uniform_storage(struct gl_shader_program * prog,struct string_to_uint_map * map,struct gl_uniform_storage * uniforms,union gl_constant_value * values,bool use_std430_as_default)684 parcel_out_uniform_storage(struct gl_shader_program *prog,
685 struct string_to_uint_map *map,
686 struct gl_uniform_storage *uniforms,
687 union gl_constant_value *values,
688 bool use_std430_as_default)
689 : buffer_block_index(0), ubo_byte_offset(0),
690 shader_type(MESA_SHADER_NONE),
691 prog(prog), map(map), uniforms(uniforms),
692 next_sampler(0), next_bindless_sampler(0), next_image(0),
693 next_bindless_image(0), next_subroutine(0),
694 use_std430_as_default(use_std430_as_default),
695 field_counter(0), current_var(NULL), explicit_location(0),
696 record_array_count(0), record_next_sampler(NULL),
697 record_next_image(NULL), record_next_bindless_sampler(NULL),
698 record_next_bindless_image(NULL),
699 values(values),
700 shader_samplers_used(0), shader_shadow_samplers(0),
701 num_bindless_samplers(0),
702 bindless_targets(NULL), num_bindless_images(0),
703 bindless_access(NULL),
704 shader_storage_blocks_write_access(0)
705 {
706 memset(this->targets, 0, sizeof(this->targets));
707 }
708
~parcel_out_uniform_storage()709 virtual ~parcel_out_uniform_storage()
710 {
711 free(this->bindless_targets);
712 free(this->bindless_access);
713 }
714
start_shader(gl_shader_stage shader_type)715 void start_shader(gl_shader_stage shader_type)
716 {
717 assert(shader_type < MESA_SHADER_STAGES);
718 this->shader_type = shader_type;
719
720 this->shader_samplers_used = 0;
721 this->shader_shadow_samplers = 0;
722 this->next_sampler = 0;
723 this->next_image = 0;
724 this->next_subroutine = 0;
725 this->record_array_count = 1;
726 memset(this->targets, 0, sizeof(this->targets));
727
728 this->num_bindless_samplers = 0;
729 this->next_bindless_sampler = 0;
730 free(this->bindless_targets);
731 this->bindless_targets = NULL;
732
733 this->num_bindless_images = 0;
734 this->next_bindless_image = 0;
735 free(this->bindless_access);
736 this->bindless_access = NULL;
737 this->shader_storage_blocks_write_access = 0;
738 }
739
set_and_process(ir_variable * var)740 void set_and_process(ir_variable *var)
741 {
742 current_var = var;
743 field_counter = 0;
744 this->record_next_sampler = new string_to_uint_map;
745 this->record_next_bindless_sampler = new string_to_uint_map;
746 this->record_next_image = new string_to_uint_map;
747 this->record_next_bindless_image = new string_to_uint_map;
748
749 buffer_block_index = -1;
750 if (var->is_in_buffer_block()) {
751 struct gl_uniform_block *blks = var->is_in_shader_storage_block() ?
752 prog->data->ShaderStorageBlocks : prog->data->UniformBlocks;
753 unsigned num_blks = var->is_in_shader_storage_block() ?
754 prog->data->NumShaderStorageBlocks : prog->data->NumUniformBlocks;
755 bool is_interface_array =
756 var->is_interface_instance() && var->type->is_array();
757
758 if (is_interface_array) {
759 unsigned l = strlen(var->get_interface_type()->name);
760
761 for (unsigned i = 0; i < num_blks; i++) {
762 if (strncmp(var->get_interface_type()->name, blks[i].name.string, l)
763 == 0 && blks[i].name.string[l] == '[') {
764 buffer_block_index = i;
765 break;
766 }
767 }
768 } else {
769 for (unsigned i = 0; i < num_blks; i++) {
770 if (strcmp(var->get_interface_type()->name, blks[i].name.string) == 0) {
771 buffer_block_index = i;
772 break;
773 }
774 }
775 }
776 assert(buffer_block_index != -1);
777
778 if (var->is_in_shader_storage_block() &&
779 !var->data.memory_read_only) {
780 unsigned array_size = is_interface_array ?
781 var->type->array_size() : 1;
782
783 STATIC_ASSERT(MAX_SHADER_STORAGE_BUFFERS <= 32);
784
785 /* Shaders that use too many SSBOs will fail to compile, which
786 * we don't care about.
787 *
788 * This is true for shaders that do not use too many SSBOs:
789 */
790 if (buffer_block_index + array_size <= 32) {
791 shader_storage_blocks_write_access |=
792 u_bit_consecutive(buffer_block_index, array_size);
793 }
794 }
795
796 /* Uniform blocks that were specified with an instance name must be
797 * handled a little bit differently. The name of the variable is the
798 * name used to reference the uniform block instead of being the name
799 * of a variable within the block. Therefore, searching for the name
800 * within the block will fail.
801 */
802 if (var->is_interface_instance()) {
803 ubo_byte_offset = 0;
804 process(var->get_interface_type(),
805 var->get_interface_type()->name,
806 use_std430_as_default);
807 } else {
808 const struct gl_uniform_block *const block =
809 &blks[buffer_block_index];
810
811 assert(var->data.location != -1);
812
813 const struct gl_uniform_buffer_variable *const ubo_var =
814 &block->Uniforms[var->data.location];
815
816 ubo_byte_offset = ubo_var->Offset;
817 process(var, use_std430_as_default);
818 }
819 } else {
820 /* Store any explicit location and reset data location so we can
821 * reuse this variable for storing the uniform slot number.
822 */
823 this->explicit_location = current_var->data.location;
824 current_var->data.location = -1;
825
826 process(var, use_std430_as_default);
827 }
828 delete this->record_next_sampler;
829 delete this->record_next_bindless_sampler;
830 delete this->record_next_image;
831 delete this->record_next_bindless_image;
832 }
833
834 int buffer_block_index;
835 int ubo_byte_offset;
836 gl_shader_stage shader_type;
837
838 private:
set_opaque_indices(const glsl_type * base_type,struct gl_uniform_storage * uniform,const char * name,unsigned & next_index,struct string_to_uint_map * record_next_index)839 bool set_opaque_indices(const glsl_type *base_type,
840 struct gl_uniform_storage *uniform,
841 const char *name, unsigned &next_index,
842 struct string_to_uint_map *record_next_index)
843 {
844 assert(base_type->is_sampler() || base_type->is_image());
845
846 if (this->record_array_count > 1) {
847 unsigned inner_array_size = MAX2(1, uniform->array_elements);
848 char *name_copy = ralloc_strdup(NULL, name);
849
850 /* Remove all array subscripts from the sampler/image name */
851 char *str_start;
852 const char *str_end;
853 while((str_start = strchr(name_copy, '[')) &&
854 (str_end = strchr(name_copy, ']'))) {
855 memmove(str_start, str_end + 1, 1 + strlen(str_end + 1));
856 }
857
858 unsigned index = 0;
859 if (record_next_index->get(index, name_copy)) {
860 /* In this case, we've already seen this uniform so we just use the
861 * next sampler/image index recorded the last time we visited.
862 */
863 uniform->opaque[shader_type].index = index;
864 index = inner_array_size + uniform->opaque[shader_type].index;
865 record_next_index->put(index, name_copy);
866
867 ralloc_free(name_copy);
868 /* Return as everything else has already been initialised in a
869 * previous pass.
870 */
871 return false;
872 } else {
873 /* We've never seen this uniform before so we need to allocate
874 * enough indices to store it.
875 *
876 * Nested struct arrays behave like arrays of arrays so we need to
877 * increase the index by the total number of elements of the
878 * sampler/image in case there is more than one sampler/image
879 * inside the structs. This allows the offset to be easily
880 * calculated for indirect indexing.
881 */
882 uniform->opaque[shader_type].index = next_index;
883 next_index += inner_array_size * this->record_array_count;
884
885 /* Store the next index for future passes over the struct array
886 */
887 index = uniform->opaque[shader_type].index + inner_array_size;
888 record_next_index->put(index, name_copy);
889 ralloc_free(name_copy);
890 }
891 } else {
892 /* Increment the sampler/image by 1 for non-arrays and by the number
893 * of array elements for arrays.
894 */
895 uniform->opaque[shader_type].index = next_index;
896 next_index += MAX2(1, uniform->array_elements);
897 }
898 return true;
899 }
900
handle_samplers(const glsl_type * base_type,struct gl_uniform_storage * uniform,const char * name)901 void handle_samplers(const glsl_type *base_type,
902 struct gl_uniform_storage *uniform, const char *name)
903 {
904 if (base_type->is_sampler()) {
905 uniform->opaque[shader_type].active = true;
906
907 const gl_texture_index target = base_type->sampler_index();
908 const unsigned shadow = base_type->sampler_shadow;
909
910 if (current_var->data.bindless) {
911 if (!set_opaque_indices(base_type, uniform, name,
912 this->next_bindless_sampler,
913 this->record_next_bindless_sampler))
914 return;
915
916 this->num_bindless_samplers = this->next_bindless_sampler;
917
918 this->bindless_targets = (gl_texture_index *)
919 realloc(this->bindless_targets,
920 this->num_bindless_samplers * sizeof(gl_texture_index));
921
922 for (unsigned i = uniform->opaque[shader_type].index;
923 i < this->num_bindless_samplers;
924 i++) {
925 this->bindless_targets[i] = target;
926 }
927 } else {
928 if (!set_opaque_indices(base_type, uniform, name,
929 this->next_sampler,
930 this->record_next_sampler))
931 return;
932
933 for (unsigned i = uniform->opaque[shader_type].index;
934 i < MIN2(this->next_sampler, MAX_SAMPLERS);
935 i++) {
936 this->targets[i] = target;
937 this->shader_samplers_used |= 1U << i;
938 this->shader_shadow_samplers |= shadow << i;
939 }
940 }
941 }
942 }
943
handle_images(const glsl_type * base_type,struct gl_uniform_storage * uniform,const char * name)944 void handle_images(const glsl_type *base_type,
945 struct gl_uniform_storage *uniform, const char *name)
946 {
947 if (base_type->is_image()) {
948 uniform->opaque[shader_type].active = true;
949
950 /* Set image access qualifiers */
951 const GLenum access =
952 current_var->data.memory_read_only ?
953 (current_var->data.memory_write_only ? GL_NONE :
954 GL_READ_ONLY) :
955 (current_var->data.memory_write_only ? GL_WRITE_ONLY :
956 GL_READ_WRITE);
957
958 if (current_var->data.bindless) {
959 if (!set_opaque_indices(base_type, uniform, name,
960 this->next_bindless_image,
961 this->record_next_bindless_image))
962 return;
963
964 this->num_bindless_images = this->next_bindless_image;
965
966 this->bindless_access = (GLenum *)
967 realloc(this->bindless_access,
968 this->num_bindless_images * sizeof(GLenum));
969
970 for (unsigned i = uniform->opaque[shader_type].index;
971 i < this->num_bindless_images;
972 i++) {
973 this->bindless_access[i] = access;
974 }
975 } else {
976 if (!set_opaque_indices(base_type, uniform, name,
977 this->next_image,
978 this->record_next_image))
979 return;
980
981 for (unsigned i = uniform->opaque[shader_type].index;
982 i < MIN2(this->next_image, MAX_IMAGE_UNIFORMS);
983 i++) {
984 prog->_LinkedShaders[shader_type]->Program->sh.ImageAccess[i] = access;
985 }
986 }
987 }
988 }
989
handle_subroutines(const glsl_type * base_type,struct gl_uniform_storage * uniform)990 void handle_subroutines(const glsl_type *base_type,
991 struct gl_uniform_storage *uniform)
992 {
993 if (base_type->is_subroutine()) {
994 uniform->opaque[shader_type].index = this->next_subroutine;
995 uniform->opaque[shader_type].active = true;
996
997 prog->_LinkedShaders[shader_type]->Program->sh.NumSubroutineUniforms++;
998
999 /* Increment the subroutine index by 1 for non-arrays and by the
1000 * number of array elements for arrays.
1001 */
1002 this->next_subroutine += MAX2(1, uniform->array_elements);
1003
1004 }
1005 }
1006
set_buffer_offset(unsigned offset)1007 virtual void set_buffer_offset(unsigned offset)
1008 {
1009 this->ubo_byte_offset = offset;
1010 }
1011
set_record_array_count(unsigned record_array_count)1012 virtual void set_record_array_count(unsigned record_array_count)
1013 {
1014 this->record_array_count = record_array_count;
1015 }
1016
enter_record(const glsl_type * type,const char *,bool row_major,const enum glsl_interface_packing packing)1017 virtual void enter_record(const glsl_type *type, const char *,
1018 bool row_major,
1019 const enum glsl_interface_packing packing)
1020 {
1021 assert(type->is_struct());
1022 if (this->buffer_block_index == -1)
1023 return;
1024 if (packing == GLSL_INTERFACE_PACKING_STD430)
1025 this->ubo_byte_offset = glsl_align(
1026 this->ubo_byte_offset, type->std430_base_alignment(row_major));
1027 else
1028 this->ubo_byte_offset = glsl_align(
1029 this->ubo_byte_offset, type->std140_base_alignment(row_major));
1030 }
1031
leave_record(const glsl_type * type,const char *,bool row_major,const enum glsl_interface_packing packing)1032 virtual void leave_record(const glsl_type *type, const char *,
1033 bool row_major,
1034 const enum glsl_interface_packing packing)
1035 {
1036 assert(type->is_struct());
1037 if (this->buffer_block_index == -1)
1038 return;
1039 if (packing == GLSL_INTERFACE_PACKING_STD430)
1040 this->ubo_byte_offset = glsl_align(
1041 this->ubo_byte_offset, type->std430_base_alignment(row_major));
1042 else
1043 this->ubo_byte_offset = glsl_align(
1044 this->ubo_byte_offset, type->std140_base_alignment(row_major));
1045 }
1046
visit_field(const glsl_type * type,const char * name,bool row_major,const glsl_type *,const enum glsl_interface_packing packing,bool)1047 virtual void visit_field(const glsl_type *type, const char *name,
1048 bool row_major, const glsl_type * /* record_type */,
1049 const enum glsl_interface_packing packing,
1050 bool /* last_field */)
1051 {
1052 assert(!type->without_array()->is_struct());
1053 assert(!type->without_array()->is_interface());
1054 assert(!(type->is_array() && type->fields.array->is_array()));
1055
1056 unsigned id = 0;
1057 bool found = this->map->get(id, name);
1058 assert(found);
1059
1060 if (!found)
1061 return;
1062
1063 const glsl_type *base_type;
1064 if (type->is_array()) {
1065 this->uniforms[id].array_elements = type->length;
1066 base_type = type->fields.array;
1067 } else {
1068 this->uniforms[id].array_elements = 0;
1069 base_type = type;
1070 }
1071
1072 /* Initialise opaque data */
1073 this->uniforms[id].opaque[shader_type].index = ~0;
1074 this->uniforms[id].opaque[shader_type].active = false;
1075
1076 if (current_var->data.used || base_type->is_subroutine())
1077 this->uniforms[id].active_shader_mask |= 1 << shader_type;
1078
1079 /* This assigns uniform indices to sampler and image uniforms. */
1080 handle_samplers(base_type, &this->uniforms[id], name);
1081 handle_images(base_type, &this->uniforms[id], name);
1082 handle_subroutines(base_type, &this->uniforms[id]);
1083
1084 /* For array of arrays or struct arrays the base location may have
1085 * already been set so don't set it again.
1086 */
1087 if (buffer_block_index == -1 && current_var->data.location == -1) {
1088 current_var->data.location = id;
1089 }
1090
1091 /* If there is already storage associated with this uniform or if the
1092 * uniform is set as builtin, it means that it was set while processing
1093 * an earlier shader stage. For example, we may be processing the
1094 * uniform in the fragment shader, but the uniform was already processed
1095 * in the vertex shader.
1096 */
1097 if (this->uniforms[id].storage != NULL || this->uniforms[id].builtin) {
1098 return;
1099 }
1100
1101 /* Assign explicit locations. */
1102 if (current_var->data.explicit_location) {
1103 /* Set sequential locations for struct fields. */
1104 if (current_var->type->without_array()->is_struct() ||
1105 current_var->type->is_array_of_arrays()) {
1106 const unsigned entries = MAX2(1, this->uniforms[id].array_elements);
1107 this->uniforms[id].remap_location =
1108 this->explicit_location + field_counter;
1109 field_counter += entries;
1110 } else {
1111 this->uniforms[id].remap_location = this->explicit_location;
1112 }
1113 } else {
1114 /* Initialize to to indicate that no location is set */
1115 this->uniforms[id].remap_location = UNMAPPED_UNIFORM_LOC;
1116 }
1117
1118 this->uniforms[id].name.string = ralloc_strdup(this->uniforms, name);
1119 resource_name_updated(&this->uniforms[id].name);
1120 this->uniforms[id].type = base_type;
1121 this->uniforms[id].num_driver_storage = 0;
1122 this->uniforms[id].driver_storage = NULL;
1123 this->uniforms[id].atomic_buffer_index = -1;
1124 this->uniforms[id].hidden =
1125 current_var->data.how_declared == ir_var_hidden;
1126 this->uniforms[id].builtin = is_gl_identifier(name);
1127
1128 this->uniforms[id].is_shader_storage =
1129 current_var->is_in_shader_storage_block();
1130 this->uniforms[id].is_bindless = current_var->data.bindless;
1131
1132 /* Do not assign storage if the uniform is a builtin or buffer object */
1133 if (!this->uniforms[id].builtin &&
1134 !this->uniforms[id].is_shader_storage &&
1135 this->buffer_block_index == -1)
1136 this->uniforms[id].storage = this->values;
1137
1138 if (this->buffer_block_index != -1) {
1139 this->uniforms[id].block_index = this->buffer_block_index;
1140
1141 unsigned alignment = type->std140_base_alignment(row_major);
1142 if (packing == GLSL_INTERFACE_PACKING_STD430)
1143 alignment = type->std430_base_alignment(row_major);
1144 this->ubo_byte_offset = glsl_align(this->ubo_byte_offset, alignment);
1145 this->uniforms[id].offset = this->ubo_byte_offset;
1146 if (packing == GLSL_INTERFACE_PACKING_STD430)
1147 this->ubo_byte_offset += type->std430_size(row_major);
1148 else
1149 this->ubo_byte_offset += type->std140_size(row_major);
1150
1151 if (type->is_array()) {
1152 if (packing == GLSL_INTERFACE_PACKING_STD430)
1153 this->uniforms[id].array_stride =
1154 type->without_array()->std430_array_stride(row_major);
1155 else
1156 this->uniforms[id].array_stride =
1157 glsl_align(type->without_array()->std140_size(row_major),
1158 16);
1159 } else {
1160 this->uniforms[id].array_stride = 0;
1161 }
1162
1163 if (type->without_array()->is_matrix()) {
1164 this->uniforms[id].matrix_stride =
1165 link_calculate_matrix_stride(type->without_array(),
1166 row_major,
1167 packing);
1168 this->uniforms[id].row_major = row_major;
1169 } else {
1170 this->uniforms[id].matrix_stride = 0;
1171 this->uniforms[id].row_major = false;
1172 }
1173 } else {
1174 this->uniforms[id].block_index = -1;
1175 this->uniforms[id].offset = -1;
1176 this->uniforms[id].array_stride = -1;
1177 this->uniforms[id].matrix_stride = -1;
1178 this->uniforms[id].row_major = false;
1179 }
1180
1181 if (!this->uniforms[id].builtin &&
1182 !this->uniforms[id].is_shader_storage &&
1183 this->buffer_block_index == -1)
1184 this->values += type->component_slots();
1185
1186 calculate_array_size_and_stride(prog, &this->uniforms[id],
1187 use_std430_as_default);
1188 }
1189
1190 /**
1191 * Current program being processed.
1192 */
1193 struct gl_shader_program *prog;
1194
1195 struct string_to_uint_map *map;
1196
1197 struct gl_uniform_storage *uniforms;
1198 unsigned next_sampler;
1199 unsigned next_bindless_sampler;
1200 unsigned next_image;
1201 unsigned next_bindless_image;
1202 unsigned next_subroutine;
1203
1204 bool use_std430_as_default;
1205
1206 /**
1207 * Field counter is used to take care that uniform structures
1208 * with explicit locations get sequential locations.
1209 */
1210 unsigned field_counter;
1211
1212 /**
1213 * Current variable being processed.
1214 */
1215 ir_variable *current_var;
1216
1217 /* Used to store the explicit location from current_var so that we can
1218 * reuse the location field for storing the uniform slot id.
1219 */
1220 int explicit_location;
1221
1222 /* Stores total struct array elements including nested structs */
1223 unsigned record_array_count;
1224
1225 /* Map for temporarily storing next sampler index when handling samplers in
1226 * struct arrays.
1227 */
1228 struct string_to_uint_map *record_next_sampler;
1229
1230 /* Map for temporarily storing next imager index when handling images in
1231 * struct arrays.
1232 */
1233 struct string_to_uint_map *record_next_image;
1234
1235 /* Map for temporarily storing next bindless sampler index when handling
1236 * bindless samplers in struct arrays.
1237 */
1238 struct string_to_uint_map *record_next_bindless_sampler;
1239
1240 /* Map for temporarily storing next bindless image index when handling
1241 * bindless images in struct arrays.
1242 */
1243 struct string_to_uint_map *record_next_bindless_image;
1244
1245 public:
1246 union gl_constant_value *values;
1247
1248 gl_texture_index targets[MAX_SAMPLERS];
1249
1250 /**
1251 * Mask of samplers used by the current shader stage.
1252 */
1253 unsigned shader_samplers_used;
1254
1255 /**
1256 * Mask of samplers used by the current shader stage for shadows.
1257 */
1258 unsigned shader_shadow_samplers;
1259
1260 /**
1261 * Number of bindless samplers used by the current shader stage.
1262 */
1263 unsigned num_bindless_samplers;
1264
1265 /**
1266 * Texture targets for bindless samplers used by the current stage.
1267 */
1268 gl_texture_index *bindless_targets;
1269
1270 /**
1271 * Number of bindless images used by the current shader stage.
1272 */
1273 unsigned num_bindless_images;
1274
1275 /**
1276 * Access types for bindless images used by the current stage.
1277 */
1278 GLenum *bindless_access;
1279
1280 /**
1281 * Bitmask of shader storage blocks not declared as read-only.
1282 */
1283 unsigned shader_storage_blocks_write_access;
1284 };
1285
1286 static bool
variable_is_referenced(ir_array_refcount_visitor & v,ir_variable * var)1287 variable_is_referenced(ir_array_refcount_visitor &v, ir_variable *var)
1288 {
1289 ir_array_refcount_entry *const entry = v.get_variable_entry(var);
1290
1291 return entry->is_referenced;
1292
1293 }
1294
1295 /**
1296 * Walks the IR and update the references to uniform blocks in the
1297 * ir_variables to point at linked shader's list (previously, they
1298 * would point at the uniform block list in one of the pre-linked
1299 * shaders).
1300 */
1301 static void
link_update_uniform_buffer_variables(struct gl_linked_shader * shader,unsigned stage)1302 link_update_uniform_buffer_variables(struct gl_linked_shader *shader,
1303 unsigned stage)
1304 {
1305 ir_array_refcount_visitor v;
1306
1307 v.run(shader->ir);
1308
1309 foreach_in_list(ir_instruction, node, shader->ir) {
1310 ir_variable *const var = node->as_variable();
1311
1312 if (var == NULL || !var->is_in_buffer_block())
1313 continue;
1314
1315 assert(var->data.mode == ir_var_uniform ||
1316 var->data.mode == ir_var_shader_storage);
1317
1318 unsigned num_blocks = var->data.mode == ir_var_uniform ?
1319 shader->Program->info.num_ubos : shader->Program->info.num_ssbos;
1320 struct gl_uniform_block **blks = var->data.mode == ir_var_uniform ?
1321 shader->Program->sh.UniformBlocks :
1322 shader->Program->sh.ShaderStorageBlocks;
1323
1324 if (var->is_interface_instance()) {
1325 const ir_array_refcount_entry *const entry = v.get_variable_entry(var);
1326
1327 if (entry->is_referenced) {
1328 /* Since this is an interface instance, the instance type will be
1329 * same as the array-stripped variable type. If the variable type
1330 * is an array, then the block names will be suffixed with [0]
1331 * through [n-1]. Unlike for non-interface instances, there will
1332 * not be structure types here, so the only name sentinel that we
1333 * have to worry about is [.
1334 */
1335 assert(var->type->without_array() == var->get_interface_type());
1336 const char sentinel = var->type->is_array() ? '[' : '\0';
1337
1338 const ptrdiff_t len = strlen(var->get_interface_type()->name);
1339 for (unsigned i = 0; i < num_blocks; i++) {
1340 const char *const begin = blks[i]->name.string;
1341 const char *const end = strchr(begin, sentinel);
1342
1343 if (end == NULL)
1344 continue;
1345
1346 if (len != (end - begin))
1347 continue;
1348
1349 /* Even when a match is found, do not "break" here. This could
1350 * be an array of instances, and all elements of the array need
1351 * to be marked as referenced.
1352 */
1353 if (strncmp(begin, var->get_interface_type()->name, len) == 0 &&
1354 (!var->type->is_array() ||
1355 entry->is_linearized_index_referenced(blks[i]->linearized_array_index))) {
1356 blks[i]->stageref |= 1U << stage;
1357 }
1358 }
1359 }
1360
1361 var->data.location = 0;
1362 continue;
1363 }
1364
1365 bool found = false;
1366 char sentinel = '\0';
1367
1368 if (var->type->is_struct()) {
1369 sentinel = '.';
1370 } else if (var->type->is_array() && (var->type->fields.array->is_array()
1371 || var->type->without_array()->is_struct())) {
1372 sentinel = '[';
1373 }
1374
1375 const unsigned l = strlen(var->name);
1376 for (unsigned i = 0; i < num_blocks; i++) {
1377 for (unsigned j = 0; j < blks[i]->NumUniforms; j++) {
1378 if (sentinel) {
1379 const char *begin = blks[i]->Uniforms[j].Name;
1380 const char *end = strchr(begin, sentinel);
1381
1382 if (end == NULL)
1383 continue;
1384
1385 if ((ptrdiff_t) l != (end - begin))
1386 continue;
1387
1388 found = strncmp(var->name, begin, l) == 0;
1389 } else {
1390 found = strcmp(var->name, blks[i]->Uniforms[j].Name) == 0;
1391 }
1392
1393 if (found) {
1394 var->data.location = j;
1395
1396 if (variable_is_referenced(v, var))
1397 blks[i]->stageref |= 1U << stage;
1398
1399 break;
1400 }
1401 }
1402
1403 if (found)
1404 break;
1405 }
1406 assert(found);
1407 }
1408 }
1409
1410 /**
1411 * Combine the hidden uniform hash map with the uniform hash map so that the
1412 * hidden uniforms will be given indicies at the end of the uniform storage
1413 * array.
1414 */
1415 static void
assign_hidden_uniform_slot_id(const char * name,unsigned hidden_id,void * closure)1416 assign_hidden_uniform_slot_id(const char *name, unsigned hidden_id,
1417 void *closure)
1418 {
1419 count_uniform_size *uniform_size = (count_uniform_size *) closure;
1420 unsigned hidden_uniform_start = uniform_size->num_active_uniforms -
1421 uniform_size->num_hidden_uniforms;
1422
1423 uniform_size->map->put(hidden_uniform_start + hidden_id, name);
1424 }
1425
1426 static void
link_setup_uniform_remap_tables(const struct gl_constants * consts,struct gl_shader_program * prog)1427 link_setup_uniform_remap_tables(const struct gl_constants *consts,
1428 struct gl_shader_program *prog)
1429 {
1430 unsigned total_entries = prog->NumExplicitUniformLocations;
1431 unsigned empty_locs = prog->NumUniformRemapTable - total_entries;
1432
1433 /* Reserve all the explicit locations of the active uniforms. */
1434 for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
1435 if (prog->data->UniformStorage[i].type->is_subroutine() ||
1436 prog->data->UniformStorage[i].is_shader_storage)
1437 continue;
1438
1439 if (prog->data->UniformStorage[i].remap_location !=
1440 UNMAPPED_UNIFORM_LOC) {
1441 /* How many new entries for this uniform? */
1442 const unsigned entries =
1443 MAX2(1, prog->data->UniformStorage[i].array_elements);
1444
1445 /* Set remap table entries point to correct gl_uniform_storage. */
1446 for (unsigned j = 0; j < entries; j++) {
1447 unsigned element_loc =
1448 prog->data->UniformStorage[i].remap_location + j;
1449 assert(prog->UniformRemapTable[element_loc] ==
1450 INACTIVE_UNIFORM_EXPLICIT_LOCATION);
1451 prog->UniformRemapTable[element_loc] =
1452 &prog->data->UniformStorage[i];
1453 }
1454 }
1455 }
1456
1457 /* Reserve locations for rest of the uniforms. */
1458 for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
1459
1460 if (prog->data->UniformStorage[i].type->is_subroutine() ||
1461 prog->data->UniformStorage[i].is_shader_storage)
1462 continue;
1463
1464 /* Built-in uniforms should not get any location. */
1465 if (prog->data->UniformStorage[i].builtin)
1466 continue;
1467
1468 /* Explicit ones have been set already. */
1469 if (prog->data->UniformStorage[i].remap_location != UNMAPPED_UNIFORM_LOC)
1470 continue;
1471
1472 /* how many new entries for this uniform? */
1473 const unsigned entries =
1474 MAX2(1, prog->data->UniformStorage[i].array_elements);
1475
1476 /* Find UniformRemapTable for empty blocks where we can fit this uniform. */
1477 int chosen_location = -1;
1478
1479 if (empty_locs)
1480 chosen_location = link_util_find_empty_block(prog, &prog->data->UniformStorage[i]);
1481
1482 /* Add new entries to the total amount for checking against MAX_UNIFORM-
1483 * _LOCATIONS. This only applies to the default uniform block (-1),
1484 * because locations of uniform block entries are not assignable.
1485 */
1486 if (prog->data->UniformStorage[i].block_index == -1)
1487 total_entries += entries;
1488
1489 if (chosen_location != -1) {
1490 empty_locs -= entries;
1491 } else {
1492 chosen_location = prog->NumUniformRemapTable;
1493
1494 /* resize remap table to fit new entries */
1495 prog->UniformRemapTable =
1496 reralloc(prog,
1497 prog->UniformRemapTable,
1498 gl_uniform_storage *,
1499 prog->NumUniformRemapTable + entries);
1500 prog->NumUniformRemapTable += entries;
1501 }
1502
1503 /* set pointers for this uniform */
1504 for (unsigned j = 0; j < entries; j++)
1505 prog->UniformRemapTable[chosen_location + j] =
1506 &prog->data->UniformStorage[i];
1507
1508 /* set the base location in remap table for the uniform */
1509 prog->data->UniformStorage[i].remap_location = chosen_location;
1510 }
1511
1512 /* Verify that total amount of entries for explicit and implicit locations
1513 * is less than MAX_UNIFORM_LOCATIONS.
1514 */
1515
1516 if (total_entries > consts->MaxUserAssignableUniformLocations) {
1517 linker_error(prog, "count of uniform locations > MAX_UNIFORM_LOCATIONS"
1518 "(%u > %u)", total_entries,
1519 consts->MaxUserAssignableUniformLocations);
1520 }
1521
1522 /* Reserve all the explicit locations of the active subroutine uniforms. */
1523 for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
1524 if (!prog->data->UniformStorage[i].type->is_subroutine())
1525 continue;
1526
1527 if (prog->data->UniformStorage[i].remap_location == UNMAPPED_UNIFORM_LOC)
1528 continue;
1529
1530 /* How many new entries for this uniform? */
1531 const unsigned entries =
1532 MAX2(1, prog->data->UniformStorage[i].array_elements);
1533
1534 unsigned mask = prog->data->linked_stages;
1535 while (mask) {
1536 const int j = u_bit_scan(&mask);
1537 struct gl_program *p = prog->_LinkedShaders[j]->Program;
1538
1539 if (!prog->data->UniformStorage[i].opaque[j].active)
1540 continue;
1541
1542 /* Set remap table entries point to correct gl_uniform_storage. */
1543 for (unsigned k = 0; k < entries; k++) {
1544 unsigned element_loc =
1545 prog->data->UniformStorage[i].remap_location + k;
1546 assert(p->sh.SubroutineUniformRemapTable[element_loc] ==
1547 INACTIVE_UNIFORM_EXPLICIT_LOCATION);
1548 p->sh.SubroutineUniformRemapTable[element_loc] =
1549 &prog->data->UniformStorage[i];
1550 }
1551 }
1552 }
1553
1554 /* reserve subroutine locations */
1555 for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
1556 if (!prog->data->UniformStorage[i].type->is_subroutine())
1557 continue;
1558
1559 if (prog->data->UniformStorage[i].remap_location !=
1560 UNMAPPED_UNIFORM_LOC)
1561 continue;
1562
1563 const unsigned entries =
1564 MAX2(1, prog->data->UniformStorage[i].array_elements);
1565
1566 unsigned mask = prog->data->linked_stages;
1567 while (mask) {
1568 const int j = u_bit_scan(&mask);
1569 struct gl_program *p = prog->_LinkedShaders[j]->Program;
1570
1571 if (!prog->data->UniformStorage[i].opaque[j].active)
1572 continue;
1573
1574 p->sh.SubroutineUniformRemapTable =
1575 reralloc(p,
1576 p->sh.SubroutineUniformRemapTable,
1577 gl_uniform_storage *,
1578 p->sh.NumSubroutineUniformRemapTable + entries);
1579
1580 for (unsigned k = 0; k < entries; k++) {
1581 p->sh.SubroutineUniformRemapTable[p->sh.NumSubroutineUniformRemapTable + k] =
1582 &prog->data->UniformStorage[i];
1583 }
1584 prog->data->UniformStorage[i].remap_location =
1585 p->sh.NumSubroutineUniformRemapTable;
1586 p->sh.NumSubroutineUniformRemapTable += entries;
1587 }
1588 }
1589 }
1590
1591 static void
link_assign_uniform_storage(const struct gl_constants * consts,struct gl_shader_program * prog,const unsigned num_data_slots)1592 link_assign_uniform_storage(const struct gl_constants *consts,
1593 struct gl_shader_program *prog,
1594 const unsigned num_data_slots)
1595 {
1596 /* On the outside chance that there were no uniforms, bail out.
1597 */
1598 if (prog->data->NumUniformStorage == 0)
1599 return;
1600
1601 unsigned int boolean_true = consts->UniformBooleanTrue;
1602
1603 union gl_constant_value *data;
1604 if (prog->data->UniformStorage == NULL) {
1605 prog->data->UniformStorage = rzalloc_array(prog->data,
1606 struct gl_uniform_storage,
1607 prog->data->NumUniformStorage);
1608 data = rzalloc_array(prog->data->UniformStorage,
1609 union gl_constant_value, num_data_slots);
1610 prog->data->UniformDataDefaults =
1611 rzalloc_array(prog->data->UniformStorage,
1612 union gl_constant_value, num_data_slots);
1613 } else {
1614 data = prog->data->UniformDataSlots;
1615 }
1616
1617 #ifndef NDEBUG
1618 union gl_constant_value *data_end = &data[num_data_slots];
1619 #endif
1620
1621 parcel_out_uniform_storage parcel(prog, prog->UniformHash,
1622 prog->data->UniformStorage, data,
1623 consts->UseSTD430AsDefaultPacking);
1624
1625 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1626 struct gl_linked_shader *shader = prog->_LinkedShaders[i];
1627
1628 if (!shader)
1629 continue;
1630
1631 parcel.start_shader((gl_shader_stage)i);
1632
1633 foreach_in_list(ir_instruction, node, shader->ir) {
1634 ir_variable *const var = node->as_variable();
1635
1636 if ((var == NULL) || (var->data.mode != ir_var_uniform &&
1637 var->data.mode != ir_var_shader_storage))
1638 continue;
1639
1640 parcel.set_and_process(var);
1641 }
1642
1643 shader->Program->SamplersUsed = parcel.shader_samplers_used;
1644 shader->shadow_samplers = parcel.shader_shadow_samplers;
1645 shader->Program->sh.ShaderStorageBlocksWriteAccess =
1646 parcel.shader_storage_blocks_write_access;
1647
1648 if (parcel.num_bindless_samplers > 0) {
1649 shader->Program->sh.NumBindlessSamplers = parcel.num_bindless_samplers;
1650 shader->Program->sh.BindlessSamplers =
1651 rzalloc_array(shader->Program, gl_bindless_sampler,
1652 parcel.num_bindless_samplers);
1653 for (unsigned j = 0; j < parcel.num_bindless_samplers; j++) {
1654 shader->Program->sh.BindlessSamplers[j].target =
1655 parcel.bindless_targets[j];
1656 }
1657 }
1658
1659 if (parcel.num_bindless_images > 0) {
1660 shader->Program->sh.NumBindlessImages = parcel.num_bindless_images;
1661 shader->Program->sh.BindlessImages =
1662 rzalloc_array(shader->Program, gl_bindless_image,
1663 parcel.num_bindless_images);
1664 for (unsigned j = 0; j < parcel.num_bindless_images; j++) {
1665 shader->Program->sh.BindlessImages[j].access =
1666 parcel.bindless_access[j];
1667 }
1668 }
1669
1670 STATIC_ASSERT(ARRAY_SIZE(shader->Program->sh.SamplerTargets) ==
1671 ARRAY_SIZE(parcel.targets));
1672 for (unsigned j = 0; j < ARRAY_SIZE(parcel.targets); j++)
1673 shader->Program->sh.SamplerTargets[j] = parcel.targets[j];
1674 }
1675
1676 #ifndef NDEBUG
1677 for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
1678 assert(prog->data->UniformStorage[i].storage != NULL ||
1679 prog->data->UniformStorage[i].builtin ||
1680 prog->data->UniformStorage[i].is_shader_storage ||
1681 prog->data->UniformStorage[i].block_index != -1);
1682 }
1683
1684 assert(parcel.values == data_end);
1685 #endif
1686
1687 link_setup_uniform_remap_tables(consts, prog);
1688
1689 /* Set shader cache fields */
1690 prog->data->NumUniformDataSlots = num_data_slots;
1691 prog->data->UniformDataSlots = data;
1692
1693 link_set_uniform_initializers(prog, boolean_true);
1694 }
1695
1696 void
link_assign_uniform_locations(struct gl_shader_program * prog,const struct gl_constants * consts)1697 link_assign_uniform_locations(struct gl_shader_program *prog,
1698 const struct gl_constants *consts)
1699 {
1700 ralloc_free(prog->data->UniformStorage);
1701 prog->data->UniformStorage = NULL;
1702 prog->data->NumUniformStorage = 0;
1703
1704 if (prog->UniformHash != NULL) {
1705 prog->UniformHash->clear();
1706 } else {
1707 prog->UniformHash = new string_to_uint_map;
1708 }
1709
1710 /* First pass: Count the uniform resources used by the user-defined
1711 * uniforms. While this happens, each active uniform will have an index
1712 * assigned to it.
1713 *
1714 * Note: this is *NOT* the index that is returned to the application by
1715 * glGetUniformLocation.
1716 */
1717 struct string_to_uint_map *hiddenUniforms = new string_to_uint_map;
1718 count_uniform_size uniform_size(prog->UniformHash, hiddenUniforms,
1719 consts->UseSTD430AsDefaultPacking);
1720 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1721 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
1722
1723 if (sh == NULL)
1724 continue;
1725
1726 link_update_uniform_buffer_variables(sh, i);
1727
1728 /* Reset various per-shader target counts.
1729 */
1730 uniform_size.start_shader();
1731
1732 foreach_in_list(ir_instruction, node, sh->ir) {
1733 ir_variable *const var = node->as_variable();
1734
1735 if ((var == NULL) || (var->data.mode != ir_var_uniform &&
1736 var->data.mode != ir_var_shader_storage))
1737 continue;
1738
1739 uniform_size.process(var);
1740 }
1741
1742 if (uniform_size.num_shader_samplers >
1743 consts->Program[i].MaxTextureImageUnits) {
1744 linker_error(prog, "Too many %s shader texture samplers\n",
1745 _mesa_shader_stage_to_string(i));
1746 continue;
1747 }
1748
1749 if (uniform_size.num_shader_images >
1750 consts->Program[i].MaxImageUniforms) {
1751 linker_error(prog, "Too many %s shader image uniforms (%u > %u)\n",
1752 _mesa_shader_stage_to_string(i),
1753 sh->Program->info.num_images,
1754 consts->Program[i].MaxImageUniforms);
1755 continue;
1756 }
1757
1758 sh->Program->info.num_textures = uniform_size.num_shader_samplers;
1759 sh->Program->info.num_images = uniform_size.num_shader_images;
1760 sh->num_uniform_components = uniform_size.num_shader_uniform_components;
1761 sh->num_combined_uniform_components = sh->num_uniform_components;
1762
1763 for (unsigned i = 0; i < sh->Program->info.num_ubos; i++) {
1764 sh->num_combined_uniform_components +=
1765 sh->Program->sh.UniformBlocks[i]->UniformBufferSize / 4;
1766 }
1767 }
1768
1769 if (prog->data->LinkStatus == LINKING_FAILURE) {
1770 delete hiddenUniforms;
1771 return;
1772 }
1773
1774 prog->data->NumUniformStorage = uniform_size.num_active_uniforms;
1775 prog->data->NumHiddenUniforms = uniform_size.num_hidden_uniforms;
1776
1777 /* assign hidden uniforms a slot id */
1778 hiddenUniforms->iterate(assign_hidden_uniform_slot_id, &uniform_size);
1779 delete hiddenUniforms;
1780
1781 link_assign_uniform_storage(consts, prog, uniform_size.num_values);
1782 }
1783