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