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