6dd1f535451434343f4ce49b4afa0ce6312638fe
[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 "glsl_symbol_table.h"
28 #include "program/hash_table.h"
29
30 /**
31 * \file link_uniforms.cpp
32 * Assign locations for GLSL uniforms.
33 *
34 * \author Ian Romanick <ian.d.romanick@intel.com>
35 */
36
37 void
38 uniform_field_visitor::process(ir_variable *var)
39 {
40 const glsl_type *t = var->type;
41
42 /* Only strdup the name if we actually will need to modify it. */
43 if (t->is_record() || (t->is_array() && t->fields.array->is_record())) {
44 char *name = ralloc_strdup(NULL, var->name);
45 recursion(var->type, &name, strlen(name));
46 ralloc_free(name);
47 } else {
48 this->visit_field(t, var->name);
49 }
50 }
51
52 void
53 uniform_field_visitor::recursion(const glsl_type *t, char **name,
54 unsigned name_length)
55 {
56 /* Records need to have each field processed individually.
57 *
58 * Arrays of records need to have each array element processed
59 * individually, then each field of the resulting array elements processed
60 * individually.
61 */
62 if (t->is_record()) {
63 for (unsigned i = 0; i < t->length; i++) {
64 const char *field = t->fields.structure[i].name;
65
66 /* Append '.field' to the current uniform name. */
67 ralloc_asprintf_rewrite_tail(name, name_length, ".%s", field);
68
69 recursion(t->fields.structure[i].type, name,
70 name_length + 1 + strlen(field));
71 }
72 } else if (t->is_array() && t->fields.array->is_record()) {
73 for (unsigned i = 0; i < t->length; i++) {
74 char subscript[13];
75
76 /* Append the subscript to the current uniform name */
77 const unsigned subscript_length = snprintf(subscript, 13, "[%u]", i);
78 ralloc_asprintf_rewrite_tail(name, name_length, "%s", subscript);
79
80 recursion(t->fields.array, name, name_length + subscript_length);
81 }
82 } else {
83 this->visit_field(t, *name);
84 }
85 }