Use line number information from entire function expression
[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 void
50 copy_constant_to_storage(union gl_constant_value *storage,
51 const ir_constant *val,
52 const enum glsl_base_type base_type,
53 const unsigned int elements)
54 {
55 for (unsigned int i = 0; i < elements; i++) {
56 switch (base_type) {
57 case GLSL_TYPE_UINT:
58 storage[i].u = val->value.u[i];
59 break;
60 case GLSL_TYPE_INT:
61 case GLSL_TYPE_SAMPLER:
62 storage[i].i = val->value.i[i];
63 break;
64 case GLSL_TYPE_FLOAT:
65 storage[i].f = val->value.f[i];
66 break;
67 case GLSL_TYPE_BOOL:
68 storage[i].b = int(val->value.b[i]);
69 break;
70 case GLSL_TYPE_ARRAY:
71 case GLSL_TYPE_STRUCT:
72 case GLSL_TYPE_ATOMIC_UINT:
73 case GLSL_TYPE_INTERFACE:
74 case GLSL_TYPE_VOID:
75 case GLSL_TYPE_ERROR:
76 /* All other types should have already been filtered by other
77 * paths in the caller.
78 */
79 assert(!"Should not get here.");
80 break;
81 }
82 }
83 }
84
85 void
86 set_uniform_binding(void *mem_ctx, gl_shader_program *prog,
87 const char *name, const glsl_type *type, int binding)
88 {
89 struct gl_uniform_storage *const storage =
90 get_storage(prog->UniformStorage, prog->NumUserUniformStorage, name);
91
92 if (storage == NULL) {
93 assert(storage != NULL);
94 return;
95 }
96
97 if (storage->type->is_sampler()) {
98 unsigned elements = MAX2(storage->array_elements, 1);
99
100 /* From section 4.4.4 of the GLSL 4.20 specification:
101 * "If the binding identifier is used with an array, the first element
102 * of the array takes the specified unit and each subsequent element
103 * takes the next consecutive unit."
104 */
105 for (unsigned int i = 0; i < elements; i++) {
106 storage->storage[i].i = binding + i;
107 }
108
109 for (int sh = 0; sh < MESA_SHADER_TYPES; sh++) {
110 gl_shader *shader = prog->_LinkedShaders[sh];
111
112 if (shader && storage->sampler[sh].active) {
113 for (unsigned i = 0; i < elements; i++) {
114 unsigned index = storage->sampler[sh].index + i;
115
116 shader->SamplerUnits[index] = storage->storage[i].i;
117 }
118 }
119 }
120 } else if (storage->block_index != -1) {
121 /* This is a field of a UBO. val is the binding index. */
122 for (int i = 0; i < MESA_SHADER_TYPES; i++) {
123 int stage_index = prog->UniformBlockStageIndex[i][storage->block_index];
124
125 if (stage_index != -1) {
126 struct gl_shader *sh = prog->_LinkedShaders[i];
127 sh->UniformBlocks[stage_index].Binding = binding;
128 }
129 }
130 }
131
132 storage->initialized = true;
133 }
134
135 void
136 set_uniform_initializer(void *mem_ctx, gl_shader_program *prog,
137 const char *name, const glsl_type *type,
138 ir_constant *val)
139 {
140 if (type->is_record()) {
141 ir_constant *field_constant;
142
143 field_constant = (ir_constant *)val->components.get_head();
144
145 for (unsigned int i = 0; i < type->length; i++) {
146 const glsl_type *field_type = type->fields.structure[i].type;
147 const char *field_name = ralloc_asprintf(mem_ctx, "%s.%s", name,
148 type->fields.structure[i].name);
149 set_uniform_initializer(mem_ctx, prog, field_name,
150 field_type, field_constant);
151 field_constant = (ir_constant *)field_constant->next;
152 }
153 return;
154 } else if (type->is_array() && type->fields.array->is_record()) {
155 const glsl_type *const element_type = type->fields.array;
156
157 for (unsigned int i = 0; i < type->length; i++) {
158 const char *element_name = ralloc_asprintf(mem_ctx, "%s[%d]", name, i);
159
160 set_uniform_initializer(mem_ctx, prog, element_name,
161 element_type, val->array_elements[i]);
162 }
163 return;
164 }
165
166 struct gl_uniform_storage *const storage =
167 get_storage(prog->UniformStorage,
168 prog->NumUserUniformStorage,
169 name);
170 if (storage == NULL) {
171 assert(storage != NULL);
172 return;
173 }
174
175 if (val->type->is_array()) {
176 const enum glsl_base_type base_type =
177 val->array_elements[0]->type->base_type;
178 const unsigned int elements = val->array_elements[0]->type->components();
179 unsigned int idx = 0;
180
181 assert(val->type->length >= storage->array_elements);
182 for (unsigned int i = 0; i < storage->array_elements; i++) {
183 copy_constant_to_storage(& storage->storage[idx],
184 val->array_elements[i],
185 base_type,
186 elements);
187
188 idx += elements;
189 }
190 } else {
191 copy_constant_to_storage(storage->storage,
192 val,
193 val->type->base_type,
194 val->type->components());
195
196 if (storage->type->is_sampler()) {
197 for (int sh = 0; sh < MESA_SHADER_TYPES; sh++) {
198 gl_shader *shader = prog->_LinkedShaders[sh];
199
200 if (shader && storage->sampler[sh].active) {
201 unsigned index = storage->sampler[sh].index;
202
203 shader->SamplerUnits[index] = storage->storage[0].i;
204 }
205 }
206 }
207 }
208
209 storage->initialized = true;
210 }
211 }
212
213 void
214 link_set_uniform_initializers(struct gl_shader_program *prog)
215 {
216 void *mem_ctx = NULL;
217
218 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
219 struct gl_shader *shader = prog->_LinkedShaders[i];
220
221 if (shader == NULL)
222 continue;
223
224 foreach_list(node, shader->ir) {
225 ir_variable *const var = ((ir_instruction *) node)->as_variable();
226
227 if (!var || var->data.mode != ir_var_uniform)
228 continue;
229
230 if (!mem_ctx)
231 mem_ctx = ralloc_context(NULL);
232
233 if (var->data.explicit_binding) {
234 linker::set_uniform_binding(mem_ctx, prog, var->name,
235 var->type, var->data.binding);
236 } else if (var->constant_value) {
237 linker::set_uniform_initializer(mem_ctx, prog, var->name,
238 var->type, var->constant_value);
239 }
240 }
241 }
242
243 ralloc_free(mem_ctx);
244 }