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"
35 #include "util/u_string.h"
41 * Return true if interface members mismatch and its not allowed by GLSL.
44 interstage_member_mismatch(struct gl_shader_program
*prog
,
45 const glsl_type
*c
, const glsl_type
*p
) {
47 if (c
->length
!= p
->length
)
50 for (unsigned i
= 0; i
< c
->length
; i
++) {
51 if (c
->fields
.structure
[i
].type
!= p
->fields
.structure
[i
].type
)
53 if (strcmp(c
->fields
.structure
[i
].name
,
54 p
->fields
.structure
[i
].name
) != 0)
56 if (c
->fields
.structure
[i
].location
!=
57 p
->fields
.structure
[i
].location
)
59 if (c
->fields
.structure
[i
].patch
!=
60 p
->fields
.structure
[i
].patch
)
63 /* From Section 4.5 (Interpolation Qualifiers) of the GLSL 4.40 spec:
65 * "It is a link-time error if, within the same stage, the
66 * interpolation qualifiers of variables of the same name do not
69 if (prog
->IsES
|| prog
->data
->Version
< 440)
70 if (c
->fields
.structure
[i
].interpolation
!=
71 p
->fields
.structure
[i
].interpolation
)
74 /* From Section 4.3.4 (Input Variables) of the GLSL ES 3.0 spec:
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).
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.
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.
88 if (!prog
->IsES
|| prog
->data
->Version
< 310)
89 if (c
->fields
.structure
[i
].centroid
!=
90 p
->fields
.structure
[i
].centroid
)
93 if (c
->fields
.structure
[i
].sample
!=
94 p
->fields
.structure
[i
].sample
)
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.
107 intrastage_match(ir_variable
*a
,
109 struct gl_shader_program
*prog
)
111 /* Types must match. */
112 if (a
->get_interface_type() != b
->get_interface_type()) {
113 /* Exception: if both the interface blocks are implicitly declared,
114 * don't force their types to match. They might mismatch due to the two
115 * shaders using different GLSL versions, and that's ok.
117 if ((a
->data
.how_declared
!= ir_var_declared_implicitly
||
118 b
->data
.how_declared
!= ir_var_declared_implicitly
) &&
120 interstage_member_mismatch(prog
, a
->get_interface_type(),
121 b
->get_interface_type())))
125 /* Presence/absence of interface names must match. */
126 if (a
->is_interface_instance() != b
->is_interface_instance())
129 /* For uniforms, instance names need not match. For shader ins/outs,
130 * it's not clear from the spec whether they need to match, but
131 * Mesa's implementation relies on them matching.
133 if (a
->is_interface_instance() && b
->data
.mode
!= ir_var_uniform
&&
134 b
->data
.mode
!= ir_var_shader_storage
&&
135 strcmp(a
->name
, b
->name
) != 0) {
139 /* If a block is an array then it must match across the shader.
140 * Unsized arrays are also processed and matched agaist sized arrays.
142 if (b
->type
!= a
->type
&& (b
->type
->is_array() || a
->type
->is_array()) &&
143 (b
->is_interface_instance() || a
->is_interface_instance()) &&
144 !validate_intrastage_arrays(prog
, b
, a
))
151 * Check if two interfaces match, according to interstage (in/out) interface
154 * If \c extra_array_level is true, the consumer interface is required to be
155 * an array and the producer interface is required to be a non-array.
156 * This is used for tessellation control and geometry shader consumers.
159 interstage_match(struct gl_shader_program
*prog
, ir_variable
*producer
,
160 ir_variable
*consumer
, bool extra_array_level
)
162 /* Types must match. */
163 if (consumer
->get_interface_type() != producer
->get_interface_type()) {
164 /* Exception: if both the interface blocks are implicitly declared,
165 * don't force their types to match. They might mismatch due to the two
166 * shaders using different GLSL versions, and that's ok.
168 * Also we store some member information such as interpolation in
169 * glsl_type that doesn't always have to match across shader stages.
170 * Therefore we make a pass over the members glsl_struct_field to make
171 * sure we don't reject shaders where fields don't need to match.
173 if ((consumer
->data
.how_declared
!= ir_var_declared_implicitly
||
174 producer
->data
.how_declared
!= ir_var_declared_implicitly
) &&
175 interstage_member_mismatch(prog
, consumer
->get_interface_type(),
176 producer
->get_interface_type()))
180 /* Ignore outermost array if geom shader */
181 const glsl_type
*consumer_instance_type
;
182 if (extra_array_level
) {
183 consumer_instance_type
= consumer
->type
->fields
.array
;
185 consumer_instance_type
= consumer
->type
;
188 /* If a block is an array then it must match across shaders.
189 * Since unsized arrays have been ruled out, we can check this by just
190 * making sure the types are equal.
192 if ((consumer
->is_interface_instance() &&
193 consumer_instance_type
->is_array()) ||
194 (producer
->is_interface_instance() &&
195 producer
->type
->is_array())) {
196 if (consumer_instance_type
!= producer
->type
)
205 * This class keeps track of a mapping from an interface block name to the
206 * necessary information about that interface block to determine whether to
207 * generate a link error.
209 * Note: this class is expected to be short lived, so it doesn't make copies
210 * of the strings it references; it simply borrows the pointers from the
213 class interface_block_definitions
216 interface_block_definitions()
217 : mem_ctx(ralloc_context(NULL
)),
218 ht(_mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
219 _mesa_key_string_equal
))
223 ~interface_block_definitions()
225 ralloc_free(mem_ctx
);
226 _mesa_hash_table_destroy(ht
, NULL
);
230 * Lookup the interface definition. Return NULL if none is found.
232 ir_variable
*lookup(ir_variable
*var
)
234 if (var
->data
.explicit_location
&&
235 var
->data
.location
>= VARYING_SLOT_VAR0
) {
236 char location_str
[11];
237 util_snprintf(location_str
, 11, "%d", var
->data
.location
);
239 const struct hash_entry
*entry
=
240 _mesa_hash_table_search(ht
, location_str
);
241 return entry
? (ir_variable
*) entry
->data
: NULL
;
243 const struct hash_entry
*entry
=
244 _mesa_hash_table_search(ht
,
245 var
->get_interface_type()->without_array()->name
);
246 return entry
? (ir_variable
*) entry
->data
: NULL
;
251 * Add a new interface definition.
253 void store(ir_variable
*var
)
255 if (var
->data
.explicit_location
&&
256 var
->data
.location
>= VARYING_SLOT_VAR0
) {
257 /* If explicit location is given then lookup the variable by location.
258 * We turn the location into a string and use this as the hash key
259 * rather than the name. Note: We allocate enough space for a 32-bit
260 * unsigned location value which is overkill but future proof.
262 char location_str
[11];
263 util_snprintf(location_str
, 11, "%d", var
->data
.location
);
264 _mesa_hash_table_insert(ht
, ralloc_strdup(mem_ctx
, location_str
), var
);
266 _mesa_hash_table_insert(ht
,
267 var
->get_interface_type()->without_array()->name
, var
);
273 * Ralloc context for data structures allocated by this class.
278 * Hash table mapping interface block name to an \c
285 }; /* anonymous namespace */
289 validate_intrastage_interface_blocks(struct gl_shader_program
*prog
,
290 const gl_shader
**shader_list
,
291 unsigned num_shaders
)
293 interface_block_definitions in_interfaces
;
294 interface_block_definitions out_interfaces
;
295 interface_block_definitions uniform_interfaces
;
296 interface_block_definitions buffer_interfaces
;
298 for (unsigned int i
= 0; i
< num_shaders
; i
++) {
299 if (shader_list
[i
] == NULL
)
302 foreach_in_list(ir_instruction
, node
, shader_list
[i
]->ir
) {
303 ir_variable
*var
= node
->as_variable();
307 const glsl_type
*iface_type
= var
->get_interface_type();
309 if (iface_type
== NULL
)
312 interface_block_definitions
*definitions
;
313 switch (var
->data
.mode
) {
314 case ir_var_shader_in
:
315 definitions
= &in_interfaces
;
317 case ir_var_shader_out
:
318 definitions
= &out_interfaces
;
321 definitions
= &uniform_interfaces
;
323 case ir_var_shader_storage
:
324 definitions
= &buffer_interfaces
;
327 /* Only in, out, and uniform interfaces are legal, so we should
330 assert(!"illegal interface type");
334 ir_variable
*prev_def
= definitions
->lookup(var
);
335 if (prev_def
== NULL
) {
336 /* This is the first time we've seen the interface, so save
337 * it into the appropriate data structure.
339 definitions
->store(var
);
340 } else if (!intrastage_match(prev_def
, var
, prog
)) {
341 linker_error(prog
, "definitions of interface block `%s' do not"
342 " match\n", iface_type
->name
);
350 is_builtin_gl_in_block(ir_variable
*var
, int consumer_stage
)
352 return !strcmp(var
->name
, "gl_in") &&
353 (consumer_stage
== MESA_SHADER_TESS_CTRL
||
354 consumer_stage
== MESA_SHADER_TESS_EVAL
||
355 consumer_stage
== MESA_SHADER_GEOMETRY
);
359 validate_interstage_inout_blocks(struct gl_shader_program
*prog
,
360 const gl_linked_shader
*producer
,
361 const gl_linked_shader
*consumer
)
363 interface_block_definitions definitions
;
364 /* VS -> GS, VS -> TCS, VS -> TES, TES -> GS */
365 const bool extra_array_level
= (producer
->Stage
== MESA_SHADER_VERTEX
&&
366 consumer
->Stage
!= MESA_SHADER_FRAGMENT
) ||
367 consumer
->Stage
== MESA_SHADER_GEOMETRY
;
369 /* Check that block re-declarations of gl_PerVertex are compatible
370 * across shaders: From OpenGL Shading Language 4.5, section
371 * "7.1 Built-In Language Variables", page 130 of the PDF:
373 * "If multiple shaders using members of a built-in block belonging
374 * to the same interface are linked together in the same program,
375 * they must all redeclare the built-in block in the same way, as
376 * described in section 4.3.9 “Interface Blocks” for interface-block
377 * matching, or a link-time error will result."
379 * This is done explicitly outside of iterating the member variable
380 * declarations because it is possible that the variables are not used and
381 * so they would have been optimised out.
383 const glsl_type
*consumer_iface
=
384 consumer
->symbols
->get_interface("gl_PerVertex",
387 const glsl_type
*producer_iface
=
388 producer
->symbols
->get_interface("gl_PerVertex",
391 if (producer_iface
&& consumer_iface
&&
392 interstage_member_mismatch(prog
, consumer_iface
, producer_iface
)) {
393 linker_error(prog
, "Incompatible or missing gl_PerVertex re-declaration "
394 "in consecutive shaders");
398 /* Add output interfaces from the producer to the symbol table. */
399 foreach_in_list(ir_instruction
, node
, producer
->ir
) {
400 ir_variable
*var
= node
->as_variable();
401 if (!var
|| !var
->get_interface_type() || var
->data
.mode
!= ir_var_shader_out
)
404 definitions
.store(var
);
407 /* Verify that the consumer's input interfaces match. */
408 foreach_in_list(ir_instruction
, node
, consumer
->ir
) {
409 ir_variable
*var
= node
->as_variable();
410 if (!var
|| !var
->get_interface_type() || var
->data
.mode
!= ir_var_shader_in
)
413 ir_variable
*producer_def
= definitions
.lookup(var
);
415 /* The producer doesn't generate this input: fail to link. Skip built-in
416 * 'gl_in[]' since that may not be present if the producer does not
417 * write to any of the pre-defined outputs (e.g. if the vertex shader
418 * does not write to gl_Position, etc), which is allowed and results in
419 * undefined behavior.
421 * From Section 4.3.4 (Inputs) of the GLSL 1.50 spec:
423 * "Only the input variables that are actually read need to be written
424 * by the previous stage; it is allowed to have superfluous
425 * declarations of input variables."
427 if (producer_def
== NULL
&&
428 !is_builtin_gl_in_block(var
, consumer
->Stage
) && var
->data
.used
) {
429 linker_error(prog
, "Input block `%s' is not an output of "
430 "the previous stage\n", var
->get_interface_type()->name
);
435 !interstage_match(prog
, producer_def
, var
, extra_array_level
)) {
436 linker_error(prog
, "definitions of interface block `%s' do not "
437 "match\n", var
->get_interface_type()->name
);
445 validate_interstage_uniform_blocks(struct gl_shader_program
*prog
,
446 gl_linked_shader
**stages
)
448 interface_block_definitions definitions
;
450 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
451 if (stages
[i
] == NULL
)
454 const gl_linked_shader
*stage
= stages
[i
];
455 foreach_in_list(ir_instruction
, node
, stage
->ir
) {
456 ir_variable
*var
= node
->as_variable();
457 if (!var
|| !var
->get_interface_type() ||
458 (var
->data
.mode
!= ir_var_uniform
&&
459 var
->data
.mode
!= ir_var_shader_storage
))
462 ir_variable
*old_def
= definitions
.lookup(var
);
463 if (old_def
== NULL
) {
464 definitions
.store(var
);
466 /* Interstage uniform matching rules are the same as intrastage
467 * uniform matchin rules (for uniforms, it is as though all
468 * shaders are in the same shader stage).
470 if (!intrastage_match(old_def
, var
, prog
)) {
471 linker_error(prog
, "definitions of uniform block `%s' do not "
472 "match\n", var
->get_interface_type()->name
);