2 * Copyright © 2011 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
26 #include "ir_uniform.h"
27 #include "glsl_symbol_table.h"
29 #include "string_to_uint_map.h"
30 #include "ir_array_refcount.h"
32 #include "main/mtypes.h"
33 #include "util/strndup.h"
36 * \file link_uniforms.cpp
37 * Assign locations for GLSL uniforms.
39 * \author Ian Romanick <ian.d.romanick@intel.com>
43 * Used by linker to indicate uniforms that have no location set.
45 #define UNMAPPED_UNIFORM_LOC ~0u
48 get_top_level_name(const char *name
)
50 const char *first_dot
= strchr(name
, '.');
51 const char *first_square_bracket
= strchr(name
, '[');
54 /* The ARB_program_interface_query spec says:
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
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
;
72 name_size
= first_square_bracket
- name
;
74 return strndup(name
, name_size
);
78 get_var_name(const char *name
)
80 const char *first_dot
= strchr(name
, '.');
85 return strndup(first_dot
+1, strlen(first_dot
) - 1);
89 is_top_level_shader_storage_block_member(const char* name
,
90 const char* interface_name
,
91 const char* field_name
)
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.
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.
104 * Full instanced name is: interface name + '.' + var name +
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__
);
114 snprintf(full_instanced_name
, name_length
, "%s.%s",
115 interface_name
, field_name
);
117 /* Check if its top-level shader storage block member of an
118 * instanced interface block, or of a unnamed interface block.
120 if (strcmp(name
, full_instanced_name
) == 0 ||
121 strcmp(name
, field_name
) == 0)
124 free(full_instanced_name
);
129 get_array_size(struct gl_uniform_storage
*uni
, const glsl_struct_field
*field
,
130 char *interface_name
, char *var_name
)
132 /* The ARB_program_interface_query spec says:
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
142 if (is_top_level_shader_storage_block_member(uni
->name
,
146 else if (field
->type
->is_array())
147 return field
->type
->length
;
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
)
157 /* The ARB_program_interface_query spec says:
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>."
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
;
174 if (is_top_level_shader_storage_block_member(uni
->name
,
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);
184 return MAX2(array_type
->std140_base_alignment(row_major
), 16);
186 return array_type
->std430_array_stride(row_major
);
193 calculate_array_size_and_stride(struct gl_shader_program
*shProg
,
194 struct gl_uniform_storage
*uni
,
195 bool use_std430_as_default
)
197 if (!uni
->is_shader_storage
)
200 int block_index
= uni
->block_index
;
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
);
209 if (strcmp(var_name
, interface_name
) == 0) {
210 /* Deal with instanced array of SSBOs */
211 char *temp_name
= get_var_name(uni
->name
);
213 linker_error(shProg
, "Out of memory during linking.\n");
214 goto write_top_level_array_size_and_stride
;
217 var_name
= get_top_level_name(temp_name
);
220 linker_error(shProg
, "Out of memory during linking.\n");
221 goto write_top_level_array_size_and_stride
;
225 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
226 const gl_linked_shader
*sh
= shProg
->_LinkedShaders
[i
];
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
)
236 const glsl_type
*iface
= var
->get_interface_type();
238 if (strcmp(interface_name
, iface
->name
) != 0)
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)
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
;
253 write_top_level_array_size_and_stride
:
254 free(interface_name
);
256 uni
->top_level_array_stride
= array_stride
;
257 uni
->top_level_array_size
= array_size
;
261 program_resource_visitor::process(const glsl_type
*type
, const char *name
,
262 bool use_std430_as_default
)
264 assert(type
->without_array()->is_struct()
265 || type
->without_array()->is_interface());
267 unsigned record_array_count
= 1;
268 char *name_copy
= ralloc_strdup(NULL
, name
);
270 enum glsl_interface_packing packing
=
271 type
->get_internal_ifc_packing(use_std430_as_default
);
273 recursion(type
, &name_copy
, strlen(name
), false, NULL
, packing
, false,
274 record_array_count
, NULL
);
275 ralloc_free(name_copy
);
279 program_resource_visitor::process(ir_variable
*var
, bool use_std430_as_default
)
282 var
->data
.from_named_ifc_block
? var
->get_interface_type() : var
->type
;
283 process(var
, t
, use_std430_as_default
);
287 program_resource_visitor::process(ir_variable
*var
, const glsl_type
*var_type
,
288 bool use_std430_as_default
)
290 unsigned record_array_count
= 1;
291 const bool row_major
=
292 var
->data
.matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
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
);
299 const glsl_type
*t
= var_type
;
300 const glsl_type
*t_without_array
= t
->without_array();
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.
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
);
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
?
316 fields
.structure
[t_without_array
->field_index(var
->name
)] : NULL
;
318 recursion(t
, &name
, strlen(name
), row_major
, NULL
, packing
,
319 false, record_array_count
, ifc_member
);
322 this->set_record_array_count(record_array_count
);
323 this->visit_field(t
, var
->name
, row_major
, NULL
, packing
, false);
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
,
333 unsigned record_array_count
,
334 const glsl_struct_field
*named_ifc_member
)
336 /* Records need to have each field processed individually.
338 * Arrays of records need to have each array element processed
339 * individually, then each field of the resulting array elements processed
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())
352 this->enter_record(t
, *name
, row_major
, packing
);
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
;
358 if (t
->is_interface() && t
->fields
.structure
[i
].offset
!= -1)
359 this->set_buffer_offset(t
->fields
.structure
[i
].offset
);
361 /* Append '.field' to the current variable name. */
362 if (name_length
== 0) {
363 ralloc_asprintf_rewrite_tail(name
, &new_length
, "%s", field
);
365 ralloc_asprintf_rewrite_tail(name
, &new_length
, ".%s", field
);
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
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;
383 recursion(t
->fields
.structure
[i
].type
, name
, new_length
,
387 (i
+ 1) == t
->length
, record_array_count
, NULL
);
389 /* Only the first leaf-field of the record gets called with the
390 * record type pointer.
395 if (t
->is_struct()) {
396 (*name
)[name_length
] = '\0';
397 this->leave_record(t
, *name
, row_major
, packing
);
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
;
405 unsigned length
= t
->length
;
407 /* Shader storage block unsized arrays: add subscript [0] to variable
410 if (t
->is_unsized_array())
413 record_array_count
*= length
;
415 for (unsigned i
= 0; i
< length
; i
++) {
416 size_t new_length
= name_length
;
418 /* Append the subscript to the current variable name */
419 ralloc_asprintf_rewrite_tail(name
, &new_length
, "[%u]", i
);
421 recursion(t
->fields
.array
, name
, new_length
, row_major
,
424 (i
+ 1) == t
->length
, record_array_count
,
427 /* Only the first leaf-field of the record gets called with the
428 * record type pointer.
433 this->set_record_array_count(record_array_count
);
434 this->visit_field(t
, *name
, row_major
, record_type
, packing
, last_field
);
439 program_resource_visitor::enter_record(const glsl_type
*, const char *, bool,
440 const enum glsl_interface_packing
)
445 program_resource_visitor::leave_record(const glsl_type
*, const char *, bool,
446 const enum glsl_interface_packing
)
451 program_resource_visitor::set_buffer_offset(unsigned)
456 program_resource_visitor::set_record_array_count(unsigned)
463 * Class to help calculate the storage requirements for a set of uniforms
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.
469 * If the same uniform is added multiple times (i.e., once for each shader
470 * target), it will only be accounted once.
472 class count_uniform_size
: public program_resource_visitor
{
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
)
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;
495 void process(ir_variable
*var
)
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
);
505 program_resource_visitor::process(var
, use_std430_as_default
);
509 * Total number of active uniforms counted
511 unsigned num_active_uniforms
;
513 unsigned num_hidden_uniforms
;
516 * Number of data values required to back the storage for the active uniforms
521 * Number of samplers used
523 unsigned num_shader_samplers
;
526 * Number of images used
528 unsigned num_shader_images
;
531 * Number of uniforms used in the current shader
533 unsigned num_shader_uniform_components
;
536 * Number of subroutine uniforms used
538 unsigned num_shader_subroutines
;
540 bool is_buffer_block
;
541 bool is_shader_storage
;
543 struct string_to_uint_map
*map
;
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 */)
552 assert(!type
->without_array()->is_struct());
553 assert(!type
->without_array()->is_interface());
554 assert(!(type
->is_array() && type
->fields
.array
->is_array()));
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.
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;
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.
578 if (!is_shader_storage
)
579 this->num_shader_uniform_components
+= values
;
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.
585 if (!is_buffer_block
)
586 this->num_shader_uniform_components
+= values
;
589 /* If the uniform is already in the map, there's nothing more to do.
592 if (this->map
->get(id
, name
))
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
++;
599 this->map
->put(this->num_active_uniforms
-this->num_hidden_uniforms
,
603 /* Each leaf uniform occupies one entry in the list of active
606 this->num_active_uniforms
++;
608 if(!is_gl_identifier(name
) && !is_shader_storage
&& !is_buffer_block
)
609 this->num_values
+= values
;
612 struct string_to_uint_map
*hidden_map
;
615 * Current variable being processed.
617 ir_variable
*current_var
;
619 bool use_std430_as_default
;
622 } /* anonymous namespace */
625 link_calculate_matrix_stride(const glsl_type
*matrix
, bool row_major
,
626 enum glsl_interface_packing packing
)
628 const unsigned N
= matrix
->is_double() ? 8 : 4;
629 const unsigned items
=
630 row_major
? matrix
->matrix_columns
: matrix
->vector_elements
;
634 /* Matrix stride for std430 mat2xY matrices are not rounded up to
637 * Section 7.6.2.2 "Standard Uniform Block Layout" of the OpenGL 4.3 spec
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,
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.
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).
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
661 return packing
== GLSL_INTERFACE_PACKING_STD430
662 ? (items
< 3 ? items
* N
: glsl_align(items
* N
, 16))
663 : glsl_align(items
* N
, 16);
667 * Class to help parcel out pieces of backing storage to uniforms
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.
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
681 class parcel_out_uniform_storage
: public program_resource_visitor
{
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 : prog(prog
), map(map
), uniforms(uniforms
),
689 use_std430_as_default(use_std430_as_default
), values(values
),
690 bindless_targets(NULL
), bindless_access(NULL
),
691 shader_storage_blocks_write_access(0)
695 virtual ~parcel_out_uniform_storage()
697 free(this->bindless_targets
);
698 free(this->bindless_access
);
701 void start_shader(gl_shader_stage shader_type
)
703 assert(shader_type
< MESA_SHADER_STAGES
);
704 this->shader_type
= shader_type
;
706 this->shader_samplers_used
= 0;
707 this->shader_shadow_samplers
= 0;
708 this->next_sampler
= 0;
709 this->next_image
= 0;
710 this->next_subroutine
= 0;
711 this->record_array_count
= 1;
712 memset(this->targets
, 0, sizeof(this->targets
));
714 this->num_bindless_samplers
= 0;
715 this->next_bindless_sampler
= 0;
716 free(this->bindless_targets
);
717 this->bindless_targets
= NULL
;
719 this->num_bindless_images
= 0;
720 this->next_bindless_image
= 0;
721 free(this->bindless_access
);
722 this->bindless_access
= NULL
;
723 this->shader_storage_blocks_write_access
= 0;
726 void set_and_process(ir_variable
*var
)
730 this->record_next_sampler
= new string_to_uint_map
;
731 this->record_next_bindless_sampler
= new string_to_uint_map
;
732 this->record_next_image
= new string_to_uint_map
;
733 this->record_next_bindless_image
= new string_to_uint_map
;
735 buffer_block_index
= -1;
736 if (var
->is_in_buffer_block()) {
737 struct gl_uniform_block
*blks
= var
->is_in_shader_storage_block() ?
738 prog
->data
->ShaderStorageBlocks
: prog
->data
->UniformBlocks
;
739 unsigned num_blks
= var
->is_in_shader_storage_block() ?
740 prog
->data
->NumShaderStorageBlocks
: prog
->data
->NumUniformBlocks
;
741 bool is_interface_array
=
742 var
->is_interface_instance() && var
->type
->is_array();
744 if (is_interface_array
) {
745 unsigned l
= strlen(var
->get_interface_type()->name
);
747 for (unsigned i
= 0; i
< num_blks
; i
++) {
748 if (strncmp(var
->get_interface_type()->name
, blks
[i
].Name
, l
)
749 == 0 && blks
[i
].Name
[l
] == '[') {
750 buffer_block_index
= i
;
755 for (unsigned i
= 0; i
< num_blks
; i
++) {
756 if (strcmp(var
->get_interface_type()->name
, blks
[i
].Name
) == 0) {
757 buffer_block_index
= i
;
762 assert(buffer_block_index
!= -1);
764 if (var
->is_in_shader_storage_block() &&
765 !var
->data
.memory_read_only
) {
766 unsigned array_size
= is_interface_array
?
767 var
->type
->array_size() : 1;
769 STATIC_ASSERT(MAX_SHADER_STORAGE_BUFFERS
<= 32);
771 /* Shaders that use too many SSBOs will fail to compile, which
772 * we don't care about.
774 * This is true for shaders that do not use too many SSBOs:
776 if (buffer_block_index
+ array_size
<= 32) {
777 shader_storage_blocks_write_access
|=
778 u_bit_consecutive(buffer_block_index
, array_size
);
782 /* Uniform blocks that were specified with an instance name must be
783 * handled a little bit differently. The name of the variable is the
784 * name used to reference the uniform block instead of being the name
785 * of a variable within the block. Therefore, searching for the name
786 * within the block will fail.
788 if (var
->is_interface_instance()) {
790 process(var
->get_interface_type(),
791 var
->get_interface_type()->name
,
792 use_std430_as_default
);
794 const struct gl_uniform_block
*const block
=
795 &blks
[buffer_block_index
];
797 assert(var
->data
.location
!= -1);
799 const struct gl_uniform_buffer_variable
*const ubo_var
=
800 &block
->Uniforms
[var
->data
.location
];
802 ubo_byte_offset
= ubo_var
->Offset
;
803 process(var
, use_std430_as_default
);
806 /* Store any explicit location and reset data location so we can
807 * reuse this variable for storing the uniform slot number.
809 this->explicit_location
= current_var
->data
.location
;
810 current_var
->data
.location
= -1;
812 process(var
, use_std430_as_default
);
814 delete this->record_next_sampler
;
815 delete this->record_next_bindless_sampler
;
816 delete this->record_next_image
;
817 delete this->record_next_bindless_image
;
820 int buffer_block_index
;
822 gl_shader_stage shader_type
;
825 bool set_opaque_indices(const glsl_type
*base_type
,
826 struct gl_uniform_storage
*uniform
,
827 const char *name
, unsigned &next_index
,
828 struct string_to_uint_map
*record_next_index
)
830 assert(base_type
->is_sampler() || base_type
->is_image());
832 if (this->record_array_count
> 1) {
833 unsigned inner_array_size
= MAX2(1, uniform
->array_elements
);
834 char *name_copy
= ralloc_strdup(NULL
, name
);
836 /* Remove all array subscripts from the sampler/image name */
839 while((str_start
= strchr(name_copy
, '[')) &&
840 (str_end
= strchr(name_copy
, ']'))) {
841 memmove(str_start
, str_end
+ 1, 1 + strlen(str_end
+ 1));
845 if (record_next_index
->get(index
, name_copy
)) {
846 /* In this case, we've already seen this uniform so we just use the
847 * next sampler/image index recorded the last time we visited.
849 uniform
->opaque
[shader_type
].index
= index
;
850 index
= inner_array_size
+ uniform
->opaque
[shader_type
].index
;
851 record_next_index
->put(index
, name_copy
);
853 ralloc_free(name_copy
);
854 /* Return as everything else has already been initialised in a
859 /* We've never seen this uniform before so we need to allocate
860 * enough indices to store it.
862 * Nested struct arrays behave like arrays of arrays so we need to
863 * increase the index by the total number of elements of the
864 * sampler/image in case there is more than one sampler/image
865 * inside the structs. This allows the offset to be easily
866 * calculated for indirect indexing.
868 uniform
->opaque
[shader_type
].index
= next_index
;
869 next_index
+= inner_array_size
* this->record_array_count
;
871 /* Store the next index for future passes over the struct array
873 index
= uniform
->opaque
[shader_type
].index
+ inner_array_size
;
874 record_next_index
->put(index
, name_copy
);
875 ralloc_free(name_copy
);
878 /* Increment the sampler/image by 1 for non-arrays and by the number
879 * of array elements for arrays.
881 uniform
->opaque
[shader_type
].index
= next_index
;
882 next_index
+= MAX2(1, uniform
->array_elements
);
887 void handle_samplers(const glsl_type
*base_type
,
888 struct gl_uniform_storage
*uniform
, const char *name
)
890 if (base_type
->is_sampler()) {
891 uniform
->opaque
[shader_type
].active
= true;
893 const gl_texture_index target
= base_type
->sampler_index();
894 const unsigned shadow
= base_type
->sampler_shadow
;
896 if (current_var
->data
.bindless
) {
897 if (!set_opaque_indices(base_type
, uniform
, name
,
898 this->next_bindless_sampler
,
899 this->record_next_bindless_sampler
))
902 this->num_bindless_samplers
= this->next_bindless_sampler
;
904 this->bindless_targets
= (gl_texture_index
*)
905 realloc(this->bindless_targets
,
906 this->num_bindless_samplers
* sizeof(gl_texture_index
));
908 for (unsigned i
= uniform
->opaque
[shader_type
].index
;
909 i
< this->num_bindless_samplers
;
911 this->bindless_targets
[i
] = target
;
914 if (!set_opaque_indices(base_type
, uniform
, name
,
916 this->record_next_sampler
))
919 for (unsigned i
= uniform
->opaque
[shader_type
].index
;
920 i
< MIN2(this->next_sampler
, MAX_SAMPLERS
);
922 this->targets
[i
] = target
;
923 this->shader_samplers_used
|= 1U << i
;
924 this->shader_shadow_samplers
|= shadow
<< i
;
930 void handle_images(const glsl_type
*base_type
,
931 struct gl_uniform_storage
*uniform
, const char *name
)
933 if (base_type
->is_image()) {
934 uniform
->opaque
[shader_type
].active
= true;
936 /* Set image access qualifiers */
937 const GLenum access
=
938 current_var
->data
.memory_read_only
?
939 (current_var
->data
.memory_write_only
? GL_NONE
:
941 (current_var
->data
.memory_write_only
? GL_WRITE_ONLY
:
944 if (current_var
->data
.bindless
) {
945 if (!set_opaque_indices(base_type
, uniform
, name
,
946 this->next_bindless_image
,
947 this->record_next_bindless_image
))
950 this->num_bindless_images
= this->next_bindless_image
;
952 this->bindless_access
= (GLenum
*)
953 realloc(this->bindless_access
,
954 this->num_bindless_images
* sizeof(GLenum
));
956 for (unsigned i
= uniform
->opaque
[shader_type
].index
;
957 i
< this->num_bindless_images
;
959 this->bindless_access
[i
] = access
;
962 if (!set_opaque_indices(base_type
, uniform
, name
,
964 this->record_next_image
))
967 for (unsigned i
= uniform
->opaque
[shader_type
].index
;
968 i
< MIN2(this->next_image
, MAX_IMAGE_UNIFORMS
);
970 prog
->_LinkedShaders
[shader_type
]->Program
->sh
.ImageAccess
[i
] = access
;
976 void handle_subroutines(const glsl_type
*base_type
,
977 struct gl_uniform_storage
*uniform
)
979 if (base_type
->is_subroutine()) {
980 uniform
->opaque
[shader_type
].index
= this->next_subroutine
;
981 uniform
->opaque
[shader_type
].active
= true;
983 prog
->_LinkedShaders
[shader_type
]->Program
->sh
.NumSubroutineUniforms
++;
985 /* Increment the subroutine index by 1 for non-arrays and by the
986 * number of array elements for arrays.
988 this->next_subroutine
+= MAX2(1, uniform
->array_elements
);
993 virtual void set_buffer_offset(unsigned offset
)
995 this->ubo_byte_offset
= offset
;
998 virtual void set_record_array_count(unsigned record_array_count
)
1000 this->record_array_count
= record_array_count
;
1003 virtual void enter_record(const glsl_type
*type
, const char *,
1005 const enum glsl_interface_packing packing
)
1007 assert(type
->is_struct());
1008 if (this->buffer_block_index
== -1)
1010 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
1011 this->ubo_byte_offset
= glsl_align(
1012 this->ubo_byte_offset
, type
->std430_base_alignment(row_major
));
1014 this->ubo_byte_offset
= glsl_align(
1015 this->ubo_byte_offset
, type
->std140_base_alignment(row_major
));
1018 virtual void leave_record(const glsl_type
*type
, const char *,
1020 const enum glsl_interface_packing packing
)
1022 assert(type
->is_struct());
1023 if (this->buffer_block_index
== -1)
1025 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
1026 this->ubo_byte_offset
= glsl_align(
1027 this->ubo_byte_offset
, type
->std430_base_alignment(row_major
));
1029 this->ubo_byte_offset
= glsl_align(
1030 this->ubo_byte_offset
, type
->std140_base_alignment(row_major
));
1033 virtual void visit_field(const glsl_type
*type
, const char *name
,
1034 bool row_major
, const glsl_type
* /* record_type */,
1035 const enum glsl_interface_packing packing
,
1036 bool /* last_field */)
1038 assert(!type
->without_array()->is_struct());
1039 assert(!type
->without_array()->is_interface());
1040 assert(!(type
->is_array() && type
->fields
.array
->is_array()));
1043 bool found
= this->map
->get(id
, name
);
1049 const glsl_type
*base_type
;
1050 if (type
->is_array()) {
1051 this->uniforms
[id
].array_elements
= type
->length
;
1052 base_type
= type
->fields
.array
;
1054 this->uniforms
[id
].array_elements
= 0;
1058 /* Initialise opaque data */
1059 this->uniforms
[id
].opaque
[shader_type
].index
= ~0;
1060 this->uniforms
[id
].opaque
[shader_type
].active
= false;
1062 if (current_var
->data
.used
|| base_type
->is_subroutine())
1063 this->uniforms
[id
].active_shader_mask
|= 1 << shader_type
;
1065 /* This assigns uniform indices to sampler and image uniforms. */
1066 handle_samplers(base_type
, &this->uniforms
[id
], name
);
1067 handle_images(base_type
, &this->uniforms
[id
], name
);
1068 handle_subroutines(base_type
, &this->uniforms
[id
]);
1070 /* For array of arrays or struct arrays the base location may have
1071 * already been set so don't set it again.
1073 if (buffer_block_index
== -1 && current_var
->data
.location
== -1) {
1074 current_var
->data
.location
= id
;
1077 /* If there is already storage associated with this uniform or if the
1078 * uniform is set as builtin, it means that it was set while processing
1079 * an earlier shader stage. For example, we may be processing the
1080 * uniform in the fragment shader, but the uniform was already processed
1081 * in the vertex shader.
1083 if (this->uniforms
[id
].storage
!= NULL
|| this->uniforms
[id
].builtin
) {
1087 /* Assign explicit locations. */
1088 if (current_var
->data
.explicit_location
) {
1089 /* Set sequential locations for struct fields. */
1090 if (current_var
->type
->without_array()->is_struct() ||
1091 current_var
->type
->is_array_of_arrays()) {
1092 const unsigned entries
= MAX2(1, this->uniforms
[id
].array_elements
);
1093 this->uniforms
[id
].remap_location
=
1094 this->explicit_location
+ field_counter
;
1095 field_counter
+= entries
;
1097 this->uniforms
[id
].remap_location
= this->explicit_location
;
1100 /* Initialize to to indicate that no location is set */
1101 this->uniforms
[id
].remap_location
= UNMAPPED_UNIFORM_LOC
;
1104 this->uniforms
[id
].name
= ralloc_strdup(this->uniforms
, name
);
1105 this->uniforms
[id
].type
= base_type
;
1106 this->uniforms
[id
].num_driver_storage
= 0;
1107 this->uniforms
[id
].driver_storage
= NULL
;
1108 this->uniforms
[id
].atomic_buffer_index
= -1;
1109 this->uniforms
[id
].hidden
=
1110 current_var
->data
.how_declared
== ir_var_hidden
;
1111 this->uniforms
[id
].builtin
= is_gl_identifier(name
);
1113 this->uniforms
[id
].is_shader_storage
=
1114 current_var
->is_in_shader_storage_block();
1115 this->uniforms
[id
].is_bindless
= current_var
->data
.bindless
;
1117 /* Do not assign storage if the uniform is a builtin or buffer object */
1118 if (!this->uniforms
[id
].builtin
&&
1119 !this->uniforms
[id
].is_shader_storage
&&
1120 this->buffer_block_index
== -1)
1121 this->uniforms
[id
].storage
= this->values
;
1123 if (this->buffer_block_index
!= -1) {
1124 this->uniforms
[id
].block_index
= this->buffer_block_index
;
1126 unsigned alignment
= type
->std140_base_alignment(row_major
);
1127 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
1128 alignment
= type
->std430_base_alignment(row_major
);
1129 this->ubo_byte_offset
= glsl_align(this->ubo_byte_offset
, alignment
);
1130 this->uniforms
[id
].offset
= this->ubo_byte_offset
;
1131 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
1132 this->ubo_byte_offset
+= type
->std430_size(row_major
);
1134 this->ubo_byte_offset
+= type
->std140_size(row_major
);
1136 if (type
->is_array()) {
1137 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
1138 this->uniforms
[id
].array_stride
=
1139 type
->without_array()->std430_array_stride(row_major
);
1141 this->uniforms
[id
].array_stride
=
1142 glsl_align(type
->without_array()->std140_size(row_major
),
1145 this->uniforms
[id
].array_stride
= 0;
1148 if (type
->without_array()->is_matrix()) {
1149 this->uniforms
[id
].matrix_stride
=
1150 link_calculate_matrix_stride(type
->without_array(),
1153 this->uniforms
[id
].row_major
= row_major
;
1155 this->uniforms
[id
].matrix_stride
= 0;
1156 this->uniforms
[id
].row_major
= false;
1159 this->uniforms
[id
].block_index
= -1;
1160 this->uniforms
[id
].offset
= -1;
1161 this->uniforms
[id
].array_stride
= -1;
1162 this->uniforms
[id
].matrix_stride
= -1;
1163 this->uniforms
[id
].row_major
= false;
1166 if (!this->uniforms
[id
].builtin
&&
1167 !this->uniforms
[id
].is_shader_storage
&&
1168 this->buffer_block_index
== -1)
1169 this->values
+= type
->component_slots();
1171 calculate_array_size_and_stride(prog
, &this->uniforms
[id
],
1172 use_std430_as_default
);
1176 * Current program being processed.
1178 struct gl_shader_program
*prog
;
1180 struct string_to_uint_map
*map
;
1182 struct gl_uniform_storage
*uniforms
;
1183 unsigned next_sampler
;
1184 unsigned next_bindless_sampler
;
1185 unsigned next_image
;
1186 unsigned next_bindless_image
;
1187 unsigned next_subroutine
;
1189 bool use_std430_as_default
;
1192 * Field counter is used to take care that uniform structures
1193 * with explicit locations get sequential locations.
1195 unsigned field_counter
;
1198 * Current variable being processed.
1200 ir_variable
*current_var
;
1202 /* Used to store the explicit location from current_var so that we can
1203 * reuse the location field for storing the uniform slot id.
1205 int explicit_location
;
1207 /* Stores total struct array elements including nested structs */
1208 unsigned record_array_count
;
1210 /* Map for temporarily storing next sampler index when handling samplers in
1213 struct string_to_uint_map
*record_next_sampler
;
1215 /* Map for temporarily storing next imager index when handling images in
1218 struct string_to_uint_map
*record_next_image
;
1220 /* Map for temporarily storing next bindless sampler index when handling
1221 * bindless samplers in struct arrays.
1223 struct string_to_uint_map
*record_next_bindless_sampler
;
1225 /* Map for temporarily storing next bindless image index when handling
1226 * bindless images in struct arrays.
1228 struct string_to_uint_map
*record_next_bindless_image
;
1231 union gl_constant_value
*values
;
1233 gl_texture_index targets
[MAX_SAMPLERS
];
1236 * Mask of samplers used by the current shader stage.
1238 unsigned shader_samplers_used
;
1241 * Mask of samplers used by the current shader stage for shadows.
1243 unsigned shader_shadow_samplers
;
1246 * Number of bindless samplers used by the current shader stage.
1248 unsigned num_bindless_samplers
;
1251 * Texture targets for bindless samplers used by the current stage.
1253 gl_texture_index
*bindless_targets
;
1256 * Number of bindless images used by the current shader stage.
1258 unsigned num_bindless_images
;
1261 * Access types for bindless images used by the current stage.
1263 GLenum
*bindless_access
;
1266 * Bitmask of shader storage blocks not declared as read-only.
1268 unsigned shader_storage_blocks_write_access
;
1272 variable_is_referenced(ir_array_refcount_visitor
&v
, ir_variable
*var
)
1274 ir_array_refcount_entry
*const entry
= v
.get_variable_entry(var
);
1276 return entry
->is_referenced
;
1281 * Walks the IR and update the references to uniform blocks in the
1282 * ir_variables to point at linked shader's list (previously, they
1283 * would point at the uniform block list in one of the pre-linked
1287 link_update_uniform_buffer_variables(struct gl_linked_shader
*shader
,
1290 ir_array_refcount_visitor v
;
1294 foreach_in_list(ir_instruction
, node
, shader
->ir
) {
1295 ir_variable
*const var
= node
->as_variable();
1297 if (var
== NULL
|| !var
->is_in_buffer_block())
1300 assert(var
->data
.mode
== ir_var_uniform
||
1301 var
->data
.mode
== ir_var_shader_storage
);
1303 unsigned num_blocks
= var
->data
.mode
== ir_var_uniform
?
1304 shader
->Program
->info
.num_ubos
: shader
->Program
->info
.num_ssbos
;
1305 struct gl_uniform_block
**blks
= var
->data
.mode
== ir_var_uniform
?
1306 shader
->Program
->sh
.UniformBlocks
:
1307 shader
->Program
->sh
.ShaderStorageBlocks
;
1309 if (var
->is_interface_instance()) {
1310 const ir_array_refcount_entry
*const entry
= v
.get_variable_entry(var
);
1312 if (entry
->is_referenced
) {
1313 /* Since this is an interface instance, the instance type will be
1314 * same as the array-stripped variable type. If the variable type
1315 * is an array, then the block names will be suffixed with [0]
1316 * through [n-1]. Unlike for non-interface instances, there will
1317 * not be structure types here, so the only name sentinel that we
1318 * have to worry about is [.
1320 assert(var
->type
->without_array() == var
->get_interface_type());
1321 const char sentinel
= var
->type
->is_array() ? '[' : '\0';
1323 const ptrdiff_t len
= strlen(var
->get_interface_type()->name
);
1324 for (unsigned i
= 0; i
< num_blocks
; i
++) {
1325 const char *const begin
= blks
[i
]->Name
;
1326 const char *const end
= strchr(begin
, sentinel
);
1331 if (len
!= (end
- begin
))
1334 /* Even when a match is found, do not "break" here. This could
1335 * be an array of instances, and all elements of the array need
1336 * to be marked as referenced.
1338 if (strncmp(begin
, var
->get_interface_type()->name
, len
) == 0 &&
1339 (!var
->type
->is_array() ||
1340 entry
->is_linearized_index_referenced(blks
[i
]->linearized_array_index
))) {
1341 blks
[i
]->stageref
|= 1U << stage
;
1346 var
->data
.location
= 0;
1351 char sentinel
= '\0';
1353 if (var
->type
->is_struct()) {
1355 } else if (var
->type
->is_array() && (var
->type
->fields
.array
->is_array()
1356 || var
->type
->without_array()->is_struct())) {
1360 const unsigned l
= strlen(var
->name
);
1361 for (unsigned i
= 0; i
< num_blocks
; i
++) {
1362 for (unsigned j
= 0; j
< blks
[i
]->NumUniforms
; j
++) {
1364 const char *begin
= blks
[i
]->Uniforms
[j
].Name
;
1365 const char *end
= strchr(begin
, sentinel
);
1370 if ((ptrdiff_t) l
!= (end
- begin
))
1373 found
= strncmp(var
->name
, begin
, l
) == 0;
1375 found
= strcmp(var
->name
, blks
[i
]->Uniforms
[j
].Name
) == 0;
1379 var
->data
.location
= j
;
1381 if (variable_is_referenced(v
, var
))
1382 blks
[i
]->stageref
|= 1U << stage
;
1396 * Combine the hidden uniform hash map with the uniform hash map so that the
1397 * hidden uniforms will be given indicies at the end of the uniform storage
1401 assign_hidden_uniform_slot_id(const char *name
, unsigned hidden_id
,
1404 count_uniform_size
*uniform_size
= (count_uniform_size
*) closure
;
1405 unsigned hidden_uniform_start
= uniform_size
->num_active_uniforms
-
1406 uniform_size
->num_hidden_uniforms
;
1408 uniform_size
->map
->put(hidden_uniform_start
+ hidden_id
, name
);
1412 link_setup_uniform_remap_tables(struct gl_context
*ctx
,
1413 struct gl_shader_program
*prog
)
1415 unsigned total_entries
= prog
->NumExplicitUniformLocations
;
1416 unsigned empty_locs
= prog
->NumUniformRemapTable
- total_entries
;
1418 /* Reserve all the explicit locations of the active uniforms. */
1419 for (unsigned i
= 0; i
< prog
->data
->NumUniformStorage
; i
++) {
1420 if (prog
->data
->UniformStorage
[i
].type
->is_subroutine() ||
1421 prog
->data
->UniformStorage
[i
].is_shader_storage
)
1424 if (prog
->data
->UniformStorage
[i
].remap_location
!=
1425 UNMAPPED_UNIFORM_LOC
) {
1426 /* How many new entries for this uniform? */
1427 const unsigned entries
=
1428 MAX2(1, prog
->data
->UniformStorage
[i
].array_elements
);
1430 /* Set remap table entries point to correct gl_uniform_storage. */
1431 for (unsigned j
= 0; j
< entries
; j
++) {
1432 unsigned element_loc
=
1433 prog
->data
->UniformStorage
[i
].remap_location
+ j
;
1434 assert(prog
->UniformRemapTable
[element_loc
] ==
1435 INACTIVE_UNIFORM_EXPLICIT_LOCATION
);
1436 prog
->UniformRemapTable
[element_loc
] =
1437 &prog
->data
->UniformStorage
[i
];
1442 /* Reserve locations for rest of the uniforms. */
1443 for (unsigned i
= 0; i
< prog
->data
->NumUniformStorage
; i
++) {
1445 if (prog
->data
->UniformStorage
[i
].type
->is_subroutine() ||
1446 prog
->data
->UniformStorage
[i
].is_shader_storage
)
1449 /* Built-in uniforms should not get any location. */
1450 if (prog
->data
->UniformStorage
[i
].builtin
)
1453 /* Explicit ones have been set already. */
1454 if (prog
->data
->UniformStorage
[i
].remap_location
!= UNMAPPED_UNIFORM_LOC
)
1457 /* how many new entries for this uniform? */
1458 const unsigned entries
=
1459 MAX2(1, prog
->data
->UniformStorage
[i
].array_elements
);
1461 /* Find UniformRemapTable for empty blocks where we can fit this uniform. */
1462 int chosen_location
= -1;
1465 chosen_location
= link_util_find_empty_block(prog
, &prog
->data
->UniformStorage
[i
]);
1467 /* Add new entries to the total amount for checking against MAX_UNIFORM-
1468 * _LOCATIONS. This only applies to the default uniform block (-1),
1469 * because locations of uniform block entries are not assignable.
1471 if (prog
->data
->UniformStorage
[i
].block_index
== -1)
1472 total_entries
+= entries
;
1474 if (chosen_location
!= -1) {
1475 empty_locs
-= entries
;
1477 chosen_location
= prog
->NumUniformRemapTable
;
1479 /* resize remap table to fit new entries */
1480 prog
->UniformRemapTable
=
1482 prog
->UniformRemapTable
,
1483 gl_uniform_storage
*,
1484 prog
->NumUniformRemapTable
+ entries
);
1485 prog
->NumUniformRemapTable
+= entries
;
1488 /* set pointers for this uniform */
1489 for (unsigned j
= 0; j
< entries
; j
++)
1490 prog
->UniformRemapTable
[chosen_location
+ j
] =
1491 &prog
->data
->UniformStorage
[i
];
1493 /* set the base location in remap table for the uniform */
1494 prog
->data
->UniformStorage
[i
].remap_location
= chosen_location
;
1497 /* Verify that total amount of entries for explicit and implicit locations
1498 * is less than MAX_UNIFORM_LOCATIONS.
1501 if (total_entries
> ctx
->Const
.MaxUserAssignableUniformLocations
) {
1502 linker_error(prog
, "count of uniform locations > MAX_UNIFORM_LOCATIONS"
1503 "(%u > %u)", total_entries
,
1504 ctx
->Const
.MaxUserAssignableUniformLocations
);
1507 /* Reserve all the explicit locations of the active subroutine uniforms. */
1508 for (unsigned i
= 0; i
< prog
->data
->NumUniformStorage
; i
++) {
1509 if (!prog
->data
->UniformStorage
[i
].type
->is_subroutine())
1512 if (prog
->data
->UniformStorage
[i
].remap_location
== UNMAPPED_UNIFORM_LOC
)
1515 /* How many new entries for this uniform? */
1516 const unsigned entries
=
1517 MAX2(1, prog
->data
->UniformStorage
[i
].array_elements
);
1519 unsigned mask
= prog
->data
->linked_stages
;
1521 const int j
= u_bit_scan(&mask
);
1522 struct gl_program
*p
= prog
->_LinkedShaders
[j
]->Program
;
1524 if (!prog
->data
->UniformStorage
[i
].opaque
[j
].active
)
1527 /* Set remap table entries point to correct gl_uniform_storage. */
1528 for (unsigned k
= 0; k
< entries
; k
++) {
1529 unsigned element_loc
=
1530 prog
->data
->UniformStorage
[i
].remap_location
+ k
;
1531 assert(p
->sh
.SubroutineUniformRemapTable
[element_loc
] ==
1532 INACTIVE_UNIFORM_EXPLICIT_LOCATION
);
1533 p
->sh
.SubroutineUniformRemapTable
[element_loc
] =
1534 &prog
->data
->UniformStorage
[i
];
1539 /* reserve subroutine locations */
1540 for (unsigned i
= 0; i
< prog
->data
->NumUniformStorage
; i
++) {
1541 if (!prog
->data
->UniformStorage
[i
].type
->is_subroutine())
1544 if (prog
->data
->UniformStorage
[i
].remap_location
!=
1545 UNMAPPED_UNIFORM_LOC
)
1548 const unsigned entries
=
1549 MAX2(1, prog
->data
->UniformStorage
[i
].array_elements
);
1551 unsigned mask
= prog
->data
->linked_stages
;
1553 const int j
= u_bit_scan(&mask
);
1554 struct gl_program
*p
= prog
->_LinkedShaders
[j
]->Program
;
1556 if (!prog
->data
->UniformStorage
[i
].opaque
[j
].active
)
1559 p
->sh
.SubroutineUniformRemapTable
=
1561 p
->sh
.SubroutineUniformRemapTable
,
1562 gl_uniform_storage
*,
1563 p
->sh
.NumSubroutineUniformRemapTable
+ entries
);
1565 for (unsigned k
= 0; k
< entries
; k
++) {
1566 p
->sh
.SubroutineUniformRemapTable
[p
->sh
.NumSubroutineUniformRemapTable
+ k
] =
1567 &prog
->data
->UniformStorage
[i
];
1569 prog
->data
->UniformStorage
[i
].remap_location
=
1570 p
->sh
.NumSubroutineUniformRemapTable
;
1571 p
->sh
.NumSubroutineUniformRemapTable
+= entries
;
1577 link_assign_uniform_storage(struct gl_context
*ctx
,
1578 struct gl_shader_program
*prog
,
1579 const unsigned num_data_slots
)
1581 /* On the outside chance that there were no uniforms, bail out.
1583 if (prog
->data
->NumUniformStorage
== 0)
1586 unsigned int boolean_true
= ctx
->Const
.UniformBooleanTrue
;
1588 union gl_constant_value
*data
;
1589 if (prog
->data
->UniformStorage
== NULL
) {
1590 prog
->data
->UniformStorage
= rzalloc_array(prog
->data
,
1591 struct gl_uniform_storage
,
1592 prog
->data
->NumUniformStorage
);
1593 data
= rzalloc_array(prog
->data
->UniformStorage
,
1594 union gl_constant_value
, num_data_slots
);
1595 prog
->data
->UniformDataDefaults
=
1596 rzalloc_array(prog
->data
->UniformStorage
,
1597 union gl_constant_value
, num_data_slots
);
1599 data
= prog
->data
->UniformDataSlots
;
1603 union gl_constant_value
*data_end
= &data
[num_data_slots
];
1606 parcel_out_uniform_storage
parcel(prog
, prog
->UniformHash
,
1607 prog
->data
->UniformStorage
, data
,
1608 ctx
->Const
.UseSTD430AsDefaultPacking
);
1610 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1611 struct gl_linked_shader
*shader
= prog
->_LinkedShaders
[i
];
1616 parcel
.start_shader((gl_shader_stage
)i
);
1618 foreach_in_list(ir_instruction
, node
, shader
->ir
) {
1619 ir_variable
*const var
= node
->as_variable();
1621 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
&&
1622 var
->data
.mode
!= ir_var_shader_storage
))
1625 parcel
.set_and_process(var
);
1628 shader
->Program
->SamplersUsed
= parcel
.shader_samplers_used
;
1629 shader
->shadow_samplers
= parcel
.shader_shadow_samplers
;
1630 shader
->Program
->sh
.ShaderStorageBlocksWriteAccess
=
1631 parcel
.shader_storage_blocks_write_access
;
1633 if (parcel
.num_bindless_samplers
> 0) {
1634 shader
->Program
->sh
.NumBindlessSamplers
= parcel
.num_bindless_samplers
;
1635 shader
->Program
->sh
.BindlessSamplers
=
1636 rzalloc_array(shader
->Program
, gl_bindless_sampler
,
1637 parcel
.num_bindless_samplers
);
1638 for (unsigned j
= 0; j
< parcel
.num_bindless_samplers
; j
++) {
1639 shader
->Program
->sh
.BindlessSamplers
[j
].target
=
1640 parcel
.bindless_targets
[j
];
1644 if (parcel
.num_bindless_images
> 0) {
1645 shader
->Program
->sh
.NumBindlessImages
= parcel
.num_bindless_images
;
1646 shader
->Program
->sh
.BindlessImages
=
1647 rzalloc_array(shader
->Program
, gl_bindless_image
,
1648 parcel
.num_bindless_images
);
1649 for (unsigned j
= 0; j
< parcel
.num_bindless_images
; j
++) {
1650 shader
->Program
->sh
.BindlessImages
[j
].access
=
1651 parcel
.bindless_access
[j
];
1655 STATIC_ASSERT(ARRAY_SIZE(shader
->Program
->sh
.SamplerTargets
) ==
1656 ARRAY_SIZE(parcel
.targets
));
1657 for (unsigned j
= 0; j
< ARRAY_SIZE(parcel
.targets
); j
++)
1658 shader
->Program
->sh
.SamplerTargets
[j
] = parcel
.targets
[j
];
1662 for (unsigned i
= 0; i
< prog
->data
->NumUniformStorage
; i
++) {
1663 assert(prog
->data
->UniformStorage
[i
].storage
!= NULL
||
1664 prog
->data
->UniformStorage
[i
].builtin
||
1665 prog
->data
->UniformStorage
[i
].is_shader_storage
||
1666 prog
->data
->UniformStorage
[i
].block_index
!= -1);
1669 assert(parcel
.values
== data_end
);
1672 link_setup_uniform_remap_tables(ctx
, prog
);
1674 /* Set shader cache fields */
1675 prog
->data
->NumUniformDataSlots
= num_data_slots
;
1676 prog
->data
->UniformDataSlots
= data
;
1678 link_set_uniform_initializers(prog
, boolean_true
);
1682 link_assign_uniform_locations(struct gl_shader_program
*prog
,
1683 struct gl_context
*ctx
)
1685 ralloc_free(prog
->data
->UniformStorage
);
1686 prog
->data
->UniformStorage
= NULL
;
1687 prog
->data
->NumUniformStorage
= 0;
1689 if (prog
->UniformHash
!= NULL
) {
1690 prog
->UniformHash
->clear();
1692 prog
->UniformHash
= new string_to_uint_map
;
1695 /* First pass: Count the uniform resources used by the user-defined
1696 * uniforms. While this happens, each active uniform will have an index
1699 * Note: this is *NOT* the index that is returned to the application by
1700 * glGetUniformLocation.
1702 struct string_to_uint_map
*hiddenUniforms
= new string_to_uint_map
;
1703 count_uniform_size
uniform_size(prog
->UniformHash
, hiddenUniforms
,
1704 ctx
->Const
.UseSTD430AsDefaultPacking
);
1705 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1706 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
1711 link_update_uniform_buffer_variables(sh
, i
);
1713 /* Reset various per-shader target counts.
1715 uniform_size
.start_shader();
1717 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
1718 ir_variable
*const var
= node
->as_variable();
1720 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
&&
1721 var
->data
.mode
!= ir_var_shader_storage
))
1724 uniform_size
.process(var
);
1727 if (uniform_size
.num_shader_samplers
>
1728 ctx
->Const
.Program
[i
].MaxTextureImageUnits
) {
1729 linker_error(prog
, "Too many %s shader texture samplers\n",
1730 _mesa_shader_stage_to_string(i
));
1734 if (uniform_size
.num_shader_images
>
1735 ctx
->Const
.Program
[i
].MaxImageUniforms
) {
1736 linker_error(prog
, "Too many %s shader image uniforms (%u > %u)\n",
1737 _mesa_shader_stage_to_string(i
),
1738 sh
->Program
->info
.num_images
,
1739 ctx
->Const
.Program
[i
].MaxImageUniforms
);
1743 sh
->Program
->info
.num_textures
= uniform_size
.num_shader_samplers
;
1744 sh
->Program
->info
.num_images
= uniform_size
.num_shader_images
;
1745 sh
->num_uniform_components
= uniform_size
.num_shader_uniform_components
;
1746 sh
->num_combined_uniform_components
= sh
->num_uniform_components
;
1748 for (unsigned i
= 0; i
< sh
->Program
->info
.num_ubos
; i
++) {
1749 sh
->num_combined_uniform_components
+=
1750 sh
->Program
->sh
.UniformBlocks
[i
]->UniformBufferSize
/ 4;
1754 if (prog
->data
->LinkStatus
== LINKING_FAILURE
) {
1755 delete hiddenUniforms
;
1759 prog
->data
->NumUniformStorage
= uniform_size
.num_active_uniforms
;
1760 prog
->data
->NumHiddenUniforms
= uniform_size
.num_hidden_uniforms
;
1762 /* assign hidden uniforms a slot id */
1763 hiddenUniforms
->iterate(assign_hidden_uniform_slot_id
, &uniform_size
);
1764 delete hiddenUniforms
;
1766 link_assign_uniform_storage(ctx
, prog
, uniform_size
.num_values
);