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