glsl: Mark as active all elements of shared/std140 block arrays
[mesa.git] / src / glsl / link_uniform_block_active_visitor.cpp
1 /*
2 * Copyright © 2013 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 "link_uniform_block_active_visitor.h"
25 #include "program.h"
26
27 link_uniform_block_active *
28 process_block(void *mem_ctx, struct hash_table *ht, ir_variable *var)
29 {
30 const hash_entry *const existing_block =
31 _mesa_hash_table_search(ht, var->get_interface_type()->name);
32
33 const glsl_type *const block_type = var->is_interface_instance()
34 ? var->type : var->get_interface_type();
35
36
37 /* If a block with this block-name has not previously been seen, add it.
38 * If a block with this block-name has been seen, it must be identical to
39 * the block currently being examined.
40 */
41 if (existing_block == NULL) {
42 link_uniform_block_active *const b =
43 rzalloc(mem_ctx, struct link_uniform_block_active);
44
45 b->type = block_type;
46 b->has_instance_name = var->is_interface_instance();
47 b->is_shader_storage = var->data.mode == ir_var_shader_storage;
48
49 if (var->data.explicit_binding) {
50 b->has_binding = true;
51 b->binding = var->data.binding;
52 } else {
53 b->has_binding = false;
54 b->binding = 0;
55 }
56
57 _mesa_hash_table_insert(ht, var->get_interface_type()->name, (void *) b);
58 return b;
59 } else {
60 link_uniform_block_active *const b =
61 (link_uniform_block_active *) existing_block->data;
62
63 if (b->type != block_type
64 || b->has_instance_name != var->is_interface_instance())
65 return NULL;
66 else
67 return b;
68 }
69
70 assert(!"Should not get here.");
71 return NULL;
72 }
73
74 ir_visitor_status
75 link_uniform_block_active_visitor::visit(ir_variable *var)
76 {
77 if (!var->is_in_buffer_block())
78 return visit_continue;
79
80 /* Section 2.11.6 (Uniform Variables) of the OpenGL ES 3.0.3 spec says:
81 *
82 * "All members of a named uniform block declared with a shared or
83 * std140 layout qualifier are considered active, even if they are not
84 * referenced in any shader in the program. The uniform block itself is
85 * also considered active, even if no member of the block is
86 * referenced."
87 */
88 if (var->get_interface_type()->interface_packing ==
89 GLSL_INTERFACE_PACKING_PACKED)
90 return visit_continue;
91
92 /* Process the block. Bail if there was an error.
93 */
94 link_uniform_block_active *const b =
95 process_block(this->mem_ctx, this->ht, var);
96 if (b == NULL) {
97 linker_error(this->prog,
98 "uniform block `%s' has mismatching definitions",
99 var->get_interface_type()->name);
100 this->success = false;
101 return visit_stop;
102 }
103
104 assert(b->num_array_elements == 0);
105 assert(b->array_elements == NULL);
106 assert(b->type != NULL);
107 assert(!b->type->is_array() || b->has_instance_name);
108
109 /* For uniform block arrays declared with a shared or std140 layout
110 * qualifier, mark all its instances as used.
111 */
112 if (b->type->is_array() && b->type->length > 0) {
113 b->num_array_elements = b->type->length;
114 b->array_elements = reralloc(this->mem_ctx,
115 b->array_elements,
116 unsigned,
117 b->num_array_elements);
118
119 for (unsigned i = 0; i < b->num_array_elements; i++) {
120 b->array_elements[i] = i;
121 }
122 }
123
124 return visit_continue;
125 }
126
127 ir_visitor_status
128 link_uniform_block_active_visitor::visit_enter(ir_dereference_array *ir)
129 {
130 ir_dereference_variable *const d = ir->array->as_dereference_variable();
131 ir_variable *const var = (d == NULL) ? NULL : d->var;
132
133 /* If the r-value being dereferenced is not a variable (e.g., a field of a
134 * structure) or is not a uniform block instance, continue.
135 *
136 * WARNING: It is not enough for the variable to be part of uniform block.
137 * It must represent the entire block. Arrays (or matrices) inside blocks
138 * that lack an instance name are handled by the ir_dereference_variable
139 * function.
140 */
141 if (var == NULL
142 || !var->is_in_buffer_block()
143 || !var->is_interface_instance())
144 return visit_continue;
145
146 /* Process the block. Bail if there was an error.
147 */
148 link_uniform_block_active *const b =
149 process_block(this->mem_ctx, this->ht, var);
150 if (b == NULL) {
151 linker_error(prog,
152 "uniform block `%s' has mismatching definitions",
153 var->get_interface_type()->name);
154 this->success = false;
155 return visit_stop;
156 }
157
158 /* Block arrays must be declared with an instance name.
159 */
160 assert(b->has_instance_name);
161 assert((b->num_array_elements == 0) == (b->array_elements == NULL));
162 assert(b->type != NULL);
163
164 /* If the block array was declared with a shared or
165 * std140 layout qualifier, all its instances have been already marked
166 * as used in link_uniform_block_active_visitor::visit(ir_variable *).
167 */
168 if (var->get_interface_type()->interface_packing !=
169 GLSL_INTERFACE_PACKING_PACKED)
170 return visit_continue_with_parent;
171
172 ir_constant *c = ir->array_index->as_constant();
173
174 if (c) {
175 /* Index is a constant, so mark just that element used, if not already */
176 const unsigned idx = c->get_uint_component(0);
177
178 unsigned i;
179 for (i = 0; i < b->num_array_elements; i++) {
180 if (b->array_elements[i] == idx)
181 break;
182 }
183
184 assert(i <= b->num_array_elements);
185
186 if (i == b->num_array_elements) {
187 b->array_elements = reralloc(this->mem_ctx,
188 b->array_elements,
189 unsigned,
190 b->num_array_elements + 1);
191
192 b->array_elements[b->num_array_elements] = idx;
193
194 b->num_array_elements++;
195 }
196 } else {
197 /* The array index is not a constant, so mark the entire array used. */
198 assert(b->type->is_array());
199 if (b->num_array_elements < b->type->length) {
200 b->num_array_elements = b->type->length;
201 b->array_elements = reralloc(this->mem_ctx,
202 b->array_elements,
203 unsigned,
204 b->num_array_elements);
205
206 for (unsigned i = 0; i < b->num_array_elements; i++) {
207 b->array_elements[i] = i;
208 }
209 }
210 }
211
212 return visit_continue_with_parent;
213 }
214
215 ir_visitor_status
216 link_uniform_block_active_visitor::visit(ir_dereference_variable *ir)
217 {
218 ir_variable *var = ir->var;
219
220 if (!var->is_in_buffer_block())
221 return visit_continue;
222
223 assert(!var->is_interface_instance() || !var->type->is_array());
224
225 /* Process the block. Bail if there was an error.
226 */
227 link_uniform_block_active *const b =
228 process_block(this->mem_ctx, this->ht, var);
229 if (b == NULL) {
230 linker_error(this->prog,
231 "uniform block `%s' has mismatching definitions",
232 var->get_interface_type()->name);
233 this->success = false;
234 return visit_stop;
235 }
236
237 assert(b->num_array_elements == 0);
238 assert(b->array_elements == NULL);
239 assert(b->type != NULL);
240
241 return visit_continue;
242 }