208778e4ada6297facbc8932b71634b2d3272474
[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);
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 (t->is_record() || (t->is_array() && t->fields.array->is_record())) {
79 char *name = ralloc_strdup(NULL, var->name);
80 recursion(var->type, &name, strlen(name), false);
81 ralloc_free(name);
82 } else if (t->is_interface()) {
83 char *name = ralloc_strdup(NULL, var->type->name);
84 recursion(var->type, &name, strlen(name), false);
85 ralloc_free(name);
86 } else if (t->is_array() && t->fields.array->is_interface()) {
87 char *name = ralloc_strdup(NULL, var->type->fields.array->name);
88 recursion(var->type, &name, strlen(name), false);
89 ralloc_free(name);
90 } else {
91 this->visit_field(t, var->name, false);
92 }
93 }
94
95 void
96 program_resource_visitor::recursion(const glsl_type *t, char **name,
97 size_t name_length, bool row_major)
98 {
99 /* Records need to have each field processed individually.
100 *
101 * Arrays of records need to have each array element processed
102 * individually, then each field of the resulting array elements processed
103 * individually.
104 */
105 if (t->is_record() || t->is_interface()) {
106 for (unsigned i = 0; i < t->length; i++) {
107 const char *field = t->fields.structure[i].name;
108 size_t new_length = name_length;
109
110 if (t->fields.structure[i].type->is_record())
111 this->visit_field(&t->fields.structure[i]);
112
113 /* Append '.field' to the current variable name. */
114 if (name_length == 0) {
115 ralloc_asprintf_rewrite_tail(name, &new_length, "%s", field);
116 } else {
117 ralloc_asprintf_rewrite_tail(name, &new_length, ".%s", field);
118 }
119
120 recursion(t->fields.structure[i].type, name, new_length,
121 t->fields.structure[i].row_major);
122 }
123 } else if (t->is_array() && (t->fields.array->is_record()
124 || t->fields.array->is_interface())) {
125 for (unsigned i = 0; i < t->length; i++) {
126 size_t new_length = name_length;
127
128 /* Append the subscript to the current variable name */
129 ralloc_asprintf_rewrite_tail(name, &new_length, "[%u]", i);
130
131 recursion(t->fields.array, name, new_length,
132 t->fields.structure[i].row_major);
133 }
134 } else {
135 this->visit_field(t, *name, row_major);
136 }
137 }
138
139 void
140 program_resource_visitor::visit_field(const glsl_struct_field *field)
141 {
142 (void) field;
143 /* empty */
144 }
145
146 /**
147 * Class to help calculate the storage requirements for a set of uniforms
148 *
149 * As uniforms are added to the active set the number of active uniforms and
150 * the storage requirements for those uniforms are accumulated. The active
151 * uniforms are added the the hash table supplied to the constructor.
152 *
153 * If the same uniform is added multiple times (i.e., once for each shader
154 * target), it will only be accounted once.
155 */
156 class count_uniform_size : public program_resource_visitor {
157 public:
158 count_uniform_size(struct string_to_uint_map *map)
159 : num_active_uniforms(0), num_values(0), num_shader_samplers(0),
160 num_shader_uniform_components(0), map(map)
161 {
162 /* empty */
163 }
164
165 void start_shader()
166 {
167 this->num_shader_samplers = 0;
168 this->num_shader_uniform_components = 0;
169 }
170
171 void process(ir_variable *var)
172 {
173 this->is_ubo_var = var->is_in_uniform_block();
174 if (var->is_interface_instance())
175 program_resource_visitor::process(var->interface_type,
176 var->interface_type->name);
177 else
178 program_resource_visitor::process(var);
179 }
180
181 /**
182 * Total number of active uniforms counted
183 */
184 unsigned num_active_uniforms;
185
186 /**
187 * Number of data values required to back the storage for the active uniforms
188 */
189 unsigned num_values;
190
191 /**
192 * Number of samplers used
193 */
194 unsigned num_shader_samplers;
195
196 /**
197 * Number of uniforms used in the current shader
198 */
199 unsigned num_shader_uniform_components;
200
201 bool is_ubo_var;
202
203 private:
204 virtual void visit_field(const glsl_type *type, const char *name,
205 bool row_major)
206 {
207 assert(!type->is_record());
208 assert(!(type->is_array() && type->fields.array->is_record()));
209 assert(!type->is_interface());
210 assert(!(type->is_array() && type->fields.array->is_interface()));
211
212 (void) row_major;
213
214 /* Count the number of samplers regardless of whether the uniform is
215 * already in the hash table. The hash table prevents adding the same
216 * uniform for multiple shader targets, but in this case we want to
217 * count it for each shader target.
218 */
219 const unsigned values = values_for_type(type);
220 if (type->contains_sampler()) {
221 this->num_shader_samplers +=
222 type->is_array() ? type->array_size() : 1;
223 } else {
224 /* Accumulate the total number of uniform slots used by this shader.
225 * Note that samplers do not count against this limit because they
226 * don't use any storage on current hardware.
227 */
228 if (!is_ubo_var)
229 this->num_shader_uniform_components += values;
230 }
231
232 /* If the uniform is already in the map, there's nothing more to do.
233 */
234 unsigned id;
235 if (this->map->get(id, name))
236 return;
237
238 this->map->put(this->num_active_uniforms, name);
239
240 /* Each leaf uniform occupies one entry in the list of active
241 * uniforms.
242 */
243 this->num_active_uniforms++;
244 this->num_values += values;
245 }
246
247 struct string_to_uint_map *map;
248 };
249
250 /**
251 * Class to help parcel out pieces of backing storage to uniforms
252 *
253 * Each uniform processed has some range of the \c gl_constant_value
254 * structures associated with it. The association is done by finding
255 * the uniform in the \c string_to_uint_map and using the value from
256 * the map to connect that slot in the \c gl_uniform_storage table
257 * with the next available slot in the \c gl_constant_value array.
258 *
259 * \warning
260 * This class assumes that every uniform that will be processed is
261 * already in the \c string_to_uint_map. In addition, it assumes that
262 * the \c gl_uniform_storage and \c gl_constant_value arrays are "big
263 * enough."
264 */
265 class parcel_out_uniform_storage : public program_resource_visitor {
266 public:
267 parcel_out_uniform_storage(struct string_to_uint_map *map,
268 struct gl_uniform_storage *uniforms,
269 union gl_constant_value *values)
270 : map(map), uniforms(uniforms), values(values)
271 {
272 }
273
274 void start_shader(gl_shader_type shader_type)
275 {
276 assert(shader_type < MESA_SHADER_TYPES);
277 this->shader_type = shader_type;
278
279 this->shader_samplers_used = 0;
280 this->shader_shadow_samplers = 0;
281 this->next_sampler = 0;
282 memset(this->targets, 0, sizeof(this->targets));
283 }
284
285 void set_and_process(struct gl_shader_program *prog,
286 ir_variable *var)
287 {
288 ubo_block_index = -1;
289 if (var->is_in_uniform_block()) {
290 if (var->is_interface_instance() && var->type->is_array()) {
291 unsigned l = strlen(var->interface_type->name);
292
293 for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
294 if (strncmp(var->interface_type->name,
295 prog->UniformBlocks[i].Name,
296 l) == 0
297 && prog->UniformBlocks[i].Name[l] == '[') {
298 ubo_block_index = i;
299 break;
300 }
301 }
302 } else {
303 for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
304 if (strcmp(var->interface_type->name,
305 prog->UniformBlocks[i].Name) == 0) {
306 ubo_block_index = i;
307 break;
308 }
309 }
310 }
311 assert(ubo_block_index != -1);
312
313 /* Uniform blocks that were specified with an instance name must be
314 * handled a little bit differently. The name of the variable is the
315 * name used to reference the uniform block instead of being the name
316 * of a variable within the block. Therefore, searching for the name
317 * within the block will fail.
318 */
319 if (var->is_interface_instance()) {
320 ubo_byte_offset = 0;
321 ubo_row_major = false;
322 } else {
323 const struct gl_uniform_block *const block =
324 &prog->UniformBlocks[ubo_block_index];
325
326 assert(var->location != -1);
327
328 const struct gl_uniform_buffer_variable *const ubo_var =
329 &block->Uniforms[var->location];
330
331 ubo_row_major = ubo_var->RowMajor;
332 ubo_byte_offset = ubo_var->Offset;
333 }
334
335 if (var->is_interface_instance())
336 process(var->interface_type, var->interface_type->name);
337 else
338 process(var);
339 } else
340 process(var);
341 }
342
343 int ubo_block_index;
344 int ubo_byte_offset;
345 bool ubo_row_major;
346 gl_shader_type shader_type;
347
348 private:
349 void handle_samplers(const glsl_type *base_type,
350 struct gl_uniform_storage *uniform)
351 {
352 if (base_type->is_sampler()) {
353 uniform->sampler[shader_type].index = this->next_sampler;
354 uniform->sampler[shader_type].active = true;
355
356 /* Increment the sampler by 1 for non-arrays and by the number of
357 * array elements for arrays.
358 */
359 this->next_sampler +=
360 MAX2(1, uniform->array_elements);
361
362 const gl_texture_index target = base_type->sampler_index();
363 const unsigned shadow = base_type->sampler_shadow;
364 for (unsigned i = uniform->sampler[shader_type].index;
365 i < MIN2(this->next_sampler, MAX_SAMPLERS);
366 i++) {
367 this->targets[i] = target;
368 this->shader_samplers_used |= 1U << i;
369 this->shader_shadow_samplers |= shadow << i;
370 }
371 } else {
372 uniform->sampler[shader_type].index = ~0;
373 uniform->sampler[shader_type].active = false;
374 }
375 }
376
377 virtual void visit_field(const glsl_type *type, const char *name,
378 bool row_major)
379 {
380 assert(!type->is_record());
381 assert(!(type->is_array() && type->fields.array->is_record()));
382 assert(!type->is_interface());
383 assert(!(type->is_array() && type->fields.array->is_interface()));
384
385 (void) row_major;
386
387 unsigned id;
388 bool found = this->map->get(id, name);
389 assert(found);
390
391 if (!found)
392 return;
393
394 const glsl_type *base_type;
395 if (type->is_array()) {
396 this->uniforms[id].array_elements = type->length;
397 base_type = type->fields.array;
398 } else {
399 this->uniforms[id].array_elements = 0;
400 base_type = type;
401 }
402
403 /* This assigns sampler uniforms to sampler units. */
404 handle_samplers(base_type, &this->uniforms[id]);
405
406 /* If there is already storage associated with this uniform, it means
407 * that it was set while processing an earlier shader stage. For
408 * example, we may be processing the uniform in the fragment shader, but
409 * the uniform was already processed in the vertex shader.
410 */
411 if (this->uniforms[id].storage != NULL) {
412 return;
413 }
414
415 this->uniforms[id].name = ralloc_strdup(this->uniforms, name);
416 this->uniforms[id].type = base_type;
417 this->uniforms[id].initialized = 0;
418 this->uniforms[id].num_driver_storage = 0;
419 this->uniforms[id].driver_storage = NULL;
420 this->uniforms[id].storage = this->values;
421 if (this->ubo_block_index != -1) {
422 this->uniforms[id].block_index = this->ubo_block_index;
423
424 unsigned alignment = type->std140_base_alignment(ubo_row_major);
425 this->ubo_byte_offset = glsl_align(this->ubo_byte_offset, alignment);
426 this->uniforms[id].offset = this->ubo_byte_offset;
427 this->ubo_byte_offset += type->std140_size(ubo_row_major);
428
429 if (type->is_array()) {
430 this->uniforms[id].array_stride =
431 glsl_align(type->fields.array->std140_size(ubo_row_major), 16);
432 } else {
433 this->uniforms[id].array_stride = 0;
434 }
435
436 if (type->is_matrix() ||
437 (type->is_array() && type->fields.array->is_matrix())) {
438 this->uniforms[id].matrix_stride = 16;
439 this->uniforms[id].row_major = ubo_row_major;
440 } else {
441 this->uniforms[id].matrix_stride = 0;
442 this->uniforms[id].row_major = false;
443 }
444 } else {
445 this->uniforms[id].block_index = -1;
446 this->uniforms[id].offset = -1;
447 this->uniforms[id].array_stride = -1;
448 this->uniforms[id].matrix_stride = -1;
449 this->uniforms[id].row_major = false;
450 }
451
452 this->values += values_for_type(type);
453 }
454
455 struct string_to_uint_map *map;
456
457 struct gl_uniform_storage *uniforms;
458 unsigned next_sampler;
459
460 public:
461 union gl_constant_value *values;
462
463 gl_texture_index targets[MAX_SAMPLERS];
464
465 /**
466 * Mask of samplers used by the current shader stage.
467 */
468 unsigned shader_samplers_used;
469
470 /**
471 * Mask of samplers used by the current shader stage for shadows.
472 */
473 unsigned shader_shadow_samplers;
474 };
475
476 /**
477 * Merges a uniform block into an array of uniform blocks that may or
478 * may not already contain a copy of it.
479 *
480 * Returns the index of the new block in the array.
481 */
482 int
483 link_cross_validate_uniform_block(void *mem_ctx,
484 struct gl_uniform_block **linked_blocks,
485 unsigned int *num_linked_blocks,
486 struct gl_uniform_block *new_block)
487 {
488 for (unsigned int i = 0; i < *num_linked_blocks; i++) {
489 struct gl_uniform_block *old_block = &(*linked_blocks)[i];
490
491 if (strcmp(old_block->Name, new_block->Name) == 0)
492 return link_uniform_blocks_are_compatible(old_block, new_block)
493 ? i : -1;
494 }
495
496 *linked_blocks = reralloc(mem_ctx, *linked_blocks,
497 struct gl_uniform_block,
498 *num_linked_blocks + 1);
499 int linked_block_index = (*num_linked_blocks)++;
500 struct gl_uniform_block *linked_block = &(*linked_blocks)[linked_block_index];
501
502 memcpy(linked_block, new_block, sizeof(*new_block));
503 linked_block->Uniforms = ralloc_array(*linked_blocks,
504 struct gl_uniform_buffer_variable,
505 linked_block->NumUniforms);
506
507 memcpy(linked_block->Uniforms,
508 new_block->Uniforms,
509 sizeof(*linked_block->Uniforms) * linked_block->NumUniforms);
510
511 for (unsigned int i = 0; i < linked_block->NumUniforms; i++) {
512 struct gl_uniform_buffer_variable *ubo_var =
513 &linked_block->Uniforms[i];
514
515 if (ubo_var->Name == ubo_var->IndexName) {
516 ubo_var->Name = ralloc_strdup(*linked_blocks, ubo_var->Name);
517 ubo_var->IndexName = ubo_var->Name;
518 } else {
519 ubo_var->Name = ralloc_strdup(*linked_blocks, ubo_var->Name);
520 ubo_var->IndexName = ralloc_strdup(*linked_blocks, ubo_var->IndexName);
521 }
522 }
523
524 return linked_block_index;
525 }
526
527 /**
528 * Walks the IR and update the references to uniform blocks in the
529 * ir_variables to point at linked shader's list (previously, they
530 * would point at the uniform block list in one of the pre-linked
531 * shaders).
532 */
533 static void
534 link_update_uniform_buffer_variables(struct gl_shader *shader)
535 {
536 foreach_list(node, shader->ir) {
537 ir_variable *const var = ((ir_instruction *) node)->as_variable();
538
539 if ((var == NULL) || !var->is_in_uniform_block())
540 continue;
541
542 assert(var->mode == ir_var_uniform);
543
544 if (var->is_interface_instance()) {
545 var->location = 0;
546 continue;
547 }
548
549 bool found = false;
550 char sentinel = '\0';
551
552 if (var->type->is_record()) {
553 sentinel = '.';
554 } else if (var->type->is_array()
555 && var->type->fields.array->is_record()) {
556 sentinel = '[';
557 }
558
559 const unsigned l = strlen(var->name);
560 for (unsigned i = 0; i < shader->NumUniformBlocks; i++) {
561 for (unsigned j = 0; j < shader->UniformBlocks[i].NumUniforms; j++) {
562 if (sentinel) {
563 const char *begin = shader->UniformBlocks[i].Uniforms[j].Name;
564 const char *end = strchr(begin, sentinel);
565
566 if (end == NULL)
567 continue;
568
569 if (l != (end - begin))
570 continue;
571
572 if (strncmp(var->name, begin, l) == 0) {
573 found = true;
574 var->location = j;
575 break;
576 }
577 } else if (!strcmp(var->name,
578 shader->UniformBlocks[i].Uniforms[j].Name)) {
579 found = true;
580 var->location = j;
581 break;
582 }
583 }
584 if (found)
585 break;
586 }
587 assert(found);
588 }
589 }
590
591 void
592 link_assign_uniform_block_offsets(struct gl_shader *shader)
593 {
594 for (unsigned b = 0; b < shader->NumUniformBlocks; b++) {
595 struct gl_uniform_block *block = &shader->UniformBlocks[b];
596
597 unsigned offset = 0;
598 for (unsigned int i = 0; i < block->NumUniforms; i++) {
599 struct gl_uniform_buffer_variable *ubo_var = &block->Uniforms[i];
600 const struct glsl_type *type = ubo_var->Type;
601
602 unsigned alignment = type->std140_base_alignment(ubo_var->RowMajor);
603 unsigned size = type->std140_size(ubo_var->RowMajor);
604
605 offset = glsl_align(offset, alignment);
606 ubo_var->Offset = offset;
607 offset += size;
608 }
609
610 /* From the GL_ARB_uniform_buffer_object spec:
611 *
612 * "For uniform blocks laid out according to [std140] rules,
613 * the minimum buffer object size returned by the
614 * UNIFORM_BLOCK_DATA_SIZE query is derived by taking the
615 * offset of the last basic machine unit consumed by the
616 * last uniform of the uniform block (including any
617 * end-of-array or end-of-structure padding), adding one,
618 * and rounding up to the next multiple of the base
619 * alignment required for a vec4."
620 */
621 block->UniformBufferSize = glsl_align(offset, 16);
622 }
623 }
624
625 void
626 link_assign_uniform_locations(struct gl_shader_program *prog)
627 {
628 ralloc_free(prog->UniformStorage);
629 prog->UniformStorage = NULL;
630 prog->NumUserUniformStorage = 0;
631
632 if (prog->UniformHash != NULL) {
633 prog->UniformHash->clear();
634 } else {
635 prog->UniformHash = new string_to_uint_map;
636 }
637
638 /* First pass: Count the uniform resources used by the user-defined
639 * uniforms. While this happens, each active uniform will have an index
640 * assigned to it.
641 *
642 * Note: this is *NOT* the index that is returned to the application by
643 * glGetUniformLocation.
644 */
645 count_uniform_size uniform_size(prog->UniformHash);
646 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
647 struct gl_shader *sh = prog->_LinkedShaders[i];
648
649 if (sh == NULL)
650 continue;
651
652 /* Uniforms that lack an initializer in the shader code have an initial
653 * value of zero. This includes sampler uniforms.
654 *
655 * Page 24 (page 30 of the PDF) of the GLSL 1.20 spec says:
656 *
657 * "The link time initial value is either the value of the variable's
658 * initializer, if present, or 0 if no initializer is present. Sampler
659 * types cannot have initializers."
660 */
661 memset(sh->SamplerUnits, 0, sizeof(sh->SamplerUnits));
662
663 link_update_uniform_buffer_variables(sh);
664
665 /* Reset various per-shader target counts.
666 */
667 uniform_size.start_shader();
668
669 foreach_list(node, sh->ir) {
670 ir_variable *const var = ((ir_instruction *) node)->as_variable();
671
672 if ((var == NULL) || (var->mode != ir_var_uniform))
673 continue;
674
675 /* FINISHME: Update code to process built-in uniforms!
676 */
677 if (strncmp("gl_", var->name, 3) == 0) {
678 uniform_size.num_shader_uniform_components +=
679 var->type->component_slots();
680 continue;
681 }
682
683 uniform_size.process(var);
684 }
685
686 sh->num_samplers = uniform_size.num_shader_samplers;
687 sh->num_uniform_components = uniform_size.num_shader_uniform_components;
688
689 sh->num_combined_uniform_components = sh->num_uniform_components;
690 for (unsigned i = 0; i < sh->NumUniformBlocks; i++) {
691 sh->num_combined_uniform_components +=
692 sh->UniformBlocks[i].UniformBufferSize / 4;
693 }
694 }
695
696 const unsigned num_user_uniforms = uniform_size.num_active_uniforms;
697 const unsigned num_data_slots = uniform_size.num_values;
698
699 /* On the outside chance that there were no uniforms, bail out.
700 */
701 if (num_user_uniforms == 0)
702 return;
703
704 struct gl_uniform_storage *uniforms =
705 rzalloc_array(prog, struct gl_uniform_storage, num_user_uniforms);
706 union gl_constant_value *data =
707 rzalloc_array(uniforms, union gl_constant_value, num_data_slots);
708 #ifndef NDEBUG
709 union gl_constant_value *data_end = &data[num_data_slots];
710 #endif
711
712 parcel_out_uniform_storage parcel(prog->UniformHash, uniforms, data);
713
714 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
715 if (prog->_LinkedShaders[i] == NULL)
716 continue;
717
718 parcel.start_shader((gl_shader_type)i);
719
720 foreach_list(node, prog->_LinkedShaders[i]->ir) {
721 ir_variable *const var = ((ir_instruction *) node)->as_variable();
722
723 if ((var == NULL) || (var->mode != ir_var_uniform))
724 continue;
725
726 /* FINISHME: Update code to process built-in uniforms!
727 */
728 if (strncmp("gl_", var->name, 3) == 0)
729 continue;
730
731 parcel.set_and_process(prog, var);
732 }
733
734 prog->_LinkedShaders[i]->active_samplers = parcel.shader_samplers_used;
735 prog->_LinkedShaders[i]->shadow_samplers = parcel.shader_shadow_samplers;
736
737 STATIC_ASSERT(sizeof(prog->_LinkedShaders[i]->SamplerTargets) == sizeof(parcel.targets));
738 memcpy(prog->_LinkedShaders[i]->SamplerTargets, parcel.targets,
739 sizeof(prog->_LinkedShaders[i]->SamplerTargets));
740 }
741
742 /* Determine the size of the largest uniform array queryable via
743 * glGetUniformLocation. Using this as the location scale guarantees that
744 * there is enough "room" for the array index to be stored in the low order
745 * part of the uniform location. It also makes the locations be more
746 * tightly packed.
747 */
748 unsigned max_array_size = 1;
749 for (unsigned i = 0; i < num_user_uniforms; i++) {
750 if (uniforms[i].array_elements > max_array_size)
751 max_array_size = uniforms[i].array_elements;
752 }
753
754 prog->UniformLocationBaseScale = max_array_size;
755
756 #ifndef NDEBUG
757 for (unsigned i = 0; i < num_user_uniforms; i++) {
758 assert(uniforms[i].storage != NULL);
759 }
760
761 assert(parcel.values == data_end);
762 #endif
763
764 prog->NumUserUniformStorage = num_user_uniforms;
765 prog->UniformStorage = uniforms;
766
767 link_set_uniform_initializers(prog);
768
769 return;
770 }