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 (state
->has_enhanced_layouts()) {
233 if (!this->flags
.q
.explicit_xfb_buffer
) {
234 if (q
.flags
.q
.xfb_buffer
) {
235 this->flags
.q
.xfb_buffer
= 1;
236 this->xfb_buffer
= q
.xfb_buffer
;
237 } else if (!this->flags
.q
.xfb_buffer
&& this->flags
.q
.out
) {
238 /* Assign global xfb_buffer value */
239 this->flags
.q
.xfb_buffer
= 1;
240 this->xfb_buffer
= state
->out_qualifier
->xfb_buffer
;
244 if (q
.flags
.q
.explicit_xfb_stride
)
245 this->xfb_stride
= q
.xfb_stride
;
247 /* Merge all we xfb_stride qualifiers into the global out */
248 if (q
.flags
.q
.explicit_xfb_stride
|| this->flags
.q
.xfb_stride
) {
250 /* Set xfb_stride flag to 0 to avoid adding duplicates every time
253 this->flags
.q
.xfb_stride
= 0;
256 if (process_qualifier_constant(state
, loc
, "xfb_buffer",
257 this->xfb_buffer
, &buff_idx
)) {
258 if (state
->out_qualifier
->out_xfb_stride
[buff_idx
]) {
259 state
->out_qualifier
->out_xfb_stride
[buff_idx
]->merge_qualifier(
260 new(state
) ast_layout_expression(*loc
, this->xfb_stride
));
262 state
->out_qualifier
->out_xfb_stride
[buff_idx
] =
263 new(state
) ast_layout_expression(*loc
, this->xfb_stride
);
269 if (q
.flags
.q
.vertices
) {
270 if (this->vertices
) {
271 this->vertices
->merge_qualifier(q
.vertices
);
273 this->vertices
= q
.vertices
;
277 if (q
.flags
.q
.vertex_spacing
) {
278 if (this->flags
.q
.vertex_spacing
&& this->vertex_spacing
!= q
.vertex_spacing
) {
279 _mesa_glsl_error(loc
, state
,
280 "conflicting vertex spacing used");
283 this->vertex_spacing
= q
.vertex_spacing
;
286 if (q
.flags
.q
.ordering
) {
287 if (this->flags
.q
.ordering
&& this->ordering
!= q
.ordering
) {
288 _mesa_glsl_error(loc
, state
,
289 "conflicting ordering used");
292 this->ordering
= q
.ordering
;
295 if (q
.flags
.q
.point_mode
) {
296 if (this->flags
.q
.point_mode
&& this->point_mode
!= q
.point_mode
) {
297 _mesa_glsl_error(loc
, state
,
298 "conflicting point mode used");
301 this->point_mode
= q
.point_mode
;
304 if ((q
.flags
.i
& ubo_mat_mask
.flags
.i
) != 0)
305 this->flags
.i
&= ~ubo_mat_mask
.flags
.i
;
306 if ((q
.flags
.i
& ubo_layout_mask
.flags
.i
) != 0)
307 this->flags
.i
&= ~ubo_layout_mask
.flags
.i
;
309 for (int i
= 0; i
< 3; i
++) {
310 if (q
.flags
.q
.local_size
& (1 << i
)) {
311 if (this->local_size
[i
]) {
312 this->local_size
[i
]->merge_qualifier(q
.local_size
[i
]);
314 this->local_size
[i
] = q
.local_size
[i
];
319 this->flags
.i
|= q
.flags
.i
;
321 if (this->flags
.q
.in
&&
322 (this->flags
.i
& ~input_layout_mask
.flags
.i
) != 0) {
323 _mesa_glsl_error(loc
, state
,
324 "invalid input layout qualifier used");
328 if (q
.flags
.q
.explicit_align
)
329 this->align
= q
.align
;
331 if (q
.flags
.q
.explicit_location
)
332 this->location
= q
.location
;
334 if (q
.flags
.q
.explicit_index
)
335 this->index
= q
.index
;
337 if (q
.flags
.q
.explicit_binding
)
338 this->binding
= q
.binding
;
340 if (q
.flags
.q
.explicit_offset
|| q
.flags
.q
.explicit_xfb_offset
)
341 this->offset
= q
.offset
;
343 if (q
.precision
!= ast_precision_none
)
344 this->precision
= q
.precision
;
346 if (q
.flags
.q
.explicit_image_format
) {
347 this->image_format
= q
.image_format
;
348 this->image_base_type
= q
.image_base_type
;
355 ast_type_qualifier::merge_out_qualifier(YYLTYPE
*loc
,
356 _mesa_glsl_parse_state
*state
,
357 const ast_type_qualifier
&q
,
358 ast_node
* &node
, bool create_node
)
360 void *mem_ctx
= state
;
361 const bool r
= this->merge_qualifier(loc
, state
, q
, false);
363 if (state
->stage
== MESA_SHADER_GEOMETRY
) {
364 if (q
.flags
.q
.prim_type
) {
365 /* Make sure this is a valid output primitive type. */
366 switch (q
.prim_type
) {
369 case GL_TRIANGLE_STRIP
:
372 _mesa_glsl_error(loc
, state
, "invalid geometry shader output "
378 /* Allow future assigments of global out's stream id value */
379 this->flags
.q
.explicit_stream
= 0;
380 } else if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
382 node
= new(mem_ctx
) ast_tcs_output_layout(*loc
);
385 _mesa_glsl_error(loc
, state
, "out layout qualifiers only valid in "
386 "tessellation control or geometry shaders");
389 /* Allow future assigments of global out's */
390 this->flags
.q
.explicit_xfb_buffer
= 0;
391 this->flags
.q
.explicit_xfb_stride
= 0;
397 ast_type_qualifier::merge_in_qualifier(YYLTYPE
*loc
,
398 _mesa_glsl_parse_state
*state
,
399 const ast_type_qualifier
&q
,
400 ast_node
* &node
, bool create_node
)
402 void *mem_ctx
= state
;
403 bool create_gs_ast
= false;
404 bool create_cs_ast
= false;
405 ast_type_qualifier valid_in_mask
;
406 valid_in_mask
.flags
.i
= 0;
408 switch (state
->stage
) {
409 case MESA_SHADER_TESS_EVAL
:
410 if (q
.flags
.q
.prim_type
) {
411 /* Make sure this is a valid input primitive type. */
412 switch (q
.prim_type
) {
418 _mesa_glsl_error(loc
, state
,
419 "invalid tessellation evaluation "
420 "shader input primitive type");
425 valid_in_mask
.flags
.q
.prim_type
= 1;
426 valid_in_mask
.flags
.q
.vertex_spacing
= 1;
427 valid_in_mask
.flags
.q
.ordering
= 1;
428 valid_in_mask
.flags
.q
.point_mode
= 1;
430 case MESA_SHADER_GEOMETRY
:
431 if (q
.flags
.q
.prim_type
) {
432 /* Make sure this is a valid input primitive type. */
433 switch (q
.prim_type
) {
436 case GL_LINES_ADJACENCY
:
438 case GL_TRIANGLES_ADJACENCY
:
441 _mesa_glsl_error(loc
, state
,
442 "invalid geometry shader input primitive type");
448 q
.flags
.q
.prim_type
&&
449 !state
->in_qualifier
->flags
.q
.prim_type
;
451 valid_in_mask
.flags
.q
.prim_type
= 1;
452 valid_in_mask
.flags
.q
.invocations
= 1;
454 case MESA_SHADER_FRAGMENT
:
455 valid_in_mask
.flags
.q
.early_fragment_tests
= 1;
457 case MESA_SHADER_COMPUTE
:
459 q
.flags
.q
.local_size
!= 0 &&
460 state
->in_qualifier
->flags
.q
.local_size
== 0;
462 valid_in_mask
.flags
.q
.local_size
= 7;
465 _mesa_glsl_error(loc
, state
,
466 "input layout qualifiers only valid in "
467 "geometry, fragment and compute shaders");
471 /* Generate an error when invalid input layout qualifiers are used. */
472 if ((q
.flags
.i
& ~valid_in_mask
.flags
.i
) != 0) {
473 _mesa_glsl_error(loc
, state
,
474 "invalid input layout qualifiers used");
478 /* Input layout qualifiers can be specified multiple
479 * times in separate declarations, as long as they match.
481 if (this->flags
.q
.prim_type
) {
482 if (q
.flags
.q
.prim_type
&&
483 this->prim_type
!= q
.prim_type
) {
484 _mesa_glsl_error(loc
, state
,
485 "conflicting input primitive %s specified",
486 state
->stage
== MESA_SHADER_GEOMETRY
?
489 } else if (q
.flags
.q
.prim_type
) {
490 state
->in_qualifier
->flags
.q
.prim_type
= 1;
491 state
->in_qualifier
->prim_type
= q
.prim_type
;
494 if (q
.flags
.q
.invocations
) {
495 this->flags
.q
.invocations
= 1;
496 if (this->invocations
) {
497 this->invocations
->merge_qualifier(q
.invocations
);
499 this->invocations
= q
.invocations
;
503 if (q
.flags
.q
.early_fragment_tests
) {
504 state
->fs_early_fragment_tests
= true;
507 if (this->flags
.q
.vertex_spacing
) {
508 if (q
.flags
.q
.vertex_spacing
&&
509 this->vertex_spacing
!= q
.vertex_spacing
) {
510 _mesa_glsl_error(loc
, state
,
511 "conflicting vertex spacing specified");
513 } else if (q
.flags
.q
.vertex_spacing
) {
514 this->flags
.q
.vertex_spacing
= 1;
515 this->vertex_spacing
= q
.vertex_spacing
;
518 if (this->flags
.q
.ordering
) {
519 if (q
.flags
.q
.ordering
&&
520 this->ordering
!= q
.ordering
) {
521 _mesa_glsl_error(loc
, state
,
522 "conflicting ordering specified");
524 } else if (q
.flags
.q
.ordering
) {
525 this->flags
.q
.ordering
= 1;
526 this->ordering
= q
.ordering
;
529 if (this->flags
.q
.point_mode
) {
530 if (q
.flags
.q
.point_mode
&&
531 this->point_mode
!= q
.point_mode
) {
532 _mesa_glsl_error(loc
, state
,
533 "conflicting point mode specified");
535 } else if (q
.flags
.q
.point_mode
) {
536 this->flags
.q
.point_mode
= 1;
537 this->point_mode
= q
.point_mode
;
542 node
= new(mem_ctx
) ast_gs_input_layout(*loc
, q
.prim_type
);
543 } else if (create_cs_ast
) {
544 node
= new(mem_ctx
) ast_cs_input_layout(*loc
, q
.local_size
);
552 ast_layout_expression::process_qualifier_constant(struct _mesa_glsl_parse_state
*state
,
553 const char *qual_indentifier
,
558 bool first_pass
= true;
564 for (exec_node
*node
= layout_const_expressions
.head
;
565 !node
->is_tail_sentinel(); node
= node
->next
) {
567 exec_list dummy_instructions
;
568 ast_node
*const_expression
= exec_node_data(ast_node
, node
, link
);
570 ir_rvalue
*const ir
= const_expression
->hir(&dummy_instructions
, state
);
572 ir_constant
*const const_int
= ir
->constant_expression_value();
573 if (const_int
== NULL
|| !const_int
->type
->is_integer()) {
574 YYLTYPE loc
= const_expression
->get_location();
575 _mesa_glsl_error(&loc
, state
, "%s must be an integral constant "
576 "expression", qual_indentifier
);
580 if (const_int
->value
.i
[0] < min_value
) {
581 YYLTYPE loc
= const_expression
->get_location();
582 _mesa_glsl_error(&loc
, state
, "%s layout qualifier is invalid "
583 "(%d < %d)", qual_indentifier
,
584 const_int
->value
.i
[0], min_value
);
588 if (!first_pass
&& *value
!= const_int
->value
.u
[0]) {
589 YYLTYPE loc
= const_expression
->get_location();
590 _mesa_glsl_error(&loc
, state
, "%s layout qualifier does not "
591 "match previous declaration (%d vs %d)",
592 qual_indentifier
, *value
, const_int
->value
.i
[0]);
596 *value
= 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());
612 process_qualifier_constant(struct _mesa_glsl_parse_state
*state
,
614 const char *qual_indentifier
,
615 ast_expression
*const_expression
,
618 exec_list dummy_instructions
;
620 if (const_expression
== NULL
) {
625 ir_rvalue
*const ir
= const_expression
->hir(&dummy_instructions
, state
);
627 ir_constant
*const const_int
= ir
->constant_expression_value();
628 if (const_int
== NULL
|| !const_int
->type
->is_integer()) {
629 _mesa_glsl_error(loc
, state
, "%s must be an integral constant "
630 "expression", qual_indentifier
);
634 if (const_int
->value
.i
[0] < 0) {
635 _mesa_glsl_error(loc
, state
, "%s layout qualifier is invalid (%d < 0)",
636 qual_indentifier
, const_int
->value
.u
[0]);
640 /* If the location is const (and we've verified that
641 * it is) then no instructions should have been emitted
642 * when we converted it to HIR. If they were emitted,
643 * then either the location isn't const after all, or
644 * we are emitting unnecessary instructions.
646 assert(dummy_instructions
.is_empty());
648 *value
= const_int
->value
.u
[0];