nir/drawpixels: handle load_color0, load_input, load_interpolated_input
[mesa.git] / src / compiler / 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 #include "linker_util.h"
27
28 static link_uniform_block_active *
29 process_block(void *mem_ctx, struct hash_table *ht, ir_variable *var)
30 {
31 const hash_entry *const existing_block =
32 _mesa_hash_table_search(ht, var->get_interface_type()->name);
33
34 const glsl_type *const block_type = var->is_interface_instance()
35 ? var->type : var->get_interface_type();
36
37
38 /* If a block with this block-name has not previously been seen, add it.
39 * If a block with this block-name has been seen, it must be identical to
40 * the block currently being examined.
41 */
42 if (existing_block == NULL) {
43 link_uniform_block_active *const b =
44 rzalloc(mem_ctx, struct link_uniform_block_active);
45
46 b->type = block_type;
47 b->has_instance_name = var->is_interface_instance();
48 b->is_shader_storage = var->data.mode == ir_var_shader_storage;
49
50 if (var->data.explicit_binding) {
51 b->has_binding = true;
52 b->binding = var->data.binding;
53 } else {
54 b->has_binding = false;
55 b->binding = 0;
56 }
57
58 _mesa_hash_table_insert(ht, var->get_interface_type()->name, (void *) b);
59 return b;
60 } else {
61 link_uniform_block_active *const b =
62 (link_uniform_block_active *) existing_block->data;
63
64 if (b->type != block_type
65 || b->has_instance_name != var->is_interface_instance())
66 return NULL;
67 else
68 return b;
69 }
70
71 assert(!"Should not get here.");
72 return NULL;
73 }
74
75 /* For arrays of arrays this function will give us a middle ground between
76 * detecting inactive uniform blocks and structuring them in a way that makes
77 * it easy to calculate the offset for indirect indexing.
78 *
79 * For example given the shader:
80 *
81 * uniform ArraysOfArraysBlock
82 * {
83 * vec4 a;
84 * } i[3][4][5];
85 *
86 * void main()
87 * {
88 * vec4 b = i[0][1][1].a;
89 * gl_Position = i[2][2][3].a + b;
90 * }
91 *
92 * There are only 2 active blocks above but for the sake of indirect indexing
93 * and not over complicating the code we will end up with a count of 8. Here
94 * each dimension has 2 different indices counted so we end up with 2*2*2
95 */
96 static struct uniform_block_array_elements **
97 process_arrays(void *mem_ctx, ir_dereference_array *ir,
98 struct link_uniform_block_active *block)
99 {
100 if (ir) {
101 struct uniform_block_array_elements **ub_array_ptr =
102 process_arrays(mem_ctx, ir->array->as_dereference_array(), block);
103 if (*ub_array_ptr == NULL) {
104 *ub_array_ptr = rzalloc(mem_ctx, struct uniform_block_array_elements);
105 (*ub_array_ptr)->ir = ir;
106 }
107
108 struct uniform_block_array_elements *ub_array = *ub_array_ptr;
109 ir_constant *c = ir->array_index->as_constant();
110 if (c) {
111 /* Index is a constant, so mark just that element used, if not
112 * already.
113 */
114 const unsigned idx = c->get_uint_component(0);
115
116 unsigned i;
117 for (i = 0; i < ub_array->num_array_elements; i++) {
118 if (ub_array->array_elements[i] == idx)
119 break;
120 }
121
122 assert(i <= ub_array->num_array_elements);
123
124 if (i == ub_array->num_array_elements) {
125 ub_array->array_elements = reralloc(mem_ctx,
126 ub_array->array_elements,
127 unsigned,
128 ub_array->num_array_elements + 1);
129
130 ub_array->array_elements[ub_array->num_array_elements] = idx;
131
132 ub_array->num_array_elements++;
133 }
134 } else {
135 /* The array index is not a constant, so mark the entire array used. */
136 assert(ir->array->type->is_array());
137 if (ub_array->num_array_elements < ir->array->type->length) {
138 ub_array->num_array_elements = ir->array->type->length;
139 ub_array->array_elements = reralloc(mem_ctx,
140 ub_array->array_elements,
141 unsigned,
142 ub_array->num_array_elements);
143
144 for (unsigned i = 0; i < ub_array->num_array_elements; i++) {
145 ub_array->array_elements[i] = i;
146 }
147 }
148 }
149
150 return &ub_array->array;
151 } else {
152 return &block->array;
153 }
154 }
155
156 ir_visitor_status
157 link_uniform_block_active_visitor::visit(ir_variable *var)
158 {
159 if (!var->is_in_buffer_block())
160 return visit_continue;
161
162 /* Section 2.11.6 (Uniform Variables) of the OpenGL ES 3.0.3 spec says:
163 *
164 * "All members of a named uniform block declared with a shared or
165 * std140 layout qualifier are considered active, even if they are not
166 * referenced in any shader in the program. The uniform block itself is
167 * also considered active, even if no member of the block is
168 * referenced."
169 */
170 if (var->get_interface_type_packing() == GLSL_INTERFACE_PACKING_PACKED)
171 return visit_continue;
172
173 /* Process the block. Bail if there was an error. */
174 link_uniform_block_active *const b =
175 process_block(this->mem_ctx, this->ht, var);
176 if (b == NULL) {
177 linker_error(this->prog,
178 "uniform block `%s' has mismatching definitions",
179 var->get_interface_type()->name);
180 this->success = false;
181 return visit_stop;
182 }
183
184 assert(b->array == NULL);
185 assert(b->type != NULL);
186 assert(!b->type->is_array() || b->has_instance_name);
187
188 /* For uniform block arrays declared with a shared or std140 layout
189 * qualifier, mark all its instances as used.
190 */
191 const glsl_type *type = b->type;
192 struct uniform_block_array_elements **ub_array = &b->array;
193 while (type->is_array()) {
194 assert(b->type->length > 0);
195
196 *ub_array = rzalloc(this->mem_ctx, struct uniform_block_array_elements);
197 (*ub_array)->num_array_elements = type->length;
198 (*ub_array)->array_elements = reralloc(this->mem_ctx,
199 (*ub_array)->array_elements,
200 unsigned,
201 (*ub_array)->num_array_elements);
202
203 for (unsigned i = 0; i < (*ub_array)->num_array_elements; i++) {
204 (*ub_array)->array_elements[i] = i;
205 }
206 ub_array = &(*ub_array)->array;
207 type = type->fields.array;
208 }
209
210 return visit_continue;
211 }
212
213 ir_visitor_status
214 link_uniform_block_active_visitor::visit_enter(ir_dereference_array *ir)
215 {
216 /* cycle through arrays of arrays */
217 ir_dereference_array *base_ir = ir;
218 while (base_ir->array->ir_type == ir_type_dereference_array)
219 base_ir = base_ir->array->as_dereference_array();
220
221 ir_dereference_variable *const d =
222 base_ir->array->as_dereference_variable();
223 ir_variable *const var = (d == NULL) ? NULL : d->var;
224
225 /* If the r-value being dereferenced is not a variable (e.g., a field of a
226 * structure) or is not a uniform block instance, continue.
227 *
228 * WARNING: It is not enough for the variable to be part of uniform block.
229 * It must represent the entire block. Arrays (or matrices) inside blocks
230 * that lack an instance name are handled by the ir_dereference_variable
231 * function.
232 */
233 if (var == NULL
234 || !var->is_in_buffer_block()
235 || !var->is_interface_instance())
236 return visit_continue;
237
238 /* Process the block. Bail if there was an error. */
239 link_uniform_block_active *const b =
240 process_block(this->mem_ctx, this->ht, var);
241 if (b == NULL) {
242 linker_error(prog,
243 "uniform block `%s' has mismatching definitions",
244 var->get_interface_type()->name);
245 this->success = false;
246 return visit_stop;
247 }
248
249 /* Block arrays must be declared with an instance name.
250 */
251 assert(b->has_instance_name);
252 assert(b->type != NULL);
253
254 /* If the block array was declared with a shared or std140 layout
255 * qualifier, all its instances have been already marked as used in
256 * link_uniform_block_active_visitor::visit(ir_variable *).
257 */
258 if (var->get_interface_type_packing() == GLSL_INTERFACE_PACKING_PACKED) {
259 b->var = var;
260 process_arrays(this->mem_ctx, ir, b);
261 }
262
263 return visit_continue_with_parent;
264 }
265
266 ir_visitor_status
267 link_uniform_block_active_visitor::visit(ir_dereference_variable *ir)
268 {
269 ir_variable *var = ir->var;
270
271 if (!var->is_in_buffer_block())
272 return visit_continue;
273
274 assert(!var->is_interface_instance() || !var->type->is_array());
275
276 /* Process the block. Bail if there was an error. */
277 link_uniform_block_active *const b =
278 process_block(this->mem_ctx, this->ht, var);
279 if (b == NULL) {
280 linker_error(this->prog,
281 "uniform block `%s' has mismatching definitions",
282 var->get_interface_type()->name);
283 this->success = false;
284 return visit_stop;
285 }
286
287 assert(b->array == NULL);
288 assert(b->type != NULL);
289
290 return visit_continue;
291 }