glsl: Rework interface block linking.
[mesa.git] / src / glsl / link_interface_blocks.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 /**
25 * \file link_interface_blocks.cpp
26 * Linker support for GLSL's interface blocks.
27 */
28
29 #include "ir.h"
30 #include "glsl_symbol_table.h"
31 #include "linker.h"
32 #include "main/macros.h"
33 #include "program/hash_table.h"
34
35
36 namespace {
37
38 /**
39 * Information about a single interface block definition that we need to keep
40 * track of in order to check linkage rules.
41 *
42 * Note: this class is expected to be short lived, so it doesn't make copies
43 * of the strings it references; it simply borrows the pointers from the
44 * ir_variable class.
45 */
46 struct interface_block_definition
47 {
48 /**
49 * Extract an interface block definition from an ir_variable that
50 * represents either the interface instance (for named interfaces), or a
51 * member of the interface (for unnamed interfaces).
52 */
53 explicit interface_block_definition(const ir_variable *var)
54 : type(var->get_interface_type()),
55 instance_name(NULL),
56 array_size(-1)
57 {
58 if (var->is_interface_instance()) {
59 instance_name = var->name;
60 if (var->type->is_array())
61 array_size = var->type->length;
62 }
63 }
64
65 /**
66 * Interface block type
67 */
68 const glsl_type *type;
69
70 /**
71 * For a named interface block, the instance name. Otherwise NULL.
72 */
73 const char *instance_name;
74
75 /**
76 * For an interface block array, the array size (or 0 if unsized).
77 * Otherwise -1.
78 */
79 int array_size;
80 };
81
82
83 /**
84 * Check if two interfaces match, according to intrastage interface matching
85 * rules. If they do, and the first interface uses an unsized array, it will
86 * be updated to reflect the array size declared in the second interface.
87 */
88 bool
89 intrastage_match(interface_block_definition *a,
90 const interface_block_definition *b,
91 ir_variable_mode mode)
92 {
93 /* Types must match. */
94 if (a->type != b->type)
95 return false;
96
97 /* Presence/absence of interface names must match. */
98 if ((a->instance_name == NULL) != (b->instance_name == NULL))
99 return false;
100
101 /* For uniforms, instance names need not match. For shader ins/outs,
102 * it's not clear from the spec whether they need to match, but
103 * Mesa's implementation relies on them matching.
104 */
105 if (a->instance_name != NULL && mode != ir_var_uniform &&
106 strcmp(a->instance_name, b->instance_name) != 0) {
107 return false;
108 }
109
110 /* Array vs. nonarray must be consistent, and sizes must be
111 * consistent, with the exception that unsized arrays match sized
112 * arrays.
113 */
114 if ((a->array_size == -1) != (b->array_size == -1))
115 return false;
116 if (b->array_size != 0) {
117 if (a->array_size == 0)
118 a->array_size = b->array_size;
119 else if (a->array_size != b->array_size)
120 return false;
121 }
122
123 return true;
124 }
125
126
127 /**
128 * Check if two interfaces match, according to interstage (in/out) interface
129 * matching rules.
130 *
131 * If \c extra_array_level is true, then vertex-to-geometry shader matching
132 * rules are enforced (i.e. a successful match requires the consumer interface
133 * to be an array and the producer interface to be a non-array).
134 */
135 bool
136 interstage_match(const interface_block_definition *producer,
137 const interface_block_definition *consumer,
138 bool extra_array_level)
139 {
140 /* Unsized arrays should not occur during interstage linking. They
141 * should have all been assigned a size by link_intrastage_shaders.
142 */
143 assert(consumer->array_size != 0);
144 assert(producer->array_size != 0);
145
146 /* Types must match. */
147 if (consumer->type != producer->type)
148 return false;
149 if (extra_array_level) {
150 /* Consumer must be an array, and producer must not. */
151 if (consumer->array_size == -1)
152 return false;
153 if (producer->array_size != -1)
154 return false;
155 } else {
156 /* Array vs. nonarray must be consistent, and sizes must be consistent.
157 * Since unsized arrays have been ruled out, we can check this by just
158 * making sure the sizes are equal.
159 */
160 if (consumer->array_size != producer->array_size)
161 return false;
162 }
163 return true;
164 }
165
166
167 /**
168 * This class keeps track of a mapping from an interface block name to the
169 * necessary information about that interface block to determine whether to
170 * generate a link error.
171 *
172 * Note: this class is expected to be short lived, so it doesn't make copies
173 * of the strings it references; it simply borrows the pointers from the
174 * ir_variable class.
175 */
176 class interface_block_definitions
177 {
178 public:
179 interface_block_definitions()
180 : mem_ctx(ralloc_context(NULL)),
181 ht(hash_table_ctor(0, hash_table_string_hash,
182 hash_table_string_compare))
183 {
184 }
185
186 ~interface_block_definitions()
187 {
188 hash_table_dtor(ht);
189 ralloc_free(mem_ctx);
190 }
191
192 /**
193 * Lookup the interface definition having the given block name. Return
194 * NULL if none is found.
195 */
196 interface_block_definition *lookup(const char *block_name)
197 {
198 return (interface_block_definition *) hash_table_find(ht, block_name);
199 }
200
201 /**
202 * Add a new interface definition.
203 */
204 void store(const interface_block_definition &def)
205 {
206 interface_block_definition *hash_entry =
207 rzalloc(mem_ctx, interface_block_definition);
208 *hash_entry = def;
209 hash_table_insert(ht, hash_entry, def.type->name);
210 }
211
212 private:
213 /**
214 * Ralloc context for data structures allocated by this class.
215 */
216 void *mem_ctx;
217
218 /**
219 * Hash table mapping interface block name to an \c
220 * interface_block_definition struct. interface_block_definition structs
221 * are allocated using \c mem_ctx.
222 */
223 hash_table *ht;
224 };
225
226
227 }; /* anonymous namespace */
228
229
230 void
231 validate_intrastage_interface_blocks(struct gl_shader_program *prog,
232 const gl_shader **shader_list,
233 unsigned num_shaders)
234 {
235 interface_block_definitions in_interfaces;
236 interface_block_definitions out_interfaces;
237 interface_block_definitions uniform_interfaces;
238
239 for (unsigned int i = 0; i < num_shaders; i++) {
240 if (shader_list[i] == NULL)
241 continue;
242
243 foreach_list(node, shader_list[i]->ir) {
244 ir_variable *var = ((ir_instruction *) node)->as_variable();
245 if (!var)
246 continue;
247
248 const glsl_type *iface_type = var->get_interface_type();
249
250 if (iface_type == NULL)
251 continue;
252
253 interface_block_definitions *definitions;
254 switch (var->mode) {
255 case ir_var_shader_in:
256 definitions = &in_interfaces;
257 break;
258 case ir_var_shader_out:
259 definitions = &out_interfaces;
260 break;
261 case ir_var_uniform:
262 definitions = &uniform_interfaces;
263 break;
264 default:
265 /* Only in, out, and uniform interfaces are legal, so we should
266 * never get here.
267 */
268 assert(!"illegal interface type");
269 continue;
270 }
271
272 const interface_block_definition def(var);
273 interface_block_definition *prev_def =
274 definitions->lookup(iface_type->name);
275
276 if (prev_def == NULL) {
277 /* This is the first time we've seen the interface, so save
278 * it into the appropriate data structure.
279 */
280 definitions->store(def);
281 } else if (!intrastage_match(prev_def, &def,
282 (ir_variable_mode) var->mode)) {
283 linker_error(prog, "definitions of interface block `%s' do not"
284 " match\n", iface_type->name);
285 return;
286 }
287 }
288 }
289 }
290
291 void
292 validate_interstage_interface_blocks(struct gl_shader_program *prog,
293 const gl_shader *producer,
294 const gl_shader *consumer)
295 {
296 interface_block_definitions inout_interfaces;
297 interface_block_definitions uniform_interfaces;
298 const bool extra_array_level = consumer->Type == GL_GEOMETRY_SHADER;
299
300 /* Add non-output interfaces from the consumer to the symbol table. */
301 foreach_list(node, consumer->ir) {
302 ir_variable *var = ((ir_instruction *) node)->as_variable();
303 if (!var || !var->get_interface_type() || var->mode == ir_var_shader_out)
304 continue;
305
306 interface_block_definitions *definitions = var->mode == ir_var_uniform ?
307 &uniform_interfaces : &inout_interfaces;
308 definitions->store(interface_block_definition(var));
309 }
310
311 /* Verify that the producer's interfaces match. */
312 foreach_list(node, producer->ir) {
313 ir_variable *var = ((ir_instruction *) node)->as_variable();
314 if (!var || !var->get_interface_type() || var->mode == ir_var_shader_in)
315 continue;
316
317 interface_block_definitions *definitions = var->mode == ir_var_uniform ?
318 &uniform_interfaces : &inout_interfaces;
319 interface_block_definition *consumer_def =
320 definitions->lookup(var->get_interface_type()->name);
321
322 /* The consumer doesn't use this output block. Ignore it. */
323 if (consumer_def == NULL)
324 continue;
325
326 const interface_block_definition producer_def(var);
327 bool match;
328 if (var->mode == ir_var_uniform) {
329 /* Uniform matching rules are the same for interstage and intrastage
330 * linking.
331 */
332 match = intrastage_match(consumer_def, &producer_def,
333 (ir_variable_mode) var->mode);
334 } else {
335 match = interstage_match(&producer_def, consumer_def,
336 extra_array_level);
337 }
338
339 if (!match) {
340 linker_error(prog, "definitions of interface block `%s' do not "
341 "match\n", var->get_interface_type()->name);
342 return;
343 }
344 }
345 }