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
);
67 ralloc_free(name_copy
);
71 program_resource_visitor::process(ir_variable
*var
)
73 const glsl_type
*t
= var
->type
;
75 /* false is always passed for the row_major parameter to the other
76 * processing functions because no information is available to do
77 * otherwise. See the warning in linker.h.
80 /* Only strdup the name if we actually will need to modify it. */
81 if (var
->data
.from_named_ifc_block_array
) {
82 /* lower_named_interface_blocks created this variable by lowering an
83 * interface block array to an array variable. For example if the
84 * original source code was:
86 * out Blk { vec4 bar } foo[3];
88 * Then the variable is now:
92 * We need to visit each array element using the names constructed like
99 assert(t
->is_array());
100 const glsl_type
*ifc_type
= var
->get_interface_type();
101 char *name
= ralloc_strdup(NULL
, ifc_type
->name
);
102 size_t name_length
= strlen(name
);
103 for (unsigned i
= 0; i
< t
->length
; i
++) {
104 size_t new_length
= name_length
;
105 ralloc_asprintf_rewrite_tail(&name
, &new_length
, "[%u].%s", i
,
107 /* Note: row_major is only meaningful for uniform blocks, and
108 * lowering is only applied to non-uniform interface blocks, so we
109 * can safely pass false for row_major.
111 recursion(var
->type
, &name
, new_length
, false, NULL
);
114 } else if (var
->data
.from_named_ifc_block_nonarray
) {
115 /* lower_named_interface_blocks created this variable by lowering a
116 * named interface block (non-array) to an ordinary variable. For
117 * example if the original source code was:
119 * out Blk { vec4 bar } foo;
121 * Then the variable is now:
125 * We need to visit this variable using the name:
129 const glsl_type
*ifc_type
= var
->get_interface_type();
130 char *name
= ralloc_asprintf(NULL
, "%s.%s", ifc_type
->name
, var
->name
);
131 /* Note: row_major is only meaningful for uniform blocks, and lowering
132 * is only applied to non-uniform interface blocks, so we can safely
133 * pass false for row_major.
135 recursion(var
->type
, &name
, strlen(name
), false, NULL
);
137 } else if (t
->without_array()->is_record()) {
138 char *name
= ralloc_strdup(NULL
, var
->name
);
139 recursion(var
->type
, &name
, strlen(name
), false, NULL
);
141 } else if (t
->is_interface()) {
142 char *name
= ralloc_strdup(NULL
, var
->type
->name
);
143 recursion(var
->type
, &name
, strlen(name
), false, NULL
);
145 } else if (t
->is_array() && t
->fields
.array
->is_interface()) {
146 char *name
= ralloc_strdup(NULL
, var
->type
->fields
.array
->name
);
147 recursion(var
->type
, &name
, strlen(name
), false, NULL
);
150 this->visit_field(t
, var
->name
, false, NULL
);
155 program_resource_visitor::recursion(const glsl_type
*t
, char **name
,
156 size_t name_length
, bool row_major
,
157 const glsl_type
*record_type
)
159 /* Records need to have each field processed individually.
161 * Arrays of records need to have each array element processed
162 * individually, then each field of the resulting array elements processed
165 if (t
->is_record() || t
->is_interface()) {
166 if (record_type
== NULL
&& t
->is_record())
169 for (unsigned i
= 0; i
< t
->length
; i
++) {
170 const char *field
= t
->fields
.structure
[i
].name
;
171 size_t new_length
= name_length
;
173 if (t
->fields
.structure
[i
].type
->is_record())
174 this->visit_field(&t
->fields
.structure
[i
]);
176 /* Append '.field' to the current variable name. */
177 if (name_length
== 0) {
178 ralloc_asprintf_rewrite_tail(name
, &new_length
, "%s", field
);
180 ralloc_asprintf_rewrite_tail(name
, &new_length
, ".%s", field
);
183 recursion(t
->fields
.structure
[i
].type
, name
, new_length
,
184 t
->fields
.structure
[i
].row_major
, record_type
);
186 /* Only the first leaf-field of the record gets called with the
187 * record type pointer.
191 } else if (t
->is_array() && (t
->fields
.array
->is_record()
192 || t
->fields
.array
->is_interface())) {
193 if (record_type
== NULL
&& t
->fields
.array
->is_record())
194 record_type
= t
->fields
.array
;
196 for (unsigned i
= 0; i
< t
->length
; i
++) {
197 size_t new_length
= name_length
;
199 /* Append the subscript to the current variable name */
200 ralloc_asprintf_rewrite_tail(name
, &new_length
, "[%u]", i
);
202 recursion(t
->fields
.array
, name
, new_length
, row_major
,
205 /* Only the first leaf-field of the record gets called with the
206 * record type pointer.
211 this->visit_field(t
, *name
, row_major
, record_type
);
216 program_resource_visitor::visit_field(const glsl_type
*type
, const char *name
,
220 visit_field(type
, name
, row_major
);
224 program_resource_visitor::visit_field(const glsl_struct_field
*field
)
233 * Class to help calculate the storage requirements for a set of uniforms
235 * As uniforms are added to the active set the number of active uniforms and
236 * the storage requirements for those uniforms are accumulated. The active
237 * uniforms are added the the hash table supplied to the constructor.
239 * If the same uniform is added multiple times (i.e., once for each shader
240 * target), it will only be accounted once.
242 class count_uniform_size
: public program_resource_visitor
{
244 count_uniform_size(struct string_to_uint_map
*map
)
245 : num_active_uniforms(0), num_values(0), num_shader_samplers(0),
246 num_shader_images(0), num_shader_uniform_components(0),
247 is_ubo_var(false), map(map
)
254 this->num_shader_samplers
= 0;
255 this->num_shader_images
= 0;
256 this->num_shader_uniform_components
= 0;
259 void process(ir_variable
*var
)
261 this->is_ubo_var
= var
->is_in_uniform_block();
262 if (var
->is_interface_instance())
263 program_resource_visitor::process(var
->get_interface_type(),
264 var
->get_interface_type()->name
);
266 program_resource_visitor::process(var
);
270 * Total number of active uniforms counted
272 unsigned num_active_uniforms
;
275 * Number of data values required to back the storage for the active uniforms
280 * Number of samplers used
282 unsigned num_shader_samplers
;
285 * Number of images used
287 unsigned num_shader_images
;
290 * Number of uniforms used in the current shader
292 unsigned num_shader_uniform_components
;
297 virtual void visit_field(const glsl_type
*type
, const char *name
,
300 assert(!type
->without_array()->is_record());
301 assert(!type
->without_array()->is_interface());
305 /* Count the number of samplers regardless of whether the uniform is
306 * already in the hash table. The hash table prevents adding the same
307 * uniform for multiple shader targets, but in this case we want to
308 * count it for each shader target.
310 const unsigned values
= values_for_type(type
);
311 if (type
->contains_sampler()) {
312 this->num_shader_samplers
+= values
;
313 } else if (type
->contains_image()) {
314 this->num_shader_images
+= values
;
316 /* As drivers are likely to represent image uniforms as
317 * scalar indices, count them against the limit of uniform
318 * components in the default block. The spec allows image
319 * uniforms to use up no more than one scalar slot.
321 this->num_shader_uniform_components
+= values
;
323 /* Accumulate the total number of uniform slots used by this shader.
324 * Note that samplers do not count against this limit because they
325 * don't use any storage on current hardware.
328 this->num_shader_uniform_components
+= values
;
331 /* If the uniform is already in the map, there's nothing more to do.
334 if (this->map
->get(id
, name
))
337 this->map
->put(this->num_active_uniforms
, name
);
339 /* Each leaf uniform occupies one entry in the list of active
342 this->num_active_uniforms
++;
343 this->num_values
+= values
;
346 struct string_to_uint_map
*map
;
349 } /* anonymous namespace */
352 * Class to help parcel out pieces of backing storage to uniforms
354 * Each uniform processed has some range of the \c gl_constant_value
355 * structures associated with it. The association is done by finding
356 * the uniform in the \c string_to_uint_map and using the value from
357 * the map to connect that slot in the \c gl_uniform_storage table
358 * with the next available slot in the \c gl_constant_value array.
361 * This class assumes that every uniform that will be processed is
362 * already in the \c string_to_uint_map. In addition, it assumes that
363 * the \c gl_uniform_storage and \c gl_constant_value arrays are "big
366 class parcel_out_uniform_storage
: public program_resource_visitor
{
368 parcel_out_uniform_storage(struct string_to_uint_map
*map
,
369 struct gl_uniform_storage
*uniforms
,
370 union gl_constant_value
*values
)
371 : map(map
), uniforms(uniforms
), values(values
)
375 void start_shader(gl_shader_stage shader_type
)
377 assert(shader_type
< MESA_SHADER_STAGES
);
378 this->shader_type
= shader_type
;
380 this->shader_samplers_used
= 0;
381 this->shader_shadow_samplers
= 0;
382 this->next_sampler
= 0;
383 this->next_image
= 0;
384 memset(this->targets
, 0, sizeof(this->targets
));
387 void set_and_process(struct gl_shader_program
*prog
,
393 ubo_block_index
= -1;
394 if (var
->is_in_uniform_block()) {
395 if (var
->is_interface_instance() && var
->type
->is_array()) {
396 unsigned l
= strlen(var
->get_interface_type()->name
);
398 for (unsigned i
= 0; i
< prog
->NumUniformBlocks
; i
++) {
399 if (strncmp(var
->get_interface_type()->name
,
400 prog
->UniformBlocks
[i
].Name
,
402 && prog
->UniformBlocks
[i
].Name
[l
] == '[') {
408 for (unsigned i
= 0; i
< prog
->NumUniformBlocks
; i
++) {
409 if (strcmp(var
->get_interface_type()->name
,
410 prog
->UniformBlocks
[i
].Name
) == 0) {
416 assert(ubo_block_index
!= -1);
418 /* Uniform blocks that were specified with an instance name must be
419 * handled a little bit differently. The name of the variable is the
420 * name used to reference the uniform block instead of being the name
421 * of a variable within the block. Therefore, searching for the name
422 * within the block will fail.
424 if (var
->is_interface_instance()) {
426 ubo_row_major
= false;
428 const struct gl_uniform_block
*const block
=
429 &prog
->UniformBlocks
[ubo_block_index
];
431 assert(var
->data
.location
!= -1);
433 const struct gl_uniform_buffer_variable
*const ubo_var
=
434 &block
->Uniforms
[var
->data
.location
];
436 ubo_row_major
= ubo_var
->RowMajor
;
437 ubo_byte_offset
= ubo_var
->Offset
;
440 if (var
->is_interface_instance())
441 process(var
->get_interface_type(),
442 var
->get_interface_type()->name
);
452 gl_shader_stage shader_type
;
455 void handle_samplers(const glsl_type
*base_type
,
456 struct gl_uniform_storage
*uniform
)
458 if (base_type
->is_sampler()) {
459 uniform
->sampler
[shader_type
].index
= this->next_sampler
;
460 uniform
->sampler
[shader_type
].active
= true;
462 /* Increment the sampler by 1 for non-arrays and by the number of
463 * array elements for arrays.
465 this->next_sampler
+=
466 MAX2(1, uniform
->array_elements
);
468 const gl_texture_index target
= base_type
->sampler_index();
469 const unsigned shadow
= base_type
->sampler_shadow
;
470 for (unsigned i
= uniform
->sampler
[shader_type
].index
;
471 i
< MIN2(this->next_sampler
, MAX_SAMPLERS
);
473 this->targets
[i
] = target
;
474 this->shader_samplers_used
|= 1U << i
;
475 this->shader_shadow_samplers
|= shadow
<< i
;
478 uniform
->sampler
[shader_type
].index
= ~0;
479 uniform
->sampler
[shader_type
].active
= false;
483 void handle_images(const glsl_type
*base_type
,
484 struct gl_uniform_storage
*uniform
)
486 if (base_type
->is_image()) {
487 uniform
->image
[shader_type
].index
= this->next_image
;
488 uniform
->image
[shader_type
].active
= true;
490 /* Increment the image index by 1 for non-arrays and by the
491 * number of array elements for arrays.
493 this->next_image
+= MAX2(1, uniform
->array_elements
);
496 uniform
->image
[shader_type
].index
= ~0;
497 uniform
->image
[shader_type
].active
= false;
501 virtual void visit_field(const glsl_type
*type
, const char *name
,
507 assert(!"Should not get here.");
510 virtual void visit_field(const glsl_type
*type
, const char *name
,
511 bool row_major
, const glsl_type
*record_type
)
513 assert(!type
->without_array()->is_record());
514 assert(!type
->without_array()->is_interface());
519 bool found
= this->map
->get(id
, name
);
525 const glsl_type
*base_type
;
526 if (type
->is_array()) {
527 this->uniforms
[id
].array_elements
= type
->length
;
528 base_type
= type
->fields
.array
;
530 this->uniforms
[id
].array_elements
= 0;
534 /* This assigns uniform indices to sampler and image uniforms. */
535 handle_samplers(base_type
, &this->uniforms
[id
]);
536 handle_images(base_type
, &this->uniforms
[id
]);
538 /* If there is already storage associated with this uniform, it means
539 * that it was set while processing an earlier shader stage. For
540 * example, we may be processing the uniform in the fragment shader, but
541 * the uniform was already processed in the vertex shader.
543 if (this->uniforms
[id
].storage
!= NULL
) {
547 /* Assign explicit locations. */
548 if (current_var
->data
.explicit_location
) {
549 /* Set sequential locations for struct fields. */
550 if (record_type
!= NULL
) {
551 const unsigned entries
= MAX2(1, this->uniforms
[id
].array_elements
);
552 this->uniforms
[id
].remap_location
=
553 current_var
->data
.location
+ field_counter
;
554 field_counter
+= entries
;
556 this->uniforms
[id
].remap_location
= current_var
->data
.location
;
559 /* Initialize to to indicate that no location is set */
560 this->uniforms
[id
].remap_location
= UNMAPPED_UNIFORM_LOC
;
563 this->uniforms
[id
].name
= ralloc_strdup(this->uniforms
, name
);
564 this->uniforms
[id
].type
= base_type
;
565 this->uniforms
[id
].initialized
= 0;
566 this->uniforms
[id
].num_driver_storage
= 0;
567 this->uniforms
[id
].driver_storage
= NULL
;
568 this->uniforms
[id
].storage
= this->values
;
569 this->uniforms
[id
].atomic_buffer_index
= -1;
570 if (this->ubo_block_index
!= -1) {
571 this->uniforms
[id
].block_index
= this->ubo_block_index
;
573 const unsigned alignment
= record_type
574 ? record_type
->std140_base_alignment(ubo_row_major
)
575 : type
->std140_base_alignment(ubo_row_major
);
576 this->ubo_byte_offset
= glsl_align(this->ubo_byte_offset
, alignment
);
577 this->uniforms
[id
].offset
= this->ubo_byte_offset
;
578 this->ubo_byte_offset
+= type
->std140_size(ubo_row_major
);
580 if (type
->is_array()) {
581 this->uniforms
[id
].array_stride
=
582 glsl_align(type
->fields
.array
->std140_size(ubo_row_major
), 16);
584 this->uniforms
[id
].array_stride
= 0;
587 if (type
->without_array()->is_matrix()) {
588 this->uniforms
[id
].matrix_stride
= 16;
589 this->uniforms
[id
].row_major
= ubo_row_major
;
591 this->uniforms
[id
].matrix_stride
= 0;
592 this->uniforms
[id
].row_major
= false;
595 this->uniforms
[id
].block_index
= -1;
596 this->uniforms
[id
].offset
= -1;
597 this->uniforms
[id
].array_stride
= -1;
598 this->uniforms
[id
].matrix_stride
= -1;
599 this->uniforms
[id
].row_major
= false;
602 this->values
+= values_for_type(type
);
605 struct string_to_uint_map
*map
;
607 struct gl_uniform_storage
*uniforms
;
608 unsigned next_sampler
;
612 union gl_constant_value
*values
;
614 gl_texture_index targets
[MAX_SAMPLERS
];
617 * Current variable being processed.
619 ir_variable
*current_var
;
622 * Field counter is used to take care that uniform structures
623 * with explicit locations get sequential locations.
625 unsigned field_counter
;
628 * Mask of samplers used by the current shader stage.
630 unsigned shader_samplers_used
;
633 * Mask of samplers used by the current shader stage for shadows.
635 unsigned shader_shadow_samplers
;
639 * Merges a uniform block into an array of uniform blocks that may or
640 * may not already contain a copy of it.
642 * Returns the index of the new block in the array.
645 link_cross_validate_uniform_block(void *mem_ctx
,
646 struct gl_uniform_block
**linked_blocks
,
647 unsigned int *num_linked_blocks
,
648 struct gl_uniform_block
*new_block
)
650 for (unsigned int i
= 0; i
< *num_linked_blocks
; i
++) {
651 struct gl_uniform_block
*old_block
= &(*linked_blocks
)[i
];
653 if (strcmp(old_block
->Name
, new_block
->Name
) == 0)
654 return link_uniform_blocks_are_compatible(old_block
, new_block
)
658 *linked_blocks
= reralloc(mem_ctx
, *linked_blocks
,
659 struct gl_uniform_block
,
660 *num_linked_blocks
+ 1);
661 int linked_block_index
= (*num_linked_blocks
)++;
662 struct gl_uniform_block
*linked_block
= &(*linked_blocks
)[linked_block_index
];
664 memcpy(linked_block
, new_block
, sizeof(*new_block
));
665 linked_block
->Uniforms
= ralloc_array(*linked_blocks
,
666 struct gl_uniform_buffer_variable
,
667 linked_block
->NumUniforms
);
669 memcpy(linked_block
->Uniforms
,
671 sizeof(*linked_block
->Uniforms
) * linked_block
->NumUniforms
);
673 for (unsigned int i
= 0; i
< linked_block
->NumUniforms
; i
++) {
674 struct gl_uniform_buffer_variable
*ubo_var
=
675 &linked_block
->Uniforms
[i
];
677 if (ubo_var
->Name
== ubo_var
->IndexName
) {
678 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
679 ubo_var
->IndexName
= ubo_var
->Name
;
681 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
682 ubo_var
->IndexName
= ralloc_strdup(*linked_blocks
, ubo_var
->IndexName
);
686 return linked_block_index
;
690 * Walks the IR and update the references to uniform blocks in the
691 * ir_variables to point at linked shader's list (previously, they
692 * would point at the uniform block list in one of the pre-linked
696 link_update_uniform_buffer_variables(struct gl_shader
*shader
)
698 foreach_in_list(ir_instruction
, node
, shader
->ir
) {
699 ir_variable
*const var
= node
->as_variable();
701 if ((var
== NULL
) || !var
->is_in_uniform_block())
704 assert(var
->data
.mode
== ir_var_uniform
);
706 if (var
->is_interface_instance()) {
707 var
->data
.location
= 0;
712 char sentinel
= '\0';
714 if (var
->type
->is_record()) {
716 } else if (var
->type
->is_array()
717 && var
->type
->fields
.array
->is_record()) {
721 const unsigned l
= strlen(var
->name
);
722 for (unsigned i
= 0; i
< shader
->NumUniformBlocks
; i
++) {
723 for (unsigned j
= 0; j
< shader
->UniformBlocks
[i
].NumUniforms
; j
++) {
725 const char *begin
= shader
->UniformBlocks
[i
].Uniforms
[j
].Name
;
726 const char *end
= strchr(begin
, sentinel
);
731 if (l
!= (end
- begin
))
734 if (strncmp(var
->name
, begin
, l
) == 0) {
736 var
->data
.location
= j
;
739 } else if (!strcmp(var
->name
,
740 shader
->UniformBlocks
[i
].Uniforms
[j
].Name
)) {
742 var
->data
.location
= j
;
754 link_assign_uniform_block_offsets(struct gl_shader
*shader
)
756 for (unsigned b
= 0; b
< shader
->NumUniformBlocks
; b
++) {
757 struct gl_uniform_block
*block
= &shader
->UniformBlocks
[b
];
760 for (unsigned int i
= 0; i
< block
->NumUniforms
; i
++) {
761 struct gl_uniform_buffer_variable
*ubo_var
= &block
->Uniforms
[i
];
762 const struct glsl_type
*type
= ubo_var
->Type
;
764 unsigned alignment
= type
->std140_base_alignment(ubo_var
->RowMajor
);
765 unsigned size
= type
->std140_size(ubo_var
->RowMajor
);
767 offset
= glsl_align(offset
, alignment
);
768 ubo_var
->Offset
= offset
;
772 /* From the GL_ARB_uniform_buffer_object spec:
774 * "For uniform blocks laid out according to [std140] rules,
775 * the minimum buffer object size returned by the
776 * UNIFORM_BLOCK_DATA_SIZE query is derived by taking the
777 * offset of the last basic machine unit consumed by the
778 * last uniform of the uniform block (including any
779 * end-of-array or end-of-structure padding), adding one,
780 * and rounding up to the next multiple of the base
781 * alignment required for a vec4."
783 block
->UniformBufferSize
= glsl_align(offset
, 16);
788 * Scan the program for image uniforms and store image unit access
789 * information into the gl_shader data structure.
792 link_set_image_access_qualifiers(struct gl_shader_program
*prog
)
794 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
795 gl_shader
*sh
= prog
->_LinkedShaders
[i
];
800 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
801 ir_variable
*var
= node
->as_variable();
803 if (var
&& var
->data
.mode
== ir_var_uniform
&&
804 var
->type
->contains_image()) {
806 bool found
= prog
->UniformHash
->get(id
, var
->name
);
809 const gl_uniform_storage
*storage
= &prog
->UniformStorage
[id
];
810 const unsigned index
= storage
->image
[i
].index
;
811 const GLenum access
= (var
->data
.image
.read_only
? GL_READ_ONLY
:
812 var
->data
.image
.write_only
? GL_WRITE_ONLY
:
815 for (unsigned j
= 0; j
< MAX2(1, storage
->array_elements
); ++j
)
816 sh
->ImageAccess
[index
+ j
] = access
;
823 link_assign_uniform_locations(struct gl_shader_program
*prog
)
825 ralloc_free(prog
->UniformStorage
);
826 prog
->UniformStorage
= NULL
;
827 prog
->NumUserUniformStorage
= 0;
829 if (prog
->UniformHash
!= NULL
) {
830 prog
->UniformHash
->clear();
832 prog
->UniformHash
= new string_to_uint_map
;
835 /* First pass: Count the uniform resources used by the user-defined
836 * uniforms. While this happens, each active uniform will have an index
839 * Note: this is *NOT* the index that is returned to the application by
840 * glGetUniformLocation.
842 count_uniform_size
uniform_size(prog
->UniformHash
);
843 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
844 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
849 /* Uniforms that lack an initializer in the shader code have an initial
850 * value of zero. This includes sampler uniforms.
852 * Page 24 (page 30 of the PDF) of the GLSL 1.20 spec says:
854 * "The link time initial value is either the value of the variable's
855 * initializer, if present, or 0 if no initializer is present. Sampler
856 * types cannot have initializers."
858 memset(sh
->SamplerUnits
, 0, sizeof(sh
->SamplerUnits
));
859 memset(sh
->ImageUnits
, 0, sizeof(sh
->ImageUnits
));
861 link_update_uniform_buffer_variables(sh
);
863 /* Reset various per-shader target counts.
865 uniform_size
.start_shader();
867 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
868 ir_variable
*const var
= node
->as_variable();
870 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
))
873 /* FINISHME: Update code to process built-in uniforms!
875 if (is_gl_identifier(var
->name
)) {
876 uniform_size
.num_shader_uniform_components
+=
877 var
->type
->component_slots();
881 uniform_size
.process(var
);
884 sh
->num_samplers
= uniform_size
.num_shader_samplers
;
885 sh
->NumImages
= uniform_size
.num_shader_images
;
886 sh
->num_uniform_components
= uniform_size
.num_shader_uniform_components
;
888 sh
->num_combined_uniform_components
= sh
->num_uniform_components
;
889 for (unsigned i
= 0; i
< sh
->NumUniformBlocks
; i
++) {
890 sh
->num_combined_uniform_components
+=
891 sh
->UniformBlocks
[i
].UniformBufferSize
/ 4;
895 const unsigned num_user_uniforms
= uniform_size
.num_active_uniforms
;
896 const unsigned num_data_slots
= uniform_size
.num_values
;
898 /* On the outside chance that there were no uniforms, bail out.
900 if (num_user_uniforms
== 0)
903 struct gl_uniform_storage
*uniforms
=
904 rzalloc_array(prog
, struct gl_uniform_storage
, num_user_uniforms
);
905 union gl_constant_value
*data
=
906 rzalloc_array(uniforms
, union gl_constant_value
, num_data_slots
);
908 union gl_constant_value
*data_end
= &data
[num_data_slots
];
911 parcel_out_uniform_storage
parcel(prog
->UniformHash
, uniforms
, data
);
913 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
914 if (prog
->_LinkedShaders
[i
] == NULL
)
917 parcel
.start_shader((gl_shader_stage
)i
);
919 foreach_in_list(ir_instruction
, node
, prog
->_LinkedShaders
[i
]->ir
) {
920 ir_variable
*const var
= node
->as_variable();
922 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
))
925 /* FINISHME: Update code to process built-in uniforms!
927 if (is_gl_identifier(var
->name
))
930 parcel
.set_and_process(prog
, var
);
933 prog
->_LinkedShaders
[i
]->active_samplers
= parcel
.shader_samplers_used
;
934 prog
->_LinkedShaders
[i
]->shadow_samplers
= parcel
.shader_shadow_samplers
;
936 STATIC_ASSERT(sizeof(prog
->_LinkedShaders
[i
]->SamplerTargets
) == sizeof(parcel
.targets
));
937 memcpy(prog
->_LinkedShaders
[i
]->SamplerTargets
, parcel
.targets
,
938 sizeof(prog
->_LinkedShaders
[i
]->SamplerTargets
));
941 /* Reserve all the explicit locations of the active uniforms. */
942 for (unsigned i
= 0; i
< num_user_uniforms
; i
++) {
943 if (uniforms
[i
].remap_location
!= UNMAPPED_UNIFORM_LOC
) {
944 /* How many new entries for this uniform? */
945 const unsigned entries
= MAX2(1, uniforms
[i
].array_elements
);
947 /* Set remap table entries point to correct gl_uniform_storage. */
948 for (unsigned j
= 0; j
< entries
; j
++) {
949 unsigned element_loc
= uniforms
[i
].remap_location
+ j
;
950 assert(prog
->UniformRemapTable
[element_loc
] ==
951 INACTIVE_UNIFORM_EXPLICIT_LOCATION
);
952 prog
->UniformRemapTable
[element_loc
] = &uniforms
[i
];
957 /* Reserve locations for rest of the uniforms. */
958 for (unsigned i
= 0; i
< num_user_uniforms
; i
++) {
960 /* Explicit ones have been set already. */
961 if (uniforms
[i
].remap_location
!= UNMAPPED_UNIFORM_LOC
)
964 /* how many new entries for this uniform? */
965 const unsigned entries
= MAX2(1, uniforms
[i
].array_elements
);
967 /* resize remap table to fit new entries */
968 prog
->UniformRemapTable
=
970 prog
->UniformRemapTable
,
971 gl_uniform_storage
*,
972 prog
->NumUniformRemapTable
+ entries
);
974 /* set pointers for this uniform */
975 for (unsigned j
= 0; j
< entries
; j
++)
976 prog
->UniformRemapTable
[prog
->NumUniformRemapTable
+j
] = &uniforms
[i
];
978 /* set the base location in remap table for the uniform */
979 uniforms
[i
].remap_location
= prog
->NumUniformRemapTable
;
981 prog
->NumUniformRemapTable
+= entries
;
985 for (unsigned i
= 0; i
< num_user_uniforms
; i
++) {
986 assert(uniforms
[i
].storage
!= NULL
);
989 assert(parcel
.values
== data_end
);
992 prog
->NumUserUniformStorage
= num_user_uniforms
;
993 prog
->UniformStorage
= uniforms
;
995 link_set_image_access_qualifiers(prog
);
996 link_set_uniform_initializers(prog
);