glsl: ignore all but the rightmost layout qualifier name from the rightmost layout...
[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 (q.flags.q.prim_type) {
272 r &= validate_prim_type(loc, state, *this, q);
273 this->flags.q.prim_type = 1;
274 this->prim_type = q.prim_type;
275 }
276
277 if (q.flags.q.max_vertices) {
278 if (this->flags.q.max_vertices
279 && !is_single_layout_merge && !is_multiple_layouts_merge) {
280 this->max_vertices->merge_qualifier(q.max_vertices);
281 } else {
282 this->flags.q.max_vertices = 1;
283 this->max_vertices = q.max_vertices;
284 }
285 }
286
287 if (q.flags.q.subroutine_def) {
288 if (this->flags.q.subroutine_def) {
289 _mesa_glsl_error(loc, state,
290 "conflicting subroutine qualifiers used");
291 } else {
292 this->subroutine_list = q.subroutine_list;
293 }
294 }
295
296 if (q.flags.q.invocations) {
297 if (this->flags.q.invocations
298 && !is_single_layout_merge && !is_multiple_layouts_merge) {
299 this->invocations->merge_qualifier(q.invocations);
300 } else {
301 this->flags.q.invocations = 1;
302 this->invocations = q.invocations;
303 }
304 }
305
306 if (state->stage == MESA_SHADER_GEOMETRY &&
307 state->has_explicit_attrib_stream()) {
308 if (!this->flags.q.explicit_stream) {
309 if (q.flags.q.stream) {
310 this->flags.q.stream = 1;
311 this->stream = q.stream;
312 } else if (!this->flags.q.stream && this->flags.q.out &&
313 !this->flags.q.in) {
314 /* Assign default global stream value */
315 this->flags.q.stream = 1;
316 this->stream = state->out_qualifier->stream;
317 }
318 }
319 }
320
321 if (state->has_enhanced_layouts()) {
322 if (!this->flags.q.explicit_xfb_buffer) {
323 if (q.flags.q.xfb_buffer) {
324 this->flags.q.xfb_buffer = 1;
325 this->xfb_buffer = q.xfb_buffer;
326 } else if (!this->flags.q.xfb_buffer && this->flags.q.out &&
327 !this->flags.q.in) {
328 /* Assign global xfb_buffer value */
329 this->flags.q.xfb_buffer = 1;
330 this->xfb_buffer = state->out_qualifier->xfb_buffer;
331 }
332 }
333
334 if (q.flags.q.explicit_xfb_stride)
335 this->xfb_stride = q.xfb_stride;
336
337 /* Merge all we xfb_stride qualifiers into the global out */
338 if (q.flags.q.explicit_xfb_stride || this->flags.q.xfb_stride) {
339
340 /* Set xfb_stride flag to 0 to avoid adding duplicates every time
341 * there is a merge.
342 */
343 this->flags.q.xfb_stride = 0;
344
345 unsigned buff_idx;
346 if (process_qualifier_constant(state, loc, "xfb_buffer",
347 this->xfb_buffer, &buff_idx)) {
348 if (state->out_qualifier->out_xfb_stride[buff_idx]
349 && !is_single_layout_merge && !is_multiple_layouts_merge) {
350 state->out_qualifier->out_xfb_stride[buff_idx]->merge_qualifier(
351 new(state->linalloc) ast_layout_expression(*loc, this->xfb_stride));
352 } else {
353 state->out_qualifier->out_xfb_stride[buff_idx] =
354 new(state->linalloc) ast_layout_expression(*loc, this->xfb_stride);
355 }
356 }
357 }
358 }
359
360 if (q.flags.q.vertices) {
361 if (this->flags.q.vertices
362 && !is_single_layout_merge && !is_multiple_layouts_merge) {
363 this->vertices->merge_qualifier(q.vertices);
364 } else {
365 this->flags.q.vertices = 1;
366 this->vertices = q.vertices;
367 }
368 }
369
370 if (q.flags.q.vertex_spacing) {
371 r &= validate_vertex_spacing(loc, state, *this, q);
372 this->flags.q.vertex_spacing = 1;
373 this->vertex_spacing = q.vertex_spacing;
374 }
375
376 if (q.flags.q.ordering) {
377 r &= validate_ordering(loc, state, *this, q);
378 this->flags.q.ordering = 1;
379 this->ordering = q.ordering;
380 }
381
382 if (q.flags.q.point_mode) {
383 r &= validate_point_mode(loc, state, *this, q);
384 this->flags.q.point_mode = 1;
385 this->point_mode = q.point_mode;
386 }
387
388 if (q.flags.q.early_fragment_tests)
389 this->flags.q.early_fragment_tests = true;
390
391 if ((q.flags.i & ubo_mat_mask.flags.i) != 0)
392 this->flags.i &= ~ubo_mat_mask.flags.i;
393 if ((q.flags.i & ubo_layout_mask.flags.i) != 0)
394 this->flags.i &= ~ubo_layout_mask.flags.i;
395
396 for (int i = 0; i < 3; i++) {
397 if (q.flags.q.local_size & (1 << i)) {
398 if (this->local_size[i]
399 && !is_single_layout_merge && !is_multiple_layouts_merge) {
400 this->local_size[i]->merge_qualifier(q.local_size[i]);
401 } else {
402 this->local_size[i] = q.local_size[i];
403 }
404 }
405 }
406
407 if (q.flags.q.local_size_variable)
408 this->flags.q.local_size_variable = true;
409
410 this->flags.i |= q.flags.i;
411
412 if (this->flags.q.in &&
413 (this->flags.i & ~input_layout_mask.flags.i) != 0) {
414 _mesa_glsl_error(loc, state, "invalid input layout qualifier used");
415 return false;
416 }
417
418 if (q.flags.q.explicit_align)
419 this->align = q.align;
420
421 if (q.flags.q.explicit_location)
422 this->location = q.location;
423
424 if (q.flags.q.explicit_index)
425 this->index = q.index;
426
427 if (q.flags.q.explicit_component)
428 this->component = q.component;
429
430 if (q.flags.q.explicit_binding)
431 this->binding = q.binding;
432
433 if (q.flags.q.explicit_offset || q.flags.q.explicit_xfb_offset)
434 this->offset = q.offset;
435
436 if (q.precision != ast_precision_none)
437 this->precision = q.precision;
438
439 if (q.flags.q.explicit_image_format) {
440 this->image_format = q.image_format;
441 this->image_base_type = q.image_base_type;
442 }
443
444 return r;
445 }
446
447 bool
448 ast_type_qualifier::validate_out_qualifier(YYLTYPE *loc,
449 _mesa_glsl_parse_state *state)
450 {
451 bool r = true;
452 ast_type_qualifier valid_out_mask;
453 valid_out_mask.flags.i = 0;
454
455 switch (state->stage) {
456 case MESA_SHADER_GEOMETRY:
457 if (this->flags.q.prim_type) {
458 /* Make sure this is a valid output primitive type. */
459 switch (this->prim_type) {
460 case GL_POINTS:
461 case GL_LINE_STRIP:
462 case GL_TRIANGLE_STRIP:
463 break;
464 default:
465 r = false;
466 _mesa_glsl_error(loc, state, "invalid geometry shader output "
467 "primitive type");
468 break;
469 }
470 }
471
472 valid_out_mask.flags.q.stream = 1;
473 valid_out_mask.flags.q.explicit_stream = 1;
474 valid_out_mask.flags.q.explicit_xfb_buffer = 1;
475 valid_out_mask.flags.q.xfb_buffer = 1;
476 valid_out_mask.flags.q.explicit_xfb_stride = 1;
477 valid_out_mask.flags.q.xfb_stride = 1;
478 valid_out_mask.flags.q.max_vertices = 1;
479 valid_out_mask.flags.q.prim_type = 1;
480 break;
481 case MESA_SHADER_TESS_CTRL:
482 valid_out_mask.flags.q.vertices = 1;
483 valid_out_mask.flags.q.explicit_xfb_buffer = 1;
484 valid_out_mask.flags.q.xfb_buffer = 1;
485 valid_out_mask.flags.q.explicit_xfb_stride = 1;
486 valid_out_mask.flags.q.xfb_stride = 1;
487 break;
488 case MESA_SHADER_TESS_EVAL:
489 case MESA_SHADER_VERTEX:
490 valid_out_mask.flags.q.explicit_xfb_buffer = 1;
491 valid_out_mask.flags.q.xfb_buffer = 1;
492 valid_out_mask.flags.q.explicit_xfb_stride = 1;
493 valid_out_mask.flags.q.xfb_stride = 1;
494 break;
495 case MESA_SHADER_FRAGMENT:
496 valid_out_mask.flags.q.blend_support = 1;
497 break;
498 default:
499 r = false;
500 _mesa_glsl_error(loc, state,
501 "out layout qualifiers only valid in "
502 "geometry, tessellation, vertex and fragment shaders");
503 }
504
505 /* Generate an error when invalid output layout qualifiers are used. */
506 if ((this->flags.i & ~valid_out_mask.flags.i) != 0) {
507 r = false;
508 _mesa_glsl_error(loc, state, "invalid output layout qualifiers used");
509 }
510
511 return r;
512 }
513
514 bool
515 ast_type_qualifier::merge_into_out_qualifier(YYLTYPE *loc,
516 _mesa_glsl_parse_state *state,
517 ast_node* &node, bool create_node)
518 {
519 const bool r = state->out_qualifier->merge_qualifier(loc, state,
520 *this, false);
521
522 switch (state->stage) {
523 case MESA_SHADER_GEOMETRY:
524 /* Allow future assignments of global out's stream id value */
525 state->out_qualifier->flags.q.explicit_stream = 0;
526 break;
527 case MESA_SHADER_TESS_CTRL:
528 if (create_node)
529 node = new(state->linalloc) ast_tcs_output_layout(*loc);
530 break;
531 default:
532 break;
533 }
534
535 /* Allow future assignments of global out's */
536 state->out_qualifier->flags.q.explicit_xfb_buffer = 0;
537 state->out_qualifier->flags.q.explicit_xfb_stride = 0;
538
539 return r;
540 }
541
542 bool
543 ast_type_qualifier::validate_in_qualifier(YYLTYPE *loc,
544 _mesa_glsl_parse_state *state)
545 {
546 bool r = true;
547 ast_type_qualifier valid_in_mask;
548 valid_in_mask.flags.i = 0;
549
550 switch (state->stage) {
551 case MESA_SHADER_TESS_EVAL:
552 if (this->flags.q.prim_type) {
553 /* Make sure this is a valid input primitive type. */
554 switch (this->prim_type) {
555 case GL_TRIANGLES:
556 case GL_QUADS:
557 case GL_ISOLINES:
558 break;
559 default:
560 r = false;
561 _mesa_glsl_error(loc, state,
562 "invalid tessellation evaluation "
563 "shader input primitive type");
564 break;
565 }
566 }
567
568 valid_in_mask.flags.q.prim_type = 1;
569 valid_in_mask.flags.q.vertex_spacing = 1;
570 valid_in_mask.flags.q.ordering = 1;
571 valid_in_mask.flags.q.point_mode = 1;
572 break;
573 case MESA_SHADER_GEOMETRY:
574 if (this->flags.q.prim_type) {
575 /* Make sure this is a valid input primitive type. */
576 switch (this->prim_type) {
577 case GL_POINTS:
578 case GL_LINES:
579 case GL_LINES_ADJACENCY:
580 case GL_TRIANGLES:
581 case GL_TRIANGLES_ADJACENCY:
582 break;
583 default:
584 r = false;
585 _mesa_glsl_error(loc, state,
586 "invalid geometry shader input primitive type");
587 break;
588 }
589 }
590
591 valid_in_mask.flags.q.prim_type = 1;
592 valid_in_mask.flags.q.invocations = 1;
593 break;
594 case MESA_SHADER_FRAGMENT:
595 valid_in_mask.flags.q.early_fragment_tests = 1;
596 break;
597 case MESA_SHADER_COMPUTE:
598 valid_in_mask.flags.q.local_size = 7;
599 valid_in_mask.flags.q.local_size_variable = 1;
600 break;
601 default:
602 r = false;
603 _mesa_glsl_error(loc, state,
604 "input layout qualifiers only valid in "
605 "geometry, tessellation, fragment and compute shaders");
606 break;
607 }
608
609 /* Generate an error when invalid input layout qualifiers are used. */
610 if ((this->flags.i & ~valid_in_mask.flags.i) != 0) {
611 r = false;
612 _mesa_glsl_error(loc, state, "invalid input layout qualifiers used");
613 }
614
615 /* The checks below are also performed when merging but we want to spit an
616 * error against the default global input qualifier as soon as we can, with
617 * the closest error location in the shader.
618 */
619 r &= validate_prim_type(loc, state, *state->in_qualifier, *this);
620 r &= validate_vertex_spacing(loc, state, *state->in_qualifier, *this);
621 r &= validate_ordering(loc, state, *state->in_qualifier, *this);
622 r &= validate_point_mode(loc, state, *state->in_qualifier, *this);
623
624 return r;
625 }
626
627 bool
628 ast_type_qualifier::merge_into_in_qualifier(YYLTYPE *loc,
629 _mesa_glsl_parse_state *state,
630 ast_node* &node, bool create_node)
631 {
632 bool r = true;
633 void *lin_ctx = state->linalloc;
634
635 /* We create the gs_input_layout node before merging so, in the future, no
636 * more repeated nodes will be created as we will have the flag set.
637 */
638 if (state->stage == MESA_SHADER_GEOMETRY
639 && this->flags.q.prim_type && !state->in_qualifier->flags.q.prim_type
640 && create_node) {
641 node = new(lin_ctx) ast_gs_input_layout(*loc, this->prim_type);
642 }
643
644 r = state->in_qualifier->merge_qualifier(loc, state, *this, false);
645
646 if (state->in_qualifier->flags.q.early_fragment_tests) {
647 state->fs_early_fragment_tests = true;
648 state->in_qualifier->flags.q.early_fragment_tests = false;
649 }
650
651 /* We allow the creation of multiple cs_input_layout nodes. Coherence among
652 * all existing nodes is checked later, when the AST node is transformed
653 * into HIR.
654 */
655 if (state->in_qualifier->flags.q.local_size) {
656 if (create_node)
657 node = new(lin_ctx) ast_cs_input_layout(*loc, state->in_qualifier->local_size);
658 state->in_qualifier->flags.q.local_size = 0;
659 for (int i = 0; i < 3; i++)
660 state->in_qualifier->local_size[i] = NULL;
661 }
662
663 if (state->in_qualifier->flags.q.local_size_variable) {
664 state->cs_input_local_size_variable_specified = true;
665 state->in_qualifier->flags.q.local_size_variable = false;
666 }
667
668 return r;
669 }
670
671 /**
672 * Check if the current type qualifier has any illegal flags.
673 *
674 * If so, print an error message, followed by a list of illegal flags.
675 *
676 * \param message The error message to print.
677 * \param allowed_flags A list of valid flags.
678 */
679 bool
680 ast_type_qualifier::validate_flags(YYLTYPE *loc,
681 _mesa_glsl_parse_state *state,
682 const ast_type_qualifier &allowed_flags,
683 const char *message, const char *name)
684 {
685 ast_type_qualifier bad;
686 bad.flags.i = this->flags.i & ~allowed_flags.flags.i;
687 if (bad.flags.i == 0)
688 return true;
689
690 _mesa_glsl_error(loc, state,
691 "%s '%s':"
692 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s"
693 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s"
694 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
695 message, name,
696 bad.flags.q.invariant ? " invariant" : "",
697 bad.flags.q.precise ? " precise" : "",
698 bad.flags.q.constant ? " constant" : "",
699 bad.flags.q.attribute ? " attribute" : "",
700 bad.flags.q.varying ? " varying" : "",
701 bad.flags.q.in ? " in" : "",
702 bad.flags.q.out ? " out" : "",
703 bad.flags.q.centroid ? " centroid" : "",
704 bad.flags.q.sample ? " sample" : "",
705 bad.flags.q.patch ? " patch" : "",
706 bad.flags.q.uniform ? " uniform" : "",
707 bad.flags.q.buffer ? " buffer" : "",
708 bad.flags.q.shared_storage ? " shared_storage" : "",
709 bad.flags.q.smooth ? " smooth" : "",
710 bad.flags.q.flat ? " flat" : "",
711 bad.flags.q.noperspective ? " noperspective" : "",
712 bad.flags.q.origin_upper_left ? " origin_upper_left" : "",
713 bad.flags.q.pixel_center_integer ? " pixel_center_integer" : "",
714 bad.flags.q.explicit_align ? " align" : "",
715 bad.flags.q.explicit_component ? " component" : "",
716 bad.flags.q.explicit_location ? " location" : "",
717 bad.flags.q.explicit_index ? " index" : "",
718 bad.flags.q.explicit_binding ? " binding" : "",
719 bad.flags.q.explicit_offset ? " offset" : "",
720 bad.flags.q.depth_any ? " depth_any" : "",
721 bad.flags.q.depth_greater ? " depth_greater" : "",
722 bad.flags.q.depth_less ? " depth_less" : "",
723 bad.flags.q.depth_unchanged ? " depth_unchanged" : "",
724 bad.flags.q.std140 ? " std140" : "",
725 bad.flags.q.std430 ? " std430" : "",
726 bad.flags.q.shared ? " shared" : "",
727 bad.flags.q.packed ? " packed" : "",
728 bad.flags.q.column_major ? " column_major" : "",
729 bad.flags.q.row_major ? " row_major" : "",
730 bad.flags.q.prim_type ? " prim_type" : "",
731 bad.flags.q.max_vertices ? " max_vertices" : "",
732 bad.flags.q.local_size ? " local_size" : "",
733 bad.flags.q.local_size_variable ? " local_size_variable" : "",
734 bad.flags.q.early_fragment_tests ? " early_fragment_tests" : "",
735 bad.flags.q.explicit_image_format ? " image_format" : "",
736 bad.flags.q.coherent ? " coherent" : "",
737 bad.flags.q._volatile ? " _volatile" : "",
738 bad.flags.q.restrict_flag ? " restrict_flag" : "",
739 bad.flags.q.read_only ? " read_only" : "",
740 bad.flags.q.write_only ? " write_only" : "",
741 bad.flags.q.invocations ? " invocations" : "",
742 bad.flags.q.stream ? " stream" : "",
743 bad.flags.q.explicit_stream ? " stream" : "",
744 bad.flags.q.explicit_xfb_offset ? " xfb_offset" : "",
745 bad.flags.q.xfb_buffer ? " xfb_buffer" : "",
746 bad.flags.q.explicit_xfb_buffer ? " xfb_buffer" : "",
747 bad.flags.q.xfb_stride ? " xfb_stride" : "",
748 bad.flags.q.explicit_xfb_stride ? " xfb_stride" : "",
749 bad.flags.q.vertex_spacing ? " vertex_spacing" : "",
750 bad.flags.q.ordering ? " ordering" : "",
751 bad.flags.q.point_mode ? " point_mode" : "",
752 bad.flags.q.vertices ? " vertices" : "",
753 bad.flags.q.subroutine ? " subroutine" : "",
754 bad.flags.q.subroutine_def ? " subroutine_def" : "");
755 return false;
756 }
757
758 bool
759 ast_layout_expression::process_qualifier_constant(struct _mesa_glsl_parse_state *state,
760 const char *qual_indentifier,
761 unsigned *value,
762 bool can_be_zero,
763 bool must_match)
764 {
765 int min_value = 0;
766 bool first_pass = true;
767 *value = 0;
768
769 if (!can_be_zero)
770 min_value = 1;
771
772 for (exec_node *node = layout_const_expressions.get_head_raw();
773 !node->is_tail_sentinel(); node = node->next) {
774
775 exec_list dummy_instructions;
776 ast_node *const_expression = exec_node_data(ast_node, node, link);
777
778 ir_rvalue *const ir = const_expression->hir(&dummy_instructions, state);
779
780 ir_constant *const const_int = ir->constant_expression_value();
781 if (const_int == NULL || !const_int->type->is_integer()) {
782 YYLTYPE loc = const_expression->get_location();
783 _mesa_glsl_error(&loc, state, "%s must be an integral constant "
784 "expression", qual_indentifier);
785 return false;
786 }
787
788 if (const_int->value.i[0] < min_value) {
789 YYLTYPE loc = const_expression->get_location();
790 _mesa_glsl_error(&loc, state, "%s layout qualifier is invalid "
791 "(%d < %d)", qual_indentifier,
792 const_int->value.i[0], min_value);
793 return false;
794 }
795
796 /* From section 4.4 "Layout Qualifiers" of the GLSL 4.50 spec:
797 * "When the same layout-qualifier-name occurs multiple times,
798 * in a single declaration, the last occurrence overrides the
799 * former occurrence(s)."
800 */
801 if (!first_pass) {
802 if ((must_match || !state->has_420pack()) && *value != const_int->value.u[0]) {
803 YYLTYPE loc = const_expression->get_location();
804 _mesa_glsl_error(&loc, state, "%s layout qualifier does not "
805 "match previous declaration (%d vs %d)",
806 qual_indentifier, *value, const_int->value.i[0]);
807 return false;
808 }
809 } else {
810 first_pass = false;
811 *value = const_int->value.u[0];
812 }
813
814 /* If the location is const (and we've verified that
815 * it is) then no instructions should have been emitted
816 * when we converted it to HIR. If they were emitted,
817 * then either the location isn't const after all, or
818 * we are emitting unnecessary instructions.
819 */
820 assert(dummy_instructions.is_empty());
821 }
822
823 return true;
824 }
825
826 bool
827 process_qualifier_constant(struct _mesa_glsl_parse_state *state,
828 YYLTYPE *loc,
829 const char *qual_indentifier,
830 ast_expression *const_expression,
831 unsigned *value)
832 {
833 exec_list dummy_instructions;
834
835 if (const_expression == NULL) {
836 *value = 0;
837 return true;
838 }
839
840 ir_rvalue *const ir = const_expression->hir(&dummy_instructions, state);
841
842 ir_constant *const const_int = ir->constant_expression_value();
843 if (const_int == NULL || !const_int->type->is_integer()) {
844 _mesa_glsl_error(loc, state, "%s must be an integral constant "
845 "expression", qual_indentifier);
846 return false;
847 }
848
849 if (const_int->value.i[0] < 0) {
850 _mesa_glsl_error(loc, state, "%s layout qualifier is invalid (%d < 0)",
851 qual_indentifier, const_int->value.u[0]);
852 return false;
853 }
854
855 /* If the location is const (and we've verified that
856 * it is) then no instructions should have been emitted
857 * when we converted it to HIR. If they were emitted,
858 * then either the location isn't const after all, or
859 * we are emitting unnecessary instructions.
860 */
861 assert(dummy_instructions.is_empty());
862
863 *value = const_int->value.u[0];
864 return true;
865 }