glsl: parse new transform feedback layout qualifiers
[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 (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;
241 }
242 }
243
244 if (q.flags.q.explicit_xfb_stride)
245 this->xfb_stride = q.xfb_stride;
246
247 /* Merge all we xfb_stride qualifiers into the global out */
248 if (q.flags.q.explicit_xfb_stride || this->flags.q.xfb_stride) {
249
250 /* Set xfb_stride flag to 0 to avoid adding duplicates every time
251 * there is a merge.
252 */
253 this->flags.q.xfb_stride = 0;
254
255 unsigned buff_idx;
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));
261 } else {
262 state->out_qualifier->out_xfb_stride[buff_idx] =
263 new(state) ast_layout_expression(*loc, this->xfb_stride);
264 }
265 }
266 }
267 }
268
269 if (q.flags.q.vertices) {
270 if (this->vertices) {
271 this->vertices->merge_qualifier(q.vertices);
272 } else {
273 this->vertices = q.vertices;
274 }
275 }
276
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");
281 return false;
282 }
283 this->vertex_spacing = q.vertex_spacing;
284 }
285
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");
290 return false;
291 }
292 this->ordering = q.ordering;
293 }
294
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");
299 return false;
300 }
301 this->point_mode = q.point_mode;
302 }
303
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;
308
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]);
313 } else {
314 this->local_size[i] = q.local_size[i];
315 }
316 }
317 }
318
319 this->flags.i |= q.flags.i;
320
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");
325 return false;
326 }
327
328 if (q.flags.q.explicit_align)
329 this->align = q.align;
330
331 if (q.flags.q.explicit_location)
332 this->location = q.location;
333
334 if (q.flags.q.explicit_index)
335 this->index = q.index;
336
337 if (q.flags.q.explicit_binding)
338 this->binding = q.binding;
339
340 if (q.flags.q.explicit_offset || q.flags.q.explicit_xfb_offset)
341 this->offset = q.offset;
342
343 if (q.precision != ast_precision_none)
344 this->precision = q.precision;
345
346 if (q.flags.q.explicit_image_format) {
347 this->image_format = q.image_format;
348 this->image_base_type = q.image_base_type;
349 }
350
351 return true;
352 }
353
354 bool
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)
359 {
360 void *mem_ctx = state;
361 const bool r = this->merge_qualifier(loc, state, q, false);
362
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) {
367 case GL_POINTS:
368 case GL_LINE_STRIP:
369 case GL_TRIANGLE_STRIP:
370 break;
371 default:
372 _mesa_glsl_error(loc, state, "invalid geometry shader output "
373 "primitive type");
374 break;
375 }
376 }
377
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) {
381 if (create_node) {
382 node = new(mem_ctx) ast_tcs_output_layout(*loc);
383 }
384 } else {
385 _mesa_glsl_error(loc, state, "out layout qualifiers only valid in "
386 "tessellation control or geometry shaders");
387 }
388
389 /* Allow future assigments of global out's */
390 this->flags.q.explicit_xfb_buffer = 0;
391 this->flags.q.explicit_xfb_stride = 0;
392
393 return r;
394 }
395
396 bool
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)
401 {
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;
407
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) {
413 case GL_TRIANGLES:
414 case GL_QUADS:
415 case GL_ISOLINES:
416 break;
417 default:
418 _mesa_glsl_error(loc, state,
419 "invalid tessellation evaluation "
420 "shader input primitive type");
421 break;
422 }
423 }
424
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;
429 break;
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) {
434 case GL_POINTS:
435 case GL_LINES:
436 case GL_LINES_ADJACENCY:
437 case GL_TRIANGLES:
438 case GL_TRIANGLES_ADJACENCY:
439 break;
440 default:
441 _mesa_glsl_error(loc, state,
442 "invalid geometry shader input primitive type");
443 break;
444 }
445 }
446
447 create_gs_ast |=
448 q.flags.q.prim_type &&
449 !state->in_qualifier->flags.q.prim_type;
450
451 valid_in_mask.flags.q.prim_type = 1;
452 valid_in_mask.flags.q.invocations = 1;
453 break;
454 case MESA_SHADER_FRAGMENT:
455 valid_in_mask.flags.q.early_fragment_tests = 1;
456 break;
457 case MESA_SHADER_COMPUTE:
458 create_cs_ast |=
459 q.flags.q.local_size != 0 &&
460 state->in_qualifier->flags.q.local_size == 0;
461
462 valid_in_mask.flags.q.local_size = 7;
463 break;
464 default:
465 _mesa_glsl_error(loc, state,
466 "input layout qualifiers only valid in "
467 "geometry, fragment and compute shaders");
468 break;
469 }
470
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");
475 return false;
476 }
477
478 /* Input layout qualifiers can be specified multiple
479 * times in separate declarations, as long as they match.
480 */
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 ?
487 "type" : "mode");
488 }
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;
492 }
493
494 if (q.flags.q.invocations) {
495 this->flags.q.invocations = 1;
496 if (this->invocations) {
497 this->invocations->merge_qualifier(q.invocations);
498 } else {
499 this->invocations = q.invocations;
500 }
501 }
502
503 if (q.flags.q.early_fragment_tests) {
504 state->fs_early_fragment_tests = true;
505 }
506
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");
512 }
513 } else if (q.flags.q.vertex_spacing) {
514 this->flags.q.vertex_spacing = 1;
515 this->vertex_spacing = q.vertex_spacing;
516 }
517
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");
523 }
524 } else if (q.flags.q.ordering) {
525 this->flags.q.ordering = 1;
526 this->ordering = q.ordering;
527 }
528
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");
534 }
535 } else if (q.flags.q.point_mode) {
536 this->flags.q.point_mode = 1;
537 this->point_mode = q.point_mode;
538 }
539
540 if (create_node) {
541 if (create_gs_ast) {
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);
545 }
546 }
547
548 return true;
549 }
550
551 bool
552 ast_layout_expression::process_qualifier_constant(struct _mesa_glsl_parse_state *state,
553 const char *qual_indentifier,
554 unsigned *value,
555 bool can_be_zero)
556 {
557 int min_value = 0;
558 bool first_pass = true;
559 *value = 0;
560
561 if (!can_be_zero)
562 min_value = 1;
563
564 for (exec_node *node = layout_const_expressions.head;
565 !node->is_tail_sentinel(); node = node->next) {
566
567 exec_list dummy_instructions;
568 ast_node *const_expression = exec_node_data(ast_node, node, link);
569
570 ir_rvalue *const ir = const_expression->hir(&dummy_instructions, state);
571
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);
577 return false;
578 }
579
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);
585 return false;
586 }
587
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]);
593 return false;
594 } else {
595 first_pass = false;
596 *value = const_int->value.u[0];
597 }
598
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.
604 */
605 assert(dummy_instructions.is_empty());
606 }
607
608 return true;
609 }
610
611 bool
612 process_qualifier_constant(struct _mesa_glsl_parse_state *state,
613 YYLTYPE *loc,
614 const char *qual_indentifier,
615 ast_expression *const_expression,
616 unsigned *value)
617 {
618 exec_list dummy_instructions;
619
620 if (const_expression == NULL) {
621 *value = 0;
622 return true;
623 }
624
625 ir_rvalue *const ir = const_expression->hir(&dummy_instructions, state);
626
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);
631 return false;
632 }
633
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]);
637 return false;
638 }
639
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.
645 */
646 assert(dummy_instructions.is_empty());
647
648 *value = const_int->value.u[0];
649 return true;
650 }