2 * Copyright © 2013 Intel Corporation
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:
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
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.
25 * \file link_interface_blocks.cpp
26 * Linker support for GLSL's interface blocks.
30 #include "glsl_symbol_table.h"
32 #include "main/macros.h"
33 #include "program/hash_table.h"
39 * Information about a single interface block definition that we need to keep
40 * track of in order to check linkage rules.
42 * Note: this class is expected to be short lived, so it doesn't make copies
43 * of the strings it references; it simply borrows the pointers from the
46 struct interface_block_definition
49 * Extract an interface block definition from an ir_variable that
50 * represents either the interface instance (for named interfaces), or a
51 * member of the interface (for unnamed interfaces).
53 explicit interface_block_definition(const ir_variable
*var
)
54 : type(var
->get_interface_type()),
58 if (var
->is_interface_instance()) {
59 instance_name
= var
->name
;
60 if (var
->type
->is_array())
61 array_size
= var
->type
->length
;
66 * Interface block type
68 const glsl_type
*type
;
71 * For a named interface block, the instance name. Otherwise NULL.
73 const char *instance_name
;
76 * For an interface block array, the array size (or 0 if unsized).
84 * Check if two interfaces match, according to intrastage interface matching
85 * rules. If they do, and the first interface uses an unsized array, it will
86 * be updated to reflect the array size declared in the second interface.
89 intrastage_match(interface_block_definition
*a
,
90 const interface_block_definition
*b
,
91 ir_variable_mode mode
)
93 /* Types must match. */
94 if (a
->type
!= b
->type
)
97 /* Presence/absence of interface names must match. */
98 if ((a
->instance_name
== NULL
) != (b
->instance_name
== NULL
))
101 /* For uniforms, instance names need not match. For shader ins/outs,
102 * it's not clear from the spec whether they need to match, but
103 * Mesa's implementation relies on them matching.
105 if (a
->instance_name
!= NULL
&& mode
!= ir_var_uniform
&&
106 strcmp(a
->instance_name
, b
->instance_name
) != 0) {
110 /* Array vs. nonarray must be consistent, and sizes must be
111 * consistent, with the exception that unsized arrays match sized
114 if ((a
->array_size
== -1) != (b
->array_size
== -1))
116 if (b
->array_size
!= 0) {
117 if (a
->array_size
== 0)
118 a
->array_size
= b
->array_size
;
119 else if (a
->array_size
!= b
->array_size
)
128 * Check if two interfaces match, according to interstage (in/out) interface
131 * If \c extra_array_level is true, then vertex-to-geometry shader matching
132 * rules are enforced (i.e. a successful match requires the consumer interface
133 * to be an array and the producer interface to be a non-array).
136 interstage_match(const interface_block_definition
*producer
,
137 const interface_block_definition
*consumer
,
138 bool extra_array_level
)
140 /* Unsized arrays should not occur during interstage linking. They
141 * should have all been assigned a size by link_intrastage_shaders.
143 assert(consumer
->array_size
!= 0);
144 assert(producer
->array_size
!= 0);
146 /* Types must match. */
147 if (consumer
->type
!= producer
->type
)
149 if (extra_array_level
) {
150 /* Consumer must be an array, and producer must not. */
151 if (consumer
->array_size
== -1)
153 if (producer
->array_size
!= -1)
156 /* Array vs. nonarray must be consistent, and sizes must be consistent.
157 * Since unsized arrays have been ruled out, we can check this by just
158 * making sure the sizes are equal.
160 if (consumer
->array_size
!= producer
->array_size
)
168 * This class keeps track of a mapping from an interface block name to the
169 * necessary information about that interface block to determine whether to
170 * generate a link error.
172 * Note: this class is expected to be short lived, so it doesn't make copies
173 * of the strings it references; it simply borrows the pointers from the
176 class interface_block_definitions
179 interface_block_definitions()
180 : mem_ctx(ralloc_context(NULL
)),
181 ht(hash_table_ctor(0, hash_table_string_hash
,
182 hash_table_string_compare
))
186 ~interface_block_definitions()
189 ralloc_free(mem_ctx
);
193 * Lookup the interface definition having the given block name. Return
194 * NULL if none is found.
196 interface_block_definition
*lookup(const char *block_name
)
198 return (interface_block_definition
*) hash_table_find(ht
, block_name
);
202 * Add a new interface definition.
204 void store(const interface_block_definition
&def
)
206 interface_block_definition
*hash_entry
=
207 rzalloc(mem_ctx
, interface_block_definition
);
209 hash_table_insert(ht
, hash_entry
, def
.type
->name
);
214 * Ralloc context for data structures allocated by this class.
219 * Hash table mapping interface block name to an \c
220 * interface_block_definition struct. interface_block_definition structs
221 * are allocated using \c mem_ctx.
227 }; /* anonymous namespace */
231 validate_intrastage_interface_blocks(struct gl_shader_program
*prog
,
232 const gl_shader
**shader_list
,
233 unsigned num_shaders
)
235 interface_block_definitions in_interfaces
;
236 interface_block_definitions out_interfaces
;
237 interface_block_definitions uniform_interfaces
;
239 for (unsigned int i
= 0; i
< num_shaders
; i
++) {
240 if (shader_list
[i
] == NULL
)
243 foreach_list(node
, shader_list
[i
]->ir
) {
244 ir_variable
*var
= ((ir_instruction
*) node
)->as_variable();
248 const glsl_type
*iface_type
= var
->get_interface_type();
250 if (iface_type
== NULL
)
253 interface_block_definitions
*definitions
;
255 case ir_var_shader_in
:
256 definitions
= &in_interfaces
;
258 case ir_var_shader_out
:
259 definitions
= &out_interfaces
;
262 definitions
= &uniform_interfaces
;
265 /* Only in, out, and uniform interfaces are legal, so we should
268 assert(!"illegal interface type");
272 const interface_block_definition
def(var
);
273 interface_block_definition
*prev_def
=
274 definitions
->lookup(iface_type
->name
);
276 if (prev_def
== NULL
) {
277 /* This is the first time we've seen the interface, so save
278 * it into the appropriate data structure.
280 definitions
->store(def
);
281 } else if (!intrastage_match(prev_def
, &def
,
282 (ir_variable_mode
) var
->mode
)) {
283 linker_error(prog
, "definitions of interface block `%s' do not"
284 " match\n", iface_type
->name
);
292 validate_interstage_interface_blocks(struct gl_shader_program
*prog
,
293 const gl_shader
*producer
,
294 const gl_shader
*consumer
)
296 interface_block_definitions inout_interfaces
;
297 interface_block_definitions uniform_interfaces
;
298 const bool extra_array_level
= consumer
->Type
== GL_GEOMETRY_SHADER
;
300 /* Add non-output interfaces from the consumer to the symbol table. */
301 foreach_list(node
, consumer
->ir
) {
302 ir_variable
*var
= ((ir_instruction
*) node
)->as_variable();
303 if (!var
|| !var
->get_interface_type() || var
->mode
== ir_var_shader_out
)
306 interface_block_definitions
*definitions
= var
->mode
== ir_var_uniform
?
307 &uniform_interfaces
: &inout_interfaces
;
308 definitions
->store(interface_block_definition(var
));
311 /* Verify that the producer's interfaces match. */
312 foreach_list(node
, producer
->ir
) {
313 ir_variable
*var
= ((ir_instruction
*) node
)->as_variable();
314 if (!var
|| !var
->get_interface_type() || var
->mode
== ir_var_shader_in
)
317 interface_block_definitions
*definitions
= var
->mode
== ir_var_uniform
?
318 &uniform_interfaces
: &inout_interfaces
;
319 interface_block_definition
*consumer_def
=
320 definitions
->lookup(var
->get_interface_type()->name
);
322 /* The consumer doesn't use this output block. Ignore it. */
323 if (consumer_def
== NULL
)
326 const interface_block_definition
producer_def(var
);
328 if (var
->mode
== ir_var_uniform
) {
329 /* Uniform matching rules are the same for interstage and intrastage
332 match
= intrastage_match(consumer_def
, &producer_def
,
333 (ir_variable_mode
) var
->mode
);
335 match
= interstage_match(&producer_def
, consumer_def
,
340 linker_error(prog
, "definitions of interface block `%s' do not "
341 "match\n", var
->get_interface_type()->name
);