glsl: Add error message for intrastage interface block mismatch.
[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
34 bool
35 validate_intrastage_interface_blocks(struct gl_shader_program *prog,
36 const gl_shader **shader_list,
37 unsigned num_shaders)
38 {
39 glsl_symbol_table interfaces;
40
41 for (unsigned int i = 0; i < num_shaders; i++) {
42 if (shader_list[i] == NULL)
43 continue;
44
45 foreach_list(node, shader_list[i]->ir) {
46 ir_variable *var = ((ir_instruction *) node)->as_variable();
47 if (!var)
48 continue;
49
50 const glsl_type *iface_type = var->interface_type;
51
52 if (iface_type == NULL)
53 continue;
54
55 const glsl_type *old_iface_type =
56 interfaces.get_interface(iface_type->name,
57 (enum ir_variable_mode) var->mode);
58
59 if (old_iface_type == NULL) {
60 /* This is the first time we've seen the interface, so save
61 * it into our symbol table.
62 */
63 interfaces.add_interface(iface_type->name, iface_type,
64 (enum ir_variable_mode) var->mode);
65 } else if (old_iface_type != iface_type) {
66 linker_error(prog, "definitions of interface block `%s' do not"
67 " match\n", iface_type->name);
68 return false;
69 }
70 }
71 }
72
73 return true;
74 }
75
76 bool
77 validate_interstage_interface_blocks(const gl_shader *producer,
78 const gl_shader *consumer)
79 {
80 glsl_symbol_table interfaces;
81
82 /* Add non-output interfaces from the consumer to the symbol table. */
83 foreach_list(node, consumer->ir) {
84 ir_variable *var = ((ir_instruction *) node)->as_variable();
85 if (!var || !var->interface_type || var->mode == ir_var_shader_out)
86 continue;
87
88 interfaces.add_interface(var->interface_type->name,
89 var->interface_type,
90 (enum ir_variable_mode) var->mode);
91 }
92
93 /* Verify that the producer's interfaces match. */
94 foreach_list(node, producer->ir) {
95 ir_variable *var = ((ir_instruction *) node)->as_variable();
96 if (!var || !var->interface_type || var->mode == ir_var_shader_in)
97 continue;
98
99 enum ir_variable_mode consumer_mode =
100 var->mode == ir_var_uniform ? ir_var_uniform : ir_var_shader_in;
101 const glsl_type *expected_type =
102 interfaces.get_interface(var->interface_type->name, consumer_mode);
103
104 /* The consumer doesn't use this output block. Ignore it. */
105 if (expected_type == NULL)
106 continue;
107
108 if (var->interface_type != expected_type)
109 return false;
110 }
111
112 return true;
113 }