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"
32 static inline unsigned int
33 align(unsigned int a
, unsigned int align
)
35 return (a
+ align
- 1) / align
* align
;
39 * \file link_uniforms.cpp
40 * Assign locations for GLSL uniforms.
42 * \author Ian Romanick <ian.d.romanick@intel.com>
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 uniform_field_visitor::process(ir_variable
*var
)
63 const glsl_type
*t
= var
->type
;
65 /* false is always passed for the row_major parameter to the other
66 * processing functions because no information is available to do
67 * otherwise. See the warning in linker.h.
70 /* Only strdup the name if we actually will need to modify it. */
71 if (t
->is_record() || (t
->is_array() && t
->fields
.array
->is_record())) {
72 char *name
= ralloc_strdup(NULL
, var
->name
);
73 recursion(var
->type
, &name
, strlen(name
), false);
76 this->visit_field(t
, var
->name
, false);
81 uniform_field_visitor::recursion(const glsl_type
*t
, char **name
,
82 size_t name_length
, bool row_major
)
84 /* Records need to have each field processed individually.
86 * Arrays of records need to have each array element processed
87 * individually, then each field of the resulting array elements processed
91 for (unsigned i
= 0; i
< t
->length
; i
++) {
92 const char *field
= t
->fields
.structure
[i
].name
;
93 size_t new_length
= name_length
;
95 /* Append '.field' to the current uniform name. */
96 ralloc_asprintf_rewrite_tail(name
, &new_length
, ".%s", field
);
98 recursion(t
->fields
.structure
[i
].type
, name
, new_length
,
99 t
->fields
.structure
[i
].row_major
);
101 } else if (t
->is_array() && t
->fields
.array
->is_record()) {
102 for (unsigned i
= 0; i
< t
->length
; i
++) {
103 size_t new_length
= name_length
;
105 /* Append the subscript to the current uniform name */
106 ralloc_asprintf_rewrite_tail(name
, &new_length
, "[%u]", i
);
108 recursion(t
->fields
.array
, name
, new_length
,
109 t
->fields
.structure
[i
].row_major
);
112 this->visit_field(t
, *name
, row_major
);
117 uniform_field_visitor::visit_field(const glsl_struct_field
*field
)
124 * Class to help calculate the storage requirements for a set of uniforms
126 * As uniforms are added to the active set the number of active uniforms and
127 * the storage requirements for those uniforms are accumulated. The active
128 * uniforms are added the the hash table supplied to the constructor.
130 * If the same uniform is added multiple times (i.e., once for each shader
131 * target), it will only be accounted once.
133 class count_uniform_size
: public uniform_field_visitor
{
135 count_uniform_size(struct string_to_uint_map
*map
)
136 : num_active_uniforms(0), num_values(0), num_shader_samplers(0),
137 num_shader_uniform_components(0), map(map
)
144 this->num_shader_samplers
= 0;
145 this->num_shader_uniform_components
= 0;
149 * Total number of active uniforms counted
151 unsigned num_active_uniforms
;
154 * Number of data values required to back the storage for the active uniforms
159 * Number of samplers used
161 unsigned num_shader_samplers
;
164 * Number of uniforms used in the current shader
166 unsigned num_shader_uniform_components
;
169 virtual void visit_field(const glsl_type
*type
, const char *name
,
172 assert(!type
->is_record());
173 assert(!(type
->is_array() && type
->fields
.array
->is_record()));
177 /* Count the number of samplers regardless of whether the uniform is
178 * already in the hash table. The hash table prevents adding the same
179 * uniform for multiple shader targets, but in this case we want to
180 * count it for each shader target.
182 const unsigned values
= values_for_type(type
);
183 if (type
->contains_sampler()) {
184 this->num_shader_samplers
+=
185 type
->is_array() ? type
->array_size() : 1;
187 /* Accumulate the total number of uniform slots used by this shader.
188 * Note that samplers do not count against this limit because they
189 * don't use any storage on current hardware.
191 this->num_shader_uniform_components
+= values
;
194 /* If the uniform is already in the map, there's nothing more to do.
197 if (this->map
->get(id
, name
))
200 this->map
->put(this->num_active_uniforms
, name
);
202 /* Each leaf uniform occupies one entry in the list of active
205 this->num_active_uniforms
++;
206 this->num_values
+= values
;
209 struct string_to_uint_map
*map
;
213 * Class to help parcel out pieces of backing storage to uniforms
215 * Each uniform processed has some range of the \c gl_constant_value
216 * structures associated with it. The association is done by finding
217 * the uniform in the \c string_to_uint_map and using the value from
218 * the map to connect that slot in the \c gl_uniform_storage table
219 * with the next available slot in the \c gl_constant_value array.
222 * This class assumes that every uniform that will be processed is
223 * already in the \c string_to_uint_map. In addition, it assumes that
224 * the \c gl_uniform_storage and \c gl_constant_value arrays are "big
227 class parcel_out_uniform_storage
: public uniform_field_visitor
{
229 parcel_out_uniform_storage(struct string_to_uint_map
*map
,
230 struct gl_uniform_storage
*uniforms
,
231 union gl_constant_value
*values
)
232 : map(map
), uniforms(uniforms
), next_sampler(0), values(values
)
234 memset(this->targets
, 0, sizeof(this->targets
));
239 this->shader_samplers_used
= 0;
240 this->shader_shadow_samplers
= 0;
243 void set_and_process(struct gl_shader_program
*prog
,
244 struct gl_shader
*shader
,
248 if (var
->is_in_uniform_block()) {
249 struct gl_uniform_block
*block
=
250 &shader
->UniformBlocks
[var
->uniform_block
];
252 ubo_block_index
= -1;
253 for (unsigned i
= 0; i
< prog
->NumUniformBlocks
; i
++) {
254 if (!strcmp(prog
->UniformBlocks
[i
].Name
,
255 shader
->UniformBlocks
[var
->uniform_block
].Name
)) {
260 assert(ubo_block_index
!= -1);
262 ubo_var_index
= var
->location
;
263 ubo_var
= &block
->Uniforms
[var
->location
];
264 ubo_byte_offset
= ubo_var
->Offset
;
270 struct gl_uniform_buffer_variable
*ubo_var
;
276 virtual void visit_field(const glsl_type
*type
, const char *name
,
279 assert(!type
->is_record());
280 assert(!(type
->is_array() && type
->fields
.array
->is_record()));
285 bool found
= this->map
->get(id
, name
);
291 /* If there is already storage associated with this uniform, it means
292 * that it was set while processing an earlier shader stage. For
293 * example, we may be processing the uniform in the fragment shader, but
294 * the uniform was already processed in the vertex shader.
296 if (this->uniforms
[id
].storage
!= NULL
) {
297 /* If the uniform already has storage set from another shader stage,
298 * mark the samplers used for this shader stage.
300 if (type
->contains_sampler()) {
301 const unsigned count
= MAX2(1, this->uniforms
[id
].array_elements
);
302 const unsigned shadow
= (type
->is_array())
303 ? type
->fields
.array
->sampler_shadow
: type
->sampler_shadow
;
305 for (unsigned i
= 0; i
< count
; i
++) {
306 const unsigned s
= this->uniforms
[id
].sampler
+ i
;
308 this->shader_samplers_used
|= 1U << s
;
309 this->shader_shadow_samplers
|= shadow
<< s
;
316 const glsl_type
*base_type
;
317 if (type
->is_array()) {
318 this->uniforms
[id
].array_elements
= type
->length
;
319 base_type
= type
->fields
.array
;
321 this->uniforms
[id
].array_elements
= 0;
325 if (base_type
->is_sampler()) {
326 this->uniforms
[id
].sampler
= this->next_sampler
;
328 /* Increment the sampler by 1 for non-arrays and by the number of
329 * array elements for arrays.
331 this->next_sampler
+= MAX2(1, this->uniforms
[id
].array_elements
);
333 const gl_texture_index target
= base_type
->sampler_index();
334 const unsigned shadow
= base_type
->sampler_shadow
;
335 for (unsigned i
= this->uniforms
[id
].sampler
336 ; i
< MIN2(this->next_sampler
, MAX_SAMPLERS
)
338 this->targets
[i
] = target
;
339 this->shader_samplers_used
|= 1U << i
;
340 this->shader_shadow_samplers
|= shadow
<< i
;
344 this->uniforms
[id
].sampler
= ~0;
347 this->uniforms
[id
].name
= ralloc_strdup(this->uniforms
, name
);
348 this->uniforms
[id
].type
= base_type
;
349 this->uniforms
[id
].initialized
= 0;
350 this->uniforms
[id
].num_driver_storage
= 0;
351 this->uniforms
[id
].driver_storage
= NULL
;
352 this->uniforms
[id
].storage
= this->values
;
354 this->uniforms
[id
].block_index
= this->ubo_block_index
;
356 unsigned alignment
= type
->std140_base_alignment(ubo_var
->RowMajor
);
357 this->ubo_byte_offset
= align(this->ubo_byte_offset
, alignment
);
358 this->uniforms
[id
].offset
= this->ubo_byte_offset
;
359 this->ubo_byte_offset
+= type
->std140_size(ubo_var
->RowMajor
);
361 if (type
->is_array()) {
362 this->uniforms
[id
].array_stride
=
363 align(type
->fields
.array
->std140_size(ubo_var
->RowMajor
), 16);
365 this->uniforms
[id
].array_stride
= 0;
368 if (type
->is_matrix() ||
369 (type
->is_array() && type
->fields
.array
->is_matrix())) {
370 this->uniforms
[id
].matrix_stride
= 16;
371 this->uniforms
[id
].row_major
= ubo_var
->RowMajor
;
373 this->uniforms
[id
].matrix_stride
= 0;
374 this->uniforms
[id
].row_major
= false;
377 this->uniforms
[id
].block_index
= -1;
378 this->uniforms
[id
].offset
= -1;
379 this->uniforms
[id
].array_stride
= -1;
380 this->uniforms
[id
].matrix_stride
= -1;
381 this->uniforms
[id
].row_major
= false;
384 this->values
+= values_for_type(type
);
387 struct string_to_uint_map
*map
;
389 struct gl_uniform_storage
*uniforms
;
390 unsigned next_sampler
;
393 union gl_constant_value
*values
;
395 gl_texture_index targets
[MAX_SAMPLERS
];
398 * Mask of samplers used by the current shader stage.
400 unsigned shader_samplers_used
;
403 * Mask of samplers used by the current shader stage for shadows.
405 unsigned shader_shadow_samplers
;
409 * Merges a uniform block into an array of uniform blocks that may or
410 * may not already contain a copy of it.
412 * Returns the index of the new block in the array.
415 link_cross_validate_uniform_block(void *mem_ctx
,
416 struct gl_uniform_block
**linked_blocks
,
417 unsigned int *num_linked_blocks
,
418 struct gl_uniform_block
*new_block
)
420 for (unsigned int i
= 0; i
< *num_linked_blocks
; i
++) {
421 struct gl_uniform_block
*old_block
= &(*linked_blocks
)[i
];
423 if (strcmp(old_block
->Name
, new_block
->Name
) == 0)
424 return link_uniform_blocks_are_compatible(old_block
, new_block
)
428 *linked_blocks
= reralloc(mem_ctx
, *linked_blocks
,
429 struct gl_uniform_block
,
430 *num_linked_blocks
+ 1);
431 int linked_block_index
= (*num_linked_blocks
)++;
432 struct gl_uniform_block
*linked_block
= &(*linked_blocks
)[linked_block_index
];
434 memcpy(linked_block
, new_block
, sizeof(*new_block
));
435 linked_block
->Uniforms
= ralloc_array(*linked_blocks
,
436 struct gl_uniform_buffer_variable
,
437 linked_block
->NumUniforms
);
439 memcpy(linked_block
->Uniforms
,
441 sizeof(*linked_block
->Uniforms
) * linked_block
->NumUniforms
);
443 for (unsigned int i
= 0; i
< linked_block
->NumUniforms
; i
++) {
444 struct gl_uniform_buffer_variable
*ubo_var
=
445 &linked_block
->Uniforms
[i
];
447 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
450 return linked_block_index
;
454 * Walks the IR and update the references to uniform blocks in the
455 * ir_variables to point at linked shader's list (previously, they
456 * would point at the uniform block list in one of the pre-linked
460 link_update_uniform_buffer_variables(struct gl_shader
*shader
)
462 foreach_list(node
, shader
->ir
) {
463 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
465 if ((var
== NULL
) || !var
->is_in_uniform_block())
468 assert(var
->mode
== ir_var_uniform
);
471 for (unsigned i
= 0; i
< shader
->NumUniformBlocks
; i
++) {
472 for (unsigned j
= 0; j
< shader
->UniformBlocks
[i
].NumUniforms
; j
++) {
473 if (!strcmp(var
->name
, shader
->UniformBlocks
[i
].Uniforms
[j
].Name
)) {
475 var
->uniform_block
= i
;
488 link_assign_uniform_block_offsets(struct gl_shader
*shader
)
490 for (unsigned b
= 0; b
< shader
->NumUniformBlocks
; b
++) {
491 struct gl_uniform_block
*block
= &shader
->UniformBlocks
[b
];
494 for (unsigned int i
= 0; i
< block
->NumUniforms
; i
++) {
495 struct gl_uniform_buffer_variable
*ubo_var
= &block
->Uniforms
[i
];
496 const struct glsl_type
*type
= ubo_var
->Type
;
498 unsigned alignment
= type
->std140_base_alignment(ubo_var
->RowMajor
);
499 unsigned size
= type
->std140_size(ubo_var
->RowMajor
);
501 offset
= align(offset
, alignment
);
502 ubo_var
->Offset
= offset
;
506 /* From the GL_ARB_uniform_buffer_object spec:
508 * "For uniform blocks laid out according to [std140] rules,
509 * the minimum buffer object size returned by the
510 * UNIFORM_BLOCK_DATA_SIZE query is derived by taking the
511 * offset of the last basic machine unit consumed by the
512 * last uniform of the uniform block (including any
513 * end-of-array or end-of-structure padding), adding one,
514 * and rounding up to the next multiple of the base
515 * alignment required for a vec4."
517 block
->UniformBufferSize
= align(offset
, 16);
522 link_assign_uniform_locations(struct gl_shader_program
*prog
)
524 ralloc_free(prog
->UniformStorage
);
525 prog
->UniformStorage
= NULL
;
526 prog
->NumUserUniformStorage
= 0;
528 if (prog
->UniformHash
!= NULL
) {
529 prog
->UniformHash
->clear();
531 prog
->UniformHash
= new string_to_uint_map
;
534 /* Uniforms that lack an initializer in the shader code have an initial
535 * value of zero. This includes sampler uniforms.
537 * Page 24 (page 30 of the PDF) of the GLSL 1.20 spec says:
539 * "The link time initial value is either the value of the variable's
540 * initializer, if present, or 0 if no initializer is present. Sampler
541 * types cannot have initializers."
543 memset(prog
->SamplerUnits
, 0, sizeof(prog
->SamplerUnits
));
545 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
546 if (prog
->_LinkedShaders
[i
] == NULL
)
549 link_update_uniform_buffer_variables(prog
->_LinkedShaders
[i
]);
552 /* First pass: Count the uniform resources used by the user-defined
553 * uniforms. While this happens, each active uniform will have an index
556 * Note: this is *NOT* the index that is returned to the application by
557 * glGetUniformLocation.
559 count_uniform_size
uniform_size(prog
->UniformHash
);
560 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
561 if (prog
->_LinkedShaders
[i
] == NULL
)
564 /* Reset various per-shader target counts.
566 uniform_size
.start_shader();
568 foreach_list(node
, prog
->_LinkedShaders
[i
]->ir
) {
569 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
571 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
))
574 /* FINISHME: Update code to process built-in uniforms!
576 if (strncmp("gl_", var
->name
, 3) == 0) {
577 uniform_size
.num_shader_uniform_components
+=
578 var
->type
->component_slots();
582 uniform_size
.process(var
);
585 prog
->_LinkedShaders
[i
]->num_samplers
= uniform_size
.num_shader_samplers
;
586 prog
->_LinkedShaders
[i
]->num_uniform_components
=
587 uniform_size
.num_shader_uniform_components
;
590 const unsigned num_user_uniforms
= uniform_size
.num_active_uniforms
;
591 const unsigned num_data_slots
= uniform_size
.num_values
;
593 /* On the outside chance that there were no uniforms, bail out.
595 if (num_user_uniforms
== 0)
598 struct gl_uniform_storage
*uniforms
=
599 rzalloc_array(prog
, struct gl_uniform_storage
, num_user_uniforms
);
600 union gl_constant_value
*data
=
601 rzalloc_array(uniforms
, union gl_constant_value
, num_data_slots
);
603 union gl_constant_value
*data_end
= &data
[num_data_slots
];
606 parcel_out_uniform_storage
parcel(prog
->UniformHash
, uniforms
, data
);
608 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
609 if (prog
->_LinkedShaders
[i
] == NULL
)
612 /* Reset various per-shader target counts.
614 parcel
.start_shader();
616 foreach_list(node
, prog
->_LinkedShaders
[i
]->ir
) {
617 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
619 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
))
622 /* FINISHME: Update code to process built-in uniforms!
624 if (strncmp("gl_", var
->name
, 3) == 0)
627 parcel
.set_and_process(prog
, prog
->_LinkedShaders
[i
], var
);
630 prog
->_LinkedShaders
[i
]->active_samplers
= parcel
.shader_samplers_used
;
631 prog
->_LinkedShaders
[i
]->shadow_samplers
= parcel
.shader_shadow_samplers
;
634 assert(sizeof(prog
->SamplerTargets
) == sizeof(parcel
.targets
));
635 memcpy(prog
->SamplerTargets
, parcel
.targets
, sizeof(prog
->SamplerTargets
));
638 for (unsigned i
= 0; i
< num_user_uniforms
; i
++) {
639 assert(uniforms
[i
].storage
!= NULL
);
642 assert(parcel
.values
== data_end
);
645 prog
->NumUserUniformStorage
= num_user_uniforms
;
646 prog
->UniformStorage
= uniforms
;
648 link_set_uniform_initializers(prog
);