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 /* Only strdup the name if we actually will need to modify it. */
66 if (t
->is_record() || (t
->is_array() && t
->fields
.array
->is_record())) {
67 char *name
= ralloc_strdup(NULL
, var
->name
);
68 recursion(var
->type
, &name
, strlen(name
));
71 this->visit_field(t
, var
->name
);
76 uniform_field_visitor::recursion(const glsl_type
*t
, char **name
,
79 /* Records need to have each field processed individually.
81 * Arrays of records need to have each array element processed
82 * individually, then each field of the resulting array elements processed
86 for (unsigned i
= 0; i
< t
->length
; i
++) {
87 const char *field
= t
->fields
.structure
[i
].name
;
88 size_t new_length
= name_length
;
90 /* Append '.field' to the current uniform name. */
91 ralloc_asprintf_rewrite_tail(name
, &new_length
, ".%s", field
);
93 recursion(t
->fields
.structure
[i
].type
, name
, new_length
);
95 } else if (t
->is_array() && t
->fields
.array
->is_record()) {
96 for (unsigned i
= 0; i
< t
->length
; i
++) {
97 size_t new_length
= name_length
;
99 /* Append the subscript to the current uniform name */
100 ralloc_asprintf_rewrite_tail(name
, &new_length
, "[%u]", i
);
102 recursion(t
->fields
.array
, name
, new_length
);
105 this->visit_field(t
, *name
);
110 * Class to help calculate the storage requirements for a set of uniforms
112 * As uniforms are added to the active set the number of active uniforms and
113 * the storage requirements for those uniforms are accumulated. The active
114 * uniforms are added the the hash table supplied to the constructor.
116 * If the same uniform is added multiple times (i.e., once for each shader
117 * target), it will only be accounted once.
119 class count_uniform_size
: public uniform_field_visitor
{
121 count_uniform_size(struct string_to_uint_map
*map
)
122 : num_active_uniforms(0), num_values(0), num_shader_samplers(0),
123 num_shader_uniform_components(0), map(map
)
130 this->num_shader_samplers
= 0;
131 this->num_shader_uniform_components
= 0;
135 * Total number of active uniforms counted
137 unsigned num_active_uniforms
;
140 * Number of data values required to back the storage for the active uniforms
145 * Number of samplers used
147 unsigned num_shader_samplers
;
150 * Number of uniforms used in the current shader
152 unsigned num_shader_uniform_components
;
155 virtual void visit_field(const glsl_type
*type
, const char *name
)
157 assert(!type
->is_record());
158 assert(!(type
->is_array() && type
->fields
.array
->is_record()));
160 /* Count the number of samplers regardless of whether the uniform is
161 * already in the hash table. The hash table prevents adding the same
162 * uniform for multiple shader targets, but in this case we want to
163 * count it for each shader target.
165 const unsigned values
= values_for_type(type
);
166 if (type
->contains_sampler()) {
167 this->num_shader_samplers
+=
168 type
->is_array() ? type
->array_size() : 1;
170 /* Accumulate the total number of uniform slots used by this shader.
171 * Note that samplers do not count against this limit because they
172 * don't use any storage on current hardware.
174 this->num_shader_uniform_components
+= values
;
177 /* If the uniform is already in the map, there's nothing more to do.
180 if (this->map
->get(id
, name
))
183 this->map
->put(this->num_active_uniforms
, name
);
185 /* Each leaf uniform occupies one entry in the list of active
188 this->num_active_uniforms
++;
189 this->num_values
+= values
;
192 struct string_to_uint_map
*map
;
196 * Class to help parcel out pieces of backing storage to uniforms
198 * Each uniform processed has some range of the \c gl_constant_value
199 * structures associated with it. The association is done by finding
200 * the uniform in the \c string_to_uint_map and using the value from
201 * the map to connect that slot in the \c gl_uniform_storage table
202 * with the next available slot in the \c gl_constant_value array.
205 * This class assumes that every uniform that will be processed is
206 * already in the \c string_to_uint_map. In addition, it assumes that
207 * the \c gl_uniform_storage and \c gl_constant_value arrays are "big
210 class parcel_out_uniform_storage
: public uniform_field_visitor
{
212 parcel_out_uniform_storage(struct string_to_uint_map
*map
,
213 struct gl_uniform_storage
*uniforms
,
214 union gl_constant_value
*values
)
215 : map(map
), uniforms(uniforms
), next_sampler(0), values(values
)
217 memset(this->targets
, 0, sizeof(this->targets
));
222 this->shader_samplers_used
= 0;
223 this->shader_shadow_samplers
= 0;
226 void set_and_process(struct gl_shader_program
*prog
,
230 if (var
->uniform_block
!= -1) {
231 struct gl_uniform_block
*block
=
232 &prog
->UniformBlocks
[var
->uniform_block
];
234 ubo_block_index
= var
->uniform_block
;
235 ubo_var_index
= var
->location
;
236 ubo_var
= &block
->Uniforms
[var
->location
];
237 ubo_byte_offset
= ubo_var
->Offset
;
243 struct gl_uniform_buffer_variable
*ubo_var
;
249 virtual void visit_field(const glsl_type
*type
, const char *name
)
251 assert(!type
->is_record());
252 assert(!(type
->is_array() && type
->fields
.array
->is_record()));
255 bool found
= this->map
->get(id
, name
);
261 /* If there is already storage associated with this uniform, it means
262 * that it was set while processing an earlier shader stage. For
263 * example, we may be processing the uniform in the fragment shader, but
264 * the uniform was already processed in the vertex shader.
266 if (this->uniforms
[id
].storage
!= NULL
) {
267 /* If the uniform already has storage set from another shader stage,
268 * mark the samplers used for this shader stage.
270 if (type
->contains_sampler()) {
271 const unsigned count
= MAX2(1, this->uniforms
[id
].array_elements
);
272 const unsigned shadow
= (type
->is_array())
273 ? type
->fields
.array
->sampler_shadow
: type
->sampler_shadow
;
275 for (unsigned i
= 0; i
< count
; i
++) {
276 const unsigned s
= this->uniforms
[id
].sampler
+ i
;
278 this->shader_samplers_used
|= 1U << s
;
279 this->shader_shadow_samplers
|= shadow
<< s
;
286 const glsl_type
*base_type
;
287 if (type
->is_array()) {
288 this->uniforms
[id
].array_elements
= type
->length
;
289 base_type
= type
->fields
.array
;
291 this->uniforms
[id
].array_elements
= 0;
295 if (base_type
->is_sampler()) {
296 this->uniforms
[id
].sampler
= this->next_sampler
;
298 /* Increment the sampler by 1 for non-arrays and by the number of
299 * array elements for arrays.
301 this->next_sampler
+= MAX2(1, this->uniforms
[id
].array_elements
);
303 const gl_texture_index target
= base_type
->sampler_index();
304 const unsigned shadow
= base_type
->sampler_shadow
;
305 for (unsigned i
= this->uniforms
[id
].sampler
306 ; i
< this->next_sampler
308 this->targets
[i
] = target
;
309 this->shader_samplers_used
|= 1U << i
;
310 this->shader_shadow_samplers
|= shadow
<< i
;
314 this->uniforms
[id
].sampler
= ~0;
317 this->uniforms
[id
].name
= ralloc_strdup(this->uniforms
, name
);
318 this->uniforms
[id
].type
= base_type
;
319 this->uniforms
[id
].initialized
= 0;
320 this->uniforms
[id
].num_driver_storage
= 0;
321 this->uniforms
[id
].driver_storage
= NULL
;
322 this->uniforms
[id
].storage
= this->values
;
324 this->uniforms
[id
].block_index
= this->ubo_block_index
;
326 unsigned alignment
= type
->std140_base_alignment(ubo_var
->RowMajor
);
327 this->ubo_byte_offset
= align(this->ubo_byte_offset
, alignment
);
328 this->uniforms
[id
].offset
= this->ubo_byte_offset
;
329 this->ubo_byte_offset
+= type
->std140_size(ubo_var
->RowMajor
);
331 if (type
->is_array()) {
332 this->uniforms
[id
].array_stride
=
333 align(type
->fields
.array
->std140_size(ubo_var
->RowMajor
), 16);
335 this->uniforms
[id
].array_stride
= 0;
338 if (type
->is_matrix() ||
339 (type
->is_array() && type
->fields
.array
->is_matrix())) {
340 this->uniforms
[id
].matrix_stride
= 16;
341 this->uniforms
[id
].row_major
= ubo_var
->RowMajor
;
343 this->uniforms
[id
].matrix_stride
= 0;
344 this->uniforms
[id
].row_major
= false;
347 this->uniforms
[id
].block_index
= -1;
348 this->uniforms
[id
].offset
= -1;
349 this->uniforms
[id
].array_stride
= -1;
350 this->uniforms
[id
].matrix_stride
= -1;
351 this->uniforms
[id
].row_major
= false;
354 this->values
+= values_for_type(type
);
357 struct string_to_uint_map
*map
;
359 struct gl_uniform_storage
*uniforms
;
360 unsigned next_sampler
;
363 union gl_constant_value
*values
;
365 gl_texture_index targets
[MAX_SAMPLERS
];
368 * Mask of samplers used by the current shader stage.
370 unsigned shader_samplers_used
;
373 * Mask of samplers used by the current shader stage for shadows.
375 unsigned shader_shadow_samplers
;
379 * Merges a uniform block into an array of uniform blocks that may or
380 * may not already contain a copy of it.
382 * Returns the index of the new block in the array.
385 link_cross_validate_uniform_block(void *mem_ctx
,
386 struct gl_uniform_block
**linked_blocks
,
387 unsigned int *num_linked_blocks
,
388 struct gl_uniform_block
*new_block
)
390 for (unsigned int i
= 0; i
< *num_linked_blocks
; i
++) {
391 struct gl_uniform_block
*old_block
= &(*linked_blocks
)[i
];
392 if (strcmp(old_block
->Name
, new_block
->Name
) == 0) {
393 if (old_block
->NumUniforms
!= new_block
->NumUniforms
) {
397 for (unsigned j
= 0; j
< old_block
->NumUniforms
; j
++) {
398 if (strcmp(old_block
->Uniforms
[j
].Name
,
399 new_block
->Uniforms
[j
].Name
) != 0)
402 if (old_block
->Uniforms
[j
].Offset
!=
403 new_block
->Uniforms
[j
].Offset
)
406 if (old_block
->Uniforms
[j
].RowMajor
!=
407 new_block
->Uniforms
[j
].RowMajor
)
414 *linked_blocks
= reralloc(mem_ctx
, *linked_blocks
,
415 struct gl_uniform_block
,
416 *num_linked_blocks
+ 1);
417 int linked_block_index
= (*num_linked_blocks
)++;
418 struct gl_uniform_block
*linked_block
= &(*linked_blocks
)[linked_block_index
];
420 memcpy(linked_block
, new_block
, sizeof(*new_block
));
421 linked_block
->Uniforms
= ralloc_array(*linked_blocks
,
422 struct gl_uniform_buffer_variable
,
423 linked_block
->NumUniforms
);
425 memcpy(linked_block
->Uniforms
,
427 sizeof(*linked_block
->Uniforms
) * linked_block
->NumUniforms
);
429 for (unsigned int i
= 0; i
< linked_block
->NumUniforms
; i
++) {
430 struct gl_uniform_buffer_variable
*ubo_var
=
431 &linked_block
->Uniforms
[i
];
433 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
436 return linked_block_index
;
440 * Walks the IR and update the references to uniform blocks in the
441 * ir_variables to point at linked shader's list (previously, they
442 * would point at the uniform block list in one of the pre-linked
446 link_update_uniform_buffer_variables(struct gl_shader
*shader
)
448 foreach_list(node
, shader
->ir
) {
449 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
451 if ((var
== NULL
) || (var
->uniform_block
== -1))
454 assert(var
->mode
== ir_var_uniform
);
457 for (unsigned i
= 0; i
< shader
->NumUniformBlocks
; i
++) {
458 for (unsigned j
= 0; j
< shader
->UniformBlocks
[i
].NumUniforms
; j
++) {
459 if (!strcmp(var
->name
, shader
->UniformBlocks
[i
].Uniforms
[j
].Name
)) {
461 var
->uniform_block
= i
;
476 link_assign_uniform_block_offsets(struct gl_shader
*shader
)
478 for (unsigned b
= 0; b
< shader
->NumUniformBlocks
; b
++) {
479 struct gl_uniform_block
*block
= &shader
->UniformBlocks
[b
];
482 for (unsigned int i
= 0; i
< block
->NumUniforms
; i
++) {
483 struct gl_uniform_buffer_variable
*ubo_var
= &block
->Uniforms
[i
];
484 const struct glsl_type
*type
= ubo_var
->Type
;
486 unsigned alignment
= type
->std140_base_alignment(ubo_var
->RowMajor
);
487 unsigned size
= type
->std140_size(ubo_var
->RowMajor
);
489 offset
= align(offset
, alignment
);
490 ubo_var
->Offset
= offset
;
493 block
->UniformBufferSize
= offset
;
498 link_assign_uniform_locations(struct gl_shader_program
*prog
)
500 ralloc_free(prog
->UniformStorage
);
501 prog
->UniformStorage
= NULL
;
502 prog
->NumUserUniformStorage
= 0;
504 if (prog
->UniformHash
!= NULL
) {
505 prog
->UniformHash
->clear();
507 prog
->UniformHash
= new string_to_uint_map
;
510 /* Uniforms that lack an initializer in the shader code have an initial
511 * value of zero. This includes sampler uniforms.
513 * Page 24 (page 30 of the PDF) of the GLSL 1.20 spec says:
515 * "The link time initial value is either the value of the variable's
516 * initializer, if present, or 0 if no initializer is present. Sampler
517 * types cannot have initializers."
519 memset(prog
->SamplerUnits
, 0, sizeof(prog
->SamplerUnits
));
521 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
522 if (prog
->_LinkedShaders
[i
] == NULL
)
525 if (!link_update_uniform_buffer_variables(prog
->_LinkedShaders
[i
]))
529 /* First pass: Count the uniform resources used by the user-defined
530 * uniforms. While this happens, each active uniform will have an index
533 * Note: this is *NOT* the index that is returned to the application by
534 * glGetUniformLocation.
536 count_uniform_size
uniform_size(prog
->UniformHash
);
537 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
538 if (prog
->_LinkedShaders
[i
] == NULL
)
541 /* Reset various per-shader target counts.
543 uniform_size
.start_shader();
545 foreach_list(node
, prog
->_LinkedShaders
[i
]->ir
) {
546 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
548 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
))
551 /* FINISHME: Update code to process built-in uniforms!
553 if (strncmp("gl_", var
->name
, 3) == 0)
556 uniform_size
.process(var
);
559 prog
->_LinkedShaders
[i
]->num_samplers
= uniform_size
.num_shader_samplers
;
560 prog
->_LinkedShaders
[i
]->num_uniform_components
=
561 uniform_size
.num_shader_uniform_components
;
564 const unsigned num_user_uniforms
= uniform_size
.num_active_uniforms
;
565 const unsigned num_data_slots
= uniform_size
.num_values
;
567 /* On the outside chance that there were no uniforms, bail out.
569 if (num_user_uniforms
== 0)
572 struct gl_uniform_storage
*uniforms
=
573 rzalloc_array(prog
, struct gl_uniform_storage
, num_user_uniforms
);
574 union gl_constant_value
*data
=
575 rzalloc_array(uniforms
, union gl_constant_value
, num_data_slots
);
577 union gl_constant_value
*data_end
= &data
[num_data_slots
];
580 parcel_out_uniform_storage
parcel(prog
->UniformHash
, uniforms
, data
);
582 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
583 if (prog
->_LinkedShaders
[i
] == NULL
)
586 /* Reset various per-shader target counts.
588 parcel
.start_shader();
590 foreach_list(node
, prog
->_LinkedShaders
[i
]->ir
) {
591 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
593 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
))
596 /* FINISHME: Update code to process built-in uniforms!
598 if (strncmp("gl_", var
->name
, 3) == 0)
601 parcel
.set_and_process(prog
, var
);
604 prog
->_LinkedShaders
[i
]->active_samplers
= parcel
.shader_samplers_used
;
605 prog
->_LinkedShaders
[i
]->shadow_samplers
= parcel
.shader_shadow_samplers
;
608 assert(sizeof(prog
->SamplerTargets
) == sizeof(parcel
.targets
));
609 memcpy(prog
->SamplerTargets
, parcel
.targets
, sizeof(prog
->SamplerTargets
));
612 for (unsigned i
= 0; i
< num_user_uniforms
; i
++) {
613 assert(uniforms
[i
].storage
!= NULL
);
616 assert(parcel
.values
== data_end
);
619 prog
->NumUserUniformStorage
= num_user_uniforms
;
620 prog
->UniformStorage
= uniforms
;
622 link_set_uniform_initializers(prog
);