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