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