2 * Copyright © 2010 Intel Corporation
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:
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
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.
27 ast_type_specifier::print(void) const
32 printf("%s ", type_name
);
35 if (array_specifier
) {
36 array_specifier
->print();
41 ast_fully_specified_type::has_qualifiers(_mesa_glsl_parse_state
*state
) const
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 subroutine_only
.flags
.q
.subroutine_def
= 1;
48 if (state
->has_explicit_uniform_location()) {
49 subroutine_only
.flags
.q
.explicit_index
= 1;
51 return (this->qualifier
.flags
.i
& ~subroutine_only
.flags
.i
) != 0;
54 bool ast_type_qualifier::has_interpolation() const
56 return this->flags
.q
.smooth
58 || this->flags
.q
.noperspective
;
62 ast_type_qualifier::has_layout() const
64 return this->flags
.q
.origin_upper_left
65 || this->flags
.q
.pixel_center_integer
66 || this->flags
.q
.depth_any
67 || this->flags
.q
.depth_greater
68 || this->flags
.q
.depth_less
69 || this->flags
.q
.depth_unchanged
70 || this->flags
.q
.std140
71 || this->flags
.q
.std430
72 || this->flags
.q
.shared
73 || this->flags
.q
.column_major
74 || this->flags
.q
.row_major
75 || this->flags
.q
.packed
76 || this->flags
.q
.explicit_align
77 || this->flags
.q
.explicit_location
78 || this->flags
.q
.explicit_image_format
79 || this->flags
.q
.explicit_index
80 || this->flags
.q
.explicit_binding
81 || this->flags
.q
.explicit_offset
82 || this->flags
.q
.explicit_stream
;
86 ast_type_qualifier::has_storage() const
88 return this->flags
.q
.constant
89 || this->flags
.q
.attribute
90 || this->flags
.q
.varying
93 || this->flags
.q
.uniform
94 || this->flags
.q
.buffer
95 || this->flags
.q
.shared_storage
;
99 ast_type_qualifier::has_auxiliary_storage() const
101 return this->flags
.q
.centroid
102 || this->flags
.q
.sample
103 || this->flags
.q
.patch
;
107 * This function merges both duplicate identifies within a single layout and
108 * multiple layout qualifiers on a single variable declaration. The
109 * is_single_layout_merge param is used differentiate between the two.
112 ast_type_qualifier::merge_qualifier(YYLTYPE
*loc
,
113 _mesa_glsl_parse_state
*state
,
114 const ast_type_qualifier
&q
,
115 bool is_single_layout_merge
)
117 ast_type_qualifier ubo_mat_mask
;
118 ubo_mat_mask
.flags
.i
= 0;
119 ubo_mat_mask
.flags
.q
.row_major
= 1;
120 ubo_mat_mask
.flags
.q
.column_major
= 1;
122 ast_type_qualifier ubo_layout_mask
;
123 ubo_layout_mask
.flags
.i
= 0;
124 ubo_layout_mask
.flags
.q
.std140
= 1;
125 ubo_layout_mask
.flags
.q
.packed
= 1;
126 ubo_layout_mask
.flags
.q
.shared
= 1;
127 ubo_layout_mask
.flags
.q
.std430
= 1;
129 ast_type_qualifier ubo_binding_mask
;
130 ubo_binding_mask
.flags
.i
= 0;
131 ubo_binding_mask
.flags
.q
.explicit_binding
= 1;
132 ubo_binding_mask
.flags
.q
.explicit_offset
= 1;
134 ast_type_qualifier stream_layout_mask
;
135 stream_layout_mask
.flags
.i
= 0;
136 stream_layout_mask
.flags
.q
.stream
= 1;
138 /* FIXME: We should probably do interface and function param validation
141 ast_type_qualifier input_layout_mask
;
142 input_layout_mask
.flags
.i
= 0;
143 input_layout_mask
.flags
.q
.centroid
= 1;
144 /* Function params can have constant */
145 input_layout_mask
.flags
.q
.constant
= 1;
146 input_layout_mask
.flags
.q
.explicit_location
= 1;
147 input_layout_mask
.flags
.q
.flat
= 1;
148 input_layout_mask
.flags
.q
.in
= 1;
149 input_layout_mask
.flags
.q
.invariant
= 1;
150 input_layout_mask
.flags
.q
.noperspective
= 1;
151 input_layout_mask
.flags
.q
.origin_upper_left
= 1;
152 /* Function params 'inout' will set this */
153 input_layout_mask
.flags
.q
.out
= 1;
154 input_layout_mask
.flags
.q
.patch
= 1;
155 input_layout_mask
.flags
.q
.pixel_center_integer
= 1;
156 input_layout_mask
.flags
.q
.precise
= 1;
157 input_layout_mask
.flags
.q
.sample
= 1;
158 input_layout_mask
.flags
.q
.smooth
= 1;
160 /* Uniform block layout qualifiers get to overwrite each
161 * other (rightmost having priority), while all other
162 * qualifiers currently don't allow duplicates.
164 ast_type_qualifier allowed_duplicates_mask
;
165 allowed_duplicates_mask
.flags
.i
=
166 ubo_mat_mask
.flags
.i
|
167 ubo_layout_mask
.flags
.i
|
168 ubo_binding_mask
.flags
.i
;
170 /* Geometry shaders can have several layout qualifiers
171 * assigning different stream values.
173 if (state
->stage
== MESA_SHADER_GEOMETRY
)
174 allowed_duplicates_mask
.flags
.i
|=
175 stream_layout_mask
.flags
.i
;
177 if (is_single_layout_merge
&& !state
->has_enhanced_layouts() &&
178 (this->flags
.i
& q
.flags
.i
& ~allowed_duplicates_mask
.flags
.i
) != 0) {
179 _mesa_glsl_error(loc
, state
,
180 "duplicate layout qualifiers used");
184 if (q
.flags
.q
.prim_type
) {
185 if (this->flags
.q
.prim_type
&& this->prim_type
!= q
.prim_type
) {
186 _mesa_glsl_error(loc
, state
,
187 "conflicting primitive type qualifiers used");
190 this->prim_type
= q
.prim_type
;
193 if (q
.flags
.q
.max_vertices
) {
194 if (this->max_vertices
) {
195 this->max_vertices
->merge_qualifier(q
.max_vertices
);
197 this->max_vertices
= q
.max_vertices
;
201 if (q
.flags
.q
.subroutine_def
) {
202 if (this->flags
.q
.subroutine_def
) {
203 _mesa_glsl_error(loc
, state
,
204 "conflicting subroutine qualifiers used");
206 this->subroutine_list
= q
.subroutine_list
;
210 if (q
.flags
.q
.invocations
) {
211 if (this->invocations
) {
212 this->invocations
->merge_qualifier(q
.invocations
);
214 this->invocations
= q
.invocations
;
218 if (state
->stage
== MESA_SHADER_GEOMETRY
&&
219 state
->has_explicit_attrib_stream()) {
220 if (!this->flags
.q
.explicit_stream
) {
221 if (q
.flags
.q
.stream
) {
222 this->flags
.q
.stream
= 1;
223 this->stream
= q
.stream
;
224 } else if (!this->flags
.q
.stream
&& this->flags
.q
.out
) {
225 /* Assign default global stream value */
226 this->flags
.q
.stream
= 1;
227 this->stream
= state
->out_qualifier
->stream
;
232 if (q
.flags
.q
.vertices
) {
233 if (this->vertices
) {
234 this->vertices
->merge_qualifier(q
.vertices
);
236 this->vertices
= q
.vertices
;
240 if (q
.flags
.q
.vertex_spacing
) {
241 if (this->flags
.q
.vertex_spacing
&& this->vertex_spacing
!= q
.vertex_spacing
) {
242 _mesa_glsl_error(loc
, state
,
243 "conflicting vertex spacing used");
246 this->vertex_spacing
= q
.vertex_spacing
;
249 if (q
.flags
.q
.ordering
) {
250 if (this->flags
.q
.ordering
&& this->ordering
!= q
.ordering
) {
251 _mesa_glsl_error(loc
, state
,
252 "conflicting ordering used");
255 this->ordering
= q
.ordering
;
258 if (q
.flags
.q
.point_mode
) {
259 if (this->flags
.q
.point_mode
&& this->point_mode
!= q
.point_mode
) {
260 _mesa_glsl_error(loc
, state
,
261 "conflicting point mode used");
264 this->point_mode
= q
.point_mode
;
267 if ((q
.flags
.i
& ubo_mat_mask
.flags
.i
) != 0)
268 this->flags
.i
&= ~ubo_mat_mask
.flags
.i
;
269 if ((q
.flags
.i
& ubo_layout_mask
.flags
.i
) != 0)
270 this->flags
.i
&= ~ubo_layout_mask
.flags
.i
;
272 for (int i
= 0; i
< 3; i
++) {
273 if (q
.flags
.q
.local_size
& (1 << i
)) {
274 if (this->local_size
[i
]) {
275 this->local_size
[i
]->merge_qualifier(q
.local_size
[i
]);
277 this->local_size
[i
] = q
.local_size
[i
];
282 this->flags
.i
|= q
.flags
.i
;
284 if (this->flags
.q
.in
&&
285 (this->flags
.i
& ~input_layout_mask
.flags
.i
) != 0) {
286 _mesa_glsl_error(loc
, state
,
287 "invalid input layout qualifier used");
291 if (q
.flags
.q
.explicit_align
)
292 this->align
= q
.align
;
294 if (q
.flags
.q
.explicit_location
)
295 this->location
= q
.location
;
297 if (q
.flags
.q
.explicit_index
)
298 this->index
= q
.index
;
300 if (q
.flags
.q
.explicit_binding
)
301 this->binding
= q
.binding
;
303 if (q
.flags
.q
.explicit_offset
)
304 this->offset
= q
.offset
;
306 if (q
.precision
!= ast_precision_none
)
307 this->precision
= q
.precision
;
309 if (q
.flags
.q
.explicit_image_format
) {
310 this->image_format
= q
.image_format
;
311 this->image_base_type
= q
.image_base_type
;
318 ast_type_qualifier::merge_out_qualifier(YYLTYPE
*loc
,
319 _mesa_glsl_parse_state
*state
,
320 const ast_type_qualifier
&q
,
321 ast_node
* &node
, bool create_node
)
323 void *mem_ctx
= state
;
324 const bool r
= this->merge_qualifier(loc
, state
, q
, false);
326 if (state
->stage
== MESA_SHADER_GEOMETRY
) {
327 if (q
.flags
.q
.prim_type
) {
328 /* Make sure this is a valid output primitive type. */
329 switch (q
.prim_type
) {
332 case GL_TRIANGLE_STRIP
:
335 _mesa_glsl_error(loc
, state
, "invalid geometry shader output "
341 /* Allow future assigments of global out's stream id value */
342 this->flags
.q
.explicit_stream
= 0;
343 } else if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
345 node
= new(mem_ctx
) ast_tcs_output_layout(*loc
);
348 _mesa_glsl_error(loc
, state
, "out layout qualifiers only valid in "
349 "tessellation control or geometry shaders");
356 ast_type_qualifier::merge_in_qualifier(YYLTYPE
*loc
,
357 _mesa_glsl_parse_state
*state
,
358 const ast_type_qualifier
&q
,
359 ast_node
* &node
, bool create_node
)
361 void *mem_ctx
= state
;
362 bool create_gs_ast
= false;
363 bool create_cs_ast
= false;
364 ast_type_qualifier valid_in_mask
;
365 valid_in_mask
.flags
.i
= 0;
367 switch (state
->stage
) {
368 case MESA_SHADER_TESS_EVAL
:
369 if (q
.flags
.q
.prim_type
) {
370 /* Make sure this is a valid input primitive type. */
371 switch (q
.prim_type
) {
377 _mesa_glsl_error(loc
, state
,
378 "invalid tessellation evaluation "
379 "shader input primitive type");
384 valid_in_mask
.flags
.q
.prim_type
= 1;
385 valid_in_mask
.flags
.q
.vertex_spacing
= 1;
386 valid_in_mask
.flags
.q
.ordering
= 1;
387 valid_in_mask
.flags
.q
.point_mode
= 1;
389 case MESA_SHADER_GEOMETRY
:
390 if (q
.flags
.q
.prim_type
) {
391 /* Make sure this is a valid input primitive type. */
392 switch (q
.prim_type
) {
395 case GL_LINES_ADJACENCY
:
397 case GL_TRIANGLES_ADJACENCY
:
400 _mesa_glsl_error(loc
, state
,
401 "invalid geometry shader input primitive type");
407 q
.flags
.q
.prim_type
&&
408 !state
->in_qualifier
->flags
.q
.prim_type
;
410 valid_in_mask
.flags
.q
.prim_type
= 1;
411 valid_in_mask
.flags
.q
.invocations
= 1;
413 case MESA_SHADER_FRAGMENT
:
414 valid_in_mask
.flags
.q
.early_fragment_tests
= 1;
416 case MESA_SHADER_COMPUTE
:
418 q
.flags
.q
.local_size
!= 0 &&
419 state
->in_qualifier
->flags
.q
.local_size
== 0;
421 valid_in_mask
.flags
.q
.local_size
= 7;
424 _mesa_glsl_error(loc
, state
,
425 "input layout qualifiers only valid in "
426 "geometry, fragment and compute shaders");
430 /* Generate an error when invalid input layout qualifiers are used. */
431 if ((q
.flags
.i
& ~valid_in_mask
.flags
.i
) != 0) {
432 _mesa_glsl_error(loc
, state
,
433 "invalid input layout qualifiers used");
437 /* Input layout qualifiers can be specified multiple
438 * times in separate declarations, as long as they match.
440 if (this->flags
.q
.prim_type
) {
441 if (q
.flags
.q
.prim_type
&&
442 this->prim_type
!= q
.prim_type
) {
443 _mesa_glsl_error(loc
, state
,
444 "conflicting input primitive %s specified",
445 state
->stage
== MESA_SHADER_GEOMETRY
?
448 } else if (q
.flags
.q
.prim_type
) {
449 state
->in_qualifier
->flags
.q
.prim_type
= 1;
450 state
->in_qualifier
->prim_type
= q
.prim_type
;
453 if (q
.flags
.q
.invocations
) {
454 this->flags
.q
.invocations
= 1;
455 if (this->invocations
) {
456 this->invocations
->merge_qualifier(q
.invocations
);
458 this->invocations
= q
.invocations
;
462 if (q
.flags
.q
.early_fragment_tests
) {
463 state
->fs_early_fragment_tests
= true;
466 if (this->flags
.q
.vertex_spacing
) {
467 if (q
.flags
.q
.vertex_spacing
&&
468 this->vertex_spacing
!= q
.vertex_spacing
) {
469 _mesa_glsl_error(loc
, state
,
470 "conflicting vertex spacing specified");
472 } else if (q
.flags
.q
.vertex_spacing
) {
473 this->flags
.q
.vertex_spacing
= 1;
474 this->vertex_spacing
= q
.vertex_spacing
;
477 if (this->flags
.q
.ordering
) {
478 if (q
.flags
.q
.ordering
&&
479 this->ordering
!= q
.ordering
) {
480 _mesa_glsl_error(loc
, state
,
481 "conflicting ordering specified");
483 } else if (q
.flags
.q
.ordering
) {
484 this->flags
.q
.ordering
= 1;
485 this->ordering
= q
.ordering
;
488 if (this->flags
.q
.point_mode
) {
489 if (q
.flags
.q
.point_mode
&&
490 this->point_mode
!= q
.point_mode
) {
491 _mesa_glsl_error(loc
, state
,
492 "conflicting point mode specified");
494 } else if (q
.flags
.q
.point_mode
) {
495 this->flags
.q
.point_mode
= 1;
496 this->point_mode
= q
.point_mode
;
501 node
= new(mem_ctx
) ast_gs_input_layout(*loc
, q
.prim_type
);
502 } else if (create_cs_ast
) {
503 node
= new(mem_ctx
) ast_cs_input_layout(*loc
, q
.local_size
);
511 ast_layout_expression::process_qualifier_constant(struct _mesa_glsl_parse_state
*state
,
512 const char *qual_indentifier
,
517 bool first_pass
= true;
523 for (exec_node
*node
= layout_const_expressions
.head
;
524 !node
->is_tail_sentinel(); node
= node
->next
) {
526 exec_list dummy_instructions
;
527 ast_node
*const_expression
= exec_node_data(ast_node
, node
, link
);
529 ir_rvalue
*const ir
= const_expression
->hir(&dummy_instructions
, state
);
531 ir_constant
*const const_int
= ir
->constant_expression_value();
532 if (const_int
== NULL
|| !const_int
->type
->is_integer()) {
533 YYLTYPE loc
= const_expression
->get_location();
534 _mesa_glsl_error(&loc
, state
, "%s must be an integral constant "
535 "expression", qual_indentifier
);
539 if (const_int
->value
.i
[0] < min_value
) {
540 YYLTYPE loc
= const_expression
->get_location();
541 _mesa_glsl_error(&loc
, state
, "%s layout qualifier is invalid "
542 "(%d < %d)", qual_indentifier
,
543 const_int
->value
.i
[0], min_value
);
547 if (!first_pass
&& *value
!= const_int
->value
.u
[0]) {
548 YYLTYPE loc
= const_expression
->get_location();
549 _mesa_glsl_error(&loc
, state
, "%s layout qualifier does not "
550 "match previous declaration (%d vs %d)",
551 qual_indentifier
, *value
, const_int
->value
.i
[0]);
555 *value
= const_int
->value
.u
[0];
558 /* If the location is const (and we've verified that
559 * it is) then no instructions should have been emitted
560 * when we converted it to HIR. If they were emitted,
561 * then either the location isn't const after all, or
562 * we are emitting unnecessary instructions.
564 assert(dummy_instructions
.is_empty());
571 process_qualifier_constant(struct _mesa_glsl_parse_state
*state
,
573 const char *qual_indentifier
,
574 ast_expression
*const_expression
,
577 exec_list dummy_instructions
;
579 if (const_expression
== NULL
) {
584 ir_rvalue
*const ir
= const_expression
->hir(&dummy_instructions
, state
);
586 ir_constant
*const const_int
= ir
->constant_expression_value();
587 if (const_int
== NULL
|| !const_int
->type
->is_integer()) {
588 _mesa_glsl_error(loc
, state
, "%s must be an integral constant "
589 "expression", qual_indentifier
);
593 if (const_int
->value
.i
[0] < 0) {
594 _mesa_glsl_error(loc
, state
, "%s layout qualifier is invalid (%d < 0)",
595 qual_indentifier
, const_int
->value
.u
[0]);
599 /* If the location is const (and we've verified that
600 * it is) then no instructions should have been emitted
601 * when we converted it to HIR. If they were emitted,
602 * then either the location isn't const after all, or
603 * we are emitting unnecessary instructions.
605 assert(dummy_instructions
.is_empty());
607 *value
= const_int
->value
.u
[0];