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 "main/mtypes.h"
34 #include "util/hash_table.h"
40 * Return true if interface members mismatch and its not allowed by GLSL.
43 interstage_member_mismatch(struct gl_shader_program
*prog
,
44 const glsl_type
*c
, const glsl_type
*p
) {
46 if (c
->length
!= p
->length
)
49 for (unsigned i
= 0; i
< c
->length
; i
++) {
50 if (c
->fields
.structure
[i
].type
!= p
->fields
.structure
[i
].type
)
52 if (strcmp(c
->fields
.structure
[i
].name
,
53 p
->fields
.structure
[i
].name
) != 0)
55 if (c
->fields
.structure
[i
].location
!=
56 p
->fields
.structure
[i
].location
)
58 if (c
->fields
.structure
[i
].patch
!=
59 p
->fields
.structure
[i
].patch
)
62 /* From Section 4.5 (Interpolation Qualifiers) of the GLSL 4.40 spec:
64 * "It is a link-time error if, within the same stage, the
65 * interpolation qualifiers of variables of the same name do not
68 if (prog
->IsES
|| prog
->data
->Version
< 440)
69 if (c
->fields
.structure
[i
].interpolation
!=
70 p
->fields
.structure
[i
].interpolation
)
73 /* From Section 4.3.4 (Input Variables) of the GLSL ES 3.0 spec:
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).
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.
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.
87 if (!prog
->IsES
|| prog
->data
->Version
< 310)
88 if (c
->fields
.structure
[i
].centroid
!=
89 p
->fields
.structure
[i
].centroid
)
92 if (c
->fields
.structure
[i
].sample
!=
93 p
->fields
.structure
[i
].sample
)
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.
106 intrastage_match(ir_variable
*a
,
108 struct gl_shader_program
*prog
)
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.
116 if ((a
->data
.how_declared
!= ir_var_declared_implicitly
||
117 b
->data
.how_declared
!= ir_var_declared_implicitly
) &&
119 interstage_member_mismatch(prog
, a
->get_interface_type(),
120 b
->get_interface_type())))
124 /* Presence/absence of interface names must match. */
125 if (a
->is_interface_instance() != b
->is_interface_instance())
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.
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) {
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.
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
))
150 * Check if two interfaces match, according to interstage (in/out) interface
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.
158 interstage_match(struct gl_shader_program
*prog
, ir_variable
*producer
,
159 ir_variable
*consumer
, bool extra_array_level
)
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.
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.
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()))
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
;
184 consumer_instance_type
= consumer
->type
;
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.
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
)
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.
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
212 class interface_block_definitions
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
))
222 ~interface_block_definitions()
224 ralloc_free(mem_ctx
);
225 _mesa_hash_table_destroy(ht
, NULL
);
229 * Lookup the interface definition. Return NULL if none is found.
231 ir_variable
*lookup(ir_variable
*var
)
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
);
238 const struct hash_entry
*entry
=
239 _mesa_hash_table_search(ht
, location_str
);
240 return entry
? (ir_variable
*) entry
->data
: NULL
;
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
;
250 * Add a new interface definition.
252 void store(ir_variable
*var
)
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.
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
);
265 _mesa_hash_table_insert(ht
,
266 var
->get_interface_type()->without_array()->name
, var
);
272 * Ralloc context for data structures allocated by this class.
277 * Hash table mapping interface block name to an \c
284 }; /* anonymous namespace */
288 validate_intrastage_interface_blocks(struct gl_shader_program
*prog
,
289 const gl_shader
**shader_list
,
290 unsigned num_shaders
)
292 interface_block_definitions in_interfaces
;
293 interface_block_definitions out_interfaces
;
294 interface_block_definitions uniform_interfaces
;
295 interface_block_definitions buffer_interfaces
;
297 for (unsigned int i
= 0; i
< num_shaders
; i
++) {
298 if (shader_list
[i
] == NULL
)
301 foreach_in_list(ir_instruction
, node
, shader_list
[i
]->ir
) {
302 ir_variable
*var
= node
->as_variable();
306 const glsl_type
*iface_type
= var
->get_interface_type();
308 if (iface_type
== NULL
)
311 interface_block_definitions
*definitions
;
312 switch (var
->data
.mode
) {
313 case ir_var_shader_in
:
314 definitions
= &in_interfaces
;
316 case ir_var_shader_out
:
317 definitions
= &out_interfaces
;
320 definitions
= &uniform_interfaces
;
322 case ir_var_shader_storage
:
323 definitions
= &buffer_interfaces
;
326 /* Only in, out, and uniform interfaces are legal, so we should
329 assert(!"illegal interface type");
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.
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
);
349 is_builtin_gl_in_block(ir_variable
*var
, int consumer_stage
)
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
);
358 validate_interstage_inout_blocks(struct gl_shader_program
*prog
,
359 const gl_linked_shader
*producer
,
360 const gl_linked_shader
*consumer
)
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
;
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:
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."
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.
382 const glsl_type
*consumer_iface
=
383 consumer
->symbols
->get_interface("gl_PerVertex",
386 const glsl_type
*producer_iface
=
387 producer
->symbols
->get_interface("gl_PerVertex",
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");
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
)
403 definitions
.store(var
);
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
)
412 ir_variable
*producer_def
= definitions
.lookup(var
);
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.
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
);
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
);
438 validate_interstage_uniform_blocks(struct gl_shader_program
*prog
,
439 gl_linked_shader
**stages
)
441 interface_block_definitions definitions
;
443 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
444 if (stages
[i
] == NULL
)
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
))
455 ir_variable
*old_def
= definitions
.lookup(var
);
456 if (old_def
== NULL
) {
457 definitions
.store(var
);
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).
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
);