a6683a09b2e1205e503a3e19b0304cc1c1abedba
[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 if (this->ubo_block_index != -1) {
589 this->uniforms[id].block_index = this->ubo_block_index;
590
591 const unsigned alignment = record_type
592 ? record_type->std140_base_alignment(row_major)
593 : type->std140_base_alignment(row_major);
594 this->ubo_byte_offset = glsl_align(this->ubo_byte_offset, alignment);
595 this->uniforms[id].offset = this->ubo_byte_offset;
596 this->ubo_byte_offset += type->std140_size(row_major);
597
598 if (last_field)
599 this->ubo_byte_offset = glsl_align(this->ubo_byte_offset, 16);
600
601 if (type->is_array()) {
602 this->uniforms[id].array_stride =
603 glsl_align(type->fields.array->std140_size(row_major), 16);
604 } else {
605 this->uniforms[id].array_stride = 0;
606 }
607
608 if (type->without_array()->is_matrix()) {
609 this->uniforms[id].matrix_stride = 16;
610 this->uniforms[id].row_major = row_major;
611 } else {
612 this->uniforms[id].matrix_stride = 0;
613 this->uniforms[id].row_major = false;
614 }
615 } else {
616 this->uniforms[id].block_index = -1;
617 this->uniforms[id].offset = -1;
618 this->uniforms[id].array_stride = -1;
619 this->uniforms[id].matrix_stride = -1;
620 this->uniforms[id].row_major = false;
621 }
622
623 this->values += values_for_type(type);
624 }
625
626 struct string_to_uint_map *map;
627
628 struct gl_uniform_storage *uniforms;
629 unsigned next_sampler;
630 unsigned next_image;
631
632 public:
633 union gl_constant_value *values;
634
635 gl_texture_index targets[MAX_SAMPLERS];
636
637 /**
638 * Current variable being processed.
639 */
640 ir_variable *current_var;
641
642 /**
643 * Field counter is used to take care that uniform structures
644 * with explicit locations get sequential locations.
645 */
646 unsigned field_counter;
647
648 /**
649 * Mask of samplers used by the current shader stage.
650 */
651 unsigned shader_samplers_used;
652
653 /**
654 * Mask of samplers used by the current shader stage for shadows.
655 */
656 unsigned shader_shadow_samplers;
657 };
658
659 /**
660 * Merges a uniform block into an array of uniform blocks that may or
661 * may not already contain a copy of it.
662 *
663 * Returns the index of the new block in the array.
664 */
665 int
666 link_cross_validate_uniform_block(void *mem_ctx,
667 struct gl_uniform_block **linked_blocks,
668 unsigned int *num_linked_blocks,
669 struct gl_uniform_block *new_block)
670 {
671 for (unsigned int i = 0; i < *num_linked_blocks; i++) {
672 struct gl_uniform_block *old_block = &(*linked_blocks)[i];
673
674 if (strcmp(old_block->Name, new_block->Name) == 0)
675 return link_uniform_blocks_are_compatible(old_block, new_block)
676 ? i : -1;
677 }
678
679 *linked_blocks = reralloc(mem_ctx, *linked_blocks,
680 struct gl_uniform_block,
681 *num_linked_blocks + 1);
682 int linked_block_index = (*num_linked_blocks)++;
683 struct gl_uniform_block *linked_block = &(*linked_blocks)[linked_block_index];
684
685 memcpy(linked_block, new_block, sizeof(*new_block));
686 linked_block->Uniforms = ralloc_array(*linked_blocks,
687 struct gl_uniform_buffer_variable,
688 linked_block->NumUniforms);
689
690 memcpy(linked_block->Uniforms,
691 new_block->Uniforms,
692 sizeof(*linked_block->Uniforms) * linked_block->NumUniforms);
693
694 for (unsigned int i = 0; i < linked_block->NumUniforms; i++) {
695 struct gl_uniform_buffer_variable *ubo_var =
696 &linked_block->Uniforms[i];
697
698 if (ubo_var->Name == ubo_var->IndexName) {
699 ubo_var->Name = ralloc_strdup(*linked_blocks, ubo_var->Name);
700 ubo_var->IndexName = ubo_var->Name;
701 } else {
702 ubo_var->Name = ralloc_strdup(*linked_blocks, ubo_var->Name);
703 ubo_var->IndexName = ralloc_strdup(*linked_blocks, ubo_var->IndexName);
704 }
705 }
706
707 return linked_block_index;
708 }
709
710 /**
711 * Walks the IR and update the references to uniform blocks in the
712 * ir_variables to point at linked shader's list (previously, they
713 * would point at the uniform block list in one of the pre-linked
714 * shaders).
715 */
716 static void
717 link_update_uniform_buffer_variables(struct gl_shader *shader)
718 {
719 foreach_in_list(ir_instruction, node, shader->ir) {
720 ir_variable *const var = node->as_variable();
721
722 if ((var == NULL) || !var->is_in_uniform_block())
723 continue;
724
725 assert(var->data.mode == ir_var_uniform);
726
727 if (var->is_interface_instance()) {
728 var->data.location = 0;
729 continue;
730 }
731
732 bool found = false;
733 char sentinel = '\0';
734
735 if (var->type->is_record()) {
736 sentinel = '.';
737 } else if (var->type->is_array()
738 && var->type->fields.array->is_record()) {
739 sentinel = '[';
740 }
741
742 const unsigned l = strlen(var->name);
743 for (unsigned i = 0; i < shader->NumUniformBlocks; i++) {
744 for (unsigned j = 0; j < shader->UniformBlocks[i].NumUniforms; j++) {
745 if (sentinel) {
746 const char *begin = shader->UniformBlocks[i].Uniforms[j].Name;
747 const char *end = strchr(begin, sentinel);
748
749 if (end == NULL)
750 continue;
751
752 if ((ptrdiff_t) l != (end - begin))
753 continue;
754
755 if (strncmp(var->name, begin, l) == 0) {
756 found = true;
757 var->data.location = j;
758 break;
759 }
760 } else if (!strcmp(var->name,
761 shader->UniformBlocks[i].Uniforms[j].Name)) {
762 found = true;
763 var->data.location = j;
764 break;
765 }
766 }
767 if (found)
768 break;
769 }
770 assert(found);
771 }
772 }
773
774 void
775 link_assign_uniform_block_offsets(struct gl_shader *shader)
776 {
777 for (unsigned b = 0; b < shader->NumUniformBlocks; b++) {
778 struct gl_uniform_block *block = &shader->UniformBlocks[b];
779
780 unsigned offset = 0;
781 for (unsigned int i = 0; i < block->NumUniforms; i++) {
782 struct gl_uniform_buffer_variable *ubo_var = &block->Uniforms[i];
783 const struct glsl_type *type = ubo_var->Type;
784
785 unsigned alignment = type->std140_base_alignment(ubo_var->RowMajor);
786 unsigned size = type->std140_size(ubo_var->RowMajor);
787
788 offset = glsl_align(offset, alignment);
789 ubo_var->Offset = offset;
790 offset += size;
791 }
792
793 /* From the GL_ARB_uniform_buffer_object spec:
794 *
795 * "For uniform blocks laid out according to [std140] rules,
796 * the minimum buffer object size returned by the
797 * UNIFORM_BLOCK_DATA_SIZE query is derived by taking the
798 * offset of the last basic machine unit consumed by the
799 * last uniform of the uniform block (including any
800 * end-of-array or end-of-structure padding), adding one,
801 * and rounding up to the next multiple of the base
802 * alignment required for a vec4."
803 */
804 block->UniformBufferSize = glsl_align(offset, 16);
805 }
806 }
807
808 /**
809 * Scan the program for image uniforms and store image unit access
810 * information into the gl_shader data structure.
811 */
812 static void
813 link_set_image_access_qualifiers(struct gl_shader_program *prog)
814 {
815 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
816 gl_shader *sh = prog->_LinkedShaders[i];
817
818 if (sh == NULL)
819 continue;
820
821 foreach_in_list(ir_instruction, node, sh->ir) {
822 ir_variable *var = node->as_variable();
823
824 if (var && var->data.mode == ir_var_uniform &&
825 var->type->contains_image()) {
826 unsigned id = 0;
827 bool found = prog->UniformHash->get(id, var->name);
828 assert(found);
829 (void) found;
830 const gl_uniform_storage *storage = &prog->UniformStorage[id];
831 const unsigned index = storage->image[i].index;
832 const GLenum access = (var->data.image_read_only ? GL_READ_ONLY :
833 var->data.image_write_only ? GL_WRITE_ONLY :
834 GL_READ_WRITE);
835
836 for (unsigned j = 0; j < MAX2(1, storage->array_elements); ++j)
837 sh->ImageAccess[index + j] = access;
838 }
839 }
840 }
841 }
842
843 void
844 link_assign_uniform_locations(struct gl_shader_program *prog,
845 unsigned int boolean_true)
846 {
847 ralloc_free(prog->UniformStorage);
848 prog->UniformStorage = NULL;
849 prog->NumUserUniformStorage = 0;
850
851 if (prog->UniformHash != NULL) {
852 prog->UniformHash->clear();
853 } else {
854 prog->UniformHash = new string_to_uint_map;
855 }
856
857 /* First pass: Count the uniform resources used by the user-defined
858 * uniforms. While this happens, each active uniform will have an index
859 * assigned to it.
860 *
861 * Note: this is *NOT* the index that is returned to the application by
862 * glGetUniformLocation.
863 */
864 count_uniform_size uniform_size(prog->UniformHash);
865 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
866 struct gl_shader *sh = prog->_LinkedShaders[i];
867
868 if (sh == NULL)
869 continue;
870
871 /* Uniforms that lack an initializer in the shader code have an initial
872 * value of zero. This includes sampler uniforms.
873 *
874 * Page 24 (page 30 of the PDF) of the GLSL 1.20 spec says:
875 *
876 * "The link time initial value is either the value of the variable's
877 * initializer, if present, or 0 if no initializer is present. Sampler
878 * types cannot have initializers."
879 */
880 memset(sh->SamplerUnits, 0, sizeof(sh->SamplerUnits));
881 memset(sh->ImageUnits, 0, sizeof(sh->ImageUnits));
882
883 link_update_uniform_buffer_variables(sh);
884
885 /* Reset various per-shader target counts.
886 */
887 uniform_size.start_shader();
888
889 foreach_in_list(ir_instruction, node, sh->ir) {
890 ir_variable *const var = node->as_variable();
891
892 if ((var == NULL) || (var->data.mode != ir_var_uniform))
893 continue;
894
895 /* FINISHME: Update code to process built-in uniforms!
896 */
897 if (is_gl_identifier(var->name)) {
898 uniform_size.num_shader_uniform_components +=
899 var->type->component_slots();
900 continue;
901 }
902
903 uniform_size.process(var);
904 }
905
906 sh->num_samplers = uniform_size.num_shader_samplers;
907 sh->NumImages = uniform_size.num_shader_images;
908 sh->num_uniform_components = uniform_size.num_shader_uniform_components;
909
910 sh->num_combined_uniform_components = sh->num_uniform_components;
911 for (unsigned i = 0; i < sh->NumUniformBlocks; i++) {
912 sh->num_combined_uniform_components +=
913 sh->UniformBlocks[i].UniformBufferSize / 4;
914 }
915 }
916
917 const unsigned num_user_uniforms = uniform_size.num_active_uniforms;
918 const unsigned num_data_slots = uniform_size.num_values;
919
920 /* On the outside chance that there were no uniforms, bail out.
921 */
922 if (num_user_uniforms == 0)
923 return;
924
925 struct gl_uniform_storage *uniforms =
926 rzalloc_array(prog, struct gl_uniform_storage, num_user_uniforms);
927 union gl_constant_value *data =
928 rzalloc_array(uniforms, union gl_constant_value, num_data_slots);
929 #ifndef NDEBUG
930 union gl_constant_value *data_end = &data[num_data_slots];
931 #endif
932
933 parcel_out_uniform_storage parcel(prog->UniformHash, uniforms, data);
934
935 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
936 if (prog->_LinkedShaders[i] == NULL)
937 continue;
938
939 parcel.start_shader((gl_shader_stage)i);
940
941 foreach_in_list(ir_instruction, node, prog->_LinkedShaders[i]->ir) {
942 ir_variable *const var = node->as_variable();
943
944 if ((var == NULL) || (var->data.mode != ir_var_uniform))
945 continue;
946
947 /* FINISHME: Update code to process built-in uniforms!
948 */
949 if (is_gl_identifier(var->name))
950 continue;
951
952 parcel.set_and_process(prog, var);
953 }
954
955 prog->_LinkedShaders[i]->active_samplers = parcel.shader_samplers_used;
956 prog->_LinkedShaders[i]->shadow_samplers = parcel.shader_shadow_samplers;
957
958 STATIC_ASSERT(sizeof(prog->_LinkedShaders[i]->SamplerTargets) == sizeof(parcel.targets));
959 memcpy(prog->_LinkedShaders[i]->SamplerTargets, parcel.targets,
960 sizeof(prog->_LinkedShaders[i]->SamplerTargets));
961 }
962
963 /* Reserve all the explicit locations of the active uniforms. */
964 for (unsigned i = 0; i < num_user_uniforms; i++) {
965 if (uniforms[i].remap_location != UNMAPPED_UNIFORM_LOC) {
966 /* How many new entries for this uniform? */
967 const unsigned entries = MAX2(1, uniforms[i].array_elements);
968
969 /* Set remap table entries point to correct gl_uniform_storage. */
970 for (unsigned j = 0; j < entries; j++) {
971 unsigned element_loc = uniforms[i].remap_location + j;
972 assert(prog->UniformRemapTable[element_loc] ==
973 INACTIVE_UNIFORM_EXPLICIT_LOCATION);
974 prog->UniformRemapTable[element_loc] = &uniforms[i];
975 }
976 }
977 }
978
979 /* Reserve locations for rest of the uniforms. */
980 for (unsigned i = 0; i < num_user_uniforms; i++) {
981
982 /* Explicit ones have been set already. */
983 if (uniforms[i].remap_location != UNMAPPED_UNIFORM_LOC)
984 continue;
985
986 /* how many new entries for this uniform? */
987 const unsigned entries = MAX2(1, uniforms[i].array_elements);
988
989 /* resize remap table to fit new entries */
990 prog->UniformRemapTable =
991 reralloc(prog,
992 prog->UniformRemapTable,
993 gl_uniform_storage *,
994 prog->NumUniformRemapTable + entries);
995
996 /* set pointers for this uniform */
997 for (unsigned j = 0; j < entries; j++)
998 prog->UniformRemapTable[prog->NumUniformRemapTable+j] = &uniforms[i];
999
1000 /* set the base location in remap table for the uniform */
1001 uniforms[i].remap_location = prog->NumUniformRemapTable;
1002
1003 prog->NumUniformRemapTable += entries;
1004 }
1005
1006 #ifndef NDEBUG
1007 for (unsigned i = 0; i < num_user_uniforms; i++) {
1008 assert(uniforms[i].storage != NULL);
1009 }
1010
1011 assert(parcel.values == data_end);
1012 #endif
1013
1014 prog->NumUserUniformStorage = num_user_uniforms;
1015 prog->UniformStorage = uniforms;
1016
1017 link_set_image_access_qualifiers(prog);
1018 link_set_uniform_initializers(prog, boolean_true);
1019
1020 return;
1021 }