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"
33 * \file link_uniforms.cpp
34 * Assign locations for GLSL uniforms.
36 * \author Ian Romanick <ian.d.romanick@intel.com>
40 * Used by linker to indicate uniforms that have no location set.
42 #define UNMAPPED_UNIFORM_LOC ~0u
45 * Count the backing storage requirements for a type
48 values_for_type(const glsl_type
*type
)
50 if (type
->is_sampler() || type
->is_subroutine()) {
52 } else if (type
->is_array() && (type
->fields
.array
->is_sampler() ||
53 type
->fields
.array
->is_subroutine())) {
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 char *name_copy
= ralloc_strdup(NULL
, name
);
67 recursion(type
, &name_copy
, strlen(name
), false, NULL
, false);
68 ralloc_free(name_copy
);
72 program_resource_visitor::process(ir_variable
*var
)
74 const glsl_type
*t
= var
->type
;
75 const bool row_major
=
76 var
->data
.matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
78 /* false is always passed for the row_major parameter to the other
79 * processing functions because no information is available to do
80 * otherwise. See the warning in linker.h.
83 /* Only strdup the name if we actually will need to modify it. */
84 if (var
->data
.from_named_ifc_block_array
) {
85 /* lower_named_interface_blocks created this variable by lowering an
86 * interface block array to an array variable. For example if the
87 * original source code was:
89 * out Blk { vec4 bar } foo[3];
91 * Then the variable is now:
95 * We need to visit each array element using the names constructed like
102 assert(t
->is_array());
103 const glsl_type
*ifc_type
= var
->get_interface_type();
104 char *name
= ralloc_strdup(NULL
, ifc_type
->name
);
105 size_t name_length
= strlen(name
);
106 for (unsigned i
= 0; i
< t
->length
; i
++) {
107 size_t new_length
= name_length
;
108 ralloc_asprintf_rewrite_tail(&name
, &new_length
, "[%u].%s", i
,
110 /* Note: row_major is only meaningful for uniform blocks, and
111 * lowering is only applied to non-uniform interface blocks, so we
112 * can safely pass false for row_major.
114 recursion(var
->type
, &name
, new_length
, row_major
, NULL
, false);
117 } else if (var
->data
.from_named_ifc_block_nonarray
) {
118 /* lower_named_interface_blocks created this variable by lowering a
119 * named interface block (non-array) to an ordinary variable. For
120 * example if the original source code was:
122 * out Blk { vec4 bar } foo;
124 * Then the variable is now:
128 * We need to visit this variable using the name:
132 const glsl_type
*ifc_type
= var
->get_interface_type();
133 char *name
= ralloc_asprintf(NULL
, "%s.%s", ifc_type
->name
, var
->name
);
134 /* Note: row_major is only meaningful for uniform blocks, and lowering
135 * is only applied to non-uniform interface blocks, so we can safely
136 * pass false for row_major.
138 recursion(var
->type
, &name
, strlen(name
), row_major
, NULL
, false);
140 } else if (t
->without_array()->is_record()) {
141 char *name
= ralloc_strdup(NULL
, var
->name
);
142 recursion(var
->type
, &name
, strlen(name
), row_major
, NULL
, false);
144 } else if (t
->is_interface()) {
145 char *name
= ralloc_strdup(NULL
, var
->type
->name
);
146 recursion(var
->type
, &name
, strlen(name
), row_major
, NULL
, false);
148 } else if (t
->is_array() && t
->fields
.array
->is_interface()) {
149 char *name
= ralloc_strdup(NULL
, var
->type
->fields
.array
->name
);
150 recursion(var
->type
, &name
, strlen(name
), row_major
, NULL
, false);
153 this->visit_field(t
, var
->name
, row_major
, NULL
, false);
158 program_resource_visitor::recursion(const glsl_type
*t
, char **name
,
159 size_t name_length
, bool row_major
,
160 const glsl_type
*record_type
,
163 /* Records need to have each field processed individually.
165 * Arrays of records need to have each array element processed
166 * individually, then each field of the resulting array elements processed
169 if (t
->is_record() || t
->is_interface()) {
170 if (record_type
== NULL
&& t
->is_record())
174 this->enter_record(t
, *name
, row_major
);
176 for (unsigned i
= 0; i
< t
->length
; i
++) {
177 const char *field
= t
->fields
.structure
[i
].name
;
178 size_t new_length
= name_length
;
180 if (t
->fields
.structure
[i
].type
->is_record())
181 this->visit_field(&t
->fields
.structure
[i
]);
183 /* Append '.field' to the current variable name. */
184 if (name_length
== 0) {
185 ralloc_asprintf_rewrite_tail(name
, &new_length
, "%s", field
);
187 ralloc_asprintf_rewrite_tail(name
, &new_length
, ".%s", field
);
190 /* The layout of structures at the top level of the block is set
191 * during parsing. For matrices contained in multiple levels of
192 * structures in the block, the inner structures have no layout.
193 * These cases must potentially inherit the layout from the outer
196 bool field_row_major
= row_major
;
197 const enum glsl_matrix_layout matrix_layout
=
198 glsl_matrix_layout(t
->fields
.structure
[i
].matrix_layout
);
199 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
200 field_row_major
= true;
201 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
202 field_row_major
= false;
205 recursion(t
->fields
.structure
[i
].type
, name
, new_length
,
208 (i
+ 1) == t
->length
);
210 /* Only the first leaf-field of the record gets called with the
211 * record type pointer.
216 if (t
->is_record()) {
217 (*name
)[name_length
] = '\0';
218 this->leave_record(t
, *name
, row_major
);
220 } else if (t
->is_array() && (t
->fields
.array
->is_record()
221 || t
->fields
.array
->is_interface())) {
222 if (record_type
== NULL
&& t
->fields
.array
->is_record())
223 record_type
= t
->fields
.array
;
225 for (unsigned i
= 0; i
< t
->length
; i
++) {
226 size_t new_length
= name_length
;
228 /* Append the subscript to the current variable name */
229 ralloc_asprintf_rewrite_tail(name
, &new_length
, "[%u]", i
);
231 recursion(t
->fields
.array
, name
, new_length
, row_major
,
233 (i
+ 1) == t
->length
);
235 /* Only the first leaf-field of the record gets called with the
236 * record type pointer.
241 this->visit_field(t
, *name
, row_major
, record_type
, last_field
);
246 program_resource_visitor::visit_field(const glsl_type
*type
, const char *name
,
249 bool /* last_field */)
251 visit_field(type
, name
, row_major
);
255 program_resource_visitor::visit_field(const glsl_struct_field
*field
)
262 program_resource_visitor::enter_record(const glsl_type
*, const char *, bool)
267 program_resource_visitor::leave_record(const glsl_type
*, const char *, bool)
274 * Class to help calculate the storage requirements for a set of uniforms
276 * As uniforms are added to the active set the number of active uniforms and
277 * the storage requirements for those uniforms are accumulated. The active
278 * uniforms are added to the hash table supplied to the constructor.
280 * If the same uniform is added multiple times (i.e., once for each shader
281 * target), it will only be accounted once.
283 class count_uniform_size
: public program_resource_visitor
{
285 count_uniform_size(struct string_to_uint_map
*map
)
286 : num_active_uniforms(0), num_values(0), num_shader_samplers(0),
287 num_shader_images(0), num_shader_uniform_components(0),
288 num_shader_subroutines(0),
289 is_ubo_var(false), map(map
)
296 this->num_shader_samplers
= 0;
297 this->num_shader_images
= 0;
298 this->num_shader_uniform_components
= 0;
299 this->num_shader_subroutines
= 0;
302 void process(ir_variable
*var
)
304 this->is_ubo_var
= var
->is_in_buffer_block();
305 if (var
->is_interface_instance())
306 program_resource_visitor::process(var
->get_interface_type(),
307 var
->get_interface_type()->name
);
309 program_resource_visitor::process(var
);
313 * Total number of active uniforms counted
315 unsigned num_active_uniforms
;
318 * Number of data values required to back the storage for the active uniforms
323 * Number of samplers used
325 unsigned num_shader_samplers
;
328 * Number of images used
330 unsigned num_shader_images
;
333 * Number of uniforms used in the current shader
335 unsigned num_shader_uniform_components
;
338 * Number of subroutine uniforms used
340 unsigned num_shader_subroutines
;
345 virtual void visit_field(const glsl_type
*type
, const char *name
,
348 assert(!type
->without_array()->is_record());
349 assert(!type
->without_array()->is_interface());
353 /* Count the number of samplers regardless of whether the uniform is
354 * already in the hash table. The hash table prevents adding the same
355 * uniform for multiple shader targets, but in this case we want to
356 * count it for each shader target.
358 const unsigned values
= values_for_type(type
);
359 if (type
->contains_subroutine()) {
360 this->num_shader_subroutines
+= values
;
361 } else if (type
->contains_sampler()) {
362 this->num_shader_samplers
+= values
;
363 } else if (type
->contains_image()) {
364 this->num_shader_images
+= values
;
366 /* As drivers are likely to represent image uniforms as
367 * scalar indices, count them against the limit of uniform
368 * components in the default block. The spec allows image
369 * uniforms to use up no more than one scalar slot.
371 this->num_shader_uniform_components
+= values
;
373 /* Accumulate the total number of uniform slots used by this shader.
374 * Note that samplers do not count against this limit because they
375 * don't use any storage on current hardware.
378 this->num_shader_uniform_components
+= values
;
381 /* If the uniform is already in the map, there's nothing more to do.
384 if (this->map
->get(id
, name
))
387 this->map
->put(this->num_active_uniforms
, name
);
389 /* Each leaf uniform occupies one entry in the list of active
392 this->num_active_uniforms
++;
393 this->num_values
+= values
;
396 struct string_to_uint_map
*map
;
399 } /* anonymous namespace */
402 * Class to help parcel out pieces of backing storage to uniforms
404 * Each uniform processed has some range of the \c gl_constant_value
405 * structures associated with it. The association is done by finding
406 * the uniform in the \c string_to_uint_map and using the value from
407 * the map to connect that slot in the \c gl_uniform_storage table
408 * with the next available slot in the \c gl_constant_value array.
411 * This class assumes that every uniform that will be processed is
412 * already in the \c string_to_uint_map. In addition, it assumes that
413 * the \c gl_uniform_storage and \c gl_constant_value arrays are "big
416 class parcel_out_uniform_storage
: public program_resource_visitor
{
418 parcel_out_uniform_storage(struct string_to_uint_map
*map
,
419 struct gl_uniform_storage
*uniforms
,
420 union gl_constant_value
*values
)
421 : 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 memset(this->targets
, 0, sizeof(this->targets
));
438 void set_and_process(struct gl_shader_program
*prog
,
444 ubo_block_index
= -1;
445 if (var
->is_in_buffer_block()) {
446 if (var
->is_interface_instance() && var
->type
->is_array()) {
447 unsigned l
= strlen(var
->get_interface_type()->name
);
449 for (unsigned i
= 0; i
< prog
->NumUniformBlocks
; i
++) {
450 if (strncmp(var
->get_interface_type()->name
,
451 prog
->UniformBlocks
[i
].Name
,
453 && prog
->UniformBlocks
[i
].Name
[l
] == '[') {
459 for (unsigned i
= 0; i
< prog
->NumUniformBlocks
; i
++) {
460 if (strcmp(var
->get_interface_type()->name
,
461 prog
->UniformBlocks
[i
].Name
) == 0) {
467 assert(ubo_block_index
!= -1);
469 /* Uniform blocks that were specified with an instance name must be
470 * handled a little bit differently. The name of the variable is the
471 * name used to reference the uniform block instead of being the name
472 * of a variable within the block. Therefore, searching for the name
473 * within the block will fail.
475 if (var
->is_interface_instance()) {
478 const struct gl_uniform_block
*const block
=
479 &prog
->UniformBlocks
[ubo_block_index
];
481 assert(var
->data
.location
!= -1);
483 const struct gl_uniform_buffer_variable
*const ubo_var
=
484 &block
->Uniforms
[var
->data
.location
];
486 ubo_byte_offset
= ubo_var
->Offset
;
489 if (var
->is_interface_instance())
490 process(var
->get_interface_type(),
491 var
->get_interface_type()->name
);
500 gl_shader_stage shader_type
;
503 void handle_samplers(const glsl_type
*base_type
,
504 struct gl_uniform_storage
*uniform
)
506 if (base_type
->is_sampler()) {
507 uniform
->sampler
[shader_type
].index
= this->next_sampler
;
508 uniform
->sampler
[shader_type
].active
= true;
510 /* Increment the sampler by 1 for non-arrays and by the number of
511 * array elements for arrays.
513 this->next_sampler
+=
514 MAX2(1, uniform
->array_elements
);
516 const gl_texture_index target
= base_type
->sampler_index();
517 const unsigned shadow
= base_type
->sampler_shadow
;
518 for (unsigned i
= uniform
->sampler
[shader_type
].index
;
519 i
< MIN2(this->next_sampler
, MAX_SAMPLERS
);
521 this->targets
[i
] = target
;
522 this->shader_samplers_used
|= 1U << i
;
523 this->shader_shadow_samplers
|= shadow
<< i
;
526 uniform
->sampler
[shader_type
].index
= ~0;
527 uniform
->sampler
[shader_type
].active
= false;
531 void handle_images(const glsl_type
*base_type
,
532 struct gl_uniform_storage
*uniform
)
534 if (base_type
->is_image()) {
535 uniform
->image
[shader_type
].index
= this->next_image
;
536 uniform
->image
[shader_type
].active
= true;
538 /* Increment the image index by 1 for non-arrays and by the
539 * number of array elements for arrays.
541 this->next_image
+= MAX2(1, uniform
->array_elements
);
544 uniform
->image
[shader_type
].index
= ~0;
545 uniform
->image
[shader_type
].active
= false;
549 void handle_subroutines(const glsl_type
*base_type
,
550 struct gl_uniform_storage
*uniform
)
552 if (base_type
->is_subroutine()) {
553 uniform
->subroutine
[shader_type
].index
= this->next_subroutine
;
554 uniform
->subroutine
[shader_type
].active
= true;
556 /* Increment the subroutine index by 1 for non-arrays and by the
557 * number of array elements for arrays.
559 this->next_subroutine
+= MAX2(1, uniform
->array_elements
);
562 uniform
->subroutine
[shader_type
].index
= ~0;
563 uniform
->subroutine
[shader_type
].active
= false;
567 virtual void visit_field(const glsl_type
*type
, const char *name
,
573 assert(!"Should not get here.");
576 virtual void enter_record(const glsl_type
*type
, const char *,
578 assert(type
->is_record());
579 if (this->ubo_block_index
== -1)
581 this->ubo_byte_offset
= glsl_align(
582 this->ubo_byte_offset
, type
->std140_base_alignment(row_major
));
585 virtual void leave_record(const glsl_type
*type
, const char *,
587 assert(type
->is_record());
588 if (this->ubo_block_index
== -1)
590 this->ubo_byte_offset
= glsl_align(
591 this->ubo_byte_offset
, type
->std140_base_alignment(row_major
));
594 virtual void visit_field(const glsl_type
*type
, const char *name
,
595 bool row_major
, const glsl_type
*record_type
,
596 bool /* last_field */)
598 assert(!type
->without_array()->is_record());
599 assert(!type
->without_array()->is_interface());
602 bool found
= this->map
->get(id
, name
);
608 const glsl_type
*base_type
;
609 if (type
->is_array()) {
610 this->uniforms
[id
].array_elements
= type
->length
;
611 base_type
= type
->fields
.array
;
613 this->uniforms
[id
].array_elements
= 0;
617 /* This assigns uniform indices to sampler and image uniforms. */
618 handle_samplers(base_type
, &this->uniforms
[id
]);
619 handle_images(base_type
, &this->uniforms
[id
]);
620 handle_subroutines(base_type
, &this->uniforms
[id
]);
622 /* If there is already storage associated with this uniform or if the
623 * uniform is set as builtin, it means that it was set while processing
624 * an earlier shader stage. For example, we may be processing the
625 * uniform in the fragment shader, but the uniform was already processed
626 * in the vertex shader.
628 if (this->uniforms
[id
].storage
!= NULL
|| this->uniforms
[id
].builtin
) {
632 /* Assign explicit locations. */
633 if (current_var
->data
.explicit_location
) {
634 /* Set sequential locations for struct fields. */
635 if (record_type
!= NULL
) {
636 const unsigned entries
= MAX2(1, this->uniforms
[id
].array_elements
);
637 this->uniforms
[id
].remap_location
=
638 current_var
->data
.location
+ field_counter
;
639 field_counter
+= entries
;
641 this->uniforms
[id
].remap_location
= current_var
->data
.location
;
644 /* Initialize to to indicate that no location is set */
645 this->uniforms
[id
].remap_location
= UNMAPPED_UNIFORM_LOC
;
648 this->uniforms
[id
].name
= ralloc_strdup(this->uniforms
, name
);
649 this->uniforms
[id
].type
= base_type
;
650 this->uniforms
[id
].initialized
= 0;
651 this->uniforms
[id
].num_driver_storage
= 0;
652 this->uniforms
[id
].driver_storage
= NULL
;
653 this->uniforms
[id
].atomic_buffer_index
= -1;
654 this->uniforms
[id
].hidden
=
655 current_var
->data
.how_declared
== ir_var_hidden
;
656 this->uniforms
[id
].builtin
= is_gl_identifier(name
);
658 /* Do not assign storage if the uniform is builtin */
659 if (!this->uniforms
[id
].builtin
)
660 this->uniforms
[id
].storage
= this->values
;
662 if (this->ubo_block_index
!= -1) {
663 this->uniforms
[id
].block_index
= this->ubo_block_index
;
665 const unsigned alignment
= type
->std140_base_alignment(row_major
);
666 this->ubo_byte_offset
= glsl_align(this->ubo_byte_offset
, alignment
);
667 this->uniforms
[id
].offset
= this->ubo_byte_offset
;
668 this->ubo_byte_offset
+= type
->std140_size(row_major
);
670 if (type
->is_array()) {
671 this->uniforms
[id
].array_stride
=
672 glsl_align(type
->fields
.array
->std140_size(row_major
), 16);
674 this->uniforms
[id
].array_stride
= 0;
677 if (type
->without_array()->is_matrix()) {
678 const glsl_type
*matrix
= type
->without_array();
679 const unsigned N
= matrix
->base_type
== GLSL_TYPE_DOUBLE
? 8 : 4;
680 const unsigned items
= row_major
? matrix
->matrix_columns
: matrix
->vector_elements
;
683 this->uniforms
[id
].matrix_stride
= glsl_align(items
* N
, 16);
684 this->uniforms
[id
].row_major
= row_major
;
686 this->uniforms
[id
].matrix_stride
= 0;
687 this->uniforms
[id
].row_major
= false;
690 this->uniforms
[id
].block_index
= -1;
691 this->uniforms
[id
].offset
= -1;
692 this->uniforms
[id
].array_stride
= -1;
693 this->uniforms
[id
].matrix_stride
= -1;
694 this->uniforms
[id
].row_major
= false;
697 this->values
+= values_for_type(type
);
700 struct string_to_uint_map
*map
;
702 struct gl_uniform_storage
*uniforms
;
703 unsigned next_sampler
;
705 unsigned next_subroutine
;
708 union gl_constant_value
*values
;
710 gl_texture_index targets
[MAX_SAMPLERS
];
713 * Current variable being processed.
715 ir_variable
*current_var
;
718 * Field counter is used to take care that uniform structures
719 * with explicit locations get sequential locations.
721 unsigned field_counter
;
724 * Mask of samplers used by the current shader stage.
726 unsigned shader_samplers_used
;
729 * Mask of samplers used by the current shader stage for shadows.
731 unsigned shader_shadow_samplers
;
735 * Merges a uniform block into an array of uniform blocks that may or
736 * may not already contain a copy of it.
738 * Returns the index of the new block in the array.
741 link_cross_validate_uniform_block(void *mem_ctx
,
742 struct gl_uniform_block
**linked_blocks
,
743 unsigned int *num_linked_blocks
,
744 struct gl_uniform_block
*new_block
)
746 for (unsigned int i
= 0; i
< *num_linked_blocks
; i
++) {
747 struct gl_uniform_block
*old_block
= &(*linked_blocks
)[i
];
749 if (strcmp(old_block
->Name
, new_block
->Name
) == 0)
750 return link_uniform_blocks_are_compatible(old_block
, new_block
)
754 *linked_blocks
= reralloc(mem_ctx
, *linked_blocks
,
755 struct gl_uniform_block
,
756 *num_linked_blocks
+ 1);
757 int linked_block_index
= (*num_linked_blocks
)++;
758 struct gl_uniform_block
*linked_block
= &(*linked_blocks
)[linked_block_index
];
760 memcpy(linked_block
, new_block
, sizeof(*new_block
));
761 linked_block
->Uniforms
= ralloc_array(*linked_blocks
,
762 struct gl_uniform_buffer_variable
,
763 linked_block
->NumUniforms
);
765 memcpy(linked_block
->Uniforms
,
767 sizeof(*linked_block
->Uniforms
) * linked_block
->NumUniforms
);
769 for (unsigned int i
= 0; i
< linked_block
->NumUniforms
; i
++) {
770 struct gl_uniform_buffer_variable
*ubo_var
=
771 &linked_block
->Uniforms
[i
];
773 if (ubo_var
->Name
== ubo_var
->IndexName
) {
774 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
775 ubo_var
->IndexName
= ubo_var
->Name
;
777 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
778 ubo_var
->IndexName
= ralloc_strdup(*linked_blocks
, ubo_var
->IndexName
);
782 return linked_block_index
;
786 * Walks the IR and update the references to uniform blocks in the
787 * ir_variables to point at linked shader's list (previously, they
788 * would point at the uniform block list in one of the pre-linked
792 link_update_uniform_buffer_variables(struct gl_shader
*shader
)
794 foreach_in_list(ir_instruction
, node
, shader
->ir
) {
795 ir_variable
*const var
= node
->as_variable();
797 if ((var
== NULL
) || !var
->is_in_buffer_block())
800 assert(var
->data
.mode
== ir_var_uniform
||
801 var
->data
.mode
== ir_var_shader_storage
);
803 if (var
->is_interface_instance()) {
804 var
->data
.location
= 0;
809 char sentinel
= '\0';
811 if (var
->type
->is_record()) {
813 } else if (var
->type
->is_array()
814 && var
->type
->fields
.array
->is_record()) {
818 const unsigned l
= strlen(var
->name
);
819 for (unsigned i
= 0; i
< shader
->NumUniformBlocks
; i
++) {
820 for (unsigned j
= 0; j
< shader
->UniformBlocks
[i
].NumUniforms
; j
++) {
822 const char *begin
= shader
->UniformBlocks
[i
].Uniforms
[j
].Name
;
823 const char *end
= strchr(begin
, sentinel
);
828 if ((ptrdiff_t) l
!= (end
- begin
))
831 if (strncmp(var
->name
, begin
, l
) == 0) {
833 var
->data
.location
= j
;
836 } else if (!strcmp(var
->name
,
837 shader
->UniformBlocks
[i
].Uniforms
[j
].Name
)) {
839 var
->data
.location
= j
;
851 * Scan the program for image uniforms and store image unit access
852 * information into the gl_shader data structure.
855 link_set_image_access_qualifiers(struct gl_shader_program
*prog
)
857 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
858 gl_shader
*sh
= prog
->_LinkedShaders
[i
];
863 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
864 ir_variable
*var
= node
->as_variable();
866 if (var
&& var
->data
.mode
== ir_var_uniform
&&
867 var
->type
->contains_image()) {
869 bool found
= prog
->UniformHash
->get(id
, var
->name
);
872 const gl_uniform_storage
*storage
= &prog
->UniformStorage
[id
];
873 const unsigned index
= storage
->image
[i
].index
;
874 const GLenum access
= (var
->data
.image_read_only
? GL_READ_ONLY
:
875 var
->data
.image_write_only
? GL_WRITE_ONLY
:
878 for (unsigned j
= 0; j
< MAX2(1, storage
->array_elements
); ++j
)
879 sh
->ImageAccess
[index
+ j
] = access
;
886 * Sort the array of uniform storage so that the non-hidden uniforms are first
888 * This function sorts the list "in place." This is important because some of
889 * the storage accessible from \c uniforms has \c uniforms as its \c ralloc
890 * context. If \c uniforms is freed, some other storage will also be freed.
893 move_hidden_uniforms_to_end(struct gl_shader_program
*prog
,
894 struct gl_uniform_storage
*uniforms
,
895 unsigned num_elements
)
897 struct gl_uniform_storage
*sorted_uniforms
=
898 ralloc_array(prog
, struct gl_uniform_storage
, num_elements
);
899 unsigned hidden_uniforms
= 0;
902 /* Add the non-hidden uniforms. */
903 for (unsigned i
= 0; i
< num_elements
; i
++) {
904 if (!uniforms
[i
].hidden
)
905 sorted_uniforms
[j
++] = uniforms
[i
];
908 /* Add and count the hidden uniforms. */
909 for (unsigned i
= 0; i
< num_elements
; i
++) {
910 if (uniforms
[i
].hidden
) {
911 sorted_uniforms
[j
++] = uniforms
[i
];
916 assert(prog
->UniformHash
!= NULL
);
917 prog
->UniformHash
->clear();
918 for (unsigned i
= 0; i
< num_elements
; i
++) {
919 if (sorted_uniforms
[i
].name
!= NULL
)
920 prog
->UniformHash
->put(i
, sorted_uniforms
[i
].name
);
923 memcpy(uniforms
, sorted_uniforms
, sizeof(uniforms
[0]) * num_elements
);
924 ralloc_free(sorted_uniforms
);
926 return hidden_uniforms
;
930 link_assign_uniform_locations(struct gl_shader_program
*prog
,
931 unsigned int boolean_true
)
933 ralloc_free(prog
->UniformStorage
);
934 prog
->UniformStorage
= NULL
;
935 prog
->NumUniformStorage
= 0;
937 if (prog
->UniformHash
!= NULL
) {
938 prog
->UniformHash
->clear();
940 prog
->UniformHash
= new string_to_uint_map
;
943 /* First pass: Count the uniform resources used by the user-defined
944 * uniforms. While this happens, each active uniform will have an index
947 * Note: this is *NOT* the index that is returned to the application by
948 * glGetUniformLocation.
950 count_uniform_size
uniform_size(prog
->UniformHash
);
951 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
952 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
957 /* Uniforms that lack an initializer in the shader code have an initial
958 * value of zero. This includes sampler uniforms.
960 * Page 24 (page 30 of the PDF) of the GLSL 1.20 spec says:
962 * "The link time initial value is either the value of the variable's
963 * initializer, if present, or 0 if no initializer is present. Sampler
964 * types cannot have initializers."
966 memset(sh
->SamplerUnits
, 0, sizeof(sh
->SamplerUnits
));
967 memset(sh
->ImageUnits
, 0, sizeof(sh
->ImageUnits
));
969 link_update_uniform_buffer_variables(sh
);
971 /* Reset various per-shader target counts.
973 uniform_size
.start_shader();
975 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
976 ir_variable
*const var
= node
->as_variable();
978 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
&&
979 var
->data
.mode
!= ir_var_shader_storage
))
982 uniform_size
.process(var
);
985 sh
->num_samplers
= uniform_size
.num_shader_samplers
;
986 sh
->NumImages
= uniform_size
.num_shader_images
;
987 sh
->num_uniform_components
= uniform_size
.num_shader_uniform_components
;
988 sh
->num_combined_uniform_components
= sh
->num_uniform_components
;
990 for (unsigned i
= 0; i
< sh
->NumUniformBlocks
; i
++) {
991 sh
->num_combined_uniform_components
+=
992 sh
->UniformBlocks
[i
].UniformBufferSize
/ 4;
996 const unsigned num_uniforms
= uniform_size
.num_active_uniforms
;
997 const unsigned num_data_slots
= uniform_size
.num_values
;
999 /* On the outside chance that there were no uniforms, bail out.
1001 if (num_uniforms
== 0)
1004 struct gl_uniform_storage
*uniforms
=
1005 rzalloc_array(prog
, struct gl_uniform_storage
, num_uniforms
);
1006 union gl_constant_value
*data
=
1007 rzalloc_array(uniforms
, union gl_constant_value
, num_data_slots
);
1009 union gl_constant_value
*data_end
= &data
[num_data_slots
];
1012 parcel_out_uniform_storage
parcel(prog
->UniformHash
, uniforms
, data
);
1014 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1015 if (prog
->_LinkedShaders
[i
] == NULL
)
1018 parcel
.start_shader((gl_shader_stage
)i
);
1020 foreach_in_list(ir_instruction
, node
, prog
->_LinkedShaders
[i
]->ir
) {
1021 ir_variable
*const var
= node
->as_variable();
1023 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
&& var
->data
.mode
!= ir_var_shader_storage
))
1026 parcel
.set_and_process(prog
, var
);
1029 prog
->_LinkedShaders
[i
]->active_samplers
= parcel
.shader_samplers_used
;
1030 prog
->_LinkedShaders
[i
]->shadow_samplers
= parcel
.shader_shadow_samplers
;
1032 STATIC_ASSERT(sizeof(prog
->_LinkedShaders
[i
]->SamplerTargets
) == sizeof(parcel
.targets
));
1033 memcpy(prog
->_LinkedShaders
[i
]->SamplerTargets
, parcel
.targets
,
1034 sizeof(prog
->_LinkedShaders
[i
]->SamplerTargets
));
1037 const unsigned hidden_uniforms
=
1038 move_hidden_uniforms_to_end(prog
, uniforms
, num_uniforms
);
1040 /* Reserve all the explicit locations of the active uniforms. */
1041 for (unsigned i
= 0; i
< num_uniforms
; i
++) {
1042 if (uniforms
[i
].type
->is_subroutine())
1045 if (uniforms
[i
].remap_location
!= UNMAPPED_UNIFORM_LOC
) {
1046 /* How many new entries for this uniform? */
1047 const unsigned entries
= MAX2(1, uniforms
[i
].array_elements
);
1049 /* Set remap table entries point to correct gl_uniform_storage. */
1050 for (unsigned j
= 0; j
< entries
; j
++) {
1051 unsigned element_loc
= uniforms
[i
].remap_location
+ j
;
1052 assert(prog
->UniformRemapTable
[element_loc
] ==
1053 INACTIVE_UNIFORM_EXPLICIT_LOCATION
);
1054 prog
->UniformRemapTable
[element_loc
] = &uniforms
[i
];
1059 /* Reserve locations for rest of the uniforms. */
1060 for (unsigned i
= 0; i
< num_uniforms
; i
++) {
1062 if (uniforms
[i
].type
->is_subroutine())
1064 /* Built-in uniforms should not get any location. */
1065 if (uniforms
[i
].builtin
)
1068 /* Explicit ones have been set already. */
1069 if (uniforms
[i
].remap_location
!= UNMAPPED_UNIFORM_LOC
)
1072 /* how many new entries for this uniform? */
1073 const unsigned entries
= MAX2(1, uniforms
[i
].array_elements
);
1075 /* resize remap table to fit new entries */
1076 prog
->UniformRemapTable
=
1078 prog
->UniformRemapTable
,
1079 gl_uniform_storage
*,
1080 prog
->NumUniformRemapTable
+ entries
);
1082 /* set pointers for this uniform */
1083 for (unsigned j
= 0; j
< entries
; j
++)
1084 prog
->UniformRemapTable
[prog
->NumUniformRemapTable
+j
] = &uniforms
[i
];
1086 /* set the base location in remap table for the uniform */
1087 uniforms
[i
].remap_location
= prog
->NumUniformRemapTable
;
1089 prog
->NumUniformRemapTable
+= entries
;
1092 /* Reserve all the explicit locations of the active subroutine uniforms. */
1093 for (unsigned i
= 0; i
< num_uniforms
; i
++) {
1094 if (!uniforms
[i
].type
->is_subroutine())
1097 if (uniforms
[i
].remap_location
== UNMAPPED_UNIFORM_LOC
)
1100 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; j
++) {
1101 struct gl_shader
*sh
= prog
->_LinkedShaders
[j
];
1105 if (!uniforms
[i
].subroutine
[j
].active
)
1108 /* How many new entries for this uniform? */
1109 const unsigned entries
= MAX2(1, uniforms
[i
].array_elements
);
1111 /* Set remap table entries point to correct gl_uniform_storage. */
1112 for (unsigned k
= 0; k
< entries
; k
++) {
1113 unsigned element_loc
= uniforms
[i
].remap_location
+ k
;
1114 assert(sh
->SubroutineUniformRemapTable
[element_loc
] ==
1115 INACTIVE_UNIFORM_EXPLICIT_LOCATION
);
1116 sh
->SubroutineUniformRemapTable
[element_loc
] = &uniforms
[i
];
1121 /* reserve subroutine locations */
1122 for (unsigned i
= 0; i
< num_uniforms
; i
++) {
1124 if (!uniforms
[i
].type
->is_subroutine())
1126 const unsigned entries
= MAX2(1, uniforms
[i
].array_elements
);
1128 if (uniforms
[i
].remap_location
!= UNMAPPED_UNIFORM_LOC
)
1130 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; j
++) {
1131 struct gl_shader
*sh
= prog
->_LinkedShaders
[j
];
1135 if (!uniforms
[i
].subroutine
[j
].active
)
1138 sh
->SubroutineUniformRemapTable
=
1140 sh
->SubroutineUniformRemapTable
,
1141 gl_uniform_storage
*,
1142 sh
->NumSubroutineUniformRemapTable
+ entries
);
1144 for (unsigned k
= 0; k
< entries
; k
++)
1145 sh
->SubroutineUniformRemapTable
[sh
->NumSubroutineUniformRemapTable
+ k
] = &uniforms
[i
];
1146 uniforms
[i
].remap_location
= sh
->NumSubroutineUniformRemapTable
;
1147 sh
->NumSubroutineUniformRemapTable
+= entries
;
1152 for (unsigned i
= 0; i
< num_uniforms
; i
++) {
1153 assert(uniforms
[i
].storage
!= NULL
|| uniforms
[i
].builtin
);
1156 assert(parcel
.values
== data_end
);
1159 prog
->NumUniformStorage
= num_uniforms
;
1160 prog
->NumHiddenUniforms
= hidden_uniforms
;
1161 prog
->UniformStorage
= uniforms
;
1163 link_set_image_access_qualifiers(prog
);
1164 link_set_uniform_initializers(prog
, boolean_true
);