glsl: Modify uniform_field_visitor::recursion to take a row_major parameter
[mesa.git] / src / glsl / link_uniforms.cpp
1 /*
2 * Copyright © 2011 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 #include "program.h"
31
32 static inline unsigned int
33 align(unsigned int a, unsigned int align)
34 {
35 return (a + align - 1) / align * align;
36 }
37
38 /**
39 * \file link_uniforms.cpp
40 * Assign locations for GLSL uniforms.
41 *
42 * \author Ian Romanick <ian.d.romanick@intel.com>
43 */
44
45 /**
46 * Count the backing storage requirements for a type
47 */
48 static unsigned
49 values_for_type(const glsl_type *type)
50 {
51 if (type->is_sampler()) {
52 return 1;
53 } else if (type->is_array() && type->fields.array->is_sampler()) {
54 return type->array_size();
55 } else {
56 return type->component_slots();
57 }
58 }
59
60 void
61 uniform_field_visitor::process(ir_variable *var)
62 {
63 const glsl_type *t = var->type;
64
65 /* false is always passed for the row_major parameter to the other
66 * processing functions because no information is available to do
67 * otherwise. See the warning in linker.h.
68 */
69
70 /* Only strdup the name if we actually will need to modify it. */
71 if (t->is_record() || (t->is_array() && t->fields.array->is_record())) {
72 char *name = ralloc_strdup(NULL, var->name);
73 recursion(var->type, &name, strlen(name), false);
74 ralloc_free(name);
75 } else {
76 this->visit_field(t, var->name);
77 }
78 }
79
80 void
81 uniform_field_visitor::recursion(const glsl_type *t, char **name,
82 size_t name_length, bool row_major)
83 {
84 /* Records need to have each field processed individually.
85 *
86 * Arrays of records need to have each array element processed
87 * individually, then each field of the resulting array elements processed
88 * individually.
89 */
90 if (t->is_record()) {
91 for (unsigned i = 0; i < t->length; i++) {
92 const char *field = t->fields.structure[i].name;
93 size_t new_length = name_length;
94
95 /* Append '.field' to the current uniform name. */
96 ralloc_asprintf_rewrite_tail(name, &new_length, ".%s", field);
97
98 recursion(t->fields.structure[i].type, name, new_length,
99 t->fields.structure[i].row_major);
100 }
101 } else if (t->is_array() && t->fields.array->is_record()) {
102 for (unsigned i = 0; i < t->length; i++) {
103 size_t new_length = name_length;
104
105 /* Append the subscript to the current uniform name */
106 ralloc_asprintf_rewrite_tail(name, &new_length, "[%u]", i);
107
108 recursion(t->fields.array, name, new_length,
109 t->fields.structure[i].row_major);
110 }
111 } else {
112 this->visit_field(t, *name);
113 }
114 }
115
116 /**
117 * Class to help calculate the storage requirements for a set of uniforms
118 *
119 * As uniforms are added to the active set the number of active uniforms and
120 * the storage requirements for those uniforms are accumulated. The active
121 * uniforms are added the the hash table supplied to the constructor.
122 *
123 * If the same uniform is added multiple times (i.e., once for each shader
124 * target), it will only be accounted once.
125 */
126 class count_uniform_size : public uniform_field_visitor {
127 public:
128 count_uniform_size(struct string_to_uint_map *map)
129 : num_active_uniforms(0), num_values(0), num_shader_samplers(0),
130 num_shader_uniform_components(0), map(map)
131 {
132 /* empty */
133 }
134
135 void start_shader()
136 {
137 this->num_shader_samplers = 0;
138 this->num_shader_uniform_components = 0;
139 }
140
141 /**
142 * Total number of active uniforms counted
143 */
144 unsigned num_active_uniforms;
145
146 /**
147 * Number of data values required to back the storage for the active uniforms
148 */
149 unsigned num_values;
150
151 /**
152 * Number of samplers used
153 */
154 unsigned num_shader_samplers;
155
156 /**
157 * Number of uniforms used in the current shader
158 */
159 unsigned num_shader_uniform_components;
160
161 private:
162 virtual void visit_field(const glsl_type *type, const char *name)
163 {
164 assert(!type->is_record());
165 assert(!(type->is_array() && type->fields.array->is_record()));
166
167 /* Count the number of samplers regardless of whether the uniform is
168 * already in the hash table. The hash table prevents adding the same
169 * uniform for multiple shader targets, but in this case we want to
170 * count it for each shader target.
171 */
172 const unsigned values = values_for_type(type);
173 if (type->contains_sampler()) {
174 this->num_shader_samplers +=
175 type->is_array() ? type->array_size() : 1;
176 } else {
177 /* Accumulate the total number of uniform slots used by this shader.
178 * Note that samplers do not count against this limit because they
179 * don't use any storage on current hardware.
180 */
181 this->num_shader_uniform_components += values;
182 }
183
184 /* If the uniform is already in the map, there's nothing more to do.
185 */
186 unsigned id;
187 if (this->map->get(id, name))
188 return;
189
190 this->map->put(this->num_active_uniforms, name);
191
192 /* Each leaf uniform occupies one entry in the list of active
193 * uniforms.
194 */
195 this->num_active_uniforms++;
196 this->num_values += values;
197 }
198
199 struct string_to_uint_map *map;
200 };
201
202 /**
203 * Class to help parcel out pieces of backing storage to uniforms
204 *
205 * Each uniform processed has some range of the \c gl_constant_value
206 * structures associated with it. The association is done by finding
207 * the uniform in the \c string_to_uint_map and using the value from
208 * the map to connect that slot in the \c gl_uniform_storage table
209 * with the next available slot in the \c gl_constant_value array.
210 *
211 * \warning
212 * This class assumes that every uniform that will be processed is
213 * already in the \c string_to_uint_map. In addition, it assumes that
214 * the \c gl_uniform_storage and \c gl_constant_value arrays are "big
215 * enough."
216 */
217 class parcel_out_uniform_storage : public uniform_field_visitor {
218 public:
219 parcel_out_uniform_storage(struct string_to_uint_map *map,
220 struct gl_uniform_storage *uniforms,
221 union gl_constant_value *values)
222 : map(map), uniforms(uniforms), next_sampler(0), values(values)
223 {
224 memset(this->targets, 0, sizeof(this->targets));
225 }
226
227 void start_shader()
228 {
229 this->shader_samplers_used = 0;
230 this->shader_shadow_samplers = 0;
231 }
232
233 void set_and_process(struct gl_shader_program *prog,
234 struct gl_shader *shader,
235 ir_variable *var)
236 {
237 ubo_var = NULL;
238 if (var->is_in_uniform_block()) {
239 struct gl_uniform_block *block =
240 &shader->UniformBlocks[var->uniform_block];
241
242 ubo_block_index = -1;
243 for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
244 if (!strcmp(prog->UniformBlocks[i].Name,
245 shader->UniformBlocks[var->uniform_block].Name)) {
246 ubo_block_index = i;
247 break;
248 }
249 }
250 assert(ubo_block_index != -1);
251
252 ubo_var_index = var->location;
253 ubo_var = &block->Uniforms[var->location];
254 ubo_byte_offset = ubo_var->Offset;
255 }
256
257 process(var);
258 }
259
260 struct gl_uniform_buffer_variable *ubo_var;
261 int ubo_block_index;
262 int ubo_var_index;
263 int ubo_byte_offset;
264
265 private:
266 virtual void visit_field(const glsl_type *type, const char *name)
267 {
268 assert(!type->is_record());
269 assert(!(type->is_array() && type->fields.array->is_record()));
270
271 unsigned id;
272 bool found = this->map->get(id, name);
273 assert(found);
274
275 if (!found)
276 return;
277
278 /* If there is already storage associated with this uniform, it means
279 * that it was set while processing an earlier shader stage. For
280 * example, we may be processing the uniform in the fragment shader, but
281 * the uniform was already processed in the vertex shader.
282 */
283 if (this->uniforms[id].storage != NULL) {
284 /* If the uniform already has storage set from another shader stage,
285 * mark the samplers used for this shader stage.
286 */
287 if (type->contains_sampler()) {
288 const unsigned count = MAX2(1, this->uniforms[id].array_elements);
289 const unsigned shadow = (type->is_array())
290 ? type->fields.array->sampler_shadow : type->sampler_shadow;
291
292 for (unsigned i = 0; i < count; i++) {
293 const unsigned s = this->uniforms[id].sampler + i;
294
295 this->shader_samplers_used |= 1U << s;
296 this->shader_shadow_samplers |= shadow << s;
297 }
298 }
299
300 return;
301 }
302
303 const glsl_type *base_type;
304 if (type->is_array()) {
305 this->uniforms[id].array_elements = type->length;
306 base_type = type->fields.array;
307 } else {
308 this->uniforms[id].array_elements = 0;
309 base_type = type;
310 }
311
312 if (base_type->is_sampler()) {
313 this->uniforms[id].sampler = this->next_sampler;
314
315 /* Increment the sampler by 1 for non-arrays and by the number of
316 * array elements for arrays.
317 */
318 this->next_sampler += MAX2(1, this->uniforms[id].array_elements);
319
320 const gl_texture_index target = base_type->sampler_index();
321 const unsigned shadow = base_type->sampler_shadow;
322 for (unsigned i = this->uniforms[id].sampler
323 ; i < MIN2(this->next_sampler, MAX_SAMPLERS)
324 ; i++) {
325 this->targets[i] = target;
326 this->shader_samplers_used |= 1U << i;
327 this->shader_shadow_samplers |= shadow << i;
328 }
329
330 } else {
331 this->uniforms[id].sampler = ~0;
332 }
333
334 this->uniforms[id].name = ralloc_strdup(this->uniforms, name);
335 this->uniforms[id].type = base_type;
336 this->uniforms[id].initialized = 0;
337 this->uniforms[id].num_driver_storage = 0;
338 this->uniforms[id].driver_storage = NULL;
339 this->uniforms[id].storage = this->values;
340 if (this->ubo_var) {
341 this->uniforms[id].block_index = this->ubo_block_index;
342
343 unsigned alignment = type->std140_base_alignment(ubo_var->RowMajor);
344 this->ubo_byte_offset = align(this->ubo_byte_offset, alignment);
345 this->uniforms[id].offset = this->ubo_byte_offset;
346 this->ubo_byte_offset += type->std140_size(ubo_var->RowMajor);
347
348 if (type->is_array()) {
349 this->uniforms[id].array_stride =
350 align(type->fields.array->std140_size(ubo_var->RowMajor), 16);
351 } else {
352 this->uniforms[id].array_stride = 0;
353 }
354
355 if (type->is_matrix() ||
356 (type->is_array() && type->fields.array->is_matrix())) {
357 this->uniforms[id].matrix_stride = 16;
358 this->uniforms[id].row_major = ubo_var->RowMajor;
359 } else {
360 this->uniforms[id].matrix_stride = 0;
361 this->uniforms[id].row_major = false;
362 }
363 } else {
364 this->uniforms[id].block_index = -1;
365 this->uniforms[id].offset = -1;
366 this->uniforms[id].array_stride = -1;
367 this->uniforms[id].matrix_stride = -1;
368 this->uniforms[id].row_major = false;
369 }
370
371 this->values += values_for_type(type);
372 }
373
374 struct string_to_uint_map *map;
375
376 struct gl_uniform_storage *uniforms;
377 unsigned next_sampler;
378
379 public:
380 union gl_constant_value *values;
381
382 gl_texture_index targets[MAX_SAMPLERS];
383
384 /**
385 * Mask of samplers used by the current shader stage.
386 */
387 unsigned shader_samplers_used;
388
389 /**
390 * Mask of samplers used by the current shader stage for shadows.
391 */
392 unsigned shader_shadow_samplers;
393 };
394
395 /**
396 * Merges a uniform block into an array of uniform blocks that may or
397 * may not already contain a copy of it.
398 *
399 * Returns the index of the new block in the array.
400 */
401 int
402 link_cross_validate_uniform_block(void *mem_ctx,
403 struct gl_uniform_block **linked_blocks,
404 unsigned int *num_linked_blocks,
405 struct gl_uniform_block *new_block)
406 {
407 for (unsigned int i = 0; i < *num_linked_blocks; i++) {
408 struct gl_uniform_block *old_block = &(*linked_blocks)[i];
409
410 if (strcmp(old_block->Name, new_block->Name) == 0)
411 return link_uniform_blocks_are_compatible(old_block, new_block)
412 ? i : -1;
413 }
414
415 *linked_blocks = reralloc(mem_ctx, *linked_blocks,
416 struct gl_uniform_block,
417 *num_linked_blocks + 1);
418 int linked_block_index = (*num_linked_blocks)++;
419 struct gl_uniform_block *linked_block = &(*linked_blocks)[linked_block_index];
420
421 memcpy(linked_block, new_block, sizeof(*new_block));
422 linked_block->Uniforms = ralloc_array(*linked_blocks,
423 struct gl_uniform_buffer_variable,
424 linked_block->NumUniforms);
425
426 memcpy(linked_block->Uniforms,
427 new_block->Uniforms,
428 sizeof(*linked_block->Uniforms) * linked_block->NumUniforms);
429
430 for (unsigned int i = 0; i < linked_block->NumUniforms; i++) {
431 struct gl_uniform_buffer_variable *ubo_var =
432 &linked_block->Uniforms[i];
433
434 ubo_var->Name = ralloc_strdup(*linked_blocks, ubo_var->Name);
435 }
436
437 return linked_block_index;
438 }
439
440 /**
441 * Walks the IR and update the references to uniform blocks in the
442 * ir_variables to point at linked shader's list (previously, they
443 * would point at the uniform block list in one of the pre-linked
444 * shaders).
445 */
446 static void
447 link_update_uniform_buffer_variables(struct gl_shader *shader)
448 {
449 foreach_list(node, shader->ir) {
450 ir_variable *const var = ((ir_instruction *) node)->as_variable();
451
452 if ((var == NULL) || !var->is_in_uniform_block())
453 continue;
454
455 assert(var->mode == ir_var_uniform);
456
457 bool found = false;
458 for (unsigned i = 0; i < shader->NumUniformBlocks; i++) {
459 for (unsigned j = 0; j < shader->UniformBlocks[i].NumUniforms; j++) {
460 if (!strcmp(var->name, shader->UniformBlocks[i].Uniforms[j].Name)) {
461 found = true;
462 var->uniform_block = i;
463 var->location = j;
464 break;
465 }
466 }
467 if (found)
468 break;
469 }
470 assert(found);
471 }
472 }
473
474 void
475 link_assign_uniform_block_offsets(struct gl_shader *shader)
476 {
477 for (unsigned b = 0; b < shader->NumUniformBlocks; b++) {
478 struct gl_uniform_block *block = &shader->UniformBlocks[b];
479
480 unsigned offset = 0;
481 for (unsigned int i = 0; i < block->NumUniforms; i++) {
482 struct gl_uniform_buffer_variable *ubo_var = &block->Uniforms[i];
483 const struct glsl_type *type = ubo_var->Type;
484
485 unsigned alignment = type->std140_base_alignment(ubo_var->RowMajor);
486 unsigned size = type->std140_size(ubo_var->RowMajor);
487
488 offset = align(offset, alignment);
489 ubo_var->Offset = offset;
490 offset += size;
491 }
492
493 /* From the GL_ARB_uniform_buffer_object spec:
494 *
495 * "For uniform blocks laid out according to [std140] rules,
496 * the minimum buffer object size returned by the
497 * UNIFORM_BLOCK_DATA_SIZE query is derived by taking the
498 * offset of the last basic machine unit consumed by the
499 * last uniform of the uniform block (including any
500 * end-of-array or end-of-structure padding), adding one,
501 * and rounding up to the next multiple of the base
502 * alignment required for a vec4."
503 */
504 block->UniformBufferSize = align(offset, 16);
505 }
506 }
507
508 void
509 link_assign_uniform_locations(struct gl_shader_program *prog)
510 {
511 ralloc_free(prog->UniformStorage);
512 prog->UniformStorage = NULL;
513 prog->NumUserUniformStorage = 0;
514
515 if (prog->UniformHash != NULL) {
516 prog->UniformHash->clear();
517 } else {
518 prog->UniformHash = new string_to_uint_map;
519 }
520
521 /* Uniforms that lack an initializer in the shader code have an initial
522 * value of zero. This includes sampler uniforms.
523 *
524 * Page 24 (page 30 of the PDF) of the GLSL 1.20 spec says:
525 *
526 * "The link time initial value is either the value of the variable's
527 * initializer, if present, or 0 if no initializer is present. Sampler
528 * types cannot have initializers."
529 */
530 memset(prog->SamplerUnits, 0, sizeof(prog->SamplerUnits));
531
532 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
533 if (prog->_LinkedShaders[i] == NULL)
534 continue;
535
536 link_update_uniform_buffer_variables(prog->_LinkedShaders[i]);
537 }
538
539 /* First pass: Count the uniform resources used by the user-defined
540 * uniforms. While this happens, each active uniform will have an index
541 * assigned to it.
542 *
543 * Note: this is *NOT* the index that is returned to the application by
544 * glGetUniformLocation.
545 */
546 count_uniform_size uniform_size(prog->UniformHash);
547 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
548 if (prog->_LinkedShaders[i] == NULL)
549 continue;
550
551 /* Reset various per-shader target counts.
552 */
553 uniform_size.start_shader();
554
555 foreach_list(node, prog->_LinkedShaders[i]->ir) {
556 ir_variable *const var = ((ir_instruction *) node)->as_variable();
557
558 if ((var == NULL) || (var->mode != ir_var_uniform))
559 continue;
560
561 /* FINISHME: Update code to process built-in uniforms!
562 */
563 if (strncmp("gl_", var->name, 3) == 0) {
564 uniform_size.num_shader_uniform_components +=
565 var->type->component_slots();
566 continue;
567 }
568
569 uniform_size.process(var);
570 }
571
572 prog->_LinkedShaders[i]->num_samplers = uniform_size.num_shader_samplers;
573 prog->_LinkedShaders[i]->num_uniform_components =
574 uniform_size.num_shader_uniform_components;
575 }
576
577 const unsigned num_user_uniforms = uniform_size.num_active_uniforms;
578 const unsigned num_data_slots = uniform_size.num_values;
579
580 /* On the outside chance that there were no uniforms, bail out.
581 */
582 if (num_user_uniforms == 0)
583 return;
584
585 struct gl_uniform_storage *uniforms =
586 rzalloc_array(prog, struct gl_uniform_storage, num_user_uniforms);
587 union gl_constant_value *data =
588 rzalloc_array(uniforms, union gl_constant_value, num_data_slots);
589 #ifndef NDEBUG
590 union gl_constant_value *data_end = &data[num_data_slots];
591 #endif
592
593 parcel_out_uniform_storage parcel(prog->UniformHash, uniforms, data);
594
595 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
596 if (prog->_LinkedShaders[i] == NULL)
597 continue;
598
599 /* Reset various per-shader target counts.
600 */
601 parcel.start_shader();
602
603 foreach_list(node, prog->_LinkedShaders[i]->ir) {
604 ir_variable *const var = ((ir_instruction *) node)->as_variable();
605
606 if ((var == NULL) || (var->mode != ir_var_uniform))
607 continue;
608
609 /* FINISHME: Update code to process built-in uniforms!
610 */
611 if (strncmp("gl_", var->name, 3) == 0)
612 continue;
613
614 parcel.set_and_process(prog, prog->_LinkedShaders[i], var);
615 }
616
617 prog->_LinkedShaders[i]->active_samplers = parcel.shader_samplers_used;
618 prog->_LinkedShaders[i]->shadow_samplers = parcel.shader_shadow_samplers;
619 }
620
621 assert(sizeof(prog->SamplerTargets) == sizeof(parcel.targets));
622 memcpy(prog->SamplerTargets, parcel.targets, sizeof(prog->SamplerTargets));
623
624 #ifndef NDEBUG
625 for (unsigned i = 0; i < num_user_uniforms; i++) {
626 assert(uniforms[i].storage != NULL);
627 }
628
629 assert(parcel.values == data_end);
630 #endif
631
632 prog->NumUserUniformStorage = num_user_uniforms;
633 prog->UniformStorage = uniforms;
634
635 link_set_uniform_initializers(prog);
636
637 return;
638 }