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