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 "glsl_types.h"
56 #include "program/hash_table.h"
57 #include "main/shaderobj.h"
59 #include "ir_builder.h"
61 using namespace ir_builder
;
64 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
65 exec_list
*instructions
);
67 remove_per_vertex_blocks(exec_list
*instructions
,
68 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
);
71 * Visitor class that finds the first instance of any write-only variable that
72 * is ever read, if any
74 class read_from_write_only_variable_visitor
: public ir_hierarchical_visitor
77 read_from_write_only_variable_visitor() : found(NULL
)
81 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
83 if (this->in_assignee
)
84 return visit_continue
;
86 ir_variable
*var
= ir
->variable_referenced();
87 /* We can have image_write_only set on both images and buffer variables,
88 * but in the former there is a distinction between reads from
89 * the variable itself (write_only) and from the memory they point to
90 * (image_write_only), while in the case of buffer variables there is
91 * no such distinction, that is why this check here is limited to
92 * buffer variables alone.
94 if (!var
|| var
->data
.mode
!= ir_var_shader_storage
)
95 return visit_continue
;
97 if (var
->data
.image_write_only
) {
102 return visit_continue
;
105 ir_variable
*get_variable() {
114 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
116 _mesa_glsl_initialize_variables(instructions
, state
);
118 state
->symbols
->separate_function_namespace
= state
->language_version
== 110;
120 state
->current_function
= NULL
;
122 state
->toplevel_ir
= instructions
;
124 state
->gs_input_prim_type_specified
= false;
125 state
->tcs_output_vertices_specified
= false;
126 state
->cs_input_local_size_specified
= false;
128 /* Section 4.2 of the GLSL 1.20 specification states:
129 * "The built-in functions are scoped in a scope outside the global scope
130 * users declare global variables in. That is, a shader's global scope,
131 * available for user-defined functions and global variables, is nested
132 * inside the scope containing the built-in functions."
134 * Since built-in functions like ftransform() access built-in variables,
135 * it follows that those must be in the outer scope as well.
137 * We push scope here to create this nesting effect...but don't pop.
138 * This way, a shader's globals are still in the symbol table for use
141 state
->symbols
->push_scope();
143 foreach_list_typed (ast_node
, ast
, link
, & state
->translation_unit
)
144 ast
->hir(instructions
, state
);
146 detect_recursion_unlinked(state
, instructions
);
147 detect_conflicting_assignments(state
, instructions
);
149 state
->toplevel_ir
= NULL
;
151 /* Move all of the variable declarations to the front of the IR list, and
152 * reverse the order. This has the (intended!) side effect that vertex
153 * shader inputs and fragment shader outputs will appear in the IR in the
154 * same order that they appeared in the shader code. This results in the
155 * locations being assigned in the declared order. Many (arguably buggy)
156 * applications depend on this behavior, and it matches what nearly all
159 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
160 ir_variable
*const var
= node
->as_variable();
166 instructions
->push_head(var
);
169 /* Figure out if gl_FragCoord is actually used in fragment shader */
170 ir_variable
*const var
= state
->symbols
->get_variable("gl_FragCoord");
172 state
->fs_uses_gl_fragcoord
= var
->data
.used
;
174 /* From section 7.1 (Built-In Language Variables) of the GLSL 4.10 spec:
176 * If multiple shaders using members of a built-in block belonging to
177 * the same interface are linked together in the same program, they
178 * must all redeclare the built-in block in the same way, as described
179 * in section 4.3.7 "Interface Blocks" for interface block matching, or
180 * a link error will result.
182 * The phrase "using members of a built-in block" implies that if two
183 * shaders are linked together and one of them *does not use* any members
184 * of the built-in block, then that shader does not need to have a matching
185 * redeclaration of the built-in block.
187 * This appears to be a clarification to the behaviour established for
188 * gl_PerVertex by GLSL 1.50, therefore implement it regardless of GLSL
191 * The definition of "interface" in section 4.3.7 that applies here is as
194 * The boundary between adjacent programmable pipeline stages: This
195 * spans all the outputs in all compilation units of the first stage
196 * and all the inputs in all compilation units of the second stage.
198 * Therefore this rule applies to both inter- and intra-stage linking.
200 * The easiest way to implement this is to check whether the shader uses
201 * gl_PerVertex right after ast-to-ir conversion, and if it doesn't, simply
202 * remove all the relevant variable declaration from the IR, so that the
203 * linker won't see them and complain about mismatches.
205 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_in
);
206 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_out
);
208 /* Check that we don't have reads from write-only variables */
209 read_from_write_only_variable_visitor v
;
211 ir_variable
*error_var
= v
.get_variable();
213 /* It would be nice to have proper location information, but for that
214 * we would need to check this as we process each kind of AST node
217 memset(&loc
, 0, sizeof(loc
));
218 _mesa_glsl_error(&loc
, state
, "Read from write-only variable `%s'",
224 static ir_expression_operation
225 get_conversion_operation(const glsl_type
*to
, const glsl_type
*from
,
226 struct _mesa_glsl_parse_state
*state
)
228 switch (to
->base_type
) {
229 case GLSL_TYPE_FLOAT
:
230 switch (from
->base_type
) {
231 case GLSL_TYPE_INT
: return ir_unop_i2f
;
232 case GLSL_TYPE_UINT
: return ir_unop_u2f
;
233 case GLSL_TYPE_DOUBLE
: return ir_unop_d2f
;
234 default: return (ir_expression_operation
)0;
238 if (!state
->is_version(400, 0) && !state
->ARB_gpu_shader5_enable
)
239 return (ir_expression_operation
)0;
240 switch (from
->base_type
) {
241 case GLSL_TYPE_INT
: return ir_unop_i2u
;
242 default: return (ir_expression_operation
)0;
245 case GLSL_TYPE_DOUBLE
:
246 if (!state
->has_double())
247 return (ir_expression_operation
)0;
248 switch (from
->base_type
) {
249 case GLSL_TYPE_INT
: return ir_unop_i2d
;
250 case GLSL_TYPE_UINT
: return ir_unop_u2d
;
251 case GLSL_TYPE_FLOAT
: return ir_unop_f2d
;
252 default: return (ir_expression_operation
)0;
255 default: return (ir_expression_operation
)0;
261 * If a conversion is available, convert one operand to a different type
263 * The \c from \c ir_rvalue is converted "in place".
265 * \param to Type that the operand it to be converted to
266 * \param from Operand that is being converted
267 * \param state GLSL compiler state
270 * If a conversion is possible (or unnecessary), \c true is returned.
271 * Otherwise \c false is returned.
274 apply_implicit_conversion(const glsl_type
*to
, ir_rvalue
* &from
,
275 struct _mesa_glsl_parse_state
*state
)
278 if (to
->base_type
== from
->type
->base_type
)
281 /* Prior to GLSL 1.20, there are no implicit conversions */
282 if (!state
->is_version(120, 0))
285 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
287 * "There are no implicit array or structure conversions. For
288 * example, an array of int cannot be implicitly converted to an
291 if (!to
->is_numeric() || !from
->type
->is_numeric())
294 /* We don't actually want the specific type `to`, we want a type
295 * with the same base type as `to`, but the same vector width as
298 to
= glsl_type::get_instance(to
->base_type
, from
->type
->vector_elements
,
299 from
->type
->matrix_columns
);
301 ir_expression_operation op
= get_conversion_operation(to
, from
->type
, state
);
303 from
= new(ctx
) ir_expression(op
, to
, from
, NULL
);
311 static const struct glsl_type
*
312 arithmetic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
314 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
316 const glsl_type
*type_a
= value_a
->type
;
317 const glsl_type
*type_b
= value_b
->type
;
319 /* From GLSL 1.50 spec, page 56:
321 * "The arithmetic binary operators add (+), subtract (-),
322 * multiply (*), and divide (/) operate on integer and
323 * floating-point scalars, vectors, and matrices."
325 if (!type_a
->is_numeric() || !type_b
->is_numeric()) {
326 _mesa_glsl_error(loc
, state
,
327 "operands to arithmetic operators must be numeric");
328 return glsl_type::error_type
;
332 /* "If one operand is floating-point based and the other is
333 * not, then the conversions from Section 4.1.10 "Implicit
334 * Conversions" are applied to the non-floating-point-based operand."
336 if (!apply_implicit_conversion(type_a
, value_b
, state
)
337 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
338 _mesa_glsl_error(loc
, state
,
339 "could not implicitly convert operands to "
340 "arithmetic operator");
341 return glsl_type::error_type
;
343 type_a
= value_a
->type
;
344 type_b
= value_b
->type
;
346 /* "If the operands are integer types, they must both be signed or
349 * From this rule and the preceeding conversion it can be inferred that
350 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
351 * The is_numeric check above already filtered out the case where either
352 * type is not one of these, so now the base types need only be tested for
355 if (type_a
->base_type
!= type_b
->base_type
) {
356 _mesa_glsl_error(loc
, state
,
357 "base type mismatch for arithmetic operator");
358 return glsl_type::error_type
;
361 /* "All arithmetic binary operators result in the same fundamental type
362 * (signed integer, unsigned integer, or floating-point) as the
363 * operands they operate on, after operand type conversion. After
364 * conversion, the following cases are valid
366 * * The two operands are scalars. In this case the operation is
367 * applied, resulting in a scalar."
369 if (type_a
->is_scalar() && type_b
->is_scalar())
372 /* "* One operand is a scalar, and the other is a vector or matrix.
373 * In this case, the scalar operation is applied independently to each
374 * component of the vector or matrix, resulting in the same size
377 if (type_a
->is_scalar()) {
378 if (!type_b
->is_scalar())
380 } else if (type_b
->is_scalar()) {
384 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
385 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
388 assert(!type_a
->is_scalar());
389 assert(!type_b
->is_scalar());
391 /* "* The two operands are vectors of the same size. In this case, the
392 * operation is done component-wise resulting in the same size
395 if (type_a
->is_vector() && type_b
->is_vector()) {
396 if (type_a
== type_b
) {
399 _mesa_glsl_error(loc
, state
,
400 "vector size mismatch for arithmetic operator");
401 return glsl_type::error_type
;
405 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
406 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
407 * <vector, vector> have been handled. At least one of the operands must
408 * be matrix. Further, since there are no integer matrix types, the base
409 * type of both operands must be float.
411 assert(type_a
->is_matrix() || type_b
->is_matrix());
412 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
||
413 type_a
->base_type
== GLSL_TYPE_DOUBLE
);
414 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
||
415 type_b
->base_type
== GLSL_TYPE_DOUBLE
);
417 /* "* The operator is add (+), subtract (-), or divide (/), and the
418 * operands are matrices with the same number of rows and the same
419 * number of columns. In this case, the operation is done component-
420 * wise resulting in the same size matrix."
421 * * The operator is multiply (*), where both operands are matrices or
422 * one operand is a vector and the other a matrix. A right vector
423 * operand is treated as a column vector and a left vector operand as a
424 * row vector. In all these cases, it is required that the number of
425 * columns of the left operand is equal to the number of rows of the
426 * right operand. Then, the multiply (*) operation does a linear
427 * algebraic multiply, yielding an object that has the same number of
428 * rows as the left operand and the same number of columns as the right
429 * operand. Section 5.10 "Vector and Matrix Operations" explains in
430 * more detail how vectors and matrices are operated on."
433 if (type_a
== type_b
)
436 const glsl_type
*type
= glsl_type::get_mul_type(type_a
, type_b
);
438 if (type
== glsl_type::error_type
) {
439 _mesa_glsl_error(loc
, state
,
440 "size mismatch for matrix multiplication");
447 /* "All other cases are illegal."
449 _mesa_glsl_error(loc
, state
, "type mismatch");
450 return glsl_type::error_type
;
454 static const struct glsl_type
*
455 unary_arithmetic_result_type(const struct glsl_type
*type
,
456 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
458 /* From GLSL 1.50 spec, page 57:
460 * "The arithmetic unary operators negate (-), post- and pre-increment
461 * and decrement (-- and ++) operate on integer or floating-point
462 * values (including vectors and matrices). All unary operators work
463 * component-wise on their operands. These result with the same type
466 if (!type
->is_numeric()) {
467 _mesa_glsl_error(loc
, state
,
468 "operands to arithmetic operators must be numeric");
469 return glsl_type::error_type
;
476 * \brief Return the result type of a bit-logic operation.
478 * If the given types to the bit-logic operator are invalid, return
479 * glsl_type::error_type.
481 * \param type_a Type of LHS of bit-logic op
482 * \param type_b Type of RHS of bit-logic op
484 static const struct glsl_type
*
485 bit_logic_result_type(const struct glsl_type
*type_a
,
486 const struct glsl_type
*type_b
,
488 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
490 if (!state
->check_bitwise_operations_allowed(loc
)) {
491 return glsl_type::error_type
;
494 /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
496 * "The bitwise operators and (&), exclusive-or (^), and inclusive-or
497 * (|). The operands must be of type signed or unsigned integers or
500 if (!type_a
->is_integer()) {
501 _mesa_glsl_error(loc
, state
, "LHS of `%s' must be an integer",
502 ast_expression::operator_string(op
));
503 return glsl_type::error_type
;
505 if (!type_b
->is_integer()) {
506 _mesa_glsl_error(loc
, state
, "RHS of `%s' must be an integer",
507 ast_expression::operator_string(op
));
508 return glsl_type::error_type
;
511 /* "The fundamental types of the operands (signed or unsigned) must
514 if (type_a
->base_type
!= type_b
->base_type
) {
515 _mesa_glsl_error(loc
, state
, "operands of `%s' must have the same "
516 "base type", ast_expression::operator_string(op
));
517 return glsl_type::error_type
;
520 /* "The operands cannot be vectors of differing size." */
521 if (type_a
->is_vector() &&
522 type_b
->is_vector() &&
523 type_a
->vector_elements
!= type_b
->vector_elements
) {
524 _mesa_glsl_error(loc
, state
, "operands of `%s' cannot be vectors of "
525 "different sizes", ast_expression::operator_string(op
));
526 return glsl_type::error_type
;
529 /* "If one operand is a scalar and the other a vector, the scalar is
530 * applied component-wise to the vector, resulting in the same type as
531 * the vector. The fundamental types of the operands [...] will be the
532 * resulting fundamental type."
534 if (type_a
->is_scalar())
540 static const struct glsl_type
*
541 modulus_result_type(const struct glsl_type
*type_a
,
542 const struct glsl_type
*type_b
,
543 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
545 if (!state
->check_version(130, 300, loc
, "operator '%%' is reserved")) {
546 return glsl_type::error_type
;
549 /* From GLSL 1.50 spec, page 56:
550 * "The operator modulus (%) operates on signed or unsigned integers or
551 * integer vectors. The operand types must both be signed or both be
554 if (!type_a
->is_integer()) {
555 _mesa_glsl_error(loc
, state
, "LHS of operator %% must be an integer");
556 return glsl_type::error_type
;
558 if (!type_b
->is_integer()) {
559 _mesa_glsl_error(loc
, state
, "RHS of operator %% must be an integer");
560 return glsl_type::error_type
;
562 if (type_a
->base_type
!= type_b
->base_type
) {
563 _mesa_glsl_error(loc
, state
,
564 "operands of %% must have the same base type");
565 return glsl_type::error_type
;
568 /* "The operands cannot be vectors of differing size. If one operand is
569 * a scalar and the other vector, then the scalar is applied component-
570 * wise to the vector, resulting in the same type as the vector. If both
571 * are vectors of the same size, the result is computed component-wise."
573 if (type_a
->is_vector()) {
574 if (!type_b
->is_vector()
575 || (type_a
->vector_elements
== type_b
->vector_elements
))
580 /* "The operator modulus (%) is not defined for any other data types
581 * (non-integer types)."
583 _mesa_glsl_error(loc
, state
, "type mismatch");
584 return glsl_type::error_type
;
588 static const struct glsl_type
*
589 relational_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
590 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
592 const glsl_type
*type_a
= value_a
->type
;
593 const glsl_type
*type_b
= value_b
->type
;
595 /* From GLSL 1.50 spec, page 56:
596 * "The relational operators greater than (>), less than (<), greater
597 * than or equal (>=), and less than or equal (<=) operate only on
598 * scalar integer and scalar floating-point expressions."
600 if (!type_a
->is_numeric()
601 || !type_b
->is_numeric()
602 || !type_a
->is_scalar()
603 || !type_b
->is_scalar()) {
604 _mesa_glsl_error(loc
, state
,
605 "operands to relational operators must be scalar and "
607 return glsl_type::error_type
;
610 /* "Either the operands' types must match, or the conversions from
611 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
612 * operand, after which the types must match."
614 if (!apply_implicit_conversion(type_a
, value_b
, state
)
615 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
616 _mesa_glsl_error(loc
, state
,
617 "could not implicitly convert operands to "
618 "relational operator");
619 return glsl_type::error_type
;
621 type_a
= value_a
->type
;
622 type_b
= value_b
->type
;
624 if (type_a
->base_type
!= type_b
->base_type
) {
625 _mesa_glsl_error(loc
, state
, "base type mismatch");
626 return glsl_type::error_type
;
629 /* "The result is scalar Boolean."
631 return glsl_type::bool_type
;
635 * \brief Return the result type of a bit-shift operation.
637 * If the given types to the bit-shift operator are invalid, return
638 * glsl_type::error_type.
640 * \param type_a Type of LHS of bit-shift op
641 * \param type_b Type of RHS of bit-shift op
643 static const struct glsl_type
*
644 shift_result_type(const struct glsl_type
*type_a
,
645 const struct glsl_type
*type_b
,
647 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
649 if (!state
->check_bitwise_operations_allowed(loc
)) {
650 return glsl_type::error_type
;
653 /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
655 * "The shift operators (<<) and (>>). For both operators, the operands
656 * must be signed or unsigned integers or integer vectors. One operand
657 * can be signed while the other is unsigned."
659 if (!type_a
->is_integer()) {
660 _mesa_glsl_error(loc
, state
, "LHS of operator %s must be an integer or "
661 "integer vector", ast_expression::operator_string(op
));
662 return glsl_type::error_type
;
665 if (!type_b
->is_integer()) {
666 _mesa_glsl_error(loc
, state
, "RHS of operator %s must be an integer or "
667 "integer vector", ast_expression::operator_string(op
));
668 return glsl_type::error_type
;
671 /* "If the first operand is a scalar, the second operand has to be
674 if (type_a
->is_scalar() && !type_b
->is_scalar()) {
675 _mesa_glsl_error(loc
, state
, "if the first operand of %s is scalar, the "
676 "second must be scalar as well",
677 ast_expression::operator_string(op
));
678 return glsl_type::error_type
;
681 /* If both operands are vectors, check that they have same number of
684 if (type_a
->is_vector() &&
685 type_b
->is_vector() &&
686 type_a
->vector_elements
!= type_b
->vector_elements
) {
687 _mesa_glsl_error(loc
, state
, "vector operands to operator %s must "
688 "have same number of elements",
689 ast_expression::operator_string(op
));
690 return glsl_type::error_type
;
693 /* "In all cases, the resulting type will be the same type as the left
700 * Returns the innermost array index expression in an rvalue tree.
701 * This is the largest indexing level -- if an array of blocks, then
702 * it is the block index rather than an indexing expression for an
703 * array-typed member of an array of blocks.
706 find_innermost_array_index(ir_rvalue
*rv
)
708 ir_dereference_array
*last
= NULL
;
710 if (rv
->as_dereference_array()) {
711 last
= rv
->as_dereference_array();
713 } else if (rv
->as_dereference_record())
714 rv
= rv
->as_dereference_record()->record
;
715 else if (rv
->as_swizzle())
716 rv
= rv
->as_swizzle()->val
;
722 return last
->array_index
;
728 * Validates that a value can be assigned to a location with a specified type
730 * Validates that \c rhs can be assigned to some location. If the types are
731 * not an exact match but an automatic conversion is possible, \c rhs will be
735 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
736 * Otherwise the actual RHS to be assigned will be returned. This may be
737 * \c rhs, or it may be \c rhs after some type conversion.
740 * In addition to being used for assignments, this function is used to
741 * type-check return values.
744 validate_assignment(struct _mesa_glsl_parse_state
*state
,
745 YYLTYPE loc
, ir_rvalue
*lhs
,
746 ir_rvalue
*rhs
, bool is_initializer
)
748 /* If there is already some error in the RHS, just return it. Anything
749 * else will lead to an avalanche of error message back to the user.
751 if (rhs
->type
->is_error())
754 /* In the Tessellation Control Shader:
755 * If a per-vertex output variable is used as an l-value, it is an error
756 * if the expression indicating the vertex number is not the identifier
759 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
760 ir_variable
*var
= lhs
->variable_referenced();
761 if (var
->data
.mode
== ir_var_shader_out
&& !var
->data
.patch
) {
762 ir_rvalue
*index
= find_innermost_array_index(lhs
);
763 ir_variable
*index_var
= index
? index
->variable_referenced() : NULL
;
764 if (!index_var
|| strcmp(index_var
->name
, "gl_InvocationID") != 0) {
765 _mesa_glsl_error(&loc
, state
,
766 "Tessellation control shader outputs can only "
767 "be indexed by gl_InvocationID");
773 /* If the types are identical, the assignment can trivially proceed.
775 if (rhs
->type
== lhs
->type
)
778 /* If the array element types are the same and the LHS is unsized,
779 * the assignment is okay for initializers embedded in variable
782 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
783 * is handled by ir_dereference::is_lvalue.
785 if (lhs
->type
->is_unsized_array() && rhs
->type
->is_array()
786 && (lhs
->type
->fields
.array
== rhs
->type
->fields
.array
)) {
787 if (is_initializer
) {
790 _mesa_glsl_error(&loc
, state
,
791 "implicitly sized arrays cannot be assigned");
796 /* Check for implicit conversion in GLSL 1.20 */
797 if (apply_implicit_conversion(lhs
->type
, rhs
, state
)) {
798 if (rhs
->type
== lhs
->type
)
802 _mesa_glsl_error(&loc
, state
,
803 "%s of type %s cannot be assigned to "
804 "variable of type %s",
805 is_initializer
? "initializer" : "value",
806 rhs
->type
->name
, lhs
->type
->name
);
812 mark_whole_array_access(ir_rvalue
*access
)
814 ir_dereference_variable
*deref
= access
->as_dereference_variable();
816 if (deref
&& deref
->var
) {
817 deref
->var
->data
.max_array_access
= deref
->type
->length
- 1;
822 do_assignment(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
823 const char *non_lvalue_description
,
824 ir_rvalue
*lhs
, ir_rvalue
*rhs
,
825 ir_rvalue
**out_rvalue
, bool needs_rvalue
,
830 bool error_emitted
= (lhs
->type
->is_error() || rhs
->type
->is_error());
831 ir_rvalue
*extract_channel
= NULL
;
833 /* If the assignment LHS comes back as an ir_binop_vector_extract
834 * expression, move it to the RHS as an ir_triop_vector_insert.
836 if (lhs
->ir_type
== ir_type_expression
) {
837 ir_expression
*const lhs_expr
= lhs
->as_expression();
839 if (unlikely(lhs_expr
->operation
== ir_binop_vector_extract
)) {
841 validate_assignment(state
, lhs_loc
, lhs
,
842 rhs
, is_initializer
);
844 if (new_rhs
== NULL
) {
848 * - LHS: (expression float vector_extract <vec> <channel>)
852 * - RHS: (expression vec2 vector_insert <vec> <channel> <scalar>)
854 * The LHS type is now a vector instead of a scalar. Since GLSL
855 * allows assignments to be used as rvalues, we need to re-extract
856 * the channel from assignment_temp when returning the rvalue.
858 extract_channel
= lhs_expr
->operands
[1];
859 rhs
= new(ctx
) ir_expression(ir_triop_vector_insert
,
860 lhs_expr
->operands
[0]->type
,
861 lhs_expr
->operands
[0],
864 lhs
= lhs_expr
->operands
[0]->clone(ctx
, NULL
);
869 ir_variable
*lhs_var
= lhs
->variable_referenced();
871 lhs_var
->data
.assigned
= true;
873 if (!error_emitted
) {
874 if (non_lvalue_description
!= NULL
) {
875 _mesa_glsl_error(&lhs_loc
, state
,
877 non_lvalue_description
);
878 error_emitted
= true;
879 } else if (lhs_var
!= NULL
&& (lhs_var
->data
.read_only
||
880 (lhs_var
->data
.mode
== ir_var_shader_storage
&&
881 lhs_var
->data
.image_read_only
))) {
882 /* We can have image_read_only set on both images and buffer variables,
883 * but in the former there is a distinction between assignments to
884 * the variable itself (read_only) and to the memory they point to
885 * (image_read_only), while in the case of buffer variables there is
886 * no such distinction, that is why this check here is limited to
887 * buffer variables alone.
889 _mesa_glsl_error(&lhs_loc
, state
,
890 "assignment to read-only variable '%s'",
892 error_emitted
= true;
893 } else if (lhs
->type
->is_array() &&
894 !state
->check_version(120, 300, &lhs_loc
,
895 "whole array assignment forbidden")) {
896 /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
898 * "Other binary or unary expressions, non-dereferenced
899 * arrays, function names, swizzles with repeated fields,
900 * and constants cannot be l-values."
902 * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00.
904 error_emitted
= true;
905 } else if (!lhs
->is_lvalue()) {
906 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
907 error_emitted
= true;
912 validate_assignment(state
, lhs_loc
, lhs
, rhs
, is_initializer
);
913 if (new_rhs
!= NULL
) {
916 /* If the LHS array was not declared with a size, it takes it size from
917 * the RHS. If the LHS is an l-value and a whole array, it must be a
918 * dereference of a variable. Any other case would require that the LHS
919 * is either not an l-value or not a whole array.
921 if (lhs
->type
->is_unsized_array()) {
922 ir_dereference
*const d
= lhs
->as_dereference();
926 ir_variable
*const var
= d
->variable_referenced();
930 if (var
->data
.max_array_access
>= unsigned(rhs
->type
->array_size())) {
931 /* FINISHME: This should actually log the location of the RHS. */
932 _mesa_glsl_error(& lhs_loc
, state
, "array size must be > %u due to "
934 var
->data
.max_array_access
);
937 var
->type
= glsl_type::get_array_instance(lhs
->type
->fields
.array
,
938 rhs
->type
->array_size());
941 if (lhs
->type
->is_array()) {
942 mark_whole_array_access(rhs
);
943 mark_whole_array_access(lhs
);
947 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
948 * but not post_inc) need the converted assigned value as an rvalue
949 * to handle things like:
954 ir_variable
*var
= new(ctx
) ir_variable(rhs
->type
, "assignment_tmp",
956 instructions
->push_tail(var
);
957 instructions
->push_tail(assign(var
, rhs
));
959 if (!error_emitted
) {
960 ir_dereference_variable
*deref_var
= new(ctx
) ir_dereference_variable(var
);
961 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, deref_var
));
963 ir_rvalue
*rvalue
= new(ctx
) ir_dereference_variable(var
);
965 if (extract_channel
) {
966 rvalue
= new(ctx
) ir_expression(ir_binop_vector_extract
,
968 extract_channel
->clone(ctx
, NULL
));
971 *out_rvalue
= rvalue
;
974 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, rhs
));
978 return error_emitted
;
982 get_lvalue_copy(exec_list
*instructions
, ir_rvalue
*lvalue
)
984 void *ctx
= ralloc_parent(lvalue
);
987 var
= new(ctx
) ir_variable(lvalue
->type
, "_post_incdec_tmp",
989 instructions
->push_tail(var
);
991 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
994 return new(ctx
) ir_dereference_variable(var
);
999 ast_node::hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
1001 (void) instructions
;
1008 ast_function_expression::hir_no_rvalue(exec_list
*instructions
,
1009 struct _mesa_glsl_parse_state
*state
)
1011 (void)hir(instructions
, state
);
1015 ast_aggregate_initializer::hir_no_rvalue(exec_list
*instructions
,
1016 struct _mesa_glsl_parse_state
*state
)
1018 (void)hir(instructions
, state
);
1022 do_comparison(void *mem_ctx
, int operation
, ir_rvalue
*op0
, ir_rvalue
*op1
)
1025 ir_rvalue
*cmp
= NULL
;
1027 if (operation
== ir_binop_all_equal
)
1028 join_op
= ir_binop_logic_and
;
1030 join_op
= ir_binop_logic_or
;
1032 switch (op0
->type
->base_type
) {
1033 case GLSL_TYPE_FLOAT
:
1034 case GLSL_TYPE_UINT
:
1036 case GLSL_TYPE_BOOL
:
1037 case GLSL_TYPE_DOUBLE
:
1038 return new(mem_ctx
) ir_expression(operation
, op0
, op1
);
1040 case GLSL_TYPE_ARRAY
: {
1041 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
1042 ir_rvalue
*e0
, *e1
, *result
;
1044 e0
= new(mem_ctx
) ir_dereference_array(op0
->clone(mem_ctx
, NULL
),
1045 new(mem_ctx
) ir_constant(i
));
1046 e1
= new(mem_ctx
) ir_dereference_array(op1
->clone(mem_ctx
, NULL
),
1047 new(mem_ctx
) ir_constant(i
));
1048 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
1051 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
1057 mark_whole_array_access(op0
);
1058 mark_whole_array_access(op1
);
1062 case GLSL_TYPE_STRUCT
: {
1063 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
1064 ir_rvalue
*e0
, *e1
, *result
;
1065 const char *field_name
= op0
->type
->fields
.structure
[i
].name
;
1067 e0
= new(mem_ctx
) ir_dereference_record(op0
->clone(mem_ctx
, NULL
),
1069 e1
= new(mem_ctx
) ir_dereference_record(op1
->clone(mem_ctx
, NULL
),
1071 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
1074 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
1082 case GLSL_TYPE_ERROR
:
1083 case GLSL_TYPE_VOID
:
1084 case GLSL_TYPE_SAMPLER
:
1085 case GLSL_TYPE_IMAGE
:
1086 case GLSL_TYPE_INTERFACE
:
1087 case GLSL_TYPE_ATOMIC_UINT
:
1088 case GLSL_TYPE_SUBROUTINE
:
1089 /* I assume a comparison of a struct containing a sampler just
1090 * ignores the sampler present in the type.
1096 cmp
= new(mem_ctx
) ir_constant(true);
1101 /* For logical operations, we want to ensure that the operands are
1102 * scalar booleans. If it isn't, emit an error and return a constant
1103 * boolean to avoid triggering cascading error messages.
1106 get_scalar_boolean_operand(exec_list
*instructions
,
1107 struct _mesa_glsl_parse_state
*state
,
1108 ast_expression
*parent_expr
,
1110 const char *operand_name
,
1111 bool *error_emitted
)
1113 ast_expression
*expr
= parent_expr
->subexpressions
[operand
];
1115 ir_rvalue
*val
= expr
->hir(instructions
, state
);
1117 if (val
->type
->is_boolean() && val
->type
->is_scalar())
1120 if (!*error_emitted
) {
1121 YYLTYPE loc
= expr
->get_location();
1122 _mesa_glsl_error(&loc
, state
, "%s of `%s' must be scalar boolean",
1124 parent_expr
->operator_string(parent_expr
->oper
));
1125 *error_emitted
= true;
1128 return new(ctx
) ir_constant(true);
1132 * If name refers to a builtin array whose maximum allowed size is less than
1133 * size, report an error and return true. Otherwise return false.
1136 check_builtin_array_max_size(const char *name
, unsigned size
,
1137 YYLTYPE loc
, struct _mesa_glsl_parse_state
*state
)
1139 if ((strcmp("gl_TexCoord", name
) == 0)
1140 && (size
> state
->Const
.MaxTextureCoords
)) {
1141 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1143 * "The size [of gl_TexCoord] can be at most
1144 * gl_MaxTextureCoords."
1146 _mesa_glsl_error(&loc
, state
, "`gl_TexCoord' array size cannot "
1147 "be larger than gl_MaxTextureCoords (%u)",
1148 state
->Const
.MaxTextureCoords
);
1149 } else if (strcmp("gl_ClipDistance", name
) == 0
1150 && size
> state
->Const
.MaxClipPlanes
) {
1151 /* From section 7.1 (Vertex Shader Special Variables) of the
1154 * "The gl_ClipDistance array is predeclared as unsized and
1155 * must be sized by the shader either redeclaring it with a
1156 * size or indexing it only with integral constant
1157 * expressions. ... The size can be at most
1158 * gl_MaxClipDistances."
1160 _mesa_glsl_error(&loc
, state
, "`gl_ClipDistance' array size cannot "
1161 "be larger than gl_MaxClipDistances (%u)",
1162 state
->Const
.MaxClipPlanes
);
1167 * Create the constant 1, of a which is appropriate for incrementing and
1168 * decrementing values of the given GLSL type. For example, if type is vec4,
1169 * this creates a constant value of 1.0 having type float.
1171 * If the given type is invalid for increment and decrement operators, return
1172 * a floating point 1--the error will be detected later.
1175 constant_one_for_inc_dec(void *ctx
, const glsl_type
*type
)
1177 switch (type
->base_type
) {
1178 case GLSL_TYPE_UINT
:
1179 return new(ctx
) ir_constant((unsigned) 1);
1181 return new(ctx
) ir_constant(1);
1183 case GLSL_TYPE_FLOAT
:
1184 return new(ctx
) ir_constant(1.0f
);
1189 ast_expression::hir(exec_list
*instructions
,
1190 struct _mesa_glsl_parse_state
*state
)
1192 return do_hir(instructions
, state
, true);
1196 ast_expression::hir_no_rvalue(exec_list
*instructions
,
1197 struct _mesa_glsl_parse_state
*state
)
1199 do_hir(instructions
, state
, false);
1203 ast_expression::do_hir(exec_list
*instructions
,
1204 struct _mesa_glsl_parse_state
*state
,
1208 static const int operations
[AST_NUM_OPERATORS
] = {
1209 -1, /* ast_assign doesn't convert to ir_expression. */
1210 -1, /* ast_plus doesn't convert to ir_expression. */
1224 ir_binop_any_nequal
,
1234 /* Note: The following block of expression types actually convert
1235 * to multiple IR instructions.
1237 ir_binop_mul
, /* ast_mul_assign */
1238 ir_binop_div
, /* ast_div_assign */
1239 ir_binop_mod
, /* ast_mod_assign */
1240 ir_binop_add
, /* ast_add_assign */
1241 ir_binop_sub
, /* ast_sub_assign */
1242 ir_binop_lshift
, /* ast_ls_assign */
1243 ir_binop_rshift
, /* ast_rs_assign */
1244 ir_binop_bit_and
, /* ast_and_assign */
1245 ir_binop_bit_xor
, /* ast_xor_assign */
1246 ir_binop_bit_or
, /* ast_or_assign */
1248 -1, /* ast_conditional doesn't convert to ir_expression. */
1249 ir_binop_add
, /* ast_pre_inc. */
1250 ir_binop_sub
, /* ast_pre_dec. */
1251 ir_binop_add
, /* ast_post_inc. */
1252 ir_binop_sub
, /* ast_post_dec. */
1253 -1, /* ast_field_selection doesn't conv to ir_expression. */
1254 -1, /* ast_array_index doesn't convert to ir_expression. */
1255 -1, /* ast_function_call doesn't conv to ir_expression. */
1256 -1, /* ast_identifier doesn't convert to ir_expression. */
1257 -1, /* ast_int_constant doesn't convert to ir_expression. */
1258 -1, /* ast_uint_constant doesn't conv to ir_expression. */
1259 -1, /* ast_float_constant doesn't conv to ir_expression. */
1260 -1, /* ast_bool_constant doesn't conv to ir_expression. */
1261 -1, /* ast_sequence doesn't convert to ir_expression. */
1263 ir_rvalue
*result
= NULL
;
1265 const struct glsl_type
*type
; /* a temporary variable for switch cases */
1266 bool error_emitted
= false;
1269 loc
= this->get_location();
1271 switch (this->oper
) {
1273 assert(!"ast_aggregate: Should never get here.");
1277 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1278 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1281 do_assignment(instructions
, state
,
1282 this->subexpressions
[0]->non_lvalue_description
,
1283 op
[0], op
[1], &result
, needs_rvalue
, false,
1284 this->subexpressions
[0]->get_location());
1289 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1291 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1293 error_emitted
= type
->is_error();
1299 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1301 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1303 error_emitted
= type
->is_error();
1305 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1313 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1314 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1316 type
= arithmetic_result_type(op
[0], op
[1],
1317 (this->oper
== ast_mul
),
1319 error_emitted
= type
->is_error();
1321 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1326 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1327 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1329 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1331 assert(operations
[this->oper
] == ir_binop_mod
);
1333 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1335 error_emitted
= type
->is_error();
1340 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1341 error_emitted
= true;
1344 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1345 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1346 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1348 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1350 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1357 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1358 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1360 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
1362 /* The relational operators must either generate an error or result
1363 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1365 assert(type
->is_error()
1366 || ((type
->base_type
== GLSL_TYPE_BOOL
)
1367 && type
->is_scalar()));
1369 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1371 error_emitted
= type
->is_error();
1376 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1377 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1379 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1381 * "The equality operators equal (==), and not equal (!=)
1382 * operate on all types. They result in a scalar Boolean. If
1383 * the operand types do not match, then there must be a
1384 * conversion from Section 4.1.10 "Implicit Conversions"
1385 * applied to one operand that can make them match, in which
1386 * case this conversion is done."
1389 if (op
[0]->type
== glsl_type::void_type
|| op
[1]->type
== glsl_type::void_type
) {
1390 _mesa_glsl_error(& loc
, state
, "`%s': wrong operand types: "
1391 "no operation `%1$s' exists that takes a left-hand "
1392 "operand of type 'void' or a right operand of type "
1393 "'void'", (this->oper
== ast_equal
) ? "==" : "!=");
1394 error_emitted
= true;
1395 } else if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
1396 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
1397 || (op
[0]->type
!= op
[1]->type
)) {
1398 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
1399 "type", (this->oper
== ast_equal
) ? "==" : "!=");
1400 error_emitted
= true;
1401 } else if ((op
[0]->type
->is_array() || op
[1]->type
->is_array()) &&
1402 !state
->check_version(120, 300, &loc
,
1403 "array comparisons forbidden")) {
1404 error_emitted
= true;
1405 } else if ((op
[0]->type
->contains_opaque() ||
1406 op
[1]->type
->contains_opaque())) {
1407 _mesa_glsl_error(&loc
, state
, "opaque type comparisons forbidden");
1408 error_emitted
= true;
1411 if (error_emitted
) {
1412 result
= new(ctx
) ir_constant(false);
1414 result
= do_comparison(ctx
, operations
[this->oper
], op
[0], op
[1]);
1415 assert(result
->type
== glsl_type::bool_type
);
1422 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1423 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1424 type
= bit_logic_result_type(op
[0]->type
, op
[1]->type
, this->oper
,
1426 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1428 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1432 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1434 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1435 error_emitted
= true;
1438 if (!op
[0]->type
->is_integer()) {
1439 _mesa_glsl_error(&loc
, state
, "operand of `~' must be an integer");
1440 error_emitted
= true;
1443 type
= error_emitted
? glsl_type::error_type
: op
[0]->type
;
1444 result
= new(ctx
) ir_expression(ir_unop_bit_not
, type
, op
[0], NULL
);
1447 case ast_logic_and
: {
1448 exec_list rhs_instructions
;
1449 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1450 "LHS", &error_emitted
);
1451 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1452 "RHS", &error_emitted
);
1454 if (rhs_instructions
.is_empty()) {
1455 result
= new(ctx
) ir_expression(ir_binop_logic_and
, op
[0], op
[1]);
1456 type
= result
->type
;
1458 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1461 instructions
->push_tail(tmp
);
1463 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1464 instructions
->push_tail(stmt
);
1466 stmt
->then_instructions
.append_list(&rhs_instructions
);
1467 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1468 ir_assignment
*const then_assign
=
1469 new(ctx
) ir_assignment(then_deref
, op
[1]);
1470 stmt
->then_instructions
.push_tail(then_assign
);
1472 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1473 ir_assignment
*const else_assign
=
1474 new(ctx
) ir_assignment(else_deref
, new(ctx
) ir_constant(false));
1475 stmt
->else_instructions
.push_tail(else_assign
);
1477 result
= new(ctx
) ir_dereference_variable(tmp
);
1483 case ast_logic_or
: {
1484 exec_list rhs_instructions
;
1485 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1486 "LHS", &error_emitted
);
1487 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1488 "RHS", &error_emitted
);
1490 if (rhs_instructions
.is_empty()) {
1491 result
= new(ctx
) ir_expression(ir_binop_logic_or
, op
[0], op
[1]);
1492 type
= result
->type
;
1494 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1497 instructions
->push_tail(tmp
);
1499 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1500 instructions
->push_tail(stmt
);
1502 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1503 ir_assignment
*const then_assign
=
1504 new(ctx
) ir_assignment(then_deref
, new(ctx
) ir_constant(true));
1505 stmt
->then_instructions
.push_tail(then_assign
);
1507 stmt
->else_instructions
.append_list(&rhs_instructions
);
1508 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1509 ir_assignment
*const else_assign
=
1510 new(ctx
) ir_assignment(else_deref
, op
[1]);
1511 stmt
->else_instructions
.push_tail(else_assign
);
1513 result
= new(ctx
) ir_dereference_variable(tmp
);
1520 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1522 * "The logical binary operators and (&&), or ( | | ), and
1523 * exclusive or (^^). They operate only on two Boolean
1524 * expressions and result in a Boolean expression."
1526 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0, "LHS",
1528 op
[1] = get_scalar_boolean_operand(instructions
, state
, this, 1, "RHS",
1531 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1536 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1537 "operand", &error_emitted
);
1539 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1543 case ast_mul_assign
:
1544 case ast_div_assign
:
1545 case ast_add_assign
:
1546 case ast_sub_assign
: {
1547 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1548 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1550 type
= arithmetic_result_type(op
[0], op
[1],
1551 (this->oper
== ast_mul_assign
),
1554 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1558 do_assignment(instructions
, state
,
1559 this->subexpressions
[0]->non_lvalue_description
,
1560 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1561 &result
, needs_rvalue
, false,
1562 this->subexpressions
[0]->get_location());
1564 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1565 * explicitly test for this because none of the binary expression
1566 * operators allow array operands either.
1572 case ast_mod_assign
: {
1573 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1574 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1576 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1578 assert(operations
[this->oper
] == ir_binop_mod
);
1580 ir_rvalue
*temp_rhs
;
1581 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1585 do_assignment(instructions
, state
,
1586 this->subexpressions
[0]->non_lvalue_description
,
1587 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1588 &result
, needs_rvalue
, false,
1589 this->subexpressions
[0]->get_location());
1594 case ast_rs_assign
: {
1595 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1596 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1597 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1599 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1600 type
, op
[0], op
[1]);
1602 do_assignment(instructions
, state
,
1603 this->subexpressions
[0]->non_lvalue_description
,
1604 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1605 &result
, needs_rvalue
, false,
1606 this->subexpressions
[0]->get_location());
1610 case ast_and_assign
:
1611 case ast_xor_assign
:
1612 case ast_or_assign
: {
1613 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1614 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1615 type
= bit_logic_result_type(op
[0]->type
, op
[1]->type
, this->oper
,
1617 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1618 type
, op
[0], op
[1]);
1620 do_assignment(instructions
, state
,
1621 this->subexpressions
[0]->non_lvalue_description
,
1622 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1623 &result
, needs_rvalue
, false,
1624 this->subexpressions
[0]->get_location());
1628 case ast_conditional
: {
1629 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1631 * "The ternary selection operator (?:). It operates on three
1632 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1633 * first expression, which must result in a scalar Boolean."
1635 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1636 "condition", &error_emitted
);
1638 /* The :? operator is implemented by generating an anonymous temporary
1639 * followed by an if-statement. The last instruction in each branch of
1640 * the if-statement assigns a value to the anonymous temporary. This
1641 * temporary is the r-value of the expression.
1643 exec_list then_instructions
;
1644 exec_list else_instructions
;
1646 op
[1] = this->subexpressions
[1]->hir(&then_instructions
, state
);
1647 op
[2] = this->subexpressions
[2]->hir(&else_instructions
, state
);
1649 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1651 * "The second and third expressions can be any type, as
1652 * long their types match, or there is a conversion in
1653 * Section 4.1.10 "Implicit Conversions" that can be applied
1654 * to one of the expressions to make their types match. This
1655 * resulting matching type is the type of the entire
1658 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1659 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1660 || (op
[1]->type
!= op
[2]->type
)) {
1661 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1663 _mesa_glsl_error(& loc
, state
, "second and third operands of ?: "
1664 "operator must have matching types");
1665 error_emitted
= true;
1666 type
= glsl_type::error_type
;
1671 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1673 * "The second and third expressions must be the same type, but can
1674 * be of any type other than an array."
1676 if (type
->is_array() &&
1677 !state
->check_version(120, 300, &loc
,
1678 "second and third operands of ?: operator "
1679 "cannot be arrays")) {
1680 error_emitted
= true;
1683 /* From section 4.1.7 of the GLSL 4.50 spec (Opaque Types):
1685 * "Except for array indexing, structure member selection, and
1686 * parentheses, opaque variables are not allowed to be operands in
1687 * expressions; such use results in a compile-time error."
1689 if (type
->contains_opaque()) {
1690 _mesa_glsl_error(&loc
, state
, "opaque variables cannot be operands "
1691 "of the ?: operator");
1692 error_emitted
= true;
1695 ir_constant
*cond_val
= op
[0]->constant_expression_value();
1697 if (then_instructions
.is_empty()
1698 && else_instructions
.is_empty()
1699 && cond_val
!= NULL
) {
1700 result
= cond_val
->value
.b
[0] ? op
[1] : op
[2];
1702 /* The copy to conditional_tmp reads the whole array. */
1703 if (type
->is_array()) {
1704 mark_whole_array_access(op
[1]);
1705 mark_whole_array_access(op
[2]);
1708 ir_variable
*const tmp
=
1709 new(ctx
) ir_variable(type
, "conditional_tmp", ir_var_temporary
);
1710 instructions
->push_tail(tmp
);
1712 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1713 instructions
->push_tail(stmt
);
1715 then_instructions
.move_nodes_to(& stmt
->then_instructions
);
1716 ir_dereference
*const then_deref
=
1717 new(ctx
) ir_dereference_variable(tmp
);
1718 ir_assignment
*const then_assign
=
1719 new(ctx
) ir_assignment(then_deref
, op
[1]);
1720 stmt
->then_instructions
.push_tail(then_assign
);
1722 else_instructions
.move_nodes_to(& stmt
->else_instructions
);
1723 ir_dereference
*const else_deref
=
1724 new(ctx
) ir_dereference_variable(tmp
);
1725 ir_assignment
*const else_assign
=
1726 new(ctx
) ir_assignment(else_deref
, op
[2]);
1727 stmt
->else_instructions
.push_tail(else_assign
);
1729 result
= new(ctx
) ir_dereference_variable(tmp
);
1736 this->non_lvalue_description
= (this->oper
== ast_pre_inc
)
1737 ? "pre-increment operation" : "pre-decrement operation";
1739 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1740 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1742 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1744 ir_rvalue
*temp_rhs
;
1745 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1749 do_assignment(instructions
, state
,
1750 this->subexpressions
[0]->non_lvalue_description
,
1751 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1752 &result
, needs_rvalue
, false,
1753 this->subexpressions
[0]->get_location());
1758 case ast_post_dec
: {
1759 this->non_lvalue_description
= (this->oper
== ast_post_inc
)
1760 ? "post-increment operation" : "post-decrement operation";
1761 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1762 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1764 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1766 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1768 ir_rvalue
*temp_rhs
;
1769 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1772 /* Get a temporary of a copy of the lvalue before it's modified.
1773 * This may get thrown away later.
1775 result
= get_lvalue_copy(instructions
, op
[0]->clone(ctx
, NULL
));
1777 ir_rvalue
*junk_rvalue
;
1779 do_assignment(instructions
, state
,
1780 this->subexpressions
[0]->non_lvalue_description
,
1781 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1782 &junk_rvalue
, false, false,
1783 this->subexpressions
[0]->get_location());
1788 case ast_field_selection
:
1789 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1792 case ast_array_index
: {
1793 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1795 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1796 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1798 result
= _mesa_ast_array_index_to_hir(ctx
, state
, op
[0], op
[1],
1801 if (result
->type
->is_error())
1802 error_emitted
= true;
1807 case ast_function_call
:
1808 /* Should *NEVER* get here. ast_function_call should always be handled
1809 * by ast_function_expression::hir.
1814 case ast_identifier
: {
1815 /* ast_identifier can appear several places in a full abstract syntax
1816 * tree. This particular use must be at location specified in the grammar
1817 * as 'variable_identifier'.
1820 state
->symbols
->get_variable(this->primary_expression
.identifier
);
1823 var
->data
.used
= true;
1824 result
= new(ctx
) ir_dereference_variable(var
);
1826 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
1827 this->primary_expression
.identifier
);
1829 result
= ir_rvalue::error_value(ctx
);
1830 error_emitted
= true;
1835 case ast_int_constant
:
1836 result
= new(ctx
) ir_constant(this->primary_expression
.int_constant
);
1839 case ast_uint_constant
:
1840 result
= new(ctx
) ir_constant(this->primary_expression
.uint_constant
);
1843 case ast_float_constant
:
1844 result
= new(ctx
) ir_constant(this->primary_expression
.float_constant
);
1847 case ast_bool_constant
:
1848 result
= new(ctx
) ir_constant(bool(this->primary_expression
.bool_constant
));
1851 case ast_double_constant
:
1852 result
= new(ctx
) ir_constant(this->primary_expression
.double_constant
);
1855 case ast_sequence
: {
1856 /* It should not be possible to generate a sequence in the AST without
1857 * any expressions in it.
1859 assert(!this->expressions
.is_empty());
1861 /* The r-value of a sequence is the last expression in the sequence. If
1862 * the other expressions in the sequence do not have side-effects (and
1863 * therefore add instructions to the instruction list), they get dropped
1866 exec_node
*previous_tail_pred
= NULL
;
1867 YYLTYPE previous_operand_loc
= loc
;
1869 foreach_list_typed (ast_node
, ast
, link
, &this->expressions
) {
1870 /* If one of the operands of comma operator does not generate any
1871 * code, we want to emit a warning. At each pass through the loop
1872 * previous_tail_pred will point to the last instruction in the
1873 * stream *before* processing the previous operand. Naturally,
1874 * instructions->tail_pred will point to the last instruction in the
1875 * stream *after* processing the previous operand. If the two
1876 * pointers match, then the previous operand had no effect.
1878 * The warning behavior here differs slightly from GCC. GCC will
1879 * only emit a warning if none of the left-hand operands have an
1880 * effect. However, it will emit a warning for each. I believe that
1881 * there are some cases in C (especially with GCC extensions) where
1882 * it is useful to have an intermediate step in a sequence have no
1883 * effect, but I don't think these cases exist in GLSL. Either way,
1884 * it would be a giant hassle to replicate that behavior.
1886 if (previous_tail_pred
== instructions
->tail_pred
) {
1887 _mesa_glsl_warning(&previous_operand_loc
, state
,
1888 "left-hand operand of comma expression has "
1892 /* tail_pred is directly accessed instead of using the get_tail()
1893 * method for performance reasons. get_tail() has extra code to
1894 * return NULL when the list is empty. We don't care about that
1895 * here, so using tail_pred directly is fine.
1897 previous_tail_pred
= instructions
->tail_pred
;
1898 previous_operand_loc
= ast
->get_location();
1900 result
= ast
->hir(instructions
, state
);
1903 /* Any errors should have already been emitted in the loop above.
1905 error_emitted
= true;
1909 type
= NULL
; /* use result->type, not type. */
1910 assert(result
!= NULL
|| !needs_rvalue
);
1912 if (result
&& result
->type
->is_error() && !error_emitted
)
1913 _mesa_glsl_error(& loc
, state
, "type mismatch");
1920 ast_expression_statement::hir(exec_list
*instructions
,
1921 struct _mesa_glsl_parse_state
*state
)
1923 /* It is possible to have expression statements that don't have an
1924 * expression. This is the solitary semicolon:
1926 * for (i = 0; i < 5; i++)
1929 * In this case the expression will be NULL. Test for NULL and don't do
1930 * anything in that case.
1932 if (expression
!= NULL
)
1933 expression
->hir_no_rvalue(instructions
, state
);
1935 /* Statements do not have r-values.
1942 ast_compound_statement::hir(exec_list
*instructions
,
1943 struct _mesa_glsl_parse_state
*state
)
1946 state
->symbols
->push_scope();
1948 foreach_list_typed (ast_node
, ast
, link
, &this->statements
)
1949 ast
->hir(instructions
, state
);
1952 state
->symbols
->pop_scope();
1954 /* Compound statements do not have r-values.
1960 * Evaluate the given exec_node (which should be an ast_node representing
1961 * a single array dimension) and return its integer value.
1964 process_array_size(exec_node
*node
,
1965 struct _mesa_glsl_parse_state
*state
)
1967 exec_list dummy_instructions
;
1969 ast_node
*array_size
= exec_node_data(ast_node
, node
, link
);
1970 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
1971 YYLTYPE loc
= array_size
->get_location();
1974 _mesa_glsl_error(& loc
, state
,
1975 "array size could not be resolved");
1979 if (!ir
->type
->is_integer()) {
1980 _mesa_glsl_error(& loc
, state
,
1981 "array size must be integer type");
1985 if (!ir
->type
->is_scalar()) {
1986 _mesa_glsl_error(& loc
, state
,
1987 "array size must be scalar type");
1991 ir_constant
*const size
= ir
->constant_expression_value();
1993 _mesa_glsl_error(& loc
, state
, "array size must be a "
1994 "constant valued expression");
1998 if (size
->value
.i
[0] <= 0) {
1999 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
2003 assert(size
->type
== ir
->type
);
2005 /* If the array size is const (and we've verified that
2006 * it is) then no instructions should have been emitted
2007 * when we converted it to HIR. If they were emitted,
2008 * then either the array size isn't const after all, or
2009 * we are emitting unnecessary instructions.
2011 assert(dummy_instructions
.is_empty());
2013 return size
->value
.u
[0];
2016 static const glsl_type
*
2017 process_array_type(YYLTYPE
*loc
, const glsl_type
*base
,
2018 ast_array_specifier
*array_specifier
,
2019 struct _mesa_glsl_parse_state
*state
)
2021 const glsl_type
*array_type
= base
;
2023 if (array_specifier
!= NULL
) {
2024 if (base
->is_array()) {
2026 /* From page 19 (page 25) of the GLSL 1.20 spec:
2028 * "Only one-dimensional arrays may be declared."
2030 if (!state
->ARB_arrays_of_arrays_enable
) {
2031 _mesa_glsl_error(loc
, state
,
2032 "invalid array of `%s'"
2033 "GL_ARB_arrays_of_arrays "
2034 "required for defining arrays of arrays",
2036 return glsl_type::error_type
;
2039 if (base
->length
== 0) {
2040 _mesa_glsl_error(loc
, state
,
2041 "only the outermost array dimension can "
2044 return glsl_type::error_type
;
2048 for (exec_node
*node
= array_specifier
->array_dimensions
.tail_pred
;
2049 !node
->is_head_sentinel(); node
= node
->prev
) {
2050 unsigned array_size
= process_array_size(node
, state
);
2051 array_type
= glsl_type::get_array_instance(array_type
, array_size
);
2054 if (array_specifier
->is_unsized_array
)
2055 array_type
= glsl_type::get_array_instance(array_type
, 0);
2063 ast_type_specifier::glsl_type(const char **name
,
2064 struct _mesa_glsl_parse_state
*state
) const
2066 const struct glsl_type
*type
;
2068 type
= state
->symbols
->get_type(this->type_name
);
2069 *name
= this->type_name
;
2071 YYLTYPE loc
= this->get_location();
2072 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
2078 ast_fully_specified_type::glsl_type(const char **name
,
2079 struct _mesa_glsl_parse_state
*state
) const
2081 const struct glsl_type
*type
= this->specifier
->glsl_type(name
, state
);
2086 if (type
->base_type
== GLSL_TYPE_FLOAT
2088 && state
->stage
== MESA_SHADER_FRAGMENT
2089 && this->qualifier
.precision
== ast_precision_none
2090 && state
->symbols
->get_variable("#default precision") == NULL
) {
2091 YYLTYPE loc
= this->get_location();
2092 _mesa_glsl_error(&loc
, state
,
2093 "no precision specified this scope for type `%s'",
2101 * Determine whether a toplevel variable declaration declares a varying. This
2102 * function operates by examining the variable's mode and the shader target,
2103 * so it correctly identifies linkage variables regardless of whether they are
2104 * declared using the deprecated "varying" syntax or the new "in/out" syntax.
2106 * Passing a non-toplevel variable declaration (e.g. a function parameter) to
2107 * this function will produce undefined results.
2110 is_varying_var(ir_variable
*var
, gl_shader_stage target
)
2113 case MESA_SHADER_VERTEX
:
2114 return var
->data
.mode
== ir_var_shader_out
;
2115 case MESA_SHADER_FRAGMENT
:
2116 return var
->data
.mode
== ir_var_shader_in
;
2118 return var
->data
.mode
== ir_var_shader_out
|| var
->data
.mode
== ir_var_shader_in
;
2124 * Matrix layout qualifiers are only allowed on certain types
2127 validate_matrix_layout_for_type(struct _mesa_glsl_parse_state
*state
,
2129 const glsl_type
*type
,
2132 if (var
&& !var
->is_in_buffer_block()) {
2133 /* Layout qualifiers may only apply to interface blocks and fields in
2136 _mesa_glsl_error(loc
, state
,
2137 "uniform block layout qualifiers row_major and "
2138 "column_major may not be applied to variables "
2139 "outside of uniform blocks");
2140 } else if (!type
->is_matrix()) {
2141 /* The OpenGL ES 3.0 conformance tests did not originally allow
2142 * matrix layout qualifiers on non-matrices. However, the OpenGL
2143 * 4.4 and OpenGL ES 3.0 (revision TBD) specifications were
2144 * amended to specifically allow these layouts on all types. Emit
2145 * a warning so that people know their code may not be portable.
2147 _mesa_glsl_warning(loc
, state
,
2148 "uniform block layout qualifiers row_major and "
2149 "column_major applied to non-matrix types may "
2150 "be rejected by older compilers");
2151 } else if (type
->is_record()) {
2152 /* We allow 'layout(row_major)' on structure types because it's the only
2153 * way to get row-major layouts on matrices contained in structures.
2155 _mesa_glsl_warning(loc
, state
,
2156 "uniform block layout qualifiers row_major and "
2157 "column_major applied to structure types is not "
2158 "strictly conformant and may be rejected by other "
2164 validate_binding_qualifier(struct _mesa_glsl_parse_state
*state
,
2166 const glsl_type
*type
,
2167 const ast_type_qualifier
*qual
)
2169 if (!qual
->flags
.q
.uniform
&& !qual
->flags
.q
.buffer
) {
2170 _mesa_glsl_error(loc
, state
,
2171 "the \"binding\" qualifier only applies to uniforms and "
2172 "shader storage buffer objects");
2176 if (qual
->binding
< 0) {
2177 _mesa_glsl_error(loc
, state
, "binding values must be >= 0");
2181 const struct gl_context
*const ctx
= state
->ctx
;
2182 unsigned elements
= type
->is_array() ? type
->arrays_of_arrays_size() : 1;
2183 unsigned max_index
= qual
->binding
+ elements
- 1;
2184 const glsl_type
*base_type
= type
->without_array();
2186 if (base_type
->is_interface()) {
2187 /* UBOs. From page 60 of the GLSL 4.20 specification:
2188 * "If the binding point for any uniform block instance is less than zero,
2189 * or greater than or equal to the implementation-dependent maximum
2190 * number of uniform buffer bindings, a compilation error will occur.
2191 * When the binding identifier is used with a uniform block instanced as
2192 * an array of size N, all elements of the array from binding through
2193 * binding + N – 1 must be within this range."
2195 * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
2197 if (qual
->flags
.q
.uniform
&&
2198 max_index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2199 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d UBOs exceeds "
2200 "the maximum number of UBO binding points (%d)",
2201 qual
->binding
, elements
,
2202 ctx
->Const
.MaxUniformBufferBindings
);
2206 /* SSBOs. From page 67 of the GLSL 4.30 specification:
2207 * "If the binding point for any uniform or shader storage block instance
2208 * is less than zero, or greater than or equal to the
2209 * implementation-dependent maximum number of uniform buffer bindings, a
2210 * compile-time error will occur. When the binding identifier is used
2211 * with a uniform or shader storage block instanced as an array of size
2212 * N, all elements of the array from binding through binding + N – 1 must
2213 * be within this range."
2215 if (qual
->flags
.q
.buffer
&&
2216 max_index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2217 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d SSBOs exceeds "
2218 "the maximum number of SSBO binding points (%d)",
2219 qual
->binding
, elements
,
2220 ctx
->Const
.MaxShaderStorageBufferBindings
);
2223 } else if (base_type
->is_sampler()) {
2224 /* Samplers. From page 63 of the GLSL 4.20 specification:
2225 * "If the binding is less than zero, or greater than or equal to the
2226 * implementation-dependent maximum supported number of units, a
2227 * compilation error will occur. When the binding identifier is used
2228 * with an array of size N, all elements of the array from binding
2229 * through binding + N - 1 must be within this range."
2231 unsigned limit
= ctx
->Const
.MaxCombinedTextureImageUnits
;
2233 if (max_index
>= limit
) {
2234 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d samplers "
2235 "exceeds the maximum number of texture image units "
2236 "(%d)", qual
->binding
, elements
, limit
);
2240 } else if (base_type
->contains_atomic()) {
2241 assert(ctx
->Const
.MaxAtomicBufferBindings
<= MAX_COMBINED_ATOMIC_BUFFERS
);
2242 if (unsigned(qual
->binding
) >= ctx
->Const
.MaxAtomicBufferBindings
) {
2243 _mesa_glsl_error(loc
, state
, "layout(binding = %d) exceeds the "
2244 " maximum number of atomic counter buffer bindings"
2245 "(%d)", qual
->binding
,
2246 ctx
->Const
.MaxAtomicBufferBindings
);
2250 } else if (state
->is_version(420, 310) && base_type
->is_image()) {
2251 assert(ctx
->Const
.MaxImageUnits
<= MAX_IMAGE_UNITS
);
2252 if (max_index
>= ctx
->Const
.MaxImageUnits
) {
2253 _mesa_glsl_error(loc
, state
, "Image binding %d exceeds the "
2254 " maximum number of image units (%d)", max_index
,
2255 ctx
->Const
.MaxImageUnits
);
2260 _mesa_glsl_error(loc
, state
,
2261 "the \"binding\" qualifier only applies to uniform "
2262 "blocks, opaque variables, or arrays thereof");
2270 static glsl_interp_qualifier
2271 interpret_interpolation_qualifier(const struct ast_type_qualifier
*qual
,
2272 ir_variable_mode mode
,
2273 struct _mesa_glsl_parse_state
*state
,
2276 glsl_interp_qualifier interpolation
;
2277 if (qual
->flags
.q
.flat
)
2278 interpolation
= INTERP_QUALIFIER_FLAT
;
2279 else if (qual
->flags
.q
.noperspective
)
2280 interpolation
= INTERP_QUALIFIER_NOPERSPECTIVE
;
2281 else if (qual
->flags
.q
.smooth
)
2282 interpolation
= INTERP_QUALIFIER_SMOOTH
;
2284 interpolation
= INTERP_QUALIFIER_NONE
;
2286 if (interpolation
!= INTERP_QUALIFIER_NONE
) {
2287 if (mode
!= ir_var_shader_in
&& mode
!= ir_var_shader_out
) {
2288 _mesa_glsl_error(loc
, state
,
2289 "interpolation qualifier `%s' can only be applied to "
2290 "shader inputs or outputs.",
2291 interpolation_string(interpolation
));
2295 if ((state
->stage
== MESA_SHADER_VERTEX
&& mode
== ir_var_shader_in
) ||
2296 (state
->stage
== MESA_SHADER_FRAGMENT
&& mode
== ir_var_shader_out
)) {
2297 _mesa_glsl_error(loc
, state
,
2298 "interpolation qualifier `%s' cannot be applied to "
2299 "vertex shader inputs or fragment shader outputs",
2300 interpolation_string(interpolation
));
2304 return interpolation
;
2309 validate_explicit_location(const struct ast_type_qualifier
*qual
,
2311 struct _mesa_glsl_parse_state
*state
,
2316 /* Checks for GL_ARB_explicit_uniform_location. */
2317 if (qual
->flags
.q
.uniform
) {
2318 if (!state
->check_explicit_uniform_location_allowed(loc
, var
))
2321 const struct gl_context
*const ctx
= state
->ctx
;
2322 unsigned max_loc
= qual
->location
+ var
->type
->uniform_locations() - 1;
2324 /* ARB_explicit_uniform_location specification states:
2326 * "The explicitly defined locations and the generated locations
2327 * must be in the range of 0 to MAX_UNIFORM_LOCATIONS minus one."
2329 * "Valid locations for default-block uniform variable locations
2330 * are in the range of 0 to the implementation-defined maximum
2331 * number of uniform locations."
2333 if (qual
->location
< 0) {
2334 _mesa_glsl_error(loc
, state
,
2335 "explicit location < 0 for uniform %s", var
->name
);
2339 if (max_loc
>= ctx
->Const
.MaxUserAssignableUniformLocations
) {
2340 _mesa_glsl_error(loc
, state
, "location(s) consumed by uniform %s "
2341 ">= MAX_UNIFORM_LOCATIONS (%u)", var
->name
,
2342 ctx
->Const
.MaxUserAssignableUniformLocations
);
2346 var
->data
.explicit_location
= true;
2347 var
->data
.location
= qual
->location
;
2351 /* Between GL_ARB_explicit_attrib_location an
2352 * GL_ARB_separate_shader_objects, the inputs and outputs of any shader
2353 * stage can be assigned explicit locations. The checking here associates
2354 * the correct extension with the correct stage's input / output:
2358 * vertex explicit_loc sso
2359 * tess control sso sso
2362 * fragment sso explicit_loc
2364 switch (state
->stage
) {
2365 case MESA_SHADER_VERTEX
:
2366 if (var
->data
.mode
== ir_var_shader_in
) {
2367 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
2373 if (var
->data
.mode
== ir_var_shader_out
) {
2374 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
2383 case MESA_SHADER_TESS_CTRL
:
2384 case MESA_SHADER_TESS_EVAL
:
2385 case MESA_SHADER_GEOMETRY
:
2386 if (var
->data
.mode
== ir_var_shader_in
|| var
->data
.mode
== ir_var_shader_out
) {
2387 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
2396 case MESA_SHADER_FRAGMENT
:
2397 if (var
->data
.mode
== ir_var_shader_in
) {
2398 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
2404 if (var
->data
.mode
== ir_var_shader_out
) {
2405 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
2414 case MESA_SHADER_COMPUTE
:
2415 _mesa_glsl_error(loc
, state
,
2416 "compute shader variables cannot be given "
2417 "explicit locations");
2422 _mesa_glsl_error(loc
, state
,
2423 "%s cannot be given an explicit location in %s shader",
2425 _mesa_shader_stage_to_string(state
->stage
));
2427 var
->data
.explicit_location
= true;
2429 /* This bit of silliness is needed because invalid explicit locations
2430 * are supposed to be flagged during linking. Small negative values
2431 * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
2432 * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
2433 * The linker needs to be able to differentiate these cases. This
2434 * ensures that negative values stay negative.
2436 if (qual
->location
>= 0) {
2437 switch (state
->stage
) {
2438 case MESA_SHADER_VERTEX
:
2439 var
->data
.location
= (var
->data
.mode
== ir_var_shader_in
)
2440 ? (qual
->location
+ VERT_ATTRIB_GENERIC0
)
2441 : (qual
->location
+ VARYING_SLOT_VAR0
);
2444 case MESA_SHADER_TESS_CTRL
:
2445 case MESA_SHADER_TESS_EVAL
:
2446 case MESA_SHADER_GEOMETRY
:
2447 if (var
->data
.patch
)
2448 var
->data
.location
= qual
->location
+ VARYING_SLOT_PATCH0
;
2450 var
->data
.location
= qual
->location
+ VARYING_SLOT_VAR0
;
2453 case MESA_SHADER_FRAGMENT
:
2454 var
->data
.location
= (var
->data
.mode
== ir_var_shader_out
)
2455 ? (qual
->location
+ FRAG_RESULT_DATA0
)
2456 : (qual
->location
+ VARYING_SLOT_VAR0
);
2458 case MESA_SHADER_COMPUTE
:
2459 assert(!"Unexpected shader type");
2463 var
->data
.location
= qual
->location
;
2466 if (qual
->flags
.q
.explicit_index
) {
2467 /* From the GLSL 4.30 specification, section 4.4.2 (Output
2468 * Layout Qualifiers):
2470 * "It is also a compile-time error if a fragment shader
2471 * sets a layout index to less than 0 or greater than 1."
2473 * Older specifications don't mandate a behavior; we take
2474 * this as a clarification and always generate the error.
2476 if (qual
->index
< 0 || qual
->index
> 1) {
2477 _mesa_glsl_error(loc
, state
,
2478 "explicit index may only be 0 or 1");
2480 var
->data
.explicit_index
= true;
2481 var
->data
.index
= qual
->index
;
2488 apply_image_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
2490 struct _mesa_glsl_parse_state
*state
,
2493 const glsl_type
*base_type
= var
->type
->without_array();
2495 if (base_type
->is_image()) {
2496 if (var
->data
.mode
!= ir_var_uniform
&&
2497 var
->data
.mode
!= ir_var_function_in
) {
2498 _mesa_glsl_error(loc
, state
, "image variables may only be declared as "
2499 "function parameters or uniform-qualified "
2500 "global variables");
2503 var
->data
.image_read_only
|= qual
->flags
.q
.read_only
;
2504 var
->data
.image_write_only
|= qual
->flags
.q
.write_only
;
2505 var
->data
.image_coherent
|= qual
->flags
.q
.coherent
;
2506 var
->data
.image_volatile
|= qual
->flags
.q
._volatile
;
2507 var
->data
.image_restrict
|= qual
->flags
.q
.restrict_flag
;
2508 var
->data
.read_only
= true;
2510 if (qual
->flags
.q
.explicit_image_format
) {
2511 if (var
->data
.mode
== ir_var_function_in
) {
2512 _mesa_glsl_error(loc
, state
, "format qualifiers cannot be "
2513 "used on image function parameters");
2516 if (qual
->image_base_type
!= base_type
->sampler_type
) {
2517 _mesa_glsl_error(loc
, state
, "format qualifier doesn't match the "
2518 "base data type of the image");
2521 var
->data
.image_format
= qual
->image_format
;
2523 if (var
->data
.mode
== ir_var_uniform
) {
2524 if (state
->es_shader
) {
2525 _mesa_glsl_error(loc
, state
, "all image uniforms "
2526 "must have a format layout qualifier");
2528 } else if (!qual
->flags
.q
.write_only
) {
2529 _mesa_glsl_error(loc
, state
, "image uniforms not qualified with "
2530 "`writeonly' must have a format layout "
2535 var
->data
.image_format
= GL_NONE
;
2538 /* From page 70 of the GLSL ES 3.1 specification:
2540 * "Except for image variables qualified with the format qualifiers
2541 * r32f, r32i, and r32ui, image variables must specify either memory
2542 * qualifier readonly or the memory qualifier writeonly."
2544 if (state
->es_shader
&&
2545 var
->data
.image_format
!= GL_R32F
&&
2546 var
->data
.image_format
!= GL_R32I
&&
2547 var
->data
.image_format
!= GL_R32UI
&&
2548 !var
->data
.image_read_only
&&
2549 !var
->data
.image_write_only
) {
2550 _mesa_glsl_error(loc
, state
, "image variables of format other than "
2551 "r32f, r32i or r32ui must be qualified `readonly' or "
2555 } else if (qual
->flags
.q
.read_only
||
2556 qual
->flags
.q
.write_only
||
2557 qual
->flags
.q
.coherent
||
2558 qual
->flags
.q
._volatile
||
2559 qual
->flags
.q
.restrict_flag
||
2560 qual
->flags
.q
.explicit_image_format
) {
2561 _mesa_glsl_error(loc
, state
, "memory qualifiers may only be applied to "
2566 static inline const char*
2567 get_layout_qualifier_string(bool origin_upper_left
, bool pixel_center_integer
)
2569 if (origin_upper_left
&& pixel_center_integer
)
2570 return "origin_upper_left, pixel_center_integer";
2571 else if (origin_upper_left
)
2572 return "origin_upper_left";
2573 else if (pixel_center_integer
)
2574 return "pixel_center_integer";
2580 is_conflicting_fragcoord_redeclaration(struct _mesa_glsl_parse_state
*state
,
2581 const struct ast_type_qualifier
*qual
)
2583 /* If gl_FragCoord was previously declared, and the qualifiers were
2584 * different in any way, return true.
2586 if (state
->fs_redeclares_gl_fragcoord
) {
2587 return (state
->fs_pixel_center_integer
!= qual
->flags
.q
.pixel_center_integer
2588 || state
->fs_origin_upper_left
!= qual
->flags
.q
.origin_upper_left
);
2595 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
2597 struct _mesa_glsl_parse_state
*state
,
2601 STATIC_ASSERT(sizeof(qual
->flags
.q
) <= sizeof(qual
->flags
.i
));
2603 if (qual
->flags
.q
.invariant
) {
2604 if (var
->data
.used
) {
2605 _mesa_glsl_error(loc
, state
,
2606 "variable `%s' may not be redeclared "
2607 "`invariant' after being used",
2610 var
->data
.invariant
= 1;
2614 if (qual
->flags
.q
.precise
) {
2615 if (var
->data
.used
) {
2616 _mesa_glsl_error(loc
, state
,
2617 "variable `%s' may not be redeclared "
2618 "`precise' after being used",
2621 var
->data
.precise
= 1;
2625 if (qual
->flags
.q
.subroutine
&& !qual
->flags
.q
.uniform
) {
2626 _mesa_glsl_error(loc
, state
,
2627 "`subroutine' may only be applied to uniforms, "
2628 "subroutine type declarations, or function definitions");
2631 if (qual
->flags
.q
.constant
|| qual
->flags
.q
.attribute
2632 || qual
->flags
.q
.uniform
2633 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
2634 var
->data
.read_only
= 1;
2636 if (qual
->flags
.q
.centroid
)
2637 var
->data
.centroid
= 1;
2639 if (qual
->flags
.q
.sample
)
2640 var
->data
.sample
= 1;
2642 if (state
->stage
== MESA_SHADER_GEOMETRY
&&
2643 qual
->flags
.q
.out
&& qual
->flags
.q
.stream
) {
2644 var
->data
.stream
= qual
->stream
;
2647 if (qual
->flags
.q
.patch
)
2648 var
->data
.patch
= 1;
2650 if (qual
->flags
.q
.attribute
&& state
->stage
!= MESA_SHADER_VERTEX
) {
2651 var
->type
= glsl_type::error_type
;
2652 _mesa_glsl_error(loc
, state
,
2653 "`attribute' variables may not be declared in the "
2655 _mesa_shader_stage_to_string(state
->stage
));
2658 /* Disallow layout qualifiers which may only appear on layout declarations. */
2659 if (qual
->flags
.q
.prim_type
) {
2660 _mesa_glsl_error(loc
, state
,
2661 "Primitive type may only be specified on GS input or output "
2662 "layout declaration, not on variables.");
2665 /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
2667 * "However, the const qualifier cannot be used with out or inout."
2669 * The same section of the GLSL 4.40 spec further clarifies this saying:
2671 * "The const qualifier cannot be used with out or inout, or a
2672 * compile-time error results."
2674 if (is_parameter
&& qual
->flags
.q
.constant
&& qual
->flags
.q
.out
) {
2675 _mesa_glsl_error(loc
, state
,
2676 "`const' may not be applied to `out' or `inout' "
2677 "function parameters");
2680 /* If there is no qualifier that changes the mode of the variable, leave
2681 * the setting alone.
2683 assert(var
->data
.mode
!= ir_var_temporary
);
2684 if (qual
->flags
.q
.in
&& qual
->flags
.q
.out
)
2685 var
->data
.mode
= ir_var_function_inout
;
2686 else if (qual
->flags
.q
.in
)
2687 var
->data
.mode
= is_parameter
? ir_var_function_in
: ir_var_shader_in
;
2688 else if (qual
->flags
.q
.attribute
2689 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
2690 var
->data
.mode
= ir_var_shader_in
;
2691 else if (qual
->flags
.q
.out
)
2692 var
->data
.mode
= is_parameter
? ir_var_function_out
: ir_var_shader_out
;
2693 else if (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_VERTEX
))
2694 var
->data
.mode
= ir_var_shader_out
;
2695 else if (qual
->flags
.q
.uniform
)
2696 var
->data
.mode
= ir_var_uniform
;
2697 else if (qual
->flags
.q
.buffer
)
2698 var
->data
.mode
= ir_var_shader_storage
;
2700 if (!is_parameter
&& is_varying_var(var
, state
->stage
)) {
2701 /* User-defined ins/outs are not permitted in compute shaders. */
2702 if (state
->stage
== MESA_SHADER_COMPUTE
) {
2703 _mesa_glsl_error(loc
, state
,
2704 "user-defined input and output variables are not "
2705 "permitted in compute shaders");
2708 /* This variable is being used to link data between shader stages (in
2709 * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
2710 * that is allowed for such purposes.
2712 * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
2714 * "The varying qualifier can be used only with the data types
2715 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
2718 * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
2719 * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
2721 * "Fragment inputs can only be signed and unsigned integers and
2722 * integer vectors, float, floating-point vectors, matrices, or
2723 * arrays of these. Structures cannot be input.
2725 * Similar text exists in the section on vertex shader outputs.
2727 * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
2728 * 3.00 spec allows structs as well. Varying structs are also allowed
2731 switch (var
->type
->get_scalar_type()->base_type
) {
2732 case GLSL_TYPE_FLOAT
:
2733 /* Ok in all GLSL versions */
2735 case GLSL_TYPE_UINT
:
2737 if (state
->is_version(130, 300))
2739 _mesa_glsl_error(loc
, state
,
2740 "varying variables must be of base type float in %s",
2741 state
->get_version_string());
2743 case GLSL_TYPE_STRUCT
:
2744 if (state
->is_version(150, 300))
2746 _mesa_glsl_error(loc
, state
,
2747 "varying variables may not be of type struct");
2749 case GLSL_TYPE_DOUBLE
:
2752 _mesa_glsl_error(loc
, state
, "illegal type for a varying variable");
2757 if (state
->all_invariant
&& (state
->current_function
== NULL
)) {
2758 switch (state
->stage
) {
2759 case MESA_SHADER_VERTEX
:
2760 if (var
->data
.mode
== ir_var_shader_out
)
2761 var
->data
.invariant
= true;
2763 case MESA_SHADER_TESS_CTRL
:
2764 case MESA_SHADER_TESS_EVAL
:
2765 case MESA_SHADER_GEOMETRY
:
2766 if ((var
->data
.mode
== ir_var_shader_in
)
2767 || (var
->data
.mode
== ir_var_shader_out
))
2768 var
->data
.invariant
= true;
2770 case MESA_SHADER_FRAGMENT
:
2771 if (var
->data
.mode
== ir_var_shader_in
)
2772 var
->data
.invariant
= true;
2774 case MESA_SHADER_COMPUTE
:
2775 /* Invariance isn't meaningful in compute shaders. */
2780 var
->data
.interpolation
=
2781 interpret_interpolation_qualifier(qual
, (ir_variable_mode
) var
->data
.mode
,
2784 var
->data
.pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
2785 var
->data
.origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
2786 if ((qual
->flags
.q
.origin_upper_left
|| qual
->flags
.q
.pixel_center_integer
)
2787 && (strcmp(var
->name
, "gl_FragCoord") != 0)) {
2788 const char *const qual_string
= (qual
->flags
.q
.origin_upper_left
)
2789 ? "origin_upper_left" : "pixel_center_integer";
2791 _mesa_glsl_error(loc
, state
,
2792 "layout qualifier `%s' can only be applied to "
2793 "fragment shader input `gl_FragCoord'",
2797 if (var
->name
!= NULL
&& strcmp(var
->name
, "gl_FragCoord") == 0) {
2799 /* Section 4.3.8.1, page 39 of GLSL 1.50 spec says:
2801 * "Within any shader, the first redeclarations of gl_FragCoord
2802 * must appear before any use of gl_FragCoord."
2804 * Generate a compiler error if above condition is not met by the
2807 ir_variable
*earlier
= state
->symbols
->get_variable("gl_FragCoord");
2808 if (earlier
!= NULL
&&
2809 earlier
->data
.used
&&
2810 !state
->fs_redeclares_gl_fragcoord
) {
2811 _mesa_glsl_error(loc
, state
,
2812 "gl_FragCoord used before its first redeclaration "
2813 "in fragment shader");
2816 /* Make sure all gl_FragCoord redeclarations specify the same layout
2819 if (is_conflicting_fragcoord_redeclaration(state
, qual
)) {
2820 const char *const qual_string
=
2821 get_layout_qualifier_string(qual
->flags
.q
.origin_upper_left
,
2822 qual
->flags
.q
.pixel_center_integer
);
2824 const char *const state_string
=
2825 get_layout_qualifier_string(state
->fs_origin_upper_left
,
2826 state
->fs_pixel_center_integer
);
2828 _mesa_glsl_error(loc
, state
,
2829 "gl_FragCoord redeclared with different layout "
2830 "qualifiers (%s) and (%s) ",
2834 state
->fs_origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
2835 state
->fs_pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
2836 state
->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers
=
2837 !qual
->flags
.q
.origin_upper_left
&& !qual
->flags
.q
.pixel_center_integer
;
2838 state
->fs_redeclares_gl_fragcoord
=
2839 state
->fs_origin_upper_left
||
2840 state
->fs_pixel_center_integer
||
2841 state
->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers
;
2844 if (qual
->flags
.q
.explicit_location
) {
2845 validate_explicit_location(qual
, var
, state
, loc
);
2846 } else if (qual
->flags
.q
.explicit_index
) {
2847 _mesa_glsl_error(loc
, state
, "explicit index requires explicit location");
2850 if (qual
->flags
.q
.explicit_binding
&&
2851 validate_binding_qualifier(state
, loc
, var
->type
, qual
)) {
2852 var
->data
.explicit_binding
= true;
2853 var
->data
.binding
= qual
->binding
;
2856 if (var
->type
->contains_atomic()) {
2857 if (var
->data
.mode
== ir_var_uniform
) {
2858 if (var
->data
.explicit_binding
) {
2860 &state
->atomic_counter_offsets
[var
->data
.binding
];
2862 if (*offset
% ATOMIC_COUNTER_SIZE
)
2863 _mesa_glsl_error(loc
, state
,
2864 "misaligned atomic counter offset");
2866 var
->data
.atomic
.offset
= *offset
;
2867 *offset
+= var
->type
->atomic_size();
2870 _mesa_glsl_error(loc
, state
,
2871 "atomic counters require explicit binding point");
2873 } else if (var
->data
.mode
!= ir_var_function_in
) {
2874 _mesa_glsl_error(loc
, state
, "atomic counters may only be declared as "
2875 "function parameters or uniform-qualified "
2876 "global variables");
2880 /* Does the declaration use the deprecated 'attribute' or 'varying'
2883 const bool uses_deprecated_qualifier
= qual
->flags
.q
.attribute
2884 || qual
->flags
.q
.varying
;
2887 /* Validate auxiliary storage qualifiers */
2889 /* From section 4.3.4 of the GLSL 1.30 spec:
2890 * "It is an error to use centroid in in a vertex shader."
2892 * From section 4.3.4 of the GLSL ES 3.00 spec:
2893 * "It is an error to use centroid in or interpolation qualifiers in
2894 * a vertex shader input."
2897 /* Section 4.3.6 of the GLSL 1.30 specification states:
2898 * "It is an error to use centroid out in a fragment shader."
2900 * The GL_ARB_shading_language_420pack extension specification states:
2901 * "It is an error to use auxiliary storage qualifiers or interpolation
2902 * qualifiers on an output in a fragment shader."
2904 if (qual
->flags
.q
.sample
&& (!is_varying_var(var
, state
->stage
) || uses_deprecated_qualifier
)) {
2905 _mesa_glsl_error(loc
, state
,
2906 "sample qualifier may only be used on `in` or `out` "
2907 "variables between shader stages");
2909 if (qual
->flags
.q
.centroid
&& !is_varying_var(var
, state
->stage
)) {
2910 _mesa_glsl_error(loc
, state
,
2911 "centroid qualifier may only be used with `in', "
2912 "`out' or `varying' variables between shader stages");
2916 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
2917 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
2918 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
2919 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
2920 * These extensions and all following extensions that add the 'layout'
2921 * keyword have been modified to require the use of 'in' or 'out'.
2923 * The following extension do not allow the deprecated keywords:
2925 * GL_AMD_conservative_depth
2926 * GL_ARB_conservative_depth
2927 * GL_ARB_gpu_shader5
2928 * GL_ARB_separate_shader_objects
2929 * GL_ARB_tessellation_shader
2930 * GL_ARB_transform_feedback3
2931 * GL_ARB_uniform_buffer_object
2933 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
2934 * allow layout with the deprecated keywords.
2936 const bool relaxed_layout_qualifier_checking
=
2937 state
->ARB_fragment_coord_conventions_enable
;
2939 if (qual
->has_layout() && uses_deprecated_qualifier
) {
2940 if (relaxed_layout_qualifier_checking
) {
2941 _mesa_glsl_warning(loc
, state
,
2942 "`layout' qualifier may not be used with "
2943 "`attribute' or `varying'");
2945 _mesa_glsl_error(loc
, state
,
2946 "`layout' qualifier may not be used with "
2947 "`attribute' or `varying'");
2951 /* Layout qualifiers for gl_FragDepth, which are enabled by extension
2952 * AMD_conservative_depth.
2954 int depth_layout_count
= qual
->flags
.q
.depth_any
2955 + qual
->flags
.q
.depth_greater
2956 + qual
->flags
.q
.depth_less
2957 + qual
->flags
.q
.depth_unchanged
;
2958 if (depth_layout_count
> 0
2959 && !state
->AMD_conservative_depth_enable
2960 && !state
->ARB_conservative_depth_enable
) {
2961 _mesa_glsl_error(loc
, state
,
2962 "extension GL_AMD_conservative_depth or "
2963 "GL_ARB_conservative_depth must be enabled "
2964 "to use depth layout qualifiers");
2965 } else if (depth_layout_count
> 0
2966 && strcmp(var
->name
, "gl_FragDepth") != 0) {
2967 _mesa_glsl_error(loc
, state
,
2968 "depth layout qualifiers can be applied only to "
2970 } else if (depth_layout_count
> 1
2971 && strcmp(var
->name
, "gl_FragDepth") == 0) {
2972 _mesa_glsl_error(loc
, state
,
2973 "at most one depth layout qualifier can be applied to "
2976 if (qual
->flags
.q
.depth_any
)
2977 var
->data
.depth_layout
= ir_depth_layout_any
;
2978 else if (qual
->flags
.q
.depth_greater
)
2979 var
->data
.depth_layout
= ir_depth_layout_greater
;
2980 else if (qual
->flags
.q
.depth_less
)
2981 var
->data
.depth_layout
= ir_depth_layout_less
;
2982 else if (qual
->flags
.q
.depth_unchanged
)
2983 var
->data
.depth_layout
= ir_depth_layout_unchanged
;
2985 var
->data
.depth_layout
= ir_depth_layout_none
;
2987 if (qual
->flags
.q
.std140
||
2988 qual
->flags
.q
.std430
||
2989 qual
->flags
.q
.packed
||
2990 qual
->flags
.q
.shared
) {
2991 _mesa_glsl_error(loc
, state
,
2992 "uniform and shader storage block layout qualifiers "
2993 "std140, std430, packed, and shared can only be "
2994 "applied to uniform or shader storage blocks, not "
2998 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
2999 validate_matrix_layout_for_type(state
, loc
, var
->type
, var
);
3002 apply_image_qualifier_to_variable(qual
, var
, state
, loc
);
3004 /* From section 4.4.1.3 of the GLSL 4.50 specification (Fragment Shader
3007 * "Fragment shaders also allow the following layout qualifier on in only
3008 * (not with variable declarations)
3009 * layout-qualifier-id
3010 * early_fragment_tests
3013 if (qual
->flags
.q
.early_fragment_tests
) {
3014 _mesa_glsl_error(loc
, state
, "early_fragment_tests layout qualifier only "
3015 "valid in fragment shader input layout declaration.");
3020 * Get the variable that is being redeclared by this declaration
3022 * Semantic checks to verify the validity of the redeclaration are also
3023 * performed. If semantic checks fail, compilation error will be emitted via
3024 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
3027 * A pointer to an existing variable in the current scope if the declaration
3028 * is a redeclaration, \c NULL otherwise.
3030 static ir_variable
*
3031 get_variable_being_redeclared(ir_variable
*var
, YYLTYPE loc
,
3032 struct _mesa_glsl_parse_state
*state
,
3033 bool allow_all_redeclarations
)
3035 /* Check if this declaration is actually a re-declaration, either to
3036 * resize an array or add qualifiers to an existing variable.
3038 * This is allowed for variables in the current scope, or when at
3039 * global scope (for built-ins in the implicit outer scope).
3041 ir_variable
*earlier
= state
->symbols
->get_variable(var
->name
);
3042 if (earlier
== NULL
||
3043 (state
->current_function
!= NULL
&&
3044 !state
->symbols
->name_declared_this_scope(var
->name
))) {
3049 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
3051 * "It is legal to declare an array without a size and then
3052 * later re-declare the same name as an array of the same
3053 * type and specify a size."
3055 if (earlier
->type
->is_unsized_array() && var
->type
->is_array()
3056 && (var
->type
->fields
.array
== earlier
->type
->fields
.array
)) {
3057 /* FINISHME: This doesn't match the qualifiers on the two
3058 * FINISHME: declarations. It's not 100% clear whether this is
3059 * FINISHME: required or not.
3062 const unsigned size
= unsigned(var
->type
->array_size());
3063 check_builtin_array_max_size(var
->name
, size
, loc
, state
);
3064 if ((size
> 0) && (size
<= earlier
->data
.max_array_access
)) {
3065 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
3067 earlier
->data
.max_array_access
);
3070 earlier
->type
= var
->type
;
3073 } else if ((state
->ARB_fragment_coord_conventions_enable
||
3074 state
->is_version(150, 0))
3075 && strcmp(var
->name
, "gl_FragCoord") == 0
3076 && earlier
->type
== var
->type
3077 && earlier
->data
.mode
== var
->data
.mode
) {
3078 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
3081 earlier
->data
.origin_upper_left
= var
->data
.origin_upper_left
;
3082 earlier
->data
.pixel_center_integer
= var
->data
.pixel_center_integer
;
3084 /* According to section 4.3.7 of the GLSL 1.30 spec,
3085 * the following built-in varaibles can be redeclared with an
3086 * interpolation qualifier:
3089 * * gl_FrontSecondaryColor
3090 * * gl_BackSecondaryColor
3092 * * gl_SecondaryColor
3094 } else if (state
->is_version(130, 0)
3095 && (strcmp(var
->name
, "gl_FrontColor") == 0
3096 || strcmp(var
->name
, "gl_BackColor") == 0
3097 || strcmp(var
->name
, "gl_FrontSecondaryColor") == 0
3098 || strcmp(var
->name
, "gl_BackSecondaryColor") == 0
3099 || strcmp(var
->name
, "gl_Color") == 0
3100 || strcmp(var
->name
, "gl_SecondaryColor") == 0)
3101 && earlier
->type
== var
->type
3102 && earlier
->data
.mode
== var
->data
.mode
) {
3103 earlier
->data
.interpolation
= var
->data
.interpolation
;
3105 /* Layout qualifiers for gl_FragDepth. */
3106 } else if ((state
->AMD_conservative_depth_enable
||
3107 state
->ARB_conservative_depth_enable
)
3108 && strcmp(var
->name
, "gl_FragDepth") == 0
3109 && earlier
->type
== var
->type
3110 && earlier
->data
.mode
== var
->data
.mode
) {
3112 /** From the AMD_conservative_depth spec:
3113 * Within any shader, the first redeclarations of gl_FragDepth
3114 * must appear before any use of gl_FragDepth.
3116 if (earlier
->data
.used
) {
3117 _mesa_glsl_error(&loc
, state
,
3118 "the first redeclaration of gl_FragDepth "
3119 "must appear before any use of gl_FragDepth");
3122 /* Prevent inconsistent redeclaration of depth layout qualifier. */
3123 if (earlier
->data
.depth_layout
!= ir_depth_layout_none
3124 && earlier
->data
.depth_layout
!= var
->data
.depth_layout
) {
3125 _mesa_glsl_error(&loc
, state
,
3126 "gl_FragDepth: depth layout is declared here "
3127 "as '%s, but it was previously declared as "
3129 depth_layout_string(var
->data
.depth_layout
),
3130 depth_layout_string(earlier
->data
.depth_layout
));
3133 earlier
->data
.depth_layout
= var
->data
.depth_layout
;
3135 } else if (allow_all_redeclarations
) {
3136 if (earlier
->data
.mode
!= var
->data
.mode
) {
3137 _mesa_glsl_error(&loc
, state
,
3138 "redeclaration of `%s' with incorrect qualifiers",
3140 } else if (earlier
->type
!= var
->type
) {
3141 _mesa_glsl_error(&loc
, state
,
3142 "redeclaration of `%s' has incorrect type",
3146 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
3153 * Generate the IR for an initializer in a variable declaration
3156 process_initializer(ir_variable
*var
, ast_declaration
*decl
,
3157 ast_fully_specified_type
*type
,
3158 exec_list
*initializer_instructions
,
3159 struct _mesa_glsl_parse_state
*state
)
3161 ir_rvalue
*result
= NULL
;
3163 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
3165 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
3167 * "All uniform variables are read-only and are initialized either
3168 * directly by an application via API commands, or indirectly by
3171 if (var
->data
.mode
== ir_var_uniform
) {
3172 state
->check_version(120, 0, &initializer_loc
,
3173 "cannot initialize uniforms");
3176 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
3178 * "Buffer variables cannot have initializers."
3180 if (var
->data
.mode
== ir_var_shader_storage
) {
3181 _mesa_glsl_error(& initializer_loc
, state
,
3182 "SSBO variables cannot have initializers");
3185 /* From section 4.1.7 of the GLSL 4.40 spec:
3187 * "Opaque variables [...] are initialized only through the
3188 * OpenGL API; they cannot be declared with an initializer in a
3191 if (var
->type
->contains_opaque()) {
3192 _mesa_glsl_error(& initializer_loc
, state
,
3193 "cannot initialize opaque variable");
3196 if ((var
->data
.mode
== ir_var_shader_in
) && (state
->current_function
== NULL
)) {
3197 _mesa_glsl_error(& initializer_loc
, state
,
3198 "cannot initialize %s shader input / %s",
3199 _mesa_shader_stage_to_string(state
->stage
),
3200 (state
->stage
== MESA_SHADER_VERTEX
)
3201 ? "attribute" : "varying");
3204 if (var
->data
.mode
== ir_var_shader_out
&& state
->current_function
== NULL
) {
3205 _mesa_glsl_error(&initializer_loc
, state
,
3206 "cannot initialize %s shader output",
3207 _mesa_shader_stage_to_string(state
->stage
));
3210 /* If the initializer is an ast_aggregate_initializer, recursively store
3211 * type information from the LHS into it, so that its hir() function can do
3214 if (decl
->initializer
->oper
== ast_aggregate
)
3215 _mesa_ast_set_aggregate_type(var
->type
, decl
->initializer
);
3217 ir_dereference
*const lhs
= new(state
) ir_dereference_variable(var
);
3218 ir_rvalue
*rhs
= decl
->initializer
->hir(initializer_instructions
, state
);
3220 /* Calculate the constant value if this is a const or uniform
3223 if (type
->qualifier
.flags
.q
.constant
3224 || type
->qualifier
.flags
.q
.uniform
) {
3225 ir_rvalue
*new_rhs
= validate_assignment(state
, initializer_loc
,
3227 if (new_rhs
!= NULL
) {
3230 ir_constant
*constant_value
= rhs
->constant_expression_value();
3231 if (!constant_value
) {
3232 /* If ARB_shading_language_420pack is enabled, initializers of
3233 * const-qualified local variables do not have to be constant
3234 * expressions. Const-qualified global variables must still be
3235 * initialized with constant expressions.
3237 if (!state
->ARB_shading_language_420pack_enable
3238 || state
->current_function
== NULL
) {
3239 _mesa_glsl_error(& initializer_loc
, state
,
3240 "initializer of %s variable `%s' must be a "
3241 "constant expression",
3242 (type
->qualifier
.flags
.q
.constant
)
3243 ? "const" : "uniform",
3245 if (var
->type
->is_numeric()) {
3246 /* Reduce cascading errors. */
3247 var
->constant_value
= ir_constant::zero(state
, var
->type
);
3251 rhs
= constant_value
;
3252 var
->constant_value
= constant_value
;
3255 if (var
->type
->is_numeric()) {
3256 /* Reduce cascading errors. */
3257 var
->constant_value
= ir_constant::zero(state
, var
->type
);
3262 if (rhs
&& !rhs
->type
->is_error()) {
3263 bool temp
= var
->data
.read_only
;
3264 if (type
->qualifier
.flags
.q
.constant
)
3265 var
->data
.read_only
= false;
3267 /* Never emit code to initialize a uniform.
3269 const glsl_type
*initializer_type
;
3270 if (!type
->qualifier
.flags
.q
.uniform
) {
3271 do_assignment(initializer_instructions
, state
,
3276 type
->get_location());
3277 initializer_type
= result
->type
;
3279 initializer_type
= rhs
->type
;
3281 var
->constant_initializer
= rhs
->constant_expression_value();
3282 var
->data
.has_initializer
= true;
3284 /* If the declared variable is an unsized array, it must inherrit
3285 * its full type from the initializer. A declaration such as
3287 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
3291 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
3293 * The assignment generated in the if-statement (below) will also
3294 * automatically handle this case for non-uniforms.
3296 * If the declared variable is not an array, the types must
3297 * already match exactly. As a result, the type assignment
3298 * here can be done unconditionally. For non-uniforms the call
3299 * to do_assignment can change the type of the initializer (via
3300 * the implicit conversion rules). For uniforms the initializer
3301 * must be a constant expression, and the type of that expression
3302 * was validated above.
3304 var
->type
= initializer_type
;
3306 var
->data
.read_only
= temp
;
3313 validate_layout_qualifier_vertex_count(struct _mesa_glsl_parse_state
*state
,
3314 YYLTYPE loc
, ir_variable
*var
,
3315 unsigned num_vertices
,
3317 const char *var_category
)
3319 if (var
->type
->is_unsized_array()) {
3320 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says:
3322 * All geometry shader input unsized array declarations will be
3323 * sized by an earlier input layout qualifier, when present, as per
3324 * the following table.
3326 * Followed by a table mapping each allowed input layout qualifier to
3327 * the corresponding input length.
3329 * Similarly for tessellation control shader outputs.
3331 if (num_vertices
!= 0)
3332 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
3335 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec
3336 * includes the following examples of compile-time errors:
3338 * // code sequence within one shader...
3339 * in vec4 Color1[]; // size unknown
3340 * ...Color1.length()...// illegal, length() unknown
3341 * in vec4 Color2[2]; // size is 2
3342 * ...Color1.length()...// illegal, Color1 still has no size
3343 * in vec4 Color3[3]; // illegal, input sizes are inconsistent
3344 * layout(lines) in; // legal, input size is 2, matching
3345 * in vec4 Color4[3]; // illegal, contradicts layout
3348 * To detect the case illustrated by Color3, we verify that the size of
3349 * an explicitly-sized array matches the size of any previously declared
3350 * explicitly-sized array. To detect the case illustrated by Color4, we
3351 * verify that the size of an explicitly-sized array is consistent with
3352 * any previously declared input layout.
3354 if (num_vertices
!= 0 && var
->type
->length
!= num_vertices
) {
3355 _mesa_glsl_error(&loc
, state
,
3356 "%s size contradicts previously declared layout "
3357 "(size is %u, but layout requires a size of %u)",
3358 var_category
, var
->type
->length
, num_vertices
);
3359 } else if (*size
!= 0 && var
->type
->length
!= *size
) {
3360 _mesa_glsl_error(&loc
, state
,
3361 "%s sizes are inconsistent (size is %u, but a "
3362 "previous declaration has size %u)",
3363 var_category
, var
->type
->length
, *size
);
3365 *size
= var
->type
->length
;
3371 handle_tess_ctrl_shader_output_decl(struct _mesa_glsl_parse_state
*state
,
3372 YYLTYPE loc
, ir_variable
*var
)
3374 unsigned num_vertices
= 0;
3376 if (state
->tcs_output_vertices_specified
) {
3377 num_vertices
= state
->out_qualifier
->vertices
;
3380 if (!var
->type
->is_array() && !var
->data
.patch
) {
3381 _mesa_glsl_error(&loc
, state
,
3382 "tessellation control shader outputs must be arrays");
3384 /* To avoid cascading failures, short circuit the checks below. */
3388 if (var
->data
.patch
)
3391 validate_layout_qualifier_vertex_count(state
, loc
, var
, num_vertices
,
3392 &state
->tcs_output_size
,
3393 "tessellation control shader output");
3397 * Do additional processing necessary for tessellation control/evaluation shader
3398 * input declarations. This covers both interface block arrays and bare input
3402 handle_tess_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
3403 YYLTYPE loc
, ir_variable
*var
)
3405 if (!var
->type
->is_array() && !var
->data
.patch
) {
3406 _mesa_glsl_error(&loc
, state
,
3407 "per-vertex tessellation shader inputs must be arrays");
3408 /* Avoid cascading failures. */
3412 if (var
->data
.patch
)
3415 /* Unsized arrays are implicitly sized to gl_MaxPatchVertices. */
3416 if (var
->type
->is_unsized_array()) {
3417 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
3418 state
->Const
.MaxPatchVertices
);
3424 * Do additional processing necessary for geometry shader input declarations
3425 * (this covers both interface blocks arrays and bare input variables).
3428 handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
3429 YYLTYPE loc
, ir_variable
*var
)
3431 unsigned num_vertices
= 0;
3433 if (state
->gs_input_prim_type_specified
) {
3434 num_vertices
= vertices_per_prim(state
->in_qualifier
->prim_type
);
3437 /* Geometry shader input variables must be arrays. Caller should have
3438 * reported an error for this.
3440 if (!var
->type
->is_array()) {
3441 assert(state
->error
);
3443 /* To avoid cascading failures, short circuit the checks below. */
3447 validate_layout_qualifier_vertex_count(state
, loc
, var
, num_vertices
,
3448 &state
->gs_input_size
,
3449 "geometry shader input");
3453 validate_identifier(const char *identifier
, YYLTYPE loc
,
3454 struct _mesa_glsl_parse_state
*state
)
3456 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
3458 * "Identifiers starting with "gl_" are reserved for use by
3459 * OpenGL, and may not be declared in a shader as either a
3460 * variable or a function."
3462 if (is_gl_identifier(identifier
)) {
3463 _mesa_glsl_error(&loc
, state
,
3464 "identifier `%s' uses reserved `gl_' prefix",
3466 } else if (strstr(identifier
, "__")) {
3467 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
3470 * "In addition, all identifiers containing two
3471 * consecutive underscores (__) are reserved as
3472 * possible future keywords."
3474 * The intention is that names containing __ are reserved for internal
3475 * use by the implementation, and names prefixed with GL_ are reserved
3476 * for use by Khronos. Names simply containing __ are dangerous to use,
3477 * but should be allowed.
3479 * A future version of the GLSL specification will clarify this.
3481 _mesa_glsl_warning(&loc
, state
,
3482 "identifier `%s' uses reserved `__' string",
3488 precision_qualifier_allowed(const glsl_type
*type
)
3490 /* Precision qualifiers apply to floating point, integer and opaque
3493 * Section 4.5.2 (Precision Qualifiers) of the GLSL 1.30 spec says:
3494 * "Any floating point or any integer declaration can have the type
3495 * preceded by one of these precision qualifiers [...] Literal
3496 * constants do not have precision qualifiers. Neither do Boolean
3499 * Section 4.5 (Precision and Precision Qualifiers) of the GLSL 1.30
3502 * "Precision qualifiers are added for code portability with OpenGL
3503 * ES, not for functionality. They have the same syntax as in OpenGL
3506 * Section 8 (Built-In Functions) of the GLSL ES 1.00 spec says:
3508 * "uniform lowp sampler2D sampler;
3511 * lowp vec4 col = texture2D (sampler, coord);
3512 * // texture2D returns lowp"
3514 * From this, we infer that GLSL 1.30 (and later) should allow precision
3515 * qualifiers on sampler types just like float and integer types.
3517 return type
->is_float()
3518 || type
->is_integer()
3519 || type
->is_record()
3520 || type
->contains_opaque();
3524 ast_declarator_list::hir(exec_list
*instructions
,
3525 struct _mesa_glsl_parse_state
*state
)
3528 const struct glsl_type
*decl_type
;
3529 const char *type_name
= NULL
;
3530 ir_rvalue
*result
= NULL
;
3531 YYLTYPE loc
= this->get_location();
3533 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
3535 * "To ensure that a particular output variable is invariant, it is
3536 * necessary to use the invariant qualifier. It can either be used to
3537 * qualify a previously declared variable as being invariant
3539 * invariant gl_Position; // make existing gl_Position be invariant"
3541 * In these cases the parser will set the 'invariant' flag in the declarator
3542 * list, and the type will be NULL.
3544 if (this->invariant
) {
3545 assert(this->type
== NULL
);
3547 if (state
->current_function
!= NULL
) {
3548 _mesa_glsl_error(& loc
, state
,
3549 "all uses of `invariant' keyword must be at global "
3553 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
3554 assert(decl
->array_specifier
== NULL
);
3555 assert(decl
->initializer
== NULL
);
3557 ir_variable
*const earlier
=
3558 state
->symbols
->get_variable(decl
->identifier
);
3559 if (earlier
== NULL
) {
3560 _mesa_glsl_error(& loc
, state
,
3561 "undeclared variable `%s' cannot be marked "
3562 "invariant", decl
->identifier
);
3563 } else if (!is_varying_var(earlier
, state
->stage
)) {
3564 _mesa_glsl_error(&loc
, state
,
3565 "`%s' cannot be marked invariant; interfaces between "
3566 "shader stages only.", decl
->identifier
);
3567 } else if (earlier
->data
.used
) {
3568 _mesa_glsl_error(& loc
, state
,
3569 "variable `%s' may not be redeclared "
3570 "`invariant' after being used",
3573 earlier
->data
.invariant
= true;
3577 /* Invariant redeclarations do not have r-values.
3582 if (this->precise
) {
3583 assert(this->type
== NULL
);
3585 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
3586 assert(decl
->array_specifier
== NULL
);
3587 assert(decl
->initializer
== NULL
);
3589 ir_variable
*const earlier
=
3590 state
->symbols
->get_variable(decl
->identifier
);
3591 if (earlier
== NULL
) {
3592 _mesa_glsl_error(& loc
, state
,
3593 "undeclared variable `%s' cannot be marked "
3594 "precise", decl
->identifier
);
3595 } else if (state
->current_function
!= NULL
&&
3596 !state
->symbols
->name_declared_this_scope(decl
->identifier
)) {
3597 /* Note: we have to check if we're in a function, since
3598 * builtins are treated as having come from another scope.
3600 _mesa_glsl_error(& loc
, state
,
3601 "variable `%s' from an outer scope may not be "
3602 "redeclared `precise' in this scope",
3604 } else if (earlier
->data
.used
) {
3605 _mesa_glsl_error(& loc
, state
,
3606 "variable `%s' may not be redeclared "
3607 "`precise' after being used",
3610 earlier
->data
.precise
= true;
3614 /* Precise redeclarations do not have r-values either. */
3618 assert(this->type
!= NULL
);
3619 assert(!this->invariant
);
3620 assert(!this->precise
);
3622 /* The type specifier may contain a structure definition. Process that
3623 * before any of the variable declarations.
3625 (void) this->type
->specifier
->hir(instructions
, state
);
3627 decl_type
= this->type
->glsl_type(& type_name
, state
);
3629 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
3630 * "Buffer variables may only be declared inside interface blocks
3631 * (section 4.3.9 “Interface Blocks”), which are then referred to as
3632 * shader storage blocks. It is a compile-time error to declare buffer
3633 * variables at global scope (outside a block)."
3635 if (type
->qualifier
.flags
.q
.buffer
&& !decl_type
->is_interface()) {
3636 _mesa_glsl_error(&loc
, state
,
3637 "buffer variables cannot be declared outside "
3638 "interface blocks");
3641 /* An offset-qualified atomic counter declaration sets the default
3642 * offset for the next declaration within the same atomic counter
3645 if (decl_type
&& decl_type
->contains_atomic()) {
3646 if (type
->qualifier
.flags
.q
.explicit_binding
&&
3647 type
->qualifier
.flags
.q
.explicit_offset
)
3648 state
->atomic_counter_offsets
[type
->qualifier
.binding
] =
3649 type
->qualifier
.offset
;
3652 if (this->declarations
.is_empty()) {
3653 /* If there is no structure involved in the program text, there are two
3654 * possible scenarios:
3656 * - The program text contained something like 'vec4;'. This is an
3657 * empty declaration. It is valid but weird. Emit a warning.
3659 * - The program text contained something like 'S;' and 'S' is not the
3660 * name of a known structure type. This is both invalid and weird.
3663 * - The program text contained something like 'mediump float;'
3664 * when the programmer probably meant 'precision mediump
3665 * float;' Emit a warning with a description of what they
3666 * probably meant to do.
3668 * Note that if decl_type is NULL and there is a structure involved,
3669 * there must have been some sort of error with the structure. In this
3670 * case we assume that an error was already generated on this line of
3671 * code for the structure. There is no need to generate an additional,
3674 assert(this->type
->specifier
->structure
== NULL
|| decl_type
!= NULL
3677 if (decl_type
== NULL
) {
3678 _mesa_glsl_error(&loc
, state
,
3679 "invalid type `%s' in empty declaration",
3681 } else if (decl_type
->base_type
== GLSL_TYPE_ATOMIC_UINT
) {
3682 /* Empty atomic counter declarations are allowed and useful
3683 * to set the default offset qualifier.
3686 } else if (this->type
->qualifier
.precision
!= ast_precision_none
) {
3687 if (this->type
->specifier
->structure
!= NULL
) {
3688 _mesa_glsl_error(&loc
, state
,
3689 "precision qualifiers can't be applied "
3692 static const char *const precision_names
[] = {
3699 _mesa_glsl_warning(&loc
, state
,
3700 "empty declaration with precision qualifier, "
3701 "to set the default precision, use "
3702 "`precision %s %s;'",
3703 precision_names
[this->type
->qualifier
.precision
],
3706 } else if (this->type
->specifier
->structure
== NULL
) {
3707 _mesa_glsl_warning(&loc
, state
, "empty declaration");
3711 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
3712 const struct glsl_type
*var_type
;
3714 const char *identifier
= decl
->identifier
;
3715 /* FINISHME: Emit a warning if a variable declaration shadows a
3716 * FINISHME: declaration at a higher scope.
3719 if ((decl_type
== NULL
) || decl_type
->is_void()) {
3720 if (type_name
!= NULL
) {
3721 _mesa_glsl_error(& loc
, state
,
3722 "invalid type `%s' in declaration of `%s'",
3723 type_name
, decl
->identifier
);
3725 _mesa_glsl_error(& loc
, state
,
3726 "invalid type in declaration of `%s'",
3732 if (this->type
->qualifier
.flags
.q
.subroutine
) {
3736 t
= state
->symbols
->get_type(this->type
->specifier
->type_name
);
3738 _mesa_glsl_error(& loc
, state
,
3739 "invalid type in declaration of `%s'",
3741 name
= ralloc_asprintf(ctx
, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state
->stage
), decl
->identifier
);
3746 var_type
= process_array_type(&loc
, decl_type
, decl
->array_specifier
,
3749 var
= new(ctx
) ir_variable(var_type
, identifier
, ir_var_auto
);
3751 /* The 'varying in' and 'varying out' qualifiers can only be used with
3752 * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
3755 if (this->type
->qualifier
.flags
.q
.varying
) {
3756 if (this->type
->qualifier
.flags
.q
.in
) {
3757 _mesa_glsl_error(& loc
, state
,
3758 "`varying in' qualifier in declaration of "
3759 "`%s' only valid for geometry shaders using "
3760 "ARB_geometry_shader4 or EXT_geometry_shader4",
3762 } else if (this->type
->qualifier
.flags
.q
.out
) {
3763 _mesa_glsl_error(& loc
, state
,
3764 "`varying out' qualifier in declaration of "
3765 "`%s' only valid for geometry shaders using "
3766 "ARB_geometry_shader4 or EXT_geometry_shader4",
3771 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
3773 * "Global variables can only use the qualifiers const,
3774 * attribute, uniform, or varying. Only one may be
3777 * Local variables can only use the qualifier const."
3779 * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
3780 * any extension that adds the 'layout' keyword.
3782 if (!state
->is_version(130, 300)
3783 && !state
->has_explicit_attrib_location()
3784 && !state
->has_separate_shader_objects()
3785 && !state
->ARB_fragment_coord_conventions_enable
) {
3786 if (this->type
->qualifier
.flags
.q
.out
) {
3787 _mesa_glsl_error(& loc
, state
,
3788 "`out' qualifier in declaration of `%s' "
3789 "only valid for function parameters in %s",
3790 decl
->identifier
, state
->get_version_string());
3792 if (this->type
->qualifier
.flags
.q
.in
) {
3793 _mesa_glsl_error(& loc
, state
,
3794 "`in' qualifier in declaration of `%s' "
3795 "only valid for function parameters in %s",
3796 decl
->identifier
, state
->get_version_string());
3798 /* FINISHME: Test for other invalid qualifiers. */
3801 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
3804 if (this->type
->qualifier
.flags
.q
.invariant
) {
3805 if (!is_varying_var(var
, state
->stage
)) {
3806 _mesa_glsl_error(&loc
, state
,
3807 "`%s' cannot be marked invariant; interfaces between "
3808 "shader stages only", var
->name
);
3812 if (state
->current_function
!= NULL
) {
3813 const char *mode
= NULL
;
3814 const char *extra
= "";
3816 /* There is no need to check for 'inout' here because the parser will
3817 * only allow that in function parameter lists.
3819 if (this->type
->qualifier
.flags
.q
.attribute
) {
3821 } else if (this->type
->qualifier
.flags
.q
.subroutine
) {
3822 mode
= "subroutine uniform";
3823 } else if (this->type
->qualifier
.flags
.q
.uniform
) {
3825 } else if (this->type
->qualifier
.flags
.q
.varying
) {
3827 } else if (this->type
->qualifier
.flags
.q
.in
) {
3829 extra
= " or in function parameter list";
3830 } else if (this->type
->qualifier
.flags
.q
.out
) {
3832 extra
= " or in function parameter list";
3836 _mesa_glsl_error(& loc
, state
,
3837 "%s variable `%s' must be declared at "
3839 mode
, var
->name
, extra
);
3841 } else if (var
->data
.mode
== ir_var_shader_in
) {
3842 var
->data
.read_only
= true;
3844 if (state
->stage
== MESA_SHADER_VERTEX
) {
3845 bool error_emitted
= false;
3847 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
3849 * "Vertex shader inputs can only be float, floating-point
3850 * vectors, matrices, signed and unsigned integers and integer
3851 * vectors. Vertex shader inputs can also form arrays of these
3852 * types, but not structures."
3854 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
3856 * "Vertex shader inputs can only be float, floating-point
3857 * vectors, matrices, signed and unsigned integers and integer
3858 * vectors. They cannot be arrays or structures."
3860 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
3862 * "The attribute qualifier can be used only with float,
3863 * floating-point vectors, and matrices. Attribute variables
3864 * cannot be declared as arrays or structures."
3866 * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
3868 * "Vertex shader inputs can only be float, floating-point
3869 * vectors, matrices, signed and unsigned integers and integer
3870 * vectors. Vertex shader inputs cannot be arrays or
3873 const glsl_type
*check_type
= var
->type
->without_array();
3875 switch (check_type
->base_type
) {
3876 case GLSL_TYPE_FLOAT
:
3878 case GLSL_TYPE_UINT
:
3880 if (state
->is_version(120, 300))
3882 case GLSL_TYPE_DOUBLE
:
3883 if (check_type
->base_type
== GLSL_TYPE_DOUBLE
&& (state
->is_version(410, 0) || state
->ARB_vertex_attrib_64bit_enable
))
3887 _mesa_glsl_error(& loc
, state
,
3888 "vertex shader input / attribute cannot have "
3890 var
->type
->is_array() ? "array of " : "",
3892 error_emitted
= true;
3895 if (!error_emitted
&& var
->type
->is_array() &&
3896 !state
->check_version(150, 0, &loc
,
3897 "vertex shader input / attribute "
3898 "cannot have array type")) {
3899 error_emitted
= true;
3901 } else if (state
->stage
== MESA_SHADER_GEOMETRY
) {
3902 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
3904 * Geometry shader input variables get the per-vertex values
3905 * written out by vertex shader output variables of the same
3906 * names. Since a geometry shader operates on a set of
3907 * vertices, each input varying variable (or input block, see
3908 * interface blocks below) needs to be declared as an array.
3910 if (!var
->type
->is_array()) {
3911 _mesa_glsl_error(&loc
, state
,
3912 "geometry shader inputs must be arrays");
3915 handle_geometry_shader_input_decl(state
, loc
, var
);
3916 } else if (state
->stage
== MESA_SHADER_FRAGMENT
) {
3917 /* From section 4.3.4 (Input Variables) of the GLSL ES 3.10 spec:
3919 * It is a compile-time error to declare a fragment shader
3920 * input with, or that contains, any of the following types:
3924 * * An array of arrays
3925 * * An array of structures
3926 * * A structure containing an array
3927 * * A structure containing a structure
3929 if (state
->es_shader
) {
3930 const glsl_type
*check_type
= var
->type
->without_array();
3931 if (check_type
->is_boolean() ||
3932 check_type
->contains_opaque()) {
3933 _mesa_glsl_error(&loc
, state
,
3934 "fragment shader input cannot have type %s",
3937 if (var
->type
->is_array() &&
3938 var
->type
->fields
.array
->is_array()) {
3939 _mesa_glsl_error(&loc
, state
,
3941 "cannot have an array of arrays",
3942 _mesa_shader_stage_to_string(state
->stage
));
3944 if (var
->type
->is_array() &&
3945 var
->type
->fields
.array
->is_record()) {
3946 _mesa_glsl_error(&loc
, state
,
3947 "fragment shader input "
3948 "cannot have an array of structs");
3950 if (var
->type
->is_record()) {
3951 for (unsigned i
= 0; i
< var
->type
->length
; i
++) {
3952 if (var
->type
->fields
.structure
[i
].type
->is_array() ||
3953 var
->type
->fields
.structure
[i
].type
->is_record())
3954 _mesa_glsl_error(&loc
, state
,
3955 "fragement shader input cannot have "
3956 "a struct that contains an "
3961 } else if (state
->stage
== MESA_SHADER_TESS_CTRL
||
3962 state
->stage
== MESA_SHADER_TESS_EVAL
) {
3963 handle_tess_shader_input_decl(state
, loc
, var
);
3965 } else if (var
->data
.mode
== ir_var_shader_out
) {
3966 const glsl_type
*check_type
= var
->type
->without_array();
3968 /* From section 4.3.6 (Output variables) of the GLSL 4.40 spec:
3970 * It is a compile-time error to declare a vertex, tessellation
3971 * evaluation, tessellation control, or geometry shader output
3972 * that contains any of the following:
3974 * * A Boolean type (bool, bvec2 ...)
3977 if (check_type
->is_boolean() || check_type
->contains_opaque())
3978 _mesa_glsl_error(&loc
, state
,
3979 "%s shader output cannot have type %s",
3980 _mesa_shader_stage_to_string(state
->stage
),
3983 /* From section 4.3.6 (Output variables) of the GLSL 4.40 spec:
3985 * It is a compile-time error to declare a fragment shader output
3986 * that contains any of the following:
3988 * * A Boolean type (bool, bvec2 ...)
3989 * * A double-precision scalar or vector (double, dvec2 ...)
3994 if (state
->stage
== MESA_SHADER_FRAGMENT
) {
3995 if (check_type
->is_record() || check_type
->is_matrix())
3996 _mesa_glsl_error(&loc
, state
,
3997 "fragment shader output "
3998 "cannot have struct or matrix type");
3999 switch (check_type
->base_type
) {
4000 case GLSL_TYPE_UINT
:
4002 case GLSL_TYPE_FLOAT
:
4005 _mesa_glsl_error(&loc
, state
,
4006 "fragment shader output cannot have "
4007 "type %s", check_type
->name
);
4011 /* From section 4.3.6 (Output Variables) of the GLSL ES 3.10 spec:
4013 * It is a compile-time error to declare a vertex shader output
4014 * with, or that contains, any of the following types:
4018 * * An array of arrays
4019 * * An array of structures
4020 * * A structure containing an array
4021 * * A structure containing a structure
4023 * It is a compile-time error to declare a fragment shader output
4024 * with, or that contains, any of the following types:
4030 * * An array of array
4032 if (state
->es_shader
) {
4033 if (var
->type
->is_array() &&
4034 var
->type
->fields
.array
->is_array()) {
4035 _mesa_glsl_error(&loc
, state
,
4037 "cannot have an array of arrays",
4038 _mesa_shader_stage_to_string(state
->stage
));
4040 if (state
->stage
== MESA_SHADER_VERTEX
) {
4041 if (var
->type
->is_array() &&
4042 var
->type
->fields
.array
->is_record()) {
4043 _mesa_glsl_error(&loc
, state
,
4044 "vertex shader output "
4045 "cannot have an array of structs");
4047 if (var
->type
->is_record()) {
4048 for (unsigned i
= 0; i
< var
->type
->length
; i
++) {
4049 if (var
->type
->fields
.structure
[i
].type
->is_array() ||
4050 var
->type
->fields
.structure
[i
].type
->is_record())
4051 _mesa_glsl_error(&loc
, state
,
4052 "vertex shader output cannot have a "
4053 "struct that contains an "
4060 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
4061 handle_tess_ctrl_shader_output_decl(state
, loc
, var
);
4063 } else if (var
->type
->contains_subroutine()) {
4064 /* declare subroutine uniforms as hidden */
4065 var
->data
.how_declared
= ir_var_hidden
;
4068 /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
4069 * so must integer vertex outputs.
4071 * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
4072 * "Fragment shader inputs that are signed or unsigned integers or
4073 * integer vectors must be qualified with the interpolation qualifier
4076 * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
4077 * "Fragment shader inputs that are, or contain, signed or unsigned
4078 * integers or integer vectors must be qualified with the
4079 * interpolation qualifier flat."
4081 * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
4082 * "Vertex shader outputs that are, or contain, signed or unsigned
4083 * integers or integer vectors must be qualified with the
4084 * interpolation qualifier flat."
4086 * Note that prior to GLSL 1.50, this requirement applied to vertex
4087 * outputs rather than fragment inputs. That creates problems in the
4088 * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
4089 * desktop GL shaders. For GLSL ES shaders, we follow the spec and
4090 * apply the restriction to both vertex outputs and fragment inputs.
4092 * Note also that the desktop GLSL specs are missing the text "or
4093 * contain"; this is presumably an oversight, since there is no
4094 * reasonable way to interpolate a fragment shader input that contains
4097 if (state
->is_version(130, 300) &&
4098 var
->type
->contains_integer() &&
4099 var
->data
.interpolation
!= INTERP_QUALIFIER_FLAT
&&
4100 ((state
->stage
== MESA_SHADER_FRAGMENT
&& var
->data
.mode
== ir_var_shader_in
)
4101 || (state
->stage
== MESA_SHADER_VERTEX
&& var
->data
.mode
== ir_var_shader_out
4102 && state
->es_shader
))) {
4103 const char *var_type
= (state
->stage
== MESA_SHADER_VERTEX
) ?
4104 "vertex output" : "fragment input";
4105 _mesa_glsl_error(&loc
, state
, "if a %s is (or contains) "
4106 "an integer, then it must be qualified with 'flat'",
4110 /* Double fragment inputs must be qualified with 'flat'. */
4111 if (var
->type
->contains_double() &&
4112 var
->data
.interpolation
!= INTERP_QUALIFIER_FLAT
&&
4113 state
->stage
== MESA_SHADER_FRAGMENT
&&
4114 var
->data
.mode
== ir_var_shader_in
) {
4115 _mesa_glsl_error(&loc
, state
, "if a fragment input is (or contains) "
4116 "a double, then it must be qualified with 'flat'",
4120 /* Interpolation qualifiers cannot be applied to 'centroid' and
4121 * 'centroid varying'.
4123 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
4124 * "interpolation qualifiers may only precede the qualifiers in,
4125 * centroid in, out, or centroid out in a declaration. They do not apply
4126 * to the deprecated storage qualifiers varying or centroid varying."
4128 * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
4130 if (state
->is_version(130, 0)
4131 && this->type
->qualifier
.has_interpolation()
4132 && this->type
->qualifier
.flags
.q
.varying
) {
4134 const char *i
= this->type
->qualifier
.interpolation_string();
4137 if (this->type
->qualifier
.flags
.q
.centroid
)
4138 s
= "centroid varying";
4142 _mesa_glsl_error(&loc
, state
,
4143 "qualifier '%s' cannot be applied to the "
4144 "deprecated storage qualifier '%s'", i
, s
);
4148 /* Interpolation qualifiers can only apply to vertex shader outputs and
4149 * fragment shader inputs.
4151 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
4152 * "Outputs from a vertex shader (out) and inputs to a fragment
4153 * shader (in) can be further qualified with one or more of these
4154 * interpolation qualifiers"
4156 * From page 31 (page 37 of the PDF) of the GLSL ES 3.00 spec:
4157 * "These interpolation qualifiers may only precede the qualifiers
4158 * in, centroid in, out, or centroid out in a declaration. They do
4159 * not apply to inputs into a vertex shader or outputs from a
4162 if (state
->is_version(130, 300)
4163 && this->type
->qualifier
.has_interpolation()) {
4165 const char *i
= this->type
->qualifier
.interpolation_string();
4168 switch (state
->stage
) {
4169 case MESA_SHADER_VERTEX
:
4170 if (this->type
->qualifier
.flags
.q
.in
) {
4171 _mesa_glsl_error(&loc
, state
,
4172 "qualifier '%s' cannot be applied to vertex "
4173 "shader inputs", i
);
4176 case MESA_SHADER_FRAGMENT
:
4177 if (this->type
->qualifier
.flags
.q
.out
) {
4178 _mesa_glsl_error(&loc
, state
,
4179 "qualifier '%s' cannot be applied to fragment "
4180 "shader outputs", i
);
4189 /* From section 4.3.4 of the GLSL 4.00 spec:
4190 * "Input variables may not be declared using the patch in qualifier
4191 * in tessellation control or geometry shaders."
4193 * From section 4.3.6 of the GLSL 4.00 spec:
4194 * "It is an error to use patch out in a vertex, tessellation
4195 * evaluation, or geometry shader."
4197 * This doesn't explicitly forbid using them in a fragment shader, but
4198 * that's probably just an oversight.
4200 if (state
->stage
!= MESA_SHADER_TESS_EVAL
4201 && this->type
->qualifier
.flags
.q
.patch
4202 && this->type
->qualifier
.flags
.q
.in
) {
4204 _mesa_glsl_error(&loc
, state
, "'patch in' can only be used in a "
4205 "tessellation evaluation shader");
4208 if (state
->stage
!= MESA_SHADER_TESS_CTRL
4209 && this->type
->qualifier
.flags
.q
.patch
4210 && this->type
->qualifier
.flags
.q
.out
) {
4212 _mesa_glsl_error(&loc
, state
, "'patch out' can only be used in a "
4213 "tessellation control shader");
4216 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
4218 if (this->type
->qualifier
.precision
!= ast_precision_none
) {
4219 state
->check_precision_qualifiers_allowed(&loc
);
4223 /* If a precision qualifier is allowed on a type, it is allowed on
4224 * an array of that type.
4226 if (!(this->type
->qualifier
.precision
== ast_precision_none
4227 || precision_qualifier_allowed(var
->type
->without_array()))) {
4229 _mesa_glsl_error(&loc
, state
,
4230 "precision qualifiers apply only to floating point"
4231 ", integer and opaque types");
4234 /* From section 4.1.7 of the GLSL 4.40 spec:
4236 * "[Opaque types] can only be declared as function
4237 * parameters or uniform-qualified variables."
4239 if (var_type
->contains_opaque() &&
4240 !this->type
->qualifier
.flags
.q
.uniform
) {
4241 _mesa_glsl_error(&loc
, state
,
4242 "opaque variables must be declared uniform");
4245 /* Process the initializer and add its instructions to a temporary
4246 * list. This list will be added to the instruction stream (below) after
4247 * the declaration is added. This is done because in some cases (such as
4248 * redeclarations) the declaration may not actually be added to the
4249 * instruction stream.
4251 exec_list initializer_instructions
;
4253 /* Examine var name here since var may get deleted in the next call */
4254 bool var_is_gl_id
= is_gl_identifier(var
->name
);
4256 ir_variable
*earlier
=
4257 get_variable_being_redeclared(var
, decl
->get_location(), state
,
4258 false /* allow_all_redeclarations */);
4259 if (earlier
!= NULL
) {
4261 earlier
->data
.how_declared
== ir_var_declared_in_block
) {
4262 _mesa_glsl_error(&loc
, state
,
4263 "`%s' has already been redeclared using "
4264 "gl_PerVertex", earlier
->name
);
4266 earlier
->data
.how_declared
= ir_var_declared_normally
;
4269 if (decl
->initializer
!= NULL
) {
4270 result
= process_initializer((earlier
== NULL
) ? var
: earlier
,
4272 &initializer_instructions
, state
);
4275 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
4277 * "It is an error to write to a const variable outside of
4278 * its declaration, so they must be initialized when
4281 if (this->type
->qualifier
.flags
.q
.constant
&& decl
->initializer
== NULL
) {
4282 _mesa_glsl_error(& loc
, state
,
4283 "const declaration of `%s' must be initialized",
4287 if (state
->es_shader
) {
4288 const glsl_type
*const t
= (earlier
== NULL
)
4289 ? var
->type
: earlier
->type
;
4291 if (t
->is_unsized_array())
4292 /* Section 10.17 of the GLSL ES 1.00 specification states that
4293 * unsized array declarations have been removed from the language.
4294 * Arrays that are sized using an initializer are still explicitly
4295 * sized. However, GLSL ES 1.00 does not allow array
4296 * initializers. That is only allowed in GLSL ES 3.00.
4298 * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
4300 * "An array type can also be formed without specifying a size
4301 * if the definition includes an initializer:
4303 * float x[] = float[2] (1.0, 2.0); // declares an array of size 2
4304 * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
4309 _mesa_glsl_error(& loc
, state
,
4310 "unsized array declarations are not allowed in "
4314 /* If the declaration is not a redeclaration, there are a few additional
4315 * semantic checks that must be applied. In addition, variable that was
4316 * created for the declaration should be added to the IR stream.
4318 if (earlier
== NULL
) {
4319 validate_identifier(decl
->identifier
, loc
, state
);
4321 /* Add the variable to the symbol table. Note that the initializer's
4322 * IR was already processed earlier (though it hasn't been emitted
4323 * yet), without the variable in scope.
4325 * This differs from most C-like languages, but it follows the GLSL
4326 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
4329 * "Within a declaration, the scope of a name starts immediately
4330 * after the initializer if present or immediately after the name
4331 * being declared if not."
4333 if (!state
->symbols
->add_variable(var
)) {
4334 YYLTYPE loc
= this->get_location();
4335 _mesa_glsl_error(&loc
, state
, "name `%s' already taken in the "
4336 "current scope", decl
->identifier
);
4340 /* Push the variable declaration to the top. It means that all the
4341 * variable declarations will appear in a funny last-to-first order,
4342 * but otherwise we run into trouble if a function is prototyped, a
4343 * global var is decled, then the function is defined with usage of
4344 * the global var. See glslparsertest's CorrectModule.frag.
4346 instructions
->push_head(var
);
4349 instructions
->append_list(&initializer_instructions
);
4353 /* Generally, variable declarations do not have r-values. However,
4354 * one is used for the declaration in
4356 * while (bool b = some_condition()) {
4360 * so we return the rvalue from the last seen declaration here.
4367 ast_parameter_declarator::hir(exec_list
*instructions
,
4368 struct _mesa_glsl_parse_state
*state
)
4371 const struct glsl_type
*type
;
4372 const char *name
= NULL
;
4373 YYLTYPE loc
= this->get_location();
4375 type
= this->type
->glsl_type(& name
, state
);
4379 _mesa_glsl_error(& loc
, state
,
4380 "invalid type `%s' in declaration of `%s'",
4381 name
, this->identifier
);
4383 _mesa_glsl_error(& loc
, state
,
4384 "invalid type in declaration of `%s'",
4388 type
= glsl_type::error_type
;
4391 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
4393 * "Functions that accept no input arguments need not use void in the
4394 * argument list because prototypes (or definitions) are required and
4395 * therefore there is no ambiguity when an empty argument list "( )" is
4396 * declared. The idiom "(void)" as a parameter list is provided for
4399 * Placing this check here prevents a void parameter being set up
4400 * for a function, which avoids tripping up checks for main taking
4401 * parameters and lookups of an unnamed symbol.
4403 if (type
->is_void()) {
4404 if (this->identifier
!= NULL
)
4405 _mesa_glsl_error(& loc
, state
,
4406 "named parameter cannot have type `void'");
4412 if (formal_parameter
&& (this->identifier
== NULL
)) {
4413 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
4417 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
4418 * call already handled the "vec4[..] foo" case.
4420 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
4422 if (!type
->is_error() && type
->is_unsized_array()) {
4423 _mesa_glsl_error(&loc
, state
, "arrays passed as parameters must have "
4425 type
= glsl_type::error_type
;
4429 ir_variable
*var
= new(ctx
)
4430 ir_variable(type
, this->identifier
, ir_var_function_in
);
4432 /* Apply any specified qualifiers to the parameter declaration. Note that
4433 * for function parameters the default mode is 'in'.
4435 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
,
4438 /* From section 4.1.7 of the GLSL 4.40 spec:
4440 * "Opaque variables cannot be treated as l-values; hence cannot
4441 * be used as out or inout function parameters, nor can they be
4444 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
4445 && type
->contains_opaque()) {
4446 _mesa_glsl_error(&loc
, state
, "out and inout parameters cannot "
4447 "contain opaque variables");
4448 type
= glsl_type::error_type
;
4451 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
4453 * "When calling a function, expressions that do not evaluate to
4454 * l-values cannot be passed to parameters declared as out or inout."
4456 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
4458 * "Other binary or unary expressions, non-dereferenced arrays,
4459 * function names, swizzles with repeated fields, and constants
4460 * cannot be l-values."
4462 * So for GLSL 1.10, passing an array as an out or inout parameter is not
4463 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
4465 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
4467 && !state
->check_version(120, 100, &loc
,
4468 "arrays cannot be out or inout parameters")) {
4469 type
= glsl_type::error_type
;
4472 instructions
->push_tail(var
);
4474 /* Parameter declarations do not have r-values.
4481 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
4483 exec_list
*ir_parameters
,
4484 _mesa_glsl_parse_state
*state
)
4486 ast_parameter_declarator
*void_param
= NULL
;
4489 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
4490 param
->formal_parameter
= formal
;
4491 param
->hir(ir_parameters
, state
);
4499 if ((void_param
!= NULL
) && (count
> 1)) {
4500 YYLTYPE loc
= void_param
->get_location();
4502 _mesa_glsl_error(& loc
, state
,
4503 "`void' parameter must be only parameter");
4509 emit_function(_mesa_glsl_parse_state
*state
, ir_function
*f
)
4511 /* IR invariants disallow function declarations or definitions
4512 * nested within other function definitions. But there is no
4513 * requirement about the relative order of function declarations
4514 * and definitions with respect to one another. So simply insert
4515 * the new ir_function block at the end of the toplevel instruction
4518 state
->toplevel_ir
->push_tail(f
);
4523 ast_function::hir(exec_list
*instructions
,
4524 struct _mesa_glsl_parse_state
*state
)
4527 ir_function
*f
= NULL
;
4528 ir_function_signature
*sig
= NULL
;
4529 exec_list hir_parameters
;
4530 YYLTYPE loc
= this->get_location();
4532 const char *const name
= identifier
;
4534 /* New functions are always added to the top-level IR instruction stream,
4535 * so this instruction list pointer is ignored. See also emit_function
4538 (void) instructions
;
4540 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
4542 * "Function declarations (prototypes) cannot occur inside of functions;
4543 * they must be at global scope, or for the built-in functions, outside
4544 * the global scope."
4546 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
4548 * "User defined functions may only be defined within the global scope."
4550 * Note that this language does not appear in GLSL 1.10.
4552 if ((state
->current_function
!= NULL
) &&
4553 state
->is_version(120, 100)) {
4554 YYLTYPE loc
= this->get_location();
4555 _mesa_glsl_error(&loc
, state
,
4556 "declaration of function `%s' not allowed within "
4557 "function body", name
);
4560 validate_identifier(name
, this->get_location(), state
);
4562 /* Convert the list of function parameters to HIR now so that they can be
4563 * used below to compare this function's signature with previously seen
4564 * signatures for functions with the same name.
4566 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
4568 & hir_parameters
, state
);
4570 const char *return_type_name
;
4571 const glsl_type
*return_type
=
4572 this->return_type
->glsl_type(& return_type_name
, state
);
4575 YYLTYPE loc
= this->get_location();
4576 _mesa_glsl_error(&loc
, state
,
4577 "function `%s' has undeclared return type `%s'",
4578 name
, return_type_name
);
4579 return_type
= glsl_type::error_type
;
4582 /* ARB_shader_subroutine states:
4583 * "Subroutine declarations cannot be prototyped. It is an error to prepend
4584 * subroutine(...) to a function declaration."
4586 if (this->return_type
->qualifier
.flags
.q
.subroutine_def
&& !is_definition
) {
4587 YYLTYPE loc
= this->get_location();
4588 _mesa_glsl_error(&loc
, state
,
4589 "function declaration `%s' cannot have subroutine prepended",
4593 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
4594 * "No qualifier is allowed on the return type of a function."
4596 if (this->return_type
->has_qualifiers()) {
4597 YYLTYPE loc
= this->get_location();
4598 _mesa_glsl_error(& loc
, state
,
4599 "function `%s' return type has qualifiers", name
);
4602 /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
4604 * "Arrays are allowed as arguments and as the return type. In both
4605 * cases, the array must be explicitly sized."
4607 if (return_type
->is_unsized_array()) {
4608 YYLTYPE loc
= this->get_location();
4609 _mesa_glsl_error(& loc
, state
,
4610 "function `%s' return type array must be explicitly "
4614 /* From section 4.1.7 of the GLSL 4.40 spec:
4616 * "[Opaque types] can only be declared as function parameters
4617 * or uniform-qualified variables."
4619 if (return_type
->contains_opaque()) {
4620 YYLTYPE loc
= this->get_location();
4621 _mesa_glsl_error(&loc
, state
,
4622 "function `%s' return type can't contain an opaque type",
4626 /* Create an ir_function if one doesn't already exist. */
4627 f
= state
->symbols
->get_function(name
);
4629 f
= new(ctx
) ir_function(name
);
4630 if (!this->return_type
->qualifier
.flags
.q
.subroutine
) {
4631 if (!state
->symbols
->add_function(f
)) {
4632 /* This function name shadows a non-function use of the same name. */
4633 YYLTYPE loc
= this->get_location();
4634 _mesa_glsl_error(&loc
, state
, "function name `%s' conflicts with "
4635 "non-function", name
);
4639 emit_function(state
, f
);
4642 /* From GLSL ES 3.0 spec, chapter 6.1 "Function Definitions", page 71:
4644 * "A shader cannot redefine or overload built-in functions."
4646 * While in GLSL ES 1.0 specification, chapter 8 "Built-in Functions":
4648 * "User code can overload the built-in functions but cannot redefine
4651 if (state
->es_shader
&& state
->language_version
>= 300) {
4652 /* Local shader has no exact candidates; check the built-ins. */
4653 _mesa_glsl_initialize_builtin_functions();
4654 if (_mesa_glsl_find_builtin_function_by_name(name
)) {
4655 YYLTYPE loc
= this->get_location();
4656 _mesa_glsl_error(& loc
, state
,
4657 "A shader cannot redefine or overload built-in "
4658 "function `%s' in GLSL ES 3.00", name
);
4663 /* Verify that this function's signature either doesn't match a previously
4664 * seen signature for a function with the same name, or, if a match is found,
4665 * that the previously seen signature does not have an associated definition.
4667 if (state
->es_shader
|| f
->has_user_signature()) {
4668 sig
= f
->exact_matching_signature(state
, &hir_parameters
);
4670 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
4671 if (badvar
!= NULL
) {
4672 YYLTYPE loc
= this->get_location();
4674 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
4675 "qualifiers don't match prototype", name
, badvar
);
4678 if (sig
->return_type
!= return_type
) {
4679 YYLTYPE loc
= this->get_location();
4681 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
4682 "match prototype", name
);
4685 if (sig
->is_defined
) {
4686 if (is_definition
) {
4687 YYLTYPE loc
= this->get_location();
4688 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
4690 /* We just encountered a prototype that exactly matches a
4691 * function that's already been defined. This is redundant,
4692 * and we should ignore it.
4700 /* Verify the return type of main() */
4701 if (strcmp(name
, "main") == 0) {
4702 if (! return_type
->is_void()) {
4703 YYLTYPE loc
= this->get_location();
4705 _mesa_glsl_error(& loc
, state
, "main() must return void");
4708 if (!hir_parameters
.is_empty()) {
4709 YYLTYPE loc
= this->get_location();
4711 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
4715 /* Finish storing the information about this new function in its signature.
4718 sig
= new(ctx
) ir_function_signature(return_type
);
4719 f
->add_signature(sig
);
4722 sig
->replace_parameters(&hir_parameters
);
4725 if (this->return_type
->qualifier
.flags
.q
.subroutine_def
) {
4728 f
->num_subroutine_types
= this->return_type
->qualifier
.subroutine_list
->declarations
.length();
4729 f
->subroutine_types
= ralloc_array(state
, const struct glsl_type
*,
4730 f
->num_subroutine_types
);
4732 foreach_list_typed(ast_declaration
, decl
, link
, &this->return_type
->qualifier
.subroutine_list
->declarations
) {
4733 const struct glsl_type
*type
;
4734 /* the subroutine type must be already declared */
4735 type
= state
->symbols
->get_type(decl
->identifier
);
4737 _mesa_glsl_error(& loc
, state
, "unknown type '%s' in subroutine function definition", decl
->identifier
);
4739 f
->subroutine_types
[idx
++] = type
;
4741 state
->subroutines
= (ir_function
**)reralloc(state
, state
->subroutines
,
4743 state
->num_subroutines
+ 1);
4744 state
->subroutines
[state
->num_subroutines
] = f
;
4745 state
->num_subroutines
++;
4749 if (this->return_type
->qualifier
.flags
.q
.subroutine
) {
4750 if (!state
->symbols
->add_type(this->identifier
, glsl_type::get_subroutine_instance(this->identifier
))) {
4751 _mesa_glsl_error(& loc
, state
, "type '%s' previously defined", this->identifier
);
4754 state
->subroutine_types
= (ir_function
**)reralloc(state
, state
->subroutine_types
,
4756 state
->num_subroutine_types
+ 1);
4757 state
->subroutine_types
[state
->num_subroutine_types
] = f
;
4758 state
->num_subroutine_types
++;
4760 f
->is_subroutine
= true;
4763 /* Function declarations (prototypes) do not have r-values.
4770 ast_function_definition::hir(exec_list
*instructions
,
4771 struct _mesa_glsl_parse_state
*state
)
4773 prototype
->is_definition
= true;
4774 prototype
->hir(instructions
, state
);
4776 ir_function_signature
*signature
= prototype
->signature
;
4777 if (signature
== NULL
)
4780 assert(state
->current_function
== NULL
);
4781 state
->current_function
= signature
;
4782 state
->found_return
= false;
4784 /* Duplicate parameters declared in the prototype as concrete variables.
4785 * Add these to the symbol table.
4787 state
->symbols
->push_scope();
4788 foreach_in_list(ir_variable
, var
, &signature
->parameters
) {
4789 assert(var
->as_variable() != NULL
);
4791 /* The only way a parameter would "exist" is if two parameters have
4794 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
4795 YYLTYPE loc
= this->get_location();
4797 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
4799 state
->symbols
->add_variable(var
);
4803 /* Convert the body of the function to HIR. */
4804 this->body
->hir(&signature
->body
, state
);
4805 signature
->is_defined
= true;
4807 state
->symbols
->pop_scope();
4809 assert(state
->current_function
== signature
);
4810 state
->current_function
= NULL
;
4812 if (!signature
->return_type
->is_void() && !state
->found_return
) {
4813 YYLTYPE loc
= this->get_location();
4814 _mesa_glsl_error(& loc
, state
, "function `%s' has non-void return type "
4815 "%s, but no return statement",
4816 signature
->function_name(),
4817 signature
->return_type
->name
);
4820 /* Function definitions do not have r-values.
4827 ast_jump_statement::hir(exec_list
*instructions
,
4828 struct _mesa_glsl_parse_state
*state
)
4835 assert(state
->current_function
);
4837 if (opt_return_value
) {
4838 ir_rvalue
*ret
= opt_return_value
->hir(instructions
, state
);
4840 /* The value of the return type can be NULL if the shader says
4841 * 'return foo();' and foo() is a function that returns void.
4843 * NOTE: The GLSL spec doesn't say that this is an error. The type
4844 * of the return value is void. If the return type of the function is
4845 * also void, then this should compile without error. Seriously.
4847 const glsl_type
*const ret_type
=
4848 (ret
== NULL
) ? glsl_type::void_type
: ret
->type
;
4850 /* Implicit conversions are not allowed for return values prior to
4851 * ARB_shading_language_420pack.
4853 if (state
->current_function
->return_type
!= ret_type
) {
4854 YYLTYPE loc
= this->get_location();
4856 if (state
->ARB_shading_language_420pack_enable
) {
4857 if (!apply_implicit_conversion(state
->current_function
->return_type
,
4859 _mesa_glsl_error(& loc
, state
,
4860 "could not implicitly convert return value "
4861 "to %s, in function `%s'",
4862 state
->current_function
->return_type
->name
,
4863 state
->current_function
->function_name());
4866 _mesa_glsl_error(& loc
, state
,
4867 "`return' with wrong type %s, in function `%s' "
4870 state
->current_function
->function_name(),
4871 state
->current_function
->return_type
->name
);
4873 } else if (state
->current_function
->return_type
->base_type
==
4875 YYLTYPE loc
= this->get_location();
4877 /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
4878 * specs add a clarification:
4880 * "A void function can only use return without a return argument, even if
4881 * the return argument has void type. Return statements only accept values:
4884 * void func2() { return func1(); } // illegal return statement"
4886 _mesa_glsl_error(& loc
, state
,
4887 "void functions can only use `return' without a "
4891 inst
= new(ctx
) ir_return(ret
);
4893 if (state
->current_function
->return_type
->base_type
!=
4895 YYLTYPE loc
= this->get_location();
4897 _mesa_glsl_error(& loc
, state
,
4898 "`return' with no value, in function %s returning "
4900 state
->current_function
->function_name());
4902 inst
= new(ctx
) ir_return
;
4905 state
->found_return
= true;
4906 instructions
->push_tail(inst
);
4911 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
4912 YYLTYPE loc
= this->get_location();
4914 _mesa_glsl_error(& loc
, state
,
4915 "`discard' may only appear in a fragment shader");
4917 instructions
->push_tail(new(ctx
) ir_discard
);
4922 if (mode
== ast_continue
&&
4923 state
->loop_nesting_ast
== NULL
) {
4924 YYLTYPE loc
= this->get_location();
4926 _mesa_glsl_error(& loc
, state
, "continue may only appear in a loop");
4927 } else if (mode
== ast_break
&&
4928 state
->loop_nesting_ast
== NULL
&&
4929 state
->switch_state
.switch_nesting_ast
== NULL
) {
4930 YYLTYPE loc
= this->get_location();
4932 _mesa_glsl_error(& loc
, state
,
4933 "break may only appear in a loop or a switch");
4935 /* For a loop, inline the for loop expression again, since we don't
4936 * know where near the end of the loop body the normal copy of it is
4937 * going to be placed. Same goes for the condition for a do-while
4940 if (state
->loop_nesting_ast
!= NULL
&&
4941 mode
== ast_continue
&& !state
->switch_state
.is_switch_innermost
) {
4942 if (state
->loop_nesting_ast
->rest_expression
) {
4943 state
->loop_nesting_ast
->rest_expression
->hir(instructions
,
4946 if (state
->loop_nesting_ast
->mode
==
4947 ast_iteration_statement::ast_do_while
) {
4948 state
->loop_nesting_ast
->condition_to_hir(instructions
, state
);
4952 if (state
->switch_state
.is_switch_innermost
&&
4953 mode
== ast_continue
) {
4954 /* Set 'continue_inside' to true. */
4955 ir_rvalue
*const true_val
= new (ctx
) ir_constant(true);
4956 ir_dereference_variable
*deref_continue_inside_var
=
4957 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
4958 instructions
->push_tail(new(ctx
) ir_assignment(deref_continue_inside_var
,
4961 /* Break out from the switch, continue for the loop will
4962 * be called right after switch. */
4963 ir_loop_jump
*const jump
=
4964 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
4965 instructions
->push_tail(jump
);
4967 } else if (state
->switch_state
.is_switch_innermost
&&
4968 mode
== ast_break
) {
4969 /* Force break out of switch by inserting a break. */
4970 ir_loop_jump
*const jump
=
4971 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
4972 instructions
->push_tail(jump
);
4974 ir_loop_jump
*const jump
=
4975 new(ctx
) ir_loop_jump((mode
== ast_break
)
4976 ? ir_loop_jump::jump_break
4977 : ir_loop_jump::jump_continue
);
4978 instructions
->push_tail(jump
);
4985 /* Jump instructions do not have r-values.
4992 ast_selection_statement::hir(exec_list
*instructions
,
4993 struct _mesa_glsl_parse_state
*state
)
4997 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
4999 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
5001 * "Any expression whose type evaluates to a Boolean can be used as the
5002 * conditional expression bool-expression. Vector types are not accepted
5003 * as the expression to if."
5005 * The checks are separated so that higher quality diagnostics can be
5006 * generated for cases where both rules are violated.
5008 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
5009 YYLTYPE loc
= this->condition
->get_location();
5011 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
5015 ir_if
*const stmt
= new(ctx
) ir_if(condition
);
5017 if (then_statement
!= NULL
) {
5018 state
->symbols
->push_scope();
5019 then_statement
->hir(& stmt
->then_instructions
, state
);
5020 state
->symbols
->pop_scope();
5023 if (else_statement
!= NULL
) {
5024 state
->symbols
->push_scope();
5025 else_statement
->hir(& stmt
->else_instructions
, state
);
5026 state
->symbols
->pop_scope();
5029 instructions
->push_tail(stmt
);
5031 /* if-statements do not have r-values.
5038 ast_switch_statement::hir(exec_list
*instructions
,
5039 struct _mesa_glsl_parse_state
*state
)
5043 ir_rvalue
*const test_expression
=
5044 this->test_expression
->hir(instructions
, state
);
5046 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
5048 * "The type of init-expression in a switch statement must be a
5051 if (!test_expression
->type
->is_scalar() ||
5052 !test_expression
->type
->is_integer()) {
5053 YYLTYPE loc
= this->test_expression
->get_location();
5055 _mesa_glsl_error(& loc
,
5057 "switch-statement expression must be scalar "
5061 /* Track the switch-statement nesting in a stack-like manner.
5063 struct glsl_switch_state saved
= state
->switch_state
;
5065 state
->switch_state
.is_switch_innermost
= true;
5066 state
->switch_state
.switch_nesting_ast
= this;
5067 state
->switch_state
.labels_ht
= hash_table_ctor(0, hash_table_pointer_hash
,
5068 hash_table_pointer_compare
);
5069 state
->switch_state
.previous_default
= NULL
;
5071 /* Initalize is_fallthru state to false.
5073 ir_rvalue
*const is_fallthru_val
= new (ctx
) ir_constant(false);
5074 state
->switch_state
.is_fallthru_var
=
5075 new(ctx
) ir_variable(glsl_type::bool_type
,
5076 "switch_is_fallthru_tmp",
5078 instructions
->push_tail(state
->switch_state
.is_fallthru_var
);
5080 ir_dereference_variable
*deref_is_fallthru_var
=
5081 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
5082 instructions
->push_tail(new(ctx
) ir_assignment(deref_is_fallthru_var
,
5085 /* Initialize continue_inside state to false.
5087 state
->switch_state
.continue_inside
=
5088 new(ctx
) ir_variable(glsl_type::bool_type
,
5089 "continue_inside_tmp",
5091 instructions
->push_tail(state
->switch_state
.continue_inside
);
5093 ir_rvalue
*const false_val
= new (ctx
) ir_constant(false);
5094 ir_dereference_variable
*deref_continue_inside_var
=
5095 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
5096 instructions
->push_tail(new(ctx
) ir_assignment(deref_continue_inside_var
,
5099 state
->switch_state
.run_default
=
5100 new(ctx
) ir_variable(glsl_type::bool_type
,
5103 instructions
->push_tail(state
->switch_state
.run_default
);
5105 /* Loop around the switch is used for flow control. */
5106 ir_loop
* loop
= new(ctx
) ir_loop();
5107 instructions
->push_tail(loop
);
5109 /* Cache test expression.
5111 test_to_hir(&loop
->body_instructions
, state
);
5113 /* Emit code for body of switch stmt.
5115 body
->hir(&loop
->body_instructions
, state
);
5117 /* Insert a break at the end to exit loop. */
5118 ir_loop_jump
*jump
= new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
5119 loop
->body_instructions
.push_tail(jump
);
5121 /* If we are inside loop, check if continue got called inside switch. */
5122 if (state
->loop_nesting_ast
!= NULL
) {
5123 ir_dereference_variable
*deref_continue_inside
=
5124 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
5125 ir_if
*irif
= new(ctx
) ir_if(deref_continue_inside
);
5126 ir_loop_jump
*jump
= new(ctx
) ir_loop_jump(ir_loop_jump::jump_continue
);
5128 if (state
->loop_nesting_ast
!= NULL
) {
5129 if (state
->loop_nesting_ast
->rest_expression
) {
5130 state
->loop_nesting_ast
->rest_expression
->hir(&irif
->then_instructions
,
5133 if (state
->loop_nesting_ast
->mode
==
5134 ast_iteration_statement::ast_do_while
) {
5135 state
->loop_nesting_ast
->condition_to_hir(&irif
->then_instructions
, state
);
5138 irif
->then_instructions
.push_tail(jump
);
5139 instructions
->push_tail(irif
);
5142 hash_table_dtor(state
->switch_state
.labels_ht
);
5144 state
->switch_state
= saved
;
5146 /* Switch statements do not have r-values. */
5152 ast_switch_statement::test_to_hir(exec_list
*instructions
,
5153 struct _mesa_glsl_parse_state
*state
)
5157 /* Cache value of test expression. */
5158 ir_rvalue
*const test_val
=
5159 test_expression
->hir(instructions
,
5162 state
->switch_state
.test_var
= new(ctx
) ir_variable(test_val
->type
,
5165 ir_dereference_variable
*deref_test_var
=
5166 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
5168 instructions
->push_tail(state
->switch_state
.test_var
);
5169 instructions
->push_tail(new(ctx
) ir_assignment(deref_test_var
, test_val
));
5174 ast_switch_body::hir(exec_list
*instructions
,
5175 struct _mesa_glsl_parse_state
*state
)
5178 stmts
->hir(instructions
, state
);
5180 /* Switch bodies do not have r-values. */
5185 ast_case_statement_list::hir(exec_list
*instructions
,
5186 struct _mesa_glsl_parse_state
*state
)
5188 exec_list default_case
, after_default
, tmp
;
5190 foreach_list_typed (ast_case_statement
, case_stmt
, link
, & this->cases
) {
5191 case_stmt
->hir(&tmp
, state
);
5194 if (state
->switch_state
.previous_default
&& default_case
.is_empty()) {
5195 default_case
.append_list(&tmp
);
5199 /* If default case found, append 'after_default' list. */
5200 if (!default_case
.is_empty())
5201 after_default
.append_list(&tmp
);
5203 instructions
->append_list(&tmp
);
5206 /* Handle the default case. This is done here because default might not be
5207 * the last case. We need to add checks against following cases first to see
5208 * if default should be chosen or not.
5210 if (!default_case
.is_empty()) {
5212 ir_rvalue
*const true_val
= new (state
) ir_constant(true);
5213 ir_dereference_variable
*deref_run_default_var
=
5214 new(state
) ir_dereference_variable(state
->switch_state
.run_default
);
5216 /* Choose to run default case initially, following conditional
5217 * assignments might change this.
5219 ir_assignment
*const init_var
=
5220 new(state
) ir_assignment(deref_run_default_var
, true_val
);
5221 instructions
->push_tail(init_var
);
5223 /* Default case was the last one, no checks required. */
5224 if (after_default
.is_empty()) {
5225 instructions
->append_list(&default_case
);
5229 foreach_in_list(ir_instruction
, ir
, &after_default
) {
5230 ir_assignment
*assign
= ir
->as_assignment();
5235 /* Clone the check between case label and init expression. */
5236 ir_expression
*exp
= (ir_expression
*) assign
->condition
;
5237 ir_expression
*clone
= exp
->clone(state
, NULL
);
5239 ir_dereference_variable
*deref_var
=
5240 new(state
) ir_dereference_variable(state
->switch_state
.run_default
);
5241 ir_rvalue
*const false_val
= new (state
) ir_constant(false);
5243 ir_assignment
*const set_false
=
5244 new(state
) ir_assignment(deref_var
, false_val
, clone
);
5246 instructions
->push_tail(set_false
);
5249 /* Append default case and all cases after it. */
5250 instructions
->append_list(&default_case
);
5251 instructions
->append_list(&after_default
);
5254 /* Case statements do not have r-values. */
5259 ast_case_statement::hir(exec_list
*instructions
,
5260 struct _mesa_glsl_parse_state
*state
)
5262 labels
->hir(instructions
, state
);
5264 /* Guard case statements depending on fallthru state. */
5265 ir_dereference_variable
*const deref_fallthru_guard
=
5266 new(state
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
5267 ir_if
*const test_fallthru
= new(state
) ir_if(deref_fallthru_guard
);
5269 foreach_list_typed (ast_node
, stmt
, link
, & this->stmts
)
5270 stmt
->hir(& test_fallthru
->then_instructions
, state
);
5272 instructions
->push_tail(test_fallthru
);
5274 /* Case statements do not have r-values. */
5280 ast_case_label_list::hir(exec_list
*instructions
,
5281 struct _mesa_glsl_parse_state
*state
)
5283 foreach_list_typed (ast_case_label
, label
, link
, & this->labels
)
5284 label
->hir(instructions
, state
);
5286 /* Case labels do not have r-values. */
5291 ast_case_label::hir(exec_list
*instructions
,
5292 struct _mesa_glsl_parse_state
*state
)
5296 ir_dereference_variable
*deref_fallthru_var
=
5297 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
5299 ir_rvalue
*const true_val
= new(ctx
) ir_constant(true);
5301 /* If not default case, ... */
5302 if (this->test_value
!= NULL
) {
5303 /* Conditionally set fallthru state based on
5304 * comparison of cached test expression value to case label.
5306 ir_rvalue
*const label_rval
= this->test_value
->hir(instructions
, state
);
5307 ir_constant
*label_const
= label_rval
->constant_expression_value();
5310 YYLTYPE loc
= this->test_value
->get_location();
5312 _mesa_glsl_error(& loc
, state
,
5313 "switch statement case label must be a "
5314 "constant expression");
5316 /* Stuff a dummy value in to allow processing to continue. */
5317 label_const
= new(ctx
) ir_constant(0);
5319 ast_expression
*previous_label
= (ast_expression
*)
5320 hash_table_find(state
->switch_state
.labels_ht
,
5321 (void *)(uintptr_t)label_const
->value
.u
[0]);
5323 if (previous_label
) {
5324 YYLTYPE loc
= this->test_value
->get_location();
5325 _mesa_glsl_error(& loc
, state
, "duplicate case value");
5327 loc
= previous_label
->get_location();
5328 _mesa_glsl_error(& loc
, state
, "this is the previous case label");
5330 hash_table_insert(state
->switch_state
.labels_ht
,
5332 (void *)(uintptr_t)label_const
->value
.u
[0]);
5336 ir_dereference_variable
*deref_test_var
=
5337 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
5339 ir_expression
*test_cond
= new(ctx
) ir_expression(ir_binop_all_equal
,
5344 * From GLSL 4.40 specification section 6.2 ("Selection"):
5346 * "The type of the init-expression value in a switch statement must
5347 * be a scalar int or uint. The type of the constant-expression value
5348 * in a case label also must be a scalar int or uint. When any pair
5349 * of these values is tested for "equal value" and the types do not
5350 * match, an implicit conversion will be done to convert the int to a
5351 * uint (see section 4.1.10 “Implicit Conversions”) before the compare
5354 if (label_const
->type
!= state
->switch_state
.test_var
->type
) {
5355 YYLTYPE loc
= this->test_value
->get_location();
5357 const glsl_type
*type_a
= label_const
->type
;
5358 const glsl_type
*type_b
= state
->switch_state
.test_var
->type
;
5360 /* Check if int->uint implicit conversion is supported. */
5361 bool integer_conversion_supported
=
5362 glsl_type::int_type
->can_implicitly_convert_to(glsl_type::uint_type
,
5365 if ((!type_a
->is_integer() || !type_b
->is_integer()) ||
5366 !integer_conversion_supported
) {
5367 _mesa_glsl_error(&loc
, state
, "type mismatch with switch "
5368 "init-expression and case label (%s != %s)",
5369 type_a
->name
, type_b
->name
);
5371 /* Conversion of the case label. */
5372 if (type_a
->base_type
== GLSL_TYPE_INT
) {
5373 if (!apply_implicit_conversion(glsl_type::uint_type
,
5374 test_cond
->operands
[0], state
))
5375 _mesa_glsl_error(&loc
, state
, "implicit type conversion error");
5377 /* Conversion of the init-expression value. */
5378 if (!apply_implicit_conversion(glsl_type::uint_type
,
5379 test_cond
->operands
[1], state
))
5380 _mesa_glsl_error(&loc
, state
, "implicit type conversion error");
5385 ir_assignment
*set_fallthru_on_test
=
5386 new(ctx
) ir_assignment(deref_fallthru_var
, true_val
, test_cond
);
5388 instructions
->push_tail(set_fallthru_on_test
);
5389 } else { /* default case */
5390 if (state
->switch_state
.previous_default
) {
5391 YYLTYPE loc
= this->get_location();
5392 _mesa_glsl_error(& loc
, state
,
5393 "multiple default labels in one switch");
5395 loc
= state
->switch_state
.previous_default
->get_location();
5396 _mesa_glsl_error(& loc
, state
, "this is the first default label");
5398 state
->switch_state
.previous_default
= this;
5400 /* Set fallthru condition on 'run_default' bool. */
5401 ir_dereference_variable
*deref_run_default
=
5402 new(ctx
) ir_dereference_variable(state
->switch_state
.run_default
);
5403 ir_rvalue
*const cond_true
= new(ctx
) ir_constant(true);
5404 ir_expression
*test_cond
= new(ctx
) ir_expression(ir_binop_all_equal
,
5408 /* Set falltrhu state. */
5409 ir_assignment
*set_fallthru
=
5410 new(ctx
) ir_assignment(deref_fallthru_var
, true_val
, test_cond
);
5412 instructions
->push_tail(set_fallthru
);
5415 /* Case statements do not have r-values. */
5420 ast_iteration_statement::condition_to_hir(exec_list
*instructions
,
5421 struct _mesa_glsl_parse_state
*state
)
5425 if (condition
!= NULL
) {
5426 ir_rvalue
*const cond
=
5427 condition
->hir(instructions
, state
);
5430 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
5431 YYLTYPE loc
= condition
->get_location();
5433 _mesa_glsl_error(& loc
, state
,
5434 "loop condition must be scalar boolean");
5436 /* As the first code in the loop body, generate a block that looks
5437 * like 'if (!condition) break;' as the loop termination condition.
5439 ir_rvalue
*const not_cond
=
5440 new(ctx
) ir_expression(ir_unop_logic_not
, cond
);
5442 ir_if
*const if_stmt
= new(ctx
) ir_if(not_cond
);
5444 ir_jump
*const break_stmt
=
5445 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
5447 if_stmt
->then_instructions
.push_tail(break_stmt
);
5448 instructions
->push_tail(if_stmt
);
5455 ast_iteration_statement::hir(exec_list
*instructions
,
5456 struct _mesa_glsl_parse_state
*state
)
5460 /* For-loops and while-loops start a new scope, but do-while loops do not.
5462 if (mode
!= ast_do_while
)
5463 state
->symbols
->push_scope();
5465 if (init_statement
!= NULL
)
5466 init_statement
->hir(instructions
, state
);
5468 ir_loop
*const stmt
= new(ctx
) ir_loop();
5469 instructions
->push_tail(stmt
);
5471 /* Track the current loop nesting. */
5472 ast_iteration_statement
*nesting_ast
= state
->loop_nesting_ast
;
5474 state
->loop_nesting_ast
= this;
5476 /* Likewise, indicate that following code is closest to a loop,
5477 * NOT closest to a switch.
5479 bool saved_is_switch_innermost
= state
->switch_state
.is_switch_innermost
;
5480 state
->switch_state
.is_switch_innermost
= false;
5482 if (mode
!= ast_do_while
)
5483 condition_to_hir(&stmt
->body_instructions
, state
);
5486 body
->hir(& stmt
->body_instructions
, state
);
5488 if (rest_expression
!= NULL
)
5489 rest_expression
->hir(& stmt
->body_instructions
, state
);
5491 if (mode
== ast_do_while
)
5492 condition_to_hir(&stmt
->body_instructions
, state
);
5494 if (mode
!= ast_do_while
)
5495 state
->symbols
->pop_scope();
5497 /* Restore previous nesting before returning. */
5498 state
->loop_nesting_ast
= nesting_ast
;
5499 state
->switch_state
.is_switch_innermost
= saved_is_switch_innermost
;
5501 /* Loops do not have r-values.
5508 * Determine if the given type is valid for establishing a default precision
5511 * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
5513 * "The precision statement
5515 * precision precision-qualifier type;
5517 * can be used to establish a default precision qualifier. The type field
5518 * can be either int or float or any of the sampler types, and the
5519 * precision-qualifier can be lowp, mediump, or highp."
5521 * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
5522 * qualifiers on sampler types, but this seems like an oversight (since the
5523 * intention of including these in GLSL 1.30 is to allow compatibility with ES
5524 * shaders). So we allow int, float, and all sampler types regardless of GLSL
5528 is_valid_default_precision_type(const struct glsl_type
*const type
)
5533 switch (type
->base_type
) {
5535 case GLSL_TYPE_FLOAT
:
5536 /* "int" and "float" are valid, but vectors and matrices are not. */
5537 return type
->vector_elements
== 1 && type
->matrix_columns
== 1;
5538 case GLSL_TYPE_SAMPLER
:
5539 case GLSL_TYPE_IMAGE
:
5540 case GLSL_TYPE_ATOMIC_UINT
:
5549 ast_type_specifier::hir(exec_list
*instructions
,
5550 struct _mesa_glsl_parse_state
*state
)
5552 if (this->default_precision
== ast_precision_none
&& this->structure
== NULL
)
5555 YYLTYPE loc
= this->get_location();
5557 /* If this is a precision statement, check that the type to which it is
5558 * applied is either float or int.
5560 * From section 4.5.3 of the GLSL 1.30 spec:
5561 * "The precision statement
5562 * precision precision-qualifier type;
5563 * can be used to establish a default precision qualifier. The type
5564 * field can be either int or float [...]. Any other types or
5565 * qualifiers will result in an error.
5567 if (this->default_precision
!= ast_precision_none
) {
5568 if (!state
->check_precision_qualifiers_allowed(&loc
))
5571 if (this->structure
!= NULL
) {
5572 _mesa_glsl_error(&loc
, state
,
5573 "precision qualifiers do not apply to structures");
5577 if (this->array_specifier
!= NULL
) {
5578 _mesa_glsl_error(&loc
, state
,
5579 "default precision statements do not apply to "
5584 const struct glsl_type
*const type
=
5585 state
->symbols
->get_type(this->type_name
);
5586 if (!is_valid_default_precision_type(type
)) {
5587 _mesa_glsl_error(&loc
, state
,
5588 "default precision statements apply only to "
5589 "float, int, and opaque types");
5593 if (type
->base_type
== GLSL_TYPE_FLOAT
5595 && state
->stage
== MESA_SHADER_FRAGMENT
) {
5596 /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
5599 * "The fragment language has no default precision qualifier for
5600 * floating point types."
5602 * As a result, we have to track whether or not default precision has
5603 * been specified for float in GLSL ES fragment shaders.
5605 * Earlier in that same section, the spec says:
5607 * "Non-precision qualified declarations will use the precision
5608 * qualifier specified in the most recent precision statement
5609 * that is still in scope. The precision statement has the same
5610 * scoping rules as variable declarations. If it is declared
5611 * inside a compound statement, its effect stops at the end of
5612 * the innermost statement it was declared in. Precision
5613 * statements in nested scopes override precision statements in
5614 * outer scopes. Multiple precision statements for the same basic
5615 * type can appear inside the same scope, with later statements
5616 * overriding earlier statements within that scope."
5618 * Default precision specifications follow the same scope rules as
5619 * variables. So, we can track the state of the default float
5620 * precision in the symbol table, and the rules will just work. This
5621 * is a slight abuse of the symbol table, but it has the semantics
5624 ir_variable
*const junk
=
5625 new(state
) ir_variable(type
, "#default precision",
5628 state
->symbols
->add_variable(junk
);
5631 /* FINISHME: Translate precision statements into IR. */
5635 /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
5636 * process_record_constructor() can do type-checking on C-style initializer
5637 * expressions of structs, but ast_struct_specifier should only be translated
5638 * to HIR if it is declaring the type of a structure.
5640 * The ->is_declaration field is false for initializers of variables
5641 * declared separately from the struct's type definition.
5643 * struct S { ... }; (is_declaration = true)
5644 * struct T { ... } t = { ... }; (is_declaration = true)
5645 * S s = { ... }; (is_declaration = false)
5647 if (this->structure
!= NULL
&& this->structure
->is_declaration
)
5648 return this->structure
->hir(instructions
, state
);
5655 * Process a structure or interface block tree into an array of structure fields
5657 * After parsing, where there are some syntax differnces, structures and
5658 * interface blocks are almost identical. They are similar enough that the
5659 * AST for each can be processed the same way into a set of
5660 * \c glsl_struct_field to describe the members.
5662 * If we're processing an interface block, var_mode should be the type of the
5663 * interface block (ir_var_shader_in, ir_var_shader_out, ir_var_uniform or
5664 * ir_var_shader_storage). If we're processing a structure, var_mode should be
5668 * The number of fields processed. A pointer to the array structure fields is
5669 * stored in \c *fields_ret.
5672 ast_process_structure_or_interface_block(exec_list
*instructions
,
5673 struct _mesa_glsl_parse_state
*state
,
5674 exec_list
*declarations
,
5676 glsl_struct_field
**fields_ret
,
5678 enum glsl_matrix_layout matrix_layout
,
5679 bool allow_reserved_names
,
5680 ir_variable_mode var_mode
,
5681 ast_type_qualifier
*layout
)
5683 unsigned decl_count
= 0;
5685 /* For blocks that accept memory qualifiers (i.e. shader storage), verify
5686 * that we don't have incompatible qualifiers
5688 if (layout
&& layout
->flags
.q
.read_only
&& layout
->flags
.q
.write_only
) {
5689 _mesa_glsl_error(&loc
, state
,
5690 "Interface block sets both readonly and writeonly");
5693 /* Make an initial pass over the list of fields to determine how
5694 * many there are. Each element in this list is an ast_declarator_list.
5695 * This means that we actually need to count the number of elements in the
5696 * 'declarations' list in each of the elements.
5698 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
5699 decl_count
+= decl_list
->declarations
.length();
5702 /* Allocate storage for the fields and process the field
5703 * declarations. As the declarations are processed, try to also convert
5704 * the types to HIR. This ensures that structure definitions embedded in
5705 * other structure definitions or in interface blocks are processed.
5707 glsl_struct_field
*const fields
= ralloc_array(state
, glsl_struct_field
,
5711 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
5712 const char *type_name
;
5714 decl_list
->type
->specifier
->hir(instructions
, state
);
5716 /* Section 10.9 of the GLSL ES 1.00 specification states that
5717 * embedded structure definitions have been removed from the language.
5719 if (state
->es_shader
&& decl_list
->type
->specifier
->structure
!= NULL
) {
5720 _mesa_glsl_error(&loc
, state
, "embedded structure definitions are "
5721 "not allowed in GLSL ES 1.00");
5724 const glsl_type
*decl_type
=
5725 decl_list
->type
->glsl_type(& type_name
, state
);
5727 foreach_list_typed (ast_declaration
, decl
, link
,
5728 &decl_list
->declarations
) {
5729 if (!allow_reserved_names
)
5730 validate_identifier(decl
->identifier
, loc
, state
);
5732 /* From section 4.3.9 of the GLSL 4.40 spec:
5734 * "[In interface blocks] opaque types are not allowed."
5736 * It should be impossible for decl_type to be NULL here. Cases that
5737 * might naturally lead to decl_type being NULL, especially for the
5738 * is_interface case, will have resulted in compilation having
5739 * already halted due to a syntax error.
5743 if (is_interface
&& decl_type
->contains_opaque()) {
5744 YYLTYPE loc
= decl_list
->get_location();
5745 _mesa_glsl_error(&loc
, state
,
5746 "uniform/buffer in non-default interface block contains "
5750 if (decl_type
->contains_atomic()) {
5751 /* From section 4.1.7.3 of the GLSL 4.40 spec:
5753 * "Members of structures cannot be declared as atomic counter
5756 YYLTYPE loc
= decl_list
->get_location();
5757 _mesa_glsl_error(&loc
, state
, "atomic counter in structure, "
5758 "shader storage block or uniform block");
5761 if (decl_type
->contains_image()) {
5762 /* FINISHME: Same problem as with atomic counters.
5763 * FINISHME: Request clarification from Khronos and add
5764 * FINISHME: spec quotation here.
5766 YYLTYPE loc
= decl_list
->get_location();
5767 _mesa_glsl_error(&loc
, state
,
5768 "image in structure, shader storage block or "
5772 const struct ast_type_qualifier
*const qual
=
5773 & decl_list
->type
->qualifier
;
5775 if (qual
->flags
.q
.explicit_binding
)
5776 validate_binding_qualifier(state
, &loc
, decl_type
, qual
);
5778 if (qual
->flags
.q
.std140
||
5779 qual
->flags
.q
.std430
||
5780 qual
->flags
.q
.packed
||
5781 qual
->flags
.q
.shared
) {
5782 _mesa_glsl_error(&loc
, state
,
5783 "uniform/shader storage block layout qualifiers "
5784 "std140, std430, packed, and shared can only be "
5785 "applied to uniform/shader storage blocks, not "
5789 if (qual
->flags
.q
.constant
) {
5790 YYLTYPE loc
= decl_list
->get_location();
5791 _mesa_glsl_error(&loc
, state
,
5792 "const storage qualifier cannot be applied "
5793 "to struct or interface block members");
5796 const struct glsl_type
*field_type
=
5797 process_array_type(&loc
, decl_type
, decl
->array_specifier
, state
);
5798 fields
[i
].type
= field_type
;
5799 fields
[i
].name
= decl
->identifier
;
5800 fields
[i
].location
= -1;
5801 fields
[i
].interpolation
=
5802 interpret_interpolation_qualifier(qual
, var_mode
, state
, &loc
);
5803 fields
[i
].centroid
= qual
->flags
.q
.centroid
? 1 : 0;
5804 fields
[i
].sample
= qual
->flags
.q
.sample
? 1 : 0;
5805 fields
[i
].patch
= qual
->flags
.q
.patch
? 1 : 0;
5807 /* Only save explicitly defined streams in block's field */
5808 fields
[i
].stream
= qual
->flags
.q
.explicit_stream
? qual
->stream
: -1;
5810 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
5811 if (!qual
->flags
.q
.uniform
&& !qual
->flags
.q
.buffer
) {
5812 _mesa_glsl_error(&loc
, state
,
5813 "row_major and column_major can only be "
5814 "applied to interface blocks");
5816 validate_matrix_layout_for_type(state
, &loc
, field_type
, NULL
);
5819 if (qual
->flags
.q
.uniform
&& qual
->has_interpolation()) {
5820 _mesa_glsl_error(&loc
, state
,
5821 "interpolation qualifiers cannot be used "
5822 "with uniform interface blocks");
5825 if ((qual
->flags
.q
.uniform
|| !is_interface
) &&
5826 qual
->has_auxiliary_storage()) {
5827 _mesa_glsl_error(&loc
, state
,
5828 "auxiliary storage qualifiers cannot be used "
5829 "in uniform blocks or structures.");
5832 /* Propogate row- / column-major information down the fields of the
5833 * structure or interface block. Structures need this data because
5834 * the structure may contain a structure that contains ... a matrix
5835 * that need the proper layout.
5837 if (field_type
->without_array()->is_matrix()
5838 || field_type
->without_array()->is_record()) {
5839 /* If no layout is specified for the field, inherit the layout
5842 fields
[i
].matrix_layout
= matrix_layout
;
5844 if (qual
->flags
.q
.row_major
)
5845 fields
[i
].matrix_layout
= GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
5846 else if (qual
->flags
.q
.column_major
)
5847 fields
[i
].matrix_layout
= GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
;
5849 /* If we're processing an interface block, the matrix layout must
5850 * be decided by this point.
5852 assert(!is_interface
5853 || fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
5854 || fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
);
5857 /* Image qualifiers are allowed on buffer variables, which can only
5858 * be defined inside shader storage buffer objects
5860 if (layout
&& var_mode
== ir_var_shader_storage
) {
5861 if (qual
->flags
.q
.read_only
&& qual
->flags
.q
.write_only
) {
5862 _mesa_glsl_error(&loc
, state
,
5863 "buffer variable `%s' can't be "
5864 "readonly and writeonly.", fields
[i
].name
);
5867 /* For readonly and writeonly qualifiers the field definition,
5868 * if set, overwrites the layout qualifier.
5870 bool read_only
= layout
->flags
.q
.read_only
;
5871 bool write_only
= layout
->flags
.q
.write_only
;
5873 if (qual
->flags
.q
.read_only
) {
5876 } else if (qual
->flags
.q
.write_only
) {
5881 fields
[i
].image_read_only
= read_only
;
5882 fields
[i
].image_write_only
= write_only
;
5884 /* For other qualifiers, we set the flag if either the layout
5885 * qualifier or the field qualifier are set
5887 fields
[i
].image_coherent
= qual
->flags
.q
.coherent
||
5888 layout
->flags
.q
.coherent
;
5889 fields
[i
].image_volatile
= qual
->flags
.q
._volatile
||
5890 layout
->flags
.q
._volatile
;
5891 fields
[i
].image_restrict
= qual
->flags
.q
.restrict_flag
||
5892 layout
->flags
.q
.restrict_flag
;
5899 assert(i
== decl_count
);
5901 *fields_ret
= fields
;
5907 ast_struct_specifier::hir(exec_list
*instructions
,
5908 struct _mesa_glsl_parse_state
*state
)
5910 YYLTYPE loc
= this->get_location();
5912 /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
5914 * "Anonymous structures are not supported; so embedded structures must
5915 * have a declarator. A name given to an embedded struct is scoped at
5916 * the same level as the struct it is embedded in."
5918 * The same section of the GLSL 1.20 spec says:
5920 * "Anonymous structures are not supported. Embedded structures are not
5923 * struct S { float f; };
5925 * S; // Error: anonymous structures disallowed
5926 * struct { ... }; // Error: embedded structures disallowed
5927 * S s; // Okay: nested structures with name are allowed
5930 * The GLSL ES 1.00 and 3.00 specs have similar langauge and examples. So,
5931 * we allow embedded structures in 1.10 only.
5933 if (state
->language_version
!= 110 && state
->struct_specifier_depth
!= 0)
5934 _mesa_glsl_error(&loc
, state
,
5935 "embedded structure declarations are not allowed");
5937 state
->struct_specifier_depth
++;
5939 glsl_struct_field
*fields
;
5940 unsigned decl_count
=
5941 ast_process_structure_or_interface_block(instructions
,
5943 &this->declarations
,
5947 GLSL_MATRIX_LAYOUT_INHERITED
,
5948 false /* allow_reserved_names */,
5952 validate_identifier(this->name
, loc
, state
);
5954 const glsl_type
*t
=
5955 glsl_type::get_record_instance(fields
, decl_count
, this->name
);
5957 if (!state
->symbols
->add_type(name
, t
)) {
5958 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
5960 const glsl_type
**s
= reralloc(state
, state
->user_structures
,
5962 state
->num_user_structures
+ 1);
5964 s
[state
->num_user_structures
] = t
;
5965 state
->user_structures
= s
;
5966 state
->num_user_structures
++;
5970 state
->struct_specifier_depth
--;
5972 /* Structure type definitions do not have r-values.
5979 * Visitor class which detects whether a given interface block has been used.
5981 class interface_block_usage_visitor
: public ir_hierarchical_visitor
5984 interface_block_usage_visitor(ir_variable_mode mode
, const glsl_type
*block
)
5985 : mode(mode
), block(block
), found(false)
5989 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
5991 if (ir
->var
->data
.mode
== mode
&& ir
->var
->get_interface_type() == block
) {
5995 return visit_continue
;
5998 bool usage_found() const
6004 ir_variable_mode mode
;
6005 const glsl_type
*block
;
6010 is_unsized_array_last_element(ir_variable
*v
)
6012 const glsl_type
*interface_type
= v
->get_interface_type();
6013 int length
= interface_type
->length
;
6015 assert(v
->type
->is_unsized_array());
6017 /* Check if it is the last element of the interface */
6018 if (strcmp(interface_type
->fields
.structure
[length
-1].name
, v
->name
) == 0)
6024 ast_interface_block::hir(exec_list
*instructions
,
6025 struct _mesa_glsl_parse_state
*state
)
6027 YYLTYPE loc
= this->get_location();
6029 /* Interface blocks must be declared at global scope */
6030 if (state
->current_function
!= NULL
) {
6031 _mesa_glsl_error(&loc
, state
,
6032 "Interface block `%s' must be declared "
6037 if (!this->layout
.flags
.q
.buffer
&&
6038 this->layout
.flags
.q
.std430
) {
6039 _mesa_glsl_error(&loc
, state
,
6040 "std430 storage block layout qualifier is supported "
6041 "only for shader storage blocks");
6044 /* The ast_interface_block has a list of ast_declarator_lists. We
6045 * need to turn those into ir_variables with an association
6046 * with this uniform block.
6048 enum glsl_interface_packing packing
;
6049 if (this->layout
.flags
.q
.shared
) {
6050 packing
= GLSL_INTERFACE_PACKING_SHARED
;
6051 } else if (this->layout
.flags
.q
.packed
) {
6052 packing
= GLSL_INTERFACE_PACKING_PACKED
;
6053 } else if (this->layout
.flags
.q
.std430
) {
6054 packing
= GLSL_INTERFACE_PACKING_STD430
;
6056 /* The default layout is std140.
6058 packing
= GLSL_INTERFACE_PACKING_STD140
;
6061 ir_variable_mode var_mode
;
6062 const char *iface_type_name
;
6063 if (this->layout
.flags
.q
.in
) {
6064 var_mode
= ir_var_shader_in
;
6065 iface_type_name
= "in";
6066 } else if (this->layout
.flags
.q
.out
) {
6067 var_mode
= ir_var_shader_out
;
6068 iface_type_name
= "out";
6069 } else if (this->layout
.flags
.q
.uniform
) {
6070 var_mode
= ir_var_uniform
;
6071 iface_type_name
= "uniform";
6072 } else if (this->layout
.flags
.q
.buffer
) {
6073 var_mode
= ir_var_shader_storage
;
6074 iface_type_name
= "buffer";
6076 var_mode
= ir_var_auto
;
6077 iface_type_name
= "UNKNOWN";
6078 assert(!"interface block layout qualifier not found!");
6081 enum glsl_matrix_layout matrix_layout
= GLSL_MATRIX_LAYOUT_INHERITED
;
6082 if (this->layout
.flags
.q
.row_major
)
6083 matrix_layout
= GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
6084 else if (this->layout
.flags
.q
.column_major
)
6085 matrix_layout
= GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
;
6087 bool redeclaring_per_vertex
= strcmp(this->block_name
, "gl_PerVertex") == 0;
6088 exec_list declared_variables
;
6089 glsl_struct_field
*fields
;
6091 /* Treat an interface block as one level of nesting, so that embedded struct
6092 * specifiers will be disallowed.
6094 state
->struct_specifier_depth
++;
6096 unsigned int num_variables
=
6097 ast_process_structure_or_interface_block(&declared_variables
,
6099 &this->declarations
,
6104 redeclaring_per_vertex
,
6108 state
->struct_specifier_depth
--;
6110 if (!redeclaring_per_vertex
) {
6111 validate_identifier(this->block_name
, loc
, state
);
6113 /* From section 4.3.9 ("Interface Blocks") of the GLSL 4.50 spec:
6115 * "Block names have no other use within a shader beyond interface
6116 * matching; it is a compile-time error to use a block name at global
6117 * scope for anything other than as a block name."
6119 ir_variable
*var
= state
->symbols
->get_variable(this->block_name
);
6120 if (var
&& !var
->type
->is_interface()) {
6121 _mesa_glsl_error(&loc
, state
, "Block name `%s' is "
6122 "already used in the scope.",
6127 const glsl_type
*earlier_per_vertex
= NULL
;
6128 if (redeclaring_per_vertex
) {
6129 /* Find the previous declaration of gl_PerVertex. If we're redeclaring
6130 * the named interface block gl_in, we can find it by looking at the
6131 * previous declaration of gl_in. Otherwise we can find it by looking
6132 * at the previous decalartion of any of the built-in outputs,
6135 * Also check that the instance name and array-ness of the redeclaration
6139 case ir_var_shader_in
:
6140 if (ir_variable
*earlier_gl_in
=
6141 state
->symbols
->get_variable("gl_in")) {
6142 earlier_per_vertex
= earlier_gl_in
->get_interface_type();
6144 _mesa_glsl_error(&loc
, state
,
6145 "redeclaration of gl_PerVertex input not allowed "
6147 _mesa_shader_stage_to_string(state
->stage
));
6149 if (this->instance_name
== NULL
||
6150 strcmp(this->instance_name
, "gl_in") != 0 || this->array_specifier
== NULL
) {
6151 _mesa_glsl_error(&loc
, state
,
6152 "gl_PerVertex input must be redeclared as "
6156 case ir_var_shader_out
:
6157 if (ir_variable
*earlier_gl_Position
=
6158 state
->symbols
->get_variable("gl_Position")) {
6159 earlier_per_vertex
= earlier_gl_Position
->get_interface_type();
6160 } else if (ir_variable
*earlier_gl_out
=
6161 state
->symbols
->get_variable("gl_out")) {
6162 earlier_per_vertex
= earlier_gl_out
->get_interface_type();
6164 _mesa_glsl_error(&loc
, state
,
6165 "redeclaration of gl_PerVertex output not "
6166 "allowed in the %s shader",
6167 _mesa_shader_stage_to_string(state
->stage
));
6169 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
6170 if (this->instance_name
== NULL
||
6171 strcmp(this->instance_name
, "gl_out") != 0 || this->array_specifier
== NULL
) {
6172 _mesa_glsl_error(&loc
, state
,
6173 "gl_PerVertex output must be redeclared as "
6177 if (this->instance_name
!= NULL
) {
6178 _mesa_glsl_error(&loc
, state
,
6179 "gl_PerVertex output may not be redeclared with "
6180 "an instance name");
6185 _mesa_glsl_error(&loc
, state
,
6186 "gl_PerVertex must be declared as an input or an "
6191 if (earlier_per_vertex
== NULL
) {
6192 /* An error has already been reported. Bail out to avoid null
6193 * dereferences later in this function.
6198 /* Copy locations from the old gl_PerVertex interface block. */
6199 for (unsigned i
= 0; i
< num_variables
; i
++) {
6200 int j
= earlier_per_vertex
->field_index(fields
[i
].name
);
6202 _mesa_glsl_error(&loc
, state
,
6203 "redeclaration of gl_PerVertex must be a subset "
6204 "of the built-in members of gl_PerVertex");
6206 fields
[i
].location
=
6207 earlier_per_vertex
->fields
.structure
[j
].location
;
6208 fields
[i
].interpolation
=
6209 earlier_per_vertex
->fields
.structure
[j
].interpolation
;
6210 fields
[i
].centroid
=
6211 earlier_per_vertex
->fields
.structure
[j
].centroid
;
6213 earlier_per_vertex
->fields
.structure
[j
].sample
;
6215 earlier_per_vertex
->fields
.structure
[j
].patch
;
6219 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10
6222 * If a built-in interface block is redeclared, it must appear in
6223 * the shader before any use of any member included in the built-in
6224 * declaration, or a compilation error will result.
6226 * This appears to be a clarification to the behaviour established for
6227 * gl_PerVertex by GLSL 1.50, therefore we implement this behaviour
6228 * regardless of GLSL version.
6230 interface_block_usage_visitor
v(var_mode
, earlier_per_vertex
);
6231 v
.run(instructions
);
6232 if (v
.usage_found()) {
6233 _mesa_glsl_error(&loc
, state
,
6234 "redeclaration of a built-in interface block must "
6235 "appear before any use of any member of the "
6240 const glsl_type
*block_type
=
6241 glsl_type::get_interface_instance(fields
,
6245 if (this->layout
.flags
.q
.explicit_binding
)
6246 validate_binding_qualifier(state
, &loc
, block_type
, &this->layout
);
6248 if (!state
->symbols
->add_interface(block_type
->name
, block_type
, var_mode
)) {
6249 YYLTYPE loc
= this->get_location();
6250 _mesa_glsl_error(&loc
, state
, "interface block `%s' with type `%s' "
6251 "already taken in the current scope",
6252 this->block_name
, iface_type_name
);
6255 /* Since interface blocks cannot contain statements, it should be
6256 * impossible for the block to generate any instructions.
6258 assert(declared_variables
.is_empty());
6260 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
6262 * Geometry shader input variables get the per-vertex values written
6263 * out by vertex shader output variables of the same names. Since a
6264 * geometry shader operates on a set of vertices, each input varying
6265 * variable (or input block, see interface blocks below) needs to be
6266 * declared as an array.
6268 if (state
->stage
== MESA_SHADER_GEOMETRY
&& this->array_specifier
== NULL
&&
6269 var_mode
== ir_var_shader_in
) {
6270 _mesa_glsl_error(&loc
, state
, "geometry shader inputs must be arrays");
6271 } else if ((state
->stage
== MESA_SHADER_TESS_CTRL
||
6272 state
->stage
== MESA_SHADER_TESS_EVAL
) &&
6273 this->array_specifier
== NULL
&&
6274 var_mode
== ir_var_shader_in
) {
6275 _mesa_glsl_error(&loc
, state
, "per-vertex tessellation shader inputs must be arrays");
6276 } else if (state
->stage
== MESA_SHADER_TESS_CTRL
&&
6277 this->array_specifier
== NULL
&&
6278 var_mode
== ir_var_shader_out
) {
6279 _mesa_glsl_error(&loc
, state
, "tessellation control shader outputs must be arrays");
6283 /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
6286 * "If an instance name (instance-name) is used, then it puts all the
6287 * members inside a scope within its own name space, accessed with the
6288 * field selector ( . ) operator (analogously to structures)."
6290 if (this->instance_name
) {
6291 if (redeclaring_per_vertex
) {
6292 /* When a built-in in an unnamed interface block is redeclared,
6293 * get_variable_being_redeclared() calls
6294 * check_builtin_array_max_size() to make sure that built-in array
6295 * variables aren't redeclared to illegal sizes. But we're looking
6296 * at a redeclaration of a named built-in interface block. So we
6297 * have to manually call check_builtin_array_max_size() for all parts
6298 * of the interface that are arrays.
6300 for (unsigned i
= 0; i
< num_variables
; i
++) {
6301 if (fields
[i
].type
->is_array()) {
6302 const unsigned size
= fields
[i
].type
->array_size();
6303 check_builtin_array_max_size(fields
[i
].name
, size
, loc
, state
);
6307 validate_identifier(this->instance_name
, loc
, state
);
6312 if (this->array_specifier
!= NULL
) {
6313 /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
6315 * For uniform blocks declared an array, each individual array
6316 * element corresponds to a separate buffer object backing one
6317 * instance of the block. As the array size indicates the number
6318 * of buffer objects needed, uniform block array declarations
6319 * must specify an array size.
6321 * And a few paragraphs later:
6323 * Geometry shader input blocks must be declared as arrays and
6324 * follow the array declaration and linking rules for all
6325 * geometry shader inputs. All other input and output block
6326 * arrays must specify an array size.
6328 * The same applies to tessellation shaders.
6330 * The upshot of this is that the only circumstance where an
6331 * interface array size *doesn't* need to be specified is on a
6332 * geometry shader input, tessellation control shader input,
6333 * tessellation control shader output, and tessellation evaluation
6336 if (this->array_specifier
->is_unsized_array
) {
6337 bool allow_inputs
= state
->stage
== MESA_SHADER_GEOMETRY
||
6338 state
->stage
== MESA_SHADER_TESS_CTRL
||
6339 state
->stage
== MESA_SHADER_TESS_EVAL
;
6340 bool allow_outputs
= state
->stage
== MESA_SHADER_TESS_CTRL
;
6342 if (this->layout
.flags
.q
.in
) {
6344 _mesa_glsl_error(&loc
, state
,
6345 "unsized input block arrays not allowed in "
6347 _mesa_shader_stage_to_string(state
->stage
));
6348 } else if (this->layout
.flags
.q
.out
) {
6350 _mesa_glsl_error(&loc
, state
,
6351 "unsized output block arrays not allowed in "
6353 _mesa_shader_stage_to_string(state
->stage
));
6355 /* by elimination, this is a uniform block array */
6356 _mesa_glsl_error(&loc
, state
,
6357 "unsized uniform block arrays not allowed in "
6359 _mesa_shader_stage_to_string(state
->stage
));
6363 const glsl_type
*block_array_type
=
6364 process_array_type(&loc
, block_type
, this->array_specifier
, state
);
6366 /* From section 4.3.9 (Interface Blocks) of the GLSL ES 3.10 spec:
6368 * * Arrays of arrays of blocks are not allowed
6370 if (state
->es_shader
&& block_array_type
->is_array() &&
6371 block_array_type
->fields
.array
->is_array()) {
6372 _mesa_glsl_error(&loc
, state
,
6373 "arrays of arrays interface blocks are "
6377 if (this->layout
.flags
.q
.explicit_binding
)
6378 validate_binding_qualifier(state
, &loc
, block_array_type
,
6381 var
= new(state
) ir_variable(block_array_type
,
6382 this->instance_name
,
6385 var
= new(state
) ir_variable(block_type
,
6386 this->instance_name
,
6390 var
->data
.matrix_layout
= matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
6391 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
: matrix_layout
;
6393 if (var_mode
== ir_var_shader_in
|| var_mode
== ir_var_uniform
)
6394 var
->data
.read_only
= true;
6396 if (state
->stage
== MESA_SHADER_GEOMETRY
&& var_mode
== ir_var_shader_in
)
6397 handle_geometry_shader_input_decl(state
, loc
, var
);
6398 else if ((state
->stage
== MESA_SHADER_TESS_CTRL
||
6399 state
->stage
== MESA_SHADER_TESS_EVAL
) && var_mode
== ir_var_shader_in
)
6400 handle_tess_shader_input_decl(state
, loc
, var
);
6401 else if (state
->stage
== MESA_SHADER_TESS_CTRL
&& var_mode
== ir_var_shader_out
)
6402 handle_tess_ctrl_shader_output_decl(state
, loc
, var
);
6404 for (unsigned i
= 0; i
< num_variables
; i
++) {
6405 if (fields
[i
].type
->is_unsized_array()) {
6406 if (var_mode
== ir_var_shader_storage
) {
6407 if (i
!= (num_variables
- 1)) {
6408 _mesa_glsl_error(&loc
, state
, "unsized array `%s' definition: "
6409 "only last member of a shader storage block "
6410 "can be defined as unsized array",
6414 /* From GLSL ES 3.10 spec, section 4.1.9 "Arrays":
6416 * "If an array is declared as the last member of a shader storage
6417 * block and the size is not specified at compile-time, it is
6418 * sized at run-time. In all other cases, arrays are sized only
6421 if (state
->es_shader
) {
6422 _mesa_glsl_error(&loc
, state
, "unsized array `%s' definition: "
6423 "only last member of a shader storage block "
6424 "can be defined as unsized array",
6431 if (ir_variable
*earlier
=
6432 state
->symbols
->get_variable(this->instance_name
)) {
6433 if (!redeclaring_per_vertex
) {
6434 _mesa_glsl_error(&loc
, state
, "`%s' redeclared",
6435 this->instance_name
);
6437 earlier
->data
.how_declared
= ir_var_declared_normally
;
6438 earlier
->type
= var
->type
;
6439 earlier
->reinit_interface_type(block_type
);
6442 /* Propagate the "binding" keyword into this UBO's fields;
6443 * the UBO declaration itself doesn't get an ir_variable unless it
6444 * has an instance name. This is ugly.
6446 var
->data
.explicit_binding
= this->layout
.flags
.q
.explicit_binding
;
6447 var
->data
.binding
= this->layout
.binding
;
6449 state
->symbols
->add_variable(var
);
6450 instructions
->push_tail(var
);
6453 /* In order to have an array size, the block must also be declared with
6456 assert(this->array_specifier
== NULL
);
6458 for (unsigned i
= 0; i
< num_variables
; i
++) {
6460 new(state
) ir_variable(fields
[i
].type
,
6461 ralloc_strdup(state
, fields
[i
].name
),
6463 var
->data
.interpolation
= fields
[i
].interpolation
;
6464 var
->data
.centroid
= fields
[i
].centroid
;
6465 var
->data
.sample
= fields
[i
].sample
;
6466 var
->data
.patch
= fields
[i
].patch
;
6467 var
->init_interface_type(block_type
);
6469 if (var_mode
== ir_var_shader_in
|| var_mode
== ir_var_uniform
)
6470 var
->data
.read_only
= true;
6472 if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
) {
6473 var
->data
.matrix_layout
= matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
6474 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
: matrix_layout
;
6476 var
->data
.matrix_layout
= fields
[i
].matrix_layout
;
6479 if (fields
[i
].stream
!= -1 &&
6480 ((unsigned)fields
[i
].stream
) != this->layout
.stream
) {
6481 _mesa_glsl_error(&loc
, state
,
6482 "stream layout qualifier on "
6483 "interface block member `%s' does not match "
6484 "the interface block (%d vs %d)",
6485 var
->name
, fields
[i
].stream
, this->layout
.stream
);
6488 var
->data
.stream
= this->layout
.stream
;
6490 if (var
->data
.mode
== ir_var_shader_storage
) {
6491 var
->data
.image_read_only
= fields
[i
].image_read_only
;
6492 var
->data
.image_write_only
= fields
[i
].image_write_only
;
6493 var
->data
.image_coherent
= fields
[i
].image_coherent
;
6494 var
->data
.image_volatile
= fields
[i
].image_volatile
;
6495 var
->data
.image_restrict
= fields
[i
].image_restrict
;
6498 /* Examine var name here since var may get deleted in the next call */
6499 bool var_is_gl_id
= is_gl_identifier(var
->name
);
6501 if (redeclaring_per_vertex
) {
6502 ir_variable
*earlier
=
6503 get_variable_being_redeclared(var
, loc
, state
,
6504 true /* allow_all_redeclarations */);
6505 if (!var_is_gl_id
|| earlier
== NULL
) {
6506 _mesa_glsl_error(&loc
, state
,
6507 "redeclaration of gl_PerVertex can only "
6508 "include built-in variables");
6509 } else if (earlier
->data
.how_declared
== ir_var_declared_normally
) {
6510 _mesa_glsl_error(&loc
, state
,
6511 "`%s' has already been redeclared",
6514 earlier
->data
.how_declared
= ir_var_declared_in_block
;
6515 earlier
->reinit_interface_type(block_type
);
6520 if (state
->symbols
->get_variable(var
->name
) != NULL
)
6521 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
6523 /* Propagate the "binding" keyword into this UBO/SSBO's fields.
6524 * The UBO declaration itself doesn't get an ir_variable unless it
6525 * has an instance name. This is ugly.
6527 var
->data
.explicit_binding
= this->layout
.flags
.q
.explicit_binding
;
6528 var
->data
.binding
= this->layout
.binding
;
6530 if (var
->type
->is_unsized_array()) {
6531 if (var
->is_in_shader_storage_block()) {
6532 if (!is_unsized_array_last_element(var
)) {
6533 _mesa_glsl_error(&loc
, state
, "unsized array `%s' definition: "
6534 "only last member of a shader storage block "
6535 "can be defined as unsized array",
6538 var
->data
.from_ssbo_unsized_array
= true;
6540 /* From GLSL ES 3.10 spec, section 4.1.9 "Arrays":
6542 * "If an array is declared as the last member of a shader storage
6543 * block and the size is not specified at compile-time, it is
6544 * sized at run-time. In all other cases, arrays are sized only
6547 if (state
->es_shader
) {
6548 _mesa_glsl_error(&loc
, state
, "unsized array `%s' definition: "
6549 "only last member of a shader storage block "
6550 "can be defined as unsized array",
6556 state
->symbols
->add_variable(var
);
6557 instructions
->push_tail(var
);
6560 if (redeclaring_per_vertex
&& block_type
!= earlier_per_vertex
) {
6561 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec:
6563 * It is also a compilation error ... to redeclare a built-in
6564 * block and then use a member from that built-in block that was
6565 * not included in the redeclaration.
6567 * This appears to be a clarification to the behaviour established
6568 * for gl_PerVertex by GLSL 1.50, therefore we implement this
6569 * behaviour regardless of GLSL version.
6571 * To prevent the shader from using a member that was not included in
6572 * the redeclaration, we disable any ir_variables that are still
6573 * associated with the old declaration of gl_PerVertex (since we've
6574 * already updated all of the variables contained in the new
6575 * gl_PerVertex to point to it).
6577 * As a side effect this will prevent
6578 * validate_intrastage_interface_blocks() from getting confused and
6579 * thinking there are conflicting definitions of gl_PerVertex in the
6582 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
6583 ir_variable
*const var
= node
->as_variable();
6585 var
->get_interface_type() == earlier_per_vertex
&&
6586 var
->data
.mode
== var_mode
) {
6587 if (var
->data
.how_declared
== ir_var_declared_normally
) {
6588 _mesa_glsl_error(&loc
, state
,
6589 "redeclaration of gl_PerVertex cannot "
6590 "follow a redeclaration of `%s'",
6593 state
->symbols
->disable_variable(var
->name
);
6605 ast_tcs_output_layout::hir(exec_list
*instructions
,
6606 struct _mesa_glsl_parse_state
*state
)
6608 YYLTYPE loc
= this->get_location();
6610 /* If any tessellation control output layout declaration preceded this
6611 * one, make sure it was consistent with this one.
6613 if (state
->tcs_output_vertices_specified
&&
6614 state
->out_qualifier
->vertices
!= this->vertices
) {
6615 _mesa_glsl_error(&loc
, state
,
6616 "tessellation control shader output layout does not "
6617 "match previous declaration");
6621 /* If any shader outputs occurred before this declaration and specified an
6622 * array size, make sure the size they specified is consistent with the
6625 unsigned num_vertices
= this->vertices
;
6626 if (state
->tcs_output_size
!= 0 && state
->tcs_output_size
!= num_vertices
) {
6627 _mesa_glsl_error(&loc
, state
,
6628 "this tessellation control shader output layout "
6629 "specifies %u vertices, but a previous output "
6630 "is declared with size %u",
6631 num_vertices
, state
->tcs_output_size
);
6635 state
->tcs_output_vertices_specified
= true;
6637 /* If any shader outputs occurred before this declaration and did not
6638 * specify an array size, their size is determined now.
6640 foreach_in_list (ir_instruction
, node
, instructions
) {
6641 ir_variable
*var
= node
->as_variable();
6642 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_out
)
6645 /* Note: Not all tessellation control shader output are arrays. */
6646 if (!var
->type
->is_unsized_array() || var
->data
.patch
)
6649 if (var
->data
.max_array_access
>= num_vertices
) {
6650 _mesa_glsl_error(&loc
, state
,
6651 "this tessellation control shader output layout "
6652 "specifies %u vertices, but an access to element "
6653 "%u of output `%s' already exists", num_vertices
,
6654 var
->data
.max_array_access
, var
->name
);
6656 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
6666 ast_gs_input_layout::hir(exec_list
*instructions
,
6667 struct _mesa_glsl_parse_state
*state
)
6669 YYLTYPE loc
= this->get_location();
6671 /* If any geometry input layout declaration preceded this one, make sure it
6672 * was consistent with this one.
6674 if (state
->gs_input_prim_type_specified
&&
6675 state
->in_qualifier
->prim_type
!= this->prim_type
) {
6676 _mesa_glsl_error(&loc
, state
,
6677 "geometry shader input layout does not match"
6678 " previous declaration");
6682 /* If any shader inputs occurred before this declaration and specified an
6683 * array size, make sure the size they specified is consistent with the
6686 unsigned num_vertices
= vertices_per_prim(this->prim_type
);
6687 if (state
->gs_input_size
!= 0 && state
->gs_input_size
!= num_vertices
) {
6688 _mesa_glsl_error(&loc
, state
,
6689 "this geometry shader input layout implies %u vertices"
6690 " per primitive, but a previous input is declared"
6691 " with size %u", num_vertices
, state
->gs_input_size
);
6695 state
->gs_input_prim_type_specified
= true;
6697 /* If any shader inputs occurred before this declaration and did not
6698 * specify an array size, their size is determined now.
6700 foreach_in_list(ir_instruction
, node
, instructions
) {
6701 ir_variable
*var
= node
->as_variable();
6702 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_in
)
6705 /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
6709 if (var
->type
->is_unsized_array()) {
6710 if (var
->data
.max_array_access
>= num_vertices
) {
6711 _mesa_glsl_error(&loc
, state
,
6712 "this geometry shader input layout implies %u"
6713 " vertices, but an access to element %u of input"
6714 " `%s' already exists", num_vertices
,
6715 var
->data
.max_array_access
, var
->name
);
6717 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
6728 ast_cs_input_layout::hir(exec_list
*instructions
,
6729 struct _mesa_glsl_parse_state
*state
)
6731 YYLTYPE loc
= this->get_location();
6733 /* If any compute input layout declaration preceded this one, make sure it
6734 * was consistent with this one.
6736 if (state
->cs_input_local_size_specified
) {
6737 for (int i
= 0; i
< 3; i
++) {
6738 if (state
->cs_input_local_size
[i
] != this->local_size
[i
]) {
6739 _mesa_glsl_error(&loc
, state
,
6740 "compute shader input layout does not match"
6741 " previous declaration");
6747 /* From the ARB_compute_shader specification:
6749 * If the local size of the shader in any dimension is greater
6750 * than the maximum size supported by the implementation for that
6751 * dimension, a compile-time error results.
6753 * It is not clear from the spec how the error should be reported if
6754 * the total size of the work group exceeds
6755 * MAX_COMPUTE_WORK_GROUP_INVOCATIONS, but it seems reasonable to
6756 * report it at compile time as well.
6758 GLuint64 total_invocations
= 1;
6759 for (int i
= 0; i
< 3; i
++) {
6760 if (this->local_size
[i
] > state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]) {
6761 _mesa_glsl_error(&loc
, state
,
6762 "local_size_%c exceeds MAX_COMPUTE_WORK_GROUP_SIZE"
6764 state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]);
6767 total_invocations
*= this->local_size
[i
];
6768 if (total_invocations
>
6769 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
) {
6770 _mesa_glsl_error(&loc
, state
,
6771 "product of local_sizes exceeds "
6772 "MAX_COMPUTE_WORK_GROUP_INVOCATIONS (%d)",
6773 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
);
6778 state
->cs_input_local_size_specified
= true;
6779 for (int i
= 0; i
< 3; i
++)
6780 state
->cs_input_local_size
[i
] = this->local_size
[i
];
6782 /* We may now declare the built-in constant gl_WorkGroupSize (see
6783 * builtin_variable_generator::generate_constants() for why we didn't
6784 * declare it earlier).
6786 ir_variable
*var
= new(state
->symbols
)
6787 ir_variable(glsl_type::uvec3_type
, "gl_WorkGroupSize", ir_var_auto
);
6788 var
->data
.how_declared
= ir_var_declared_implicitly
;
6789 var
->data
.read_only
= true;
6790 instructions
->push_tail(var
);
6791 state
->symbols
->add_variable(var
);
6792 ir_constant_data data
;
6793 memset(&data
, 0, sizeof(data
));
6794 for (int i
= 0; i
< 3; i
++)
6795 data
.u
[i
] = this->local_size
[i
];
6796 var
->constant_value
= new(var
) ir_constant(glsl_type::uvec3_type
, &data
);
6797 var
->constant_initializer
=
6798 new(var
) ir_constant(glsl_type::uvec3_type
, &data
);
6799 var
->data
.has_initializer
= true;
6806 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
6807 exec_list
*instructions
)
6809 bool gl_FragColor_assigned
= false;
6810 bool gl_FragData_assigned
= false;
6811 bool user_defined_fs_output_assigned
= false;
6812 ir_variable
*user_defined_fs_output
= NULL
;
6814 /* It would be nice to have proper location information. */
6816 memset(&loc
, 0, sizeof(loc
));
6818 foreach_in_list(ir_instruction
, node
, instructions
) {
6819 ir_variable
*var
= node
->as_variable();
6821 if (!var
|| !var
->data
.assigned
)
6824 if (strcmp(var
->name
, "gl_FragColor") == 0)
6825 gl_FragColor_assigned
= true;
6826 else if (strcmp(var
->name
, "gl_FragData") == 0)
6827 gl_FragData_assigned
= true;
6828 else if (!is_gl_identifier(var
->name
)) {
6829 if (state
->stage
== MESA_SHADER_FRAGMENT
&&
6830 var
->data
.mode
== ir_var_shader_out
) {
6831 user_defined_fs_output_assigned
= true;
6832 user_defined_fs_output
= var
;
6837 /* From the GLSL 1.30 spec:
6839 * "If a shader statically assigns a value to gl_FragColor, it
6840 * may not assign a value to any element of gl_FragData. If a
6841 * shader statically writes a value to any element of
6842 * gl_FragData, it may not assign a value to
6843 * gl_FragColor. That is, a shader may assign values to either
6844 * gl_FragColor or gl_FragData, but not both. Multiple shaders
6845 * linked together must also consistently write just one of
6846 * these variables. Similarly, if user declared output
6847 * variables are in use (statically assigned to), then the
6848 * built-in variables gl_FragColor and gl_FragData may not be
6849 * assigned to. These incorrect usages all generate compile
6852 if (gl_FragColor_assigned
&& gl_FragData_assigned
) {
6853 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
6854 "`gl_FragColor' and `gl_FragData'");
6855 } else if (gl_FragColor_assigned
&& user_defined_fs_output_assigned
) {
6856 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
6857 "`gl_FragColor' and `%s'",
6858 user_defined_fs_output
->name
);
6859 } else if (gl_FragData_assigned
&& user_defined_fs_output_assigned
) {
6860 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
6861 "`gl_FragData' and `%s'",
6862 user_defined_fs_output
->name
);
6868 remove_per_vertex_blocks(exec_list
*instructions
,
6869 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
)
6871 /* Find the gl_PerVertex interface block of the appropriate (in/out) mode,
6872 * if it exists in this shader type.
6874 const glsl_type
*per_vertex
= NULL
;
6876 case ir_var_shader_in
:
6877 if (ir_variable
*gl_in
= state
->symbols
->get_variable("gl_in"))
6878 per_vertex
= gl_in
->get_interface_type();
6880 case ir_var_shader_out
:
6881 if (ir_variable
*gl_Position
=
6882 state
->symbols
->get_variable("gl_Position")) {
6883 per_vertex
= gl_Position
->get_interface_type();
6887 assert(!"Unexpected mode");
6891 /* If we didn't find a built-in gl_PerVertex interface block, then we don't
6892 * need to do anything.
6894 if (per_vertex
== NULL
)
6897 /* If the interface block is used by the shader, then we don't need to do
6900 interface_block_usage_visitor
v(mode
, per_vertex
);
6901 v
.run(instructions
);
6902 if (v
.usage_found())
6905 /* Remove any ir_variable declarations that refer to the interface block
6908 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
6909 ir_variable
*const var
= node
->as_variable();
6910 if (var
!= NULL
&& var
->get_interface_type() == per_vertex
&&
6911 var
->data
.mode
== mode
) {
6912 state
->symbols
->disable_variable(var
->name
);