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()) {
52 } else if (type
->is_array() && type
->fields
.array
->is_sampler()) {
53 return type
->array_size();
55 return type
->component_slots();
60 program_resource_visitor::process(const glsl_type
*type
, const char *name
)
62 assert(type
->without_array()->is_record()
63 || type
->without_array()->is_interface());
65 char *name_copy
= ralloc_strdup(NULL
, name
);
66 recursion(type
, &name_copy
, strlen(name
), false, NULL
, false);
67 ralloc_free(name_copy
);
71 program_resource_visitor::process(ir_variable
*var
)
73 const glsl_type
*t
= var
->type
;
74 const bool row_major
=
75 var
->data
.matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
77 /* false is always passed for the row_major parameter to the other
78 * processing functions because no information is available to do
79 * otherwise. See the warning in linker.h.
82 /* Only strdup the name if we actually will need to modify it. */
83 if (var
->data
.from_named_ifc_block_array
) {
84 /* lower_named_interface_blocks created this variable by lowering an
85 * interface block array to an array variable. For example if the
86 * original source code was:
88 * out Blk { vec4 bar } foo[3];
90 * Then the variable is now:
94 * We need to visit each array element using the names constructed like
101 assert(t
->is_array());
102 const glsl_type
*ifc_type
= var
->get_interface_type();
103 char *name
= ralloc_strdup(NULL
, ifc_type
->name
);
104 size_t name_length
= strlen(name
);
105 for (unsigned i
= 0; i
< t
->length
; i
++) {
106 size_t new_length
= name_length
;
107 ralloc_asprintf_rewrite_tail(&name
, &new_length
, "[%u].%s", i
,
109 /* Note: row_major is only meaningful for uniform blocks, and
110 * lowering is only applied to non-uniform interface blocks, so we
111 * can safely pass false for row_major.
113 recursion(var
->type
, &name
, new_length
, row_major
, NULL
, false);
116 } else if (var
->data
.from_named_ifc_block_nonarray
) {
117 /* lower_named_interface_blocks created this variable by lowering a
118 * named interface block (non-array) to an ordinary variable. For
119 * example if the original source code was:
121 * out Blk { vec4 bar } foo;
123 * Then the variable is now:
127 * We need to visit this variable using the name:
131 const glsl_type
*ifc_type
= var
->get_interface_type();
132 char *name
= ralloc_asprintf(NULL
, "%s.%s", ifc_type
->name
, var
->name
);
133 /* Note: row_major is only meaningful for uniform blocks, and lowering
134 * is only applied to non-uniform interface blocks, so we can safely
135 * pass false for row_major.
137 recursion(var
->type
, &name
, strlen(name
), row_major
, NULL
, false);
139 } else if (t
->without_array()->is_record()) {
140 char *name
= ralloc_strdup(NULL
, var
->name
);
141 recursion(var
->type
, &name
, strlen(name
), row_major
, NULL
, false);
143 } else if (t
->is_interface()) {
144 char *name
= ralloc_strdup(NULL
, var
->type
->name
);
145 recursion(var
->type
, &name
, strlen(name
), row_major
, NULL
, false);
147 } else if (t
->is_array() && t
->fields
.array
->is_interface()) {
148 char *name
= ralloc_strdup(NULL
, var
->type
->fields
.array
->name
);
149 recursion(var
->type
, &name
, strlen(name
), row_major
, NULL
, false);
152 this->visit_field(t
, var
->name
, row_major
, NULL
, false);
157 program_resource_visitor::recursion(const glsl_type
*t
, char **name
,
158 size_t name_length
, bool row_major
,
159 const glsl_type
*record_type
,
162 /* Records need to have each field processed individually.
164 * Arrays of records need to have each array element processed
165 * individually, then each field of the resulting array elements processed
168 if (t
->is_record() || t
->is_interface()) {
169 if (record_type
== NULL
&& t
->is_record())
172 for (unsigned i
= 0; i
< t
->length
; i
++) {
173 const char *field
= t
->fields
.structure
[i
].name
;
174 size_t new_length
= name_length
;
176 if (t
->fields
.structure
[i
].type
->is_record())
177 this->visit_field(&t
->fields
.structure
[i
]);
179 /* Append '.field' to the current variable name. */
180 if (name_length
== 0) {
181 ralloc_asprintf_rewrite_tail(name
, &new_length
, "%s", field
);
183 ralloc_asprintf_rewrite_tail(name
, &new_length
, ".%s", field
);
186 /* The layout of structures at the top level of the block is set
187 * during parsing. For matrices contained in multiple levels of
188 * structures in the block, the inner structures have no layout.
189 * These cases must potentially inherit the layout from the outer
192 bool field_row_major
= row_major
;
193 const enum glsl_matrix_layout matrix_layout
=
194 glsl_matrix_layout(t
->fields
.structure
[i
].matrix_layout
);
195 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
196 field_row_major
= true;
197 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
198 field_row_major
= false;
201 recursion(t
->fields
.structure
[i
].type
, name
, new_length
,
204 (i
+ 1) == t
->length
);
206 /* Only the first leaf-field of the record gets called with the
207 * record type pointer.
211 } else if (t
->is_array() && (t
->fields
.array
->is_record()
212 || t
->fields
.array
->is_interface())) {
213 if (record_type
== NULL
&& t
->fields
.array
->is_record())
214 record_type
= t
->fields
.array
;
216 for (unsigned i
= 0; i
< t
->length
; i
++) {
217 size_t new_length
= name_length
;
219 /* Append the subscript to the current variable name */
220 ralloc_asprintf_rewrite_tail(name
, &new_length
, "[%u]", i
);
222 recursion(t
->fields
.array
, name
, new_length
, row_major
,
224 (i
+ 1) == t
->length
);
226 /* Only the first leaf-field of the record gets called with the
227 * record type pointer.
232 this->visit_field(t
, *name
, row_major
, record_type
, last_field
);
237 program_resource_visitor::visit_field(const glsl_type
*type
, const char *name
,
240 bool /* last_field */)
242 visit_field(type
, name
, row_major
);
246 program_resource_visitor::visit_field(const glsl_struct_field
*field
)
255 * Class to help calculate the storage requirements for a set of uniforms
257 * As uniforms are added to the active set the number of active uniforms and
258 * the storage requirements for those uniforms are accumulated. The active
259 * uniforms are added the the hash table supplied to the constructor.
261 * If the same uniform is added multiple times (i.e., once for each shader
262 * target), it will only be accounted once.
264 class count_uniform_size
: public program_resource_visitor
{
266 count_uniform_size(struct string_to_uint_map
*map
)
267 : num_active_uniforms(0), num_values(0), num_shader_samplers(0),
268 num_shader_images(0), num_shader_uniform_components(0),
269 is_ubo_var(false), map(map
)
276 this->num_shader_samplers
= 0;
277 this->num_shader_images
= 0;
278 this->num_shader_uniform_components
= 0;
281 void process(ir_variable
*var
)
283 this->is_ubo_var
= var
->is_in_uniform_block();
284 if (var
->is_interface_instance())
285 program_resource_visitor::process(var
->get_interface_type(),
286 var
->get_interface_type()->name
);
288 program_resource_visitor::process(var
);
292 * Total number of active uniforms counted
294 unsigned num_active_uniforms
;
297 * Number of data values required to back the storage for the active uniforms
302 * Number of samplers used
304 unsigned num_shader_samplers
;
307 * Number of images used
309 unsigned num_shader_images
;
312 * Number of uniforms used in the current shader
314 unsigned num_shader_uniform_components
;
319 virtual void visit_field(const glsl_type
*type
, const char *name
,
322 assert(!type
->without_array()->is_record());
323 assert(!type
->without_array()->is_interface());
327 /* Count the number of samplers regardless of whether the uniform is
328 * already in the hash table. The hash table prevents adding the same
329 * uniform for multiple shader targets, but in this case we want to
330 * count it for each shader target.
332 const unsigned values
= values_for_type(type
);
333 if (type
->contains_sampler()) {
334 this->num_shader_samplers
+= values
;
335 } else if (type
->contains_image()) {
336 this->num_shader_images
+= values
;
338 /* As drivers are likely to represent image uniforms as
339 * scalar indices, count them against the limit of uniform
340 * components in the default block. The spec allows image
341 * uniforms to use up no more than one scalar slot.
343 this->num_shader_uniform_components
+= values
;
345 /* Accumulate the total number of uniform slots used by this shader.
346 * Note that samplers do not count against this limit because they
347 * don't use any storage on current hardware.
350 this->num_shader_uniform_components
+= values
;
353 /* If the uniform is already in the map, there's nothing more to do.
356 if (this->map
->get(id
, name
))
359 this->map
->put(this->num_active_uniforms
, name
);
361 /* Each leaf uniform occupies one entry in the list of active
364 this->num_active_uniforms
++;
365 this->num_values
+= values
;
368 struct string_to_uint_map
*map
;
371 } /* anonymous namespace */
374 * Class to help parcel out pieces of backing storage to uniforms
376 * Each uniform processed has some range of the \c gl_constant_value
377 * structures associated with it. The association is done by finding
378 * the uniform in the \c string_to_uint_map and using the value from
379 * the map to connect that slot in the \c gl_uniform_storage table
380 * with the next available slot in the \c gl_constant_value array.
383 * This class assumes that every uniform that will be processed is
384 * already in the \c string_to_uint_map. In addition, it assumes that
385 * the \c gl_uniform_storage and \c gl_constant_value arrays are "big
388 class parcel_out_uniform_storage
: public program_resource_visitor
{
390 parcel_out_uniform_storage(struct string_to_uint_map
*map
,
391 struct gl_uniform_storage
*uniforms
,
392 union gl_constant_value
*values
)
393 : map(map
), uniforms(uniforms
), values(values
)
397 void start_shader(gl_shader_stage shader_type
)
399 assert(shader_type
< MESA_SHADER_STAGES
);
400 this->shader_type
= shader_type
;
402 this->shader_samplers_used
= 0;
403 this->shader_shadow_samplers
= 0;
404 this->next_sampler
= 0;
405 this->next_image
= 0;
406 memset(this->targets
, 0, sizeof(this->targets
));
409 void set_and_process(struct gl_shader_program
*prog
,
415 ubo_block_index
= -1;
416 if (var
->is_in_uniform_block()) {
417 if (var
->is_interface_instance() && var
->type
->is_array()) {
418 unsigned l
= strlen(var
->get_interface_type()->name
);
420 for (unsigned i
= 0; i
< prog
->NumUniformBlocks
; i
++) {
421 if (strncmp(var
->get_interface_type()->name
,
422 prog
->UniformBlocks
[i
].Name
,
424 && prog
->UniformBlocks
[i
].Name
[l
] == '[') {
430 for (unsigned i
= 0; i
< prog
->NumUniformBlocks
; i
++) {
431 if (strcmp(var
->get_interface_type()->name
,
432 prog
->UniformBlocks
[i
].Name
) == 0) {
438 assert(ubo_block_index
!= -1);
440 /* Uniform blocks that were specified with an instance name must be
441 * handled a little bit differently. The name of the variable is the
442 * name used to reference the uniform block instead of being the name
443 * of a variable within the block. Therefore, searching for the name
444 * within the block will fail.
446 if (var
->is_interface_instance()) {
449 const struct gl_uniform_block
*const block
=
450 &prog
->UniformBlocks
[ubo_block_index
];
452 assert(var
->data
.location
!= -1);
454 const struct gl_uniform_buffer_variable
*const ubo_var
=
455 &block
->Uniforms
[var
->data
.location
];
457 ubo_byte_offset
= ubo_var
->Offset
;
460 if (var
->is_interface_instance())
461 process(var
->get_interface_type(),
462 var
->get_interface_type()->name
);
471 gl_shader_stage shader_type
;
474 void handle_samplers(const glsl_type
*base_type
,
475 struct gl_uniform_storage
*uniform
)
477 if (base_type
->is_sampler()) {
478 uniform
->sampler
[shader_type
].index
= this->next_sampler
;
479 uniform
->sampler
[shader_type
].active
= true;
481 /* Increment the sampler by 1 for non-arrays and by the number of
482 * array elements for arrays.
484 this->next_sampler
+=
485 MAX2(1, uniform
->array_elements
);
487 const gl_texture_index target
= base_type
->sampler_index();
488 const unsigned shadow
= base_type
->sampler_shadow
;
489 for (unsigned i
= uniform
->sampler
[shader_type
].index
;
490 i
< MIN2(this->next_sampler
, MAX_SAMPLERS
);
492 this->targets
[i
] = target
;
493 this->shader_samplers_used
|= 1U << i
;
494 this->shader_shadow_samplers
|= shadow
<< i
;
497 uniform
->sampler
[shader_type
].index
= ~0;
498 uniform
->sampler
[shader_type
].active
= false;
502 void handle_images(const glsl_type
*base_type
,
503 struct gl_uniform_storage
*uniform
)
505 if (base_type
->is_image()) {
506 uniform
->image
[shader_type
].index
= this->next_image
;
507 uniform
->image
[shader_type
].active
= true;
509 /* Increment the image index by 1 for non-arrays and by the
510 * number of array elements for arrays.
512 this->next_image
+= MAX2(1, uniform
->array_elements
);
515 uniform
->image
[shader_type
].index
= ~0;
516 uniform
->image
[shader_type
].active
= false;
520 virtual void visit_field(const glsl_type
*type
, const char *name
,
526 assert(!"Should not get here.");
529 virtual void visit_field(const glsl_type
*type
, const char *name
,
530 bool row_major
, const glsl_type
*record_type
,
533 assert(!type
->without_array()->is_record());
534 assert(!type
->without_array()->is_interface());
537 bool found
= this->map
->get(id
, name
);
543 const glsl_type
*base_type
;
544 if (type
->is_array()) {
545 this->uniforms
[id
].array_elements
= type
->length
;
546 base_type
= type
->fields
.array
;
548 this->uniforms
[id
].array_elements
= 0;
552 /* This assigns uniform indices to sampler and image uniforms. */
553 handle_samplers(base_type
, &this->uniforms
[id
]);
554 handle_images(base_type
, &this->uniforms
[id
]);
556 /* If there is already storage associated with this uniform, it means
557 * that it was set while processing an earlier shader stage. For
558 * example, we may be processing the uniform in the fragment shader, but
559 * the uniform was already processed in the vertex shader.
561 if (this->uniforms
[id
].storage
!= NULL
) {
565 /* Assign explicit locations. */
566 if (current_var
->data
.explicit_location
) {
567 /* Set sequential locations for struct fields. */
568 if (record_type
!= NULL
) {
569 const unsigned entries
= MAX2(1, this->uniforms
[id
].array_elements
);
570 this->uniforms
[id
].remap_location
=
571 current_var
->data
.location
+ field_counter
;
572 field_counter
+= entries
;
574 this->uniforms
[id
].remap_location
= current_var
->data
.location
;
577 /* Initialize to to indicate that no location is set */
578 this->uniforms
[id
].remap_location
= UNMAPPED_UNIFORM_LOC
;
581 this->uniforms
[id
].name
= ralloc_strdup(this->uniforms
, name
);
582 this->uniforms
[id
].type
= base_type
;
583 this->uniforms
[id
].initialized
= 0;
584 this->uniforms
[id
].num_driver_storage
= 0;
585 this->uniforms
[id
].driver_storage
= NULL
;
586 this->uniforms
[id
].storage
= this->values
;
587 this->uniforms
[id
].atomic_buffer_index
= -1;
588 this->uniforms
[id
].hidden
=
589 current_var
->data
.how_declared
== ir_var_hidden
;
590 if (this->ubo_block_index
!= -1) {
591 this->uniforms
[id
].block_index
= this->ubo_block_index
;
593 const unsigned alignment
= record_type
594 ? record_type
->std140_base_alignment(row_major
)
595 : type
->std140_base_alignment(row_major
);
596 this->ubo_byte_offset
= glsl_align(this->ubo_byte_offset
, alignment
);
597 this->uniforms
[id
].offset
= this->ubo_byte_offset
;
598 this->ubo_byte_offset
+= type
->std140_size(row_major
);
601 this->ubo_byte_offset
= glsl_align(this->ubo_byte_offset
, 16);
603 if (type
->is_array()) {
604 this->uniforms
[id
].array_stride
=
605 glsl_align(type
->fields
.array
->std140_size(row_major
), 16);
607 this->uniforms
[id
].array_stride
= 0;
610 if (type
->without_array()->is_matrix()) {
611 this->uniforms
[id
].matrix_stride
= 16;
612 this->uniforms
[id
].row_major
= row_major
;
614 this->uniforms
[id
].matrix_stride
= 0;
615 this->uniforms
[id
].row_major
= false;
618 this->uniforms
[id
].block_index
= -1;
619 this->uniforms
[id
].offset
= -1;
620 this->uniforms
[id
].array_stride
= -1;
621 this->uniforms
[id
].matrix_stride
= -1;
622 this->uniforms
[id
].row_major
= false;
625 this->values
+= values_for_type(type
);
628 struct string_to_uint_map
*map
;
630 struct gl_uniform_storage
*uniforms
;
631 unsigned next_sampler
;
635 union gl_constant_value
*values
;
637 gl_texture_index targets
[MAX_SAMPLERS
];
640 * Current variable being processed.
642 ir_variable
*current_var
;
645 * Field counter is used to take care that uniform structures
646 * with explicit locations get sequential locations.
648 unsigned field_counter
;
651 * Mask of samplers used by the current shader stage.
653 unsigned shader_samplers_used
;
656 * Mask of samplers used by the current shader stage for shadows.
658 unsigned shader_shadow_samplers
;
662 * Merges a uniform block into an array of uniform blocks that may or
663 * may not already contain a copy of it.
665 * Returns the index of the new block in the array.
668 link_cross_validate_uniform_block(void *mem_ctx
,
669 struct gl_uniform_block
**linked_blocks
,
670 unsigned int *num_linked_blocks
,
671 struct gl_uniform_block
*new_block
)
673 for (unsigned int i
= 0; i
< *num_linked_blocks
; i
++) {
674 struct gl_uniform_block
*old_block
= &(*linked_blocks
)[i
];
676 if (strcmp(old_block
->Name
, new_block
->Name
) == 0)
677 return link_uniform_blocks_are_compatible(old_block
, new_block
)
681 *linked_blocks
= reralloc(mem_ctx
, *linked_blocks
,
682 struct gl_uniform_block
,
683 *num_linked_blocks
+ 1);
684 int linked_block_index
= (*num_linked_blocks
)++;
685 struct gl_uniform_block
*linked_block
= &(*linked_blocks
)[linked_block_index
];
687 memcpy(linked_block
, new_block
, sizeof(*new_block
));
688 linked_block
->Uniforms
= ralloc_array(*linked_blocks
,
689 struct gl_uniform_buffer_variable
,
690 linked_block
->NumUniforms
);
692 memcpy(linked_block
->Uniforms
,
694 sizeof(*linked_block
->Uniforms
) * linked_block
->NumUniforms
);
696 for (unsigned int i
= 0; i
< linked_block
->NumUniforms
; i
++) {
697 struct gl_uniform_buffer_variable
*ubo_var
=
698 &linked_block
->Uniforms
[i
];
700 if (ubo_var
->Name
== ubo_var
->IndexName
) {
701 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
702 ubo_var
->IndexName
= ubo_var
->Name
;
704 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
705 ubo_var
->IndexName
= ralloc_strdup(*linked_blocks
, ubo_var
->IndexName
);
709 return linked_block_index
;
713 * Walks the IR and update the references to uniform blocks in the
714 * ir_variables to point at linked shader's list (previously, they
715 * would point at the uniform block list in one of the pre-linked
719 link_update_uniform_buffer_variables(struct gl_shader
*shader
)
721 foreach_in_list(ir_instruction
, node
, shader
->ir
) {
722 ir_variable
*const var
= node
->as_variable();
724 if ((var
== NULL
) || !var
->is_in_uniform_block())
727 assert(var
->data
.mode
== ir_var_uniform
);
729 if (var
->is_interface_instance()) {
730 var
->data
.location
= 0;
735 char sentinel
= '\0';
737 if (var
->type
->is_record()) {
739 } else if (var
->type
->is_array()
740 && var
->type
->fields
.array
->is_record()) {
744 const unsigned l
= strlen(var
->name
);
745 for (unsigned i
= 0; i
< shader
->NumUniformBlocks
; i
++) {
746 for (unsigned j
= 0; j
< shader
->UniformBlocks
[i
].NumUniforms
; j
++) {
748 const char *begin
= shader
->UniformBlocks
[i
].Uniforms
[j
].Name
;
749 const char *end
= strchr(begin
, sentinel
);
754 if ((ptrdiff_t) l
!= (end
- begin
))
757 if (strncmp(var
->name
, begin
, l
) == 0) {
759 var
->data
.location
= j
;
762 } else if (!strcmp(var
->name
,
763 shader
->UniformBlocks
[i
].Uniforms
[j
].Name
)) {
765 var
->data
.location
= j
;
777 * Scan the program for image uniforms and store image unit access
778 * information into the gl_shader data structure.
781 link_set_image_access_qualifiers(struct gl_shader_program
*prog
)
783 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
784 gl_shader
*sh
= prog
->_LinkedShaders
[i
];
789 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
790 ir_variable
*var
= node
->as_variable();
792 if (var
&& var
->data
.mode
== ir_var_uniform
&&
793 var
->type
->contains_image()) {
795 bool found
= prog
->UniformHash
->get(id
, var
->name
);
798 const gl_uniform_storage
*storage
= &prog
->UniformStorage
[id
];
799 const unsigned index
= storage
->image
[i
].index
;
800 const GLenum access
= (var
->data
.image_read_only
? GL_READ_ONLY
:
801 var
->data
.image_write_only
? GL_WRITE_ONLY
:
804 for (unsigned j
= 0; j
< MAX2(1, storage
->array_elements
); ++j
)
805 sh
->ImageAccess
[index
+ j
] = access
;
812 * Sort the array of uniform storage so that the non-hidden uniforms are first
814 * This function sorts the list "in place." This is important because some of
815 * the storage accessible from \c uniforms has \c uniforms as its \c ralloc
816 * context. If \c uniforms is freed, some other storage will also be freed.
819 move_hidden_uniforms_to_end(struct gl_shader_program
*prog
,
820 struct gl_uniform_storage
*uniforms
,
821 unsigned num_elements
)
823 struct gl_uniform_storage
*sorted_uniforms
=
824 ralloc_array(prog
, struct gl_uniform_storage
, num_elements
);
825 unsigned hidden_uniforms
= 0;
828 /* Add the non-hidden uniforms. */
829 for (unsigned i
= 0; i
< num_elements
; i
++) {
830 if (!uniforms
[i
].hidden
)
831 sorted_uniforms
[j
++] = uniforms
[i
];
834 /* Add and count the hidden uniforms. */
835 for (unsigned i
= 0; i
< num_elements
; i
++) {
836 if (uniforms
[i
].hidden
) {
837 sorted_uniforms
[j
++] = uniforms
[i
];
842 assert(prog
->UniformHash
!= NULL
);
843 prog
->UniformHash
->clear();
844 for (unsigned i
= 0; i
< num_elements
; i
++) {
845 if (sorted_uniforms
[i
].name
!= NULL
)
846 prog
->UniformHash
->put(i
, sorted_uniforms
[i
].name
);
849 memcpy(uniforms
, sorted_uniforms
, sizeof(uniforms
[0]) * num_elements
);
850 ralloc_free(sorted_uniforms
);
852 return hidden_uniforms
;
856 link_assign_uniform_locations(struct gl_shader_program
*prog
,
857 unsigned int boolean_true
)
859 ralloc_free(prog
->UniformStorage
);
860 prog
->UniformStorage
= NULL
;
861 prog
->NumUserUniformStorage
= 0;
863 if (prog
->UniformHash
!= NULL
) {
864 prog
->UniformHash
->clear();
866 prog
->UniformHash
= new string_to_uint_map
;
869 /* First pass: Count the uniform resources used by the user-defined
870 * uniforms. While this happens, each active uniform will have an index
873 * Note: this is *NOT* the index that is returned to the application by
874 * glGetUniformLocation.
876 count_uniform_size
uniform_size(prog
->UniformHash
);
877 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
878 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
883 /* Uniforms that lack an initializer in the shader code have an initial
884 * value of zero. This includes sampler uniforms.
886 * Page 24 (page 30 of the PDF) of the GLSL 1.20 spec says:
888 * "The link time initial value is either the value of the variable's
889 * initializer, if present, or 0 if no initializer is present. Sampler
890 * types cannot have initializers."
892 memset(sh
->SamplerUnits
, 0, sizeof(sh
->SamplerUnits
));
893 memset(sh
->ImageUnits
, 0, sizeof(sh
->ImageUnits
));
895 link_update_uniform_buffer_variables(sh
);
897 /* Reset various per-shader target counts.
899 uniform_size
.start_shader();
901 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
902 ir_variable
*const var
= node
->as_variable();
904 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
))
907 /* FINISHME: Update code to process built-in uniforms!
909 if (is_gl_identifier(var
->name
)) {
910 uniform_size
.num_shader_uniform_components
+=
911 var
->type
->component_slots();
915 uniform_size
.process(var
);
918 sh
->num_samplers
= uniform_size
.num_shader_samplers
;
919 sh
->NumImages
= uniform_size
.num_shader_images
;
920 sh
->num_uniform_components
= uniform_size
.num_shader_uniform_components
;
922 sh
->num_combined_uniform_components
= sh
->num_uniform_components
;
923 for (unsigned i
= 0; i
< sh
->NumUniformBlocks
; i
++) {
924 sh
->num_combined_uniform_components
+=
925 sh
->UniformBlocks
[i
].UniformBufferSize
/ 4;
929 const unsigned num_user_uniforms
= uniform_size
.num_active_uniforms
;
930 const unsigned num_data_slots
= uniform_size
.num_values
;
932 /* On the outside chance that there were no uniforms, bail out.
934 if (num_user_uniforms
== 0)
937 struct gl_uniform_storage
*uniforms
=
938 rzalloc_array(prog
, struct gl_uniform_storage
, num_user_uniforms
);
939 union gl_constant_value
*data
=
940 rzalloc_array(uniforms
, union gl_constant_value
, num_data_slots
);
942 union gl_constant_value
*data_end
= &data
[num_data_slots
];
945 parcel_out_uniform_storage
parcel(prog
->UniformHash
, uniforms
, data
);
947 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
948 if (prog
->_LinkedShaders
[i
] == NULL
)
951 parcel
.start_shader((gl_shader_stage
)i
);
953 foreach_in_list(ir_instruction
, node
, prog
->_LinkedShaders
[i
]->ir
) {
954 ir_variable
*const var
= node
->as_variable();
956 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
))
959 /* FINISHME: Update code to process built-in uniforms!
961 if (is_gl_identifier(var
->name
))
964 parcel
.set_and_process(prog
, var
);
967 prog
->_LinkedShaders
[i
]->active_samplers
= parcel
.shader_samplers_used
;
968 prog
->_LinkedShaders
[i
]->shadow_samplers
= parcel
.shader_shadow_samplers
;
970 STATIC_ASSERT(sizeof(prog
->_LinkedShaders
[i
]->SamplerTargets
) == sizeof(parcel
.targets
));
971 memcpy(prog
->_LinkedShaders
[i
]->SamplerTargets
, parcel
.targets
,
972 sizeof(prog
->_LinkedShaders
[i
]->SamplerTargets
));
975 const unsigned hidden_uniforms
=
976 move_hidden_uniforms_to_end(prog
, uniforms
, num_user_uniforms
);
978 /* Reserve all the explicit locations of the active uniforms. */
979 for (unsigned i
= 0; i
< num_user_uniforms
; i
++) {
980 if (uniforms
[i
].remap_location
!= UNMAPPED_UNIFORM_LOC
) {
981 /* How many new entries for this uniform? */
982 const unsigned entries
= MAX2(1, uniforms
[i
].array_elements
);
984 /* Set remap table entries point to correct gl_uniform_storage. */
985 for (unsigned j
= 0; j
< entries
; j
++) {
986 unsigned element_loc
= uniforms
[i
].remap_location
+ j
;
987 assert(prog
->UniformRemapTable
[element_loc
] ==
988 INACTIVE_UNIFORM_EXPLICIT_LOCATION
);
989 prog
->UniformRemapTable
[element_loc
] = &uniforms
[i
];
994 /* Reserve locations for rest of the uniforms. */
995 for (unsigned i
= 0; i
< num_user_uniforms
; i
++) {
997 /* Explicit ones have been set already. */
998 if (uniforms
[i
].remap_location
!= UNMAPPED_UNIFORM_LOC
)
1001 /* how many new entries for this uniform? */
1002 const unsigned entries
= MAX2(1, uniforms
[i
].array_elements
);
1004 /* resize remap table to fit new entries */
1005 prog
->UniformRemapTable
=
1007 prog
->UniformRemapTable
,
1008 gl_uniform_storage
*,
1009 prog
->NumUniformRemapTable
+ entries
);
1011 /* set pointers for this uniform */
1012 for (unsigned j
= 0; j
< entries
; j
++)
1013 prog
->UniformRemapTable
[prog
->NumUniformRemapTable
+j
] = &uniforms
[i
];
1015 /* set the base location in remap table for the uniform */
1016 uniforms
[i
].remap_location
= prog
->NumUniformRemapTable
;
1018 prog
->NumUniformRemapTable
+= entries
;
1022 for (unsigned i
= 0; i
< num_user_uniforms
; i
++) {
1023 assert(uniforms
[i
].storage
!= NULL
);
1026 assert(parcel
.values
== data_end
);
1029 prog
->NumUserUniformStorage
= num_user_uniforms
;
1030 prog
->NumHiddenUniforms
= hidden_uniforms
;
1031 prog
->UniformStorage
= uniforms
;
1033 link_set_image_access_qualifiers(prog
);
1034 link_set_uniform_initializers(prog
, boolean_true
);