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.
26 * Convert abstract syntax to to high-level intermediate reprensentation (HIR).
28 * During the conversion to HIR, the majority of the symantic checking is
29 * preformed on the program. This includes:
31 * * Symbol table management
35 * The majority of this work could be done during parsing, and the parser could
36 * probably generate HIR directly. However, this results in frequent changes
37 * to the parser code. Since we do not assume that every system this complier
38 * is built on will have Flex and Bison installed, we have to store the code
39 * generated by these tools in our version control system. In other parts of
40 * the system we've seen problems where a parser was changed but the generated
41 * code was not committed, merge conflicts where created because two developers
42 * had slightly different versions of Bison installed, etc.
44 * I have also noticed that running Bison generated parsers in GDB is very
45 * irritating. When you get a segfault on '$$ = $1->foo', you can't very
46 * well 'print $1' in GDB.
48 * As a result, my preference is to put as little C code as possible in the
49 * parser (and lexer) sources.
52 #include "glsl_symbol_table.h"
53 #include "glsl_parser_extras.h"
55 #include "compiler/glsl_types.h"
56 #include "util/hash_table.h"
57 #include "main/mtypes.h"
58 #include "main/macros.h"
59 #include "main/shaderobj.h"
61 #include "ir_builder.h"
62 #include "builtin_functions.h"
64 using namespace ir_builder
;
67 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
68 exec_list
*instructions
);
70 remove_per_vertex_blocks(exec_list
*instructions
,
71 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
);
74 * Visitor class that finds the first instance of any write-only variable that
75 * is ever read, if any
77 class read_from_write_only_variable_visitor
: public ir_hierarchical_visitor
80 read_from_write_only_variable_visitor() : found(NULL
)
84 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
86 if (this->in_assignee
)
87 return visit_continue
;
89 ir_variable
*var
= ir
->variable_referenced();
90 /* We can have memory_write_only set on both images and buffer variables,
91 * but in the former there is a distinction between reads from
92 * the variable itself (write_only) and from the memory they point to
93 * (memory_write_only), while in the case of buffer variables there is
94 * no such distinction, that is why this check here is limited to
95 * buffer variables alone.
97 if (!var
|| var
->data
.mode
!= ir_var_shader_storage
)
98 return visit_continue
;
100 if (var
->data
.memory_write_only
) {
105 return visit_continue
;
108 ir_variable
*get_variable() {
112 virtual ir_visitor_status
visit_enter(ir_expression
*ir
)
114 /* .length() doesn't actually read anything */
115 if (ir
->operation
== ir_unop_ssbo_unsized_array_length
)
116 return visit_continue_with_parent
;
118 return visit_continue
;
126 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
128 _mesa_glsl_initialize_variables(instructions
, state
);
130 state
->symbols
->separate_function_namespace
= state
->language_version
== 110;
132 state
->current_function
= NULL
;
134 state
->toplevel_ir
= instructions
;
136 state
->gs_input_prim_type_specified
= false;
137 state
->tcs_output_vertices_specified
= false;
138 state
->cs_input_local_size_specified
= false;
140 /* Section 4.2 of the GLSL 1.20 specification states:
141 * "The built-in functions are scoped in a scope outside the global scope
142 * users declare global variables in. That is, a shader's global scope,
143 * available for user-defined functions and global variables, is nested
144 * inside the scope containing the built-in functions."
146 * Since built-in functions like ftransform() access built-in variables,
147 * it follows that those must be in the outer scope as well.
149 * We push scope here to create this nesting effect...but don't pop.
150 * This way, a shader's globals are still in the symbol table for use
153 state
->symbols
->push_scope();
155 foreach_list_typed (ast_node
, ast
, link
, & state
->translation_unit
)
156 ast
->hir(instructions
, state
);
158 detect_recursion_unlinked(state
, instructions
);
159 detect_conflicting_assignments(state
, instructions
);
161 state
->toplevel_ir
= NULL
;
163 /* Move all of the variable declarations to the front of the IR list, and
164 * reverse the order. This has the (intended!) side effect that vertex
165 * shader inputs and fragment shader outputs will appear in the IR in the
166 * same order that they appeared in the shader code. This results in the
167 * locations being assigned in the declared order. Many (arguably buggy)
168 * applications depend on this behavior, and it matches what nearly all
171 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
172 ir_variable
*const var
= node
->as_variable();
178 instructions
->push_head(var
);
181 /* Figure out if gl_FragCoord is actually used in fragment shader */
182 ir_variable
*const var
= state
->symbols
->get_variable("gl_FragCoord");
184 state
->fs_uses_gl_fragcoord
= var
->data
.used
;
186 /* From section 7.1 (Built-In Language Variables) of the GLSL 4.10 spec:
188 * If multiple shaders using members of a built-in block belonging to
189 * the same interface are linked together in the same program, they
190 * must all redeclare the built-in block in the same way, as described
191 * in section 4.3.7 "Interface Blocks" for interface block matching, or
192 * a link error will result.
194 * The phrase "using members of a built-in block" implies that if two
195 * shaders are linked together and one of them *does not use* any members
196 * of the built-in block, then that shader does not need to have a matching
197 * redeclaration of the built-in block.
199 * This appears to be a clarification to the behaviour established for
200 * gl_PerVertex by GLSL 1.50, therefore implement it regardless of GLSL
203 * The definition of "interface" in section 4.3.7 that applies here is as
206 * The boundary between adjacent programmable pipeline stages: This
207 * spans all the outputs in all compilation units of the first stage
208 * and all the inputs in all compilation units of the second stage.
210 * Therefore this rule applies to both inter- and intra-stage linking.
212 * The easiest way to implement this is to check whether the shader uses
213 * gl_PerVertex right after ast-to-ir conversion, and if it doesn't, simply
214 * remove all the relevant variable declaration from the IR, so that the
215 * linker won't see them and complain about mismatches.
217 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_in
);
218 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_out
);
220 /* Check that we don't have reads from write-only variables */
221 read_from_write_only_variable_visitor v
;
223 ir_variable
*error_var
= v
.get_variable();
225 /* It would be nice to have proper location information, but for that
226 * we would need to check this as we process each kind of AST node
229 memset(&loc
, 0, sizeof(loc
));
230 _mesa_glsl_error(&loc
, state
, "Read from write-only variable `%s'",
236 static ir_expression_operation
237 get_implicit_conversion_operation(const glsl_type
*to
, const glsl_type
*from
,
238 struct _mesa_glsl_parse_state
*state
)
240 switch (to
->base_type
) {
241 case GLSL_TYPE_FLOAT
:
242 switch (from
->base_type
) {
243 case GLSL_TYPE_INT
: return ir_unop_i2f
;
244 case GLSL_TYPE_UINT
: return ir_unop_u2f
;
245 default: return (ir_expression_operation
)0;
249 if (!state
->is_version(400, 0) && !state
->ARB_gpu_shader5_enable
250 && !state
->MESA_shader_integer_functions_enable
)
251 return (ir_expression_operation
)0;
252 switch (from
->base_type
) {
253 case GLSL_TYPE_INT
: return ir_unop_i2u
;
254 default: return (ir_expression_operation
)0;
257 case GLSL_TYPE_DOUBLE
:
258 if (!state
->has_double())
259 return (ir_expression_operation
)0;
260 switch (from
->base_type
) {
261 case GLSL_TYPE_INT
: return ir_unop_i2d
;
262 case GLSL_TYPE_UINT
: return ir_unop_u2d
;
263 case GLSL_TYPE_FLOAT
: return ir_unop_f2d
;
264 case GLSL_TYPE_INT64
: return ir_unop_i642d
;
265 case GLSL_TYPE_UINT64
: return ir_unop_u642d
;
266 default: return (ir_expression_operation
)0;
269 case GLSL_TYPE_UINT64
:
270 if (!state
->has_int64())
271 return (ir_expression_operation
)0;
272 switch (from
->base_type
) {
273 case GLSL_TYPE_INT
: return ir_unop_i2u64
;
274 case GLSL_TYPE_UINT
: return ir_unop_u2u64
;
275 case GLSL_TYPE_INT64
: return ir_unop_i642u64
;
276 default: return (ir_expression_operation
)0;
279 case GLSL_TYPE_INT64
:
280 if (!state
->has_int64())
281 return (ir_expression_operation
)0;
282 switch (from
->base_type
) {
283 case GLSL_TYPE_INT
: return ir_unop_i2i64
;
284 default: return (ir_expression_operation
)0;
287 default: return (ir_expression_operation
)0;
293 * If a conversion is available, convert one operand to a different type
295 * The \c from \c ir_rvalue is converted "in place".
297 * \param to Type that the operand it to be converted to
298 * \param from Operand that is being converted
299 * \param state GLSL compiler state
302 * If a conversion is possible (or unnecessary), \c true is returned.
303 * Otherwise \c false is returned.
306 apply_implicit_conversion(const glsl_type
*to
, ir_rvalue
* &from
,
307 struct _mesa_glsl_parse_state
*state
)
310 if (to
->base_type
== from
->type
->base_type
)
313 /* Prior to GLSL 1.20, there are no implicit conversions */
314 if (!state
->is_version(120, 0))
317 /* ESSL does not allow implicit conversions */
318 if (state
->es_shader
)
321 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
323 * "There are no implicit array or structure conversions. For
324 * example, an array of int cannot be implicitly converted to an
327 if (!to
->is_numeric() || !from
->type
->is_numeric())
330 /* We don't actually want the specific type `to`, we want a type
331 * with the same base type as `to`, but the same vector width as
334 to
= glsl_type::get_instance(to
->base_type
, from
->type
->vector_elements
,
335 from
->type
->matrix_columns
);
337 ir_expression_operation op
= get_implicit_conversion_operation(to
, from
->type
, state
);
339 from
= new(ctx
) ir_expression(op
, to
, from
, NULL
);
347 static const struct glsl_type
*
348 arithmetic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
350 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
352 const glsl_type
*type_a
= value_a
->type
;
353 const glsl_type
*type_b
= value_b
->type
;
355 /* From GLSL 1.50 spec, page 56:
357 * "The arithmetic binary operators add (+), subtract (-),
358 * multiply (*), and divide (/) operate on integer and
359 * floating-point scalars, vectors, and matrices."
361 if (!type_a
->is_numeric() || !type_b
->is_numeric()) {
362 _mesa_glsl_error(loc
, state
,
363 "operands to arithmetic operators must be numeric");
364 return glsl_type::error_type
;
368 /* "If one operand is floating-point based and the other is
369 * not, then the conversions from Section 4.1.10 "Implicit
370 * Conversions" are applied to the non-floating-point-based operand."
372 if (!apply_implicit_conversion(type_a
, value_b
, state
)
373 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
374 _mesa_glsl_error(loc
, state
,
375 "could not implicitly convert operands to "
376 "arithmetic operator");
377 return glsl_type::error_type
;
379 type_a
= value_a
->type
;
380 type_b
= value_b
->type
;
382 /* "If the operands are integer types, they must both be signed or
385 * From this rule and the preceeding conversion it can be inferred that
386 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
387 * The is_numeric check above already filtered out the case where either
388 * type is not one of these, so now the base types need only be tested for
391 if (type_a
->base_type
!= type_b
->base_type
) {
392 _mesa_glsl_error(loc
, state
,
393 "base type mismatch for arithmetic operator");
394 return glsl_type::error_type
;
397 /* "All arithmetic binary operators result in the same fundamental type
398 * (signed integer, unsigned integer, or floating-point) as the
399 * operands they operate on, after operand type conversion. After
400 * conversion, the following cases are valid
402 * * The two operands are scalars. In this case the operation is
403 * applied, resulting in a scalar."
405 if (type_a
->is_scalar() && type_b
->is_scalar())
408 /* "* One operand is a scalar, and the other is a vector or matrix.
409 * In this case, the scalar operation is applied independently to each
410 * component of the vector or matrix, resulting in the same size
413 if (type_a
->is_scalar()) {
414 if (!type_b
->is_scalar())
416 } else if (type_b
->is_scalar()) {
420 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
421 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
424 assert(!type_a
->is_scalar());
425 assert(!type_b
->is_scalar());
427 /* "* The two operands are vectors of the same size. In this case, the
428 * operation is done component-wise resulting in the same size
431 if (type_a
->is_vector() && type_b
->is_vector()) {
432 if (type_a
== type_b
) {
435 _mesa_glsl_error(loc
, state
,
436 "vector size mismatch for arithmetic operator");
437 return glsl_type::error_type
;
441 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
442 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
443 * <vector, vector> have been handled. At least one of the operands must
444 * be matrix. Further, since there are no integer matrix types, the base
445 * type of both operands must be float.
447 assert(type_a
->is_matrix() || type_b
->is_matrix());
448 assert(type_a
->is_float() || type_a
->is_double());
449 assert(type_b
->is_float() || type_b
->is_double());
451 /* "* The operator is add (+), subtract (-), or divide (/), and the
452 * operands are matrices with the same number of rows and the same
453 * number of columns. In this case, the operation is done component-
454 * wise resulting in the same size matrix."
455 * * The operator is multiply (*), where both operands are matrices or
456 * one operand is a vector and the other a matrix. A right vector
457 * operand is treated as a column vector and a left vector operand as a
458 * row vector. In all these cases, it is required that the number of
459 * columns of the left operand is equal to the number of rows of the
460 * right operand. Then, the multiply (*) operation does a linear
461 * algebraic multiply, yielding an object that has the same number of
462 * rows as the left operand and the same number of columns as the right
463 * operand. Section 5.10 "Vector and Matrix Operations" explains in
464 * more detail how vectors and matrices are operated on."
467 if (type_a
== type_b
)
470 const glsl_type
*type
= glsl_type::get_mul_type(type_a
, type_b
);
472 if (type
== glsl_type::error_type
) {
473 _mesa_glsl_error(loc
, state
,
474 "size mismatch for matrix multiplication");
481 /* "All other cases are illegal."
483 _mesa_glsl_error(loc
, state
, "type mismatch");
484 return glsl_type::error_type
;
488 static const struct glsl_type
*
489 unary_arithmetic_result_type(const struct glsl_type
*type
,
490 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
492 /* From GLSL 1.50 spec, page 57:
494 * "The arithmetic unary operators negate (-), post- and pre-increment
495 * and decrement (-- and ++) operate on integer or floating-point
496 * values (including vectors and matrices). All unary operators work
497 * component-wise on their operands. These result with the same type
500 if (!type
->is_numeric()) {
501 _mesa_glsl_error(loc
, state
,
502 "operands to arithmetic operators must be numeric");
503 return glsl_type::error_type
;
510 * \brief Return the result type of a bit-logic operation.
512 * If the given types to the bit-logic operator are invalid, return
513 * glsl_type::error_type.
515 * \param value_a LHS of bit-logic op
516 * \param value_b RHS of bit-logic op
518 static const struct glsl_type
*
519 bit_logic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
521 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
523 const glsl_type
*type_a
= value_a
->type
;
524 const glsl_type
*type_b
= value_b
->type
;
526 if (!state
->check_bitwise_operations_allowed(loc
)) {
527 return glsl_type::error_type
;
530 /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
532 * "The bitwise operators and (&), exclusive-or (^), and inclusive-or
533 * (|). The operands must be of type signed or unsigned integers or
536 if (!type_a
->is_integer_32_64()) {
537 _mesa_glsl_error(loc
, state
, "LHS of `%s' must be an integer",
538 ast_expression::operator_string(op
));
539 return glsl_type::error_type
;
541 if (!type_b
->is_integer_32_64()) {
542 _mesa_glsl_error(loc
, state
, "RHS of `%s' must be an integer",
543 ast_expression::operator_string(op
));
544 return glsl_type::error_type
;
547 /* Prior to GLSL 4.0 / GL_ARB_gpu_shader5, implicit conversions didn't
548 * make sense for bitwise operations, as they don't operate on floats.
550 * GLSL 4.0 added implicit int -> uint conversions, which are relevant
551 * here. It wasn't clear whether or not we should apply them to bitwise
552 * operations. However, Khronos has decided that they should in future
553 * language revisions. Applications also rely on this behavior. We opt
554 * to apply them in general, but issue a portability warning.
556 * See https://www.khronos.org/bugzilla/show_bug.cgi?id=1405
558 if (type_a
->base_type
!= type_b
->base_type
) {
559 if (!apply_implicit_conversion(type_a
, value_b
, state
)
560 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
561 _mesa_glsl_error(loc
, state
,
562 "could not implicitly convert operands to "
564 ast_expression::operator_string(op
));
565 return glsl_type::error_type
;
567 _mesa_glsl_warning(loc
, state
,
568 "some implementations may not support implicit "
569 "int -> uint conversions for `%s' operators; "
570 "consider casting explicitly for portability",
571 ast_expression::operator_string(op
));
573 type_a
= value_a
->type
;
574 type_b
= value_b
->type
;
577 /* "The fundamental types of the operands (signed or unsigned) must
580 if (type_a
->base_type
!= type_b
->base_type
) {
581 _mesa_glsl_error(loc
, state
, "operands of `%s' must have the same "
582 "base type", ast_expression::operator_string(op
));
583 return glsl_type::error_type
;
586 /* "The operands cannot be vectors of differing size." */
587 if (type_a
->is_vector() &&
588 type_b
->is_vector() &&
589 type_a
->vector_elements
!= type_b
->vector_elements
) {
590 _mesa_glsl_error(loc
, state
, "operands of `%s' cannot be vectors of "
591 "different sizes", ast_expression::operator_string(op
));
592 return glsl_type::error_type
;
595 /* "If one operand is a scalar and the other a vector, the scalar is
596 * applied component-wise to the vector, resulting in the same type as
597 * the vector. The fundamental types of the operands [...] will be the
598 * resulting fundamental type."
600 if (type_a
->is_scalar())
606 static const struct glsl_type
*
607 modulus_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
608 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
610 const glsl_type
*type_a
= value_a
->type
;
611 const glsl_type
*type_b
= value_b
->type
;
613 if (!state
->check_version(130, 300, loc
, "operator '%%' is reserved")) {
614 return glsl_type::error_type
;
617 /* Section 5.9 (Expressions) of the GLSL 4.00 specification says:
619 * "The operator modulus (%) operates on signed or unsigned integers or
622 if (!type_a
->is_integer_32_64()) {
623 _mesa_glsl_error(loc
, state
, "LHS of operator %% must be an integer");
624 return glsl_type::error_type
;
626 if (!type_b
->is_integer_32_64()) {
627 _mesa_glsl_error(loc
, state
, "RHS of operator %% must be an integer");
628 return glsl_type::error_type
;
631 /* "If the fundamental types in the operands do not match, then the
632 * conversions from section 4.1.10 "Implicit Conversions" are applied
633 * to create matching types."
635 * Note that GLSL 4.00 (and GL_ARB_gpu_shader5) introduced implicit
636 * int -> uint conversion rules. Prior to that, there were no implicit
637 * conversions. So it's harmless to apply them universally - no implicit
638 * conversions will exist. If the types don't match, we'll receive false,
639 * and raise an error, satisfying the GLSL 1.50 spec, page 56:
641 * "The operand types must both be signed or unsigned."
643 if (!apply_implicit_conversion(type_a
, value_b
, state
) &&
644 !apply_implicit_conversion(type_b
, value_a
, state
)) {
645 _mesa_glsl_error(loc
, state
,
646 "could not implicitly convert operands to "
647 "modulus (%%) operator");
648 return glsl_type::error_type
;
650 type_a
= value_a
->type
;
651 type_b
= value_b
->type
;
653 /* "The operands cannot be vectors of differing size. If one operand is
654 * a scalar and the other vector, then the scalar is applied component-
655 * wise to the vector, resulting in the same type as the vector. If both
656 * are vectors of the same size, the result is computed component-wise."
658 if (type_a
->is_vector()) {
659 if (!type_b
->is_vector()
660 || (type_a
->vector_elements
== type_b
->vector_elements
))
665 /* "The operator modulus (%) is not defined for any other data types
666 * (non-integer types)."
668 _mesa_glsl_error(loc
, state
, "type mismatch");
669 return glsl_type::error_type
;
673 static const struct glsl_type
*
674 relational_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
675 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
677 const glsl_type
*type_a
= value_a
->type
;
678 const glsl_type
*type_b
= value_b
->type
;
680 /* From GLSL 1.50 spec, page 56:
681 * "The relational operators greater than (>), less than (<), greater
682 * than or equal (>=), and less than or equal (<=) operate only on
683 * scalar integer and scalar floating-point expressions."
685 if (!type_a
->is_numeric()
686 || !type_b
->is_numeric()
687 || !type_a
->is_scalar()
688 || !type_b
->is_scalar()) {
689 _mesa_glsl_error(loc
, state
,
690 "operands to relational operators must be scalar and "
692 return glsl_type::error_type
;
695 /* "Either the operands' types must match, or the conversions from
696 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
697 * operand, after which the types must match."
699 if (!apply_implicit_conversion(type_a
, value_b
, state
)
700 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
701 _mesa_glsl_error(loc
, state
,
702 "could not implicitly convert operands to "
703 "relational operator");
704 return glsl_type::error_type
;
706 type_a
= value_a
->type
;
707 type_b
= value_b
->type
;
709 if (type_a
->base_type
!= type_b
->base_type
) {
710 _mesa_glsl_error(loc
, state
, "base type mismatch");
711 return glsl_type::error_type
;
714 /* "The result is scalar Boolean."
716 return glsl_type::bool_type
;
720 * \brief Return the result type of a bit-shift operation.
722 * If the given types to the bit-shift operator are invalid, return
723 * glsl_type::error_type.
725 * \param type_a Type of LHS of bit-shift op
726 * \param type_b Type of RHS of bit-shift op
728 static const struct glsl_type
*
729 shift_result_type(const struct glsl_type
*type_a
,
730 const struct glsl_type
*type_b
,
732 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
734 if (!state
->check_bitwise_operations_allowed(loc
)) {
735 return glsl_type::error_type
;
738 /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
740 * "The shift operators (<<) and (>>). For both operators, the operands
741 * must be signed or unsigned integers or integer vectors. One operand
742 * can be signed while the other is unsigned."
744 if (!type_a
->is_integer_32_64()) {
745 _mesa_glsl_error(loc
, state
, "LHS of operator %s must be an integer or "
746 "integer vector", ast_expression::operator_string(op
));
747 return glsl_type::error_type
;
750 if (!type_b
->is_integer()) {
751 _mesa_glsl_error(loc
, state
, "RHS of operator %s must be an integer or "
752 "integer vector", ast_expression::operator_string(op
));
753 return glsl_type::error_type
;
756 /* "If the first operand is a scalar, the second operand has to be
759 if (type_a
->is_scalar() && !type_b
->is_scalar()) {
760 _mesa_glsl_error(loc
, state
, "if the first operand of %s is scalar, the "
761 "second must be scalar as well",
762 ast_expression::operator_string(op
));
763 return glsl_type::error_type
;
766 /* If both operands are vectors, check that they have same number of
769 if (type_a
->is_vector() &&
770 type_b
->is_vector() &&
771 type_a
->vector_elements
!= type_b
->vector_elements
) {
772 _mesa_glsl_error(loc
, state
, "vector operands to operator %s must "
773 "have same number of elements",
774 ast_expression::operator_string(op
));
775 return glsl_type::error_type
;
778 /* "In all cases, the resulting type will be the same type as the left
785 * Returns the innermost array index expression in an rvalue tree.
786 * This is the largest indexing level -- if an array of blocks, then
787 * it is the block index rather than an indexing expression for an
788 * array-typed member of an array of blocks.
791 find_innermost_array_index(ir_rvalue
*rv
)
793 ir_dereference_array
*last
= NULL
;
795 if (rv
->as_dereference_array()) {
796 last
= rv
->as_dereference_array();
798 } else if (rv
->as_dereference_record())
799 rv
= rv
->as_dereference_record()->record
;
800 else if (rv
->as_swizzle())
801 rv
= rv
->as_swizzle()->val
;
807 return last
->array_index
;
813 * Validates that a value can be assigned to a location with a specified type
815 * Validates that \c rhs can be assigned to some location. If the types are
816 * not an exact match but an automatic conversion is possible, \c rhs will be
820 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
821 * Otherwise the actual RHS to be assigned will be returned. This may be
822 * \c rhs, or it may be \c rhs after some type conversion.
825 * In addition to being used for assignments, this function is used to
826 * type-check return values.
829 validate_assignment(struct _mesa_glsl_parse_state
*state
,
830 YYLTYPE loc
, ir_rvalue
*lhs
,
831 ir_rvalue
*rhs
, bool is_initializer
)
833 /* If there is already some error in the RHS, just return it. Anything
834 * else will lead to an avalanche of error message back to the user.
836 if (rhs
->type
->is_error())
839 /* In the Tessellation Control Shader:
840 * If a per-vertex output variable is used as an l-value, it is an error
841 * if the expression indicating the vertex number is not the identifier
844 if (state
->stage
== MESA_SHADER_TESS_CTRL
&& !lhs
->type
->is_error()) {
845 ir_variable
*var
= lhs
->variable_referenced();
846 if (var
&& var
->data
.mode
== ir_var_shader_out
&& !var
->data
.patch
) {
847 ir_rvalue
*index
= find_innermost_array_index(lhs
);
848 ir_variable
*index_var
= index
? index
->variable_referenced() : NULL
;
849 if (!index_var
|| strcmp(index_var
->name
, "gl_InvocationID") != 0) {
850 _mesa_glsl_error(&loc
, state
,
851 "Tessellation control shader outputs can only "
852 "be indexed by gl_InvocationID");
858 /* If the types are identical, the assignment can trivially proceed.
860 if (rhs
->type
== lhs
->type
)
863 /* If the array element types are the same and the LHS is unsized,
864 * the assignment is okay for initializers embedded in variable
867 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
868 * is handled by ir_dereference::is_lvalue.
870 const glsl_type
*lhs_t
= lhs
->type
;
871 const glsl_type
*rhs_t
= rhs
->type
;
872 bool unsized_array
= false;
873 while(lhs_t
->is_array()) {
875 break; /* the rest of the inner arrays match so break out early */
876 if (!rhs_t
->is_array()) {
877 unsized_array
= false;
878 break; /* number of dimensions mismatch */
880 if (lhs_t
->length
== rhs_t
->length
) {
881 lhs_t
= lhs_t
->fields
.array
;
882 rhs_t
= rhs_t
->fields
.array
;
884 } else if (lhs_t
->is_unsized_array()) {
885 unsized_array
= true;
887 unsized_array
= false;
888 break; /* sized array mismatch */
890 lhs_t
= lhs_t
->fields
.array
;
891 rhs_t
= rhs_t
->fields
.array
;
894 if (is_initializer
) {
897 _mesa_glsl_error(&loc
, state
,
898 "implicitly sized arrays cannot be assigned");
903 /* Check for implicit conversion in GLSL 1.20 */
904 if (apply_implicit_conversion(lhs
->type
, rhs
, state
)) {
905 if (rhs
->type
== lhs
->type
)
909 _mesa_glsl_error(&loc
, state
,
910 "%s of type %s cannot be assigned to "
911 "variable of type %s",
912 is_initializer
? "initializer" : "value",
913 rhs
->type
->name
, lhs
->type
->name
);
919 mark_whole_array_access(ir_rvalue
*access
)
921 ir_dereference_variable
*deref
= access
->as_dereference_variable();
923 if (deref
&& deref
->var
) {
924 deref
->var
->data
.max_array_access
= deref
->type
->length
- 1;
929 do_assignment(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
930 const char *non_lvalue_description
,
931 ir_rvalue
*lhs
, ir_rvalue
*rhs
,
932 ir_rvalue
**out_rvalue
, bool needs_rvalue
,
937 bool error_emitted
= (lhs
->type
->is_error() || rhs
->type
->is_error());
939 ir_variable
*lhs_var
= lhs
->variable_referenced();
941 lhs_var
->data
.assigned
= true;
943 if (!error_emitted
) {
944 if (non_lvalue_description
!= NULL
) {
945 _mesa_glsl_error(&lhs_loc
, state
,
947 non_lvalue_description
);
948 error_emitted
= true;
949 } else if (lhs_var
!= NULL
&& (lhs_var
->data
.read_only
||
950 (lhs_var
->data
.mode
== ir_var_shader_storage
&&
951 lhs_var
->data
.memory_read_only
))) {
952 /* We can have memory_read_only set on both images and buffer variables,
953 * but in the former there is a distinction between assignments to
954 * the variable itself (read_only) and to the memory they point to
955 * (memory_read_only), while in the case of buffer variables there is
956 * no such distinction, that is why this check here is limited to
957 * buffer variables alone.
959 _mesa_glsl_error(&lhs_loc
, state
,
960 "assignment to read-only variable '%s'",
962 error_emitted
= true;
963 } else if (lhs
->type
->is_array() &&
964 !state
->check_version(120, 300, &lhs_loc
,
965 "whole array assignment forbidden")) {
966 /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
968 * "Other binary or unary expressions, non-dereferenced
969 * arrays, function names, swizzles with repeated fields,
970 * and constants cannot be l-values."
972 * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00.
974 error_emitted
= true;
975 } else if (!lhs
->is_lvalue(state
)) {
976 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
977 error_emitted
= true;
982 validate_assignment(state
, lhs_loc
, lhs
, rhs
, is_initializer
);
983 if (new_rhs
!= NULL
) {
986 /* If the LHS array was not declared with a size, it takes it size from
987 * the RHS. If the LHS is an l-value and a whole array, it must be a
988 * dereference of a variable. Any other case would require that the LHS
989 * is either not an l-value or not a whole array.
991 if (lhs
->type
->is_unsized_array()) {
992 ir_dereference
*const d
= lhs
->as_dereference();
996 ir_variable
*const var
= d
->variable_referenced();
1000 if (var
->data
.max_array_access
>= rhs
->type
->array_size()) {
1001 /* FINISHME: This should actually log the location of the RHS. */
1002 _mesa_glsl_error(& lhs_loc
, state
, "array size must be > %u due to "
1004 var
->data
.max_array_access
);
1007 var
->type
= glsl_type::get_array_instance(lhs
->type
->fields
.array
,
1008 rhs
->type
->array_size());
1009 d
->type
= var
->type
;
1011 if (lhs
->type
->is_array()) {
1012 mark_whole_array_access(rhs
);
1013 mark_whole_array_access(lhs
);
1017 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
1018 * but not post_inc) need the converted assigned value as an rvalue
1019 * to handle things like:
1025 if (!error_emitted
) {
1026 ir_variable
*var
= new(ctx
) ir_variable(rhs
->type
, "assignment_tmp",
1028 instructions
->push_tail(var
);
1029 instructions
->push_tail(assign(var
, rhs
));
1031 ir_dereference_variable
*deref_var
=
1032 new(ctx
) ir_dereference_variable(var
);
1033 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, deref_var
));
1034 rvalue
= new(ctx
) ir_dereference_variable(var
);
1036 rvalue
= ir_rvalue::error_value(ctx
);
1038 *out_rvalue
= rvalue
;
1041 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, rhs
));
1045 return error_emitted
;
1049 get_lvalue_copy(exec_list
*instructions
, ir_rvalue
*lvalue
)
1051 void *ctx
= ralloc_parent(lvalue
);
1054 var
= new(ctx
) ir_variable(lvalue
->type
, "_post_incdec_tmp",
1056 instructions
->push_tail(var
);
1058 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
1061 return new(ctx
) ir_dereference_variable(var
);
1066 ast_node::hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
1068 (void) instructions
;
1075 ast_node::has_sequence_subexpression() const
1081 ast_node::set_is_lhs(bool /* new_value */)
1086 ast_function_expression::hir_no_rvalue(exec_list
*instructions
,
1087 struct _mesa_glsl_parse_state
*state
)
1089 (void)hir(instructions
, state
);
1093 ast_aggregate_initializer::hir_no_rvalue(exec_list
*instructions
,
1094 struct _mesa_glsl_parse_state
*state
)
1096 (void)hir(instructions
, state
);
1100 do_comparison(void *mem_ctx
, int operation
, ir_rvalue
*op0
, ir_rvalue
*op1
)
1103 ir_rvalue
*cmp
= NULL
;
1105 if (operation
== ir_binop_all_equal
)
1106 join_op
= ir_binop_logic_and
;
1108 join_op
= ir_binop_logic_or
;
1110 switch (op0
->type
->base_type
) {
1111 case GLSL_TYPE_FLOAT
:
1112 case GLSL_TYPE_FLOAT16
:
1113 case GLSL_TYPE_UINT
:
1115 case GLSL_TYPE_BOOL
:
1116 case GLSL_TYPE_DOUBLE
:
1117 case GLSL_TYPE_UINT64
:
1118 case GLSL_TYPE_INT64
:
1119 case GLSL_TYPE_UINT16
:
1120 case GLSL_TYPE_INT16
:
1121 case GLSL_TYPE_UINT8
:
1122 case GLSL_TYPE_INT8
:
1123 return new(mem_ctx
) ir_expression(operation
, op0
, op1
);
1125 case GLSL_TYPE_ARRAY
: {
1126 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
1127 ir_rvalue
*e0
, *e1
, *result
;
1129 e0
= new(mem_ctx
) ir_dereference_array(op0
->clone(mem_ctx
, NULL
),
1130 new(mem_ctx
) ir_constant(i
));
1131 e1
= new(mem_ctx
) ir_dereference_array(op1
->clone(mem_ctx
, NULL
),
1132 new(mem_ctx
) ir_constant(i
));
1133 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
1136 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
1142 mark_whole_array_access(op0
);
1143 mark_whole_array_access(op1
);
1147 case GLSL_TYPE_STRUCT
: {
1148 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
1149 ir_rvalue
*e0
, *e1
, *result
;
1150 const char *field_name
= op0
->type
->fields
.structure
[i
].name
;
1152 e0
= new(mem_ctx
) ir_dereference_record(op0
->clone(mem_ctx
, NULL
),
1154 e1
= new(mem_ctx
) ir_dereference_record(op1
->clone(mem_ctx
, NULL
),
1156 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
1159 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
1167 case GLSL_TYPE_ERROR
:
1168 case GLSL_TYPE_VOID
:
1169 case GLSL_TYPE_SAMPLER
:
1170 case GLSL_TYPE_IMAGE
:
1171 case GLSL_TYPE_INTERFACE
:
1172 case GLSL_TYPE_ATOMIC_UINT
:
1173 case GLSL_TYPE_SUBROUTINE
:
1174 case GLSL_TYPE_FUNCTION
:
1175 /* I assume a comparison of a struct containing a sampler just
1176 * ignores the sampler present in the type.
1182 cmp
= new(mem_ctx
) ir_constant(true);
1187 /* For logical operations, we want to ensure that the operands are
1188 * scalar booleans. If it isn't, emit an error and return a constant
1189 * boolean to avoid triggering cascading error messages.
1192 get_scalar_boolean_operand(exec_list
*instructions
,
1193 struct _mesa_glsl_parse_state
*state
,
1194 ast_expression
*parent_expr
,
1196 const char *operand_name
,
1197 bool *error_emitted
)
1199 ast_expression
*expr
= parent_expr
->subexpressions
[operand
];
1201 ir_rvalue
*val
= expr
->hir(instructions
, state
);
1203 if (val
->type
->is_boolean() && val
->type
->is_scalar())
1206 if (!*error_emitted
) {
1207 YYLTYPE loc
= expr
->get_location();
1208 _mesa_glsl_error(&loc
, state
, "%s of `%s' must be scalar boolean",
1210 parent_expr
->operator_string(parent_expr
->oper
));
1211 *error_emitted
= true;
1214 return new(ctx
) ir_constant(true);
1218 * If name refers to a builtin array whose maximum allowed size is less than
1219 * size, report an error and return true. Otherwise return false.
1222 check_builtin_array_max_size(const char *name
, unsigned size
,
1223 YYLTYPE loc
, struct _mesa_glsl_parse_state
*state
)
1225 if ((strcmp("gl_TexCoord", name
) == 0)
1226 && (size
> state
->Const
.MaxTextureCoords
)) {
1227 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1229 * "The size [of gl_TexCoord] can be at most
1230 * gl_MaxTextureCoords."
1232 _mesa_glsl_error(&loc
, state
, "`gl_TexCoord' array size cannot "
1233 "be larger than gl_MaxTextureCoords (%u)",
1234 state
->Const
.MaxTextureCoords
);
1235 } else if (strcmp("gl_ClipDistance", name
) == 0) {
1236 state
->clip_dist_size
= size
;
1237 if (size
+ state
->cull_dist_size
> state
->Const
.MaxClipPlanes
) {
1238 /* From section 7.1 (Vertex Shader Special Variables) of the
1241 * "The gl_ClipDistance array is predeclared as unsized and
1242 * must be sized by the shader either redeclaring it with a
1243 * size or indexing it only with integral constant
1244 * expressions. ... The size can be at most
1245 * gl_MaxClipDistances."
1247 _mesa_glsl_error(&loc
, state
, "`gl_ClipDistance' array size cannot "
1248 "be larger than gl_MaxClipDistances (%u)",
1249 state
->Const
.MaxClipPlanes
);
1251 } else if (strcmp("gl_CullDistance", name
) == 0) {
1252 state
->cull_dist_size
= size
;
1253 if (size
+ state
->clip_dist_size
> state
->Const
.MaxClipPlanes
) {
1254 /* From the ARB_cull_distance spec:
1256 * "The gl_CullDistance array is predeclared as unsized and
1257 * must be sized by the shader either redeclaring it with
1258 * a size or indexing it only with integral constant
1259 * expressions. The size determines the number and set of
1260 * enabled cull distances and can be at most
1261 * gl_MaxCullDistances."
1263 _mesa_glsl_error(&loc
, state
, "`gl_CullDistance' array size cannot "
1264 "be larger than gl_MaxCullDistances (%u)",
1265 state
->Const
.MaxClipPlanes
);
1271 * Create the constant 1, of a which is appropriate for incrementing and
1272 * decrementing values of the given GLSL type. For example, if type is vec4,
1273 * this creates a constant value of 1.0 having type float.
1275 * If the given type is invalid for increment and decrement operators, return
1276 * a floating point 1--the error will be detected later.
1279 constant_one_for_inc_dec(void *ctx
, const glsl_type
*type
)
1281 switch (type
->base_type
) {
1282 case GLSL_TYPE_UINT
:
1283 return new(ctx
) ir_constant((unsigned) 1);
1285 return new(ctx
) ir_constant(1);
1286 case GLSL_TYPE_UINT64
:
1287 return new(ctx
) ir_constant((uint64_t) 1);
1288 case GLSL_TYPE_INT64
:
1289 return new(ctx
) ir_constant((int64_t) 1);
1291 case GLSL_TYPE_FLOAT
:
1292 return new(ctx
) ir_constant(1.0f
);
1297 ast_expression::hir(exec_list
*instructions
,
1298 struct _mesa_glsl_parse_state
*state
)
1300 return do_hir(instructions
, state
, true);
1304 ast_expression::hir_no_rvalue(exec_list
*instructions
,
1305 struct _mesa_glsl_parse_state
*state
)
1307 do_hir(instructions
, state
, false);
1311 ast_expression::set_is_lhs(bool new_value
)
1313 /* is_lhs is tracked only to print "variable used uninitialized" warnings,
1314 * if we lack an identifier we can just skip it.
1316 if (this->primary_expression
.identifier
== NULL
)
1319 this->is_lhs
= new_value
;
1321 /* We need to go through the subexpressions tree to cover cases like
1322 * ast_field_selection
1324 if (this->subexpressions
[0] != NULL
)
1325 this->subexpressions
[0]->set_is_lhs(new_value
);
1329 ast_expression::do_hir(exec_list
*instructions
,
1330 struct _mesa_glsl_parse_state
*state
,
1334 static const int operations
[AST_NUM_OPERATORS
] = {
1335 -1, /* ast_assign doesn't convert to ir_expression. */
1336 -1, /* ast_plus doesn't convert to ir_expression. */
1346 ir_binop_less
, /* This is correct. See the ast_greater case below. */
1347 ir_binop_gequal
, /* This is correct. See the ast_lequal case below. */
1350 ir_binop_any_nequal
,
1360 /* Note: The following block of expression types actually convert
1361 * to multiple IR instructions.
1363 ir_binop_mul
, /* ast_mul_assign */
1364 ir_binop_div
, /* ast_div_assign */
1365 ir_binop_mod
, /* ast_mod_assign */
1366 ir_binop_add
, /* ast_add_assign */
1367 ir_binop_sub
, /* ast_sub_assign */
1368 ir_binop_lshift
, /* ast_ls_assign */
1369 ir_binop_rshift
, /* ast_rs_assign */
1370 ir_binop_bit_and
, /* ast_and_assign */
1371 ir_binop_bit_xor
, /* ast_xor_assign */
1372 ir_binop_bit_or
, /* ast_or_assign */
1374 -1, /* ast_conditional doesn't convert to ir_expression. */
1375 ir_binop_add
, /* ast_pre_inc. */
1376 ir_binop_sub
, /* ast_pre_dec. */
1377 ir_binop_add
, /* ast_post_inc. */
1378 ir_binop_sub
, /* ast_post_dec. */
1379 -1, /* ast_field_selection doesn't conv to ir_expression. */
1380 -1, /* ast_array_index doesn't convert to ir_expression. */
1381 -1, /* ast_function_call doesn't conv to ir_expression. */
1382 -1, /* ast_identifier doesn't convert to ir_expression. */
1383 -1, /* ast_int_constant doesn't convert to ir_expression. */
1384 -1, /* ast_uint_constant doesn't conv to ir_expression. */
1385 -1, /* ast_float_constant doesn't conv to ir_expression. */
1386 -1, /* ast_bool_constant doesn't conv to ir_expression. */
1387 -1, /* ast_sequence doesn't convert to ir_expression. */
1388 -1, /* ast_aggregate shouldn't ever even get here. */
1390 ir_rvalue
*result
= NULL
;
1392 const struct glsl_type
*type
, *orig_type
;
1393 bool error_emitted
= false;
1396 loc
= this->get_location();
1398 switch (this->oper
) {
1400 unreachable("ast_aggregate: Should never get here.");
1403 this->subexpressions
[0]->set_is_lhs(true);
1404 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1405 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1408 do_assignment(instructions
, state
,
1409 this->subexpressions
[0]->non_lvalue_description
,
1410 op
[0], op
[1], &result
, needs_rvalue
, false,
1411 this->subexpressions
[0]->get_location());
1416 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1418 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1420 error_emitted
= type
->is_error();
1426 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1428 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1430 error_emitted
= type
->is_error();
1432 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1440 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1441 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1443 type
= arithmetic_result_type(op
[0], op
[1],
1444 (this->oper
== ast_mul
),
1446 error_emitted
= type
->is_error();
1448 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1453 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1454 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1456 type
= modulus_result_type(op
[0], op
[1], state
, &loc
);
1458 assert(operations
[this->oper
] == ir_binop_mod
);
1460 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1462 error_emitted
= type
->is_error();
1467 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1468 error_emitted
= true;
1471 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1472 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1473 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1475 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1477 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1484 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1485 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1487 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
1489 /* The relational operators must either generate an error or result
1490 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1492 assert(type
->is_error()
1493 || (type
->is_boolean() && type
->is_scalar()));
1495 /* Like NIR, GLSL IR does not have opcodes for > or <=. Instead, swap
1496 * the arguments and use < or >=.
1498 if (this->oper
== ast_greater
|| this->oper
== ast_lequal
) {
1499 ir_rvalue
*const tmp
= op
[0];
1504 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1506 error_emitted
= type
->is_error();
1511 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1512 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1514 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1516 * "The equality operators equal (==), and not equal (!=)
1517 * operate on all types. They result in a scalar Boolean. If
1518 * the operand types do not match, then there must be a
1519 * conversion from Section 4.1.10 "Implicit Conversions"
1520 * applied to one operand that can make them match, in which
1521 * case this conversion is done."
1524 if (op
[0]->type
== glsl_type::void_type
|| op
[1]->type
== glsl_type::void_type
) {
1525 _mesa_glsl_error(& loc
, state
, "`%s': wrong operand types: "
1526 "no operation `%1$s' exists that takes a left-hand "
1527 "operand of type 'void' or a right operand of type "
1528 "'void'", (this->oper
== ast_equal
) ? "==" : "!=");
1529 error_emitted
= true;
1530 } else if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
1531 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
1532 || (op
[0]->type
!= op
[1]->type
)) {
1533 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
1534 "type", (this->oper
== ast_equal
) ? "==" : "!=");
1535 error_emitted
= true;
1536 } else if ((op
[0]->type
->is_array() || op
[1]->type
->is_array()) &&
1537 !state
->check_version(120, 300, &loc
,
1538 "array comparisons forbidden")) {
1539 error_emitted
= true;
1540 } else if ((op
[0]->type
->contains_subroutine() ||
1541 op
[1]->type
->contains_subroutine())) {
1542 _mesa_glsl_error(&loc
, state
, "subroutine comparisons forbidden");
1543 error_emitted
= true;
1544 } else if ((op
[0]->type
->contains_opaque() ||
1545 op
[1]->type
->contains_opaque())) {
1546 _mesa_glsl_error(&loc
, state
, "opaque type comparisons forbidden");
1547 error_emitted
= true;
1550 if (error_emitted
) {
1551 result
= new(ctx
) ir_constant(false);
1553 result
= do_comparison(ctx
, operations
[this->oper
], op
[0], op
[1]);
1554 assert(result
->type
== glsl_type::bool_type
);
1561 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1562 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1563 type
= bit_logic_result_type(op
[0], op
[1], this->oper
, state
, &loc
);
1564 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1566 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1570 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1572 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1573 error_emitted
= true;
1576 if (!op
[0]->type
->is_integer_32_64()) {
1577 _mesa_glsl_error(&loc
, state
, "operand of `~' must be an integer");
1578 error_emitted
= true;
1581 type
= error_emitted
? glsl_type::error_type
: op
[0]->type
;
1582 result
= new(ctx
) ir_expression(ir_unop_bit_not
, type
, op
[0], NULL
);
1585 case ast_logic_and
: {
1586 exec_list rhs_instructions
;
1587 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1588 "LHS", &error_emitted
);
1589 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1590 "RHS", &error_emitted
);
1592 if (rhs_instructions
.is_empty()) {
1593 result
= new(ctx
) ir_expression(ir_binop_logic_and
, op
[0], op
[1]);
1595 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1598 instructions
->push_tail(tmp
);
1600 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1601 instructions
->push_tail(stmt
);
1603 stmt
->then_instructions
.append_list(&rhs_instructions
);
1604 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1605 ir_assignment
*const then_assign
=
1606 new(ctx
) ir_assignment(then_deref
, op
[1]);
1607 stmt
->then_instructions
.push_tail(then_assign
);
1609 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1610 ir_assignment
*const else_assign
=
1611 new(ctx
) ir_assignment(else_deref
, new(ctx
) ir_constant(false));
1612 stmt
->else_instructions
.push_tail(else_assign
);
1614 result
= new(ctx
) ir_dereference_variable(tmp
);
1619 case ast_logic_or
: {
1620 exec_list rhs_instructions
;
1621 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1622 "LHS", &error_emitted
);
1623 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1624 "RHS", &error_emitted
);
1626 if (rhs_instructions
.is_empty()) {
1627 result
= new(ctx
) ir_expression(ir_binop_logic_or
, op
[0], op
[1]);
1629 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1632 instructions
->push_tail(tmp
);
1634 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1635 instructions
->push_tail(stmt
);
1637 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1638 ir_assignment
*const then_assign
=
1639 new(ctx
) ir_assignment(then_deref
, new(ctx
) ir_constant(true));
1640 stmt
->then_instructions
.push_tail(then_assign
);
1642 stmt
->else_instructions
.append_list(&rhs_instructions
);
1643 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1644 ir_assignment
*const else_assign
=
1645 new(ctx
) ir_assignment(else_deref
, op
[1]);
1646 stmt
->else_instructions
.push_tail(else_assign
);
1648 result
= new(ctx
) ir_dereference_variable(tmp
);
1654 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1656 * "The logical binary operators and (&&), or ( | | ), and
1657 * exclusive or (^^). They operate only on two Boolean
1658 * expressions and result in a Boolean expression."
1660 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0, "LHS",
1662 op
[1] = get_scalar_boolean_operand(instructions
, state
, this, 1, "RHS",
1665 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1670 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1671 "operand", &error_emitted
);
1673 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1677 case ast_mul_assign
:
1678 case ast_div_assign
:
1679 case ast_add_assign
:
1680 case ast_sub_assign
: {
1681 this->subexpressions
[0]->set_is_lhs(true);
1682 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1683 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1685 orig_type
= op
[0]->type
;
1686 type
= arithmetic_result_type(op
[0], op
[1],
1687 (this->oper
== ast_mul_assign
),
1690 if (type
!= orig_type
) {
1691 _mesa_glsl_error(& loc
, state
,
1692 "could not implicitly convert "
1693 "%s to %s", type
->name
, orig_type
->name
);
1694 type
= glsl_type::error_type
;
1697 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1701 do_assignment(instructions
, state
,
1702 this->subexpressions
[0]->non_lvalue_description
,
1703 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1704 &result
, needs_rvalue
, false,
1705 this->subexpressions
[0]->get_location());
1707 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1708 * explicitly test for this because none of the binary expression
1709 * operators allow array operands either.
1715 case ast_mod_assign
: {
1716 this->subexpressions
[0]->set_is_lhs(true);
1717 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1718 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1720 orig_type
= op
[0]->type
;
1721 type
= modulus_result_type(op
[0], op
[1], state
, &loc
);
1723 if (type
!= orig_type
) {
1724 _mesa_glsl_error(& loc
, state
,
1725 "could not implicitly convert "
1726 "%s to %s", type
->name
, orig_type
->name
);
1727 type
= glsl_type::error_type
;
1730 assert(operations
[this->oper
] == ir_binop_mod
);
1732 ir_rvalue
*temp_rhs
;
1733 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1737 do_assignment(instructions
, state
,
1738 this->subexpressions
[0]->non_lvalue_description
,
1739 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1740 &result
, needs_rvalue
, false,
1741 this->subexpressions
[0]->get_location());
1746 case ast_rs_assign
: {
1747 this->subexpressions
[0]->set_is_lhs(true);
1748 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1749 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1750 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1752 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1753 type
, op
[0], op
[1]);
1755 do_assignment(instructions
, state
,
1756 this->subexpressions
[0]->non_lvalue_description
,
1757 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1758 &result
, needs_rvalue
, false,
1759 this->subexpressions
[0]->get_location());
1763 case ast_and_assign
:
1764 case ast_xor_assign
:
1765 case ast_or_assign
: {
1766 this->subexpressions
[0]->set_is_lhs(true);
1767 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1768 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1770 orig_type
= op
[0]->type
;
1771 type
= bit_logic_result_type(op
[0], op
[1], this->oper
, state
, &loc
);
1773 if (type
!= orig_type
) {
1774 _mesa_glsl_error(& loc
, state
,
1775 "could not implicitly convert "
1776 "%s to %s", type
->name
, orig_type
->name
);
1777 type
= glsl_type::error_type
;
1780 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1781 type
, op
[0], op
[1]);
1783 do_assignment(instructions
, state
,
1784 this->subexpressions
[0]->non_lvalue_description
,
1785 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1786 &result
, needs_rvalue
, false,
1787 this->subexpressions
[0]->get_location());
1791 case ast_conditional
: {
1792 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1794 * "The ternary selection operator (?:). It operates on three
1795 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1796 * first expression, which must result in a scalar Boolean."
1798 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1799 "condition", &error_emitted
);
1801 /* The :? operator is implemented by generating an anonymous temporary
1802 * followed by an if-statement. The last instruction in each branch of
1803 * the if-statement assigns a value to the anonymous temporary. This
1804 * temporary is the r-value of the expression.
1806 exec_list then_instructions
;
1807 exec_list else_instructions
;
1809 op
[1] = this->subexpressions
[1]->hir(&then_instructions
, state
);
1810 op
[2] = this->subexpressions
[2]->hir(&else_instructions
, state
);
1812 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1814 * "The second and third expressions can be any type, as
1815 * long their types match, or there is a conversion in
1816 * Section 4.1.10 "Implicit Conversions" that can be applied
1817 * to one of the expressions to make their types match. This
1818 * resulting matching type is the type of the entire
1821 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1822 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1823 || (op
[1]->type
!= op
[2]->type
)) {
1824 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1826 _mesa_glsl_error(& loc
, state
, "second and third operands of ?: "
1827 "operator must have matching types");
1828 error_emitted
= true;
1829 type
= glsl_type::error_type
;
1834 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1836 * "The second and third expressions must be the same type, but can
1837 * be of any type other than an array."
1839 if (type
->is_array() &&
1840 !state
->check_version(120, 300, &loc
,
1841 "second and third operands of ?: operator "
1842 "cannot be arrays")) {
1843 error_emitted
= true;
1846 /* From section 4.1.7 of the GLSL 4.50 spec (Opaque Types):
1848 * "Except for array indexing, structure member selection, and
1849 * parentheses, opaque variables are not allowed to be operands in
1850 * expressions; such use results in a compile-time error."
1852 if (type
->contains_opaque()) {
1853 _mesa_glsl_error(&loc
, state
, "opaque variables cannot be operands "
1854 "of the ?: operator");
1855 error_emitted
= true;
1858 ir_constant
*cond_val
= op
[0]->constant_expression_value(ctx
);
1860 if (then_instructions
.is_empty()
1861 && else_instructions
.is_empty()
1862 && cond_val
!= NULL
) {
1863 result
= cond_val
->value
.b
[0] ? op
[1] : op
[2];
1865 /* The copy to conditional_tmp reads the whole array. */
1866 if (type
->is_array()) {
1867 mark_whole_array_access(op
[1]);
1868 mark_whole_array_access(op
[2]);
1871 ir_variable
*const tmp
=
1872 new(ctx
) ir_variable(type
, "conditional_tmp", ir_var_temporary
);
1873 instructions
->push_tail(tmp
);
1875 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1876 instructions
->push_tail(stmt
);
1878 then_instructions
.move_nodes_to(& stmt
->then_instructions
);
1879 ir_dereference
*const then_deref
=
1880 new(ctx
) ir_dereference_variable(tmp
);
1881 ir_assignment
*const then_assign
=
1882 new(ctx
) ir_assignment(then_deref
, op
[1]);
1883 stmt
->then_instructions
.push_tail(then_assign
);
1885 else_instructions
.move_nodes_to(& stmt
->else_instructions
);
1886 ir_dereference
*const else_deref
=
1887 new(ctx
) ir_dereference_variable(tmp
);
1888 ir_assignment
*const else_assign
=
1889 new(ctx
) ir_assignment(else_deref
, op
[2]);
1890 stmt
->else_instructions
.push_tail(else_assign
);
1892 result
= new(ctx
) ir_dereference_variable(tmp
);
1899 this->non_lvalue_description
= (this->oper
== ast_pre_inc
)
1900 ? "pre-increment operation" : "pre-decrement operation";
1902 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1903 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1905 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1907 ir_rvalue
*temp_rhs
;
1908 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1912 do_assignment(instructions
, state
,
1913 this->subexpressions
[0]->non_lvalue_description
,
1914 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1915 &result
, needs_rvalue
, false,
1916 this->subexpressions
[0]->get_location());
1921 case ast_post_dec
: {
1922 this->non_lvalue_description
= (this->oper
== ast_post_inc
)
1923 ? "post-increment operation" : "post-decrement operation";
1924 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1925 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1927 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1929 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1931 ir_rvalue
*temp_rhs
;
1932 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1935 /* Get a temporary of a copy of the lvalue before it's modified.
1936 * This may get thrown away later.
1938 result
= get_lvalue_copy(instructions
, op
[0]->clone(ctx
, NULL
));
1940 ir_rvalue
*junk_rvalue
;
1942 do_assignment(instructions
, state
,
1943 this->subexpressions
[0]->non_lvalue_description
,
1944 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1945 &junk_rvalue
, false, false,
1946 this->subexpressions
[0]->get_location());
1951 case ast_field_selection
:
1952 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1955 case ast_array_index
: {
1956 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1958 /* Getting if an array is being used uninitialized is beyond what we get
1959 * from ir_value.data.assigned. Setting is_lhs as true would force to
1960 * not raise a uninitialized warning when using an array
1962 subexpressions
[0]->set_is_lhs(true);
1963 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1964 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1966 result
= _mesa_ast_array_index_to_hir(ctx
, state
, op
[0], op
[1],
1969 if (result
->type
->is_error())
1970 error_emitted
= true;
1975 case ast_unsized_array_dim
:
1976 unreachable("ast_unsized_array_dim: Should never get here.");
1978 case ast_function_call
:
1979 /* Should *NEVER* get here. ast_function_call should always be handled
1980 * by ast_function_expression::hir.
1982 unreachable("ast_function_call: handled elsewhere ");
1984 case ast_identifier
: {
1985 /* ast_identifier can appear several places in a full abstract syntax
1986 * tree. This particular use must be at location specified in the grammar
1987 * as 'variable_identifier'.
1990 state
->symbols
->get_variable(this->primary_expression
.identifier
);
1993 /* the identifier might be a subroutine name */
1995 sub_name
= ralloc_asprintf(ctx
, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state
->stage
), this->primary_expression
.identifier
);
1996 var
= state
->symbols
->get_variable(sub_name
);
1997 ralloc_free(sub_name
);
2001 var
->data
.used
= true;
2002 result
= new(ctx
) ir_dereference_variable(var
);
2004 if ((var
->data
.mode
== ir_var_auto
|| var
->data
.mode
== ir_var_shader_out
)
2006 && result
->variable_referenced()->data
.assigned
!= true
2007 && !is_gl_identifier(var
->name
)) {
2008 _mesa_glsl_warning(&loc
, state
, "`%s' used uninitialized",
2009 this->primary_expression
.identifier
);
2012 /* From the EXT_shader_framebuffer_fetch spec:
2014 * "Unless the GL_EXT_shader_framebuffer_fetch extension has been
2015 * enabled in addition, it's an error to use gl_LastFragData if it
2016 * hasn't been explicitly redeclared with layout(noncoherent)."
2018 if (var
->data
.fb_fetch_output
&& var
->data
.memory_coherent
&&
2019 !state
->EXT_shader_framebuffer_fetch_enable
) {
2020 _mesa_glsl_error(&loc
, state
,
2021 "invalid use of framebuffer fetch output not "
2022 "qualified with layout(noncoherent)");
2026 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
2027 this->primary_expression
.identifier
);
2029 result
= ir_rvalue::error_value(ctx
);
2030 error_emitted
= true;
2035 case ast_int_constant
:
2036 result
= new(ctx
) ir_constant(this->primary_expression
.int_constant
);
2039 case ast_uint_constant
:
2040 result
= new(ctx
) ir_constant(this->primary_expression
.uint_constant
);
2043 case ast_float_constant
:
2044 result
= new(ctx
) ir_constant(this->primary_expression
.float_constant
);
2047 case ast_bool_constant
:
2048 result
= new(ctx
) ir_constant(bool(this->primary_expression
.bool_constant
));
2051 case ast_double_constant
:
2052 result
= new(ctx
) ir_constant(this->primary_expression
.double_constant
);
2055 case ast_uint64_constant
:
2056 result
= new(ctx
) ir_constant(this->primary_expression
.uint64_constant
);
2059 case ast_int64_constant
:
2060 result
= new(ctx
) ir_constant(this->primary_expression
.int64_constant
);
2063 case ast_sequence
: {
2064 /* It should not be possible to generate a sequence in the AST without
2065 * any expressions in it.
2067 assert(!this->expressions
.is_empty());
2069 /* The r-value of a sequence is the last expression in the sequence. If
2070 * the other expressions in the sequence do not have side-effects (and
2071 * therefore add instructions to the instruction list), they get dropped
2074 exec_node
*previous_tail
= NULL
;
2075 YYLTYPE previous_operand_loc
= loc
;
2077 foreach_list_typed (ast_node
, ast
, link
, &this->expressions
) {
2078 /* If one of the operands of comma operator does not generate any
2079 * code, we want to emit a warning. At each pass through the loop
2080 * previous_tail will point to the last instruction in the stream
2081 * *before* processing the previous operand. Naturally,
2082 * instructions->get_tail_raw() will point to the last instruction in
2083 * the stream *after* processing the previous operand. If the two
2084 * pointers match, then the previous operand had no effect.
2086 * The warning behavior here differs slightly from GCC. GCC will
2087 * only emit a warning if none of the left-hand operands have an
2088 * effect. However, it will emit a warning for each. I believe that
2089 * there are some cases in C (especially with GCC extensions) where
2090 * it is useful to have an intermediate step in a sequence have no
2091 * effect, but I don't think these cases exist in GLSL. Either way,
2092 * it would be a giant hassle to replicate that behavior.
2094 if (previous_tail
== instructions
->get_tail_raw()) {
2095 _mesa_glsl_warning(&previous_operand_loc
, state
,
2096 "left-hand operand of comma expression has "
2100 /* The tail is directly accessed instead of using the get_tail()
2101 * method for performance reasons. get_tail() has extra code to
2102 * return NULL when the list is empty. We don't care about that
2103 * here, so using get_tail_raw() is fine.
2105 previous_tail
= instructions
->get_tail_raw();
2106 previous_operand_loc
= ast
->get_location();
2108 result
= ast
->hir(instructions
, state
);
2111 /* Any errors should have already been emitted in the loop above.
2113 error_emitted
= true;
2117 type
= NULL
; /* use result->type, not type. */
2118 assert(result
!= NULL
|| !needs_rvalue
);
2120 if (result
&& result
->type
->is_error() && !error_emitted
)
2121 _mesa_glsl_error(& loc
, state
, "type mismatch");
2127 ast_expression::has_sequence_subexpression() const
2129 switch (this->oper
) {
2138 return this->subexpressions
[0]->has_sequence_subexpression();
2160 case ast_array_index
:
2161 case ast_mul_assign
:
2162 case ast_div_assign
:
2163 case ast_add_assign
:
2164 case ast_sub_assign
:
2165 case ast_mod_assign
:
2168 case ast_and_assign
:
2169 case ast_xor_assign
:
2171 return this->subexpressions
[0]->has_sequence_subexpression() ||
2172 this->subexpressions
[1]->has_sequence_subexpression();
2174 case ast_conditional
:
2175 return this->subexpressions
[0]->has_sequence_subexpression() ||
2176 this->subexpressions
[1]->has_sequence_subexpression() ||
2177 this->subexpressions
[2]->has_sequence_subexpression();
2182 case ast_field_selection
:
2183 case ast_identifier
:
2184 case ast_int_constant
:
2185 case ast_uint_constant
:
2186 case ast_float_constant
:
2187 case ast_bool_constant
:
2188 case ast_double_constant
:
2189 case ast_int64_constant
:
2190 case ast_uint64_constant
:
2196 case ast_function_call
:
2197 unreachable("should be handled by ast_function_expression::hir");
2199 case ast_unsized_array_dim
:
2200 unreachable("ast_unsized_array_dim: Should never get here.");
2207 ast_expression_statement::hir(exec_list
*instructions
,
2208 struct _mesa_glsl_parse_state
*state
)
2210 /* It is possible to have expression statements that don't have an
2211 * expression. This is the solitary semicolon:
2213 * for (i = 0; i < 5; i++)
2216 * In this case the expression will be NULL. Test for NULL and don't do
2217 * anything in that case.
2219 if (expression
!= NULL
)
2220 expression
->hir_no_rvalue(instructions
, state
);
2222 /* Statements do not have r-values.
2229 ast_compound_statement::hir(exec_list
*instructions
,
2230 struct _mesa_glsl_parse_state
*state
)
2233 state
->symbols
->push_scope();
2235 foreach_list_typed (ast_node
, ast
, link
, &this->statements
)
2236 ast
->hir(instructions
, state
);
2239 state
->symbols
->pop_scope();
2241 /* Compound statements do not have r-values.
2247 * Evaluate the given exec_node (which should be an ast_node representing
2248 * a single array dimension) and return its integer value.
2251 process_array_size(exec_node
*node
,
2252 struct _mesa_glsl_parse_state
*state
)
2254 void *mem_ctx
= state
;
2256 exec_list dummy_instructions
;
2258 ast_node
*array_size
= exec_node_data(ast_node
, node
, link
);
2261 * Dimensions other than the outermost dimension can by unsized if they
2262 * are immediately sized by a constructor or initializer.
2264 if (((ast_expression
*)array_size
)->oper
== ast_unsized_array_dim
)
2267 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
2268 YYLTYPE loc
= array_size
->get_location();
2271 _mesa_glsl_error(& loc
, state
,
2272 "array size could not be resolved");
2276 if (!ir
->type
->is_integer()) {
2277 _mesa_glsl_error(& loc
, state
,
2278 "array size must be integer type");
2282 if (!ir
->type
->is_scalar()) {
2283 _mesa_glsl_error(& loc
, state
,
2284 "array size must be scalar type");
2288 ir_constant
*const size
= ir
->constant_expression_value(mem_ctx
);
2290 (state
->is_version(120, 300) &&
2291 array_size
->has_sequence_subexpression())) {
2292 _mesa_glsl_error(& loc
, state
, "array size must be a "
2293 "constant valued expression");
2297 if (size
->value
.i
[0] <= 0) {
2298 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
2302 assert(size
->type
== ir
->type
);
2304 /* If the array size is const (and we've verified that
2305 * it is) then no instructions should have been emitted
2306 * when we converted it to HIR. If they were emitted,
2307 * then either the array size isn't const after all, or
2308 * we are emitting unnecessary instructions.
2310 assert(dummy_instructions
.is_empty());
2312 return size
->value
.u
[0];
2315 static const glsl_type
*
2316 process_array_type(YYLTYPE
*loc
, const glsl_type
*base
,
2317 ast_array_specifier
*array_specifier
,
2318 struct _mesa_glsl_parse_state
*state
)
2320 const glsl_type
*array_type
= base
;
2322 if (array_specifier
!= NULL
) {
2323 if (base
->is_array()) {
2325 /* From page 19 (page 25) of the GLSL 1.20 spec:
2327 * "Only one-dimensional arrays may be declared."
2329 if (!state
->check_arrays_of_arrays_allowed(loc
)) {
2330 return glsl_type::error_type
;
2334 for (exec_node
*node
= array_specifier
->array_dimensions
.get_tail_raw();
2335 !node
->is_head_sentinel(); node
= node
->prev
) {
2336 unsigned array_size
= process_array_size(node
, state
);
2337 array_type
= glsl_type::get_array_instance(array_type
, array_size
);
2345 precision_qualifier_allowed(const glsl_type
*type
)
2347 /* Precision qualifiers apply to floating point, integer and opaque
2350 * Section 4.5.2 (Precision Qualifiers) of the GLSL 1.30 spec says:
2351 * "Any floating point or any integer declaration can have the type
2352 * preceded by one of these precision qualifiers [...] Literal
2353 * constants do not have precision qualifiers. Neither do Boolean
2356 * Section 4.5 (Precision and Precision Qualifiers) of the GLSL 1.30
2359 * "Precision qualifiers are added for code portability with OpenGL
2360 * ES, not for functionality. They have the same syntax as in OpenGL
2363 * Section 8 (Built-In Functions) of the GLSL ES 1.00 spec says:
2365 * "uniform lowp sampler2D sampler;
2368 * lowp vec4 col = texture2D (sampler, coord);
2369 * // texture2D returns lowp"
2371 * From this, we infer that GLSL 1.30 (and later) should allow precision
2372 * qualifiers on sampler types just like float and integer types.
2374 const glsl_type
*const t
= type
->without_array();
2376 return (t
->is_float() || t
->is_integer() || t
->contains_opaque()) &&
2381 ast_type_specifier::glsl_type(const char **name
,
2382 struct _mesa_glsl_parse_state
*state
) const
2384 const struct glsl_type
*type
;
2386 if (this->type
!= NULL
)
2389 type
= structure
->type
;
2391 type
= state
->symbols
->get_type(this->type_name
);
2392 *name
= this->type_name
;
2394 YYLTYPE loc
= this->get_location();
2395 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
2401 * From the OpenGL ES 3.0 spec, 4.5.4 Default Precision Qualifiers:
2403 * "The precision statement
2405 * precision precision-qualifier type;
2407 * can be used to establish a default precision qualifier. The type field can
2408 * be either int or float or any of the sampler types, (...) If type is float,
2409 * the directive applies to non-precision-qualified floating point type
2410 * (scalar, vector, and matrix) declarations. If type is int, the directive
2411 * applies to all non-precision-qualified integer type (scalar, vector, signed,
2412 * and unsigned) declarations."
2414 * We use the symbol table to keep the values of the default precisions for
2415 * each 'type' in each scope and we use the 'type' string from the precision
2416 * statement as key in the symbol table. When we want to retrieve the default
2417 * precision associated with a given glsl_type we need to know the type string
2418 * associated with it. This is what this function returns.
2421 get_type_name_for_precision_qualifier(const glsl_type
*type
)
2423 switch (type
->base_type
) {
2424 case GLSL_TYPE_FLOAT
:
2426 case GLSL_TYPE_UINT
:
2429 case GLSL_TYPE_ATOMIC_UINT
:
2430 return "atomic_uint";
2431 case GLSL_TYPE_IMAGE
:
2433 case GLSL_TYPE_SAMPLER
: {
2434 const unsigned type_idx
=
2435 type
->sampler_array
+ 2 * type
->sampler_shadow
;
2436 const unsigned offset
= type
->is_sampler() ? 0 : 4;
2437 assert(type_idx
< 4);
2438 switch (type
->sampled_type
) {
2439 case GLSL_TYPE_FLOAT
:
2440 switch (type
->sampler_dimensionality
) {
2441 case GLSL_SAMPLER_DIM_1D
: {
2442 assert(type
->is_sampler());
2443 static const char *const names
[4] = {
2444 "sampler1D", "sampler1DArray",
2445 "sampler1DShadow", "sampler1DArrayShadow"
2447 return names
[type_idx
];
2449 case GLSL_SAMPLER_DIM_2D
: {
2450 static const char *const names
[8] = {
2451 "sampler2D", "sampler2DArray",
2452 "sampler2DShadow", "sampler2DArrayShadow",
2453 "image2D", "image2DArray", NULL
, NULL
2455 return names
[offset
+ type_idx
];
2457 case GLSL_SAMPLER_DIM_3D
: {
2458 static const char *const names
[8] = {
2459 "sampler3D", NULL
, NULL
, NULL
,
2460 "image3D", NULL
, NULL
, NULL
2462 return names
[offset
+ type_idx
];
2464 case GLSL_SAMPLER_DIM_CUBE
: {
2465 static const char *const names
[8] = {
2466 "samplerCube", "samplerCubeArray",
2467 "samplerCubeShadow", "samplerCubeArrayShadow",
2468 "imageCube", NULL
, NULL
, NULL
2470 return names
[offset
+ type_idx
];
2472 case GLSL_SAMPLER_DIM_MS
: {
2473 assert(type
->is_sampler());
2474 static const char *const names
[4] = {
2475 "sampler2DMS", "sampler2DMSArray", NULL
, NULL
2477 return names
[type_idx
];
2479 case GLSL_SAMPLER_DIM_RECT
: {
2480 assert(type
->is_sampler());
2481 static const char *const names
[4] = {
2482 "samplerRect", NULL
, "samplerRectShadow", NULL
2484 return names
[type_idx
];
2486 case GLSL_SAMPLER_DIM_BUF
: {
2487 static const char *const names
[8] = {
2488 "samplerBuffer", NULL
, NULL
, NULL
,
2489 "imageBuffer", NULL
, NULL
, NULL
2491 return names
[offset
+ type_idx
];
2493 case GLSL_SAMPLER_DIM_EXTERNAL
: {
2494 assert(type
->is_sampler());
2495 static const char *const names
[4] = {
2496 "samplerExternalOES", NULL
, NULL
, NULL
2498 return names
[type_idx
];
2501 unreachable("Unsupported sampler/image dimensionality");
2502 } /* sampler/image float dimensionality */
2505 switch (type
->sampler_dimensionality
) {
2506 case GLSL_SAMPLER_DIM_1D
: {
2507 assert(type
->is_sampler());
2508 static const char *const names
[4] = {
2509 "isampler1D", "isampler1DArray", NULL
, NULL
2511 return names
[type_idx
];
2513 case GLSL_SAMPLER_DIM_2D
: {
2514 static const char *const names
[8] = {
2515 "isampler2D", "isampler2DArray", NULL
, NULL
,
2516 "iimage2D", "iimage2DArray", NULL
, NULL
2518 return names
[offset
+ type_idx
];
2520 case GLSL_SAMPLER_DIM_3D
: {
2521 static const char *const names
[8] = {
2522 "isampler3D", NULL
, NULL
, NULL
,
2523 "iimage3D", NULL
, NULL
, NULL
2525 return names
[offset
+ type_idx
];
2527 case GLSL_SAMPLER_DIM_CUBE
: {
2528 static const char *const names
[8] = {
2529 "isamplerCube", "isamplerCubeArray", NULL
, NULL
,
2530 "iimageCube", NULL
, NULL
, NULL
2532 return names
[offset
+ type_idx
];
2534 case GLSL_SAMPLER_DIM_MS
: {
2535 assert(type
->is_sampler());
2536 static const char *const names
[4] = {
2537 "isampler2DMS", "isampler2DMSArray", NULL
, NULL
2539 return names
[type_idx
];
2541 case GLSL_SAMPLER_DIM_RECT
: {
2542 assert(type
->is_sampler());
2543 static const char *const names
[4] = {
2544 "isamplerRect", NULL
, "isamplerRectShadow", NULL
2546 return names
[type_idx
];
2548 case GLSL_SAMPLER_DIM_BUF
: {
2549 static const char *const names
[8] = {
2550 "isamplerBuffer", NULL
, NULL
, NULL
,
2551 "iimageBuffer", NULL
, NULL
, NULL
2553 return names
[offset
+ type_idx
];
2556 unreachable("Unsupported isampler/iimage dimensionality");
2557 } /* sampler/image int dimensionality */
2559 case GLSL_TYPE_UINT
:
2560 switch (type
->sampler_dimensionality
) {
2561 case GLSL_SAMPLER_DIM_1D
: {
2562 assert(type
->is_sampler());
2563 static const char *const names
[4] = {
2564 "usampler1D", "usampler1DArray", NULL
, NULL
2566 return names
[type_idx
];
2568 case GLSL_SAMPLER_DIM_2D
: {
2569 static const char *const names
[8] = {
2570 "usampler2D", "usampler2DArray", NULL
, NULL
,
2571 "uimage2D", "uimage2DArray", NULL
, NULL
2573 return names
[offset
+ type_idx
];
2575 case GLSL_SAMPLER_DIM_3D
: {
2576 static const char *const names
[8] = {
2577 "usampler3D", NULL
, NULL
, NULL
,
2578 "uimage3D", NULL
, NULL
, NULL
2580 return names
[offset
+ type_idx
];
2582 case GLSL_SAMPLER_DIM_CUBE
: {
2583 static const char *const names
[8] = {
2584 "usamplerCube", "usamplerCubeArray", NULL
, NULL
,
2585 "uimageCube", NULL
, NULL
, NULL
2587 return names
[offset
+ type_idx
];
2589 case GLSL_SAMPLER_DIM_MS
: {
2590 assert(type
->is_sampler());
2591 static const char *const names
[4] = {
2592 "usampler2DMS", "usampler2DMSArray", NULL
, NULL
2594 return names
[type_idx
];
2596 case GLSL_SAMPLER_DIM_RECT
: {
2597 assert(type
->is_sampler());
2598 static const char *const names
[4] = {
2599 "usamplerRect", NULL
, "usamplerRectShadow", NULL
2601 return names
[type_idx
];
2603 case GLSL_SAMPLER_DIM_BUF
: {
2604 static const char *const names
[8] = {
2605 "usamplerBuffer", NULL
, NULL
, NULL
,
2606 "uimageBuffer", NULL
, NULL
, NULL
2608 return names
[offset
+ type_idx
];
2611 unreachable("Unsupported usampler/uimage dimensionality");
2612 } /* sampler/image uint dimensionality */
2615 unreachable("Unsupported sampler/image type");
2616 } /* sampler/image type */
2618 } /* GLSL_TYPE_SAMPLER/GLSL_TYPE_IMAGE */
2621 unreachable("Unsupported type");
2626 select_gles_precision(unsigned qual_precision
,
2627 const glsl_type
*type
,
2628 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
2630 /* Precision qualifiers do not have any meaning in Desktop GLSL.
2631 * In GLES we take the precision from the type qualifier if present,
2632 * otherwise, if the type of the variable allows precision qualifiers at
2633 * all, we look for the default precision qualifier for that type in the
2636 assert(state
->es_shader
);
2638 unsigned precision
= GLSL_PRECISION_NONE
;
2639 if (qual_precision
) {
2640 precision
= qual_precision
;
2641 } else if (precision_qualifier_allowed(type
)) {
2642 const char *type_name
=
2643 get_type_name_for_precision_qualifier(type
->without_array());
2644 assert(type_name
!= NULL
);
2647 state
->symbols
->get_default_precision_qualifier(type_name
);
2648 if (precision
== ast_precision_none
) {
2649 _mesa_glsl_error(loc
, state
,
2650 "No precision specified in this scope for type `%s'",
2656 /* Section 4.1.7.3 (Atomic Counters) of the GLSL ES 3.10 spec says:
2658 * "The default precision of all atomic types is highp. It is an error to
2659 * declare an atomic type with a different precision or to specify the
2660 * default precision for an atomic type to be lowp or mediump."
2662 if (type
->is_atomic_uint() && precision
!= ast_precision_high
) {
2663 _mesa_glsl_error(loc
, state
,
2664 "atomic_uint can only have highp precision qualifier");
2671 ast_fully_specified_type::glsl_type(const char **name
,
2672 struct _mesa_glsl_parse_state
*state
) const
2674 return this->specifier
->glsl_type(name
, state
);
2678 * Determine whether a toplevel variable declaration declares a varying. This
2679 * function operates by examining the variable's mode and the shader target,
2680 * so it correctly identifies linkage variables regardless of whether they are
2681 * declared using the deprecated "varying" syntax or the new "in/out" syntax.
2683 * Passing a non-toplevel variable declaration (e.g. a function parameter) to
2684 * this function will produce undefined results.
2687 is_varying_var(ir_variable
*var
, gl_shader_stage target
)
2690 case MESA_SHADER_VERTEX
:
2691 return var
->data
.mode
== ir_var_shader_out
;
2692 case MESA_SHADER_FRAGMENT
:
2693 return var
->data
.mode
== ir_var_shader_in
;
2695 return var
->data
.mode
== ir_var_shader_out
|| var
->data
.mode
== ir_var_shader_in
;
2700 is_allowed_invariant(ir_variable
*var
, struct _mesa_glsl_parse_state
*state
)
2702 if (is_varying_var(var
, state
->stage
))
2705 /* From Section 4.6.1 ("The Invariant Qualifier") GLSL 1.20 spec:
2706 * "Only variables output from a vertex shader can be candidates
2709 if (!state
->is_version(130, 0))
2713 * Later specs remove this language - so allowed invariant
2714 * on fragment shader outputs as well.
2716 if (state
->stage
== MESA_SHADER_FRAGMENT
&&
2717 var
->data
.mode
== ir_var_shader_out
)
2723 * Matrix layout qualifiers are only allowed on certain types
2726 validate_matrix_layout_for_type(struct _mesa_glsl_parse_state
*state
,
2728 const glsl_type
*type
,
2731 if (var
&& !var
->is_in_buffer_block()) {
2732 /* Layout qualifiers may only apply to interface blocks and fields in
2735 _mesa_glsl_error(loc
, state
,
2736 "uniform block layout qualifiers row_major and "
2737 "column_major may not be applied to variables "
2738 "outside of uniform blocks");
2739 } else if (!type
->without_array()->is_matrix()) {
2740 /* The OpenGL ES 3.0 conformance tests did not originally allow
2741 * matrix layout qualifiers on non-matrices. However, the OpenGL
2742 * 4.4 and OpenGL ES 3.0 (revision TBD) specifications were
2743 * amended to specifically allow these layouts on all types. Emit
2744 * a warning so that people know their code may not be portable.
2746 _mesa_glsl_warning(loc
, state
,
2747 "uniform block layout qualifiers row_major and "
2748 "column_major applied to non-matrix types may "
2749 "be rejected by older compilers");
2754 validate_xfb_buffer_qualifier(YYLTYPE
*loc
,
2755 struct _mesa_glsl_parse_state
*state
,
2756 unsigned xfb_buffer
) {
2757 if (xfb_buffer
>= state
->Const
.MaxTransformFeedbackBuffers
) {
2758 _mesa_glsl_error(loc
, state
,
2759 "invalid xfb_buffer specified %d is larger than "
2760 "MAX_TRANSFORM_FEEDBACK_BUFFERS - 1 (%d).",
2762 state
->Const
.MaxTransformFeedbackBuffers
- 1);
2769 /* From the ARB_enhanced_layouts spec:
2771 * "Variables and block members qualified with *xfb_offset* can be
2772 * scalars, vectors, matrices, structures, and (sized) arrays of these.
2773 * The offset must be a multiple of the size of the first component of
2774 * the first qualified variable or block member, or a compile-time error
2775 * results. Further, if applied to an aggregate containing a double,
2776 * the offset must also be a multiple of 8, and the space taken in the
2777 * buffer will be a multiple of 8.
2780 validate_xfb_offset_qualifier(YYLTYPE
*loc
,
2781 struct _mesa_glsl_parse_state
*state
,
2782 int xfb_offset
, const glsl_type
*type
,
2783 unsigned component_size
) {
2784 const glsl_type
*t_without_array
= type
->without_array();
2786 if (xfb_offset
!= -1 && type
->is_unsized_array()) {
2787 _mesa_glsl_error(loc
, state
,
2788 "xfb_offset can't be used with unsized arrays.");
2792 /* Make sure nested structs don't contain unsized arrays, and validate
2793 * any xfb_offsets on interface members.
2795 if (t_without_array
->is_record() || t_without_array
->is_interface())
2796 for (unsigned int i
= 0; i
< t_without_array
->length
; i
++) {
2797 const glsl_type
*member_t
= t_without_array
->fields
.structure
[i
].type
;
2799 /* When the interface block doesn't have an xfb_offset qualifier then
2800 * we apply the component size rules at the member level.
2802 if (xfb_offset
== -1)
2803 component_size
= member_t
->contains_double() ? 8 : 4;
2805 int xfb_offset
= t_without_array
->fields
.structure
[i
].offset
;
2806 validate_xfb_offset_qualifier(loc
, state
, xfb_offset
, member_t
,
2810 /* Nested structs or interface block without offset may not have had an
2811 * offset applied yet so return.
2813 if (xfb_offset
== -1) {
2817 if (xfb_offset
% component_size
) {
2818 _mesa_glsl_error(loc
, state
,
2819 "invalid qualifier xfb_offset=%d must be a multiple "
2820 "of the first component size of the first qualified "
2821 "variable or block member. Or double if an aggregate "
2822 "that contains a double (%d).",
2823 xfb_offset
, component_size
);
2831 validate_stream_qualifier(YYLTYPE
*loc
, struct _mesa_glsl_parse_state
*state
,
2834 if (stream
>= state
->ctx
->Const
.MaxVertexStreams
) {
2835 _mesa_glsl_error(loc
, state
,
2836 "invalid stream specified %d is larger than "
2837 "MAX_VERTEX_STREAMS - 1 (%d).",
2838 stream
, state
->ctx
->Const
.MaxVertexStreams
- 1);
2846 apply_explicit_binding(struct _mesa_glsl_parse_state
*state
,
2849 const glsl_type
*type
,
2850 const ast_type_qualifier
*qual
)
2852 if (!qual
->flags
.q
.uniform
&& !qual
->flags
.q
.buffer
) {
2853 _mesa_glsl_error(loc
, state
,
2854 "the \"binding\" qualifier only applies to uniforms and "
2855 "shader storage buffer objects");
2859 unsigned qual_binding
;
2860 if (!process_qualifier_constant(state
, loc
, "binding", qual
->binding
,
2865 const struct gl_context
*const ctx
= state
->ctx
;
2866 unsigned elements
= type
->is_array() ? type
->arrays_of_arrays_size() : 1;
2867 unsigned max_index
= qual_binding
+ elements
- 1;
2868 const glsl_type
*base_type
= type
->without_array();
2870 if (base_type
->is_interface()) {
2871 /* UBOs. From page 60 of the GLSL 4.20 specification:
2872 * "If the binding point for any uniform block instance is less than zero,
2873 * or greater than or equal to the implementation-dependent maximum
2874 * number of uniform buffer bindings, a compilation error will occur.
2875 * When the binding identifier is used with a uniform block instanced as
2876 * an array of size N, all elements of the array from binding through
2877 * binding + N – 1 must be within this range."
2879 * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
2881 if (qual
->flags
.q
.uniform
&&
2882 max_index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2883 _mesa_glsl_error(loc
, state
, "layout(binding = %u) for %d UBOs exceeds "
2884 "the maximum number of UBO binding points (%d)",
2885 qual_binding
, elements
,
2886 ctx
->Const
.MaxUniformBufferBindings
);
2890 /* SSBOs. From page 67 of the GLSL 4.30 specification:
2891 * "If the binding point for any uniform or shader storage block instance
2892 * is less than zero, or greater than or equal to the
2893 * implementation-dependent maximum number of uniform buffer bindings, a
2894 * compile-time error will occur. When the binding identifier is used
2895 * with a uniform or shader storage block instanced as an array of size
2896 * N, all elements of the array from binding through binding + N – 1 must
2897 * be within this range."
2899 if (qual
->flags
.q
.buffer
&&
2900 max_index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2901 _mesa_glsl_error(loc
, state
, "layout(binding = %u) for %d SSBOs exceeds "
2902 "the maximum number of SSBO binding points (%d)",
2903 qual_binding
, elements
,
2904 ctx
->Const
.MaxShaderStorageBufferBindings
);
2907 } else if (base_type
->is_sampler()) {
2908 /* Samplers. From page 63 of the GLSL 4.20 specification:
2909 * "If the binding is less than zero, or greater than or equal to the
2910 * implementation-dependent maximum supported number of units, a
2911 * compilation error will occur. When the binding identifier is used
2912 * with an array of size N, all elements of the array from binding
2913 * through binding + N - 1 must be within this range."
2915 unsigned limit
= ctx
->Const
.MaxCombinedTextureImageUnits
;
2917 if (max_index
>= limit
) {
2918 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d samplers "
2919 "exceeds the maximum number of texture image units "
2920 "(%u)", qual_binding
, elements
, limit
);
2924 } else if (base_type
->contains_atomic()) {
2925 assert(ctx
->Const
.MaxAtomicBufferBindings
<= MAX_COMBINED_ATOMIC_BUFFERS
);
2926 if (qual_binding
>= ctx
->Const
.MaxAtomicBufferBindings
) {
2927 _mesa_glsl_error(loc
, state
, "layout(binding = %d) exceeds the "
2928 "maximum number of atomic counter buffer bindings "
2929 "(%u)", qual_binding
,
2930 ctx
->Const
.MaxAtomicBufferBindings
);
2934 } else if ((state
->is_version(420, 310) ||
2935 state
->ARB_shading_language_420pack_enable
) &&
2936 base_type
->is_image()) {
2937 assert(ctx
->Const
.MaxImageUnits
<= MAX_IMAGE_UNITS
);
2938 if (max_index
>= ctx
->Const
.MaxImageUnits
) {
2939 _mesa_glsl_error(loc
, state
, "Image binding %d exceeds the "
2940 "maximum number of image units (%d)", max_index
,
2941 ctx
->Const
.MaxImageUnits
);
2946 _mesa_glsl_error(loc
, state
,
2947 "the \"binding\" qualifier only applies to uniform "
2948 "blocks, storage blocks, opaque variables, or arrays "
2953 var
->data
.explicit_binding
= true;
2954 var
->data
.binding
= qual_binding
;
2960 validate_fragment_flat_interpolation_input(struct _mesa_glsl_parse_state
*state
,
2962 const glsl_interp_mode interpolation
,
2963 const struct glsl_type
*var_type
,
2964 ir_variable_mode mode
)
2966 if (state
->stage
!= MESA_SHADER_FRAGMENT
||
2967 interpolation
== INTERP_MODE_FLAT
||
2968 mode
!= ir_var_shader_in
)
2971 /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
2972 * so must integer vertex outputs.
2974 * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
2975 * "Fragment shader inputs that are signed or unsigned integers or
2976 * integer vectors must be qualified with the interpolation qualifier
2979 * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
2980 * "Fragment shader inputs that are, or contain, signed or unsigned
2981 * integers or integer vectors must be qualified with the
2982 * interpolation qualifier flat."
2984 * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
2985 * "Vertex shader outputs that are, or contain, signed or unsigned
2986 * integers or integer vectors must be qualified with the
2987 * interpolation qualifier flat."
2989 * Note that prior to GLSL 1.50, this requirement applied to vertex
2990 * outputs rather than fragment inputs. That creates problems in the
2991 * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
2992 * desktop GL shaders. For GLSL ES shaders, we follow the spec and
2993 * apply the restriction to both vertex outputs and fragment inputs.
2995 * Note also that the desktop GLSL specs are missing the text "or
2996 * contain"; this is presumably an oversight, since there is no
2997 * reasonable way to interpolate a fragment shader input that contains
2998 * an integer. See Khronos bug #15671.
3000 if (state
->is_version(130, 300)
3001 && var_type
->contains_integer()) {
3002 _mesa_glsl_error(loc
, state
, "if a fragment input is (or contains) "
3003 "an integer, then it must be qualified with 'flat'");
3006 /* Double fragment inputs must be qualified with 'flat'.
3008 * From the "Overview" of the ARB_gpu_shader_fp64 extension spec:
3009 * "This extension does not support interpolation of double-precision
3010 * values; doubles used as fragment shader inputs must be qualified as
3013 * From section 4.3.4 ("Inputs") of the GLSL 4.00 spec:
3014 * "Fragment shader inputs that are signed or unsigned integers, integer
3015 * vectors, or any double-precision floating-point type must be
3016 * qualified with the interpolation qualifier flat."
3018 * Note that the GLSL specs are missing the text "or contain"; this is
3019 * presumably an oversight. See Khronos bug #15671.
3021 * The 'double' type does not exist in GLSL ES so far.
3023 if (state
->has_double()
3024 && var_type
->contains_double()) {
3025 _mesa_glsl_error(loc
, state
, "if a fragment input is (or contains) "
3026 "a double, then it must be qualified with 'flat'");
3029 /* Bindless sampler/image fragment inputs must be qualified with 'flat'.
3031 * From section 4.3.4 of the ARB_bindless_texture spec:
3033 * "(modify last paragraph, p. 35, allowing samplers and images as
3034 * fragment shader inputs) ... Fragment inputs can only be signed and
3035 * unsigned integers and integer vectors, floating point scalars,
3036 * floating-point vectors, matrices, sampler and image types, or arrays
3037 * or structures of these. Fragment shader inputs that are signed or
3038 * unsigned integers, integer vectors, or any double-precision floating-
3039 * point type, or any sampler or image type must be qualified with the
3040 * interpolation qualifier "flat"."
3042 if (state
->has_bindless()
3043 && (var_type
->contains_sampler() || var_type
->contains_image())) {
3044 _mesa_glsl_error(loc
, state
, "if a fragment input is (or contains) "
3045 "a bindless sampler (or image), then it must be "
3046 "qualified with 'flat'");
3051 validate_interpolation_qualifier(struct _mesa_glsl_parse_state
*state
,
3053 const glsl_interp_mode interpolation
,
3054 const struct ast_type_qualifier
*qual
,
3055 const struct glsl_type
*var_type
,
3056 ir_variable_mode mode
)
3058 /* Interpolation qualifiers can only apply to shader inputs or outputs, but
3059 * not to vertex shader inputs nor fragment shader outputs.
3061 * From section 4.3 ("Storage Qualifiers") of the GLSL 1.30 spec:
3062 * "Outputs from a vertex shader (out) and inputs to a fragment
3063 * shader (in) can be further qualified with one or more of these
3064 * interpolation qualifiers"
3066 * "These interpolation qualifiers may only precede the qualifiers in,
3067 * centroid in, out, or centroid out in a declaration. They do not apply
3068 * to the deprecated storage qualifiers varying or centroid
3069 * varying. They also do not apply to inputs into a vertex shader or
3070 * outputs from a fragment shader."
3072 * From section 4.3 ("Storage Qualifiers") of the GLSL ES 3.00 spec:
3073 * "Outputs from a shader (out) and inputs to a shader (in) can be
3074 * further qualified with one of these interpolation qualifiers."
3076 * "These interpolation qualifiers may only precede the qualifiers
3077 * in, centroid in, out, or centroid out in a declaration. They do
3078 * not apply to inputs into a vertex shader or outputs from a
3081 if (state
->is_version(130, 300)
3082 && interpolation
!= INTERP_MODE_NONE
) {
3083 const char *i
= interpolation_string(interpolation
);
3084 if (mode
!= ir_var_shader_in
&& mode
!= ir_var_shader_out
)
3085 _mesa_glsl_error(loc
, state
,
3086 "interpolation qualifier `%s' can only be applied to "
3087 "shader inputs or outputs.", i
);
3089 switch (state
->stage
) {
3090 case MESA_SHADER_VERTEX
:
3091 if (mode
== ir_var_shader_in
) {
3092 _mesa_glsl_error(loc
, state
,
3093 "interpolation qualifier '%s' cannot be applied to "
3094 "vertex shader inputs", i
);
3097 case MESA_SHADER_FRAGMENT
:
3098 if (mode
== ir_var_shader_out
) {
3099 _mesa_glsl_error(loc
, state
,
3100 "interpolation qualifier '%s' cannot be applied to "
3101 "fragment shader outputs", i
);
3109 /* Interpolation qualifiers cannot be applied to 'centroid' and
3110 * 'centroid varying'.
3112 * From section 4.3 ("Storage Qualifiers") of the GLSL 1.30 spec:
3113 * "interpolation qualifiers may only precede the qualifiers in,
3114 * centroid in, out, or centroid out in a declaration. They do not apply
3115 * to the deprecated storage qualifiers varying or centroid varying."
3117 * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
3119 if (state
->is_version(130, 0)
3120 && interpolation
!= INTERP_MODE_NONE
3121 && qual
->flags
.q
.varying
) {
3123 const char *i
= interpolation_string(interpolation
);
3125 if (qual
->flags
.q
.centroid
)
3126 s
= "centroid varying";
3130 _mesa_glsl_error(loc
, state
,
3131 "qualifier '%s' cannot be applied to the "
3132 "deprecated storage qualifier '%s'", i
, s
);
3135 validate_fragment_flat_interpolation_input(state
, loc
, interpolation
,
3139 static glsl_interp_mode
3140 interpret_interpolation_qualifier(const struct ast_type_qualifier
*qual
,
3141 const struct glsl_type
*var_type
,
3142 ir_variable_mode mode
,
3143 struct _mesa_glsl_parse_state
*state
,
3146 glsl_interp_mode interpolation
;
3147 if (qual
->flags
.q
.flat
)
3148 interpolation
= INTERP_MODE_FLAT
;
3149 else if (qual
->flags
.q
.noperspective
)
3150 interpolation
= INTERP_MODE_NOPERSPECTIVE
;
3151 else if (qual
->flags
.q
.smooth
)
3152 interpolation
= INTERP_MODE_SMOOTH
;
3154 interpolation
= INTERP_MODE_NONE
;
3156 validate_interpolation_qualifier(state
, loc
,
3158 qual
, var_type
, mode
);
3160 return interpolation
;
3165 apply_explicit_location(const struct ast_type_qualifier
*qual
,
3167 struct _mesa_glsl_parse_state
*state
,
3172 unsigned qual_location
;
3173 if (!process_qualifier_constant(state
, loc
, "location", qual
->location
,
3178 /* Checks for GL_ARB_explicit_uniform_location. */
3179 if (qual
->flags
.q
.uniform
) {
3180 if (!state
->check_explicit_uniform_location_allowed(loc
, var
))
3183 const struct gl_context
*const ctx
= state
->ctx
;
3184 unsigned max_loc
= qual_location
+ var
->type
->uniform_locations() - 1;
3186 if (max_loc
>= ctx
->Const
.MaxUserAssignableUniformLocations
) {
3187 _mesa_glsl_error(loc
, state
, "location(s) consumed by uniform %s "
3188 ">= MAX_UNIFORM_LOCATIONS (%u)", var
->name
,
3189 ctx
->Const
.MaxUserAssignableUniformLocations
);
3193 var
->data
.explicit_location
= true;
3194 var
->data
.location
= qual_location
;
3198 /* Between GL_ARB_explicit_attrib_location an
3199 * GL_ARB_separate_shader_objects, the inputs and outputs of any shader
3200 * stage can be assigned explicit locations. The checking here associates
3201 * the correct extension with the correct stage's input / output:
3205 * vertex explicit_loc sso
3206 * tess control sso sso
3209 * fragment sso explicit_loc
3211 switch (state
->stage
) {
3212 case MESA_SHADER_VERTEX
:
3213 if (var
->data
.mode
== ir_var_shader_in
) {
3214 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
3220 if (var
->data
.mode
== ir_var_shader_out
) {
3221 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
3230 case MESA_SHADER_TESS_CTRL
:
3231 case MESA_SHADER_TESS_EVAL
:
3232 case MESA_SHADER_GEOMETRY
:
3233 if (var
->data
.mode
== ir_var_shader_in
|| var
->data
.mode
== ir_var_shader_out
) {
3234 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
3243 case MESA_SHADER_FRAGMENT
:
3244 if (var
->data
.mode
== ir_var_shader_in
) {
3245 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
3251 if (var
->data
.mode
== ir_var_shader_out
) {
3252 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
3261 case MESA_SHADER_COMPUTE
:
3262 _mesa_glsl_error(loc
, state
,
3263 "compute shader variables cannot be given "
3264 "explicit locations");
3272 _mesa_glsl_error(loc
, state
,
3273 "%s cannot be given an explicit location in %s shader",
3275 _mesa_shader_stage_to_string(state
->stage
));
3277 var
->data
.explicit_location
= true;
3279 switch (state
->stage
) {
3280 case MESA_SHADER_VERTEX
:
3281 var
->data
.location
= (var
->data
.mode
== ir_var_shader_in
)
3282 ? (qual_location
+ VERT_ATTRIB_GENERIC0
)
3283 : (qual_location
+ VARYING_SLOT_VAR0
);
3286 case MESA_SHADER_TESS_CTRL
:
3287 case MESA_SHADER_TESS_EVAL
:
3288 case MESA_SHADER_GEOMETRY
:
3289 if (var
->data
.patch
)
3290 var
->data
.location
= qual_location
+ VARYING_SLOT_PATCH0
;
3292 var
->data
.location
= qual_location
+ VARYING_SLOT_VAR0
;
3295 case MESA_SHADER_FRAGMENT
:
3296 var
->data
.location
= (var
->data
.mode
== ir_var_shader_out
)
3297 ? (qual_location
+ FRAG_RESULT_DATA0
)
3298 : (qual_location
+ VARYING_SLOT_VAR0
);
3301 assert(!"Unexpected shader type");
3305 /* Check if index was set for the uniform instead of the function */
3306 if (qual
->flags
.q
.explicit_index
&& qual
->is_subroutine_decl()) {
3307 _mesa_glsl_error(loc
, state
, "an index qualifier can only be "
3308 "used with subroutine functions");
3312 unsigned qual_index
;
3313 if (qual
->flags
.q
.explicit_index
&&
3314 process_qualifier_constant(state
, loc
, "index", qual
->index
,
3316 /* From the GLSL 4.30 specification, section 4.4.2 (Output
3317 * Layout Qualifiers):
3319 * "It is also a compile-time error if a fragment shader
3320 * sets a layout index to less than 0 or greater than 1."
3322 * Older specifications don't mandate a behavior; we take
3323 * this as a clarification and always generate the error.
3325 if (qual_index
> 1) {
3326 _mesa_glsl_error(loc
, state
,
3327 "explicit index may only be 0 or 1");
3329 var
->data
.explicit_index
= true;
3330 var
->data
.index
= qual_index
;
3337 validate_storage_for_sampler_image_types(ir_variable
*var
,
3338 struct _mesa_glsl_parse_state
*state
,
3341 /* From section 4.1.7 of the GLSL 4.40 spec:
3343 * "[Opaque types] can only be declared as function
3344 * parameters or uniform-qualified variables."
3346 * From section 4.1.7 of the ARB_bindless_texture spec:
3348 * "Samplers may be declared as shader inputs and outputs, as uniform
3349 * variables, as temporary variables, and as function parameters."
3351 * From section 4.1.X of the ARB_bindless_texture spec:
3353 * "Images may be declared as shader inputs and outputs, as uniform
3354 * variables, as temporary variables, and as function parameters."
3356 if (state
->has_bindless()) {
3357 if (var
->data
.mode
!= ir_var_auto
&&
3358 var
->data
.mode
!= ir_var_uniform
&&
3359 var
->data
.mode
!= ir_var_shader_in
&&
3360 var
->data
.mode
!= ir_var_shader_out
&&
3361 var
->data
.mode
!= ir_var_function_in
&&
3362 var
->data
.mode
!= ir_var_function_out
&&
3363 var
->data
.mode
!= ir_var_function_inout
) {
3364 _mesa_glsl_error(loc
, state
, "bindless image/sampler variables may "
3365 "only be declared as shader inputs and outputs, as "
3366 "uniform variables, as temporary variables and as "
3367 "function parameters");
3371 if (var
->data
.mode
!= ir_var_uniform
&&
3372 var
->data
.mode
!= ir_var_function_in
) {
3373 _mesa_glsl_error(loc
, state
, "image/sampler variables may only be "
3374 "declared as function parameters or "
3375 "uniform-qualified global variables");
3383 validate_memory_qualifier_for_type(struct _mesa_glsl_parse_state
*state
,
3385 const struct ast_type_qualifier
*qual
,
3386 const glsl_type
*type
)
3388 /* From Section 4.10 (Memory Qualifiers) of the GLSL 4.50 spec:
3390 * "Memory qualifiers are only supported in the declarations of image
3391 * variables, buffer variables, and shader storage blocks; it is an error
3392 * to use such qualifiers in any other declarations.
3394 if (!type
->is_image() && !qual
->flags
.q
.buffer
) {
3395 if (qual
->flags
.q
.read_only
||
3396 qual
->flags
.q
.write_only
||
3397 qual
->flags
.q
.coherent
||
3398 qual
->flags
.q
._volatile
||
3399 qual
->flags
.q
.restrict_flag
) {
3400 _mesa_glsl_error(loc
, state
, "memory qualifiers may only be applied "
3401 "in the declarations of image variables, buffer "
3402 "variables, and shader storage blocks");
3410 validate_image_format_qualifier_for_type(struct _mesa_glsl_parse_state
*state
,
3412 const struct ast_type_qualifier
*qual
,
3413 const glsl_type
*type
)
3415 /* From section 4.4.6.2 (Format Layout Qualifiers) of the GLSL 4.50 spec:
3417 * "Format layout qualifiers can be used on image variable declarations
3418 * (those declared with a basic type having “image ” in its keyword)."
3420 if (!type
->is_image() && qual
->flags
.q
.explicit_image_format
) {
3421 _mesa_glsl_error(loc
, state
, "format layout qualifiers may only be "
3422 "applied to images");
3429 apply_image_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
3431 struct _mesa_glsl_parse_state
*state
,
3434 const glsl_type
*base_type
= var
->type
->without_array();
3436 if (!validate_image_format_qualifier_for_type(state
, loc
, qual
, base_type
) ||
3437 !validate_memory_qualifier_for_type(state
, loc
, qual
, base_type
))
3440 if (!base_type
->is_image())
3443 if (!validate_storage_for_sampler_image_types(var
, state
, loc
))
3446 var
->data
.memory_read_only
|= qual
->flags
.q
.read_only
;
3447 var
->data
.memory_write_only
|= qual
->flags
.q
.write_only
;
3448 var
->data
.memory_coherent
|= qual
->flags
.q
.coherent
;
3449 var
->data
.memory_volatile
|= qual
->flags
.q
._volatile
;
3450 var
->data
.memory_restrict
|= qual
->flags
.q
.restrict_flag
;
3452 if (qual
->flags
.q
.explicit_image_format
) {
3453 if (var
->data
.mode
== ir_var_function_in
) {
3454 _mesa_glsl_error(loc
, state
, "format qualifiers cannot be used on "
3455 "image function parameters");
3458 if (qual
->image_base_type
!= base_type
->sampled_type
) {
3459 _mesa_glsl_error(loc
, state
, "format qualifier doesn't match the base "
3460 "data type of the image");
3463 var
->data
.image_format
= qual
->image_format
;
3465 if (var
->data
.mode
== ir_var_uniform
) {
3466 if (state
->es_shader
) {
3467 _mesa_glsl_error(loc
, state
, "all image uniforms must have a "
3468 "format layout qualifier");
3469 } else if (!qual
->flags
.q
.write_only
) {
3470 _mesa_glsl_error(loc
, state
, "image uniforms not qualified with "
3471 "`writeonly' must have a format layout qualifier");
3474 var
->data
.image_format
= GL_NONE
;
3477 /* From page 70 of the GLSL ES 3.1 specification:
3479 * "Except for image variables qualified with the format qualifiers r32f,
3480 * r32i, and r32ui, image variables must specify either memory qualifier
3481 * readonly or the memory qualifier writeonly."
3483 if (state
->es_shader
&&
3484 var
->data
.image_format
!= GL_R32F
&&
3485 var
->data
.image_format
!= GL_R32I
&&
3486 var
->data
.image_format
!= GL_R32UI
&&
3487 !var
->data
.memory_read_only
&&
3488 !var
->data
.memory_write_only
) {
3489 _mesa_glsl_error(loc
, state
, "image variables of format other than r32f, "
3490 "r32i or r32ui must be qualified `readonly' or "
3495 static inline const char*
3496 get_layout_qualifier_string(bool origin_upper_left
, bool pixel_center_integer
)
3498 if (origin_upper_left
&& pixel_center_integer
)
3499 return "origin_upper_left, pixel_center_integer";
3500 else if (origin_upper_left
)
3501 return "origin_upper_left";
3502 else if (pixel_center_integer
)
3503 return "pixel_center_integer";
3509 is_conflicting_fragcoord_redeclaration(struct _mesa_glsl_parse_state
*state
,
3510 const struct ast_type_qualifier
*qual
)
3512 /* If gl_FragCoord was previously declared, and the qualifiers were
3513 * different in any way, return true.
3515 if (state
->fs_redeclares_gl_fragcoord
) {
3516 return (state
->fs_pixel_center_integer
!= qual
->flags
.q
.pixel_center_integer
3517 || state
->fs_origin_upper_left
!= qual
->flags
.q
.origin_upper_left
);
3524 validate_array_dimensions(const glsl_type
*t
,
3525 struct _mesa_glsl_parse_state
*state
,
3527 if (t
->is_array()) {
3528 t
= t
->fields
.array
;
3529 while (t
->is_array()) {
3530 if (t
->is_unsized_array()) {
3531 _mesa_glsl_error(loc
, state
,
3532 "only the outermost array dimension can "
3537 t
= t
->fields
.array
;
3543 apply_bindless_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
3545 struct _mesa_glsl_parse_state
*state
,
3548 bool has_local_qualifiers
= qual
->flags
.q
.bindless_sampler
||
3549 qual
->flags
.q
.bindless_image
||
3550 qual
->flags
.q
.bound_sampler
||
3551 qual
->flags
.q
.bound_image
;
3553 /* The ARB_bindless_texture spec says:
3555 * "Modify Section 4.4.6 Opaque-Uniform Layout Qualifiers of the GLSL 4.30
3558 * "If these layout qualifiers are applied to other types of default block
3559 * uniforms, or variables with non-uniform storage, a compile-time error
3560 * will be generated."
3562 if (has_local_qualifiers
&& !qual
->flags
.q
.uniform
) {
3563 _mesa_glsl_error(loc
, state
, "ARB_bindless_texture layout qualifiers "
3564 "can only be applied to default block uniforms or "
3565 "variables with uniform storage");
3569 /* The ARB_bindless_texture spec doesn't state anything in this situation,
3570 * but it makes sense to only allow bindless_sampler/bound_sampler for
3571 * sampler types, and respectively bindless_image/bound_image for image
3574 if ((qual
->flags
.q
.bindless_sampler
|| qual
->flags
.q
.bound_sampler
) &&
3575 !var
->type
->contains_sampler()) {
3576 _mesa_glsl_error(loc
, state
, "bindless_sampler or bound_sampler can only "
3577 "be applied to sampler types");
3581 if ((qual
->flags
.q
.bindless_image
|| qual
->flags
.q
.bound_image
) &&
3582 !var
->type
->contains_image()) {
3583 _mesa_glsl_error(loc
, state
, "bindless_image or bound_image can only be "
3584 "applied to image types");
3588 /* The bindless_sampler/bindless_image (and respectively
3589 * bound_sampler/bound_image) layout qualifiers can be set at global and at
3592 if (var
->type
->contains_sampler() || var
->type
->contains_image()) {
3593 var
->data
.bindless
= qual
->flags
.q
.bindless_sampler
||
3594 qual
->flags
.q
.bindless_image
||
3595 state
->bindless_sampler_specified
||
3596 state
->bindless_image_specified
;
3598 var
->data
.bound
= qual
->flags
.q
.bound_sampler
||
3599 qual
->flags
.q
.bound_image
||
3600 state
->bound_sampler_specified
||
3601 state
->bound_image_specified
;
3606 apply_layout_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
3608 struct _mesa_glsl_parse_state
*state
,
3611 if (var
->name
!= NULL
&& strcmp(var
->name
, "gl_FragCoord") == 0) {
3613 /* Section 4.3.8.1, page 39 of GLSL 1.50 spec says:
3615 * "Within any shader, the first redeclarations of gl_FragCoord
3616 * must appear before any use of gl_FragCoord."
3618 * Generate a compiler error if above condition is not met by the
3621 ir_variable
*earlier
= state
->symbols
->get_variable("gl_FragCoord");
3622 if (earlier
!= NULL
&&
3623 earlier
->data
.used
&&
3624 !state
->fs_redeclares_gl_fragcoord
) {
3625 _mesa_glsl_error(loc
, state
,
3626 "gl_FragCoord used before its first redeclaration "
3627 "in fragment shader");
3630 /* Make sure all gl_FragCoord redeclarations specify the same layout
3633 if (is_conflicting_fragcoord_redeclaration(state
, qual
)) {
3634 const char *const qual_string
=
3635 get_layout_qualifier_string(qual
->flags
.q
.origin_upper_left
,
3636 qual
->flags
.q
.pixel_center_integer
);
3638 const char *const state_string
=
3639 get_layout_qualifier_string(state
->fs_origin_upper_left
,
3640 state
->fs_pixel_center_integer
);
3642 _mesa_glsl_error(loc
, state
,
3643 "gl_FragCoord redeclared with different layout "
3644 "qualifiers (%s) and (%s) ",
3648 state
->fs_origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
3649 state
->fs_pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
3650 state
->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers
=
3651 !qual
->flags
.q
.origin_upper_left
&& !qual
->flags
.q
.pixel_center_integer
;
3652 state
->fs_redeclares_gl_fragcoord
=
3653 state
->fs_origin_upper_left
||
3654 state
->fs_pixel_center_integer
||
3655 state
->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers
;
3658 var
->data
.pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
3659 var
->data
.origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
3660 if ((qual
->flags
.q
.origin_upper_left
|| qual
->flags
.q
.pixel_center_integer
)
3661 && (strcmp(var
->name
, "gl_FragCoord") != 0)) {
3662 const char *const qual_string
= (qual
->flags
.q
.origin_upper_left
)
3663 ? "origin_upper_left" : "pixel_center_integer";
3665 _mesa_glsl_error(loc
, state
,
3666 "layout qualifier `%s' can only be applied to "
3667 "fragment shader input `gl_FragCoord'",
3671 if (qual
->flags
.q
.explicit_location
) {
3672 apply_explicit_location(qual
, var
, state
, loc
);
3674 if (qual
->flags
.q
.explicit_component
) {
3675 unsigned qual_component
;
3676 if (process_qualifier_constant(state
, loc
, "component",
3677 qual
->component
, &qual_component
)) {
3678 const glsl_type
*type
= var
->type
->without_array();
3679 unsigned components
= type
->component_slots();
3681 if (type
->is_matrix() || type
->is_record()) {
3682 _mesa_glsl_error(loc
, state
, "component layout qualifier "
3683 "cannot be applied to a matrix, a structure, "
3684 "a block, or an array containing any of "
3686 } else if (qual_component
!= 0 &&
3687 (qual_component
+ components
- 1) > 3) {
3688 _mesa_glsl_error(loc
, state
, "component overflow (%u > 3)",
3689 (qual_component
+ components
- 1));
3690 } else if (qual_component
== 1 && type
->is_64bit()) {
3691 /* We don't bother checking for 3 as it should be caught by the
3692 * overflow check above.
3694 _mesa_glsl_error(loc
, state
, "doubles cannot begin at "
3695 "component 1 or 3");
3697 var
->data
.explicit_component
= true;
3698 var
->data
.location_frac
= qual_component
;
3702 } else if (qual
->flags
.q
.explicit_index
) {
3703 if (!qual
->subroutine_list
)
3704 _mesa_glsl_error(loc
, state
,
3705 "explicit index requires explicit location");
3706 } else if (qual
->flags
.q
.explicit_component
) {
3707 _mesa_glsl_error(loc
, state
,
3708 "explicit component requires explicit location");
3711 if (qual
->flags
.q
.explicit_binding
) {
3712 apply_explicit_binding(state
, loc
, var
, var
->type
, qual
);
3715 if (state
->stage
== MESA_SHADER_GEOMETRY
&&
3716 qual
->flags
.q
.out
&& qual
->flags
.q
.stream
) {
3717 unsigned qual_stream
;
3718 if (process_qualifier_constant(state
, loc
, "stream", qual
->stream
,
3720 validate_stream_qualifier(loc
, state
, qual_stream
)) {
3721 var
->data
.stream
= qual_stream
;
3725 if (qual
->flags
.q
.out
&& qual
->flags
.q
.xfb_buffer
) {
3726 unsigned qual_xfb_buffer
;
3727 if (process_qualifier_constant(state
, loc
, "xfb_buffer",
3728 qual
->xfb_buffer
, &qual_xfb_buffer
) &&
3729 validate_xfb_buffer_qualifier(loc
, state
, qual_xfb_buffer
)) {
3730 var
->data
.xfb_buffer
= qual_xfb_buffer
;
3731 if (qual
->flags
.q
.explicit_xfb_buffer
)
3732 var
->data
.explicit_xfb_buffer
= true;
3736 if (qual
->flags
.q
.explicit_xfb_offset
) {
3737 unsigned qual_xfb_offset
;
3738 unsigned component_size
= var
->type
->contains_double() ? 8 : 4;
3740 if (process_qualifier_constant(state
, loc
, "xfb_offset",
3741 qual
->offset
, &qual_xfb_offset
) &&
3742 validate_xfb_offset_qualifier(loc
, state
, (int) qual_xfb_offset
,
3743 var
->type
, component_size
)) {
3744 var
->data
.offset
= qual_xfb_offset
;
3745 var
->data
.explicit_xfb_offset
= true;
3749 if (qual
->flags
.q
.explicit_xfb_stride
) {
3750 unsigned qual_xfb_stride
;
3751 if (process_qualifier_constant(state
, loc
, "xfb_stride",
3752 qual
->xfb_stride
, &qual_xfb_stride
)) {
3753 var
->data
.xfb_stride
= qual_xfb_stride
;
3754 var
->data
.explicit_xfb_stride
= true;
3758 if (var
->type
->contains_atomic()) {
3759 if (var
->data
.mode
== ir_var_uniform
) {
3760 if (var
->data
.explicit_binding
) {
3762 &state
->atomic_counter_offsets
[var
->data
.binding
];
3764 if (*offset
% ATOMIC_COUNTER_SIZE
)
3765 _mesa_glsl_error(loc
, state
,
3766 "misaligned atomic counter offset");
3768 var
->data
.offset
= *offset
;
3769 *offset
+= var
->type
->atomic_size();
3772 _mesa_glsl_error(loc
, state
,
3773 "atomic counters require explicit binding point");
3775 } else if (var
->data
.mode
!= ir_var_function_in
) {
3776 _mesa_glsl_error(loc
, state
, "atomic counters may only be declared as "
3777 "function parameters or uniform-qualified "
3778 "global variables");
3782 if (var
->type
->contains_sampler() &&
3783 !validate_storage_for_sampler_image_types(var
, state
, loc
))
3786 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
3787 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
3788 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
3789 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
3790 * These extensions and all following extensions that add the 'layout'
3791 * keyword have been modified to require the use of 'in' or 'out'.
3793 * The following extension do not allow the deprecated keywords:
3795 * GL_AMD_conservative_depth
3796 * GL_ARB_conservative_depth
3797 * GL_ARB_gpu_shader5
3798 * GL_ARB_separate_shader_objects
3799 * GL_ARB_tessellation_shader
3800 * GL_ARB_transform_feedback3
3801 * GL_ARB_uniform_buffer_object
3803 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
3804 * allow layout with the deprecated keywords.
3806 const bool relaxed_layout_qualifier_checking
=
3807 state
->ARB_fragment_coord_conventions_enable
;
3809 const bool uses_deprecated_qualifier
= qual
->flags
.q
.attribute
3810 || qual
->flags
.q
.varying
;
3811 if (qual
->has_layout() && uses_deprecated_qualifier
) {
3812 if (relaxed_layout_qualifier_checking
) {
3813 _mesa_glsl_warning(loc
, state
,
3814 "`layout' qualifier may not be used with "
3815 "`attribute' or `varying'");
3817 _mesa_glsl_error(loc
, state
,
3818 "`layout' qualifier may not be used with "
3819 "`attribute' or `varying'");
3823 /* Layout qualifiers for gl_FragDepth, which are enabled by extension
3824 * AMD_conservative_depth.
3826 if (qual
->flags
.q
.depth_type
3827 && !state
->is_version(420, 0)
3828 && !state
->AMD_conservative_depth_enable
3829 && !state
->ARB_conservative_depth_enable
) {
3830 _mesa_glsl_error(loc
, state
,
3831 "extension GL_AMD_conservative_depth or "
3832 "GL_ARB_conservative_depth must be enabled "
3833 "to use depth layout qualifiers");
3834 } else if (qual
->flags
.q
.depth_type
3835 && strcmp(var
->name
, "gl_FragDepth") != 0) {
3836 _mesa_glsl_error(loc
, state
,
3837 "depth layout qualifiers can be applied only to "
3841 switch (qual
->depth_type
) {
3843 var
->data
.depth_layout
= ir_depth_layout_any
;
3845 case ast_depth_greater
:
3846 var
->data
.depth_layout
= ir_depth_layout_greater
;
3848 case ast_depth_less
:
3849 var
->data
.depth_layout
= ir_depth_layout_less
;
3851 case ast_depth_unchanged
:
3852 var
->data
.depth_layout
= ir_depth_layout_unchanged
;
3855 var
->data
.depth_layout
= ir_depth_layout_none
;
3859 if (qual
->flags
.q
.std140
||
3860 qual
->flags
.q
.std430
||
3861 qual
->flags
.q
.packed
||
3862 qual
->flags
.q
.shared
) {
3863 _mesa_glsl_error(loc
, state
,
3864 "uniform and shader storage block layout qualifiers "
3865 "std140, std430, packed, and shared can only be "
3866 "applied to uniform or shader storage blocks, not "
3870 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
3871 validate_matrix_layout_for_type(state
, loc
, var
->type
, var
);
3874 /* From section 4.4.1.3 of the GLSL 4.50 specification (Fragment Shader
3877 * "Fragment shaders also allow the following layout qualifier on in only
3878 * (not with variable declarations)
3879 * layout-qualifier-id
3880 * early_fragment_tests
3883 if (qual
->flags
.q
.early_fragment_tests
) {
3884 _mesa_glsl_error(loc
, state
, "early_fragment_tests layout qualifier only "
3885 "valid in fragment shader input layout declaration.");
3888 if (qual
->flags
.q
.inner_coverage
) {
3889 _mesa_glsl_error(loc
, state
, "inner_coverage layout qualifier only "
3890 "valid in fragment shader input layout declaration.");
3893 if (qual
->flags
.q
.post_depth_coverage
) {
3894 _mesa_glsl_error(loc
, state
, "post_depth_coverage layout qualifier only "
3895 "valid in fragment shader input layout declaration.");
3898 if (state
->has_bindless())
3899 apply_bindless_qualifier_to_variable(qual
, var
, state
, loc
);
3901 if (qual
->flags
.q
.pixel_interlock_ordered
||
3902 qual
->flags
.q
.pixel_interlock_unordered
||
3903 qual
->flags
.q
.sample_interlock_ordered
||
3904 qual
->flags
.q
.sample_interlock_unordered
) {
3905 _mesa_glsl_error(loc
, state
, "interlock layout qualifiers: "
3906 "pixel_interlock_ordered, pixel_interlock_unordered, "
3907 "sample_interlock_ordered and sample_interlock_unordered, "
3908 "only valid in fragment shader input layout declaration.");
3913 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
3915 struct _mesa_glsl_parse_state
*state
,
3919 STATIC_ASSERT(sizeof(qual
->flags
.q
) <= sizeof(qual
->flags
.i
));
3921 if (qual
->flags
.q
.invariant
) {
3922 if (var
->data
.used
) {
3923 _mesa_glsl_error(loc
, state
,
3924 "variable `%s' may not be redeclared "
3925 "`invariant' after being used",
3928 var
->data
.invariant
= 1;
3932 if (qual
->flags
.q
.precise
) {
3933 if (var
->data
.used
) {
3934 _mesa_glsl_error(loc
, state
,
3935 "variable `%s' may not be redeclared "
3936 "`precise' after being used",
3939 var
->data
.precise
= 1;
3943 if (qual
->is_subroutine_decl() && !qual
->flags
.q
.uniform
) {
3944 _mesa_glsl_error(loc
, state
,
3945 "`subroutine' may only be applied to uniforms, "
3946 "subroutine type declarations, or function definitions");
3949 if (qual
->flags
.q
.constant
|| qual
->flags
.q
.attribute
3950 || qual
->flags
.q
.uniform
3951 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
3952 var
->data
.read_only
= 1;
3954 if (qual
->flags
.q
.centroid
)
3955 var
->data
.centroid
= 1;
3957 if (qual
->flags
.q
.sample
)
3958 var
->data
.sample
= 1;
3960 /* Precision qualifiers do not hold any meaning in Desktop GLSL */
3961 if (state
->es_shader
) {
3962 var
->data
.precision
=
3963 select_gles_precision(qual
->precision
, var
->type
, state
, loc
);
3966 if (qual
->flags
.q
.patch
)
3967 var
->data
.patch
= 1;
3969 if (qual
->flags
.q
.attribute
&& state
->stage
!= MESA_SHADER_VERTEX
) {
3970 var
->type
= glsl_type::error_type
;
3971 _mesa_glsl_error(loc
, state
,
3972 "`attribute' variables may not be declared in the "
3974 _mesa_shader_stage_to_string(state
->stage
));
3977 /* Disallow layout qualifiers which may only appear on layout declarations. */
3978 if (qual
->flags
.q
.prim_type
) {
3979 _mesa_glsl_error(loc
, state
,
3980 "Primitive type may only be specified on GS input or output "
3981 "layout declaration, not on variables.");
3984 /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
3986 * "However, the const qualifier cannot be used with out or inout."
3988 * The same section of the GLSL 4.40 spec further clarifies this saying:
3990 * "The const qualifier cannot be used with out or inout, or a
3991 * compile-time error results."
3993 if (is_parameter
&& qual
->flags
.q
.constant
&& qual
->flags
.q
.out
) {
3994 _mesa_glsl_error(loc
, state
,
3995 "`const' may not be applied to `out' or `inout' "
3996 "function parameters");
3999 /* If there is no qualifier that changes the mode of the variable, leave
4000 * the setting alone.
4002 assert(var
->data
.mode
!= ir_var_temporary
);
4003 if (qual
->flags
.q
.in
&& qual
->flags
.q
.out
)
4004 var
->data
.mode
= is_parameter
? ir_var_function_inout
: ir_var_shader_out
;
4005 else if (qual
->flags
.q
.in
)
4006 var
->data
.mode
= is_parameter
? ir_var_function_in
: ir_var_shader_in
;
4007 else if (qual
->flags
.q
.attribute
4008 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
4009 var
->data
.mode
= ir_var_shader_in
;
4010 else if (qual
->flags
.q
.out
)
4011 var
->data
.mode
= is_parameter
? ir_var_function_out
: ir_var_shader_out
;
4012 else if (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_VERTEX
))
4013 var
->data
.mode
= ir_var_shader_out
;
4014 else if (qual
->flags
.q
.uniform
)
4015 var
->data
.mode
= ir_var_uniform
;
4016 else if (qual
->flags
.q
.buffer
)
4017 var
->data
.mode
= ir_var_shader_storage
;
4018 else if (qual
->flags
.q
.shared_storage
)
4019 var
->data
.mode
= ir_var_shader_shared
;
4021 if (!is_parameter
&& state
->has_framebuffer_fetch() &&
4022 state
->stage
== MESA_SHADER_FRAGMENT
) {
4023 if (state
->is_version(130, 300))
4024 var
->data
.fb_fetch_output
= qual
->flags
.q
.in
&& qual
->flags
.q
.out
;
4026 var
->data
.fb_fetch_output
= (strcmp(var
->name
, "gl_LastFragData") == 0);
4029 if (var
->data
.fb_fetch_output
) {
4030 var
->data
.assigned
= true;
4031 var
->data
.memory_coherent
= !qual
->flags
.q
.non_coherent
;
4033 /* From the EXT_shader_framebuffer_fetch spec:
4035 * "It is an error to declare an inout fragment output not qualified
4036 * with layout(noncoherent) if the GL_EXT_shader_framebuffer_fetch
4037 * extension hasn't been enabled."
4039 if (var
->data
.memory_coherent
&&
4040 !state
->EXT_shader_framebuffer_fetch_enable
)
4041 _mesa_glsl_error(loc
, state
,
4042 "invalid declaration of framebuffer fetch output not "
4043 "qualified with layout(noncoherent)");
4046 /* From the EXT_shader_framebuffer_fetch spec:
4048 * "Fragment outputs declared inout may specify the following layout
4049 * qualifier: [...] noncoherent"
4051 if (qual
->flags
.q
.non_coherent
)
4052 _mesa_glsl_error(loc
, state
,
4053 "invalid layout(noncoherent) qualifier not part of "
4054 "framebuffer fetch output declaration");
4057 if (!is_parameter
&& is_varying_var(var
, state
->stage
)) {
4058 /* User-defined ins/outs are not permitted in compute shaders. */
4059 if (state
->stage
== MESA_SHADER_COMPUTE
) {
4060 _mesa_glsl_error(loc
, state
,
4061 "user-defined input and output variables are not "
4062 "permitted in compute shaders");
4065 /* This variable is being used to link data between shader stages (in
4066 * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
4067 * that is allowed for such purposes.
4069 * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
4071 * "The varying qualifier can be used only with the data types
4072 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
4075 * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
4076 * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
4078 * "Fragment inputs can only be signed and unsigned integers and
4079 * integer vectors, float, floating-point vectors, matrices, or
4080 * arrays of these. Structures cannot be input.
4082 * Similar text exists in the section on vertex shader outputs.
4084 * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
4085 * 3.00 spec allows structs as well. Varying structs are also allowed
4088 * From section 4.3.4 of the ARB_bindless_texture spec:
4090 * "(modify third paragraph of the section to allow sampler and image
4091 * types) ... Vertex shader inputs can only be float,
4092 * single-precision floating-point scalars, single-precision
4093 * floating-point vectors, matrices, signed and unsigned integers
4094 * and integer vectors, sampler and image types."
4096 * From section 4.3.6 of the ARB_bindless_texture spec:
4098 * "Output variables can only be floating-point scalars,
4099 * floating-point vectors, matrices, signed or unsigned integers or
4100 * integer vectors, sampler or image types, or arrays or structures
4103 switch (var
->type
->without_array()->base_type
) {
4104 case GLSL_TYPE_FLOAT
:
4105 /* Ok in all GLSL versions */
4107 case GLSL_TYPE_UINT
:
4109 if (state
->is_version(130, 300))
4111 _mesa_glsl_error(loc
, state
,
4112 "varying variables must be of base type float in %s",
4113 state
->get_version_string());
4115 case GLSL_TYPE_STRUCT
:
4116 if (state
->is_version(150, 300))
4118 _mesa_glsl_error(loc
, state
,
4119 "varying variables may not be of type struct");
4121 case GLSL_TYPE_DOUBLE
:
4122 case GLSL_TYPE_UINT64
:
4123 case GLSL_TYPE_INT64
:
4125 case GLSL_TYPE_SAMPLER
:
4126 case GLSL_TYPE_IMAGE
:
4127 if (state
->has_bindless())
4131 _mesa_glsl_error(loc
, state
, "illegal type for a varying variable");
4136 if (state
->all_invariant
&& var
->data
.mode
== ir_var_shader_out
)
4137 var
->data
.invariant
= true;
4139 var
->data
.interpolation
=
4140 interpret_interpolation_qualifier(qual
, var
->type
,
4141 (ir_variable_mode
) var
->data
.mode
,
4144 /* Does the declaration use the deprecated 'attribute' or 'varying'
4147 const bool uses_deprecated_qualifier
= qual
->flags
.q
.attribute
4148 || qual
->flags
.q
.varying
;
4151 /* Validate auxiliary storage qualifiers */
4153 /* From section 4.3.4 of the GLSL 1.30 spec:
4154 * "It is an error to use centroid in in a vertex shader."
4156 * From section 4.3.4 of the GLSL ES 3.00 spec:
4157 * "It is an error to use centroid in or interpolation qualifiers in
4158 * a vertex shader input."
4161 /* Section 4.3.6 of the GLSL 1.30 specification states:
4162 * "It is an error to use centroid out in a fragment shader."
4164 * The GL_ARB_shading_language_420pack extension specification states:
4165 * "It is an error to use auxiliary storage qualifiers or interpolation
4166 * qualifiers on an output in a fragment shader."
4168 if (qual
->flags
.q
.sample
&& (!is_varying_var(var
, state
->stage
) || uses_deprecated_qualifier
)) {
4169 _mesa_glsl_error(loc
, state
,
4170 "sample qualifier may only be used on `in` or `out` "
4171 "variables between shader stages");
4173 if (qual
->flags
.q
.centroid
&& !is_varying_var(var
, state
->stage
)) {
4174 _mesa_glsl_error(loc
, state
,
4175 "centroid qualifier may only be used with `in', "
4176 "`out' or `varying' variables between shader stages");
4179 if (qual
->flags
.q
.shared_storage
&& state
->stage
!= MESA_SHADER_COMPUTE
) {
4180 _mesa_glsl_error(loc
, state
,
4181 "the shared storage qualifiers can only be used with "
4185 apply_image_qualifier_to_variable(qual
, var
, state
, loc
);
4189 * Get the variable that is being redeclared by this declaration or if it
4190 * does not exist, the current declared variable.
4192 * Semantic checks to verify the validity of the redeclaration are also
4193 * performed. If semantic checks fail, compilation error will be emitted via
4194 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
4197 * A pointer to an existing variable in the current scope if the declaration
4198 * is a redeclaration, current variable otherwise. \c is_declared boolean
4199 * will return \c true if the declaration is a redeclaration, \c false
4202 static ir_variable
*
4203 get_variable_being_redeclared(ir_variable
**var_ptr
, YYLTYPE loc
,
4204 struct _mesa_glsl_parse_state
*state
,
4205 bool allow_all_redeclarations
,
4206 bool *is_redeclaration
)
4208 ir_variable
*var
= *var_ptr
;
4210 /* Check if this declaration is actually a re-declaration, either to
4211 * resize an array or add qualifiers to an existing variable.
4213 * This is allowed for variables in the current scope, or when at
4214 * global scope (for built-ins in the implicit outer scope).
4216 ir_variable
*earlier
= state
->symbols
->get_variable(var
->name
);
4217 if (earlier
== NULL
||
4218 (state
->current_function
!= NULL
&&
4219 !state
->symbols
->name_declared_this_scope(var
->name
))) {
4220 *is_redeclaration
= false;
4224 *is_redeclaration
= true;
4226 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
4228 * "It is legal to declare an array without a size and then
4229 * later re-declare the same name as an array of the same
4230 * type and specify a size."
4232 if (earlier
->type
->is_unsized_array() && var
->type
->is_array()
4233 && (var
->type
->fields
.array
== earlier
->type
->fields
.array
)) {
4234 /* FINISHME: This doesn't match the qualifiers on the two
4235 * FINISHME: declarations. It's not 100% clear whether this is
4236 * FINISHME: required or not.
4239 const int size
= var
->type
->array_size();
4240 check_builtin_array_max_size(var
->name
, size
, loc
, state
);
4241 if ((size
> 0) && (size
<= earlier
->data
.max_array_access
)) {
4242 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
4244 earlier
->data
.max_array_access
);
4247 earlier
->type
= var
->type
;
4251 } else if ((state
->ARB_fragment_coord_conventions_enable
||
4252 state
->is_version(150, 0))
4253 && strcmp(var
->name
, "gl_FragCoord") == 0
4254 && earlier
->type
== var
->type
4255 && var
->data
.mode
== ir_var_shader_in
) {
4256 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
4259 earlier
->data
.origin_upper_left
= var
->data
.origin_upper_left
;
4260 earlier
->data
.pixel_center_integer
= var
->data
.pixel_center_integer
;
4262 /* According to section 4.3.7 of the GLSL 1.30 spec,
4263 * the following built-in varaibles can be redeclared with an
4264 * interpolation qualifier:
4267 * * gl_FrontSecondaryColor
4268 * * gl_BackSecondaryColor
4270 * * gl_SecondaryColor
4272 } else if (state
->is_version(130, 0)
4273 && (strcmp(var
->name
, "gl_FrontColor") == 0
4274 || strcmp(var
->name
, "gl_BackColor") == 0
4275 || strcmp(var
->name
, "gl_FrontSecondaryColor") == 0
4276 || strcmp(var
->name
, "gl_BackSecondaryColor") == 0
4277 || strcmp(var
->name
, "gl_Color") == 0
4278 || strcmp(var
->name
, "gl_SecondaryColor") == 0)
4279 && earlier
->type
== var
->type
4280 && earlier
->data
.mode
== var
->data
.mode
) {
4281 earlier
->data
.interpolation
= var
->data
.interpolation
;
4283 /* Layout qualifiers for gl_FragDepth. */
4284 } else if ((state
->is_version(420, 0) ||
4285 state
->AMD_conservative_depth_enable
||
4286 state
->ARB_conservative_depth_enable
)
4287 && strcmp(var
->name
, "gl_FragDepth") == 0
4288 && earlier
->type
== var
->type
4289 && earlier
->data
.mode
== var
->data
.mode
) {
4291 /** From the AMD_conservative_depth spec:
4292 * Within any shader, the first redeclarations of gl_FragDepth
4293 * must appear before any use of gl_FragDepth.
4295 if (earlier
->data
.used
) {
4296 _mesa_glsl_error(&loc
, state
,
4297 "the first redeclaration of gl_FragDepth "
4298 "must appear before any use of gl_FragDepth");
4301 /* Prevent inconsistent redeclaration of depth layout qualifier. */
4302 if (earlier
->data
.depth_layout
!= ir_depth_layout_none
4303 && earlier
->data
.depth_layout
!= var
->data
.depth_layout
) {
4304 _mesa_glsl_error(&loc
, state
,
4305 "gl_FragDepth: depth layout is declared here "
4306 "as '%s, but it was previously declared as "
4308 depth_layout_string(var
->data
.depth_layout
),
4309 depth_layout_string(earlier
->data
.depth_layout
));
4312 earlier
->data
.depth_layout
= var
->data
.depth_layout
;
4314 } else if (state
->has_framebuffer_fetch() &&
4315 strcmp(var
->name
, "gl_LastFragData") == 0 &&
4316 var
->type
== earlier
->type
&&
4317 var
->data
.mode
== ir_var_auto
) {
4318 /* According to the EXT_shader_framebuffer_fetch spec:
4320 * "By default, gl_LastFragData is declared with the mediump precision
4321 * qualifier. This can be changed by redeclaring the corresponding
4322 * variables with the desired precision qualifier."
4324 * "Fragment shaders may specify the following layout qualifier only for
4325 * redeclaring the built-in gl_LastFragData array [...]: noncoherent"
4327 earlier
->data
.precision
= var
->data
.precision
;
4328 earlier
->data
.memory_coherent
= var
->data
.memory_coherent
;
4330 } else if (earlier
->data
.how_declared
== ir_var_declared_implicitly
&&
4331 state
->allow_builtin_variable_redeclaration
) {
4332 /* Allow verbatim redeclarations of built-in variables. Not explicitly
4333 * valid, but some applications do it.
4335 if (earlier
->data
.mode
!= var
->data
.mode
&&
4336 !(earlier
->data
.mode
== ir_var_system_value
&&
4337 var
->data
.mode
== ir_var_shader_in
)) {
4338 _mesa_glsl_error(&loc
, state
,
4339 "redeclaration of `%s' with incorrect qualifiers",
4341 } else if (earlier
->type
!= var
->type
) {
4342 _mesa_glsl_error(&loc
, state
,
4343 "redeclaration of `%s' has incorrect type",
4346 } else if (allow_all_redeclarations
) {
4347 if (earlier
->data
.mode
!= var
->data
.mode
) {
4348 _mesa_glsl_error(&loc
, state
,
4349 "redeclaration of `%s' with incorrect qualifiers",
4351 } else if (earlier
->type
!= var
->type
) {
4352 _mesa_glsl_error(&loc
, state
,
4353 "redeclaration of `%s' has incorrect type",
4357 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
4364 * Generate the IR for an initializer in a variable declaration
4367 process_initializer(ir_variable
*var
, ast_declaration
*decl
,
4368 ast_fully_specified_type
*type
,
4369 exec_list
*initializer_instructions
,
4370 struct _mesa_glsl_parse_state
*state
)
4372 void *mem_ctx
= state
;
4373 ir_rvalue
*result
= NULL
;
4375 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
4377 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
4379 * "All uniform variables are read-only and are initialized either
4380 * directly by an application via API commands, or indirectly by
4383 if (var
->data
.mode
== ir_var_uniform
) {
4384 state
->check_version(120, 0, &initializer_loc
,
4385 "cannot initialize uniform %s",
4389 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
4391 * "Buffer variables cannot have initializers."
4393 if (var
->data
.mode
== ir_var_shader_storage
) {
4394 _mesa_glsl_error(&initializer_loc
, state
,
4395 "cannot initialize buffer variable %s",
4399 /* From section 4.1.7 of the GLSL 4.40 spec:
4401 * "Opaque variables [...] are initialized only through the
4402 * OpenGL API; they cannot be declared with an initializer in a
4405 * From section 4.1.7 of the ARB_bindless_texture spec:
4407 * "Samplers may be declared as shader inputs and outputs, as uniform
4408 * variables, as temporary variables, and as function parameters."
4410 * From section 4.1.X of the ARB_bindless_texture spec:
4412 * "Images may be declared as shader inputs and outputs, as uniform
4413 * variables, as temporary variables, and as function parameters."
4415 if (var
->type
->contains_atomic() ||
4416 (!state
->has_bindless() && var
->type
->contains_opaque())) {
4417 _mesa_glsl_error(&initializer_loc
, state
,
4418 "cannot initialize %s variable %s",
4419 var
->name
, state
->has_bindless() ? "atomic" : "opaque");
4422 if ((var
->data
.mode
== ir_var_shader_in
) && (state
->current_function
== NULL
)) {
4423 _mesa_glsl_error(&initializer_loc
, state
,
4424 "cannot initialize %s shader input / %s %s",
4425 _mesa_shader_stage_to_string(state
->stage
),
4426 (state
->stage
== MESA_SHADER_VERTEX
)
4427 ? "attribute" : "varying",
4431 if (var
->data
.mode
== ir_var_shader_out
&& state
->current_function
== NULL
) {
4432 _mesa_glsl_error(&initializer_loc
, state
,
4433 "cannot initialize %s shader output %s",
4434 _mesa_shader_stage_to_string(state
->stage
),
4438 /* If the initializer is an ast_aggregate_initializer, recursively store
4439 * type information from the LHS into it, so that its hir() function can do
4442 if (decl
->initializer
->oper
== ast_aggregate
)
4443 _mesa_ast_set_aggregate_type(var
->type
, decl
->initializer
);
4445 ir_dereference
*const lhs
= new(state
) ir_dereference_variable(var
);
4446 ir_rvalue
*rhs
= decl
->initializer
->hir(initializer_instructions
, state
);
4448 /* Calculate the constant value if this is a const or uniform
4451 * Section 4.3 (Storage Qualifiers) of the GLSL ES 1.00.17 spec says:
4453 * "Declarations of globals without a storage qualifier, or with
4454 * just the const qualifier, may include initializers, in which case
4455 * they will be initialized before the first line of main() is
4456 * executed. Such initializers must be a constant expression."
4458 * The same section of the GLSL ES 3.00.4 spec has similar language.
4460 if (type
->qualifier
.flags
.q
.constant
4461 || type
->qualifier
.flags
.q
.uniform
4462 || (state
->es_shader
&& state
->current_function
== NULL
)) {
4463 ir_rvalue
*new_rhs
= validate_assignment(state
, initializer_loc
,
4465 if (new_rhs
!= NULL
) {
4468 /* Section 4.3.3 (Constant Expressions) of the GLSL ES 3.00.4 spec
4471 * "A constant expression is one of
4475 * - an expression formed by an operator on operands that are
4476 * all constant expressions, including getting an element of
4477 * a constant array, or a field of a constant structure, or
4478 * components of a constant vector. However, the sequence
4479 * operator ( , ) and the assignment operators ( =, +=, ...)
4480 * are not included in the operators that can create a
4481 * constant expression."
4483 * Section 12.43 (Sequence operator and constant expressions) says:
4485 * "Should the following construct be allowed?
4489 * The expression within the brackets uses the sequence operator
4490 * (',') and returns the integer 3 so the construct is declaring
4491 * a single-dimensional array of size 3. In some languages, the
4492 * construct declares a two-dimensional array. It would be
4493 * preferable to make this construct illegal to avoid confusion.
4495 * One possibility is to change the definition of the sequence
4496 * operator so that it does not return a constant-expression and
4497 * hence cannot be used to declare an array size.
4499 * RESOLUTION: The result of a sequence operator is not a
4500 * constant-expression."
4502 * Section 4.3.3 (Constant Expressions) of the GLSL 4.30.9 spec
4503 * contains language almost identical to the section 4.3.3 in the
4504 * GLSL ES 3.00.4 spec. This is a new limitation for these GLSL
4507 ir_constant
*constant_value
=
4508 rhs
->constant_expression_value(mem_ctx
);
4510 if (!constant_value
||
4511 (state
->is_version(430, 300) &&
4512 decl
->initializer
->has_sequence_subexpression())) {
4513 const char *const variable_mode
=
4514 (type
->qualifier
.flags
.q
.constant
)
4516 : ((type
->qualifier
.flags
.q
.uniform
) ? "uniform" : "global");
4518 /* If ARB_shading_language_420pack is enabled, initializers of
4519 * const-qualified local variables do not have to be constant
4520 * expressions. Const-qualified global variables must still be
4521 * initialized with constant expressions.
4523 if (!state
->has_420pack()
4524 || state
->current_function
== NULL
) {
4525 _mesa_glsl_error(& initializer_loc
, state
,
4526 "initializer of %s variable `%s' must be a "
4527 "constant expression",
4530 if (var
->type
->is_numeric()) {
4531 /* Reduce cascading errors. */
4532 var
->constant_value
= type
->qualifier
.flags
.q
.constant
4533 ? ir_constant::zero(state
, var
->type
) : NULL
;
4537 rhs
= constant_value
;
4538 var
->constant_value
= type
->qualifier
.flags
.q
.constant
4539 ? constant_value
: NULL
;
4542 if (var
->type
->is_numeric()) {
4543 /* Reduce cascading errors. */
4544 rhs
= var
->constant_value
= type
->qualifier
.flags
.q
.constant
4545 ? ir_constant::zero(state
, var
->type
) : NULL
;
4550 if (rhs
&& !rhs
->type
->is_error()) {
4551 bool temp
= var
->data
.read_only
;
4552 if (type
->qualifier
.flags
.q
.constant
)
4553 var
->data
.read_only
= false;
4555 /* Never emit code to initialize a uniform.
4557 const glsl_type
*initializer_type
;
4558 if (!type
->qualifier
.flags
.q
.uniform
) {
4559 do_assignment(initializer_instructions
, state
,
4564 type
->get_location());
4565 initializer_type
= result
->type
;
4567 initializer_type
= rhs
->type
;
4569 var
->constant_initializer
= rhs
->constant_expression_value(mem_ctx
);
4570 var
->data
.has_initializer
= true;
4572 /* If the declared variable is an unsized array, it must inherrit
4573 * its full type from the initializer. A declaration such as
4575 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
4579 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
4581 * The assignment generated in the if-statement (below) will also
4582 * automatically handle this case for non-uniforms.
4584 * If the declared variable is not an array, the types must
4585 * already match exactly. As a result, the type assignment
4586 * here can be done unconditionally. For non-uniforms the call
4587 * to do_assignment can change the type of the initializer (via
4588 * the implicit conversion rules). For uniforms the initializer
4589 * must be a constant expression, and the type of that expression
4590 * was validated above.
4592 var
->type
= initializer_type
;
4594 var
->data
.read_only
= temp
;
4601 validate_layout_qualifier_vertex_count(struct _mesa_glsl_parse_state
*state
,
4602 YYLTYPE loc
, ir_variable
*var
,
4603 unsigned num_vertices
,
4605 const char *var_category
)
4607 if (var
->type
->is_unsized_array()) {
4608 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says:
4610 * All geometry shader input unsized array declarations will be
4611 * sized by an earlier input layout qualifier, when present, as per
4612 * the following table.
4614 * Followed by a table mapping each allowed input layout qualifier to
4615 * the corresponding input length.
4617 * Similarly for tessellation control shader outputs.
4619 if (num_vertices
!= 0)
4620 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
4623 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec
4624 * includes the following examples of compile-time errors:
4626 * // code sequence within one shader...
4627 * in vec4 Color1[]; // size unknown
4628 * ...Color1.length()...// illegal, length() unknown
4629 * in vec4 Color2[2]; // size is 2
4630 * ...Color1.length()...// illegal, Color1 still has no size
4631 * in vec4 Color3[3]; // illegal, input sizes are inconsistent
4632 * layout(lines) in; // legal, input size is 2, matching
4633 * in vec4 Color4[3]; // illegal, contradicts layout
4636 * To detect the case illustrated by Color3, we verify that the size of
4637 * an explicitly-sized array matches the size of any previously declared
4638 * explicitly-sized array. To detect the case illustrated by Color4, we
4639 * verify that the size of an explicitly-sized array is consistent with
4640 * any previously declared input layout.
4642 if (num_vertices
!= 0 && var
->type
->length
!= num_vertices
) {
4643 _mesa_glsl_error(&loc
, state
,
4644 "%s size contradicts previously declared layout "
4645 "(size is %u, but layout requires a size of %u)",
4646 var_category
, var
->type
->length
, num_vertices
);
4647 } else if (*size
!= 0 && var
->type
->length
!= *size
) {
4648 _mesa_glsl_error(&loc
, state
,
4649 "%s sizes are inconsistent (size is %u, but a "
4650 "previous declaration has size %u)",
4651 var_category
, var
->type
->length
, *size
);
4653 *size
= var
->type
->length
;
4659 handle_tess_ctrl_shader_output_decl(struct _mesa_glsl_parse_state
*state
,
4660 YYLTYPE loc
, ir_variable
*var
)
4662 unsigned num_vertices
= 0;
4664 if (state
->tcs_output_vertices_specified
) {
4665 if (!state
->out_qualifier
->vertices
->
4666 process_qualifier_constant(state
, "vertices",
4667 &num_vertices
, false)) {
4671 if (num_vertices
> state
->Const
.MaxPatchVertices
) {
4672 _mesa_glsl_error(&loc
, state
, "vertices (%d) exceeds "
4673 "GL_MAX_PATCH_VERTICES", num_vertices
);
4678 if (!var
->type
->is_array() && !var
->data
.patch
) {
4679 _mesa_glsl_error(&loc
, state
,
4680 "tessellation control shader outputs must be arrays");
4682 /* To avoid cascading failures, short circuit the checks below. */
4686 if (var
->data
.patch
)
4689 validate_layout_qualifier_vertex_count(state
, loc
, var
, num_vertices
,
4690 &state
->tcs_output_size
,
4691 "tessellation control shader output");
4695 * Do additional processing necessary for tessellation control/evaluation shader
4696 * input declarations. This covers both interface block arrays and bare input
4700 handle_tess_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
4701 YYLTYPE loc
, ir_variable
*var
)
4703 if (!var
->type
->is_array() && !var
->data
.patch
) {
4704 _mesa_glsl_error(&loc
, state
,
4705 "per-vertex tessellation shader inputs must be arrays");
4706 /* Avoid cascading failures. */
4710 if (var
->data
.patch
)
4713 /* The ARB_tessellation_shader spec says:
4715 * "Declaring an array size is optional. If no size is specified, it
4716 * will be taken from the implementation-dependent maximum patch size
4717 * (gl_MaxPatchVertices). If a size is specified, it must match the
4718 * maximum patch size; otherwise, a compile or link error will occur."
4720 * This text appears twice, once for TCS inputs, and again for TES inputs.
4722 if (var
->type
->is_unsized_array()) {
4723 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
4724 state
->Const
.MaxPatchVertices
);
4725 } else if (var
->type
->length
!= state
->Const
.MaxPatchVertices
) {
4726 _mesa_glsl_error(&loc
, state
,
4727 "per-vertex tessellation shader input arrays must be "
4728 "sized to gl_MaxPatchVertices (%d).",
4729 state
->Const
.MaxPatchVertices
);
4735 * Do additional processing necessary for geometry shader input declarations
4736 * (this covers both interface blocks arrays and bare input variables).
4739 handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
4740 YYLTYPE loc
, ir_variable
*var
)
4742 unsigned num_vertices
= 0;
4744 if (state
->gs_input_prim_type_specified
) {
4745 num_vertices
= vertices_per_prim(state
->in_qualifier
->prim_type
);
4748 /* Geometry shader input variables must be arrays. Caller should have
4749 * reported an error for this.
4751 if (!var
->type
->is_array()) {
4752 assert(state
->error
);
4754 /* To avoid cascading failures, short circuit the checks below. */
4758 validate_layout_qualifier_vertex_count(state
, loc
, var
, num_vertices
,
4759 &state
->gs_input_size
,
4760 "geometry shader input");
4764 validate_identifier(const char *identifier
, YYLTYPE loc
,
4765 struct _mesa_glsl_parse_state
*state
)
4767 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
4769 * "Identifiers starting with "gl_" are reserved for use by
4770 * OpenGL, and may not be declared in a shader as either a
4771 * variable or a function."
4773 if (is_gl_identifier(identifier
)) {
4774 _mesa_glsl_error(&loc
, state
,
4775 "identifier `%s' uses reserved `gl_' prefix",
4777 } else if (strstr(identifier
, "__")) {
4778 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
4781 * "In addition, all identifiers containing two
4782 * consecutive underscores (__) are reserved as
4783 * possible future keywords."
4785 * The intention is that names containing __ are reserved for internal
4786 * use by the implementation, and names prefixed with GL_ are reserved
4787 * for use by Khronos. Names simply containing __ are dangerous to use,
4788 * but should be allowed.
4790 * A future version of the GLSL specification will clarify this.
4792 _mesa_glsl_warning(&loc
, state
,
4793 "identifier `%s' uses reserved `__' string",
4799 ast_declarator_list::hir(exec_list
*instructions
,
4800 struct _mesa_glsl_parse_state
*state
)
4803 const struct glsl_type
*decl_type
;
4804 const char *type_name
= NULL
;
4805 ir_rvalue
*result
= NULL
;
4806 YYLTYPE loc
= this->get_location();
4808 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
4810 * "To ensure that a particular output variable is invariant, it is
4811 * necessary to use the invariant qualifier. It can either be used to
4812 * qualify a previously declared variable as being invariant
4814 * invariant gl_Position; // make existing gl_Position be invariant"
4816 * In these cases the parser will set the 'invariant' flag in the declarator
4817 * list, and the type will be NULL.
4819 if (this->invariant
) {
4820 assert(this->type
== NULL
);
4822 if (state
->current_function
!= NULL
) {
4823 _mesa_glsl_error(& loc
, state
,
4824 "all uses of `invariant' keyword must be at global "
4828 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
4829 assert(decl
->array_specifier
== NULL
);
4830 assert(decl
->initializer
== NULL
);
4832 ir_variable
*const earlier
=
4833 state
->symbols
->get_variable(decl
->identifier
);
4834 if (earlier
== NULL
) {
4835 _mesa_glsl_error(& loc
, state
,
4836 "undeclared variable `%s' cannot be marked "
4837 "invariant", decl
->identifier
);
4838 } else if (!is_allowed_invariant(earlier
, state
)) {
4839 _mesa_glsl_error(&loc
, state
,
4840 "`%s' cannot be marked invariant; interfaces between "
4841 "shader stages only.", decl
->identifier
);
4842 } else if (earlier
->data
.used
) {
4843 _mesa_glsl_error(& loc
, state
,
4844 "variable `%s' may not be redeclared "
4845 "`invariant' after being used",
4848 earlier
->data
.invariant
= true;
4852 /* Invariant redeclarations do not have r-values.
4857 if (this->precise
) {
4858 assert(this->type
== NULL
);
4860 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
4861 assert(decl
->array_specifier
== NULL
);
4862 assert(decl
->initializer
== NULL
);
4864 ir_variable
*const earlier
=
4865 state
->symbols
->get_variable(decl
->identifier
);
4866 if (earlier
== NULL
) {
4867 _mesa_glsl_error(& loc
, state
,
4868 "undeclared variable `%s' cannot be marked "
4869 "precise", decl
->identifier
);
4870 } else if (state
->current_function
!= NULL
&&
4871 !state
->symbols
->name_declared_this_scope(decl
->identifier
)) {
4872 /* Note: we have to check if we're in a function, since
4873 * builtins are treated as having come from another scope.
4875 _mesa_glsl_error(& loc
, state
,
4876 "variable `%s' from an outer scope may not be "
4877 "redeclared `precise' in this scope",
4879 } else if (earlier
->data
.used
) {
4880 _mesa_glsl_error(& loc
, state
,
4881 "variable `%s' may not be redeclared "
4882 "`precise' after being used",
4885 earlier
->data
.precise
= true;
4889 /* Precise redeclarations do not have r-values either. */
4893 assert(this->type
!= NULL
);
4894 assert(!this->invariant
);
4895 assert(!this->precise
);
4897 /* The type specifier may contain a structure definition. Process that
4898 * before any of the variable declarations.
4900 (void) this->type
->specifier
->hir(instructions
, state
);
4902 decl_type
= this->type
->glsl_type(& type_name
, state
);
4904 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
4905 * "Buffer variables may only be declared inside interface blocks
4906 * (section 4.3.9 “Interface Blocks”), which are then referred to as
4907 * shader storage blocks. It is a compile-time error to declare buffer
4908 * variables at global scope (outside a block)."
4910 if (type
->qualifier
.flags
.q
.buffer
&& !decl_type
->is_interface()) {
4911 _mesa_glsl_error(&loc
, state
,
4912 "buffer variables cannot be declared outside "
4913 "interface blocks");
4916 /* An offset-qualified atomic counter declaration sets the default
4917 * offset for the next declaration within the same atomic counter
4920 if (decl_type
&& decl_type
->contains_atomic()) {
4921 if (type
->qualifier
.flags
.q
.explicit_binding
&&
4922 type
->qualifier
.flags
.q
.explicit_offset
) {
4923 unsigned qual_binding
;
4924 unsigned qual_offset
;
4925 if (process_qualifier_constant(state
, &loc
, "binding",
4926 type
->qualifier
.binding
,
4928 && process_qualifier_constant(state
, &loc
, "offset",
4929 type
->qualifier
.offset
,
4931 state
->atomic_counter_offsets
[qual_binding
] = qual_offset
;
4935 ast_type_qualifier allowed_atomic_qual_mask
;
4936 allowed_atomic_qual_mask
.flags
.i
= 0;
4937 allowed_atomic_qual_mask
.flags
.q
.explicit_binding
= 1;
4938 allowed_atomic_qual_mask
.flags
.q
.explicit_offset
= 1;
4939 allowed_atomic_qual_mask
.flags
.q
.uniform
= 1;
4941 type
->qualifier
.validate_flags(&loc
, state
, allowed_atomic_qual_mask
,
4942 "invalid layout qualifier for",
4946 if (this->declarations
.is_empty()) {
4947 /* If there is no structure involved in the program text, there are two
4948 * possible scenarios:
4950 * - The program text contained something like 'vec4;'. This is an
4951 * empty declaration. It is valid but weird. Emit a warning.
4953 * - The program text contained something like 'S;' and 'S' is not the
4954 * name of a known structure type. This is both invalid and weird.
4957 * - The program text contained something like 'mediump float;'
4958 * when the programmer probably meant 'precision mediump
4959 * float;' Emit a warning with a description of what they
4960 * probably meant to do.
4962 * Note that if decl_type is NULL and there is a structure involved,
4963 * there must have been some sort of error with the structure. In this
4964 * case we assume that an error was already generated on this line of
4965 * code for the structure. There is no need to generate an additional,
4968 assert(this->type
->specifier
->structure
== NULL
|| decl_type
!= NULL
4971 if (decl_type
== NULL
) {
4972 _mesa_glsl_error(&loc
, state
,
4973 "invalid type `%s' in empty declaration",
4976 if (decl_type
->is_array()) {
4977 /* From Section 13.22 (Array Declarations) of the GLSL ES 3.2
4980 * "... any declaration that leaves the size undefined is
4981 * disallowed as this would add complexity and there are no
4984 if (state
->es_shader
&& decl_type
->is_unsized_array()) {
4985 _mesa_glsl_error(&loc
, state
, "array size must be explicitly "
4986 "or implicitly defined");
4989 /* From Section 4.12 (Empty Declarations) of the GLSL 4.5 spec:
4991 * "The combinations of types and qualifiers that cause
4992 * compile-time or link-time errors are the same whether or not
4993 * the declaration is empty."
4995 validate_array_dimensions(decl_type
, state
, &loc
);
4998 if (decl_type
->is_atomic_uint()) {
4999 /* Empty atomic counter declarations are allowed and useful
5000 * to set the default offset qualifier.
5003 } else if (this->type
->qualifier
.precision
!= ast_precision_none
) {
5004 if (this->type
->specifier
->structure
!= NULL
) {
5005 _mesa_glsl_error(&loc
, state
,
5006 "precision qualifiers can't be applied "
5009 static const char *const precision_names
[] = {
5016 _mesa_glsl_warning(&loc
, state
,
5017 "empty declaration with precision "
5018 "qualifier, to set the default precision, "
5019 "use `precision %s %s;'",
5020 precision_names
[this->type
->
5021 qualifier
.precision
],
5024 } else if (this->type
->specifier
->structure
== NULL
) {
5025 _mesa_glsl_warning(&loc
, state
, "empty declaration");
5030 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
5031 const struct glsl_type
*var_type
;
5033 const char *identifier
= decl
->identifier
;
5034 /* FINISHME: Emit a warning if a variable declaration shadows a
5035 * FINISHME: declaration at a higher scope.
5038 if ((decl_type
== NULL
) || decl_type
->is_void()) {
5039 if (type_name
!= NULL
) {
5040 _mesa_glsl_error(& loc
, state
,
5041 "invalid type `%s' in declaration of `%s'",
5042 type_name
, decl
->identifier
);
5044 _mesa_glsl_error(& loc
, state
,
5045 "invalid type in declaration of `%s'",
5051 if (this->type
->qualifier
.is_subroutine_decl()) {
5055 t
= state
->symbols
->get_type(this->type
->specifier
->type_name
);
5057 _mesa_glsl_error(& loc
, state
,
5058 "invalid type in declaration of `%s'",
5060 name
= ralloc_asprintf(ctx
, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state
->stage
), decl
->identifier
);
5065 var_type
= process_array_type(&loc
, decl_type
, decl
->array_specifier
,
5068 var
= new(ctx
) ir_variable(var_type
, identifier
, ir_var_auto
);
5070 /* The 'varying in' and 'varying out' qualifiers can only be used with
5071 * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
5074 if (this->type
->qualifier
.flags
.q
.varying
) {
5075 if (this->type
->qualifier
.flags
.q
.in
) {
5076 _mesa_glsl_error(& loc
, state
,
5077 "`varying in' qualifier in declaration of "
5078 "`%s' only valid for geometry shaders using "
5079 "ARB_geometry_shader4 or EXT_geometry_shader4",
5081 } else if (this->type
->qualifier
.flags
.q
.out
) {
5082 _mesa_glsl_error(& loc
, state
,
5083 "`varying out' qualifier in declaration of "
5084 "`%s' only valid for geometry shaders using "
5085 "ARB_geometry_shader4 or EXT_geometry_shader4",
5090 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
5092 * "Global variables can only use the qualifiers const,
5093 * attribute, uniform, or varying. Only one may be
5096 * Local variables can only use the qualifier const."
5098 * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
5099 * any extension that adds the 'layout' keyword.
5101 if (!state
->is_version(130, 300)
5102 && !state
->has_explicit_attrib_location()
5103 && !state
->has_separate_shader_objects()
5104 && !state
->ARB_fragment_coord_conventions_enable
) {
5105 if (this->type
->qualifier
.flags
.q
.out
) {
5106 _mesa_glsl_error(& loc
, state
,
5107 "`out' qualifier in declaration of `%s' "
5108 "only valid for function parameters in %s",
5109 decl
->identifier
, state
->get_version_string());
5111 if (this->type
->qualifier
.flags
.q
.in
) {
5112 _mesa_glsl_error(& loc
, state
,
5113 "`in' qualifier in declaration of `%s' "
5114 "only valid for function parameters in %s",
5115 decl
->identifier
, state
->get_version_string());
5117 /* FINISHME: Test for other invalid qualifiers. */
5120 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
5122 apply_layout_qualifier_to_variable(&this->type
->qualifier
, var
, state
,
5125 if ((var
->data
.mode
== ir_var_auto
|| var
->data
.mode
== ir_var_temporary
)
5126 && (var
->type
->is_numeric() || var
->type
->is_boolean())
5127 && state
->zero_init
) {
5128 const ir_constant_data data
= { { 0 } };
5129 var
->data
.has_initializer
= true;
5130 var
->constant_initializer
= new(var
) ir_constant(var
->type
, &data
);
5133 if (this->type
->qualifier
.flags
.q
.invariant
) {
5134 if (!is_allowed_invariant(var
, state
)) {
5135 _mesa_glsl_error(&loc
, state
,
5136 "`%s' cannot be marked invariant; interfaces between "
5137 "shader stages only", var
->name
);
5141 if (state
->current_function
!= NULL
) {
5142 const char *mode
= NULL
;
5143 const char *extra
= "";
5145 /* There is no need to check for 'inout' here because the parser will
5146 * only allow that in function parameter lists.
5148 if (this->type
->qualifier
.flags
.q
.attribute
) {
5150 } else if (this->type
->qualifier
.is_subroutine_decl()) {
5151 mode
= "subroutine uniform";
5152 } else if (this->type
->qualifier
.flags
.q
.uniform
) {
5154 } else if (this->type
->qualifier
.flags
.q
.varying
) {
5156 } else if (this->type
->qualifier
.flags
.q
.in
) {
5158 extra
= " or in function parameter list";
5159 } else if (this->type
->qualifier
.flags
.q
.out
) {
5161 extra
= " or in function parameter list";
5165 _mesa_glsl_error(& loc
, state
,
5166 "%s variable `%s' must be declared at "
5168 mode
, var
->name
, extra
);
5170 } else if (var
->data
.mode
== ir_var_shader_in
) {
5171 var
->data
.read_only
= true;
5173 if (state
->stage
== MESA_SHADER_VERTEX
) {
5174 bool error_emitted
= false;
5176 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
5178 * "Vertex shader inputs can only be float, floating-point
5179 * vectors, matrices, signed and unsigned integers and integer
5180 * vectors. Vertex shader inputs can also form arrays of these
5181 * types, but not structures."
5183 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
5185 * "Vertex shader inputs can only be float, floating-point
5186 * vectors, matrices, signed and unsigned integers and integer
5187 * vectors. They cannot be arrays or structures."
5189 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
5191 * "The attribute qualifier can be used only with float,
5192 * floating-point vectors, and matrices. Attribute variables
5193 * cannot be declared as arrays or structures."
5195 * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
5197 * "Vertex shader inputs can only be float, floating-point
5198 * vectors, matrices, signed and unsigned integers and integer
5199 * vectors. Vertex shader inputs cannot be arrays or
5202 * From section 4.3.4 of the ARB_bindless_texture spec:
5204 * "(modify third paragraph of the section to allow sampler and
5205 * image types) ... Vertex shader inputs can only be float,
5206 * single-precision floating-point scalars, single-precision
5207 * floating-point vectors, matrices, signed and unsigned
5208 * integers and integer vectors, sampler and image types."
5210 const glsl_type
*check_type
= var
->type
->without_array();
5212 switch (check_type
->base_type
) {
5213 case GLSL_TYPE_FLOAT
:
5215 case GLSL_TYPE_UINT64
:
5216 case GLSL_TYPE_INT64
:
5218 case GLSL_TYPE_UINT
:
5220 if (state
->is_version(120, 300))
5222 case GLSL_TYPE_DOUBLE
:
5223 if (check_type
->is_double() && (state
->is_version(410, 0) || state
->ARB_vertex_attrib_64bit_enable
))
5225 case GLSL_TYPE_SAMPLER
:
5226 if (check_type
->is_sampler() && state
->has_bindless())
5228 case GLSL_TYPE_IMAGE
:
5229 if (check_type
->is_image() && state
->has_bindless())
5233 _mesa_glsl_error(& loc
, state
,
5234 "vertex shader input / attribute cannot have "
5236 var
->type
->is_array() ? "array of " : "",
5238 error_emitted
= true;
5241 if (!error_emitted
&& var
->type
->is_array() &&
5242 !state
->check_version(150, 0, &loc
,
5243 "vertex shader input / attribute "
5244 "cannot have array type")) {
5245 error_emitted
= true;
5247 } else if (state
->stage
== MESA_SHADER_GEOMETRY
) {
5248 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
5250 * Geometry shader input variables get the per-vertex values
5251 * written out by vertex shader output variables of the same
5252 * names. Since a geometry shader operates on a set of
5253 * vertices, each input varying variable (or input block, see
5254 * interface blocks below) needs to be declared as an array.
5256 if (!var
->type
->is_array()) {
5257 _mesa_glsl_error(&loc
, state
,
5258 "geometry shader inputs must be arrays");
5261 handle_geometry_shader_input_decl(state
, loc
, var
);
5262 } else if (state
->stage
== MESA_SHADER_FRAGMENT
) {
5263 /* From section 4.3.4 (Input Variables) of the GLSL ES 3.10 spec:
5265 * It is a compile-time error to declare a fragment shader
5266 * input with, or that contains, any of the following types:
5270 * * An array of arrays
5271 * * An array of structures
5272 * * A structure containing an array
5273 * * A structure containing a structure
5275 if (state
->es_shader
) {
5276 const glsl_type
*check_type
= var
->type
->without_array();
5277 if (check_type
->is_boolean() ||
5278 check_type
->contains_opaque()) {
5279 _mesa_glsl_error(&loc
, state
,
5280 "fragment shader input cannot have type %s",
5283 if (var
->type
->is_array() &&
5284 var
->type
->fields
.array
->is_array()) {
5285 _mesa_glsl_error(&loc
, state
,
5287 "cannot have an array of arrays",
5288 _mesa_shader_stage_to_string(state
->stage
));
5290 if (var
->type
->is_array() &&
5291 var
->type
->fields
.array
->is_record()) {
5292 _mesa_glsl_error(&loc
, state
,
5293 "fragment shader input "
5294 "cannot have an array of structs");
5296 if (var
->type
->is_record()) {
5297 for (unsigned i
= 0; i
< var
->type
->length
; i
++) {
5298 if (var
->type
->fields
.structure
[i
].type
->is_array() ||
5299 var
->type
->fields
.structure
[i
].type
->is_record())
5300 _mesa_glsl_error(&loc
, state
,
5301 "fragment shader input cannot have "
5302 "a struct that contains an "
5307 } else if (state
->stage
== MESA_SHADER_TESS_CTRL
||
5308 state
->stage
== MESA_SHADER_TESS_EVAL
) {
5309 handle_tess_shader_input_decl(state
, loc
, var
);
5311 } else if (var
->data
.mode
== ir_var_shader_out
) {
5312 const glsl_type
*check_type
= var
->type
->without_array();
5314 /* From section 4.3.6 (Output variables) of the GLSL 4.40 spec:
5316 * It is a compile-time error to declare a fragment shader output
5317 * that contains any of the following:
5319 * * A Boolean type (bool, bvec2 ...)
5320 * * A double-precision scalar or vector (double, dvec2 ...)
5325 if (state
->stage
== MESA_SHADER_FRAGMENT
) {
5326 if (check_type
->is_record() || check_type
->is_matrix())
5327 _mesa_glsl_error(&loc
, state
,
5328 "fragment shader output "
5329 "cannot have struct or matrix type");
5330 switch (check_type
->base_type
) {
5331 case GLSL_TYPE_UINT
:
5333 case GLSL_TYPE_FLOAT
:
5336 _mesa_glsl_error(&loc
, state
,
5337 "fragment shader output cannot have "
5338 "type %s", check_type
->name
);
5342 /* From section 4.3.6 (Output Variables) of the GLSL ES 3.10 spec:
5344 * It is a compile-time error to declare a vertex shader output
5345 * with, or that contains, any of the following types:
5349 * * An array of arrays
5350 * * An array of structures
5351 * * A structure containing an array
5352 * * A structure containing a structure
5354 * It is a compile-time error to declare a fragment shader output
5355 * with, or that contains, any of the following types:
5361 * * An array of array
5363 * ES 3.20 updates this to apply to tessellation and geometry shaders
5364 * as well. Because there are per-vertex arrays in the new stages,
5365 * it strikes the "array of..." rules and replaces them with these:
5367 * * For per-vertex-arrayed variables (applies to tessellation
5368 * control, tessellation evaluation and geometry shaders):
5370 * * Per-vertex-arrayed arrays of arrays
5371 * * Per-vertex-arrayed arrays of structures
5373 * * For non-per-vertex-arrayed variables:
5375 * * An array of arrays
5376 * * An array of structures
5378 * which basically says to unwrap the per-vertex aspect and apply
5381 if (state
->es_shader
) {
5382 if (var
->type
->is_array() &&
5383 var
->type
->fields
.array
->is_array()) {
5384 _mesa_glsl_error(&loc
, state
,
5386 "cannot have an array of arrays",
5387 _mesa_shader_stage_to_string(state
->stage
));
5389 if (state
->stage
<= MESA_SHADER_GEOMETRY
) {
5390 const glsl_type
*type
= var
->type
;
5392 if (state
->stage
== MESA_SHADER_TESS_CTRL
&&
5393 !var
->data
.patch
&& var
->type
->is_array()) {
5394 type
= var
->type
->fields
.array
;
5397 if (type
->is_array() && type
->fields
.array
->is_record()) {
5398 _mesa_glsl_error(&loc
, state
,
5399 "%s shader output cannot have "
5400 "an array of structs",
5401 _mesa_shader_stage_to_string(state
->stage
));
5403 if (type
->is_record()) {
5404 for (unsigned i
= 0; i
< type
->length
; i
++) {
5405 if (type
->fields
.structure
[i
].type
->is_array() ||
5406 type
->fields
.structure
[i
].type
->is_record())
5407 _mesa_glsl_error(&loc
, state
,
5408 "%s shader output cannot have a "
5409 "struct that contains an "
5411 _mesa_shader_stage_to_string(state
->stage
));
5417 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
5418 handle_tess_ctrl_shader_output_decl(state
, loc
, var
);
5420 } else if (var
->type
->contains_subroutine()) {
5421 /* declare subroutine uniforms as hidden */
5422 var
->data
.how_declared
= ir_var_hidden
;
5425 /* From section 4.3.4 of the GLSL 4.00 spec:
5426 * "Input variables may not be declared using the patch in qualifier
5427 * in tessellation control or geometry shaders."
5429 * From section 4.3.6 of the GLSL 4.00 spec:
5430 * "It is an error to use patch out in a vertex, tessellation
5431 * evaluation, or geometry shader."
5433 * This doesn't explicitly forbid using them in a fragment shader, but
5434 * that's probably just an oversight.
5436 if (state
->stage
!= MESA_SHADER_TESS_EVAL
5437 && this->type
->qualifier
.flags
.q
.patch
5438 && this->type
->qualifier
.flags
.q
.in
) {
5440 _mesa_glsl_error(&loc
, state
, "'patch in' can only be used in a "
5441 "tessellation evaluation shader");
5444 if (state
->stage
!= MESA_SHADER_TESS_CTRL
5445 && this->type
->qualifier
.flags
.q
.patch
5446 && this->type
->qualifier
.flags
.q
.out
) {
5448 _mesa_glsl_error(&loc
, state
, "'patch out' can only be used in a "
5449 "tessellation control shader");
5452 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
5454 if (this->type
->qualifier
.precision
!= ast_precision_none
) {
5455 state
->check_precision_qualifiers_allowed(&loc
);
5458 if (this->type
->qualifier
.precision
!= ast_precision_none
&&
5459 !precision_qualifier_allowed(var
->type
)) {
5460 _mesa_glsl_error(&loc
, state
,
5461 "precision qualifiers apply only to floating point"
5462 ", integer and opaque types");
5465 /* From section 4.1.7 of the GLSL 4.40 spec:
5467 * "[Opaque types] can only be declared as function
5468 * parameters or uniform-qualified variables."
5470 * From section 4.1.7 of the ARB_bindless_texture spec:
5472 * "Samplers may be declared as shader inputs and outputs, as uniform
5473 * variables, as temporary variables, and as function parameters."
5475 * From section 4.1.X of the ARB_bindless_texture spec:
5477 * "Images may be declared as shader inputs and outputs, as uniform
5478 * variables, as temporary variables, and as function parameters."
5480 if (!this->type
->qualifier
.flags
.q
.uniform
&&
5481 (var_type
->contains_atomic() ||
5482 (!state
->has_bindless() && var_type
->contains_opaque()))) {
5483 _mesa_glsl_error(&loc
, state
,
5484 "%s variables must be declared uniform",
5485 state
->has_bindless() ? "atomic" : "opaque");
5488 /* Process the initializer and add its instructions to a temporary
5489 * list. This list will be added to the instruction stream (below) after
5490 * the declaration is added. This is done because in some cases (such as
5491 * redeclarations) the declaration may not actually be added to the
5492 * instruction stream.
5494 exec_list initializer_instructions
;
5496 /* Examine var name here since var may get deleted in the next call */
5497 bool var_is_gl_id
= is_gl_identifier(var
->name
);
5499 bool is_redeclaration
;
5500 var
= get_variable_being_redeclared(&var
, decl
->get_location(), state
,
5501 false /* allow_all_redeclarations */,
5503 if (is_redeclaration
) {
5505 var
->data
.how_declared
== ir_var_declared_in_block
) {
5506 _mesa_glsl_error(&loc
, state
,
5507 "`%s' has already been redeclared using "
5508 "gl_PerVertex", var
->name
);
5510 var
->data
.how_declared
= ir_var_declared_normally
;
5513 if (decl
->initializer
!= NULL
) {
5514 result
= process_initializer(var
,
5516 &initializer_instructions
, state
);
5518 validate_array_dimensions(var_type
, state
, &loc
);
5521 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
5523 * "It is an error to write to a const variable outside of
5524 * its declaration, so they must be initialized when
5527 if (this->type
->qualifier
.flags
.q
.constant
&& decl
->initializer
== NULL
) {
5528 _mesa_glsl_error(& loc
, state
,
5529 "const declaration of `%s' must be initialized",
5533 if (state
->es_shader
) {
5534 const glsl_type
*const t
= var
->type
;
5536 /* Skip the unsized array check for TCS/TES/GS inputs & TCS outputs.
5538 * The GL_OES_tessellation_shader spec says about inputs:
5540 * "Declaring an array size is optional. If no size is specified,
5541 * it will be taken from the implementation-dependent maximum
5542 * patch size (gl_MaxPatchVertices)."
5544 * and about TCS outputs:
5546 * "If no size is specified, it will be taken from output patch
5547 * size declared in the shader."
5549 * The GL_OES_geometry_shader spec says:
5551 * "All geometry shader input unsized array declarations will be
5552 * sized by an earlier input primitive layout qualifier, when
5553 * present, as per the following table."
5555 const bool implicitly_sized
=
5556 (var
->data
.mode
== ir_var_shader_in
&&
5557 state
->stage
>= MESA_SHADER_TESS_CTRL
&&
5558 state
->stage
<= MESA_SHADER_GEOMETRY
) ||
5559 (var
->data
.mode
== ir_var_shader_out
&&
5560 state
->stage
== MESA_SHADER_TESS_CTRL
);
5562 if (t
->is_unsized_array() && !implicitly_sized
)
5563 /* Section 10.17 of the GLSL ES 1.00 specification states that
5564 * unsized array declarations have been removed from the language.
5565 * Arrays that are sized using an initializer are still explicitly
5566 * sized. However, GLSL ES 1.00 does not allow array
5567 * initializers. That is only allowed in GLSL ES 3.00.
5569 * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
5571 * "An array type can also be formed without specifying a size
5572 * if the definition includes an initializer:
5574 * float x[] = float[2] (1.0, 2.0); // declares an array of size 2
5575 * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
5580 _mesa_glsl_error(& loc
, state
,
5581 "unsized array declarations are not allowed in "
5585 /* Section 4.4.6.1 Atomic Counter Layout Qualifiers of the GLSL 4.60 spec:
5587 * "It is a compile-time error to declare an unsized array of
5590 if (var
->type
->is_unsized_array() &&
5591 var
->type
->without_array()->base_type
== GLSL_TYPE_ATOMIC_UINT
) {
5592 _mesa_glsl_error(& loc
, state
,
5593 "Unsized array of atomic_uint is not allowed");
5596 /* If the declaration is not a redeclaration, there are a few additional
5597 * semantic checks that must be applied. In addition, variable that was
5598 * created for the declaration should be added to the IR stream.
5600 if (!is_redeclaration
) {
5601 validate_identifier(decl
->identifier
, loc
, state
);
5603 /* Add the variable to the symbol table. Note that the initializer's
5604 * IR was already processed earlier (though it hasn't been emitted
5605 * yet), without the variable in scope.
5607 * This differs from most C-like languages, but it follows the GLSL
5608 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
5611 * "Within a declaration, the scope of a name starts immediately
5612 * after the initializer if present or immediately after the name
5613 * being declared if not."
5615 if (!state
->symbols
->add_variable(var
)) {
5616 YYLTYPE loc
= this->get_location();
5617 _mesa_glsl_error(&loc
, state
, "name `%s' already taken in the "
5618 "current scope", decl
->identifier
);
5622 /* Push the variable declaration to the top. It means that all the
5623 * variable declarations will appear in a funny last-to-first order,
5624 * but otherwise we run into trouble if a function is prototyped, a
5625 * global var is decled, then the function is defined with usage of
5626 * the global var. See glslparsertest's CorrectModule.frag.
5628 instructions
->push_head(var
);
5631 instructions
->append_list(&initializer_instructions
);
5635 /* Generally, variable declarations do not have r-values. However,
5636 * one is used for the declaration in
5638 * while (bool b = some_condition()) {
5642 * so we return the rvalue from the last seen declaration here.
5649 ast_parameter_declarator::hir(exec_list
*instructions
,
5650 struct _mesa_glsl_parse_state
*state
)
5653 const struct glsl_type
*type
;
5654 const char *name
= NULL
;
5655 YYLTYPE loc
= this->get_location();
5657 type
= this->type
->glsl_type(& name
, state
);
5661 _mesa_glsl_error(& loc
, state
,
5662 "invalid type `%s' in declaration of `%s'",
5663 name
, this->identifier
);
5665 _mesa_glsl_error(& loc
, state
,
5666 "invalid type in declaration of `%s'",
5670 type
= glsl_type::error_type
;
5673 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
5675 * "Functions that accept no input arguments need not use void in the
5676 * argument list because prototypes (or definitions) are required and
5677 * therefore there is no ambiguity when an empty argument list "( )" is
5678 * declared. The idiom "(void)" as a parameter list is provided for
5681 * Placing this check here prevents a void parameter being set up
5682 * for a function, which avoids tripping up checks for main taking
5683 * parameters and lookups of an unnamed symbol.
5685 if (type
->is_void()) {
5686 if (this->identifier
!= NULL
)
5687 _mesa_glsl_error(& loc
, state
,
5688 "named parameter cannot have type `void'");
5694 if (formal_parameter
&& (this->identifier
== NULL
)) {
5695 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
5699 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
5700 * call already handled the "vec4[..] foo" case.
5702 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
5704 if (!type
->is_error() && type
->is_unsized_array()) {
5705 _mesa_glsl_error(&loc
, state
, "arrays passed as parameters must have "
5707 type
= glsl_type::error_type
;
5711 ir_variable
*var
= new(ctx
)
5712 ir_variable(type
, this->identifier
, ir_var_function_in
);
5714 /* Apply any specified qualifiers to the parameter declaration. Note that
5715 * for function parameters the default mode is 'in'.
5717 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
,
5720 /* From section 4.1.7 of the GLSL 4.40 spec:
5722 * "Opaque variables cannot be treated as l-values; hence cannot
5723 * be used as out or inout function parameters, nor can they be
5726 * From section 4.1.7 of the ARB_bindless_texture spec:
5728 * "Samplers can be used as l-values, so can be assigned into and used
5729 * as "out" and "inout" function parameters."
5731 * From section 4.1.X of the ARB_bindless_texture spec:
5733 * "Images can be used as l-values, so can be assigned into and used as
5734 * "out" and "inout" function parameters."
5736 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
5737 && (type
->contains_atomic() ||
5738 (!state
->has_bindless() && type
->contains_opaque()))) {
5739 _mesa_glsl_error(&loc
, state
, "out and inout parameters cannot "
5740 "contain %s variables",
5741 state
->has_bindless() ? "atomic" : "opaque");
5742 type
= glsl_type::error_type
;
5745 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
5747 * "When calling a function, expressions that do not evaluate to
5748 * l-values cannot be passed to parameters declared as out or inout."
5750 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
5752 * "Other binary or unary expressions, non-dereferenced arrays,
5753 * function names, swizzles with repeated fields, and constants
5754 * cannot be l-values."
5756 * So for GLSL 1.10, passing an array as an out or inout parameter is not
5757 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
5759 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
5761 && !state
->check_version(120, 100, &loc
,
5762 "arrays cannot be out or inout parameters")) {
5763 type
= glsl_type::error_type
;
5766 instructions
->push_tail(var
);
5768 /* Parameter declarations do not have r-values.
5775 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
5777 exec_list
*ir_parameters
,
5778 _mesa_glsl_parse_state
*state
)
5780 ast_parameter_declarator
*void_param
= NULL
;
5783 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
5784 param
->formal_parameter
= formal
;
5785 param
->hir(ir_parameters
, state
);
5793 if ((void_param
!= NULL
) && (count
> 1)) {
5794 YYLTYPE loc
= void_param
->get_location();
5796 _mesa_glsl_error(& loc
, state
,
5797 "`void' parameter must be only parameter");
5803 emit_function(_mesa_glsl_parse_state
*state
, ir_function
*f
)
5805 /* IR invariants disallow function declarations or definitions
5806 * nested within other function definitions. But there is no
5807 * requirement about the relative order of function declarations
5808 * and definitions with respect to one another. So simply insert
5809 * the new ir_function block at the end of the toplevel instruction
5812 state
->toplevel_ir
->push_tail(f
);
5817 ast_function::hir(exec_list
*instructions
,
5818 struct _mesa_glsl_parse_state
*state
)
5821 ir_function
*f
= NULL
;
5822 ir_function_signature
*sig
= NULL
;
5823 exec_list hir_parameters
;
5824 YYLTYPE loc
= this->get_location();
5826 const char *const name
= identifier
;
5828 /* New functions are always added to the top-level IR instruction stream,
5829 * so this instruction list pointer is ignored. See also emit_function
5832 (void) instructions
;
5834 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
5836 * "Function declarations (prototypes) cannot occur inside of functions;
5837 * they must be at global scope, or for the built-in functions, outside
5838 * the global scope."
5840 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
5842 * "User defined functions may only be defined within the global scope."
5844 * Note that this language does not appear in GLSL 1.10.
5846 if ((state
->current_function
!= NULL
) &&
5847 state
->is_version(120, 100)) {
5848 YYLTYPE loc
= this->get_location();
5849 _mesa_glsl_error(&loc
, state
,
5850 "declaration of function `%s' not allowed within "
5851 "function body", name
);
5854 validate_identifier(name
, this->get_location(), state
);
5856 /* Convert the list of function parameters to HIR now so that they can be
5857 * used below to compare this function's signature with previously seen
5858 * signatures for functions with the same name.
5860 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
5862 & hir_parameters
, state
);
5864 const char *return_type_name
;
5865 const glsl_type
*return_type
=
5866 this->return_type
->glsl_type(& return_type_name
, state
);
5869 YYLTYPE loc
= this->get_location();
5870 _mesa_glsl_error(&loc
, state
,
5871 "function `%s' has undeclared return type `%s'",
5872 name
, return_type_name
);
5873 return_type
= glsl_type::error_type
;
5876 /* ARB_shader_subroutine states:
5877 * "Subroutine declarations cannot be prototyped. It is an error to prepend
5878 * subroutine(...) to a function declaration."
5880 if (this->return_type
->qualifier
.subroutine_list
&& !is_definition
) {
5881 YYLTYPE loc
= this->get_location();
5882 _mesa_glsl_error(&loc
, state
,
5883 "function declaration `%s' cannot have subroutine prepended",
5887 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
5888 * "No qualifier is allowed on the return type of a function."
5890 if (this->return_type
->has_qualifiers(state
)) {
5891 YYLTYPE loc
= this->get_location();
5892 _mesa_glsl_error(& loc
, state
,
5893 "function `%s' return type has qualifiers", name
);
5896 /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
5898 * "Arrays are allowed as arguments and as the return type. In both
5899 * cases, the array must be explicitly sized."
5901 if (return_type
->is_unsized_array()) {
5902 YYLTYPE loc
= this->get_location();
5903 _mesa_glsl_error(& loc
, state
,
5904 "function `%s' return type array must be explicitly "
5908 /* From Section 6.1 (Function Definitions) of the GLSL 1.00 spec:
5910 * "Arrays are allowed as arguments, but not as the return type. [...]
5911 * The return type can also be a structure if the structure does not
5912 * contain an array."
5914 if (state
->language_version
== 100 && return_type
->contains_array()) {
5915 YYLTYPE loc
= this->get_location();
5916 _mesa_glsl_error(& loc
, state
,
5917 "function `%s' return type contains an array", name
);
5920 /* From section 4.1.7 of the GLSL 4.40 spec:
5922 * "[Opaque types] can only be declared as function parameters
5923 * or uniform-qualified variables."
5925 * The ARB_bindless_texture spec doesn't clearly state this, but as it says
5926 * "Replace Section 4.1.7 (Samplers), p. 25" and, "Replace Section 4.1.X,
5927 * (Images)", this should be allowed.
5929 if (return_type
->contains_atomic() ||
5930 (!state
->has_bindless() && return_type
->contains_opaque())) {
5931 YYLTYPE loc
= this->get_location();
5932 _mesa_glsl_error(&loc
, state
,
5933 "function `%s' return type can't contain an %s type",
5934 name
, state
->has_bindless() ? "atomic" : "opaque");
5938 if (return_type
->is_subroutine()) {
5939 YYLTYPE loc
= this->get_location();
5940 _mesa_glsl_error(&loc
, state
,
5941 "function `%s' return type can't be a subroutine type",
5946 /* Create an ir_function if one doesn't already exist. */
5947 f
= state
->symbols
->get_function(name
);
5949 f
= new(ctx
) ir_function(name
);
5950 if (!this->return_type
->qualifier
.is_subroutine_decl()) {
5951 if (!state
->symbols
->add_function(f
)) {
5952 /* This function name shadows a non-function use of the same name. */
5953 YYLTYPE loc
= this->get_location();
5954 _mesa_glsl_error(&loc
, state
, "function name `%s' conflicts with "
5955 "non-function", name
);
5959 emit_function(state
, f
);
5962 /* From GLSL ES 3.0 spec, chapter 6.1 "Function Definitions", page 71:
5964 * "A shader cannot redefine or overload built-in functions."
5966 * While in GLSL ES 1.0 specification, chapter 8 "Built-in Functions":
5968 * "User code can overload the built-in functions but cannot redefine
5971 if (state
->es_shader
) {
5972 /* Local shader has no exact candidates; check the built-ins. */
5973 _mesa_glsl_initialize_builtin_functions();
5974 if (state
->language_version
>= 300 &&
5975 _mesa_glsl_has_builtin_function(state
, name
)) {
5976 YYLTYPE loc
= this->get_location();
5977 _mesa_glsl_error(& loc
, state
,
5978 "A shader cannot redefine or overload built-in "
5979 "function `%s' in GLSL ES 3.00", name
);
5983 if (state
->language_version
== 100) {
5984 ir_function_signature
*sig
=
5985 _mesa_glsl_find_builtin_function(state
, name
, &hir_parameters
);
5986 if (sig
&& sig
->is_builtin()) {
5987 _mesa_glsl_error(& loc
, state
,
5988 "A shader cannot redefine built-in "
5989 "function `%s' in GLSL ES 1.00", name
);
5994 /* Verify that this function's signature either doesn't match a previously
5995 * seen signature for a function with the same name, or, if a match is found,
5996 * that the previously seen signature does not have an associated definition.
5998 if (state
->es_shader
|| f
->has_user_signature()) {
5999 sig
= f
->exact_matching_signature(state
, &hir_parameters
);
6001 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
6002 if (badvar
!= NULL
) {
6003 YYLTYPE loc
= this->get_location();
6005 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
6006 "qualifiers don't match prototype", name
, badvar
);
6009 if (sig
->return_type
!= return_type
) {
6010 YYLTYPE loc
= this->get_location();
6012 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
6013 "match prototype", name
);
6016 if (sig
->is_defined
) {
6017 if (is_definition
) {
6018 YYLTYPE loc
= this->get_location();
6019 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
6021 /* We just encountered a prototype that exactly matches a
6022 * function that's already been defined. This is redundant,
6023 * and we should ignore it.
6027 } else if (state
->language_version
== 100 && !is_definition
) {
6028 /* From the GLSL 1.00 spec, section 4.2.7:
6030 * "A particular variable, structure or function declaration
6031 * may occur at most once within a scope with the exception
6032 * that a single function prototype plus the corresponding
6033 * function definition are allowed."
6035 YYLTYPE loc
= this->get_location();
6036 _mesa_glsl_error(&loc
, state
, "function `%s' redeclared", name
);
6041 /* Verify the return type of main() */
6042 if (strcmp(name
, "main") == 0) {
6043 if (! return_type
->is_void()) {
6044 YYLTYPE loc
= this->get_location();
6046 _mesa_glsl_error(& loc
, state
, "main() must return void");
6049 if (!hir_parameters
.is_empty()) {
6050 YYLTYPE loc
= this->get_location();
6052 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
6056 /* Finish storing the information about this new function in its signature.
6059 sig
= new(ctx
) ir_function_signature(return_type
);
6060 f
->add_signature(sig
);
6063 sig
->replace_parameters(&hir_parameters
);
6066 if (this->return_type
->qualifier
.subroutine_list
) {
6069 if (this->return_type
->qualifier
.flags
.q
.explicit_index
) {
6070 unsigned qual_index
;
6071 if (process_qualifier_constant(state
, &loc
, "index",
6072 this->return_type
->qualifier
.index
,
6074 if (!state
->has_explicit_uniform_location()) {
6075 _mesa_glsl_error(&loc
, state
, "subroutine index requires "
6076 "GL_ARB_explicit_uniform_location or "
6078 } else if (qual_index
>= MAX_SUBROUTINES
) {
6079 _mesa_glsl_error(&loc
, state
,
6080 "invalid subroutine index (%d) index must "
6081 "be a number between 0 and "
6082 "GL_MAX_SUBROUTINES - 1 (%d)", qual_index
,
6083 MAX_SUBROUTINES
- 1);
6085 f
->subroutine_index
= qual_index
;
6090 f
->num_subroutine_types
= this->return_type
->qualifier
.subroutine_list
->declarations
.length();
6091 f
->subroutine_types
= ralloc_array(state
, const struct glsl_type
*,
6092 f
->num_subroutine_types
);
6094 foreach_list_typed(ast_declaration
, decl
, link
, &this->return_type
->qualifier
.subroutine_list
->declarations
) {
6095 const struct glsl_type
*type
;
6096 /* the subroutine type must be already declared */
6097 type
= state
->symbols
->get_type(decl
->identifier
);
6099 _mesa_glsl_error(& loc
, state
, "unknown type '%s' in subroutine function definition", decl
->identifier
);
6102 for (int i
= 0; i
< state
->num_subroutine_types
; i
++) {
6103 ir_function
*fn
= state
->subroutine_types
[i
];
6104 ir_function_signature
*tsig
= NULL
;
6106 if (strcmp(fn
->name
, decl
->identifier
))
6109 tsig
= fn
->matching_signature(state
, &sig
->parameters
,
6112 _mesa_glsl_error(& loc
, state
, "subroutine type mismatch '%s' - signatures do not match\n", decl
->identifier
);
6114 if (tsig
->return_type
!= sig
->return_type
) {
6115 _mesa_glsl_error(& loc
, state
, "subroutine type mismatch '%s' - return types do not match\n", decl
->identifier
);
6119 f
->subroutine_types
[idx
++] = type
;
6121 state
->subroutines
= (ir_function
**)reralloc(state
, state
->subroutines
,
6123 state
->num_subroutines
+ 1);
6124 state
->subroutines
[state
->num_subroutines
] = f
;
6125 state
->num_subroutines
++;
6129 if (this->return_type
->qualifier
.is_subroutine_decl()) {
6130 if (!state
->symbols
->add_type(this->identifier
, glsl_type::get_subroutine_instance(this->identifier
))) {
6131 _mesa_glsl_error(& loc
, state
, "type '%s' previously defined", this->identifier
);
6134 state
->subroutine_types
= (ir_function
**)reralloc(state
, state
->subroutine_types
,
6136 state
->num_subroutine_types
+ 1);
6137 state
->subroutine_types
[state
->num_subroutine_types
] = f
;
6138 state
->num_subroutine_types
++;
6140 f
->is_subroutine
= true;
6143 /* Function declarations (prototypes) do not have r-values.
6150 ast_function_definition::hir(exec_list
*instructions
,
6151 struct _mesa_glsl_parse_state
*state
)
6153 prototype
->is_definition
= true;
6154 prototype
->hir(instructions
, state
);
6156 ir_function_signature
*signature
= prototype
->signature
;
6157 if (signature
== NULL
)
6160 assert(state
->current_function
== NULL
);
6161 state
->current_function
= signature
;
6162 state
->found_return
= false;
6164 /* Duplicate parameters declared in the prototype as concrete variables.
6165 * Add these to the symbol table.
6167 state
->symbols
->push_scope();
6168 foreach_in_list(ir_variable
, var
, &signature
->parameters
) {
6169 assert(var
->as_variable() != NULL
);
6171 /* The only way a parameter would "exist" is if two parameters have
6174 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
6175 YYLTYPE loc
= this->get_location();
6177 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
6179 state
->symbols
->add_variable(var
);
6183 /* Convert the body of the function to HIR. */
6184 this->body
->hir(&signature
->body
, state
);
6185 signature
->is_defined
= true;
6187 state
->symbols
->pop_scope();
6189 assert(state
->current_function
== signature
);
6190 state
->current_function
= NULL
;
6192 if (!signature
->return_type
->is_void() && !state
->found_return
) {
6193 YYLTYPE loc
= this->get_location();
6194 _mesa_glsl_error(& loc
, state
, "function `%s' has non-void return type "
6195 "%s, but no return statement",
6196 signature
->function_name(),
6197 signature
->return_type
->name
);
6200 /* Function definitions do not have r-values.
6207 ast_jump_statement::hir(exec_list
*instructions
,
6208 struct _mesa_glsl_parse_state
*state
)
6215 assert(state
->current_function
);
6217 if (opt_return_value
) {
6218 ir_rvalue
*ret
= opt_return_value
->hir(instructions
, state
);
6220 /* The value of the return type can be NULL if the shader says
6221 * 'return foo();' and foo() is a function that returns void.
6223 * NOTE: The GLSL spec doesn't say that this is an error. The type
6224 * of the return value is void. If the return type of the function is
6225 * also void, then this should compile without error. Seriously.
6227 const glsl_type
*const ret_type
=
6228 (ret
== NULL
) ? glsl_type::void_type
: ret
->type
;
6230 /* Implicit conversions are not allowed for return values prior to
6231 * ARB_shading_language_420pack.
6233 if (state
->current_function
->return_type
!= ret_type
) {
6234 YYLTYPE loc
= this->get_location();
6236 if (state
->has_420pack()) {
6237 if (!apply_implicit_conversion(state
->current_function
->return_type
,
6239 _mesa_glsl_error(& loc
, state
,
6240 "could not implicitly convert return value "
6241 "to %s, in function `%s'",
6242 state
->current_function
->return_type
->name
,
6243 state
->current_function
->function_name());
6246 _mesa_glsl_error(& loc
, state
,
6247 "`return' with wrong type %s, in function `%s' "
6250 state
->current_function
->function_name(),
6251 state
->current_function
->return_type
->name
);
6253 } else if (state
->current_function
->return_type
->base_type
==
6255 YYLTYPE loc
= this->get_location();
6257 /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
6258 * specs add a clarification:
6260 * "A void function can only use return without a return argument, even if
6261 * the return argument has void type. Return statements only accept values:
6264 * void func2() { return func1(); } // illegal return statement"
6266 _mesa_glsl_error(& loc
, state
,
6267 "void functions can only use `return' without a "
6271 inst
= new(ctx
) ir_return(ret
);
6273 if (state
->current_function
->return_type
->base_type
!=
6275 YYLTYPE loc
= this->get_location();
6277 _mesa_glsl_error(& loc
, state
,
6278 "`return' with no value, in function %s returning "
6280 state
->current_function
->function_name());
6282 inst
= new(ctx
) ir_return
;
6285 state
->found_return
= true;
6286 instructions
->push_tail(inst
);
6291 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
6292 YYLTYPE loc
= this->get_location();
6294 _mesa_glsl_error(& loc
, state
,
6295 "`discard' may only appear in a fragment shader");
6297 instructions
->push_tail(new(ctx
) ir_discard
);
6302 if (mode
== ast_continue
&&
6303 state
->loop_nesting_ast
== NULL
) {
6304 YYLTYPE loc
= this->get_location();
6306 _mesa_glsl_error(& loc
, state
, "continue may only appear in a loop");
6307 } else if (mode
== ast_break
&&
6308 state
->loop_nesting_ast
== NULL
&&
6309 state
->switch_state
.switch_nesting_ast
== NULL
) {
6310 YYLTYPE loc
= this->get_location();
6312 _mesa_glsl_error(& loc
, state
,
6313 "break may only appear in a loop or a switch");
6315 /* For a loop, inline the for loop expression again, since we don't
6316 * know where near the end of the loop body the normal copy of it is
6317 * going to be placed. Same goes for the condition for a do-while
6320 if (state
->loop_nesting_ast
!= NULL
&&
6321 mode
== ast_continue
&& !state
->switch_state
.is_switch_innermost
) {
6322 if (state
->loop_nesting_ast
->rest_expression
) {
6323 state
->loop_nesting_ast
->rest_expression
->hir(instructions
,
6326 if (state
->loop_nesting_ast
->mode
==
6327 ast_iteration_statement::ast_do_while
) {
6328 state
->loop_nesting_ast
->condition_to_hir(instructions
, state
);
6332 if (state
->switch_state
.is_switch_innermost
&&
6333 mode
== ast_continue
) {
6334 /* Set 'continue_inside' to true. */
6335 ir_rvalue
*const true_val
= new (ctx
) ir_constant(true);
6336 ir_dereference_variable
*deref_continue_inside_var
=
6337 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
6338 instructions
->push_tail(new(ctx
) ir_assignment(deref_continue_inside_var
,
6341 /* Break out from the switch, continue for the loop will
6342 * be called right after switch. */
6343 ir_loop_jump
*const jump
=
6344 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
6345 instructions
->push_tail(jump
);
6347 } else if (state
->switch_state
.is_switch_innermost
&&
6348 mode
== ast_break
) {
6349 /* Force break out of switch by inserting a break. */
6350 ir_loop_jump
*const jump
=
6351 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
6352 instructions
->push_tail(jump
);
6354 ir_loop_jump
*const jump
=
6355 new(ctx
) ir_loop_jump((mode
== ast_break
)
6356 ? ir_loop_jump::jump_break
6357 : ir_loop_jump::jump_continue
);
6358 instructions
->push_tail(jump
);
6365 /* Jump instructions do not have r-values.
6372 ast_selection_statement::hir(exec_list
*instructions
,
6373 struct _mesa_glsl_parse_state
*state
)
6377 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
6379 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
6381 * "Any expression whose type evaluates to a Boolean can be used as the
6382 * conditional expression bool-expression. Vector types are not accepted
6383 * as the expression to if."
6385 * The checks are separated so that higher quality diagnostics can be
6386 * generated for cases where both rules are violated.
6388 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
6389 YYLTYPE loc
= this->condition
->get_location();
6391 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
6395 ir_if
*const stmt
= new(ctx
) ir_if(condition
);
6397 if (then_statement
!= NULL
) {
6398 state
->symbols
->push_scope();
6399 then_statement
->hir(& stmt
->then_instructions
, state
);
6400 state
->symbols
->pop_scope();
6403 if (else_statement
!= NULL
) {
6404 state
->symbols
->push_scope();
6405 else_statement
->hir(& stmt
->else_instructions
, state
);
6406 state
->symbols
->pop_scope();
6409 instructions
->push_tail(stmt
);
6411 /* if-statements do not have r-values.
6418 /** Value of the case label. */
6421 /** Does this label occur after the default? */
6425 * AST for the case label.
6427 * This is only used to generate error messages for duplicate labels.
6429 ast_expression
*ast
;
6432 /* Used for detection of duplicate case values, compare
6433 * given contents directly.
6436 compare_case_value(const void *a
, const void *b
)
6438 return ((struct case_label
*) a
)->value
== ((struct case_label
*) b
)->value
;
6442 /* Used for detection of duplicate case values, just
6443 * returns key contents as is.
6446 key_contents(const void *key
)
6448 return ((struct case_label
*) key
)->value
;
6453 ast_switch_statement::hir(exec_list
*instructions
,
6454 struct _mesa_glsl_parse_state
*state
)
6458 ir_rvalue
*const test_expression
=
6459 this->test_expression
->hir(instructions
, state
);
6461 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
6463 * "The type of init-expression in a switch statement must be a
6466 if (!test_expression
->type
->is_scalar() ||
6467 !test_expression
->type
->is_integer()) {
6468 YYLTYPE loc
= this->test_expression
->get_location();
6470 _mesa_glsl_error(& loc
,
6472 "switch-statement expression must be scalar "
6477 /* Track the switch-statement nesting in a stack-like manner.
6479 struct glsl_switch_state saved
= state
->switch_state
;
6481 state
->switch_state
.is_switch_innermost
= true;
6482 state
->switch_state
.switch_nesting_ast
= this;
6483 state
->switch_state
.labels_ht
=
6484 _mesa_hash_table_create(NULL
, key_contents
,
6485 compare_case_value
);
6486 state
->switch_state
.previous_default
= NULL
;
6488 /* Initalize is_fallthru state to false.
6490 ir_rvalue
*const is_fallthru_val
= new (ctx
) ir_constant(false);
6491 state
->switch_state
.is_fallthru_var
=
6492 new(ctx
) ir_variable(glsl_type::bool_type
,
6493 "switch_is_fallthru_tmp",
6495 instructions
->push_tail(state
->switch_state
.is_fallthru_var
);
6497 ir_dereference_variable
*deref_is_fallthru_var
=
6498 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
6499 instructions
->push_tail(new(ctx
) ir_assignment(deref_is_fallthru_var
,
6502 /* Initialize continue_inside state to false.
6504 state
->switch_state
.continue_inside
=
6505 new(ctx
) ir_variable(glsl_type::bool_type
,
6506 "continue_inside_tmp",
6508 instructions
->push_tail(state
->switch_state
.continue_inside
);
6510 ir_rvalue
*const false_val
= new (ctx
) ir_constant(false);
6511 ir_dereference_variable
*deref_continue_inside_var
=
6512 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
6513 instructions
->push_tail(new(ctx
) ir_assignment(deref_continue_inside_var
,
6516 state
->switch_state
.run_default
=
6517 new(ctx
) ir_variable(glsl_type::bool_type
,
6520 instructions
->push_tail(state
->switch_state
.run_default
);
6522 /* Loop around the switch is used for flow control. */
6523 ir_loop
* loop
= new(ctx
) ir_loop();
6524 instructions
->push_tail(loop
);
6526 /* Cache test expression.
6528 test_to_hir(&loop
->body_instructions
, state
);
6530 /* Emit code for body of switch stmt.
6532 body
->hir(&loop
->body_instructions
, state
);
6534 /* Insert a break at the end to exit loop. */
6535 ir_loop_jump
*jump
= new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
6536 loop
->body_instructions
.push_tail(jump
);
6538 /* If we are inside loop, check if continue got called inside switch. */
6539 if (state
->loop_nesting_ast
!= NULL
) {
6540 ir_dereference_variable
*deref_continue_inside
=
6541 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
6542 ir_if
*irif
= new(ctx
) ir_if(deref_continue_inside
);
6543 ir_loop_jump
*jump
= new(ctx
) ir_loop_jump(ir_loop_jump::jump_continue
);
6545 if (state
->loop_nesting_ast
!= NULL
) {
6546 if (state
->loop_nesting_ast
->rest_expression
) {
6547 state
->loop_nesting_ast
->rest_expression
->hir(&irif
->then_instructions
,
6550 if (state
->loop_nesting_ast
->mode
==
6551 ast_iteration_statement::ast_do_while
) {
6552 state
->loop_nesting_ast
->condition_to_hir(&irif
->then_instructions
, state
);
6555 irif
->then_instructions
.push_tail(jump
);
6556 instructions
->push_tail(irif
);
6559 _mesa_hash_table_destroy(state
->switch_state
.labels_ht
, NULL
);
6561 state
->switch_state
= saved
;
6563 /* Switch statements do not have r-values. */
6569 ast_switch_statement::test_to_hir(exec_list
*instructions
,
6570 struct _mesa_glsl_parse_state
*state
)
6574 /* set to true to avoid a duplicate "use of uninitialized variable" warning
6575 * on the switch test case. The first one would be already raised when
6576 * getting the test_expression at ast_switch_statement::hir
6578 test_expression
->set_is_lhs(true);
6579 /* Cache value of test expression. */
6580 ir_rvalue
*const test_val
= test_expression
->hir(instructions
, state
);
6582 state
->switch_state
.test_var
= new(ctx
) ir_variable(test_val
->type
,
6585 ir_dereference_variable
*deref_test_var
=
6586 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
6588 instructions
->push_tail(state
->switch_state
.test_var
);
6589 instructions
->push_tail(new(ctx
) ir_assignment(deref_test_var
, test_val
));
6594 ast_switch_body::hir(exec_list
*instructions
,
6595 struct _mesa_glsl_parse_state
*state
)
6598 stmts
->hir(instructions
, state
);
6600 /* Switch bodies do not have r-values. */
6605 ast_case_statement_list::hir(exec_list
*instructions
,
6606 struct _mesa_glsl_parse_state
*state
)
6608 exec_list default_case
, after_default
, tmp
;
6610 foreach_list_typed (ast_case_statement
, case_stmt
, link
, & this->cases
) {
6611 case_stmt
->hir(&tmp
, state
);
6614 if (state
->switch_state
.previous_default
&& default_case
.is_empty()) {
6615 default_case
.append_list(&tmp
);
6619 /* If default case found, append 'after_default' list. */
6620 if (!default_case
.is_empty())
6621 after_default
.append_list(&tmp
);
6623 instructions
->append_list(&tmp
);
6626 /* Handle the default case. This is done here because default might not be
6627 * the last case. We need to add checks against following cases first to see
6628 * if default should be chosen or not.
6630 if (!default_case
.is_empty()) {
6631 struct hash_entry
*entry
;
6632 ir_factory
body(instructions
, state
);
6634 ir_expression
*cmp
= NULL
;
6636 hash_table_foreach(state
->switch_state
.labels_ht
, entry
) {
6637 const struct case_label
*const l
= (struct case_label
*) entry
->data
;
6639 /* If the switch init-value is the value of one of the labels that
6640 * occurs after the default case, disable execution of the default
6643 if (l
->after_default
) {
6644 ir_constant
*const cnst
=
6645 state
->switch_state
.test_var
->type
->base_type
== GLSL_TYPE_UINT
6646 ? body
.constant(unsigned(l
->value
))
6647 : body
.constant(int(l
->value
));
6650 ? equal(cnst
, state
->switch_state
.test_var
)
6651 : logic_or(cmp
, equal(cnst
, state
->switch_state
.test_var
));
6656 body
.emit(assign(state
->switch_state
.run_default
, logic_not(cmp
)));
6658 body
.emit(assign(state
->switch_state
.run_default
, body
.constant(true)));
6660 /* Append default case and all cases after it. */
6661 instructions
->append_list(&default_case
);
6662 instructions
->append_list(&after_default
);
6665 /* Case statements do not have r-values. */
6670 ast_case_statement::hir(exec_list
*instructions
,
6671 struct _mesa_glsl_parse_state
*state
)
6673 labels
->hir(instructions
, state
);
6675 /* Guard case statements depending on fallthru state. */
6676 ir_dereference_variable
*const deref_fallthru_guard
=
6677 new(state
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
6678 ir_if
*const test_fallthru
= new(state
) ir_if(deref_fallthru_guard
);
6680 foreach_list_typed (ast_node
, stmt
, link
, & this->stmts
)
6681 stmt
->hir(& test_fallthru
->then_instructions
, state
);
6683 instructions
->push_tail(test_fallthru
);
6685 /* Case statements do not have r-values. */
6691 ast_case_label_list::hir(exec_list
*instructions
,
6692 struct _mesa_glsl_parse_state
*state
)
6694 foreach_list_typed (ast_case_label
, label
, link
, & this->labels
)
6695 label
->hir(instructions
, state
);
6697 /* Case labels do not have r-values. */
6702 ast_case_label::hir(exec_list
*instructions
,
6703 struct _mesa_glsl_parse_state
*state
)
6705 ir_factory
body(instructions
, state
);
6707 ir_variable
*const fallthru_var
= state
->switch_state
.is_fallthru_var
;
6709 /* If not default case, ... */
6710 if (this->test_value
!= NULL
) {
6711 /* Conditionally set fallthru state based on
6712 * comparison of cached test expression value to case label.
6714 ir_rvalue
*const label_rval
= this->test_value
->hir(instructions
, state
);
6715 ir_constant
*label_const
=
6716 label_rval
->constant_expression_value(body
.mem_ctx
);
6719 YYLTYPE loc
= this->test_value
->get_location();
6721 _mesa_glsl_error(& loc
, state
,
6722 "switch statement case label must be a "
6723 "constant expression");
6725 /* Stuff a dummy value in to allow processing to continue. */
6726 label_const
= body
.constant(0);
6729 _mesa_hash_table_search(state
->switch_state
.labels_ht
,
6730 &label_const
->value
.u
[0]);
6733 const struct case_label
*const l
=
6734 (struct case_label
*) entry
->data
;
6735 const ast_expression
*const previous_label
= l
->ast
;
6736 YYLTYPE loc
= this->test_value
->get_location();
6738 _mesa_glsl_error(& loc
, state
, "duplicate case value");
6740 loc
= previous_label
->get_location();
6741 _mesa_glsl_error(& loc
, state
, "this is the previous case label");
6743 struct case_label
*l
= ralloc(state
->switch_state
.labels_ht
,
6746 l
->value
= label_const
->value
.u
[0];
6747 l
->after_default
= state
->switch_state
.previous_default
!= NULL
;
6748 l
->ast
= this->test_value
;
6750 _mesa_hash_table_insert(state
->switch_state
.labels_ht
,
6751 &label_const
->value
.u
[0],
6756 /* Create an r-value version of the ir_constant label here (after we may
6757 * have created a fake one in error cases) that can be passed to
6758 * apply_implicit_conversion below.
6760 ir_rvalue
*label
= label_const
;
6762 ir_rvalue
*deref_test_var
=
6763 new(body
.mem_ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
6766 * From GLSL 4.40 specification section 6.2 ("Selection"):
6768 * "The type of the init-expression value in a switch statement must
6769 * be a scalar int or uint. The type of the constant-expression value
6770 * in a case label also must be a scalar int or uint. When any pair
6771 * of these values is tested for "equal value" and the types do not
6772 * match, an implicit conversion will be done to convert the int to a
6773 * uint (see section 4.1.10 “Implicit Conversions”) before the compare
6776 if (label
->type
!= state
->switch_state
.test_var
->type
) {
6777 YYLTYPE loc
= this->test_value
->get_location();
6779 const glsl_type
*type_a
= label
->type
;
6780 const glsl_type
*type_b
= state
->switch_state
.test_var
->type
;
6782 /* Check if int->uint implicit conversion is supported. */
6783 bool integer_conversion_supported
=
6784 glsl_type::int_type
->can_implicitly_convert_to(glsl_type::uint_type
,
6787 if ((!type_a
->is_integer() || !type_b
->is_integer()) ||
6788 !integer_conversion_supported
) {
6789 _mesa_glsl_error(&loc
, state
, "type mismatch with switch "
6790 "init-expression and case label (%s != %s)",
6791 type_a
->name
, type_b
->name
);
6793 /* Conversion of the case label. */
6794 if (type_a
->base_type
== GLSL_TYPE_INT
) {
6795 if (!apply_implicit_conversion(glsl_type::uint_type
,
6797 _mesa_glsl_error(&loc
, state
, "implicit type conversion error");
6799 /* Conversion of the init-expression value. */
6800 if (!apply_implicit_conversion(glsl_type::uint_type
,
6801 deref_test_var
, state
))
6802 _mesa_glsl_error(&loc
, state
, "implicit type conversion error");
6806 /* If the implicit conversion was allowed, the types will already be
6807 * the same. If the implicit conversion wasn't allowed, smash the
6808 * type of the label anyway. This will prevent the expression
6809 * constructor (below) from failing an assertion.
6811 label
->type
= deref_test_var
->type
;
6814 body
.emit(assign(fallthru_var
,
6815 logic_or(fallthru_var
, equal(label
, deref_test_var
))));
6816 } else { /* default case */
6817 if (state
->switch_state
.previous_default
) {
6818 YYLTYPE loc
= this->get_location();
6819 _mesa_glsl_error(& loc
, state
,
6820 "multiple default labels in one switch");
6822 loc
= state
->switch_state
.previous_default
->get_location();
6823 _mesa_glsl_error(& loc
, state
, "this is the first default label");
6825 state
->switch_state
.previous_default
= this;
6827 /* Set fallthru condition on 'run_default' bool. */
6828 body
.emit(assign(fallthru_var
,
6829 logic_or(fallthru_var
,
6830 state
->switch_state
.run_default
)));
6833 /* Case statements do not have r-values. */
6838 ast_iteration_statement::condition_to_hir(exec_list
*instructions
,
6839 struct _mesa_glsl_parse_state
*state
)
6843 if (condition
!= NULL
) {
6844 ir_rvalue
*const cond
=
6845 condition
->hir(instructions
, state
);
6848 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
6849 YYLTYPE loc
= condition
->get_location();
6851 _mesa_glsl_error(& loc
, state
,
6852 "loop condition must be scalar boolean");
6854 /* As the first code in the loop body, generate a block that looks
6855 * like 'if (!condition) break;' as the loop termination condition.
6857 ir_rvalue
*const not_cond
=
6858 new(ctx
) ir_expression(ir_unop_logic_not
, cond
);
6860 ir_if
*const if_stmt
= new(ctx
) ir_if(not_cond
);
6862 ir_jump
*const break_stmt
=
6863 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
6865 if_stmt
->then_instructions
.push_tail(break_stmt
);
6866 instructions
->push_tail(if_stmt
);
6873 ast_iteration_statement::hir(exec_list
*instructions
,
6874 struct _mesa_glsl_parse_state
*state
)
6878 /* For-loops and while-loops start a new scope, but do-while loops do not.
6880 if (mode
!= ast_do_while
)
6881 state
->symbols
->push_scope();
6883 if (init_statement
!= NULL
)
6884 init_statement
->hir(instructions
, state
);
6886 ir_loop
*const stmt
= new(ctx
) ir_loop();
6887 instructions
->push_tail(stmt
);
6889 /* Track the current loop nesting. */
6890 ast_iteration_statement
*nesting_ast
= state
->loop_nesting_ast
;
6892 state
->loop_nesting_ast
= this;
6894 /* Likewise, indicate that following code is closest to a loop,
6895 * NOT closest to a switch.
6897 bool saved_is_switch_innermost
= state
->switch_state
.is_switch_innermost
;
6898 state
->switch_state
.is_switch_innermost
= false;
6900 if (mode
!= ast_do_while
)
6901 condition_to_hir(&stmt
->body_instructions
, state
);
6904 body
->hir(& stmt
->body_instructions
, state
);
6906 if (rest_expression
!= NULL
)
6907 rest_expression
->hir(& stmt
->body_instructions
, state
);
6909 if (mode
== ast_do_while
)
6910 condition_to_hir(&stmt
->body_instructions
, state
);
6912 if (mode
!= ast_do_while
)
6913 state
->symbols
->pop_scope();
6915 /* Restore previous nesting before returning. */
6916 state
->loop_nesting_ast
= nesting_ast
;
6917 state
->switch_state
.is_switch_innermost
= saved_is_switch_innermost
;
6919 /* Loops do not have r-values.
6926 * Determine if the given type is valid for establishing a default precision
6929 * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
6931 * "The precision statement
6933 * precision precision-qualifier type;
6935 * can be used to establish a default precision qualifier. The type field
6936 * can be either int or float or any of the sampler types, and the
6937 * precision-qualifier can be lowp, mediump, or highp."
6939 * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
6940 * qualifiers on sampler types, but this seems like an oversight (since the
6941 * intention of including these in GLSL 1.30 is to allow compatibility with ES
6942 * shaders). So we allow int, float, and all sampler types regardless of GLSL
6946 is_valid_default_precision_type(const struct glsl_type
*const type
)
6951 switch (type
->base_type
) {
6953 case GLSL_TYPE_FLOAT
:
6954 /* "int" and "float" are valid, but vectors and matrices are not. */
6955 return type
->vector_elements
== 1 && type
->matrix_columns
== 1;
6956 case GLSL_TYPE_SAMPLER
:
6957 case GLSL_TYPE_IMAGE
:
6958 case GLSL_TYPE_ATOMIC_UINT
:
6967 ast_type_specifier::hir(exec_list
*instructions
,
6968 struct _mesa_glsl_parse_state
*state
)
6970 if (this->default_precision
== ast_precision_none
&& this->structure
== NULL
)
6973 YYLTYPE loc
= this->get_location();
6975 /* If this is a precision statement, check that the type to which it is
6976 * applied is either float or int.
6978 * From section 4.5.3 of the GLSL 1.30 spec:
6979 * "The precision statement
6980 * precision precision-qualifier type;
6981 * can be used to establish a default precision qualifier. The type
6982 * field can be either int or float [...]. Any other types or
6983 * qualifiers will result in an error.
6985 if (this->default_precision
!= ast_precision_none
) {
6986 if (!state
->check_precision_qualifiers_allowed(&loc
))
6989 if (this->structure
!= NULL
) {
6990 _mesa_glsl_error(&loc
, state
,
6991 "precision qualifiers do not apply to structures");
6995 if (this->array_specifier
!= NULL
) {
6996 _mesa_glsl_error(&loc
, state
,
6997 "default precision statements do not apply to "
7002 const struct glsl_type
*const type
=
7003 state
->symbols
->get_type(this->type_name
);
7004 if (!is_valid_default_precision_type(type
)) {
7005 _mesa_glsl_error(&loc
, state
,
7006 "default precision statements apply only to "
7007 "float, int, and opaque types");
7011 if (state
->es_shader
) {
7012 /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
7015 * "Non-precision qualified declarations will use the precision
7016 * qualifier specified in the most recent precision statement
7017 * that is still in scope. The precision statement has the same
7018 * scoping rules as variable declarations. If it is declared
7019 * inside a compound statement, its effect stops at the end of
7020 * the innermost statement it was declared in. Precision
7021 * statements in nested scopes override precision statements in
7022 * outer scopes. Multiple precision statements for the same basic
7023 * type can appear inside the same scope, with later statements
7024 * overriding earlier statements within that scope."
7026 * Default precision specifications follow the same scope rules as
7027 * variables. So, we can track the state of the default precision
7028 * qualifiers in the symbol table, and the rules will just work. This
7029 * is a slight abuse of the symbol table, but it has the semantics
7032 state
->symbols
->add_default_precision_qualifier(this->type_name
,
7033 this->default_precision
);
7036 /* FINISHME: Translate precision statements into IR. */
7040 /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
7041 * process_record_constructor() can do type-checking on C-style initializer
7042 * expressions of structs, but ast_struct_specifier should only be translated
7043 * to HIR if it is declaring the type of a structure.
7045 * The ->is_declaration field is false for initializers of variables
7046 * declared separately from the struct's type definition.
7048 * struct S { ... }; (is_declaration = true)
7049 * struct T { ... } t = { ... }; (is_declaration = true)
7050 * S s = { ... }; (is_declaration = false)
7052 if (this->structure
!= NULL
&& this->structure
->is_declaration
)
7053 return this->structure
->hir(instructions
, state
);
7060 * Process a structure or interface block tree into an array of structure fields
7062 * After parsing, where there are some syntax differnces, structures and
7063 * interface blocks are almost identical. They are similar enough that the
7064 * AST for each can be processed the same way into a set of
7065 * \c glsl_struct_field to describe the members.
7067 * If we're processing an interface block, var_mode should be the type of the
7068 * interface block (ir_var_shader_in, ir_var_shader_out, ir_var_uniform or
7069 * ir_var_shader_storage). If we're processing a structure, var_mode should be
7073 * The number of fields processed. A pointer to the array structure fields is
7074 * stored in \c *fields_ret.
7077 ast_process_struct_or_iface_block_members(exec_list
*instructions
,
7078 struct _mesa_glsl_parse_state
*state
,
7079 exec_list
*declarations
,
7080 glsl_struct_field
**fields_ret
,
7082 enum glsl_matrix_layout matrix_layout
,
7083 bool allow_reserved_names
,
7084 ir_variable_mode var_mode
,
7085 ast_type_qualifier
*layout
,
7086 unsigned block_stream
,
7087 unsigned block_xfb_buffer
,
7088 unsigned block_xfb_offset
,
7089 unsigned expl_location
,
7090 unsigned expl_align
)
7092 unsigned decl_count
= 0;
7093 unsigned next_offset
= 0;
7095 /* Make an initial pass over the list of fields to determine how
7096 * many there are. Each element in this list is an ast_declarator_list.
7097 * This means that we actually need to count the number of elements in the
7098 * 'declarations' list in each of the elements.
7100 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
7101 decl_count
+= decl_list
->declarations
.length();
7104 /* Allocate storage for the fields and process the field
7105 * declarations. As the declarations are processed, try to also convert
7106 * the types to HIR. This ensures that structure definitions embedded in
7107 * other structure definitions or in interface blocks are processed.
7109 glsl_struct_field
*const fields
= rzalloc_array(state
, glsl_struct_field
,
7112 bool first_member
= true;
7113 bool first_member_has_explicit_location
= false;
7116 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
7117 const char *type_name
;
7118 YYLTYPE loc
= decl_list
->get_location();
7120 decl_list
->type
->specifier
->hir(instructions
, state
);
7122 /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
7124 * "Anonymous structures are not supported; so embedded structures
7125 * must have a declarator. A name given to an embedded struct is
7126 * scoped at the same level as the struct it is embedded in."
7128 * The same section of the GLSL 1.20 spec says:
7130 * "Anonymous structures are not supported. Embedded structures are
7133 * The GLSL ES 1.00 and 3.00 specs have similar langauge. So, we allow
7134 * embedded structures in 1.10 only.
7136 if (state
->language_version
!= 110 &&
7137 decl_list
->type
->specifier
->structure
!= NULL
)
7138 _mesa_glsl_error(&loc
, state
,
7139 "embedded structure declarations are not allowed");
7141 const glsl_type
*decl_type
=
7142 decl_list
->type
->glsl_type(& type_name
, state
);
7144 const struct ast_type_qualifier
*const qual
=
7145 &decl_list
->type
->qualifier
;
7147 /* From section 4.3.9 of the GLSL 4.40 spec:
7149 * "[In interface blocks] opaque types are not allowed."
7151 * It should be impossible for decl_type to be NULL here. Cases that
7152 * might naturally lead to decl_type being NULL, especially for the
7153 * is_interface case, will have resulted in compilation having
7154 * already halted due to a syntax error.
7159 /* From section 4.3.7 of the ARB_bindless_texture spec:
7161 * "(remove the following bullet from the last list on p. 39,
7162 * thereby permitting sampler types in interface blocks; image
7163 * types are also permitted in blocks by this extension)"
7165 * * sampler types are not allowed
7167 if (decl_type
->contains_atomic() ||
7168 (!state
->has_bindless() && decl_type
->contains_opaque())) {
7169 _mesa_glsl_error(&loc
, state
, "uniform/buffer in non-default "
7170 "interface block contains %s variable",
7171 state
->has_bindless() ? "atomic" : "opaque");
7174 if (decl_type
->contains_atomic()) {
7175 /* From section 4.1.7.3 of the GLSL 4.40 spec:
7177 * "Members of structures cannot be declared as atomic counter
7180 _mesa_glsl_error(&loc
, state
, "atomic counter in structure");
7183 if (!state
->has_bindless() && decl_type
->contains_image()) {
7184 /* FINISHME: Same problem as with atomic counters.
7185 * FINISHME: Request clarification from Khronos and add
7186 * FINISHME: spec quotation here.
7188 _mesa_glsl_error(&loc
, state
, "image in structure");
7192 if (qual
->flags
.q
.explicit_binding
) {
7193 _mesa_glsl_error(&loc
, state
,
7194 "binding layout qualifier cannot be applied "
7195 "to struct or interface block members");
7199 if (!first_member
) {
7200 if (!layout
->flags
.q
.explicit_location
&&
7201 ((first_member_has_explicit_location
&&
7202 !qual
->flags
.q
.explicit_location
) ||
7203 (!first_member_has_explicit_location
&&
7204 qual
->flags
.q
.explicit_location
))) {
7205 _mesa_glsl_error(&loc
, state
,
7206 "when block-level location layout qualifier "
7207 "is not supplied either all members must "
7208 "have a location layout qualifier or all "
7209 "members must not have a location layout "
7213 first_member
= false;
7214 first_member_has_explicit_location
=
7215 qual
->flags
.q
.explicit_location
;
7219 if (qual
->flags
.q
.std140
||
7220 qual
->flags
.q
.std430
||
7221 qual
->flags
.q
.packed
||
7222 qual
->flags
.q
.shared
) {
7223 _mesa_glsl_error(&loc
, state
,
7224 "uniform/shader storage block layout qualifiers "
7225 "std140, std430, packed, and shared can only be "
7226 "applied to uniform/shader storage blocks, not "
7230 if (qual
->flags
.q
.constant
) {
7231 _mesa_glsl_error(&loc
, state
,
7232 "const storage qualifier cannot be applied "
7233 "to struct or interface block members");
7236 validate_memory_qualifier_for_type(state
, &loc
, qual
, decl_type
);
7237 validate_image_format_qualifier_for_type(state
, &loc
, qual
, decl_type
);
7239 /* From Section 4.4.2.3 (Geometry Outputs) of the GLSL 4.50 spec:
7241 * "A block member may be declared with a stream identifier, but
7242 * the specified stream must match the stream associated with the
7243 * containing block."
7245 if (qual
->flags
.q
.explicit_stream
) {
7246 unsigned qual_stream
;
7247 if (process_qualifier_constant(state
, &loc
, "stream",
7248 qual
->stream
, &qual_stream
) &&
7249 qual_stream
!= block_stream
) {
7250 _mesa_glsl_error(&loc
, state
, "stream layout qualifier on "
7251 "interface block member does not match "
7252 "the interface block (%u vs %u)", qual_stream
,
7258 unsigned explicit_xfb_buffer
= 0;
7259 if (qual
->flags
.q
.explicit_xfb_buffer
) {
7260 unsigned qual_xfb_buffer
;
7261 if (process_qualifier_constant(state
, &loc
, "xfb_buffer",
7262 qual
->xfb_buffer
, &qual_xfb_buffer
)) {
7263 explicit_xfb_buffer
= 1;
7264 if (qual_xfb_buffer
!= block_xfb_buffer
)
7265 _mesa_glsl_error(&loc
, state
, "xfb_buffer layout qualifier on "
7266 "interface block member does not match "
7267 "the interface block (%u vs %u)",
7268 qual_xfb_buffer
, block_xfb_buffer
);
7270 xfb_buffer
= (int) qual_xfb_buffer
;
7273 explicit_xfb_buffer
= layout
->flags
.q
.explicit_xfb_buffer
;
7274 xfb_buffer
= (int) block_xfb_buffer
;
7277 int xfb_stride
= -1;
7278 if (qual
->flags
.q
.explicit_xfb_stride
) {
7279 unsigned qual_xfb_stride
;
7280 if (process_qualifier_constant(state
, &loc
, "xfb_stride",
7281 qual
->xfb_stride
, &qual_xfb_stride
)) {
7282 xfb_stride
= (int) qual_xfb_stride
;
7286 if (qual
->flags
.q
.uniform
&& qual
->has_interpolation()) {
7287 _mesa_glsl_error(&loc
, state
,
7288 "interpolation qualifiers cannot be used "
7289 "with uniform interface blocks");
7292 if ((qual
->flags
.q
.uniform
|| !is_interface
) &&
7293 qual
->has_auxiliary_storage()) {
7294 _mesa_glsl_error(&loc
, state
,
7295 "auxiliary storage qualifiers cannot be used "
7296 "in uniform blocks or structures.");
7299 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
7300 if (!qual
->flags
.q
.uniform
&& !qual
->flags
.q
.buffer
) {
7301 _mesa_glsl_error(&loc
, state
,
7302 "row_major and column_major can only be "
7303 "applied to interface blocks");
7305 validate_matrix_layout_for_type(state
, &loc
, decl_type
, NULL
);
7308 foreach_list_typed (ast_declaration
, decl
, link
,
7309 &decl_list
->declarations
) {
7310 YYLTYPE loc
= decl
->get_location();
7312 if (!allow_reserved_names
)
7313 validate_identifier(decl
->identifier
, loc
, state
);
7315 const struct glsl_type
*field_type
=
7316 process_array_type(&loc
, decl_type
, decl
->array_specifier
, state
);
7317 validate_array_dimensions(field_type
, state
, &loc
);
7318 fields
[i
].type
= field_type
;
7319 fields
[i
].name
= decl
->identifier
;
7320 fields
[i
].interpolation
=
7321 interpret_interpolation_qualifier(qual
, field_type
,
7322 var_mode
, state
, &loc
);
7323 fields
[i
].centroid
= qual
->flags
.q
.centroid
? 1 : 0;
7324 fields
[i
].sample
= qual
->flags
.q
.sample
? 1 : 0;
7325 fields
[i
].patch
= qual
->flags
.q
.patch
? 1 : 0;
7326 fields
[i
].precision
= qual
->precision
;
7327 fields
[i
].offset
= -1;
7328 fields
[i
].explicit_xfb_buffer
= explicit_xfb_buffer
;
7329 fields
[i
].xfb_buffer
= xfb_buffer
;
7330 fields
[i
].xfb_stride
= xfb_stride
;
7332 if (qual
->flags
.q
.explicit_location
) {
7333 unsigned qual_location
;
7334 if (process_qualifier_constant(state
, &loc
, "location",
7335 qual
->location
, &qual_location
)) {
7336 fields
[i
].location
= qual_location
+
7337 (fields
[i
].patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
);
7338 expl_location
= fields
[i
].location
+
7339 fields
[i
].type
->count_attribute_slots(false);
7342 if (layout
&& layout
->flags
.q
.explicit_location
) {
7343 fields
[i
].location
= expl_location
;
7344 expl_location
+= fields
[i
].type
->count_attribute_slots(false);
7346 fields
[i
].location
= -1;
7350 /* Offset can only be used with std430 and std140 layouts an initial
7351 * value of 0 is used for error detection.
7357 if (qual
->flags
.q
.row_major
||
7358 matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
7364 if(layout
->flags
.q
.std140
) {
7365 align
= field_type
->std140_base_alignment(row_major
);
7366 size
= field_type
->std140_size(row_major
);
7367 } else if (layout
->flags
.q
.std430
) {
7368 align
= field_type
->std430_base_alignment(row_major
);
7369 size
= field_type
->std430_size(row_major
);
7373 if (qual
->flags
.q
.explicit_offset
) {
7374 unsigned qual_offset
;
7375 if (process_qualifier_constant(state
, &loc
, "offset",
7376 qual
->offset
, &qual_offset
)) {
7377 if (align
!= 0 && size
!= 0) {
7378 if (next_offset
> qual_offset
)
7379 _mesa_glsl_error(&loc
, state
, "layout qualifier "
7380 "offset overlaps previous member");
7382 if (qual_offset
% align
) {
7383 _mesa_glsl_error(&loc
, state
, "layout qualifier offset "
7384 "must be a multiple of the base "
7385 "alignment of %s", field_type
->name
);
7387 fields
[i
].offset
= qual_offset
;
7388 next_offset
= glsl_align(qual_offset
+ size
, align
);
7390 _mesa_glsl_error(&loc
, state
, "offset can only be used "
7391 "with std430 and std140 layouts");
7396 if (qual
->flags
.q
.explicit_align
|| expl_align
!= 0) {
7397 unsigned offset
= fields
[i
].offset
!= -1 ? fields
[i
].offset
:
7399 if (align
== 0 || size
== 0) {
7400 _mesa_glsl_error(&loc
, state
, "align can only be used with "
7401 "std430 and std140 layouts");
7402 } else if (qual
->flags
.q
.explicit_align
) {
7403 unsigned member_align
;
7404 if (process_qualifier_constant(state
, &loc
, "align",
7405 qual
->align
, &member_align
)) {
7406 if (member_align
== 0 ||
7407 member_align
& (member_align
- 1)) {
7408 _mesa_glsl_error(&loc
, state
, "align layout qualifier "
7409 "in not a power of 2");
7411 fields
[i
].offset
= glsl_align(offset
, member_align
);
7412 next_offset
= glsl_align(fields
[i
].offset
+ size
, align
);
7416 fields
[i
].offset
= glsl_align(offset
, expl_align
);
7417 next_offset
= glsl_align(fields
[i
].offset
+ size
, align
);
7419 } else if (!qual
->flags
.q
.explicit_offset
) {
7420 if (align
!= 0 && size
!= 0)
7421 next_offset
= glsl_align(next_offset
+ size
, align
);
7424 /* From the ARB_enhanced_layouts spec:
7426 * "The given offset applies to the first component of the first
7427 * member of the qualified entity. Then, within the qualified
7428 * entity, subsequent components are each assigned, in order, to
7429 * the next available offset aligned to a multiple of that
7430 * component's size. Aggregate types are flattened down to the
7431 * component level to get this sequence of components."
7433 if (qual
->flags
.q
.explicit_xfb_offset
) {
7434 unsigned xfb_offset
;
7435 if (process_qualifier_constant(state
, &loc
, "xfb_offset",
7436 qual
->offset
, &xfb_offset
)) {
7437 fields
[i
].offset
= xfb_offset
;
7438 block_xfb_offset
= fields
[i
].offset
+
7439 4 * field_type
->component_slots();
7442 if (layout
&& layout
->flags
.q
.explicit_xfb_offset
) {
7443 unsigned align
= field_type
->is_64bit() ? 8 : 4;
7444 fields
[i
].offset
= glsl_align(block_xfb_offset
, align
);
7445 block_xfb_offset
+= 4 * field_type
->component_slots();
7449 /* Propogate row- / column-major information down the fields of the
7450 * structure or interface block. Structures need this data because
7451 * the structure may contain a structure that contains ... a matrix
7452 * that need the proper layout.
7454 if (is_interface
&& layout
&&
7455 (layout
->flags
.q
.uniform
|| layout
->flags
.q
.buffer
) &&
7456 (field_type
->without_array()->is_matrix()
7457 || field_type
->without_array()->is_record())) {
7458 /* If no layout is specified for the field, inherit the layout
7461 fields
[i
].matrix_layout
= matrix_layout
;
7463 if (qual
->flags
.q
.row_major
)
7464 fields
[i
].matrix_layout
= GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
7465 else if (qual
->flags
.q
.column_major
)
7466 fields
[i
].matrix_layout
= GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
;
7468 /* If we're processing an uniform or buffer block, the matrix
7469 * layout must be decided by this point.
7471 assert(fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
7472 || fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
);
7475 /* Memory qualifiers are allowed on buffer and image variables, while
7476 * the format qualifier is only accepted for images.
7478 if (var_mode
== ir_var_shader_storage
||
7479 field_type
->without_array()->is_image()) {
7480 /* For readonly and writeonly qualifiers the field definition,
7481 * if set, overwrites the layout qualifier.
7483 if (qual
->flags
.q
.read_only
|| qual
->flags
.q
.write_only
) {
7484 fields
[i
].memory_read_only
= qual
->flags
.q
.read_only
;
7485 fields
[i
].memory_write_only
= qual
->flags
.q
.write_only
;
7487 fields
[i
].memory_read_only
=
7488 layout
? layout
->flags
.q
.read_only
: 0;
7489 fields
[i
].memory_write_only
=
7490 layout
? layout
->flags
.q
.write_only
: 0;
7493 /* For other qualifiers, we set the flag if either the layout
7494 * qualifier or the field qualifier are set
7496 fields
[i
].memory_coherent
= qual
->flags
.q
.coherent
||
7497 (layout
&& layout
->flags
.q
.coherent
);
7498 fields
[i
].memory_volatile
= qual
->flags
.q
._volatile
||
7499 (layout
&& layout
->flags
.q
._volatile
);
7500 fields
[i
].memory_restrict
= qual
->flags
.q
.restrict_flag
||
7501 (layout
&& layout
->flags
.q
.restrict_flag
);
7503 if (field_type
->without_array()->is_image()) {
7504 if (qual
->flags
.q
.explicit_image_format
) {
7505 if (qual
->image_base_type
!=
7506 field_type
->without_array()->sampled_type
) {
7507 _mesa_glsl_error(&loc
, state
, "format qualifier doesn't "
7508 "match the base data type of the image");
7511 fields
[i
].image_format
= qual
->image_format
;
7513 if (!qual
->flags
.q
.write_only
) {
7514 _mesa_glsl_error(&loc
, state
, "image not qualified with "
7515 "`writeonly' must have a format layout "
7519 fields
[i
].image_format
= GL_NONE
;
7528 assert(i
== decl_count
);
7530 *fields_ret
= fields
;
7536 ast_struct_specifier::hir(exec_list
*instructions
,
7537 struct _mesa_glsl_parse_state
*state
)
7539 YYLTYPE loc
= this->get_location();
7541 unsigned expl_location
= 0;
7542 if (layout
&& layout
->flags
.q
.explicit_location
) {
7543 if (!process_qualifier_constant(state
, &loc
, "location",
7544 layout
->location
, &expl_location
)) {
7547 expl_location
= VARYING_SLOT_VAR0
+ expl_location
;
7551 glsl_struct_field
*fields
;
7552 unsigned decl_count
=
7553 ast_process_struct_or_iface_block_members(instructions
,
7555 &this->declarations
,
7558 GLSL_MATRIX_LAYOUT_INHERITED
,
7559 false /* allow_reserved_names */,
7562 0, /* for interface only */
7563 0, /* for interface only */
7564 0, /* for interface only */
7566 0 /* for interface only */);
7568 validate_identifier(this->name
, loc
, state
);
7570 type
= glsl_type::get_record_instance(fields
, decl_count
, this->name
);
7572 if (!type
->is_anonymous() && !state
->symbols
->add_type(name
, type
)) {
7573 const glsl_type
*match
= state
->symbols
->get_type(name
);
7574 /* allow struct matching for desktop GL - older UE4 does this */
7575 if (match
!= NULL
&& state
->is_version(130, 0) && match
->record_compare(type
, false))
7576 _mesa_glsl_warning(& loc
, state
, "struct `%s' previously defined", name
);
7578 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
7580 const glsl_type
**s
= reralloc(state
, state
->user_structures
,
7582 state
->num_user_structures
+ 1);
7584 s
[state
->num_user_structures
] = type
;
7585 state
->user_structures
= s
;
7586 state
->num_user_structures
++;
7590 /* Structure type definitions do not have r-values.
7597 * Visitor class which detects whether a given interface block has been used.
7599 class interface_block_usage_visitor
: public ir_hierarchical_visitor
7602 interface_block_usage_visitor(ir_variable_mode mode
, const glsl_type
*block
)
7603 : mode(mode
), block(block
), found(false)
7607 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
7609 if (ir
->var
->data
.mode
== mode
&& ir
->var
->get_interface_type() == block
) {
7613 return visit_continue
;
7616 bool usage_found() const
7622 ir_variable_mode mode
;
7623 const glsl_type
*block
;
7628 is_unsized_array_last_element(ir_variable
*v
)
7630 const glsl_type
*interface_type
= v
->get_interface_type();
7631 int length
= interface_type
->length
;
7633 assert(v
->type
->is_unsized_array());
7635 /* Check if it is the last element of the interface */
7636 if (strcmp(interface_type
->fields
.structure
[length
-1].name
, v
->name
) == 0)
7642 apply_memory_qualifiers(ir_variable
*var
, glsl_struct_field field
)
7644 var
->data
.memory_read_only
= field
.memory_read_only
;
7645 var
->data
.memory_write_only
= field
.memory_write_only
;
7646 var
->data
.memory_coherent
= field
.memory_coherent
;
7647 var
->data
.memory_volatile
= field
.memory_volatile
;
7648 var
->data
.memory_restrict
= field
.memory_restrict
;
7652 ast_interface_block::hir(exec_list
*instructions
,
7653 struct _mesa_glsl_parse_state
*state
)
7655 YYLTYPE loc
= this->get_location();
7657 /* Interface blocks must be declared at global scope */
7658 if (state
->current_function
!= NULL
) {
7659 _mesa_glsl_error(&loc
, state
,
7660 "Interface block `%s' must be declared "
7665 /* Validate qualifiers:
7667 * - Layout Qualifiers as per the table in Section 4.4
7668 * ("Layout Qualifiers") of the GLSL 4.50 spec.
7670 * - Memory Qualifiers as per Section 4.10 ("Memory Qualifiers") of the
7673 * "Additionally, memory qualifiers may also be used in the declaration
7674 * of shader storage blocks"
7676 * Note the table in Section 4.4 says std430 is allowed on both uniform and
7677 * buffer blocks however Section 4.4.5 (Uniform and Shader Storage Block
7678 * Layout Qualifiers) of the GLSL 4.50 spec says:
7680 * "The std430 qualifier is supported only for shader storage blocks;
7681 * using std430 on a uniform block will result in a compile-time error."
7683 ast_type_qualifier allowed_blk_qualifiers
;
7684 allowed_blk_qualifiers
.flags
.i
= 0;
7685 if (this->layout
.flags
.q
.buffer
|| this->layout
.flags
.q
.uniform
) {
7686 allowed_blk_qualifiers
.flags
.q
.shared
= 1;
7687 allowed_blk_qualifiers
.flags
.q
.packed
= 1;
7688 allowed_blk_qualifiers
.flags
.q
.std140
= 1;
7689 allowed_blk_qualifiers
.flags
.q
.row_major
= 1;
7690 allowed_blk_qualifiers
.flags
.q
.column_major
= 1;
7691 allowed_blk_qualifiers
.flags
.q
.explicit_align
= 1;
7692 allowed_blk_qualifiers
.flags
.q
.explicit_binding
= 1;
7693 if (this->layout
.flags
.q
.buffer
) {
7694 allowed_blk_qualifiers
.flags
.q
.buffer
= 1;
7695 allowed_blk_qualifiers
.flags
.q
.std430
= 1;
7696 allowed_blk_qualifiers
.flags
.q
.coherent
= 1;
7697 allowed_blk_qualifiers
.flags
.q
._volatile
= 1;
7698 allowed_blk_qualifiers
.flags
.q
.restrict_flag
= 1;
7699 allowed_blk_qualifiers
.flags
.q
.read_only
= 1;
7700 allowed_blk_qualifiers
.flags
.q
.write_only
= 1;
7702 allowed_blk_qualifiers
.flags
.q
.uniform
= 1;
7705 /* Interface block */
7706 assert(this->layout
.flags
.q
.in
|| this->layout
.flags
.q
.out
);
7708 allowed_blk_qualifiers
.flags
.q
.explicit_location
= 1;
7709 if (this->layout
.flags
.q
.out
) {
7710 allowed_blk_qualifiers
.flags
.q
.out
= 1;
7711 if (state
->stage
== MESA_SHADER_GEOMETRY
||
7712 state
->stage
== MESA_SHADER_TESS_CTRL
||
7713 state
->stage
== MESA_SHADER_TESS_EVAL
||
7714 state
->stage
== MESA_SHADER_VERTEX
) {
7715 allowed_blk_qualifiers
.flags
.q
.explicit_xfb_offset
= 1;
7716 allowed_blk_qualifiers
.flags
.q
.explicit_xfb_buffer
= 1;
7717 allowed_blk_qualifiers
.flags
.q
.xfb_buffer
= 1;
7718 allowed_blk_qualifiers
.flags
.q
.explicit_xfb_stride
= 1;
7719 allowed_blk_qualifiers
.flags
.q
.xfb_stride
= 1;
7720 if (state
->stage
== MESA_SHADER_GEOMETRY
) {
7721 allowed_blk_qualifiers
.flags
.q
.stream
= 1;
7722 allowed_blk_qualifiers
.flags
.q
.explicit_stream
= 1;
7724 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
7725 allowed_blk_qualifiers
.flags
.q
.patch
= 1;
7729 allowed_blk_qualifiers
.flags
.q
.in
= 1;
7730 if (state
->stage
== MESA_SHADER_TESS_EVAL
) {
7731 allowed_blk_qualifiers
.flags
.q
.patch
= 1;
7736 this->layout
.validate_flags(&loc
, state
, allowed_blk_qualifiers
,
7737 "invalid qualifier for block",
7740 enum glsl_interface_packing packing
;
7741 if (this->layout
.flags
.q
.std140
) {
7742 packing
= GLSL_INTERFACE_PACKING_STD140
;
7743 } else if (this->layout
.flags
.q
.packed
) {
7744 packing
= GLSL_INTERFACE_PACKING_PACKED
;
7745 } else if (this->layout
.flags
.q
.std430
) {
7746 packing
= GLSL_INTERFACE_PACKING_STD430
;
7748 /* The default layout is shared.
7750 packing
= GLSL_INTERFACE_PACKING_SHARED
;
7753 ir_variable_mode var_mode
;
7754 const char *iface_type_name
;
7755 if (this->layout
.flags
.q
.in
) {
7756 var_mode
= ir_var_shader_in
;
7757 iface_type_name
= "in";
7758 } else if (this->layout
.flags
.q
.out
) {
7759 var_mode
= ir_var_shader_out
;
7760 iface_type_name
= "out";
7761 } else if (this->layout
.flags
.q
.uniform
) {
7762 var_mode
= ir_var_uniform
;
7763 iface_type_name
= "uniform";
7764 } else if (this->layout
.flags
.q
.buffer
) {
7765 var_mode
= ir_var_shader_storage
;
7766 iface_type_name
= "buffer";
7768 var_mode
= ir_var_auto
;
7769 iface_type_name
= "UNKNOWN";
7770 assert(!"interface block layout qualifier not found!");
7773 enum glsl_matrix_layout matrix_layout
= GLSL_MATRIX_LAYOUT_INHERITED
;
7774 if (this->layout
.flags
.q
.row_major
)
7775 matrix_layout
= GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
7776 else if (this->layout
.flags
.q
.column_major
)
7777 matrix_layout
= GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
;
7779 bool redeclaring_per_vertex
= strcmp(this->block_name
, "gl_PerVertex") == 0;
7780 exec_list declared_variables
;
7781 glsl_struct_field
*fields
;
7783 /* For blocks that accept memory qualifiers (i.e. shader storage), verify
7784 * that we don't have incompatible qualifiers
7786 if (this->layout
.flags
.q
.read_only
&& this->layout
.flags
.q
.write_only
) {
7787 _mesa_glsl_error(&loc
, state
,
7788 "Interface block sets both readonly and writeonly");
7791 unsigned qual_stream
;
7792 if (!process_qualifier_constant(state
, &loc
, "stream", this->layout
.stream
,
7794 !validate_stream_qualifier(&loc
, state
, qual_stream
)) {
7795 /* If the stream qualifier is invalid it doesn't make sense to continue
7796 * on and try to compare stream layouts on member variables against it
7797 * so just return early.
7802 unsigned qual_xfb_buffer
;
7803 if (!process_qualifier_constant(state
, &loc
, "xfb_buffer",
7804 layout
.xfb_buffer
, &qual_xfb_buffer
) ||
7805 !validate_xfb_buffer_qualifier(&loc
, state
, qual_xfb_buffer
)) {
7809 unsigned qual_xfb_offset
;
7810 if (layout
.flags
.q
.explicit_xfb_offset
) {
7811 if (!process_qualifier_constant(state
, &loc
, "xfb_offset",
7812 layout
.offset
, &qual_xfb_offset
)) {
7817 unsigned qual_xfb_stride
;
7818 if (layout
.flags
.q
.explicit_xfb_stride
) {
7819 if (!process_qualifier_constant(state
, &loc
, "xfb_stride",
7820 layout
.xfb_stride
, &qual_xfb_stride
)) {
7825 unsigned expl_location
= 0;
7826 if (layout
.flags
.q
.explicit_location
) {
7827 if (!process_qualifier_constant(state
, &loc
, "location",
7828 layout
.location
, &expl_location
)) {
7831 expl_location
+= this->layout
.flags
.q
.patch
? VARYING_SLOT_PATCH0
7832 : VARYING_SLOT_VAR0
;
7836 unsigned expl_align
= 0;
7837 if (layout
.flags
.q
.explicit_align
) {
7838 if (!process_qualifier_constant(state
, &loc
, "align",
7839 layout
.align
, &expl_align
)) {
7842 if (expl_align
== 0 || expl_align
& (expl_align
- 1)) {
7843 _mesa_glsl_error(&loc
, state
, "align layout qualifier is not a "
7850 unsigned int num_variables
=
7851 ast_process_struct_or_iface_block_members(&declared_variables
,
7853 &this->declarations
,
7857 redeclaring_per_vertex
,
7866 if (!redeclaring_per_vertex
) {
7867 validate_identifier(this->block_name
, loc
, state
);
7869 /* From section 4.3.9 ("Interface Blocks") of the GLSL 4.50 spec:
7871 * "Block names have no other use within a shader beyond interface
7872 * matching; it is a compile-time error to use a block name at global
7873 * scope for anything other than as a block name."
7875 ir_variable
*var
= state
->symbols
->get_variable(this->block_name
);
7876 if (var
&& !var
->type
->is_interface()) {
7877 _mesa_glsl_error(&loc
, state
, "Block name `%s' is "
7878 "already used in the scope.",
7883 const glsl_type
*earlier_per_vertex
= NULL
;
7884 if (redeclaring_per_vertex
) {
7885 /* Find the previous declaration of gl_PerVertex. If we're redeclaring
7886 * the named interface block gl_in, we can find it by looking at the
7887 * previous declaration of gl_in. Otherwise we can find it by looking
7888 * at the previous decalartion of any of the built-in outputs,
7891 * Also check that the instance name and array-ness of the redeclaration
7895 case ir_var_shader_in
:
7896 if (ir_variable
*earlier_gl_in
=
7897 state
->symbols
->get_variable("gl_in")) {
7898 earlier_per_vertex
= earlier_gl_in
->get_interface_type();
7900 _mesa_glsl_error(&loc
, state
,
7901 "redeclaration of gl_PerVertex input not allowed "
7903 _mesa_shader_stage_to_string(state
->stage
));
7905 if (this->instance_name
== NULL
||
7906 strcmp(this->instance_name
, "gl_in") != 0 || this->array_specifier
== NULL
||
7907 !this->array_specifier
->is_single_dimension()) {
7908 _mesa_glsl_error(&loc
, state
,
7909 "gl_PerVertex input must be redeclared as "
7913 case ir_var_shader_out
:
7914 if (ir_variable
*earlier_gl_Position
=
7915 state
->symbols
->get_variable("gl_Position")) {
7916 earlier_per_vertex
= earlier_gl_Position
->get_interface_type();
7917 } else if (ir_variable
*earlier_gl_out
=
7918 state
->symbols
->get_variable("gl_out")) {
7919 earlier_per_vertex
= earlier_gl_out
->get_interface_type();
7921 _mesa_glsl_error(&loc
, state
,
7922 "redeclaration of gl_PerVertex output not "
7923 "allowed in the %s shader",
7924 _mesa_shader_stage_to_string(state
->stage
));
7926 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
7927 if (this->instance_name
== NULL
||
7928 strcmp(this->instance_name
, "gl_out") != 0 || this->array_specifier
== NULL
) {
7929 _mesa_glsl_error(&loc
, state
,
7930 "gl_PerVertex output must be redeclared as "
7934 if (this->instance_name
!= NULL
) {
7935 _mesa_glsl_error(&loc
, state
,
7936 "gl_PerVertex output may not be redeclared with "
7937 "an instance name");
7942 _mesa_glsl_error(&loc
, state
,
7943 "gl_PerVertex must be declared as an input or an "
7948 if (earlier_per_vertex
== NULL
) {
7949 /* An error has already been reported. Bail out to avoid null
7950 * dereferences later in this function.
7955 /* Copy locations from the old gl_PerVertex interface block. */
7956 for (unsigned i
= 0; i
< num_variables
; i
++) {
7957 int j
= earlier_per_vertex
->field_index(fields
[i
].name
);
7959 _mesa_glsl_error(&loc
, state
,
7960 "redeclaration of gl_PerVertex must be a subset "
7961 "of the built-in members of gl_PerVertex");
7963 fields
[i
].location
=
7964 earlier_per_vertex
->fields
.structure
[j
].location
;
7966 earlier_per_vertex
->fields
.structure
[j
].offset
;
7967 fields
[i
].interpolation
=
7968 earlier_per_vertex
->fields
.structure
[j
].interpolation
;
7969 fields
[i
].centroid
=
7970 earlier_per_vertex
->fields
.structure
[j
].centroid
;
7972 earlier_per_vertex
->fields
.structure
[j
].sample
;
7974 earlier_per_vertex
->fields
.structure
[j
].patch
;
7975 fields
[i
].precision
=
7976 earlier_per_vertex
->fields
.structure
[j
].precision
;
7977 fields
[i
].explicit_xfb_buffer
=
7978 earlier_per_vertex
->fields
.structure
[j
].explicit_xfb_buffer
;
7979 fields
[i
].xfb_buffer
=
7980 earlier_per_vertex
->fields
.structure
[j
].xfb_buffer
;
7981 fields
[i
].xfb_stride
=
7982 earlier_per_vertex
->fields
.structure
[j
].xfb_stride
;
7986 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10
7989 * If a built-in interface block is redeclared, it must appear in
7990 * the shader before any use of any member included in the built-in
7991 * declaration, or a compilation error will result.
7993 * This appears to be a clarification to the behaviour established for
7994 * gl_PerVertex by GLSL 1.50, therefore we implement this behaviour
7995 * regardless of GLSL version.
7997 interface_block_usage_visitor
v(var_mode
, earlier_per_vertex
);
7998 v
.run(instructions
);
7999 if (v
.usage_found()) {
8000 _mesa_glsl_error(&loc
, state
,
8001 "redeclaration of a built-in interface block must "
8002 "appear before any use of any member of the "
8007 const glsl_type
*block_type
=
8008 glsl_type::get_interface_instance(fields
,
8012 GLSL_MATRIX_LAYOUT_ROW_MAJOR
,
8015 unsigned component_size
= block_type
->contains_double() ? 8 : 4;
8017 layout
.flags
.q
.explicit_xfb_offset
? (int) qual_xfb_offset
: -1;
8018 validate_xfb_offset_qualifier(&loc
, state
, xfb_offset
, block_type
,
8021 if (!state
->symbols
->add_interface(block_type
->name
, block_type
, var_mode
)) {
8022 YYLTYPE loc
= this->get_location();
8023 _mesa_glsl_error(&loc
, state
, "interface block `%s' with type `%s' "
8024 "already taken in the current scope",
8025 this->block_name
, iface_type_name
);
8028 /* Since interface blocks cannot contain statements, it should be
8029 * impossible for the block to generate any instructions.
8031 assert(declared_variables
.is_empty());
8033 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
8035 * Geometry shader input variables get the per-vertex values written
8036 * out by vertex shader output variables of the same names. Since a
8037 * geometry shader operates on a set of vertices, each input varying
8038 * variable (or input block, see interface blocks below) needs to be
8039 * declared as an array.
8041 if (state
->stage
== MESA_SHADER_GEOMETRY
&& this->array_specifier
== NULL
&&
8042 var_mode
== ir_var_shader_in
) {
8043 _mesa_glsl_error(&loc
, state
, "geometry shader inputs must be arrays");
8044 } else if ((state
->stage
== MESA_SHADER_TESS_CTRL
||
8045 state
->stage
== MESA_SHADER_TESS_EVAL
) &&
8046 !this->layout
.flags
.q
.patch
&&
8047 this->array_specifier
== NULL
&&
8048 var_mode
== ir_var_shader_in
) {
8049 _mesa_glsl_error(&loc
, state
, "per-vertex tessellation shader inputs must be arrays");
8050 } else if (state
->stage
== MESA_SHADER_TESS_CTRL
&&
8051 !this->layout
.flags
.q
.patch
&&
8052 this->array_specifier
== NULL
&&
8053 var_mode
== ir_var_shader_out
) {
8054 _mesa_glsl_error(&loc
, state
, "tessellation control shader outputs must be arrays");
8058 /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
8061 * "If an instance name (instance-name) is used, then it puts all the
8062 * members inside a scope within its own name space, accessed with the
8063 * field selector ( . ) operator (analogously to structures)."
8065 if (this->instance_name
) {
8066 if (redeclaring_per_vertex
) {
8067 /* When a built-in in an unnamed interface block is redeclared,
8068 * get_variable_being_redeclared() calls
8069 * check_builtin_array_max_size() to make sure that built-in array
8070 * variables aren't redeclared to illegal sizes. But we're looking
8071 * at a redeclaration of a named built-in interface block. So we
8072 * have to manually call check_builtin_array_max_size() for all parts
8073 * of the interface that are arrays.
8075 for (unsigned i
= 0; i
< num_variables
; i
++) {
8076 if (fields
[i
].type
->is_array()) {
8077 const unsigned size
= fields
[i
].type
->array_size();
8078 check_builtin_array_max_size(fields
[i
].name
, size
, loc
, state
);
8082 validate_identifier(this->instance_name
, loc
, state
);
8087 if (this->array_specifier
!= NULL
) {
8088 const glsl_type
*block_array_type
=
8089 process_array_type(&loc
, block_type
, this->array_specifier
, state
);
8091 /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
8093 * For uniform blocks declared an array, each individual array
8094 * element corresponds to a separate buffer object backing one
8095 * instance of the block. As the array size indicates the number
8096 * of buffer objects needed, uniform block array declarations
8097 * must specify an array size.
8099 * And a few paragraphs later:
8101 * Geometry shader input blocks must be declared as arrays and
8102 * follow the array declaration and linking rules for all
8103 * geometry shader inputs. All other input and output block
8104 * arrays must specify an array size.
8106 * The same applies to tessellation shaders.
8108 * The upshot of this is that the only circumstance where an
8109 * interface array size *doesn't* need to be specified is on a
8110 * geometry shader input, tessellation control shader input,
8111 * tessellation control shader output, and tessellation evaluation
8114 if (block_array_type
->is_unsized_array()) {
8115 bool allow_inputs
= state
->stage
== MESA_SHADER_GEOMETRY
||
8116 state
->stage
== MESA_SHADER_TESS_CTRL
||
8117 state
->stage
== MESA_SHADER_TESS_EVAL
;
8118 bool allow_outputs
= state
->stage
== MESA_SHADER_TESS_CTRL
;
8120 if (this->layout
.flags
.q
.in
) {
8122 _mesa_glsl_error(&loc
, state
,
8123 "unsized input block arrays not allowed in "
8125 _mesa_shader_stage_to_string(state
->stage
));
8126 } else if (this->layout
.flags
.q
.out
) {
8128 _mesa_glsl_error(&loc
, state
,
8129 "unsized output block arrays not allowed in "
8131 _mesa_shader_stage_to_string(state
->stage
));
8133 /* by elimination, this is a uniform block array */
8134 _mesa_glsl_error(&loc
, state
,
8135 "unsized uniform block arrays not allowed in "
8137 _mesa_shader_stage_to_string(state
->stage
));
8141 /* From section 4.3.9 (Interface Blocks) of the GLSL ES 3.10 spec:
8143 * * Arrays of arrays of blocks are not allowed
8145 if (state
->es_shader
&& block_array_type
->is_array() &&
8146 block_array_type
->fields
.array
->is_array()) {
8147 _mesa_glsl_error(&loc
, state
,
8148 "arrays of arrays interface blocks are "
8152 var
= new(state
) ir_variable(block_array_type
,
8153 this->instance_name
,
8156 var
= new(state
) ir_variable(block_type
,
8157 this->instance_name
,
8161 var
->data
.matrix_layout
= matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
8162 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
: matrix_layout
;
8164 if (var_mode
== ir_var_shader_in
|| var_mode
== ir_var_uniform
)
8165 var
->data
.read_only
= true;
8167 var
->data
.patch
= this->layout
.flags
.q
.patch
;
8169 if (state
->stage
== MESA_SHADER_GEOMETRY
&& var_mode
== ir_var_shader_in
)
8170 handle_geometry_shader_input_decl(state
, loc
, var
);
8171 else if ((state
->stage
== MESA_SHADER_TESS_CTRL
||
8172 state
->stage
== MESA_SHADER_TESS_EVAL
) && var_mode
== ir_var_shader_in
)
8173 handle_tess_shader_input_decl(state
, loc
, var
);
8174 else if (state
->stage
== MESA_SHADER_TESS_CTRL
&& var_mode
== ir_var_shader_out
)
8175 handle_tess_ctrl_shader_output_decl(state
, loc
, var
);
8177 for (unsigned i
= 0; i
< num_variables
; i
++) {
8178 if (var
->data
.mode
== ir_var_shader_storage
)
8179 apply_memory_qualifiers(var
, fields
[i
]);
8182 if (ir_variable
*earlier
=
8183 state
->symbols
->get_variable(this->instance_name
)) {
8184 if (!redeclaring_per_vertex
) {
8185 _mesa_glsl_error(&loc
, state
, "`%s' redeclared",
8186 this->instance_name
);
8188 earlier
->data
.how_declared
= ir_var_declared_normally
;
8189 earlier
->type
= var
->type
;
8190 earlier
->reinit_interface_type(block_type
);
8193 if (this->layout
.flags
.q
.explicit_binding
) {
8194 apply_explicit_binding(state
, &loc
, var
, var
->type
,
8198 var
->data
.stream
= qual_stream
;
8199 if (layout
.flags
.q
.explicit_location
) {
8200 var
->data
.location
= expl_location
;
8201 var
->data
.explicit_location
= true;
8204 state
->symbols
->add_variable(var
);
8205 instructions
->push_tail(var
);
8208 /* In order to have an array size, the block must also be declared with
8211 assert(this->array_specifier
== NULL
);
8213 for (unsigned i
= 0; i
< num_variables
; i
++) {
8215 new(state
) ir_variable(fields
[i
].type
,
8216 ralloc_strdup(state
, fields
[i
].name
),
8218 var
->data
.interpolation
= fields
[i
].interpolation
;
8219 var
->data
.centroid
= fields
[i
].centroid
;
8220 var
->data
.sample
= fields
[i
].sample
;
8221 var
->data
.patch
= fields
[i
].patch
;
8222 var
->data
.stream
= qual_stream
;
8223 var
->data
.location
= fields
[i
].location
;
8225 if (fields
[i
].location
!= -1)
8226 var
->data
.explicit_location
= true;
8228 var
->data
.explicit_xfb_buffer
= fields
[i
].explicit_xfb_buffer
;
8229 var
->data
.xfb_buffer
= fields
[i
].xfb_buffer
;
8231 if (fields
[i
].offset
!= -1)
8232 var
->data
.explicit_xfb_offset
= true;
8233 var
->data
.offset
= fields
[i
].offset
;
8235 var
->init_interface_type(block_type
);
8237 if (var_mode
== ir_var_shader_in
|| var_mode
== ir_var_uniform
)
8238 var
->data
.read_only
= true;
8240 /* Precision qualifiers do not have any meaning in Desktop GLSL */
8241 if (state
->es_shader
) {
8242 var
->data
.precision
=
8243 select_gles_precision(fields
[i
].precision
, fields
[i
].type
,
8247 if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
) {
8248 var
->data
.matrix_layout
= matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
8249 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
: matrix_layout
;
8251 var
->data
.matrix_layout
= fields
[i
].matrix_layout
;
8254 if (var
->data
.mode
== ir_var_shader_storage
)
8255 apply_memory_qualifiers(var
, fields
[i
]);
8257 /* Examine var name here since var may get deleted in the next call */
8258 bool var_is_gl_id
= is_gl_identifier(var
->name
);
8260 if (redeclaring_per_vertex
) {
8261 bool is_redeclaration
;
8263 get_variable_being_redeclared(&var
, loc
, state
,
8264 true /* allow_all_redeclarations */,
8266 if (!var_is_gl_id
|| !is_redeclaration
) {
8267 _mesa_glsl_error(&loc
, state
,
8268 "redeclaration of gl_PerVertex can only "
8269 "include built-in variables");
8270 } else if (var
->data
.how_declared
== ir_var_declared_normally
) {
8271 _mesa_glsl_error(&loc
, state
,
8272 "`%s' has already been redeclared",
8275 var
->data
.how_declared
= ir_var_declared_in_block
;
8276 var
->reinit_interface_type(block_type
);
8281 if (state
->symbols
->get_variable(var
->name
) != NULL
)
8282 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
8284 /* Propagate the "binding" keyword into this UBO/SSBO's fields.
8285 * The UBO declaration itself doesn't get an ir_variable unless it
8286 * has an instance name. This is ugly.
8288 if (this->layout
.flags
.q
.explicit_binding
) {
8289 apply_explicit_binding(state
, &loc
, var
,
8290 var
->get_interface_type(), &this->layout
);
8293 if (var
->type
->is_unsized_array()) {
8294 if (var
->is_in_shader_storage_block() &&
8295 is_unsized_array_last_element(var
)) {
8296 var
->data
.from_ssbo_unsized_array
= true;
8298 /* From GLSL ES 3.10 spec, section 4.1.9 "Arrays":
8300 * "If an array is declared as the last member of a shader storage
8301 * block and the size is not specified at compile-time, it is
8302 * sized at run-time. In all other cases, arrays are sized only
8305 * In desktop GLSL it is allowed to have unsized-arrays that are
8306 * not last, as long as we can determine that they are implicitly
8309 if (state
->es_shader
) {
8310 _mesa_glsl_error(&loc
, state
, "unsized array `%s' "
8311 "definition: only last member of a shader "
8312 "storage block can be defined as unsized "
8313 "array", fields
[i
].name
);
8318 state
->symbols
->add_variable(var
);
8319 instructions
->push_tail(var
);
8322 if (redeclaring_per_vertex
&& block_type
!= earlier_per_vertex
) {
8323 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec:
8325 * It is also a compilation error ... to redeclare a built-in
8326 * block and then use a member from that built-in block that was
8327 * not included in the redeclaration.
8329 * This appears to be a clarification to the behaviour established
8330 * for gl_PerVertex by GLSL 1.50, therefore we implement this
8331 * behaviour regardless of GLSL version.
8333 * To prevent the shader from using a member that was not included in
8334 * the redeclaration, we disable any ir_variables that are still
8335 * associated with the old declaration of gl_PerVertex (since we've
8336 * already updated all of the variables contained in the new
8337 * gl_PerVertex to point to it).
8339 * As a side effect this will prevent
8340 * validate_intrastage_interface_blocks() from getting confused and
8341 * thinking there are conflicting definitions of gl_PerVertex in the
8344 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
8345 ir_variable
*const var
= node
->as_variable();
8347 var
->get_interface_type() == earlier_per_vertex
&&
8348 var
->data
.mode
== var_mode
) {
8349 if (var
->data
.how_declared
== ir_var_declared_normally
) {
8350 _mesa_glsl_error(&loc
, state
,
8351 "redeclaration of gl_PerVertex cannot "
8352 "follow a redeclaration of `%s'",
8355 state
->symbols
->disable_variable(var
->name
);
8367 ast_tcs_output_layout::hir(exec_list
*instructions
,
8368 struct _mesa_glsl_parse_state
*state
)
8370 YYLTYPE loc
= this->get_location();
8372 unsigned num_vertices
;
8373 if (!state
->out_qualifier
->vertices
->
8374 process_qualifier_constant(state
, "vertices", &num_vertices
,
8376 /* return here to stop cascading incorrect error messages */
8380 /* If any shader outputs occurred before this declaration and specified an
8381 * array size, make sure the size they specified is consistent with the
8384 if (state
->tcs_output_size
!= 0 && state
->tcs_output_size
!= num_vertices
) {
8385 _mesa_glsl_error(&loc
, state
,
8386 "this tessellation control shader output layout "
8387 "specifies %u vertices, but a previous output "
8388 "is declared with size %u",
8389 num_vertices
, state
->tcs_output_size
);
8393 state
->tcs_output_vertices_specified
= true;
8395 /* If any shader outputs occurred before this declaration and did not
8396 * specify an array size, their size is determined now.
8398 foreach_in_list (ir_instruction
, node
, instructions
) {
8399 ir_variable
*var
= node
->as_variable();
8400 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_out
)
8403 /* Note: Not all tessellation control shader output are arrays. */
8404 if (!var
->type
->is_unsized_array() || var
->data
.patch
)
8407 if (var
->data
.max_array_access
>= (int)num_vertices
) {
8408 _mesa_glsl_error(&loc
, state
,
8409 "this tessellation control shader output layout "
8410 "specifies %u vertices, but an access to element "
8411 "%u of output `%s' already exists", num_vertices
,
8412 var
->data
.max_array_access
, var
->name
);
8414 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
8424 ast_gs_input_layout::hir(exec_list
*instructions
,
8425 struct _mesa_glsl_parse_state
*state
)
8427 YYLTYPE loc
= this->get_location();
8429 /* Should have been prevented by the parser. */
8430 assert(!state
->gs_input_prim_type_specified
8431 || state
->in_qualifier
->prim_type
== this->prim_type
);
8433 /* If any shader inputs occurred before this declaration and specified an
8434 * array size, make sure the size they specified is consistent with the
8437 unsigned num_vertices
= vertices_per_prim(this->prim_type
);
8438 if (state
->gs_input_size
!= 0 && state
->gs_input_size
!= num_vertices
) {
8439 _mesa_glsl_error(&loc
, state
,
8440 "this geometry shader input layout implies %u vertices"
8441 " per primitive, but a previous input is declared"
8442 " with size %u", num_vertices
, state
->gs_input_size
);
8446 state
->gs_input_prim_type_specified
= true;
8448 /* If any shader inputs occurred before this declaration and did not
8449 * specify an array size, their size is determined now.
8451 foreach_in_list(ir_instruction
, node
, instructions
) {
8452 ir_variable
*var
= node
->as_variable();
8453 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_in
)
8456 /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
8460 if (var
->type
->is_unsized_array()) {
8461 if (var
->data
.max_array_access
>= (int)num_vertices
) {
8462 _mesa_glsl_error(&loc
, state
,
8463 "this geometry shader input layout implies %u"
8464 " vertices, but an access to element %u of input"
8465 " `%s' already exists", num_vertices
,
8466 var
->data
.max_array_access
, var
->name
);
8468 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
8479 ast_cs_input_layout::hir(exec_list
*instructions
,
8480 struct _mesa_glsl_parse_state
*state
)
8482 YYLTYPE loc
= this->get_location();
8484 /* From the ARB_compute_shader specification:
8486 * If the local size of the shader in any dimension is greater
8487 * than the maximum size supported by the implementation for that
8488 * dimension, a compile-time error results.
8490 * It is not clear from the spec how the error should be reported if
8491 * the total size of the work group exceeds
8492 * MAX_COMPUTE_WORK_GROUP_INVOCATIONS, but it seems reasonable to
8493 * report it at compile time as well.
8495 GLuint64 total_invocations
= 1;
8496 unsigned qual_local_size
[3];
8497 for (int i
= 0; i
< 3; i
++) {
8499 char *local_size_str
= ralloc_asprintf(NULL
, "invalid local_size_%c",
8501 /* Infer a local_size of 1 for unspecified dimensions */
8502 if (this->local_size
[i
] == NULL
) {
8503 qual_local_size
[i
] = 1;
8504 } else if (!this->local_size
[i
]->
8505 process_qualifier_constant(state
, local_size_str
,
8506 &qual_local_size
[i
], false)) {
8507 ralloc_free(local_size_str
);
8510 ralloc_free(local_size_str
);
8512 if (qual_local_size
[i
] > state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]) {
8513 _mesa_glsl_error(&loc
, state
,
8514 "local_size_%c exceeds MAX_COMPUTE_WORK_GROUP_SIZE"
8516 state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]);
8519 total_invocations
*= qual_local_size
[i
];
8520 if (total_invocations
>
8521 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
) {
8522 _mesa_glsl_error(&loc
, state
,
8523 "product of local_sizes exceeds "
8524 "MAX_COMPUTE_WORK_GROUP_INVOCATIONS (%d)",
8525 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
);
8530 /* If any compute input layout declaration preceded this one, make sure it
8531 * was consistent with this one.
8533 if (state
->cs_input_local_size_specified
) {
8534 for (int i
= 0; i
< 3; i
++) {
8535 if (state
->cs_input_local_size
[i
] != qual_local_size
[i
]) {
8536 _mesa_glsl_error(&loc
, state
,
8537 "compute shader input layout does not match"
8538 " previous declaration");
8544 /* The ARB_compute_variable_group_size spec says:
8546 * If a compute shader including a *local_size_variable* qualifier also
8547 * declares a fixed local group size using the *local_size_x*,
8548 * *local_size_y*, or *local_size_z* qualifiers, a compile-time error
8551 if (state
->cs_input_local_size_variable_specified
) {
8552 _mesa_glsl_error(&loc
, state
,
8553 "compute shader can't include both a variable and a "
8554 "fixed local group size");
8558 state
->cs_input_local_size_specified
= true;
8559 for (int i
= 0; i
< 3; i
++)
8560 state
->cs_input_local_size
[i
] = qual_local_size
[i
];
8562 /* We may now declare the built-in constant gl_WorkGroupSize (see
8563 * builtin_variable_generator::generate_constants() for why we didn't
8564 * declare it earlier).
8566 ir_variable
*var
= new(state
->symbols
)
8567 ir_variable(glsl_type::uvec3_type
, "gl_WorkGroupSize", ir_var_auto
);
8568 var
->data
.how_declared
= ir_var_declared_implicitly
;
8569 var
->data
.read_only
= true;
8570 instructions
->push_tail(var
);
8571 state
->symbols
->add_variable(var
);
8572 ir_constant_data data
;
8573 memset(&data
, 0, sizeof(data
));
8574 for (int i
= 0; i
< 3; i
++)
8575 data
.u
[i
] = qual_local_size
[i
];
8576 var
->constant_value
= new(var
) ir_constant(glsl_type::uvec3_type
, &data
);
8577 var
->constant_initializer
=
8578 new(var
) ir_constant(glsl_type::uvec3_type
, &data
);
8579 var
->data
.has_initializer
= true;
8586 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
8587 exec_list
*instructions
)
8589 bool gl_FragColor_assigned
= false;
8590 bool gl_FragData_assigned
= false;
8591 bool gl_FragSecondaryColor_assigned
= false;
8592 bool gl_FragSecondaryData_assigned
= false;
8593 bool user_defined_fs_output_assigned
= false;
8594 ir_variable
*user_defined_fs_output
= NULL
;
8596 /* It would be nice to have proper location information. */
8598 memset(&loc
, 0, sizeof(loc
));
8600 foreach_in_list(ir_instruction
, node
, instructions
) {
8601 ir_variable
*var
= node
->as_variable();
8603 if (!var
|| !var
->data
.assigned
)
8606 if (strcmp(var
->name
, "gl_FragColor") == 0)
8607 gl_FragColor_assigned
= true;
8608 else if (strcmp(var
->name
, "gl_FragData") == 0)
8609 gl_FragData_assigned
= true;
8610 else if (strcmp(var
->name
, "gl_SecondaryFragColorEXT") == 0)
8611 gl_FragSecondaryColor_assigned
= true;
8612 else if (strcmp(var
->name
, "gl_SecondaryFragDataEXT") == 0)
8613 gl_FragSecondaryData_assigned
= true;
8614 else if (!is_gl_identifier(var
->name
)) {
8615 if (state
->stage
== MESA_SHADER_FRAGMENT
&&
8616 var
->data
.mode
== ir_var_shader_out
) {
8617 user_defined_fs_output_assigned
= true;
8618 user_defined_fs_output
= var
;
8623 /* From the GLSL 1.30 spec:
8625 * "If a shader statically assigns a value to gl_FragColor, it
8626 * may not assign a value to any element of gl_FragData. If a
8627 * shader statically writes a value to any element of
8628 * gl_FragData, it may not assign a value to
8629 * gl_FragColor. That is, a shader may assign values to either
8630 * gl_FragColor or gl_FragData, but not both. Multiple shaders
8631 * linked together must also consistently write just one of
8632 * these variables. Similarly, if user declared output
8633 * variables are in use (statically assigned to), then the
8634 * built-in variables gl_FragColor and gl_FragData may not be
8635 * assigned to. These incorrect usages all generate compile
8638 if (gl_FragColor_assigned
&& gl_FragData_assigned
) {
8639 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8640 "`gl_FragColor' and `gl_FragData'");
8641 } else if (gl_FragColor_assigned
&& user_defined_fs_output_assigned
) {
8642 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8643 "`gl_FragColor' and `%s'",
8644 user_defined_fs_output
->name
);
8645 } else if (gl_FragSecondaryColor_assigned
&& gl_FragSecondaryData_assigned
) {
8646 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8647 "`gl_FragSecondaryColorEXT' and"
8648 " `gl_FragSecondaryDataEXT'");
8649 } else if (gl_FragColor_assigned
&& gl_FragSecondaryData_assigned
) {
8650 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8651 "`gl_FragColor' and"
8652 " `gl_FragSecondaryDataEXT'");
8653 } else if (gl_FragData_assigned
&& gl_FragSecondaryColor_assigned
) {
8654 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8656 " `gl_FragSecondaryColorEXT'");
8657 } else if (gl_FragData_assigned
&& user_defined_fs_output_assigned
) {
8658 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8659 "`gl_FragData' and `%s'",
8660 user_defined_fs_output
->name
);
8663 if ((gl_FragSecondaryColor_assigned
|| gl_FragSecondaryData_assigned
) &&
8664 !state
->EXT_blend_func_extended_enable
) {
8665 _mesa_glsl_error(&loc
, state
,
8666 "Dual source blending requires EXT_blend_func_extended");
8672 remove_per_vertex_blocks(exec_list
*instructions
,
8673 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
)
8675 /* Find the gl_PerVertex interface block of the appropriate (in/out) mode,
8676 * if it exists in this shader type.
8678 const glsl_type
*per_vertex
= NULL
;
8680 case ir_var_shader_in
:
8681 if (ir_variable
*gl_in
= state
->symbols
->get_variable("gl_in"))
8682 per_vertex
= gl_in
->get_interface_type();
8684 case ir_var_shader_out
:
8685 if (ir_variable
*gl_Position
=
8686 state
->symbols
->get_variable("gl_Position")) {
8687 per_vertex
= gl_Position
->get_interface_type();
8691 assert(!"Unexpected mode");
8695 /* If we didn't find a built-in gl_PerVertex interface block, then we don't
8696 * need to do anything.
8698 if (per_vertex
== NULL
)
8701 /* If the interface block is used by the shader, then we don't need to do
8704 interface_block_usage_visitor
v(mode
, per_vertex
);
8705 v
.run(instructions
);
8706 if (v
.usage_found())
8709 /* Remove any ir_variable declarations that refer to the interface block
8712 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
8713 ir_variable
*const var
= node
->as_variable();
8714 if (var
!= NULL
&& var
->get_interface_type() == per_vertex
&&
8715 var
->data
.mode
== mode
) {
8716 state
->symbols
->disable_variable(var
->name
);