glsl: Add std430 support to program_resource_visitor's member functions
[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 bool row_major =
81 var->data.matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR;
82
83 const unsigned packing = var->get_interface_type() ?
84 var->get_interface_type()->interface_packing :
85 var->type->interface_packing;
86
87 /* false is always passed for the row_major parameter to the other
88 * processing functions because no information is available to do
89 * otherwise. See the warning in linker.h.
90 */
91
92 /* Only strdup the name if we actually will need to modify it. */
93 if (var->data.from_named_ifc_block_array) {
94 /* lower_named_interface_blocks created this variable by lowering an
95 * interface block array to an array variable. For example if the
96 * original source code was:
97 *
98 * out Blk { vec4 bar } foo[3];
99 *
100 * Then the variable is now:
101 *
102 * out vec4 bar[3];
103 *
104 * We need to visit each array element using the names constructed like
105 * so:
106 *
107 * Blk[0].bar
108 * Blk[1].bar
109 * Blk[2].bar
110 */
111 assert(t->is_array());
112 const glsl_type *ifc_type = var->get_interface_type();
113 char *name = ralloc_strdup(NULL, ifc_type->name);
114 size_t name_length = strlen(name);
115 for (unsigned i = 0; i < t->length; i++) {
116 size_t new_length = name_length;
117 ralloc_asprintf_rewrite_tail(&name, &new_length, "[%u].%s", i,
118 var->name);
119 /* Note: row_major is only meaningful for uniform blocks, and
120 * lowering is only applied to non-uniform interface blocks, so we
121 * can safely pass false for row_major.
122 */
123 recursion(var->type, &name, new_length, row_major, NULL, packing,
124 false, record_array_count);
125 }
126 ralloc_free(name);
127 } else if (var->data.from_named_ifc_block_nonarray) {
128 /* lower_named_interface_blocks created this variable by lowering a
129 * named interface block (non-array) to an ordinary variable. For
130 * example if the original source code was:
131 *
132 * out Blk { vec4 bar } foo;
133 *
134 * Then the variable is now:
135 *
136 * out vec4 bar;
137 *
138 * We need to visit this variable using the name:
139 *
140 * Blk.bar
141 */
142 const glsl_type *ifc_type = var->get_interface_type();
143 char *name = ralloc_asprintf(NULL, "%s.%s", ifc_type->name, var->name);
144 /* Note: row_major is only meaningful for uniform blocks, and lowering
145 * is only applied to non-uniform interface blocks, so we can safely
146 * pass false for row_major.
147 */
148 recursion(var->type, &name, strlen(name), row_major, NULL, packing,
149 false, record_array_count);
150 ralloc_free(name);
151 } else if (t->without_array()->is_record()) {
152 char *name = ralloc_strdup(NULL, var->name);
153 recursion(var->type, &name, strlen(name), row_major, NULL, packing,
154 false, record_array_count);
155 ralloc_free(name);
156 } else if (t->is_interface()) {
157 char *name = ralloc_strdup(NULL, var->type->name);
158 recursion(var->type, &name, strlen(name), row_major, NULL, packing,
159 false, record_array_count);
160 ralloc_free(name);
161 } else if (t->is_array() && t->fields.array->is_interface()) {
162 char *name = ralloc_strdup(NULL, var->type->fields.array->name);
163 recursion(var->type, &name, strlen(name), row_major, NULL, packing,
164 false, record_array_count);
165 ralloc_free(name);
166 } else {
167 this->visit_field(t, var->name, row_major, NULL, packing, false);
168 }
169 }
170
171 void
172 program_resource_visitor::recursion(const glsl_type *t, char **name,
173 size_t name_length, bool row_major,
174 const glsl_type *record_type,
175 const unsigned packing,
176 bool last_field,
177 unsigned record_array_count)
178 {
179 /* Records need to have each field processed individually.
180 *
181 * Arrays of records need to have each array element processed
182 * individually, then each field of the resulting array elements processed
183 * individually.
184 */
185 if (t->is_record() || t->is_interface()) {
186 if (record_type == NULL && t->is_record())
187 record_type = t;
188
189 if (t->is_record())
190 this->enter_record(t, *name, row_major, packing);
191
192 for (unsigned i = 0; i < t->length; i++) {
193 const char *field = t->fields.structure[i].name;
194 size_t new_length = name_length;
195
196 if (t->fields.structure[i].type->is_record())
197 this->visit_field(&t->fields.structure[i]);
198
199 /* Append '.field' to the current variable name. */
200 if (name_length == 0) {
201 ralloc_asprintf_rewrite_tail(name, &new_length, "%s", field);
202 } else {
203 ralloc_asprintf_rewrite_tail(name, &new_length, ".%s", field);
204 }
205
206 /* The layout of structures at the top level of the block is set
207 * during parsing. For matrices contained in multiple levels of
208 * structures in the block, the inner structures have no layout.
209 * These cases must potentially inherit the layout from the outer
210 * levels.
211 */
212 bool field_row_major = row_major;
213 const enum glsl_matrix_layout matrix_layout =
214 glsl_matrix_layout(t->fields.structure[i].matrix_layout);
215 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
216 field_row_major = true;
217 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
218 field_row_major = false;
219 }
220
221 recursion(t->fields.structure[i].type, name, new_length,
222 field_row_major,
223 record_type,
224 packing,
225 (i + 1) == t->length, record_array_count);
226
227 /* Only the first leaf-field of the record gets called with the
228 * record type pointer.
229 */
230 record_type = NULL;
231 }
232
233 if (t->is_record()) {
234 (*name)[name_length] = '\0';
235 this->leave_record(t, *name, row_major, packing);
236 }
237 } else if (t->is_array() && (t->fields.array->is_record()
238 || t->fields.array->is_interface())) {
239 if (record_type == NULL && t->fields.array->is_record())
240 record_type = t->fields.array;
241
242 unsigned length = t->length;
243 /* Shader storage block unsized arrays: add subscript [0] to variable
244 * names */
245 if (t->is_unsized_array())
246 length = 1;
247
248 record_array_count *= length;
249
250 for (unsigned i = 0; i < length; i++) {
251 size_t new_length = name_length;
252
253 /* Append the subscript to the current variable name */
254 ralloc_asprintf_rewrite_tail(name, &new_length, "[%u]", i);
255
256 recursion(t->fields.array, name, new_length, row_major,
257 record_type,
258 packing,
259 (i + 1) == t->length, record_array_count);
260
261 /* Only the first leaf-field of the record gets called with the
262 * record type pointer.
263 */
264 record_type = NULL;
265 }
266 } else {
267 this->set_record_array_count(record_array_count);
268 this->visit_field(t, *name, row_major, record_type, packing, last_field);
269 }
270 }
271
272 void
273 program_resource_visitor::visit_field(const glsl_type *type, const char *name,
274 bool row_major,
275 const glsl_type *,
276 const unsigned,
277 bool /* last_field */)
278 {
279 visit_field(type, name, row_major);
280 }
281
282 void
283 program_resource_visitor::visit_field(const glsl_struct_field *field)
284 {
285 (void) field;
286 /* empty */
287 }
288
289 void
290 program_resource_visitor::enter_record(const glsl_type *, const char *, bool,
291 const unsigned)
292 {
293 }
294
295 void
296 program_resource_visitor::leave_record(const glsl_type *, const char *, bool,
297 const unsigned)
298 {
299 }
300
301 void
302 program_resource_visitor::set_record_array_count(unsigned)
303 {
304 }
305
306 namespace {
307
308 /**
309 * Class to help calculate the storage requirements for a set of uniforms
310 *
311 * As uniforms are added to the active set the number of active uniforms and
312 * the storage requirements for those uniforms are accumulated. The active
313 * uniforms are added to the hash table supplied to the constructor.
314 *
315 * If the same uniform is added multiple times (i.e., once for each shader
316 * target), it will only be accounted once.
317 */
318 class count_uniform_size : public program_resource_visitor {
319 public:
320 count_uniform_size(struct string_to_uint_map *map,
321 struct string_to_uint_map *hidden_map)
322 : num_active_uniforms(0), num_hidden_uniforms(0), num_values(0),
323 num_shader_samplers(0), num_shader_images(0),
324 num_shader_uniform_components(0), num_shader_subroutines(0),
325 is_ubo_var(false), map(map), 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 if (var->is_interface_instance())
343 program_resource_visitor::process(var->get_interface_type(),
344 var->get_interface_type()->name);
345 else
346 program_resource_visitor::process(var);
347 }
348
349 /**
350 * Total number of active uniforms counted
351 */
352 unsigned num_active_uniforms;
353
354 unsigned num_hidden_uniforms;
355
356 /**
357 * Number of data values required to back the storage for the active uniforms
358 */
359 unsigned num_values;
360
361 /**
362 * Number of samplers used
363 */
364 unsigned num_shader_samplers;
365
366 /**
367 * Number of images used
368 */
369 unsigned num_shader_images;
370
371 /**
372 * Number of uniforms used in the current shader
373 */
374 unsigned num_shader_uniform_components;
375
376 /**
377 * Number of subroutine uniforms used
378 */
379 unsigned num_shader_subroutines;
380
381 bool is_ubo_var;
382
383 struct string_to_uint_map *map;
384
385 private:
386 virtual void visit_field(const glsl_type *type, const char *name,
387 bool row_major)
388 {
389 assert(!type->without_array()->is_record());
390 assert(!type->without_array()->is_interface());
391
392 (void) row_major;
393
394 /* Count the number of samplers regardless of whether the uniform is
395 * already in the hash table. The hash table prevents adding the same
396 * uniform for multiple shader targets, but in this case we want to
397 * count it for each shader target.
398 */
399 const unsigned values = values_for_type(type);
400 if (type->contains_subroutine()) {
401 this->num_shader_subroutines += values;
402 } else if (type->contains_sampler()) {
403 this->num_shader_samplers += values;
404 } else if (type->contains_image()) {
405 this->num_shader_images += values;
406
407 /* As drivers are likely to represent image uniforms as
408 * scalar indices, count them against the limit of uniform
409 * components in the default block. The spec allows image
410 * uniforms to use up no more than one scalar slot.
411 */
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)
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->NumUniformBlocks; i++) {
504 if (strncmp(var->get_interface_type()->name,
505 prog->UniformBlocks[i].Name,
506 l) == 0
507 && prog->UniformBlocks[i].Name[l] == '[') {
508 ubo_block_index = i;
509 break;
510 }
511 }
512 } else {
513 for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
514 if (strcmp(var->get_interface_type()->name,
515 prog->UniformBlocks[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->UniformBlocks[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->sampler[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->sampler[shader_type].index = index;
590 index = inner_array_size + uniform->sampler[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->sampler[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->sampler[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->sampler[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->sampler[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 } else {
636 uniform->sampler[shader_type].index = ~0;
637 uniform->sampler[shader_type].active = false;
638 }
639 }
640
641 void handle_images(const glsl_type *base_type,
642 struct gl_uniform_storage *uniform)
643 {
644 if (base_type->is_image()) {
645 uniform->image[shader_type].index = this->next_image;
646 uniform->image[shader_type].active = true;
647
648 /* Increment the image index by 1 for non-arrays and by the
649 * number of array elements for arrays.
650 */
651 this->next_image += MAX2(1, uniform->array_elements);
652
653 } else {
654 uniform->image[shader_type].index = ~0;
655 uniform->image[shader_type].active = false;
656 }
657 }
658
659 void handle_subroutines(const glsl_type *base_type,
660 struct gl_uniform_storage *uniform)
661 {
662 if (base_type->is_subroutine()) {
663 uniform->subroutine[shader_type].index = this->next_subroutine;
664 uniform->subroutine[shader_type].active = true;
665
666 /* Increment the subroutine index by 1 for non-arrays and by the
667 * number of array elements for arrays.
668 */
669 this->next_subroutine += MAX2(1, uniform->array_elements);
670
671 } else {
672 uniform->subroutine[shader_type].index = ~0;
673 uniform->subroutine[shader_type].active = false;
674 }
675 }
676
677 virtual void set_record_array_count(unsigned record_array_count)
678 {
679 this->record_array_count = record_array_count;
680 }
681
682 virtual void visit_field(const glsl_type *type, const char *name,
683 bool row_major)
684 {
685 (void) type;
686 (void) name;
687 (void) row_major;
688 assert(!"Should not get here.");
689 }
690
691 virtual void enter_record(const glsl_type *type, const char *,
692 bool row_major, const unsigned packing) {
693 assert(type->is_record());
694 if (this->ubo_block_index == -1)
695 return;
696 if (packing == GLSL_INTERFACE_PACKING_STD430)
697 this->ubo_byte_offset = glsl_align(
698 this->ubo_byte_offset, type->std430_base_alignment(row_major));
699 else
700 this->ubo_byte_offset = glsl_align(
701 this->ubo_byte_offset, type->std140_base_alignment(row_major));
702 }
703
704 virtual void leave_record(const glsl_type *type, const char *,
705 bool row_major, const unsigned packing) {
706 assert(type->is_record());
707 if (this->ubo_block_index == -1)
708 return;
709 if (packing == GLSL_INTERFACE_PACKING_STD430)
710 this->ubo_byte_offset = glsl_align(
711 this->ubo_byte_offset, type->std430_base_alignment(row_major));
712 else
713 this->ubo_byte_offset = glsl_align(
714 this->ubo_byte_offset, type->std140_base_alignment(row_major));
715 }
716
717 virtual void visit_field(const glsl_type *type, const char *name,
718 bool row_major, const glsl_type *record_type,
719 const unsigned packing,
720 bool /* last_field */)
721 {
722 assert(!type->without_array()->is_record());
723 assert(!type->without_array()->is_interface());
724
725 unsigned id;
726 bool found = this->map->get(id, name);
727 assert(found);
728
729 if (!found)
730 return;
731
732 const glsl_type *base_type;
733 if (type->is_array()) {
734 this->uniforms[id].array_elements = type->length;
735 base_type = type->fields.array;
736 } else {
737 this->uniforms[id].array_elements = 0;
738 base_type = type;
739 }
740
741 /* This assigns uniform indices to sampler and image uniforms. */
742 handle_samplers(base_type, &this->uniforms[id], name);
743 handle_images(base_type, &this->uniforms[id]);
744 handle_subroutines(base_type, &this->uniforms[id]);
745
746 /* For array of arrays or struct arrays the base location may have
747 * already been set so dont set it again.
748 */
749 if (ubo_block_index == -1 && current_var->data.location == -1) {
750 current_var->data.location = id;
751 }
752
753 /* If there is already storage associated with this uniform or if the
754 * uniform is set as builtin, it means that it was set while processing
755 * an earlier shader stage. For example, we may be processing the
756 * uniform in the fragment shader, but the uniform was already processed
757 * in the vertex shader.
758 */
759 if (this->uniforms[id].storage != NULL || this->uniforms[id].builtin) {
760 return;
761 }
762
763 /* Assign explicit locations. */
764 if (current_var->data.explicit_location) {
765 /* Set sequential locations for struct fields. */
766 if (record_type != NULL) {
767 const unsigned entries = MAX2(1, this->uniforms[id].array_elements);
768 this->uniforms[id].remap_location =
769 this->explicit_location + field_counter;
770 field_counter += entries;
771 } else {
772 this->uniforms[id].remap_location = this->explicit_location;
773 }
774 } else {
775 /* Initialize to to indicate that no location is set */
776 this->uniforms[id].remap_location = UNMAPPED_UNIFORM_LOC;
777 }
778
779 this->uniforms[id].name = ralloc_strdup(this->uniforms, name);
780 this->uniforms[id].type = base_type;
781 this->uniforms[id].initialized = 0;
782 this->uniforms[id].num_driver_storage = 0;
783 this->uniforms[id].driver_storage = NULL;
784 this->uniforms[id].atomic_buffer_index = -1;
785 this->uniforms[id].hidden =
786 current_var->data.how_declared == ir_var_hidden;
787 this->uniforms[id].builtin = is_gl_identifier(name);
788
789 /* Do not assign storage if the uniform is builtin */
790 if (!this->uniforms[id].builtin)
791 this->uniforms[id].storage = this->values;
792
793 if (this->ubo_block_index != -1) {
794 this->uniforms[id].block_index = this->ubo_block_index;
795
796 unsigned alignment = type->std140_base_alignment(row_major);
797 if (packing == GLSL_INTERFACE_PACKING_STD430)
798 alignment = type->std430_base_alignment(row_major);
799 this->ubo_byte_offset = glsl_align(this->ubo_byte_offset, alignment);
800 this->uniforms[id].offset = this->ubo_byte_offset;
801 if (packing == GLSL_INTERFACE_PACKING_STD430)
802 this->ubo_byte_offset += type->std430_size(row_major);
803 else
804 this->ubo_byte_offset += type->std140_size(row_major);
805
806 if (type->is_array()) {
807 if (packing == GLSL_INTERFACE_PACKING_STD430)
808 this->uniforms[id].array_stride =
809 type->fields.array->std430_array_stride(row_major);
810 else
811 this->uniforms[id].array_stride =
812 glsl_align(type->fields.array->std140_size(row_major), 16);
813 } else {
814 this->uniforms[id].array_stride = 0;
815 }
816
817 if (type->without_array()->is_matrix()) {
818 const glsl_type *matrix = type->without_array();
819 const unsigned N = matrix->base_type == GLSL_TYPE_DOUBLE ? 8 : 4;
820 const unsigned items = row_major ? matrix->matrix_columns : matrix->vector_elements;
821
822 assert(items <= 4);
823 if (packing == GLSL_INTERFACE_PACKING_STD430)
824 this->uniforms[id].matrix_stride = items < 3 ? items * N :
825 glsl_align(items * N, 16);
826 else
827 this->uniforms[id].matrix_stride = glsl_align(items * N, 16);
828 this->uniforms[id].row_major = row_major;
829 } else {
830 this->uniforms[id].matrix_stride = 0;
831 this->uniforms[id].row_major = false;
832 }
833 } else {
834 this->uniforms[id].block_index = -1;
835 this->uniforms[id].offset = -1;
836 this->uniforms[id].array_stride = -1;
837 this->uniforms[id].matrix_stride = -1;
838 this->uniforms[id].row_major = false;
839 }
840
841 this->values += values_for_type(type);
842 }
843
844 struct string_to_uint_map *map;
845
846 struct gl_uniform_storage *uniforms;
847 unsigned next_sampler;
848 unsigned next_image;
849 unsigned next_subroutine;
850
851 /**
852 * Field counter is used to take care that uniform structures
853 * with explicit locations get sequential locations.
854 */
855 unsigned field_counter;
856
857 /**
858 * Current variable being processed.
859 */
860 ir_variable *current_var;
861
862 /* Used to store the explicit location from current_var so that we can
863 * reuse the location field for storing the uniform slot id.
864 */
865 int explicit_location;
866
867 /* Stores total struct array elements including nested structs */
868 unsigned record_array_count;
869
870 /* Map for temporarily storing next sampler index when handling samplers in
871 * struct arrays.
872 */
873 struct string_to_uint_map *record_next_sampler;
874
875 public:
876 union gl_constant_value *values;
877
878 gl_texture_index targets[MAX_SAMPLERS];
879
880 /**
881 * Mask of samplers used by the current shader stage.
882 */
883 unsigned shader_samplers_used;
884
885 /**
886 * Mask of samplers used by the current shader stage for shadows.
887 */
888 unsigned shader_shadow_samplers;
889 };
890
891 /**
892 * Merges a uniform block into an array of uniform blocks that may or
893 * may not already contain a copy of it.
894 *
895 * Returns the index of the new block in the array.
896 */
897 int
898 link_cross_validate_uniform_block(void *mem_ctx,
899 struct gl_uniform_block **linked_blocks,
900 unsigned int *num_linked_blocks,
901 struct gl_uniform_block *new_block)
902 {
903 for (unsigned int i = 0; i < *num_linked_blocks; i++) {
904 struct gl_uniform_block *old_block = &(*linked_blocks)[i];
905
906 if (strcmp(old_block->Name, new_block->Name) == 0)
907 return link_uniform_blocks_are_compatible(old_block, new_block)
908 ? i : -1;
909 }
910
911 *linked_blocks = reralloc(mem_ctx, *linked_blocks,
912 struct gl_uniform_block,
913 *num_linked_blocks + 1);
914 int linked_block_index = (*num_linked_blocks)++;
915 struct gl_uniform_block *linked_block = &(*linked_blocks)[linked_block_index];
916
917 memcpy(linked_block, new_block, sizeof(*new_block));
918 linked_block->Uniforms = ralloc_array(*linked_blocks,
919 struct gl_uniform_buffer_variable,
920 linked_block->NumUniforms);
921
922 memcpy(linked_block->Uniforms,
923 new_block->Uniforms,
924 sizeof(*linked_block->Uniforms) * linked_block->NumUniforms);
925
926 for (unsigned int i = 0; i < linked_block->NumUniforms; i++) {
927 struct gl_uniform_buffer_variable *ubo_var =
928 &linked_block->Uniforms[i];
929
930 if (ubo_var->Name == ubo_var->IndexName) {
931 ubo_var->Name = ralloc_strdup(*linked_blocks, ubo_var->Name);
932 ubo_var->IndexName = ubo_var->Name;
933 } else {
934 ubo_var->Name = ralloc_strdup(*linked_blocks, ubo_var->Name);
935 ubo_var->IndexName = ralloc_strdup(*linked_blocks, ubo_var->IndexName);
936 }
937 }
938
939 return linked_block_index;
940 }
941
942 /**
943 * Walks the IR and update the references to uniform blocks in the
944 * ir_variables to point at linked shader's list (previously, they
945 * would point at the uniform block list in one of the pre-linked
946 * shaders).
947 */
948 static void
949 link_update_uniform_buffer_variables(struct gl_shader *shader)
950 {
951 foreach_in_list(ir_instruction, node, shader->ir) {
952 ir_variable *const var = node->as_variable();
953
954 if ((var == NULL) || !var->is_in_buffer_block())
955 continue;
956
957 assert(var->data.mode == ir_var_uniform ||
958 var->data.mode == ir_var_shader_storage);
959
960 if (var->is_interface_instance()) {
961 var->data.location = 0;
962 continue;
963 }
964
965 bool found = false;
966 char sentinel = '\0';
967
968 if (var->type->is_record()) {
969 sentinel = '.';
970 } else if (var->type->is_array()
971 && var->type->fields.array->is_record()) {
972 sentinel = '[';
973 }
974
975 const unsigned l = strlen(var->name);
976 for (unsigned i = 0; i < shader->NumUniformBlocks; i++) {
977 for (unsigned j = 0; j < shader->UniformBlocks[i].NumUniforms; j++) {
978 if (sentinel) {
979 const char *begin = shader->UniformBlocks[i].Uniforms[j].Name;
980 const char *end = strchr(begin, sentinel);
981
982 if (end == NULL)
983 continue;
984
985 if ((ptrdiff_t) l != (end - begin))
986 continue;
987
988 if (strncmp(var->name, begin, l) == 0) {
989 found = true;
990 var->data.location = j;
991 break;
992 }
993 } else if (!strcmp(var->name,
994 shader->UniformBlocks[i].Uniforms[j].Name)) {
995 found = true;
996 var->data.location = j;
997 break;
998 }
999 }
1000 if (found)
1001 break;
1002 }
1003 assert(found);
1004 }
1005 }
1006
1007 /**
1008 * Scan the program for image uniforms and store image unit access
1009 * information into the gl_shader data structure.
1010 */
1011 static void
1012 link_set_image_access_qualifiers(struct gl_shader_program *prog)
1013 {
1014 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1015 gl_shader *sh = prog->_LinkedShaders[i];
1016
1017 if (sh == NULL)
1018 continue;
1019
1020 foreach_in_list(ir_instruction, node, sh->ir) {
1021 ir_variable *var = node->as_variable();
1022
1023 if (var && var->data.mode == ir_var_uniform &&
1024 var->type->contains_image()) {
1025 unsigned id = 0;
1026 bool found = prog->UniformHash->get(id, var->name);
1027 assert(found);
1028 (void) found;
1029 const gl_uniform_storage *storage = &prog->UniformStorage[id];
1030 const unsigned index = storage->image[i].index;
1031 const GLenum access = (var->data.image_read_only ? GL_READ_ONLY :
1032 var->data.image_write_only ? GL_WRITE_ONLY :
1033 GL_READ_WRITE);
1034
1035 for (unsigned j = 0; j < MAX2(1, storage->array_elements); ++j)
1036 sh->ImageAccess[index + j] = access;
1037 }
1038 }
1039 }
1040 }
1041
1042 /**
1043 * Combine the hidden uniform hash map with the uniform hash map so that the
1044 * hidden uniforms will be given indicies at the end of the uniform storage
1045 * array.
1046 */
1047 static void
1048 assign_hidden_uniform_slot_id(const char *name, unsigned hidden_id,
1049 void *closure)
1050 {
1051 count_uniform_size *uniform_size = (count_uniform_size *) closure;
1052 unsigned hidden_uniform_start = uniform_size->num_active_uniforms -
1053 uniform_size->num_hidden_uniforms;
1054
1055 uniform_size->map->put(hidden_uniform_start + hidden_id, name);
1056 }
1057
1058 void
1059 link_assign_uniform_locations(struct gl_shader_program *prog,
1060 unsigned int boolean_true)
1061 {
1062 ralloc_free(prog->UniformStorage);
1063 prog->UniformStorage = NULL;
1064 prog->NumUniformStorage = 0;
1065
1066 if (prog->UniformHash != NULL) {
1067 prog->UniformHash->clear();
1068 } else {
1069 prog->UniformHash = new string_to_uint_map;
1070 }
1071
1072 /* First pass: Count the uniform resources used by the user-defined
1073 * uniforms. While this happens, each active uniform will have an index
1074 * assigned to it.
1075 *
1076 * Note: this is *NOT* the index that is returned to the application by
1077 * glGetUniformLocation.
1078 */
1079 struct string_to_uint_map *hiddenUniforms = new string_to_uint_map;
1080 count_uniform_size uniform_size(prog->UniformHash, hiddenUniforms);
1081 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1082 struct gl_shader *sh = prog->_LinkedShaders[i];
1083
1084 if (sh == NULL)
1085 continue;
1086
1087 /* Uniforms that lack an initializer in the shader code have an initial
1088 * value of zero. This includes sampler uniforms.
1089 *
1090 * Page 24 (page 30 of the PDF) of the GLSL 1.20 spec says:
1091 *
1092 * "The link time initial value is either the value of the variable's
1093 * initializer, if present, or 0 if no initializer is present. Sampler
1094 * types cannot have initializers."
1095 */
1096 memset(sh->SamplerUnits, 0, sizeof(sh->SamplerUnits));
1097 memset(sh->ImageUnits, 0, sizeof(sh->ImageUnits));
1098
1099 link_update_uniform_buffer_variables(sh);
1100
1101 /* Reset various per-shader target counts.
1102 */
1103 uniform_size.start_shader();
1104
1105 foreach_in_list(ir_instruction, node, sh->ir) {
1106 ir_variable *const var = node->as_variable();
1107
1108 if ((var == NULL) || (var->data.mode != ir_var_uniform &&
1109 var->data.mode != ir_var_shader_storage))
1110 continue;
1111
1112 uniform_size.process(var);
1113 }
1114
1115 sh->num_samplers = uniform_size.num_shader_samplers;
1116 sh->NumImages = uniform_size.num_shader_images;
1117 sh->num_uniform_components = uniform_size.num_shader_uniform_components;
1118 sh->num_combined_uniform_components = sh->num_uniform_components;
1119
1120 for (unsigned i = 0; i < sh->NumUniformBlocks; i++) {
1121 sh->num_combined_uniform_components +=
1122 sh->UniformBlocks[i].UniformBufferSize / 4;
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 /* On the outside chance that there were no uniforms, bail out.
1131 */
1132 if (num_uniforms == 0)
1133 return;
1134
1135 /* assign hidden uniforms a slot id */
1136 hiddenUniforms->iterate(assign_hidden_uniform_slot_id, &uniform_size);
1137 delete hiddenUniforms;
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 && var->data.mode != ir_var_shader_storage))
1159 continue;
1160
1161 parcel.set_and_process(prog, var);
1162 }
1163
1164 prog->_LinkedShaders[i]->active_samplers = parcel.shader_samplers_used;
1165 prog->_LinkedShaders[i]->shadow_samplers = parcel.shader_shadow_samplers;
1166
1167 STATIC_ASSERT(sizeof(prog->_LinkedShaders[i]->SamplerTargets) == sizeof(parcel.targets));
1168 memcpy(prog->_LinkedShaders[i]->SamplerTargets, parcel.targets,
1169 sizeof(prog->_LinkedShaders[i]->SamplerTargets));
1170 }
1171
1172 /* Reserve all the explicit locations of the active uniforms. */
1173 for (unsigned i = 0; i < num_uniforms; i++) {
1174 if (uniforms[i].type->is_subroutine())
1175 continue;
1176
1177 if (uniforms[i].remap_location != UNMAPPED_UNIFORM_LOC) {
1178 /* How many new entries for this uniform? */
1179 const unsigned entries = MAX2(1, uniforms[i].array_elements);
1180
1181 /* Set remap table entries point to correct gl_uniform_storage. */
1182 for (unsigned j = 0; j < entries; j++) {
1183 unsigned element_loc = uniforms[i].remap_location + j;
1184 assert(prog->UniformRemapTable[element_loc] ==
1185 INACTIVE_UNIFORM_EXPLICIT_LOCATION);
1186 prog->UniformRemapTable[element_loc] = &uniforms[i];
1187 }
1188 }
1189 }
1190
1191 /* Reserve locations for rest of the uniforms. */
1192 for (unsigned i = 0; i < num_uniforms; i++) {
1193
1194 if (uniforms[i].type->is_subroutine())
1195 continue;
1196 /* Built-in uniforms should not get any location. */
1197 if (uniforms[i].builtin)
1198 continue;
1199
1200 /* Explicit ones have been set already. */
1201 if (uniforms[i].remap_location != UNMAPPED_UNIFORM_LOC)
1202 continue;
1203
1204 /* how many new entries for this uniform? */
1205 const unsigned entries = MAX2(1, uniforms[i].array_elements);
1206
1207 /* resize remap table to fit new entries */
1208 prog->UniformRemapTable =
1209 reralloc(prog,
1210 prog->UniformRemapTable,
1211 gl_uniform_storage *,
1212 prog->NumUniformRemapTable + entries);
1213
1214 /* set pointers for this uniform */
1215 for (unsigned j = 0; j < entries; j++)
1216 prog->UniformRemapTable[prog->NumUniformRemapTable+j] = &uniforms[i];
1217
1218 /* set the base location in remap table for the uniform */
1219 uniforms[i].remap_location = prog->NumUniformRemapTable;
1220
1221 prog->NumUniformRemapTable += entries;
1222 }
1223
1224 /* Reserve all the explicit locations of the active subroutine uniforms. */
1225 for (unsigned i = 0; i < num_uniforms; i++) {
1226 if (!uniforms[i].type->is_subroutine())
1227 continue;
1228
1229 if (uniforms[i].remap_location == UNMAPPED_UNIFORM_LOC)
1230 continue;
1231
1232 for (unsigned j = 0; j < MESA_SHADER_STAGES; j++) {
1233 struct gl_shader *sh = prog->_LinkedShaders[j];
1234 if (!sh)
1235 continue;
1236
1237 if (!uniforms[i].subroutine[j].active)
1238 continue;
1239
1240 /* How many new entries for this uniform? */
1241 const unsigned entries = MAX2(1, uniforms[i].array_elements);
1242
1243 /* Set remap table entries point to correct gl_uniform_storage. */
1244 for (unsigned k = 0; k < entries; k++) {
1245 unsigned element_loc = uniforms[i].remap_location + k;
1246 assert(sh->SubroutineUniformRemapTable[element_loc] ==
1247 INACTIVE_UNIFORM_EXPLICIT_LOCATION);
1248 sh->SubroutineUniformRemapTable[element_loc] = &uniforms[i];
1249 }
1250 }
1251 }
1252
1253 /* reserve subroutine locations */
1254 for (unsigned i = 0; i < num_uniforms; i++) {
1255
1256 if (!uniforms[i].type->is_subroutine())
1257 continue;
1258 const unsigned entries = MAX2(1, uniforms[i].array_elements);
1259
1260 if (uniforms[i].remap_location != UNMAPPED_UNIFORM_LOC)
1261 continue;
1262 for (unsigned j = 0; j < MESA_SHADER_STAGES; j++) {
1263 struct gl_shader *sh = prog->_LinkedShaders[j];
1264 if (!sh)
1265 continue;
1266
1267 if (!uniforms[i].subroutine[j].active)
1268 continue;
1269
1270 sh->SubroutineUniformRemapTable =
1271 reralloc(sh,
1272 sh->SubroutineUniformRemapTable,
1273 gl_uniform_storage *,
1274 sh->NumSubroutineUniformRemapTable + entries);
1275
1276 for (unsigned k = 0; k < entries; k++)
1277 sh->SubroutineUniformRemapTable[sh->NumSubroutineUniformRemapTable + k] = &uniforms[i];
1278 uniforms[i].remap_location = sh->NumSubroutineUniformRemapTable;
1279 sh->NumSubroutineUniformRemapTable += entries;
1280 }
1281 }
1282
1283 #ifndef NDEBUG
1284 for (unsigned i = 0; i < num_uniforms; i++) {
1285 assert(uniforms[i].storage != NULL || uniforms[i].builtin);
1286 }
1287
1288 assert(parcel.values == data_end);
1289 #endif
1290
1291 prog->NumUniformStorage = num_uniforms;
1292 prog->NumHiddenUniforms = hidden_uniforms;
1293 prog->UniformStorage = uniforms;
1294
1295 link_set_image_access_qualifiers(prog);
1296 link_set_uniform_initializers(prog, boolean_true);
1297
1298 return;
1299 }