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