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.
24 #include "main/core.h"
27 #include "ir_uniform.h"
28 #include "glsl_symbol_table.h"
30 #include "util/string_to_uint_map.h"
31 #include "ir_array_refcount.h"
34 * \file link_uniforms.cpp
35 * Assign locations for GLSL uniforms.
37 * \author Ian Romanick <ian.d.romanick@intel.com>
41 * Used by linker to indicate uniforms that have no location set.
43 #define UNMAPPED_UNIFORM_LOC ~0u
46 program_resource_visitor::process(const glsl_type
*type
, const char *name
)
48 assert(type
->without_array()->is_record()
49 || type
->without_array()->is_interface());
51 unsigned record_array_count
= 1;
52 char *name_copy
= ralloc_strdup(NULL
, name
);
53 enum glsl_interface_packing packing
= type
->get_interface_packing();
55 recursion(type
, &name_copy
, strlen(name
), false, NULL
, packing
, false,
56 record_array_count
, NULL
);
57 ralloc_free(name_copy
);
61 program_resource_visitor::process(ir_variable
*var
)
63 unsigned record_array_count
= 1;
64 const bool row_major
=
65 var
->data
.matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
67 const enum glsl_interface_packing packing
= var
->get_interface_type() ?
68 var
->get_interface_type_packing() :
69 var
->type
->get_interface_packing();
72 var
->data
.from_named_ifc_block
? var
->get_interface_type() : var
->type
;
73 const glsl_type
*t_without_array
= t
->without_array();
75 /* false is always passed for the row_major parameter to the other
76 * processing functions because no information is available to do
77 * otherwise. See the warning in linker.h.
79 if (t_without_array
->is_record() ||
80 (t
->is_array() && t
->fields
.array
->is_array())) {
81 char *name
= ralloc_strdup(NULL
, var
->name
);
82 recursion(var
->type
, &name
, strlen(name
), row_major
, NULL
, packing
,
83 false, record_array_count
, NULL
);
85 } else if (t_without_array
->is_interface()) {
86 char *name
= ralloc_strdup(NULL
, t_without_array
->name
);
87 const glsl_struct_field
*ifc_member
= var
->data
.from_named_ifc_block
?
89 fields
.structure
[t_without_array
->field_index(var
->name
)] : NULL
;
91 recursion(t
, &name
, strlen(name
), row_major
, NULL
, packing
,
92 false, record_array_count
, ifc_member
);
95 this->set_record_array_count(record_array_count
);
96 this->visit_field(t
, var
->name
, row_major
, NULL
, packing
, false);
101 program_resource_visitor::recursion(const glsl_type
*t
, char **name
,
102 size_t name_length
, bool row_major
,
103 const glsl_type
*record_type
,
104 const enum glsl_interface_packing packing
,
106 unsigned record_array_count
,
107 const glsl_struct_field
*named_ifc_member
)
109 /* Records need to have each field processed individually.
111 * Arrays of records need to have each array element processed
112 * individually, then each field of the resulting array elements processed
115 if (t
->is_interface() && named_ifc_member
) {
116 ralloc_asprintf_rewrite_tail(name
, &name_length
, ".%s",
117 named_ifc_member
->name
);
118 recursion(named_ifc_member
->type
, name
, name_length
, row_major
, NULL
,
119 packing
, false, record_array_count
, NULL
);
120 } else if (t
->is_record() || t
->is_interface()) {
121 if (record_type
== NULL
&& t
->is_record())
125 this->enter_record(t
, *name
, row_major
, packing
);
127 for (unsigned i
= 0; i
< t
->length
; i
++) {
128 const char *field
= t
->fields
.structure
[i
].name
;
129 size_t new_length
= name_length
;
131 if (t
->fields
.structure
[i
].type
->is_record())
132 this->visit_field(&t
->fields
.structure
[i
]);
134 if (t
->is_interface() && t
->fields
.structure
[i
].offset
!= -1)
135 this->set_buffer_offset(t
->fields
.structure
[i
].offset
);
137 /* Append '.field' to the current variable name. */
138 if (name_length
== 0) {
139 ralloc_asprintf_rewrite_tail(name
, &new_length
, "%s", field
);
141 ralloc_asprintf_rewrite_tail(name
, &new_length
, ".%s", field
);
144 /* The layout of structures at the top level of the block is set
145 * during parsing. For matrices contained in multiple levels of
146 * structures in the block, the inner structures have no layout.
147 * These cases must potentially inherit the layout from the outer
150 bool field_row_major
= row_major
;
151 const enum glsl_matrix_layout matrix_layout
=
152 glsl_matrix_layout(t
->fields
.structure
[i
].matrix_layout
);
153 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
154 field_row_major
= true;
155 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
156 field_row_major
= false;
159 recursion(t
->fields
.structure
[i
].type
, name
, new_length
,
163 (i
+ 1) == t
->length
, record_array_count
, NULL
);
165 /* Only the first leaf-field of the record gets called with the
166 * record type pointer.
171 if (t
->is_record()) {
172 (*name
)[name_length
] = '\0';
173 this->leave_record(t
, *name
, row_major
, packing
);
175 } else if (t
->without_array()->is_record() ||
176 t
->without_array()->is_interface() ||
177 (t
->is_array() && t
->fields
.array
->is_array())) {
178 if (record_type
== NULL
&& t
->fields
.array
->is_record())
179 record_type
= t
->fields
.array
;
181 unsigned length
= t
->length
;
183 /* Shader storage block unsized arrays: add subscript [0] to variable
186 if (t
->is_unsized_array())
189 record_array_count
*= length
;
191 for (unsigned i
= 0; i
< length
; i
++) {
192 size_t new_length
= name_length
;
194 /* Append the subscript to the current variable name */
195 ralloc_asprintf_rewrite_tail(name
, &new_length
, "[%u]", i
);
197 recursion(t
->fields
.array
, name
, new_length
, row_major
,
200 (i
+ 1) == t
->length
, record_array_count
,
203 /* Only the first leaf-field of the record gets called with the
204 * record type pointer.
209 this->set_record_array_count(record_array_count
);
210 this->visit_field(t
, *name
, row_major
, record_type
, packing
, last_field
);
215 program_resource_visitor::visit_field(const glsl_struct_field
*)
220 program_resource_visitor::enter_record(const glsl_type
*, const char *, bool,
221 const enum glsl_interface_packing
)
226 program_resource_visitor::leave_record(const glsl_type
*, const char *, bool,
227 const enum glsl_interface_packing
)
232 program_resource_visitor::set_buffer_offset(unsigned)
237 program_resource_visitor::set_record_array_count(unsigned)
244 * Class to help calculate the storage requirements for a set of uniforms
246 * As uniforms are added to the active set the number of active uniforms and
247 * the storage requirements for those uniforms are accumulated. The active
248 * uniforms are added to the hash table supplied to the constructor.
250 * If the same uniform is added multiple times (i.e., once for each shader
251 * target), it will only be accounted once.
253 class count_uniform_size
: public program_resource_visitor
{
255 count_uniform_size(struct string_to_uint_map
*map
,
256 struct string_to_uint_map
*hidden_map
)
257 : num_active_uniforms(0), num_hidden_uniforms(0), num_values(0),
258 num_shader_samplers(0), num_shader_images(0),
259 num_shader_uniform_components(0), num_shader_subroutines(0),
260 is_buffer_block(false), is_shader_storage(false), map(map
),
261 hidden_map(hidden_map
), current_var(NULL
)
268 this->num_shader_samplers
= 0;
269 this->num_shader_images
= 0;
270 this->num_shader_uniform_components
= 0;
271 this->num_shader_subroutines
= 0;
274 void process(ir_variable
*var
)
276 this->current_var
= var
;
277 this->is_buffer_block
= var
->is_in_buffer_block();
278 this->is_shader_storage
= var
->is_in_shader_storage_block();
279 if (var
->is_interface_instance())
280 program_resource_visitor::process(var
->get_interface_type(),
281 var
->get_interface_type()->name
);
283 program_resource_visitor::process(var
);
287 * Total number of active uniforms counted
289 unsigned num_active_uniforms
;
291 unsigned num_hidden_uniforms
;
294 * Number of data values required to back the storage for the active uniforms
299 * Number of samplers used
301 unsigned num_shader_samplers
;
304 * Number of images used
306 unsigned num_shader_images
;
309 * Number of uniforms used in the current shader
311 unsigned num_shader_uniform_components
;
314 * Number of subroutine uniforms used
316 unsigned num_shader_subroutines
;
318 bool is_buffer_block
;
319 bool is_shader_storage
;
321 struct string_to_uint_map
*map
;
324 virtual void visit_field(const glsl_type
*type
, const char *name
,
325 bool /* row_major */,
326 const glsl_type
* /* record_type */,
327 const enum glsl_interface_packing
,
328 bool /* last_field */)
330 assert(!type
->without_array()->is_record());
331 assert(!type
->without_array()->is_interface());
332 assert(!(type
->is_array() && type
->fields
.array
->is_array()));
334 /* Count the number of samplers regardless of whether the uniform is
335 * already in the hash table. The hash table prevents adding the same
336 * uniform for multiple shader targets, but in this case we want to
337 * count it for each shader target.
339 const unsigned values
= type
->component_slots();
340 if (type
->contains_subroutine()) {
341 this->num_shader_subroutines
+= values
;
342 } else if (type
->contains_sampler() && !current_var
->data
.bindless
) {
343 /* Samplers (bound or bindless) are counted as two components as
344 * specified by ARB_bindless_texture. */
345 this->num_shader_samplers
+= values
/ 2;
346 } else if (type
->contains_image() && !current_var
->data
.bindless
) {
347 /* Images (bound or bindless) are counted as two components as
348 * specified by ARB_bindless_texture. */
349 this->num_shader_images
+= values
/ 2;
351 /* As drivers are likely to represent image uniforms as
352 * scalar indices, count them against the limit of uniform
353 * components in the default block. The spec allows image
354 * uniforms to use up no more than one scalar slot.
356 if (!is_shader_storage
)
357 this->num_shader_uniform_components
+= values
;
359 /* Accumulate the total number of uniform slots used by this shader.
360 * Note that samplers do not count against this limit because they
361 * don't use any storage on current hardware.
363 if (!is_buffer_block
)
364 this->num_shader_uniform_components
+= values
;
367 /* If the uniform is already in the map, there's nothing more to do.
370 if (this->map
->get(id
, name
))
373 if (this->current_var
->data
.how_declared
== ir_var_hidden
) {
374 this->hidden_map
->put(this->num_hidden_uniforms
, name
);
375 this->num_hidden_uniforms
++;
377 this->map
->put(this->num_active_uniforms
-this->num_hidden_uniforms
,
381 /* Each leaf uniform occupies one entry in the list of active
384 this->num_active_uniforms
++;
386 if(!is_gl_identifier(name
) && !is_shader_storage
&& !is_buffer_block
)
387 this->num_values
+= values
;
390 struct string_to_uint_map
*hidden_map
;
393 * Current variable being processed.
395 ir_variable
*current_var
;
398 } /* anonymous namespace */
401 * Class to help parcel out pieces of backing storage to uniforms
403 * Each uniform processed has some range of the \c gl_constant_value
404 * structures associated with it. The association is done by finding
405 * the uniform in the \c string_to_uint_map and using the value from
406 * the map to connect that slot in the \c gl_uniform_storage table
407 * with the next available slot in the \c gl_constant_value array.
410 * This class assumes that every uniform that will be processed is
411 * already in the \c string_to_uint_map. In addition, it assumes that
412 * the \c gl_uniform_storage and \c gl_constant_value arrays are "big
415 class parcel_out_uniform_storage
: public program_resource_visitor
{
417 parcel_out_uniform_storage(struct gl_shader_program
*prog
,
418 struct string_to_uint_map
*map
,
419 struct gl_uniform_storage
*uniforms
,
420 union gl_constant_value
*values
)
421 : prog(prog
), map(map
), uniforms(uniforms
), values(values
)
425 void start_shader(gl_shader_stage shader_type
)
427 assert(shader_type
< MESA_SHADER_STAGES
);
428 this->shader_type
= shader_type
;
430 this->shader_samplers_used
= 0;
431 this->shader_shadow_samplers
= 0;
432 this->next_sampler
= 0;
433 this->next_image
= 0;
434 this->next_subroutine
= 0;
435 this->record_array_count
= 1;
436 memset(this->targets
, 0, sizeof(this->targets
));
439 void set_and_process(ir_variable
*var
)
443 this->record_next_sampler
= new string_to_uint_map
;
444 this->record_next_image
= new string_to_uint_map
;
446 buffer_block_index
= -1;
447 if (var
->is_in_buffer_block()) {
448 struct gl_uniform_block
*blks
= var
->is_in_shader_storage_block() ?
449 prog
->data
->ShaderStorageBlocks
: prog
->data
->UniformBlocks
;
450 unsigned num_blks
= var
->is_in_shader_storage_block() ?
451 prog
->data
->NumShaderStorageBlocks
: prog
->data
->NumUniformBlocks
;
453 if (var
->is_interface_instance() && var
->type
->is_array()) {
454 unsigned l
= strlen(var
->get_interface_type()->name
);
456 for (unsigned i
= 0; i
< num_blks
; i
++) {
457 if (strncmp(var
->get_interface_type()->name
, blks
[i
].Name
, l
)
458 == 0 && blks
[i
].Name
[l
] == '[') {
459 buffer_block_index
= i
;
464 for (unsigned i
= 0; i
< num_blks
; i
++) {
465 if (strcmp(var
->get_interface_type()->name
, blks
[i
].Name
) == 0) {
466 buffer_block_index
= i
;
471 assert(buffer_block_index
!= -1);
473 /* Uniform blocks that were specified with an instance name must be
474 * handled a little bit differently. The name of the variable is the
475 * name used to reference the uniform block instead of being the name
476 * of a variable within the block. Therefore, searching for the name
477 * within the block will fail.
479 if (var
->is_interface_instance()) {
481 process(var
->get_interface_type(),
482 var
->get_interface_type()->name
);
484 const struct gl_uniform_block
*const block
=
485 &blks
[buffer_block_index
];
487 assert(var
->data
.location
!= -1);
489 const struct gl_uniform_buffer_variable
*const ubo_var
=
490 &block
->Uniforms
[var
->data
.location
];
492 ubo_byte_offset
= ubo_var
->Offset
;
496 /* Store any explicit location and reset data location so we can
497 * reuse this variable for storing the uniform slot number.
499 this->explicit_location
= current_var
->data
.location
;
500 current_var
->data
.location
= -1;
504 delete this->record_next_sampler
;
505 delete this->record_next_image
;
508 int buffer_block_index
;
510 gl_shader_stage shader_type
;
513 bool set_opaque_indices(const glsl_type
*base_type
,
514 struct gl_uniform_storage
*uniform
,
515 const char *name
, unsigned &next_index
,
516 struct string_to_uint_map
*record_next_index
)
518 assert(base_type
->is_sampler() || base_type
->is_image());
520 if (this->record_array_count
> 1) {
521 unsigned inner_array_size
= MAX2(1, uniform
->array_elements
);
522 char *name_copy
= ralloc_strdup(NULL
, name
);
524 /* Remove all array subscripts from the sampler/image name */
527 while((str_start
= strchr(name_copy
, '[')) &&
528 (str_end
= strchr(name_copy
, ']'))) {
529 memmove(str_start
, str_end
+ 1, 1 + strlen(str_end
+ 1));
533 if (record_next_index
->get(index
, name_copy
)) {
534 /* In this case, we've already seen this uniform so we just use the
535 * next sampler/image index recorded the last time we visited.
537 uniform
->opaque
[shader_type
].index
= index
;
538 index
= inner_array_size
+ uniform
->opaque
[shader_type
].index
;
539 record_next_index
->put(index
, name_copy
);
541 ralloc_free(name_copy
);
542 /* Return as everything else has already been initialised in a
547 /* We've never seen this uniform before so we need to allocate
548 * enough indices to store it.
550 * Nested struct arrays behave like arrays of arrays so we need to
551 * increase the index by the total number of elements of the
552 * sampler/image in case there is more than one sampler/image
553 * inside the structs. This allows the offset to be easily
554 * calculated for indirect indexing.
556 uniform
->opaque
[shader_type
].index
= next_index
;
557 next_index
+= inner_array_size
* this->record_array_count
;
559 /* Store the next index for future passes over the struct array
561 index
= uniform
->opaque
[shader_type
].index
+ inner_array_size
;
562 record_next_index
->put(index
, name_copy
);
563 ralloc_free(name_copy
);
566 /* Increment the sampler/image by 1 for non-arrays and by the number
567 * of array elements for arrays.
569 uniform
->opaque
[shader_type
].index
= next_index
;
570 next_index
+= MAX2(1, uniform
->array_elements
);
575 void handle_samplers(const glsl_type
*base_type
,
576 struct gl_uniform_storage
*uniform
, const char *name
)
578 if (base_type
->is_sampler()) {
579 uniform
->opaque
[shader_type
].active
= true;
581 if (!set_opaque_indices(base_type
, uniform
, name
, this->next_sampler
,
582 this->record_next_sampler
))
585 const gl_texture_index target
= base_type
->sampler_index();
586 const unsigned shadow
= base_type
->sampler_shadow
;
587 for (unsigned i
= uniform
->opaque
[shader_type
].index
;
588 i
< MIN2(this->next_sampler
, MAX_SAMPLERS
);
590 this->targets
[i
] = target
;
591 this->shader_samplers_used
|= 1U << i
;
592 this->shader_shadow_samplers
|= shadow
<< i
;
597 void handle_images(const glsl_type
*base_type
,
598 struct gl_uniform_storage
*uniform
, const char *name
)
600 if (base_type
->is_image()) {
601 uniform
->opaque
[shader_type
].active
= true;
603 if (!set_opaque_indices(base_type
, uniform
, name
, this->next_image
,
604 this->record_next_image
))
607 /* Set image access qualifiers */
608 const GLenum access
=
609 (current_var
->data
.memory_read_only
? GL_READ_ONLY
:
610 current_var
->data
.memory_write_only
? GL_WRITE_ONLY
:
613 for (unsigned i
= uniform
->opaque
[shader_type
].index
;
614 i
< MIN2(this->next_image
, MAX_IMAGE_UNIFORMS
);
616 prog
->_LinkedShaders
[shader_type
]->Program
->sh
.ImageAccess
[i
] = access
;
621 void handle_subroutines(const glsl_type
*base_type
,
622 struct gl_uniform_storage
*uniform
)
624 if (base_type
->is_subroutine()) {
625 uniform
->opaque
[shader_type
].index
= this->next_subroutine
;
626 uniform
->opaque
[shader_type
].active
= true;
628 prog
->_LinkedShaders
[shader_type
]->Program
->sh
.NumSubroutineUniforms
++;
630 /* Increment the subroutine index by 1 for non-arrays and by the
631 * number of array elements for arrays.
633 this->next_subroutine
+= MAX2(1, uniform
->array_elements
);
638 virtual void set_buffer_offset(unsigned offset
)
640 this->ubo_byte_offset
= offset
;
643 virtual void set_record_array_count(unsigned record_array_count
)
645 this->record_array_count
= record_array_count
;
648 virtual void enter_record(const glsl_type
*type
, const char *,
650 const enum glsl_interface_packing packing
)
652 assert(type
->is_record());
653 if (this->buffer_block_index
== -1)
655 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
656 this->ubo_byte_offset
= glsl_align(
657 this->ubo_byte_offset
, type
->std430_base_alignment(row_major
));
659 this->ubo_byte_offset
= glsl_align(
660 this->ubo_byte_offset
, type
->std140_base_alignment(row_major
));
663 virtual void leave_record(const glsl_type
*type
, const char *,
665 const enum glsl_interface_packing packing
)
667 assert(type
->is_record());
668 if (this->buffer_block_index
== -1)
670 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
671 this->ubo_byte_offset
= glsl_align(
672 this->ubo_byte_offset
, type
->std430_base_alignment(row_major
));
674 this->ubo_byte_offset
= glsl_align(
675 this->ubo_byte_offset
, type
->std140_base_alignment(row_major
));
678 virtual void visit_field(const glsl_type
*type
, const char *name
,
679 bool row_major
, const glsl_type
* /* record_type */,
680 const enum glsl_interface_packing packing
,
681 bool /* last_field */)
683 assert(!type
->without_array()->is_record());
684 assert(!type
->without_array()->is_interface());
685 assert(!(type
->is_array() && type
->fields
.array
->is_array()));
688 bool found
= this->map
->get(id
, name
);
694 const glsl_type
*base_type
;
695 if (type
->is_array()) {
696 this->uniforms
[id
].array_elements
= type
->length
;
697 base_type
= type
->fields
.array
;
699 this->uniforms
[id
].array_elements
= 0;
703 /* Initialise opaque data */
704 this->uniforms
[id
].opaque
[shader_type
].index
= ~0;
705 this->uniforms
[id
].opaque
[shader_type
].active
= false;
707 /* This assigns uniform indices to sampler and image uniforms. */
708 handle_samplers(base_type
, &this->uniforms
[id
], name
);
709 handle_images(base_type
, &this->uniforms
[id
], name
);
710 handle_subroutines(base_type
, &this->uniforms
[id
]);
712 /* For array of arrays or struct arrays the base location may have
713 * already been set so don't set it again.
715 if (buffer_block_index
== -1 && current_var
->data
.location
== -1) {
716 current_var
->data
.location
= id
;
719 /* If there is already storage associated with this uniform or if the
720 * uniform is set as builtin, it means that it was set while processing
721 * an earlier shader stage. For example, we may be processing the
722 * uniform in the fragment shader, but the uniform was already processed
723 * in the vertex shader.
725 if (this->uniforms
[id
].storage
!= NULL
|| this->uniforms
[id
].builtin
) {
729 /* Assign explicit locations. */
730 if (current_var
->data
.explicit_location
) {
731 /* Set sequential locations for struct fields. */
732 if (current_var
->type
->without_array()->is_record() ||
733 current_var
->type
->is_array_of_arrays()) {
734 const unsigned entries
= MAX2(1, this->uniforms
[id
].array_elements
);
735 this->uniforms
[id
].remap_location
=
736 this->explicit_location
+ field_counter
;
737 field_counter
+= entries
;
739 this->uniforms
[id
].remap_location
= this->explicit_location
;
742 /* Initialize to to indicate that no location is set */
743 this->uniforms
[id
].remap_location
= UNMAPPED_UNIFORM_LOC
;
746 this->uniforms
[id
].name
= ralloc_strdup(this->uniforms
, name
);
747 this->uniforms
[id
].type
= base_type
;
748 this->uniforms
[id
].num_driver_storage
= 0;
749 this->uniforms
[id
].driver_storage
= NULL
;
750 this->uniforms
[id
].atomic_buffer_index
= -1;
751 this->uniforms
[id
].hidden
=
752 current_var
->data
.how_declared
== ir_var_hidden
;
753 this->uniforms
[id
].builtin
= is_gl_identifier(name
);
755 this->uniforms
[id
].is_shader_storage
=
756 current_var
->is_in_shader_storage_block();
758 /* Do not assign storage if the uniform is a builtin or buffer object */
759 if (!this->uniforms
[id
].builtin
&&
760 !this->uniforms
[id
].is_shader_storage
&&
761 this->buffer_block_index
== -1)
762 this->uniforms
[id
].storage
= this->values
;
764 if (this->buffer_block_index
!= -1) {
765 this->uniforms
[id
].block_index
= this->buffer_block_index
;
767 unsigned alignment
= type
->std140_base_alignment(row_major
);
768 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
769 alignment
= type
->std430_base_alignment(row_major
);
770 this->ubo_byte_offset
= glsl_align(this->ubo_byte_offset
, alignment
);
771 this->uniforms
[id
].offset
= this->ubo_byte_offset
;
772 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
773 this->ubo_byte_offset
+= type
->std430_size(row_major
);
775 this->ubo_byte_offset
+= type
->std140_size(row_major
);
777 if (type
->is_array()) {
778 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
779 this->uniforms
[id
].array_stride
=
780 type
->without_array()->std430_array_stride(row_major
);
782 this->uniforms
[id
].array_stride
=
783 glsl_align(type
->without_array()->std140_size(row_major
),
786 this->uniforms
[id
].array_stride
= 0;
789 if (type
->without_array()->is_matrix()) {
790 const glsl_type
*matrix
= type
->without_array();
791 const unsigned N
= matrix
->is_double() ? 8 : 4;
792 const unsigned items
=
793 row_major
? matrix
->matrix_columns
: matrix
->vector_elements
;
796 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
797 this->uniforms
[id
].matrix_stride
= items
< 3 ? items
* N
:
798 glsl_align(items
* N
, 16);
800 this->uniforms
[id
].matrix_stride
= glsl_align(items
* N
, 16);
801 this->uniforms
[id
].row_major
= row_major
;
803 this->uniforms
[id
].matrix_stride
= 0;
804 this->uniforms
[id
].row_major
= false;
807 this->uniforms
[id
].block_index
= -1;
808 this->uniforms
[id
].offset
= -1;
809 this->uniforms
[id
].array_stride
= -1;
810 this->uniforms
[id
].matrix_stride
= -1;
811 this->uniforms
[id
].row_major
= false;
814 if (!this->uniforms
[id
].builtin
&&
815 !this->uniforms
[id
].is_shader_storage
&&
816 this->buffer_block_index
== -1)
817 this->values
+= type
->component_slots();
821 * Current program being processed.
823 struct gl_shader_program
*prog
;
825 struct string_to_uint_map
*map
;
827 struct gl_uniform_storage
*uniforms
;
828 unsigned next_sampler
;
830 unsigned next_subroutine
;
833 * Field counter is used to take care that uniform structures
834 * with explicit locations get sequential locations.
836 unsigned field_counter
;
839 * Current variable being processed.
841 ir_variable
*current_var
;
843 /* Used to store the explicit location from current_var so that we can
844 * reuse the location field for storing the uniform slot id.
846 int explicit_location
;
848 /* Stores total struct array elements including nested structs */
849 unsigned record_array_count
;
851 /* Map for temporarily storing next sampler index when handling samplers in
854 struct string_to_uint_map
*record_next_sampler
;
856 /* Map for temporarily storing next imager index when handling images in
859 struct string_to_uint_map
*record_next_image
;
862 union gl_constant_value
*values
;
864 gl_texture_index targets
[MAX_SAMPLERS
];
867 * Mask of samplers used by the current shader stage.
869 unsigned shader_samplers_used
;
872 * Mask of samplers used by the current shader stage for shadows.
874 unsigned shader_shadow_samplers
;
878 variable_is_referenced(ir_array_refcount_visitor
&v
, ir_variable
*var
)
880 ir_array_refcount_entry
*const entry
= v
.get_variable_entry(var
);
882 return entry
->is_referenced
;
887 * Walks the IR and update the references to uniform blocks in the
888 * ir_variables to point at linked shader's list (previously, they
889 * would point at the uniform block list in one of the pre-linked
893 link_update_uniform_buffer_variables(struct gl_linked_shader
*shader
,
896 ir_array_refcount_visitor v
;
900 foreach_in_list(ir_instruction
, node
, shader
->ir
) {
901 ir_variable
*const var
= node
->as_variable();
903 if (var
== NULL
|| !var
->is_in_buffer_block())
906 assert(var
->data
.mode
== ir_var_uniform
||
907 var
->data
.mode
== ir_var_shader_storage
);
909 unsigned num_blocks
= var
->data
.mode
== ir_var_uniform
?
910 shader
->Program
->info
.num_ubos
: shader
->Program
->info
.num_ssbos
;
911 struct gl_uniform_block
**blks
= var
->data
.mode
== ir_var_uniform
?
912 shader
->Program
->sh
.UniformBlocks
:
913 shader
->Program
->sh
.ShaderStorageBlocks
;
915 if (var
->is_interface_instance()) {
916 const ir_array_refcount_entry
*const entry
= v
.get_variable_entry(var
);
918 if (entry
->is_referenced
) {
919 /* Since this is an interface instance, the instance type will be
920 * same as the array-stripped variable type. If the variable type
921 * is an array, then the block names will be suffixed with [0]
922 * through [n-1]. Unlike for non-interface instances, there will
923 * not be structure types here, so the only name sentinel that we
924 * have to worry about is [.
926 assert(var
->type
->without_array() == var
->get_interface_type());
927 const char sentinel
= var
->type
->is_array() ? '[' : '\0';
929 const ptrdiff_t len
= strlen(var
->get_interface_type()->name
);
930 for (unsigned i
= 0; i
< num_blocks
; i
++) {
931 const char *const begin
= blks
[i
]->Name
;
932 const char *const end
= strchr(begin
, sentinel
);
937 if (len
!= (end
- begin
))
940 /* Even when a match is found, do not "break" here. This could
941 * be an array of instances, and all elements of the array need
942 * to be marked as referenced.
944 if (strncmp(begin
, var
->get_interface_type()->name
, len
) == 0 &&
945 (!var
->type
->is_array() ||
946 entry
->is_linearized_index_referenced(blks
[i
]->linearized_array_index
))) {
947 blks
[i
]->stageref
|= 1U << stage
;
952 var
->data
.location
= 0;
957 char sentinel
= '\0';
959 if (var
->type
->is_record()) {
961 } else if (var
->type
->is_array() && (var
->type
->fields
.array
->is_array()
962 || var
->type
->without_array()->is_record())) {
966 const unsigned l
= strlen(var
->name
);
967 for (unsigned i
= 0; i
< num_blocks
; i
++) {
968 for (unsigned j
= 0; j
< blks
[i
]->NumUniforms
; j
++) {
970 const char *begin
= blks
[i
]->Uniforms
[j
].Name
;
971 const char *end
= strchr(begin
, sentinel
);
976 if ((ptrdiff_t) l
!= (end
- begin
))
979 found
= strncmp(var
->name
, begin
, l
) == 0;
981 found
= strcmp(var
->name
, blks
[i
]->Uniforms
[j
].Name
) == 0;
985 var
->data
.location
= j
;
987 if (variable_is_referenced(v
, var
))
988 blks
[i
]->stageref
|= 1U << stage
;
1002 * Combine the hidden uniform hash map with the uniform hash map so that the
1003 * hidden uniforms will be given indicies at the end of the uniform storage
1007 assign_hidden_uniform_slot_id(const char *name
, unsigned hidden_id
,
1010 count_uniform_size
*uniform_size
= (count_uniform_size
*) closure
;
1011 unsigned hidden_uniform_start
= uniform_size
->num_active_uniforms
-
1012 uniform_size
->num_hidden_uniforms
;
1014 uniform_size
->map
->put(hidden_uniform_start
+ hidden_id
, name
);
1018 * Search through the list of empty blocks to find one that fits the current
1022 find_empty_block(struct gl_shader_program
*prog
,
1023 struct gl_uniform_storage
*uniform
)
1025 const unsigned entries
= MAX2(1, uniform
->array_elements
);
1027 foreach_list_typed(struct empty_uniform_block
, block
, link
,
1028 &prog
->EmptyUniformLocations
) {
1029 /* Found a block with enough slots to fit the uniform */
1030 if (block
->slots
== entries
) {
1031 unsigned start
= block
->start
;
1032 exec_node_remove(&block
->link
);
1036 /* Found a block with more slots than needed. It can still be used. */
1037 } else if (block
->slots
> entries
) {
1038 unsigned start
= block
->start
;
1039 block
->start
+= entries
;
1040 block
->slots
-= entries
;
1050 link_setup_uniform_remap_tables(struct gl_context
*ctx
,
1051 struct gl_shader_program
*prog
)
1053 unsigned total_entries
= prog
->NumExplicitUniformLocations
;
1054 unsigned empty_locs
= prog
->NumUniformRemapTable
- total_entries
;
1056 /* Reserve all the explicit locations of the active uniforms. */
1057 for (unsigned i
= 0; i
< prog
->data
->NumUniformStorage
; i
++) {
1058 if (prog
->data
->UniformStorage
[i
].type
->is_subroutine() ||
1059 prog
->data
->UniformStorage
[i
].is_shader_storage
)
1062 if (prog
->data
->UniformStorage
[i
].remap_location
!=
1063 UNMAPPED_UNIFORM_LOC
) {
1064 /* How many new entries for this uniform? */
1065 const unsigned entries
=
1066 MAX2(1, prog
->data
->UniformStorage
[i
].array_elements
);
1068 /* Set remap table entries point to correct gl_uniform_storage. */
1069 for (unsigned j
= 0; j
< entries
; j
++) {
1070 unsigned element_loc
=
1071 prog
->data
->UniformStorage
[i
].remap_location
+ j
;
1072 assert(prog
->UniformRemapTable
[element_loc
] ==
1073 INACTIVE_UNIFORM_EXPLICIT_LOCATION
);
1074 prog
->UniformRemapTable
[element_loc
] =
1075 &prog
->data
->UniformStorage
[i
];
1080 /* Reserve locations for rest of the uniforms. */
1081 for (unsigned i
= 0; i
< prog
->data
->NumUniformStorage
; i
++) {
1083 if (prog
->data
->UniformStorage
[i
].type
->is_subroutine() ||
1084 prog
->data
->UniformStorage
[i
].is_shader_storage
)
1087 /* Built-in uniforms should not get any location. */
1088 if (prog
->data
->UniformStorage
[i
].builtin
)
1091 /* Explicit ones have been set already. */
1092 if (prog
->data
->UniformStorage
[i
].remap_location
!= UNMAPPED_UNIFORM_LOC
)
1095 /* how many new entries for this uniform? */
1096 const unsigned entries
=
1097 MAX2(1, prog
->data
->UniformStorage
[i
].array_elements
);
1099 /* Find UniformRemapTable for empty blocks where we can fit this uniform. */
1100 int chosen_location
= -1;
1103 chosen_location
= find_empty_block(prog
, &prog
->data
->UniformStorage
[i
]);
1105 /* Add new entries to the total amount of entries. */
1106 total_entries
+= entries
;
1108 if (chosen_location
!= -1) {
1109 empty_locs
-= entries
;
1111 chosen_location
= prog
->NumUniformRemapTable
;
1113 /* resize remap table to fit new entries */
1114 prog
->UniformRemapTable
=
1116 prog
->UniformRemapTable
,
1117 gl_uniform_storage
*,
1118 prog
->NumUniformRemapTable
+ entries
);
1119 prog
->NumUniformRemapTable
+= entries
;
1122 /* set pointers for this uniform */
1123 for (unsigned j
= 0; j
< entries
; j
++)
1124 prog
->UniformRemapTable
[chosen_location
+ j
] =
1125 &prog
->data
->UniformStorage
[i
];
1127 /* set the base location in remap table for the uniform */
1128 prog
->data
->UniformStorage
[i
].remap_location
= chosen_location
;
1131 /* Verify that total amount of entries for explicit and implicit locations
1132 * is less than MAX_UNIFORM_LOCATIONS.
1135 if (total_entries
> ctx
->Const
.MaxUserAssignableUniformLocations
) {
1136 linker_error(prog
, "count of uniform locations > MAX_UNIFORM_LOCATIONS"
1137 "(%u > %u)", total_entries
,
1138 ctx
->Const
.MaxUserAssignableUniformLocations
);
1141 /* Reserve all the explicit locations of the active subroutine uniforms. */
1142 for (unsigned i
= 0; i
< prog
->data
->NumUniformStorage
; i
++) {
1143 if (!prog
->data
->UniformStorage
[i
].type
->is_subroutine())
1146 if (prog
->data
->UniformStorage
[i
].remap_location
== UNMAPPED_UNIFORM_LOC
)
1149 /* How many new entries for this uniform? */
1150 const unsigned entries
=
1151 MAX2(1, prog
->data
->UniformStorage
[i
].array_elements
);
1153 unsigned mask
= prog
->data
->linked_stages
;
1155 const int j
= u_bit_scan(&mask
);
1156 struct gl_program
*p
= prog
->_LinkedShaders
[j
]->Program
;
1158 if (!prog
->data
->UniformStorage
[i
].opaque
[j
].active
)
1161 /* Set remap table entries point to correct gl_uniform_storage. */
1162 for (unsigned k
= 0; k
< entries
; k
++) {
1163 unsigned element_loc
=
1164 prog
->data
->UniformStorage
[i
].remap_location
+ k
;
1165 assert(p
->sh
.SubroutineUniformRemapTable
[element_loc
] ==
1166 INACTIVE_UNIFORM_EXPLICIT_LOCATION
);
1167 p
->sh
.SubroutineUniformRemapTable
[element_loc
] =
1168 &prog
->data
->UniformStorage
[i
];
1173 /* reserve subroutine locations */
1174 for (unsigned i
= 0; i
< prog
->data
->NumUniformStorage
; i
++) {
1175 if (!prog
->data
->UniformStorage
[i
].type
->is_subroutine())
1178 if (prog
->data
->UniformStorage
[i
].remap_location
!=
1179 UNMAPPED_UNIFORM_LOC
)
1182 const unsigned entries
=
1183 MAX2(1, prog
->data
->UniformStorage
[i
].array_elements
);
1185 unsigned mask
= prog
->data
->linked_stages
;
1187 const int j
= u_bit_scan(&mask
);
1188 struct gl_program
*p
= prog
->_LinkedShaders
[j
]->Program
;
1190 if (!prog
->data
->UniformStorage
[i
].opaque
[j
].active
)
1193 p
->sh
.SubroutineUniformRemapTable
=
1195 p
->sh
.SubroutineUniformRemapTable
,
1196 gl_uniform_storage
*,
1197 p
->sh
.NumSubroutineUniformRemapTable
+ entries
);
1199 for (unsigned k
= 0; k
< entries
; k
++) {
1200 p
->sh
.SubroutineUniformRemapTable
[p
->sh
.NumSubroutineUniformRemapTable
+ k
] =
1201 &prog
->data
->UniformStorage
[i
];
1203 prog
->data
->UniformStorage
[i
].remap_location
=
1204 p
->sh
.NumSubroutineUniformRemapTable
;
1205 p
->sh
.NumSubroutineUniformRemapTable
+= entries
;
1211 link_assign_uniform_storage(struct gl_context
*ctx
,
1212 struct gl_shader_program
*prog
,
1213 const unsigned num_data_slots
)
1215 /* On the outside chance that there were no uniforms, bail out.
1217 if (prog
->data
->NumUniformStorage
== 0)
1220 unsigned int boolean_true
= ctx
->Const
.UniformBooleanTrue
;
1222 union gl_constant_value
*data
;
1223 if (prog
->data
->UniformStorage
== NULL
) {
1224 prog
->data
->UniformStorage
= rzalloc_array(prog
,
1225 struct gl_uniform_storage
,
1226 prog
->data
->NumUniformStorage
);
1227 data
= rzalloc_array(prog
->data
->UniformStorage
,
1228 union gl_constant_value
, num_data_slots
);
1230 data
= prog
->data
->UniformDataSlots
;
1234 union gl_constant_value
*data_end
= &data
[num_data_slots
];
1237 parcel_out_uniform_storage
parcel(prog
, prog
->UniformHash
,
1238 prog
->data
->UniformStorage
, data
);
1240 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1241 if (prog
->_LinkedShaders
[i
] == NULL
)
1244 parcel
.start_shader((gl_shader_stage
)i
);
1246 foreach_in_list(ir_instruction
, node
, prog
->_LinkedShaders
[i
]->ir
) {
1247 ir_variable
*const var
= node
->as_variable();
1249 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
&&
1250 var
->data
.mode
!= ir_var_shader_storage
))
1253 parcel
.set_and_process(var
);
1256 prog
->_LinkedShaders
[i
]->Program
->SamplersUsed
=
1257 parcel
.shader_samplers_used
;
1258 prog
->_LinkedShaders
[i
]->shadow_samplers
= parcel
.shader_shadow_samplers
;
1260 STATIC_ASSERT(sizeof(prog
->_LinkedShaders
[i
]->Program
->sh
.SamplerTargets
) ==
1261 sizeof(parcel
.targets
));
1262 memcpy(prog
->_LinkedShaders
[i
]->Program
->sh
.SamplerTargets
,
1264 sizeof(prog
->_LinkedShaders
[i
]->Program
->sh
.SamplerTargets
));
1267 /* If this is a fallback compile for a cache miss we already have the
1268 * correct uniform mappings and we don't want to reinitialise uniforms so
1271 if (prog
->data
->cache_fallback
)
1275 for (unsigned i
= 0; i
< prog
->data
->NumUniformStorage
; i
++) {
1276 assert(prog
->data
->UniformStorage
[i
].storage
!= NULL
||
1277 prog
->data
->UniformStorage
[i
].builtin
||
1278 prog
->data
->UniformStorage
[i
].is_shader_storage
||
1279 prog
->data
->UniformStorage
[i
].block_index
!= -1);
1282 assert(parcel
.values
== data_end
);
1285 link_setup_uniform_remap_tables(ctx
, prog
);
1287 /* Set shader cache fields */
1288 prog
->data
->NumUniformDataSlots
= num_data_slots
;
1289 prog
->data
->UniformDataSlots
= data
;
1291 link_set_uniform_initializers(prog
, boolean_true
);
1295 link_assign_uniform_locations(struct gl_shader_program
*prog
,
1296 struct gl_context
*ctx
)
1298 if (!prog
->data
->cache_fallback
) {
1299 ralloc_free(prog
->data
->UniformStorage
);
1300 prog
->data
->UniformStorage
= NULL
;
1301 prog
->data
->NumUniformStorage
= 0;
1304 if (prog
->UniformHash
!= NULL
) {
1305 prog
->UniformHash
->clear();
1307 prog
->UniformHash
= new string_to_uint_map
;
1310 /* First pass: Count the uniform resources used by the user-defined
1311 * uniforms. While this happens, each active uniform will have an index
1314 * Note: this is *NOT* the index that is returned to the application by
1315 * glGetUniformLocation.
1317 struct string_to_uint_map
*hiddenUniforms
= new string_to_uint_map
;
1318 count_uniform_size
uniform_size(prog
->UniformHash
, hiddenUniforms
);
1319 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1320 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
1325 link_update_uniform_buffer_variables(sh
, i
);
1327 /* Reset various per-shader target counts.
1329 uniform_size
.start_shader();
1331 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
1332 ir_variable
*const var
= node
->as_variable();
1334 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
&&
1335 var
->data
.mode
!= ir_var_shader_storage
))
1338 uniform_size
.process(var
);
1341 sh
->Program
->info
.num_textures
= uniform_size
.num_shader_samplers
;
1342 sh
->Program
->info
.num_images
= uniform_size
.num_shader_images
;
1343 sh
->num_uniform_components
= uniform_size
.num_shader_uniform_components
;
1344 sh
->num_combined_uniform_components
= sh
->num_uniform_components
;
1346 for (unsigned i
= 0; i
< sh
->Program
->info
.num_ubos
; i
++) {
1347 sh
->num_combined_uniform_components
+=
1348 sh
->Program
->sh
.UniformBlocks
[i
]->UniformBufferSize
/ 4;
1352 prog
->data
->NumUniformStorage
= uniform_size
.num_active_uniforms
;
1353 prog
->data
->NumHiddenUniforms
= uniform_size
.num_hidden_uniforms
;
1355 /* assign hidden uniforms a slot id */
1356 hiddenUniforms
->iterate(assign_hidden_uniform_slot_id
, &uniform_size
);
1357 delete hiddenUniforms
;
1359 link_assign_uniform_storage(ctx
, prog
, uniform_size
.num_values
);