glsl: don't always reject shaders with mismatching ifc blocks
[mesa.git] / src / compiler / 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 "util/hash_table.h"
34
35
36 namespace {
37
38 /**
39 * Check if two interfaces match, according to intrastage interface matching
40 * rules. If they do, and the first interface uses an unsized array, it will
41 * be updated to reflect the array size declared in the second interface.
42 */
43 bool
44 intrastage_match(ir_variable *a,
45 ir_variable *b,
46 struct gl_shader_program *prog)
47 {
48 /* Types must match. */
49 if (a->get_interface_type() != b->get_interface_type()) {
50 /* Exception: if both the interface blocks are implicitly declared,
51 * don't force their types to match. They might mismatch due to the two
52 * shaders using different GLSL versions, and that's ok.
53 */
54 if (a->data.how_declared != ir_var_declared_implicitly ||
55 b->data.how_declared != ir_var_declared_implicitly)
56 return false;
57 }
58
59 /* Presence/absence of interface names must match. */
60 if (a->is_interface_instance() != b->is_interface_instance())
61 return false;
62
63 /* For uniforms, instance names need not match. For shader ins/outs,
64 * it's not clear from the spec whether they need to match, but
65 * Mesa's implementation relies on them matching.
66 */
67 if (a->is_interface_instance() && b->data.mode != ir_var_uniform &&
68 b->data.mode != ir_var_shader_storage &&
69 strcmp(a->name, b->name) != 0) {
70 return false;
71 }
72
73 /* If a block is an array then it must match across the shader.
74 * Unsized arrays are also processed and matched agaist sized arrays.
75 */
76 if (b->type != a->type &&
77 (b->is_interface_instance() || a->is_interface_instance()) &&
78 !validate_intrastage_arrays(prog, b, a))
79 return false;
80
81 return true;
82 }
83
84 /**
85 * Return true if interface members mismatch and its not allowed by GLSL.
86 */
87 static bool
88 interstage_member_mismatch(struct gl_shader_program *prog,
89 const glsl_type *c, const glsl_type *p) {
90
91 if (c->length != p->length)
92 return true;
93
94 for (unsigned i = 0; i < c->length; i++) {
95 if (c->fields.structure[i].type != p->fields.structure[i].type)
96 return true;
97 if (strcmp(c->fields.structure[i].name,
98 p->fields.structure[i].name) != 0)
99 return true;
100 if (c->fields.structure[i].location !=
101 p->fields.structure[i].location)
102 return true;
103 if (c->fields.structure[i].patch !=
104 p->fields.structure[i].patch)
105 return true;
106
107 /* From Section 4.5 (Interpolation Qualifiers) of the GLSL 4.40 spec:
108 *
109 * "It is a link-time error if, within the same stage, the
110 * interpolation qualifiers of variables of the same name do not
111 * match."
112 */
113 if (prog->IsES || prog->Version < 440)
114 if (c->fields.structure[i].interpolation !=
115 p->fields.structure[i].interpolation)
116 return true;
117
118 /* From Section 4.3.4 (Input Variables) of the GLSL ES 3.0 spec:
119 *
120 * "The output of the vertex shader and the input of the fragment
121 * shader form an interface. For this interface, vertex shader
122 * output variables and fragment shader input variables of the same
123 * name must match in type and qualification (other than precision
124 * and out matching to in).
125 *
126 * The table in Section 9.2.1 Linked Shaders of the GLSL ES 3.1 spec
127 * says that centroid no longer needs to match for varyings.
128 *
129 * The table in Section 9.2.1 Linked Shaders of the GLSL ES 3.2 spec
130 * says that sample need not match for varyings.
131 */
132 if (!prog->IsES || prog->Version < 310)
133 if (c->fields.structure[i].centroid !=
134 p->fields.structure[i].centroid)
135 return true;
136 if (!prog->IsES)
137 if (c->fields.structure[i].sample !=
138 p->fields.structure[i].sample)
139 return true;
140 }
141
142 return false;
143 }
144
145 /**
146 * Check if two interfaces match, according to interstage (in/out) interface
147 * matching rules.
148 *
149 * If \c extra_array_level is true, the consumer interface is required to be
150 * an array and the producer interface is required to be a non-array.
151 * This is used for tessellation control and geometry shader consumers.
152 */
153 static bool
154 interstage_match(struct gl_shader_program *prog, ir_variable *producer,
155 ir_variable *consumer, bool extra_array_level)
156 {
157 /* Unsized arrays should not occur during interstage linking. They
158 * should have all been assigned a size by link_intrastage_shaders.
159 */
160 assert(!consumer->type->is_unsized_array());
161 assert(!producer->type->is_unsized_array());
162
163 /* Types must match. */
164 if (consumer->get_interface_type() != producer->get_interface_type()) {
165 /* Exception: if both the interface blocks are implicitly declared,
166 * don't force their types to match. They might mismatch due to the two
167 * shaders using different GLSL versions, and that's ok.
168 *
169 * Also we store some member information such as interpolation in
170 * glsl_type that doesn't always have to match across shader stages.
171 * Therefore we make a pass over the members glsl_struct_field to make
172 * sure we don't reject shaders where fields don't need to match.
173 */
174 if ((consumer->data.how_declared != ir_var_declared_implicitly ||
175 producer->data.how_declared != ir_var_declared_implicitly) &&
176 interstage_member_mismatch(prog, consumer->get_interface_type(),
177 producer->get_interface_type()))
178 return false;
179 }
180
181 /* Ignore outermost array if geom shader */
182 const glsl_type *consumer_instance_type;
183 if (extra_array_level) {
184 consumer_instance_type = consumer->type->fields.array;
185 } else {
186 consumer_instance_type = consumer->type;
187 }
188
189 /* If a block is an array then it must match across shaders.
190 * Since unsized arrays have been ruled out, we can check this by just
191 * making sure the types are equal.
192 */
193 if ((consumer->is_interface_instance() &&
194 consumer_instance_type->is_array()) ||
195 (producer->is_interface_instance() &&
196 producer->type->is_array())) {
197 if (consumer_instance_type != producer->type)
198 return false;
199 }
200
201 return true;
202 }
203
204
205 /**
206 * This class keeps track of a mapping from an interface block name to the
207 * necessary information about that interface block to determine whether to
208 * generate a link error.
209 *
210 * Note: this class is expected to be short lived, so it doesn't make copies
211 * of the strings it references; it simply borrows the pointers from the
212 * ir_variable class.
213 */
214 class interface_block_definitions
215 {
216 public:
217 interface_block_definitions()
218 : mem_ctx(ralloc_context(NULL)),
219 ht(_mesa_hash_table_create(NULL, _mesa_key_hash_string,
220 _mesa_key_string_equal))
221 {
222 }
223
224 ~interface_block_definitions()
225 {
226 ralloc_free(mem_ctx);
227 _mesa_hash_table_destroy(ht, NULL);
228 }
229
230 /**
231 * Lookup the interface definition. Return NULL if none is found.
232 */
233 ir_variable *lookup(ir_variable *var)
234 {
235 if (var->data.explicit_location &&
236 var->data.location >= VARYING_SLOT_VAR0) {
237 char location_str[11];
238 snprintf(location_str, 11, "%d", var->data.location);
239
240 const struct hash_entry *entry =
241 _mesa_hash_table_search(ht, location_str);
242 return entry ? (ir_variable *) entry->data : NULL;
243 } else {
244 const struct hash_entry *entry =
245 _mesa_hash_table_search(ht, var->get_interface_type()->name);
246 return entry ? (ir_variable *) entry->data : NULL;
247 }
248 }
249
250 /**
251 * Add a new interface definition.
252 */
253 void store(ir_variable *var)
254 {
255 if (var->data.explicit_location &&
256 var->data.location >= VARYING_SLOT_VAR0) {
257 /* If explicit location is given then lookup the variable by location.
258 * We turn the location into a string and use this as the hash key
259 * rather than the name. Note: We allocate enough space for a 32-bit
260 * unsigned location value which is overkill but future proof.
261 */
262 char location_str[11];
263 snprintf(location_str, 11, "%d", var->data.location);
264 _mesa_hash_table_insert(ht, ralloc_strdup(mem_ctx, location_str), var);
265 } else {
266 _mesa_hash_table_insert(ht, var->get_interface_type()->name, var);
267 }
268 }
269
270 private:
271 /**
272 * Ralloc context for data structures allocated by this class.
273 */
274 void *mem_ctx;
275
276 /**
277 * Hash table mapping interface block name to an \c
278 * ir_variable.
279 */
280 hash_table *ht;
281 };
282
283
284 }; /* anonymous namespace */
285
286
287 void
288 validate_intrastage_interface_blocks(struct gl_shader_program *prog,
289 const gl_shader **shader_list,
290 unsigned num_shaders)
291 {
292 interface_block_definitions in_interfaces;
293 interface_block_definitions out_interfaces;
294 interface_block_definitions uniform_interfaces;
295 interface_block_definitions buffer_interfaces;
296
297 for (unsigned int i = 0; i < num_shaders; i++) {
298 if (shader_list[i] == NULL)
299 continue;
300
301 foreach_in_list(ir_instruction, node, shader_list[i]->ir) {
302 ir_variable *var = node->as_variable();
303 if (!var)
304 continue;
305
306 const glsl_type *iface_type = var->get_interface_type();
307
308 if (iface_type == NULL)
309 continue;
310
311 interface_block_definitions *definitions;
312 switch (var->data.mode) {
313 case ir_var_shader_in:
314 definitions = &in_interfaces;
315 break;
316 case ir_var_shader_out:
317 definitions = &out_interfaces;
318 break;
319 case ir_var_uniform:
320 definitions = &uniform_interfaces;
321 break;
322 case ir_var_shader_storage:
323 definitions = &buffer_interfaces;
324 break;
325 default:
326 /* Only in, out, and uniform interfaces are legal, so we should
327 * never get here.
328 */
329 assert(!"illegal interface type");
330 continue;
331 }
332
333 ir_variable *prev_def = definitions->lookup(var);
334 if (prev_def == NULL) {
335 /* This is the first time we've seen the interface, so save
336 * it into the appropriate data structure.
337 */
338 definitions->store(var);
339 } else if (!intrastage_match(prev_def, var, prog)) {
340 linker_error(prog, "definitions of interface block `%s' do not"
341 " match\n", iface_type->name);
342 return;
343 }
344 }
345 }
346 }
347
348 void
349 validate_interstage_inout_blocks(struct gl_shader_program *prog,
350 const gl_shader *producer,
351 const gl_shader *consumer)
352 {
353 interface_block_definitions definitions;
354 /* VS -> GS, VS -> TCS, VS -> TES, TES -> GS */
355 const bool extra_array_level = (producer->Stage == MESA_SHADER_VERTEX &&
356 consumer->Stage != MESA_SHADER_FRAGMENT) ||
357 consumer->Stage == MESA_SHADER_GEOMETRY;
358
359 /* Add input interfaces from the consumer to the symbol table. */
360 foreach_in_list(ir_instruction, node, consumer->ir) {
361 ir_variable *var = node->as_variable();
362 if (!var || !var->get_interface_type() || var->data.mode != ir_var_shader_in)
363 continue;
364
365 definitions.store(var);
366 }
367
368 /* Verify that the producer's output interfaces match. */
369 foreach_in_list(ir_instruction, node, producer->ir) {
370 ir_variable *var = node->as_variable();
371 if (!var || !var->get_interface_type() || var->data.mode != ir_var_shader_out)
372 continue;
373
374 ir_variable *consumer_def = definitions.lookup(var);
375
376 /* The consumer doesn't use this output block. Ignore it. */
377 if (consumer_def == NULL)
378 continue;
379
380 if (!interstage_match(prog, var, consumer_def, extra_array_level)) {
381 linker_error(prog, "definitions of interface block `%s' do not "
382 "match\n", var->get_interface_type()->name);
383 return;
384 }
385 }
386 }
387
388
389 void
390 validate_interstage_uniform_blocks(struct gl_shader_program *prog,
391 gl_shader **stages, int num_stages)
392 {
393 interface_block_definitions definitions;
394
395 for (int i = 0; i < num_stages; i++) {
396 if (stages[i] == NULL)
397 continue;
398
399 const gl_shader *stage = stages[i];
400 foreach_in_list(ir_instruction, node, stage->ir) {
401 ir_variable *var = node->as_variable();
402 if (!var || !var->get_interface_type() ||
403 (var->data.mode != ir_var_uniform &&
404 var->data.mode != ir_var_shader_storage))
405 continue;
406
407 ir_variable *old_def = definitions.lookup(var);
408 if (old_def == NULL) {
409 definitions.store(var);
410 } else {
411 /* Interstage uniform matching rules are the same as intrastage
412 * uniform matchin rules (for uniforms, it is as though all
413 * shaders are in the same shader stage).
414 */
415 if (!intrastage_match(old_def, var, prog)) {
416 linker_error(prog, "definitions of interface block `%s' do not "
417 "match\n", var->get_interface_type()->name);
418 return;
419 }
420 }
421 }
422 }
423 }