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
,
110 bool match_precision
)
112 /* Types must match. */
113 if (a
->get_interface_type() != b
->get_interface_type()) {
114 /* Exception: if both the interface blocks are implicitly declared,
115 * don't force their types to match. They might mismatch due to the two
116 * shaders using different GLSL versions, and that's ok.
118 if ((a
->data
.how_declared
!= ir_var_declared_implicitly
||
119 b
->data
.how_declared
!= ir_var_declared_implicitly
) &&
121 interstage_member_mismatch(prog
, a
->get_interface_type(),
122 b
->get_interface_type())))
126 /* Presence/absence of interface names must match. */
127 if (a
->is_interface_instance() != b
->is_interface_instance())
130 /* For uniforms, instance names need not match. For shader ins/outs,
131 * it's not clear from the spec whether they need to match, but
132 * Mesa's implementation relies on them matching.
134 if (a
->is_interface_instance() && b
->data
.mode
!= ir_var_uniform
&&
135 b
->data
.mode
!= ir_var_shader_storage
&&
136 strcmp(a
->name
, b
->name
) != 0) {
140 bool type_match
= (match_precision
?
142 a
->type
->compare_no_precision(b
->type
));
144 /* If a block is an array then it must match across the shader.
145 * Unsized arrays are also processed and matched agaist sized arrays.
147 if (!type_match
&& (b
->type
->is_array() || a
->type
->is_array()) &&
148 (b
->is_interface_instance() || a
->is_interface_instance()) &&
149 !validate_intrastage_arrays(prog
, b
, a
, match_precision
))
156 * Check if two interfaces match, according to interstage (in/out) interface
159 * If \c extra_array_level is true, the consumer interface is required to be
160 * an array and the producer interface is required to be a non-array.
161 * This is used for tessellation control and geometry shader consumers.
164 interstage_match(struct gl_shader_program
*prog
, ir_variable
*producer
,
165 ir_variable
*consumer
, bool extra_array_level
)
167 /* Types must match. */
168 if (consumer
->get_interface_type() != producer
->get_interface_type()) {
169 /* Exception: if both the interface blocks are implicitly declared,
170 * don't force their types to match. They might mismatch due to the two
171 * shaders using different GLSL versions, and that's ok.
173 * Also we store some member information such as interpolation in
174 * glsl_type that doesn't always have to match across shader stages.
175 * Therefore we make a pass over the members glsl_struct_field to make
176 * sure we don't reject shaders where fields don't need to match.
178 if ((consumer
->data
.how_declared
!= ir_var_declared_implicitly
||
179 producer
->data
.how_declared
!= ir_var_declared_implicitly
) &&
180 interstage_member_mismatch(prog
, consumer
->get_interface_type(),
181 producer
->get_interface_type()))
185 /* Ignore outermost array if geom shader */
186 const glsl_type
*consumer_instance_type
;
187 if (extra_array_level
) {
188 consumer_instance_type
= consumer
->type
->fields
.array
;
190 consumer_instance_type
= consumer
->type
;
193 /* If a block is an array then it must match across shaders.
194 * Since unsized arrays have been ruled out, we can check this by just
195 * making sure the types are equal.
197 if ((consumer
->is_interface_instance() &&
198 consumer_instance_type
->is_array()) ||
199 (producer
->is_interface_instance() &&
200 producer
->type
->is_array())) {
201 if (consumer_instance_type
!= producer
->type
)
210 * This class keeps track of a mapping from an interface block name to the
211 * necessary information about that interface block to determine whether to
212 * generate a link error.
214 * Note: this class is expected to be short lived, so it doesn't make copies
215 * of the strings it references; it simply borrows the pointers from the
218 class interface_block_definitions
221 interface_block_definitions()
222 : mem_ctx(ralloc_context(NULL
)),
223 ht(_mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
224 _mesa_key_string_equal
))
228 ~interface_block_definitions()
230 ralloc_free(mem_ctx
);
231 _mesa_hash_table_destroy(ht
, NULL
);
235 * Lookup the interface definition. Return NULL if none is found.
237 ir_variable
*lookup(ir_variable
*var
)
239 if (var
->data
.explicit_location
&&
240 var
->data
.location
>= VARYING_SLOT_VAR0
) {
241 char location_str
[11];
242 util_snprintf(location_str
, 11, "%d", var
->data
.location
);
244 const struct hash_entry
*entry
=
245 _mesa_hash_table_search(ht
, location_str
);
246 return entry
? (ir_variable
*) entry
->data
: NULL
;
248 const struct hash_entry
*entry
=
249 _mesa_hash_table_search(ht
,
250 var
->get_interface_type()->without_array()->name
);
251 return entry
? (ir_variable
*) entry
->data
: NULL
;
256 * Add a new interface definition.
258 void store(ir_variable
*var
)
260 if (var
->data
.explicit_location
&&
261 var
->data
.location
>= VARYING_SLOT_VAR0
) {
262 /* If explicit location is given then lookup the variable by location.
263 * We turn the location into a string and use this as the hash key
264 * rather than the name. Note: We allocate enough space for a 32-bit
265 * unsigned location value which is overkill but future proof.
267 char location_str
[11];
268 util_snprintf(location_str
, 11, "%d", var
->data
.location
);
269 _mesa_hash_table_insert(ht
, ralloc_strdup(mem_ctx
, location_str
), var
);
271 _mesa_hash_table_insert(ht
,
272 var
->get_interface_type()->without_array()->name
, var
);
278 * Ralloc context for data structures allocated by this class.
283 * Hash table mapping interface block name to an \c
290 }; /* anonymous namespace */
294 validate_intrastage_interface_blocks(struct gl_shader_program
*prog
,
295 const gl_shader
**shader_list
,
296 unsigned num_shaders
)
298 interface_block_definitions in_interfaces
;
299 interface_block_definitions out_interfaces
;
300 interface_block_definitions uniform_interfaces
;
301 interface_block_definitions buffer_interfaces
;
303 for (unsigned int i
= 0; i
< num_shaders
; i
++) {
304 if (shader_list
[i
] == NULL
)
307 foreach_in_list(ir_instruction
, node
, shader_list
[i
]->ir
) {
308 ir_variable
*var
= node
->as_variable();
312 const glsl_type
*iface_type
= var
->get_interface_type();
314 if (iface_type
== NULL
)
317 interface_block_definitions
*definitions
;
318 switch (var
->data
.mode
) {
319 case ir_var_shader_in
:
320 definitions
= &in_interfaces
;
322 case ir_var_shader_out
:
323 definitions
= &out_interfaces
;
326 definitions
= &uniform_interfaces
;
328 case ir_var_shader_storage
:
329 definitions
= &buffer_interfaces
;
332 /* Only in, out, and uniform interfaces are legal, so we should
335 assert(!"illegal interface type");
339 ir_variable
*prev_def
= definitions
->lookup(var
);
340 if (prev_def
== NULL
) {
341 /* This is the first time we've seen the interface, so save
342 * it into the appropriate data structure.
344 definitions
->store(var
);
345 } else if (!intrastage_match(prev_def
, var
, prog
,
346 true /* match_precision */)) {
347 linker_error(prog
, "definitions of interface block `%s' do not"
348 " match\n", iface_type
->name
);
356 is_builtin_gl_in_block(ir_variable
*var
, int consumer_stage
)
358 return !strcmp(var
->name
, "gl_in") &&
359 (consumer_stage
== MESA_SHADER_TESS_CTRL
||
360 consumer_stage
== MESA_SHADER_TESS_EVAL
||
361 consumer_stage
== MESA_SHADER_GEOMETRY
);
365 validate_interstage_inout_blocks(struct gl_shader_program
*prog
,
366 const gl_linked_shader
*producer
,
367 const gl_linked_shader
*consumer
)
369 interface_block_definitions definitions
;
370 /* VS -> GS, VS -> TCS, VS -> TES, TES -> GS */
371 const bool extra_array_level
= (producer
->Stage
== MESA_SHADER_VERTEX
&&
372 consumer
->Stage
!= MESA_SHADER_FRAGMENT
) ||
373 consumer
->Stage
== MESA_SHADER_GEOMETRY
;
375 /* Check that block re-declarations of gl_PerVertex are compatible
376 * across shaders: From OpenGL Shading Language 4.5, section
377 * "7.1 Built-In Language Variables", page 130 of the PDF:
379 * "If multiple shaders using members of a built-in block belonging
380 * to the same interface are linked together in the same program,
381 * they must all redeclare the built-in block in the same way, as
382 * described in section 4.3.9 “Interface Blocks” for interface-block
383 * matching, or a link-time error will result."
385 * This is done explicitly outside of iterating the member variable
386 * declarations because it is possible that the variables are not used and
387 * so they would have been optimised out.
389 const glsl_type
*consumer_iface
=
390 consumer
->symbols
->get_interface("gl_PerVertex",
393 const glsl_type
*producer_iface
=
394 producer
->symbols
->get_interface("gl_PerVertex",
397 if (producer_iface
&& consumer_iface
&&
398 interstage_member_mismatch(prog
, consumer_iface
, producer_iface
)) {
399 linker_error(prog
, "Incompatible or missing gl_PerVertex re-declaration "
400 "in consecutive shaders");
404 /* Add output interfaces from the producer to the symbol table. */
405 foreach_in_list(ir_instruction
, node
, producer
->ir
) {
406 ir_variable
*var
= node
->as_variable();
407 if (!var
|| !var
->get_interface_type() || var
->data
.mode
!= ir_var_shader_out
)
410 definitions
.store(var
);
413 /* Verify that the consumer's input interfaces match. */
414 foreach_in_list(ir_instruction
, node
, consumer
->ir
) {
415 ir_variable
*var
= node
->as_variable();
416 if (!var
|| !var
->get_interface_type() || var
->data
.mode
!= ir_var_shader_in
)
419 ir_variable
*producer_def
= definitions
.lookup(var
);
421 /* The producer doesn't generate this input: fail to link. Skip built-in
422 * 'gl_in[]' since that may not be present if the producer does not
423 * write to any of the pre-defined outputs (e.g. if the vertex shader
424 * does not write to gl_Position, etc), which is allowed and results in
425 * undefined behavior.
427 * From Section 4.3.4 (Inputs) of the GLSL 1.50 spec:
429 * "Only the input variables that are actually read need to be written
430 * by the previous stage; it is allowed to have superfluous
431 * declarations of input variables."
433 if (producer_def
== NULL
&&
434 !is_builtin_gl_in_block(var
, consumer
->Stage
) && var
->data
.used
) {
435 linker_error(prog
, "Input block `%s' is not an output of "
436 "the previous stage\n", var
->get_interface_type()->name
);
441 !interstage_match(prog
, producer_def
, var
, extra_array_level
)) {
442 linker_error(prog
, "definitions of interface block `%s' do not "
443 "match\n", var
->get_interface_type()->name
);
451 validate_interstage_uniform_blocks(struct gl_shader_program
*prog
,
452 gl_linked_shader
**stages
)
454 interface_block_definitions definitions
;
456 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
457 if (stages
[i
] == NULL
)
460 const gl_linked_shader
*stage
= stages
[i
];
461 foreach_in_list(ir_instruction
, node
, stage
->ir
) {
462 ir_variable
*var
= node
->as_variable();
463 if (!var
|| !var
->get_interface_type() ||
464 (var
->data
.mode
!= ir_var_uniform
&&
465 var
->data
.mode
!= ir_var_shader_storage
))
468 ir_variable
*old_def
= definitions
.lookup(var
);
469 if (old_def
== NULL
) {
470 definitions
.store(var
);
472 /* Interstage uniform matching rules are the same as intrastage
473 * uniform matchin rules (for uniforms, it is as though all
474 * shaders are in the same shader stage).
476 if (!intrastage_match(old_def
, var
, prog
, false /* precision */)) {
477 linker_error(prog
, "definitions of uniform block `%s' do not "
478 "match\n", var
->get_interface_type()->name
);