nir: Allow var_mem_global in nir_lower_vars_to_explicit_types
[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 bool match_precision)
111 {
112 /* Types must match. */
113 if (a->get_interface_type() != b->get_interface_type()) {
114 /* Exception: if both the interface blocks are implicitly declared,
115 * don't force their types to match. They might mismatch due to the two
116 * shaders using different GLSL versions, and that's ok.
117 */
118 if ((a->data.how_declared != ir_var_declared_implicitly ||
119 b->data.how_declared != ir_var_declared_implicitly) &&
120 (!prog->IsES ||
121 interstage_member_mismatch(prog, a->get_interface_type(),
122 b->get_interface_type())))
123 return false;
124 }
125
126 /* Presence/absence of interface names must match. */
127 if (a->is_interface_instance() != b->is_interface_instance())
128 return false;
129
130 /* For uniforms, instance names need not match. For shader ins/outs,
131 * it's not clear from the spec whether they need to match, but
132 * Mesa's implementation relies on them matching.
133 */
134 if (a->is_interface_instance() && b->data.mode != ir_var_uniform &&
135 b->data.mode != ir_var_shader_storage &&
136 strcmp(a->name, b->name) != 0) {
137 return false;
138 }
139
140 bool type_match = (match_precision ?
141 a->type == b->type :
142 a->type->compare_no_precision(b->type));
143
144 /* If a block is an array then it must match across the shader.
145 * Unsized arrays are also processed and matched agaist sized arrays.
146 */
147 if (!type_match && (b->type->is_array() || a->type->is_array()) &&
148 (b->is_interface_instance() || a->is_interface_instance()) &&
149 !validate_intrastage_arrays(prog, b, a, match_precision))
150 return false;
151
152 return true;
153 }
154
155 /**
156 * Check if two interfaces match, according to interstage (in/out) interface
157 * matching rules.
158 *
159 * If \c extra_array_level is true, the consumer interface is required to be
160 * an array and the producer interface is required to be a non-array.
161 * This is used for tessellation control and geometry shader consumers.
162 */
163 static bool
164 interstage_match(struct gl_shader_program *prog, ir_variable *producer,
165 ir_variable *consumer, bool extra_array_level)
166 {
167 /* Types must match. */
168 if (consumer->get_interface_type() != producer->get_interface_type()) {
169 /* Exception: if both the interface blocks are implicitly declared,
170 * don't force their types to match. They might mismatch due to the two
171 * shaders using different GLSL versions, and that's ok.
172 *
173 * Also we store some member information such as interpolation in
174 * glsl_type that doesn't always have to match across shader stages.
175 * Therefore we make a pass over the members glsl_struct_field to make
176 * sure we don't reject shaders where fields don't need to match.
177 */
178 if ((consumer->data.how_declared != ir_var_declared_implicitly ||
179 producer->data.how_declared != ir_var_declared_implicitly) &&
180 interstage_member_mismatch(prog, consumer->get_interface_type(),
181 producer->get_interface_type()))
182 return false;
183 }
184
185 /* Ignore outermost array if geom shader */
186 const glsl_type *consumer_instance_type;
187 if (extra_array_level) {
188 consumer_instance_type = consumer->type->fields.array;
189 } else {
190 consumer_instance_type = consumer->type;
191 }
192
193 /* If a block is an array then it must match across shaders.
194 * Since unsized arrays have been ruled out, we can check this by just
195 * making sure the types are equal.
196 */
197 if ((consumer->is_interface_instance() &&
198 consumer_instance_type->is_array()) ||
199 (producer->is_interface_instance() &&
200 producer->type->is_array())) {
201 if (consumer_instance_type != producer->type)
202 return false;
203 }
204
205 return true;
206 }
207
208
209 /**
210 * This class keeps track of a mapping from an interface block name to the
211 * necessary information about that interface block to determine whether to
212 * generate a link error.
213 *
214 * Note: this class is expected to be short lived, so it doesn't make copies
215 * of the strings it references; it simply borrows the pointers from the
216 * ir_variable class.
217 */
218 class interface_block_definitions
219 {
220 public:
221 interface_block_definitions()
222 : mem_ctx(ralloc_context(NULL)),
223 ht(_mesa_hash_table_create(NULL, _mesa_hash_string,
224 _mesa_key_string_equal))
225 {
226 }
227
228 ~interface_block_definitions()
229 {
230 ralloc_free(mem_ctx);
231 _mesa_hash_table_destroy(ht, NULL);
232 }
233
234 /**
235 * Lookup the interface definition. Return NULL if none is found.
236 */
237 ir_variable *lookup(ir_variable *var)
238 {
239 if (var->data.explicit_location &&
240 var->data.location >= VARYING_SLOT_VAR0) {
241 char location_str[11];
242 snprintf(location_str, 11, "%d", var->data.location);
243
244 const struct hash_entry *entry =
245 _mesa_hash_table_search(ht, location_str);
246 return entry ? (ir_variable *) entry->data : NULL;
247 } else {
248 const struct hash_entry *entry =
249 _mesa_hash_table_search(ht,
250 var->get_interface_type()->without_array()->name);
251 return entry ? (ir_variable *) entry->data : NULL;
252 }
253 }
254
255 /**
256 * Add a new interface definition.
257 */
258 void store(ir_variable *var)
259 {
260 if (var->data.explicit_location &&
261 var->data.location >= VARYING_SLOT_VAR0) {
262 /* If explicit location is given then lookup the variable by location.
263 * We turn the location into a string and use this as the hash key
264 * rather than the name. Note: We allocate enough space for a 32-bit
265 * unsigned location value which is overkill but future proof.
266 */
267 char location_str[11];
268 snprintf(location_str, 11, "%d", var->data.location);
269 _mesa_hash_table_insert(ht, ralloc_strdup(mem_ctx, location_str), var);
270 } else {
271 _mesa_hash_table_insert(ht,
272 var->get_interface_type()->without_array()->name, var);
273 }
274 }
275
276 private:
277 /**
278 * Ralloc context for data structures allocated by this class.
279 */
280 void *mem_ctx;
281
282 /**
283 * Hash table mapping interface block name to an \c
284 * ir_variable.
285 */
286 hash_table *ht;
287 };
288
289
290 }; /* anonymous namespace */
291
292
293 void
294 validate_intrastage_interface_blocks(struct gl_shader_program *prog,
295 const gl_shader **shader_list,
296 unsigned num_shaders)
297 {
298 interface_block_definitions in_interfaces;
299 interface_block_definitions out_interfaces;
300 interface_block_definitions uniform_interfaces;
301 interface_block_definitions buffer_interfaces;
302
303 for (unsigned int i = 0; i < num_shaders; i++) {
304 if (shader_list[i] == NULL)
305 continue;
306
307 foreach_in_list(ir_instruction, node, shader_list[i]->ir) {
308 ir_variable *var = node->as_variable();
309 if (!var)
310 continue;
311
312 const glsl_type *iface_type = var->get_interface_type();
313
314 if (iface_type == NULL)
315 continue;
316
317 interface_block_definitions *definitions;
318 switch (var->data.mode) {
319 case ir_var_shader_in:
320 definitions = &in_interfaces;
321 break;
322 case ir_var_shader_out:
323 definitions = &out_interfaces;
324 break;
325 case ir_var_uniform:
326 definitions = &uniform_interfaces;
327 break;
328 case ir_var_shader_storage:
329 definitions = &buffer_interfaces;
330 break;
331 default:
332 /* Only in, out, and uniform interfaces are legal, so we should
333 * never get here.
334 */
335 assert(!"illegal interface type");
336 continue;
337 }
338
339 ir_variable *prev_def = definitions->lookup(var);
340 if (prev_def == NULL) {
341 /* This is the first time we've seen the interface, so save
342 * it into the appropriate data structure.
343 */
344 definitions->store(var);
345 } else if (!intrastage_match(prev_def, var, prog,
346 true /* match_precision */)) {
347 linker_error(prog, "definitions of interface block `%s' do not"
348 " match\n", iface_type->name);
349 return;
350 }
351 }
352 }
353 }
354
355 static bool
356 is_builtin_gl_in_block(ir_variable *var, int consumer_stage)
357 {
358 return !strcmp(var->name, "gl_in") &&
359 (consumer_stage == MESA_SHADER_TESS_CTRL ||
360 consumer_stage == MESA_SHADER_TESS_EVAL ||
361 consumer_stage == MESA_SHADER_GEOMETRY);
362 }
363
364 void
365 validate_interstage_inout_blocks(struct gl_shader_program *prog,
366 const gl_linked_shader *producer,
367 const gl_linked_shader *consumer)
368 {
369 interface_block_definitions definitions;
370 /* VS -> GS, VS -> TCS, VS -> TES, TES -> GS */
371 const bool extra_array_level = (producer->Stage == MESA_SHADER_VERTEX &&
372 consumer->Stage != MESA_SHADER_FRAGMENT) ||
373 consumer->Stage == MESA_SHADER_GEOMETRY;
374
375 /* Check that block re-declarations of gl_PerVertex are compatible
376 * across shaders: From OpenGL Shading Language 4.5, section
377 * "7.1 Built-In Language Variables", page 130 of the PDF:
378 *
379 * "If multiple shaders using members of a built-in block belonging
380 * to the same interface are linked together in the same program,
381 * they must all redeclare the built-in block in the same way, as
382 * described in section 4.3.9 “Interface Blocks” for interface-block
383 * matching, or a link-time error will result."
384 *
385 * This is done explicitly outside of iterating the member variable
386 * declarations because it is possible that the variables are not used and
387 * so they would have been optimised out.
388 */
389 const glsl_type *consumer_iface =
390 consumer->symbols->get_interface("gl_PerVertex",
391 ir_var_shader_in);
392
393 const glsl_type *producer_iface =
394 producer->symbols->get_interface("gl_PerVertex",
395 ir_var_shader_out);
396
397 if (producer_iface && consumer_iface &&
398 interstage_member_mismatch(prog, consumer_iface, producer_iface)) {
399 linker_error(prog, "Incompatible or missing gl_PerVertex re-declaration "
400 "in consecutive shaders");
401 return;
402 }
403
404 /* Desktop OpenGL requires redeclaration of the built-in interfaces for
405 * SSO programs. Passes above implement following rules:
406 *
407 * From Section 7.4 (Program Pipeline Objects) of the OpenGL 4.6 Core
408 * spec:
409 *
410 * "To use any built-in input or output in the gl_PerVertex and
411 * gl_PerFragment blocks in separable program objects, shader code
412 * must redeclare those blocks prior to use. A separable program
413 * will fail to link if:
414 *
415 * it contains multiple shaders of a single type with different
416 * redeclarations of these built-in input and output blocks; or
417 *
418 * any shader uses a built-in block member not found in the
419 * redeclaration of that block."
420 *
421 * ARB_separate_shader_objects issues section (issue #28) states that
422 * redeclaration is not required for GLSL shaders using #version 140 or
423 * earlier (since interface blocks are not possible with older versions).
424 *
425 * From Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1
426 * spec:
427 *
428 * "Built-in inputs or outputs do not affect interface matching."
429 *
430 * GL_OES_shader_io_blocks adds following:
431 *
432 * "When using any built-in input or output in the gl_PerVertex block
433 * in separable program objects, shader code may redeclare that block
434 * prior to use. If the shader does not redeclare the block, the
435 * intrinsically declared definition of that block will be used."
436 */
437
438 /* Add output interfaces from the producer to the symbol table. */
439 foreach_in_list(ir_instruction, node, producer->ir) {
440 ir_variable *var = node->as_variable();
441 if (!var || !var->get_interface_type() || var->data.mode != ir_var_shader_out)
442 continue;
443
444 /* Built-in interface redeclaration check. */
445 if (prog->SeparateShader && !prog->IsES && prog->data->Version >= 150 &&
446 var->data.how_declared == ir_var_declared_implicitly &&
447 var->data.used && !producer_iface) {
448 linker_error(prog, "missing output builtin block %s redeclaration "
449 "in separable shader program",
450 var->get_interface_type()->name);
451 return;
452 }
453
454 definitions.store(var);
455 }
456
457 /* Verify that the consumer's input interfaces match. */
458 foreach_in_list(ir_instruction, node, consumer->ir) {
459 ir_variable *var = node->as_variable();
460 if (!var || !var->get_interface_type() || var->data.mode != ir_var_shader_in)
461 continue;
462
463 ir_variable *producer_def = definitions.lookup(var);
464
465 /* Built-in interface redeclaration check. */
466 if (prog->SeparateShader && !prog->IsES && prog->data->Version >= 150 &&
467 var->data.how_declared == ir_var_declared_implicitly &&
468 var->data.used && !producer_iface) {
469 linker_error(prog, "missing input builtin block %s redeclaration "
470 "in separable shader program",
471 var->get_interface_type()->name);
472 return;
473 }
474
475 /* The producer doesn't generate this input: fail to link. Skip built-in
476 * 'gl_in[]' since that may not be present if the producer does not
477 * write to any of the pre-defined outputs (e.g. if the vertex shader
478 * does not write to gl_Position, etc), which is allowed and results in
479 * undefined behavior.
480 *
481 * From Section 4.3.4 (Inputs) of the GLSL 1.50 spec:
482 *
483 * "Only the input variables that are actually read need to be written
484 * by the previous stage; it is allowed to have superfluous
485 * declarations of input variables."
486 */
487 if (producer_def == NULL &&
488 !is_builtin_gl_in_block(var, consumer->Stage) && var->data.used) {
489 linker_error(prog, "Input block `%s' is not an output of "
490 "the previous stage\n", var->get_interface_type()->name);
491 return;
492 }
493
494 if (producer_def &&
495 !interstage_match(prog, producer_def, var, extra_array_level)) {
496 linker_error(prog, "definitions of interface block `%s' do not "
497 "match\n", var->get_interface_type()->name);
498 return;
499 }
500 }
501 }
502
503
504 void
505 validate_interstage_uniform_blocks(struct gl_shader_program *prog,
506 gl_linked_shader **stages)
507 {
508 interface_block_definitions definitions;
509
510 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
511 if (stages[i] == NULL)
512 continue;
513
514 const gl_linked_shader *stage = stages[i];
515 foreach_in_list(ir_instruction, node, stage->ir) {
516 ir_variable *var = node->as_variable();
517 if (!var || !var->get_interface_type() ||
518 (var->data.mode != ir_var_uniform &&
519 var->data.mode != ir_var_shader_storage))
520 continue;
521
522 ir_variable *old_def = definitions.lookup(var);
523 if (old_def == NULL) {
524 definitions.store(var);
525 } else {
526 /* Interstage uniform matching rules are the same as intrastage
527 * uniform matchin rules (for uniforms, it is as though all
528 * shaders are in the same shader stage).
529 */
530 if (!intrastage_match(old_def, var, prog, false /* precision */)) {
531 linker_error(prog, "definitions of uniform block `%s' do not "
532 "match\n", var->get_interface_type()->name);
533 return;
534 }
535 }
536 }
537 }
538 }