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