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
);
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
);
117 * Class to help calculate the storage requirements for a set of uniforms
119 * As uniforms are added to the active set the number of active uniforms and
120 * the storage requirements for those uniforms are accumulated. The active
121 * uniforms are added the the hash table supplied to the constructor.
123 * If the same uniform is added multiple times (i.e., once for each shader
124 * target), it will only be accounted once.
126 class count_uniform_size
: public uniform_field_visitor
{
128 count_uniform_size(struct string_to_uint_map
*map
)
129 : num_active_uniforms(0), num_values(0), num_shader_samplers(0),
130 num_shader_uniform_components(0), map(map
)
137 this->num_shader_samplers
= 0;
138 this->num_shader_uniform_components
= 0;
142 * Total number of active uniforms counted
144 unsigned num_active_uniforms
;
147 * Number of data values required to back the storage for the active uniforms
152 * Number of samplers used
154 unsigned num_shader_samplers
;
157 * Number of uniforms used in the current shader
159 unsigned num_shader_uniform_components
;
162 virtual void visit_field(const glsl_type
*type
, const char *name
)
164 assert(!type
->is_record());
165 assert(!(type
->is_array() && type
->fields
.array
->is_record()));
167 /* Count the number of samplers regardless of whether the uniform is
168 * already in the hash table. The hash table prevents adding the same
169 * uniform for multiple shader targets, but in this case we want to
170 * count it for each shader target.
172 const unsigned values
= values_for_type(type
);
173 if (type
->contains_sampler()) {
174 this->num_shader_samplers
+=
175 type
->is_array() ? type
->array_size() : 1;
177 /* Accumulate the total number of uniform slots used by this shader.
178 * Note that samplers do not count against this limit because they
179 * don't use any storage on current hardware.
181 this->num_shader_uniform_components
+= values
;
184 /* If the uniform is already in the map, there's nothing more to do.
187 if (this->map
->get(id
, name
))
190 this->map
->put(this->num_active_uniforms
, name
);
192 /* Each leaf uniform occupies one entry in the list of active
195 this->num_active_uniforms
++;
196 this->num_values
+= values
;
199 struct string_to_uint_map
*map
;
203 * Class to help parcel out pieces of backing storage to uniforms
205 * Each uniform processed has some range of the \c gl_constant_value
206 * structures associated with it. The association is done by finding
207 * the uniform in the \c string_to_uint_map and using the value from
208 * the map to connect that slot in the \c gl_uniform_storage table
209 * with the next available slot in the \c gl_constant_value array.
212 * This class assumes that every uniform that will be processed is
213 * already in the \c string_to_uint_map. In addition, it assumes that
214 * the \c gl_uniform_storage and \c gl_constant_value arrays are "big
217 class parcel_out_uniform_storage
: public uniform_field_visitor
{
219 parcel_out_uniform_storage(struct string_to_uint_map
*map
,
220 struct gl_uniform_storage
*uniforms
,
221 union gl_constant_value
*values
)
222 : map(map
), uniforms(uniforms
), next_sampler(0), values(values
)
224 memset(this->targets
, 0, sizeof(this->targets
));
229 this->shader_samplers_used
= 0;
230 this->shader_shadow_samplers
= 0;
233 void set_and_process(struct gl_shader_program
*prog
,
234 struct gl_shader
*shader
,
238 if (var
->is_in_uniform_block()) {
239 struct gl_uniform_block
*block
=
240 &shader
->UniformBlocks
[var
->uniform_block
];
242 ubo_block_index
= -1;
243 for (unsigned i
= 0; i
< prog
->NumUniformBlocks
; i
++) {
244 if (!strcmp(prog
->UniformBlocks
[i
].Name
,
245 shader
->UniformBlocks
[var
->uniform_block
].Name
)) {
250 assert(ubo_block_index
!= -1);
252 ubo_var_index
= var
->location
;
253 ubo_var
= &block
->Uniforms
[var
->location
];
254 ubo_byte_offset
= ubo_var
->Offset
;
260 struct gl_uniform_buffer_variable
*ubo_var
;
266 virtual void visit_field(const glsl_type
*type
, const char *name
)
268 assert(!type
->is_record());
269 assert(!(type
->is_array() && type
->fields
.array
->is_record()));
272 bool found
= this->map
->get(id
, name
);
278 /* If there is already storage associated with this uniform, it means
279 * that it was set while processing an earlier shader stage. For
280 * example, we may be processing the uniform in the fragment shader, but
281 * the uniform was already processed in the vertex shader.
283 if (this->uniforms
[id
].storage
!= NULL
) {
284 /* If the uniform already has storage set from another shader stage,
285 * mark the samplers used for this shader stage.
287 if (type
->contains_sampler()) {
288 const unsigned count
= MAX2(1, this->uniforms
[id
].array_elements
);
289 const unsigned shadow
= (type
->is_array())
290 ? type
->fields
.array
->sampler_shadow
: type
->sampler_shadow
;
292 for (unsigned i
= 0; i
< count
; i
++) {
293 const unsigned s
= this->uniforms
[id
].sampler
+ i
;
295 this->shader_samplers_used
|= 1U << s
;
296 this->shader_shadow_samplers
|= shadow
<< s
;
303 const glsl_type
*base_type
;
304 if (type
->is_array()) {
305 this->uniforms
[id
].array_elements
= type
->length
;
306 base_type
= type
->fields
.array
;
308 this->uniforms
[id
].array_elements
= 0;
312 if (base_type
->is_sampler()) {
313 this->uniforms
[id
].sampler
= this->next_sampler
;
315 /* Increment the sampler by 1 for non-arrays and by the number of
316 * array elements for arrays.
318 this->next_sampler
+= MAX2(1, this->uniforms
[id
].array_elements
);
320 const gl_texture_index target
= base_type
->sampler_index();
321 const unsigned shadow
= base_type
->sampler_shadow
;
322 for (unsigned i
= this->uniforms
[id
].sampler
323 ; i
< MIN2(this->next_sampler
, MAX_SAMPLERS
)
325 this->targets
[i
] = target
;
326 this->shader_samplers_used
|= 1U << i
;
327 this->shader_shadow_samplers
|= shadow
<< i
;
331 this->uniforms
[id
].sampler
= ~0;
334 this->uniforms
[id
].name
= ralloc_strdup(this->uniforms
, name
);
335 this->uniforms
[id
].type
= base_type
;
336 this->uniforms
[id
].initialized
= 0;
337 this->uniforms
[id
].num_driver_storage
= 0;
338 this->uniforms
[id
].driver_storage
= NULL
;
339 this->uniforms
[id
].storage
= this->values
;
341 this->uniforms
[id
].block_index
= this->ubo_block_index
;
343 unsigned alignment
= type
->std140_base_alignment(ubo_var
->RowMajor
);
344 this->ubo_byte_offset
= align(this->ubo_byte_offset
, alignment
);
345 this->uniforms
[id
].offset
= this->ubo_byte_offset
;
346 this->ubo_byte_offset
+= type
->std140_size(ubo_var
->RowMajor
);
348 if (type
->is_array()) {
349 this->uniforms
[id
].array_stride
=
350 align(type
->fields
.array
->std140_size(ubo_var
->RowMajor
), 16);
352 this->uniforms
[id
].array_stride
= 0;
355 if (type
->is_matrix() ||
356 (type
->is_array() && type
->fields
.array
->is_matrix())) {
357 this->uniforms
[id
].matrix_stride
= 16;
358 this->uniforms
[id
].row_major
= ubo_var
->RowMajor
;
360 this->uniforms
[id
].matrix_stride
= 0;
361 this->uniforms
[id
].row_major
= false;
364 this->uniforms
[id
].block_index
= -1;
365 this->uniforms
[id
].offset
= -1;
366 this->uniforms
[id
].array_stride
= -1;
367 this->uniforms
[id
].matrix_stride
= -1;
368 this->uniforms
[id
].row_major
= false;
371 this->values
+= values_for_type(type
);
374 struct string_to_uint_map
*map
;
376 struct gl_uniform_storage
*uniforms
;
377 unsigned next_sampler
;
380 union gl_constant_value
*values
;
382 gl_texture_index targets
[MAX_SAMPLERS
];
385 * Mask of samplers used by the current shader stage.
387 unsigned shader_samplers_used
;
390 * Mask of samplers used by the current shader stage for shadows.
392 unsigned shader_shadow_samplers
;
396 * Merges a uniform block into an array of uniform blocks that may or
397 * may not already contain a copy of it.
399 * Returns the index of the new block in the array.
402 link_cross_validate_uniform_block(void *mem_ctx
,
403 struct gl_uniform_block
**linked_blocks
,
404 unsigned int *num_linked_blocks
,
405 struct gl_uniform_block
*new_block
)
407 for (unsigned int i
= 0; i
< *num_linked_blocks
; i
++) {
408 struct gl_uniform_block
*old_block
= &(*linked_blocks
)[i
];
410 if (strcmp(old_block
->Name
, new_block
->Name
) == 0)
411 return link_uniform_blocks_are_compatible(old_block
, new_block
)
415 *linked_blocks
= reralloc(mem_ctx
, *linked_blocks
,
416 struct gl_uniform_block
,
417 *num_linked_blocks
+ 1);
418 int linked_block_index
= (*num_linked_blocks
)++;
419 struct gl_uniform_block
*linked_block
= &(*linked_blocks
)[linked_block_index
];
421 memcpy(linked_block
, new_block
, sizeof(*new_block
));
422 linked_block
->Uniforms
= ralloc_array(*linked_blocks
,
423 struct gl_uniform_buffer_variable
,
424 linked_block
->NumUniforms
);
426 memcpy(linked_block
->Uniforms
,
428 sizeof(*linked_block
->Uniforms
) * linked_block
->NumUniforms
);
430 for (unsigned int i
= 0; i
< linked_block
->NumUniforms
; i
++) {
431 struct gl_uniform_buffer_variable
*ubo_var
=
432 &linked_block
->Uniforms
[i
];
434 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
437 return linked_block_index
;
441 * Walks the IR and update the references to uniform blocks in the
442 * ir_variables to point at linked shader's list (previously, they
443 * would point at the uniform block list in one of the pre-linked
447 link_update_uniform_buffer_variables(struct gl_shader
*shader
)
449 foreach_list(node
, shader
->ir
) {
450 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
452 if ((var
== NULL
) || !var
->is_in_uniform_block())
455 assert(var
->mode
== ir_var_uniform
);
458 for (unsigned i
= 0; i
< shader
->NumUniformBlocks
; i
++) {
459 for (unsigned j
= 0; j
< shader
->UniformBlocks
[i
].NumUniforms
; j
++) {
460 if (!strcmp(var
->name
, shader
->UniformBlocks
[i
].Uniforms
[j
].Name
)) {
462 var
->uniform_block
= i
;
475 link_assign_uniform_block_offsets(struct gl_shader
*shader
)
477 for (unsigned b
= 0; b
< shader
->NumUniformBlocks
; b
++) {
478 struct gl_uniform_block
*block
= &shader
->UniformBlocks
[b
];
481 for (unsigned int i
= 0; i
< block
->NumUniforms
; i
++) {
482 struct gl_uniform_buffer_variable
*ubo_var
= &block
->Uniforms
[i
];
483 const struct glsl_type
*type
= ubo_var
->Type
;
485 unsigned alignment
= type
->std140_base_alignment(ubo_var
->RowMajor
);
486 unsigned size
= type
->std140_size(ubo_var
->RowMajor
);
488 offset
= align(offset
, alignment
);
489 ubo_var
->Offset
= offset
;
493 /* From the GL_ARB_uniform_buffer_object spec:
495 * "For uniform blocks laid out according to [std140] rules,
496 * the minimum buffer object size returned by the
497 * UNIFORM_BLOCK_DATA_SIZE query is derived by taking the
498 * offset of the last basic machine unit consumed by the
499 * last uniform of the uniform block (including any
500 * end-of-array or end-of-structure padding), adding one,
501 * and rounding up to the next multiple of the base
502 * alignment required for a vec4."
504 block
->UniformBufferSize
= align(offset
, 16);
509 link_assign_uniform_locations(struct gl_shader_program
*prog
)
511 ralloc_free(prog
->UniformStorage
);
512 prog
->UniformStorage
= NULL
;
513 prog
->NumUserUniformStorage
= 0;
515 if (prog
->UniformHash
!= NULL
) {
516 prog
->UniformHash
->clear();
518 prog
->UniformHash
= new string_to_uint_map
;
521 /* Uniforms that lack an initializer in the shader code have an initial
522 * value of zero. This includes sampler uniforms.
524 * Page 24 (page 30 of the PDF) of the GLSL 1.20 spec says:
526 * "The link time initial value is either the value of the variable's
527 * initializer, if present, or 0 if no initializer is present. Sampler
528 * types cannot have initializers."
530 memset(prog
->SamplerUnits
, 0, sizeof(prog
->SamplerUnits
));
532 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
533 if (prog
->_LinkedShaders
[i
] == NULL
)
536 link_update_uniform_buffer_variables(prog
->_LinkedShaders
[i
]);
539 /* First pass: Count the uniform resources used by the user-defined
540 * uniforms. While this happens, each active uniform will have an index
543 * Note: this is *NOT* the index that is returned to the application by
544 * glGetUniformLocation.
546 count_uniform_size
uniform_size(prog
->UniformHash
);
547 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
548 if (prog
->_LinkedShaders
[i
] == NULL
)
551 /* Reset various per-shader target counts.
553 uniform_size
.start_shader();
555 foreach_list(node
, prog
->_LinkedShaders
[i
]->ir
) {
556 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
558 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
))
561 /* FINISHME: Update code to process built-in uniforms!
563 if (strncmp("gl_", var
->name
, 3) == 0) {
564 uniform_size
.num_shader_uniform_components
+=
565 var
->type
->component_slots();
569 uniform_size
.process(var
);
572 prog
->_LinkedShaders
[i
]->num_samplers
= uniform_size
.num_shader_samplers
;
573 prog
->_LinkedShaders
[i
]->num_uniform_components
=
574 uniform_size
.num_shader_uniform_components
;
577 const unsigned num_user_uniforms
= uniform_size
.num_active_uniforms
;
578 const unsigned num_data_slots
= uniform_size
.num_values
;
580 /* On the outside chance that there were no uniforms, bail out.
582 if (num_user_uniforms
== 0)
585 struct gl_uniform_storage
*uniforms
=
586 rzalloc_array(prog
, struct gl_uniform_storage
, num_user_uniforms
);
587 union gl_constant_value
*data
=
588 rzalloc_array(uniforms
, union gl_constant_value
, num_data_slots
);
590 union gl_constant_value
*data_end
= &data
[num_data_slots
];
593 parcel_out_uniform_storage
parcel(prog
->UniformHash
, uniforms
, data
);
595 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
596 if (prog
->_LinkedShaders
[i
] == NULL
)
599 /* Reset various per-shader target counts.
601 parcel
.start_shader();
603 foreach_list(node
, prog
->_LinkedShaders
[i
]->ir
) {
604 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
606 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
))
609 /* FINISHME: Update code to process built-in uniforms!
611 if (strncmp("gl_", var
->name
, 3) == 0)
614 parcel
.set_and_process(prog
, prog
->_LinkedShaders
[i
], var
);
617 prog
->_LinkedShaders
[i
]->active_samplers
= parcel
.shader_samplers_used
;
618 prog
->_LinkedShaders
[i
]->shadow_samplers
= parcel
.shader_shadow_samplers
;
621 assert(sizeof(prog
->SamplerTargets
) == sizeof(parcel
.targets
));
622 memcpy(prog
->SamplerTargets
, parcel
.targets
, sizeof(prog
->SamplerTargets
));
625 for (unsigned i
= 0; i
< num_user_uniforms
; i
++) {
626 assert(uniforms
[i
].storage
!= NULL
);
629 assert(parcel
.values
== data_end
);
632 prog
->NumUserUniformStorage
= num_user_uniforms
;
633 prog
->UniformStorage
= uniforms
;
635 link_set_uniform_initializers(prog
);