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