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 if (this->ubo_block_index
!= -1) {
589 this->uniforms
[id
].block_index
= this->ubo_block_index
;
591 const unsigned alignment
= record_type
592 ? record_type
->std140_base_alignment(row_major
)
593 : type
->std140_base_alignment(row_major
);
594 this->ubo_byte_offset
= glsl_align(this->ubo_byte_offset
, alignment
);
595 this->uniforms
[id
].offset
= this->ubo_byte_offset
;
596 this->ubo_byte_offset
+= type
->std140_size(row_major
);
599 this->ubo_byte_offset
= glsl_align(this->ubo_byte_offset
, 16);
601 if (type
->is_array()) {
602 this->uniforms
[id
].array_stride
=
603 glsl_align(type
->fields
.array
->std140_size(row_major
), 16);
605 this->uniforms
[id
].array_stride
= 0;
608 if (type
->without_array()->is_matrix()) {
609 this->uniforms
[id
].matrix_stride
= 16;
610 this->uniforms
[id
].row_major
= row_major
;
612 this->uniforms
[id
].matrix_stride
= 0;
613 this->uniforms
[id
].row_major
= false;
616 this->uniforms
[id
].block_index
= -1;
617 this->uniforms
[id
].offset
= -1;
618 this->uniforms
[id
].array_stride
= -1;
619 this->uniforms
[id
].matrix_stride
= -1;
620 this->uniforms
[id
].row_major
= false;
623 this->values
+= values_for_type(type
);
626 struct string_to_uint_map
*map
;
628 struct gl_uniform_storage
*uniforms
;
629 unsigned next_sampler
;
633 union gl_constant_value
*values
;
635 gl_texture_index targets
[MAX_SAMPLERS
];
638 * Current variable being processed.
640 ir_variable
*current_var
;
643 * Field counter is used to take care that uniform structures
644 * with explicit locations get sequential locations.
646 unsigned field_counter
;
649 * Mask of samplers used by the current shader stage.
651 unsigned shader_samplers_used
;
654 * Mask of samplers used by the current shader stage for shadows.
656 unsigned shader_shadow_samplers
;
660 * Merges a uniform block into an array of uniform blocks that may or
661 * may not already contain a copy of it.
663 * Returns the index of the new block in the array.
666 link_cross_validate_uniform_block(void *mem_ctx
,
667 struct gl_uniform_block
**linked_blocks
,
668 unsigned int *num_linked_blocks
,
669 struct gl_uniform_block
*new_block
)
671 for (unsigned int i
= 0; i
< *num_linked_blocks
; i
++) {
672 struct gl_uniform_block
*old_block
= &(*linked_blocks
)[i
];
674 if (strcmp(old_block
->Name
, new_block
->Name
) == 0)
675 return link_uniform_blocks_are_compatible(old_block
, new_block
)
679 *linked_blocks
= reralloc(mem_ctx
, *linked_blocks
,
680 struct gl_uniform_block
,
681 *num_linked_blocks
+ 1);
682 int linked_block_index
= (*num_linked_blocks
)++;
683 struct gl_uniform_block
*linked_block
= &(*linked_blocks
)[linked_block_index
];
685 memcpy(linked_block
, new_block
, sizeof(*new_block
));
686 linked_block
->Uniforms
= ralloc_array(*linked_blocks
,
687 struct gl_uniform_buffer_variable
,
688 linked_block
->NumUniforms
);
690 memcpy(linked_block
->Uniforms
,
692 sizeof(*linked_block
->Uniforms
) * linked_block
->NumUniforms
);
694 for (unsigned int i
= 0; i
< linked_block
->NumUniforms
; i
++) {
695 struct gl_uniform_buffer_variable
*ubo_var
=
696 &linked_block
->Uniforms
[i
];
698 if (ubo_var
->Name
== ubo_var
->IndexName
) {
699 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
700 ubo_var
->IndexName
= ubo_var
->Name
;
702 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
703 ubo_var
->IndexName
= ralloc_strdup(*linked_blocks
, ubo_var
->IndexName
);
707 return linked_block_index
;
711 * Walks the IR and update the references to uniform blocks in the
712 * ir_variables to point at linked shader's list (previously, they
713 * would point at the uniform block list in one of the pre-linked
717 link_update_uniform_buffer_variables(struct gl_shader
*shader
)
719 foreach_in_list(ir_instruction
, node
, shader
->ir
) {
720 ir_variable
*const var
= node
->as_variable();
722 if ((var
== NULL
) || !var
->is_in_uniform_block())
725 assert(var
->data
.mode
== ir_var_uniform
);
727 if (var
->is_interface_instance()) {
728 var
->data
.location
= 0;
733 char sentinel
= '\0';
735 if (var
->type
->is_record()) {
737 } else if (var
->type
->is_array()
738 && var
->type
->fields
.array
->is_record()) {
742 const unsigned l
= strlen(var
->name
);
743 for (unsigned i
= 0; i
< shader
->NumUniformBlocks
; i
++) {
744 for (unsigned j
= 0; j
< shader
->UniformBlocks
[i
].NumUniforms
; j
++) {
746 const char *begin
= shader
->UniformBlocks
[i
].Uniforms
[j
].Name
;
747 const char *end
= strchr(begin
, sentinel
);
752 if ((ptrdiff_t) l
!= (end
- begin
))
755 if (strncmp(var
->name
, begin
, l
) == 0) {
757 var
->data
.location
= j
;
760 } else if (!strcmp(var
->name
,
761 shader
->UniformBlocks
[i
].Uniforms
[j
].Name
)) {
763 var
->data
.location
= j
;
775 link_assign_uniform_block_offsets(struct gl_shader
*shader
)
777 for (unsigned b
= 0; b
< shader
->NumUniformBlocks
; b
++) {
778 struct gl_uniform_block
*block
= &shader
->UniformBlocks
[b
];
781 for (unsigned int i
= 0; i
< block
->NumUniforms
; i
++) {
782 struct gl_uniform_buffer_variable
*ubo_var
= &block
->Uniforms
[i
];
783 const struct glsl_type
*type
= ubo_var
->Type
;
785 unsigned alignment
= type
->std140_base_alignment(ubo_var
->RowMajor
);
786 unsigned size
= type
->std140_size(ubo_var
->RowMajor
);
788 offset
= glsl_align(offset
, alignment
);
789 ubo_var
->Offset
= offset
;
793 /* From the GL_ARB_uniform_buffer_object spec:
795 * "For uniform blocks laid out according to [std140] rules,
796 * the minimum buffer object size returned by the
797 * UNIFORM_BLOCK_DATA_SIZE query is derived by taking the
798 * offset of the last basic machine unit consumed by the
799 * last uniform of the uniform block (including any
800 * end-of-array or end-of-structure padding), adding one,
801 * and rounding up to the next multiple of the base
802 * alignment required for a vec4."
804 block
->UniformBufferSize
= glsl_align(offset
, 16);
809 * Scan the program for image uniforms and store image unit access
810 * information into the gl_shader data structure.
813 link_set_image_access_qualifiers(struct gl_shader_program
*prog
)
815 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
816 gl_shader
*sh
= prog
->_LinkedShaders
[i
];
821 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
822 ir_variable
*var
= node
->as_variable();
824 if (var
&& var
->data
.mode
== ir_var_uniform
&&
825 var
->type
->contains_image()) {
827 bool found
= prog
->UniformHash
->get(id
, var
->name
);
830 const gl_uniform_storage
*storage
= &prog
->UniformStorage
[id
];
831 const unsigned index
= storage
->image
[i
].index
;
832 const GLenum access
= (var
->data
.image_read_only
? GL_READ_ONLY
:
833 var
->data
.image_write_only
? GL_WRITE_ONLY
:
836 for (unsigned j
= 0; j
< MAX2(1, storage
->array_elements
); ++j
)
837 sh
->ImageAccess
[index
+ j
] = access
;
844 link_assign_uniform_locations(struct gl_shader_program
*prog
,
845 unsigned int boolean_true
)
847 ralloc_free(prog
->UniformStorage
);
848 prog
->UniformStorage
= NULL
;
849 prog
->NumUserUniformStorage
= 0;
851 if (prog
->UniformHash
!= NULL
) {
852 prog
->UniformHash
->clear();
854 prog
->UniformHash
= new string_to_uint_map
;
857 /* First pass: Count the uniform resources used by the user-defined
858 * uniforms. While this happens, each active uniform will have an index
861 * Note: this is *NOT* the index that is returned to the application by
862 * glGetUniformLocation.
864 count_uniform_size
uniform_size(prog
->UniformHash
);
865 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
866 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
871 /* Uniforms that lack an initializer in the shader code have an initial
872 * value of zero. This includes sampler uniforms.
874 * Page 24 (page 30 of the PDF) of the GLSL 1.20 spec says:
876 * "The link time initial value is either the value of the variable's
877 * initializer, if present, or 0 if no initializer is present. Sampler
878 * types cannot have initializers."
880 memset(sh
->SamplerUnits
, 0, sizeof(sh
->SamplerUnits
));
881 memset(sh
->ImageUnits
, 0, sizeof(sh
->ImageUnits
));
883 link_update_uniform_buffer_variables(sh
);
885 /* Reset various per-shader target counts.
887 uniform_size
.start_shader();
889 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
890 ir_variable
*const var
= node
->as_variable();
892 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
))
895 /* FINISHME: Update code to process built-in uniforms!
897 if (is_gl_identifier(var
->name
)) {
898 uniform_size
.num_shader_uniform_components
+=
899 var
->type
->component_slots();
903 uniform_size
.process(var
);
906 sh
->num_samplers
= uniform_size
.num_shader_samplers
;
907 sh
->NumImages
= uniform_size
.num_shader_images
;
908 sh
->num_uniform_components
= uniform_size
.num_shader_uniform_components
;
910 sh
->num_combined_uniform_components
= sh
->num_uniform_components
;
911 for (unsigned i
= 0; i
< sh
->NumUniformBlocks
; i
++) {
912 sh
->num_combined_uniform_components
+=
913 sh
->UniformBlocks
[i
].UniformBufferSize
/ 4;
917 const unsigned num_user_uniforms
= uniform_size
.num_active_uniforms
;
918 const unsigned num_data_slots
= uniform_size
.num_values
;
920 /* On the outside chance that there were no uniforms, bail out.
922 if (num_user_uniforms
== 0)
925 struct gl_uniform_storage
*uniforms
=
926 rzalloc_array(prog
, struct gl_uniform_storage
, num_user_uniforms
);
927 union gl_constant_value
*data
=
928 rzalloc_array(uniforms
, union gl_constant_value
, num_data_slots
);
930 union gl_constant_value
*data_end
= &data
[num_data_slots
];
933 parcel_out_uniform_storage
parcel(prog
->UniformHash
, uniforms
, data
);
935 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
936 if (prog
->_LinkedShaders
[i
] == NULL
)
939 parcel
.start_shader((gl_shader_stage
)i
);
941 foreach_in_list(ir_instruction
, node
, prog
->_LinkedShaders
[i
]->ir
) {
942 ir_variable
*const var
= node
->as_variable();
944 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
))
947 /* FINISHME: Update code to process built-in uniforms!
949 if (is_gl_identifier(var
->name
))
952 parcel
.set_and_process(prog
, var
);
955 prog
->_LinkedShaders
[i
]->active_samplers
= parcel
.shader_samplers_used
;
956 prog
->_LinkedShaders
[i
]->shadow_samplers
= parcel
.shader_shadow_samplers
;
958 STATIC_ASSERT(sizeof(prog
->_LinkedShaders
[i
]->SamplerTargets
) == sizeof(parcel
.targets
));
959 memcpy(prog
->_LinkedShaders
[i
]->SamplerTargets
, parcel
.targets
,
960 sizeof(prog
->_LinkedShaders
[i
]->SamplerTargets
));
963 /* Reserve all the explicit locations of the active uniforms. */
964 for (unsigned i
= 0; i
< num_user_uniforms
; i
++) {
965 if (uniforms
[i
].remap_location
!= UNMAPPED_UNIFORM_LOC
) {
966 /* How many new entries for this uniform? */
967 const unsigned entries
= MAX2(1, uniforms
[i
].array_elements
);
969 /* Set remap table entries point to correct gl_uniform_storage. */
970 for (unsigned j
= 0; j
< entries
; j
++) {
971 unsigned element_loc
= uniforms
[i
].remap_location
+ j
;
972 assert(prog
->UniformRemapTable
[element_loc
] ==
973 INACTIVE_UNIFORM_EXPLICIT_LOCATION
);
974 prog
->UniformRemapTable
[element_loc
] = &uniforms
[i
];
979 /* Reserve locations for rest of the uniforms. */
980 for (unsigned i
= 0; i
< num_user_uniforms
; i
++) {
982 /* Explicit ones have been set already. */
983 if (uniforms
[i
].remap_location
!= UNMAPPED_UNIFORM_LOC
)
986 /* how many new entries for this uniform? */
987 const unsigned entries
= MAX2(1, uniforms
[i
].array_elements
);
989 /* resize remap table to fit new entries */
990 prog
->UniformRemapTable
=
992 prog
->UniformRemapTable
,
993 gl_uniform_storage
*,
994 prog
->NumUniformRemapTable
+ entries
);
996 /* set pointers for this uniform */
997 for (unsigned j
= 0; j
< entries
; j
++)
998 prog
->UniformRemapTable
[prog
->NumUniformRemapTable
+j
] = &uniforms
[i
];
1000 /* set the base location in remap table for the uniform */
1001 uniforms
[i
].remap_location
= prog
->NumUniformRemapTable
;
1003 prog
->NumUniformRemapTable
+= entries
;
1007 for (unsigned i
= 0; i
< num_user_uniforms
; i
++) {
1008 assert(uniforms
[i
].storage
!= NULL
);
1011 assert(parcel
.values
== data_end
);
1014 prog
->NumUserUniformStorage
= num_user_uniforms
;
1015 prog
->UniformStorage
= uniforms
;
1017 link_set_image_access_qualifiers(prog
);
1018 link_set_uniform_initializers(prog
, boolean_true
);