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