glsl: Add an ir_variable::reinit_interface_type() function.
[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 void
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->get_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;
69 }
70 }
71 }
72 }
73
74 void
75 validate_interstage_interface_blocks(struct gl_shader_program *prog,
76 const gl_shader *producer,
77 const gl_shader *consumer)
78 {
79 glsl_symbol_table interfaces;
80
81 /* Add non-output interfaces from the consumer to the symbol table. */
82 foreach_list(node, consumer->ir) {
83 ir_variable *var = ((ir_instruction *) node)->as_variable();
84 if (!var || !var->get_interface_type() || var->mode == ir_var_shader_out)
85 continue;
86
87 interfaces.add_interface(var->get_interface_type()->name,
88 var->get_interface_type(),
89 (enum ir_variable_mode) var->mode);
90 }
91
92 /* Verify that the producer's interfaces match. */
93 foreach_list(node, producer->ir) {
94 ir_variable *var = ((ir_instruction *) node)->as_variable();
95 if (!var || !var->get_interface_type() || var->mode == ir_var_shader_in)
96 continue;
97
98 enum ir_variable_mode consumer_mode =
99 var->mode == ir_var_uniform ? ir_var_uniform : ir_var_shader_in;
100 const glsl_type *expected_type =
101 interfaces.get_interface(var->get_interface_type()->name,
102 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->get_interface_type() != expected_type) {
109 linker_error(prog, "definitions of interface block `%s' do not "
110 "match\n", var->get_interface_type()->name);
111 return;
112 }
113 }
114 }