util/hash_set: Rework the API to know about hashing
[mesa.git] / src / glsl / link_uniform_initializers.cpp
1 /*
2 * Copyright © 2012 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
29 /* These functions are put in a "private" namespace instead of being marked
30 * static so that the unit tests can access them. See
31 * http://code.google.com/p/googletest/wiki/AdvancedGuide#Testing_Private_Code
32 */
33 namespace linker {
34
35 gl_uniform_storage *
36 get_storage(gl_uniform_storage *storage, unsigned num_storage,
37 const char *name)
38 {
39 for (unsigned int i = 0; i < num_storage; i++) {
40 if (strcmp(name, storage[i].name) == 0)
41 return &storage[i];
42 }
43
44 return NULL;
45 }
46
47 static unsigned
48 get_uniform_block_index(const gl_shader_program *shProg,
49 const char *uniformBlockName)
50 {
51 for (unsigned i = 0; i < shProg->NumUniformBlocks; i++) {
52 if (!strcmp(shProg->UniformBlocks[i].Name, uniformBlockName))
53 return i;
54 }
55
56 return GL_INVALID_INDEX;
57 }
58
59 void
60 copy_constant_to_storage(union gl_constant_value *storage,
61 const ir_constant *val,
62 const enum glsl_base_type base_type,
63 const unsigned int elements,
64 unsigned int boolean_true)
65 {
66 for (unsigned int i = 0; i < elements; i++) {
67 switch (base_type) {
68 case GLSL_TYPE_UINT:
69 storage[i].u = val->value.u[i];
70 break;
71 case GLSL_TYPE_INT:
72 case GLSL_TYPE_SAMPLER:
73 storage[i].i = val->value.i[i];
74 break;
75 case GLSL_TYPE_FLOAT:
76 storage[i].f = val->value.f[i];
77 break;
78 case GLSL_TYPE_BOOL:
79 storage[i].b = val->value.b[i] ? boolean_true : 0;
80 break;
81 case GLSL_TYPE_ARRAY:
82 case GLSL_TYPE_STRUCT:
83 case GLSL_TYPE_IMAGE:
84 case GLSL_TYPE_ATOMIC_UINT:
85 case GLSL_TYPE_INTERFACE:
86 case GLSL_TYPE_VOID:
87 case GLSL_TYPE_ERROR:
88 /* All other types should have already been filtered by other
89 * paths in the caller.
90 */
91 assert(!"Should not get here.");
92 break;
93 }
94 }
95 }
96
97 void
98 set_sampler_binding(gl_shader_program *prog, const char *name, int binding)
99 {
100 struct gl_uniform_storage *const storage =
101 get_storage(prog->UniformStorage, prog->NumUserUniformStorage, name);
102
103 if (storage == NULL) {
104 assert(storage != NULL);
105 return;
106 }
107
108 const unsigned elements = MAX2(storage->array_elements, 1);
109
110 /* Section 4.4.4 (Opaque-Uniform Layout Qualifiers) of the GLSL 4.20 spec
111 * says:
112 *
113 * "If the binding identifier is used with an array, the first element
114 * of the array takes the specified unit and each subsequent element
115 * takes the next consecutive unit."
116 */
117 for (unsigned int i = 0; i < elements; i++) {
118 storage->storage[i].i = binding + i;
119 }
120
121 for (int sh = 0; sh < MESA_SHADER_STAGES; sh++) {
122 gl_shader *shader = prog->_LinkedShaders[sh];
123
124 if (shader && storage->sampler[sh].active) {
125 for (unsigned i = 0; i < elements; i++) {
126 unsigned index = storage->sampler[sh].index + i;
127
128 shader->SamplerUnits[index] = storage->storage[i].i;
129 }
130 }
131 }
132
133 storage->initialized = true;
134 }
135
136 void
137 set_block_binding(gl_shader_program *prog, const char *block_name, int binding)
138 {
139 const unsigned block_index = get_uniform_block_index(prog, block_name);
140
141 if (block_index == GL_INVALID_INDEX) {
142 assert(block_index != GL_INVALID_INDEX);
143 return;
144 }
145
146 /* This is a field of a UBO. val is the binding index. */
147 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
148 int stage_index = prog->UniformBlockStageIndex[i][block_index];
149
150 if (stage_index != -1) {
151 struct gl_shader *sh = prog->_LinkedShaders[i];
152 sh->UniformBlocks[stage_index].Binding = binding;
153 }
154 }
155 }
156
157 void
158 set_uniform_initializer(void *mem_ctx, gl_shader_program *prog,
159 const char *name, const glsl_type *type,
160 ir_constant *val, unsigned int boolean_true)
161 {
162 if (type->is_record()) {
163 ir_constant *field_constant;
164
165 field_constant = (ir_constant *)val->components.get_head();
166
167 for (unsigned int i = 0; i < type->length; i++) {
168 const glsl_type *field_type = type->fields.structure[i].type;
169 const char *field_name = ralloc_asprintf(mem_ctx, "%s.%s", name,
170 type->fields.structure[i].name);
171 set_uniform_initializer(mem_ctx, prog, field_name,
172 field_type, field_constant, boolean_true);
173 field_constant = (ir_constant *)field_constant->next;
174 }
175 return;
176 } else if (type->is_array() && type->fields.array->is_record()) {
177 const glsl_type *const element_type = type->fields.array;
178
179 for (unsigned int i = 0; i < type->length; i++) {
180 const char *element_name = ralloc_asprintf(mem_ctx, "%s[%d]", name, i);
181
182 set_uniform_initializer(mem_ctx, prog, element_name,
183 element_type, val->array_elements[i],
184 boolean_true);
185 }
186 return;
187 }
188
189 struct gl_uniform_storage *const storage =
190 get_storage(prog->UniformStorage,
191 prog->NumUserUniformStorage,
192 name);
193 if (storage == NULL) {
194 assert(storage != NULL);
195 return;
196 }
197
198 if (val->type->is_array()) {
199 const enum glsl_base_type base_type =
200 val->array_elements[0]->type->base_type;
201 const unsigned int elements = val->array_elements[0]->type->components();
202 unsigned int idx = 0;
203
204 assert(val->type->length >= storage->array_elements);
205 for (unsigned int i = 0; i < storage->array_elements; i++) {
206 copy_constant_to_storage(& storage->storage[idx],
207 val->array_elements[i],
208 base_type,
209 elements,
210 boolean_true);
211
212 idx += elements;
213 }
214 } else {
215 copy_constant_to_storage(storage->storage,
216 val,
217 val->type->base_type,
218 val->type->components(),
219 boolean_true);
220
221 if (storage->type->is_sampler()) {
222 for (int sh = 0; sh < MESA_SHADER_STAGES; sh++) {
223 gl_shader *shader = prog->_LinkedShaders[sh];
224
225 if (shader && storage->sampler[sh].active) {
226 unsigned index = storage->sampler[sh].index;
227
228 shader->SamplerUnits[index] = storage->storage[0].i;
229 }
230 }
231 }
232 }
233
234 storage->initialized = true;
235 }
236 }
237
238 void
239 link_set_uniform_initializers(struct gl_shader_program *prog,
240 unsigned int boolean_true)
241 {
242 void *mem_ctx = NULL;
243
244 for (unsigned int i = 0; i < MESA_SHADER_STAGES; i++) {
245 struct gl_shader *shader = prog->_LinkedShaders[i];
246
247 if (shader == NULL)
248 continue;
249
250 foreach_in_list(ir_instruction, node, shader->ir) {
251 ir_variable *const var = node->as_variable();
252
253 if (!var || var->data.mode != ir_var_uniform)
254 continue;
255
256 if (!mem_ctx)
257 mem_ctx = ralloc_context(NULL);
258
259 if (var->data.explicit_binding) {
260 const glsl_type *const type = var->type;
261
262 if (type->without_array()->is_sampler()) {
263 linker::set_sampler_binding(prog, var->name, var->data.binding);
264 } else if (var->is_in_uniform_block()) {
265 const glsl_type *const iface_type = var->get_interface_type();
266
267 /* If the variable is an array and it is an interface instance,
268 * we need to set the binding for each array element. Just
269 * checking that the variable is an array is not sufficient.
270 * The variable could be an array element of a uniform block
271 * that lacks an instance name. For example:
272 *
273 * uniform U {
274 * float f[4];
275 * };
276 *
277 * In this case "f" would pass is_in_uniform_block (above) and
278 * type->is_array(), but it will fail is_interface_instance().
279 */
280 if (var->is_interface_instance() && var->type->is_array()) {
281 for (unsigned i = 0; i < var->type->length; i++) {
282 const char *name =
283 ralloc_asprintf(mem_ctx, "%s[%u]", iface_type->name, i);
284
285 /* Section 4.4.3 (Uniform Block Layout Qualifiers) of the
286 * GLSL 4.20 spec says:
287 *
288 * "If the binding identifier is used with a uniform
289 * block instanced as an array then the first element
290 * of the array takes the specified block binding and
291 * each subsequent element takes the next consecutive
292 * uniform block binding point."
293 */
294 linker::set_block_binding(prog, name,
295 var->data.binding + i);
296 }
297 } else {
298 linker::set_block_binding(prog, iface_type->name,
299 var->data.binding);
300 }
301 } else if (type->contains_atomic()) {
302 /* we don't actually need to do anything. */
303 } else {
304 assert(!"Explicit binding not on a sampler, UBO or atomic.");
305 }
306 } else if (var->constant_value) {
307 linker::set_uniform_initializer(mem_ctx, prog, var->name,
308 var->type, var->constant_value,
309 boolean_true);
310 }
311 }
312 }
313
314 ralloc_free(mem_ctx);
315 }