glsl: reject invalid input layout qualifiers
[mesa.git] / src / compiler / glsl / ast_type.cpp
1 /*
2 * Copyright © 2010 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 #include "ast.h"
25
26 void
27 ast_type_specifier::print(void) const
28 {
29 if (structure) {
30 structure->print();
31 } else {
32 printf("%s ", type_name);
33 }
34
35 if (array_specifier) {
36 array_specifier->print();
37 }
38 }
39
40 bool
41 ast_fully_specified_type::has_qualifiers(_mesa_glsl_parse_state *state) const
42 {
43 /* 'subroutine' isnt a real qualifier. */
44 ast_type_qualifier subroutine_only;
45 subroutine_only.flags.i = 0;
46 subroutine_only.flags.q.subroutine = 1;
47 subroutine_only.flags.q.subroutine_def = 1;
48 if (state->has_explicit_uniform_location()) {
49 subroutine_only.flags.q.explicit_index = 1;
50 }
51 return (this->qualifier.flags.i & ~subroutine_only.flags.i) != 0;
52 }
53
54 bool ast_type_qualifier::has_interpolation() const
55 {
56 return this->flags.q.smooth
57 || this->flags.q.flat
58 || this->flags.q.noperspective;
59 }
60
61 bool
62 ast_type_qualifier::has_layout() const
63 {
64 return this->flags.q.origin_upper_left
65 || this->flags.q.pixel_center_integer
66 || this->flags.q.depth_any
67 || this->flags.q.depth_greater
68 || this->flags.q.depth_less
69 || this->flags.q.depth_unchanged
70 || this->flags.q.std140
71 || this->flags.q.std430
72 || this->flags.q.shared
73 || this->flags.q.column_major
74 || this->flags.q.row_major
75 || this->flags.q.packed
76 || this->flags.q.explicit_location
77 || this->flags.q.explicit_image_format
78 || this->flags.q.explicit_index
79 || this->flags.q.explicit_binding
80 || this->flags.q.explicit_offset
81 || this->flags.q.explicit_stream;
82 }
83
84 bool
85 ast_type_qualifier::has_storage() const
86 {
87 return this->flags.q.constant
88 || this->flags.q.attribute
89 || this->flags.q.varying
90 || this->flags.q.in
91 || this->flags.q.out
92 || this->flags.q.uniform
93 || this->flags.q.buffer
94 || this->flags.q.shared_storage;
95 }
96
97 bool
98 ast_type_qualifier::has_auxiliary_storage() const
99 {
100 return this->flags.q.centroid
101 || this->flags.q.sample
102 || this->flags.q.patch;
103 }
104
105 /**
106 * This function merges both duplicate identifies within a single layout and
107 * multiple layout qualifiers on a single variable declaration. The
108 * is_single_layout_merge param is used differentiate between the two.
109 */
110 bool
111 ast_type_qualifier::merge_qualifier(YYLTYPE *loc,
112 _mesa_glsl_parse_state *state,
113 const ast_type_qualifier &q,
114 bool is_single_layout_merge)
115 {
116 ast_type_qualifier ubo_mat_mask;
117 ubo_mat_mask.flags.i = 0;
118 ubo_mat_mask.flags.q.row_major = 1;
119 ubo_mat_mask.flags.q.column_major = 1;
120
121 ast_type_qualifier ubo_layout_mask;
122 ubo_layout_mask.flags.i = 0;
123 ubo_layout_mask.flags.q.std140 = 1;
124 ubo_layout_mask.flags.q.packed = 1;
125 ubo_layout_mask.flags.q.shared = 1;
126 ubo_layout_mask.flags.q.std430 = 1;
127
128 ast_type_qualifier ubo_binding_mask;
129 ubo_binding_mask.flags.i = 0;
130 ubo_binding_mask.flags.q.explicit_binding = 1;
131 ubo_binding_mask.flags.q.explicit_offset = 1;
132
133 ast_type_qualifier stream_layout_mask;
134 stream_layout_mask.flags.i = 0;
135 stream_layout_mask.flags.q.stream = 1;
136
137 /* FIXME: We should probably do interface and function param validation
138 * separately.
139 */
140 ast_type_qualifier input_layout_mask;
141 input_layout_mask.flags.i = 0;
142 input_layout_mask.flags.q.centroid = 1;
143 /* Function params can have constant */
144 input_layout_mask.flags.q.constant = 1;
145 input_layout_mask.flags.q.explicit_location = 1;
146 input_layout_mask.flags.q.flat = 1;
147 input_layout_mask.flags.q.in = 1;
148 input_layout_mask.flags.q.invariant = 1;
149 input_layout_mask.flags.q.noperspective = 1;
150 input_layout_mask.flags.q.origin_upper_left = 1;
151 /* Function params 'inout' will set this */
152 input_layout_mask.flags.q.out = 1;
153 input_layout_mask.flags.q.patch = 1;
154 input_layout_mask.flags.q.pixel_center_integer = 1;
155 input_layout_mask.flags.q.precise = 1;
156 input_layout_mask.flags.q.sample = 1;
157 input_layout_mask.flags.q.smooth = 1;
158
159 /* Uniform block layout qualifiers get to overwrite each
160 * other (rightmost having priority), while all other
161 * qualifiers currently don't allow duplicates.
162 */
163 ast_type_qualifier allowed_duplicates_mask;
164 allowed_duplicates_mask.flags.i =
165 ubo_mat_mask.flags.i |
166 ubo_layout_mask.flags.i |
167 ubo_binding_mask.flags.i;
168
169 /* Geometry shaders can have several layout qualifiers
170 * assigning different stream values.
171 */
172 if (state->stage == MESA_SHADER_GEOMETRY)
173 allowed_duplicates_mask.flags.i |=
174 stream_layout_mask.flags.i;
175
176 if (is_single_layout_merge && !state->has_enhanced_layouts() &&
177 (this->flags.i & q.flags.i & ~allowed_duplicates_mask.flags.i) != 0) {
178 _mesa_glsl_error(loc, state,
179 "duplicate layout qualifiers used");
180 return false;
181 }
182
183 if (q.flags.q.prim_type) {
184 if (this->flags.q.prim_type && this->prim_type != q.prim_type) {
185 _mesa_glsl_error(loc, state,
186 "conflicting primitive type qualifiers used");
187 return false;
188 }
189 this->prim_type = q.prim_type;
190 }
191
192 if (q.flags.q.max_vertices) {
193 if (this->max_vertices) {
194 this->max_vertices->merge_qualifier(q.max_vertices);
195 } else {
196 this->max_vertices = q.max_vertices;
197 }
198 }
199
200 if (q.flags.q.subroutine_def) {
201 if (this->flags.q.subroutine_def) {
202 _mesa_glsl_error(loc, state,
203 "conflicting subroutine qualifiers used");
204 } else {
205 this->subroutine_list = q.subroutine_list;
206 }
207 }
208
209 if (q.flags.q.invocations) {
210 if (this->invocations) {
211 this->invocations->merge_qualifier(q.invocations);
212 } else {
213 this->invocations = q.invocations;
214 }
215 }
216
217 if (state->stage == MESA_SHADER_GEOMETRY &&
218 state->has_explicit_attrib_stream()) {
219 if (!this->flags.q.explicit_stream) {
220 if (q.flags.q.stream) {
221 this->flags.q.stream = 1;
222 this->stream = q.stream;
223 } else if (!this->flags.q.stream && this->flags.q.out) {
224 /* Assign default global stream value */
225 this->flags.q.stream = 1;
226 this->stream = state->out_qualifier->stream;
227 }
228 }
229 }
230
231 if (q.flags.q.vertices) {
232 if (this->vertices) {
233 this->vertices->merge_qualifier(q.vertices);
234 } else {
235 this->vertices = q.vertices;
236 }
237 }
238
239 if (q.flags.q.vertex_spacing) {
240 if (this->flags.q.vertex_spacing && this->vertex_spacing != q.vertex_spacing) {
241 _mesa_glsl_error(loc, state,
242 "conflicting vertex spacing used");
243 return false;
244 }
245 this->vertex_spacing = q.vertex_spacing;
246 }
247
248 if (q.flags.q.ordering) {
249 if (this->flags.q.ordering && this->ordering != q.ordering) {
250 _mesa_glsl_error(loc, state,
251 "conflicting ordering used");
252 return false;
253 }
254 this->ordering = q.ordering;
255 }
256
257 if (q.flags.q.point_mode) {
258 if (this->flags.q.point_mode && this->point_mode != q.point_mode) {
259 _mesa_glsl_error(loc, state,
260 "conflicting point mode used");
261 return false;
262 }
263 this->point_mode = q.point_mode;
264 }
265
266 if ((q.flags.i & ubo_mat_mask.flags.i) != 0)
267 this->flags.i &= ~ubo_mat_mask.flags.i;
268 if ((q.flags.i & ubo_layout_mask.flags.i) != 0)
269 this->flags.i &= ~ubo_layout_mask.flags.i;
270
271 for (int i = 0; i < 3; i++) {
272 if (q.flags.q.local_size & (1 << i)) {
273 if (this->local_size[i]) {
274 this->local_size[i]->merge_qualifier(q.local_size[i]);
275 } else {
276 this->local_size[i] = q.local_size[i];
277 }
278 }
279 }
280
281 this->flags.i |= q.flags.i;
282
283 if (this->flags.q.in &&
284 (this->flags.i & ~input_layout_mask.flags.i) != 0) {
285 _mesa_glsl_error(loc, state,
286 "invalid input layout qualifier used");
287 return false;
288 }
289
290 if (q.flags.q.explicit_location)
291 this->location = q.location;
292
293 if (q.flags.q.explicit_index)
294 this->index = q.index;
295
296 if (q.flags.q.explicit_binding)
297 this->binding = q.binding;
298
299 if (q.flags.q.explicit_offset)
300 this->offset = q.offset;
301
302 if (q.precision != ast_precision_none)
303 this->precision = q.precision;
304
305 if (q.flags.q.explicit_image_format) {
306 this->image_format = q.image_format;
307 this->image_base_type = q.image_base_type;
308 }
309
310 return true;
311 }
312
313 bool
314 ast_type_qualifier::merge_out_qualifier(YYLTYPE *loc,
315 _mesa_glsl_parse_state *state,
316 const ast_type_qualifier &q,
317 ast_node* &node, bool create_node)
318 {
319 void *mem_ctx = state;
320 const bool r = this->merge_qualifier(loc, state, q, false);
321
322 if (state->stage == MESA_SHADER_GEOMETRY) {
323 if (q.flags.q.prim_type) {
324 /* Make sure this is a valid output primitive type. */
325 switch (q.prim_type) {
326 case GL_POINTS:
327 case GL_LINE_STRIP:
328 case GL_TRIANGLE_STRIP:
329 break;
330 default:
331 _mesa_glsl_error(loc, state, "invalid geometry shader output "
332 "primitive type");
333 break;
334 }
335 }
336
337 /* Allow future assigments of global out's stream id value */
338 this->flags.q.explicit_stream = 0;
339 } else if (state->stage == MESA_SHADER_TESS_CTRL) {
340 if (create_node) {
341 node = new(mem_ctx) ast_tcs_output_layout(*loc);
342 }
343 } else {
344 _mesa_glsl_error(loc, state, "out layout qualifiers only valid in "
345 "tessellation control or geometry shaders");
346 }
347
348 return r;
349 }
350
351 bool
352 ast_type_qualifier::merge_in_qualifier(YYLTYPE *loc,
353 _mesa_glsl_parse_state *state,
354 const ast_type_qualifier &q,
355 ast_node* &node, bool create_node)
356 {
357 void *mem_ctx = state;
358 bool create_gs_ast = false;
359 bool create_cs_ast = false;
360 ast_type_qualifier valid_in_mask;
361 valid_in_mask.flags.i = 0;
362
363 switch (state->stage) {
364 case MESA_SHADER_TESS_EVAL:
365 if (q.flags.q.prim_type) {
366 /* Make sure this is a valid input primitive type. */
367 switch (q.prim_type) {
368 case GL_TRIANGLES:
369 case GL_QUADS:
370 case GL_ISOLINES:
371 break;
372 default:
373 _mesa_glsl_error(loc, state,
374 "invalid tessellation evaluation "
375 "shader input primitive type");
376 break;
377 }
378 }
379
380 valid_in_mask.flags.q.prim_type = 1;
381 valid_in_mask.flags.q.vertex_spacing = 1;
382 valid_in_mask.flags.q.ordering = 1;
383 valid_in_mask.flags.q.point_mode = 1;
384 break;
385 case MESA_SHADER_GEOMETRY:
386 if (q.flags.q.prim_type) {
387 /* Make sure this is a valid input primitive type. */
388 switch (q.prim_type) {
389 case GL_POINTS:
390 case GL_LINES:
391 case GL_LINES_ADJACENCY:
392 case GL_TRIANGLES:
393 case GL_TRIANGLES_ADJACENCY:
394 break;
395 default:
396 _mesa_glsl_error(loc, state,
397 "invalid geometry shader input primitive type");
398 break;
399 }
400 }
401
402 create_gs_ast |=
403 q.flags.q.prim_type &&
404 !state->in_qualifier->flags.q.prim_type;
405
406 valid_in_mask.flags.q.prim_type = 1;
407 valid_in_mask.flags.q.invocations = 1;
408 break;
409 case MESA_SHADER_FRAGMENT:
410 valid_in_mask.flags.q.early_fragment_tests = 1;
411 break;
412 case MESA_SHADER_COMPUTE:
413 create_cs_ast |=
414 q.flags.q.local_size != 0 &&
415 state->in_qualifier->flags.q.local_size == 0;
416
417 valid_in_mask.flags.q.local_size = 7;
418 break;
419 default:
420 _mesa_glsl_error(loc, state,
421 "input layout qualifiers only valid in "
422 "geometry, fragment and compute shaders");
423 break;
424 }
425
426 /* Generate an error when invalid input layout qualifiers are used. */
427 if ((q.flags.i & ~valid_in_mask.flags.i) != 0) {
428 _mesa_glsl_error(loc, state,
429 "invalid input layout qualifiers used");
430 return false;
431 }
432
433 /* Input layout qualifiers can be specified multiple
434 * times in separate declarations, as long as they match.
435 */
436 if (this->flags.q.prim_type) {
437 if (q.flags.q.prim_type &&
438 this->prim_type != q.prim_type) {
439 _mesa_glsl_error(loc, state,
440 "conflicting input primitive %s specified",
441 state->stage == MESA_SHADER_GEOMETRY ?
442 "type" : "mode");
443 }
444 } else if (q.flags.q.prim_type) {
445 state->in_qualifier->flags.q.prim_type = 1;
446 state->in_qualifier->prim_type = q.prim_type;
447 }
448
449 if (q.flags.q.invocations) {
450 this->flags.q.invocations = 1;
451 if (this->invocations) {
452 this->invocations->merge_qualifier(q.invocations);
453 } else {
454 this->invocations = q.invocations;
455 }
456 }
457
458 if (q.flags.q.early_fragment_tests) {
459 state->fs_early_fragment_tests = true;
460 }
461
462 if (this->flags.q.vertex_spacing) {
463 if (q.flags.q.vertex_spacing &&
464 this->vertex_spacing != q.vertex_spacing) {
465 _mesa_glsl_error(loc, state,
466 "conflicting vertex spacing specified");
467 }
468 } else if (q.flags.q.vertex_spacing) {
469 this->flags.q.vertex_spacing = 1;
470 this->vertex_spacing = q.vertex_spacing;
471 }
472
473 if (this->flags.q.ordering) {
474 if (q.flags.q.ordering &&
475 this->ordering != q.ordering) {
476 _mesa_glsl_error(loc, state,
477 "conflicting ordering specified");
478 }
479 } else if (q.flags.q.ordering) {
480 this->flags.q.ordering = 1;
481 this->ordering = q.ordering;
482 }
483
484 if (this->flags.q.point_mode) {
485 if (q.flags.q.point_mode &&
486 this->point_mode != q.point_mode) {
487 _mesa_glsl_error(loc, state,
488 "conflicting point mode specified");
489 }
490 } else if (q.flags.q.point_mode) {
491 this->flags.q.point_mode = 1;
492 this->point_mode = q.point_mode;
493 }
494
495 if (create_node) {
496 if (create_gs_ast) {
497 node = new(mem_ctx) ast_gs_input_layout(*loc, q.prim_type);
498 } else if (create_cs_ast) {
499 node = new(mem_ctx) ast_cs_input_layout(*loc, q.local_size);
500 }
501 }
502
503 return true;
504 }
505
506 bool
507 ast_layout_expression::process_qualifier_constant(struct _mesa_glsl_parse_state *state,
508 const char *qual_indentifier,
509 unsigned *value,
510 bool can_be_zero)
511 {
512 int min_value = 0;
513 bool first_pass = true;
514 *value = 0;
515
516 if (!can_be_zero)
517 min_value = 1;
518
519 for (exec_node *node = layout_const_expressions.head;
520 !node->is_tail_sentinel(); node = node->next) {
521
522 exec_list dummy_instructions;
523 ast_node *const_expression = exec_node_data(ast_node, node, link);
524
525 ir_rvalue *const ir = const_expression->hir(&dummy_instructions, state);
526
527 ir_constant *const const_int = ir->constant_expression_value();
528 if (const_int == NULL || !const_int->type->is_integer()) {
529 YYLTYPE loc = const_expression->get_location();
530 _mesa_glsl_error(&loc, state, "%s must be an integral constant "
531 "expression", qual_indentifier);
532 return false;
533 }
534
535 if (const_int->value.i[0] < min_value) {
536 YYLTYPE loc = const_expression->get_location();
537 _mesa_glsl_error(&loc, state, "%s layout qualifier is invalid "
538 "(%d < %d)", qual_indentifier,
539 const_int->value.i[0], min_value);
540 return false;
541 }
542
543 if (!first_pass && *value != const_int->value.u[0]) {
544 YYLTYPE loc = const_expression->get_location();
545 _mesa_glsl_error(&loc, state, "%s layout qualifier does not "
546 "match previous declaration (%d vs %d)",
547 qual_indentifier, *value, const_int->value.i[0]);
548 return false;
549 } else {
550 first_pass = false;
551 *value = const_int->value.u[0];
552 }
553
554 /* If the location is const (and we've verified that
555 * it is) then no instructions should have been emitted
556 * when we converted it to HIR. If they were emitted,
557 * then either the location isn't const after all, or
558 * we are emitting unnecessary instructions.
559 */
560 assert(dummy_instructions.is_empty());
561 }
562
563 return true;
564 }