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 * Count the backing storage requirements for a type
43 values_for_type(const glsl_type
*type
)
45 if (type
->is_sampler()) {
47 } else if (type
->is_array() && type
->fields
.array
->is_sampler()) {
48 return type
->array_size();
50 return type
->component_slots();
55 program_resource_visitor::process(const glsl_type
*type
, const char *name
)
57 assert(type
->is_record()
58 || (type
->is_array() && type
->fields
.array
->is_record())
59 || type
->is_interface()
60 || (type
->is_array() && type
->fields
.array
->is_interface()));
62 char *name_copy
= ralloc_strdup(NULL
, name
);
63 recursion(type
, &name_copy
, strlen(name
), false, NULL
);
64 ralloc_free(name_copy
);
68 program_resource_visitor::process(ir_variable
*var
)
70 const glsl_type
*t
= var
->type
;
72 /* false is always passed for the row_major parameter to the other
73 * processing functions because no information is available to do
74 * otherwise. See the warning in linker.h.
77 /* Only strdup the name if we actually will need to modify it. */
78 if (var
->data
.from_named_ifc_block_array
) {
79 /* lower_named_interface_blocks created this variable by lowering an
80 * interface block array to an array variable. For example if the
81 * original source code was:
83 * out Blk { vec4 bar } foo[3];
85 * Then the variable is now:
89 * We need to visit each array element using the names constructed like
96 assert(t
->is_array());
97 const glsl_type
*ifc_type
= var
->get_interface_type();
98 char *name
= ralloc_strdup(NULL
, ifc_type
->name
);
99 size_t name_length
= strlen(name
);
100 for (unsigned i
= 0; i
< t
->length
; i
++) {
101 size_t new_length
= name_length
;
102 ralloc_asprintf_rewrite_tail(&name
, &new_length
, "[%u].%s", i
,
104 /* Note: row_major is only meaningful for uniform blocks, and
105 * lowering is only applied to non-uniform interface blocks, so we
106 * can safely pass false for row_major.
108 recursion(var
->type
, &name
, new_length
, false, NULL
);
111 } else if (var
->data
.from_named_ifc_block_nonarray
) {
112 /* lower_named_interface_blocks created this variable by lowering a
113 * named interface block (non-array) to an ordinary variable. For
114 * example if the original source code was:
116 * out Blk { vec4 bar } foo;
118 * Then the variable is now:
122 * We need to visit this variable using the name:
126 const glsl_type
*ifc_type
= var
->get_interface_type();
127 char *name
= ralloc_asprintf(NULL
, "%s.%s", ifc_type
->name
, var
->name
);
128 /* Note: row_major is only meaningful for uniform blocks, and lowering
129 * is only applied to non-uniform interface blocks, so we can safely
130 * pass false for row_major.
132 recursion(var
->type
, &name
, strlen(name
), false, NULL
);
134 } else if (t
->is_record() || (t
->is_array() && t
->fields
.array
->is_record())) {
135 char *name
= ralloc_strdup(NULL
, var
->name
);
136 recursion(var
->type
, &name
, strlen(name
), false, NULL
);
138 } else if (t
->is_interface()) {
139 char *name
= ralloc_strdup(NULL
, var
->type
->name
);
140 recursion(var
->type
, &name
, strlen(name
), false, NULL
);
142 } else if (t
->is_array() && t
->fields
.array
->is_interface()) {
143 char *name
= ralloc_strdup(NULL
, var
->type
->fields
.array
->name
);
144 recursion(var
->type
, &name
, strlen(name
), false, NULL
);
147 this->visit_field(t
, var
->name
, false, NULL
);
152 program_resource_visitor::recursion(const glsl_type
*t
, char **name
,
153 size_t name_length
, bool row_major
,
154 const glsl_type
*record_type
)
156 /* Records need to have each field processed individually.
158 * Arrays of records need to have each array element processed
159 * individually, then each field of the resulting array elements processed
162 if (t
->is_record() || t
->is_interface()) {
163 if (record_type
== NULL
&& t
->is_record())
166 for (unsigned i
= 0; i
< t
->length
; i
++) {
167 const char *field
= t
->fields
.structure
[i
].name
;
168 size_t new_length
= name_length
;
170 if (t
->fields
.structure
[i
].type
->is_record())
171 this->visit_field(&t
->fields
.structure
[i
]);
173 /* Append '.field' to the current variable name. */
174 if (name_length
== 0) {
175 ralloc_asprintf_rewrite_tail(name
, &new_length
, "%s", field
);
177 ralloc_asprintf_rewrite_tail(name
, &new_length
, ".%s", field
);
180 recursion(t
->fields
.structure
[i
].type
, name
, new_length
,
181 t
->fields
.structure
[i
].row_major
, record_type
);
183 /* Only the first leaf-field of the record gets called with the
184 * record type pointer.
188 } else if (t
->is_array() && (t
->fields
.array
->is_record()
189 || t
->fields
.array
->is_interface())) {
190 if (record_type
== NULL
&& t
->fields
.array
->is_record())
191 record_type
= t
->fields
.array
;
193 for (unsigned i
= 0; i
< t
->length
; i
++) {
194 size_t new_length
= name_length
;
196 /* Append the subscript to the current variable name */
197 ralloc_asprintf_rewrite_tail(name
, &new_length
, "[%u]", i
);
199 recursion(t
->fields
.array
, name
, new_length
, row_major
,
202 /* Only the first leaf-field of the record gets called with the
203 * record type pointer.
208 this->visit_field(t
, *name
, row_major
, record_type
);
213 program_resource_visitor::visit_field(const glsl_type
*type
, const char *name
,
215 const glsl_type
*record_type
)
217 visit_field(type
, name
, row_major
);
221 program_resource_visitor::visit_field(const glsl_struct_field
*field
)
230 * Class to help calculate the storage requirements for a set of uniforms
232 * As uniforms are added to the active set the number of active uniforms and
233 * the storage requirements for those uniforms are accumulated. The active
234 * uniforms are added the the hash table supplied to the constructor.
236 * If the same uniform is added multiple times (i.e., once for each shader
237 * target), it will only be accounted once.
239 class count_uniform_size
: public program_resource_visitor
{
241 count_uniform_size(struct string_to_uint_map
*map
)
242 : num_active_uniforms(0), num_values(0), num_shader_samplers(0),
243 num_shader_images(0), num_shader_uniform_components(0),
244 is_ubo_var(false), map(map
)
251 this->num_shader_samplers
= 0;
252 this->num_shader_images
= 0;
253 this->num_shader_uniform_components
= 0;
256 void process(ir_variable
*var
)
258 this->is_ubo_var
= var
->is_in_uniform_block();
259 if (var
->is_interface_instance())
260 program_resource_visitor::process(var
->get_interface_type(),
261 var
->get_interface_type()->name
);
263 program_resource_visitor::process(var
);
267 * Total number of active uniforms counted
269 unsigned num_active_uniforms
;
272 * Number of data values required to back the storage for the active uniforms
277 * Number of samplers used
279 unsigned num_shader_samplers
;
282 * Number of images used
284 unsigned num_shader_images
;
287 * Number of uniforms used in the current shader
289 unsigned num_shader_uniform_components
;
294 virtual void visit_field(const glsl_type
*type
, const char *name
,
297 assert(!type
->is_record());
298 assert(!(type
->is_array() && type
->fields
.array
->is_record()));
299 assert(!type
->is_interface());
300 assert(!(type
->is_array() && type
->fields
.array
->is_interface()));
304 /* Count the number of samplers regardless of whether the uniform is
305 * already in the hash table. The hash table prevents adding the same
306 * uniform for multiple shader targets, but in this case we want to
307 * count it for each shader target.
309 const unsigned values
= values_for_type(type
);
310 if (type
->contains_sampler()) {
311 this->num_shader_samplers
+=
312 type
->is_array() ? type
->array_size() : 1;
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
,
390 ubo_block_index
= -1;
391 if (var
->is_in_uniform_block()) {
392 if (var
->is_interface_instance() && var
->type
->is_array()) {
393 unsigned l
= strlen(var
->get_interface_type()->name
);
395 for (unsigned i
= 0; i
< prog
->NumUniformBlocks
; i
++) {
396 if (strncmp(var
->get_interface_type()->name
,
397 prog
->UniformBlocks
[i
].Name
,
399 && prog
->UniformBlocks
[i
].Name
[l
] == '[') {
405 for (unsigned i
= 0; i
< prog
->NumUniformBlocks
; i
++) {
406 if (strcmp(var
->get_interface_type()->name
,
407 prog
->UniformBlocks
[i
].Name
) == 0) {
413 assert(ubo_block_index
!= -1);
415 /* Uniform blocks that were specified with an instance name must be
416 * handled a little bit differently. The name of the variable is the
417 * name used to reference the uniform block instead of being the name
418 * of a variable within the block. Therefore, searching for the name
419 * within the block will fail.
421 if (var
->is_interface_instance()) {
423 ubo_row_major
= false;
425 const struct gl_uniform_block
*const block
=
426 &prog
->UniformBlocks
[ubo_block_index
];
428 assert(var
->data
.location
!= -1);
430 const struct gl_uniform_buffer_variable
*const ubo_var
=
431 &block
->Uniforms
[var
->data
.location
];
433 ubo_row_major
= ubo_var
->RowMajor
;
434 ubo_byte_offset
= ubo_var
->Offset
;
437 if (var
->is_interface_instance())
438 process(var
->get_interface_type(),
439 var
->get_interface_type()->name
);
449 gl_shader_stage shader_type
;
452 void handle_samplers(const glsl_type
*base_type
,
453 struct gl_uniform_storage
*uniform
)
455 if (base_type
->is_sampler()) {
456 uniform
->sampler
[shader_type
].index
= this->next_sampler
;
457 uniform
->sampler
[shader_type
].active
= true;
459 /* Increment the sampler by 1 for non-arrays and by the number of
460 * array elements for arrays.
462 this->next_sampler
+=
463 MAX2(1, uniform
->array_elements
);
465 const gl_texture_index target
= base_type
->sampler_index();
466 const unsigned shadow
= base_type
->sampler_shadow
;
467 for (unsigned i
= uniform
->sampler
[shader_type
].index
;
468 i
< MIN2(this->next_sampler
, MAX_SAMPLERS
);
470 this->targets
[i
] = target
;
471 this->shader_samplers_used
|= 1U << i
;
472 this->shader_shadow_samplers
|= shadow
<< i
;
475 uniform
->sampler
[shader_type
].index
= ~0;
476 uniform
->sampler
[shader_type
].active
= false;
480 void handle_images(const glsl_type
*base_type
,
481 struct gl_uniform_storage
*uniform
)
483 if (base_type
->is_image()) {
484 uniform
->image
[shader_type
].index
= this->next_image
;
485 uniform
->image
[shader_type
].active
= true;
487 /* Increment the image index by 1 for non-arrays and by the
488 * number of array elements for arrays.
490 this->next_image
+= MAX2(1, uniform
->array_elements
);
493 uniform
->image
[shader_type
].index
= ~0;
494 uniform
->image
[shader_type
].active
= false;
498 virtual void visit_field(const glsl_type
*type
, const char *name
,
504 assert(!"Should not get here.");
507 virtual void visit_field(const glsl_type
*type
, const char *name
,
508 bool row_major
, const glsl_type
*record_type
)
510 assert(!type
->is_record());
511 assert(!(type
->is_array() && type
->fields
.array
->is_record()));
512 assert(!type
->is_interface());
513 assert(!(type
->is_array() && type
->fields
.array
->is_interface()));
518 bool found
= this->map
->get(id
, name
);
524 const glsl_type
*base_type
;
525 if (type
->is_array()) {
526 this->uniforms
[id
].array_elements
= type
->length
;
527 base_type
= type
->fields
.array
;
529 this->uniforms
[id
].array_elements
= 0;
533 /* This assigns uniform indices to sampler and image uniforms. */
534 handle_samplers(base_type
, &this->uniforms
[id
]);
535 handle_images(base_type
, &this->uniforms
[id
]);
537 /* If there is already storage associated with this uniform, it means
538 * that it was set while processing an earlier shader stage. For
539 * example, we may be processing the uniform in the fragment shader, but
540 * the uniform was already processed in the vertex shader.
542 if (this->uniforms
[id
].storage
!= NULL
) {
546 this->uniforms
[id
].name
= ralloc_strdup(this->uniforms
, name
);
547 this->uniforms
[id
].type
= base_type
;
548 this->uniforms
[id
].initialized
= 0;
549 this->uniforms
[id
].num_driver_storage
= 0;
550 this->uniforms
[id
].driver_storage
= NULL
;
551 this->uniforms
[id
].storage
= this->values
;
552 this->uniforms
[id
].atomic_buffer_index
= -1;
553 if (this->ubo_block_index
!= -1) {
554 this->uniforms
[id
].block_index
= this->ubo_block_index
;
556 const unsigned alignment
= record_type
557 ? record_type
->std140_base_alignment(ubo_row_major
)
558 : type
->std140_base_alignment(ubo_row_major
);
559 this->ubo_byte_offset
= glsl_align(this->ubo_byte_offset
, alignment
);
560 this->uniforms
[id
].offset
= this->ubo_byte_offset
;
561 this->ubo_byte_offset
+= type
->std140_size(ubo_row_major
);
563 if (type
->is_array()) {
564 this->uniforms
[id
].array_stride
=
565 glsl_align(type
->fields
.array
->std140_size(ubo_row_major
), 16);
567 this->uniforms
[id
].array_stride
= 0;
570 if (type
->is_matrix() ||
571 (type
->is_array() && type
->fields
.array
->is_matrix())) {
572 this->uniforms
[id
].matrix_stride
= 16;
573 this->uniforms
[id
].row_major
= ubo_row_major
;
575 this->uniforms
[id
].matrix_stride
= 0;
576 this->uniforms
[id
].row_major
= false;
579 this->uniforms
[id
].block_index
= -1;
580 this->uniforms
[id
].offset
= -1;
581 this->uniforms
[id
].array_stride
= -1;
582 this->uniforms
[id
].matrix_stride
= -1;
583 this->uniforms
[id
].row_major
= false;
586 this->values
+= values_for_type(type
);
589 struct string_to_uint_map
*map
;
591 struct gl_uniform_storage
*uniforms
;
592 unsigned next_sampler
;
596 union gl_constant_value
*values
;
598 gl_texture_index targets
[MAX_SAMPLERS
];
601 * Mask of samplers used by the current shader stage.
603 unsigned shader_samplers_used
;
606 * Mask of samplers used by the current shader stage for shadows.
608 unsigned shader_shadow_samplers
;
612 * Merges a uniform block into an array of uniform blocks that may or
613 * may not already contain a copy of it.
615 * Returns the index of the new block in the array.
618 link_cross_validate_uniform_block(void *mem_ctx
,
619 struct gl_uniform_block
**linked_blocks
,
620 unsigned int *num_linked_blocks
,
621 struct gl_uniform_block
*new_block
)
623 for (unsigned int i
= 0; i
< *num_linked_blocks
; i
++) {
624 struct gl_uniform_block
*old_block
= &(*linked_blocks
)[i
];
626 if (strcmp(old_block
->Name
, new_block
->Name
) == 0)
627 return link_uniform_blocks_are_compatible(old_block
, new_block
)
631 *linked_blocks
= reralloc(mem_ctx
, *linked_blocks
,
632 struct gl_uniform_block
,
633 *num_linked_blocks
+ 1);
634 int linked_block_index
= (*num_linked_blocks
)++;
635 struct gl_uniform_block
*linked_block
= &(*linked_blocks
)[linked_block_index
];
637 memcpy(linked_block
, new_block
, sizeof(*new_block
));
638 linked_block
->Uniforms
= ralloc_array(*linked_blocks
,
639 struct gl_uniform_buffer_variable
,
640 linked_block
->NumUniforms
);
642 memcpy(linked_block
->Uniforms
,
644 sizeof(*linked_block
->Uniforms
) * linked_block
->NumUniforms
);
646 for (unsigned int i
= 0; i
< linked_block
->NumUniforms
; i
++) {
647 struct gl_uniform_buffer_variable
*ubo_var
=
648 &linked_block
->Uniforms
[i
];
650 if (ubo_var
->Name
== ubo_var
->IndexName
) {
651 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
652 ubo_var
->IndexName
= ubo_var
->Name
;
654 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
655 ubo_var
->IndexName
= ralloc_strdup(*linked_blocks
, ubo_var
->IndexName
);
659 return linked_block_index
;
663 * Walks the IR and update the references to uniform blocks in the
664 * ir_variables to point at linked shader's list (previously, they
665 * would point at the uniform block list in one of the pre-linked
669 link_update_uniform_buffer_variables(struct gl_shader
*shader
)
671 foreach_list(node
, shader
->ir
) {
672 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
674 if ((var
== NULL
) || !var
->is_in_uniform_block())
677 assert(var
->data
.mode
== ir_var_uniform
);
679 if (var
->is_interface_instance()) {
680 var
->data
.location
= 0;
685 char sentinel
= '\0';
687 if (var
->type
->is_record()) {
689 } else if (var
->type
->is_array()
690 && var
->type
->fields
.array
->is_record()) {
694 const unsigned l
= strlen(var
->name
);
695 for (unsigned i
= 0; i
< shader
->NumUniformBlocks
; i
++) {
696 for (unsigned j
= 0; j
< shader
->UniformBlocks
[i
].NumUniforms
; j
++) {
698 const char *begin
= shader
->UniformBlocks
[i
].Uniforms
[j
].Name
;
699 const char *end
= strchr(begin
, sentinel
);
704 if (l
!= (end
- begin
))
707 if (strncmp(var
->name
, begin
, l
) == 0) {
709 var
->data
.location
= j
;
712 } else if (!strcmp(var
->name
,
713 shader
->UniformBlocks
[i
].Uniforms
[j
].Name
)) {
715 var
->data
.location
= j
;
727 link_assign_uniform_block_offsets(struct gl_shader
*shader
)
729 for (unsigned b
= 0; b
< shader
->NumUniformBlocks
; b
++) {
730 struct gl_uniform_block
*block
= &shader
->UniformBlocks
[b
];
733 for (unsigned int i
= 0; i
< block
->NumUniforms
; i
++) {
734 struct gl_uniform_buffer_variable
*ubo_var
= &block
->Uniforms
[i
];
735 const struct glsl_type
*type
= ubo_var
->Type
;
737 unsigned alignment
= type
->std140_base_alignment(ubo_var
->RowMajor
);
738 unsigned size
= type
->std140_size(ubo_var
->RowMajor
);
740 offset
= glsl_align(offset
, alignment
);
741 ubo_var
->Offset
= offset
;
745 /* From the GL_ARB_uniform_buffer_object spec:
747 * "For uniform blocks laid out according to [std140] rules,
748 * the minimum buffer object size returned by the
749 * UNIFORM_BLOCK_DATA_SIZE query is derived by taking the
750 * offset of the last basic machine unit consumed by the
751 * last uniform of the uniform block (including any
752 * end-of-array or end-of-structure padding), adding one,
753 * and rounding up to the next multiple of the base
754 * alignment required for a vec4."
756 block
->UniformBufferSize
= glsl_align(offset
, 16);
761 * Scan the program for image uniforms and store image unit access
762 * information into the gl_shader data structure.
765 link_set_image_access_qualifiers(struct gl_shader_program
*prog
)
767 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
768 gl_shader
*sh
= prog
->_LinkedShaders
[i
];
773 foreach_list(node
, sh
->ir
) {
774 ir_variable
*var
= ((ir_instruction
*) node
)->as_variable();
776 if (var
&& var
->data
.mode
== ir_var_uniform
&&
777 var
->type
->contains_image()) {
779 bool found
= prog
->UniformHash
->get(id
, var
->name
);
782 const gl_uniform_storage
*storage
= &prog
->UniformStorage
[id
];
783 const unsigned index
= storage
->image
[i
].index
;
784 const GLenum access
= (var
->data
.image
.read_only
? GL_READ_ONLY
:
785 var
->data
.image
.write_only
? GL_WRITE_ONLY
:
788 for (unsigned j
= 0; j
< MAX2(1, storage
->array_elements
); ++j
)
789 sh
->ImageAccess
[index
+ j
] = access
;
796 link_assign_uniform_locations(struct gl_shader_program
*prog
)
798 ralloc_free(prog
->UniformStorage
);
799 prog
->UniformStorage
= NULL
;
800 prog
->NumUserUniformStorage
= 0;
802 ralloc_free(prog
->UniformRemapTable
);
803 prog
->UniformRemapTable
= NULL
;
804 prog
->NumUniformRemapTable
= 0;
806 if (prog
->UniformHash
!= NULL
) {
807 prog
->UniformHash
->clear();
809 prog
->UniformHash
= new string_to_uint_map
;
812 /* First pass: Count the uniform resources used by the user-defined
813 * uniforms. While this happens, each active uniform will have an index
816 * Note: this is *NOT* the index that is returned to the application by
817 * glGetUniformLocation.
819 count_uniform_size
uniform_size(prog
->UniformHash
);
820 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
821 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
826 /* Uniforms that lack an initializer in the shader code have an initial
827 * value of zero. This includes sampler uniforms.
829 * Page 24 (page 30 of the PDF) of the GLSL 1.20 spec says:
831 * "The link time initial value is either the value of the variable's
832 * initializer, if present, or 0 if no initializer is present. Sampler
833 * types cannot have initializers."
835 memset(sh
->SamplerUnits
, 0, sizeof(sh
->SamplerUnits
));
836 memset(sh
->ImageUnits
, 0, sizeof(sh
->ImageUnits
));
838 link_update_uniform_buffer_variables(sh
);
840 /* Reset various per-shader target counts.
842 uniform_size
.start_shader();
844 foreach_list(node
, sh
->ir
) {
845 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
847 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
))
850 /* FINISHME: Update code to process built-in uniforms!
852 if (strncmp("gl_", var
->name
, 3) == 0) {
853 uniform_size
.num_shader_uniform_components
+=
854 var
->type
->component_slots();
858 uniform_size
.process(var
);
861 sh
->num_samplers
= uniform_size
.num_shader_samplers
;
862 sh
->NumImages
= uniform_size
.num_shader_images
;
863 sh
->num_uniform_components
= uniform_size
.num_shader_uniform_components
;
865 sh
->num_combined_uniform_components
= sh
->num_uniform_components
;
866 for (unsigned i
= 0; i
< sh
->NumUniformBlocks
; i
++) {
867 sh
->num_combined_uniform_components
+=
868 sh
->UniformBlocks
[i
].UniformBufferSize
/ 4;
872 const unsigned num_user_uniforms
= uniform_size
.num_active_uniforms
;
873 const unsigned num_data_slots
= uniform_size
.num_values
;
875 /* On the outside chance that there were no uniforms, bail out.
877 if (num_user_uniforms
== 0)
880 struct gl_uniform_storage
*uniforms
=
881 rzalloc_array(prog
, struct gl_uniform_storage
, num_user_uniforms
);
882 union gl_constant_value
*data
=
883 rzalloc_array(uniforms
, union gl_constant_value
, num_data_slots
);
885 union gl_constant_value
*data_end
= &data
[num_data_slots
];
888 parcel_out_uniform_storage
parcel(prog
->UniformHash
, uniforms
, data
);
890 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
891 if (prog
->_LinkedShaders
[i
] == NULL
)
894 parcel
.start_shader((gl_shader_stage
)i
);
896 foreach_list(node
, prog
->_LinkedShaders
[i
]->ir
) {
897 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
899 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
))
902 /* FINISHME: Update code to process built-in uniforms!
904 if (strncmp("gl_", var
->name
, 3) == 0)
907 parcel
.set_and_process(prog
, var
);
910 prog
->_LinkedShaders
[i
]->active_samplers
= parcel
.shader_samplers_used
;
911 prog
->_LinkedShaders
[i
]->shadow_samplers
= parcel
.shader_shadow_samplers
;
913 STATIC_ASSERT(sizeof(prog
->_LinkedShaders
[i
]->SamplerTargets
) == sizeof(parcel
.targets
));
914 memcpy(prog
->_LinkedShaders
[i
]->SamplerTargets
, parcel
.targets
,
915 sizeof(prog
->_LinkedShaders
[i
]->SamplerTargets
));
918 /* Build the uniform remap table that is used to set/get uniform locations */
919 for (unsigned i
= 0; i
< num_user_uniforms
; i
++) {
921 /* how many new entries for this uniform? */
922 const unsigned entries
= MAX2(1, uniforms
[i
].array_elements
);
924 /* resize remap table to fit new entries */
925 prog
->UniformRemapTable
=
927 prog
->UniformRemapTable
,
928 gl_uniform_storage
*,
929 prog
->NumUniformRemapTable
+ entries
);
931 /* set pointers for this uniform */
932 for (unsigned j
= 0; j
< entries
; j
++)
933 prog
->UniformRemapTable
[prog
->NumUniformRemapTable
+j
] = &uniforms
[i
];
935 /* set the base location in remap table for the uniform */
936 uniforms
[i
].remap_location
= prog
->NumUniformRemapTable
;
938 prog
->NumUniformRemapTable
+= entries
;
942 for (unsigned i
= 0; i
< num_user_uniforms
; i
++) {
943 assert(uniforms
[i
].storage
!= NULL
);
946 assert(parcel
.values
== data_end
);
949 prog
->NumUserUniformStorage
= num_user_uniforms
;
950 prog
->UniformStorage
= uniforms
;
952 link_set_image_access_qualifiers(prog
);
953 link_set_uniform_initializers(prog
);