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
;
82 /* Append '.field' to the current uniform name. */
83 ralloc_asprintf_rewrite_tail(name
, name_length
, ".%s", field
);
85 recursion(t
->fields
.structure
[i
].type
, name
,
86 name_length
+ 1 + strlen(field
));
88 } else if (t
->is_array() && t
->fields
.array
->is_record()) {
89 for (unsigned i
= 0; i
< t
->length
; i
++) {
92 /* Append the subscript to the current uniform name */
93 const unsigned subscript_length
= snprintf(subscript
, 13, "[%u]", i
);
94 ralloc_asprintf_rewrite_tail(name
, name_length
, "%s", subscript
);
96 recursion(t
->fields
.array
, name
, name_length
+ subscript_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 char *key
= strdup(name
);
178 this->map
->put(this->num_active_uniforms
, key
);
180 /* Each leaf uniform occupies one entry in the list of active
183 this->num_active_uniforms
++;
184 this->num_values
+= values
;
187 struct string_to_uint_map
*map
;
191 * Class to help parcel out pieces of backing storage to uniforms
193 * Each uniform processed has some range of the \c gl_constant_value
194 * structures associated with it. The association is done by finding
195 * the uniform in the \c string_to_uint_map and using the value from
196 * the map to connect that slot in the \c gl_uniform_storage table
197 * with the next available slot in the \c gl_constant_value array.
200 * This class assumes that every uniform that will be processed is
201 * already in the \c string_to_uint_map. In addition, it assumes that
202 * the \c gl_uniform_storage and \c gl_constant_value arrays are "big
205 class parcel_out_uniform_storage
: public uniform_field_visitor
{
207 parcel_out_uniform_storage(struct string_to_uint_map
*map
,
208 struct gl_uniform_storage
*uniforms
,
209 union gl_constant_value
*values
)
210 : map(map
), uniforms(uniforms
), next_sampler(0), values(values
)
212 memset(this->targets
, 0, sizeof(this->targets
));
216 virtual void visit_field(const glsl_type
*type
, const char *name
)
218 assert(!type
->is_record());
219 assert(!(type
->is_array() && type
->fields
.array
->is_record()));
222 bool found
= this->map
->get(id
, name
);
228 /* If there is already storage associated with this uniform, it means
229 * that it was set while processing an earlier shader stage. For
230 * example, we may be processing the uniform in the fragment shader, but
231 * the uniform was already processed in the vertex shader.
233 if (this->uniforms
[id
].storage
!= NULL
)
236 const glsl_type
*base_type
;
237 if (type
->is_array()) {
238 this->uniforms
[id
].array_elements
= type
->length
;
239 base_type
= type
->fields
.array
;
241 this->uniforms
[id
].array_elements
= 0;
245 if (base_type
->is_sampler()) {
246 this->uniforms
[id
].sampler
= this->next_sampler
;
248 /* Increment the sampler by 1 for non-arrays and by the number of
249 * array elements for arrays.
251 this->next_sampler
+= MAX2(1, this->uniforms
[id
].array_elements
);
253 const gl_texture_index target
= base_type
->sampler_index();
254 for (unsigned i
= this->uniforms
[id
].sampler
255 ; i
< this->next_sampler
257 this->targets
[i
] = target
;
261 this->uniforms
[id
].sampler
= ~0;
264 this->uniforms
[id
].name
= strdup(name
);
265 this->uniforms
[id
].type
= base_type
;
266 this->uniforms
[id
].initialized
= 0;
267 this->uniforms
[id
].num_driver_storage
= 0;
268 this->uniforms
[id
].driver_storage
= NULL
;
269 this->uniforms
[id
].storage
= this->values
;
271 this->values
+= values_for_type(type
);
274 struct string_to_uint_map
*map
;
276 struct gl_uniform_storage
*uniforms
;
277 unsigned next_sampler
;
280 union gl_constant_value
*values
;
282 gl_texture_index targets
[MAX_SAMPLERS
];
286 link_assign_uniform_locations(struct gl_shader_program
*prog
)
288 ralloc_free(prog
->UniformStorage
);
289 prog
->UniformStorage
= NULL
;
290 prog
->NumUserUniformStorage
= 0;
292 if (prog
->UniformHash
!= NULL
) {
293 prog
->UniformHash
->clear();
295 prog
->UniformHash
= new string_to_uint_map
;
298 for (unsigned i
= 0; i
< Elements(prog
->SamplerUnits
); i
++) {
299 prog
->SamplerUnits
[i
] = i
;
302 /* First pass: Count the uniform resources used by the user-defined
303 * uniforms. While this happens, each active uniform will have an index
306 * Note: this is *NOT* the index that is returned to the application by
307 * glGetUniformLocation.
309 count_uniform_size
uniform_size(prog
->UniformHash
);
310 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
311 if (prog
->_LinkedShaders
[i
] == NULL
)
314 /* Reset various per-shader target counts.
316 uniform_size
.start_shader();
318 foreach_list(node
, prog
->_LinkedShaders
[i
]->ir
) {
319 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
321 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
))
324 /* FINISHME: Update code to process built-in uniforms!
326 if (strncmp("gl_", var
->name
, 3) == 0)
329 uniform_size
.process(var
);
332 prog
->_LinkedShaders
[i
]->num_samplers
= uniform_size
.num_shader_samplers
;
333 prog
->_LinkedShaders
[i
]->num_uniform_components
=
334 uniform_size
.num_shader_uniform_components
;
337 const unsigned num_user_uniforms
= uniform_size
.num_active_uniforms
;
338 const unsigned num_data_slots
= uniform_size
.num_values
;
340 /* On the outside chance that there were no uniforms, bail out.
342 if (num_user_uniforms
== 0)
345 struct gl_uniform_storage
*uniforms
=
346 rzalloc_array(prog
, struct gl_uniform_storage
, num_user_uniforms
);
347 union gl_constant_value
*data
=
348 rzalloc_array(uniforms
, union gl_constant_value
, num_data_slots
);
350 union gl_constant_value
*data_end
= &data
[num_data_slots
];
353 parcel_out_uniform_storage
parcel(prog
->UniformHash
, uniforms
, data
);
355 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
356 if (prog
->_LinkedShaders
[i
] == NULL
)
359 foreach_list(node
, prog
->_LinkedShaders
[i
]->ir
) {
360 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
362 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
))
365 /* FINISHME: Update code to process built-in uniforms!
367 if (strncmp("gl_", var
->name
, 3) == 0)
374 assert(sizeof(prog
->SamplerTargets
) == sizeof(parcel
.targets
));
375 memcpy(prog
->SamplerTargets
, parcel
.targets
, sizeof(prog
->SamplerTargets
));
378 for (unsigned i
= 0; i
< num_user_uniforms
; i
++) {
379 assert(uniforms
[i
].storage
!= NULL
);
382 assert(parcel
.values
== data_end
);
385 prog
->NumUserUniformStorage
= num_user_uniforms
;
386 prog
->UniformStorage
= uniforms
;