mesa/cs: Add a MESA_SHADER_COMPUTE stage and update switch statements.
[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_uniform_components(0), is_ubo_var(false), map(map)
244 {
245 /* empty */
246 }
247
248 void start_shader()
249 {
250 this->num_shader_samplers = 0;
251 this->num_shader_uniform_components = 0;
252 }
253
254 void process(ir_variable *var)
255 {
256 this->is_ubo_var = var->is_in_uniform_block();
257 if (var->is_interface_instance())
258 program_resource_visitor::process(var->get_interface_type(),
259 var->get_interface_type()->name);
260 else
261 program_resource_visitor::process(var);
262 }
263
264 /**
265 * Total number of active uniforms counted
266 */
267 unsigned num_active_uniforms;
268
269 /**
270 * Number of data values required to back the storage for the active uniforms
271 */
272 unsigned num_values;
273
274 /**
275 * Number of samplers used
276 */
277 unsigned num_shader_samplers;
278
279 /**
280 * Number of uniforms used in the current shader
281 */
282 unsigned num_shader_uniform_components;
283
284 bool is_ubo_var;
285
286 private:
287 virtual void visit_field(const glsl_type *type, const char *name,
288 bool row_major)
289 {
290 assert(!type->is_record());
291 assert(!(type->is_array() && type->fields.array->is_record()));
292 assert(!type->is_interface());
293 assert(!(type->is_array() && type->fields.array->is_interface()));
294
295 (void) row_major;
296
297 /* Count the number of samplers regardless of whether the uniform is
298 * already in the hash table. The hash table prevents adding the same
299 * uniform for multiple shader targets, but in this case we want to
300 * count it for each shader target.
301 */
302 const unsigned values = values_for_type(type);
303 if (type->contains_sampler()) {
304 this->num_shader_samplers +=
305 type->is_array() ? type->array_size() : 1;
306 } else {
307 /* Accumulate the total number of uniform slots used by this shader.
308 * Note that samplers do not count against this limit because they
309 * don't use any storage on current hardware.
310 */
311 if (!is_ubo_var)
312 this->num_shader_uniform_components += values;
313 }
314
315 /* If the uniform is already in the map, there's nothing more to do.
316 */
317 unsigned id;
318 if (this->map->get(id, name))
319 return;
320
321 this->map->put(this->num_active_uniforms, name);
322
323 /* Each leaf uniform occupies one entry in the list of active
324 * uniforms.
325 */
326 this->num_active_uniforms++;
327 this->num_values += values;
328 }
329
330 struct string_to_uint_map *map;
331 };
332
333 } /* anonymous namespace */
334
335 /**
336 * Class to help parcel out pieces of backing storage to uniforms
337 *
338 * Each uniform processed has some range of the \c gl_constant_value
339 * structures associated with it. The association is done by finding
340 * the uniform in the \c string_to_uint_map and using the value from
341 * the map to connect that slot in the \c gl_uniform_storage table
342 * with the next available slot in the \c gl_constant_value array.
343 *
344 * \warning
345 * This class assumes that every uniform that will be processed is
346 * already in the \c string_to_uint_map. In addition, it assumes that
347 * the \c gl_uniform_storage and \c gl_constant_value arrays are "big
348 * enough."
349 */
350 class parcel_out_uniform_storage : public program_resource_visitor {
351 public:
352 parcel_out_uniform_storage(struct string_to_uint_map *map,
353 struct gl_uniform_storage *uniforms,
354 union gl_constant_value *values)
355 : map(map), uniforms(uniforms), values(values)
356 {
357 }
358
359 void start_shader(gl_shader_stage shader_type)
360 {
361 assert(shader_type < MESA_SHADER_STAGES);
362 this->shader_type = shader_type;
363
364 this->shader_samplers_used = 0;
365 this->shader_shadow_samplers = 0;
366 this->next_sampler = 0;
367 memset(this->targets, 0, sizeof(this->targets));
368 }
369
370 void set_and_process(struct gl_shader_program *prog,
371 ir_variable *var)
372 {
373 ubo_block_index = -1;
374 if (var->is_in_uniform_block()) {
375 if (var->is_interface_instance() && var->type->is_array()) {
376 unsigned l = strlen(var->get_interface_type()->name);
377
378 for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
379 if (strncmp(var->get_interface_type()->name,
380 prog->UniformBlocks[i].Name,
381 l) == 0
382 && prog->UniformBlocks[i].Name[l] == '[') {
383 ubo_block_index = i;
384 break;
385 }
386 }
387 } else {
388 for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
389 if (strcmp(var->get_interface_type()->name,
390 prog->UniformBlocks[i].Name) == 0) {
391 ubo_block_index = i;
392 break;
393 }
394 }
395 }
396 assert(ubo_block_index != -1);
397
398 /* Uniform blocks that were specified with an instance name must be
399 * handled a little bit differently. The name of the variable is the
400 * name used to reference the uniform block instead of being the name
401 * of a variable within the block. Therefore, searching for the name
402 * within the block will fail.
403 */
404 if (var->is_interface_instance()) {
405 ubo_byte_offset = 0;
406 ubo_row_major = false;
407 } else {
408 const struct gl_uniform_block *const block =
409 &prog->UniformBlocks[ubo_block_index];
410
411 assert(var->data.location != -1);
412
413 const struct gl_uniform_buffer_variable *const ubo_var =
414 &block->Uniforms[var->data.location];
415
416 ubo_row_major = ubo_var->RowMajor;
417 ubo_byte_offset = ubo_var->Offset;
418 }
419
420 if (var->is_interface_instance())
421 process(var->get_interface_type(),
422 var->get_interface_type()->name);
423 else
424 process(var);
425 } else
426 process(var);
427 }
428
429 int ubo_block_index;
430 int ubo_byte_offset;
431 bool ubo_row_major;
432 gl_shader_stage shader_type;
433
434 private:
435 void handle_samplers(const glsl_type *base_type,
436 struct gl_uniform_storage *uniform)
437 {
438 if (base_type->is_sampler()) {
439 uniform->sampler[shader_type].index = this->next_sampler;
440 uniform->sampler[shader_type].active = true;
441
442 /* Increment the sampler by 1 for non-arrays and by the number of
443 * array elements for arrays.
444 */
445 this->next_sampler +=
446 MAX2(1, uniform->array_elements);
447
448 const gl_texture_index target = base_type->sampler_index();
449 const unsigned shadow = base_type->sampler_shadow;
450 for (unsigned i = uniform->sampler[shader_type].index;
451 i < MIN2(this->next_sampler, MAX_SAMPLERS);
452 i++) {
453 this->targets[i] = target;
454 this->shader_samplers_used |= 1U << i;
455 this->shader_shadow_samplers |= shadow << i;
456 }
457 } else {
458 uniform->sampler[shader_type].index = ~0;
459 uniform->sampler[shader_type].active = false;
460 }
461 }
462
463 virtual void visit_field(const glsl_type *type, const char *name,
464 bool row_major)
465 {
466 (void) type;
467 (void) name;
468 (void) row_major;
469 assert(!"Should not get here.");
470 }
471
472 virtual void visit_field(const glsl_type *type, const char *name,
473 bool row_major, const glsl_type *record_type)
474 {
475 assert(!type->is_record());
476 assert(!(type->is_array() && type->fields.array->is_record()));
477 assert(!type->is_interface());
478 assert(!(type->is_array() && type->fields.array->is_interface()));
479
480 (void) row_major;
481
482 unsigned id;
483 bool found = this->map->get(id, name);
484 assert(found);
485
486 if (!found)
487 return;
488
489 const glsl_type *base_type;
490 if (type->is_array()) {
491 this->uniforms[id].array_elements = type->length;
492 base_type = type->fields.array;
493 } else {
494 this->uniforms[id].array_elements = 0;
495 base_type = type;
496 }
497
498 /* This assigns sampler uniforms to sampler units. */
499 handle_samplers(base_type, &this->uniforms[id]);
500
501 /* If there is already storage associated with this uniform, it means
502 * that it was set while processing an earlier shader stage. For
503 * example, we may be processing the uniform in the fragment shader, but
504 * the uniform was already processed in the vertex shader.
505 */
506 if (this->uniforms[id].storage != NULL) {
507 return;
508 }
509
510 this->uniforms[id].name = ralloc_strdup(this->uniforms, name);
511 this->uniforms[id].type = base_type;
512 this->uniforms[id].initialized = 0;
513 this->uniforms[id].num_driver_storage = 0;
514 this->uniforms[id].driver_storage = NULL;
515 this->uniforms[id].storage = this->values;
516 this->uniforms[id].atomic_buffer_index = -1;
517 if (this->ubo_block_index != -1) {
518 this->uniforms[id].block_index = this->ubo_block_index;
519
520 const unsigned alignment = record_type
521 ? record_type->std140_base_alignment(ubo_row_major)
522 : type->std140_base_alignment(ubo_row_major);
523 this->ubo_byte_offset = glsl_align(this->ubo_byte_offset, alignment);
524 this->uniforms[id].offset = this->ubo_byte_offset;
525 this->ubo_byte_offset += type->std140_size(ubo_row_major);
526
527 if (type->is_array()) {
528 this->uniforms[id].array_stride =
529 glsl_align(type->fields.array->std140_size(ubo_row_major), 16);
530 } else {
531 this->uniforms[id].array_stride = 0;
532 }
533
534 if (type->is_matrix() ||
535 (type->is_array() && type->fields.array->is_matrix())) {
536 this->uniforms[id].matrix_stride = 16;
537 this->uniforms[id].row_major = ubo_row_major;
538 } else {
539 this->uniforms[id].matrix_stride = 0;
540 this->uniforms[id].row_major = false;
541 }
542 } else {
543 this->uniforms[id].block_index = -1;
544 this->uniforms[id].offset = -1;
545 this->uniforms[id].array_stride = -1;
546 this->uniforms[id].matrix_stride = -1;
547 this->uniforms[id].row_major = false;
548 }
549
550 this->values += values_for_type(type);
551 }
552
553 struct string_to_uint_map *map;
554
555 struct gl_uniform_storage *uniforms;
556 unsigned next_sampler;
557
558 public:
559 union gl_constant_value *values;
560
561 gl_texture_index targets[MAX_SAMPLERS];
562
563 /**
564 * Mask of samplers used by the current shader stage.
565 */
566 unsigned shader_samplers_used;
567
568 /**
569 * Mask of samplers used by the current shader stage for shadows.
570 */
571 unsigned shader_shadow_samplers;
572 };
573
574 /**
575 * Merges a uniform block into an array of uniform blocks that may or
576 * may not already contain a copy of it.
577 *
578 * Returns the index of the new block in the array.
579 */
580 int
581 link_cross_validate_uniform_block(void *mem_ctx,
582 struct gl_uniform_block **linked_blocks,
583 unsigned int *num_linked_blocks,
584 struct gl_uniform_block *new_block)
585 {
586 for (unsigned int i = 0; i < *num_linked_blocks; i++) {
587 struct gl_uniform_block *old_block = &(*linked_blocks)[i];
588
589 if (strcmp(old_block->Name, new_block->Name) == 0)
590 return link_uniform_blocks_are_compatible(old_block, new_block)
591 ? i : -1;
592 }
593
594 *linked_blocks = reralloc(mem_ctx, *linked_blocks,
595 struct gl_uniform_block,
596 *num_linked_blocks + 1);
597 int linked_block_index = (*num_linked_blocks)++;
598 struct gl_uniform_block *linked_block = &(*linked_blocks)[linked_block_index];
599
600 memcpy(linked_block, new_block, sizeof(*new_block));
601 linked_block->Uniforms = ralloc_array(*linked_blocks,
602 struct gl_uniform_buffer_variable,
603 linked_block->NumUniforms);
604
605 memcpy(linked_block->Uniforms,
606 new_block->Uniforms,
607 sizeof(*linked_block->Uniforms) * linked_block->NumUniforms);
608
609 for (unsigned int i = 0; i < linked_block->NumUniforms; i++) {
610 struct gl_uniform_buffer_variable *ubo_var =
611 &linked_block->Uniforms[i];
612
613 if (ubo_var->Name == ubo_var->IndexName) {
614 ubo_var->Name = ralloc_strdup(*linked_blocks, ubo_var->Name);
615 ubo_var->IndexName = ubo_var->Name;
616 } else {
617 ubo_var->Name = ralloc_strdup(*linked_blocks, ubo_var->Name);
618 ubo_var->IndexName = ralloc_strdup(*linked_blocks, ubo_var->IndexName);
619 }
620 }
621
622 return linked_block_index;
623 }
624
625 /**
626 * Walks the IR and update the references to uniform blocks in the
627 * ir_variables to point at linked shader's list (previously, they
628 * would point at the uniform block list in one of the pre-linked
629 * shaders).
630 */
631 static void
632 link_update_uniform_buffer_variables(struct gl_shader *shader)
633 {
634 foreach_list(node, shader->ir) {
635 ir_variable *const var = ((ir_instruction *) node)->as_variable();
636
637 if ((var == NULL) || !var->is_in_uniform_block())
638 continue;
639
640 assert(var->data.mode == ir_var_uniform);
641
642 if (var->is_interface_instance()) {
643 var->data.location = 0;
644 continue;
645 }
646
647 bool found = false;
648 char sentinel = '\0';
649
650 if (var->type->is_record()) {
651 sentinel = '.';
652 } else if (var->type->is_array()
653 && var->type->fields.array->is_record()) {
654 sentinel = '[';
655 }
656
657 const unsigned l = strlen(var->name);
658 for (unsigned i = 0; i < shader->NumUniformBlocks; i++) {
659 for (unsigned j = 0; j < shader->UniformBlocks[i].NumUniforms; j++) {
660 if (sentinel) {
661 const char *begin = shader->UniformBlocks[i].Uniforms[j].Name;
662 const char *end = strchr(begin, sentinel);
663
664 if (end == NULL)
665 continue;
666
667 if (l != (end - begin))
668 continue;
669
670 if (strncmp(var->name, begin, l) == 0) {
671 found = true;
672 var->data.location = j;
673 break;
674 }
675 } else if (!strcmp(var->name,
676 shader->UniformBlocks[i].Uniforms[j].Name)) {
677 found = true;
678 var->data.location = j;
679 break;
680 }
681 }
682 if (found)
683 break;
684 }
685 assert(found);
686 }
687 }
688
689 void
690 link_assign_uniform_block_offsets(struct gl_shader *shader)
691 {
692 for (unsigned b = 0; b < shader->NumUniformBlocks; b++) {
693 struct gl_uniform_block *block = &shader->UniformBlocks[b];
694
695 unsigned offset = 0;
696 for (unsigned int i = 0; i < block->NumUniforms; i++) {
697 struct gl_uniform_buffer_variable *ubo_var = &block->Uniforms[i];
698 const struct glsl_type *type = ubo_var->Type;
699
700 unsigned alignment = type->std140_base_alignment(ubo_var->RowMajor);
701 unsigned size = type->std140_size(ubo_var->RowMajor);
702
703 offset = glsl_align(offset, alignment);
704 ubo_var->Offset = offset;
705 offset += size;
706 }
707
708 /* From the GL_ARB_uniform_buffer_object spec:
709 *
710 * "For uniform blocks laid out according to [std140] rules,
711 * the minimum buffer object size returned by the
712 * UNIFORM_BLOCK_DATA_SIZE query is derived by taking the
713 * offset of the last basic machine unit consumed by the
714 * last uniform of the uniform block (including any
715 * end-of-array or end-of-structure padding), adding one,
716 * and rounding up to the next multiple of the base
717 * alignment required for a vec4."
718 */
719 block->UniformBufferSize = glsl_align(offset, 16);
720 }
721 }
722
723 void
724 link_assign_uniform_locations(struct gl_shader_program *prog)
725 {
726 ralloc_free(prog->UniformStorage);
727 prog->UniformStorage = NULL;
728 prog->NumUserUniformStorage = 0;
729
730 if (prog->UniformHash != NULL) {
731 prog->UniformHash->clear();
732 } else {
733 prog->UniformHash = new string_to_uint_map;
734 }
735
736 /* First pass: Count the uniform resources used by the user-defined
737 * uniforms. While this happens, each active uniform will have an index
738 * assigned to it.
739 *
740 * Note: this is *NOT* the index that is returned to the application by
741 * glGetUniformLocation.
742 */
743 count_uniform_size uniform_size(prog->UniformHash);
744 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
745 struct gl_shader *sh = prog->_LinkedShaders[i];
746
747 if (sh == NULL)
748 continue;
749
750 /* Uniforms that lack an initializer in the shader code have an initial
751 * value of zero. This includes sampler uniforms.
752 *
753 * Page 24 (page 30 of the PDF) of the GLSL 1.20 spec says:
754 *
755 * "The link time initial value is either the value of the variable's
756 * initializer, if present, or 0 if no initializer is present. Sampler
757 * types cannot have initializers."
758 */
759 memset(sh->SamplerUnits, 0, sizeof(sh->SamplerUnits));
760
761 link_update_uniform_buffer_variables(sh);
762
763 /* Reset various per-shader target counts.
764 */
765 uniform_size.start_shader();
766
767 foreach_list(node, sh->ir) {
768 ir_variable *const var = ((ir_instruction *) node)->as_variable();
769
770 if ((var == NULL) || (var->data.mode != ir_var_uniform))
771 continue;
772
773 /* FINISHME: Update code to process built-in uniforms!
774 */
775 if (strncmp("gl_", var->name, 3) == 0) {
776 uniform_size.num_shader_uniform_components +=
777 var->type->component_slots();
778 continue;
779 }
780
781 uniform_size.process(var);
782 }
783
784 sh->num_samplers = uniform_size.num_shader_samplers;
785 sh->num_uniform_components = uniform_size.num_shader_uniform_components;
786
787 sh->num_combined_uniform_components = sh->num_uniform_components;
788 for (unsigned i = 0; i < sh->NumUniformBlocks; i++) {
789 sh->num_combined_uniform_components +=
790 sh->UniformBlocks[i].UniformBufferSize / 4;
791 }
792 }
793
794 const unsigned num_user_uniforms = uniform_size.num_active_uniforms;
795 const unsigned num_data_slots = uniform_size.num_values;
796
797 /* On the outside chance that there were no uniforms, bail out.
798 */
799 if (num_user_uniforms == 0)
800 return;
801
802 struct gl_uniform_storage *uniforms =
803 rzalloc_array(prog, struct gl_uniform_storage, num_user_uniforms);
804 union gl_constant_value *data =
805 rzalloc_array(uniforms, union gl_constant_value, num_data_slots);
806 #ifndef NDEBUG
807 union gl_constant_value *data_end = &data[num_data_slots];
808 #endif
809
810 parcel_out_uniform_storage parcel(prog->UniformHash, uniforms, data);
811
812 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
813 if (prog->_LinkedShaders[i] == NULL)
814 continue;
815
816 parcel.start_shader((gl_shader_stage)i);
817
818 foreach_list(node, prog->_LinkedShaders[i]->ir) {
819 ir_variable *const var = ((ir_instruction *) node)->as_variable();
820
821 if ((var == NULL) || (var->data.mode != ir_var_uniform))
822 continue;
823
824 /* FINISHME: Update code to process built-in uniforms!
825 */
826 if (strncmp("gl_", var->name, 3) == 0)
827 continue;
828
829 parcel.set_and_process(prog, var);
830 }
831
832 prog->_LinkedShaders[i]->active_samplers = parcel.shader_samplers_used;
833 prog->_LinkedShaders[i]->shadow_samplers = parcel.shader_shadow_samplers;
834
835 STATIC_ASSERT(sizeof(prog->_LinkedShaders[i]->SamplerTargets) == sizeof(parcel.targets));
836 memcpy(prog->_LinkedShaders[i]->SamplerTargets, parcel.targets,
837 sizeof(prog->_LinkedShaders[i]->SamplerTargets));
838 }
839
840 /* Determine the size of the largest uniform array queryable via
841 * glGetUniformLocation. Using this as the location scale guarantees that
842 * there is enough "room" for the array index to be stored in the low order
843 * part of the uniform location. It also makes the locations be more
844 * tightly packed.
845 */
846 unsigned max_array_size = 1;
847 for (unsigned i = 0; i < num_user_uniforms; i++) {
848 if (uniforms[i].array_elements > max_array_size)
849 max_array_size = uniforms[i].array_elements;
850 }
851
852 prog->UniformLocationBaseScale = max_array_size;
853
854 #ifndef NDEBUG
855 for (unsigned i = 0; i < num_user_uniforms; i++) {
856 assert(uniforms[i].storage != NULL);
857 }
858
859 assert(parcel.values == data_end);
860 #endif
861
862 prog->NumUserUniformStorage = num_user_uniforms;
863 prog->UniformStorage = uniforms;
864
865 link_set_uniform_initializers(prog);
866
867 return;
868 }