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 "util/hash_table.h"
39 * Return true if interface members mismatch and its not allowed by GLSL.
42 interstage_member_mismatch(struct gl_shader_program
*prog
,
43 const glsl_type
*c
, const glsl_type
*p
) {
45 if (c
->length
!= p
->length
)
48 for (unsigned i
= 0; i
< c
->length
; i
++) {
49 if (c
->fields
.structure
[i
].type
!= p
->fields
.structure
[i
].type
)
51 if (strcmp(c
->fields
.structure
[i
].name
,
52 p
->fields
.structure
[i
].name
) != 0)
54 if (c
->fields
.structure
[i
].location
!=
55 p
->fields
.structure
[i
].location
)
57 if (c
->fields
.structure
[i
].patch
!=
58 p
->fields
.structure
[i
].patch
)
61 /* From Section 4.5 (Interpolation Qualifiers) of the GLSL 4.40 spec:
63 * "It is a link-time error if, within the same stage, the
64 * interpolation qualifiers of variables of the same name do not
67 if (prog
->IsES
|| prog
->data
->Version
< 440)
68 if (c
->fields
.structure
[i
].interpolation
!=
69 p
->fields
.structure
[i
].interpolation
)
72 /* From Section 4.3.4 (Input Variables) of the GLSL ES 3.0 spec:
74 * "The output of the vertex shader and the input of the fragment
75 * shader form an interface. For this interface, vertex shader
76 * output variables and fragment shader input variables of the same
77 * name must match in type and qualification (other than precision
78 * and out matching to in).
80 * The table in Section 9.2.1 Linked Shaders of the GLSL ES 3.1 spec
81 * says that centroid no longer needs to match for varyings.
83 * The table in Section 9.2.1 Linked Shaders of the GLSL ES 3.2 spec
84 * says that sample need not match for varyings.
86 if (!prog
->IsES
|| prog
->data
->Version
< 310)
87 if (c
->fields
.structure
[i
].centroid
!=
88 p
->fields
.structure
[i
].centroid
)
91 if (c
->fields
.structure
[i
].sample
!=
92 p
->fields
.structure
[i
].sample
)
100 * Check if two interfaces match, according to intrastage interface matching
101 * rules. If they do, and the first interface uses an unsized array, it will
102 * be updated to reflect the array size declared in the second interface.
105 intrastage_match(ir_variable
*a
,
107 struct gl_shader_program
*prog
)
109 /* Types must match. */
110 if (a
->get_interface_type() != b
->get_interface_type()) {
111 /* Exception: if both the interface blocks are implicitly declared,
112 * don't force their types to match. They might mismatch due to the two
113 * shaders using different GLSL versions, and that's ok.
115 if ((a
->data
.how_declared
!= ir_var_declared_implicitly
||
116 b
->data
.how_declared
!= ir_var_declared_implicitly
) &&
117 (!prog
->IsES
|| prog
->data
->Version
!= 310 ||
118 interstage_member_mismatch(prog
, a
->get_interface_type(),
119 b
->get_interface_type())))
123 /* Presence/absence of interface names must match. */
124 if (a
->is_interface_instance() != b
->is_interface_instance())
127 /* For uniforms, instance names need not match. For shader ins/outs,
128 * it's not clear from the spec whether they need to match, but
129 * Mesa's implementation relies on them matching.
131 if (a
->is_interface_instance() && b
->data
.mode
!= ir_var_uniform
&&
132 b
->data
.mode
!= ir_var_shader_storage
&&
133 strcmp(a
->name
, b
->name
) != 0) {
137 /* If a block is an array then it must match across the shader.
138 * Unsized arrays are also processed and matched agaist sized arrays.
140 if (b
->type
!= a
->type
&&
141 (b
->is_interface_instance() || a
->is_interface_instance()) &&
142 !validate_intrastage_arrays(prog
, b
, a
))
149 * Check if two interfaces match, according to interstage (in/out) interface
152 * If \c extra_array_level is true, the consumer interface is required to be
153 * an array and the producer interface is required to be a non-array.
154 * This is used for tessellation control and geometry shader consumers.
157 interstage_match(struct gl_shader_program
*prog
, ir_variable
*producer
,
158 ir_variable
*consumer
, bool extra_array_level
)
160 /* Types must match. */
161 if (consumer
->get_interface_type() != producer
->get_interface_type()) {
162 /* Exception: if both the interface blocks are implicitly declared,
163 * don't force their types to match. They might mismatch due to the two
164 * shaders using different GLSL versions, and that's ok.
166 * Also we store some member information such as interpolation in
167 * glsl_type that doesn't always have to match across shader stages.
168 * Therefore we make a pass over the members glsl_struct_field to make
169 * sure we don't reject shaders where fields don't need to match.
171 if ((consumer
->data
.how_declared
!= ir_var_declared_implicitly
||
172 producer
->data
.how_declared
!= ir_var_declared_implicitly
) &&
173 interstage_member_mismatch(prog
, consumer
->get_interface_type(),
174 producer
->get_interface_type()))
178 /* Ignore outermost array if geom shader */
179 const glsl_type
*consumer_instance_type
;
180 if (extra_array_level
) {
181 consumer_instance_type
= consumer
->type
->fields
.array
;
183 consumer_instance_type
= consumer
->type
;
186 /* If a block is an array then it must match across shaders.
187 * Since unsized arrays have been ruled out, we can check this by just
188 * making sure the types are equal.
190 if ((consumer
->is_interface_instance() &&
191 consumer_instance_type
->is_array()) ||
192 (producer
->is_interface_instance() &&
193 producer
->type
->is_array())) {
194 if (consumer_instance_type
!= producer
->type
)
203 * This class keeps track of a mapping from an interface block name to the
204 * necessary information about that interface block to determine whether to
205 * generate a link error.
207 * Note: this class is expected to be short lived, so it doesn't make copies
208 * of the strings it references; it simply borrows the pointers from the
211 class interface_block_definitions
214 interface_block_definitions()
215 : mem_ctx(ralloc_context(NULL
)),
216 ht(_mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
217 _mesa_key_string_equal
))
221 ~interface_block_definitions()
223 ralloc_free(mem_ctx
);
224 _mesa_hash_table_destroy(ht
, NULL
);
228 * Lookup the interface definition. Return NULL if none is found.
230 ir_variable
*lookup(ir_variable
*var
)
232 if (var
->data
.explicit_location
&&
233 var
->data
.location
>= VARYING_SLOT_VAR0
) {
234 char location_str
[11];
235 snprintf(location_str
, 11, "%d", var
->data
.location
);
237 const struct hash_entry
*entry
=
238 _mesa_hash_table_search(ht
, location_str
);
239 return entry
? (ir_variable
*) entry
->data
: NULL
;
241 const struct hash_entry
*entry
=
242 _mesa_hash_table_search(ht
,
243 var
->get_interface_type()->without_array()->name
);
244 return entry
? (ir_variable
*) entry
->data
: NULL
;
249 * Add a new interface definition.
251 void store(ir_variable
*var
)
253 if (var
->data
.explicit_location
&&
254 var
->data
.location
>= VARYING_SLOT_VAR0
) {
255 /* If explicit location is given then lookup the variable by location.
256 * We turn the location into a string and use this as the hash key
257 * rather than the name. Note: We allocate enough space for a 32-bit
258 * unsigned location value which is overkill but future proof.
260 char location_str
[11];
261 snprintf(location_str
, 11, "%d", var
->data
.location
);
262 _mesa_hash_table_insert(ht
, ralloc_strdup(mem_ctx
, location_str
), var
);
264 _mesa_hash_table_insert(ht
,
265 var
->get_interface_type()->without_array()->name
, var
);
271 * Ralloc context for data structures allocated by this class.
276 * Hash table mapping interface block name to an \c
283 }; /* anonymous namespace */
287 validate_intrastage_interface_blocks(struct gl_shader_program
*prog
,
288 const gl_shader
**shader_list
,
289 unsigned num_shaders
)
291 interface_block_definitions in_interfaces
;
292 interface_block_definitions out_interfaces
;
293 interface_block_definitions uniform_interfaces
;
294 interface_block_definitions buffer_interfaces
;
296 for (unsigned int i
= 0; i
< num_shaders
; i
++) {
297 if (shader_list
[i
] == NULL
)
300 foreach_in_list(ir_instruction
, node
, shader_list
[i
]->ir
) {
301 ir_variable
*var
= node
->as_variable();
305 const glsl_type
*iface_type
= var
->get_interface_type();
307 if (iface_type
== NULL
)
310 interface_block_definitions
*definitions
;
311 switch (var
->data
.mode
) {
312 case ir_var_shader_in
:
313 definitions
= &in_interfaces
;
315 case ir_var_shader_out
:
316 definitions
= &out_interfaces
;
319 definitions
= &uniform_interfaces
;
321 case ir_var_shader_storage
:
322 definitions
= &buffer_interfaces
;
325 /* Only in, out, and uniform interfaces are legal, so we should
328 assert(!"illegal interface type");
332 ir_variable
*prev_def
= definitions
->lookup(var
);
333 if (prev_def
== NULL
) {
334 /* This is the first time we've seen the interface, so save
335 * it into the appropriate data structure.
337 definitions
->store(var
);
338 } else if (!intrastage_match(prev_def
, var
, prog
)) {
339 linker_error(prog
, "definitions of interface block `%s' do not"
340 " match\n", iface_type
->name
);
348 is_builtin_gl_in_block(ir_variable
*var
, int consumer_stage
)
350 return !strcmp(var
->name
, "gl_in") &&
351 (consumer_stage
== MESA_SHADER_TESS_CTRL
||
352 consumer_stage
== MESA_SHADER_TESS_EVAL
||
353 consumer_stage
== MESA_SHADER_GEOMETRY
);
357 validate_interstage_inout_blocks(struct gl_shader_program
*prog
,
358 const gl_linked_shader
*producer
,
359 const gl_linked_shader
*consumer
)
361 interface_block_definitions definitions
;
362 /* VS -> GS, VS -> TCS, VS -> TES, TES -> GS */
363 const bool extra_array_level
= (producer
->Stage
== MESA_SHADER_VERTEX
&&
364 consumer
->Stage
!= MESA_SHADER_FRAGMENT
) ||
365 consumer
->Stage
== MESA_SHADER_GEOMETRY
;
367 /* Check that block re-declarations of gl_PerVertex are compatible
368 * across shaders: From OpenGL Shading Language 4.5, section
369 * "7.1 Built-In Language Variables", page 130 of the PDF:
371 * "If multiple shaders using members of a built-in block belonging
372 * to the same interface are linked together in the same program,
373 * they must all redeclare the built-in block in the same way, as
374 * described in section 4.3.9 “Interface Blocks” for interface-block
375 * matching, or a link-time error will result."
377 * This is done explicitly outside of iterating the member variable
378 * declarations because it is possible that the variables are not used and
379 * so they would have been optimised out.
381 const glsl_type
*consumer_iface
=
382 consumer
->symbols
->get_interface("gl_PerVertex",
385 const glsl_type
*producer_iface
=
386 producer
->symbols
->get_interface("gl_PerVertex",
389 if (producer_iface
&& consumer_iface
&&
390 interstage_member_mismatch(prog
, consumer_iface
, producer_iface
)) {
391 linker_error(prog
, "Incompatible or missing gl_PerVertex re-declaration "
392 "in consecutive shaders");
396 /* Add output interfaces from the producer to the symbol table. */
397 foreach_in_list(ir_instruction
, node
, producer
->ir
) {
398 ir_variable
*var
= node
->as_variable();
399 if (!var
|| !var
->get_interface_type() || var
->data
.mode
!= ir_var_shader_out
)
402 definitions
.store(var
);
405 /* Verify that the consumer's input interfaces match. */
406 foreach_in_list(ir_instruction
, node
, consumer
->ir
) {
407 ir_variable
*var
= node
->as_variable();
408 if (!var
|| !var
->get_interface_type() || var
->data
.mode
!= ir_var_shader_in
)
411 ir_variable
*producer_def
= definitions
.lookup(var
);
413 /* The producer doesn't generate this input: fail to link. Skip built-in
414 * 'gl_in[]' since that may not be present if the producer does not
415 * write to any of the pre-defined outputs (e.g. if the vertex shader
416 * does not write to gl_Position, etc), which is allowed and results in
417 * undefined behavior.
419 if (producer_def
== NULL
&&
420 !is_builtin_gl_in_block(var
, consumer
->Stage
)) {
421 linker_error(prog
, "Input block `%s' is not an output of "
422 "the previous stage\n", var
->get_interface_type()->name
);
427 !interstage_match(prog
, producer_def
, var
, extra_array_level
)) {
428 linker_error(prog
, "definitions of interface block `%s' do not "
429 "match\n", var
->get_interface_type()->name
);
437 validate_interstage_uniform_blocks(struct gl_shader_program
*prog
,
438 gl_linked_shader
**stages
)
440 interface_block_definitions definitions
;
442 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
443 if (stages
[i
] == NULL
)
446 const gl_linked_shader
*stage
= stages
[i
];
447 foreach_in_list(ir_instruction
, node
, stage
->ir
) {
448 ir_variable
*var
= node
->as_variable();
449 if (!var
|| !var
->get_interface_type() ||
450 (var
->data
.mode
!= ir_var_uniform
&&
451 var
->data
.mode
!= ir_var_shader_storage
))
454 ir_variable
*old_def
= definitions
.lookup(var
);
455 if (old_def
== NULL
) {
456 definitions
.store(var
);
458 /* Interstage uniform matching rules are the same as intrastage
459 * uniform matchin rules (for uniforms, it is as though all
460 * shaders are in the same shader stage).
462 if (!intrastage_match(old_def
, var
, prog
)) {
463 linker_error(prog
, "definitions of uniform block `%s' do not "
464 "match\n", var
->get_interface_type()->name
);