glsl: move uniform calculation to link_uniforms
[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 #include "util/hash_table.h"
32
33 /**
34 * \file link_uniforms.cpp
35 * Assign locations for GLSL uniforms.
36 *
37 * \author Ian Romanick <ian.d.romanick@intel.com>
38 */
39
40 /**
41 * Used by linker to indicate uniforms that have no location set.
42 */
43 #define UNMAPPED_UNIFORM_LOC ~0u
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 program_resource_visitor::process(const glsl_type *type, const char *name)
62 {
63 assert(type->without_array()->is_record()
64 || type->without_array()->is_interface());
65
66 unsigned record_array_count = 1;
67 char *name_copy = ralloc_strdup(NULL, name);
68 unsigned packing = type->interface_packing;
69
70 recursion(type, &name_copy, strlen(name), false, NULL, packing, false,
71 record_array_count);
72 ralloc_free(name_copy);
73 }
74
75 void
76 program_resource_visitor::process(ir_variable *var)
77 {
78 unsigned record_array_count = 1;
79 const glsl_type *t = var->type;
80 const glsl_type *t_without_array = var->type->without_array();
81 const bool row_major =
82 var->data.matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR;
83
84 const unsigned packing = var->get_interface_type() ?
85 var->get_interface_type()->interface_packing :
86 var->type->interface_packing;
87
88 /* false is always passed for the row_major parameter to the other
89 * processing functions because no information is available to do
90 * otherwise. See the warning in linker.h.
91 */
92
93 /* Only strdup the name if we actually will need to modify it. */
94 if (var->data.from_named_ifc_block_array) {
95 /* lower_named_interface_blocks created this variable by lowering an
96 * interface block array to an array variable. For example if the
97 * original source code was:
98 *
99 * out Blk { vec4 bar } foo[3];
100 *
101 * Then the variable is now:
102 *
103 * out vec4 bar[3];
104 *
105 * We need to visit each array element using the names constructed like
106 * so:
107 *
108 * Blk[0].bar
109 * Blk[1].bar
110 * Blk[2].bar
111 */
112 assert(t->is_array());
113 const glsl_type *ifc_type = var->get_interface_type();
114 char *name = ralloc_strdup(NULL, ifc_type->name);
115 size_t name_length = strlen(name);
116 for (unsigned i = 0; i < t->length; i++) {
117 size_t new_length = name_length;
118 ralloc_asprintf_rewrite_tail(&name, &new_length, "[%u].%s", i,
119 var->name);
120 /* Note: row_major is only meaningful for uniform blocks, and
121 * lowering is only applied to non-uniform interface blocks, so we
122 * can safely pass false for row_major.
123 */
124 recursion(var->type, &name, new_length, row_major, NULL, packing,
125 false, record_array_count);
126 }
127 ralloc_free(name);
128 } else if (var->data.from_named_ifc_block_nonarray) {
129 /* lower_named_interface_blocks created this variable by lowering a
130 * named interface block (non-array) to an ordinary variable. For
131 * example if the original source code was:
132 *
133 * out Blk { vec4 bar } foo;
134 *
135 * Then the variable is now:
136 *
137 * out vec4 bar;
138 *
139 * We need to visit this variable using the name:
140 *
141 * Blk.bar
142 */
143 const glsl_type *ifc_type = var->get_interface_type();
144 char *name = ralloc_asprintf(NULL, "%s.%s", ifc_type->name, var->name);
145 /* Note: row_major is only meaningful for uniform blocks, and lowering
146 * is only applied to non-uniform interface blocks, so we can safely
147 * pass false for row_major.
148 */
149 recursion(var->type, &name, strlen(name), row_major, NULL, packing,
150 false, record_array_count);
151 ralloc_free(name);
152 } else if (t_without_array->is_record() ||
153 (t->is_array() && t->fields.array->is_array())) {
154 char *name = ralloc_strdup(NULL, var->name);
155 recursion(var->type, &name, strlen(name), row_major, NULL, packing,
156 false, record_array_count);
157 ralloc_free(name);
158 } else if (t_without_array->is_interface()) {
159 char *name = ralloc_strdup(NULL, t_without_array->name);
160 recursion(var->type, &name, strlen(name), row_major, NULL, packing,
161 false, record_array_count);
162 ralloc_free(name);
163 } else {
164 this->set_record_array_count(record_array_count);
165 this->visit_field(t, var->name, row_major, NULL, packing, false);
166 }
167 }
168
169 void
170 program_resource_visitor::recursion(const glsl_type *t, char **name,
171 size_t name_length, bool row_major,
172 const glsl_type *record_type,
173 const unsigned packing,
174 bool last_field,
175 unsigned record_array_count)
176 {
177 /* Records need to have each field processed individually.
178 *
179 * Arrays of records need to have each array element processed
180 * individually, then each field of the resulting array elements processed
181 * individually.
182 */
183 if (t->is_record() || t->is_interface()) {
184 if (record_type == NULL && t->is_record())
185 record_type = t;
186
187 if (t->is_record())
188 this->enter_record(t, *name, row_major, packing);
189
190 for (unsigned i = 0; i < t->length; i++) {
191 const char *field = t->fields.structure[i].name;
192 size_t new_length = name_length;
193
194 if (t->fields.structure[i].type->is_record())
195 this->visit_field(&t->fields.structure[i]);
196
197 /* Append '.field' to the current variable name. */
198 if (name_length == 0) {
199 ralloc_asprintf_rewrite_tail(name, &new_length, "%s", field);
200 } else {
201 ralloc_asprintf_rewrite_tail(name, &new_length, ".%s", field);
202 }
203
204 /* The layout of structures at the top level of the block is set
205 * during parsing. For matrices contained in multiple levels of
206 * structures in the block, the inner structures have no layout.
207 * These cases must potentially inherit the layout from the outer
208 * levels.
209 */
210 bool field_row_major = row_major;
211 const enum glsl_matrix_layout matrix_layout =
212 glsl_matrix_layout(t->fields.structure[i].matrix_layout);
213 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
214 field_row_major = true;
215 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
216 field_row_major = false;
217 }
218
219 recursion(t->fields.structure[i].type, name, new_length,
220 field_row_major,
221 record_type,
222 packing,
223 (i + 1) == t->length, record_array_count);
224
225 /* Only the first leaf-field of the record gets called with the
226 * record type pointer.
227 */
228 record_type = NULL;
229 }
230
231 if (t->is_record()) {
232 (*name)[name_length] = '\0';
233 this->leave_record(t, *name, row_major, packing);
234 }
235 } else if (t->without_array()->is_record() ||
236 t->without_array()->is_interface() ||
237 (t->is_array() && t->fields.array->is_array())) {
238 if (record_type == NULL && t->fields.array->is_record())
239 record_type = t->fields.array;
240
241 unsigned length = t->length;
242 /* Shader storage block unsized arrays: add subscript [0] to variable
243 * names */
244 if (t->is_unsized_array())
245 length = 1;
246
247 record_array_count *= length;
248
249 for (unsigned i = 0; i < length; i++) {
250 size_t new_length = name_length;
251
252 /* Append the subscript to the current variable name */
253 ralloc_asprintf_rewrite_tail(name, &new_length, "[%u]", i);
254
255 recursion(t->fields.array, name, new_length, row_major,
256 record_type,
257 packing,
258 (i + 1) == t->length, record_array_count);
259
260 /* Only the first leaf-field of the record gets called with the
261 * record type pointer.
262 */
263 record_type = NULL;
264 }
265 } else {
266 this->set_record_array_count(record_array_count);
267 this->visit_field(t, *name, row_major, record_type, packing, last_field);
268 }
269 }
270
271 void
272 program_resource_visitor::visit_field(const glsl_type *type, const char *name,
273 bool row_major,
274 const glsl_type *,
275 const unsigned,
276 bool /* last_field */)
277 {
278 visit_field(type, name, row_major);
279 }
280
281 void
282 program_resource_visitor::visit_field(const glsl_struct_field *field)
283 {
284 (void) field;
285 /* empty */
286 }
287
288 void
289 program_resource_visitor::enter_record(const glsl_type *, const char *, bool,
290 const unsigned)
291 {
292 }
293
294 void
295 program_resource_visitor::leave_record(const glsl_type *, const char *, bool,
296 const unsigned)
297 {
298 }
299
300 void
301 program_resource_visitor::set_record_array_count(unsigned)
302 {
303 }
304
305 namespace {
306
307 /**
308 * Class to help calculate the storage requirements for a set of uniforms
309 *
310 * As uniforms are added to the active set the number of active uniforms and
311 * the storage requirements for those uniforms are accumulated. The active
312 * uniforms are added to the hash table supplied to the constructor.
313 *
314 * If the same uniform is added multiple times (i.e., once for each shader
315 * target), it will only be accounted once.
316 */
317 class count_uniform_size : public program_resource_visitor {
318 public:
319 count_uniform_size(struct string_to_uint_map *map,
320 struct string_to_uint_map *hidden_map)
321 : num_active_uniforms(0), num_hidden_uniforms(0), num_values(0),
322 num_shader_samplers(0), num_shader_images(0),
323 num_shader_uniform_components(0), num_shader_subroutines(0),
324 is_ubo_var(false), is_shader_storage(false), map(map),
325 hidden_map(hidden_map)
326 {
327 /* empty */
328 }
329
330 void start_shader()
331 {
332 this->num_shader_samplers = 0;
333 this->num_shader_images = 0;
334 this->num_shader_uniform_components = 0;
335 this->num_shader_subroutines = 0;
336 }
337
338 void process(ir_variable *var)
339 {
340 this->current_var = var;
341 this->is_ubo_var = var->is_in_buffer_block();
342 this->is_shader_storage = var->is_in_shader_storage_block();
343 if (var->is_interface_instance())
344 program_resource_visitor::process(var->get_interface_type(),
345 var->get_interface_type()->name);
346 else
347 program_resource_visitor::process(var);
348 }
349
350 /**
351 * Total number of active uniforms counted
352 */
353 unsigned num_active_uniforms;
354
355 unsigned num_hidden_uniforms;
356
357 /**
358 * Number of data values required to back the storage for the active uniforms
359 */
360 unsigned num_values;
361
362 /**
363 * Number of samplers used
364 */
365 unsigned num_shader_samplers;
366
367 /**
368 * Number of images used
369 */
370 unsigned num_shader_images;
371
372 /**
373 * Number of uniforms used in the current shader
374 */
375 unsigned num_shader_uniform_components;
376
377 /**
378 * Number of subroutine uniforms used
379 */
380 unsigned num_shader_subroutines;
381
382 bool is_ubo_var;
383 bool is_shader_storage;
384
385 struct string_to_uint_map *map;
386
387 private:
388 virtual void visit_field(const glsl_type *type, const char *name,
389 bool row_major)
390 {
391 assert(!type->without_array()->is_record());
392 assert(!type->without_array()->is_interface());
393 assert(!(type->is_array() && type->fields.array->is_array()));
394
395 (void) row_major;
396
397 /* Count the number of samplers regardless of whether the uniform is
398 * already in the hash table. The hash table prevents adding the same
399 * uniform for multiple shader targets, but in this case we want to
400 * count it for each shader target.
401 */
402 const unsigned values = values_for_type(type);
403 if (type->contains_subroutine()) {
404 this->num_shader_subroutines += values;
405 } else if (type->contains_sampler()) {
406 this->num_shader_samplers += values;
407 } else if (type->contains_image()) {
408 this->num_shader_images += values;
409
410 /* As drivers are likely to represent image uniforms as
411 * scalar indices, count them against the limit of uniform
412 * components in the default block. The spec allows image
413 * uniforms to use up no more than one scalar slot.
414 */
415 if(!is_shader_storage)
416 this->num_shader_uniform_components += values;
417 } else {
418 /* Accumulate the total number of uniform slots used by this shader.
419 * Note that samplers do not count against this limit because they
420 * don't use any storage on current hardware.
421 */
422 if (!is_ubo_var && !is_shader_storage)
423 this->num_shader_uniform_components += values;
424 }
425
426 /* If the uniform is already in the map, there's nothing more to do.
427 */
428 unsigned id;
429 if (this->map->get(id, name))
430 return;
431
432 if (this->current_var->data.how_declared == ir_var_hidden) {
433 this->hidden_map->put(this->num_hidden_uniforms, name);
434 this->num_hidden_uniforms++;
435 } else {
436 this->map->put(this->num_active_uniforms-this->num_hidden_uniforms,
437 name);
438 }
439
440 /* Each leaf uniform occupies one entry in the list of active
441 * uniforms.
442 */
443 this->num_active_uniforms++;
444 this->num_values += values;
445 }
446
447 struct string_to_uint_map *hidden_map;
448
449 /**
450 * Current variable being processed.
451 */
452 ir_variable *current_var;
453 };
454
455 } /* anonymous namespace */
456
457 /**
458 * Class to help parcel out pieces of backing storage to uniforms
459 *
460 * Each uniform processed has some range of the \c gl_constant_value
461 * structures associated with it. The association is done by finding
462 * the uniform in the \c string_to_uint_map and using the value from
463 * the map to connect that slot in the \c gl_uniform_storage table
464 * with the next available slot in the \c gl_constant_value array.
465 *
466 * \warning
467 * This class assumes that every uniform that will be processed is
468 * already in the \c string_to_uint_map. In addition, it assumes that
469 * the \c gl_uniform_storage and \c gl_constant_value arrays are "big
470 * enough."
471 */
472 class parcel_out_uniform_storage : public program_resource_visitor {
473 public:
474 parcel_out_uniform_storage(struct string_to_uint_map *map,
475 struct gl_uniform_storage *uniforms,
476 union gl_constant_value *values)
477 : map(map), uniforms(uniforms), values(values)
478 {
479 }
480
481 void start_shader(gl_shader_stage shader_type)
482 {
483 assert(shader_type < MESA_SHADER_STAGES);
484 this->shader_type = shader_type;
485
486 this->shader_samplers_used = 0;
487 this->shader_shadow_samplers = 0;
488 this->next_sampler = 0;
489 this->next_image = 0;
490 this->next_subroutine = 0;
491 this->record_array_count = 1;
492 memset(this->targets, 0, sizeof(this->targets));
493 }
494
495 void set_and_process(struct gl_shader_program *prog,
496 ir_variable *var)
497 {
498 current_var = var;
499 field_counter = 0;
500 this->record_next_sampler = new string_to_uint_map;
501
502 ubo_block_index = -1;
503 if (var->is_in_buffer_block()) {
504 if (var->is_interface_instance() && var->type->is_array()) {
505 unsigned l = strlen(var->get_interface_type()->name);
506
507 for (unsigned i = 0; i < prog->NumBufferInterfaceBlocks; i++) {
508 if (strncmp(var->get_interface_type()->name,
509 prog->BufferInterfaceBlocks[i].Name,
510 l) == 0
511 && prog->BufferInterfaceBlocks[i].Name[l] == '[') {
512 ubo_block_index = i;
513 break;
514 }
515 }
516 } else {
517 for (unsigned i = 0; i < prog->NumBufferInterfaceBlocks; i++) {
518 if (strcmp(var->get_interface_type()->name,
519 prog->BufferInterfaceBlocks[i].Name) == 0) {
520 ubo_block_index = i;
521 break;
522 }
523 }
524 }
525 assert(ubo_block_index != -1);
526
527 /* Uniform blocks that were specified with an instance name must be
528 * handled a little bit differently. The name of the variable is the
529 * name used to reference the uniform block instead of being the name
530 * of a variable within the block. Therefore, searching for the name
531 * within the block will fail.
532 */
533 if (var->is_interface_instance()) {
534 ubo_byte_offset = 0;
535 process(var->get_interface_type(),
536 var->get_interface_type()->name);
537 } else {
538 const struct gl_uniform_block *const block =
539 &prog->BufferInterfaceBlocks[ubo_block_index];
540
541 assert(var->data.location != -1);
542
543 const struct gl_uniform_buffer_variable *const ubo_var =
544 &block->Uniforms[var->data.location];
545
546 ubo_byte_offset = ubo_var->Offset;
547 process(var);
548 }
549 } else {
550 /* Store any explicit location and reset data location so we can
551 * reuse this variable for storing the uniform slot number.
552 */
553 this->explicit_location = current_var->data.location;
554 current_var->data.location = -1;
555
556 process(var);
557 }
558 delete this->record_next_sampler;
559 }
560
561 int ubo_block_index;
562 int ubo_byte_offset;
563 gl_shader_stage shader_type;
564
565 private:
566 void handle_samplers(const glsl_type *base_type,
567 struct gl_uniform_storage *uniform, const char *name)
568 {
569 if (base_type->is_sampler()) {
570 uniform->opaque[shader_type].active = true;
571
572 /* Handle multiple samplers inside struct arrays */
573 if (this->record_array_count > 1) {
574 unsigned inner_array_size = MAX2(1, uniform->array_elements);
575 char *name_copy = ralloc_strdup(NULL, name);
576
577 /* Remove all array subscripts from the sampler name */
578 char *str_start;
579 const char *str_end;
580 while((str_start = strchr(name_copy, '[')) &&
581 (str_end = strchr(name_copy, ']'))) {
582 memmove(str_start, str_end + 1, 1 + strlen(str_end));
583 }
584
585 unsigned index = 0;
586 if (this->record_next_sampler->get(index, name_copy)) {
587 /* In this case, we've already seen this uniform so we just use
588 * the next sampler index recorded the last time we visited.
589 */
590 uniform->opaque[shader_type].index = index;
591 index = inner_array_size + uniform->opaque[shader_type].index;
592 this->record_next_sampler->put(index, name_copy);
593
594 ralloc_free(name_copy);
595 /* Return as everything else has already been initialised in a
596 * previous pass.
597 */
598 return;
599 } else {
600 /* We've never seen this uniform before so we need to allocate
601 * enough indices to store it.
602 *
603 * Nested struct arrays behave like arrays of arrays so we need
604 * to increase the index by the total number of elements of the
605 * sampler in case there is more than one sampler inside the
606 * structs. This allows the offset to be easily calculated for
607 * indirect indexing.
608 */
609 uniform->opaque[shader_type].index = this->next_sampler;
610 this->next_sampler +=
611 inner_array_size * this->record_array_count;
612
613 /* Store the next index for future passes over the struct array
614 */
615 index = uniform->opaque[shader_type].index + inner_array_size;
616 this->record_next_sampler->put(index, name_copy);
617 ralloc_free(name_copy);
618 }
619 } else {
620 /* Increment the sampler by 1 for non-arrays and by the number of
621 * array elements for arrays.
622 */
623 uniform->opaque[shader_type].index = this->next_sampler;
624 this->next_sampler += MAX2(1, uniform->array_elements);
625 }
626
627 const gl_texture_index target = base_type->sampler_index();
628 const unsigned shadow = base_type->sampler_shadow;
629 for (unsigned i = uniform->opaque[shader_type].index;
630 i < MIN2(this->next_sampler, MAX_SAMPLERS);
631 i++) {
632 this->targets[i] = target;
633 this->shader_samplers_used |= 1U << i;
634 this->shader_shadow_samplers |= shadow << i;
635 }
636 }
637 }
638
639 void handle_images(const glsl_type *base_type,
640 struct gl_uniform_storage *uniform)
641 {
642 if (base_type->is_image()) {
643 uniform->opaque[shader_type].index = this->next_image;
644 uniform->opaque[shader_type].active = true;
645
646 /* Increment the image index by 1 for non-arrays and by the
647 * number of array elements for arrays.
648 */
649 this->next_image += MAX2(1, uniform->array_elements);
650
651 }
652 }
653
654 void handle_subroutines(const glsl_type *base_type,
655 struct gl_uniform_storage *uniform)
656 {
657 if (base_type->is_subroutine()) {
658 uniform->opaque[shader_type].index = this->next_subroutine;
659 uniform->opaque[shader_type].active = true;
660
661 /* Increment the subroutine index by 1 for non-arrays and by the
662 * number of array elements for arrays.
663 */
664 this->next_subroutine += MAX2(1, uniform->array_elements);
665
666 }
667 }
668
669 virtual void set_record_array_count(unsigned record_array_count)
670 {
671 this->record_array_count = record_array_count;
672 }
673
674 virtual void visit_field(const glsl_type *type, const char *name,
675 bool row_major)
676 {
677 (void) type;
678 (void) name;
679 (void) row_major;
680 assert(!"Should not get here.");
681 }
682
683 virtual void enter_record(const glsl_type *type, const char *,
684 bool row_major, const unsigned packing) {
685 assert(type->is_record());
686 if (this->ubo_block_index == -1)
687 return;
688 if (packing == GLSL_INTERFACE_PACKING_STD430)
689 this->ubo_byte_offset = glsl_align(
690 this->ubo_byte_offset, type->std430_base_alignment(row_major));
691 else
692 this->ubo_byte_offset = glsl_align(
693 this->ubo_byte_offset, type->std140_base_alignment(row_major));
694 }
695
696 virtual void leave_record(const glsl_type *type, const char *,
697 bool row_major, const unsigned packing) {
698 assert(type->is_record());
699 if (this->ubo_block_index == -1)
700 return;
701 if (packing == GLSL_INTERFACE_PACKING_STD430)
702 this->ubo_byte_offset = glsl_align(
703 this->ubo_byte_offset, type->std430_base_alignment(row_major));
704 else
705 this->ubo_byte_offset = glsl_align(
706 this->ubo_byte_offset, type->std140_base_alignment(row_major));
707 }
708
709 virtual void visit_field(const glsl_type *type, const char *name,
710 bool row_major, const glsl_type *record_type,
711 const unsigned packing,
712 bool /* last_field */)
713 {
714 assert(!type->without_array()->is_record());
715 assert(!type->without_array()->is_interface());
716 assert(!(type->is_array() && type->fields.array->is_array()));
717
718 unsigned id;
719 bool found = this->map->get(id, name);
720 assert(found);
721
722 if (!found)
723 return;
724
725 const glsl_type *base_type;
726 if (type->is_array()) {
727 this->uniforms[id].array_elements = type->length;
728 base_type = type->fields.array;
729 } else {
730 this->uniforms[id].array_elements = 0;
731 base_type = type;
732 }
733
734 /* Initialise opaque data */
735 this->uniforms[id].opaque[shader_type].index = ~0;
736 this->uniforms[id].opaque[shader_type].active = false;
737
738 /* This assigns uniform indices to sampler and image uniforms. */
739 handle_samplers(base_type, &this->uniforms[id], name);
740 handle_images(base_type, &this->uniforms[id]);
741 handle_subroutines(base_type, &this->uniforms[id]);
742
743 /* For array of arrays or struct arrays the base location may have
744 * already been set so don't set it again.
745 */
746 if (ubo_block_index == -1 && current_var->data.location == -1) {
747 current_var->data.location = id;
748 }
749
750 /* If there is already storage associated with this uniform or if the
751 * uniform is set as builtin, it means that it was set while processing
752 * an earlier shader stage. For example, we may be processing the
753 * uniform in the fragment shader, but the uniform was already processed
754 * in the vertex shader.
755 */
756 if (this->uniforms[id].storage != NULL || this->uniforms[id].builtin) {
757 return;
758 }
759
760 /* Assign explicit locations. */
761 if (current_var->data.explicit_location) {
762 /* Set sequential locations for struct fields. */
763 if (current_var->type->without_array()->is_record() ||
764 current_var->type->is_array_of_arrays()) {
765 const unsigned entries = MAX2(1, this->uniforms[id].array_elements);
766 this->uniforms[id].remap_location =
767 this->explicit_location + field_counter;
768 field_counter += entries;
769 } else {
770 this->uniforms[id].remap_location = this->explicit_location;
771 }
772 } else {
773 /* Initialize to to indicate that no location is set */
774 this->uniforms[id].remap_location = UNMAPPED_UNIFORM_LOC;
775 }
776
777 this->uniforms[id].name = ralloc_strdup(this->uniforms, name);
778 this->uniforms[id].type = base_type;
779 this->uniforms[id].initialized = 0;
780 this->uniforms[id].num_driver_storage = 0;
781 this->uniforms[id].driver_storage = NULL;
782 this->uniforms[id].atomic_buffer_index = -1;
783 this->uniforms[id].hidden =
784 current_var->data.how_declared == ir_var_hidden;
785 this->uniforms[id].builtin = is_gl_identifier(name);
786
787 /* Do not assign storage if the uniform is builtin */
788 if (!this->uniforms[id].builtin)
789 this->uniforms[id].storage = this->values;
790
791 this->uniforms[id].is_shader_storage =
792 current_var->is_in_shader_storage_block();
793
794 if (this->ubo_block_index != -1) {
795 this->uniforms[id].block_index = this->ubo_block_index;
796
797 unsigned alignment = type->std140_base_alignment(row_major);
798 if (packing == GLSL_INTERFACE_PACKING_STD430)
799 alignment = type->std430_base_alignment(row_major);
800 this->ubo_byte_offset = glsl_align(this->ubo_byte_offset, alignment);
801 this->uniforms[id].offset = this->ubo_byte_offset;
802 if (packing == GLSL_INTERFACE_PACKING_STD430)
803 this->ubo_byte_offset += type->std430_size(row_major);
804 else
805 this->ubo_byte_offset += type->std140_size(row_major);
806
807 if (type->is_array()) {
808 if (packing == GLSL_INTERFACE_PACKING_STD430)
809 this->uniforms[id].array_stride =
810 type->without_array()->std430_array_stride(row_major);
811 else
812 this->uniforms[id].array_stride =
813 glsl_align(type->without_array()->std140_size(row_major),
814 16);
815 } else {
816 this->uniforms[id].array_stride = 0;
817 }
818
819 if (type->without_array()->is_matrix()) {
820 const glsl_type *matrix = type->without_array();
821 const unsigned N = matrix->base_type == GLSL_TYPE_DOUBLE ? 8 : 4;
822 const unsigned items =
823 row_major ? matrix->matrix_columns : matrix->vector_elements;
824
825 assert(items <= 4);
826 if (packing == GLSL_INTERFACE_PACKING_STD430)
827 this->uniforms[id].matrix_stride = items < 3 ? items * N :
828 glsl_align(items * N, 16);
829 else
830 this->uniforms[id].matrix_stride = glsl_align(items * N, 16);
831 this->uniforms[id].row_major = row_major;
832 } else {
833 this->uniforms[id].matrix_stride = 0;
834 this->uniforms[id].row_major = false;
835 }
836 } else {
837 this->uniforms[id].block_index = -1;
838 this->uniforms[id].offset = -1;
839 this->uniforms[id].array_stride = -1;
840 this->uniforms[id].matrix_stride = -1;
841 this->uniforms[id].row_major = false;
842 }
843
844 this->values += values_for_type(type);
845 }
846
847 struct string_to_uint_map *map;
848
849 struct gl_uniform_storage *uniforms;
850 unsigned next_sampler;
851 unsigned next_image;
852 unsigned next_subroutine;
853
854 /**
855 * Field counter is used to take care that uniform structures
856 * with explicit locations get sequential locations.
857 */
858 unsigned field_counter;
859
860 /**
861 * Current variable being processed.
862 */
863 ir_variable *current_var;
864
865 /* Used to store the explicit location from current_var so that we can
866 * reuse the location field for storing the uniform slot id.
867 */
868 int explicit_location;
869
870 /* Stores total struct array elements including nested structs */
871 unsigned record_array_count;
872
873 /* Map for temporarily storing next sampler index when handling samplers in
874 * struct arrays.
875 */
876 struct string_to_uint_map *record_next_sampler;
877
878 public:
879 union gl_constant_value *values;
880
881 gl_texture_index targets[MAX_SAMPLERS];
882
883 /**
884 * Mask of samplers used by the current shader stage.
885 */
886 unsigned shader_samplers_used;
887
888 /**
889 * Mask of samplers used by the current shader stage for shadows.
890 */
891 unsigned shader_shadow_samplers;
892 };
893
894 /**
895 * Merges a uniform block into an array of uniform blocks that may or
896 * may not already contain a copy of it.
897 *
898 * Returns the index of the new block in the array.
899 */
900 int
901 link_cross_validate_uniform_block(void *mem_ctx,
902 struct gl_uniform_block **linked_blocks,
903 unsigned int *num_linked_blocks,
904 struct gl_uniform_block *new_block)
905 {
906 for (unsigned int i = 0; i < *num_linked_blocks; i++) {
907 struct gl_uniform_block *old_block = &(*linked_blocks)[i];
908
909 if (strcmp(old_block->Name, new_block->Name) == 0)
910 return link_uniform_blocks_are_compatible(old_block, new_block)
911 ? i : -1;
912 }
913
914 *linked_blocks = reralloc(mem_ctx, *linked_blocks,
915 struct gl_uniform_block,
916 *num_linked_blocks + 1);
917 int linked_block_index = (*num_linked_blocks)++;
918 struct gl_uniform_block *linked_block = &(*linked_blocks)[linked_block_index];
919
920 memcpy(linked_block, new_block, sizeof(*new_block));
921 linked_block->Uniforms = ralloc_array(*linked_blocks,
922 struct gl_uniform_buffer_variable,
923 linked_block->NumUniforms);
924
925 memcpy(linked_block->Uniforms,
926 new_block->Uniforms,
927 sizeof(*linked_block->Uniforms) * linked_block->NumUniforms);
928
929 for (unsigned int i = 0; i < linked_block->NumUniforms; i++) {
930 struct gl_uniform_buffer_variable *ubo_var =
931 &linked_block->Uniforms[i];
932
933 if (ubo_var->Name == ubo_var->IndexName) {
934 ubo_var->Name = ralloc_strdup(*linked_blocks, ubo_var->Name);
935 ubo_var->IndexName = ubo_var->Name;
936 } else {
937 ubo_var->Name = ralloc_strdup(*linked_blocks, ubo_var->Name);
938 ubo_var->IndexName = ralloc_strdup(*linked_blocks, ubo_var->IndexName);
939 }
940 }
941
942 return linked_block_index;
943 }
944
945 /**
946 * Walks the IR and update the references to uniform blocks in the
947 * ir_variables to point at linked shader's list (previously, they
948 * would point at the uniform block list in one of the pre-linked
949 * shaders).
950 */
951 static void
952 link_update_uniform_buffer_variables(struct gl_shader *shader)
953 {
954 foreach_in_list(ir_instruction, node, shader->ir) {
955 ir_variable *const var = node->as_variable();
956
957 if ((var == NULL) || !var->is_in_buffer_block())
958 continue;
959
960 assert(var->data.mode == ir_var_uniform ||
961 var->data.mode == ir_var_shader_storage);
962
963 if (var->is_interface_instance()) {
964 var->data.location = 0;
965 continue;
966 }
967
968 bool found = false;
969 char sentinel = '\0';
970
971 if (var->type->is_record()) {
972 sentinel = '.';
973 } else if (var->type->is_array() && (var->type->fields.array->is_array()
974 || var->type->without_array()->is_record())) {
975 sentinel = '[';
976 }
977
978 const unsigned l = strlen(var->name);
979 for (unsigned i = 0; i < shader->NumBufferInterfaceBlocks; i++) {
980 for (unsigned j = 0; j < shader->BufferInterfaceBlocks[i].NumUniforms; j++) {
981 if (sentinel) {
982 const char *begin = shader->BufferInterfaceBlocks[i].Uniforms[j].Name;
983 const char *end = strchr(begin, sentinel);
984
985 if (end == NULL)
986 continue;
987
988 if ((ptrdiff_t) l != (end - begin))
989 continue;
990
991 if (strncmp(var->name, begin, l) == 0) {
992 found = true;
993 var->data.location = j;
994 break;
995 }
996 } else if (!strcmp(var->name,
997 shader->BufferInterfaceBlocks[i].Uniforms[j].Name)) {
998 found = true;
999 var->data.location = j;
1000 break;
1001 }
1002 }
1003 if (found)
1004 break;
1005 }
1006 assert(found);
1007 }
1008 }
1009
1010 static void
1011 link_set_image_access_qualifiers(struct gl_shader_program *prog,
1012 gl_shader *sh, unsigned shader_stage,
1013 ir_variable *var, const glsl_type *type,
1014 char **name, size_t name_length)
1015 {
1016 /* Handle arrays of arrays */
1017 if (type->is_array() && type->fields.array->is_array()) {
1018 for (unsigned i = 0; i < type->length; i++) {
1019 size_t new_length = name_length;
1020
1021 /* Append the subscript to the current variable name */
1022 ralloc_asprintf_rewrite_tail(name, &new_length, "[%u]", i);
1023
1024 link_set_image_access_qualifiers(prog, sh, shader_stage, var,
1025 type->fields.array, name,
1026 new_length);
1027 }
1028 } else {
1029 unsigned id = 0;
1030 bool found = prog->UniformHash->get(id, *name);
1031 assert(found);
1032 (void) found;
1033 const gl_uniform_storage *storage = &prog->UniformStorage[id];
1034 const unsigned index = storage->opaque[shader_stage].index;
1035 const GLenum access = (var->data.image_read_only ? GL_READ_ONLY :
1036 var->data.image_write_only ? GL_WRITE_ONLY :
1037 GL_READ_WRITE);
1038
1039 for (unsigned j = 0; j < MAX2(1, storage->array_elements); ++j)
1040 sh->ImageAccess[index + j] = access;
1041 }
1042 }
1043
1044 /**
1045 * Combine the hidden uniform hash map with the uniform hash map so that the
1046 * hidden uniforms will be given indicies at the end of the uniform storage
1047 * array.
1048 */
1049 static void
1050 assign_hidden_uniform_slot_id(const char *name, unsigned hidden_id,
1051 void *closure)
1052 {
1053 count_uniform_size *uniform_size = (count_uniform_size *) closure;
1054 unsigned hidden_uniform_start = uniform_size->num_active_uniforms -
1055 uniform_size->num_hidden_uniforms;
1056
1057 uniform_size->map->put(hidden_uniform_start + hidden_id, name);
1058 }
1059
1060 /**
1061 * Search UniformRemapTable for empty block big enough to hold given uniform.
1062 * TODO Optimize this algorithm later if it turns out to be a major bottleneck.
1063 */
1064 static int
1065 find_empty_block(struct gl_shader_program *prog,
1066 struct gl_uniform_storage *uniform)
1067 {
1068 const unsigned entries = MAX2(1, uniform->array_elements);
1069 for (unsigned i = 0, j; i < prog->NumUniformRemapTable; i++) {
1070 /* We found empty space in UniformRemapTable. */
1071 if (prog->UniformRemapTable[i] == NULL) {
1072 for (j = i; j < entries && j < prog->NumUniformRemapTable; j++) {
1073 if (prog->UniformRemapTable[j] != NULL) {
1074 /* Entries do not fit in this space, continue searching
1075 * after this location.
1076 */
1077 i = j + 1;
1078 break;
1079 }
1080 }
1081 /* Entries fit, we can return this location. */
1082 if (i != j + 1) {
1083 return i;
1084 }
1085 }
1086 }
1087 return -1;
1088 }
1089
1090 void
1091 link_assign_uniform_locations(struct gl_shader_program *prog,
1092 unsigned int boolean_true,
1093 unsigned int max_locations)
1094 {
1095 ralloc_free(prog->UniformStorage);
1096 prog->UniformStorage = NULL;
1097 prog->NumUniformStorage = 0;
1098
1099 if (prog->UniformHash != NULL) {
1100 prog->UniformHash->clear();
1101 } else {
1102 prog->UniformHash = new string_to_uint_map;
1103 }
1104
1105 /* First pass: Count the uniform resources used by the user-defined
1106 * uniforms. While this happens, each active uniform will have an index
1107 * assigned to it.
1108 *
1109 * Note: this is *NOT* the index that is returned to the application by
1110 * glGetUniformLocation.
1111 */
1112 struct string_to_uint_map *hiddenUniforms = new string_to_uint_map;
1113 count_uniform_size uniform_size(prog->UniformHash, hiddenUniforms);
1114 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1115 struct gl_shader *sh = prog->_LinkedShaders[i];
1116
1117 if (sh == NULL)
1118 continue;
1119
1120 /* Uniforms that lack an initializer in the shader code have an initial
1121 * value of zero. This includes sampler uniforms.
1122 *
1123 * Page 24 (page 30 of the PDF) of the GLSL 1.20 spec says:
1124 *
1125 * "The link time initial value is either the value of the variable's
1126 * initializer, if present, or 0 if no initializer is present. Sampler
1127 * types cannot have initializers."
1128 */
1129 memset(sh->SamplerUnits, 0, sizeof(sh->SamplerUnits));
1130 memset(sh->ImageUnits, 0, sizeof(sh->ImageUnits));
1131
1132 link_update_uniform_buffer_variables(sh);
1133
1134 /* Reset various per-shader target counts.
1135 */
1136 uniform_size.start_shader();
1137
1138 foreach_in_list(ir_instruction, node, sh->ir) {
1139 ir_variable *const var = node->as_variable();
1140
1141 if ((var == NULL) || (var->data.mode != ir_var_uniform &&
1142 var->data.mode != ir_var_shader_storage))
1143 continue;
1144
1145 uniform_size.process(var);
1146 }
1147
1148 sh->num_samplers = uniform_size.num_shader_samplers;
1149 sh->NumImages = uniform_size.num_shader_images;
1150 sh->num_uniform_components = uniform_size.num_shader_uniform_components;
1151 sh->num_combined_uniform_components = sh->num_uniform_components;
1152
1153 for (unsigned i = 0; i < sh->NumBufferInterfaceBlocks; i++) {
1154 if (!sh->BufferInterfaceBlocks[i].IsShaderStorage) {
1155 sh->num_combined_uniform_components +=
1156 sh->BufferInterfaceBlocks[i].UniformBufferSize / 4;
1157 }
1158 }
1159 }
1160
1161 const unsigned num_uniforms = uniform_size.num_active_uniforms;
1162 const unsigned num_data_slots = uniform_size.num_values;
1163 const unsigned hidden_uniforms = uniform_size.num_hidden_uniforms;
1164
1165 /* assign hidden uniforms a slot id */
1166 hiddenUniforms->iterate(assign_hidden_uniform_slot_id, &uniform_size);
1167 delete hiddenUniforms;
1168
1169 /* On the outside chance that there were no uniforms, bail out.
1170 */
1171 if (num_uniforms == 0)
1172 return;
1173
1174 struct gl_uniform_storage *uniforms =
1175 rzalloc_array(prog, struct gl_uniform_storage, num_uniforms);
1176 union gl_constant_value *data =
1177 rzalloc_array(uniforms, union gl_constant_value, num_data_slots);
1178 #ifndef NDEBUG
1179 union gl_constant_value *data_end = &data[num_data_slots];
1180 #endif
1181
1182 parcel_out_uniform_storage parcel(prog->UniformHash, uniforms, data);
1183
1184 unsigned total_entries = 0;
1185
1186 /* Calculate amount of 'holes' left after explicit locations were
1187 * reserved from UniformRemapTable.
1188 */
1189 unsigned empty_locs = 0;
1190 for (unsigned i = 0; i < prog->NumUniformRemapTable; i++)
1191 if (prog->UniformRemapTable[i] == NULL)
1192 empty_locs++;
1193
1194 /* Add all the reserved explicit locations - empty locations in remap table. */
1195 if (prog->NumUniformRemapTable)
1196 total_entries = (prog->NumUniformRemapTable - 1) - empty_locs;
1197
1198 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1199 if (prog->_LinkedShaders[i] == NULL)
1200 continue;
1201
1202 parcel.start_shader((gl_shader_stage)i);
1203
1204 foreach_in_list(ir_instruction, node, prog->_LinkedShaders[i]->ir) {
1205 ir_variable *const var = node->as_variable();
1206
1207 if ((var == NULL) || (var->data.mode != ir_var_uniform &&
1208 var->data.mode != ir_var_shader_storage))
1209 continue;
1210
1211 parcel.set_and_process(prog, var);
1212 }
1213
1214 prog->_LinkedShaders[i]->active_samplers = parcel.shader_samplers_used;
1215 prog->_LinkedShaders[i]->shadow_samplers = parcel.shader_shadow_samplers;
1216
1217 STATIC_ASSERT(sizeof(prog->_LinkedShaders[i]->SamplerTargets) ==
1218 sizeof(parcel.targets));
1219 memcpy(prog->_LinkedShaders[i]->SamplerTargets, parcel.targets,
1220 sizeof(prog->_LinkedShaders[i]->SamplerTargets));
1221 }
1222
1223 /* Reserve all the explicit locations of the active uniforms. */
1224 for (unsigned i = 0; i < num_uniforms; i++) {
1225 if (uniforms[i].type->is_subroutine() ||
1226 uniforms[i].is_shader_storage)
1227 continue;
1228
1229 if (uniforms[i].remap_location != UNMAPPED_UNIFORM_LOC) {
1230 /* How many new entries for this uniform? */
1231 const unsigned entries = MAX2(1, uniforms[i].array_elements);
1232
1233 /* Set remap table entries point to correct gl_uniform_storage. */
1234 for (unsigned j = 0; j < entries; j++) {
1235 unsigned element_loc = uniforms[i].remap_location + j;
1236 assert(prog->UniformRemapTable[element_loc] ==
1237 INACTIVE_UNIFORM_EXPLICIT_LOCATION);
1238 prog->UniformRemapTable[element_loc] = &uniforms[i];
1239 }
1240 }
1241 }
1242
1243 /* Reserve locations for rest of the uniforms. */
1244 for (unsigned i = 0; i < num_uniforms; i++) {
1245
1246 if (uniforms[i].type->is_subroutine() ||
1247 uniforms[i].is_shader_storage)
1248 continue;
1249
1250 /* Built-in uniforms should not get any location. */
1251 if (uniforms[i].builtin)
1252 continue;
1253
1254 /* Explicit ones have been set already. */
1255 if (uniforms[i].remap_location != UNMAPPED_UNIFORM_LOC)
1256 continue;
1257
1258 /* how many new entries for this uniform? */
1259 const unsigned entries = MAX2(1, uniforms[i].array_elements);
1260
1261 /* Find UniformRemapTable for empty blocks where we can fit this uniform. */
1262 int chosen_location = -1;
1263
1264 if (empty_locs)
1265 chosen_location = find_empty_block(prog, &uniforms[i]);
1266
1267 if (chosen_location != -1) {
1268 empty_locs -= entries;
1269 } else {
1270 chosen_location = prog->NumUniformRemapTable;
1271
1272 /* Add new entries to the total amount of entries. */
1273 total_entries += entries;
1274
1275 /* resize remap table to fit new entries */
1276 prog->UniformRemapTable =
1277 reralloc(prog,
1278 prog->UniformRemapTable,
1279 gl_uniform_storage *,
1280 prog->NumUniformRemapTable + entries);
1281 prog->NumUniformRemapTable += entries;
1282 }
1283
1284 /* set pointers for this uniform */
1285 for (unsigned j = 0; j < entries; j++)
1286 prog->UniformRemapTable[chosen_location + j] = &uniforms[i];
1287
1288 /* set the base location in remap table for the uniform */
1289 uniforms[i].remap_location = chosen_location;
1290 }
1291
1292 /* Verify that total amount of entries for explicit and implicit locations
1293 * is less than MAX_UNIFORM_LOCATIONS.
1294 */
1295 if (total_entries >= max_locations) {
1296 linker_error(prog, "count of uniform locations >= MAX_UNIFORM_LOCATIONS"
1297 "(%u >= %u)", total_entries, max_locations);
1298 }
1299
1300 /* Reserve all the explicit locations of the active subroutine uniforms. */
1301 for (unsigned i = 0; i < num_uniforms; i++) {
1302 if (!uniforms[i].type->is_subroutine())
1303 continue;
1304
1305 if (uniforms[i].remap_location == UNMAPPED_UNIFORM_LOC)
1306 continue;
1307
1308 for (unsigned j = 0; j < MESA_SHADER_STAGES; j++) {
1309 struct gl_shader *sh = prog->_LinkedShaders[j];
1310 if (!sh)
1311 continue;
1312
1313 if (!uniforms[i].opaque[j].active)
1314 continue;
1315
1316 /* How many new entries for this uniform? */
1317 const unsigned entries = MAX2(1, uniforms[i].array_elements);
1318
1319 /* Set remap table entries point to correct gl_uniform_storage. */
1320 for (unsigned k = 0; k < entries; k++) {
1321 unsigned element_loc = uniforms[i].remap_location + k;
1322 assert(sh->SubroutineUniformRemapTable[element_loc] ==
1323 INACTIVE_UNIFORM_EXPLICIT_LOCATION);
1324 sh->SubroutineUniformRemapTable[element_loc] = &uniforms[i];
1325 }
1326 }
1327 }
1328
1329 /* reserve subroutine locations */
1330 for (unsigned i = 0; i < num_uniforms; i++) {
1331
1332 if (!uniforms[i].type->is_subroutine())
1333 continue;
1334 const unsigned entries = MAX2(1, uniforms[i].array_elements);
1335
1336 if (uniforms[i].remap_location != UNMAPPED_UNIFORM_LOC)
1337 continue;
1338 for (unsigned j = 0; j < MESA_SHADER_STAGES; j++) {
1339 struct gl_shader *sh = prog->_LinkedShaders[j];
1340 if (!sh)
1341 continue;
1342
1343 if (!uniforms[i].opaque[j].active)
1344 continue;
1345
1346 sh->SubroutineUniformRemapTable =
1347 reralloc(sh,
1348 sh->SubroutineUniformRemapTable,
1349 gl_uniform_storage *,
1350 sh->NumSubroutineUniformRemapTable + entries);
1351
1352 for (unsigned k = 0; k < entries; k++)
1353 sh->SubroutineUniformRemapTable[sh->NumSubroutineUniformRemapTable + k] = &uniforms[i];
1354 uniforms[i].remap_location = sh->NumSubroutineUniformRemapTable;
1355 sh->NumSubroutineUniformRemapTable += entries;
1356 }
1357 }
1358
1359 #ifndef NDEBUG
1360 for (unsigned i = 0; i < num_uniforms; i++) {
1361 assert(uniforms[i].storage != NULL || uniforms[i].builtin);
1362 }
1363
1364 assert(parcel.values == data_end);
1365 #endif
1366
1367 prog->NumUniformStorage = num_uniforms;
1368 prog->NumHiddenUniforms = hidden_uniforms;
1369 prog->UniformStorage = uniforms;
1370
1371 /**
1372 * Scan the program for image uniforms and store image unit access
1373 * information into the gl_shader data structure.
1374 */
1375 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1376 gl_shader *sh = prog->_LinkedShaders[i];
1377
1378 if (sh == NULL)
1379 continue;
1380
1381 foreach_in_list(ir_instruction, node, sh->ir) {
1382 ir_variable *var = node->as_variable();
1383
1384 if (var && var->data.mode == ir_var_uniform &&
1385 var->type->contains_image()) {
1386 char *name_copy = ralloc_strdup(NULL, var->name);
1387 link_set_image_access_qualifiers(prog, sh, i, var, var->type,
1388 &name_copy, strlen(var->name));
1389 ralloc_free(name_copy);
1390 }
1391 }
1392 }
1393
1394 link_set_uniform_initializers(prog, boolean_true);
1395
1396 return;
1397 }