glsl: Add null check in loop_analysis.cpp
[mesa.git] / src / 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 "glsl_symbol_table.h"
29 #include "program/hash_table.h"
30
31 /* These functions are put in a "private" namespace instead of being marked
32 * static so that the unit tests can access them. See
33 * http://code.google.com/p/googletest/wiki/AdvancedGuide#Testing_Private_Code
34 */
35 namespace linker {
36
37 gl_uniform_storage *
38 get_storage(gl_uniform_storage *storage, unsigned num_storage,
39 const char *name)
40 {
41 for (unsigned int i = 0; i < num_storage; i++) {
42 if (strcmp(name, storage[i].name) == 0)
43 return &storage[i];
44 }
45
46 return NULL;
47 }
48
49 static unsigned
50 get_uniform_block_index(const gl_shader_program *shProg,
51 const char *uniformBlockName)
52 {
53 for (unsigned i = 0; i < shProg->NumUniformBlocks; i++) {
54 if (!strcmp(shProg->UniformBlocks[i].Name, uniformBlockName))
55 return i;
56 }
57
58 return GL_INVALID_INDEX;
59 }
60
61 void
62 copy_constant_to_storage(union gl_constant_value *storage,
63 const ir_constant *val,
64 const enum glsl_base_type base_type,
65 const unsigned int elements)
66 {
67 for (unsigned int i = 0; i < elements; i++) {
68 switch (base_type) {
69 case GLSL_TYPE_UINT:
70 storage[i].u = val->value.u[i];
71 break;
72 case GLSL_TYPE_INT:
73 case GLSL_TYPE_SAMPLER:
74 storage[i].i = val->value.i[i];
75 break;
76 case GLSL_TYPE_FLOAT:
77 storage[i].f = val->value.f[i];
78 break;
79 case GLSL_TYPE_BOOL:
80 storage[i].b = int(val->value.b[i]);
81 break;
82 case GLSL_TYPE_ARRAY:
83 case GLSL_TYPE_STRUCT:
84 case GLSL_TYPE_IMAGE:
85 case GLSL_TYPE_ATOMIC_UINT:
86 case GLSL_TYPE_INTERFACE:
87 case GLSL_TYPE_VOID:
88 case GLSL_TYPE_ERROR:
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 void
99 set_sampler_binding(gl_shader_program *prog, const char *name, int binding)
100 {
101 struct gl_uniform_storage *const storage =
102 get_storage(prog->UniformStorage, prog->NumUserUniformStorage, name);
103
104 if (storage == NULL) {
105 assert(storage != NULL);
106 return;
107 }
108
109 const unsigned elements = MAX2(storage->array_elements, 1);
110
111 /* Section 4.4.4 (Opaque-Uniform Layout Qualifiers) of the GLSL 4.20 spec
112 * says:
113 *
114 * "If the binding identifier is used with an array, the first element
115 * of the array takes the specified unit and each subsequent element
116 * takes the next consecutive unit."
117 */
118 for (unsigned int i = 0; i < elements; i++) {
119 storage->storage[i].i = binding + i;
120 }
121
122 for (int sh = 0; sh < MESA_SHADER_STAGES; sh++) {
123 gl_shader *shader = prog->_LinkedShaders[sh];
124
125 if (shader && storage->sampler[sh].active) {
126 for (unsigned i = 0; i < elements; i++) {
127 unsigned index = storage->sampler[sh].index + i;
128
129 shader->SamplerUnits[index] = storage->storage[i].i;
130 }
131 }
132 }
133
134 storage->initialized = true;
135 }
136
137 void
138 set_block_binding(gl_shader_program *prog, const char *block_name, int binding)
139 {
140 const unsigned block_index = get_uniform_block_index(prog, block_name);
141
142 if (block_index == GL_INVALID_INDEX) {
143 assert(block_index != GL_INVALID_INDEX);
144 return;
145 }
146
147 /* This is a field of a UBO. val is the binding index. */
148 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
149 int stage_index = prog->UniformBlockStageIndex[i][block_index];
150
151 if (stage_index != -1) {
152 struct gl_shader *sh = prog->_LinkedShaders[i];
153 sh->UniformBlocks[stage_index].Binding = binding;
154 }
155 }
156 }
157
158 void
159 set_uniform_initializer(void *mem_ctx, gl_shader_program *prog,
160 const char *name, const glsl_type *type,
161 ir_constant *val)
162 {
163 if (type->is_record()) {
164 ir_constant *field_constant;
165
166 field_constant = (ir_constant *)val->components.get_head();
167
168 for (unsigned int i = 0; i < type->length; i++) {
169 const glsl_type *field_type = type->fields.structure[i].type;
170 const char *field_name = ralloc_asprintf(mem_ctx, "%s.%s", name,
171 type->fields.structure[i].name);
172 set_uniform_initializer(mem_ctx, prog, field_name,
173 field_type, field_constant);
174 field_constant = (ir_constant *)field_constant->next;
175 }
176 return;
177 } else if (type->is_array() && type->fields.array->is_record()) {
178 const glsl_type *const element_type = type->fields.array;
179
180 for (unsigned int i = 0; i < type->length; i++) {
181 const char *element_name = ralloc_asprintf(mem_ctx, "%s[%d]", name, i);
182
183 set_uniform_initializer(mem_ctx, prog, element_name,
184 element_type, val->array_elements[i]);
185 }
186 return;
187 }
188
189 struct gl_uniform_storage *const storage =
190 get_storage(prog->UniformStorage,
191 prog->NumUserUniformStorage,
192 name);
193 if (storage == NULL) {
194 assert(storage != NULL);
195 return;
196 }
197
198 if (val->type->is_array()) {
199 const enum glsl_base_type base_type =
200 val->array_elements[0]->type->base_type;
201 const unsigned int elements = val->array_elements[0]->type->components();
202 unsigned int idx = 0;
203
204 assert(val->type->length >= storage->array_elements);
205 for (unsigned int i = 0; i < storage->array_elements; i++) {
206 copy_constant_to_storage(& storage->storage[idx],
207 val->array_elements[i],
208 base_type,
209 elements);
210
211 idx += elements;
212 }
213 } else {
214 copy_constant_to_storage(storage->storage,
215 val,
216 val->type->base_type,
217 val->type->components());
218
219 if (storage->type->is_sampler()) {
220 for (int sh = 0; sh < MESA_SHADER_STAGES; sh++) {
221 gl_shader *shader = prog->_LinkedShaders[sh];
222
223 if (shader && storage->sampler[sh].active) {
224 unsigned index = storage->sampler[sh].index;
225
226 shader->SamplerUnits[index] = storage->storage[0].i;
227 }
228 }
229 }
230 }
231
232 storage->initialized = true;
233 }
234 }
235
236 void
237 link_set_uniform_initializers(struct gl_shader_program *prog)
238 {
239 void *mem_ctx = NULL;
240
241 for (unsigned int i = 0; i < MESA_SHADER_STAGES; i++) {
242 struct gl_shader *shader = prog->_LinkedShaders[i];
243
244 if (shader == NULL)
245 continue;
246
247 foreach_list(node, shader->ir) {
248 ir_variable *const var = ((ir_instruction *) node)->as_variable();
249
250 if (!var || var->data.mode != ir_var_uniform)
251 continue;
252
253 if (!mem_ctx)
254 mem_ctx = ralloc_context(NULL);
255
256 if (var->data.explicit_binding) {
257 const glsl_type *const type = var->type;
258
259 if (type->is_sampler()
260 || (type->is_array() && type->fields.array->is_sampler())) {
261 linker::set_sampler_binding(prog, var->name, var->data.binding);
262 } else if (var->is_in_uniform_block()) {
263 const glsl_type *const iface_type = var->get_interface_type();
264
265 /* If the variable is an array and it is an interface instance,
266 * we need to set the binding for each array element. Just
267 * checking that the variable is an array is not sufficient.
268 * The variable could be an array element of a uniform block
269 * that lacks an instance name. For example:
270 *
271 * uniform U {
272 * float f[4];
273 * };
274 *
275 * In this case "f" would pass is_in_uniform_block (above) and
276 * type->is_array(), but it will fail is_interface_instance().
277 */
278 if (var->is_interface_instance() && var->type->is_array()) {
279 for (unsigned i = 0; i < var->type->length; i++) {
280 const char *name =
281 ralloc_asprintf(mem_ctx, "%s[%u]", iface_type->name, i);
282
283 /* Section 4.4.3 (Uniform Block Layout Qualifiers) of the
284 * GLSL 4.20 spec says:
285 *
286 * "If the binding identifier is used with a uniform
287 * block instanced as an array then the first element
288 * of the array takes the specified block binding and
289 * each subsequent element takes the next consecutive
290 * uniform block binding point."
291 */
292 linker::set_block_binding(prog, name,
293 var->data.binding + i);
294 }
295 } else {
296 linker::set_block_binding(prog, iface_type->name,
297 var->data.binding);
298 }
299 } else if (type->contains_atomic()) {
300 /* we don't actually need to do anything. */
301 } else {
302 assert(!"Explicit binding not on a sampler, UBO or atomic.");
303 }
304 } else if (var->constant_value) {
305 linker::set_uniform_initializer(mem_ctx, prog, var->name,
306 var->type, var->constant_value);
307 }
308 }
309 }
310
311 ralloc_free(mem_ctx);
312 }