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_location
77 || this->flags
.q
.explicit_image_format
78 || this->flags
.q
.explicit_index
79 || this->flags
.q
.explicit_binding
80 || this->flags
.q
.explicit_offset
81 || this->flags
.q
.explicit_stream
;
85 ast_type_qualifier::has_storage() const
87 return this->flags
.q
.constant
88 || this->flags
.q
.attribute
89 || this->flags
.q
.varying
92 || this->flags
.q
.uniform
93 || this->flags
.q
.buffer
94 || this->flags
.q
.shared_storage
;
98 ast_type_qualifier::has_auxiliary_storage() const
100 return this->flags
.q
.centroid
101 || this->flags
.q
.sample
102 || this->flags
.q
.patch
;
106 * This function merges both duplicate identifies within a single layout and
107 * multiple layout qualifiers on a single variable declaration. The
108 * is_single_layout_merge param is used differentiate between the two.
111 ast_type_qualifier::merge_qualifier(YYLTYPE
*loc
,
112 _mesa_glsl_parse_state
*state
,
113 const ast_type_qualifier
&q
,
114 bool is_single_layout_merge
)
116 ast_type_qualifier ubo_mat_mask
;
117 ubo_mat_mask
.flags
.i
= 0;
118 ubo_mat_mask
.flags
.q
.row_major
= 1;
119 ubo_mat_mask
.flags
.q
.column_major
= 1;
121 ast_type_qualifier ubo_layout_mask
;
122 ubo_layout_mask
.flags
.i
= 0;
123 ubo_layout_mask
.flags
.q
.std140
= 1;
124 ubo_layout_mask
.flags
.q
.packed
= 1;
125 ubo_layout_mask
.flags
.q
.shared
= 1;
126 ubo_layout_mask
.flags
.q
.std430
= 1;
128 ast_type_qualifier ubo_binding_mask
;
129 ubo_binding_mask
.flags
.i
= 0;
130 ubo_binding_mask
.flags
.q
.explicit_binding
= 1;
131 ubo_binding_mask
.flags
.q
.explicit_offset
= 1;
133 ast_type_qualifier stream_layout_mask
;
134 stream_layout_mask
.flags
.i
= 0;
135 stream_layout_mask
.flags
.q
.stream
= 1;
137 /* FIXME: We should probably do interface and function param validation
140 ast_type_qualifier input_layout_mask
;
141 input_layout_mask
.flags
.i
= 0;
142 input_layout_mask
.flags
.q
.centroid
= 1;
143 /* Function params can have constant */
144 input_layout_mask
.flags
.q
.constant
= 1;
145 input_layout_mask
.flags
.q
.explicit_location
= 1;
146 input_layout_mask
.flags
.q
.flat
= 1;
147 input_layout_mask
.flags
.q
.in
= 1;
148 input_layout_mask
.flags
.q
.invariant
= 1;
149 input_layout_mask
.flags
.q
.noperspective
= 1;
150 input_layout_mask
.flags
.q
.origin_upper_left
= 1;
151 /* Function params 'inout' will set this */
152 input_layout_mask
.flags
.q
.out
= 1;
153 input_layout_mask
.flags
.q
.patch
= 1;
154 input_layout_mask
.flags
.q
.pixel_center_integer
= 1;
155 input_layout_mask
.flags
.q
.precise
= 1;
156 input_layout_mask
.flags
.q
.sample
= 1;
157 input_layout_mask
.flags
.q
.smooth
= 1;
159 /* Uniform block layout qualifiers get to overwrite each
160 * other (rightmost having priority), while all other
161 * qualifiers currently don't allow duplicates.
163 ast_type_qualifier allowed_duplicates_mask
;
164 allowed_duplicates_mask
.flags
.i
=
165 ubo_mat_mask
.flags
.i
|
166 ubo_layout_mask
.flags
.i
|
167 ubo_binding_mask
.flags
.i
;
169 /* Geometry shaders can have several layout qualifiers
170 * assigning different stream values.
172 if (state
->stage
== MESA_SHADER_GEOMETRY
)
173 allowed_duplicates_mask
.flags
.i
|=
174 stream_layout_mask
.flags
.i
;
176 if (is_single_layout_merge
&& !state
->has_enhanced_layouts() &&
177 (this->flags
.i
& q
.flags
.i
& ~allowed_duplicates_mask
.flags
.i
) != 0) {
178 _mesa_glsl_error(loc
, state
,
179 "duplicate layout qualifiers used");
183 if (q
.flags
.q
.prim_type
) {
184 if (this->flags
.q
.prim_type
&& this->prim_type
!= q
.prim_type
) {
185 _mesa_glsl_error(loc
, state
,
186 "conflicting primitive type qualifiers used");
189 this->prim_type
= q
.prim_type
;
192 if (q
.flags
.q
.max_vertices
) {
193 if (this->max_vertices
) {
194 this->max_vertices
->merge_qualifier(q
.max_vertices
);
196 this->max_vertices
= q
.max_vertices
;
200 if (q
.flags
.q
.subroutine_def
) {
201 if (this->flags
.q
.subroutine_def
) {
202 _mesa_glsl_error(loc
, state
,
203 "conflicting subroutine qualifiers used");
205 this->subroutine_list
= q
.subroutine_list
;
209 if (q
.flags
.q
.invocations
) {
210 if (this->invocations
) {
211 this->invocations
->merge_qualifier(q
.invocations
);
213 this->invocations
= q
.invocations
;
217 if (state
->stage
== MESA_SHADER_GEOMETRY
&&
218 state
->has_explicit_attrib_stream()) {
219 if (!this->flags
.q
.explicit_stream
) {
220 if (q
.flags
.q
.stream
) {
221 this->flags
.q
.stream
= 1;
222 this->stream
= q
.stream
;
223 } else if (!this->flags
.q
.stream
&& this->flags
.q
.out
) {
224 /* Assign default global stream value */
225 this->flags
.q
.stream
= 1;
226 this->stream
= state
->out_qualifier
->stream
;
231 if (q
.flags
.q
.vertices
) {
232 if (this->vertices
) {
233 this->vertices
->merge_qualifier(q
.vertices
);
235 this->vertices
= q
.vertices
;
239 if (q
.flags
.q
.vertex_spacing
) {
240 if (this->flags
.q
.vertex_spacing
&& this->vertex_spacing
!= q
.vertex_spacing
) {
241 _mesa_glsl_error(loc
, state
,
242 "conflicting vertex spacing used");
245 this->vertex_spacing
= q
.vertex_spacing
;
248 if (q
.flags
.q
.ordering
) {
249 if (this->flags
.q
.ordering
&& this->ordering
!= q
.ordering
) {
250 _mesa_glsl_error(loc
, state
,
251 "conflicting ordering used");
254 this->ordering
= q
.ordering
;
257 if (q
.flags
.q
.point_mode
) {
258 if (this->flags
.q
.point_mode
&& this->point_mode
!= q
.point_mode
) {
259 _mesa_glsl_error(loc
, state
,
260 "conflicting point mode used");
263 this->point_mode
= q
.point_mode
;
266 if ((q
.flags
.i
& ubo_mat_mask
.flags
.i
) != 0)
267 this->flags
.i
&= ~ubo_mat_mask
.flags
.i
;
268 if ((q
.flags
.i
& ubo_layout_mask
.flags
.i
) != 0)
269 this->flags
.i
&= ~ubo_layout_mask
.flags
.i
;
271 for (int i
= 0; i
< 3; i
++) {
272 if (q
.flags
.q
.local_size
& (1 << i
)) {
273 if (this->local_size
[i
]) {
274 this->local_size
[i
]->merge_qualifier(q
.local_size
[i
]);
276 this->local_size
[i
] = q
.local_size
[i
];
281 this->flags
.i
|= q
.flags
.i
;
283 if (this->flags
.q
.in
&&
284 (this->flags
.i
& ~input_layout_mask
.flags
.i
) != 0) {
285 _mesa_glsl_error(loc
, state
,
286 "invalid input layout qualifier used");
290 if (q
.flags
.q
.explicit_location
)
291 this->location
= q
.location
;
293 if (q
.flags
.q
.explicit_index
)
294 this->index
= q
.index
;
296 if (q
.flags
.q
.explicit_binding
)
297 this->binding
= q
.binding
;
299 if (q
.flags
.q
.explicit_offset
)
300 this->offset
= q
.offset
;
302 if (q
.precision
!= ast_precision_none
)
303 this->precision
= q
.precision
;
305 if (q
.flags
.q
.explicit_image_format
) {
306 this->image_format
= q
.image_format
;
307 this->image_base_type
= q
.image_base_type
;
314 ast_type_qualifier::merge_out_qualifier(YYLTYPE
*loc
,
315 _mesa_glsl_parse_state
*state
,
316 const ast_type_qualifier
&q
,
317 ast_node
* &node
, bool create_node
)
319 void *mem_ctx
= state
;
320 const bool r
= this->merge_qualifier(loc
, state
, q
, false);
322 if (state
->stage
== MESA_SHADER_GEOMETRY
) {
323 if (q
.flags
.q
.prim_type
) {
324 /* Make sure this is a valid output primitive type. */
325 switch (q
.prim_type
) {
328 case GL_TRIANGLE_STRIP
:
331 _mesa_glsl_error(loc
, state
, "invalid geometry shader output "
337 /* Allow future assigments of global out's stream id value */
338 this->flags
.q
.explicit_stream
= 0;
339 } else if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
341 node
= new(mem_ctx
) ast_tcs_output_layout(*loc
);
344 _mesa_glsl_error(loc
, state
, "out layout qualifiers only valid in "
345 "tessellation control or geometry shaders");
352 ast_type_qualifier::merge_in_qualifier(YYLTYPE
*loc
,
353 _mesa_glsl_parse_state
*state
,
354 const ast_type_qualifier
&q
,
355 ast_node
* &node
, bool create_node
)
357 void *mem_ctx
= state
;
358 bool create_gs_ast
= false;
359 bool create_cs_ast
= false;
360 ast_type_qualifier valid_in_mask
;
361 valid_in_mask
.flags
.i
= 0;
363 switch (state
->stage
) {
364 case MESA_SHADER_TESS_EVAL
:
365 if (q
.flags
.q
.prim_type
) {
366 /* Make sure this is a valid input primitive type. */
367 switch (q
.prim_type
) {
373 _mesa_glsl_error(loc
, state
,
374 "invalid tessellation evaluation "
375 "shader input primitive type");
380 valid_in_mask
.flags
.q
.prim_type
= 1;
381 valid_in_mask
.flags
.q
.vertex_spacing
= 1;
382 valid_in_mask
.flags
.q
.ordering
= 1;
383 valid_in_mask
.flags
.q
.point_mode
= 1;
385 case MESA_SHADER_GEOMETRY
:
386 if (q
.flags
.q
.prim_type
) {
387 /* Make sure this is a valid input primitive type. */
388 switch (q
.prim_type
) {
391 case GL_LINES_ADJACENCY
:
393 case GL_TRIANGLES_ADJACENCY
:
396 _mesa_glsl_error(loc
, state
,
397 "invalid geometry shader input primitive type");
403 q
.flags
.q
.prim_type
&&
404 !state
->in_qualifier
->flags
.q
.prim_type
;
406 valid_in_mask
.flags
.q
.prim_type
= 1;
407 valid_in_mask
.flags
.q
.invocations
= 1;
409 case MESA_SHADER_FRAGMENT
:
410 valid_in_mask
.flags
.q
.early_fragment_tests
= 1;
412 case MESA_SHADER_COMPUTE
:
414 q
.flags
.q
.local_size
!= 0 &&
415 state
->in_qualifier
->flags
.q
.local_size
== 0;
417 valid_in_mask
.flags
.q
.local_size
= 7;
420 _mesa_glsl_error(loc
, state
,
421 "input layout qualifiers only valid in "
422 "geometry, fragment and compute shaders");
426 /* Generate an error when invalid input layout qualifiers are used. */
427 if ((q
.flags
.i
& ~valid_in_mask
.flags
.i
) != 0) {
428 _mesa_glsl_error(loc
, state
,
429 "invalid input layout qualifiers used");
433 /* Input layout qualifiers can be specified multiple
434 * times in separate declarations, as long as they match.
436 if (this->flags
.q
.prim_type
) {
437 if (q
.flags
.q
.prim_type
&&
438 this->prim_type
!= q
.prim_type
) {
439 _mesa_glsl_error(loc
, state
,
440 "conflicting input primitive %s specified",
441 state
->stage
== MESA_SHADER_GEOMETRY
?
444 } else if (q
.flags
.q
.prim_type
) {
445 state
->in_qualifier
->flags
.q
.prim_type
= 1;
446 state
->in_qualifier
->prim_type
= q
.prim_type
;
449 if (q
.flags
.q
.invocations
) {
450 this->flags
.q
.invocations
= 1;
451 if (this->invocations
) {
452 this->invocations
->merge_qualifier(q
.invocations
);
454 this->invocations
= q
.invocations
;
458 if (q
.flags
.q
.early_fragment_tests
) {
459 state
->fs_early_fragment_tests
= true;
462 if (this->flags
.q
.vertex_spacing
) {
463 if (q
.flags
.q
.vertex_spacing
&&
464 this->vertex_spacing
!= q
.vertex_spacing
) {
465 _mesa_glsl_error(loc
, state
,
466 "conflicting vertex spacing specified");
468 } else if (q
.flags
.q
.vertex_spacing
) {
469 this->flags
.q
.vertex_spacing
= 1;
470 this->vertex_spacing
= q
.vertex_spacing
;
473 if (this->flags
.q
.ordering
) {
474 if (q
.flags
.q
.ordering
&&
475 this->ordering
!= q
.ordering
) {
476 _mesa_glsl_error(loc
, state
,
477 "conflicting ordering specified");
479 } else if (q
.flags
.q
.ordering
) {
480 this->flags
.q
.ordering
= 1;
481 this->ordering
= q
.ordering
;
484 if (this->flags
.q
.point_mode
) {
485 if (q
.flags
.q
.point_mode
&&
486 this->point_mode
!= q
.point_mode
) {
487 _mesa_glsl_error(loc
, state
,
488 "conflicting point mode specified");
490 } else if (q
.flags
.q
.point_mode
) {
491 this->flags
.q
.point_mode
= 1;
492 this->point_mode
= q
.point_mode
;
497 node
= new(mem_ctx
) ast_gs_input_layout(*loc
, q
.prim_type
);
498 } else if (create_cs_ast
) {
499 node
= new(mem_ctx
) ast_cs_input_layout(*loc
, q
.local_size
);
507 ast_layout_expression::process_qualifier_constant(struct _mesa_glsl_parse_state
*state
,
508 const char *qual_indentifier
,
513 bool first_pass
= true;
519 for (exec_node
*node
= layout_const_expressions
.head
;
520 !node
->is_tail_sentinel(); node
= node
->next
) {
522 exec_list dummy_instructions
;
523 ast_node
*const_expression
= exec_node_data(ast_node
, node
, link
);
525 ir_rvalue
*const ir
= const_expression
->hir(&dummy_instructions
, state
);
527 ir_constant
*const const_int
= ir
->constant_expression_value();
528 if (const_int
== NULL
|| !const_int
->type
->is_integer()) {
529 YYLTYPE loc
= const_expression
->get_location();
530 _mesa_glsl_error(&loc
, state
, "%s must be an integral constant "
531 "expression", qual_indentifier
);
535 if (const_int
->value
.i
[0] < min_value
) {
536 YYLTYPE loc
= const_expression
->get_location();
537 _mesa_glsl_error(&loc
, state
, "%s layout qualifier is invalid "
538 "(%d < %d)", qual_indentifier
,
539 const_int
->value
.i
[0], min_value
);
543 if (!first_pass
&& *value
!= const_int
->value
.u
[0]) {
544 YYLTYPE loc
= const_expression
->get_location();
545 _mesa_glsl_error(&loc
, state
, "%s layout qualifier does not "
546 "match previous declaration (%d vs %d)",
547 qual_indentifier
, *value
, const_int
->value
.i
[0]);
551 *value
= const_int
->value
.u
[0];
554 /* If the location is const (and we've verified that
555 * it is) then no instructions should have been emitted
556 * when we converted it to HIR. If they were emitted,
557 * then either the location isn't const after all, or
558 * we are emitting unnecessary instructions.
560 assert(dummy_instructions
.is_empty());