Merge remote-tracking branch 'mesa-public/master' into vulkan
[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_DOUBLE:
79 /* XXX need to check on big-endian */
80 storage[i * 2].u = *(uint32_t *)&val->value.d[i];
81 storage[i * 2 + 1].u = *(((uint32_t *)&val->value.d[i]) + 1);
82 break;
83 case GLSL_TYPE_BOOL:
84 storage[i].b = val->value.b[i] ? boolean_true : 0;
85 break;
86 case GLSL_TYPE_ARRAY:
87 case GLSL_TYPE_STRUCT:
88 case GLSL_TYPE_IMAGE:
89 case GLSL_TYPE_ATOMIC_UINT:
90 case GLSL_TYPE_INTERFACE:
91 case GLSL_TYPE_FUNCTION:
92 case GLSL_TYPE_VOID:
93 case GLSL_TYPE_ERROR:
94 /* All other types should have already been filtered by other
95 * paths in the caller.
96 */
97 assert(!"Should not get here.");
98 break;
99 }
100 }
101 }
102
103 void
104 set_sampler_binding(gl_shader_program *prog, const char *name, int binding)
105 {
106 struct gl_uniform_storage *const storage =
107 get_storage(prog->UniformStorage, prog->NumUniformStorage, name);
108
109 if (storage == NULL) {
110 assert(storage != NULL);
111 return;
112 }
113
114 const unsigned elements = MAX2(storage->array_elements, 1);
115
116 /* Section 4.4.4 (Opaque-Uniform Layout Qualifiers) of the GLSL 4.20 spec
117 * says:
118 *
119 * "If the binding identifier is used with an array, the first element
120 * of the array takes the specified unit and each subsequent element
121 * takes the next consecutive unit."
122 */
123 for (unsigned int i = 0; i < elements; i++) {
124 storage->storage[i].i = binding + i;
125 }
126
127 for (int sh = 0; sh < MESA_SHADER_STAGES; sh++) {
128 gl_shader *shader = prog->_LinkedShaders[sh];
129
130 if (shader && storage->sampler[sh].active) {
131 for (unsigned i = 0; i < elements; i++) {
132 unsigned index = storage->sampler[sh].index + i;
133
134 shader->SamplerUnits[index] = storage->storage[i].i;
135 }
136 }
137 }
138
139 storage->initialized = true;
140 }
141
142 void
143 set_block_binding(gl_shader_program *prog, const char *block_name, int binding)
144 {
145 const unsigned block_index = get_uniform_block_index(prog, block_name);
146
147 if (block_index == GL_INVALID_INDEX) {
148 assert(block_index != GL_INVALID_INDEX);
149 return;
150 }
151
152 /* This is a field of a UBO. val is the binding index. */
153 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
154 int stage_index = prog->UniformBlockStageIndex[i][block_index];
155
156 if (stage_index != -1) {
157 struct gl_shader *sh = prog->_LinkedShaders[i];
158 sh->UniformBlocks[stage_index].Binding = binding;
159 }
160 }
161 }
162
163 void
164 set_uniform_initializer(void *mem_ctx, gl_shader_program *prog,
165 const char *name, const glsl_type *type,
166 ir_constant *val, unsigned int boolean_true)
167 {
168 if (type->is_record()) {
169 ir_constant *field_constant;
170
171 field_constant = (ir_constant *)val->components.get_head();
172
173 for (unsigned int i = 0; i < type->length; i++) {
174 const glsl_type *field_type = type->fields.structure[i].type;
175 const char *field_name = ralloc_asprintf(mem_ctx, "%s.%s", name,
176 type->fields.structure[i].name);
177 set_uniform_initializer(mem_ctx, prog, field_name,
178 field_type, field_constant, boolean_true);
179 field_constant = (ir_constant *)field_constant->next;
180 }
181 return;
182 } else if (type->is_array() && type->fields.array->is_record()) {
183 const glsl_type *const element_type = type->fields.array;
184
185 for (unsigned int i = 0; i < type->length; i++) {
186 const char *element_name = ralloc_asprintf(mem_ctx, "%s[%d]", name, i);
187
188 set_uniform_initializer(mem_ctx, prog, element_name,
189 element_type, val->array_elements[i],
190 boolean_true);
191 }
192 return;
193 }
194
195 struct gl_uniform_storage *const storage =
196 get_storage(prog->UniformStorage,
197 prog->NumUniformStorage,
198 name);
199 if (storage == NULL) {
200 assert(storage != NULL);
201 return;
202 }
203
204 if (val->type->is_array()) {
205 const enum glsl_base_type base_type =
206 val->array_elements[0]->type->base_type;
207 const unsigned int elements = val->array_elements[0]->type->components();
208 unsigned int idx = 0;
209 unsigned dmul = (base_type == GLSL_TYPE_DOUBLE) ? 2 : 1;
210
211 assert(val->type->length >= storage->array_elements);
212 for (unsigned int i = 0; i < storage->array_elements; i++) {
213 copy_constant_to_storage(& storage->storage[idx],
214 val->array_elements[i],
215 base_type,
216 elements,
217 boolean_true);
218
219 idx += elements * dmul;
220 }
221 } else {
222 copy_constant_to_storage(storage->storage,
223 val,
224 val->type->base_type,
225 val->type->components(),
226 boolean_true);
227
228 if (storage->type->is_sampler()) {
229 for (int sh = 0; sh < MESA_SHADER_STAGES; sh++) {
230 gl_shader *shader = prog->_LinkedShaders[sh];
231
232 if (shader && storage->sampler[sh].active) {
233 unsigned index = storage->sampler[sh].index;
234
235 shader->SamplerUnits[index] = storage->storage[0].i;
236 }
237 }
238 }
239 }
240
241 storage->initialized = true;
242 }
243 }
244
245 void
246 link_set_uniform_initializers(struct gl_shader_program *prog,
247 unsigned int boolean_true)
248 {
249 void *mem_ctx = NULL;
250
251 for (unsigned int i = 0; i < MESA_SHADER_STAGES; i++) {
252 struct gl_shader *shader = prog->_LinkedShaders[i];
253
254 if (shader == NULL)
255 continue;
256
257 foreach_in_list(ir_instruction, node, shader->ir) {
258 ir_variable *const var = node->as_variable();
259
260 if (!var || var->data.mode != ir_var_uniform)
261 continue;
262
263 if (!mem_ctx)
264 mem_ctx = ralloc_context(NULL);
265
266 if (var->data.explicit_binding) {
267 const glsl_type *const type = var->type;
268
269 if (type->without_array()->is_sampler()) {
270 linker::set_sampler_binding(prog, var->name, var->data.binding);
271 } else if (var->is_in_uniform_block()) {
272 const glsl_type *const iface_type = var->get_interface_type();
273
274 /* If the variable is an array and it is an interface instance,
275 * we need to set the binding for each array element. Just
276 * checking that the variable is an array is not sufficient.
277 * The variable could be an array element of a uniform block
278 * that lacks an instance name. For example:
279 *
280 * uniform U {
281 * float f[4];
282 * };
283 *
284 * In this case "f" would pass is_in_uniform_block (above) and
285 * type->is_array(), but it will fail is_interface_instance().
286 */
287 if (var->is_interface_instance() && var->type->is_array()) {
288 for (unsigned i = 0; i < var->type->length; i++) {
289 const char *name =
290 ralloc_asprintf(mem_ctx, "%s[%u]", iface_type->name, i);
291
292 /* Section 4.4.3 (Uniform Block Layout Qualifiers) of the
293 * GLSL 4.20 spec says:
294 *
295 * "If the binding identifier is used with a uniform
296 * block instanced as an array then the first element
297 * of the array takes the specified block binding and
298 * each subsequent element takes the next consecutive
299 * uniform block binding point."
300 */
301 linker::set_block_binding(prog, name,
302 var->data.binding + i);
303 }
304 } else {
305 linker::set_block_binding(prog, iface_type->name,
306 var->data.binding);
307 }
308 } else if (type->contains_atomic()) {
309 /* we don't actually need to do anything. */
310 } else {
311 assert(!"Explicit binding not on a sampler, UBO or atomic.");
312 }
313 } else if (var->constant_value) {
314 linker::set_uniform_initializer(mem_ctx, prog, var->name,
315 var->type, var->constant_value,
316 boolean_true);
317 }
318 }
319 }
320
321 ralloc_free(mem_ctx);
322 }