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