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