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