glsl: Add new uniform_field_visitor::visit_field variant
[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 static inline unsigned int
33 align(unsigned int a, unsigned int align)
34 {
35 return (a + align - 1) / align * align;
36 }
37
38 /**
39 * \file link_uniforms.cpp
40 * Assign locations for GLSL uniforms.
41 *
42 * \author Ian Romanick <ian.d.romanick@intel.com>
43 */
44
45 /**
46 * Count the backing storage requirements for a type
47 */
48 static unsigned
49 values_for_type(const glsl_type *type)
50 {
51 if (type->is_sampler()) {
52 return 1;
53 } else if (type->is_array() && type->fields.array->is_sampler()) {
54 return type->array_size();
55 } else {
56 return type->component_slots();
57 }
58 }
59
60 void
61 uniform_field_visitor::process(ir_variable *var)
62 {
63 const glsl_type *t = var->type;
64
65 /* false is always passed for the row_major parameter to the other
66 * processing functions because no information is available to do
67 * otherwise. See the warning in linker.h.
68 */
69
70 /* Only strdup the name if we actually will need to modify it. */
71 if (t->is_record() || (t->is_array() && t->fields.array->is_record())) {
72 char *name = ralloc_strdup(NULL, var->name);
73 recursion(var->type, &name, strlen(name), false);
74 ralloc_free(name);
75 } else {
76 this->visit_field(t, var->name, false);
77 }
78 }
79
80 void
81 uniform_field_visitor::recursion(const glsl_type *t, char **name,
82 size_t name_length, bool row_major)
83 {
84 /* Records need to have each field processed individually.
85 *
86 * Arrays of records need to have each array element processed
87 * individually, then each field of the resulting array elements processed
88 * individually.
89 */
90 if (t->is_record()) {
91 for (unsigned i = 0; i < t->length; i++) {
92 const char *field = t->fields.structure[i].name;
93 size_t new_length = name_length;
94
95 /* Append '.field' to the current uniform name. */
96 ralloc_asprintf_rewrite_tail(name, &new_length, ".%s", field);
97
98 recursion(t->fields.structure[i].type, name, new_length,
99 t->fields.structure[i].row_major);
100 }
101 } else if (t->is_array() && t->fields.array->is_record()) {
102 for (unsigned i = 0; i < t->length; i++) {
103 size_t new_length = name_length;
104
105 /* Append the subscript to the current uniform name */
106 ralloc_asprintf_rewrite_tail(name, &new_length, "[%u]", i);
107
108 recursion(t->fields.array, name, new_length,
109 t->fields.structure[i].row_major);
110 }
111 } else {
112 this->visit_field(t, *name, row_major);
113 }
114 }
115
116 void
117 uniform_field_visitor::visit_field(const glsl_struct_field *field)
118 {
119 (void) field;
120 /* empty */
121 }
122
123 /**
124 * Class to help calculate the storage requirements for a set of uniforms
125 *
126 * As uniforms are added to the active set the number of active uniforms and
127 * the storage requirements for those uniforms are accumulated. The active
128 * uniforms are added the the hash table supplied to the constructor.
129 *
130 * If the same uniform is added multiple times (i.e., once for each shader
131 * target), it will only be accounted once.
132 */
133 class count_uniform_size : public uniform_field_visitor {
134 public:
135 count_uniform_size(struct string_to_uint_map *map)
136 : num_active_uniforms(0), num_values(0), num_shader_samplers(0),
137 num_shader_uniform_components(0), map(map)
138 {
139 /* empty */
140 }
141
142 void start_shader()
143 {
144 this->num_shader_samplers = 0;
145 this->num_shader_uniform_components = 0;
146 }
147
148 /**
149 * Total number of active uniforms counted
150 */
151 unsigned num_active_uniforms;
152
153 /**
154 * Number of data values required to back the storage for the active uniforms
155 */
156 unsigned num_values;
157
158 /**
159 * Number of samplers used
160 */
161 unsigned num_shader_samplers;
162
163 /**
164 * Number of uniforms used in the current shader
165 */
166 unsigned num_shader_uniform_components;
167
168 private:
169 virtual void visit_field(const glsl_type *type, const char *name,
170 bool row_major)
171 {
172 assert(!type->is_record());
173 assert(!(type->is_array() && type->fields.array->is_record()));
174
175 (void) row_major;
176
177 /* Count the number of samplers regardless of whether the uniform is
178 * already in the hash table. The hash table prevents adding the same
179 * uniform for multiple shader targets, but in this case we want to
180 * count it for each shader target.
181 */
182 const unsigned values = values_for_type(type);
183 if (type->contains_sampler()) {
184 this->num_shader_samplers +=
185 type->is_array() ? type->array_size() : 1;
186 } else {
187 /* Accumulate the total number of uniform slots used by this shader.
188 * Note that samplers do not count against this limit because they
189 * don't use any storage on current hardware.
190 */
191 this->num_shader_uniform_components += values;
192 }
193
194 /* If the uniform is already in the map, there's nothing more to do.
195 */
196 unsigned id;
197 if (this->map->get(id, name))
198 return;
199
200 this->map->put(this->num_active_uniforms, name);
201
202 /* Each leaf uniform occupies one entry in the list of active
203 * uniforms.
204 */
205 this->num_active_uniforms++;
206 this->num_values += values;
207 }
208
209 struct string_to_uint_map *map;
210 };
211
212 /**
213 * Class to help parcel out pieces of backing storage to uniforms
214 *
215 * Each uniform processed has some range of the \c gl_constant_value
216 * structures associated with it. The association is done by finding
217 * the uniform in the \c string_to_uint_map and using the value from
218 * the map to connect that slot in the \c gl_uniform_storage table
219 * with the next available slot in the \c gl_constant_value array.
220 *
221 * \warning
222 * This class assumes that every uniform that will be processed is
223 * already in the \c string_to_uint_map. In addition, it assumes that
224 * the \c gl_uniform_storage and \c gl_constant_value arrays are "big
225 * enough."
226 */
227 class parcel_out_uniform_storage : public uniform_field_visitor {
228 public:
229 parcel_out_uniform_storage(struct string_to_uint_map *map,
230 struct gl_uniform_storage *uniforms,
231 union gl_constant_value *values)
232 : map(map), uniforms(uniforms), next_sampler(0), values(values)
233 {
234 memset(this->targets, 0, sizeof(this->targets));
235 }
236
237 void start_shader()
238 {
239 this->shader_samplers_used = 0;
240 this->shader_shadow_samplers = 0;
241 }
242
243 void set_and_process(struct gl_shader_program *prog,
244 struct gl_shader *shader,
245 ir_variable *var)
246 {
247 ubo_var = NULL;
248 if (var->is_in_uniform_block()) {
249 struct gl_uniform_block *block =
250 &shader->UniformBlocks[var->uniform_block];
251
252 ubo_block_index = -1;
253 for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
254 if (!strcmp(prog->UniformBlocks[i].Name,
255 shader->UniformBlocks[var->uniform_block].Name)) {
256 ubo_block_index = i;
257 break;
258 }
259 }
260 assert(ubo_block_index != -1);
261
262 ubo_var_index = var->location;
263 ubo_var = &block->Uniforms[var->location];
264 ubo_byte_offset = ubo_var->Offset;
265 }
266
267 process(var);
268 }
269
270 struct gl_uniform_buffer_variable *ubo_var;
271 int ubo_block_index;
272 int ubo_var_index;
273 int ubo_byte_offset;
274
275 private:
276 virtual void visit_field(const glsl_type *type, const char *name,
277 bool row_major)
278 {
279 assert(!type->is_record());
280 assert(!(type->is_array() && type->fields.array->is_record()));
281
282 (void) row_major;
283
284 unsigned id;
285 bool found = this->map->get(id, name);
286 assert(found);
287
288 if (!found)
289 return;
290
291 /* If there is already storage associated with this uniform, it means
292 * that it was set while processing an earlier shader stage. For
293 * example, we may be processing the uniform in the fragment shader, but
294 * the uniform was already processed in the vertex shader.
295 */
296 if (this->uniforms[id].storage != NULL) {
297 /* If the uniform already has storage set from another shader stage,
298 * mark the samplers used for this shader stage.
299 */
300 if (type->contains_sampler()) {
301 const unsigned count = MAX2(1, this->uniforms[id].array_elements);
302 const unsigned shadow = (type->is_array())
303 ? type->fields.array->sampler_shadow : type->sampler_shadow;
304
305 for (unsigned i = 0; i < count; i++) {
306 const unsigned s = this->uniforms[id].sampler + i;
307
308 this->shader_samplers_used |= 1U << s;
309 this->shader_shadow_samplers |= shadow << s;
310 }
311 }
312
313 return;
314 }
315
316 const glsl_type *base_type;
317 if (type->is_array()) {
318 this->uniforms[id].array_elements = type->length;
319 base_type = type->fields.array;
320 } else {
321 this->uniforms[id].array_elements = 0;
322 base_type = type;
323 }
324
325 if (base_type->is_sampler()) {
326 this->uniforms[id].sampler = this->next_sampler;
327
328 /* Increment the sampler by 1 for non-arrays and by the number of
329 * array elements for arrays.
330 */
331 this->next_sampler += MAX2(1, this->uniforms[id].array_elements);
332
333 const gl_texture_index target = base_type->sampler_index();
334 const unsigned shadow = base_type->sampler_shadow;
335 for (unsigned i = this->uniforms[id].sampler
336 ; i < MIN2(this->next_sampler, MAX_SAMPLERS)
337 ; i++) {
338 this->targets[i] = target;
339 this->shader_samplers_used |= 1U << i;
340 this->shader_shadow_samplers |= shadow << i;
341 }
342
343 } else {
344 this->uniforms[id].sampler = ~0;
345 }
346
347 this->uniforms[id].name = ralloc_strdup(this->uniforms, name);
348 this->uniforms[id].type = base_type;
349 this->uniforms[id].initialized = 0;
350 this->uniforms[id].num_driver_storage = 0;
351 this->uniforms[id].driver_storage = NULL;
352 this->uniforms[id].storage = this->values;
353 if (this->ubo_var) {
354 this->uniforms[id].block_index = this->ubo_block_index;
355
356 unsigned alignment = type->std140_base_alignment(ubo_var->RowMajor);
357 this->ubo_byte_offset = align(this->ubo_byte_offset, alignment);
358 this->uniforms[id].offset = this->ubo_byte_offset;
359 this->ubo_byte_offset += type->std140_size(ubo_var->RowMajor);
360
361 if (type->is_array()) {
362 this->uniforms[id].array_stride =
363 align(type->fields.array->std140_size(ubo_var->RowMajor), 16);
364 } else {
365 this->uniforms[id].array_stride = 0;
366 }
367
368 if (type->is_matrix() ||
369 (type->is_array() && type->fields.array->is_matrix())) {
370 this->uniforms[id].matrix_stride = 16;
371 this->uniforms[id].row_major = ubo_var->RowMajor;
372 } else {
373 this->uniforms[id].matrix_stride = 0;
374 this->uniforms[id].row_major = false;
375 }
376 } else {
377 this->uniforms[id].block_index = -1;
378 this->uniforms[id].offset = -1;
379 this->uniforms[id].array_stride = -1;
380 this->uniforms[id].matrix_stride = -1;
381 this->uniforms[id].row_major = false;
382 }
383
384 this->values += values_for_type(type);
385 }
386
387 struct string_to_uint_map *map;
388
389 struct gl_uniform_storage *uniforms;
390 unsigned next_sampler;
391
392 public:
393 union gl_constant_value *values;
394
395 gl_texture_index targets[MAX_SAMPLERS];
396
397 /**
398 * Mask of samplers used by the current shader stage.
399 */
400 unsigned shader_samplers_used;
401
402 /**
403 * Mask of samplers used by the current shader stage for shadows.
404 */
405 unsigned shader_shadow_samplers;
406 };
407
408 /**
409 * Merges a uniform block into an array of uniform blocks that may or
410 * may not already contain a copy of it.
411 *
412 * Returns the index of the new block in the array.
413 */
414 int
415 link_cross_validate_uniform_block(void *mem_ctx,
416 struct gl_uniform_block **linked_blocks,
417 unsigned int *num_linked_blocks,
418 struct gl_uniform_block *new_block)
419 {
420 for (unsigned int i = 0; i < *num_linked_blocks; i++) {
421 struct gl_uniform_block *old_block = &(*linked_blocks)[i];
422
423 if (strcmp(old_block->Name, new_block->Name) == 0)
424 return link_uniform_blocks_are_compatible(old_block, new_block)
425 ? i : -1;
426 }
427
428 *linked_blocks = reralloc(mem_ctx, *linked_blocks,
429 struct gl_uniform_block,
430 *num_linked_blocks + 1);
431 int linked_block_index = (*num_linked_blocks)++;
432 struct gl_uniform_block *linked_block = &(*linked_blocks)[linked_block_index];
433
434 memcpy(linked_block, new_block, sizeof(*new_block));
435 linked_block->Uniforms = ralloc_array(*linked_blocks,
436 struct gl_uniform_buffer_variable,
437 linked_block->NumUniforms);
438
439 memcpy(linked_block->Uniforms,
440 new_block->Uniforms,
441 sizeof(*linked_block->Uniforms) * linked_block->NumUniforms);
442
443 for (unsigned int i = 0; i < linked_block->NumUniforms; i++) {
444 struct gl_uniform_buffer_variable *ubo_var =
445 &linked_block->Uniforms[i];
446
447 ubo_var->Name = ralloc_strdup(*linked_blocks, ubo_var->Name);
448 }
449
450 return linked_block_index;
451 }
452
453 /**
454 * Walks the IR and update the references to uniform blocks in the
455 * ir_variables to point at linked shader's list (previously, they
456 * would point at the uniform block list in one of the pre-linked
457 * shaders).
458 */
459 static void
460 link_update_uniform_buffer_variables(struct gl_shader *shader)
461 {
462 foreach_list(node, shader->ir) {
463 ir_variable *const var = ((ir_instruction *) node)->as_variable();
464
465 if ((var == NULL) || !var->is_in_uniform_block())
466 continue;
467
468 assert(var->mode == ir_var_uniform);
469
470 bool found = false;
471 for (unsigned i = 0; i < shader->NumUniformBlocks; i++) {
472 for (unsigned j = 0; j < shader->UniformBlocks[i].NumUniforms; j++) {
473 if (!strcmp(var->name, shader->UniformBlocks[i].Uniforms[j].Name)) {
474 found = true;
475 var->uniform_block = i;
476 var->location = j;
477 break;
478 }
479 }
480 if (found)
481 break;
482 }
483 assert(found);
484 }
485 }
486
487 void
488 link_assign_uniform_block_offsets(struct gl_shader *shader)
489 {
490 for (unsigned b = 0; b < shader->NumUniformBlocks; b++) {
491 struct gl_uniform_block *block = &shader->UniformBlocks[b];
492
493 unsigned offset = 0;
494 for (unsigned int i = 0; i < block->NumUniforms; i++) {
495 struct gl_uniform_buffer_variable *ubo_var = &block->Uniforms[i];
496 const struct glsl_type *type = ubo_var->Type;
497
498 unsigned alignment = type->std140_base_alignment(ubo_var->RowMajor);
499 unsigned size = type->std140_size(ubo_var->RowMajor);
500
501 offset = align(offset, alignment);
502 ubo_var->Offset = offset;
503 offset += size;
504 }
505
506 /* From the GL_ARB_uniform_buffer_object spec:
507 *
508 * "For uniform blocks laid out according to [std140] rules,
509 * the minimum buffer object size returned by the
510 * UNIFORM_BLOCK_DATA_SIZE query is derived by taking the
511 * offset of the last basic machine unit consumed by the
512 * last uniform of the uniform block (including any
513 * end-of-array or end-of-structure padding), adding one,
514 * and rounding up to the next multiple of the base
515 * alignment required for a vec4."
516 */
517 block->UniformBufferSize = align(offset, 16);
518 }
519 }
520
521 void
522 link_assign_uniform_locations(struct gl_shader_program *prog)
523 {
524 ralloc_free(prog->UniformStorage);
525 prog->UniformStorage = NULL;
526 prog->NumUserUniformStorage = 0;
527
528 if (prog->UniformHash != NULL) {
529 prog->UniformHash->clear();
530 } else {
531 prog->UniformHash = new string_to_uint_map;
532 }
533
534 /* Uniforms that lack an initializer in the shader code have an initial
535 * value of zero. This includes sampler uniforms.
536 *
537 * Page 24 (page 30 of the PDF) of the GLSL 1.20 spec says:
538 *
539 * "The link time initial value is either the value of the variable's
540 * initializer, if present, or 0 if no initializer is present. Sampler
541 * types cannot have initializers."
542 */
543 memset(prog->SamplerUnits, 0, sizeof(prog->SamplerUnits));
544
545 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
546 if (prog->_LinkedShaders[i] == NULL)
547 continue;
548
549 link_update_uniform_buffer_variables(prog->_LinkedShaders[i]);
550 }
551
552 /* First pass: Count the uniform resources used by the user-defined
553 * uniforms. While this happens, each active uniform will have an index
554 * assigned to it.
555 *
556 * Note: this is *NOT* the index that is returned to the application by
557 * glGetUniformLocation.
558 */
559 count_uniform_size uniform_size(prog->UniformHash);
560 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
561 if (prog->_LinkedShaders[i] == NULL)
562 continue;
563
564 /* Reset various per-shader target counts.
565 */
566 uniform_size.start_shader();
567
568 foreach_list(node, prog->_LinkedShaders[i]->ir) {
569 ir_variable *const var = ((ir_instruction *) node)->as_variable();
570
571 if ((var == NULL) || (var->mode != ir_var_uniform))
572 continue;
573
574 /* FINISHME: Update code to process built-in uniforms!
575 */
576 if (strncmp("gl_", var->name, 3) == 0) {
577 uniform_size.num_shader_uniform_components +=
578 var->type->component_slots();
579 continue;
580 }
581
582 uniform_size.process(var);
583 }
584
585 prog->_LinkedShaders[i]->num_samplers = uniform_size.num_shader_samplers;
586 prog->_LinkedShaders[i]->num_uniform_components =
587 uniform_size.num_shader_uniform_components;
588 }
589
590 const unsigned num_user_uniforms = uniform_size.num_active_uniforms;
591 const unsigned num_data_slots = uniform_size.num_values;
592
593 /* On the outside chance that there were no uniforms, bail out.
594 */
595 if (num_user_uniforms == 0)
596 return;
597
598 struct gl_uniform_storage *uniforms =
599 rzalloc_array(prog, struct gl_uniform_storage, num_user_uniforms);
600 union gl_constant_value *data =
601 rzalloc_array(uniforms, union gl_constant_value, num_data_slots);
602 #ifndef NDEBUG
603 union gl_constant_value *data_end = &data[num_data_slots];
604 #endif
605
606 parcel_out_uniform_storage parcel(prog->UniformHash, uniforms, data);
607
608 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
609 if (prog->_LinkedShaders[i] == NULL)
610 continue;
611
612 /* Reset various per-shader target counts.
613 */
614 parcel.start_shader();
615
616 foreach_list(node, prog->_LinkedShaders[i]->ir) {
617 ir_variable *const var = ((ir_instruction *) node)->as_variable();
618
619 if ((var == NULL) || (var->mode != ir_var_uniform))
620 continue;
621
622 /* FINISHME: Update code to process built-in uniforms!
623 */
624 if (strncmp("gl_", var->name, 3) == 0)
625 continue;
626
627 parcel.set_and_process(prog, prog->_LinkedShaders[i], var);
628 }
629
630 prog->_LinkedShaders[i]->active_samplers = parcel.shader_samplers_used;
631 prog->_LinkedShaders[i]->shadow_samplers = parcel.shader_shadow_samplers;
632 }
633
634 assert(sizeof(prog->SamplerTargets) == sizeof(parcel.targets));
635 memcpy(prog->SamplerTargets, parcel.targets, sizeof(prog->SamplerTargets));
636
637 #ifndef NDEBUG
638 for (unsigned i = 0; i < num_user_uniforms; i++) {
639 assert(uniforms[i].storage != NULL);
640 }
641
642 assert(parcel.values == data_end);
643 #endif
644
645 prog->NumUserUniformStorage = num_user_uniforms;
646 prog->UniformStorage = uniforms;
647
648 link_set_uniform_initializers(prog);
649
650 return;
651 }