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