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 this->is_ubo_var
= var
->is_in_uniform_block();
174 if (var
->is_interface_instance())
175 program_resource_visitor::process(var
->interface_type
,
176 var
->interface_type
->name
);
178 program_resource_visitor::process(var
);
182 * Total number of active uniforms counted
184 unsigned num_active_uniforms
;
187 * Number of data values required to back the storage for the active uniforms
192 * Number of samplers used
194 unsigned num_shader_samplers
;
197 * Number of uniforms used in the current shader
199 unsigned num_shader_uniform_components
;
204 virtual void visit_field(const glsl_type
*type
, const char *name
,
207 assert(!type
->is_record());
208 assert(!(type
->is_array() && type
->fields
.array
->is_record()));
209 assert(!type
->is_interface());
210 assert(!(type
->is_array() && type
->fields
.array
->is_interface()));
214 /* Count the number of samplers regardless of whether the uniform is
215 * already in the hash table. The hash table prevents adding the same
216 * uniform for multiple shader targets, but in this case we want to
217 * count it for each shader target.
219 const unsigned values
= values_for_type(type
);
220 if (type
->contains_sampler()) {
221 this->num_shader_samplers
+=
222 type
->is_array() ? type
->array_size() : 1;
224 /* Accumulate the total number of uniform slots used by this shader.
225 * Note that samplers do not count against this limit because they
226 * don't use any storage on current hardware.
229 this->num_shader_uniform_components
+= values
;
232 /* If the uniform is already in the map, there's nothing more to do.
235 if (this->map
->get(id
, name
))
238 this->map
->put(this->num_active_uniforms
, name
);
240 /* Each leaf uniform occupies one entry in the list of active
243 this->num_active_uniforms
++;
244 this->num_values
+= values
;
247 struct string_to_uint_map
*map
;
251 * Class to help parcel out pieces of backing storage to uniforms
253 * Each uniform processed has some range of the \c gl_constant_value
254 * structures associated with it. The association is done by finding
255 * the uniform in the \c string_to_uint_map and using the value from
256 * the map to connect that slot in the \c gl_uniform_storage table
257 * with the next available slot in the \c gl_constant_value array.
260 * This class assumes that every uniform that will be processed is
261 * already in the \c string_to_uint_map. In addition, it assumes that
262 * the \c gl_uniform_storage and \c gl_constant_value arrays are "big
265 class parcel_out_uniform_storage
: public program_resource_visitor
{
267 parcel_out_uniform_storage(struct string_to_uint_map
*map
,
268 struct gl_uniform_storage
*uniforms
,
269 union gl_constant_value
*values
)
270 : map(map
), uniforms(uniforms
), values(values
)
274 void start_shader(gl_shader_type shader_type
)
276 assert(shader_type
< MESA_SHADER_TYPES
);
277 this->shader_type
= shader_type
;
279 this->shader_samplers_used
= 0;
280 this->shader_shadow_samplers
= 0;
281 this->next_sampler
= 0;
282 memset(this->targets
, 0, sizeof(this->targets
));
285 void set_and_process(struct gl_shader_program
*prog
,
288 ubo_block_index
= -1;
289 if (var
->is_in_uniform_block()) {
290 if (var
->is_interface_instance() && var
->type
->is_array()) {
291 unsigned l
= strlen(var
->interface_type
->name
);
293 for (unsigned i
= 0; i
< prog
->NumUniformBlocks
; i
++) {
294 if (strncmp(var
->interface_type
->name
,
295 prog
->UniformBlocks
[i
].Name
,
297 && prog
->UniformBlocks
[i
].Name
[l
] == '[') {
303 for (unsigned i
= 0; i
< prog
->NumUniformBlocks
; i
++) {
304 if (strcmp(var
->interface_type
->name
,
305 prog
->UniformBlocks
[i
].Name
) == 0) {
311 assert(ubo_block_index
!= -1);
313 /* Uniform blocks that were specified with an instance name must be
314 * handled a little bit differently. The name of the variable is the
315 * name used to reference the uniform block instead of being the name
316 * of a variable within the block. Therefore, searching for the name
317 * within the block will fail.
319 if (var
->is_interface_instance()) {
321 ubo_row_major
= false;
323 const struct gl_uniform_block
*const block
=
324 &prog
->UniformBlocks
[ubo_block_index
];
326 assert(var
->location
!= -1);
328 const struct gl_uniform_buffer_variable
*const ubo_var
=
329 &block
->Uniforms
[var
->location
];
331 ubo_row_major
= ubo_var
->RowMajor
;
332 ubo_byte_offset
= ubo_var
->Offset
;
335 if (var
->is_interface_instance())
336 process(var
->interface_type
, var
->interface_type
->name
);
346 gl_shader_type shader_type
;
349 void handle_samplers(const glsl_type
*base_type
,
350 struct gl_uniform_storage
*uniform
)
352 if (base_type
->is_sampler()) {
353 uniform
->sampler
[shader_type
].index
= this->next_sampler
;
354 uniform
->sampler
[shader_type
].active
= true;
356 /* Increment the sampler by 1 for non-arrays and by the number of
357 * array elements for arrays.
359 this->next_sampler
+=
360 MAX2(1, uniform
->array_elements
);
362 const gl_texture_index target
= base_type
->sampler_index();
363 const unsigned shadow
= base_type
->sampler_shadow
;
364 for (unsigned i
= uniform
->sampler
[shader_type
].index
;
365 i
< MIN2(this->next_sampler
, MAX_SAMPLERS
);
367 this->targets
[i
] = target
;
368 this->shader_samplers_used
|= 1U << i
;
369 this->shader_shadow_samplers
|= shadow
<< i
;
372 uniform
->sampler
[shader_type
].index
= ~0;
373 uniform
->sampler
[shader_type
].active
= false;
377 virtual void visit_field(const glsl_type
*type
, const char *name
,
380 assert(!type
->is_record());
381 assert(!(type
->is_array() && type
->fields
.array
->is_record()));
382 assert(!type
->is_interface());
383 assert(!(type
->is_array() && type
->fields
.array
->is_interface()));
388 bool found
= this->map
->get(id
, name
);
394 const glsl_type
*base_type
;
395 if (type
->is_array()) {
396 this->uniforms
[id
].array_elements
= type
->length
;
397 base_type
= type
->fields
.array
;
399 this->uniforms
[id
].array_elements
= 0;
403 /* This assigns sampler uniforms to sampler units. */
404 handle_samplers(base_type
, &this->uniforms
[id
]);
406 /* If there is already storage associated with this uniform, it means
407 * that it was set while processing an earlier shader stage. For
408 * example, we may be processing the uniform in the fragment shader, but
409 * the uniform was already processed in the vertex shader.
411 if (this->uniforms
[id
].storage
!= NULL
) {
415 this->uniforms
[id
].name
= ralloc_strdup(this->uniforms
, name
);
416 this->uniforms
[id
].type
= base_type
;
417 this->uniforms
[id
].initialized
= 0;
418 this->uniforms
[id
].num_driver_storage
= 0;
419 this->uniforms
[id
].driver_storage
= NULL
;
420 this->uniforms
[id
].storage
= this->values
;
421 if (this->ubo_block_index
!= -1) {
422 this->uniforms
[id
].block_index
= this->ubo_block_index
;
424 unsigned alignment
= type
->std140_base_alignment(ubo_row_major
);
425 this->ubo_byte_offset
= glsl_align(this->ubo_byte_offset
, alignment
);
426 this->uniforms
[id
].offset
= this->ubo_byte_offset
;
427 this->ubo_byte_offset
+= type
->std140_size(ubo_row_major
);
429 if (type
->is_array()) {
430 this->uniforms
[id
].array_stride
=
431 glsl_align(type
->fields
.array
->std140_size(ubo_row_major
), 16);
433 this->uniforms
[id
].array_stride
= 0;
436 if (type
->is_matrix() ||
437 (type
->is_array() && type
->fields
.array
->is_matrix())) {
438 this->uniforms
[id
].matrix_stride
= 16;
439 this->uniforms
[id
].row_major
= ubo_row_major
;
441 this->uniforms
[id
].matrix_stride
= 0;
442 this->uniforms
[id
].row_major
= false;
445 this->uniforms
[id
].block_index
= -1;
446 this->uniforms
[id
].offset
= -1;
447 this->uniforms
[id
].array_stride
= -1;
448 this->uniforms
[id
].matrix_stride
= -1;
449 this->uniforms
[id
].row_major
= false;
452 this->values
+= values_for_type(type
);
455 struct string_to_uint_map
*map
;
457 struct gl_uniform_storage
*uniforms
;
458 unsigned next_sampler
;
461 union gl_constant_value
*values
;
463 gl_texture_index targets
[MAX_SAMPLERS
];
466 * Mask of samplers used by the current shader stage.
468 unsigned shader_samplers_used
;
471 * Mask of samplers used by the current shader stage for shadows.
473 unsigned shader_shadow_samplers
;
477 * Merges a uniform block into an array of uniform blocks that may or
478 * may not already contain a copy of it.
480 * Returns the index of the new block in the array.
483 link_cross_validate_uniform_block(void *mem_ctx
,
484 struct gl_uniform_block
**linked_blocks
,
485 unsigned int *num_linked_blocks
,
486 struct gl_uniform_block
*new_block
)
488 for (unsigned int i
= 0; i
< *num_linked_blocks
; i
++) {
489 struct gl_uniform_block
*old_block
= &(*linked_blocks
)[i
];
491 if (strcmp(old_block
->Name
, new_block
->Name
) == 0)
492 return link_uniform_blocks_are_compatible(old_block
, new_block
)
496 *linked_blocks
= reralloc(mem_ctx
, *linked_blocks
,
497 struct gl_uniform_block
,
498 *num_linked_blocks
+ 1);
499 int linked_block_index
= (*num_linked_blocks
)++;
500 struct gl_uniform_block
*linked_block
= &(*linked_blocks
)[linked_block_index
];
502 memcpy(linked_block
, new_block
, sizeof(*new_block
));
503 linked_block
->Uniforms
= ralloc_array(*linked_blocks
,
504 struct gl_uniform_buffer_variable
,
505 linked_block
->NumUniforms
);
507 memcpy(linked_block
->Uniforms
,
509 sizeof(*linked_block
->Uniforms
) * linked_block
->NumUniforms
);
511 for (unsigned int i
= 0; i
< linked_block
->NumUniforms
; i
++) {
512 struct gl_uniform_buffer_variable
*ubo_var
=
513 &linked_block
->Uniforms
[i
];
515 if (ubo_var
->Name
== ubo_var
->IndexName
) {
516 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
517 ubo_var
->IndexName
= ubo_var
->Name
;
519 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
520 ubo_var
->IndexName
= ralloc_strdup(*linked_blocks
, ubo_var
->IndexName
);
524 return linked_block_index
;
528 * Walks the IR and update the references to uniform blocks in the
529 * ir_variables to point at linked shader's list (previously, they
530 * would point at the uniform block list in one of the pre-linked
534 link_update_uniform_buffer_variables(struct gl_shader
*shader
)
536 foreach_list(node
, shader
->ir
) {
537 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
539 if ((var
== NULL
) || !var
->is_in_uniform_block())
542 assert(var
->mode
== ir_var_uniform
);
544 if (var
->is_interface_instance()) {
550 char sentinel
= '\0';
552 if (var
->type
->is_record()) {
554 } else if (var
->type
->is_array()
555 && var
->type
->fields
.array
->is_record()) {
559 const unsigned l
= strlen(var
->name
);
560 for (unsigned i
= 0; i
< shader
->NumUniformBlocks
; i
++) {
561 for (unsigned j
= 0; j
< shader
->UniformBlocks
[i
].NumUniforms
; j
++) {
563 const char *begin
= shader
->UniformBlocks
[i
].Uniforms
[j
].Name
;
564 const char *end
= strchr(begin
, sentinel
);
569 if (l
!= (end
- begin
))
572 if (strncmp(var
->name
, begin
, l
) == 0) {
577 } else if (!strcmp(var
->name
,
578 shader
->UniformBlocks
[i
].Uniforms
[j
].Name
)) {
592 link_assign_uniform_block_offsets(struct gl_shader
*shader
)
594 for (unsigned b
= 0; b
< shader
->NumUniformBlocks
; b
++) {
595 struct gl_uniform_block
*block
= &shader
->UniformBlocks
[b
];
598 for (unsigned int i
= 0; i
< block
->NumUniforms
; i
++) {
599 struct gl_uniform_buffer_variable
*ubo_var
= &block
->Uniforms
[i
];
600 const struct glsl_type
*type
= ubo_var
->Type
;
602 unsigned alignment
= type
->std140_base_alignment(ubo_var
->RowMajor
);
603 unsigned size
= type
->std140_size(ubo_var
->RowMajor
);
605 offset
= glsl_align(offset
, alignment
);
606 ubo_var
->Offset
= offset
;
610 /* From the GL_ARB_uniform_buffer_object spec:
612 * "For uniform blocks laid out according to [std140] rules,
613 * the minimum buffer object size returned by the
614 * UNIFORM_BLOCK_DATA_SIZE query is derived by taking the
615 * offset of the last basic machine unit consumed by the
616 * last uniform of the uniform block (including any
617 * end-of-array or end-of-structure padding), adding one,
618 * and rounding up to the next multiple of the base
619 * alignment required for a vec4."
621 block
->UniformBufferSize
= glsl_align(offset
, 16);
626 link_assign_uniform_locations(struct gl_shader_program
*prog
)
628 ralloc_free(prog
->UniformStorage
);
629 prog
->UniformStorage
= NULL
;
630 prog
->NumUserUniformStorage
= 0;
632 if (prog
->UniformHash
!= NULL
) {
633 prog
->UniformHash
->clear();
635 prog
->UniformHash
= new string_to_uint_map
;
638 /* First pass: Count the uniform resources used by the user-defined
639 * uniforms. While this happens, each active uniform will have an index
642 * Note: this is *NOT* the index that is returned to the application by
643 * glGetUniformLocation.
645 count_uniform_size
uniform_size(prog
->UniformHash
);
646 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
647 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
652 /* Uniforms that lack an initializer in the shader code have an initial
653 * value of zero. This includes sampler uniforms.
655 * Page 24 (page 30 of the PDF) of the GLSL 1.20 spec says:
657 * "The link time initial value is either the value of the variable's
658 * initializer, if present, or 0 if no initializer is present. Sampler
659 * types cannot have initializers."
661 memset(sh
->SamplerUnits
, 0, sizeof(sh
->SamplerUnits
));
663 link_update_uniform_buffer_variables(sh
);
665 /* Reset various per-shader target counts.
667 uniform_size
.start_shader();
669 foreach_list(node
, sh
->ir
) {
670 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
672 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
))
675 /* FINISHME: Update code to process built-in uniforms!
677 if (strncmp("gl_", var
->name
, 3) == 0) {
678 uniform_size
.num_shader_uniform_components
+=
679 var
->type
->component_slots();
683 uniform_size
.process(var
);
686 sh
->num_samplers
= uniform_size
.num_shader_samplers
;
687 sh
->num_uniform_components
= uniform_size
.num_shader_uniform_components
;
689 sh
->num_combined_uniform_components
= sh
->num_uniform_components
;
690 for (unsigned i
= 0; i
< sh
->NumUniformBlocks
; i
++) {
691 sh
->num_combined_uniform_components
+=
692 sh
->UniformBlocks
[i
].UniformBufferSize
/ 4;
696 const unsigned num_user_uniforms
= uniform_size
.num_active_uniforms
;
697 const unsigned num_data_slots
= uniform_size
.num_values
;
699 /* On the outside chance that there were no uniforms, bail out.
701 if (num_user_uniforms
== 0)
704 struct gl_uniform_storage
*uniforms
=
705 rzalloc_array(prog
, struct gl_uniform_storage
, num_user_uniforms
);
706 union gl_constant_value
*data
=
707 rzalloc_array(uniforms
, union gl_constant_value
, num_data_slots
);
709 union gl_constant_value
*data_end
= &data
[num_data_slots
];
712 parcel_out_uniform_storage
parcel(prog
->UniformHash
, uniforms
, data
);
714 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
715 if (prog
->_LinkedShaders
[i
] == NULL
)
718 parcel
.start_shader((gl_shader_type
)i
);
720 foreach_list(node
, prog
->_LinkedShaders
[i
]->ir
) {
721 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
723 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
))
726 /* FINISHME: Update code to process built-in uniforms!
728 if (strncmp("gl_", var
->name
, 3) == 0)
731 parcel
.set_and_process(prog
, var
);
734 prog
->_LinkedShaders
[i
]->active_samplers
= parcel
.shader_samplers_used
;
735 prog
->_LinkedShaders
[i
]->shadow_samplers
= parcel
.shader_shadow_samplers
;
737 STATIC_ASSERT(sizeof(prog
->_LinkedShaders
[i
]->SamplerTargets
) == sizeof(parcel
.targets
));
738 memcpy(prog
->_LinkedShaders
[i
]->SamplerTargets
, parcel
.targets
,
739 sizeof(prog
->_LinkedShaders
[i
]->SamplerTargets
));
742 prog
->UniformLocationBaseScale
= (1U<<16);
745 for (unsigned i
= 0; i
< num_user_uniforms
; i
++) {
746 assert(uniforms
[i
].storage
!= NULL
);
749 assert(parcel
.values
== data_end
);
752 prog
->NumUserUniformStorage
= num_user_uniforms
;
753 prog
->UniformStorage
= uniforms
;
755 link_set_uniform_initializers(prog
);