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