glsl: simplified error checking for duplicated 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_align
77 || this->flags.q.explicit_component
78 || this->flags.q.explicit_location
79 || this->flags.q.explicit_image_format
80 || this->flags.q.explicit_index
81 || this->flags.q.explicit_binding
82 || this->flags.q.explicit_offset
83 || this->flags.q.explicit_stream
84 || this->flags.q.explicit_xfb_buffer
85 || this->flags.q.explicit_xfb_offset
86 || this->flags.q.explicit_xfb_stride;
87 }
88
89 bool
90 ast_type_qualifier::has_storage() const
91 {
92 return this->flags.q.constant
93 || this->flags.q.attribute
94 || this->flags.q.varying
95 || this->flags.q.in
96 || this->flags.q.out
97 || this->flags.q.uniform
98 || this->flags.q.buffer
99 || this->flags.q.shared_storage;
100 }
101
102 bool
103 ast_type_qualifier::has_auxiliary_storage() const
104 {
105 return this->flags.q.centroid
106 || this->flags.q.sample
107 || this->flags.q.patch;
108 }
109
110 bool ast_type_qualifier::has_memory() const
111 {
112 return this->flags.q.coherent
113 || this->flags.q._volatile
114 || this->flags.q.restrict_flag
115 || this->flags.q.read_only
116 || this->flags.q.write_only;
117 }
118
119 static bool
120 validate_prim_type(YYLTYPE *loc,
121 _mesa_glsl_parse_state *state,
122 const ast_type_qualifier &qualifier,
123 const ast_type_qualifier &new_qualifier)
124 {
125 /* Input layout qualifiers can be specified multiple
126 * times in separate declarations, as long as they match.
127 */
128 if (qualifier.flags.q.prim_type && new_qualifier.flags.q.prim_type
129 && qualifier.prim_type != new_qualifier.prim_type) {
130 _mesa_glsl_error(loc, state,
131 "conflicting input primitive %s specified",
132 state->stage == MESA_SHADER_GEOMETRY ?
133 "type" : "mode");
134 return false;
135 }
136
137 return true;
138 }
139
140 static bool
141 validate_vertex_spacing(YYLTYPE *loc,
142 _mesa_glsl_parse_state *state,
143 const ast_type_qualifier &qualifier,
144 const ast_type_qualifier &new_qualifier)
145 {
146 if (qualifier.flags.q.vertex_spacing && new_qualifier.flags.q.vertex_spacing
147 && qualifier.vertex_spacing != new_qualifier.vertex_spacing) {
148 _mesa_glsl_error(loc, state,
149 "conflicting vertex spacing specified");
150 return false;
151 }
152
153 return true;
154 }
155
156 static bool
157 validate_ordering(YYLTYPE *loc,
158 _mesa_glsl_parse_state *state,
159 const ast_type_qualifier &qualifier,
160 const ast_type_qualifier &new_qualifier)
161 {
162 if (qualifier.flags.q.ordering && new_qualifier.flags.q.ordering
163 && qualifier.ordering != new_qualifier.ordering) {
164 _mesa_glsl_error(loc, state,
165 "conflicting ordering specified");
166 return false;
167 }
168
169 return true;
170 }
171
172 static bool
173 validate_point_mode(YYLTYPE *loc,
174 _mesa_glsl_parse_state *state,
175 const ast_type_qualifier &qualifier,
176 const ast_type_qualifier &new_qualifier)
177 {
178 /* Point mode can only be true if the flag is set. */
179 assert (!qualifier.flags.q.point_mode || !new_qualifier.flags.q.point_mode
180 || (qualifier.point_mode && new_qualifier.point_mode));
181
182 return true;
183 }
184
185 /**
186 * This function merges duplicate layout identifiers.
187 *
188 * It deals with duplicates within a single layout qualifier, among multiple
189 * layout qualifiers on a single declaration and on several declarations for
190 * the same variable.
191 *
192 * The is_single_layout_merge and is_multiple_layouts_merge parameters are
193 * used to differentiate among them.
194 */
195 bool
196 ast_type_qualifier::merge_qualifier(YYLTYPE *loc,
197 _mesa_glsl_parse_state *state,
198 const ast_type_qualifier &q,
199 bool is_single_layout_merge,
200 bool is_multiple_layouts_merge)
201 {
202 bool r = true;
203 ast_type_qualifier ubo_mat_mask;
204 ubo_mat_mask.flags.i = 0;
205 ubo_mat_mask.flags.q.row_major = 1;
206 ubo_mat_mask.flags.q.column_major = 1;
207
208 ast_type_qualifier ubo_layout_mask;
209 ubo_layout_mask.flags.i = 0;
210 ubo_layout_mask.flags.q.std140 = 1;
211 ubo_layout_mask.flags.q.packed = 1;
212 ubo_layout_mask.flags.q.shared = 1;
213 ubo_layout_mask.flags.q.std430 = 1;
214
215 ast_type_qualifier ubo_binding_mask;
216 ubo_binding_mask.flags.i = 0;
217 ubo_binding_mask.flags.q.explicit_binding = 1;
218 ubo_binding_mask.flags.q.explicit_offset = 1;
219
220 ast_type_qualifier stream_layout_mask;
221 stream_layout_mask.flags.i = 0;
222 stream_layout_mask.flags.q.stream = 1;
223
224 /* FIXME: We should probably do interface and function param validation
225 * separately.
226 */
227 ast_type_qualifier input_layout_mask;
228 input_layout_mask.flags.i = 0;
229 input_layout_mask.flags.q.centroid = 1;
230 /* Function params can have constant */
231 input_layout_mask.flags.q.constant = 1;
232 input_layout_mask.flags.q.explicit_component = 1;
233 input_layout_mask.flags.q.explicit_location = 1;
234 input_layout_mask.flags.q.flat = 1;
235 input_layout_mask.flags.q.in = 1;
236 input_layout_mask.flags.q.invariant = 1;
237 input_layout_mask.flags.q.noperspective = 1;
238 input_layout_mask.flags.q.origin_upper_left = 1;
239 /* Function params 'inout' will set this */
240 input_layout_mask.flags.q.out = 1;
241 input_layout_mask.flags.q.patch = 1;
242 input_layout_mask.flags.q.pixel_center_integer = 1;
243 input_layout_mask.flags.q.precise = 1;
244 input_layout_mask.flags.q.sample = 1;
245 input_layout_mask.flags.q.smooth = 1;
246
247 /* Uniform block layout qualifiers get to overwrite each
248 * other (rightmost having priority), while all other
249 * qualifiers currently don't allow duplicates.
250 */
251 ast_type_qualifier allowed_duplicates_mask;
252 allowed_duplicates_mask.flags.i =
253 ubo_mat_mask.flags.i |
254 ubo_layout_mask.flags.i |
255 ubo_binding_mask.flags.i;
256
257 /* Geometry shaders can have several layout qualifiers
258 * assigning different stream values.
259 */
260 if (state->stage == MESA_SHADER_GEOMETRY) {
261 allowed_duplicates_mask.flags.i |=
262 stream_layout_mask.flags.i;
263 }
264
265 if (is_single_layout_merge && !state->has_enhanced_layouts() &&
266 (this->flags.i & q.flags.i & ~allowed_duplicates_mask.flags.i) != 0) {
267 _mesa_glsl_error(loc, state, "duplicate layout qualifiers used");
268 return false;
269 }
270
271 if (is_multiple_layouts_merge && !state->has_420pack_or_es31()) {
272 _mesa_glsl_error(loc, state,
273 "duplicate layout(...) qualifiers");
274 return false;
275 }
276
277 if (q.flags.q.prim_type) {
278 r &= validate_prim_type(loc, state, *this, q);
279 this->flags.q.prim_type = 1;
280 this->prim_type = q.prim_type;
281 }
282
283 if (q.flags.q.max_vertices) {
284 if (this->flags.q.max_vertices
285 && !is_single_layout_merge && !is_multiple_layouts_merge) {
286 this->max_vertices->merge_qualifier(q.max_vertices);
287 } else {
288 this->flags.q.max_vertices = 1;
289 this->max_vertices = q.max_vertices;
290 }
291 }
292
293 if (q.flags.q.subroutine_def) {
294 if (this->flags.q.subroutine_def) {
295 _mesa_glsl_error(loc, state,
296 "conflicting subroutine qualifiers used");
297 } else {
298 this->subroutine_list = q.subroutine_list;
299 }
300 }
301
302 if (q.flags.q.invocations) {
303 if (this->flags.q.invocations
304 && !is_single_layout_merge && !is_multiple_layouts_merge) {
305 this->invocations->merge_qualifier(q.invocations);
306 } else {
307 this->flags.q.invocations = 1;
308 this->invocations = q.invocations;
309 }
310 }
311
312 if (state->stage == MESA_SHADER_GEOMETRY &&
313 state->has_explicit_attrib_stream()) {
314 if (!this->flags.q.explicit_stream) {
315 if (q.flags.q.stream) {
316 this->flags.q.stream = 1;
317 this->stream = q.stream;
318 } else if (!this->flags.q.stream && this->flags.q.out &&
319 !this->flags.q.in) {
320 /* Assign default global stream value */
321 this->flags.q.stream = 1;
322 this->stream = state->out_qualifier->stream;
323 }
324 }
325 }
326
327 if (state->has_enhanced_layouts()) {
328 if (!this->flags.q.explicit_xfb_buffer) {
329 if (q.flags.q.xfb_buffer) {
330 this->flags.q.xfb_buffer = 1;
331 this->xfb_buffer = q.xfb_buffer;
332 } else if (!this->flags.q.xfb_buffer && this->flags.q.out &&
333 !this->flags.q.in) {
334 /* Assign global xfb_buffer value */
335 this->flags.q.xfb_buffer = 1;
336 this->xfb_buffer = state->out_qualifier->xfb_buffer;
337 }
338 }
339
340 if (q.flags.q.explicit_xfb_stride)
341 this->xfb_stride = q.xfb_stride;
342
343 /* Merge all we xfb_stride qualifiers into the global out */
344 if (q.flags.q.explicit_xfb_stride || this->flags.q.xfb_stride) {
345
346 /* Set xfb_stride flag to 0 to avoid adding duplicates every time
347 * there is a merge.
348 */
349 this->flags.q.xfb_stride = 0;
350
351 unsigned buff_idx;
352 if (process_qualifier_constant(state, loc, "xfb_buffer",
353 this->xfb_buffer, &buff_idx)) {
354 if (state->out_qualifier->out_xfb_stride[buff_idx]
355 && !is_single_layout_merge && !is_multiple_layouts_merge) {
356 state->out_qualifier->out_xfb_stride[buff_idx]->merge_qualifier(
357 new(state->linalloc) ast_layout_expression(*loc, this->xfb_stride));
358 } else {
359 state->out_qualifier->out_xfb_stride[buff_idx] =
360 new(state->linalloc) ast_layout_expression(*loc, this->xfb_stride);
361 }
362 }
363 }
364 }
365
366 if (q.flags.q.vertices) {
367 if (this->flags.q.vertices
368 && !is_single_layout_merge && !is_multiple_layouts_merge) {
369 this->vertices->merge_qualifier(q.vertices);
370 } else {
371 this->flags.q.vertices = 1;
372 this->vertices = q.vertices;
373 }
374 }
375
376 if (q.flags.q.vertex_spacing) {
377 r &= validate_vertex_spacing(loc, state, *this, q);
378 this->flags.q.vertex_spacing = 1;
379 this->vertex_spacing = q.vertex_spacing;
380 }
381
382 if (q.flags.q.ordering) {
383 r &= validate_ordering(loc, state, *this, q);
384 this->flags.q.ordering = 1;
385 this->ordering = q.ordering;
386 }
387
388 if (q.flags.q.point_mode) {
389 r &= validate_point_mode(loc, state, *this, q);
390 this->flags.q.point_mode = 1;
391 this->point_mode = q.point_mode;
392 }
393
394 if (q.flags.q.early_fragment_tests)
395 this->flags.q.early_fragment_tests = true;
396
397 if ((q.flags.i & ubo_mat_mask.flags.i) != 0)
398 this->flags.i &= ~ubo_mat_mask.flags.i;
399 if ((q.flags.i & ubo_layout_mask.flags.i) != 0)
400 this->flags.i &= ~ubo_layout_mask.flags.i;
401
402 for (int i = 0; i < 3; i++) {
403 if (q.flags.q.local_size & (1 << i)) {
404 if (this->local_size[i]
405 && !is_single_layout_merge && !is_multiple_layouts_merge) {
406 this->local_size[i]->merge_qualifier(q.local_size[i]);
407 } else {
408 this->local_size[i] = q.local_size[i];
409 }
410 }
411 }
412
413 if (q.flags.q.local_size_variable)
414 this->flags.q.local_size_variable = true;
415
416 this->flags.i |= q.flags.i;
417
418 if (this->flags.q.in &&
419 (this->flags.i & ~input_layout_mask.flags.i) != 0) {
420 _mesa_glsl_error(loc, state, "invalid input layout qualifier used");
421 return false;
422 }
423
424 if (q.flags.q.explicit_align)
425 this->align = q.align;
426
427 if (q.flags.q.explicit_location)
428 this->location = q.location;
429
430 if (q.flags.q.explicit_index)
431 this->index = q.index;
432
433 if (q.flags.q.explicit_component)
434 this->component = q.component;
435
436 if (q.flags.q.explicit_binding)
437 this->binding = q.binding;
438
439 if (q.flags.q.explicit_offset || q.flags.q.explicit_xfb_offset)
440 this->offset = q.offset;
441
442 if (q.precision != ast_precision_none)
443 this->precision = q.precision;
444
445 if (q.flags.q.explicit_image_format) {
446 this->image_format = q.image_format;
447 this->image_base_type = q.image_base_type;
448 }
449
450 return r;
451 }
452
453 bool
454 ast_type_qualifier::validate_out_qualifier(YYLTYPE *loc,
455 _mesa_glsl_parse_state *state)
456 {
457 bool r = true;
458 ast_type_qualifier valid_out_mask;
459 valid_out_mask.flags.i = 0;
460
461 switch (state->stage) {
462 case MESA_SHADER_GEOMETRY:
463 if (this->flags.q.prim_type) {
464 /* Make sure this is a valid output primitive type. */
465 switch (this->prim_type) {
466 case GL_POINTS:
467 case GL_LINE_STRIP:
468 case GL_TRIANGLE_STRIP:
469 break;
470 default:
471 r = false;
472 _mesa_glsl_error(loc, state, "invalid geometry shader output "
473 "primitive type");
474 break;
475 }
476 }
477
478 valid_out_mask.flags.q.stream = 1;
479 valid_out_mask.flags.q.explicit_stream = 1;
480 valid_out_mask.flags.q.explicit_xfb_buffer = 1;
481 valid_out_mask.flags.q.xfb_buffer = 1;
482 valid_out_mask.flags.q.explicit_xfb_stride = 1;
483 valid_out_mask.flags.q.xfb_stride = 1;
484 valid_out_mask.flags.q.max_vertices = 1;
485 valid_out_mask.flags.q.prim_type = 1;
486 break;
487 case MESA_SHADER_TESS_CTRL:
488 valid_out_mask.flags.q.vertices = 1;
489 valid_out_mask.flags.q.explicit_xfb_buffer = 1;
490 valid_out_mask.flags.q.xfb_buffer = 1;
491 valid_out_mask.flags.q.explicit_xfb_stride = 1;
492 valid_out_mask.flags.q.xfb_stride = 1;
493 break;
494 case MESA_SHADER_TESS_EVAL:
495 case MESA_SHADER_VERTEX:
496 valid_out_mask.flags.q.explicit_xfb_buffer = 1;
497 valid_out_mask.flags.q.xfb_buffer = 1;
498 valid_out_mask.flags.q.explicit_xfb_stride = 1;
499 valid_out_mask.flags.q.xfb_stride = 1;
500 break;
501 case MESA_SHADER_FRAGMENT:
502 valid_out_mask.flags.q.blend_support = 1;
503 break;
504 default:
505 r = false;
506 _mesa_glsl_error(loc, state,
507 "out layout qualifiers only valid in "
508 "geometry, tessellation, vertex and fragment shaders");
509 }
510
511 /* Generate an error when invalid output layout qualifiers are used. */
512 if ((this->flags.i & ~valid_out_mask.flags.i) != 0) {
513 r = false;
514 _mesa_glsl_error(loc, state, "invalid output layout qualifiers used");
515 }
516
517 return r;
518 }
519
520 bool
521 ast_type_qualifier::merge_into_out_qualifier(YYLTYPE *loc,
522 _mesa_glsl_parse_state *state,
523 ast_node* &node)
524 {
525 const bool r = state->out_qualifier->merge_qualifier(loc, state,
526 *this, false);
527
528 switch (state->stage) {
529 case MESA_SHADER_GEOMETRY:
530 /* Allow future assignments of global out's stream id value */
531 state->out_qualifier->flags.q.explicit_stream = 0;
532 break;
533 case MESA_SHADER_TESS_CTRL:
534 node = new(state->linalloc) ast_tcs_output_layout(*loc);
535 break;
536 default:
537 break;
538 }
539
540 /* Allow future assignments of global out's */
541 state->out_qualifier->flags.q.explicit_xfb_buffer = 0;
542 state->out_qualifier->flags.q.explicit_xfb_stride = 0;
543
544 return r;
545 }
546
547 bool
548 ast_type_qualifier::validate_in_qualifier(YYLTYPE *loc,
549 _mesa_glsl_parse_state *state)
550 {
551 bool r = true;
552 ast_type_qualifier valid_in_mask;
553 valid_in_mask.flags.i = 0;
554
555 switch (state->stage) {
556 case MESA_SHADER_TESS_EVAL:
557 if (this->flags.q.prim_type) {
558 /* Make sure this is a valid input primitive type. */
559 switch (this->prim_type) {
560 case GL_TRIANGLES:
561 case GL_QUADS:
562 case GL_ISOLINES:
563 break;
564 default:
565 r = false;
566 _mesa_glsl_error(loc, state,
567 "invalid tessellation evaluation "
568 "shader input primitive type");
569 break;
570 }
571 }
572
573 valid_in_mask.flags.q.prim_type = 1;
574 valid_in_mask.flags.q.vertex_spacing = 1;
575 valid_in_mask.flags.q.ordering = 1;
576 valid_in_mask.flags.q.point_mode = 1;
577 break;
578 case MESA_SHADER_GEOMETRY:
579 if (this->flags.q.prim_type) {
580 /* Make sure this is a valid input primitive type. */
581 switch (this->prim_type) {
582 case GL_POINTS:
583 case GL_LINES:
584 case GL_LINES_ADJACENCY:
585 case GL_TRIANGLES:
586 case GL_TRIANGLES_ADJACENCY:
587 break;
588 default:
589 r = false;
590 _mesa_glsl_error(loc, state,
591 "invalid geometry shader input primitive type");
592 break;
593 }
594 }
595
596 valid_in_mask.flags.q.prim_type = 1;
597 valid_in_mask.flags.q.invocations = 1;
598 break;
599 case MESA_SHADER_FRAGMENT:
600 valid_in_mask.flags.q.early_fragment_tests = 1;
601 break;
602 case MESA_SHADER_COMPUTE:
603 valid_in_mask.flags.q.local_size = 7;
604 valid_in_mask.flags.q.local_size_variable = 1;
605 break;
606 default:
607 r = false;
608 _mesa_glsl_error(loc, state,
609 "input layout qualifiers only valid in "
610 "geometry, tessellation, fragment and compute shaders");
611 break;
612 }
613
614 /* Generate an error when invalid input layout qualifiers are used. */
615 if ((this->flags.i & ~valid_in_mask.flags.i) != 0) {
616 r = false;
617 _mesa_glsl_error(loc, state, "invalid input layout qualifiers used");
618 }
619
620 /* The checks below are also performed when merging but we want to spit an
621 * error against the default global input qualifier as soon as we can, with
622 * the closest error location in the shader.
623 */
624 r &= validate_prim_type(loc, state, *state->in_qualifier, *this);
625 r &= validate_vertex_spacing(loc, state, *state->in_qualifier, *this);
626 r &= validate_ordering(loc, state, *state->in_qualifier, *this);
627 r &= validate_point_mode(loc, state, *state->in_qualifier, *this);
628
629 return r;
630 }
631
632 bool
633 ast_type_qualifier::merge_into_in_qualifier(YYLTYPE *loc,
634 _mesa_glsl_parse_state *state,
635 ast_node* &node)
636 {
637 bool r = true;
638 void *lin_ctx = state->linalloc;
639
640 /* We create the gs_input_layout node before merging so, in the future, no
641 * more repeated nodes will be created as we will have the flag set.
642 */
643 if (state->stage == MESA_SHADER_GEOMETRY
644 && this->flags.q.prim_type && !state->in_qualifier->flags.q.prim_type) {
645 node = new(lin_ctx) ast_gs_input_layout(*loc, this->prim_type);
646 }
647
648 r = state->in_qualifier->merge_qualifier(loc, state, *this, false);
649
650 if (state->in_qualifier->flags.q.early_fragment_tests) {
651 state->fs_early_fragment_tests = true;
652 state->in_qualifier->flags.q.early_fragment_tests = false;
653 }
654
655 /* We allow the creation of multiple cs_input_layout nodes. Coherence among
656 * all existing nodes is checked later, when the AST node is transformed
657 * into HIR.
658 */
659 if (state->in_qualifier->flags.q.local_size) {
660 node = new(lin_ctx) ast_cs_input_layout(*loc,
661 state->in_qualifier->local_size);
662 state->in_qualifier->flags.q.local_size = 0;
663 for (int i = 0; i < 3; i++)
664 state->in_qualifier->local_size[i] = NULL;
665 }
666
667 if (state->in_qualifier->flags.q.local_size_variable) {
668 state->cs_input_local_size_variable_specified = true;
669 state->in_qualifier->flags.q.local_size_variable = false;
670 }
671
672 return r;
673 }
674
675 /**
676 * Check if the current type qualifier has any illegal flags.
677 *
678 * If so, print an error message, followed by a list of illegal flags.
679 *
680 * \param message The error message to print.
681 * \param allowed_flags A list of valid flags.
682 */
683 bool
684 ast_type_qualifier::validate_flags(YYLTYPE *loc,
685 _mesa_glsl_parse_state *state,
686 const ast_type_qualifier &allowed_flags,
687 const char *message, const char *name)
688 {
689 ast_type_qualifier bad;
690 bad.flags.i = this->flags.i & ~allowed_flags.flags.i;
691 if (bad.flags.i == 0)
692 return true;
693
694 _mesa_glsl_error(loc, state,
695 "%s '%s':"
696 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s"
697 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s"
698 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
699 message, name,
700 bad.flags.q.invariant ? " invariant" : "",
701 bad.flags.q.precise ? " precise" : "",
702 bad.flags.q.constant ? " constant" : "",
703 bad.flags.q.attribute ? " attribute" : "",
704 bad.flags.q.varying ? " varying" : "",
705 bad.flags.q.in ? " in" : "",
706 bad.flags.q.out ? " out" : "",
707 bad.flags.q.centroid ? " centroid" : "",
708 bad.flags.q.sample ? " sample" : "",
709 bad.flags.q.patch ? " patch" : "",
710 bad.flags.q.uniform ? " uniform" : "",
711 bad.flags.q.buffer ? " buffer" : "",
712 bad.flags.q.shared_storage ? " shared_storage" : "",
713 bad.flags.q.smooth ? " smooth" : "",
714 bad.flags.q.flat ? " flat" : "",
715 bad.flags.q.noperspective ? " noperspective" : "",
716 bad.flags.q.origin_upper_left ? " origin_upper_left" : "",
717 bad.flags.q.pixel_center_integer ? " pixel_center_integer" : "",
718 bad.flags.q.explicit_align ? " align" : "",
719 bad.flags.q.explicit_component ? " component" : "",
720 bad.flags.q.explicit_location ? " location" : "",
721 bad.flags.q.explicit_index ? " index" : "",
722 bad.flags.q.explicit_binding ? " binding" : "",
723 bad.flags.q.explicit_offset ? " offset" : "",
724 bad.flags.q.depth_any ? " depth_any" : "",
725 bad.flags.q.depth_greater ? " depth_greater" : "",
726 bad.flags.q.depth_less ? " depth_less" : "",
727 bad.flags.q.depth_unchanged ? " depth_unchanged" : "",
728 bad.flags.q.std140 ? " std140" : "",
729 bad.flags.q.std430 ? " std430" : "",
730 bad.flags.q.shared ? " shared" : "",
731 bad.flags.q.packed ? " packed" : "",
732 bad.flags.q.column_major ? " column_major" : "",
733 bad.flags.q.row_major ? " row_major" : "",
734 bad.flags.q.prim_type ? " prim_type" : "",
735 bad.flags.q.max_vertices ? " max_vertices" : "",
736 bad.flags.q.local_size ? " local_size" : "",
737 bad.flags.q.local_size_variable ? " local_size_variable" : "",
738 bad.flags.q.early_fragment_tests ? " early_fragment_tests" : "",
739 bad.flags.q.explicit_image_format ? " image_format" : "",
740 bad.flags.q.coherent ? " coherent" : "",
741 bad.flags.q._volatile ? " _volatile" : "",
742 bad.flags.q.restrict_flag ? " restrict_flag" : "",
743 bad.flags.q.read_only ? " read_only" : "",
744 bad.flags.q.write_only ? " write_only" : "",
745 bad.flags.q.invocations ? " invocations" : "",
746 bad.flags.q.stream ? " stream" : "",
747 bad.flags.q.explicit_stream ? " stream" : "",
748 bad.flags.q.explicit_xfb_offset ? " xfb_offset" : "",
749 bad.flags.q.xfb_buffer ? " xfb_buffer" : "",
750 bad.flags.q.explicit_xfb_buffer ? " xfb_buffer" : "",
751 bad.flags.q.xfb_stride ? " xfb_stride" : "",
752 bad.flags.q.explicit_xfb_stride ? " xfb_stride" : "",
753 bad.flags.q.vertex_spacing ? " vertex_spacing" : "",
754 bad.flags.q.ordering ? " ordering" : "",
755 bad.flags.q.point_mode ? " point_mode" : "",
756 bad.flags.q.vertices ? " vertices" : "",
757 bad.flags.q.subroutine ? " subroutine" : "",
758 bad.flags.q.subroutine_def ? " subroutine_def" : "");
759 return false;
760 }
761
762 bool
763 ast_layout_expression::process_qualifier_constant(struct _mesa_glsl_parse_state *state,
764 const char *qual_indentifier,
765 unsigned *value,
766 bool can_be_zero,
767 bool must_match)
768 {
769 int min_value = 0;
770 bool first_pass = true;
771 *value = 0;
772
773 if (!can_be_zero)
774 min_value = 1;
775
776 for (exec_node *node = layout_const_expressions.get_head_raw();
777 !node->is_tail_sentinel(); node = node->next) {
778
779 exec_list dummy_instructions;
780 ast_node *const_expression = exec_node_data(ast_node, node, link);
781
782 ir_rvalue *const ir = const_expression->hir(&dummy_instructions, state);
783
784 ir_constant *const const_int = ir->constant_expression_value();
785 if (const_int == NULL || !const_int->type->is_integer()) {
786 YYLTYPE loc = const_expression->get_location();
787 _mesa_glsl_error(&loc, state, "%s must be an integral constant "
788 "expression", qual_indentifier);
789 return false;
790 }
791
792 if (const_int->value.i[0] < min_value) {
793 YYLTYPE loc = const_expression->get_location();
794 _mesa_glsl_error(&loc, state, "%s layout qualifier is invalid "
795 "(%d < %d)", qual_indentifier,
796 const_int->value.i[0], min_value);
797 return false;
798 }
799
800 /* From section 4.4 "Layout Qualifiers" of the GLSL 4.50 spec:
801 * "When the same layout-qualifier-name occurs multiple times,
802 * in a single declaration, the last occurrence overrides the
803 * former occurrence(s)."
804 */
805 if (!first_pass) {
806 if ((must_match || !state->has_420pack()) && *value != const_int->value.u[0]) {
807 YYLTYPE loc = const_expression->get_location();
808 _mesa_glsl_error(&loc, state, "%s layout qualifier does not "
809 "match previous declaration (%d vs %d)",
810 qual_indentifier, *value, const_int->value.i[0]);
811 return false;
812 }
813 } else {
814 first_pass = false;
815 *value = const_int->value.u[0];
816 }
817
818 /* If the location is const (and we've verified that
819 * it is) then no instructions should have been emitted
820 * when we converted it to HIR. If they were emitted,
821 * then either the location isn't const after all, or
822 * we are emitting unnecessary instructions.
823 */
824 assert(dummy_instructions.is_empty());
825 }
826
827 return true;
828 }
829
830 bool
831 process_qualifier_constant(struct _mesa_glsl_parse_state *state,
832 YYLTYPE *loc,
833 const char *qual_indentifier,
834 ast_expression *const_expression,
835 unsigned *value)
836 {
837 exec_list dummy_instructions;
838
839 if (const_expression == NULL) {
840 *value = 0;
841 return true;
842 }
843
844 ir_rvalue *const ir = const_expression->hir(&dummy_instructions, state);
845
846 ir_constant *const const_int = ir->constant_expression_value();
847 if (const_int == NULL || !const_int->type->is_integer()) {
848 _mesa_glsl_error(loc, state, "%s must be an integral constant "
849 "expression", qual_indentifier);
850 return false;
851 }
852
853 if (const_int->value.i[0] < 0) {
854 _mesa_glsl_error(loc, state, "%s layout qualifier is invalid (%d < 0)",
855 qual_indentifier, const_int->value.u[0]);
856 return false;
857 }
858
859 /* If the location is const (and we've verified that
860 * it is) then no instructions should have been emitted
861 * when we converted it to HIR. If they were emitted,
862 * then either the location isn't const after all, or
863 * we are emitting unnecessary instructions.
864 */
865 assert(dummy_instructions.is_empty());
866
867 *value = const_int->value.u[0];
868 return true;
869 }