glsl: parse align layout qualifier
[mesa.git] / src / compiler / glsl / ast_type.cpp
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
26 void
27 ast_type_specifier::print(void) const
28 {
29 if (structure) {
30 structure->print();
31 } else {
32 printf("%s ", type_name);
33 }
34
35 if (array_specifier) {
36 array_specifier->print();
37 }
38 }
39
40 bool
41 ast_fully_specified_type::has_qualifiers(_mesa_glsl_parse_state *state) const
42 {
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;
50 }
51 return (this->qualifier.flags.i & ~subroutine_only.flags.i) != 0;
52 }
53
54 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
62 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_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;
83 }
84
85 bool
86 ast_type_qualifier::has_storage() const
87 {
88 return this->flags.q.constant
89 || this->flags.q.attribute
90 || this->flags.q.varying
91 || this->flags.q.in
92 || this->flags.q.out
93 || this->flags.q.uniform
94 || this->flags.q.buffer
95 || this->flags.q.shared_storage;
96 }
97
98 bool
99 ast_type_qualifier::has_auxiliary_storage() const
100 {
101 return this->flags.q.centroid
102 || this->flags.q.sample
103 || this->flags.q.patch;
104 }
105
106 /**
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.
110 */
111 bool
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)
116 {
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;
121
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;
128
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;
133
134 ast_type_qualifier stream_layout_mask;
135 stream_layout_mask.flags.i = 0;
136 stream_layout_mask.flags.q.stream = 1;
137
138 /* FIXME: We should probably do interface and function param validation
139 * separately.
140 */
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;
159
160 /* Uniform block layout qualifiers get to overwrite each
161 * other (rightmost having priority), while all other
162 * qualifiers currently don't allow duplicates.
163 */
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;
169
170 /* Geometry shaders can have several layout qualifiers
171 * assigning different stream values.
172 */
173 if (state->stage == MESA_SHADER_GEOMETRY)
174 allowed_duplicates_mask.flags.i |=
175 stream_layout_mask.flags.i;
176
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");
181 return false;
182 }
183
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");
188 return false;
189 }
190 this->prim_type = q.prim_type;
191 }
192
193 if (q.flags.q.max_vertices) {
194 if (this->max_vertices) {
195 this->max_vertices->merge_qualifier(q.max_vertices);
196 } else {
197 this->max_vertices = q.max_vertices;
198 }
199 }
200
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");
205 } else {
206 this->subroutine_list = q.subroutine_list;
207 }
208 }
209
210 if (q.flags.q.invocations) {
211 if (this->invocations) {
212 this->invocations->merge_qualifier(q.invocations);
213 } else {
214 this->invocations = q.invocations;
215 }
216 }
217
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;
228 }
229 }
230 }
231
232 if (q.flags.q.vertices) {
233 if (this->vertices) {
234 this->vertices->merge_qualifier(q.vertices);
235 } else {
236 this->vertices = q.vertices;
237 }
238 }
239
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");
244 return false;
245 }
246 this->vertex_spacing = q.vertex_spacing;
247 }
248
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");
253 return false;
254 }
255 this->ordering = q.ordering;
256 }
257
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");
262 return false;
263 }
264 this->point_mode = q.point_mode;
265 }
266
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;
271
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]);
276 } else {
277 this->local_size[i] = q.local_size[i];
278 }
279 }
280 }
281
282 this->flags.i |= q.flags.i;
283
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");
288 return false;
289 }
290
291 if (q.flags.q.explicit_align)
292 this->align = q.align;
293
294 if (q.flags.q.explicit_location)
295 this->location = q.location;
296
297 if (q.flags.q.explicit_index)
298 this->index = q.index;
299
300 if (q.flags.q.explicit_binding)
301 this->binding = q.binding;
302
303 if (q.flags.q.explicit_offset)
304 this->offset = q.offset;
305
306 if (q.precision != ast_precision_none)
307 this->precision = q.precision;
308
309 if (q.flags.q.explicit_image_format) {
310 this->image_format = q.image_format;
311 this->image_base_type = q.image_base_type;
312 }
313
314 return true;
315 }
316
317 bool
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)
322 {
323 void *mem_ctx = state;
324 const bool r = this->merge_qualifier(loc, state, q, false);
325
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) {
330 case GL_POINTS:
331 case GL_LINE_STRIP:
332 case GL_TRIANGLE_STRIP:
333 break;
334 default:
335 _mesa_glsl_error(loc, state, "invalid geometry shader output "
336 "primitive type");
337 break;
338 }
339 }
340
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) {
344 if (create_node) {
345 node = new(mem_ctx) ast_tcs_output_layout(*loc);
346 }
347 } else {
348 _mesa_glsl_error(loc, state, "out layout qualifiers only valid in "
349 "tessellation control or geometry shaders");
350 }
351
352 return r;
353 }
354
355 bool
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)
360 {
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;
366
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) {
372 case GL_TRIANGLES:
373 case GL_QUADS:
374 case GL_ISOLINES:
375 break;
376 default:
377 _mesa_glsl_error(loc, state,
378 "invalid tessellation evaluation "
379 "shader input primitive type");
380 break;
381 }
382 }
383
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;
388 break;
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) {
393 case GL_POINTS:
394 case GL_LINES:
395 case GL_LINES_ADJACENCY:
396 case GL_TRIANGLES:
397 case GL_TRIANGLES_ADJACENCY:
398 break;
399 default:
400 _mesa_glsl_error(loc, state,
401 "invalid geometry shader input primitive type");
402 break;
403 }
404 }
405
406 create_gs_ast |=
407 q.flags.q.prim_type &&
408 !state->in_qualifier->flags.q.prim_type;
409
410 valid_in_mask.flags.q.prim_type = 1;
411 valid_in_mask.flags.q.invocations = 1;
412 break;
413 case MESA_SHADER_FRAGMENT:
414 valid_in_mask.flags.q.early_fragment_tests = 1;
415 break;
416 case MESA_SHADER_COMPUTE:
417 create_cs_ast |=
418 q.flags.q.local_size != 0 &&
419 state->in_qualifier->flags.q.local_size == 0;
420
421 valid_in_mask.flags.q.local_size = 7;
422 break;
423 default:
424 _mesa_glsl_error(loc, state,
425 "input layout qualifiers only valid in "
426 "geometry, fragment and compute shaders");
427 break;
428 }
429
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");
434 return false;
435 }
436
437 /* Input layout qualifiers can be specified multiple
438 * times in separate declarations, as long as they match.
439 */
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 ?
446 "type" : "mode");
447 }
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;
451 }
452
453 if (q.flags.q.invocations) {
454 this->flags.q.invocations = 1;
455 if (this->invocations) {
456 this->invocations->merge_qualifier(q.invocations);
457 } else {
458 this->invocations = q.invocations;
459 }
460 }
461
462 if (q.flags.q.early_fragment_tests) {
463 state->fs_early_fragment_tests = true;
464 }
465
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");
471 }
472 } else if (q.flags.q.vertex_spacing) {
473 this->flags.q.vertex_spacing = 1;
474 this->vertex_spacing = q.vertex_spacing;
475 }
476
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");
482 }
483 } else if (q.flags.q.ordering) {
484 this->flags.q.ordering = 1;
485 this->ordering = q.ordering;
486 }
487
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");
493 }
494 } else if (q.flags.q.point_mode) {
495 this->flags.q.point_mode = 1;
496 this->point_mode = q.point_mode;
497 }
498
499 if (create_node) {
500 if (create_gs_ast) {
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);
504 }
505 }
506
507 return true;
508 }
509
510 bool
511 ast_layout_expression::process_qualifier_constant(struct _mesa_glsl_parse_state *state,
512 const char *qual_indentifier,
513 unsigned *value,
514 bool can_be_zero)
515 {
516 int min_value = 0;
517 bool first_pass = true;
518 *value = 0;
519
520 if (!can_be_zero)
521 min_value = 1;
522
523 for (exec_node *node = layout_const_expressions.head;
524 !node->is_tail_sentinel(); node = node->next) {
525
526 exec_list dummy_instructions;
527 ast_node *const_expression = exec_node_data(ast_node, node, link);
528
529 ir_rvalue *const ir = const_expression->hir(&dummy_instructions, state);
530
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);
536 return false;
537 }
538
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);
544 return false;
545 }
546
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]);
552 return false;
553 } else {
554 first_pass = false;
555 *value = const_int->value.u[0];
556 }
557
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.
563 */
564 assert(dummy_instructions.is_empty());
565 }
566
567 return true;
568 }