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