glsl/linker: always validate explicit location among inputs
[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 "main/mtypes.h"
34 #include "util/hash_table.h"
35 #include "util/u_string.h"
36
37
38 namespace {
39
40 /**
41 * Return true if interface members mismatch and its not allowed by GLSL.
42 */
43 static bool
44 interstage_member_mismatch(struct gl_shader_program *prog,
45 const glsl_type *c, const glsl_type *p) {
46
47 if (c->length != p->length)
48 return true;
49
50 for (unsigned i = 0; i < c->length; i++) {
51 if (c->fields.structure[i].type != p->fields.structure[i].type)
52 return true;
53 if (strcmp(c->fields.structure[i].name,
54 p->fields.structure[i].name) != 0)
55 return true;
56 if (c->fields.structure[i].location !=
57 p->fields.structure[i].location)
58 return true;
59 if (c->fields.structure[i].patch !=
60 p->fields.structure[i].patch)
61 return true;
62
63 /* From Section 4.5 (Interpolation Qualifiers) of the GLSL 4.40 spec:
64 *
65 * "It is a link-time error if, within the same stage, the
66 * interpolation qualifiers of variables of the same name do not
67 * match."
68 */
69 if (prog->IsES || prog->data->Version < 440)
70 if (c->fields.structure[i].interpolation !=
71 p->fields.structure[i].interpolation)
72 return true;
73
74 /* From Section 4.3.4 (Input Variables) of the GLSL ES 3.0 spec:
75 *
76 * "The output of the vertex shader and the input of the fragment
77 * shader form an interface. For this interface, vertex shader
78 * output variables and fragment shader input variables of the same
79 * name must match in type and qualification (other than precision
80 * and out matching to in).
81 *
82 * The table in Section 9.2.1 Linked Shaders of the GLSL ES 3.1 spec
83 * says that centroid no longer needs to match for varyings.
84 *
85 * The table in Section 9.2.1 Linked Shaders of the GLSL ES 3.2 spec
86 * says that sample need not match for varyings.
87 */
88 if (!prog->IsES || prog->data->Version < 310)
89 if (c->fields.structure[i].centroid !=
90 p->fields.structure[i].centroid)
91 return true;
92 if (!prog->IsES)
93 if (c->fields.structure[i].sample !=
94 p->fields.structure[i].sample)
95 return true;
96 }
97
98 return false;
99 }
100
101 /**
102 * Check if two interfaces match, according to intrastage interface matching
103 * rules. If they do, and the first interface uses an unsized array, it will
104 * be updated to reflect the array size declared in the second interface.
105 */
106 bool
107 intrastage_match(ir_variable *a,
108 ir_variable *b,
109 struct gl_shader_program *prog)
110 {
111 /* Types must match. */
112 if (a->get_interface_type() != b->get_interface_type()) {
113 /* Exception: if both the interface blocks are implicitly declared,
114 * don't force their types to match. They might mismatch due to the two
115 * shaders using different GLSL versions, and that's ok.
116 */
117 if ((a->data.how_declared != ir_var_declared_implicitly ||
118 b->data.how_declared != ir_var_declared_implicitly) &&
119 (!prog->IsES ||
120 interstage_member_mismatch(prog, a->get_interface_type(),
121 b->get_interface_type())))
122 return false;
123 }
124
125 /* Presence/absence of interface names must match. */
126 if (a->is_interface_instance() != b->is_interface_instance())
127 return false;
128
129 /* For uniforms, instance names need not match. For shader ins/outs,
130 * it's not clear from the spec whether they need to match, but
131 * Mesa's implementation relies on them matching.
132 */
133 if (a->is_interface_instance() && b->data.mode != ir_var_uniform &&
134 b->data.mode != ir_var_shader_storage &&
135 strcmp(a->name, b->name) != 0) {
136 return false;
137 }
138
139 /* If a block is an array then it must match across the shader.
140 * Unsized arrays are also processed and matched agaist sized arrays.
141 */
142 if (b->type != a->type && (b->type->is_array() || a->type->is_array()) &&
143 (b->is_interface_instance() || a->is_interface_instance()) &&
144 !validate_intrastage_arrays(prog, b, a))
145 return false;
146
147 return true;
148 }
149
150 /**
151 * Check if two interfaces match, according to interstage (in/out) interface
152 * matching rules.
153 *
154 * If \c extra_array_level is true, the consumer interface is required to be
155 * an array and the producer interface is required to be a non-array.
156 * This is used for tessellation control and geometry shader consumers.
157 */
158 static bool
159 interstage_match(struct gl_shader_program *prog, ir_variable *producer,
160 ir_variable *consumer, bool extra_array_level)
161 {
162 /* Types must match. */
163 if (consumer->get_interface_type() != producer->get_interface_type()) {
164 /* Exception: if both the interface blocks are implicitly declared,
165 * don't force their types to match. They might mismatch due to the two
166 * shaders using different GLSL versions, and that's ok.
167 *
168 * Also we store some member information such as interpolation in
169 * glsl_type that doesn't always have to match across shader stages.
170 * Therefore we make a pass over the members glsl_struct_field to make
171 * sure we don't reject shaders where fields don't need to match.
172 */
173 if ((consumer->data.how_declared != ir_var_declared_implicitly ||
174 producer->data.how_declared != ir_var_declared_implicitly) &&
175 interstage_member_mismatch(prog, consumer->get_interface_type(),
176 producer->get_interface_type()))
177 return false;
178 }
179
180 /* Ignore outermost array if geom shader */
181 const glsl_type *consumer_instance_type;
182 if (extra_array_level) {
183 consumer_instance_type = consumer->type->fields.array;
184 } else {
185 consumer_instance_type = consumer->type;
186 }
187
188 /* If a block is an array then it must match across shaders.
189 * Since unsized arrays have been ruled out, we can check this by just
190 * making sure the types are equal.
191 */
192 if ((consumer->is_interface_instance() &&
193 consumer_instance_type->is_array()) ||
194 (producer->is_interface_instance() &&
195 producer->type->is_array())) {
196 if (consumer_instance_type != producer->type)
197 return false;
198 }
199
200 return true;
201 }
202
203
204 /**
205 * This class keeps track of a mapping from an interface block name to the
206 * necessary information about that interface block to determine whether to
207 * generate a link error.
208 *
209 * Note: this class is expected to be short lived, so it doesn't make copies
210 * of the strings it references; it simply borrows the pointers from the
211 * ir_variable class.
212 */
213 class interface_block_definitions
214 {
215 public:
216 interface_block_definitions()
217 : mem_ctx(ralloc_context(NULL)),
218 ht(_mesa_hash_table_create(NULL, _mesa_key_hash_string,
219 _mesa_key_string_equal))
220 {
221 }
222
223 ~interface_block_definitions()
224 {
225 ralloc_free(mem_ctx);
226 _mesa_hash_table_destroy(ht, NULL);
227 }
228
229 /**
230 * Lookup the interface definition. Return NULL if none is found.
231 */
232 ir_variable *lookup(ir_variable *var)
233 {
234 if (var->data.explicit_location &&
235 var->data.location >= VARYING_SLOT_VAR0) {
236 char location_str[11];
237 util_snprintf(location_str, 11, "%d", var->data.location);
238
239 const struct hash_entry *entry =
240 _mesa_hash_table_search(ht, location_str);
241 return entry ? (ir_variable *) entry->data : NULL;
242 } else {
243 const struct hash_entry *entry =
244 _mesa_hash_table_search(ht,
245 var->get_interface_type()->without_array()->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 util_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,
267 var->get_interface_type()->without_array()->name, var);
268 }
269 }
270
271 private:
272 /**
273 * Ralloc context for data structures allocated by this class.
274 */
275 void *mem_ctx;
276
277 /**
278 * Hash table mapping interface block name to an \c
279 * ir_variable.
280 */
281 hash_table *ht;
282 };
283
284
285 }; /* anonymous namespace */
286
287
288 void
289 validate_intrastage_interface_blocks(struct gl_shader_program *prog,
290 const gl_shader **shader_list,
291 unsigned num_shaders)
292 {
293 interface_block_definitions in_interfaces;
294 interface_block_definitions out_interfaces;
295 interface_block_definitions uniform_interfaces;
296 interface_block_definitions buffer_interfaces;
297
298 for (unsigned int i = 0; i < num_shaders; i++) {
299 if (shader_list[i] == NULL)
300 continue;
301
302 foreach_in_list(ir_instruction, node, shader_list[i]->ir) {
303 ir_variable *var = node->as_variable();
304 if (!var)
305 continue;
306
307 const glsl_type *iface_type = var->get_interface_type();
308
309 if (iface_type == NULL)
310 continue;
311
312 interface_block_definitions *definitions;
313 switch (var->data.mode) {
314 case ir_var_shader_in:
315 definitions = &in_interfaces;
316 break;
317 case ir_var_shader_out:
318 definitions = &out_interfaces;
319 break;
320 case ir_var_uniform:
321 definitions = &uniform_interfaces;
322 break;
323 case ir_var_shader_storage:
324 definitions = &buffer_interfaces;
325 break;
326 default:
327 /* Only in, out, and uniform interfaces are legal, so we should
328 * never get here.
329 */
330 assert(!"illegal interface type");
331 continue;
332 }
333
334 ir_variable *prev_def = definitions->lookup(var);
335 if (prev_def == NULL) {
336 /* This is the first time we've seen the interface, so save
337 * it into the appropriate data structure.
338 */
339 definitions->store(var);
340 } else if (!intrastage_match(prev_def, var, prog)) {
341 linker_error(prog, "definitions of interface block `%s' do not"
342 " match\n", iface_type->name);
343 return;
344 }
345 }
346 }
347 }
348
349 static bool
350 is_builtin_gl_in_block(ir_variable *var, int consumer_stage)
351 {
352 return !strcmp(var->name, "gl_in") &&
353 (consumer_stage == MESA_SHADER_TESS_CTRL ||
354 consumer_stage == MESA_SHADER_TESS_EVAL ||
355 consumer_stage == MESA_SHADER_GEOMETRY);
356 }
357
358 void
359 validate_interstage_inout_blocks(struct gl_shader_program *prog,
360 const gl_linked_shader *producer,
361 const gl_linked_shader *consumer)
362 {
363 interface_block_definitions definitions;
364 /* VS -> GS, VS -> TCS, VS -> TES, TES -> GS */
365 const bool extra_array_level = (producer->Stage == MESA_SHADER_VERTEX &&
366 consumer->Stage != MESA_SHADER_FRAGMENT) ||
367 consumer->Stage == MESA_SHADER_GEOMETRY;
368
369 /* Check that block re-declarations of gl_PerVertex are compatible
370 * across shaders: From OpenGL Shading Language 4.5, section
371 * "7.1 Built-In Language Variables", page 130 of the PDF:
372 *
373 * "If multiple shaders using members of a built-in block belonging
374 * to the same interface are linked together in the same program,
375 * they must all redeclare the built-in block in the same way, as
376 * described in section 4.3.9 “Interface Blocks” for interface-block
377 * matching, or a link-time error will result."
378 *
379 * This is done explicitly outside of iterating the member variable
380 * declarations because it is possible that the variables are not used and
381 * so they would have been optimised out.
382 */
383 const glsl_type *consumer_iface =
384 consumer->symbols->get_interface("gl_PerVertex",
385 ir_var_shader_in);
386
387 const glsl_type *producer_iface =
388 producer->symbols->get_interface("gl_PerVertex",
389 ir_var_shader_out);
390
391 if (producer_iface && consumer_iface &&
392 interstage_member_mismatch(prog, consumer_iface, producer_iface)) {
393 linker_error(prog, "Incompatible or missing gl_PerVertex re-declaration "
394 "in consecutive shaders");
395 return;
396 }
397
398 /* Add output interfaces from the producer to the symbol table. */
399 foreach_in_list(ir_instruction, node, producer->ir) {
400 ir_variable *var = node->as_variable();
401 if (!var || !var->get_interface_type() || var->data.mode != ir_var_shader_out)
402 continue;
403
404 definitions.store(var);
405 }
406
407 /* Verify that the consumer's input interfaces match. */
408 foreach_in_list(ir_instruction, node, consumer->ir) {
409 ir_variable *var = node->as_variable();
410 if (!var || !var->get_interface_type() || var->data.mode != ir_var_shader_in)
411 continue;
412
413 ir_variable *producer_def = definitions.lookup(var);
414
415 /* The producer doesn't generate this input: fail to link. Skip built-in
416 * 'gl_in[]' since that may not be present if the producer does not
417 * write to any of the pre-defined outputs (e.g. if the vertex shader
418 * does not write to gl_Position, etc), which is allowed and results in
419 * undefined behavior.
420 *
421 * From Section 4.3.4 (Inputs) of the GLSL 1.50 spec:
422 *
423 * "Only the input variables that are actually read need to be written
424 * by the previous stage; it is allowed to have superfluous
425 * declarations of input variables."
426 */
427 if (producer_def == NULL &&
428 !is_builtin_gl_in_block(var, consumer->Stage) && var->data.used) {
429 linker_error(prog, "Input block `%s' is not an output of "
430 "the previous stage\n", var->get_interface_type()->name);
431 return;
432 }
433
434 if (producer_def &&
435 !interstage_match(prog, producer_def, var, extra_array_level)) {
436 linker_error(prog, "definitions of interface block `%s' do not "
437 "match\n", var->get_interface_type()->name);
438 return;
439 }
440 }
441 }
442
443
444 void
445 validate_interstage_uniform_blocks(struct gl_shader_program *prog,
446 gl_linked_shader **stages)
447 {
448 interface_block_definitions definitions;
449
450 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
451 if (stages[i] == NULL)
452 continue;
453
454 const gl_linked_shader *stage = stages[i];
455 foreach_in_list(ir_instruction, node, stage->ir) {
456 ir_variable *var = node->as_variable();
457 if (!var || !var->get_interface_type() ||
458 (var->data.mode != ir_var_uniform &&
459 var->data.mode != ir_var_shader_storage))
460 continue;
461
462 ir_variable *old_def = definitions.lookup(var);
463 if (old_def == NULL) {
464 definitions.store(var);
465 } else {
466 /* Interstage uniform matching rules are the same as intrastage
467 * uniform matchin rules (for uniforms, it is as though all
468 * shaders are in the same shader stage).
469 */
470 if (!intrastage_match(old_def, var, prog)) {
471 linker_error(prog, "definitions of uniform block `%s' do not "
472 "match\n", var->get_interface_type()->name);
473 return;
474 }
475 }
476 }
477 }
478 }