f9ddb13cd566b88d2feb58e10d5447dd38df7745
[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(ir_variable *var)
54 : var(var),
55 type(var->get_interface_type()),
56 instance_name(NULL)
57 {
58 if (var->is_interface_instance()) {
59 instance_name = var->name;
60 }
61 explicitly_declared = (var->data.how_declared != ir_var_declared_implicitly);
62 }
63 /**
64 * Interface block ir_variable
65 */
66 ir_variable *var;
67
68 /**
69 * Interface block type
70 */
71 const glsl_type *type;
72
73 /**
74 * For a named interface block, the instance name. Otherwise NULL.
75 */
76 const char *instance_name;
77
78 /**
79 * True if this interface block was explicitly declared in the shader;
80 * false if it was an implicitly declared built-in interface block.
81 */
82 bool explicitly_declared;
83 };
84
85
86 /**
87 * Check if two interfaces match, according to intrastage interface matching
88 * rules. If they do, and the first interface uses an unsized array, it will
89 * be updated to reflect the array size declared in the second interface.
90 */
91 bool
92 intrastage_match(interface_block_definition *a,
93 const interface_block_definition *b,
94 ir_variable_mode mode,
95 struct gl_shader_program *prog)
96 {
97 /* Types must match. */
98 if (a->type != b->type) {
99 /* Exception: if both the interface blocks are implicitly declared,
100 * don't force their types to match. They might mismatch due to the two
101 * shaders using different GLSL versions, and that's ok.
102 */
103 if (a->explicitly_declared || b->explicitly_declared)
104 return false;
105 }
106
107 /* Presence/absence of interface names must match. */
108 if ((a->instance_name == NULL) != (b->instance_name == NULL))
109 return false;
110
111 /* For uniforms, instance names need not match. For shader ins/outs,
112 * it's not clear from the spec whether they need to match, but
113 * Mesa's implementation relies on them matching.
114 */
115 if (a->instance_name != NULL &&
116 mode != ir_var_uniform && mode != ir_var_shader_storage &&
117 strcmp(a->instance_name, b->instance_name) != 0) {
118 return false;
119 }
120
121 /* If a block is an array then it must match across the shader.
122 * Unsized arrays are also processed and matched agaist sized arrays.
123 */
124 if (b->var->type != a->var->type &&
125 (b->instance_name != NULL || a->instance_name != NULL) &&
126 !validate_intrastage_arrays(prog, b->var, a->var))
127 return false;
128
129 return true;
130 }
131
132
133 /**
134 * Check if two interfaces match, according to interstage (in/out) interface
135 * matching rules.
136 *
137 * If \c extra_array_level is true, then vertex-to-geometry shader matching
138 * rules are enforced (i.e. a successful match requires the consumer interface
139 * to be an array and the producer interface to be a non-array).
140 */
141 bool
142 interstage_match(const interface_block_definition *producer,
143 const interface_block_definition *consumer,
144 bool extra_array_level)
145 {
146 /* Unsized arrays should not occur during interstage linking. They
147 * should have all been assigned a size by link_intrastage_shaders.
148 */
149 assert(!consumer->var->type->is_unsized_array());
150 assert(!producer->var->type->is_unsized_array());
151
152 /* Types must match. */
153 if (consumer->type != producer->type) {
154 /* Exception: if both the interface blocks are implicitly declared,
155 * don't force their types to match. They might mismatch due to the two
156 * shaders using different GLSL versions, and that's ok.
157 */
158 if (consumer->explicitly_declared || producer->explicitly_declared)
159 return false;
160 }
161
162 /* Ignore outermost array if geom shader */
163 const glsl_type *consumer_instance_type;
164 if (extra_array_level) {
165 consumer_instance_type = consumer->var->type->fields.array;
166 } else {
167 consumer_instance_type = consumer->var->type;
168 }
169
170 /* If a block is an array then it must match across shaders.
171 * Since unsized arrays have been ruled out, we can check this by just
172 * making sure the types are equal.
173 */
174 if ((consumer->instance_name != NULL &&
175 consumer_instance_type->is_array()) ||
176 (producer->instance_name != NULL &&
177 producer->var->type->is_array())) {
178 if (consumer_instance_type != producer->var->type)
179 return false;
180 }
181
182 return true;
183 }
184
185
186 /**
187 * This class keeps track of a mapping from an interface block name to the
188 * necessary information about that interface block to determine whether to
189 * generate a link error.
190 *
191 * Note: this class is expected to be short lived, so it doesn't make copies
192 * of the strings it references; it simply borrows the pointers from the
193 * ir_variable class.
194 */
195 class interface_block_definitions
196 {
197 public:
198 interface_block_definitions()
199 : mem_ctx(ralloc_context(NULL)),
200 ht(hash_table_ctor(0, hash_table_string_hash,
201 hash_table_string_compare))
202 {
203 }
204
205 ~interface_block_definitions()
206 {
207 hash_table_dtor(ht);
208 ralloc_free(mem_ctx);
209 }
210
211 /**
212 * Lookup the interface definition having the given block name. Return
213 * NULL if none is found.
214 */
215 interface_block_definition *lookup(const char *block_name)
216 {
217 return (interface_block_definition *) hash_table_find(ht, block_name);
218 }
219
220 /**
221 * Add a new interface definition.
222 */
223 void store(const interface_block_definition &def)
224 {
225 interface_block_definition *hash_entry =
226 rzalloc(mem_ctx, interface_block_definition);
227 *hash_entry = def;
228 hash_table_insert(ht, hash_entry, def.type->name);
229 }
230
231 private:
232 /**
233 * Ralloc context for data structures allocated by this class.
234 */
235 void *mem_ctx;
236
237 /**
238 * Hash table mapping interface block name to an \c
239 * interface_block_definition struct. interface_block_definition structs
240 * are allocated using \c mem_ctx.
241 */
242 hash_table *ht;
243 };
244
245
246 }; /* anonymous namespace */
247
248
249 void
250 validate_intrastage_interface_blocks(struct gl_shader_program *prog,
251 const gl_shader **shader_list,
252 unsigned num_shaders)
253 {
254 interface_block_definitions in_interfaces;
255 interface_block_definitions out_interfaces;
256 interface_block_definitions uniform_interfaces;
257 interface_block_definitions buffer_interfaces;
258
259 for (unsigned int i = 0; i < num_shaders; i++) {
260 if (shader_list[i] == NULL)
261 continue;
262
263 foreach_in_list(ir_instruction, node, shader_list[i]->ir) {
264 ir_variable *var = node->as_variable();
265 if (!var)
266 continue;
267
268 const glsl_type *iface_type = var->get_interface_type();
269
270 if (iface_type == NULL)
271 continue;
272
273 interface_block_definitions *definitions;
274 switch (var->data.mode) {
275 case ir_var_shader_in:
276 definitions = &in_interfaces;
277 break;
278 case ir_var_shader_out:
279 definitions = &out_interfaces;
280 break;
281 case ir_var_uniform:
282 definitions = &uniform_interfaces;
283 break;
284 case ir_var_shader_storage:
285 definitions = &buffer_interfaces;
286 break;
287 default:
288 /* Only in, out, and uniform interfaces are legal, so we should
289 * never get here.
290 */
291 assert(!"illegal interface type");
292 continue;
293 }
294
295 const interface_block_definition def(var);
296 interface_block_definition *prev_def =
297 definitions->lookup(iface_type->name);
298
299 if (prev_def == NULL) {
300 /* This is the first time we've seen the interface, so save
301 * it into the appropriate data structure.
302 */
303 definitions->store(def);
304 } else if (!intrastage_match(prev_def, &def,
305 (ir_variable_mode) var->data.mode,
306 prog)) {
307 linker_error(prog, "definitions of interface block `%s' do not"
308 " match\n", iface_type->name);
309 return;
310 }
311 }
312 }
313 }
314
315 void
316 validate_interstage_inout_blocks(struct gl_shader_program *prog,
317 const gl_shader *producer,
318 const gl_shader *consumer)
319 {
320 interface_block_definitions definitions;
321 const bool extra_array_level = consumer->Stage == MESA_SHADER_GEOMETRY;
322
323 /* Add input interfaces from the consumer to the symbol table. */
324 foreach_in_list(ir_instruction, node, consumer->ir) {
325 ir_variable *var = node->as_variable();
326 if (!var || !var->get_interface_type() || var->data.mode != ir_var_shader_in)
327 continue;
328
329 definitions.store(interface_block_definition(var));
330 }
331
332 /* Verify that the producer's output interfaces match. */
333 foreach_in_list(ir_instruction, node, producer->ir) {
334 ir_variable *var = node->as_variable();
335 if (!var || !var->get_interface_type() || var->data.mode != ir_var_shader_out)
336 continue;
337
338 interface_block_definition *consumer_def =
339 definitions.lookup(var->get_interface_type()->name);
340
341 /* The consumer doesn't use this output block. Ignore it. */
342 if (consumer_def == NULL)
343 continue;
344
345 const interface_block_definition producer_def(var);
346
347 if (!interstage_match(&producer_def, consumer_def, extra_array_level)) {
348 linker_error(prog, "definitions of interface block `%s' do not "
349 "match\n", var->get_interface_type()->name);
350 return;
351 }
352 }
353 }
354
355
356 void
357 validate_interstage_uniform_blocks(struct gl_shader_program *prog,
358 gl_shader **stages, int num_stages)
359 {
360 interface_block_definitions definitions;
361
362 for (int i = 0; i < num_stages; i++) {
363 if (stages[i] == NULL)
364 continue;
365
366 const gl_shader *stage = stages[i];
367 foreach_in_list(ir_instruction, node, stage->ir) {
368 ir_variable *var = node->as_variable();
369 if (!var || !var->get_interface_type() ||
370 (var->data.mode != ir_var_uniform &&
371 var->data.mode != ir_var_shader_storage))
372 continue;
373
374 interface_block_definition *old_def =
375 definitions.lookup(var->get_interface_type()->name);
376 const interface_block_definition new_def(var);
377 if (old_def == NULL) {
378 definitions.store(new_def);
379 } else {
380 /* Interstage uniform matching rules are the same as intrastage
381 * uniform matchin rules (for uniforms, it is as though all
382 * shaders are in the same shader stage).
383 */
384 if (!intrastage_match(old_def, &new_def,
385 (ir_variable_mode) var->data.mode,
386 prog)) {
387 linker_error(prog, "definitions of interface block `%s' do not "
388 "match\n", var->get_interface_type()->name);
389 return;
390 }
391 }
392 }
393 }
394 }