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())
173 this->enter_record(t
, *name
, row_major
);
175 for (unsigned i
= 0; i
< t
->length
; i
++) {
176 const char *field
= t
->fields
.structure
[i
].name
;
177 size_t new_length
= name_length
;
179 if (t
->fields
.structure
[i
].type
->is_record())
180 this->visit_field(&t
->fields
.structure
[i
]);
182 /* Append '.field' to the current variable name. */
183 if (name_length
== 0) {
184 ralloc_asprintf_rewrite_tail(name
, &new_length
, "%s", field
);
186 ralloc_asprintf_rewrite_tail(name
, &new_length
, ".%s", field
);
189 /* The layout of structures at the top level of the block is set
190 * during parsing. For matrices contained in multiple levels of
191 * structures in the block, the inner structures have no layout.
192 * These cases must potentially inherit the layout from the outer
195 bool field_row_major
= row_major
;
196 const enum glsl_matrix_layout matrix_layout
=
197 glsl_matrix_layout(t
->fields
.structure
[i
].matrix_layout
);
198 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
199 field_row_major
= true;
200 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
201 field_row_major
= false;
204 recursion(t
->fields
.structure
[i
].type
, name
, new_length
,
207 (i
+ 1) == t
->length
);
209 /* Only the first leaf-field of the record gets called with the
210 * record type pointer.
215 if (t
->is_record()) {
216 (*name
)[name_length
] = '\0';
217 this->leave_record(t
, *name
, row_major
);
219 } else if (t
->is_array() && (t
->fields
.array
->is_record()
220 || t
->fields
.array
->is_interface())) {
221 if (record_type
== NULL
&& t
->fields
.array
->is_record())
222 record_type
= t
->fields
.array
;
224 for (unsigned i
= 0; i
< t
->length
; i
++) {
225 size_t new_length
= name_length
;
227 /* Append the subscript to the current variable name */
228 ralloc_asprintf_rewrite_tail(name
, &new_length
, "[%u]", i
);
230 recursion(t
->fields
.array
, name
, new_length
, row_major
,
232 (i
+ 1) == t
->length
);
234 /* Only the first leaf-field of the record gets called with the
235 * record type pointer.
240 this->visit_field(t
, *name
, row_major
, record_type
, last_field
);
245 program_resource_visitor::visit_field(const glsl_type
*type
, const char *name
,
248 bool /* last_field */)
250 visit_field(type
, name
, row_major
);
254 program_resource_visitor::visit_field(const glsl_struct_field
*field
)
261 program_resource_visitor::enter_record(const glsl_type
*, const char *, bool)
266 program_resource_visitor::leave_record(const glsl_type
*, const char *, bool)
273 * Class to help calculate the storage requirements for a set of uniforms
275 * As uniforms are added to the active set the number of active uniforms and
276 * the storage requirements for those uniforms are accumulated. The active
277 * uniforms are added to the hash table supplied to the constructor.
279 * If the same uniform is added multiple times (i.e., once for each shader
280 * target), it will only be accounted once.
282 class count_uniform_size
: public program_resource_visitor
{
284 count_uniform_size(struct string_to_uint_map
*map
)
285 : num_active_uniforms(0), num_values(0), num_shader_samplers(0),
286 num_shader_images(0), num_shader_uniform_components(0),
287 num_shader_subroutines(0),
288 is_ubo_var(false), map(map
)
295 this->num_shader_samplers
= 0;
296 this->num_shader_images
= 0;
297 this->num_shader_uniform_components
= 0;
298 this->num_shader_subroutines
= 0;
301 void process(ir_variable
*var
)
303 this->is_ubo_var
= var
->is_in_buffer_block();
304 if (var
->is_interface_instance())
305 program_resource_visitor::process(var
->get_interface_type(),
306 var
->get_interface_type()->name
);
308 program_resource_visitor::process(var
);
312 * Total number of active uniforms counted
314 unsigned num_active_uniforms
;
317 * Number of data values required to back the storage for the active uniforms
322 * Number of samplers used
324 unsigned num_shader_samplers
;
327 * Number of images used
329 unsigned num_shader_images
;
332 * Number of uniforms used in the current shader
334 unsigned num_shader_uniform_components
;
337 * Number of subroutine uniforms used
339 unsigned num_shader_subroutines
;
344 virtual void visit_field(const glsl_type
*type
, const char *name
,
347 assert(!type
->without_array()->is_record());
348 assert(!type
->without_array()->is_interface());
352 /* Count the number of samplers regardless of whether the uniform is
353 * already in the hash table. The hash table prevents adding the same
354 * uniform for multiple shader targets, but in this case we want to
355 * count it for each shader target.
357 const unsigned values
= values_for_type(type
);
358 if (type
->contains_subroutine()) {
359 this->num_shader_subroutines
+= values
;
360 } else if (type
->contains_sampler()) {
361 this->num_shader_samplers
+= values
;
362 } else if (type
->contains_image()) {
363 this->num_shader_images
+= values
;
365 /* As drivers are likely to represent image uniforms as
366 * scalar indices, count them against the limit of uniform
367 * components in the default block. The spec allows image
368 * uniforms to use up no more than one scalar slot.
370 this->num_shader_uniform_components
+= values
;
372 /* Accumulate the total number of uniform slots used by this shader.
373 * Note that samplers do not count against this limit because they
374 * don't use any storage on current hardware.
377 this->num_shader_uniform_components
+= values
;
380 /* If the uniform is already in the map, there's nothing more to do.
383 if (this->map
->get(id
, name
))
386 this->map
->put(this->num_active_uniforms
, name
);
388 /* Each leaf uniform occupies one entry in the list of active
391 this->num_active_uniforms
++;
392 this->num_values
+= values
;
395 struct string_to_uint_map
*map
;
398 } /* anonymous namespace */
401 * Class to help parcel out pieces of backing storage to uniforms
403 * Each uniform processed has some range of the \c gl_constant_value
404 * structures associated with it. The association is done by finding
405 * the uniform in the \c string_to_uint_map and using the value from
406 * the map to connect that slot in the \c gl_uniform_storage table
407 * with the next available slot in the \c gl_constant_value array.
410 * This class assumes that every uniform that will be processed is
411 * already in the \c string_to_uint_map. In addition, it assumes that
412 * the \c gl_uniform_storage and \c gl_constant_value arrays are "big
415 class parcel_out_uniform_storage
: public program_resource_visitor
{
417 parcel_out_uniform_storage(struct string_to_uint_map
*map
,
418 struct gl_uniform_storage
*uniforms
,
419 union gl_constant_value
*values
)
420 : map(map
), uniforms(uniforms
), values(values
)
424 void start_shader(gl_shader_stage shader_type
)
426 assert(shader_type
< MESA_SHADER_STAGES
);
427 this->shader_type
= shader_type
;
429 this->shader_samplers_used
= 0;
430 this->shader_shadow_samplers
= 0;
431 this->next_sampler
= 0;
432 this->next_image
= 0;
433 this->next_subroutine
= 0;
434 memset(this->targets
, 0, sizeof(this->targets
));
437 void set_and_process(struct gl_shader_program
*prog
,
443 ubo_block_index
= -1;
444 if (var
->is_in_buffer_block()) {
445 if (var
->is_interface_instance() && var
->type
->is_array()) {
446 unsigned l
= strlen(var
->get_interface_type()->name
);
448 for (unsigned i
= 0; i
< prog
->NumUniformBlocks
; i
++) {
449 if (strncmp(var
->get_interface_type()->name
,
450 prog
->UniformBlocks
[i
].Name
,
452 && prog
->UniformBlocks
[i
].Name
[l
] == '[') {
458 for (unsigned i
= 0; i
< prog
->NumUniformBlocks
; i
++) {
459 if (strcmp(var
->get_interface_type()->name
,
460 prog
->UniformBlocks
[i
].Name
) == 0) {
466 assert(ubo_block_index
!= -1);
468 /* Uniform blocks that were specified with an instance name must be
469 * handled a little bit differently. The name of the variable is the
470 * name used to reference the uniform block instead of being the name
471 * of a variable within the block. Therefore, searching for the name
472 * within the block will fail.
474 if (var
->is_interface_instance()) {
477 const struct gl_uniform_block
*const block
=
478 &prog
->UniformBlocks
[ubo_block_index
];
480 assert(var
->data
.location
!= -1);
482 const struct gl_uniform_buffer_variable
*const ubo_var
=
483 &block
->Uniforms
[var
->data
.location
];
485 ubo_byte_offset
= ubo_var
->Offset
;
488 if (var
->is_interface_instance())
489 process(var
->get_interface_type(),
490 var
->get_interface_type()->name
);
499 gl_shader_stage shader_type
;
502 void handle_samplers(const glsl_type
*base_type
,
503 struct gl_uniform_storage
*uniform
)
505 if (base_type
->is_sampler()) {
506 uniform
->sampler
[shader_type
].index
= this->next_sampler
;
507 uniform
->sampler
[shader_type
].active
= true;
509 /* Increment the sampler by 1 for non-arrays and by the number of
510 * array elements for arrays.
512 this->next_sampler
+=
513 MAX2(1, uniform
->array_elements
);
515 const gl_texture_index target
= base_type
->sampler_index();
516 const unsigned shadow
= base_type
->sampler_shadow
;
517 for (unsigned i
= uniform
->sampler
[shader_type
].index
;
518 i
< MIN2(this->next_sampler
, MAX_SAMPLERS
);
520 this->targets
[i
] = target
;
521 this->shader_samplers_used
|= 1U << i
;
522 this->shader_shadow_samplers
|= shadow
<< i
;
525 uniform
->sampler
[shader_type
].index
= ~0;
526 uniform
->sampler
[shader_type
].active
= false;
530 void handle_images(const glsl_type
*base_type
,
531 struct gl_uniform_storage
*uniform
)
533 if (base_type
->is_image()) {
534 uniform
->image
[shader_type
].index
= this->next_image
;
535 uniform
->image
[shader_type
].active
= true;
537 /* Increment the image index by 1 for non-arrays and by the
538 * number of array elements for arrays.
540 this->next_image
+= MAX2(1, uniform
->array_elements
);
543 uniform
->image
[shader_type
].index
= ~0;
544 uniform
->image
[shader_type
].active
= false;
548 void handle_subroutines(const glsl_type
*base_type
,
549 struct gl_uniform_storage
*uniform
)
551 if (base_type
->is_subroutine()) {
552 uniform
->subroutine
[shader_type
].index
= this->next_subroutine
;
553 uniform
->subroutine
[shader_type
].active
= true;
555 /* Increment the subroutine index by 1 for non-arrays and by the
556 * number of array elements for arrays.
558 this->next_subroutine
+= MAX2(1, uniform
->array_elements
);
561 uniform
->subroutine
[shader_type
].index
= ~0;
562 uniform
->subroutine
[shader_type
].active
= false;
566 virtual void visit_field(const glsl_type
*type
, const char *name
,
572 assert(!"Should not get here.");
575 virtual void enter_record(const glsl_type
*type
, const char *,
577 assert(type
->is_record());
578 if (this->ubo_block_index
== -1)
580 this->ubo_byte_offset
= glsl_align(
581 this->ubo_byte_offset
, type
->std140_base_alignment(row_major
));
584 virtual void leave_record(const glsl_type
*type
, const char *,
586 assert(type
->is_record());
587 if (this->ubo_block_index
== -1)
589 this->ubo_byte_offset
= glsl_align(
590 this->ubo_byte_offset
, type
->std140_base_alignment(row_major
));
593 virtual void visit_field(const glsl_type
*type
, const char *name
,
594 bool row_major
, const glsl_type
*record_type
,
595 bool /* last_field */)
597 assert(!type
->without_array()->is_record());
598 assert(!type
->without_array()->is_interface());
601 bool found
= this->map
->get(id
, name
);
607 const glsl_type
*base_type
;
608 if (type
->is_array()) {
609 this->uniforms
[id
].array_elements
= type
->length
;
610 base_type
= type
->fields
.array
;
612 this->uniforms
[id
].array_elements
= 0;
616 /* This assigns uniform indices to sampler and image uniforms. */
617 handle_samplers(base_type
, &this->uniforms
[id
]);
618 handle_images(base_type
, &this->uniforms
[id
]);
619 handle_subroutines(base_type
, &this->uniforms
[id
]);
621 /* If there is already storage associated with this uniform or if the
622 * uniform is set as builtin, it means that it was set while processing
623 * an earlier shader stage. For example, we may be processing the
624 * uniform in the fragment shader, but the uniform was already processed
625 * in the vertex shader.
627 if (this->uniforms
[id
].storage
!= NULL
|| this->uniforms
[id
].builtin
) {
631 /* Assign explicit locations. */
632 if (current_var
->data
.explicit_location
) {
633 /* Set sequential locations for struct fields. */
634 if (record_type
!= NULL
) {
635 const unsigned entries
= MAX2(1, this->uniforms
[id
].array_elements
);
636 this->uniforms
[id
].remap_location
=
637 current_var
->data
.location
+ field_counter
;
638 field_counter
+= entries
;
640 this->uniforms
[id
].remap_location
= current_var
->data
.location
;
643 /* Initialize to to indicate that no location is set */
644 this->uniforms
[id
].remap_location
= UNMAPPED_UNIFORM_LOC
;
647 this->uniforms
[id
].name
= ralloc_strdup(this->uniforms
, name
);
648 this->uniforms
[id
].type
= base_type
;
649 this->uniforms
[id
].initialized
= 0;
650 this->uniforms
[id
].num_driver_storage
= 0;
651 this->uniforms
[id
].driver_storage
= NULL
;
652 this->uniforms
[id
].atomic_buffer_index
= -1;
653 this->uniforms
[id
].hidden
=
654 current_var
->data
.how_declared
== ir_var_hidden
;
655 this->uniforms
[id
].builtin
= is_gl_identifier(name
);
657 /* Do not assign storage if the uniform is builtin */
658 if (!this->uniforms
[id
].builtin
)
659 this->uniforms
[id
].storage
= this->values
;
661 if (this->ubo_block_index
!= -1) {
662 this->uniforms
[id
].block_index
= this->ubo_block_index
;
664 const unsigned alignment
= type
->std140_base_alignment(row_major
);
665 this->ubo_byte_offset
= glsl_align(this->ubo_byte_offset
, alignment
);
666 this->uniforms
[id
].offset
= this->ubo_byte_offset
;
667 this->ubo_byte_offset
+= type
->std140_size(row_major
);
669 if (type
->is_array()) {
670 this->uniforms
[id
].array_stride
=
671 glsl_align(type
->fields
.array
->std140_size(row_major
), 16);
673 this->uniforms
[id
].array_stride
= 0;
676 if (type
->without_array()->is_matrix()) {
677 const glsl_type
*matrix
= type
->without_array();
678 const unsigned N
= matrix
->base_type
== GLSL_TYPE_DOUBLE
? 8 : 4;
679 const unsigned items
= row_major
? matrix
->matrix_columns
: matrix
->vector_elements
;
682 this->uniforms
[id
].matrix_stride
= glsl_align(items
* N
, 16);
683 this->uniforms
[id
].row_major
= row_major
;
685 this->uniforms
[id
].matrix_stride
= 0;
686 this->uniforms
[id
].row_major
= false;
689 this->uniforms
[id
].block_index
= -1;
690 this->uniforms
[id
].offset
= -1;
691 this->uniforms
[id
].array_stride
= -1;
692 this->uniforms
[id
].matrix_stride
= -1;
693 this->uniforms
[id
].row_major
= false;
696 this->values
+= values_for_type(type
);
699 struct string_to_uint_map
*map
;
701 struct gl_uniform_storage
*uniforms
;
702 unsigned next_sampler
;
704 unsigned next_subroutine
;
707 union gl_constant_value
*values
;
709 gl_texture_index targets
[MAX_SAMPLERS
];
712 * Current variable being processed.
714 ir_variable
*current_var
;
717 * Field counter is used to take care that uniform structures
718 * with explicit locations get sequential locations.
720 unsigned field_counter
;
723 * Mask of samplers used by the current shader stage.
725 unsigned shader_samplers_used
;
728 * Mask of samplers used by the current shader stage for shadows.
730 unsigned shader_shadow_samplers
;
734 * Merges a uniform block into an array of uniform blocks that may or
735 * may not already contain a copy of it.
737 * Returns the index of the new block in the array.
740 link_cross_validate_uniform_block(void *mem_ctx
,
741 struct gl_uniform_block
**linked_blocks
,
742 unsigned int *num_linked_blocks
,
743 struct gl_uniform_block
*new_block
)
745 for (unsigned int i
= 0; i
< *num_linked_blocks
; i
++) {
746 struct gl_uniform_block
*old_block
= &(*linked_blocks
)[i
];
748 if (strcmp(old_block
->Name
, new_block
->Name
) == 0)
749 return link_uniform_blocks_are_compatible(old_block
, new_block
)
753 *linked_blocks
= reralloc(mem_ctx
, *linked_blocks
,
754 struct gl_uniform_block
,
755 *num_linked_blocks
+ 1);
756 int linked_block_index
= (*num_linked_blocks
)++;
757 struct gl_uniform_block
*linked_block
= &(*linked_blocks
)[linked_block_index
];
759 memcpy(linked_block
, new_block
, sizeof(*new_block
));
760 linked_block
->Uniforms
= ralloc_array(*linked_blocks
,
761 struct gl_uniform_buffer_variable
,
762 linked_block
->NumUniforms
);
764 memcpy(linked_block
->Uniforms
,
766 sizeof(*linked_block
->Uniforms
) * linked_block
->NumUniforms
);
768 for (unsigned int i
= 0; i
< linked_block
->NumUniforms
; i
++) {
769 struct gl_uniform_buffer_variable
*ubo_var
=
770 &linked_block
->Uniforms
[i
];
772 if (ubo_var
->Name
== ubo_var
->IndexName
) {
773 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
774 ubo_var
->IndexName
= ubo_var
->Name
;
776 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
777 ubo_var
->IndexName
= ralloc_strdup(*linked_blocks
, ubo_var
->IndexName
);
781 return linked_block_index
;
785 * Walks the IR and update the references to uniform blocks in the
786 * ir_variables to point at linked shader's list (previously, they
787 * would point at the uniform block list in one of the pre-linked
791 link_update_uniform_buffer_variables(struct gl_shader
*shader
)
793 foreach_in_list(ir_instruction
, node
, shader
->ir
) {
794 ir_variable
*const var
= node
->as_variable();
796 if ((var
== NULL
) || !var
->is_in_buffer_block())
799 assert(var
->data
.mode
== ir_var_uniform
||
800 var
->data
.mode
== ir_var_shader_storage
);
802 if (var
->is_interface_instance()) {
803 var
->data
.location
= 0;
808 char sentinel
= '\0';
810 if (var
->type
->is_record()) {
812 } else if (var
->type
->is_array()
813 && var
->type
->fields
.array
->is_record()) {
817 const unsigned l
= strlen(var
->name
);
818 for (unsigned i
= 0; i
< shader
->NumUniformBlocks
; i
++) {
819 for (unsigned j
= 0; j
< shader
->UniformBlocks
[i
].NumUniforms
; j
++) {
821 const char *begin
= shader
->UniformBlocks
[i
].Uniforms
[j
].Name
;
822 const char *end
= strchr(begin
, sentinel
);
827 if ((ptrdiff_t) l
!= (end
- begin
))
830 if (strncmp(var
->name
, begin
, l
) == 0) {
832 var
->data
.location
= j
;
835 } else if (!strcmp(var
->name
,
836 shader
->UniformBlocks
[i
].Uniforms
[j
].Name
)) {
838 var
->data
.location
= j
;
850 * Scan the program for image uniforms and store image unit access
851 * information into the gl_shader data structure.
854 link_set_image_access_qualifiers(struct gl_shader_program
*prog
)
856 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
857 gl_shader
*sh
= prog
->_LinkedShaders
[i
];
862 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
863 ir_variable
*var
= node
->as_variable();
865 if (var
&& var
->data
.mode
== ir_var_uniform
&&
866 var
->type
->contains_image()) {
868 bool found
= prog
->UniformHash
->get(id
, var
->name
);
871 const gl_uniform_storage
*storage
= &prog
->UniformStorage
[id
];
872 const unsigned index
= storage
->image
[i
].index
;
873 const GLenum access
= (var
->data
.image_read_only
? GL_READ_ONLY
:
874 var
->data
.image_write_only
? GL_WRITE_ONLY
:
877 for (unsigned j
= 0; j
< MAX2(1, storage
->array_elements
); ++j
)
878 sh
->ImageAccess
[index
+ j
] = access
;
885 * Sort the array of uniform storage so that the non-hidden uniforms are first
887 * This function sorts the list "in place." This is important because some of
888 * the storage accessible from \c uniforms has \c uniforms as its \c ralloc
889 * context. If \c uniforms is freed, some other storage will also be freed.
892 move_hidden_uniforms_to_end(struct gl_shader_program
*prog
,
893 struct gl_uniform_storage
*uniforms
,
894 unsigned num_elements
)
896 struct gl_uniform_storage
*sorted_uniforms
=
897 ralloc_array(prog
, struct gl_uniform_storage
, num_elements
);
898 unsigned hidden_uniforms
= 0;
901 /* Add the non-hidden uniforms. */
902 for (unsigned i
= 0; i
< num_elements
; i
++) {
903 if (!uniforms
[i
].hidden
)
904 sorted_uniforms
[j
++] = uniforms
[i
];
907 /* Add and count the hidden uniforms. */
908 for (unsigned i
= 0; i
< num_elements
; i
++) {
909 if (uniforms
[i
].hidden
) {
910 sorted_uniforms
[j
++] = uniforms
[i
];
915 assert(prog
->UniformHash
!= NULL
);
916 prog
->UniformHash
->clear();
917 for (unsigned i
= 0; i
< num_elements
; i
++) {
918 if (sorted_uniforms
[i
].name
!= NULL
)
919 prog
->UniformHash
->put(i
, sorted_uniforms
[i
].name
);
922 memcpy(uniforms
, sorted_uniforms
, sizeof(uniforms
[0]) * num_elements
);
923 ralloc_free(sorted_uniforms
);
925 return hidden_uniforms
;
929 link_assign_uniform_locations(struct gl_shader_program
*prog
,
930 unsigned int boolean_true
)
932 ralloc_free(prog
->UniformStorage
);
933 prog
->UniformStorage
= NULL
;
934 prog
->NumUniformStorage
= 0;
936 if (prog
->UniformHash
!= NULL
) {
937 prog
->UniformHash
->clear();
939 prog
->UniformHash
= new string_to_uint_map
;
942 /* First pass: Count the uniform resources used by the user-defined
943 * uniforms. While this happens, each active uniform will have an index
946 * Note: this is *NOT* the index that is returned to the application by
947 * glGetUniformLocation.
949 count_uniform_size
uniform_size(prog
->UniformHash
);
950 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
951 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
956 /* Uniforms that lack an initializer in the shader code have an initial
957 * value of zero. This includes sampler uniforms.
959 * Page 24 (page 30 of the PDF) of the GLSL 1.20 spec says:
961 * "The link time initial value is either the value of the variable's
962 * initializer, if present, or 0 if no initializer is present. Sampler
963 * types cannot have initializers."
965 memset(sh
->SamplerUnits
, 0, sizeof(sh
->SamplerUnits
));
966 memset(sh
->ImageUnits
, 0, sizeof(sh
->ImageUnits
));
968 link_update_uniform_buffer_variables(sh
);
970 /* Reset various per-shader target counts.
972 uniform_size
.start_shader();
974 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
975 ir_variable
*const var
= node
->as_variable();
977 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
&&
978 var
->data
.mode
!= ir_var_shader_storage
))
981 uniform_size
.process(var
);
984 sh
->num_samplers
= uniform_size
.num_shader_samplers
;
985 sh
->NumImages
= uniform_size
.num_shader_images
;
986 sh
->num_uniform_components
= uniform_size
.num_shader_uniform_components
;
987 sh
->num_combined_uniform_components
= sh
->num_uniform_components
;
989 for (unsigned i
= 0; i
< sh
->NumUniformBlocks
; i
++) {
990 sh
->num_combined_uniform_components
+=
991 sh
->UniformBlocks
[i
].UniformBufferSize
/ 4;
995 const unsigned num_uniforms
= uniform_size
.num_active_uniforms
;
996 const unsigned num_data_slots
= uniform_size
.num_values
;
998 /* On the outside chance that there were no uniforms, bail out.
1000 if (num_uniforms
== 0)
1003 struct gl_uniform_storage
*uniforms
=
1004 rzalloc_array(prog
, struct gl_uniform_storage
, num_uniforms
);
1005 union gl_constant_value
*data
=
1006 rzalloc_array(uniforms
, union gl_constant_value
, num_data_slots
);
1008 union gl_constant_value
*data_end
= &data
[num_data_slots
];
1011 parcel_out_uniform_storage
parcel(prog
->UniformHash
, uniforms
, data
);
1013 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1014 if (prog
->_LinkedShaders
[i
] == NULL
)
1017 parcel
.start_shader((gl_shader_stage
)i
);
1019 foreach_in_list(ir_instruction
, node
, prog
->_LinkedShaders
[i
]->ir
) {
1020 ir_variable
*const var
= node
->as_variable();
1022 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
&& var
->data
.mode
!= ir_var_shader_storage
))
1025 parcel
.set_and_process(prog
, var
);
1028 prog
->_LinkedShaders
[i
]->active_samplers
= parcel
.shader_samplers_used
;
1029 prog
->_LinkedShaders
[i
]->shadow_samplers
= parcel
.shader_shadow_samplers
;
1031 STATIC_ASSERT(sizeof(prog
->_LinkedShaders
[i
]->SamplerTargets
) == sizeof(parcel
.targets
));
1032 memcpy(prog
->_LinkedShaders
[i
]->SamplerTargets
, parcel
.targets
,
1033 sizeof(prog
->_LinkedShaders
[i
]->SamplerTargets
));
1036 const unsigned hidden_uniforms
=
1037 move_hidden_uniforms_to_end(prog
, uniforms
, num_uniforms
);
1039 /* Reserve all the explicit locations of the active uniforms. */
1040 for (unsigned i
= 0; i
< num_uniforms
; i
++) {
1041 if (uniforms
[i
].type
->is_subroutine())
1044 if (uniforms
[i
].remap_location
!= UNMAPPED_UNIFORM_LOC
) {
1045 /* How many new entries for this uniform? */
1046 const unsigned entries
= MAX2(1, uniforms
[i
].array_elements
);
1048 /* Set remap table entries point to correct gl_uniform_storage. */
1049 for (unsigned j
= 0; j
< entries
; j
++) {
1050 unsigned element_loc
= uniforms
[i
].remap_location
+ j
;
1051 assert(prog
->UniformRemapTable
[element_loc
] ==
1052 INACTIVE_UNIFORM_EXPLICIT_LOCATION
);
1053 prog
->UniformRemapTable
[element_loc
] = &uniforms
[i
];
1058 /* Reserve locations for rest of the uniforms. */
1059 for (unsigned i
= 0; i
< num_uniforms
; i
++) {
1061 if (uniforms
[i
].type
->is_subroutine())
1063 /* Built-in uniforms should not get any location. */
1064 if (uniforms
[i
].builtin
)
1067 /* Explicit ones have been set already. */
1068 if (uniforms
[i
].remap_location
!= UNMAPPED_UNIFORM_LOC
)
1071 /* how many new entries for this uniform? */
1072 const unsigned entries
= MAX2(1, uniforms
[i
].array_elements
);
1074 /* resize remap table to fit new entries */
1075 prog
->UniformRemapTable
=
1077 prog
->UniformRemapTable
,
1078 gl_uniform_storage
*,
1079 prog
->NumUniformRemapTable
+ entries
);
1081 /* set pointers for this uniform */
1082 for (unsigned j
= 0; j
< entries
; j
++)
1083 prog
->UniformRemapTable
[prog
->NumUniformRemapTable
+j
] = &uniforms
[i
];
1085 /* set the base location in remap table for the uniform */
1086 uniforms
[i
].remap_location
= prog
->NumUniformRemapTable
;
1088 prog
->NumUniformRemapTable
+= entries
;
1091 /* Reserve all the explicit locations of the active subroutine uniforms. */
1092 for (unsigned i
= 0; i
< num_uniforms
; i
++) {
1093 if (!uniforms
[i
].type
->is_subroutine())
1096 if (uniforms
[i
].remap_location
== UNMAPPED_UNIFORM_LOC
)
1099 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; j
++) {
1100 struct gl_shader
*sh
= prog
->_LinkedShaders
[j
];
1104 if (!uniforms
[i
].subroutine
[j
].active
)
1107 /* How many new entries for this uniform? */
1108 const unsigned entries
= MAX2(1, uniforms
[i
].array_elements
);
1110 /* Set remap table entries point to correct gl_uniform_storage. */
1111 for (unsigned k
= 0; k
< entries
; k
++) {
1112 unsigned element_loc
= uniforms
[i
].remap_location
+ k
;
1113 assert(sh
->SubroutineUniformRemapTable
[element_loc
] ==
1114 INACTIVE_UNIFORM_EXPLICIT_LOCATION
);
1115 sh
->SubroutineUniformRemapTable
[element_loc
] = &uniforms
[i
];
1120 /* reserve subroutine locations */
1121 for (unsigned i
= 0; i
< num_uniforms
; i
++) {
1123 if (!uniforms
[i
].type
->is_subroutine())
1125 const unsigned entries
= MAX2(1, uniforms
[i
].array_elements
);
1127 if (uniforms
[i
].remap_location
!= UNMAPPED_UNIFORM_LOC
)
1129 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; j
++) {
1130 struct gl_shader
*sh
= prog
->_LinkedShaders
[j
];
1134 if (!uniforms
[i
].subroutine
[j
].active
)
1137 sh
->SubroutineUniformRemapTable
=
1139 sh
->SubroutineUniformRemapTable
,
1140 gl_uniform_storage
*,
1141 sh
->NumSubroutineUniformRemapTable
+ entries
);
1143 for (unsigned k
= 0; k
< entries
; k
++)
1144 sh
->SubroutineUniformRemapTable
[sh
->NumSubroutineUniformRemapTable
+ k
] = &uniforms
[i
];
1145 uniforms
[i
].remap_location
= sh
->NumSubroutineUniformRemapTable
;
1146 sh
->NumSubroutineUniformRemapTable
+= entries
;
1151 for (unsigned i
= 0; i
< num_uniforms
; i
++) {
1152 assert(uniforms
[i
].storage
!= NULL
|| uniforms
[i
].builtin
);
1155 assert(parcel
.values
== data_end
);
1158 prog
->NumUniformStorage
= num_uniforms
;
1159 prog
->NumHiddenUniforms
= hidden_uniforms
;
1160 prog
->UniformStorage
= uniforms
;
1162 link_set_image_access_qualifiers(prog
);
1163 link_set_uniform_initializers(prog
, boolean_true
);