glsl: simplifies the merge of the default in 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_component
78 || this->flags.q.explicit_location
79 || this->flags.q.explicit_image_format
80 || this->flags.q.explicit_index
81 || this->flags.q.explicit_binding
82 || this->flags.q.explicit_offset
83 || this->flags.q.explicit_stream
84 || this->flags.q.explicit_xfb_buffer
85 || this->flags.q.explicit_xfb_offset
86 || this->flags.q.explicit_xfb_stride;
87 }
88
89 bool
90 ast_type_qualifier::has_storage() const
91 {
92 return this->flags.q.constant
93 || this->flags.q.attribute
94 || this->flags.q.varying
95 || this->flags.q.in
96 || this->flags.q.out
97 || this->flags.q.uniform
98 || this->flags.q.buffer
99 || this->flags.q.shared_storage;
100 }
101
102 bool
103 ast_type_qualifier::has_auxiliary_storage() const
104 {
105 return this->flags.q.centroid
106 || this->flags.q.sample
107 || this->flags.q.patch;
108 }
109
110 bool ast_type_qualifier::has_memory() const
111 {
112 return this->flags.q.coherent
113 || this->flags.q._volatile
114 || this->flags.q.restrict_flag
115 || this->flags.q.read_only
116 || this->flags.q.write_only;
117 }
118
119 /**
120 * This function merges both duplicate identifies within a single layout and
121 * multiple layout qualifiers on a single variable declaration. The
122 * is_single_layout_merge param is used differentiate between the two.
123 */
124 bool
125 ast_type_qualifier::merge_qualifier(YYLTYPE *loc,
126 _mesa_glsl_parse_state *state,
127 const ast_type_qualifier &q,
128 bool is_single_layout_merge)
129 {
130 ast_type_qualifier ubo_mat_mask;
131 ubo_mat_mask.flags.i = 0;
132 ubo_mat_mask.flags.q.row_major = 1;
133 ubo_mat_mask.flags.q.column_major = 1;
134
135 ast_type_qualifier ubo_layout_mask;
136 ubo_layout_mask.flags.i = 0;
137 ubo_layout_mask.flags.q.std140 = 1;
138 ubo_layout_mask.flags.q.packed = 1;
139 ubo_layout_mask.flags.q.shared = 1;
140 ubo_layout_mask.flags.q.std430 = 1;
141
142 ast_type_qualifier ubo_binding_mask;
143 ubo_binding_mask.flags.i = 0;
144 ubo_binding_mask.flags.q.explicit_binding = 1;
145 ubo_binding_mask.flags.q.explicit_offset = 1;
146
147 ast_type_qualifier stream_layout_mask;
148 stream_layout_mask.flags.i = 0;
149 stream_layout_mask.flags.q.stream = 1;
150
151 /* FIXME: We should probably do interface and function param validation
152 * separately.
153 */
154 ast_type_qualifier input_layout_mask;
155 input_layout_mask.flags.i = 0;
156 input_layout_mask.flags.q.centroid = 1;
157 /* Function params can have constant */
158 input_layout_mask.flags.q.constant = 1;
159 input_layout_mask.flags.q.explicit_component = 1;
160 input_layout_mask.flags.q.explicit_location = 1;
161 input_layout_mask.flags.q.flat = 1;
162 input_layout_mask.flags.q.in = 1;
163 input_layout_mask.flags.q.invariant = 1;
164 input_layout_mask.flags.q.noperspective = 1;
165 input_layout_mask.flags.q.origin_upper_left = 1;
166 /* Function params 'inout' will set this */
167 input_layout_mask.flags.q.out = 1;
168 input_layout_mask.flags.q.patch = 1;
169 input_layout_mask.flags.q.pixel_center_integer = 1;
170 input_layout_mask.flags.q.precise = 1;
171 input_layout_mask.flags.q.sample = 1;
172 input_layout_mask.flags.q.smooth = 1;
173
174 /* Uniform block layout qualifiers get to overwrite each
175 * other (rightmost having priority), while all other
176 * qualifiers currently don't allow duplicates.
177 */
178 ast_type_qualifier allowed_duplicates_mask;
179 allowed_duplicates_mask.flags.i =
180 ubo_mat_mask.flags.i |
181 ubo_layout_mask.flags.i |
182 ubo_binding_mask.flags.i;
183
184 /* Geometry shaders can have several layout qualifiers
185 * assigning different stream values.
186 */
187 if (state->stage == MESA_SHADER_GEOMETRY) {
188 allowed_duplicates_mask.flags.i |=
189 stream_layout_mask.flags.i;
190 }
191
192 if (is_single_layout_merge && !state->has_enhanced_layouts() &&
193 (this->flags.i & q.flags.i & ~allowed_duplicates_mask.flags.i) != 0) {
194 _mesa_glsl_error(loc, state, "duplicate layout qualifiers used");
195 return false;
196 }
197
198 if (q.flags.q.prim_type) {
199 if (this->flags.q.prim_type && this->prim_type != q.prim_type) {
200 _mesa_glsl_error(loc, state,
201 "conflicting input primitive %s specified",
202 state->stage == MESA_SHADER_GEOMETRY ?
203 "type" : "mode");
204 return false;
205 }
206 this->flags.q.prim_type = 1;
207 this->prim_type = q.prim_type;
208 }
209
210 if (q.flags.q.max_vertices) {
211 if (this->flags.q.max_vertices && !is_single_layout_merge) {
212 this->max_vertices->merge_qualifier(q.max_vertices);
213 } else {
214 this->flags.q.max_vertices = 1;
215 this->max_vertices = q.max_vertices;
216 }
217 }
218
219 if (q.flags.q.subroutine_def) {
220 if (this->flags.q.subroutine_def) {
221 _mesa_glsl_error(loc, state,
222 "conflicting subroutine qualifiers used");
223 } else {
224 this->subroutine_list = q.subroutine_list;
225 }
226 }
227
228 if (q.flags.q.invocations) {
229 if (this->flags.q.invocations && !is_single_layout_merge) {
230 this->invocations->merge_qualifier(q.invocations);
231 } else {
232 this->flags.q.invocations = 1;
233 this->invocations = q.invocations;
234 }
235 }
236
237 if (state->stage == MESA_SHADER_GEOMETRY &&
238 state->has_explicit_attrib_stream()) {
239 if (!this->flags.q.explicit_stream) {
240 if (q.flags.q.stream) {
241 this->flags.q.stream = 1;
242 this->stream = q.stream;
243 } else if (!this->flags.q.stream && this->flags.q.out &&
244 !this->flags.q.in) {
245 /* Assign default global stream value */
246 this->flags.q.stream = 1;
247 this->stream = state->out_qualifier->stream;
248 }
249 }
250 }
251
252 if (state->has_enhanced_layouts()) {
253 if (!this->flags.q.explicit_xfb_buffer) {
254 if (q.flags.q.xfb_buffer) {
255 this->flags.q.xfb_buffer = 1;
256 this->xfb_buffer = q.xfb_buffer;
257 } else if (!this->flags.q.xfb_buffer && this->flags.q.out &&
258 !this->flags.q.in) {
259 /* Assign global xfb_buffer value */
260 this->flags.q.xfb_buffer = 1;
261 this->xfb_buffer = state->out_qualifier->xfb_buffer;
262 }
263 }
264
265 if (q.flags.q.explicit_xfb_stride)
266 this->xfb_stride = q.xfb_stride;
267
268 /* Merge all we xfb_stride qualifiers into the global out */
269 if (q.flags.q.explicit_xfb_stride || this->flags.q.xfb_stride) {
270
271 /* Set xfb_stride flag to 0 to avoid adding duplicates every time
272 * there is a merge.
273 */
274 this->flags.q.xfb_stride = 0;
275
276 unsigned buff_idx;
277 if (process_qualifier_constant(state, loc, "xfb_buffer",
278 this->xfb_buffer, &buff_idx)) {
279 if (state->out_qualifier->out_xfb_stride[buff_idx]
280 && !is_single_layout_merge) {
281 state->out_qualifier->out_xfb_stride[buff_idx]->merge_qualifier(
282 new(state->linalloc) ast_layout_expression(*loc, this->xfb_stride));
283 } else {
284 state->out_qualifier->out_xfb_stride[buff_idx] =
285 new(state->linalloc) ast_layout_expression(*loc, this->xfb_stride);
286 }
287 }
288 }
289 }
290
291 if (q.flags.q.vertices) {
292 if (this->flags.q.vertices && !is_single_layout_merge) {
293 this->vertices->merge_qualifier(q.vertices);
294 } else {
295 this->flags.q.vertices = 1;
296 this->vertices = q.vertices;
297 }
298 }
299
300 if (q.flags.q.vertex_spacing) {
301 if (this->flags.q.vertex_spacing && this->vertex_spacing != q.vertex_spacing) {
302 _mesa_glsl_error(loc, state, "conflicting vertex spacing used");
303 return false;
304 }
305 this->flags.q.vertex_spacing = 1;
306 this->vertex_spacing = q.vertex_spacing;
307 }
308
309 if (q.flags.q.ordering) {
310 if (this->flags.q.ordering && this->ordering != q.ordering) {
311 _mesa_glsl_error(loc, state, "conflicting ordering used");
312 return false;
313 }
314 this->flags.q.ordering = 1;
315 this->ordering = q.ordering;
316 }
317
318 if (q.flags.q.point_mode) {
319 if (this->flags.q.point_mode && this->point_mode != q.point_mode) {
320 _mesa_glsl_error(loc, state, "conflicting point mode used");
321 return false;
322 }
323 this->flags.q.point_mode = 1;
324 this->point_mode = q.point_mode;
325 }
326
327 if (q.flags.q.early_fragment_tests)
328 this->flags.q.early_fragment_tests = true;
329
330 if ((q.flags.i & ubo_mat_mask.flags.i) != 0)
331 this->flags.i &= ~ubo_mat_mask.flags.i;
332 if ((q.flags.i & ubo_layout_mask.flags.i) != 0)
333 this->flags.i &= ~ubo_layout_mask.flags.i;
334
335 for (int i = 0; i < 3; i++) {
336 if (q.flags.q.local_size & (1 << i)) {
337 if (this->local_size[i] && !is_single_layout_merge) {
338 this->local_size[i]->merge_qualifier(q.local_size[i]);
339 } else {
340 this->local_size[i] = q.local_size[i];
341 }
342 }
343 }
344
345 if (q.flags.q.local_size_variable)
346 this->flags.q.local_size_variable = true;
347
348 this->flags.i |= q.flags.i;
349
350 if (this->flags.q.in &&
351 (this->flags.i & ~input_layout_mask.flags.i) != 0) {
352 _mesa_glsl_error(loc, state, "invalid input layout qualifier used");
353 return false;
354 }
355
356 if (q.flags.q.explicit_align)
357 this->align = q.align;
358
359 if (q.flags.q.explicit_location)
360 this->location = q.location;
361
362 if (q.flags.q.explicit_index)
363 this->index = q.index;
364
365 if (q.flags.q.explicit_component)
366 this->component = q.component;
367
368 if (q.flags.q.explicit_binding)
369 this->binding = q.binding;
370
371 if (q.flags.q.explicit_offset || q.flags.q.explicit_xfb_offset)
372 this->offset = q.offset;
373
374 if (q.precision != ast_precision_none)
375 this->precision = q.precision;
376
377 if (q.flags.q.explicit_image_format) {
378 this->image_format = q.image_format;
379 this->image_base_type = q.image_base_type;
380 }
381
382 return true;
383 }
384
385 bool
386 ast_type_qualifier::validate_out_qualifier(YYLTYPE *loc,
387 _mesa_glsl_parse_state *state)
388 {
389 bool r = true;
390 ast_type_qualifier valid_out_mask;
391 valid_out_mask.flags.i = 0;
392
393 switch (state->stage) {
394 case MESA_SHADER_GEOMETRY:
395 if (this->flags.q.prim_type) {
396 /* Make sure this is a valid output primitive type. */
397 switch (this->prim_type) {
398 case GL_POINTS:
399 case GL_LINE_STRIP:
400 case GL_TRIANGLE_STRIP:
401 break;
402 default:
403 r = false;
404 _mesa_glsl_error(loc, state, "invalid geometry shader output "
405 "primitive type");
406 break;
407 }
408 }
409
410 valid_out_mask.flags.q.stream = 1;
411 valid_out_mask.flags.q.explicit_stream = 1;
412 valid_out_mask.flags.q.explicit_xfb_buffer = 1;
413 valid_out_mask.flags.q.xfb_buffer = 1;
414 valid_out_mask.flags.q.explicit_xfb_stride = 1;
415 valid_out_mask.flags.q.xfb_stride = 1;
416 valid_out_mask.flags.q.max_vertices = 1;
417 valid_out_mask.flags.q.prim_type = 1;
418 break;
419 case MESA_SHADER_TESS_CTRL:
420 valid_out_mask.flags.q.vertices = 1;
421 valid_out_mask.flags.q.explicit_xfb_buffer = 1;
422 valid_out_mask.flags.q.xfb_buffer = 1;
423 valid_out_mask.flags.q.explicit_xfb_stride = 1;
424 valid_out_mask.flags.q.xfb_stride = 1;
425 break;
426 case MESA_SHADER_TESS_EVAL:
427 case MESA_SHADER_VERTEX:
428 valid_out_mask.flags.q.explicit_xfb_buffer = 1;
429 valid_out_mask.flags.q.xfb_buffer = 1;
430 valid_out_mask.flags.q.explicit_xfb_stride = 1;
431 valid_out_mask.flags.q.xfb_stride = 1;
432 break;
433 case MESA_SHADER_FRAGMENT:
434 valid_out_mask.flags.q.blend_support = 1;
435 break;
436 default:
437 r = false;
438 _mesa_glsl_error(loc, state,
439 "out layout qualifiers only valid in "
440 "geometry, tessellation, vertex and fragment shaders");
441 }
442
443 /* Generate an error when invalid output layout qualifiers are used. */
444 if ((this->flags.i & ~valid_out_mask.flags.i) != 0) {
445 r = false;
446 _mesa_glsl_error(loc, state, "invalid output layout qualifiers used");
447 }
448
449 return r;
450 }
451
452 bool
453 ast_type_qualifier::merge_into_out_qualifier(YYLTYPE *loc,
454 _mesa_glsl_parse_state *state,
455 ast_node* &node, bool create_node)
456 {
457 const bool r = state->out_qualifier->merge_qualifier(loc, state,
458 *this, false);
459
460 switch (state->stage) {
461 case MESA_SHADER_GEOMETRY:
462 /* Allow future assignments of global out's stream id value */
463 state->out_qualifier->flags.q.explicit_stream = 0;
464 break;
465 case MESA_SHADER_TESS_CTRL:
466 if (create_node)
467 node = new(state->linalloc) ast_tcs_output_layout(*loc);
468 break;
469 default:
470 break;
471 }
472
473 /* Allow future assignments of global out's */
474 state->out_qualifier->flags.q.explicit_xfb_buffer = 0;
475 state->out_qualifier->flags.q.explicit_xfb_stride = 0;
476
477 return r;
478 }
479
480 bool
481 ast_type_qualifier::validate_in_qualifier(YYLTYPE *loc,
482 _mesa_glsl_parse_state *state)
483 {
484 bool r = true;
485 ast_type_qualifier valid_in_mask;
486 valid_in_mask.flags.i = 0;
487
488 switch (state->stage) {
489 case MESA_SHADER_TESS_EVAL:
490 if (this->flags.q.prim_type) {
491 /* Make sure this is a valid input primitive type. */
492 switch (this->prim_type) {
493 case GL_TRIANGLES:
494 case GL_QUADS:
495 case GL_ISOLINES:
496 break;
497 default:
498 r = false;
499 _mesa_glsl_error(loc, state,
500 "invalid tessellation evaluation "
501 "shader input primitive type");
502 break;
503 }
504 }
505
506 valid_in_mask.flags.q.prim_type = 1;
507 valid_in_mask.flags.q.vertex_spacing = 1;
508 valid_in_mask.flags.q.ordering = 1;
509 valid_in_mask.flags.q.point_mode = 1;
510 break;
511 case MESA_SHADER_GEOMETRY:
512 if (this->flags.q.prim_type) {
513 /* Make sure this is a valid input primitive type. */
514 switch (this->prim_type) {
515 case GL_POINTS:
516 case GL_LINES:
517 case GL_LINES_ADJACENCY:
518 case GL_TRIANGLES:
519 case GL_TRIANGLES_ADJACENCY:
520 break;
521 default:
522 r = false;
523 _mesa_glsl_error(loc, state,
524 "invalid geometry shader input primitive type");
525 break;
526 }
527 }
528
529 valid_in_mask.flags.q.prim_type = 1;
530 valid_in_mask.flags.q.invocations = 1;
531 break;
532 case MESA_SHADER_FRAGMENT:
533 valid_in_mask.flags.q.early_fragment_tests = 1;
534 break;
535 case MESA_SHADER_COMPUTE:
536 valid_in_mask.flags.q.local_size = 7;
537 valid_in_mask.flags.q.local_size_variable = 1;
538 break;
539 default:
540 r = false;
541 _mesa_glsl_error(loc, state,
542 "input layout qualifiers only valid in "
543 "geometry, tessellation, fragment and compute shaders");
544 break;
545 }
546
547 /* Generate an error when invalid input layout qualifiers are used. */
548 if ((this->flags.i & ~valid_in_mask.flags.i) != 0) {
549 r = false;
550 _mesa_glsl_error(loc, state, "invalid input layout qualifiers used");
551 }
552
553 /* The checks below are also performed when merging but we want to spit an
554 * error against the default global input qualifier as soon as we can, with
555 * the closest error location in the shader.
556 */
557
558 /* Input layout qualifiers can be specified multiple
559 * times in separate declarations, as long as they match.
560 */
561 if (state->in_qualifier->flags.q.prim_type && this->flags.q.prim_type
562 && state->in_qualifier->prim_type != this->prim_type) {
563 r = false;
564 _mesa_glsl_error(loc, state,
565 "conflicting input primitive %s specified",
566 state->stage == MESA_SHADER_GEOMETRY ?
567 "type" : "mode");
568 }
569
570 if (state->in_qualifier->flags.q.vertex_spacing
571 && this->flags.q.vertex_spacing
572 && state->in_qualifier->vertex_spacing != this->vertex_spacing) {
573 r = false;
574 _mesa_glsl_error(loc, state,
575 "conflicting vertex spacing specified");
576 }
577
578 if (state->in_qualifier->flags.q.ordering && this->flags.q.ordering
579 && state->in_qualifier->ordering != this->ordering) {
580 r = false;
581 _mesa_glsl_error(loc, state,
582 "conflicting ordering specified");
583 }
584
585 if (state->in_qualifier->flags.q.point_mode && this->flags.q.point_mode
586 && state->in_qualifier->point_mode != this->point_mode) {
587 r = false;
588 _mesa_glsl_error(loc, state,
589 "conflicting point mode specified");
590 }
591
592 return r;
593 }
594
595 bool
596 ast_type_qualifier::merge_into_in_qualifier(YYLTYPE *loc,
597 _mesa_glsl_parse_state *state,
598 ast_node* &node, bool create_node)
599 {
600 bool r = true;
601 void *lin_ctx = state->linalloc;
602
603 /* We create the gs_input_layout node before merging so, in the future, no
604 * more repeated nodes will be created as we will have the flag set.
605 */
606 if (state->stage == MESA_SHADER_GEOMETRY
607 && this->flags.q.prim_type && !state->in_qualifier->flags.q.prim_type
608 && create_node) {
609 node = new(lin_ctx) ast_gs_input_layout(*loc, this->prim_type);
610 }
611
612 r = state->in_qualifier->merge_qualifier(loc, state, *this, false);
613
614 if (state->in_qualifier->flags.q.early_fragment_tests) {
615 state->fs_early_fragment_tests = true;
616 state->in_qualifier->flags.q.early_fragment_tests = false;
617 }
618
619 /* We allow the creation of multiple cs_input_layout nodes. Coherence among
620 * all existing nodes is checked later, when the AST node is transformed
621 * into HIR.
622 */
623 if (state->in_qualifier->flags.q.local_size) {
624 if (create_node)
625 node = new(lin_ctx) ast_cs_input_layout(*loc, state->in_qualifier->local_size);
626 state->in_qualifier->flags.q.local_size = 0;
627 for (int i = 0; i < 3; i++)
628 state->in_qualifier->local_size[i] = NULL;
629 }
630
631 if (state->in_qualifier->flags.q.local_size_variable) {
632 state->cs_input_local_size_variable_specified = true;
633 state->in_qualifier->flags.q.local_size_variable = false;
634 }
635
636 return r;
637 }
638
639 /**
640 * Check if the current type qualifier has any illegal flags.
641 *
642 * If so, print an error message, followed by a list of illegal flags.
643 *
644 * \param message The error message to print.
645 * \param allowed_flags A list of valid flags.
646 */
647 bool
648 ast_type_qualifier::validate_flags(YYLTYPE *loc,
649 _mesa_glsl_parse_state *state,
650 const ast_type_qualifier &allowed_flags,
651 const char *message, const char *name)
652 {
653 ast_type_qualifier bad;
654 bad.flags.i = this->flags.i & ~allowed_flags.flags.i;
655 if (bad.flags.i == 0)
656 return true;
657
658 _mesa_glsl_error(loc, state,
659 "%s '%s':"
660 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s"
661 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s"
662 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
663 message, name,
664 bad.flags.q.invariant ? " invariant" : "",
665 bad.flags.q.precise ? " precise" : "",
666 bad.flags.q.constant ? " constant" : "",
667 bad.flags.q.attribute ? " attribute" : "",
668 bad.flags.q.varying ? " varying" : "",
669 bad.flags.q.in ? " in" : "",
670 bad.flags.q.out ? " out" : "",
671 bad.flags.q.centroid ? " centroid" : "",
672 bad.flags.q.sample ? " sample" : "",
673 bad.flags.q.patch ? " patch" : "",
674 bad.flags.q.uniform ? " uniform" : "",
675 bad.flags.q.buffer ? " buffer" : "",
676 bad.flags.q.shared_storage ? " shared_storage" : "",
677 bad.flags.q.smooth ? " smooth" : "",
678 bad.flags.q.flat ? " flat" : "",
679 bad.flags.q.noperspective ? " noperspective" : "",
680 bad.flags.q.origin_upper_left ? " origin_upper_left" : "",
681 bad.flags.q.pixel_center_integer ? " pixel_center_integer" : "",
682 bad.flags.q.explicit_align ? " align" : "",
683 bad.flags.q.explicit_component ? " component" : "",
684 bad.flags.q.explicit_location ? " location" : "",
685 bad.flags.q.explicit_index ? " index" : "",
686 bad.flags.q.explicit_binding ? " binding" : "",
687 bad.flags.q.explicit_offset ? " offset" : "",
688 bad.flags.q.depth_any ? " depth_any" : "",
689 bad.flags.q.depth_greater ? " depth_greater" : "",
690 bad.flags.q.depth_less ? " depth_less" : "",
691 bad.flags.q.depth_unchanged ? " depth_unchanged" : "",
692 bad.flags.q.std140 ? " std140" : "",
693 bad.flags.q.std430 ? " std430" : "",
694 bad.flags.q.shared ? " shared" : "",
695 bad.flags.q.packed ? " packed" : "",
696 bad.flags.q.column_major ? " column_major" : "",
697 bad.flags.q.row_major ? " row_major" : "",
698 bad.flags.q.prim_type ? " prim_type" : "",
699 bad.flags.q.max_vertices ? " max_vertices" : "",
700 bad.flags.q.local_size ? " local_size" : "",
701 bad.flags.q.local_size_variable ? " local_size_variable" : "",
702 bad.flags.q.early_fragment_tests ? " early_fragment_tests" : "",
703 bad.flags.q.explicit_image_format ? " image_format" : "",
704 bad.flags.q.coherent ? " coherent" : "",
705 bad.flags.q._volatile ? " _volatile" : "",
706 bad.flags.q.restrict_flag ? " restrict_flag" : "",
707 bad.flags.q.read_only ? " read_only" : "",
708 bad.flags.q.write_only ? " write_only" : "",
709 bad.flags.q.invocations ? " invocations" : "",
710 bad.flags.q.stream ? " stream" : "",
711 bad.flags.q.explicit_stream ? " stream" : "",
712 bad.flags.q.explicit_xfb_offset ? " xfb_offset" : "",
713 bad.flags.q.xfb_buffer ? " xfb_buffer" : "",
714 bad.flags.q.explicit_xfb_buffer ? " xfb_buffer" : "",
715 bad.flags.q.xfb_stride ? " xfb_stride" : "",
716 bad.flags.q.explicit_xfb_stride ? " xfb_stride" : "",
717 bad.flags.q.vertex_spacing ? " vertex_spacing" : "",
718 bad.flags.q.ordering ? " ordering" : "",
719 bad.flags.q.point_mode ? " point_mode" : "",
720 bad.flags.q.vertices ? " vertices" : "",
721 bad.flags.q.subroutine ? " subroutine" : "",
722 bad.flags.q.subroutine_def ? " subroutine_def" : "");
723 return false;
724 }
725
726 bool
727 ast_layout_expression::process_qualifier_constant(struct _mesa_glsl_parse_state *state,
728 const char *qual_indentifier,
729 unsigned *value,
730 bool can_be_zero,
731 bool must_match)
732 {
733 int min_value = 0;
734 bool first_pass = true;
735 *value = 0;
736
737 if (!can_be_zero)
738 min_value = 1;
739
740 for (exec_node *node = layout_const_expressions.get_head_raw();
741 !node->is_tail_sentinel(); node = node->next) {
742
743 exec_list dummy_instructions;
744 ast_node *const_expression = exec_node_data(ast_node, node, link);
745
746 ir_rvalue *const ir = const_expression->hir(&dummy_instructions, state);
747
748 ir_constant *const const_int = ir->constant_expression_value();
749 if (const_int == NULL || !const_int->type->is_integer()) {
750 YYLTYPE loc = const_expression->get_location();
751 _mesa_glsl_error(&loc, state, "%s must be an integral constant "
752 "expression", qual_indentifier);
753 return false;
754 }
755
756 if (const_int->value.i[0] < min_value) {
757 YYLTYPE loc = const_expression->get_location();
758 _mesa_glsl_error(&loc, state, "%s layout qualifier is invalid "
759 "(%d < %d)", qual_indentifier,
760 const_int->value.i[0], min_value);
761 return false;
762 }
763
764 /* From section 4.4 "Layout Qualifiers" of the GLSL 4.50 spec:
765 * "When the same layout-qualifier-name occurs multiple times,
766 * in a single declaration, the last occurrence overrides the
767 * former occurrence(s)."
768 */
769 if (!first_pass) {
770 if ((must_match || !state->has_420pack()) && *value != const_int->value.u[0]) {
771 YYLTYPE loc = const_expression->get_location();
772 _mesa_glsl_error(&loc, state, "%s layout qualifier does not "
773 "match previous declaration (%d vs %d)",
774 qual_indentifier, *value, const_int->value.i[0]);
775 return false;
776 }
777 } else {
778 first_pass = false;
779 *value = const_int->value.u[0];
780 }
781
782 /* If the location is const (and we've verified that
783 * it is) then no instructions should have been emitted
784 * when we converted it to HIR. If they were emitted,
785 * then either the location isn't const after all, or
786 * we are emitting unnecessary instructions.
787 */
788 assert(dummy_instructions.is_empty());
789 }
790
791 return true;
792 }
793
794 bool
795 process_qualifier_constant(struct _mesa_glsl_parse_state *state,
796 YYLTYPE *loc,
797 const char *qual_indentifier,
798 ast_expression *const_expression,
799 unsigned *value)
800 {
801 exec_list dummy_instructions;
802
803 if (const_expression == NULL) {
804 *value = 0;
805 return true;
806 }
807
808 ir_rvalue *const ir = const_expression->hir(&dummy_instructions, state);
809
810 ir_constant *const const_int = ir->constant_expression_value();
811 if (const_int == NULL || !const_int->type->is_integer()) {
812 _mesa_glsl_error(loc, state, "%s must be an integral constant "
813 "expression", qual_indentifier);
814 return false;
815 }
816
817 if (const_int->value.i[0] < 0) {
818 _mesa_glsl_error(loc, state, "%s layout qualifier is invalid (%d < 0)",
819 qual_indentifier, const_int->value.u[0]);
820 return false;
821 }
822
823 /* If the location is const (and we've verified that
824 * it is) then no instructions should have been emitted
825 * when we converted it to HIR. If they were emitted,
826 * then either the location isn't const after all, or
827 * we are emitting unnecessary instructions.
828 */
829 assert(dummy_instructions.is_empty());
830
831 *value = const_int->value.u[0];
832 return true;
833 }