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