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"
31 #include "util/hash_table.h"
34 * \file link_uniforms.cpp
35 * Assign locations for GLSL uniforms.
37 * \author Ian Romanick <ian.d.romanick@intel.com>
41 * Used by linker to indicate uniforms that have no location set.
43 #define UNMAPPED_UNIFORM_LOC ~0u
46 * Count the backing storage requirements for a type
49 values_for_type(const glsl_type
*type
)
51 if (type
->is_sampler()) {
53 } else if (type
->is_array() && type
->fields
.array
->is_sampler()) {
54 return type
->array_size();
56 return type
->component_slots();
61 program_resource_visitor::process(const glsl_type
*type
, const char *name
)
63 assert(type
->without_array()->is_record()
64 || type
->without_array()->is_interface());
66 unsigned record_array_count
= 1;
67 char *name_copy
= ralloc_strdup(NULL
, name
);
68 enum glsl_interface_packing packing
= type
->get_interface_packing();
70 recursion(type
, &name_copy
, strlen(name
), false, NULL
, packing
, false,
71 record_array_count
, NULL
);
72 ralloc_free(name_copy
);
76 program_resource_visitor::process(ir_variable
*var
)
78 unsigned record_array_count
= 1;
79 const bool row_major
=
80 var
->data
.matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
82 const enum glsl_interface_packing packing
= var
->get_interface_type() ?
83 var
->get_interface_type_packing() :
84 var
->type
->get_interface_packing();
87 var
->data
.from_named_ifc_block
? var
->get_interface_type() : var
->type
;
88 const glsl_type
*t_without_array
= t
->without_array();
90 /* false is always passed for the row_major parameter to the other
91 * processing functions because no information is available to do
92 * otherwise. See the warning in linker.h.
94 if (t_without_array
->is_record() ||
95 (t
->is_array() && t
->fields
.array
->is_array())) {
96 char *name
= ralloc_strdup(NULL
, var
->name
);
97 recursion(var
->type
, &name
, strlen(name
), row_major
, NULL
, packing
,
98 false, record_array_count
, NULL
);
100 } else if (t_without_array
->is_interface()) {
101 char *name
= ralloc_strdup(NULL
, t_without_array
->name
);
102 const glsl_struct_field
*ifc_member
= var
->data
.from_named_ifc_block
?
104 fields
.structure
[t_without_array
->field_index(var
->name
)] : NULL
;
106 recursion(t
, &name
, strlen(name
), row_major
, NULL
, packing
,
107 false, record_array_count
, ifc_member
);
110 this->set_record_array_count(record_array_count
);
111 this->visit_field(t
, var
->name
, row_major
, NULL
, packing
, false);
116 program_resource_visitor::recursion(const glsl_type
*t
, char **name
,
117 size_t name_length
, bool row_major
,
118 const glsl_type
*record_type
,
119 const enum glsl_interface_packing packing
,
121 unsigned record_array_count
,
122 const glsl_struct_field
*named_ifc_member
)
124 /* Records need to have each field processed individually.
126 * Arrays of records need to have each array element processed
127 * individually, then each field of the resulting array elements processed
130 if (t
->is_interface() && named_ifc_member
) {
131 ralloc_asprintf_rewrite_tail(name
, &name_length
, ".%s",
132 named_ifc_member
->name
);
133 recursion(named_ifc_member
->type
, name
, name_length
, row_major
, NULL
,
134 packing
, false, record_array_count
, NULL
);
135 } else if (t
->is_record() || t
->is_interface()) {
136 if (record_type
== NULL
&& t
->is_record())
140 this->enter_record(t
, *name
, row_major
, packing
);
142 for (unsigned i
= 0; i
< t
->length
; i
++) {
143 const char *field
= t
->fields
.structure
[i
].name
;
144 size_t new_length
= name_length
;
146 if (t
->fields
.structure
[i
].type
->is_record())
147 this->visit_field(&t
->fields
.structure
[i
]);
149 if (t
->is_interface() && t
->fields
.structure
[i
].offset
!= -1)
150 this->set_buffer_offset(t
->fields
.structure
[i
].offset
);
152 /* Append '.field' to the current variable name. */
153 if (name_length
== 0) {
154 ralloc_asprintf_rewrite_tail(name
, &new_length
, "%s", field
);
156 ralloc_asprintf_rewrite_tail(name
, &new_length
, ".%s", field
);
159 /* The layout of structures at the top level of the block is set
160 * during parsing. For matrices contained in multiple levels of
161 * structures in the block, the inner structures have no layout.
162 * These cases must potentially inherit the layout from the outer
165 bool field_row_major
= row_major
;
166 const enum glsl_matrix_layout matrix_layout
=
167 glsl_matrix_layout(t
->fields
.structure
[i
].matrix_layout
);
168 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
169 field_row_major
= true;
170 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
171 field_row_major
= false;
174 recursion(t
->fields
.structure
[i
].type
, name
, new_length
,
178 (i
+ 1) == t
->length
, record_array_count
, NULL
);
180 /* Only the first leaf-field of the record gets called with the
181 * record type pointer.
186 if (t
->is_record()) {
187 (*name
)[name_length
] = '\0';
188 this->leave_record(t
, *name
, row_major
, packing
);
190 } else if (t
->without_array()->is_record() ||
191 t
->without_array()->is_interface() ||
192 (t
->is_array() && t
->fields
.array
->is_array())) {
193 if (record_type
== NULL
&& t
->fields
.array
->is_record())
194 record_type
= t
->fields
.array
;
196 unsigned length
= t
->length
;
197 /* Shader storage block unsized arrays: add subscript [0] to variable
199 if (t
->is_unsized_array())
202 record_array_count
*= length
;
204 for (unsigned i
= 0; i
< length
; i
++) {
205 size_t new_length
= name_length
;
207 /* Append the subscript to the current variable name */
208 ralloc_asprintf_rewrite_tail(name
, &new_length
, "[%u]", i
);
210 recursion(t
->fields
.array
, name
, new_length
, row_major
,
213 (i
+ 1) == t
->length
, record_array_count
,
216 /* Only the first leaf-field of the record gets called with the
217 * record type pointer.
222 this->set_record_array_count(record_array_count
);
223 this->visit_field(t
, *name
, row_major
, record_type
, packing
, last_field
);
228 program_resource_visitor::visit_field(const glsl_type
*type
, const char *name
,
231 const enum glsl_interface_packing
,
232 bool /* last_field */)
234 visit_field(type
, name
, row_major
);
238 program_resource_visitor::visit_field(const glsl_struct_field
*field
)
245 program_resource_visitor::enter_record(const glsl_type
*, const char *, bool,
246 const enum glsl_interface_packing
)
251 program_resource_visitor::leave_record(const glsl_type
*, const char *, bool,
252 const enum glsl_interface_packing
)
257 program_resource_visitor::set_buffer_offset(unsigned)
262 program_resource_visitor::set_record_array_count(unsigned)
269 * Class to help calculate the storage requirements for a set of uniforms
271 * As uniforms are added to the active set the number of active uniforms and
272 * the storage requirements for those uniforms are accumulated. The active
273 * uniforms are added to the hash table supplied to the constructor.
275 * If the same uniform is added multiple times (i.e., once for each shader
276 * target), it will only be accounted once.
278 class count_uniform_size
: public program_resource_visitor
{
280 count_uniform_size(struct string_to_uint_map
*map
,
281 struct string_to_uint_map
*hidden_map
)
282 : num_active_uniforms(0), num_hidden_uniforms(0), num_values(0),
283 num_shader_samplers(0), num_shader_images(0),
284 num_shader_uniform_components(0), num_shader_subroutines(0),
285 is_buffer_block(false), is_shader_storage(false), map(map
),
286 hidden_map(hidden_map
)
293 this->num_shader_samplers
= 0;
294 this->num_shader_images
= 0;
295 this->num_shader_uniform_components
= 0;
296 this->num_shader_subroutines
= 0;
299 void process(ir_variable
*var
)
301 this->current_var
= var
;
302 this->is_buffer_block
= var
->is_in_buffer_block();
303 this->is_shader_storage
= var
->is_in_shader_storage_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
;
316 unsigned num_hidden_uniforms
;
319 * Number of data values required to back the storage for the active uniforms
324 * Number of samplers used
326 unsigned num_shader_samplers
;
329 * Number of images used
331 unsigned num_shader_images
;
334 * Number of uniforms used in the current shader
336 unsigned num_shader_uniform_components
;
339 * Number of subroutine uniforms used
341 unsigned num_shader_subroutines
;
343 bool is_buffer_block
;
344 bool is_shader_storage
;
346 struct string_to_uint_map
*map
;
349 virtual void visit_field(const glsl_type
*type
, const char *name
,
352 assert(!type
->without_array()->is_record());
353 assert(!type
->without_array()->is_interface());
354 assert(!(type
->is_array() && type
->fields
.array
->is_array()));
358 /* Count the number of samplers regardless of whether the uniform is
359 * already in the hash table. The hash table prevents adding the same
360 * uniform for multiple shader targets, but in this case we want to
361 * count it for each shader target.
363 const unsigned values
= values_for_type(type
);
364 if (type
->contains_subroutine()) {
365 this->num_shader_subroutines
+= values
;
366 } else if (type
->contains_sampler()) {
367 this->num_shader_samplers
+= values
;
368 } else if (type
->contains_image()) {
369 this->num_shader_images
+= values
;
371 /* As drivers are likely to represent image uniforms as
372 * scalar indices, count them against the limit of uniform
373 * components in the default block. The spec allows image
374 * uniforms to use up no more than one scalar slot.
376 if(!is_shader_storage
)
377 this->num_shader_uniform_components
+= values
;
379 /* Accumulate the total number of uniform slots used by this shader.
380 * Note that samplers do not count against this limit because they
381 * don't use any storage on current hardware.
383 if (!is_buffer_block
)
384 this->num_shader_uniform_components
+= values
;
387 /* If the uniform is already in the map, there's nothing more to do.
390 if (this->map
->get(id
, name
))
393 if (this->current_var
->data
.how_declared
== ir_var_hidden
) {
394 this->hidden_map
->put(this->num_hidden_uniforms
, name
);
395 this->num_hidden_uniforms
++;
397 this->map
->put(this->num_active_uniforms
-this->num_hidden_uniforms
,
401 /* Each leaf uniform occupies one entry in the list of active
404 this->num_active_uniforms
++;
406 if(!is_gl_identifier(name
) && !is_shader_storage
&& !is_buffer_block
)
407 this->num_values
+= values
;
410 struct string_to_uint_map
*hidden_map
;
413 * Current variable being processed.
415 ir_variable
*current_var
;
418 } /* anonymous namespace */
421 * Class to help parcel out pieces of backing storage to uniforms
423 * Each uniform processed has some range of the \c gl_constant_value
424 * structures associated with it. The association is done by finding
425 * the uniform in the \c string_to_uint_map and using the value from
426 * the map to connect that slot in the \c gl_uniform_storage table
427 * with the next available slot in the \c gl_constant_value array.
430 * This class assumes that every uniform that will be processed is
431 * already in the \c string_to_uint_map. In addition, it assumes that
432 * the \c gl_uniform_storage and \c gl_constant_value arrays are "big
435 class parcel_out_uniform_storage
: public program_resource_visitor
{
437 parcel_out_uniform_storage(struct gl_shader_program
*prog
,
438 struct string_to_uint_map
*map
,
439 struct gl_uniform_storage
*uniforms
,
440 union gl_constant_value
*values
)
441 : prog(prog
), map(map
), uniforms(uniforms
), values(values
)
445 void start_shader(gl_shader_stage shader_type
)
447 assert(shader_type
< MESA_SHADER_STAGES
);
448 this->shader_type
= shader_type
;
450 this->shader_samplers_used
= 0;
451 this->shader_shadow_samplers
= 0;
452 this->next_sampler
= 0;
453 this->next_image
= 0;
454 this->next_subroutine
= 0;
455 this->record_array_count
= 1;
456 memset(this->targets
, 0, sizeof(this->targets
));
459 void set_and_process(ir_variable
*var
)
463 this->record_next_sampler
= new string_to_uint_map
;
465 buffer_block_index
= -1;
466 if (var
->is_in_buffer_block()) {
467 struct gl_uniform_block
*blks
= var
->is_in_shader_storage_block() ?
468 prog
->ShaderStorageBlocks
: prog
->UniformBlocks
;
469 unsigned num_blks
= var
->is_in_shader_storage_block() ?
470 prog
->NumShaderStorageBlocks
: prog
->NumUniformBlocks
;
472 if (var
->is_interface_instance() && var
->type
->is_array()) {
473 unsigned l
= strlen(var
->get_interface_type()->name
);
475 for (unsigned i
= 0; i
< num_blks
; i
++) {
476 if (strncmp(var
->get_interface_type()->name
, blks
[i
].Name
, l
)
477 == 0 && blks
[i
].Name
[l
] == '[') {
478 buffer_block_index
= i
;
483 for (unsigned i
= 0; i
< num_blks
; i
++) {
484 if (strcmp(var
->get_interface_type()->name
, blks
[i
].Name
) ==
486 buffer_block_index
= i
;
491 assert(buffer_block_index
!= -1);
493 /* Uniform blocks that were specified with an instance name must be
494 * handled a little bit differently. The name of the variable is the
495 * name used to reference the uniform block instead of being the name
496 * of a variable within the block. Therefore, searching for the name
497 * within the block will fail.
499 if (var
->is_interface_instance()) {
501 process(var
->get_interface_type(),
502 var
->get_interface_type()->name
);
504 const struct gl_uniform_block
*const block
=
505 &blks
[buffer_block_index
];
507 assert(var
->data
.location
!= -1);
509 const struct gl_uniform_buffer_variable
*const ubo_var
=
510 &block
->Uniforms
[var
->data
.location
];
512 ubo_byte_offset
= ubo_var
->Offset
;
516 /* Store any explicit location and reset data location so we can
517 * reuse this variable for storing the uniform slot number.
519 this->explicit_location
= current_var
->data
.location
;
520 current_var
->data
.location
= -1;
524 delete this->record_next_sampler
;
527 int buffer_block_index
;
529 gl_shader_stage shader_type
;
532 void handle_samplers(const glsl_type
*base_type
,
533 struct gl_uniform_storage
*uniform
, const char *name
)
535 if (base_type
->is_sampler()) {
536 uniform
->opaque
[shader_type
].active
= true;
538 /* Handle multiple samplers inside struct arrays */
539 if (this->record_array_count
> 1) {
540 unsigned inner_array_size
= MAX2(1, uniform
->array_elements
);
541 char *name_copy
= ralloc_strdup(NULL
, name
);
543 /* Remove all array subscripts from the sampler name */
546 while((str_start
= strchr(name_copy
, '[')) &&
547 (str_end
= strchr(name_copy
, ']'))) {
548 memmove(str_start
, str_end
+ 1, 1 + strlen(str_end
));
552 if (this->record_next_sampler
->get(index
, name_copy
)) {
553 /* In this case, we've already seen this uniform so we just use
554 * the next sampler index recorded the last time we visited.
556 uniform
->opaque
[shader_type
].index
= index
;
557 index
= inner_array_size
+ uniform
->opaque
[shader_type
].index
;
558 this->record_next_sampler
->put(index
, name_copy
);
560 ralloc_free(name_copy
);
561 /* Return as everything else has already been initialised in a
566 /* We've never seen this uniform before so we need to allocate
567 * enough indices to store it.
569 * Nested struct arrays behave like arrays of arrays so we need
570 * to increase the index by the total number of elements of the
571 * sampler in case there is more than one sampler inside the
572 * structs. This allows the offset to be easily calculated for
575 uniform
->opaque
[shader_type
].index
= this->next_sampler
;
576 this->next_sampler
+=
577 inner_array_size
* this->record_array_count
;
579 /* Store the next index for future passes over the struct array
581 index
= uniform
->opaque
[shader_type
].index
+ inner_array_size
;
582 this->record_next_sampler
->put(index
, name_copy
);
583 ralloc_free(name_copy
);
586 /* Increment the sampler by 1 for non-arrays and by the number of
587 * array elements for arrays.
589 uniform
->opaque
[shader_type
].index
= this->next_sampler
;
590 this->next_sampler
+= MAX2(1, uniform
->array_elements
);
593 const gl_texture_index target
= base_type
->sampler_index();
594 const unsigned shadow
= base_type
->sampler_shadow
;
595 for (unsigned i
= uniform
->opaque
[shader_type
].index
;
596 i
< MIN2(this->next_sampler
, MAX_SAMPLERS
);
598 this->targets
[i
] = target
;
599 this->shader_samplers_used
|= 1U << i
;
600 this->shader_shadow_samplers
|= shadow
<< i
;
605 void handle_images(const glsl_type
*base_type
,
606 struct gl_uniform_storage
*uniform
)
608 if (base_type
->is_image()) {
609 uniform
->opaque
[shader_type
].index
= this->next_image
;
610 uniform
->opaque
[shader_type
].active
= true;
612 /* Set image access qualifiers */
613 const GLenum access
=
614 (current_var
->data
.image_read_only
? GL_READ_ONLY
:
615 current_var
->data
.image_write_only
? GL_WRITE_ONLY
:
618 const unsigned first
= this->next_image
;
620 /* Increment the image index by 1 for non-arrays and by the
621 * number of array elements for arrays.
623 this->next_image
+= MAX2(1, uniform
->array_elements
);
625 for (unsigned i
= first
; i
< MIN2(next_image
, MAX_IMAGE_UNIFORMS
); i
++)
626 prog
->_LinkedShaders
[shader_type
]->ImageAccess
[i
] = access
;
630 void handle_subroutines(const glsl_type
*base_type
,
631 struct gl_uniform_storage
*uniform
)
633 if (base_type
->is_subroutine()) {
634 uniform
->opaque
[shader_type
].index
= this->next_subroutine
;
635 uniform
->opaque
[shader_type
].active
= true;
637 /* Increment the subroutine index by 1 for non-arrays and by the
638 * number of array elements for arrays.
640 this->next_subroutine
+= MAX2(1, uniform
->array_elements
);
645 virtual void set_buffer_offset(unsigned offset
)
647 this->ubo_byte_offset
= offset
;
650 virtual void set_record_array_count(unsigned record_array_count
)
652 this->record_array_count
= record_array_count
;
655 virtual void visit_field(const glsl_type
*type
, const char *name
,
661 assert(!"Should not get here.");
664 virtual void enter_record(const glsl_type
*type
, const char *,
665 bool row_major
, const enum glsl_interface_packing packing
) {
666 assert(type
->is_record());
667 if (this->buffer_block_index
== -1)
669 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
670 this->ubo_byte_offset
= glsl_align(
671 this->ubo_byte_offset
, type
->std430_base_alignment(row_major
));
673 this->ubo_byte_offset
= glsl_align(
674 this->ubo_byte_offset
, type
->std140_base_alignment(row_major
));
677 virtual void leave_record(const glsl_type
*type
, const char *,
678 bool row_major
, const enum glsl_interface_packing packing
) {
679 assert(type
->is_record());
680 if (this->buffer_block_index
== -1)
682 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
683 this->ubo_byte_offset
= glsl_align(
684 this->ubo_byte_offset
, type
->std430_base_alignment(row_major
));
686 this->ubo_byte_offset
= glsl_align(
687 this->ubo_byte_offset
, type
->std140_base_alignment(row_major
));
690 virtual void visit_field(const glsl_type
*type
, const char *name
,
691 bool row_major
, const glsl_type
* /* record_type */,
692 const enum glsl_interface_packing packing
,
693 bool /* last_field */)
695 assert(!type
->without_array()->is_record());
696 assert(!type
->without_array()->is_interface());
697 assert(!(type
->is_array() && type
->fields
.array
->is_array()));
700 bool found
= this->map
->get(id
, name
);
706 const glsl_type
*base_type
;
707 if (type
->is_array()) {
708 this->uniforms
[id
].array_elements
= type
->length
;
709 base_type
= type
->fields
.array
;
711 this->uniforms
[id
].array_elements
= 0;
715 /* Initialise opaque data */
716 this->uniforms
[id
].opaque
[shader_type
].index
= ~0;
717 this->uniforms
[id
].opaque
[shader_type
].active
= false;
719 /* This assigns uniform indices to sampler and image uniforms. */
720 handle_samplers(base_type
, &this->uniforms
[id
], name
);
721 handle_images(base_type
, &this->uniforms
[id
]);
722 handle_subroutines(base_type
, &this->uniforms
[id
]);
724 /* For array of arrays or struct arrays the base location may have
725 * already been set so don't set it again.
727 if (buffer_block_index
== -1 && current_var
->data
.location
== -1) {
728 current_var
->data
.location
= id
;
731 /* If there is already storage associated with this uniform or if the
732 * uniform is set as builtin, it means that it was set while processing
733 * an earlier shader stage. For example, we may be processing the
734 * uniform in the fragment shader, but the uniform was already processed
735 * in the vertex shader.
737 if (this->uniforms
[id
].storage
!= NULL
|| this->uniforms
[id
].builtin
) {
741 /* Assign explicit locations. */
742 if (current_var
->data
.explicit_location
) {
743 /* Set sequential locations for struct fields. */
744 if (current_var
->type
->without_array()->is_record() ||
745 current_var
->type
->is_array_of_arrays()) {
746 const unsigned entries
= MAX2(1, this->uniforms
[id
].array_elements
);
747 this->uniforms
[id
].remap_location
=
748 this->explicit_location
+ field_counter
;
749 field_counter
+= entries
;
751 this->uniforms
[id
].remap_location
= this->explicit_location
;
754 /* Initialize to to indicate that no location is set */
755 this->uniforms
[id
].remap_location
= UNMAPPED_UNIFORM_LOC
;
758 this->uniforms
[id
].name
= ralloc_strdup(this->uniforms
, name
);
759 this->uniforms
[id
].type
= base_type
;
760 this->uniforms
[id
].num_driver_storage
= 0;
761 this->uniforms
[id
].driver_storage
= NULL
;
762 this->uniforms
[id
].atomic_buffer_index
= -1;
763 this->uniforms
[id
].hidden
=
764 current_var
->data
.how_declared
== ir_var_hidden
;
765 this->uniforms
[id
].builtin
= is_gl_identifier(name
);
767 this->uniforms
[id
].is_shader_storage
=
768 current_var
->is_in_shader_storage_block();
770 /* Do not assign storage if the uniform is a builtin or buffer object */
771 if (!this->uniforms
[id
].builtin
&&
772 !this->uniforms
[id
].is_shader_storage
&&
773 this->buffer_block_index
== -1)
774 this->uniforms
[id
].storage
= this->values
;
776 if (this->buffer_block_index
!= -1) {
777 this->uniforms
[id
].block_index
= this->buffer_block_index
;
779 unsigned alignment
= type
->std140_base_alignment(row_major
);
780 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
781 alignment
= type
->std430_base_alignment(row_major
);
782 this->ubo_byte_offset
= glsl_align(this->ubo_byte_offset
, alignment
);
783 this->uniforms
[id
].offset
= this->ubo_byte_offset
;
784 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
785 this->ubo_byte_offset
+= type
->std430_size(row_major
);
787 this->ubo_byte_offset
+= type
->std140_size(row_major
);
789 if (type
->is_array()) {
790 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
791 this->uniforms
[id
].array_stride
=
792 type
->without_array()->std430_array_stride(row_major
);
794 this->uniforms
[id
].array_stride
=
795 glsl_align(type
->without_array()->std140_size(row_major
),
798 this->uniforms
[id
].array_stride
= 0;
801 if (type
->without_array()->is_matrix()) {
802 const glsl_type
*matrix
= type
->without_array();
803 const unsigned N
= matrix
->base_type
== GLSL_TYPE_DOUBLE
? 8 : 4;
804 const unsigned items
=
805 row_major
? matrix
->matrix_columns
: matrix
->vector_elements
;
808 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
809 this->uniforms
[id
].matrix_stride
= items
< 3 ? items
* N
:
810 glsl_align(items
* N
, 16);
812 this->uniforms
[id
].matrix_stride
= glsl_align(items
* N
, 16);
813 this->uniforms
[id
].row_major
= row_major
;
815 this->uniforms
[id
].matrix_stride
= 0;
816 this->uniforms
[id
].row_major
= false;
819 this->uniforms
[id
].block_index
= -1;
820 this->uniforms
[id
].offset
= -1;
821 this->uniforms
[id
].array_stride
= -1;
822 this->uniforms
[id
].matrix_stride
= -1;
823 this->uniforms
[id
].row_major
= false;
826 if (!this->uniforms
[id
].builtin
&&
827 !this->uniforms
[id
].is_shader_storage
&&
828 this->buffer_block_index
== -1)
829 this->values
+= values_for_type(type
);
833 * Current program being processed.
835 struct gl_shader_program
*prog
;
837 struct string_to_uint_map
*map
;
839 struct gl_uniform_storage
*uniforms
;
840 unsigned next_sampler
;
842 unsigned next_subroutine
;
845 * Field counter is used to take care that uniform structures
846 * with explicit locations get sequential locations.
848 unsigned field_counter
;
851 * Current variable being processed.
853 ir_variable
*current_var
;
855 /* Used to store the explicit location from current_var so that we can
856 * reuse the location field for storing the uniform slot id.
858 int explicit_location
;
860 /* Stores total struct array elements including nested structs */
861 unsigned record_array_count
;
863 /* Map for temporarily storing next sampler index when handling samplers in
866 struct string_to_uint_map
*record_next_sampler
;
869 union gl_constant_value
*values
;
871 gl_texture_index targets
[MAX_SAMPLERS
];
874 * Mask of samplers used by the current shader stage.
876 unsigned shader_samplers_used
;
879 * Mask of samplers used by the current shader stage for shadows.
881 unsigned shader_shadow_samplers
;
885 * Walks the IR and update the references to uniform blocks in the
886 * ir_variables to point at linked shader's list (previously, they
887 * would point at the uniform block list in one of the pre-linked
891 link_update_uniform_buffer_variables(struct gl_linked_shader
*shader
)
893 foreach_in_list(ir_instruction
, node
, shader
->ir
) {
894 ir_variable
*const var
= node
->as_variable();
896 if ((var
== NULL
) || !var
->is_in_buffer_block())
899 assert(var
->data
.mode
== ir_var_uniform
||
900 var
->data
.mode
== ir_var_shader_storage
);
902 if (var
->is_interface_instance()) {
903 var
->data
.location
= 0;
908 char sentinel
= '\0';
910 if (var
->type
->is_record()) {
912 } else if (var
->type
->is_array() && (var
->type
->fields
.array
->is_array()
913 || var
->type
->without_array()->is_record())) {
917 unsigned num_blocks
= var
->data
.mode
== ir_var_uniform
?
918 shader
->NumUniformBlocks
: shader
->NumShaderStorageBlocks
;
919 struct gl_uniform_block
**blks
= var
->data
.mode
== ir_var_uniform
?
920 shader
->UniformBlocks
: shader
->ShaderStorageBlocks
;
922 const unsigned l
= strlen(var
->name
);
923 for (unsigned i
= 0; i
< num_blocks
; i
++) {
924 for (unsigned j
= 0; j
< blks
[i
]->NumUniforms
; j
++) {
926 const char *begin
= blks
[i
]->Uniforms
[j
].Name
;
927 const char *end
= strchr(begin
, sentinel
);
932 if ((ptrdiff_t) l
!= (end
- begin
))
935 if (strncmp(var
->name
, begin
, l
) == 0) {
937 var
->data
.location
= j
;
940 } else if (!strcmp(var
->name
, blks
[i
]->Uniforms
[j
].Name
)) {
942 var
->data
.location
= j
;
954 * Combine the hidden uniform hash map with the uniform hash map so that the
955 * hidden uniforms will be given indicies at the end of the uniform storage
959 assign_hidden_uniform_slot_id(const char *name
, unsigned hidden_id
,
962 count_uniform_size
*uniform_size
= (count_uniform_size
*) closure
;
963 unsigned hidden_uniform_start
= uniform_size
->num_active_uniforms
-
964 uniform_size
->num_hidden_uniforms
;
966 uniform_size
->map
->put(hidden_uniform_start
+ hidden_id
, name
);
970 * Search through the list of empty blocks to find one that fits the current
974 find_empty_block(struct gl_shader_program
*prog
,
975 struct gl_uniform_storage
*uniform
)
977 const unsigned entries
= MAX2(1, uniform
->array_elements
);
979 foreach_list_typed(struct empty_uniform_block
, block
, link
,
980 &prog
->EmptyUniformLocations
) {
981 /* Found a block with enough slots to fit the uniform */
982 if (block
->slots
== entries
) {
983 unsigned start
= block
->start
;
984 exec_node_remove(&block
->link
);
988 /* Found a block with more slots than needed. It can still be used. */
989 } else if (block
->slots
> entries
) {
990 unsigned start
= block
->start
;
991 block
->start
+= entries
;
992 block
->slots
-= entries
;
1002 link_assign_uniform_locations(struct gl_shader_program
*prog
,
1003 unsigned int boolean_true
,
1004 unsigned int num_explicit_uniform_locs
,
1005 unsigned int max_uniform_locs
)
1007 ralloc_free(prog
->UniformStorage
);
1008 prog
->UniformStorage
= NULL
;
1009 prog
->NumUniformStorage
= 0;
1011 if (prog
->UniformHash
!= NULL
) {
1012 prog
->UniformHash
->clear();
1014 prog
->UniformHash
= new string_to_uint_map
;
1017 /* First pass: Count the uniform resources used by the user-defined
1018 * uniforms. While this happens, each active uniform will have an index
1021 * Note: this is *NOT* the index that is returned to the application by
1022 * glGetUniformLocation.
1024 struct string_to_uint_map
*hiddenUniforms
= new string_to_uint_map
;
1025 count_uniform_size
uniform_size(prog
->UniformHash
, hiddenUniforms
);
1026 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1027 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
1032 /* Uniforms that lack an initializer in the shader code have an initial
1033 * value of zero. This includes sampler uniforms.
1035 * Page 24 (page 30 of the PDF) of the GLSL 1.20 spec says:
1037 * "The link time initial value is either the value of the variable's
1038 * initializer, if present, or 0 if no initializer is present. Sampler
1039 * types cannot have initializers."
1041 memset(sh
->SamplerUnits
, 0, sizeof(sh
->SamplerUnits
));
1042 memset(sh
->ImageUnits
, 0, sizeof(sh
->ImageUnits
));
1044 link_update_uniform_buffer_variables(sh
);
1046 /* Reset various per-shader target counts.
1048 uniform_size
.start_shader();
1050 foreach_in_list(ir_instruction
, node
, sh
->ir
) {
1051 ir_variable
*const var
= node
->as_variable();
1053 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
&&
1054 var
->data
.mode
!= ir_var_shader_storage
))
1057 uniform_size
.process(var
);
1060 sh
->num_samplers
= uniform_size
.num_shader_samplers
;
1061 sh
->NumImages
= uniform_size
.num_shader_images
;
1062 sh
->num_uniform_components
= uniform_size
.num_shader_uniform_components
;
1063 sh
->num_combined_uniform_components
= sh
->num_uniform_components
;
1065 for (unsigned i
= 0; i
< sh
->NumUniformBlocks
; i
++) {
1066 sh
->num_combined_uniform_components
+=
1067 sh
->UniformBlocks
[i
]->UniformBufferSize
/ 4;
1071 const unsigned num_uniforms
= uniform_size
.num_active_uniforms
;
1072 const unsigned num_data_slots
= uniform_size
.num_values
;
1073 const unsigned hidden_uniforms
= uniform_size
.num_hidden_uniforms
;
1075 /* assign hidden uniforms a slot id */
1076 hiddenUniforms
->iterate(assign_hidden_uniform_slot_id
, &uniform_size
);
1077 delete hiddenUniforms
;
1079 /* On the outside chance that there were no uniforms, bail out.
1081 if (num_uniforms
== 0)
1084 struct gl_uniform_storage
*uniforms
=
1085 rzalloc_array(prog
, struct gl_uniform_storage
, num_uniforms
);
1086 union gl_constant_value
*data
=
1087 rzalloc_array(uniforms
, union gl_constant_value
, num_data_slots
);
1089 union gl_constant_value
*data_end
= &data
[num_data_slots
];
1092 parcel_out_uniform_storage
parcel(prog
, prog
->UniformHash
, uniforms
, data
);
1094 unsigned total_entries
= num_explicit_uniform_locs
;
1095 unsigned empty_locs
= prog
->NumUniformRemapTable
- num_explicit_uniform_locs
;
1097 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
1098 if (prog
->_LinkedShaders
[i
] == NULL
)
1101 parcel
.start_shader((gl_shader_stage
)i
);
1103 foreach_in_list(ir_instruction
, node
, prog
->_LinkedShaders
[i
]->ir
) {
1104 ir_variable
*const var
= node
->as_variable();
1106 if ((var
== NULL
) || (var
->data
.mode
!= ir_var_uniform
&&
1107 var
->data
.mode
!= ir_var_shader_storage
))
1110 parcel
.set_and_process(var
);
1113 prog
->_LinkedShaders
[i
]->active_samplers
= parcel
.shader_samplers_used
;
1114 prog
->_LinkedShaders
[i
]->shadow_samplers
= parcel
.shader_shadow_samplers
;
1116 STATIC_ASSERT(sizeof(prog
->_LinkedShaders
[i
]->SamplerTargets
) ==
1117 sizeof(parcel
.targets
));
1118 memcpy(prog
->_LinkedShaders
[i
]->SamplerTargets
, parcel
.targets
,
1119 sizeof(prog
->_LinkedShaders
[i
]->SamplerTargets
));
1122 /* Reserve all the explicit locations of the active uniforms. */
1123 for (unsigned i
= 0; i
< num_uniforms
; i
++) {
1124 if (uniforms
[i
].type
->is_subroutine() ||
1125 uniforms
[i
].is_shader_storage
)
1128 if (uniforms
[i
].remap_location
!= UNMAPPED_UNIFORM_LOC
) {
1129 /* How many new entries for this uniform? */
1130 const unsigned entries
= MAX2(1, uniforms
[i
].array_elements
);
1132 /* Set remap table entries point to correct gl_uniform_storage. */
1133 for (unsigned j
= 0; j
< entries
; j
++) {
1134 unsigned element_loc
= uniforms
[i
].remap_location
+ j
;
1135 assert(prog
->UniformRemapTable
[element_loc
] ==
1136 INACTIVE_UNIFORM_EXPLICIT_LOCATION
);
1137 prog
->UniformRemapTable
[element_loc
] = &uniforms
[i
];
1142 /* Reserve locations for rest of the uniforms. */
1143 for (unsigned i
= 0; i
< num_uniforms
; i
++) {
1145 if (uniforms
[i
].type
->is_subroutine() ||
1146 uniforms
[i
].is_shader_storage
)
1149 /* Built-in uniforms should not get any location. */
1150 if (uniforms
[i
].builtin
)
1153 /* Explicit ones have been set already. */
1154 if (uniforms
[i
].remap_location
!= UNMAPPED_UNIFORM_LOC
)
1157 /* how many new entries for this uniform? */
1158 const unsigned entries
= MAX2(1, uniforms
[i
].array_elements
);
1160 /* Find UniformRemapTable for empty blocks where we can fit this uniform. */
1161 int chosen_location
= -1;
1164 chosen_location
= find_empty_block(prog
, &uniforms
[i
]);
1166 /* Add new entries to the total amount of entries. */
1167 total_entries
+= entries
;
1169 if (chosen_location
!= -1) {
1170 empty_locs
-= entries
;
1172 chosen_location
= prog
->NumUniformRemapTable
;
1174 /* resize remap table to fit new entries */
1175 prog
->UniformRemapTable
=
1177 prog
->UniformRemapTable
,
1178 gl_uniform_storage
*,
1179 prog
->NumUniformRemapTable
+ entries
);
1180 prog
->NumUniformRemapTable
+= entries
;
1183 /* set pointers for this uniform */
1184 for (unsigned j
= 0; j
< entries
; j
++)
1185 prog
->UniformRemapTable
[chosen_location
+ j
] = &uniforms
[i
];
1187 /* set the base location in remap table for the uniform */
1188 uniforms
[i
].remap_location
= chosen_location
;
1191 /* Verify that total amount of entries for explicit and implicit locations
1192 * is less than MAX_UNIFORM_LOCATIONS.
1195 if (total_entries
> max_uniform_locs
) {
1196 linker_error(prog
, "count of uniform locations > MAX_UNIFORM_LOCATIONS"
1197 "(%u > %u)", total_entries
, max_uniform_locs
);
1200 /* Reserve all the explicit locations of the active subroutine uniforms. */
1201 for (unsigned i
= 0; i
< num_uniforms
; i
++) {
1202 if (!uniforms
[i
].type
->is_subroutine())
1205 if (uniforms
[i
].remap_location
== UNMAPPED_UNIFORM_LOC
)
1208 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; j
++) {
1209 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[j
];
1213 if (!uniforms
[i
].opaque
[j
].active
)
1216 /* How many new entries for this uniform? */
1217 const unsigned entries
= MAX2(1, uniforms
[i
].array_elements
);
1219 /* Set remap table entries point to correct gl_uniform_storage. */
1220 for (unsigned k
= 0; k
< entries
; k
++) {
1221 unsigned element_loc
= uniforms
[i
].remap_location
+ k
;
1222 assert(sh
->SubroutineUniformRemapTable
[element_loc
] ==
1223 INACTIVE_UNIFORM_EXPLICIT_LOCATION
);
1224 sh
->SubroutineUniformRemapTable
[element_loc
] = &uniforms
[i
];
1229 /* reserve subroutine locations */
1230 for (unsigned i
= 0; i
< num_uniforms
; i
++) {
1232 if (!uniforms
[i
].type
->is_subroutine())
1234 const unsigned entries
= MAX2(1, uniforms
[i
].array_elements
);
1236 if (uniforms
[i
].remap_location
!= UNMAPPED_UNIFORM_LOC
)
1238 for (unsigned j
= 0; j
< MESA_SHADER_STAGES
; j
++) {
1239 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[j
];
1243 if (!uniforms
[i
].opaque
[j
].active
)
1246 sh
->SubroutineUniformRemapTable
=
1248 sh
->SubroutineUniformRemapTable
,
1249 gl_uniform_storage
*,
1250 sh
->NumSubroutineUniformRemapTable
+ entries
);
1252 for (unsigned k
= 0; k
< entries
; k
++)
1253 sh
->SubroutineUniformRemapTable
[sh
->NumSubroutineUniformRemapTable
+ k
] = &uniforms
[i
];
1254 uniforms
[i
].remap_location
= sh
->NumSubroutineUniformRemapTable
;
1255 sh
->NumSubroutineUniformRemapTable
+= entries
;
1260 for (unsigned i
= 0; i
< num_uniforms
; i
++) {
1261 assert(uniforms
[i
].storage
!= NULL
|| uniforms
[i
].builtin
||
1262 uniforms
[i
].is_shader_storage
||
1263 uniforms
[i
].block_index
!= -1);
1266 assert(parcel
.values
== data_end
);
1269 prog
->NumUniformStorage
= num_uniforms
;
1270 prog
->NumHiddenUniforms
= hidden_uniforms
;
1271 prog
->UniformStorage
= uniforms
;
1273 link_set_uniform_initializers(prog
, boolean_true
);