Added few more stubs so that control reaches to DestroyDevice().
[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 "ir.h"
25 #include "linker.h"
26 #include "ir_uniform.h"
27 #include "string_to_uint_map.h"
28 #include "main/mtypes.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 static 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 case GLSL_TYPE_UINT64:
68 case GLSL_TYPE_INT64:
69 /* XXX need to check on big-endian */
70 memcpy(&storage[i * 2].u, &val->value.d[i], sizeof(double));
71 break;
72 case GLSL_TYPE_BOOL:
73 storage[i].b = val->value.b[i] ? boolean_true : 0;
74 break;
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 case GLSL_TYPE_UINT16:
85 case GLSL_TYPE_INT16:
86 case GLSL_TYPE_UINT8:
87 case GLSL_TYPE_INT8:
88 case GLSL_TYPE_FLOAT16:
89 /* All other types should have already been filtered by other
90 * paths in the caller.
91 */
92 assert(!"Should not get here.");
93 break;
94 }
95 }
96 }
97
98 /**
99 * Initialize an opaque uniform from the value of an explicit binding
100 * qualifier specified in the shader. Atomic counters are different because
101 * they have no storage and should be handled elsewhere.
102 */
103 static void
104 set_opaque_binding(void *mem_ctx, gl_shader_program *prog,
105 const ir_variable *var, const glsl_type *type,
106 const char *name, int *binding)
107 {
108
109 if (type->is_array() && type->fields.array->is_array()) {
110 const glsl_type *const element_type = type->fields.array;
111
112 for (unsigned int i = 0; i < type->length; i++) {
113 const char *element_name = ralloc_asprintf(mem_ctx, "%s[%d]", name, i);
114
115 set_opaque_binding(mem_ctx, prog, var, element_type,
116 element_name, binding);
117 }
118 } else {
119 struct gl_uniform_storage *const storage = get_storage(prog, name);
120
121 if (!storage)
122 return;
123
124 const unsigned elements = MAX2(storage->array_elements, 1);
125
126 /* Section 4.4.6 (Opaque-Uniform Layout Qualifiers) of the GLSL 4.50 spec
127 * says:
128 *
129 * "If the binding identifier is used with an array, the first element
130 * of the array takes the specified unit and each subsequent element
131 * takes the next consecutive unit."
132 */
133 for (unsigned int i = 0; i < elements; i++) {
134 storage->storage[i].i = (*binding)++;
135 }
136
137 for (int sh = 0; sh < MESA_SHADER_STAGES; sh++) {
138 gl_linked_shader *shader = prog->_LinkedShaders[sh];
139
140 if (!shader)
141 continue;
142 if (!storage->opaque[sh].active)
143 continue;
144
145 if (storage->type->is_sampler()) {
146 for (unsigned i = 0; i < elements; i++) {
147 const unsigned index = storage->opaque[sh].index + i;
148
149 if (var->data.bindless) {
150 if (index >= shader->Program->sh.NumBindlessSamplers)
151 break;
152 shader->Program->sh.BindlessSamplers[index].unit =
153 storage->storage[i].i;
154 shader->Program->sh.BindlessSamplers[index].bound = true;
155 shader->Program->sh.HasBoundBindlessSampler = true;
156 } else {
157 if (index >= ARRAY_SIZE(shader->Program->SamplerUnits))
158 break;
159 shader->Program->SamplerUnits[index] =
160 storage->storage[i].i;
161 }
162 }
163 } else if (storage->type->is_image()) {
164 for (unsigned i = 0; i < elements; i++) {
165 const unsigned index = storage->opaque[sh].index + i;
166
167
168 if (var->data.bindless) {
169 if (index >= shader->Program->sh.NumBindlessImages)
170 break;
171 shader->Program->sh.BindlessImages[index].unit =
172 storage->storage[i].i;
173 shader->Program->sh.BindlessImages[index].bound = true;
174 shader->Program->sh.HasBoundBindlessImage = true;
175 } else {
176 if (index >= ARRAY_SIZE(shader->Program->sh.ImageUnits))
177 break;
178 shader->Program->sh.ImageUnits[index] =
179 storage->storage[i].i;
180 }
181 }
182 }
183 }
184 }
185 }
186
187 void
188 set_uniform_initializer(void *mem_ctx, gl_shader_program *prog,
189 const char *name, const glsl_type *type,
190 ir_constant *val, unsigned int boolean_true)
191 {
192 const glsl_type *t_without_array = type->without_array();
193 if (type->is_struct()) {
194 for (unsigned int i = 0; i < type->length; i++) {
195 const glsl_type *field_type = type->fields.structure[i].type;
196 const char *field_name = ralloc_asprintf(mem_ctx, "%s.%s", name,
197 type->fields.structure[i].name);
198 set_uniform_initializer(mem_ctx, prog, field_name,
199 field_type, val->get_record_field(i),
200 boolean_true);
201 }
202 return;
203 } else if (t_without_array->is_struct() ||
204 (type->is_array() && type->fields.array->is_array())) {
205 const glsl_type *const element_type = type->fields.array;
206
207 for (unsigned int i = 0; i < type->length; i++) {
208 const char *element_name = ralloc_asprintf(mem_ctx, "%s[%d]", name, i);
209
210 set_uniform_initializer(mem_ctx, prog, element_name,
211 element_type, val->const_elements[i],
212 boolean_true);
213 }
214 return;
215 }
216
217 struct gl_uniform_storage *const storage = get_storage(prog, name);
218
219 if (!storage)
220 return;
221
222 if (val->type->is_array()) {
223 const enum glsl_base_type base_type =
224 val->const_elements[0]->type->base_type;
225 const unsigned int elements = val->const_elements[0]->type->components();
226 unsigned int idx = 0;
227 unsigned dmul = glsl_base_type_is_64bit(base_type) ? 2 : 1;
228
229 assert(val->type->length >= storage->array_elements);
230 for (unsigned int i = 0; i < storage->array_elements; i++) {
231 copy_constant_to_storage(& storage->storage[idx],
232 val->const_elements[i],
233 base_type,
234 elements,
235 boolean_true);
236
237 idx += elements * dmul;
238 }
239 } else {
240 copy_constant_to_storage(storage->storage,
241 val,
242 val->type->base_type,
243 val->type->components(),
244 boolean_true);
245
246 if (storage->type->is_sampler()) {
247 for (int sh = 0; sh < MESA_SHADER_STAGES; sh++) {
248 gl_linked_shader *shader = prog->_LinkedShaders[sh];
249
250 if (shader && storage->opaque[sh].active) {
251 unsigned index = storage->opaque[sh].index;
252
253 shader->Program->SamplerUnits[index] = storage->storage[0].i;
254 }
255 }
256 }
257 }
258 }
259 }
260
261 void
262 link_set_uniform_initializers(struct gl_shader_program *prog,
263 unsigned int boolean_true)
264 {
265 void *mem_ctx = NULL;
266
267 for (unsigned int i = 0; i < MESA_SHADER_STAGES; i++) {
268 struct gl_linked_shader *shader = prog->_LinkedShaders[i];
269
270 if (shader == NULL)
271 continue;
272
273 foreach_in_list(ir_instruction, node, shader->ir) {
274 ir_variable *const var = node->as_variable();
275
276 if (!var || (var->data.mode != ir_var_uniform &&
277 var->data.mode != ir_var_shader_storage))
278 continue;
279
280 if (!mem_ctx)
281 mem_ctx = ralloc_context(NULL);
282
283 if (var->data.explicit_binding) {
284 const glsl_type *const type = var->type;
285
286 if (var->is_in_buffer_block()) {
287 /* This case is handled by link_uniform_blocks (at
288 * process_block_array_leaf)
289 */
290 } else if (type->without_array()->is_sampler() ||
291 type->without_array()->is_image()) {
292 int binding = var->data.binding;
293 linker::set_opaque_binding(mem_ctx, prog, var, var->type,
294 var->name, &binding);
295 } else if (type->contains_atomic()) {
296 /* we don't actually need to do anything. */
297 } else {
298 assert(!"Explicit binding not on a sampler, UBO or atomic.");
299 }
300 } else if (var->constant_initializer) {
301 linker::set_uniform_initializer(mem_ctx, prog, var->name,
302 var->type, var->constant_initializer,
303 boolean_true);
304 }
305 }
306 }
307
308 memcpy(prog->data->UniformDataDefaults, prog->data->UniformDataSlots,
309 sizeof(union gl_constant_value) * prog->data->NumUniformDataSlots);
310 ralloc_free(mem_ctx);
311 }