linker: Add helper class for parcelling out backing storage to uniforms
[mesa.git] / src / glsl / link_uniforms.cpp
1 /*
2 * Copyright © 2011 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 #include "main/core.h"
25 #include "ir.h"
26 #include "linker.h"
27 #include "ir_uniform.h"
28 #include "glsl_symbol_table.h"
29 #include "program/hash_table.h"
30
31 /**
32 * \file link_uniforms.cpp
33 * Assign locations for GLSL uniforms.
34 *
35 * \author Ian Romanick <ian.d.romanick@intel.com>
36 */
37
38 /**
39 * Count the backing storage requirements for a type
40 */
41 static unsigned
42 values_for_type(const glsl_type *type)
43 {
44 if (type->is_sampler()) {
45 return 1;
46 } else if (type->is_array() && type->fields.array->is_sampler()) {
47 return type->array_size();
48 } else {
49 return type->component_slots();
50 }
51 }
52
53 void
54 uniform_field_visitor::process(ir_variable *var)
55 {
56 const glsl_type *t = var->type;
57
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));
62 ralloc_free(name);
63 } else {
64 this->visit_field(t, var->name);
65 }
66 }
67
68 void
69 uniform_field_visitor::recursion(const glsl_type *t, char **name,
70 unsigned name_length)
71 {
72 /* Records need to have each field processed individually.
73 *
74 * Arrays of records need to have each array element processed
75 * individually, then each field of the resulting array elements processed
76 * individually.
77 */
78 if (t->is_record()) {
79 for (unsigned i = 0; i < t->length; i++) {
80 const char *field = t->fields.structure[i].name;
81
82 /* Append '.field' to the current uniform name. */
83 ralloc_asprintf_rewrite_tail(name, name_length, ".%s", field);
84
85 recursion(t->fields.structure[i].type, name,
86 name_length + 1 + strlen(field));
87 }
88 } else if (t->is_array() && t->fields.array->is_record()) {
89 for (unsigned i = 0; i < t->length; i++) {
90 char subscript[13];
91
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);
95
96 recursion(t->fields.array, name, name_length + subscript_length);
97 }
98 } else {
99 this->visit_field(t, *name);
100 }
101 }
102
103 /**
104 * Class to help calculate the storage requirements for a set of uniforms
105 *
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.
109 *
110 * If the same uniform is added multiple times (i.e., once for each shader
111 * target), it will only be accounted once.
112 */
113 class count_uniform_size : public uniform_field_visitor {
114 public:
115 count_uniform_size(struct string_to_uint_map *map)
116 : num_active_uniforms(0), num_values(0), map(map)
117 {
118 /* empty */
119 }
120
121 /**
122 * Total number of active uniforms counted
123 */
124 unsigned num_active_uniforms;
125
126 /**
127 * Number of data values required to back the storage for the active uniforms
128 */
129 unsigned num_values;
130
131 private:
132 virtual void visit_field(const glsl_type *type, const char *name)
133 {
134 assert(!type->is_record());
135 assert(!(type->is_array() && type->fields.array->is_record()));
136
137 /* If the uniform is already in the map, there's nothing more to do.
138 */
139 unsigned id;
140 if (this->map->get(id, name))
141 return;
142
143 char *key = strdup(name);
144 this->map->put(this->num_active_uniforms, key);
145
146 /* Each leaf uniform occupies one entry in the list of active
147 * uniforms.
148 */
149 this->num_active_uniforms++;
150 this->num_values += values_for_type(type);
151 }
152
153 struct string_to_uint_map *map;
154 };
155
156 /**
157 * Class to help parcel out pieces of backing storage to uniforms
158 *
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.
164 *
165 * \warning
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
169 * enough."
170 */
171 class parcel_out_uniform_storage : public uniform_field_visitor {
172 public:
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)
177 {
178 /* empty */
179 }
180
181 private:
182 virtual void visit_field(const glsl_type *type, const char *name)
183 {
184 assert(!type->is_record());
185 assert(!(type->is_array() && type->fields.array->is_record()));
186
187 unsigned id;
188 bool found = this->map->get(id, name);
189 assert(found);
190
191 if (!found)
192 return;
193
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.
198 */
199 if (this->uniforms[id].storage != NULL)
200 return;
201
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;
206 } else {
207 this->uniforms[id].array_elements = 0;
208 base_type = type;
209 }
210
211 if (base_type->is_sampler()) {
212 this->uniforms[id].sampler = this->next_sampler;
213
214 /* Increment the sampler by 1 for non-arrays and by the number of
215 * array elements for arrays.
216 */
217 this->next_sampler += MAX2(1, this->uniforms[id].array_elements);
218 } else {
219 this->uniforms[id].sampler = ~0;
220 }
221
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;
228
229 this->values += values_for_type(type);
230 }
231
232 struct string_to_uint_map *map;
233
234 struct gl_uniform_storage *uniforms;
235 unsigned next_sampler;
236
237 public:
238 union gl_constant_value *values;
239 };