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