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);
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 (t
->is_record() || (t
->is_array() && t
->fields
.array
->is_record())) {
79 char *name
= ralloc_strdup(NULL
, var
->name
);
80 recursion(var
->type
, &name
, strlen(name
), false);
82 } else if (t
->is_interface()) {
83 char *name
= ralloc_strdup(NULL
, var
->type
->name
);
84 recursion(var
->type
, &name
, strlen(name
), false);
86 } else if (t
->is_array() && t
->fields
.array
->is_interface()) {
87 char *name
= ralloc_strdup(NULL
, var
->type
->fields
.array
->name
);
88 recursion(var
->type
, &name
, strlen(name
), false);
91 this->visit_field(t
, var
->name
, false);
96 program_resource_visitor::recursion(const glsl_type
*t
, char **name
,
97 size_t name_length
, bool row_major
)
99 /* Records need to have each field processed individually.
101 * Arrays of records need to have each array element processed
102 * individually, then each field of the resulting array elements processed
105 if (t
->is_record() || t
->is_interface()) {
106 for (unsigned i
= 0; i
< t
->length
; i
++) {
107 const char *field
= t
->fields
.structure
[i
].name
;
108 size_t new_length
= name_length
;
110 if (t
->fields
.structure
[i
].type
->is_record())
111 this->visit_field(&t
->fields
.structure
[i
]);
113 /* Append '.field' to the current variable name. */
114 if (name_length
== 0) {
115 ralloc_asprintf_rewrite_tail(name
, &new_length
, "%s", field
);
117 ralloc_asprintf_rewrite_tail(name
, &new_length
, ".%s", field
);
120 recursion(t
->fields
.structure
[i
].type
, name
, new_length
,
121 t
->fields
.structure
[i
].row_major
);
123 } else if (t
->is_array() && (t
->fields
.array
->is_record()
124 || t
->fields
.array
->is_interface())) {
125 for (unsigned i
= 0; i
< t
->length
; i
++) {
126 size_t new_length
= name_length
;
128 /* Append the subscript to the current variable name */
129 ralloc_asprintf_rewrite_tail(name
, &new_length
, "[%u]", i
);
131 recursion(t
->fields
.array
, name
, new_length
,
132 t
->fields
.structure
[i
].row_major
);
135 this->visit_field(t
, *name
, row_major
);
140 program_resource_visitor::visit_field(const glsl_struct_field
*field
)
147 * Class to help calculate the storage requirements for a set of uniforms
149 * As uniforms are added to the active set the number of active uniforms and
150 * the storage requirements for those uniforms are accumulated. The active
151 * uniforms are added the the hash table supplied to the constructor.
153 * If the same uniform is added multiple times (i.e., once for each shader
154 * target), it will only be accounted once.
156 class count_uniform_size
: public program_resource_visitor
{
158 count_uniform_size(struct string_to_uint_map
*map
)
159 : num_active_uniforms(0), num_values(0), num_shader_samplers(0),
160 num_shader_uniform_components(0), map(map
)
167 this->num_shader_samplers
= 0;
168 this->num_shader_uniform_components
= 0;
171 void process(ir_variable
*var
)
173 if (var
->is_interface_instance())
174 program_resource_visitor::process(var
->interface_type
,
175 var
->interface_type
->name
);
177 program_resource_visitor::process(var
);
181 * Total number of active uniforms counted
183 unsigned num_active_uniforms
;
186 * Number of data values required to back the storage for the active uniforms
191 * Number of samplers used
193 unsigned num_shader_samplers
;
196 * Number of uniforms used in the current shader
198 unsigned num_shader_uniform_components
;
201 virtual void visit_field(const glsl_type
*type
, const char *name
,
204 assert(!type
->is_record());
205 assert(!(type
->is_array() && type
->fields
.array
->is_record()));
206 assert(!type
->is_interface());
207 assert(!(type
->is_array() && type
->fields
.array
->is_interface()));
211 /* Count the number of samplers regardless of whether the uniform is
212 * already in the hash table. The hash table prevents adding the same
213 * uniform for multiple shader targets, but in this case we want to
214 * count it for each shader target.
216 const unsigned values
= values_for_type(type
);
217 if (type
->contains_sampler()) {
218 this->num_shader_samplers
+=
219 type
->is_array() ? type
->array_size() : 1;
221 /* Accumulate the total number of uniform slots used by this shader.
222 * Note that samplers do not count against this limit because they
223 * don't use any storage on current hardware.
225 this->num_shader_uniform_components
+= values
;
228 /* If the uniform is already in the map, there's nothing more to do.
231 if (this->map
->get(id
, name
))
234 this->map
->put(this->num_active_uniforms
, name
);
236 /* Each leaf uniform occupies one entry in the list of active
239 this->num_active_uniforms
++;
240 this->num_values
+= values
;
243 struct string_to_uint_map
*map
;
247 * Class to help parcel out pieces of backing storage to uniforms
249 * Each uniform processed has some range of the \c gl_constant_value
250 * structures associated with it. The association is done by finding
251 * the uniform in the \c string_to_uint_map and using the value from
252 * the map to connect that slot in the \c gl_uniform_storage table
253 * with the next available slot in the \c gl_constant_value array.
256 * This class assumes that every uniform that will be processed is
257 * already in the \c string_to_uint_map. In addition, it assumes that
258 * the \c gl_uniform_storage and \c gl_constant_value arrays are "big
261 class parcel_out_uniform_storage
: public program_resource_visitor
{
263 parcel_out_uniform_storage(struct string_to_uint_map
*map
,
264 struct gl_uniform_storage
*uniforms
,
265 union gl_constant_value
*values
)
266 : map(map
), uniforms(uniforms
), next_sampler(0), values(values
)
268 memset(this->targets
, 0, sizeof(this->targets
));
273 this->shader_samplers_used
= 0;
274 this->shader_shadow_samplers
= 0;
277 void set_and_process(struct gl_shader_program
*prog
,
280 ubo_block_index
= -1;
281 if (var
->is_in_uniform_block()) {
282 if (var
->is_interface_instance() && var
->type
->is_array()) {
283 unsigned l
= strlen(var
->interface_type
->name
);
285 for (unsigned i
= 0; i
< prog
->NumUniformBlocks
; i
++) {
286 if (strncmp(var
->interface_type
->name
,
287 prog
->UniformBlocks
[i
].Name
,
289 && prog
->UniformBlocks
[i
].Name
[l
] == '[') {
295 for (unsigned i
= 0; i
< prog
->NumUniformBlocks
; i
++) {
296 if (strcmp(var
->interface_type
->name
,
297 prog
->UniformBlocks
[i
].Name
) == 0) {
303 assert(ubo_block_index
!= -1);
305 /* Uniform blocks that were specified with an instance name must be
306 * handled a little bit differently. The name of the variable is the
307 * name used to reference the uniform block instead of being the name
308 * of a variable within the block. Therefore, searching for the name
309 * within the block will fail.
311 if (var
->is_interface_instance()) {
313 ubo_row_major
= false;
315 const struct gl_uniform_block
*const block
=
316 &prog
->UniformBlocks
[ubo_block_index
];
318 assert(var
->location
!= -1);
320 const struct gl_uniform_buffer_variable
*const ubo_var
=
321 &block
->Uniforms
[var
->location
];
323 ubo_row_major
= ubo_var
->RowMajor
;
324 ubo_byte_offset
= ubo_var
->Offset
;
327 if (var
->is_interface_instance())
328 process(var
->interface_type
, var
->interface_type
->name
);
340 virtual void visit_field(const glsl_type
*type
, const char *name
,
343 assert(!type
->is_record());
344 assert(!(type
->is_array() && type
->fields
.array
->is_record()));
345 assert(!type
->is_interface());
346 assert(!(type
->is_array() && type
->fields
.array
->is_interface()));
351 bool found
= this->map
->get(id
, name
);
357 /* If there is already storage associated with this uniform, it means
358 * that it was set while processing an earlier shader stage. For
359 * example, we may be processing the uniform in the fragment shader, but
360 * the uniform was already processed in the vertex shader.
362 if (this->uniforms
[id
].storage
!= NULL
) {
363 /* If the uniform already has storage set from another shader stage,
364 * mark the samplers used for this shader stage.
366 if (type
->contains_sampler()) {
367 const unsigned count
= MAX2(1, this->uniforms
[id
].array_elements
);
368 const unsigned shadow
= (type
->is_array())
369 ? type
->fields
.array
->sampler_shadow
: type
->sampler_shadow
;
371 for (unsigned i
= 0; i
< count
; i
++) {
372 const unsigned s
= this->uniforms
[id
].sampler
+ i
;
374 this->shader_samplers_used
|= 1U << s
;
375 this->shader_shadow_samplers
|= shadow
<< s
;
382 const glsl_type
*base_type
;
383 if (type
->is_array()) {
384 this->uniforms
[id
].array_elements
= type
->length
;
385 base_type
= type
->fields
.array
;
387 this->uniforms
[id
].array_elements
= 0;
391 if (base_type
->is_sampler()) {
392 this->uniforms
[id
].sampler
= this->next_sampler
;
394 /* Increment the sampler by 1 for non-arrays and by the number of
395 * array elements for arrays.
397 this->next_sampler
+= MAX2(1, this->uniforms
[id
].array_elements
);
399 const gl_texture_index target
= base_type
->sampler_index();
400 const unsigned shadow
= base_type
->sampler_shadow
;
401 for (unsigned i
= this->uniforms
[id
].sampler
402 ; i
< MIN2(this->next_sampler
, MAX_SAMPLERS
)
404 this->targets
[i
] = target
;
405 this->shader_samplers_used
|= 1U << i
;
406 this->shader_shadow_samplers
|= shadow
<< i
;
410 this->uniforms
[id
].sampler
= ~0;
413 this->uniforms
[id
].name
= ralloc_strdup(this->uniforms
, name
);
414 this->uniforms
[id
].type
= base_type
;
415 this->uniforms
[id
].initialized
= 0;
416 this->uniforms
[id
].num_driver_storage
= 0;
417 this->uniforms
[id
].driver_storage
= NULL
;
418 this->uniforms
[id
].storage
= this->values
;
419 if (this->ubo_block_index
!= -1) {
420 this->uniforms
[id
].block_index
= this->ubo_block_index
;
422 unsigned alignment
= type
->std140_base_alignment(ubo_row_major
);
423 this->ubo_byte_offset
= glsl_align(this->ubo_byte_offset
, alignment
);
424 this->uniforms
[id
].offset
= this->ubo_byte_offset
;
425 this->ubo_byte_offset
+= type
->std140_size(ubo_row_major
);
427 if (type
->is_array()) {
428 this->uniforms
[id
].array_stride
=
429 glsl_align(type
->fields
.array
->std140_size(ubo_row_major
), 16);
431 this->uniforms
[id
].array_stride
= 0;
434 if (type
->is_matrix() ||
435 (type
->is_array() && type
->fields
.array
->is_matrix())) {
436 this->uniforms
[id
].matrix_stride
= 16;
437 this->uniforms
[id
].row_major
= ubo_row_major
;
439 this->uniforms
[id
].matrix_stride
= 0;
440 this->uniforms
[id
].row_major
= false;
443 this->uniforms
[id
].block_index
= -1;
444 this->uniforms
[id
].offset
= -1;
445 this->uniforms
[id
].array_stride
= -1;
446 this->uniforms
[id
].matrix_stride
= -1;
447 this->uniforms
[id
].row_major
= false;
450 this->values
+= values_for_type(type
);
453 struct string_to_uint_map
*map
;
455 struct gl_uniform_storage
*uniforms
;
456 unsigned next_sampler
;
459 union gl_constant_value
*values
;
461 gl_texture_index targets
[MAX_SAMPLERS
];
464 * Mask of samplers used by the current shader stage.
466 unsigned shader_samplers_used
;
469 * Mask of samplers used by the current shader stage for shadows.
471 unsigned shader_shadow_samplers
;
475 * Merges a uniform block into an array of uniform blocks that may or
476 * may not already contain a copy of it.
478 * Returns the index of the new block in the array.
481 link_cross_validate_uniform_block(void *mem_ctx
,
482 struct gl_uniform_block
**linked_blocks
,
483 unsigned int *num_linked_blocks
,
484 struct gl_uniform_block
*new_block
)
486 for (unsigned int i
= 0; i
< *num_linked_blocks
; i
++) {
487 struct gl_uniform_block
*old_block
= &(*linked_blocks
)[i
];
489 if (strcmp(old_block
->Name
, new_block
->Name
) == 0)
490 return link_uniform_blocks_are_compatible(old_block
, new_block
)
494 *linked_blocks
= reralloc(mem_ctx
, *linked_blocks
,
495 struct gl_uniform_block
,
496 *num_linked_blocks
+ 1);
497 int linked_block_index
= (*num_linked_blocks
)++;
498 struct gl_uniform_block
*linked_block
= &(*linked_blocks
)[linked_block_index
];
500 memcpy(linked_block
, new_block
, sizeof(*new_block
));
501 linked_block
->Uniforms
= ralloc_array(*linked_blocks
,
502 struct gl_uniform_buffer_variable
,
503 linked_block
->NumUniforms
);
505 memcpy(linked_block
->Uniforms
,
507 sizeof(*linked_block
->Uniforms
) * linked_block
->NumUniforms
);
509 for (unsigned int i
= 0; i
< linked_block
->NumUniforms
; i
++) {
510 struct gl_uniform_buffer_variable
*ubo_var
=
511 &linked_block
->Uniforms
[i
];
513 if (ubo_var
->Name
== ubo_var
->IndexName
) {
514 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
515 ubo_var
->IndexName
= ubo_var
->Name
;
517 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
518 ubo_var
->IndexName
= ralloc_strdup(*linked_blocks
, ubo_var
->IndexName
);
522 return linked_block_index
;
526 * Walks the IR and update the references to uniform blocks in the
527 * ir_variables to point at linked shader's list (previously, they
528 * would point at the uniform block list in one of the pre-linked
532 link_update_uniform_buffer_variables(struct gl_shader
*shader
)
534 foreach_list(node
, shader
->ir
) {
535 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
537 if ((var
== NULL
) || !var
->is_in_uniform_block())
540 assert(var
->mode
== ir_var_uniform
);
542 if (var
->is_interface_instance()) {
548 char sentinel
= '\0';
550 if (var
->type
->is_record()) {
552 } else if (var
->type
->is_array()
553 && var
->type
->fields
.array
->is_record()) {
557 const unsigned l
= strlen(var
->name
);
558 for (unsigned i
= 0; i
< shader
->NumUniformBlocks
; i
++) {
559 for (unsigned j
= 0; j
< shader
->UniformBlocks
[i
].NumUniforms
; j
++) {
561 const char *begin
= shader
->UniformBlocks
[i
].Uniforms
[j
].Name
;
562 const char *end
= strchr(begin
, sentinel
);
567 if (l
!= (end
- begin
))
570 if (strncmp(var
->name
, begin
, l
) == 0) {
575 } else if (!strcmp(var
->name
,
576 shader
->UniformBlocks
[i
].Uniforms
[j
].Name
)) {
590 link_assign_uniform_block_offsets(struct gl_shader
*shader
)
592 for (unsigned b
= 0; b
< shader
->NumUniformBlocks
; b
++) {
593 struct gl_uniform_block
*block
= &shader
->UniformBlocks
[b
];
596 for (unsigned int i
= 0; i
< block
->NumUniforms
; i
++) {
597 struct gl_uniform_buffer_variable
*ubo_var
= &block
->Uniforms
[i
];
598 const struct glsl_type
*type
= ubo_var
->Type
;
600 unsigned alignment
= type
->std140_base_alignment(ubo_var
->RowMajor
);
601 unsigned size
= type
->std140_size(ubo_var
->RowMajor
);
603 offset
= glsl_align(offset
, alignment
);
604 ubo_var
->Offset
= offset
;
608 /* From the GL_ARB_uniform_buffer_object spec:
610 * "For uniform blocks laid out according to [std140] rules,
611 * the minimum buffer object size returned by the
612 * UNIFORM_BLOCK_DATA_SIZE query is derived by taking the
613 * offset of the last basic machine unit consumed by the
614 * last uniform of the uniform block (including any
615 * end-of-array or end-of-structure padding), adding one,
616 * and rounding up to the next multiple of the base
617 * alignment required for a vec4."
619 block
->UniformBufferSize
= glsl_align(offset
, 16);
624 link_assign_uniform_locations(struct gl_shader_program
*prog
)
626 ralloc_free(prog
->UniformStorage
);
627 prog
->UniformStorage
= NULL
;
628 prog
->NumUserUniformStorage
= 0;
630 if (prog
->UniformHash
!= NULL
) {
631 prog
->UniformHash
->clear();
633 prog
->UniformHash
= new string_to_uint_map
;
636 /* Uniforms that lack an initializer in the shader code have an initial
637 * value of zero. This includes sampler uniforms.
639 * Page 24 (page 30 of the PDF) of the GLSL 1.20 spec says:
641 * "The link time initial value is either the value of the variable's
642 * initializer, if present, or 0 if no initializer is present. Sampler
643 * types cannot have initializers."
645 memset(prog
->SamplerUnits
, 0, sizeof(prog
->SamplerUnits
));
647 /* First pass: Count the uniform resources used by the user-defined
648 * uniforms. While this happens, each active uniform will have an index
651 * Note: this is *NOT* the index that is returned to the application by
652 * glGetUniformLocation.
654 count_uniform_size
uniform_size(prog
->UniformHash
);
655 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
656 if (prog
->_LinkedShaders
[i
] == NULL
)
659 link_update_uniform_buffer_variables(prog
->_LinkedShaders
[i
]);
661 /* Reset various per-shader target counts.
663 uniform_size
.start_shader();
665 foreach_list(node
, prog
->_LinkedShaders
[i
]->ir
) {
666 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
668 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
))
671 /* FINISHME: Update code to process built-in uniforms!
673 if (strncmp("gl_", var
->name
, 3) == 0) {
674 uniform_size
.num_shader_uniform_components
+=
675 var
->type
->component_slots();
679 uniform_size
.process(var
);
682 prog
->_LinkedShaders
[i
]->num_samplers
= uniform_size
.num_shader_samplers
;
683 prog
->_LinkedShaders
[i
]->num_uniform_components
=
684 uniform_size
.num_shader_uniform_components
;
687 const unsigned num_user_uniforms
= uniform_size
.num_active_uniforms
;
688 const unsigned num_data_slots
= uniform_size
.num_values
;
690 /* On the outside chance that there were no uniforms, bail out.
692 if (num_user_uniforms
== 0)
695 struct gl_uniform_storage
*uniforms
=
696 rzalloc_array(prog
, struct gl_uniform_storage
, num_user_uniforms
);
697 union gl_constant_value
*data
=
698 rzalloc_array(uniforms
, union gl_constant_value
, num_data_slots
);
700 union gl_constant_value
*data_end
= &data
[num_data_slots
];
703 parcel_out_uniform_storage
parcel(prog
->UniformHash
, uniforms
, data
);
705 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
706 if (prog
->_LinkedShaders
[i
] == NULL
)
709 /* Reset various per-shader target counts.
711 parcel
.start_shader();
713 foreach_list(node
, prog
->_LinkedShaders
[i
]->ir
) {
714 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
716 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
))
719 /* FINISHME: Update code to process built-in uniforms!
721 if (strncmp("gl_", var
->name
, 3) == 0)
724 parcel
.set_and_process(prog
, var
);
727 prog
->_LinkedShaders
[i
]->active_samplers
= parcel
.shader_samplers_used
;
728 prog
->_LinkedShaders
[i
]->shadow_samplers
= parcel
.shader_shadow_samplers
;
731 assert(sizeof(prog
->SamplerTargets
) == sizeof(parcel
.targets
));
732 memcpy(prog
->SamplerTargets
, parcel
.targets
, sizeof(prog
->SamplerTargets
));
735 for (unsigned i
= 0; i
< num_user_uniforms
; i
++) {
736 assert(uniforms
[i
].storage
!= NULL
);
739 assert(parcel
.values
== data_end
);
742 prog
->NumUserUniformStorage
= num_user_uniforms
;
743 prog
->UniformStorage
= uniforms
;
745 link_set_uniform_initializers(prog
);