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_index
78 || this->flags
.q
.explicit_binding
79 || this->flags
.q
.explicit_offset
;
83 ast_type_qualifier::has_storage() const
85 return this->flags
.q
.constant
86 || this->flags
.q
.attribute
87 || this->flags
.q
.varying
90 || this->flags
.q
.uniform
91 || this->flags
.q
.buffer
92 || this->flags
.q
.shared_storage
;
96 ast_type_qualifier::has_auxiliary_storage() const
98 return this->flags
.q
.centroid
99 || this->flags
.q
.sample
100 || this->flags
.q
.patch
;
104 ast_type_qualifier::interpolation_string() const
106 if (this->flags
.q
.smooth
)
108 else if (this->flags
.q
.flat
)
110 else if (this->flags
.q
.noperspective
)
111 return "noperspective";
117 ast_type_qualifier::merge_qualifier(YYLTYPE
*loc
,
118 _mesa_glsl_parse_state
*state
,
119 ast_type_qualifier q
)
121 ast_type_qualifier ubo_mat_mask
;
122 ubo_mat_mask
.flags
.i
= 0;
123 ubo_mat_mask
.flags
.q
.row_major
= 1;
124 ubo_mat_mask
.flags
.q
.column_major
= 1;
126 ast_type_qualifier ubo_layout_mask
;
127 ubo_layout_mask
.flags
.i
= 0;
128 ubo_layout_mask
.flags
.q
.std140
= 1;
129 ubo_layout_mask
.flags
.q
.packed
= 1;
130 ubo_layout_mask
.flags
.q
.shared
= 1;
131 ubo_layout_mask
.flags
.q
.std430
= 1;
133 ast_type_qualifier ubo_binding_mask
;
134 ubo_binding_mask
.flags
.i
= 0;
135 ubo_binding_mask
.flags
.q
.explicit_binding
= 1;
136 ubo_binding_mask
.flags
.q
.explicit_offset
= 1;
138 ast_type_qualifier stream_layout_mask
;
139 stream_layout_mask
.flags
.i
= 0;
140 stream_layout_mask
.flags
.q
.stream
= 1;
142 /* Uniform block layout qualifiers get to overwrite each
143 * other (rightmost having priority), while all other
144 * qualifiers currently don't allow duplicates.
146 ast_type_qualifier allowed_duplicates_mask
;
147 allowed_duplicates_mask
.flags
.i
=
148 ubo_mat_mask
.flags
.i
|
149 ubo_layout_mask
.flags
.i
|
150 ubo_binding_mask
.flags
.i
;
152 /* Geometry shaders can have several layout qualifiers
153 * assigning different stream values.
155 if (state
->stage
== MESA_SHADER_GEOMETRY
)
156 allowed_duplicates_mask
.flags
.i
|=
157 stream_layout_mask
.flags
.i
;
159 if ((this->flags
.i
& q
.flags
.i
& ~allowed_duplicates_mask
.flags
.i
) != 0) {
160 _mesa_glsl_error(loc
, state
,
161 "duplicate layout qualifiers used");
165 if (q
.flags
.q
.prim_type
) {
166 if (this->flags
.q
.prim_type
&& this->prim_type
!= q
.prim_type
) {
167 _mesa_glsl_error(loc
, state
,
168 "conflicting primitive type qualifiers used");
171 this->prim_type
= q
.prim_type
;
174 if (q
.flags
.q
.max_vertices
) {
175 if (this->max_vertices
) {
176 this->max_vertices
->merge_qualifier(q
.max_vertices
);
178 this->max_vertices
= q
.max_vertices
;
182 if (q
.flags
.q
.subroutine_def
) {
183 if (this->flags
.q
.subroutine_def
) {
184 _mesa_glsl_error(loc
, state
,
185 "conflicting subroutine qualifiers used");
187 this->subroutine_list
= q
.subroutine_list
;
191 if (q
.flags
.q
.invocations
) {
192 if (this->invocations
) {
193 this->invocations
->merge_qualifier(q
.invocations
);
195 this->invocations
= q
.invocations
;
199 if (state
->stage
== MESA_SHADER_GEOMETRY
&&
200 state
->has_explicit_attrib_stream()) {
201 if (!this->flags
.q
.explicit_stream
) {
202 if (q
.flags
.q
.stream
) {
203 this->flags
.q
.stream
= 1;
204 this->stream
= q
.stream
;
205 } else if (!this->flags
.q
.stream
&& this->flags
.q
.out
) {
206 /* Assign default global stream value */
207 this->flags
.q
.stream
= 1;
208 this->stream
= state
->out_qualifier
->stream
;
211 if (q
.flags
.q
.explicit_stream
) {
212 _mesa_glsl_error(loc
, state
,
213 "duplicate layout `stream' qualifier");
218 if (q
.flags
.q
.vertices
) {
219 if (this->vertices
) {
220 this->vertices
->merge_qualifier(q
.vertices
);
222 this->vertices
= q
.vertices
;
226 if (q
.flags
.q
.vertex_spacing
) {
227 if (this->flags
.q
.vertex_spacing
&& this->vertex_spacing
!= q
.vertex_spacing
) {
228 _mesa_glsl_error(loc
, state
,
229 "conflicting vertex spacing used");
232 this->vertex_spacing
= q
.vertex_spacing
;
235 if (q
.flags
.q
.ordering
) {
236 if (this->flags
.q
.ordering
&& this->ordering
!= q
.ordering
) {
237 _mesa_glsl_error(loc
, state
,
238 "conflicting ordering used");
241 this->ordering
= q
.ordering
;
244 if (q
.flags
.q
.point_mode
) {
245 if (this->flags
.q
.point_mode
&& this->point_mode
!= q
.point_mode
) {
246 _mesa_glsl_error(loc
, state
,
247 "conflicting point mode used");
250 this->point_mode
= q
.point_mode
;
253 if ((q
.flags
.i
& ubo_mat_mask
.flags
.i
) != 0)
254 this->flags
.i
&= ~ubo_mat_mask
.flags
.i
;
255 if ((q
.flags
.i
& ubo_layout_mask
.flags
.i
) != 0)
256 this->flags
.i
&= ~ubo_layout_mask
.flags
.i
;
258 for (int i
= 0; i
< 3; i
++) {
259 if (q
.flags
.q
.local_size
& (1 << i
)) {
260 if (this->local_size
[i
]) {
261 this->local_size
[i
]->merge_qualifier(q
.local_size
[i
]);
263 this->local_size
[i
] = q
.local_size
[i
];
268 this->flags
.i
|= q
.flags
.i
;
270 if (q
.flags
.q
.explicit_location
)
271 this->location
= q
.location
;
273 if (q
.flags
.q
.explicit_index
)
274 this->index
= q
.index
;
276 if (q
.flags
.q
.explicit_binding
)
277 this->binding
= q
.binding
;
279 if (q
.flags
.q
.explicit_offset
)
280 this->offset
= q
.offset
;
282 if (q
.precision
!= ast_precision_none
)
283 this->precision
= q
.precision
;
285 if (q
.flags
.q
.explicit_image_format
) {
286 this->image_format
= q
.image_format
;
287 this->image_base_type
= q
.image_base_type
;
294 ast_type_qualifier::merge_out_qualifier(YYLTYPE
*loc
,
295 _mesa_glsl_parse_state
*state
,
296 ast_type_qualifier q
,
299 void *mem_ctx
= state
;
300 const bool r
= this->merge_qualifier(loc
, state
, q
);
302 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
303 node
= new(mem_ctx
) ast_tcs_output_layout(*loc
);
310 ast_type_qualifier::merge_in_qualifier(YYLTYPE
*loc
,
311 _mesa_glsl_parse_state
*state
,
312 ast_type_qualifier q
,
315 void *mem_ctx
= state
;
316 bool create_gs_ast
= false;
317 bool create_cs_ast
= false;
318 ast_type_qualifier valid_in_mask
;
319 valid_in_mask
.flags
.i
= 0;
321 switch (state
->stage
) {
322 case MESA_SHADER_TESS_EVAL
:
323 if (q
.flags
.q
.prim_type
) {
324 /* Make sure this is a valid input primitive type. */
325 switch (q
.prim_type
) {
331 _mesa_glsl_error(loc
, state
,
332 "invalid tessellation evaluation "
333 "shader input primitive type");
338 valid_in_mask
.flags
.q
.prim_type
= 1;
339 valid_in_mask
.flags
.q
.vertex_spacing
= 1;
340 valid_in_mask
.flags
.q
.ordering
= 1;
341 valid_in_mask
.flags
.q
.point_mode
= 1;
343 case MESA_SHADER_GEOMETRY
:
344 if (q
.flags
.q
.prim_type
) {
345 /* Make sure this is a valid input primitive type. */
346 switch (q
.prim_type
) {
349 case GL_LINES_ADJACENCY
:
351 case GL_TRIANGLES_ADJACENCY
:
354 _mesa_glsl_error(loc
, state
,
355 "invalid geometry shader input primitive type");
361 q
.flags
.q
.prim_type
&&
362 !state
->in_qualifier
->flags
.q
.prim_type
;
364 valid_in_mask
.flags
.q
.prim_type
= 1;
365 valid_in_mask
.flags
.q
.invocations
= 1;
367 case MESA_SHADER_FRAGMENT
:
368 valid_in_mask
.flags
.q
.early_fragment_tests
= 1;
370 case MESA_SHADER_COMPUTE
:
372 q
.flags
.q
.local_size
!= 0 &&
373 state
->in_qualifier
->flags
.q
.local_size
== 0;
375 valid_in_mask
.flags
.q
.local_size
= 7;
378 _mesa_glsl_error(loc
, state
,
379 "input layout qualifiers only valid in "
380 "geometry, fragment and compute shaders");
384 /* Generate an error when invalid input layout qualifiers are used. */
385 if ((q
.flags
.i
& ~valid_in_mask
.flags
.i
) != 0) {
386 _mesa_glsl_error(loc
, state
,
387 "invalid input layout qualifiers used");
391 /* Input layout qualifiers can be specified multiple
392 * times in separate declarations, as long as they match.
394 if (this->flags
.q
.prim_type
) {
395 if (q
.flags
.q
.prim_type
&&
396 this->prim_type
!= q
.prim_type
) {
397 _mesa_glsl_error(loc
, state
,
398 "conflicting input primitive %s specified",
399 state
->stage
== MESA_SHADER_GEOMETRY
?
402 } else if (q
.flags
.q
.prim_type
) {
403 state
->in_qualifier
->flags
.q
.prim_type
= 1;
404 state
->in_qualifier
->prim_type
= q
.prim_type
;
407 if (q
.flags
.q
.invocations
) {
408 this->flags
.q
.invocations
= 1;
409 if (this->invocations
) {
410 this->invocations
->merge_qualifier(q
.invocations
);
412 this->invocations
= q
.invocations
;
416 if (q
.flags
.q
.early_fragment_tests
) {
417 state
->fs_early_fragment_tests
= true;
420 if (this->flags
.q
.vertex_spacing
) {
421 if (q
.flags
.q
.vertex_spacing
&&
422 this->vertex_spacing
!= q
.vertex_spacing
) {
423 _mesa_glsl_error(loc
, state
,
424 "conflicting vertex spacing specified");
426 } else if (q
.flags
.q
.vertex_spacing
) {
427 this->flags
.q
.vertex_spacing
= 1;
428 this->vertex_spacing
= q
.vertex_spacing
;
431 if (this->flags
.q
.ordering
) {
432 if (q
.flags
.q
.ordering
&&
433 this->ordering
!= q
.ordering
) {
434 _mesa_glsl_error(loc
, state
,
435 "conflicting ordering specified");
437 } else if (q
.flags
.q
.ordering
) {
438 this->flags
.q
.ordering
= 1;
439 this->ordering
= q
.ordering
;
442 if (this->flags
.q
.point_mode
) {
443 if (q
.flags
.q
.point_mode
&&
444 this->point_mode
!= q
.point_mode
) {
445 _mesa_glsl_error(loc
, state
,
446 "conflicting point mode specified");
448 } else if (q
.flags
.q
.point_mode
) {
449 this->flags
.q
.point_mode
= 1;
450 this->point_mode
= q
.point_mode
;
454 node
= new(mem_ctx
) ast_gs_input_layout(*loc
, q
.prim_type
);
455 } else if (create_cs_ast
) {
456 node
= new(mem_ctx
) ast_cs_input_layout(*loc
, q
.local_size
);
463 ast_layout_expression::process_qualifier_constant(struct _mesa_glsl_parse_state
*state
,
464 const char *qual_indentifier
,
469 bool first_pass
= true;
475 for (exec_node
*node
= layout_const_expressions
.head
;
476 !node
->is_tail_sentinel(); node
= node
->next
) {
478 exec_list dummy_instructions
;
479 ast_node
*const_expression
= exec_node_data(ast_node
, node
, link
);
481 ir_rvalue
*const ir
= const_expression
->hir(&dummy_instructions
, state
);
483 ir_constant
*const const_int
= ir
->constant_expression_value();
484 if (const_int
== NULL
|| !const_int
->type
->is_integer()) {
485 YYLTYPE loc
= const_expression
->get_location();
486 _mesa_glsl_error(&loc
, state
, "%s must be an integral constant "
487 "expression", qual_indentifier
);
491 if (const_int
->value
.i
[0] < min_value
) {
492 YYLTYPE loc
= const_expression
->get_location();
493 _mesa_glsl_error(&loc
, state
, "%s layout qualifier is invalid "
494 "(%d < %d)", qual_indentifier
,
495 const_int
->value
.i
[0], min_value
);
499 if (!first_pass
&& *value
!= const_int
->value
.u
[0]) {
500 YYLTYPE loc
= const_expression
->get_location();
501 _mesa_glsl_error(&loc
, state
, "%s layout qualifier does not "
502 "match previous declaration (%d vs %d)",
503 qual_indentifier
, *value
, const_int
->value
.i
[0]);
507 *value
= const_int
->value
.u
[0];
510 /* If the location is const (and we've verified that
511 * it is) then no instructions should have been emitted
512 * when we converted it to HIR. If they were emitted,
513 * then either the location isn't const after all, or
514 * we are emitting unnecessary instructions.
516 assert(dummy_instructions
.is_empty());