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 uniform_field_visitor::process(ir_variable
*var
)
57 const glsl_type
*t
= var
->type
;
59 /* Only strdup the name if we actually will need to modify it. */
60 if (t
->is_record() || (t
->is_array() && t
->fields
.array
->is_record())) {
61 char *name
= ralloc_strdup(NULL
, var
->name
);
62 recursion(var
->type
, &name
, strlen(name
));
65 this->visit_field(t
, var
->name
);
70 uniform_field_visitor::recursion(const glsl_type
*t
, char **name
,
73 /* Records need to have each field processed individually.
75 * Arrays of records need to have each array element processed
76 * individually, then each field of the resulting array elements processed
80 for (unsigned i
= 0; i
< t
->length
; i
++) {
81 const char *field
= t
->fields
.structure
[i
].name
;
82 size_t new_length
= name_length
;
84 /* Append '.field' to the current uniform name. */
85 ralloc_asprintf_rewrite_tail(name
, &new_length
, ".%s", field
);
87 recursion(t
->fields
.structure
[i
].type
, name
, new_length
);
89 } else if (t
->is_array() && t
->fields
.array
->is_record()) {
90 for (unsigned i
= 0; i
< t
->length
; i
++) {
91 size_t new_length
= name_length
;
93 /* Append the subscript to the current uniform name */
94 ralloc_asprintf_rewrite_tail(name
, &new_length
, "[%u]", i
);
96 recursion(t
->fields
.array
, name
, new_length
);
99 this->visit_field(t
, *name
);
104 * Class to help calculate the storage requirements for a set of uniforms
106 * As uniforms are added to the active set the number of active uniforms and
107 * the storage requirements for those uniforms are accumulated. The active
108 * uniforms are added the the hash table supplied to the constructor.
110 * If the same uniform is added multiple times (i.e., once for each shader
111 * target), it will only be accounted once.
113 class count_uniform_size
: public uniform_field_visitor
{
115 count_uniform_size(struct string_to_uint_map
*map
)
116 : num_active_uniforms(0), num_values(0), num_shader_samplers(0),
117 num_shader_uniform_components(0), map(map
)
124 this->num_shader_samplers
= 0;
125 this->num_shader_uniform_components
= 0;
129 * Total number of active uniforms counted
131 unsigned num_active_uniforms
;
134 * Number of data values required to back the storage for the active uniforms
139 * Number of samplers used
141 unsigned num_shader_samplers
;
144 * Number of uniforms used in the current shader
146 unsigned num_shader_uniform_components
;
149 virtual void visit_field(const glsl_type
*type
, const char *name
)
151 assert(!type
->is_record());
152 assert(!(type
->is_array() && type
->fields
.array
->is_record()));
154 /* Count the number of samplers regardless of whether the uniform is
155 * already in the hash table. The hash table prevents adding the same
156 * uniform for multiple shader targets, but in this case we want to
157 * count it for each shader target.
159 const unsigned values
= values_for_type(type
);
160 if (type
->contains_sampler()) {
161 this->num_shader_samplers
+=
162 type
->is_array() ? type
->array_size() : 1;
164 /* Accumulate the total number of uniform slots used by this shader.
165 * Note that samplers do not count against this limit because they
166 * don't use any storage on current hardware.
168 this->num_shader_uniform_components
+= values
;
171 /* If the uniform is already in the map, there's nothing more to do.
174 if (this->map
->get(id
, name
))
177 this->map
->put(this->num_active_uniforms
, name
);
179 /* Each leaf uniform occupies one entry in the list of active
182 this->num_active_uniforms
++;
183 this->num_values
+= values
;
186 struct string_to_uint_map
*map
;
190 * Class to help parcel out pieces of backing storage to uniforms
192 * Each uniform processed has some range of the \c gl_constant_value
193 * structures associated with it. The association is done by finding
194 * the uniform in the \c string_to_uint_map and using the value from
195 * the map to connect that slot in the \c gl_uniform_storage table
196 * with the next available slot in the \c gl_constant_value array.
199 * This class assumes that every uniform that will be processed is
200 * already in the \c string_to_uint_map. In addition, it assumes that
201 * the \c gl_uniform_storage and \c gl_constant_value arrays are "big
204 class parcel_out_uniform_storage
: public uniform_field_visitor
{
206 parcel_out_uniform_storage(struct string_to_uint_map
*map
,
207 struct gl_uniform_storage
*uniforms
,
208 union gl_constant_value
*values
)
209 : map(map
), uniforms(uniforms
), next_sampler(0), values(values
)
211 memset(this->targets
, 0, sizeof(this->targets
));
216 this->shader_samplers_used
= 0;
217 this->shader_shadow_samplers
= 0;
220 void set_and_process(struct gl_shader_program
*prog
,
224 if (var
->uniform_block
!= -1) {
225 struct gl_uniform_block
*block
=
226 &prog
->UniformBlocks
[var
->uniform_block
];
228 ubo_block_index
= var
->uniform_block
;
229 ubo_var_index
= var
->location
;
230 ubo_var
= &block
->Uniforms
[var
->location
];
231 ubo_byte_offset
= ubo_var
->Offset
;
237 struct gl_uniform_buffer_variable
*ubo_var
;
243 virtual void visit_field(const glsl_type
*type
, const char *name
)
245 assert(!type
->is_record());
246 assert(!(type
->is_array() && type
->fields
.array
->is_record()));
249 bool found
= this->map
->get(id
, name
);
255 /* If there is already storage associated with this uniform, it means
256 * that it was set while processing an earlier shader stage. For
257 * example, we may be processing the uniform in the fragment shader, but
258 * the uniform was already processed in the vertex shader.
260 if (this->uniforms
[id
].storage
!= NULL
) {
261 /* If the uniform already has storage set from another shader stage,
262 * mark the samplers used for this shader stage.
264 if (type
->contains_sampler()) {
265 const unsigned count
= MAX2(1, this->uniforms
[id
].array_elements
);
266 const unsigned shadow
= (type
->is_array())
267 ? type
->fields
.array
->sampler_shadow
: type
->sampler_shadow
;
269 for (unsigned i
= 0; i
< count
; i
++) {
270 const unsigned s
= this->uniforms
[id
].sampler
+ i
;
272 this->shader_samplers_used
|= 1U << s
;
273 this->shader_shadow_samplers
|= shadow
<< s
;
280 const glsl_type
*base_type
;
281 if (type
->is_array()) {
282 this->uniforms
[id
].array_elements
= type
->length
;
283 base_type
= type
->fields
.array
;
285 this->uniforms
[id
].array_elements
= 0;
289 if (base_type
->is_sampler()) {
290 this->uniforms
[id
].sampler
= this->next_sampler
;
292 /* Increment the sampler by 1 for non-arrays and by the number of
293 * array elements for arrays.
295 this->next_sampler
+= MAX2(1, this->uniforms
[id
].array_elements
);
297 const gl_texture_index target
= base_type
->sampler_index();
298 const unsigned shadow
= base_type
->sampler_shadow
;
299 for (unsigned i
= this->uniforms
[id
].sampler
300 ; i
< this->next_sampler
302 this->targets
[i
] = target
;
303 this->shader_samplers_used
|= 1U << i
;
304 this->shader_shadow_samplers
|= shadow
<< i
;
308 this->uniforms
[id
].sampler
= ~0;
311 this->uniforms
[id
].name
= ralloc_strdup(this->uniforms
, name
);
312 this->uniforms
[id
].type
= base_type
;
313 this->uniforms
[id
].initialized
= 0;
314 this->uniforms
[id
].num_driver_storage
= 0;
315 this->uniforms
[id
].driver_storage
= NULL
;
316 this->uniforms
[id
].storage
= this->values
;
318 this->uniforms
[id
].block_index
= this->ubo_block_index
;
320 /* FINISHME: Actual std140 offset assignment. */
321 this->uniforms
[id
].offset
= this->ubo_byte_offset
;
322 this->ubo_byte_offset
+= 4 * type
->components();
323 this->uniforms
[id
].array_stride
= 0;
324 this->uniforms
[id
].matrix_stride
= 0;
325 this->uniforms
[id
].row_major
= base_type
->is_matrix() &&
328 this->uniforms
[id
].block_index
= -1;
329 this->uniforms
[id
].offset
= -1;
330 this->uniforms
[id
].array_stride
= -1;
331 this->uniforms
[id
].matrix_stride
= -1;
332 this->uniforms
[id
].row_major
= false;
335 this->values
+= values_for_type(type
);
338 struct string_to_uint_map
*map
;
340 struct gl_uniform_storage
*uniforms
;
341 unsigned next_sampler
;
344 union gl_constant_value
*values
;
346 gl_texture_index targets
[MAX_SAMPLERS
];
349 * Mask of samplers used by the current shader stage.
351 unsigned shader_samplers_used
;
354 * Mask of samplers used by the current shader stage for shadows.
356 unsigned shader_shadow_samplers
;
360 * Merges a uniform block into an array of uniform blocks that may or
361 * may not already contain a copy of it.
363 * Returns the index of the new block in the array.
366 link_cross_validate_uniform_block(void *mem_ctx
,
367 struct gl_uniform_block
**linked_blocks
,
368 unsigned int *num_linked_blocks
,
369 struct gl_uniform_block
*new_block
)
371 for (unsigned int i
= 0; i
< *num_linked_blocks
; i
++) {
372 struct gl_uniform_block
*old_block
= &(*linked_blocks
)[i
];
373 if (strcmp(old_block
->Name
, new_block
->Name
) == 0) {
374 if (old_block
->NumUniforms
!= new_block
->NumUniforms
) {
378 for (unsigned j
= 0; j
< old_block
->NumUniforms
; j
++) {
379 if (strcmp(old_block
->Uniforms
[j
].Name
,
380 new_block
->Uniforms
[j
].Name
) != 0)
383 if (old_block
->Uniforms
[j
].Offset
!=
384 new_block
->Uniforms
[j
].Offset
)
387 if (old_block
->Uniforms
[j
].RowMajor
!=
388 new_block
->Uniforms
[j
].RowMajor
)
395 *linked_blocks
= reralloc(mem_ctx
, *linked_blocks
,
396 struct gl_uniform_block
,
397 *num_linked_blocks
+ 1);
398 int linked_block_index
= (*num_linked_blocks
)++;
399 struct gl_uniform_block
*linked_block
= &(*linked_blocks
)[linked_block_index
];
401 memcpy(linked_block
, new_block
, sizeof(*new_block
));
402 linked_block
->Uniforms
= ralloc_array(*linked_blocks
,
403 struct gl_uniform_buffer_variable
,
404 linked_block
->NumUniforms
);
406 memcpy(linked_block
->Uniforms
,
408 sizeof(*linked_block
->Uniforms
) * linked_block
->NumUniforms
);
410 for (unsigned int i
= 0; i
< linked_block
->NumUniforms
; i
++) {
411 struct gl_uniform_buffer_variable
*ubo_var
=
412 &linked_block
->Uniforms
[i
];
414 ubo_var
->Name
= ralloc_strdup(*linked_blocks
, ubo_var
->Name
);
417 return linked_block_index
;
421 * Walks the IR and update the references to uniform blocks in the
422 * ir_variables to point at linked shader's list (previously, they
423 * would point at the uniform block list in one of the pre-linked
427 link_update_uniform_buffer_variables(struct gl_shader
*shader
)
429 foreach_list(node
, shader
->ir
) {
430 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
432 if ((var
== NULL
) || (var
->uniform_block
== -1))
435 assert(var
->mode
== ir_var_uniform
);
438 for (unsigned i
= 0; i
< shader
->NumUniformBlocks
; i
++) {
439 for (unsigned j
= 0; j
< shader
->UniformBlocks
[i
].NumUniforms
; j
++) {
440 if (!strcmp(var
->name
, shader
->UniformBlocks
[i
].Uniforms
[j
].Name
)) {
442 var
->uniform_block
= i
;
457 link_assign_uniform_locations(struct gl_shader_program
*prog
)
459 ralloc_free(prog
->UniformStorage
);
460 prog
->UniformStorage
= NULL
;
461 prog
->NumUserUniformStorage
= 0;
463 if (prog
->UniformHash
!= NULL
) {
464 prog
->UniformHash
->clear();
466 prog
->UniformHash
= new string_to_uint_map
;
469 /* Uniforms that lack an initializer in the shader code have an initial
470 * value of zero. This includes sampler uniforms.
472 * Page 24 (page 30 of the PDF) of the GLSL 1.20 spec says:
474 * "The link time initial value is either the value of the variable's
475 * initializer, if present, or 0 if no initializer is present. Sampler
476 * types cannot have initializers."
478 memset(prog
->SamplerUnits
, 0, sizeof(prog
->SamplerUnits
));
480 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
481 if (prog
->_LinkedShaders
[i
] == NULL
)
484 if (!link_update_uniform_buffer_variables(prog
->_LinkedShaders
[i
]))
488 /* First pass: Count the uniform resources used by the user-defined
489 * uniforms. While this happens, each active uniform will have an index
492 * Note: this is *NOT* the index that is returned to the application by
493 * glGetUniformLocation.
495 count_uniform_size
uniform_size(prog
->UniformHash
);
496 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
497 if (prog
->_LinkedShaders
[i
] == NULL
)
500 /* Reset various per-shader target counts.
502 uniform_size
.start_shader();
504 foreach_list(node
, prog
->_LinkedShaders
[i
]->ir
) {
505 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
507 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
))
510 /* FINISHME: Update code to process built-in uniforms!
512 if (strncmp("gl_", var
->name
, 3) == 0)
515 uniform_size
.process(var
);
518 prog
->_LinkedShaders
[i
]->num_samplers
= uniform_size
.num_shader_samplers
;
519 prog
->_LinkedShaders
[i
]->num_uniform_components
=
520 uniform_size
.num_shader_uniform_components
;
523 const unsigned num_user_uniforms
= uniform_size
.num_active_uniforms
;
524 const unsigned num_data_slots
= uniform_size
.num_values
;
526 /* On the outside chance that there were no uniforms, bail out.
528 if (num_user_uniforms
== 0)
531 struct gl_uniform_storage
*uniforms
=
532 rzalloc_array(prog
, struct gl_uniform_storage
, num_user_uniforms
);
533 union gl_constant_value
*data
=
534 rzalloc_array(uniforms
, union gl_constant_value
, num_data_slots
);
536 union gl_constant_value
*data_end
= &data
[num_data_slots
];
539 parcel_out_uniform_storage
parcel(prog
->UniformHash
, uniforms
, data
);
541 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
542 if (prog
->_LinkedShaders
[i
] == NULL
)
545 /* Reset various per-shader target counts.
547 parcel
.start_shader();
549 foreach_list(node
, prog
->_LinkedShaders
[i
]->ir
) {
550 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
552 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
))
555 /* FINISHME: Update code to process built-in uniforms!
557 if (strncmp("gl_", var
->name
, 3) == 0)
560 parcel
.set_and_process(prog
, var
);
563 prog
->_LinkedShaders
[i
]->active_samplers
= parcel
.shader_samplers_used
;
564 prog
->_LinkedShaders
[i
]->shadow_samplers
= parcel
.shader_shadow_samplers
;
567 assert(sizeof(prog
->SamplerTargets
) == sizeof(parcel
.targets
));
568 memcpy(prog
->SamplerTargets
, parcel
.targets
, sizeof(prog
->SamplerTargets
));
571 for (unsigned i
= 0; i
< num_user_uniforms
; i
++) {
572 assert(uniforms
[i
].storage
!= NULL
);
575 assert(parcel
.values
== data_end
);
578 prog
->NumUserUniformStorage
= num_user_uniforms
;
579 prog
->UniformStorage
= uniforms
;
581 link_set_uniform_initializers(prog
);