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