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 * \file link_uniforms.cpp
33 * Assign locations for GLSL uniforms.
35 * \author Ian Romanick <ian.d.romanick@intel.com>
39 * Count the backing storage requirements for a type
42 values_for_type(const glsl_type
*type
)
44 if (type
->is_sampler()) {
46 } else if (type
->is_array() && type
->fields
.array
->is_sampler()) {
47 return type
->array_size();
49 return type
->component_slots();
54 uniform_field_visitor::process(ir_variable
*var
)
56 const glsl_type
*t
= var
->type
;
58 /* Only strdup the name if we actually will need to modify it. */
59 if (t
->is_record() || (t
->is_array() && t
->fields
.array
->is_record())) {
60 char *name
= ralloc_strdup(NULL
, var
->name
);
61 recursion(var
->type
, &name
, strlen(name
));
64 this->visit_field(t
, var
->name
);
69 uniform_field_visitor::recursion(const glsl_type
*t
, char **name
,
72 /* Records need to have each field processed individually.
74 * Arrays of records need to have each array element processed
75 * individually, then each field of the resulting array elements processed
79 for (unsigned i
= 0; i
< t
->length
; i
++) {
80 const char *field
= t
->fields
.structure
[i
].name
;
81 size_t new_length
= name_length
;
83 /* Append '.field' to the current uniform name. */
84 ralloc_asprintf_rewrite_tail(name
, &new_length
, ".%s", field
);
86 recursion(t
->fields
.structure
[i
].type
, name
, new_length
);
88 } else if (t
->is_array() && t
->fields
.array
->is_record()) {
89 for (unsigned i
= 0; i
< t
->length
; i
++) {
90 size_t new_length
= name_length
;
92 /* Append the subscript to the current uniform name */
93 ralloc_asprintf_rewrite_tail(name
, &new_length
, "[%u]", i
);
95 recursion(t
->fields
.array
, name
, new_length
);
98 this->visit_field(t
, *name
);
103 * Class to help calculate the storage requirements for a set of uniforms
105 * As uniforms are added to the active set the number of active uniforms and
106 * the storage requirements for those uniforms are accumulated. The active
107 * uniforms are added the the hash table supplied to the constructor.
109 * If the same uniform is added multiple times (i.e., once for each shader
110 * target), it will only be accounted once.
112 class count_uniform_size
: public uniform_field_visitor
{
114 count_uniform_size(struct string_to_uint_map
*map
)
115 : num_active_uniforms(0), num_values(0), num_shader_samplers(0),
116 num_shader_uniform_components(0), map(map
)
123 this->num_shader_samplers
= 0;
124 this->num_shader_uniform_components
= 0;
128 * Total number of active uniforms counted
130 unsigned num_active_uniforms
;
133 * Number of data values required to back the storage for the active uniforms
138 * Number of samplers used
140 unsigned num_shader_samplers
;
143 * Number of uniforms used in the current shader
145 unsigned num_shader_uniform_components
;
148 virtual void visit_field(const glsl_type
*type
, const char *name
)
150 assert(!type
->is_record());
151 assert(!(type
->is_array() && type
->fields
.array
->is_record()));
153 /* Count the number of samplers regardless of whether the uniform is
154 * already in the hash table. The hash table prevents adding the same
155 * uniform for multiple shader targets, but in this case we want to
156 * count it for each shader target.
158 const unsigned values
= values_for_type(type
);
159 if (type
->contains_sampler()) {
160 this->num_shader_samplers
+=
161 type
->is_array() ? type
->array_size() : 1;
163 /* Accumulate the total number of uniform slots used by this shader.
164 * Note that samplers do not count against this limit because they
165 * don't use any storage on current hardware.
167 this->num_shader_uniform_components
+= values
;
170 /* If the uniform is already in the map, there's nothing more to do.
173 if (this->map
->get(id
, name
))
176 char *key
= strdup(name
);
177 this->map
->put(this->num_active_uniforms
, key
);
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;
221 virtual void visit_field(const glsl_type
*type
, const char *name
)
223 assert(!type
->is_record());
224 assert(!(type
->is_array() && type
->fields
.array
->is_record()));
227 bool found
= this->map
->get(id
, name
);
233 /* If there is already storage associated with this uniform, it means
234 * that it was set while processing an earlier shader stage. For
235 * example, we may be processing the uniform in the fragment shader, but
236 * the uniform was already processed in the vertex shader.
238 if (this->uniforms
[id
].storage
!= NULL
) {
239 /* If the uniform already has storage set from another shader stage,
240 * mark the samplers used for this shader stage.
242 if (type
->contains_sampler()) {
243 const unsigned count
= MAX2(1, this->uniforms
[id
].array_elements
);
244 const unsigned shadow
= (type
->is_array())
245 ? type
->fields
.array
->sampler_shadow
: type
->sampler_shadow
;
247 for (unsigned i
= 0; i
< count
; i
++) {
248 const unsigned s
= this->uniforms
[id
].sampler
+ i
;
250 this->shader_samplers_used
|= 1U << s
;
251 this->shader_shadow_samplers
|= shadow
<< s
;
258 const glsl_type
*base_type
;
259 if (type
->is_array()) {
260 this->uniforms
[id
].array_elements
= type
->length
;
261 base_type
= type
->fields
.array
;
263 this->uniforms
[id
].array_elements
= 0;
267 if (base_type
->is_sampler()) {
268 this->uniforms
[id
].sampler
= this->next_sampler
;
270 /* Increment the sampler by 1 for non-arrays and by the number of
271 * array elements for arrays.
273 this->next_sampler
+= MAX2(1, this->uniforms
[id
].array_elements
);
275 const gl_texture_index target
= base_type
->sampler_index();
276 const unsigned shadow
= base_type
->sampler_shadow
;
277 for (unsigned i
= this->uniforms
[id
].sampler
278 ; i
< this->next_sampler
280 this->targets
[i
] = target
;
281 this->shader_samplers_used
|= 1U << i
;
282 this->shader_shadow_samplers
|= shadow
<< i
;
286 this->uniforms
[id
].sampler
= ~0;
289 this->uniforms
[id
].name
= ralloc_strdup(this->uniforms
, name
);
290 this->uniforms
[id
].type
= base_type
;
291 this->uniforms
[id
].initialized
= 0;
292 this->uniforms
[id
].num_driver_storage
= 0;
293 this->uniforms
[id
].driver_storage
= NULL
;
294 this->uniforms
[id
].storage
= this->values
;
296 this->values
+= values_for_type(type
);
299 struct string_to_uint_map
*map
;
301 struct gl_uniform_storage
*uniforms
;
302 unsigned next_sampler
;
305 union gl_constant_value
*values
;
307 gl_texture_index targets
[MAX_SAMPLERS
];
310 * Mask of samplers used by the current shader stage.
312 unsigned shader_samplers_used
;
315 * Mask of samplers used by the current shader stage for shadows.
317 unsigned shader_shadow_samplers
;
321 link_assign_uniform_locations(struct gl_shader_program
*prog
)
323 ralloc_free(prog
->UniformStorage
);
324 prog
->UniformStorage
= NULL
;
325 prog
->NumUserUniformStorage
= 0;
327 if (prog
->UniformHash
!= NULL
) {
328 prog
->UniformHash
->clear();
330 prog
->UniformHash
= new string_to_uint_map
;
333 for (unsigned i
= 0; i
< Elements(prog
->SamplerUnits
); i
++) {
334 prog
->SamplerUnits
[i
] = i
;
337 /* First pass: Count the uniform resources used by the user-defined
338 * uniforms. While this happens, each active uniform will have an index
341 * Note: this is *NOT* the index that is returned to the application by
342 * glGetUniformLocation.
344 count_uniform_size
uniform_size(prog
->UniformHash
);
345 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
346 if (prog
->_LinkedShaders
[i
] == NULL
)
349 /* Reset various per-shader target counts.
351 uniform_size
.start_shader();
353 foreach_list(node
, prog
->_LinkedShaders
[i
]->ir
) {
354 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
356 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
))
359 /* FINISHME: Update code to process built-in uniforms!
361 if (strncmp("gl_", var
->name
, 3) == 0)
364 uniform_size
.process(var
);
367 prog
->_LinkedShaders
[i
]->num_samplers
= uniform_size
.num_shader_samplers
;
368 prog
->_LinkedShaders
[i
]->num_uniform_components
=
369 uniform_size
.num_shader_uniform_components
;
372 const unsigned num_user_uniforms
= uniform_size
.num_active_uniforms
;
373 const unsigned num_data_slots
= uniform_size
.num_values
;
375 /* On the outside chance that there were no uniforms, bail out.
377 if (num_user_uniforms
== 0)
380 struct gl_uniform_storage
*uniforms
=
381 rzalloc_array(prog
, struct gl_uniform_storage
, num_user_uniforms
);
382 union gl_constant_value
*data
=
383 rzalloc_array(uniforms
, union gl_constant_value
, num_data_slots
);
385 union gl_constant_value
*data_end
= &data
[num_data_slots
];
388 parcel_out_uniform_storage
parcel(prog
->UniformHash
, uniforms
, data
);
390 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
391 if (prog
->_LinkedShaders
[i
] == NULL
)
394 /* Reset various per-shader target counts.
396 parcel
.start_shader();
398 foreach_list(node
, prog
->_LinkedShaders
[i
]->ir
) {
399 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
401 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
))
404 /* FINISHME: Update code to process built-in uniforms!
406 if (strncmp("gl_", var
->name
, 3) == 0)
412 prog
->_LinkedShaders
[i
]->active_samplers
= parcel
.shader_samplers_used
;
413 prog
->_LinkedShaders
[i
]->shadow_samplers
= parcel
.shader_shadow_samplers
;
416 assert(sizeof(prog
->SamplerTargets
) == sizeof(parcel
.targets
));
417 memcpy(prog
->SamplerTargets
, parcel
.targets
, sizeof(prog
->SamplerTargets
));
420 for (unsigned i
= 0; i
< num_user_uniforms
; i
++) {
421 assert(uniforms
[i
].storage
!= NULL
);
424 assert(parcel
.values
== data_end
);
427 prog
->NumUserUniformStorage
= num_user_uniforms
;
428 prog
->UniformStorage
= uniforms
;