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