svga: add new svga_state_gs.c file
[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 unsigned int boolean_true)
65 {
66 for (unsigned int i = 0; i < elements; i++) {
67 switch (base_type) {
68 case GLSL_TYPE_UINT:
69 storage[i].u = val->value.u[i];
70 break;
71 case GLSL_TYPE_INT:
72 case GLSL_TYPE_SAMPLER:
73 storage[i].i = val->value.i[i];
74 break;
75 case GLSL_TYPE_FLOAT:
76 storage[i].f = val->value.f[i];
77 break;
78 case GLSL_TYPE_DOUBLE:
79 /* XXX need to check on big-endian */
80 storage[i * 2].u = *(uint32_t *)&val->value.d[i];
81 storage[i * 2 + 1].u = *(((uint32_t *)&val->value.d[i]) + 1);
82 break;
83 case GLSL_TYPE_BOOL:
84 storage[i].b = val->value.b[i] ? boolean_true : 0;
85 break;
86 case GLSL_TYPE_ARRAY:
87 case GLSL_TYPE_STRUCT:
88 case GLSL_TYPE_IMAGE:
89 case GLSL_TYPE_ATOMIC_UINT:
90 case GLSL_TYPE_INTERFACE:
91 case GLSL_TYPE_VOID:
92 case GLSL_TYPE_SUBROUTINE:
93 case GLSL_TYPE_ERROR:
94 /* All other types should have already been filtered by other
95 * paths in the caller.
96 */
97 assert(!"Should not get here.");
98 break;
99 }
100 }
101 }
102
103 /**
104 * Initialize an opaque uniform from the value of an explicit binding
105 * qualifier specified in the shader. Atomic counters are different because
106 * they have no storage and should be handled elsewhere.
107 */
108 void
109 set_opaque_binding(gl_shader_program *prog, const char *name, int binding)
110 {
111 struct gl_uniform_storage *const storage =
112 get_storage(prog->UniformStorage, prog->NumUniformStorage, name);
113
114 if (storage == NULL) {
115 assert(storage != NULL);
116 return;
117 }
118
119 const unsigned elements = MAX2(storage->array_elements, 1);
120
121 /* Section 4.4.4 (Opaque-Uniform Layout Qualifiers) of the GLSL 4.20 spec
122 * says:
123 *
124 * "If the binding identifier is used with an array, the first element
125 * of the array takes the specified unit and each subsequent element
126 * takes the next consecutive unit."
127 */
128 for (unsigned int i = 0; i < elements; i++) {
129 storage->storage[i].i = binding + i;
130 }
131
132 for (int sh = 0; sh < MESA_SHADER_STAGES; sh++) {
133 gl_shader *shader = prog->_LinkedShaders[sh];
134
135 if (shader) {
136 if (storage->type->base_type == GLSL_TYPE_SAMPLER &&
137 storage->sampler[sh].active) {
138 for (unsigned i = 0; i < elements; i++) {
139 const unsigned index = storage->sampler[sh].index + i;
140 shader->SamplerUnits[index] = storage->storage[i].i;
141 }
142
143 } else if (storage->type->base_type == GLSL_TYPE_IMAGE &&
144 storage->image[sh].active) {
145 for (unsigned i = 0; i < elements; i++) {
146 const unsigned index = storage->image[sh].index + i;
147 shader->ImageUnits[index] = storage->storage[i].i;
148 }
149 }
150 }
151 }
152
153 storage->initialized = true;
154 }
155
156 void
157 set_block_binding(gl_shader_program *prog, const char *block_name, int binding)
158 {
159 const unsigned block_index = get_uniform_block_index(prog, block_name);
160
161 if (block_index == GL_INVALID_INDEX) {
162 assert(block_index != GL_INVALID_INDEX);
163 return;
164 }
165
166 /* This is a field of a UBO. val is the binding index. */
167 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
168 int stage_index = prog->UniformBlockStageIndex[i][block_index];
169
170 if (stage_index != -1) {
171 struct gl_shader *sh = prog->_LinkedShaders[i];
172 sh->UniformBlocks[stage_index].Binding = binding;
173 }
174 }
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 if (type->is_record()) {
183 ir_constant *field_constant;
184
185 field_constant = (ir_constant *)val->components.get_head();
186
187 for (unsigned int i = 0; i < type->length; i++) {
188 const glsl_type *field_type = type->fields.structure[i].type;
189 const char *field_name = ralloc_asprintf(mem_ctx, "%s.%s", name,
190 type->fields.structure[i].name);
191 set_uniform_initializer(mem_ctx, prog, field_name,
192 field_type, field_constant, boolean_true);
193 field_constant = (ir_constant *)field_constant->next;
194 }
195 return;
196 } else if (type->is_array() && type->fields.array->is_record()) {
197 const glsl_type *const element_type = type->fields.array;
198
199 for (unsigned int i = 0; i < type->length; i++) {
200 const char *element_name = ralloc_asprintf(mem_ctx, "%s[%d]", name, i);
201
202 set_uniform_initializer(mem_ctx, prog, element_name,
203 element_type, val->array_elements[i],
204 boolean_true);
205 }
206 return;
207 }
208
209 struct gl_uniform_storage *const storage =
210 get_storage(prog->UniformStorage,
211 prog->NumUniformStorage,
212 name);
213 if (storage == NULL) {
214 assert(storage != NULL);
215 return;
216 }
217
218 if (val->type->is_array()) {
219 const enum glsl_base_type base_type =
220 val->array_elements[0]->type->base_type;
221 const unsigned int elements = val->array_elements[0]->type->components();
222 unsigned int idx = 0;
223 unsigned dmul = (base_type == GLSL_TYPE_DOUBLE) ? 2 : 1;
224
225 assert(val->type->length >= storage->array_elements);
226 for (unsigned int i = 0; i < storage->array_elements; i++) {
227 copy_constant_to_storage(& storage->storage[idx],
228 val->array_elements[i],
229 base_type,
230 elements,
231 boolean_true);
232
233 idx += elements * dmul;
234 }
235 } else {
236 copy_constant_to_storage(storage->storage,
237 val,
238 val->type->base_type,
239 val->type->components(),
240 boolean_true);
241
242 if (storage->type->is_sampler()) {
243 for (int sh = 0; sh < MESA_SHADER_STAGES; sh++) {
244 gl_shader *shader = prog->_LinkedShaders[sh];
245
246 if (shader && storage->sampler[sh].active) {
247 unsigned index = storage->sampler[sh].index;
248
249 shader->SamplerUnits[index] = storage->storage[0].i;
250 }
251 }
252 }
253 }
254
255 storage->initialized = true;
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_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 linker::set_opaque_binding(prog, var->name, var->data.binding);
287 } else if (var->is_in_buffer_block()) {
288 const glsl_type *const iface_type = var->get_interface_type();
289
290 /* If the variable is an array and it is an interface instance,
291 * we need to set the binding for each array element. Just
292 * checking that the variable is an array is not sufficient.
293 * The variable could be an array element of a uniform block
294 * that lacks an instance name. For example:
295 *
296 * uniform U {
297 * float f[4];
298 * };
299 *
300 * In this case "f" would pass is_in_buffer_block (above) and
301 * type->is_array(), but it will fail is_interface_instance().
302 */
303 if (var->is_interface_instance() && var->type->is_array()) {
304 for (unsigned i = 0; i < var->type->length; i++) {
305 const char *name =
306 ralloc_asprintf(mem_ctx, "%s[%u]", iface_type->name, i);
307
308 /* Section 4.4.3 (Uniform Block Layout Qualifiers) of the
309 * GLSL 4.20 spec says:
310 *
311 * "If the binding identifier is used with a uniform
312 * block instanced as an array then the first element
313 * of the array takes the specified block binding and
314 * each subsequent element takes the next consecutive
315 * uniform block binding point."
316 */
317 linker::set_block_binding(prog, name,
318 var->data.binding + i);
319 }
320 } else {
321 linker::set_block_binding(prog, iface_type->name,
322 var->data.binding);
323 }
324 } else if (type->contains_atomic()) {
325 /* we don't actually need to do anything. */
326 } else {
327 assert(!"Explicit binding not on a sampler, UBO or atomic.");
328 }
329 } else if (var->constant_value) {
330 linker::set_uniform_initializer(mem_ctx, prog, var->name,
331 var->type, var->constant_value,
332 boolean_true);
333 }
334 }
335 }
336
337 ralloc_free(mem_ctx);
338 }