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