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), map(map
)
122 * Total number of active uniforms counted
124 unsigned num_active_uniforms
;
127 * Number of data values required to back the storage for the active uniforms
132 virtual void visit_field(const glsl_type
*type
, const char *name
)
134 assert(!type
->is_record());
135 assert(!(type
->is_array() && type
->fields
.array
->is_record()));
137 /* If the uniform is already in the map, there's nothing more to do.
140 if (this->map
->get(id
, name
))
143 char *key
= strdup(name
);
144 this->map
->put(this->num_active_uniforms
, key
);
146 /* Each leaf uniform occupies one entry in the list of active
149 this->num_active_uniforms
++;
150 this->num_values
+= values_for_type(type
);
153 struct string_to_uint_map
*map
;
157 * Class to help parcel out pieces of backing storage to uniforms
159 * Each uniform processed has some range of the \c gl_constant_value
160 * structures associated with it. The association is done by finding
161 * the uniform in the \c string_to_uint_map and using the value from
162 * the map to connect that slot in the \c gl_uniform_storage table
163 * with the next available slot in the \c gl_constant_value array.
166 * This class assumes that every uniform that will be processed is
167 * already in the \c string_to_uint_map. In addition, it assumes that
168 * the \c gl_uniform_storage and \c gl_constant_value arrays are "big
171 class parcel_out_uniform_storage
: public uniform_field_visitor
{
173 parcel_out_uniform_storage(struct string_to_uint_map
*map
,
174 struct gl_uniform_storage
*uniforms
,
175 union gl_constant_value
*values
)
176 : map(map
), uniforms(uniforms
), next_sampler(0), values(values
)
182 virtual void visit_field(const glsl_type
*type
, const char *name
)
184 assert(!type
->is_record());
185 assert(!(type
->is_array() && type
->fields
.array
->is_record()));
188 bool found
= this->map
->get(id
, name
);
194 /* If there is already storage associated with this uniform, it means
195 * that it was set while processing an earlier shader stage. For
196 * example, we may be processing the uniform in the fragment shader, but
197 * the uniform was already processed in the vertex shader.
199 if (this->uniforms
[id
].storage
!= NULL
)
202 const glsl_type
*base_type
;
203 if (type
->is_array()) {
204 this->uniforms
[id
].array_elements
= type
->length
;
205 base_type
= type
->fields
.array
;
207 this->uniforms
[id
].array_elements
= 0;
211 if (base_type
->is_sampler()) {
212 this->uniforms
[id
].sampler
= this->next_sampler
;
214 /* Increment the sampler by 1 for non-arrays and by the number of
215 * array elements for arrays.
217 this->next_sampler
+= MAX2(1, this->uniforms
[id
].array_elements
);
219 this->uniforms
[id
].sampler
= ~0;
222 this->uniforms
[id
].name
= strdup(name
);
223 this->uniforms
[id
].type
= base_type
;
224 this->uniforms
[id
].initialized
= 0;
225 this->uniforms
[id
].num_driver_storage
= 0;
226 this->uniforms
[id
].driver_storage
= NULL
;
227 this->uniforms
[id
].storage
= this->values
;
229 this->values
+= values_for_type(type
);
232 struct string_to_uint_map
*map
;
234 struct gl_uniform_storage
*uniforms
;
235 unsigned next_sampler
;
238 union gl_constant_value
*values
;