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