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