glsl: move intrastage_match() after interstage_member_mismatch()
[mesa.git] / src / compiler / glsl / link_interface_blocks.cpp
1 /*
2 * Copyright © 2013 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 /**
25 * \file link_interface_blocks.cpp
26 * Linker support for GLSL's interface blocks.
27 */
28
29 #include "ir.h"
30 #include "glsl_symbol_table.h"
31 #include "linker.h"
32 #include "main/macros.h"
33 #include "util/hash_table.h"
34
35
36 namespace {
37
38 /**
39 * Return true if interface members mismatch and its not allowed by GLSL.
40 */
41 static bool
42 interstage_member_mismatch(struct gl_shader_program *prog,
43 const glsl_type *c, const glsl_type *p) {
44
45 if (c->length != p->length)
46 return true;
47
48 for (unsigned i = 0; i < c->length; i++) {
49 if (c->fields.structure[i].type != p->fields.structure[i].type)
50 return true;
51 if (strcmp(c->fields.structure[i].name,
52 p->fields.structure[i].name) != 0)
53 return true;
54 if (c->fields.structure[i].location !=
55 p->fields.structure[i].location)
56 return true;
57 if (c->fields.structure[i].patch !=
58 p->fields.structure[i].patch)
59 return true;
60
61 /* From Section 4.5 (Interpolation Qualifiers) of the GLSL 4.40 spec:
62 *
63 * "It is a link-time error if, within the same stage, the
64 * interpolation qualifiers of variables of the same name do not
65 * match."
66 */
67 if (prog->IsES || prog->Version < 440)
68 if (c->fields.structure[i].interpolation !=
69 p->fields.structure[i].interpolation)
70 return true;
71
72 /* From Section 4.3.4 (Input Variables) of the GLSL ES 3.0 spec:
73 *
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).
79 *
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.
82 *
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.
85 */
86 if (!prog->IsES || prog->Version < 310)
87 if (c->fields.structure[i].centroid !=
88 p->fields.structure[i].centroid)
89 return true;
90 if (!prog->IsES)
91 if (c->fields.structure[i].sample !=
92 p->fields.structure[i].sample)
93 return true;
94 }
95
96 return false;
97 }
98
99 /**
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.
103 */
104 bool
105 intrastage_match(ir_variable *a,
106 ir_variable *b,
107 struct gl_shader_program *prog)
108 {
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.
114 */
115 if (a->data.how_declared != ir_var_declared_implicitly ||
116 b->data.how_declared != ir_var_declared_implicitly)
117 return false;
118 }
119
120 /* Presence/absence of interface names must match. */
121 if (a->is_interface_instance() != b->is_interface_instance())
122 return false;
123
124 /* For uniforms, instance names need not match. For shader ins/outs,
125 * it's not clear from the spec whether they need to match, but
126 * Mesa's implementation relies on them matching.
127 */
128 if (a->is_interface_instance() && b->data.mode != ir_var_uniform &&
129 b->data.mode != ir_var_shader_storage &&
130 strcmp(a->name, b->name) != 0) {
131 return false;
132 }
133
134 /* If a block is an array then it must match across the shader.
135 * Unsized arrays are also processed and matched agaist sized arrays.
136 */
137 if (b->type != a->type &&
138 (b->is_interface_instance() || a->is_interface_instance()) &&
139 !validate_intrastage_arrays(prog, b, a))
140 return false;
141
142 return true;
143 }
144
145 /**
146 * Check if two interfaces match, according to interstage (in/out) interface
147 * matching rules.
148 *
149 * If \c extra_array_level is true, the consumer interface is required to be
150 * an array and the producer interface is required to be a non-array.
151 * This is used for tessellation control and geometry shader consumers.
152 */
153 static bool
154 interstage_match(struct gl_shader_program *prog, ir_variable *producer,
155 ir_variable *consumer, bool extra_array_level)
156 {
157 /* Types must match. */
158 if (consumer->get_interface_type() != producer->get_interface_type()) {
159 /* Exception: if both the interface blocks are implicitly declared,
160 * don't force their types to match. They might mismatch due to the two
161 * shaders using different GLSL versions, and that's ok.
162 *
163 * Also we store some member information such as interpolation in
164 * glsl_type that doesn't always have to match across shader stages.
165 * Therefore we make a pass over the members glsl_struct_field to make
166 * sure we don't reject shaders where fields don't need to match.
167 */
168 if ((consumer->data.how_declared != ir_var_declared_implicitly ||
169 producer->data.how_declared != ir_var_declared_implicitly) &&
170 interstage_member_mismatch(prog, consumer->get_interface_type(),
171 producer->get_interface_type()))
172 return false;
173 }
174
175 /* Ignore outermost array if geom shader */
176 const glsl_type *consumer_instance_type;
177 if (extra_array_level) {
178 consumer_instance_type = consumer->type->fields.array;
179 } else {
180 consumer_instance_type = consumer->type;
181 }
182
183 /* If a block is an array then it must match across shaders.
184 * Since unsized arrays have been ruled out, we can check this by just
185 * making sure the types are equal.
186 */
187 if ((consumer->is_interface_instance() &&
188 consumer_instance_type->is_array()) ||
189 (producer->is_interface_instance() &&
190 producer->type->is_array())) {
191 if (consumer_instance_type != producer->type)
192 return false;
193 }
194
195 return true;
196 }
197
198
199 /**
200 * This class keeps track of a mapping from an interface block name to the
201 * necessary information about that interface block to determine whether to
202 * generate a link error.
203 *
204 * Note: this class is expected to be short lived, so it doesn't make copies
205 * of the strings it references; it simply borrows the pointers from the
206 * ir_variable class.
207 */
208 class interface_block_definitions
209 {
210 public:
211 interface_block_definitions()
212 : mem_ctx(ralloc_context(NULL)),
213 ht(_mesa_hash_table_create(NULL, _mesa_key_hash_string,
214 _mesa_key_string_equal))
215 {
216 }
217
218 ~interface_block_definitions()
219 {
220 ralloc_free(mem_ctx);
221 _mesa_hash_table_destroy(ht, NULL);
222 }
223
224 /**
225 * Lookup the interface definition. Return NULL if none is found.
226 */
227 ir_variable *lookup(ir_variable *var)
228 {
229 if (var->data.explicit_location &&
230 var->data.location >= VARYING_SLOT_VAR0) {
231 char location_str[11];
232 snprintf(location_str, 11, "%d", var->data.location);
233
234 const struct hash_entry *entry =
235 _mesa_hash_table_search(ht, location_str);
236 return entry ? (ir_variable *) entry->data : NULL;
237 } else {
238 const struct hash_entry *entry =
239 _mesa_hash_table_search(ht,
240 var->get_interface_type()->without_array()->name);
241 return entry ? (ir_variable *) entry->data : NULL;
242 }
243 }
244
245 /**
246 * Add a new interface definition.
247 */
248 void store(ir_variable *var)
249 {
250 if (var->data.explicit_location &&
251 var->data.location >= VARYING_SLOT_VAR0) {
252 /* If explicit location is given then lookup the variable by location.
253 * We turn the location into a string and use this as the hash key
254 * rather than the name. Note: We allocate enough space for a 32-bit
255 * unsigned location value which is overkill but future proof.
256 */
257 char location_str[11];
258 snprintf(location_str, 11, "%d", var->data.location);
259 _mesa_hash_table_insert(ht, ralloc_strdup(mem_ctx, location_str), var);
260 } else {
261 _mesa_hash_table_insert(ht,
262 var->get_interface_type()->without_array()->name, var);
263 }
264 }
265
266 private:
267 /**
268 * Ralloc context for data structures allocated by this class.
269 */
270 void *mem_ctx;
271
272 /**
273 * Hash table mapping interface block name to an \c
274 * ir_variable.
275 */
276 hash_table *ht;
277 };
278
279
280 }; /* anonymous namespace */
281
282
283 void
284 validate_intrastage_interface_blocks(struct gl_shader_program *prog,
285 const gl_shader **shader_list,
286 unsigned num_shaders)
287 {
288 interface_block_definitions in_interfaces;
289 interface_block_definitions out_interfaces;
290 interface_block_definitions uniform_interfaces;
291 interface_block_definitions buffer_interfaces;
292
293 for (unsigned int i = 0; i < num_shaders; i++) {
294 if (shader_list[i] == NULL)
295 continue;
296
297 foreach_in_list(ir_instruction, node, shader_list[i]->ir) {
298 ir_variable *var = node->as_variable();
299 if (!var)
300 continue;
301
302 const glsl_type *iface_type = var->get_interface_type();
303
304 if (iface_type == NULL)
305 continue;
306
307 interface_block_definitions *definitions;
308 switch (var->data.mode) {
309 case ir_var_shader_in:
310 definitions = &in_interfaces;
311 break;
312 case ir_var_shader_out:
313 definitions = &out_interfaces;
314 break;
315 case ir_var_uniform:
316 definitions = &uniform_interfaces;
317 break;
318 case ir_var_shader_storage:
319 definitions = &buffer_interfaces;
320 break;
321 default:
322 /* Only in, out, and uniform interfaces are legal, so we should
323 * never get here.
324 */
325 assert(!"illegal interface type");
326 continue;
327 }
328
329 ir_variable *prev_def = definitions->lookup(var);
330 if (prev_def == NULL) {
331 /* This is the first time we've seen the interface, so save
332 * it into the appropriate data structure.
333 */
334 definitions->store(var);
335 } else if (!intrastage_match(prev_def, var, prog)) {
336 linker_error(prog, "definitions of interface block `%s' do not"
337 " match\n", iface_type->name);
338 return;
339 }
340 }
341 }
342 }
343
344 void
345 validate_interstage_inout_blocks(struct gl_shader_program *prog,
346 const gl_linked_shader *producer,
347 const gl_linked_shader *consumer)
348 {
349 interface_block_definitions definitions;
350 /* VS -> GS, VS -> TCS, VS -> TES, TES -> GS */
351 const bool extra_array_level = (producer->Stage == MESA_SHADER_VERTEX &&
352 consumer->Stage != MESA_SHADER_FRAGMENT) ||
353 consumer->Stage == MESA_SHADER_GEOMETRY;
354
355 /* Add input interfaces from the consumer to the symbol table. */
356 foreach_in_list(ir_instruction, node, consumer->ir) {
357 ir_variable *var = node->as_variable();
358 if (!var || !var->get_interface_type() || var->data.mode != ir_var_shader_in)
359 continue;
360
361 definitions.store(var);
362 }
363
364 /* Verify that the producer's output interfaces match. */
365 foreach_in_list(ir_instruction, node, producer->ir) {
366 ir_variable *var = node->as_variable();
367 if (!var || !var->get_interface_type() || var->data.mode != ir_var_shader_out)
368 continue;
369
370 ir_variable *consumer_def = definitions.lookup(var);
371
372 /* The consumer doesn't use this output block. Ignore it. */
373 if (consumer_def == NULL)
374 continue;
375
376 if (!interstage_match(prog, var, consumer_def, extra_array_level)) {
377 linker_error(prog, "definitions of interface block `%s' do not "
378 "match\n", var->get_interface_type()->name);
379 return;
380 }
381 }
382 }
383
384
385 void
386 validate_interstage_uniform_blocks(struct gl_shader_program *prog,
387 gl_linked_shader **stages)
388 {
389 interface_block_definitions definitions;
390
391 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
392 if (stages[i] == NULL)
393 continue;
394
395 const gl_linked_shader *stage = stages[i];
396 foreach_in_list(ir_instruction, node, stage->ir) {
397 ir_variable *var = node->as_variable();
398 if (!var || !var->get_interface_type() ||
399 (var->data.mode != ir_var_uniform &&
400 var->data.mode != ir_var_shader_storage))
401 continue;
402
403 ir_variable *old_def = definitions.lookup(var);
404 if (old_def == NULL) {
405 definitions.store(var);
406 } else {
407 /* Interstage uniform matching rules are the same as intrastage
408 * uniform matchin rules (for uniforms, it is as though all
409 * shaders are in the same shader stage).
410 */
411 if (!intrastage_match(old_def, var, prog)) {
412 linker_error(prog, "definitions of uniform block `%s' do not "
413 "match\n", var->get_interface_type()->name);
414 return;
415 }
416 }
417 }
418 }
419 }