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"
29 #include "program/hash_table.h"
31 #include "util/hash_table.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 * Count the backing storage requirements for a type
49 values_for_type(const glsl_type
*type
)
51 if (type
->is_sampler()) {
53 } else if (type
->is_array() && type
->fields
.array
->is_sampler()) {
54 return type
->array_size();
56 return type
->component_slots();
61 program_resource_visitor::process(const glsl_type
*type
, const char *name
)
63 assert(type
->without_array()->is_record()
64 || type
->without_array()->is_interface());
66 unsigned record_array_count
= 1;
67 char *name_copy
= ralloc_strdup(NULL
, name
);
68 unsigned packing
= type
->interface_packing
;
70 recursion(type
, &name_copy
, strlen(name
), false, NULL
, packing
, false,
72 ralloc_free(name_copy
);
76 program_resource_visitor::process(ir_variable
*var
)
78 unsigned record_array_count
= 1;
79 const glsl_type
*t
= var
->type
;
80 const glsl_type
*t_without_array
= var
->type
->without_array();
81 const bool row_major
=
82 var
->data
.matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
84 const unsigned packing
= var
->get_interface_type() ?
85 var
->get_interface_type()->interface_packing
:
86 var
->type
->interface_packing
;
88 /* false is always passed for the row_major parameter to the other
89 * processing functions because no information is available to do
90 * otherwise. See the warning in linker.h.
93 /* Only strdup the name if we actually will need to modify it. */
94 if (var
->data
.from_named_ifc_block_array
) {
95 /* lower_named_interface_blocks created this variable by lowering an
96 * interface block array to an array variable. For example if the
97 * original source code was:
99 * out Blk { vec4 bar } foo[3];
101 * Then the variable is now:
105 * We need to visit each array element using the names constructed like
112 assert(t
->is_array());
113 const glsl_type
*ifc_type
= var
->get_interface_type();
114 char *name
= ralloc_strdup(NULL
, ifc_type
->name
);
115 size_t name_length
= strlen(name
);
116 for (unsigned i
= 0; i
< t
->length
; i
++) {
117 size_t new_length
= name_length
;
118 ralloc_asprintf_rewrite_tail(&name
, &new_length
, "[%u].%s", i
,
120 /* Note: row_major is only meaningful for uniform blocks, and
121 * lowering is only applied to non-uniform interface blocks, so we
122 * can safely pass false for row_major.
124 recursion(var
->type
, &name
, new_length
, row_major
, NULL
, packing
,
125 false, record_array_count
);
128 } else if (var
->data
.from_named_ifc_block_nonarray
) {
129 /* lower_named_interface_blocks created this variable by lowering a
130 * named interface block (non-array) to an ordinary variable. For
131 * example if the original source code was:
133 * out Blk { vec4 bar } foo;
135 * Then the variable is now:
139 * We need to visit this variable using the name:
143 const glsl_type
*ifc_type
= var
->get_interface_type();
144 char *name
= ralloc_asprintf(NULL
, "%s.%s", ifc_type
->name
, var
->name
);
145 /* Note: row_major is only meaningful for uniform blocks, and lowering
146 * is only applied to non-uniform interface blocks, so we can safely
147 * pass false for row_major.
149 recursion(var
->type
, &name
, strlen(name
), row_major
, NULL
, packing
,
150 false, record_array_count
);
152 } else if (t_without_array
->is_record() ||
153 (t
->is_array() && t
->fields
.array
->is_array())) {
154 char *name
= ralloc_strdup(NULL
, var
->name
);
155 recursion(var
->type
, &name
, strlen(name
), row_major
, NULL
, packing
,
156 false, record_array_count
);
158 } else if (t_without_array
->is_interface()) {
159 char *name
= ralloc_strdup(NULL
, t_without_array
->name
);
160 recursion(var
->type
, &name
, strlen(name
), row_major
, NULL
, packing
,
161 false, record_array_count
);
164 this->set_record_array_count(record_array_count
);
165 this->visit_field(t
, var
->name
, row_major
, NULL
, packing
, false);
170 program_resource_visitor::recursion(const glsl_type
*t
, char **name
,
171 size_t name_length
, bool row_major
,
172 const glsl_type
*record_type
,
173 const unsigned packing
,
175 unsigned record_array_count
)
177 /* Records need to have each field processed individually.
179 * Arrays of records need to have each array element processed
180 * individually, then each field of the resulting array elements processed
183 if (t
->is_record() || t
->is_interface()) {
184 if (record_type
== NULL
&& t
->is_record())
188 this->enter_record(t
, *name
, row_major
, packing
);
190 for (unsigned i
= 0; i
< t
->length
; i
++) {
191 const char *field
= t
->fields
.structure
[i
].name
;
192 size_t new_length
= name_length
;
194 if (t
->fields
.structure
[i
].type
->is_record())
195 this->visit_field(&t
->fields
.structure
[i
]);
197 /* Append '.field' to the current variable name. */
198 if (name_length
== 0) {
199 ralloc_asprintf_rewrite_tail(name
, &new_length
, "%s", field
);
201 ralloc_asprintf_rewrite_tail(name
, &new_length
, ".%s", field
);
204 /* The layout of structures at the top level of the block is set
205 * during parsing. For matrices contained in multiple levels of
206 * structures in the block, the inner structures have no layout.
207 * These cases must potentially inherit the layout from the outer
210 bool field_row_major
= row_major
;
211 const enum glsl_matrix_layout matrix_layout
=
212 glsl_matrix_layout(t
->fields
.structure
[i
].matrix_layout
);
213 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
214 field_row_major
= true;
215 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
216 field_row_major
= false;
219 recursion(t
->fields
.structure
[i
].type
, name
, new_length
,
223 (i
+ 1) == t
->length
, record_array_count
);
225 /* Only the first leaf-field of the record gets called with the
226 * record type pointer.
231 if (t
->is_record()) {
232 (*name
)[name_length
] = '\0';
233 this->leave_record(t
, *name
, row_major
, packing
);
235 } else if (t
->without_array()->is_record() ||
236 t
->without_array()->is_interface() ||
237 (t
->is_array() && t
->fields
.array
->is_array())) {
238 if (record_type
== NULL
&& t
->fields
.array
->is_record())
239 record_type
= t
->fields
.array
;
241 unsigned length
= t
->length
;
242 /* Shader storage block unsized arrays: add subscript [0] to variable
244 if (t
->is_unsized_array())
247 record_array_count
*= length
;
249 for (unsigned i
= 0; i
< length
; i
++) {
250 size_t new_length
= name_length
;
252 /* Append the subscript to the current variable name */
253 ralloc_asprintf_rewrite_tail(name
, &new_length
, "[%u]", i
);
255 recursion(t
->fields
.array
, name
, new_length
, row_major
,
258 (i
+ 1) == t
->length
, record_array_count
);
260 /* Only the first leaf-field of the record gets called with the
261 * record type pointer.
266 this->set_record_array_count(record_array_count
);
267 this->visit_field(t
, *name
, row_major
, record_type
, packing
, last_field
);
272 program_resource_visitor::visit_field(const glsl_type
*type
, const char *name
,
276 bool /* last_field */)
278 visit_field(type
, name
, row_major
);
282 program_resource_visitor::visit_field(const glsl_struct_field
*field
)
289 program_resource_visitor::enter_record(const glsl_type
*, const char *, bool,
295 program_resource_visitor::leave_record(const glsl_type
*, const char *, bool,
301 program_resource_visitor::set_record_array_count(unsigned)
308 * Class to help calculate the storage requirements for a set of uniforms
310 * As uniforms are added to the active set the number of active uniforms and
311 * the storage requirements for those uniforms are accumulated. The active
312 * uniforms are added to the hash table supplied to the constructor.
314 * If the same uniform is added multiple times (i.e., once for each shader
315 * target), it will only be accounted once.
317 class count_uniform_size
: public program_resource_visitor
{
319 count_uniform_size(struct string_to_uint_map
*map
,
320 struct string_to_uint_map
*hidden_map
)
321 : num_active_uniforms(0), num_hidden_uniforms(0), num_values(0),
322 num_shader_samplers(0), num_shader_images(0),
323 num_shader_uniform_components(0), num_shader_subroutines(0),
324 is_ubo_var(false), is_shader_storage(false), map(map
),
325 hidden_map(hidden_map
)
332 this->num_shader_samplers
= 0;
333 this->num_shader_images
= 0;
334 this->num_shader_uniform_components
= 0;
335 this->num_shader_subroutines
= 0;
338 void process(ir_variable
*var
)
340 this->current_var
= var
;
341 this->is_ubo_var
= var
->is_in_buffer_block();
342 this->is_shader_storage
= var
->is_in_shader_storage_block();
343 if (var
->is_interface_instance())
344 program_resource_visitor::process(var
->get_interface_type(),
345 var
->get_interface_type()->name
);
347 program_resource_visitor::process(var
);
351 * Total number of active uniforms counted
353 unsigned num_active_uniforms
;
355 unsigned num_hidden_uniforms
;
358 * Number of data values required to back the storage for the active uniforms
363 * Number of samplers used
365 unsigned num_shader_samplers
;
368 * Number of images used
370 unsigned num_shader_images
;
373 * Number of uniforms used in the current shader
375 unsigned num_shader_uniform_components
;
378 * Number of subroutine uniforms used
380 unsigned num_shader_subroutines
;
383 bool is_shader_storage
;
385 struct string_to_uint_map
*map
;
388 virtual void visit_field(const glsl_type
*type
, const char *name
,
391 assert(!type
->without_array()->is_record());
392 assert(!type
->without_array()->is_interface());
393 assert(!(type
->is_array() && type
->fields
.array
->is_array()));
397 /* Count the number of samplers regardless of whether the uniform is
398 * already in the hash table. The hash table prevents adding the same
399 * uniform for multiple shader targets, but in this case we want to
400 * count it for each shader target.
402 const unsigned values
= values_for_type(type
);
403 if (type
->contains_subroutine()) {
404 this->num_shader_subroutines
+= values
;
405 } else if (type
->contains_sampler()) {
406 this->num_shader_samplers
+= values
;
407 } else if (type
->contains_image()) {
408 this->num_shader_images
+= values
;
410 /* As drivers are likely to represent image uniforms as
411 * scalar indices, count them against the limit of uniform
412 * components in the default block. The spec allows image
413 * uniforms to use up no more than one scalar slot.
415 if(!is_shader_storage
)
416 this->num_shader_uniform_components
+= values
;
418 /* Accumulate the total number of uniform slots used by this shader.
419 * Note that samplers do not count against this limit because they
420 * don't use any storage on current hardware.
422 if (!is_ubo_var
&& !is_shader_storage
)
423 this->num_shader_uniform_components
+= values
;
426 /* If the uniform is already in the map, there's nothing more to do.
429 if (this->map
->get(id
, name
))
432 if (this->current_var
->data
.how_declared
== ir_var_hidden
) {
433 this->hidden_map
->put(this->num_hidden_uniforms
, name
);
434 this->num_hidden_uniforms
++;
436 this->map
->put(this->num_active_uniforms
-this->num_hidden_uniforms
,
440 /* Each leaf uniform occupies one entry in the list of active
443 this->num_active_uniforms
++;
444 this->num_values
+= values
;
447 struct string_to_uint_map
*hidden_map
;
450 * Current variable being processed.
452 ir_variable
*current_var
;
455 } /* anonymous namespace */
458 * Class to help parcel out pieces of backing storage to uniforms
460 * Each uniform processed has some range of the \c gl_constant_value
461 * structures associated with it. The association is done by finding
462 * the uniform in the \c string_to_uint_map and using the value from
463 * the map to connect that slot in the \c gl_uniform_storage table
464 * with the next available slot in the \c gl_constant_value array.
467 * This class assumes that every uniform that will be processed is
468 * already in the \c string_to_uint_map. In addition, it assumes that
469 * the \c gl_uniform_storage and \c gl_constant_value arrays are "big
472 class parcel_out_uniform_storage
: public program_resource_visitor
{
474 parcel_out_uniform_storage(struct string_to_uint_map
*map
,
475 struct gl_uniform_storage
*uniforms
,
476 union gl_constant_value
*values
)
477 : map(map
), uniforms(uniforms
), values(values
)
481 void start_shader(gl_shader_stage shader_type
)
483 assert(shader_type
< MESA_SHADER_STAGES
);
484 this->shader_type
= shader_type
;
486 this->shader_samplers_used
= 0;
487 this->shader_shadow_samplers
= 0;
488 this->next_sampler
= 0;
489 this->next_image
= 0;
490 this->next_subroutine
= 0;
491 this->record_array_count
= 1;
492 memset(this->targets
, 0, sizeof(this->targets
));
495 void set_and_process(struct gl_shader_program
*prog
,
500 this->record_next_sampler
= new string_to_uint_map
;
502 ubo_block_index
= -1;
503 if (var
->is_in_buffer_block()) {
504 if (var
->is_interface_instance() && var
->type
->is_array()) {
505 unsigned l
= strlen(var
->get_interface_type()->name
);
507 for (unsigned i
= 0; i
< prog
->NumBufferInterfaceBlocks
; i
++) {
508 if (strncmp(var
->get_interface_type()->name
,
509 prog
->BufferInterfaceBlocks
[i
].Name
,
511 && prog
->BufferInterfaceBlocks
[i
].Name
[l
] == '[') {
517 for (unsigned i
= 0; i
< prog
->NumBufferInterfaceBlocks
; i
++) {
518 if (strcmp(var
->get_interface_type()->name
,
519 prog
->BufferInterfaceBlocks
[i
].Name
) == 0) {
525 assert(ubo_block_index
!= -1);
527 /* Uniform blocks that were specified with an instance name must be
528 * handled a little bit differently. The name of the variable is the
529 * name used to reference the uniform block instead of being the name
530 * of a variable within the block. Therefore, searching for the name
531 * within the block will fail.
533 if (var
->is_interface_instance()) {
536 const struct gl_uniform_block
*const block
=
537 &prog
->BufferInterfaceBlocks
[ubo_block_index
];
539 assert(var
->data
.location
!= -1);
541 const struct gl_uniform_buffer_variable
*const ubo_var
=
542 &block
->Uniforms
[var
->data
.location
];
544 ubo_byte_offset
= ubo_var
->Offset
;
547 if (var
->is_interface_instance())
548 process(var
->get_interface_type(),
549 var
->get_interface_type()->name
);
553 /* Store any explicit location and reset data location so we can
554 * reuse this variable for storing the uniform slot number.
556 this->explicit_location
= current_var
->data
.location
;
557 current_var
->data
.location
= -1;
561 delete this->record_next_sampler
;
566 gl_shader_stage shader_type
;
569 void handle_samplers(const glsl_type
*base_type
,
570 struct gl_uniform_storage
*uniform
, const char *name
)
572 if (base_type
->is_sampler()) {
573 uniform
->opaque
[shader_type
].active
= true;
575 /* Handle multiple samplers inside struct arrays */
576 if (this->record_array_count
> 1) {
577 unsigned inner_array_size
= MAX2(1, uniform
->array_elements
);
578 char *name_copy
= ralloc_strdup(NULL
, name
);
580 /* Remove all array subscripts from the sampler name */
583 while((str_start
= strchr(name_copy
, '[')) &&
584 (str_end
= strchr(name_copy
, ']'))) {
585 memmove(str_start
, str_end
+ 1, 1 + strlen(str_end
));
589 if (this->record_next_sampler
->get(index
, name_copy
)) {
590 /* In this case, we've already seen this uniform so we just use
591 * the next sampler index recorded the last time we visited.
593 uniform
->opaque
[shader_type
].index
= index
;
594 index
= inner_array_size
+ uniform
->opaque
[shader_type
].index
;
595 this->record_next_sampler
->put(index
, name_copy
);
597 ralloc_free(name_copy
);
598 /* Return as everything else has already been initialised in a
603 /* We've never seen this uniform before so we need to allocate
604 * enough indices to store it.
606 * Nested struct arrays behave like arrays of arrays so we need
607 * to increase the index by the total number of elements of the
608 * sampler in case there is more than one sampler inside the
609 * structs. This allows the offset to be easily calculated for
612 uniform
->opaque
[shader_type
].index
= this->next_sampler
;
613 this->next_sampler
+=
614 inner_array_size
* this->record_array_count
;
616 /* Store the next index for future passes over the struct array
618 index
= uniform
->opaque
[shader_type
].index
+ inner_array_size
;
619 this->record_next_sampler
->put(index
, name_copy
);
620 ralloc_free(name_copy
);
623 /* Increment the sampler by 1 for non-arrays and by the number of
624 * array elements for arrays.
626 uniform
->opaque
[shader_type
].index
= this->next_sampler
;
627 this->next_sampler
+= MAX2(1, uniform
->array_elements
);
630 const gl_texture_index target
= base_type
->sampler_index();
631 const unsigned shadow
= base_type
->sampler_shadow
;
632 for (unsigned i
= uniform
->opaque
[shader_type
].index
;
633 i
< MIN2(this->next_sampler
, MAX_SAMPLERS
);
635 this->targets
[i
] = target
;
636 this->shader_samplers_used
|= 1U << i
;
637 this->shader_shadow_samplers
|= shadow
<< i
;
642 void handle_images(const glsl_type
*base_type
,
643 struct gl_uniform_storage
*uniform
)
645 if (base_type
->is_image()) {
646 uniform
->opaque
[shader_type
].index
= this->next_image
;
647 uniform
->opaque
[shader_type
].active
= true;
649 /* Increment the image index by 1 for non-arrays and by the
650 * number of array elements for arrays.
652 this->next_image
+= MAX2(1, uniform
->array_elements
);
657 void handle_subroutines(const glsl_type
*base_type
,
658 struct gl_uniform_storage
*uniform
)
660 if (base_type
->is_subroutine()) {
661 uniform
->opaque
[shader_type
].index
= this->next_subroutine
;
662 uniform
->opaque
[shader_type
].active
= true;
664 /* Increment the subroutine index by 1 for non-arrays and by the
665 * number of array elements for arrays.
667 this->next_subroutine
+= MAX2(1, uniform
->array_elements
);
672 virtual void set_record_array_count(unsigned record_array_count
)
674 this->record_array_count
= record_array_count
;
677 virtual void visit_field(const glsl_type
*type
, const char *name
,
683 assert(!"Should not get here.");
686 virtual void enter_record(const glsl_type
*type
, const char *,
687 bool row_major
, const unsigned packing
) {
688 assert(type
->is_record());
689 if (this->ubo_block_index
== -1)
691 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
692 this->ubo_byte_offset
= glsl_align(
693 this->ubo_byte_offset
, type
->std430_base_alignment(row_major
));
695 this->ubo_byte_offset
= glsl_align(
696 this->ubo_byte_offset
, type
->std140_base_alignment(row_major
));
699 virtual void leave_record(const glsl_type
*type
, const char *,
700 bool row_major
, const unsigned packing
) {
701 assert(type
->is_record());
702 if (this->ubo_block_index
== -1)
704 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
705 this->ubo_byte_offset
= glsl_align(
706 this->ubo_byte_offset
, type
->std430_base_alignment(row_major
));
708 this->ubo_byte_offset
= glsl_align(
709 this->ubo_byte_offset
, type
->std140_base_alignment(row_major
));
712 virtual void visit_field(const glsl_type
*type
, const char *name
,
713 bool row_major
, const glsl_type
*record_type
,
714 const unsigned packing
,
715 bool /* last_field */)
717 assert(!type
->without_array()->is_record());
718 assert(!type
->without_array()->is_interface());
719 assert(!(type
->is_array() && type
->fields
.array
->is_array()));
722 bool found
= this->map
->get(id
, name
);
728 const glsl_type
*base_type
;
729 if (type
->is_array()) {
730 this->uniforms
[id
].array_elements
= type
->length
;
731 base_type
= type
->fields
.array
;
733 this->uniforms
[id
].array_elements
= 0;
737 /* Initialise opaque data */
738 this->uniforms
[id
].opaque
[shader_type
].index
= ~0;
739 this->uniforms
[id
].opaque
[shader_type
].active
= false;
741 /* This assigns uniform indices to sampler and image uniforms. */
742 handle_samplers(base_type
, &this->uniforms
[id
], name
);
743 handle_images(base_type
, &this->uniforms
[id
]);
744 handle_subroutines(base_type
, &this->uniforms
[id
]);
746 /* For array of arrays or struct arrays the base location may have
747 * already been set so don't set it again.
749 if (ubo_block_index
== -1 && current_var
->data
.location
== -1) {
750 current_var
->data
.location
= id
;
753 /* If there is already storage associated with this uniform or if the
754 * uniform is set as builtin, it means that it was set while processing
755 * an earlier shader stage. For example, we may be processing the
756 * uniform in the fragment shader, but the uniform was already processed
757 * in the vertex shader.
759 if (this->uniforms
[id
].storage
!= NULL
|| this->uniforms
[id
].builtin
) {
763 /* Assign explicit locations. */
764 if (current_var
->data
.explicit_location
) {
765 /* Set sequential locations for struct fields. */
766 if (current_var
->type
->without_array()->is_record() ||
767 current_var
->type
->is_array_of_arrays()) {
768 const unsigned entries
= MAX2(1, this->uniforms
[id
].array_elements
);
769 this->uniforms
[id
].remap_location
=
770 this->explicit_location
+ field_counter
;
771 field_counter
+= entries
;
773 this->uniforms
[id
].remap_location
= this->explicit_location
;
776 /* Initialize to to indicate that no location is set */
777 this->uniforms
[id
].remap_location
= UNMAPPED_UNIFORM_LOC
;
780 this->uniforms
[id
].name
= ralloc_strdup(this->uniforms
, name
);
781 this->uniforms
[id
].type
= base_type
;
782 this->uniforms
[id
].initialized
= 0;
783 this->uniforms
[id
].num_driver_storage
= 0;
784 this->uniforms
[id
].driver_storage
= NULL
;
785 this->uniforms
[id
].atomic_buffer_index
= -1;
786 this->uniforms
[id
].hidden
=
787 current_var
->data
.how_declared
== ir_var_hidden
;
788 this->uniforms
[id
].builtin
= is_gl_identifier(name
);
790 /* Do not assign storage if the uniform is builtin */
791 if (!this->uniforms
[id
].builtin
)
792 this->uniforms
[id
].storage
= this->values
;
794 this->uniforms
[id
].is_shader_storage
=
795 current_var
->is_in_shader_storage_block();
797 if (this->ubo_block_index
!= -1) {
798 this->uniforms
[id
].block_index
= this->ubo_block_index
;
800 unsigned alignment
= type
->std140_base_alignment(row_major
);
801 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
802 alignment
= type
->std430_base_alignment(row_major
);
803 this->ubo_byte_offset
= glsl_align(this->ubo_byte_offset
, alignment
);
804 this->uniforms
[id
].offset
= this->ubo_byte_offset
;
805 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
806 this->ubo_byte_offset
+= type
->std430_size(row_major
);
808 this->ubo_byte_offset
+= type
->std140_size(row_major
);
810 if (type
->is_array()) {
811 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
812 this->uniforms
[id
].array_stride
=
813 type
->without_array()->std430_array_stride(row_major
);
815 this->uniforms
[id
].array_stride
=
816 glsl_align(type
->without_array()->std140_size(row_major
),
819 this->uniforms
[id
].array_stride
= 0;
822 if (type
->without_array()->is_matrix()) {
823 const glsl_type
*matrix
= type
->without_array();
824 const unsigned N
= matrix
->base_type
== GLSL_TYPE_DOUBLE
? 8 : 4;
825 const unsigned items
=
826 row_major
? matrix
->matrix_columns
: matrix
->vector_elements
;
829 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
830 this->uniforms
[id
].matrix_stride
= items
< 3 ? items
* N
:
831 glsl_align(items
* N
, 16);
833 this->uniforms
[id
].matrix_stride
= glsl_align(items
* N
, 16);
834 this->uniforms
[id
].row_major
= row_major
;
836 this->uniforms
[id
].matrix_stride
= 0;
837 this->uniforms
[id
].row_major
= false;
840 this->uniforms
[id
].block_index
= -1;
841 this->uniforms
[id
].offset
= -1;
842 this->uniforms
[id
].array_stride
= -1;
843 this->uniforms
[id
].matrix_stride
= -1;
844 this->uniforms
[id
].row_major
= false;
847 this->values
+= values_for_type(type
);
850 struct string_to_uint_map
*map
;
852 struct gl_uniform_storage
*uniforms
;
853 unsigned next_sampler
;
855 unsigned next_subroutine
;
858 * Field counter is used to take care that uniform structures
859 * with explicit locations get sequential locations.
861 unsigned field_counter
;
864 * Current variable being processed.
866 ir_variable
*current_var
;
868 /* Used to store the explicit location from current_var so that we can
869 * reuse the location field for storing the uniform slot id.
871 int explicit_location
;
873 /* Stores total struct array elements including nested structs */
874 unsigned record_array_count
;
876 /* Map for temporarily storing next sampler index when handling samplers in
879 struct string_to_uint_map
*record_next_sampler
;
882 union gl_constant_value
*values
;
884 gl_texture_index targets
[MAX_SAMPLERS
];
887 * Mask of samplers used by the current shader stage.
889 unsigned shader_samplers_used
;
892 * Mask of samplers used by the current shader stage for shadows.
894 unsigned shader_shadow_samplers
;
898 * Merges a uniform block into an array of uniform blocks that may or
899 * may not already contain a copy of it.
901 * Returns the index of the new block in the array.
904 link_cross_validate_uniform_block(void *mem_ctx
,
905 struct gl_uniform_block
**linked_blocks
,
906 unsigned int *num_linked_blocks
,
907 struct gl_uniform_block
*new_block
)
909 for (unsigned int i
= 0; i
< *num_linked_blocks
; i
++) {
910 struct gl_uniform_block
*old_block
= &(*linked_blocks
)[i
];
912 if (strcmp(old_block
->Name
, new_block
->Name
) == 0)
913 return link_uniform_blocks_are_compatible(old_block
, new_block
)
917 *linked_blocks
= reralloc(mem_ctx
, *linked_blocks
,
918 struct gl_uniform_block
,
919 *num_linked_blocks
+ 1);
920 int linked_block_index
= (*num_linked_blocks
)++;
921 struct gl_uniform_block
*linked_block
= &(*linked_blocks
)[linked_block_index
];
923 memcpy(linked_block
, new_block
, sizeof(*new_block
));
924 linked_block
->Uniforms
= ralloc_array(*linked_blocks
,
925 struct gl_uniform_buffer_variable
,
926 linked_block
->NumUniforms
);
928 memcpy(linked_block
->Uniforms
,
930 sizeof(*linked_block
->Uniforms
) * linked_block
->NumUniforms
);
932 for (unsigned int i
= 0; i
< linked_block
->NumUniforms
; i
++) {
933 struct gl_uniform_buffer_variable
*ubo_var
=
934 &linked_block
->Uniforms
[i
];
936 if (ubo_var
->Name
== ubo_var
->IndexName
) {
937 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
938 ubo_var
->IndexName
= ubo_var
->Name
;
940 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
941 ubo_var
->IndexName
= ralloc_strdup(*linked_blocks
, ubo_var
->IndexName
);
945 return linked_block_index
;
949 * Walks the IR and update the references to uniform blocks in the
950 * ir_variables to point at linked shader's list (previously, they
951 * would point at the uniform block list in one of the pre-linked
955 link_update_uniform_buffer_variables(struct gl_shader
*shader
)
957 foreach_in_list(ir_instruction
, node
, shader
->ir
) {
958 ir_variable
*const var
= node
->as_variable();
960 if ((var
== NULL
) || !var
->is_in_buffer_block())
963 assert(var
->data
.mode
== ir_var_uniform
||
964 var
->data
.mode
== ir_var_shader_storage
);
966 if (var
->is_interface_instance()) {
967 var
->data
.location
= 0;
972 char sentinel
= '\0';
974 if (var
->type
->is_record()) {
976 } else if (var
->type
->is_array() && (var
->type
->fields
.array
->is_array()
977 || var
->type
->without_array()->is_record())) {
981 const unsigned l
= strlen(var
->name
);
982 for (unsigned i
= 0; i
< shader
->NumBufferInterfaceBlocks
; i
++) {
983 for (unsigned j
= 0; j
< shader
->BufferInterfaceBlocks
[i
].NumUniforms
; j
++) {
985 const char *begin
= shader
->BufferInterfaceBlocks
[i
].Uniforms
[j
].Name
;
986 const char *end
= strchr(begin
, sentinel
);
991 if ((ptrdiff_t) l
!= (end
- begin
))
994 if (strncmp(var
->name
, begin
, l
) == 0) {
996 var
->data
.location
= j
;
999 } else if (!strcmp(var
->name
,
1000 shader
->BufferInterfaceBlocks
[i
].Uniforms
[j
].Name
)) {
1002 var
->data
.location
= j
;
1014 * Scan the program for image uniforms and store image unit access
1015 * information into the gl_shader data structure.
1018 link_set_image_access_qualifiers(struct gl_shader_program
*prog
)
1020 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1021 gl_shader
*sh
= prog
->_LinkedShaders
[i
];
1026 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
1027 ir_variable
*var
= node
->as_variable();
1029 if (var
&& var
->data
.mode
== ir_var_uniform
&&
1030 var
->type
->contains_image()) {
1032 bool found
= prog
->UniformHash
->get(id
, var
->name
);
1035 const gl_uniform_storage
*storage
= &prog
->UniformStorage
[id
];
1036 const unsigned index
= storage
->opaque
[i
].index
;
1037 const GLenum access
= (var
->data
.image_read_only
? GL_READ_ONLY
:
1038 var
->data
.image_write_only
? GL_WRITE_ONLY
:
1041 for (unsigned j
= 0; j
< MAX2(1, storage
->array_elements
); ++j
)
1042 sh
->ImageAccess
[index
+ j
] = access
;
1049 * Combine the hidden uniform hash map with the uniform hash map so that the
1050 * hidden uniforms will be given indicies at the end of the uniform storage
1054 assign_hidden_uniform_slot_id(const char *name
, unsigned hidden_id
,
1057 count_uniform_size
*uniform_size
= (count_uniform_size
*) closure
;
1058 unsigned hidden_uniform_start
= uniform_size
->num_active_uniforms
-
1059 uniform_size
->num_hidden_uniforms
;
1061 uniform_size
->map
->put(hidden_uniform_start
+ hidden_id
, name
);
1065 link_assign_uniform_locations(struct gl_shader_program
*prog
,
1066 unsigned int boolean_true
)
1068 ralloc_free(prog
->UniformStorage
);
1069 prog
->UniformStorage
= NULL
;
1070 prog
->NumUniformStorage
= 0;
1072 if (prog
->UniformHash
!= NULL
) {
1073 prog
->UniformHash
->clear();
1075 prog
->UniformHash
= new string_to_uint_map
;
1078 /* First pass: Count the uniform resources used by the user-defined
1079 * uniforms. While this happens, each active uniform will have an index
1082 * Note: this is *NOT* the index that is returned to the application by
1083 * glGetUniformLocation.
1085 struct string_to_uint_map
*hiddenUniforms
= new string_to_uint_map
;
1086 count_uniform_size
uniform_size(prog
->UniformHash
, hiddenUniforms
);
1087 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1088 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
1093 /* Uniforms that lack an initializer in the shader code have an initial
1094 * value of zero. This includes sampler uniforms.
1096 * Page 24 (page 30 of the PDF) of the GLSL 1.20 spec says:
1098 * "The link time initial value is either the value of the variable's
1099 * initializer, if present, or 0 if no initializer is present. Sampler
1100 * types cannot have initializers."
1102 memset(sh
->SamplerUnits
, 0, sizeof(sh
->SamplerUnits
));
1103 memset(sh
->ImageUnits
, 0, sizeof(sh
->ImageUnits
));
1105 link_update_uniform_buffer_variables(sh
);
1107 /* Reset various per-shader target counts.
1109 uniform_size
.start_shader();
1111 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
1112 ir_variable
*const var
= node
->as_variable();
1114 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
&&
1115 var
->data
.mode
!= ir_var_shader_storage
))
1118 uniform_size
.process(var
);
1121 sh
->num_samplers
= uniform_size
.num_shader_samplers
;
1122 sh
->NumImages
= uniform_size
.num_shader_images
;
1123 sh
->num_uniform_components
= uniform_size
.num_shader_uniform_components
;
1124 sh
->num_combined_uniform_components
= sh
->num_uniform_components
;
1126 for (unsigned i
= 0; i
< sh
->NumBufferInterfaceBlocks
; i
++) {
1127 if (!sh
->BufferInterfaceBlocks
[i
].IsShaderStorage
) {
1128 sh
->num_combined_uniform_components
+=
1129 sh
->BufferInterfaceBlocks
[i
].UniformBufferSize
/ 4;
1134 const unsigned num_uniforms
= uniform_size
.num_active_uniforms
;
1135 const unsigned num_data_slots
= uniform_size
.num_values
;
1136 const unsigned hidden_uniforms
= uniform_size
.num_hidden_uniforms
;
1138 /* assign hidden uniforms a slot id */
1139 hiddenUniforms
->iterate(assign_hidden_uniform_slot_id
, &uniform_size
);
1140 delete hiddenUniforms
;
1142 /* On the outside chance that there were no uniforms, bail out.
1144 if (num_uniforms
== 0)
1147 struct gl_uniform_storage
*uniforms
=
1148 rzalloc_array(prog
, struct gl_uniform_storage
, num_uniforms
);
1149 union gl_constant_value
*data
=
1150 rzalloc_array(uniforms
, union gl_constant_value
, num_data_slots
);
1152 union gl_constant_value
*data_end
= &data
[num_data_slots
];
1155 parcel_out_uniform_storage
parcel(prog
->UniformHash
, uniforms
, data
);
1157 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1158 if (prog
->_LinkedShaders
[i
] == NULL
)
1161 parcel
.start_shader((gl_shader_stage
)i
);
1163 foreach_in_list(ir_instruction
, node
, prog
->_LinkedShaders
[i
]->ir
) {
1164 ir_variable
*const var
= node
->as_variable();
1166 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
&&
1167 var
->data
.mode
!= ir_var_shader_storage
))
1170 parcel
.set_and_process(prog
, var
);
1173 prog
->_LinkedShaders
[i
]->active_samplers
= parcel
.shader_samplers_used
;
1174 prog
->_LinkedShaders
[i
]->shadow_samplers
= parcel
.shader_shadow_samplers
;
1176 STATIC_ASSERT(sizeof(prog
->_LinkedShaders
[i
]->SamplerTargets
) ==
1177 sizeof(parcel
.targets
));
1178 memcpy(prog
->_LinkedShaders
[i
]->SamplerTargets
, parcel
.targets
,
1179 sizeof(prog
->_LinkedShaders
[i
]->SamplerTargets
));
1182 /* Reserve all the explicit locations of the active uniforms. */
1183 for (unsigned i
= 0; i
< num_uniforms
; i
++) {
1184 if (uniforms
[i
].type
->is_subroutine() ||
1185 uniforms
[i
].is_shader_storage
)
1188 if (uniforms
[i
].remap_location
!= UNMAPPED_UNIFORM_LOC
) {
1189 /* How many new entries for this uniform? */
1190 const unsigned entries
= MAX2(1, uniforms
[i
].array_elements
);
1192 /* Set remap table entries point to correct gl_uniform_storage. */
1193 for (unsigned j
= 0; j
< entries
; j
++) {
1194 unsigned element_loc
= uniforms
[i
].remap_location
+ j
;
1195 assert(prog
->UniformRemapTable
[element_loc
] ==
1196 INACTIVE_UNIFORM_EXPLICIT_LOCATION
);
1197 prog
->UniformRemapTable
[element_loc
] = &uniforms
[i
];
1202 /* Reserve locations for rest of the uniforms. */
1203 for (unsigned i
= 0; i
< num_uniforms
; i
++) {
1205 if (uniforms
[i
].type
->is_subroutine() ||
1206 uniforms
[i
].is_shader_storage
)
1209 /* Built-in uniforms should not get any location. */
1210 if (uniforms
[i
].builtin
)
1213 /* Explicit ones have been set already. */
1214 if (uniforms
[i
].remap_location
!= UNMAPPED_UNIFORM_LOC
)
1217 /* how many new entries for this uniform? */
1218 const unsigned entries
= MAX2(1, uniforms
[i
].array_elements
);
1220 /* resize remap table to fit new entries */
1221 prog
->UniformRemapTable
=
1223 prog
->UniformRemapTable
,
1224 gl_uniform_storage
*,
1225 prog
->NumUniformRemapTable
+ entries
);
1227 /* set pointers for this uniform */
1228 for (unsigned j
= 0; j
< entries
; j
++)
1229 prog
->UniformRemapTable
[prog
->NumUniformRemapTable
+j
] = &uniforms
[i
];
1231 /* set the base location in remap table for the uniform */
1232 uniforms
[i
].remap_location
= prog
->NumUniformRemapTable
;
1234 prog
->NumUniformRemapTable
+= entries
;
1237 /* Reserve all the explicit locations of the active subroutine uniforms. */
1238 for (unsigned i
= 0; i
< num_uniforms
; i
++) {
1239 if (!uniforms
[i
].type
->is_subroutine())
1242 if (uniforms
[i
].remap_location
== UNMAPPED_UNIFORM_LOC
)
1245 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; j
++) {
1246 struct gl_shader
*sh
= prog
->_LinkedShaders
[j
];
1250 if (!uniforms
[i
].opaque
[j
].active
)
1253 /* How many new entries for this uniform? */
1254 const unsigned entries
= MAX2(1, uniforms
[i
].array_elements
);
1256 /* Set remap table entries point to correct gl_uniform_storage. */
1257 for (unsigned k
= 0; k
< entries
; k
++) {
1258 unsigned element_loc
= uniforms
[i
].remap_location
+ k
;
1259 assert(sh
->SubroutineUniformRemapTable
[element_loc
] ==
1260 INACTIVE_UNIFORM_EXPLICIT_LOCATION
);
1261 sh
->SubroutineUniformRemapTable
[element_loc
] = &uniforms
[i
];
1266 /* reserve subroutine locations */
1267 for (unsigned i
= 0; i
< num_uniforms
; i
++) {
1269 if (!uniforms
[i
].type
->is_subroutine())
1271 const unsigned entries
= MAX2(1, uniforms
[i
].array_elements
);
1273 if (uniforms
[i
].remap_location
!= UNMAPPED_UNIFORM_LOC
)
1275 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; j
++) {
1276 struct gl_shader
*sh
= prog
->_LinkedShaders
[j
];
1280 if (!uniforms
[i
].opaque
[j
].active
)
1283 sh
->SubroutineUniformRemapTable
=
1285 sh
->SubroutineUniformRemapTable
,
1286 gl_uniform_storage
*,
1287 sh
->NumSubroutineUniformRemapTable
+ entries
);
1289 for (unsigned k
= 0; k
< entries
; k
++)
1290 sh
->SubroutineUniformRemapTable
[sh
->NumSubroutineUniformRemapTable
+ k
] = &uniforms
[i
];
1291 uniforms
[i
].remap_location
= sh
->NumSubroutineUniformRemapTable
;
1292 sh
->NumSubroutineUniformRemapTable
+= entries
;
1297 for (unsigned i
= 0; i
< num_uniforms
; i
++) {
1298 assert(uniforms
[i
].storage
!= NULL
|| uniforms
[i
].builtin
);
1301 assert(parcel
.values
== data_end
);
1304 prog
->NumUniformStorage
= num_uniforms
;
1305 prog
->NumHiddenUniforms
= hidden_uniforms
;
1306 prog
->UniformStorage
= uniforms
;
1308 link_set_image_access_qualifiers(prog
);
1309 link_set_uniform_initializers(prog
, boolean_true
);