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 "main/core.h" /* for struct gl_extensions */
53 #include "glsl_symbol_table.h"
54 #include "glsl_parser_extras.h"
56 #include "glsl_types.h"
57 #include "program/hash_table.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
);
72 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
74 _mesa_glsl_initialize_variables(instructions
, state
);
76 state
->symbols
->separate_function_namespace
= state
->language_version
== 110;
78 state
->current_function
= NULL
;
80 state
->toplevel_ir
= instructions
;
82 state
->gs_input_prim_type_specified
= false;
83 state
->cs_input_local_size_specified
= false;
85 /* Section 4.2 of the GLSL 1.20 specification states:
86 * "The built-in functions are scoped in a scope outside the global scope
87 * users declare global variables in. That is, a shader's global scope,
88 * available for user-defined functions and global variables, is nested
89 * inside the scope containing the built-in functions."
91 * Since built-in functions like ftransform() access built-in variables,
92 * it follows that those must be in the outer scope as well.
94 * We push scope here to create this nesting effect...but don't pop.
95 * This way, a shader's globals are still in the symbol table for use
98 state
->symbols
->push_scope();
100 foreach_list_typed (ast_node
, ast
, link
, & state
->translation_unit
)
101 ast
->hir(instructions
, state
);
103 detect_recursion_unlinked(state
, instructions
);
104 detect_conflicting_assignments(state
, instructions
);
106 state
->toplevel_ir
= NULL
;
108 /* Move all of the variable declarations to the front of the IR list, and
109 * reverse the order. This has the (intended!) side effect that vertex
110 * shader inputs and fragment shader outputs will appear in the IR in the
111 * same order that they appeared in the shader code. This results in the
112 * locations being assigned in the declared order. Many (arguably buggy)
113 * applications depend on this behavior, and it matches what nearly all
116 foreach_list_safe(node
, instructions
) {
117 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
123 instructions
->push_head(var
);
126 /* Figure out if gl_FragCoord is actually used in fragment shader */
127 ir_variable
*const var
= state
->symbols
->get_variable("gl_FragCoord");
129 state
->fs_uses_gl_fragcoord
= var
->data
.used
;
131 /* From section 7.1 (Built-In Language Variables) of the GLSL 4.10 spec:
133 * If multiple shaders using members of a built-in block belonging to
134 * the same interface are linked together in the same program, they
135 * must all redeclare the built-in block in the same way, as described
136 * in section 4.3.7 "Interface Blocks" for interface block matching, or
137 * a link error will result.
139 * The phrase "using members of a built-in block" implies that if two
140 * shaders are linked together and one of them *does not use* any members
141 * of the built-in block, then that shader does not need to have a matching
142 * redeclaration of the built-in block.
144 * This appears to be a clarification to the behaviour established for
145 * gl_PerVertex by GLSL 1.50, therefore implement it regardless of GLSL
148 * The definition of "interface" in section 4.3.7 that applies here is as
151 * The boundary between adjacent programmable pipeline stages: This
152 * spans all the outputs in all compilation units of the first stage
153 * and all the inputs in all compilation units of the second stage.
155 * Therefore this rule applies to both inter- and intra-stage linking.
157 * The easiest way to implement this is to check whether the shader uses
158 * gl_PerVertex right after ast-to-ir conversion, and if it doesn't, simply
159 * remove all the relevant variable declaration from the IR, so that the
160 * linker won't see them and complain about mismatches.
162 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_in
);
163 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_out
);
168 * If a conversion is available, convert one operand to a different type
170 * The \c from \c ir_rvalue is converted "in place".
172 * \param to Type that the operand it to be converted to
173 * \param from Operand that is being converted
174 * \param state GLSL compiler state
177 * If a conversion is possible (or unnecessary), \c true is returned.
178 * Otherwise \c false is returned.
181 apply_implicit_conversion(const glsl_type
*to
, ir_rvalue
* &from
,
182 struct _mesa_glsl_parse_state
*state
)
185 if (to
->base_type
== from
->type
->base_type
)
188 /* This conversion was added in GLSL 1.20. If the compilation mode is
189 * GLSL 1.10, the conversion is skipped.
191 if (!state
->is_version(120, 0))
194 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
196 * "There are no implicit array or structure conversions. For
197 * example, an array of int cannot be implicitly converted to an
198 * array of float. There are no implicit conversions between
199 * signed and unsigned integers."
201 /* FINISHME: The above comment is partially a lie. There is int/uint
202 * FINISHME: conversion for immediate constants.
204 if (!to
->is_float() || !from
->type
->is_numeric())
207 /* Convert to a floating point type with the same number of components
208 * as the original type - i.e. int to float, not int to vec4.
210 to
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, from
->type
->vector_elements
,
211 from
->type
->matrix_columns
);
213 switch (from
->type
->base_type
) {
215 from
= new(ctx
) ir_expression(ir_unop_i2f
, to
, from
, NULL
);
218 from
= new(ctx
) ir_expression(ir_unop_u2f
, to
, from
, NULL
);
221 from
= new(ctx
) ir_expression(ir_unop_b2f
, to
, from
, NULL
);
231 static const struct glsl_type
*
232 arithmetic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
234 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
236 const glsl_type
*type_a
= value_a
->type
;
237 const glsl_type
*type_b
= value_b
->type
;
239 /* From GLSL 1.50 spec, page 56:
241 * "The arithmetic binary operators add (+), subtract (-),
242 * multiply (*), and divide (/) operate on integer and
243 * floating-point scalars, vectors, and matrices."
245 if (!type_a
->is_numeric() || !type_b
->is_numeric()) {
246 _mesa_glsl_error(loc
, state
,
247 "operands to arithmetic operators must be numeric");
248 return glsl_type::error_type
;
252 /* "If one operand is floating-point based and the other is
253 * not, then the conversions from Section 4.1.10 "Implicit
254 * Conversions" are applied to the non-floating-point-based operand."
256 if (!apply_implicit_conversion(type_a
, value_b
, state
)
257 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
258 _mesa_glsl_error(loc
, state
,
259 "could not implicitly convert operands to "
260 "arithmetic operator");
261 return glsl_type::error_type
;
263 type_a
= value_a
->type
;
264 type_b
= value_b
->type
;
266 /* "If the operands are integer types, they must both be signed or
269 * From this rule and the preceeding conversion it can be inferred that
270 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
271 * The is_numeric check above already filtered out the case where either
272 * type is not one of these, so now the base types need only be tested for
275 if (type_a
->base_type
!= type_b
->base_type
) {
276 _mesa_glsl_error(loc
, state
,
277 "base type mismatch for arithmetic operator");
278 return glsl_type::error_type
;
281 /* "All arithmetic binary operators result in the same fundamental type
282 * (signed integer, unsigned integer, or floating-point) as the
283 * operands they operate on, after operand type conversion. After
284 * conversion, the following cases are valid
286 * * The two operands are scalars. In this case the operation is
287 * applied, resulting in a scalar."
289 if (type_a
->is_scalar() && type_b
->is_scalar())
292 /* "* One operand is a scalar, and the other is a vector or matrix.
293 * In this case, the scalar operation is applied independently to each
294 * component of the vector or matrix, resulting in the same size
297 if (type_a
->is_scalar()) {
298 if (!type_b
->is_scalar())
300 } else if (type_b
->is_scalar()) {
304 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
305 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
308 assert(!type_a
->is_scalar());
309 assert(!type_b
->is_scalar());
311 /* "* The two operands are vectors of the same size. In this case, the
312 * operation is done component-wise resulting in the same size
315 if (type_a
->is_vector() && type_b
->is_vector()) {
316 if (type_a
== type_b
) {
319 _mesa_glsl_error(loc
, state
,
320 "vector size mismatch for arithmetic operator");
321 return glsl_type::error_type
;
325 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
326 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
327 * <vector, vector> have been handled. At least one of the operands must
328 * be matrix. Further, since there are no integer matrix types, the base
329 * type of both operands must be float.
331 assert(type_a
->is_matrix() || type_b
->is_matrix());
332 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
);
333 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
);
335 /* "* The operator is add (+), subtract (-), or divide (/), and the
336 * operands are matrices with the same number of rows and the same
337 * number of columns. In this case, the operation is done component-
338 * wise resulting in the same size matrix."
339 * * The operator is multiply (*), where both operands are matrices or
340 * one operand is a vector and the other a matrix. A right vector
341 * operand is treated as a column vector and a left vector operand as a
342 * row vector. In all these cases, it is required that the number of
343 * columns of the left operand is equal to the number of rows of the
344 * right operand. Then, the multiply (*) operation does a linear
345 * algebraic multiply, yielding an object that has the same number of
346 * rows as the left operand and the same number of columns as the right
347 * operand. Section 5.10 "Vector and Matrix Operations" explains in
348 * more detail how vectors and matrices are operated on."
351 if (type_a
== type_b
)
354 if (type_a
->is_matrix() && type_b
->is_matrix()) {
355 /* Matrix multiply. The columns of A must match the rows of B. Given
356 * the other previously tested constraints, this means the vector type
357 * of a row from A must be the same as the vector type of a column from
360 if (type_a
->row_type() == type_b
->column_type()) {
361 /* The resulting matrix has the number of columns of matrix B and
362 * the number of rows of matrix A. We get the row count of A by
363 * looking at the size of a vector that makes up a column. The
364 * transpose (size of a row) is done for B.
366 const glsl_type
*const type
=
367 glsl_type::get_instance(type_a
->base_type
,
368 type_a
->column_type()->vector_elements
,
369 type_b
->row_type()->vector_elements
);
370 assert(type
!= glsl_type::error_type
);
374 } else if (type_a
->is_matrix()) {
375 /* A is a matrix and B is a column vector. Columns of A must match
376 * rows of B. Given the other previously tested constraints, this
377 * means the vector type of a row from A must be the same as the
378 * vector the type of B.
380 if (type_a
->row_type() == type_b
) {
381 /* The resulting vector has a number of elements equal to
382 * the number of rows of matrix A. */
383 const glsl_type
*const type
=
384 glsl_type::get_instance(type_a
->base_type
,
385 type_a
->column_type()->vector_elements
,
387 assert(type
!= glsl_type::error_type
);
392 assert(type_b
->is_matrix());
394 /* A is a row vector and B is a matrix. Columns of A must match rows
395 * of B. Given the other previously tested constraints, this means
396 * the type of A must be the same as the vector type of a column from
399 if (type_a
== type_b
->column_type()) {
400 /* The resulting vector has a number of elements equal to
401 * the number of columns of matrix B. */
402 const glsl_type
*const type
=
403 glsl_type::get_instance(type_a
->base_type
,
404 type_b
->row_type()->vector_elements
,
406 assert(type
!= glsl_type::error_type
);
412 _mesa_glsl_error(loc
, state
, "size mismatch for matrix multiplication");
413 return glsl_type::error_type
;
417 /* "All other cases are illegal."
419 _mesa_glsl_error(loc
, state
, "type mismatch");
420 return glsl_type::error_type
;
424 static const struct glsl_type
*
425 unary_arithmetic_result_type(const struct glsl_type
*type
,
426 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
428 /* From GLSL 1.50 spec, page 57:
430 * "The arithmetic unary operators negate (-), post- and pre-increment
431 * and decrement (-- and ++) operate on integer or floating-point
432 * values (including vectors and matrices). All unary operators work
433 * component-wise on their operands. These result with the same type
436 if (!type
->is_numeric()) {
437 _mesa_glsl_error(loc
, state
,
438 "operands to arithmetic operators must be numeric");
439 return glsl_type::error_type
;
446 * \brief Return the result type of a bit-logic operation.
448 * If the given types to the bit-logic operator are invalid, return
449 * glsl_type::error_type.
451 * \param type_a Type of LHS of bit-logic op
452 * \param type_b Type of RHS of bit-logic op
454 static const struct glsl_type
*
455 bit_logic_result_type(const struct glsl_type
*type_a
,
456 const struct glsl_type
*type_b
,
458 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
460 if (!state
->check_bitwise_operations_allowed(loc
)) {
461 return glsl_type::error_type
;
464 /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
466 * "The bitwise operators and (&), exclusive-or (^), and inclusive-or
467 * (|). The operands must be of type signed or unsigned integers or
470 if (!type_a
->is_integer()) {
471 _mesa_glsl_error(loc
, state
, "LHS of `%s' must be an integer",
472 ast_expression::operator_string(op
));
473 return glsl_type::error_type
;
475 if (!type_b
->is_integer()) {
476 _mesa_glsl_error(loc
, state
, "RHS of `%s' must be an integer",
477 ast_expression::operator_string(op
));
478 return glsl_type::error_type
;
481 /* "The fundamental types of the operands (signed or unsigned) must
484 if (type_a
->base_type
!= type_b
->base_type
) {
485 _mesa_glsl_error(loc
, state
, "operands of `%s' must have the same "
486 "base type", ast_expression::operator_string(op
));
487 return glsl_type::error_type
;
490 /* "The operands cannot be vectors of differing size." */
491 if (type_a
->is_vector() &&
492 type_b
->is_vector() &&
493 type_a
->vector_elements
!= type_b
->vector_elements
) {
494 _mesa_glsl_error(loc
, state
, "operands of `%s' cannot be vectors of "
495 "different sizes", ast_expression::operator_string(op
));
496 return glsl_type::error_type
;
499 /* "If one operand is a scalar and the other a vector, the scalar is
500 * applied component-wise to the vector, resulting in the same type as
501 * the vector. The fundamental types of the operands [...] will be the
502 * resulting fundamental type."
504 if (type_a
->is_scalar())
510 static const struct glsl_type
*
511 modulus_result_type(const struct glsl_type
*type_a
,
512 const struct glsl_type
*type_b
,
513 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
515 if (!state
->check_version(130, 300, loc
, "operator '%%' is reserved")) {
516 return glsl_type::error_type
;
519 /* From GLSL 1.50 spec, page 56:
520 * "The operator modulus (%) operates on signed or unsigned integers or
521 * integer vectors. The operand types must both be signed or both be
524 if (!type_a
->is_integer()) {
525 _mesa_glsl_error(loc
, state
, "LHS of operator %% must be an integer");
526 return glsl_type::error_type
;
528 if (!type_b
->is_integer()) {
529 _mesa_glsl_error(loc
, state
, "RHS of operator %% must be an integer");
530 return glsl_type::error_type
;
532 if (type_a
->base_type
!= type_b
->base_type
) {
533 _mesa_glsl_error(loc
, state
,
534 "operands of %% must have the same base type");
535 return glsl_type::error_type
;
538 /* "The operands cannot be vectors of differing size. If one operand is
539 * a scalar and the other vector, then the scalar is applied component-
540 * wise to the vector, resulting in the same type as the vector. If both
541 * are vectors of the same size, the result is computed component-wise."
543 if (type_a
->is_vector()) {
544 if (!type_b
->is_vector()
545 || (type_a
->vector_elements
== type_b
->vector_elements
))
550 /* "The operator modulus (%) is not defined for any other data types
551 * (non-integer types)."
553 _mesa_glsl_error(loc
, state
, "type mismatch");
554 return glsl_type::error_type
;
558 static const struct glsl_type
*
559 relational_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
560 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
562 const glsl_type
*type_a
= value_a
->type
;
563 const glsl_type
*type_b
= value_b
->type
;
565 /* From GLSL 1.50 spec, page 56:
566 * "The relational operators greater than (>), less than (<), greater
567 * than or equal (>=), and less than or equal (<=) operate only on
568 * scalar integer and scalar floating-point expressions."
570 if (!type_a
->is_numeric()
571 || !type_b
->is_numeric()
572 || !type_a
->is_scalar()
573 || !type_b
->is_scalar()) {
574 _mesa_glsl_error(loc
, state
,
575 "operands to relational operators must be scalar and "
577 return glsl_type::error_type
;
580 /* "Either the operands' types must match, or the conversions from
581 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
582 * operand, after which the types must match."
584 if (!apply_implicit_conversion(type_a
, value_b
, state
)
585 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
586 _mesa_glsl_error(loc
, state
,
587 "could not implicitly convert operands to "
588 "relational operator");
589 return glsl_type::error_type
;
591 type_a
= value_a
->type
;
592 type_b
= value_b
->type
;
594 if (type_a
->base_type
!= type_b
->base_type
) {
595 _mesa_glsl_error(loc
, state
, "base type mismatch");
596 return glsl_type::error_type
;
599 /* "The result is scalar Boolean."
601 return glsl_type::bool_type
;
605 * \brief Return the result type of a bit-shift operation.
607 * If the given types to the bit-shift operator are invalid, return
608 * glsl_type::error_type.
610 * \param type_a Type of LHS of bit-shift op
611 * \param type_b Type of RHS of bit-shift op
613 static const struct glsl_type
*
614 shift_result_type(const struct glsl_type
*type_a
,
615 const struct glsl_type
*type_b
,
617 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
619 if (!state
->check_bitwise_operations_allowed(loc
)) {
620 return glsl_type::error_type
;
623 /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
625 * "The shift operators (<<) and (>>). For both operators, the operands
626 * must be signed or unsigned integers or integer vectors. One operand
627 * can be signed while the other is unsigned."
629 if (!type_a
->is_integer()) {
630 _mesa_glsl_error(loc
, state
, "LHS of operator %s must be an integer or "
631 "integer vector", ast_expression::operator_string(op
));
632 return glsl_type::error_type
;
635 if (!type_b
->is_integer()) {
636 _mesa_glsl_error(loc
, state
, "RHS of operator %s must be an integer or "
637 "integer vector", ast_expression::operator_string(op
));
638 return glsl_type::error_type
;
641 /* "If the first operand is a scalar, the second operand has to be
644 if (type_a
->is_scalar() && !type_b
->is_scalar()) {
645 _mesa_glsl_error(loc
, state
, "if the first operand of %s is scalar, the "
646 "second must be scalar as well",
647 ast_expression::operator_string(op
));
648 return glsl_type::error_type
;
651 /* If both operands are vectors, check that they have same number of
654 if (type_a
->is_vector() &&
655 type_b
->is_vector() &&
656 type_a
->vector_elements
!= type_b
->vector_elements
) {
657 _mesa_glsl_error(loc
, state
, "vector operands to operator %s must "
658 "have same number of elements",
659 ast_expression::operator_string(op
));
660 return glsl_type::error_type
;
663 /* "In all cases, the resulting type will be the same type as the left
670 * Validates that a value can be assigned to a location with a specified type
672 * Validates that \c rhs can be assigned to some location. If the types are
673 * not an exact match but an automatic conversion is possible, \c rhs will be
677 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
678 * Otherwise the actual RHS to be assigned will be returned. This may be
679 * \c rhs, or it may be \c rhs after some type conversion.
682 * In addition to being used for assignments, this function is used to
683 * type-check return values.
686 validate_assignment(struct _mesa_glsl_parse_state
*state
,
687 YYLTYPE loc
, const glsl_type
*lhs_type
,
688 ir_rvalue
*rhs
, bool is_initializer
)
690 /* If there is already some error in the RHS, just return it. Anything
691 * else will lead to an avalanche of error message back to the user.
693 if (rhs
->type
->is_error())
696 /* If the types are identical, the assignment can trivially proceed.
698 if (rhs
->type
== lhs_type
)
701 /* If the array element types are the same and the LHS is unsized,
702 * the assignment is okay for initializers embedded in variable
705 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
706 * is handled by ir_dereference::is_lvalue.
708 if (lhs_type
->is_unsized_array() && rhs
->type
->is_array()
709 && (lhs_type
->element_type() == rhs
->type
->element_type())) {
710 if (is_initializer
) {
713 _mesa_glsl_error(&loc
, state
,
714 "implicitly sized arrays cannot be assigned");
719 /* Check for implicit conversion in GLSL 1.20 */
720 if (apply_implicit_conversion(lhs_type
, rhs
, state
)) {
721 if (rhs
->type
== lhs_type
)
725 _mesa_glsl_error(&loc
, state
,
726 "%s of type %s cannot be assigned to "
727 "variable of type %s",
728 is_initializer
? "initializer" : "value",
729 rhs
->type
->name
, lhs_type
->name
);
735 mark_whole_array_access(ir_rvalue
*access
)
737 ir_dereference_variable
*deref
= access
->as_dereference_variable();
739 if (deref
&& deref
->var
) {
740 deref
->var
->data
.max_array_access
= deref
->type
->length
- 1;
745 do_assignment(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
746 const char *non_lvalue_description
,
747 ir_rvalue
*lhs
, ir_rvalue
*rhs
,
748 ir_rvalue
**out_rvalue
, bool needs_rvalue
,
753 bool error_emitted
= (lhs
->type
->is_error() || rhs
->type
->is_error());
754 ir_rvalue
*extract_channel
= NULL
;
756 /* If the assignment LHS comes back as an ir_binop_vector_extract
757 * expression, move it to the RHS as an ir_triop_vector_insert.
759 if (lhs
->ir_type
== ir_type_expression
) {
760 ir_expression
*const lhs_expr
= lhs
->as_expression();
762 if (unlikely(lhs_expr
->operation
== ir_binop_vector_extract
)) {
764 validate_assignment(state
, lhs_loc
, lhs
->type
,
765 rhs
, is_initializer
);
767 if (new_rhs
== NULL
) {
771 * - LHS: (expression float vector_extract <vec> <channel>)
775 * - RHS: (expression vec2 vector_insert <vec> <channel> <scalar>)
777 * The LHS type is now a vector instead of a scalar. Since GLSL
778 * allows assignments to be used as rvalues, we need to re-extract
779 * the channel from assignment_temp when returning the rvalue.
781 extract_channel
= lhs_expr
->operands
[1];
782 rhs
= new(ctx
) ir_expression(ir_triop_vector_insert
,
783 lhs_expr
->operands
[0]->type
,
784 lhs_expr
->operands
[0],
787 lhs
= lhs_expr
->operands
[0]->clone(ctx
, NULL
);
792 ir_variable
*lhs_var
= lhs
->variable_referenced();
794 lhs_var
->data
.assigned
= true;
796 if (!error_emitted
) {
797 if (non_lvalue_description
!= NULL
) {
798 _mesa_glsl_error(&lhs_loc
, state
,
800 non_lvalue_description
);
801 error_emitted
= true;
802 } else if (lhs_var
!= NULL
&& lhs_var
->data
.read_only
) {
803 _mesa_glsl_error(&lhs_loc
, state
,
804 "assignment to read-only variable '%s'",
806 error_emitted
= true;
807 } else if (lhs
->type
->is_array() &&
808 !state
->check_version(120, 300, &lhs_loc
,
809 "whole array assignment forbidden")) {
810 /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
812 * "Other binary or unary expressions, non-dereferenced
813 * arrays, function names, swizzles with repeated fields,
814 * and constants cannot be l-values."
816 * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00.
818 error_emitted
= true;
819 } else if (!lhs
->is_lvalue()) {
820 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
821 error_emitted
= true;
826 validate_assignment(state
, lhs_loc
, lhs
->type
, rhs
, is_initializer
);
827 if (new_rhs
!= NULL
) {
830 /* If the LHS array was not declared with a size, it takes it size from
831 * the RHS. If the LHS is an l-value and a whole array, it must be a
832 * dereference of a variable. Any other case would require that the LHS
833 * is either not an l-value or not a whole array.
835 if (lhs
->type
->is_unsized_array()) {
836 ir_dereference
*const d
= lhs
->as_dereference();
840 ir_variable
*const var
= d
->variable_referenced();
844 if (var
->data
.max_array_access
>= unsigned(rhs
->type
->array_size())) {
845 /* FINISHME: This should actually log the location of the RHS. */
846 _mesa_glsl_error(& lhs_loc
, state
, "array size must be > %u due to "
848 var
->data
.max_array_access
);
851 var
->type
= glsl_type::get_array_instance(lhs
->type
->element_type(),
852 rhs
->type
->array_size());
855 if (lhs
->type
->is_array()) {
856 mark_whole_array_access(rhs
);
857 mark_whole_array_access(lhs
);
861 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
862 * but not post_inc) need the converted assigned value as an rvalue
863 * to handle things like:
868 ir_variable
*var
= new(ctx
) ir_variable(rhs
->type
, "assignment_tmp",
870 instructions
->push_tail(var
);
871 instructions
->push_tail(assign(var
, rhs
));
873 if (!error_emitted
) {
874 ir_dereference_variable
*deref_var
= new(ctx
) ir_dereference_variable(var
);
875 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, deref_var
));
877 ir_rvalue
*rvalue
= new(ctx
) ir_dereference_variable(var
);
879 if (extract_channel
) {
880 rvalue
= new(ctx
) ir_expression(ir_binop_vector_extract
,
882 extract_channel
->clone(ctx
, NULL
));
885 *out_rvalue
= rvalue
;
888 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, rhs
));
892 return error_emitted
;
896 get_lvalue_copy(exec_list
*instructions
, ir_rvalue
*lvalue
)
898 void *ctx
= ralloc_parent(lvalue
);
901 var
= new(ctx
) ir_variable(lvalue
->type
, "_post_incdec_tmp",
903 instructions
->push_tail(var
);
904 var
->data
.mode
= ir_var_auto
;
906 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
909 return new(ctx
) ir_dereference_variable(var
);
914 ast_node::hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
923 ast_function_expression::hir_no_rvalue(exec_list
*instructions
,
924 struct _mesa_glsl_parse_state
*state
)
926 (void)hir(instructions
, state
);
930 ast_aggregate_initializer::hir_no_rvalue(exec_list
*instructions
,
931 struct _mesa_glsl_parse_state
*state
)
933 (void)hir(instructions
, state
);
937 do_comparison(void *mem_ctx
, int operation
, ir_rvalue
*op0
, ir_rvalue
*op1
)
940 ir_rvalue
*cmp
= NULL
;
942 if (operation
== ir_binop_all_equal
)
943 join_op
= ir_binop_logic_and
;
945 join_op
= ir_binop_logic_or
;
947 switch (op0
->type
->base_type
) {
948 case GLSL_TYPE_FLOAT
:
952 return new(mem_ctx
) ir_expression(operation
, op0
, op1
);
954 case GLSL_TYPE_ARRAY
: {
955 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
956 ir_rvalue
*e0
, *e1
, *result
;
958 e0
= new(mem_ctx
) ir_dereference_array(op0
->clone(mem_ctx
, NULL
),
959 new(mem_ctx
) ir_constant(i
));
960 e1
= new(mem_ctx
) ir_dereference_array(op1
->clone(mem_ctx
, NULL
),
961 new(mem_ctx
) ir_constant(i
));
962 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
965 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
971 mark_whole_array_access(op0
);
972 mark_whole_array_access(op1
);
976 case GLSL_TYPE_STRUCT
: {
977 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
978 ir_rvalue
*e0
, *e1
, *result
;
979 const char *field_name
= op0
->type
->fields
.structure
[i
].name
;
981 e0
= new(mem_ctx
) ir_dereference_record(op0
->clone(mem_ctx
, NULL
),
983 e1
= new(mem_ctx
) ir_dereference_record(op1
->clone(mem_ctx
, NULL
),
985 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
988 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
996 case GLSL_TYPE_ERROR
:
998 case GLSL_TYPE_SAMPLER
:
999 case GLSL_TYPE_IMAGE
:
1000 case GLSL_TYPE_INTERFACE
:
1001 case GLSL_TYPE_ATOMIC_UINT
:
1002 /* I assume a comparison of a struct containing a sampler just
1003 * ignores the sampler present in the type.
1009 cmp
= new(mem_ctx
) ir_constant(true);
1014 /* For logical operations, we want to ensure that the operands are
1015 * scalar booleans. If it isn't, emit an error and return a constant
1016 * boolean to avoid triggering cascading error messages.
1019 get_scalar_boolean_operand(exec_list
*instructions
,
1020 struct _mesa_glsl_parse_state
*state
,
1021 ast_expression
*parent_expr
,
1023 const char *operand_name
,
1024 bool *error_emitted
)
1026 ast_expression
*expr
= parent_expr
->subexpressions
[operand
];
1028 ir_rvalue
*val
= expr
->hir(instructions
, state
);
1030 if (val
->type
->is_boolean() && val
->type
->is_scalar())
1033 if (!*error_emitted
) {
1034 YYLTYPE loc
= expr
->get_location();
1035 _mesa_glsl_error(&loc
, state
, "%s of `%s' must be scalar boolean",
1037 parent_expr
->operator_string(parent_expr
->oper
));
1038 *error_emitted
= true;
1041 return new(ctx
) ir_constant(true);
1045 * If name refers to a builtin array whose maximum allowed size is less than
1046 * size, report an error and return true. Otherwise return false.
1049 check_builtin_array_max_size(const char *name
, unsigned size
,
1050 YYLTYPE loc
, struct _mesa_glsl_parse_state
*state
)
1052 if ((strcmp("gl_TexCoord", name
) == 0)
1053 && (size
> state
->Const
.MaxTextureCoords
)) {
1054 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1056 * "The size [of gl_TexCoord] can be at most
1057 * gl_MaxTextureCoords."
1059 _mesa_glsl_error(&loc
, state
, "`gl_TexCoord' array size cannot "
1060 "be larger than gl_MaxTextureCoords (%u)",
1061 state
->Const
.MaxTextureCoords
);
1062 } else if (strcmp("gl_ClipDistance", name
) == 0
1063 && size
> state
->Const
.MaxClipPlanes
) {
1064 /* From section 7.1 (Vertex Shader Special Variables) of the
1067 * "The gl_ClipDistance array is predeclared as unsized and
1068 * must be sized by the shader either redeclaring it with a
1069 * size or indexing it only with integral constant
1070 * expressions. ... The size can be at most
1071 * gl_MaxClipDistances."
1073 _mesa_glsl_error(&loc
, state
, "`gl_ClipDistance' array size cannot "
1074 "be larger than gl_MaxClipDistances (%u)",
1075 state
->Const
.MaxClipPlanes
);
1080 * Create the constant 1, of a which is appropriate for incrementing and
1081 * decrementing values of the given GLSL type. For example, if type is vec4,
1082 * this creates a constant value of 1.0 having type float.
1084 * If the given type is invalid for increment and decrement operators, return
1085 * a floating point 1--the error will be detected later.
1088 constant_one_for_inc_dec(void *ctx
, const glsl_type
*type
)
1090 switch (type
->base_type
) {
1091 case GLSL_TYPE_UINT
:
1092 return new(ctx
) ir_constant((unsigned) 1);
1094 return new(ctx
) ir_constant(1);
1096 case GLSL_TYPE_FLOAT
:
1097 return new(ctx
) ir_constant(1.0f
);
1102 ast_expression::hir(exec_list
*instructions
,
1103 struct _mesa_glsl_parse_state
*state
)
1105 return do_hir(instructions
, state
, true);
1109 ast_expression::hir_no_rvalue(exec_list
*instructions
,
1110 struct _mesa_glsl_parse_state
*state
)
1112 do_hir(instructions
, state
, false);
1116 ast_expression::do_hir(exec_list
*instructions
,
1117 struct _mesa_glsl_parse_state
*state
,
1121 static const int operations
[AST_NUM_OPERATORS
] = {
1122 -1, /* ast_assign doesn't convert to ir_expression. */
1123 -1, /* ast_plus doesn't convert to ir_expression. */
1137 ir_binop_any_nequal
,
1147 /* Note: The following block of expression types actually convert
1148 * to multiple IR instructions.
1150 ir_binop_mul
, /* ast_mul_assign */
1151 ir_binop_div
, /* ast_div_assign */
1152 ir_binop_mod
, /* ast_mod_assign */
1153 ir_binop_add
, /* ast_add_assign */
1154 ir_binop_sub
, /* ast_sub_assign */
1155 ir_binop_lshift
, /* ast_ls_assign */
1156 ir_binop_rshift
, /* ast_rs_assign */
1157 ir_binop_bit_and
, /* ast_and_assign */
1158 ir_binop_bit_xor
, /* ast_xor_assign */
1159 ir_binop_bit_or
, /* ast_or_assign */
1161 -1, /* ast_conditional doesn't convert to ir_expression. */
1162 ir_binop_add
, /* ast_pre_inc. */
1163 ir_binop_sub
, /* ast_pre_dec. */
1164 ir_binop_add
, /* ast_post_inc. */
1165 ir_binop_sub
, /* ast_post_dec. */
1166 -1, /* ast_field_selection doesn't conv to ir_expression. */
1167 -1, /* ast_array_index doesn't convert to ir_expression. */
1168 -1, /* ast_function_call doesn't conv to ir_expression. */
1169 -1, /* ast_identifier doesn't convert to ir_expression. */
1170 -1, /* ast_int_constant doesn't convert to ir_expression. */
1171 -1, /* ast_uint_constant doesn't conv to ir_expression. */
1172 -1, /* ast_float_constant doesn't conv to ir_expression. */
1173 -1, /* ast_bool_constant doesn't conv to ir_expression. */
1174 -1, /* ast_sequence doesn't convert to ir_expression. */
1176 ir_rvalue
*result
= NULL
;
1178 const struct glsl_type
*type
; /* a temporary variable for switch cases */
1179 bool error_emitted
= false;
1182 loc
= this->get_location();
1184 switch (this->oper
) {
1186 assert(!"ast_aggregate: Should never get here.");
1190 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1191 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1194 do_assignment(instructions
, state
,
1195 this->subexpressions
[0]->non_lvalue_description
,
1196 op
[0], op
[1], &result
, needs_rvalue
, false,
1197 this->subexpressions
[0]->get_location());
1202 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1204 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1206 error_emitted
= type
->is_error();
1212 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1214 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1216 error_emitted
= type
->is_error();
1218 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1226 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1227 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1229 type
= arithmetic_result_type(op
[0], op
[1],
1230 (this->oper
== ast_mul
),
1232 error_emitted
= type
->is_error();
1234 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1239 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1240 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1242 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1244 assert(operations
[this->oper
] == ir_binop_mod
);
1246 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1248 error_emitted
= type
->is_error();
1253 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1254 error_emitted
= true;
1257 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1258 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1259 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1261 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1263 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1270 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1271 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1273 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
1275 /* The relational operators must either generate an error or result
1276 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1278 assert(type
->is_error()
1279 || ((type
->base_type
== GLSL_TYPE_BOOL
)
1280 && type
->is_scalar()));
1282 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1284 error_emitted
= type
->is_error();
1289 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1290 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1292 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1294 * "The equality operators equal (==), and not equal (!=)
1295 * operate on all types. They result in a scalar Boolean. If
1296 * the operand types do not match, then there must be a
1297 * conversion from Section 4.1.10 "Implicit Conversions"
1298 * applied to one operand that can make them match, in which
1299 * case this conversion is done."
1301 if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
1302 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
1303 || (op
[0]->type
!= op
[1]->type
)) {
1304 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
1305 "type", (this->oper
== ast_equal
) ? "==" : "!=");
1306 error_emitted
= true;
1307 } else if ((op
[0]->type
->is_array() || op
[1]->type
->is_array()) &&
1308 !state
->check_version(120, 300, &loc
,
1309 "array comparisons forbidden")) {
1310 error_emitted
= true;
1311 } else if ((op
[0]->type
->contains_opaque() ||
1312 op
[1]->type
->contains_opaque())) {
1313 _mesa_glsl_error(&loc
, state
, "opaque type comparisons forbidden");
1314 error_emitted
= true;
1317 if (error_emitted
) {
1318 result
= new(ctx
) ir_constant(false);
1320 result
= do_comparison(ctx
, operations
[this->oper
], op
[0], op
[1]);
1321 assert(result
->type
== glsl_type::bool_type
);
1328 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1329 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1330 type
= bit_logic_result_type(op
[0]->type
, op
[1]->type
, this->oper
,
1332 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1334 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1338 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1340 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1341 error_emitted
= true;
1344 if (!op
[0]->type
->is_integer()) {
1345 _mesa_glsl_error(&loc
, state
, "operand of `~' must be an integer");
1346 error_emitted
= true;
1349 type
= error_emitted
? glsl_type::error_type
: op
[0]->type
;
1350 result
= new(ctx
) ir_expression(ir_unop_bit_not
, type
, op
[0], NULL
);
1353 case ast_logic_and
: {
1354 exec_list rhs_instructions
;
1355 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1356 "LHS", &error_emitted
);
1357 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1358 "RHS", &error_emitted
);
1360 if (rhs_instructions
.is_empty()) {
1361 result
= new(ctx
) ir_expression(ir_binop_logic_and
, op
[0], op
[1]);
1362 type
= result
->type
;
1364 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1367 instructions
->push_tail(tmp
);
1369 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1370 instructions
->push_tail(stmt
);
1372 stmt
->then_instructions
.append_list(&rhs_instructions
);
1373 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1374 ir_assignment
*const then_assign
=
1375 new(ctx
) ir_assignment(then_deref
, op
[1]);
1376 stmt
->then_instructions
.push_tail(then_assign
);
1378 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1379 ir_assignment
*const else_assign
=
1380 new(ctx
) ir_assignment(else_deref
, new(ctx
) ir_constant(false));
1381 stmt
->else_instructions
.push_tail(else_assign
);
1383 result
= new(ctx
) ir_dereference_variable(tmp
);
1389 case ast_logic_or
: {
1390 exec_list rhs_instructions
;
1391 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1392 "LHS", &error_emitted
);
1393 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1394 "RHS", &error_emitted
);
1396 if (rhs_instructions
.is_empty()) {
1397 result
= new(ctx
) ir_expression(ir_binop_logic_or
, op
[0], op
[1]);
1398 type
= result
->type
;
1400 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1403 instructions
->push_tail(tmp
);
1405 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1406 instructions
->push_tail(stmt
);
1408 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1409 ir_assignment
*const then_assign
=
1410 new(ctx
) ir_assignment(then_deref
, new(ctx
) ir_constant(true));
1411 stmt
->then_instructions
.push_tail(then_assign
);
1413 stmt
->else_instructions
.append_list(&rhs_instructions
);
1414 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1415 ir_assignment
*const else_assign
=
1416 new(ctx
) ir_assignment(else_deref
, op
[1]);
1417 stmt
->else_instructions
.push_tail(else_assign
);
1419 result
= new(ctx
) ir_dereference_variable(tmp
);
1426 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1428 * "The logical binary operators and (&&), or ( | | ), and
1429 * exclusive or (^^). They operate only on two Boolean
1430 * expressions and result in a Boolean expression."
1432 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0, "LHS",
1434 op
[1] = get_scalar_boolean_operand(instructions
, state
, this, 1, "RHS",
1437 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1442 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1443 "operand", &error_emitted
);
1445 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1449 case ast_mul_assign
:
1450 case ast_div_assign
:
1451 case ast_add_assign
:
1452 case ast_sub_assign
: {
1453 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1454 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1456 type
= arithmetic_result_type(op
[0], op
[1],
1457 (this->oper
== ast_mul_assign
),
1460 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1464 do_assignment(instructions
, state
,
1465 this->subexpressions
[0]->non_lvalue_description
,
1466 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1467 &result
, needs_rvalue
, false,
1468 this->subexpressions
[0]->get_location());
1470 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1471 * explicitly test for this because none of the binary expression
1472 * operators allow array operands either.
1478 case ast_mod_assign
: {
1479 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1480 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1482 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1484 assert(operations
[this->oper
] == ir_binop_mod
);
1486 ir_rvalue
*temp_rhs
;
1487 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1491 do_assignment(instructions
, state
,
1492 this->subexpressions
[0]->non_lvalue_description
,
1493 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1494 &result
, needs_rvalue
, false,
1495 this->subexpressions
[0]->get_location());
1500 case ast_rs_assign
: {
1501 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1502 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1503 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1505 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1506 type
, op
[0], op
[1]);
1508 do_assignment(instructions
, state
,
1509 this->subexpressions
[0]->non_lvalue_description
,
1510 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1511 &result
, needs_rvalue
, false,
1512 this->subexpressions
[0]->get_location());
1516 case ast_and_assign
:
1517 case ast_xor_assign
:
1518 case ast_or_assign
: {
1519 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1520 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1521 type
= bit_logic_result_type(op
[0]->type
, op
[1]->type
, this->oper
,
1523 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1524 type
, op
[0], op
[1]);
1526 do_assignment(instructions
, state
,
1527 this->subexpressions
[0]->non_lvalue_description
,
1528 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1529 &result
, needs_rvalue
, false,
1530 this->subexpressions
[0]->get_location());
1534 case ast_conditional
: {
1535 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1537 * "The ternary selection operator (?:). It operates on three
1538 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1539 * first expression, which must result in a scalar Boolean."
1541 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1542 "condition", &error_emitted
);
1544 /* The :? operator is implemented by generating an anonymous temporary
1545 * followed by an if-statement. The last instruction in each branch of
1546 * the if-statement assigns a value to the anonymous temporary. This
1547 * temporary is the r-value of the expression.
1549 exec_list then_instructions
;
1550 exec_list else_instructions
;
1552 op
[1] = this->subexpressions
[1]->hir(&then_instructions
, state
);
1553 op
[2] = this->subexpressions
[2]->hir(&else_instructions
, state
);
1555 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1557 * "The second and third expressions can be any type, as
1558 * long their types match, or there is a conversion in
1559 * Section 4.1.10 "Implicit Conversions" that can be applied
1560 * to one of the expressions to make their types match. This
1561 * resulting matching type is the type of the entire
1564 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1565 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1566 || (op
[1]->type
!= op
[2]->type
)) {
1567 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1569 _mesa_glsl_error(& loc
, state
, "second and third operands of ?: "
1570 "operator must have matching types");
1571 error_emitted
= true;
1572 type
= glsl_type::error_type
;
1577 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1579 * "The second and third expressions must be the same type, but can
1580 * be of any type other than an array."
1582 if (type
->is_array() &&
1583 !state
->check_version(120, 300, &loc
,
1584 "second and third operands of ?: operator "
1585 "cannot be arrays")) {
1586 error_emitted
= true;
1589 ir_constant
*cond_val
= op
[0]->constant_expression_value();
1590 ir_constant
*then_val
= op
[1]->constant_expression_value();
1591 ir_constant
*else_val
= op
[2]->constant_expression_value();
1593 if (then_instructions
.is_empty()
1594 && else_instructions
.is_empty()
1595 && (cond_val
!= NULL
) && (then_val
!= NULL
) && (else_val
!= NULL
)) {
1596 result
= (cond_val
->value
.b
[0]) ? then_val
: else_val
;
1598 ir_variable
*const tmp
=
1599 new(ctx
) ir_variable(type
, "conditional_tmp", ir_var_temporary
);
1600 instructions
->push_tail(tmp
);
1602 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1603 instructions
->push_tail(stmt
);
1605 then_instructions
.move_nodes_to(& stmt
->then_instructions
);
1606 ir_dereference
*const then_deref
=
1607 new(ctx
) ir_dereference_variable(tmp
);
1608 ir_assignment
*const then_assign
=
1609 new(ctx
) ir_assignment(then_deref
, op
[1]);
1610 stmt
->then_instructions
.push_tail(then_assign
);
1612 else_instructions
.move_nodes_to(& stmt
->else_instructions
);
1613 ir_dereference
*const else_deref
=
1614 new(ctx
) ir_dereference_variable(tmp
);
1615 ir_assignment
*const else_assign
=
1616 new(ctx
) ir_assignment(else_deref
, op
[2]);
1617 stmt
->else_instructions
.push_tail(else_assign
);
1619 result
= new(ctx
) ir_dereference_variable(tmp
);
1626 this->non_lvalue_description
= (this->oper
== ast_pre_inc
)
1627 ? "pre-increment operation" : "pre-decrement operation";
1629 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1630 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1632 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1634 ir_rvalue
*temp_rhs
;
1635 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1639 do_assignment(instructions
, state
,
1640 this->subexpressions
[0]->non_lvalue_description
,
1641 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1642 &result
, needs_rvalue
, false,
1643 this->subexpressions
[0]->get_location());
1648 case ast_post_dec
: {
1649 this->non_lvalue_description
= (this->oper
== ast_post_inc
)
1650 ? "post-increment operation" : "post-decrement operation";
1651 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1652 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1654 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1656 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1658 ir_rvalue
*temp_rhs
;
1659 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1662 /* Get a temporary of a copy of the lvalue before it's modified.
1663 * This may get thrown away later.
1665 result
= get_lvalue_copy(instructions
, op
[0]->clone(ctx
, NULL
));
1667 ir_rvalue
*junk_rvalue
;
1669 do_assignment(instructions
, state
,
1670 this->subexpressions
[0]->non_lvalue_description
,
1671 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1672 &junk_rvalue
, false, false,
1673 this->subexpressions
[0]->get_location());
1678 case ast_field_selection
:
1679 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1682 case ast_array_index
: {
1683 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1685 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1686 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1688 result
= _mesa_ast_array_index_to_hir(ctx
, state
, op
[0], op
[1],
1691 if (result
->type
->is_error())
1692 error_emitted
= true;
1697 case ast_function_call
:
1698 /* Should *NEVER* get here. ast_function_call should always be handled
1699 * by ast_function_expression::hir.
1704 case ast_identifier
: {
1705 /* ast_identifier can appear several places in a full abstract syntax
1706 * tree. This particular use must be at location specified in the grammar
1707 * as 'variable_identifier'.
1710 state
->symbols
->get_variable(this->primary_expression
.identifier
);
1713 var
->data
.used
= true;
1714 result
= new(ctx
) ir_dereference_variable(var
);
1716 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
1717 this->primary_expression
.identifier
);
1719 result
= ir_rvalue::error_value(ctx
);
1720 error_emitted
= true;
1725 case ast_int_constant
:
1726 result
= new(ctx
) ir_constant(this->primary_expression
.int_constant
);
1729 case ast_uint_constant
:
1730 result
= new(ctx
) ir_constant(this->primary_expression
.uint_constant
);
1733 case ast_float_constant
:
1734 result
= new(ctx
) ir_constant(this->primary_expression
.float_constant
);
1737 case ast_bool_constant
:
1738 result
= new(ctx
) ir_constant(bool(this->primary_expression
.bool_constant
));
1741 case ast_sequence
: {
1742 /* It should not be possible to generate a sequence in the AST without
1743 * any expressions in it.
1745 assert(!this->expressions
.is_empty());
1747 /* The r-value of a sequence is the last expression in the sequence. If
1748 * the other expressions in the sequence do not have side-effects (and
1749 * therefore add instructions to the instruction list), they get dropped
1752 exec_node
*previous_tail_pred
= NULL
;
1753 YYLTYPE previous_operand_loc
= loc
;
1755 foreach_list_typed (ast_node
, ast
, link
, &this->expressions
) {
1756 /* If one of the operands of comma operator does not generate any
1757 * code, we want to emit a warning. At each pass through the loop
1758 * previous_tail_pred will point to the last instruction in the
1759 * stream *before* processing the previous operand. Naturally,
1760 * instructions->tail_pred will point to the last instruction in the
1761 * stream *after* processing the previous operand. If the two
1762 * pointers match, then the previous operand had no effect.
1764 * The warning behavior here differs slightly from GCC. GCC will
1765 * only emit a warning if none of the left-hand operands have an
1766 * effect. However, it will emit a warning for each. I believe that
1767 * there are some cases in C (especially with GCC extensions) where
1768 * it is useful to have an intermediate step in a sequence have no
1769 * effect, but I don't think these cases exist in GLSL. Either way,
1770 * it would be a giant hassle to replicate that behavior.
1772 if (previous_tail_pred
== instructions
->tail_pred
) {
1773 _mesa_glsl_warning(&previous_operand_loc
, state
,
1774 "left-hand operand of comma expression has "
1778 /* tail_pred is directly accessed instead of using the get_tail()
1779 * method for performance reasons. get_tail() has extra code to
1780 * return NULL when the list is empty. We don't care about that
1781 * here, so using tail_pred directly is fine.
1783 previous_tail_pred
= instructions
->tail_pred
;
1784 previous_operand_loc
= ast
->get_location();
1786 result
= ast
->hir(instructions
, state
);
1789 /* Any errors should have already been emitted in the loop above.
1791 error_emitted
= true;
1795 type
= NULL
; /* use result->type, not type. */
1796 assert(result
!= NULL
|| !needs_rvalue
);
1798 if (result
&& result
->type
->is_error() && !error_emitted
)
1799 _mesa_glsl_error(& loc
, state
, "type mismatch");
1806 ast_expression_statement::hir(exec_list
*instructions
,
1807 struct _mesa_glsl_parse_state
*state
)
1809 /* It is possible to have expression statements that don't have an
1810 * expression. This is the solitary semicolon:
1812 * for (i = 0; i < 5; i++)
1815 * In this case the expression will be NULL. Test for NULL and don't do
1816 * anything in that case.
1818 if (expression
!= NULL
)
1819 expression
->hir_no_rvalue(instructions
, state
);
1821 /* Statements do not have r-values.
1828 ast_compound_statement::hir(exec_list
*instructions
,
1829 struct _mesa_glsl_parse_state
*state
)
1832 state
->symbols
->push_scope();
1834 foreach_list_typed (ast_node
, ast
, link
, &this->statements
)
1835 ast
->hir(instructions
, state
);
1838 state
->symbols
->pop_scope();
1840 /* Compound statements do not have r-values.
1846 * Evaluate the given exec_node (which should be an ast_node representing
1847 * a single array dimension) and return its integer value.
1850 process_array_size(exec_node
*node
,
1851 struct _mesa_glsl_parse_state
*state
)
1853 exec_list dummy_instructions
;
1855 ast_node
*array_size
= exec_node_data(ast_node
, node
, link
);
1856 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
1857 YYLTYPE loc
= array_size
->get_location();
1860 _mesa_glsl_error(& loc
, state
,
1861 "array size could not be resolved");
1865 if (!ir
->type
->is_integer()) {
1866 _mesa_glsl_error(& loc
, state
,
1867 "array size must be integer type");
1871 if (!ir
->type
->is_scalar()) {
1872 _mesa_glsl_error(& loc
, state
,
1873 "array size must be scalar type");
1877 ir_constant
*const size
= ir
->constant_expression_value();
1879 _mesa_glsl_error(& loc
, state
, "array size must be a "
1880 "constant valued expression");
1884 if (size
->value
.i
[0] <= 0) {
1885 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
1889 assert(size
->type
== ir
->type
);
1891 /* If the array size is const (and we've verified that
1892 * it is) then no instructions should have been emitted
1893 * when we converted it to HIR. If they were emitted,
1894 * then either the array size isn't const after all, or
1895 * we are emitting unnecessary instructions.
1897 assert(dummy_instructions
.is_empty());
1899 return size
->value
.u
[0];
1902 static const glsl_type
*
1903 process_array_type(YYLTYPE
*loc
, const glsl_type
*base
,
1904 ast_array_specifier
*array_specifier
,
1905 struct _mesa_glsl_parse_state
*state
)
1907 const glsl_type
*array_type
= base
;
1909 if (array_specifier
!= NULL
) {
1910 if (base
->is_array()) {
1912 /* From page 19 (page 25) of the GLSL 1.20 spec:
1914 * "Only one-dimensional arrays may be declared."
1916 if (!state
->ARB_arrays_of_arrays_enable
) {
1917 _mesa_glsl_error(loc
, state
,
1918 "invalid array of `%s'"
1919 "GL_ARB_arrays_of_arrays "
1920 "required for defining arrays of arrays",
1922 return glsl_type::error_type
;
1925 if (base
->length
== 0) {
1926 _mesa_glsl_error(loc
, state
,
1927 "only the outermost array dimension can "
1930 return glsl_type::error_type
;
1934 for (exec_node
*node
= array_specifier
->array_dimensions
.tail_pred
;
1935 !node
->is_head_sentinel(); node
= node
->prev
) {
1936 unsigned array_size
= process_array_size(node
, state
);
1937 array_type
= glsl_type::get_array_instance(array_type
, array_size
);
1940 if (array_specifier
->is_unsized_array
)
1941 array_type
= glsl_type::get_array_instance(array_type
, 0);
1949 ast_type_specifier::glsl_type(const char **name
,
1950 struct _mesa_glsl_parse_state
*state
) const
1952 const struct glsl_type
*type
;
1954 type
= state
->symbols
->get_type(this->type_name
);
1955 *name
= this->type_name
;
1957 YYLTYPE loc
= this->get_location();
1958 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
1964 ast_fully_specified_type::glsl_type(const char **name
,
1965 struct _mesa_glsl_parse_state
*state
) const
1967 const struct glsl_type
*type
= this->specifier
->glsl_type(name
, state
);
1972 if (type
->base_type
== GLSL_TYPE_FLOAT
1974 && state
->stage
== MESA_SHADER_FRAGMENT
1975 && this->qualifier
.precision
== ast_precision_none
1976 && state
->symbols
->get_variable("#default precision") == NULL
) {
1977 YYLTYPE loc
= this->get_location();
1978 _mesa_glsl_error(&loc
, state
,
1979 "no precision specified this scope for type `%s'",
1987 * Determine whether a toplevel variable declaration declares a varying. This
1988 * function operates by examining the variable's mode and the shader target,
1989 * so it correctly identifies linkage variables regardless of whether they are
1990 * declared using the deprecated "varying" syntax or the new "in/out" syntax.
1992 * Passing a non-toplevel variable declaration (e.g. a function parameter) to
1993 * this function will produce undefined results.
1996 is_varying_var(ir_variable
*var
, gl_shader_stage target
)
1999 case MESA_SHADER_VERTEX
:
2000 return var
->data
.mode
== ir_var_shader_out
;
2001 case MESA_SHADER_FRAGMENT
:
2002 return var
->data
.mode
== ir_var_shader_in
;
2004 return var
->data
.mode
== ir_var_shader_out
|| var
->data
.mode
== ir_var_shader_in
;
2010 * Matrix layout qualifiers are only allowed on certain types
2013 validate_matrix_layout_for_type(struct _mesa_glsl_parse_state
*state
,
2015 const glsl_type
*type
,
2018 if (var
&& !var
->is_in_uniform_block()) {
2019 /* Layout qualifiers may only apply to interface blocks and fields in
2022 _mesa_glsl_error(loc
, state
,
2023 "uniform block layout qualifiers row_major and "
2024 "column_major may not be applied to variables "
2025 "outside of uniform blocks");
2026 } else if (!type
->is_matrix()) {
2027 /* The OpenGL ES 3.0 conformance tests did not originally allow
2028 * matrix layout qualifiers on non-matrices. However, the OpenGL
2029 * 4.4 and OpenGL ES 3.0 (revision TBD) specifications were
2030 * amended to specifically allow these layouts on all types. Emit
2031 * a warning so that people know their code may not be portable.
2033 _mesa_glsl_warning(loc
, state
,
2034 "uniform block layout qualifiers row_major and "
2035 "column_major applied to non-matrix types may "
2036 "be rejected by older compilers");
2037 } else if (type
->is_record()) {
2038 /* We allow 'layout(row_major)' on structure types because it's the only
2039 * way to get row-major layouts on matrices contained in structures.
2041 _mesa_glsl_warning(loc
, state
,
2042 "uniform block layout qualifiers row_major and "
2043 "column_major applied to structure types is not "
2044 "strictly conformant and may be rejected by other "
2050 validate_binding_qualifier(struct _mesa_glsl_parse_state
*state
,
2053 const ast_type_qualifier
*qual
)
2055 if (var
->data
.mode
!= ir_var_uniform
) {
2056 _mesa_glsl_error(loc
, state
,
2057 "the \"binding\" qualifier only applies to uniforms");
2061 if (qual
->binding
< 0) {
2062 _mesa_glsl_error(loc
, state
, "binding values must be >= 0");
2066 const struct gl_context
*const ctx
= state
->ctx
;
2067 unsigned elements
= var
->type
->is_array() ? var
->type
->length
: 1;
2068 unsigned max_index
= qual
->binding
+ elements
- 1;
2070 if (var
->type
->is_interface()) {
2071 /* UBOs. From page 60 of the GLSL 4.20 specification:
2072 * "If the binding point for any uniform block instance is less than zero,
2073 * or greater than or equal to the implementation-dependent maximum
2074 * number of uniform buffer bindings, a compilation error will occur.
2075 * When the binding identifier is used with a uniform block instanced as
2076 * an array of size N, all elements of the array from binding through
2077 * binding + N – 1 must be within this range."
2079 * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
2081 if (max_index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2082 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d UBOs exceeds "
2083 "the maximum number of UBO binding points (%d)",
2084 qual
->binding
, elements
,
2085 ctx
->Const
.MaxUniformBufferBindings
);
2088 } else if (var
->type
->is_sampler() ||
2089 (var
->type
->is_array() && var
->type
->fields
.array
->is_sampler())) {
2090 /* Samplers. From page 63 of the GLSL 4.20 specification:
2091 * "If the binding is less than zero, or greater than or equal to the
2092 * implementation-dependent maximum supported number of units, a
2093 * compilation error will occur. When the binding identifier is used
2094 * with an array of size N, all elements of the array from binding
2095 * through binding + N - 1 must be within this range."
2097 unsigned limit
= ctx
->Const
.Program
[state
->stage
].MaxTextureImageUnits
;
2099 if (max_index
>= limit
) {
2100 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d samplers "
2101 "exceeds the maximum number of texture image units "
2102 "(%d)", qual
->binding
, elements
, limit
);
2106 } else if (var
->type
->contains_atomic()) {
2107 assert(ctx
->Const
.MaxAtomicBufferBindings
<= MAX_COMBINED_ATOMIC_BUFFERS
);
2108 if (unsigned(qual
->binding
) >= ctx
->Const
.MaxAtomicBufferBindings
) {
2109 _mesa_glsl_error(loc
, state
, "layout(binding = %d) exceeds the "
2110 " maximum number of atomic counter buffer bindings"
2111 "(%d)", qual
->binding
,
2112 ctx
->Const
.MaxAtomicBufferBindings
);
2117 _mesa_glsl_error(loc
, state
,
2118 "the \"binding\" qualifier only applies to uniform "
2119 "blocks, samplers, atomic counters, or arrays thereof");
2127 static glsl_interp_qualifier
2128 interpret_interpolation_qualifier(const struct ast_type_qualifier
*qual
,
2129 ir_variable_mode mode
,
2130 struct _mesa_glsl_parse_state
*state
,
2133 glsl_interp_qualifier interpolation
;
2134 if (qual
->flags
.q
.flat
)
2135 interpolation
= INTERP_QUALIFIER_FLAT
;
2136 else if (qual
->flags
.q
.noperspective
)
2137 interpolation
= INTERP_QUALIFIER_NOPERSPECTIVE
;
2138 else if (qual
->flags
.q
.smooth
)
2139 interpolation
= INTERP_QUALIFIER_SMOOTH
;
2141 interpolation
= INTERP_QUALIFIER_NONE
;
2143 if (interpolation
!= INTERP_QUALIFIER_NONE
) {
2144 if (mode
!= ir_var_shader_in
&& mode
!= ir_var_shader_out
) {
2145 _mesa_glsl_error(loc
, state
,
2146 "interpolation qualifier `%s' can only be applied to "
2147 "shader inputs or outputs.",
2148 interpolation_string(interpolation
));
2152 if ((state
->stage
== MESA_SHADER_VERTEX
&& mode
== ir_var_shader_in
) ||
2153 (state
->stage
== MESA_SHADER_FRAGMENT
&& mode
== ir_var_shader_out
)) {
2154 _mesa_glsl_error(loc
, state
,
2155 "interpolation qualifier `%s' cannot be applied to "
2156 "vertex shader inputs or fragment shader outputs",
2157 interpolation_string(interpolation
));
2161 return interpolation
;
2166 validate_explicit_location(const struct ast_type_qualifier
*qual
,
2168 struct _mesa_glsl_parse_state
*state
,
2173 /* Between GL_ARB_explicit_attrib_location an
2174 * GL_ARB_separate_shader_objects, the inputs and outputs of any shader
2175 * stage can be assigned explicit locations. The checking here associates
2176 * the correct extension with the correct stage's input / output:
2180 * vertex explicit_loc sso
2182 * fragment sso explicit_loc
2184 switch (state
->stage
) {
2185 case MESA_SHADER_VERTEX
:
2186 if (var
->data
.mode
== ir_var_shader_in
) {
2187 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
2193 if (var
->data
.mode
== ir_var_shader_out
) {
2194 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
2203 case MESA_SHADER_GEOMETRY
:
2204 if (var
->data
.mode
== ir_var_shader_in
|| var
->data
.mode
== ir_var_shader_out
) {
2205 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
2214 case MESA_SHADER_FRAGMENT
:
2215 if (var
->data
.mode
== ir_var_shader_in
) {
2216 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
2222 if (var
->data
.mode
== ir_var_shader_out
) {
2223 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
2232 case MESA_SHADER_COMPUTE
:
2233 _mesa_glsl_error(loc
, state
,
2234 "compute shader variables cannot be given "
2235 "explicit locations");
2240 _mesa_glsl_error(loc
, state
,
2241 "%s cannot be given an explicit location in %s shader",
2243 _mesa_shader_stage_to_string(state
->stage
));
2245 var
->data
.explicit_location
= true;
2247 /* This bit of silliness is needed because invalid explicit locations
2248 * are supposed to be flagged during linking. Small negative values
2249 * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
2250 * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
2251 * The linker needs to be able to differentiate these cases. This
2252 * ensures that negative values stay negative.
2254 if (qual
->location
>= 0) {
2255 switch (state
->stage
) {
2256 case MESA_SHADER_VERTEX
:
2257 var
->data
.location
= (var
->data
.mode
== ir_var_shader_in
)
2258 ? (qual
->location
+ VERT_ATTRIB_GENERIC0
)
2259 : (qual
->location
+ VARYING_SLOT_VAR0
);
2262 case MESA_SHADER_GEOMETRY
:
2263 var
->data
.location
= qual
->location
+ VARYING_SLOT_VAR0
;
2266 case MESA_SHADER_FRAGMENT
:
2267 var
->data
.location
= (var
->data
.mode
== ir_var_shader_out
)
2268 ? (qual
->location
+ FRAG_RESULT_DATA0
)
2269 : (qual
->location
+ VARYING_SLOT_VAR0
);
2271 case MESA_SHADER_COMPUTE
:
2272 assert(!"Unexpected shader type");
2276 var
->data
.location
= qual
->location
;
2279 if (qual
->flags
.q
.explicit_index
) {
2280 /* From the GLSL 4.30 specification, section 4.4.2 (Output
2281 * Layout Qualifiers):
2283 * "It is also a compile-time error if a fragment shader
2284 * sets a layout index to less than 0 or greater than 1."
2286 * Older specifications don't mandate a behavior; we take
2287 * this as a clarification and always generate the error.
2289 if (qual
->index
< 0 || qual
->index
> 1) {
2290 _mesa_glsl_error(loc
, state
,
2291 "explicit index may only be 0 or 1");
2293 var
->data
.explicit_index
= true;
2294 var
->data
.index
= qual
->index
;
2301 apply_image_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
2303 struct _mesa_glsl_parse_state
*state
,
2306 const glsl_type
*base_type
=
2307 (var
->type
->is_array() ? var
->type
->element_type() : var
->type
);
2309 if (base_type
->is_image()) {
2310 if (var
->data
.mode
!= ir_var_uniform
&&
2311 var
->data
.mode
!= ir_var_function_in
) {
2312 _mesa_glsl_error(loc
, state
, "image variables may only be declared as "
2313 "function parameters or uniform-qualified "
2314 "global variables");
2317 var
->data
.image
.read_only
|= qual
->flags
.q
.read_only
;
2318 var
->data
.image
.write_only
|= qual
->flags
.q
.write_only
;
2319 var
->data
.image
.coherent
|= qual
->flags
.q
.coherent
;
2320 var
->data
.image
._volatile
|= qual
->flags
.q
._volatile
;
2321 var
->data
.image
.restrict_flag
|= qual
->flags
.q
.restrict_flag
;
2322 var
->data
.read_only
= true;
2324 if (qual
->flags
.q
.explicit_image_format
) {
2325 if (var
->data
.mode
== ir_var_function_in
) {
2326 _mesa_glsl_error(loc
, state
, "format qualifiers cannot be "
2327 "used on image function parameters");
2330 if (qual
->image_base_type
!= base_type
->sampler_type
) {
2331 _mesa_glsl_error(loc
, state
, "format qualifier doesn't match the "
2332 "base data type of the image");
2335 var
->data
.image
.format
= qual
->image_format
;
2337 if (var
->data
.mode
== ir_var_uniform
&& !qual
->flags
.q
.write_only
) {
2338 _mesa_glsl_error(loc
, state
, "uniforms not qualified with "
2339 "`writeonly' must have a format layout "
2343 var
->data
.image
.format
= GL_NONE
;
2348 static inline const char*
2349 get_layout_qualifier_string(bool origin_upper_left
, bool pixel_center_integer
)
2351 if (origin_upper_left
&& pixel_center_integer
)
2352 return "origin_upper_left, pixel_center_integer";
2353 else if (origin_upper_left
)
2354 return "origin_upper_left";
2355 else if (pixel_center_integer
)
2356 return "pixel_center_integer";
2362 is_conflicting_fragcoord_redeclaration(struct _mesa_glsl_parse_state
*state
,
2363 const struct ast_type_qualifier
*qual
)
2365 /* If gl_FragCoord was previously declared, and the qualifiers were
2366 * different in any way, return true.
2368 if (state
->fs_redeclares_gl_fragcoord
) {
2369 return (state
->fs_pixel_center_integer
!= qual
->flags
.q
.pixel_center_integer
2370 || state
->fs_origin_upper_left
!= qual
->flags
.q
.origin_upper_left
);
2377 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
2379 struct _mesa_glsl_parse_state
*state
,
2383 STATIC_ASSERT(sizeof(qual
->flags
.q
) <= sizeof(qual
->flags
.i
));
2385 if (qual
->flags
.q
.invariant
) {
2386 if (var
->data
.used
) {
2387 _mesa_glsl_error(loc
, state
,
2388 "variable `%s' may not be redeclared "
2389 "`invariant' after being used",
2392 var
->data
.invariant
= 1;
2396 if (qual
->flags
.q
.precise
) {
2397 if (var
->data
.used
) {
2398 _mesa_glsl_error(loc
, state
,
2399 "variable `%s' may not be redeclared "
2400 "`precise' after being used",
2403 var
->data
.precise
= 1;
2407 if (qual
->flags
.q
.constant
|| qual
->flags
.q
.attribute
2408 || qual
->flags
.q
.uniform
2409 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
2410 var
->data
.read_only
= 1;
2412 if (qual
->flags
.q
.centroid
)
2413 var
->data
.centroid
= 1;
2415 if (qual
->flags
.q
.sample
)
2416 var
->data
.sample
= 1;
2418 if (qual
->flags
.q
.attribute
&& state
->stage
!= MESA_SHADER_VERTEX
) {
2419 var
->type
= glsl_type::error_type
;
2420 _mesa_glsl_error(loc
, state
,
2421 "`attribute' variables may not be declared in the "
2423 _mesa_shader_stage_to_string(state
->stage
));
2426 /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
2428 * "However, the const qualifier cannot be used with out or inout."
2430 * The same section of the GLSL 4.40 spec further clarifies this saying:
2432 * "The const qualifier cannot be used with out or inout, or a
2433 * compile-time error results."
2435 if (is_parameter
&& qual
->flags
.q
.constant
&& qual
->flags
.q
.out
) {
2436 _mesa_glsl_error(loc
, state
,
2437 "`const' may not be applied to `out' or `inout' "
2438 "function parameters");
2441 /* If there is no qualifier that changes the mode of the variable, leave
2442 * the setting alone.
2444 if (qual
->flags
.q
.in
&& qual
->flags
.q
.out
)
2445 var
->data
.mode
= ir_var_function_inout
;
2446 else if (qual
->flags
.q
.in
)
2447 var
->data
.mode
= is_parameter
? ir_var_function_in
: ir_var_shader_in
;
2448 else if (qual
->flags
.q
.attribute
2449 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
2450 var
->data
.mode
= ir_var_shader_in
;
2451 else if (qual
->flags
.q
.out
)
2452 var
->data
.mode
= is_parameter
? ir_var_function_out
: ir_var_shader_out
;
2453 else if (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_VERTEX
))
2454 var
->data
.mode
= ir_var_shader_out
;
2455 else if (qual
->flags
.q
.uniform
)
2456 var
->data
.mode
= ir_var_uniform
;
2458 if (!is_parameter
&& is_varying_var(var
, state
->stage
)) {
2459 /* User-defined ins/outs are not permitted in compute shaders. */
2460 if (state
->stage
== MESA_SHADER_COMPUTE
) {
2461 _mesa_glsl_error(loc
, state
,
2462 "user-defined input and output variables are not "
2463 "permitted in compute shaders");
2466 /* This variable is being used to link data between shader stages (in
2467 * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
2468 * that is allowed for such purposes.
2470 * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
2472 * "The varying qualifier can be used only with the data types
2473 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
2476 * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
2477 * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
2479 * "Fragment inputs can only be signed and unsigned integers and
2480 * integer vectors, float, floating-point vectors, matrices, or
2481 * arrays of these. Structures cannot be input.
2483 * Similar text exists in the section on vertex shader outputs.
2485 * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
2486 * 3.00 spec allows structs as well. Varying structs are also allowed
2489 switch (var
->type
->get_scalar_type()->base_type
) {
2490 case GLSL_TYPE_FLOAT
:
2491 /* Ok in all GLSL versions */
2493 case GLSL_TYPE_UINT
:
2495 if (state
->is_version(130, 300))
2497 _mesa_glsl_error(loc
, state
,
2498 "varying variables must be of base type float in %s",
2499 state
->get_version_string());
2501 case GLSL_TYPE_STRUCT
:
2502 if (state
->is_version(150, 300))
2504 _mesa_glsl_error(loc
, state
,
2505 "varying variables may not be of type struct");
2508 _mesa_glsl_error(loc
, state
, "illegal type for a varying variable");
2513 if (state
->all_invariant
&& (state
->current_function
== NULL
)) {
2514 switch (state
->stage
) {
2515 case MESA_SHADER_VERTEX
:
2516 if (var
->data
.mode
== ir_var_shader_out
)
2517 var
->data
.invariant
= true;
2519 case MESA_SHADER_GEOMETRY
:
2520 if ((var
->data
.mode
== ir_var_shader_in
)
2521 || (var
->data
.mode
== ir_var_shader_out
))
2522 var
->data
.invariant
= true;
2524 case MESA_SHADER_FRAGMENT
:
2525 if (var
->data
.mode
== ir_var_shader_in
)
2526 var
->data
.invariant
= true;
2528 case MESA_SHADER_COMPUTE
:
2529 /* Invariance isn't meaningful in compute shaders. */
2534 var
->data
.interpolation
=
2535 interpret_interpolation_qualifier(qual
, (ir_variable_mode
) var
->data
.mode
,
2538 var
->data
.pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
2539 var
->data
.origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
2540 if ((qual
->flags
.q
.origin_upper_left
|| qual
->flags
.q
.pixel_center_integer
)
2541 && (strcmp(var
->name
, "gl_FragCoord") != 0)) {
2542 const char *const qual_string
= (qual
->flags
.q
.origin_upper_left
)
2543 ? "origin_upper_left" : "pixel_center_integer";
2545 _mesa_glsl_error(loc
, state
,
2546 "layout qualifier `%s' can only be applied to "
2547 "fragment shader input `gl_FragCoord'",
2551 if (var
->name
!= NULL
&& strcmp(var
->name
, "gl_FragCoord") == 0) {
2553 /* Section 4.3.8.1, page 39 of GLSL 1.50 spec says:
2555 * "Within any shader, the first redeclarations of gl_FragCoord
2556 * must appear before any use of gl_FragCoord."
2558 * Generate a compiler error if above condition is not met by the
2561 ir_variable
*earlier
= state
->symbols
->get_variable("gl_FragCoord");
2562 if (earlier
!= NULL
&&
2563 earlier
->data
.used
&&
2564 !state
->fs_redeclares_gl_fragcoord
) {
2565 _mesa_glsl_error(loc
, state
,
2566 "gl_FragCoord used before its first redeclaration "
2567 "in fragment shader");
2570 /* Make sure all gl_FragCoord redeclarations specify the same layout
2573 if (is_conflicting_fragcoord_redeclaration(state
, qual
)) {
2574 const char *const qual_string
=
2575 get_layout_qualifier_string(qual
->flags
.q
.origin_upper_left
,
2576 qual
->flags
.q
.pixel_center_integer
);
2578 const char *const state_string
=
2579 get_layout_qualifier_string(state
->fs_origin_upper_left
,
2580 state
->fs_pixel_center_integer
);
2582 _mesa_glsl_error(loc
, state
,
2583 "gl_FragCoord redeclared with different layout "
2584 "qualifiers (%s) and (%s) ",
2588 state
->fs_origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
2589 state
->fs_pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
2590 state
->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers
=
2591 !qual
->flags
.q
.origin_upper_left
&& !qual
->flags
.q
.pixel_center_integer
;
2592 state
->fs_redeclares_gl_fragcoord
=
2593 state
->fs_origin_upper_left
||
2594 state
->fs_pixel_center_integer
||
2595 state
->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers
;
2598 if (qual
->flags
.q
.explicit_location
) {
2599 validate_explicit_location(qual
, var
, state
, loc
);
2600 } else if (qual
->flags
.q
.explicit_index
) {
2601 _mesa_glsl_error(loc
, state
, "explicit index requires explicit location");
2604 if (qual
->flags
.q
.explicit_binding
&&
2605 validate_binding_qualifier(state
, loc
, var
, qual
)) {
2606 var
->data
.explicit_binding
= true;
2607 var
->data
.binding
= qual
->binding
;
2610 if (var
->type
->contains_atomic()) {
2611 if (var
->data
.mode
== ir_var_uniform
) {
2612 if (var
->data
.explicit_binding
) {
2614 &state
->atomic_counter_offsets
[var
->data
.binding
];
2616 if (*offset
% ATOMIC_COUNTER_SIZE
)
2617 _mesa_glsl_error(loc
, state
,
2618 "misaligned atomic counter offset");
2620 var
->data
.atomic
.offset
= *offset
;
2621 *offset
+= var
->type
->atomic_size();
2624 _mesa_glsl_error(loc
, state
,
2625 "atomic counters require explicit binding point");
2627 } else if (var
->data
.mode
!= ir_var_function_in
) {
2628 _mesa_glsl_error(loc
, state
, "atomic counters may only be declared as "
2629 "function parameters or uniform-qualified "
2630 "global variables");
2634 /* Does the declaration use the deprecated 'attribute' or 'varying'
2637 const bool uses_deprecated_qualifier
= qual
->flags
.q
.attribute
2638 || qual
->flags
.q
.varying
;
2640 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
2641 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
2642 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
2643 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
2644 * These extensions and all following extensions that add the 'layout'
2645 * keyword have been modified to require the use of 'in' or 'out'.
2647 * The following extension do not allow the deprecated keywords:
2649 * GL_AMD_conservative_depth
2650 * GL_ARB_conservative_depth
2651 * GL_ARB_gpu_shader5
2652 * GL_ARB_separate_shader_objects
2653 * GL_ARB_tesselation_shader
2654 * GL_ARB_transform_feedback3
2655 * GL_ARB_uniform_buffer_object
2657 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
2658 * allow layout with the deprecated keywords.
2660 const bool relaxed_layout_qualifier_checking
=
2661 state
->ARB_fragment_coord_conventions_enable
;
2663 if (qual
->has_layout() && uses_deprecated_qualifier
) {
2664 if (relaxed_layout_qualifier_checking
) {
2665 _mesa_glsl_warning(loc
, state
,
2666 "`layout' qualifier may not be used with "
2667 "`attribute' or `varying'");
2669 _mesa_glsl_error(loc
, state
,
2670 "`layout' qualifier may not be used with "
2671 "`attribute' or `varying'");
2675 /* Layout qualifiers for gl_FragDepth, which are enabled by extension
2676 * AMD_conservative_depth.
2678 int depth_layout_count
= qual
->flags
.q
.depth_any
2679 + qual
->flags
.q
.depth_greater
2680 + qual
->flags
.q
.depth_less
2681 + qual
->flags
.q
.depth_unchanged
;
2682 if (depth_layout_count
> 0
2683 && !state
->AMD_conservative_depth_enable
2684 && !state
->ARB_conservative_depth_enable
) {
2685 _mesa_glsl_error(loc
, state
,
2686 "extension GL_AMD_conservative_depth or "
2687 "GL_ARB_conservative_depth must be enabled "
2688 "to use depth layout qualifiers");
2689 } else if (depth_layout_count
> 0
2690 && strcmp(var
->name
, "gl_FragDepth") != 0) {
2691 _mesa_glsl_error(loc
, state
,
2692 "depth layout qualifiers can be applied only to "
2694 } else if (depth_layout_count
> 1
2695 && strcmp(var
->name
, "gl_FragDepth") == 0) {
2696 _mesa_glsl_error(loc
, state
,
2697 "at most one depth layout qualifier can be applied to "
2700 if (qual
->flags
.q
.depth_any
)
2701 var
->data
.depth_layout
= ir_depth_layout_any
;
2702 else if (qual
->flags
.q
.depth_greater
)
2703 var
->data
.depth_layout
= ir_depth_layout_greater
;
2704 else if (qual
->flags
.q
.depth_less
)
2705 var
->data
.depth_layout
= ir_depth_layout_less
;
2706 else if (qual
->flags
.q
.depth_unchanged
)
2707 var
->data
.depth_layout
= ir_depth_layout_unchanged
;
2709 var
->data
.depth_layout
= ir_depth_layout_none
;
2711 if (qual
->flags
.q
.std140
||
2712 qual
->flags
.q
.packed
||
2713 qual
->flags
.q
.shared
) {
2714 _mesa_glsl_error(loc
, state
,
2715 "uniform block layout qualifiers std140, packed, and "
2716 "shared can only be applied to uniform blocks, not "
2720 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
2721 validate_matrix_layout_for_type(state
, loc
, var
->type
, var
);
2724 if (var
->type
->contains_image())
2725 apply_image_qualifier_to_variable(qual
, var
, state
, loc
);
2729 * Get the variable that is being redeclared by this declaration
2731 * Semantic checks to verify the validity of the redeclaration are also
2732 * performed. If semantic checks fail, compilation error will be emitted via
2733 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
2736 * A pointer to an existing variable in the current scope if the declaration
2737 * is a redeclaration, \c NULL otherwise.
2739 static ir_variable
*
2740 get_variable_being_redeclared(ir_variable
*var
, YYLTYPE loc
,
2741 struct _mesa_glsl_parse_state
*state
,
2742 bool allow_all_redeclarations
)
2744 /* Check if this declaration is actually a re-declaration, either to
2745 * resize an array or add qualifiers to an existing variable.
2747 * This is allowed for variables in the current scope, or when at
2748 * global scope (for built-ins in the implicit outer scope).
2750 ir_variable
*earlier
= state
->symbols
->get_variable(var
->name
);
2751 if (earlier
== NULL
||
2752 (state
->current_function
!= NULL
&&
2753 !state
->symbols
->name_declared_this_scope(var
->name
))) {
2758 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
2760 * "It is legal to declare an array without a size and then
2761 * later re-declare the same name as an array of the same
2762 * type and specify a size."
2764 if (earlier
->type
->is_unsized_array() && var
->type
->is_array()
2765 && (var
->type
->element_type() == earlier
->type
->element_type())) {
2766 /* FINISHME: This doesn't match the qualifiers on the two
2767 * FINISHME: declarations. It's not 100% clear whether this is
2768 * FINISHME: required or not.
2771 const unsigned size
= unsigned(var
->type
->array_size());
2772 check_builtin_array_max_size(var
->name
, size
, loc
, state
);
2773 if ((size
> 0) && (size
<= earlier
->data
.max_array_access
)) {
2774 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
2776 earlier
->data
.max_array_access
);
2779 earlier
->type
= var
->type
;
2782 } else if ((state
->ARB_fragment_coord_conventions_enable
||
2783 state
->is_version(150, 0))
2784 && strcmp(var
->name
, "gl_FragCoord") == 0
2785 && earlier
->type
== var
->type
2786 && earlier
->data
.mode
== var
->data
.mode
) {
2787 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
2790 earlier
->data
.origin_upper_left
= var
->data
.origin_upper_left
;
2791 earlier
->data
.pixel_center_integer
= var
->data
.pixel_center_integer
;
2793 /* According to section 4.3.7 of the GLSL 1.30 spec,
2794 * the following built-in varaibles can be redeclared with an
2795 * interpolation qualifier:
2798 * * gl_FrontSecondaryColor
2799 * * gl_BackSecondaryColor
2801 * * gl_SecondaryColor
2803 } else if (state
->is_version(130, 0)
2804 && (strcmp(var
->name
, "gl_FrontColor") == 0
2805 || strcmp(var
->name
, "gl_BackColor") == 0
2806 || strcmp(var
->name
, "gl_FrontSecondaryColor") == 0
2807 || strcmp(var
->name
, "gl_BackSecondaryColor") == 0
2808 || strcmp(var
->name
, "gl_Color") == 0
2809 || strcmp(var
->name
, "gl_SecondaryColor") == 0)
2810 && earlier
->type
== var
->type
2811 && earlier
->data
.mode
== var
->data
.mode
) {
2812 earlier
->data
.interpolation
= var
->data
.interpolation
;
2814 /* Layout qualifiers for gl_FragDepth. */
2815 } else if ((state
->AMD_conservative_depth_enable
||
2816 state
->ARB_conservative_depth_enable
)
2817 && strcmp(var
->name
, "gl_FragDepth") == 0
2818 && earlier
->type
== var
->type
2819 && earlier
->data
.mode
== var
->data
.mode
) {
2821 /** From the AMD_conservative_depth spec:
2822 * Within any shader, the first redeclarations of gl_FragDepth
2823 * must appear before any use of gl_FragDepth.
2825 if (earlier
->data
.used
) {
2826 _mesa_glsl_error(&loc
, state
,
2827 "the first redeclaration of gl_FragDepth "
2828 "must appear before any use of gl_FragDepth");
2831 /* Prevent inconsistent redeclaration of depth layout qualifier. */
2832 if (earlier
->data
.depth_layout
!= ir_depth_layout_none
2833 && earlier
->data
.depth_layout
!= var
->data
.depth_layout
) {
2834 _mesa_glsl_error(&loc
, state
,
2835 "gl_FragDepth: depth layout is declared here "
2836 "as '%s, but it was previously declared as "
2838 depth_layout_string(var
->data
.depth_layout
),
2839 depth_layout_string(earlier
->data
.depth_layout
));
2842 earlier
->data
.depth_layout
= var
->data
.depth_layout
;
2844 } else if (allow_all_redeclarations
) {
2845 if (earlier
->data
.mode
!= var
->data
.mode
) {
2846 _mesa_glsl_error(&loc
, state
,
2847 "redeclaration of `%s' with incorrect qualifiers",
2849 } else if (earlier
->type
!= var
->type
) {
2850 _mesa_glsl_error(&loc
, state
,
2851 "redeclaration of `%s' has incorrect type",
2855 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
2862 * Generate the IR for an initializer in a variable declaration
2865 process_initializer(ir_variable
*var
, ast_declaration
*decl
,
2866 ast_fully_specified_type
*type
,
2867 exec_list
*initializer_instructions
,
2868 struct _mesa_glsl_parse_state
*state
)
2870 ir_rvalue
*result
= NULL
;
2872 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
2874 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
2876 * "All uniform variables are read-only and are initialized either
2877 * directly by an application via API commands, or indirectly by
2880 if (var
->data
.mode
== ir_var_uniform
) {
2881 state
->check_version(120, 0, &initializer_loc
,
2882 "cannot initialize uniforms");
2885 /* From section 4.1.7 of the GLSL 4.40 spec:
2887 * "Opaque variables [...] are initialized only through the
2888 * OpenGL API; they cannot be declared with an initializer in a
2891 if (var
->type
->contains_opaque()) {
2892 _mesa_glsl_error(& initializer_loc
, state
,
2893 "cannot initialize opaque variable");
2896 if ((var
->data
.mode
== ir_var_shader_in
) && (state
->current_function
== NULL
)) {
2897 _mesa_glsl_error(& initializer_loc
, state
,
2898 "cannot initialize %s shader input / %s",
2899 _mesa_shader_stage_to_string(state
->stage
),
2900 (state
->stage
== MESA_SHADER_VERTEX
)
2901 ? "attribute" : "varying");
2904 /* If the initializer is an ast_aggregate_initializer, recursively store
2905 * type information from the LHS into it, so that its hir() function can do
2908 if (decl
->initializer
->oper
== ast_aggregate
)
2909 _mesa_ast_set_aggregate_type(var
->type
, decl
->initializer
);
2911 ir_dereference
*const lhs
= new(state
) ir_dereference_variable(var
);
2912 ir_rvalue
*rhs
= decl
->initializer
->hir(initializer_instructions
, state
);
2914 /* Calculate the constant value if this is a const or uniform
2917 if (type
->qualifier
.flags
.q
.constant
2918 || type
->qualifier
.flags
.q
.uniform
) {
2919 ir_rvalue
*new_rhs
= validate_assignment(state
, initializer_loc
,
2920 var
->type
, rhs
, true);
2921 if (new_rhs
!= NULL
) {
2924 ir_constant
*constant_value
= rhs
->constant_expression_value();
2925 if (!constant_value
) {
2926 /* If ARB_shading_language_420pack is enabled, initializers of
2927 * const-qualified local variables do not have to be constant
2928 * expressions. Const-qualified global variables must still be
2929 * initialized with constant expressions.
2931 if (!state
->ARB_shading_language_420pack_enable
2932 || state
->current_function
== NULL
) {
2933 _mesa_glsl_error(& initializer_loc
, state
,
2934 "initializer of %s variable `%s' must be a "
2935 "constant expression",
2936 (type
->qualifier
.flags
.q
.constant
)
2937 ? "const" : "uniform",
2939 if (var
->type
->is_numeric()) {
2940 /* Reduce cascading errors. */
2941 var
->constant_value
= ir_constant::zero(state
, var
->type
);
2945 rhs
= constant_value
;
2946 var
->constant_value
= constant_value
;
2949 if (var
->type
->is_numeric()) {
2950 /* Reduce cascading errors. */
2951 var
->constant_value
= ir_constant::zero(state
, var
->type
);
2956 if (rhs
&& !rhs
->type
->is_error()) {
2957 bool temp
= var
->data
.read_only
;
2958 if (type
->qualifier
.flags
.q
.constant
)
2959 var
->data
.read_only
= false;
2961 /* Never emit code to initialize a uniform.
2963 const glsl_type
*initializer_type
;
2964 if (!type
->qualifier
.flags
.q
.uniform
) {
2965 do_assignment(initializer_instructions
, state
,
2970 type
->get_location());
2971 initializer_type
= result
->type
;
2973 initializer_type
= rhs
->type
;
2975 var
->constant_initializer
= rhs
->constant_expression_value();
2976 var
->data
.has_initializer
= true;
2978 /* If the declared variable is an unsized array, it must inherrit
2979 * its full type from the initializer. A declaration such as
2981 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
2985 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
2987 * The assignment generated in the if-statement (below) will also
2988 * automatically handle this case for non-uniforms.
2990 * If the declared variable is not an array, the types must
2991 * already match exactly. As a result, the type assignment
2992 * here can be done unconditionally. For non-uniforms the call
2993 * to do_assignment can change the type of the initializer (via
2994 * the implicit conversion rules). For uniforms the initializer
2995 * must be a constant expression, and the type of that expression
2996 * was validated above.
2998 var
->type
= initializer_type
;
3000 var
->data
.read_only
= temp
;
3008 * Do additional processing necessary for geometry shader input declarations
3009 * (this covers both interface blocks arrays and bare input variables).
3012 handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
3013 YYLTYPE loc
, ir_variable
*var
)
3015 unsigned num_vertices
= 0;
3016 if (state
->gs_input_prim_type_specified
) {
3017 num_vertices
= vertices_per_prim(state
->in_qualifier
->prim_type
);
3020 /* Geometry shader input variables must be arrays. Caller should have
3021 * reported an error for this.
3023 if (!var
->type
->is_array()) {
3024 assert(state
->error
);
3026 /* To avoid cascading failures, short circuit the checks below. */
3030 if (var
->type
->is_unsized_array()) {
3031 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says:
3033 * All geometry shader input unsized array declarations will be
3034 * sized by an earlier input layout qualifier, when present, as per
3035 * the following table.
3037 * Followed by a table mapping each allowed input layout qualifier to
3038 * the corresponding input length.
3040 if (num_vertices
!= 0)
3041 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
3044 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec
3045 * includes the following examples of compile-time errors:
3047 * // code sequence within one shader...
3048 * in vec4 Color1[]; // size unknown
3049 * ...Color1.length()...// illegal, length() unknown
3050 * in vec4 Color2[2]; // size is 2
3051 * ...Color1.length()...// illegal, Color1 still has no size
3052 * in vec4 Color3[3]; // illegal, input sizes are inconsistent
3053 * layout(lines) in; // legal, input size is 2, matching
3054 * in vec4 Color4[3]; // illegal, contradicts layout
3057 * To detect the case illustrated by Color3, we verify that the size of
3058 * an explicitly-sized array matches the size of any previously declared
3059 * explicitly-sized array. To detect the case illustrated by Color4, we
3060 * verify that the size of an explicitly-sized array is consistent with
3061 * any previously declared input layout.
3063 if (num_vertices
!= 0 && var
->type
->length
!= num_vertices
) {
3064 _mesa_glsl_error(&loc
, state
,
3065 "geometry shader input size contradicts previously"
3066 " declared layout (size is %u, but layout requires a"
3067 " size of %u)", var
->type
->length
, num_vertices
);
3068 } else if (state
->gs_input_size
!= 0 &&
3069 var
->type
->length
!= state
->gs_input_size
) {
3070 _mesa_glsl_error(&loc
, state
,
3071 "geometry shader input sizes are "
3072 "inconsistent (size is %u, but a previous "
3073 "declaration has size %u)",
3074 var
->type
->length
, state
->gs_input_size
);
3076 state
->gs_input_size
= var
->type
->length
;
3083 validate_identifier(const char *identifier
, YYLTYPE loc
,
3084 struct _mesa_glsl_parse_state
*state
)
3086 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
3088 * "Identifiers starting with "gl_" are reserved for use by
3089 * OpenGL, and may not be declared in a shader as either a
3090 * variable or a function."
3092 if (is_gl_identifier(identifier
)) {
3093 _mesa_glsl_error(&loc
, state
,
3094 "identifier `%s' uses reserved `gl_' prefix",
3096 } else if (strstr(identifier
, "__")) {
3097 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
3100 * "In addition, all identifiers containing two
3101 * consecutive underscores (__) are reserved as
3102 * possible future keywords."
3104 * The intention is that names containing __ are reserved for internal
3105 * use by the implementation, and names prefixed with GL_ are reserved
3106 * for use by Khronos. Names simply containing __ are dangerous to use,
3107 * but should be allowed.
3109 * A future version of the GLSL specification will clarify this.
3111 _mesa_glsl_warning(&loc
, state
,
3112 "identifier `%s' uses reserved `__' string",
3119 ast_declarator_list::hir(exec_list
*instructions
,
3120 struct _mesa_glsl_parse_state
*state
)
3123 const struct glsl_type
*decl_type
;
3124 const char *type_name
= NULL
;
3125 ir_rvalue
*result
= NULL
;
3126 YYLTYPE loc
= this->get_location();
3128 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
3130 * "To ensure that a particular output variable is invariant, it is
3131 * necessary to use the invariant qualifier. It can either be used to
3132 * qualify a previously declared variable as being invariant
3134 * invariant gl_Position; // make existing gl_Position be invariant"
3136 * In these cases the parser will set the 'invariant' flag in the declarator
3137 * list, and the type will be NULL.
3139 if (this->invariant
) {
3140 assert(this->type
== NULL
);
3142 if (state
->current_function
!= NULL
) {
3143 _mesa_glsl_error(& loc
, state
,
3144 "all uses of `invariant' keyword must be at global "
3148 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
3149 assert(decl
->array_specifier
== NULL
);
3150 assert(decl
->initializer
== NULL
);
3152 ir_variable
*const earlier
=
3153 state
->symbols
->get_variable(decl
->identifier
);
3154 if (earlier
== NULL
) {
3155 _mesa_glsl_error(& loc
, state
,
3156 "undeclared variable `%s' cannot be marked "
3157 "invariant", decl
->identifier
);
3158 } else if (!is_varying_var(earlier
, state
->stage
)) {
3159 _mesa_glsl_error(&loc
, state
,
3160 "`%s' cannot be marked invariant; interfaces between "
3161 "shader stages only.", decl
->identifier
);
3162 } else if (earlier
->data
.used
) {
3163 _mesa_glsl_error(& loc
, state
,
3164 "variable `%s' may not be redeclared "
3165 "`invariant' after being used",
3168 earlier
->data
.invariant
= true;
3172 /* Invariant redeclarations do not have r-values.
3177 assert(this->type
!= NULL
);
3178 assert(!this->invariant
);
3179 assert(!this->precise
);
3181 /* The type specifier may contain a structure definition. Process that
3182 * before any of the variable declarations.
3184 (void) this->type
->specifier
->hir(instructions
, state
);
3186 decl_type
= this->type
->glsl_type(& type_name
, state
);
3188 /* An offset-qualified atomic counter declaration sets the default
3189 * offset for the next declaration within the same atomic counter
3192 if (decl_type
&& decl_type
->contains_atomic()) {
3193 if (type
->qualifier
.flags
.q
.explicit_binding
&&
3194 type
->qualifier
.flags
.q
.explicit_offset
)
3195 state
->atomic_counter_offsets
[type
->qualifier
.binding
] =
3196 type
->qualifier
.offset
;
3199 if (this->declarations
.is_empty()) {
3200 /* If there is no structure involved in the program text, there are two
3201 * possible scenarios:
3203 * - The program text contained something like 'vec4;'. This is an
3204 * empty declaration. It is valid but weird. Emit a warning.
3206 * - The program text contained something like 'S;' and 'S' is not the
3207 * name of a known structure type. This is both invalid and weird.
3210 * - The program text contained something like 'mediump float;'
3211 * when the programmer probably meant 'precision mediump
3212 * float;' Emit a warning with a description of what they
3213 * probably meant to do.
3215 * Note that if decl_type is NULL and there is a structure involved,
3216 * there must have been some sort of error with the structure. In this
3217 * case we assume that an error was already generated on this line of
3218 * code for the structure. There is no need to generate an additional,
3221 assert(this->type
->specifier
->structure
== NULL
|| decl_type
!= NULL
3224 if (decl_type
== NULL
) {
3225 _mesa_glsl_error(&loc
, state
,
3226 "invalid type `%s' in empty declaration",
3228 } else if (decl_type
->base_type
== GLSL_TYPE_ATOMIC_UINT
) {
3229 /* Empty atomic counter declarations are allowed and useful
3230 * to set the default offset qualifier.
3233 } else if (this->type
->qualifier
.precision
!= ast_precision_none
) {
3234 if (this->type
->specifier
->structure
!= NULL
) {
3235 _mesa_glsl_error(&loc
, state
,
3236 "precision qualifiers can't be applied "
3239 static const char *const precision_names
[] = {
3246 _mesa_glsl_warning(&loc
, state
,
3247 "empty declaration with precision qualifier, "
3248 "to set the default precision, use "
3249 "`precision %s %s;'",
3250 precision_names
[this->type
->qualifier
.precision
],
3253 } else if (this->type
->specifier
->structure
== NULL
) {
3254 _mesa_glsl_warning(&loc
, state
, "empty declaration");
3258 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
3259 const struct glsl_type
*var_type
;
3262 /* FINISHME: Emit a warning if a variable declaration shadows a
3263 * FINISHME: declaration at a higher scope.
3266 if ((decl_type
== NULL
) || decl_type
->is_void()) {
3267 if (type_name
!= NULL
) {
3268 _mesa_glsl_error(& loc
, state
,
3269 "invalid type `%s' in declaration of `%s'",
3270 type_name
, decl
->identifier
);
3272 _mesa_glsl_error(& loc
, state
,
3273 "invalid type in declaration of `%s'",
3279 var_type
= process_array_type(&loc
, decl_type
, decl
->array_specifier
,
3282 var
= new(ctx
) ir_variable(var_type
, decl
->identifier
, ir_var_auto
);
3284 /* The 'varying in' and 'varying out' qualifiers can only be used with
3285 * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
3288 if (this->type
->qualifier
.flags
.q
.varying
) {
3289 if (this->type
->qualifier
.flags
.q
.in
) {
3290 _mesa_glsl_error(& loc
, state
,
3291 "`varying in' qualifier in declaration of "
3292 "`%s' only valid for geometry shaders using "
3293 "ARB_geometry_shader4 or EXT_geometry_shader4",
3295 } else if (this->type
->qualifier
.flags
.q
.out
) {
3296 _mesa_glsl_error(& loc
, state
,
3297 "`varying out' qualifier in declaration of "
3298 "`%s' only valid for geometry shaders using "
3299 "ARB_geometry_shader4 or EXT_geometry_shader4",
3304 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
3306 * "Global variables can only use the qualifiers const,
3307 * attribute, uniform, or varying. Only one may be
3310 * Local variables can only use the qualifier const."
3312 * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
3313 * any extension that adds the 'layout' keyword.
3315 if (!state
->is_version(130, 300)
3316 && !state
->has_explicit_attrib_location()
3317 && !state
->has_separate_shader_objects()
3318 && !state
->ARB_fragment_coord_conventions_enable
) {
3319 if (this->type
->qualifier
.flags
.q
.out
) {
3320 _mesa_glsl_error(& loc
, state
,
3321 "`out' qualifier in declaration of `%s' "
3322 "only valid for function parameters in %s",
3323 decl
->identifier
, state
->get_version_string());
3325 if (this->type
->qualifier
.flags
.q
.in
) {
3326 _mesa_glsl_error(& loc
, state
,
3327 "`in' qualifier in declaration of `%s' "
3328 "only valid for function parameters in %s",
3329 decl
->identifier
, state
->get_version_string());
3331 /* FINISHME: Test for other invalid qualifiers. */
3334 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
3337 if (this->type
->qualifier
.flags
.q
.invariant
) {
3338 if (!is_varying_var(var
, state
->stage
)) {
3339 _mesa_glsl_error(&loc
, state
,
3340 "`%s' cannot be marked invariant; interfaces between "
3341 "shader stages only", var
->name
);
3345 if (state
->current_function
!= NULL
) {
3346 const char *mode
= NULL
;
3347 const char *extra
= "";
3349 /* There is no need to check for 'inout' here because the parser will
3350 * only allow that in function parameter lists.
3352 if (this->type
->qualifier
.flags
.q
.attribute
) {
3354 } else if (this->type
->qualifier
.flags
.q
.uniform
) {
3356 } else if (this->type
->qualifier
.flags
.q
.varying
) {
3358 } else if (this->type
->qualifier
.flags
.q
.in
) {
3360 extra
= " or in function parameter list";
3361 } else if (this->type
->qualifier
.flags
.q
.out
) {
3363 extra
= " or in function parameter list";
3367 _mesa_glsl_error(& loc
, state
,
3368 "%s variable `%s' must be declared at "
3370 mode
, var
->name
, extra
);
3372 } else if (var
->data
.mode
== ir_var_shader_in
) {
3373 var
->data
.read_only
= true;
3375 if (state
->stage
== MESA_SHADER_VERTEX
) {
3376 bool error_emitted
= false;
3378 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
3380 * "Vertex shader inputs can only be float, floating-point
3381 * vectors, matrices, signed and unsigned integers and integer
3382 * vectors. Vertex shader inputs can also form arrays of these
3383 * types, but not structures."
3385 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
3387 * "Vertex shader inputs can only be float, floating-point
3388 * vectors, matrices, signed and unsigned integers and integer
3389 * vectors. They cannot be arrays or structures."
3391 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
3393 * "The attribute qualifier can be used only with float,
3394 * floating-point vectors, and matrices. Attribute variables
3395 * cannot be declared as arrays or structures."
3397 * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
3399 * "Vertex shader inputs can only be float, floating-point
3400 * vectors, matrices, signed and unsigned integers and integer
3401 * vectors. Vertex shader inputs cannot be arrays or
3404 const glsl_type
*check_type
= var
->type
;
3405 while (check_type
->is_array())
3406 check_type
= check_type
->element_type();
3408 switch (check_type
->base_type
) {
3409 case GLSL_TYPE_FLOAT
:
3411 case GLSL_TYPE_UINT
:
3413 if (state
->is_version(120, 300))
3417 _mesa_glsl_error(& loc
, state
,
3418 "vertex shader input / attribute cannot have "
3420 var
->type
->is_array() ? "array of " : "",
3422 error_emitted
= true;
3425 if (!error_emitted
&& var
->type
->is_array() &&
3426 !state
->check_version(150, 0, &loc
,
3427 "vertex shader input / attribute "
3428 "cannot have array type")) {
3429 error_emitted
= true;
3431 } else if (state
->stage
== MESA_SHADER_GEOMETRY
) {
3432 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
3434 * Geometry shader input variables get the per-vertex values
3435 * written out by vertex shader output variables of the same
3436 * names. Since a geometry shader operates on a set of
3437 * vertices, each input varying variable (or input block, see
3438 * interface blocks below) needs to be declared as an array.
3440 if (!var
->type
->is_array()) {
3441 _mesa_glsl_error(&loc
, state
,
3442 "geometry shader inputs must be arrays");
3445 handle_geometry_shader_input_decl(state
, loc
, var
);
3449 /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
3450 * so must integer vertex outputs.
3452 * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
3453 * "Fragment shader inputs that are signed or unsigned integers or
3454 * integer vectors must be qualified with the interpolation qualifier
3457 * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
3458 * "Fragment shader inputs that are, or contain, signed or unsigned
3459 * integers or integer vectors must be qualified with the
3460 * interpolation qualifier flat."
3462 * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
3463 * "Vertex shader outputs that are, or contain, signed or unsigned
3464 * integers or integer vectors must be qualified with the
3465 * interpolation qualifier flat."
3467 * Note that prior to GLSL 1.50, this requirement applied to vertex
3468 * outputs rather than fragment inputs. That creates problems in the
3469 * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
3470 * desktop GL shaders. For GLSL ES shaders, we follow the spec and
3471 * apply the restriction to both vertex outputs and fragment inputs.
3473 * Note also that the desktop GLSL specs are missing the text "or
3474 * contain"; this is presumably an oversight, since there is no
3475 * reasonable way to interpolate a fragment shader input that contains
3478 if (state
->is_version(130, 300) &&
3479 var
->type
->contains_integer() &&
3480 var
->data
.interpolation
!= INTERP_QUALIFIER_FLAT
&&
3481 ((state
->stage
== MESA_SHADER_FRAGMENT
&& var
->data
.mode
== ir_var_shader_in
)
3482 || (state
->stage
== MESA_SHADER_VERTEX
&& var
->data
.mode
== ir_var_shader_out
3483 && state
->es_shader
))) {
3484 const char *var_type
= (state
->stage
== MESA_SHADER_VERTEX
) ?
3485 "vertex output" : "fragment input";
3486 _mesa_glsl_error(&loc
, state
, "if a %s is (or contains) "
3487 "an integer, then it must be qualified with 'flat'",
3492 /* Interpolation qualifiers cannot be applied to 'centroid' and
3493 * 'centroid varying'.
3495 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
3496 * "interpolation qualifiers may only precede the qualifiers in,
3497 * centroid in, out, or centroid out in a declaration. They do not apply
3498 * to the deprecated storage qualifiers varying or centroid varying."
3500 * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
3502 if (state
->is_version(130, 0)
3503 && this->type
->qualifier
.has_interpolation()
3504 && this->type
->qualifier
.flags
.q
.varying
) {
3506 const char *i
= this->type
->qualifier
.interpolation_string();
3509 if (this->type
->qualifier
.flags
.q
.centroid
)
3510 s
= "centroid varying";
3514 _mesa_glsl_error(&loc
, state
,
3515 "qualifier '%s' cannot be applied to the "
3516 "deprecated storage qualifier '%s'", i
, s
);
3520 /* Interpolation qualifiers can only apply to vertex shader outputs and
3521 * fragment shader inputs.
3523 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
3524 * "Outputs from a vertex shader (out) and inputs to a fragment
3525 * shader (in) can be further qualified with one or more of these
3526 * interpolation qualifiers"
3528 * From page 31 (page 37 of the PDF) of the GLSL ES 3.00 spec:
3529 * "These interpolation qualifiers may only precede the qualifiers
3530 * in, centroid in, out, or centroid out in a declaration. They do
3531 * not apply to inputs into a vertex shader or outputs from a
3534 if (state
->is_version(130, 300)
3535 && this->type
->qualifier
.has_interpolation()) {
3537 const char *i
= this->type
->qualifier
.interpolation_string();
3540 switch (state
->stage
) {
3541 case MESA_SHADER_VERTEX
:
3542 if (this->type
->qualifier
.flags
.q
.in
) {
3543 _mesa_glsl_error(&loc
, state
,
3544 "qualifier '%s' cannot be applied to vertex "
3545 "shader inputs", i
);
3548 case MESA_SHADER_FRAGMENT
:
3549 if (this->type
->qualifier
.flags
.q
.out
) {
3550 _mesa_glsl_error(&loc
, state
,
3551 "qualifier '%s' cannot be applied to fragment "
3552 "shader outputs", i
);
3561 /* From section 4.3.4 of the GLSL 1.30 spec:
3562 * "It is an error to use centroid in in a vertex shader."
3564 * From section 4.3.4 of the GLSL ES 3.00 spec:
3565 * "It is an error to use centroid in or interpolation qualifiers in
3566 * a vertex shader input."
3568 if (state
->is_version(130, 300)
3569 && this->type
->qualifier
.flags
.q
.centroid
3570 && this->type
->qualifier
.flags
.q
.in
3571 && state
->stage
== MESA_SHADER_VERTEX
) {
3573 _mesa_glsl_error(&loc
, state
,
3574 "'centroid in' cannot be used in a vertex shader");
3577 if (state
->stage
== MESA_SHADER_VERTEX
3578 && this->type
->qualifier
.flags
.q
.sample
3579 && this->type
->qualifier
.flags
.q
.in
) {
3581 _mesa_glsl_error(&loc
, state
,
3582 "'sample in' cannot be used in a vertex shader");
3585 /* Section 4.3.6 of the GLSL 1.30 specification states:
3586 * "It is an error to use centroid out in a fragment shader."
3588 * The GL_ARB_shading_language_420pack extension specification states:
3589 * "It is an error to use auxiliary storage qualifiers or interpolation
3590 * qualifiers on an output in a fragment shader."
3592 if (state
->stage
== MESA_SHADER_FRAGMENT
&&
3593 this->type
->qualifier
.flags
.q
.out
&&
3594 this->type
->qualifier
.has_auxiliary_storage()) {
3595 _mesa_glsl_error(&loc
, state
,
3596 "auxiliary storage qualifiers cannot be used on "
3597 "fragment shader outputs");
3600 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
3602 if (this->type
->qualifier
.precision
!= ast_precision_none
) {
3603 state
->check_precision_qualifiers_allowed(&loc
);
3607 /* Precision qualifiers apply to floating point, integer and sampler
3610 * Section 4.5.2 (Precision Qualifiers) of the GLSL 1.30 spec says:
3611 * "Any floating point or any integer declaration can have the type
3612 * preceded by one of these precision qualifiers [...] Literal
3613 * constants do not have precision qualifiers. Neither do Boolean
3616 * Section 4.5 (Precision and Precision Qualifiers) of the GLSL 1.30
3619 * "Precision qualifiers are added for code portability with OpenGL
3620 * ES, not for functionality. They have the same syntax as in OpenGL
3623 * Section 8 (Built-In Functions) of the GLSL ES 1.00 spec says:
3625 * "uniform lowp sampler2D sampler;
3628 * lowp vec4 col = texture2D (sampler, coord);
3629 * // texture2D returns lowp"
3631 * From this, we infer that GLSL 1.30 (and later) should allow precision
3632 * qualifiers on sampler types just like float and integer types.
3634 if (this->type
->qualifier
.precision
!= ast_precision_none
3635 && !var
->type
->is_float()
3636 && !var
->type
->is_integer()
3637 && !var
->type
->is_record()
3638 && !var
->type
->is_sampler()
3639 && !(var
->type
->is_array()
3640 && (var
->type
->fields
.array
->is_float()
3641 || var
->type
->fields
.array
->is_integer()))) {
3643 _mesa_glsl_error(&loc
, state
,
3644 "precision qualifiers apply only to floating point"
3645 ", integer and sampler types");
3648 /* From section 4.1.7 of the GLSL 4.40 spec:
3650 * "[Opaque types] can only be declared as function
3651 * parameters or uniform-qualified variables."
3653 if (var_type
->contains_opaque() &&
3654 !this->type
->qualifier
.flags
.q
.uniform
) {
3655 _mesa_glsl_error(&loc
, state
,
3656 "opaque variables must be declared uniform");
3659 /* Process the initializer and add its instructions to a temporary
3660 * list. This list will be added to the instruction stream (below) after
3661 * the declaration is added. This is done because in some cases (such as
3662 * redeclarations) the declaration may not actually be added to the
3663 * instruction stream.
3665 exec_list initializer_instructions
;
3667 /* Examine var name here since var may get deleted in the next call */
3668 bool var_is_gl_id
= is_gl_identifier(var
->name
);
3670 ir_variable
*earlier
=
3671 get_variable_being_redeclared(var
, decl
->get_location(), state
,
3672 false /* allow_all_redeclarations */);
3673 if (earlier
!= NULL
) {
3675 earlier
->data
.how_declared
== ir_var_declared_in_block
) {
3676 _mesa_glsl_error(&loc
, state
,
3677 "`%s' has already been redeclared using "
3678 "gl_PerVertex", var
->name
);
3680 earlier
->data
.how_declared
= ir_var_declared_normally
;
3683 if (decl
->initializer
!= NULL
) {
3684 result
= process_initializer((earlier
== NULL
) ? var
: earlier
,
3686 &initializer_instructions
, state
);
3689 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
3691 * "It is an error to write to a const variable outside of
3692 * its declaration, so they must be initialized when
3695 if (this->type
->qualifier
.flags
.q
.constant
&& decl
->initializer
== NULL
) {
3696 _mesa_glsl_error(& loc
, state
,
3697 "const declaration of `%s' must be initialized",
3701 if (state
->es_shader
) {
3702 const glsl_type
*const t
= (earlier
== NULL
)
3703 ? var
->type
: earlier
->type
;
3705 if (t
->is_unsized_array())
3706 /* Section 10.17 of the GLSL ES 1.00 specification states that
3707 * unsized array declarations have been removed from the language.
3708 * Arrays that are sized using an initializer are still explicitly
3709 * sized. However, GLSL ES 1.00 does not allow array
3710 * initializers. That is only allowed in GLSL ES 3.00.
3712 * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
3714 * "An array type can also be formed without specifying a size
3715 * if the definition includes an initializer:
3717 * float x[] = float[2] (1.0, 2.0); // declares an array of size 2
3718 * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
3723 _mesa_glsl_error(& loc
, state
,
3724 "unsized array declarations are not allowed in "
3728 /* If the declaration is not a redeclaration, there are a few additional
3729 * semantic checks that must be applied. In addition, variable that was
3730 * created for the declaration should be added to the IR stream.
3732 if (earlier
== NULL
) {
3733 validate_identifier(decl
->identifier
, loc
, state
);
3735 /* Add the variable to the symbol table. Note that the initializer's
3736 * IR was already processed earlier (though it hasn't been emitted
3737 * yet), without the variable in scope.
3739 * This differs from most C-like languages, but it follows the GLSL
3740 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
3743 * "Within a declaration, the scope of a name starts immediately
3744 * after the initializer if present or immediately after the name
3745 * being declared if not."
3747 if (!state
->symbols
->add_variable(var
)) {
3748 YYLTYPE loc
= this->get_location();
3749 _mesa_glsl_error(&loc
, state
, "name `%s' already taken in the "
3750 "current scope", decl
->identifier
);
3754 /* Push the variable declaration to the top. It means that all the
3755 * variable declarations will appear in a funny last-to-first order,
3756 * but otherwise we run into trouble if a function is prototyped, a
3757 * global var is decled, then the function is defined with usage of
3758 * the global var. See glslparsertest's CorrectModule.frag.
3760 instructions
->push_head(var
);
3763 instructions
->append_list(&initializer_instructions
);
3767 /* Generally, variable declarations do not have r-values. However,
3768 * one is used for the declaration in
3770 * while (bool b = some_condition()) {
3774 * so we return the rvalue from the last seen declaration here.
3781 ast_parameter_declarator::hir(exec_list
*instructions
,
3782 struct _mesa_glsl_parse_state
*state
)
3785 const struct glsl_type
*type
;
3786 const char *name
= NULL
;
3787 YYLTYPE loc
= this->get_location();
3789 type
= this->type
->glsl_type(& name
, state
);
3793 _mesa_glsl_error(& loc
, state
,
3794 "invalid type `%s' in declaration of `%s'",
3795 name
, this->identifier
);
3797 _mesa_glsl_error(& loc
, state
,
3798 "invalid type in declaration of `%s'",
3802 type
= glsl_type::error_type
;
3805 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
3807 * "Functions that accept no input arguments need not use void in the
3808 * argument list because prototypes (or definitions) are required and
3809 * therefore there is no ambiguity when an empty argument list "( )" is
3810 * declared. The idiom "(void)" as a parameter list is provided for
3813 * Placing this check here prevents a void parameter being set up
3814 * for a function, which avoids tripping up checks for main taking
3815 * parameters and lookups of an unnamed symbol.
3817 if (type
->is_void()) {
3818 if (this->identifier
!= NULL
)
3819 _mesa_glsl_error(& loc
, state
,
3820 "named parameter cannot have type `void'");
3826 if (formal_parameter
&& (this->identifier
== NULL
)) {
3827 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
3831 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
3832 * call already handled the "vec4[..] foo" case.
3834 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
3836 if (!type
->is_error() && type
->is_unsized_array()) {
3837 _mesa_glsl_error(&loc
, state
, "arrays passed as parameters must have "
3839 type
= glsl_type::error_type
;
3843 ir_variable
*var
= new(ctx
)
3844 ir_variable(type
, this->identifier
, ir_var_function_in
);
3846 /* Apply any specified qualifiers to the parameter declaration. Note that
3847 * for function parameters the default mode is 'in'.
3849 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
,
3852 /* From section 4.1.7 of the GLSL 4.40 spec:
3854 * "Opaque variables cannot be treated as l-values; hence cannot
3855 * be used as out or inout function parameters, nor can they be
3858 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
3859 && type
->contains_opaque()) {
3860 _mesa_glsl_error(&loc
, state
, "out and inout parameters cannot "
3861 "contain opaque variables");
3862 type
= glsl_type::error_type
;
3865 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
3867 * "When calling a function, expressions that do not evaluate to
3868 * l-values cannot be passed to parameters declared as out or inout."
3870 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
3872 * "Other binary or unary expressions, non-dereferenced arrays,
3873 * function names, swizzles with repeated fields, and constants
3874 * cannot be l-values."
3876 * So for GLSL 1.10, passing an array as an out or inout parameter is not
3877 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
3879 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
3881 && !state
->check_version(120, 100, &loc
,
3882 "arrays cannot be out or inout parameters")) {
3883 type
= glsl_type::error_type
;
3886 instructions
->push_tail(var
);
3888 /* Parameter declarations do not have r-values.
3895 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
3897 exec_list
*ir_parameters
,
3898 _mesa_glsl_parse_state
*state
)
3900 ast_parameter_declarator
*void_param
= NULL
;
3903 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
3904 param
->formal_parameter
= formal
;
3905 param
->hir(ir_parameters
, state
);
3913 if ((void_param
!= NULL
) && (count
> 1)) {
3914 YYLTYPE loc
= void_param
->get_location();
3916 _mesa_glsl_error(& loc
, state
,
3917 "`void' parameter must be only parameter");
3923 emit_function(_mesa_glsl_parse_state
*state
, ir_function
*f
)
3925 /* IR invariants disallow function declarations or definitions
3926 * nested within other function definitions. But there is no
3927 * requirement about the relative order of function declarations
3928 * and definitions with respect to one another. So simply insert
3929 * the new ir_function block at the end of the toplevel instruction
3932 state
->toplevel_ir
->push_tail(f
);
3937 ast_function::hir(exec_list
*instructions
,
3938 struct _mesa_glsl_parse_state
*state
)
3941 ir_function
*f
= NULL
;
3942 ir_function_signature
*sig
= NULL
;
3943 exec_list hir_parameters
;
3945 const char *const name
= identifier
;
3947 /* New functions are always added to the top-level IR instruction stream,
3948 * so this instruction list pointer is ignored. See also emit_function
3951 (void) instructions
;
3953 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
3955 * "Function declarations (prototypes) cannot occur inside of functions;
3956 * they must be at global scope, or for the built-in functions, outside
3957 * the global scope."
3959 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
3961 * "User defined functions may only be defined within the global scope."
3963 * Note that this language does not appear in GLSL 1.10.
3965 if ((state
->current_function
!= NULL
) &&
3966 state
->is_version(120, 100)) {
3967 YYLTYPE loc
= this->get_location();
3968 _mesa_glsl_error(&loc
, state
,
3969 "declaration of function `%s' not allowed within "
3970 "function body", name
);
3973 validate_identifier(name
, this->get_location(), state
);
3975 /* Convert the list of function parameters to HIR now so that they can be
3976 * used below to compare this function's signature with previously seen
3977 * signatures for functions with the same name.
3979 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
3981 & hir_parameters
, state
);
3983 const char *return_type_name
;
3984 const glsl_type
*return_type
=
3985 this->return_type
->glsl_type(& return_type_name
, state
);
3988 YYLTYPE loc
= this->get_location();
3989 _mesa_glsl_error(&loc
, state
,
3990 "function `%s' has undeclared return type `%s'",
3991 name
, return_type_name
);
3992 return_type
= glsl_type::error_type
;
3995 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
3996 * "No qualifier is allowed on the return type of a function."
3998 if (this->return_type
->has_qualifiers()) {
3999 YYLTYPE loc
= this->get_location();
4000 _mesa_glsl_error(& loc
, state
,
4001 "function `%s' return type has qualifiers", name
);
4004 /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
4006 * "Arrays are allowed as arguments and as the return type. In both
4007 * cases, the array must be explicitly sized."
4009 if (return_type
->is_unsized_array()) {
4010 YYLTYPE loc
= this->get_location();
4011 _mesa_glsl_error(& loc
, state
,
4012 "function `%s' return type array must be explicitly "
4016 /* From section 4.1.7 of the GLSL 4.40 spec:
4018 * "[Opaque types] can only be declared as function parameters
4019 * or uniform-qualified variables."
4021 if (return_type
->contains_opaque()) {
4022 YYLTYPE loc
= this->get_location();
4023 _mesa_glsl_error(&loc
, state
,
4024 "function `%s' return type can't contain an opaque type",
4028 /* Verify that this function's signature either doesn't match a previously
4029 * seen signature for a function with the same name, or, if a match is found,
4030 * that the previously seen signature does not have an associated definition.
4032 f
= state
->symbols
->get_function(name
);
4033 if (f
!= NULL
&& (state
->es_shader
|| f
->has_user_signature())) {
4034 sig
= f
->exact_matching_signature(state
, &hir_parameters
);
4036 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
4037 if (badvar
!= NULL
) {
4038 YYLTYPE loc
= this->get_location();
4040 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
4041 "qualifiers don't match prototype", name
, badvar
);
4044 if (sig
->return_type
!= return_type
) {
4045 YYLTYPE loc
= this->get_location();
4047 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
4048 "match prototype", name
);
4051 if (sig
->is_defined
) {
4052 if (is_definition
) {
4053 YYLTYPE loc
= this->get_location();
4054 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
4056 /* We just encountered a prototype that exactly matches a
4057 * function that's already been defined. This is redundant,
4058 * and we should ignore it.
4065 f
= new(ctx
) ir_function(name
);
4066 if (!state
->symbols
->add_function(f
)) {
4067 /* This function name shadows a non-function use of the same name. */
4068 YYLTYPE loc
= this->get_location();
4070 _mesa_glsl_error(&loc
, state
, "function name `%s' conflicts with "
4071 "non-function", name
);
4075 emit_function(state
, f
);
4078 /* Verify the return type of main() */
4079 if (strcmp(name
, "main") == 0) {
4080 if (! return_type
->is_void()) {
4081 YYLTYPE loc
= this->get_location();
4083 _mesa_glsl_error(& loc
, state
, "main() must return void");
4086 if (!hir_parameters
.is_empty()) {
4087 YYLTYPE loc
= this->get_location();
4089 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
4093 /* Finish storing the information about this new function in its signature.
4096 sig
= new(ctx
) ir_function_signature(return_type
);
4097 f
->add_signature(sig
);
4100 sig
->replace_parameters(&hir_parameters
);
4103 /* Function declarations (prototypes) do not have r-values.
4110 ast_function_definition::hir(exec_list
*instructions
,
4111 struct _mesa_glsl_parse_state
*state
)
4113 prototype
->is_definition
= true;
4114 prototype
->hir(instructions
, state
);
4116 ir_function_signature
*signature
= prototype
->signature
;
4117 if (signature
== NULL
)
4120 assert(state
->current_function
== NULL
);
4121 state
->current_function
= signature
;
4122 state
->found_return
= false;
4124 /* Duplicate parameters declared in the prototype as concrete variables.
4125 * Add these to the symbol table.
4127 state
->symbols
->push_scope();
4128 foreach_list(n
, &signature
->parameters
) {
4129 ir_variable
*const var
= ((ir_instruction
*) n
)->as_variable();
4131 assert(var
!= NULL
);
4133 /* The only way a parameter would "exist" is if two parameters have
4136 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
4137 YYLTYPE loc
= this->get_location();
4139 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
4141 state
->symbols
->add_variable(var
);
4145 /* Convert the body of the function to HIR. */
4146 this->body
->hir(&signature
->body
, state
);
4147 signature
->is_defined
= true;
4149 state
->symbols
->pop_scope();
4151 assert(state
->current_function
== signature
);
4152 state
->current_function
= NULL
;
4154 if (!signature
->return_type
->is_void() && !state
->found_return
) {
4155 YYLTYPE loc
= this->get_location();
4156 _mesa_glsl_error(& loc
, state
, "function `%s' has non-void return type "
4157 "%s, but no return statement",
4158 signature
->function_name(),
4159 signature
->return_type
->name
);
4162 /* Function definitions do not have r-values.
4169 ast_jump_statement::hir(exec_list
*instructions
,
4170 struct _mesa_glsl_parse_state
*state
)
4177 assert(state
->current_function
);
4179 if (opt_return_value
) {
4180 ir_rvalue
*ret
= opt_return_value
->hir(instructions
, state
);
4182 /* The value of the return type can be NULL if the shader says
4183 * 'return foo();' and foo() is a function that returns void.
4185 * NOTE: The GLSL spec doesn't say that this is an error. The type
4186 * of the return value is void. If the return type of the function is
4187 * also void, then this should compile without error. Seriously.
4189 const glsl_type
*const ret_type
=
4190 (ret
== NULL
) ? glsl_type::void_type
: ret
->type
;
4192 /* Implicit conversions are not allowed for return values prior to
4193 * ARB_shading_language_420pack.
4195 if (state
->current_function
->return_type
!= ret_type
) {
4196 YYLTYPE loc
= this->get_location();
4198 if (state
->ARB_shading_language_420pack_enable
) {
4199 if (!apply_implicit_conversion(state
->current_function
->return_type
,
4201 _mesa_glsl_error(& loc
, state
,
4202 "could not implicitly convert return value "
4203 "to %s, in function `%s'",
4204 state
->current_function
->return_type
->name
,
4205 state
->current_function
->function_name());
4208 _mesa_glsl_error(& loc
, state
,
4209 "`return' with wrong type %s, in function `%s' "
4212 state
->current_function
->function_name(),
4213 state
->current_function
->return_type
->name
);
4215 } else if (state
->current_function
->return_type
->base_type
==
4217 YYLTYPE loc
= this->get_location();
4219 /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
4220 * specs add a clarification:
4222 * "A void function can only use return without a return argument, even if
4223 * the return argument has void type. Return statements only accept values:
4226 * void func2() { return func1(); } // illegal return statement"
4228 _mesa_glsl_error(& loc
, state
,
4229 "void functions can only use `return' without a "
4233 inst
= new(ctx
) ir_return(ret
);
4235 if (state
->current_function
->return_type
->base_type
!=
4237 YYLTYPE loc
= this->get_location();
4239 _mesa_glsl_error(& loc
, state
,
4240 "`return' with no value, in function %s returning "
4242 state
->current_function
->function_name());
4244 inst
= new(ctx
) ir_return
;
4247 state
->found_return
= true;
4248 instructions
->push_tail(inst
);
4253 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
4254 YYLTYPE loc
= this->get_location();
4256 _mesa_glsl_error(& loc
, state
,
4257 "`discard' may only appear in a fragment shader");
4259 instructions
->push_tail(new(ctx
) ir_discard
);
4264 if (mode
== ast_continue
&&
4265 state
->loop_nesting_ast
== NULL
) {
4266 YYLTYPE loc
= this->get_location();
4268 _mesa_glsl_error(& loc
, state
, "continue may only appear in a loop");
4269 } else if (mode
== ast_break
&&
4270 state
->loop_nesting_ast
== NULL
&&
4271 state
->switch_state
.switch_nesting_ast
== NULL
) {
4272 YYLTYPE loc
= this->get_location();
4274 _mesa_glsl_error(& loc
, state
,
4275 "break may only appear in a loop or a switch");
4277 /* For a loop, inline the for loop expression again, since we don't
4278 * know where near the end of the loop body the normal copy of it is
4279 * going to be placed. Same goes for the condition for a do-while
4282 if (state
->loop_nesting_ast
!= NULL
&&
4283 mode
== ast_continue
) {
4284 if (state
->loop_nesting_ast
->rest_expression
) {
4285 state
->loop_nesting_ast
->rest_expression
->hir(instructions
,
4288 if (state
->loop_nesting_ast
->mode
==
4289 ast_iteration_statement::ast_do_while
) {
4290 state
->loop_nesting_ast
->condition_to_hir(instructions
, state
);
4294 if (state
->switch_state
.is_switch_innermost
&&
4295 mode
== ast_break
) {
4296 /* Force break out of switch by setting is_break switch state.
4298 ir_variable
*const is_break_var
= state
->switch_state
.is_break_var
;
4299 ir_dereference_variable
*const deref_is_break_var
=
4300 new(ctx
) ir_dereference_variable(is_break_var
);
4301 ir_constant
*const true_val
= new(ctx
) ir_constant(true);
4302 ir_assignment
*const set_break_var
=
4303 new(ctx
) ir_assignment(deref_is_break_var
, true_val
);
4305 instructions
->push_tail(set_break_var
);
4308 ir_loop_jump
*const jump
=
4309 new(ctx
) ir_loop_jump((mode
== ast_break
)
4310 ? ir_loop_jump::jump_break
4311 : ir_loop_jump::jump_continue
);
4312 instructions
->push_tail(jump
);
4319 /* Jump instructions do not have r-values.
4326 ast_selection_statement::hir(exec_list
*instructions
,
4327 struct _mesa_glsl_parse_state
*state
)
4331 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
4333 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
4335 * "Any expression whose type evaluates to a Boolean can be used as the
4336 * conditional expression bool-expression. Vector types are not accepted
4337 * as the expression to if."
4339 * The checks are separated so that higher quality diagnostics can be
4340 * generated for cases where both rules are violated.
4342 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
4343 YYLTYPE loc
= this->condition
->get_location();
4345 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
4349 ir_if
*const stmt
= new(ctx
) ir_if(condition
);
4351 if (then_statement
!= NULL
) {
4352 state
->symbols
->push_scope();
4353 then_statement
->hir(& stmt
->then_instructions
, state
);
4354 state
->symbols
->pop_scope();
4357 if (else_statement
!= NULL
) {
4358 state
->symbols
->push_scope();
4359 else_statement
->hir(& stmt
->else_instructions
, state
);
4360 state
->symbols
->pop_scope();
4363 instructions
->push_tail(stmt
);
4365 /* if-statements do not have r-values.
4372 ast_switch_statement::hir(exec_list
*instructions
,
4373 struct _mesa_glsl_parse_state
*state
)
4377 ir_rvalue
*const test_expression
=
4378 this->test_expression
->hir(instructions
, state
);
4380 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
4382 * "The type of init-expression in a switch statement must be a
4385 if (!test_expression
->type
->is_scalar() ||
4386 !test_expression
->type
->is_integer()) {
4387 YYLTYPE loc
= this->test_expression
->get_location();
4389 _mesa_glsl_error(& loc
,
4391 "switch-statement expression must be scalar "
4395 /* Track the switch-statement nesting in a stack-like manner.
4397 struct glsl_switch_state saved
= state
->switch_state
;
4399 state
->switch_state
.is_switch_innermost
= true;
4400 state
->switch_state
.switch_nesting_ast
= this;
4401 state
->switch_state
.labels_ht
= hash_table_ctor(0, hash_table_pointer_hash
,
4402 hash_table_pointer_compare
);
4403 state
->switch_state
.previous_default
= NULL
;
4405 /* Initalize is_fallthru state to false.
4407 ir_rvalue
*const is_fallthru_val
= new (ctx
) ir_constant(false);
4408 state
->switch_state
.is_fallthru_var
=
4409 new(ctx
) ir_variable(glsl_type::bool_type
,
4410 "switch_is_fallthru_tmp",
4412 instructions
->push_tail(state
->switch_state
.is_fallthru_var
);
4414 ir_dereference_variable
*deref_is_fallthru_var
=
4415 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4416 instructions
->push_tail(new(ctx
) ir_assignment(deref_is_fallthru_var
,
4419 /* Initalize is_break state to false.
4421 ir_rvalue
*const is_break_val
= new (ctx
) ir_constant(false);
4422 state
->switch_state
.is_break_var
=
4423 new(ctx
) ir_variable(glsl_type::bool_type
,
4424 "switch_is_break_tmp",
4426 instructions
->push_tail(state
->switch_state
.is_break_var
);
4428 ir_dereference_variable
*deref_is_break_var
=
4429 new(ctx
) ir_dereference_variable(state
->switch_state
.is_break_var
);
4430 instructions
->push_tail(new(ctx
) ir_assignment(deref_is_break_var
,
4433 /* Cache test expression.
4435 test_to_hir(instructions
, state
);
4437 /* Emit code for body of switch stmt.
4439 body
->hir(instructions
, state
);
4441 hash_table_dtor(state
->switch_state
.labels_ht
);
4443 state
->switch_state
= saved
;
4445 /* Switch statements do not have r-values. */
4451 ast_switch_statement::test_to_hir(exec_list
*instructions
,
4452 struct _mesa_glsl_parse_state
*state
)
4456 /* Cache value of test expression. */
4457 ir_rvalue
*const test_val
=
4458 test_expression
->hir(instructions
,
4461 state
->switch_state
.test_var
= new(ctx
) ir_variable(test_val
->type
,
4464 ir_dereference_variable
*deref_test_var
=
4465 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
4467 instructions
->push_tail(state
->switch_state
.test_var
);
4468 instructions
->push_tail(new(ctx
) ir_assignment(deref_test_var
, test_val
));
4473 ast_switch_body::hir(exec_list
*instructions
,
4474 struct _mesa_glsl_parse_state
*state
)
4477 stmts
->hir(instructions
, state
);
4479 /* Switch bodies do not have r-values. */
4484 ast_case_statement_list::hir(exec_list
*instructions
,
4485 struct _mesa_glsl_parse_state
*state
)
4487 foreach_list_typed (ast_case_statement
, case_stmt
, link
, & this->cases
)
4488 case_stmt
->hir(instructions
, state
);
4490 /* Case statements do not have r-values. */
4495 ast_case_statement::hir(exec_list
*instructions
,
4496 struct _mesa_glsl_parse_state
*state
)
4498 labels
->hir(instructions
, state
);
4500 /* Conditionally set fallthru state based on break state. */
4501 ir_constant
*const false_val
= new(state
) ir_constant(false);
4502 ir_dereference_variable
*const deref_is_fallthru_var
=
4503 new(state
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4504 ir_dereference_variable
*const deref_is_break_var
=
4505 new(state
) ir_dereference_variable(state
->switch_state
.is_break_var
);
4506 ir_assignment
*const reset_fallthru_on_break
=
4507 new(state
) ir_assignment(deref_is_fallthru_var
,
4509 deref_is_break_var
);
4510 instructions
->push_tail(reset_fallthru_on_break
);
4512 /* Guard case statements depending on fallthru state. */
4513 ir_dereference_variable
*const deref_fallthru_guard
=
4514 new(state
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4515 ir_if
*const test_fallthru
= new(state
) ir_if(deref_fallthru_guard
);
4517 foreach_list_typed (ast_node
, stmt
, link
, & this->stmts
)
4518 stmt
->hir(& test_fallthru
->then_instructions
, state
);
4520 instructions
->push_tail(test_fallthru
);
4522 /* Case statements do not have r-values. */
4528 ast_case_label_list::hir(exec_list
*instructions
,
4529 struct _mesa_glsl_parse_state
*state
)
4531 foreach_list_typed (ast_case_label
, label
, link
, & this->labels
)
4532 label
->hir(instructions
, state
);
4534 /* Case labels do not have r-values. */
4539 ast_case_label::hir(exec_list
*instructions
,
4540 struct _mesa_glsl_parse_state
*state
)
4544 ir_dereference_variable
*deref_fallthru_var
=
4545 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4547 ir_rvalue
*const true_val
= new(ctx
) ir_constant(true);
4549 /* If not default case, ... */
4550 if (this->test_value
!= NULL
) {
4551 /* Conditionally set fallthru state based on
4552 * comparison of cached test expression value to case label.
4554 ir_rvalue
*const label_rval
= this->test_value
->hir(instructions
, state
);
4555 ir_constant
*label_const
= label_rval
->constant_expression_value();
4558 YYLTYPE loc
= this->test_value
->get_location();
4560 _mesa_glsl_error(& loc
, state
,
4561 "switch statement case label must be a "
4562 "constant expression");
4564 /* Stuff a dummy value in to allow processing to continue. */
4565 label_const
= new(ctx
) ir_constant(0);
4567 ast_expression
*previous_label
= (ast_expression
*)
4568 hash_table_find(state
->switch_state
.labels_ht
,
4569 (void *)(uintptr_t)label_const
->value
.u
[0]);
4571 if (previous_label
) {
4572 YYLTYPE loc
= this->test_value
->get_location();
4573 _mesa_glsl_error(& loc
, state
, "duplicate case value");
4575 loc
= previous_label
->get_location();
4576 _mesa_glsl_error(& loc
, state
, "this is the previous case label");
4578 hash_table_insert(state
->switch_state
.labels_ht
,
4580 (void *)(uintptr_t)label_const
->value
.u
[0]);
4584 ir_dereference_variable
*deref_test_var
=
4585 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
4587 ir_rvalue
*const test_cond
= new(ctx
) ir_expression(ir_binop_all_equal
,
4591 ir_assignment
*set_fallthru_on_test
=
4592 new(ctx
) ir_assignment(deref_fallthru_var
, true_val
, test_cond
);
4594 instructions
->push_tail(set_fallthru_on_test
);
4595 } else { /* default case */
4596 if (state
->switch_state
.previous_default
) {
4597 YYLTYPE loc
= this->get_location();
4598 _mesa_glsl_error(& loc
, state
,
4599 "multiple default labels in one switch");
4601 loc
= state
->switch_state
.previous_default
->get_location();
4602 _mesa_glsl_error(& loc
, state
, "this is the first default label");
4604 state
->switch_state
.previous_default
= this;
4606 /* Set falltrhu state. */
4607 ir_assignment
*set_fallthru
=
4608 new(ctx
) ir_assignment(deref_fallthru_var
, true_val
);
4610 instructions
->push_tail(set_fallthru
);
4613 /* Case statements do not have r-values. */
4618 ast_iteration_statement::condition_to_hir(exec_list
*instructions
,
4619 struct _mesa_glsl_parse_state
*state
)
4623 if (condition
!= NULL
) {
4624 ir_rvalue
*const cond
=
4625 condition
->hir(instructions
, state
);
4628 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
4629 YYLTYPE loc
= condition
->get_location();
4631 _mesa_glsl_error(& loc
, state
,
4632 "loop condition must be scalar boolean");
4634 /* As the first code in the loop body, generate a block that looks
4635 * like 'if (!condition) break;' as the loop termination condition.
4637 ir_rvalue
*const not_cond
=
4638 new(ctx
) ir_expression(ir_unop_logic_not
, cond
);
4640 ir_if
*const if_stmt
= new(ctx
) ir_if(not_cond
);
4642 ir_jump
*const break_stmt
=
4643 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
4645 if_stmt
->then_instructions
.push_tail(break_stmt
);
4646 instructions
->push_tail(if_stmt
);
4653 ast_iteration_statement::hir(exec_list
*instructions
,
4654 struct _mesa_glsl_parse_state
*state
)
4658 /* For-loops and while-loops start a new scope, but do-while loops do not.
4660 if (mode
!= ast_do_while
)
4661 state
->symbols
->push_scope();
4663 if (init_statement
!= NULL
)
4664 init_statement
->hir(instructions
, state
);
4666 ir_loop
*const stmt
= new(ctx
) ir_loop();
4667 instructions
->push_tail(stmt
);
4669 /* Track the current loop nesting. */
4670 ast_iteration_statement
*nesting_ast
= state
->loop_nesting_ast
;
4672 state
->loop_nesting_ast
= this;
4674 /* Likewise, indicate that following code is closest to a loop,
4675 * NOT closest to a switch.
4677 bool saved_is_switch_innermost
= state
->switch_state
.is_switch_innermost
;
4678 state
->switch_state
.is_switch_innermost
= false;
4680 if (mode
!= ast_do_while
)
4681 condition_to_hir(&stmt
->body_instructions
, state
);
4684 body
->hir(& stmt
->body_instructions
, state
);
4686 if (rest_expression
!= NULL
)
4687 rest_expression
->hir(& stmt
->body_instructions
, state
);
4689 if (mode
== ast_do_while
)
4690 condition_to_hir(&stmt
->body_instructions
, state
);
4692 if (mode
!= ast_do_while
)
4693 state
->symbols
->pop_scope();
4695 /* Restore previous nesting before returning. */
4696 state
->loop_nesting_ast
= nesting_ast
;
4697 state
->switch_state
.is_switch_innermost
= saved_is_switch_innermost
;
4699 /* Loops do not have r-values.
4706 * Determine if the given type is valid for establishing a default precision
4709 * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
4711 * "The precision statement
4713 * precision precision-qualifier type;
4715 * can be used to establish a default precision qualifier. The type field
4716 * can be either int or float or any of the sampler types, and the
4717 * precision-qualifier can be lowp, mediump, or highp."
4719 * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
4720 * qualifiers on sampler types, but this seems like an oversight (since the
4721 * intention of including these in GLSL 1.30 is to allow compatibility with ES
4722 * shaders). So we allow int, float, and all sampler types regardless of GLSL
4726 is_valid_default_precision_type(const struct glsl_type
*const type
)
4731 switch (type
->base_type
) {
4733 case GLSL_TYPE_FLOAT
:
4734 /* "int" and "float" are valid, but vectors and matrices are not. */
4735 return type
->vector_elements
== 1 && type
->matrix_columns
== 1;
4736 case GLSL_TYPE_SAMPLER
:
4745 ast_type_specifier::hir(exec_list
*instructions
,
4746 struct _mesa_glsl_parse_state
*state
)
4748 if (this->default_precision
== ast_precision_none
&& this->structure
== NULL
)
4751 YYLTYPE loc
= this->get_location();
4753 /* If this is a precision statement, check that the type to which it is
4754 * applied is either float or int.
4756 * From section 4.5.3 of the GLSL 1.30 spec:
4757 * "The precision statement
4758 * precision precision-qualifier type;
4759 * can be used to establish a default precision qualifier. The type
4760 * field can be either int or float [...]. Any other types or
4761 * qualifiers will result in an error.
4763 if (this->default_precision
!= ast_precision_none
) {
4764 if (!state
->check_precision_qualifiers_allowed(&loc
))
4767 if (this->structure
!= NULL
) {
4768 _mesa_glsl_error(&loc
, state
,
4769 "precision qualifiers do not apply to structures");
4773 if (this->array_specifier
!= NULL
) {
4774 _mesa_glsl_error(&loc
, state
,
4775 "default precision statements do not apply to "
4780 const struct glsl_type
*const type
=
4781 state
->symbols
->get_type(this->type_name
);
4782 if (!is_valid_default_precision_type(type
)) {
4783 _mesa_glsl_error(&loc
, state
,
4784 "default precision statements apply only to "
4785 "float, int, and sampler types");
4789 if (type
->base_type
== GLSL_TYPE_FLOAT
4791 && state
->stage
== MESA_SHADER_FRAGMENT
) {
4792 /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
4795 * "The fragment language has no default precision qualifier for
4796 * floating point types."
4798 * As a result, we have to track whether or not default precision has
4799 * been specified for float in GLSL ES fragment shaders.
4801 * Earlier in that same section, the spec says:
4803 * "Non-precision qualified declarations will use the precision
4804 * qualifier specified in the most recent precision statement
4805 * that is still in scope. The precision statement has the same
4806 * scoping rules as variable declarations. If it is declared
4807 * inside a compound statement, its effect stops at the end of
4808 * the innermost statement it was declared in. Precision
4809 * statements in nested scopes override precision statements in
4810 * outer scopes. Multiple precision statements for the same basic
4811 * type can appear inside the same scope, with later statements
4812 * overriding earlier statements within that scope."
4814 * Default precision specifications follow the same scope rules as
4815 * variables. So, we can track the state of the default float
4816 * precision in the symbol table, and the rules will just work. This
4817 * is a slight abuse of the symbol table, but it has the semantics
4820 ir_variable
*const junk
=
4821 new(state
) ir_variable(type
, "#default precision",
4824 state
->symbols
->add_variable(junk
);
4827 /* FINISHME: Translate precision statements into IR. */
4831 /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
4832 * process_record_constructor() can do type-checking on C-style initializer
4833 * expressions of structs, but ast_struct_specifier should only be translated
4834 * to HIR if it is declaring the type of a structure.
4836 * The ->is_declaration field is false for initializers of variables
4837 * declared separately from the struct's type definition.
4839 * struct S { ... }; (is_declaration = true)
4840 * struct T { ... } t = { ... }; (is_declaration = true)
4841 * S s = { ... }; (is_declaration = false)
4843 if (this->structure
!= NULL
&& this->structure
->is_declaration
)
4844 return this->structure
->hir(instructions
, state
);
4851 * Process a structure or interface block tree into an array of structure fields
4853 * After parsing, where there are some syntax differnces, structures and
4854 * interface blocks are almost identical. They are similar enough that the
4855 * AST for each can be processed the same way into a set of
4856 * \c glsl_struct_field to describe the members.
4858 * If we're processing an interface block, var_mode should be the type of the
4859 * interface block (ir_var_shader_in, ir_var_shader_out, or ir_var_uniform).
4860 * If we're processing a structure, var_mode should be ir_var_auto.
4863 * The number of fields processed. A pointer to the array structure fields is
4864 * stored in \c *fields_ret.
4867 ast_process_structure_or_interface_block(exec_list
*instructions
,
4868 struct _mesa_glsl_parse_state
*state
,
4869 exec_list
*declarations
,
4871 glsl_struct_field
**fields_ret
,
4873 bool block_row_major
,
4874 bool allow_reserved_names
,
4875 ir_variable_mode var_mode
)
4877 unsigned decl_count
= 0;
4879 /* Make an initial pass over the list of fields to determine how
4880 * many there are. Each element in this list is an ast_declarator_list.
4881 * This means that we actually need to count the number of elements in the
4882 * 'declarations' list in each of the elements.
4884 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
4885 foreach_list_const (decl_ptr
, & decl_list
->declarations
) {
4890 /* Allocate storage for the fields and process the field
4891 * declarations. As the declarations are processed, try to also convert
4892 * the types to HIR. This ensures that structure definitions embedded in
4893 * other structure definitions or in interface blocks are processed.
4895 glsl_struct_field
*const fields
= ralloc_array(state
, glsl_struct_field
,
4899 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
4900 const char *type_name
;
4902 decl_list
->type
->specifier
->hir(instructions
, state
);
4904 /* Section 10.9 of the GLSL ES 1.00 specification states that
4905 * embedded structure definitions have been removed from the language.
4907 if (state
->es_shader
&& decl_list
->type
->specifier
->structure
!= NULL
) {
4908 _mesa_glsl_error(&loc
, state
, "embedded structure definitions are "
4909 "not allowed in GLSL ES 1.00");
4912 const glsl_type
*decl_type
=
4913 decl_list
->type
->glsl_type(& type_name
, state
);
4915 foreach_list_typed (ast_declaration
, decl
, link
,
4916 &decl_list
->declarations
) {
4917 if (!allow_reserved_names
)
4918 validate_identifier(decl
->identifier
, loc
, state
);
4920 /* From section 4.3.9 of the GLSL 4.40 spec:
4922 * "[In interface blocks] opaque types are not allowed."
4924 * It should be impossible for decl_type to be NULL here. Cases that
4925 * might naturally lead to decl_type being NULL, especially for the
4926 * is_interface case, will have resulted in compilation having
4927 * already halted due to a syntax error.
4929 const struct glsl_type
*field_type
=
4930 decl_type
!= NULL
? decl_type
: glsl_type::error_type
;
4932 if (is_interface
&& field_type
->contains_opaque()) {
4933 YYLTYPE loc
= decl_list
->get_location();
4934 _mesa_glsl_error(&loc
, state
,
4935 "uniform in non-default uniform block contains "
4939 if (field_type
->contains_atomic()) {
4940 /* FINISHME: Add a spec quotation here once updated spec
4941 * FINISHME: language is available. See Khronos bug #10903
4942 * FINISHME: on whether atomic counters are allowed in
4943 * FINISHME: structures.
4945 YYLTYPE loc
= decl_list
->get_location();
4946 _mesa_glsl_error(&loc
, state
, "atomic counter in structure or "
4950 if (field_type
->contains_image()) {
4951 /* FINISHME: Same problem as with atomic counters.
4952 * FINISHME: Request clarification from Khronos and add
4953 * FINISHME: spec quotation here.
4955 YYLTYPE loc
= decl_list
->get_location();
4956 _mesa_glsl_error(&loc
, state
,
4957 "image in structure or uniform block");
4960 const struct ast_type_qualifier
*const qual
=
4961 & decl_list
->type
->qualifier
;
4962 if (qual
->flags
.q
.std140
||
4963 qual
->flags
.q
.packed
||
4964 qual
->flags
.q
.shared
) {
4965 _mesa_glsl_error(&loc
, state
,
4966 "uniform block layout qualifiers std140, packed, and "
4967 "shared can only be applied to uniform blocks, not "
4971 field_type
= process_array_type(&loc
, decl_type
,
4972 decl
->array_specifier
, state
);
4973 fields
[i
].type
= field_type
;
4974 fields
[i
].name
= decl
->identifier
;
4975 fields
[i
].location
= -1;
4976 fields
[i
].interpolation
=
4977 interpret_interpolation_qualifier(qual
, var_mode
, state
, &loc
);
4978 fields
[i
].centroid
= qual
->flags
.q
.centroid
? 1 : 0;
4979 fields
[i
].sample
= qual
->flags
.q
.sample
? 1 : 0;
4981 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
4982 if (!qual
->flags
.q
.uniform
) {
4983 _mesa_glsl_error(&loc
, state
,
4984 "row_major and column_major can only be "
4985 "applied to uniform interface blocks");
4987 validate_matrix_layout_for_type(state
, &loc
, field_type
, NULL
);
4990 if (qual
->flags
.q
.uniform
&& qual
->has_interpolation()) {
4991 _mesa_glsl_error(&loc
, state
,
4992 "interpolation qualifiers cannot be used "
4993 "with uniform interface blocks");
4996 if (field_type
->is_matrix() ||
4997 (field_type
->is_array() && field_type
->fields
.array
->is_matrix())) {
4998 fields
[i
].row_major
= block_row_major
;
4999 if (qual
->flags
.q
.row_major
)
5000 fields
[i
].row_major
= true;
5001 else if (qual
->flags
.q
.column_major
)
5002 fields
[i
].row_major
= false;
5009 assert(i
== decl_count
);
5011 *fields_ret
= fields
;
5017 ast_struct_specifier::hir(exec_list
*instructions
,
5018 struct _mesa_glsl_parse_state
*state
)
5020 YYLTYPE loc
= this->get_location();
5022 /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
5024 * "Anonymous structures are not supported; so embedded structures must
5025 * have a declarator. A name given to an embedded struct is scoped at
5026 * the same level as the struct it is embedded in."
5028 * The same section of the GLSL 1.20 spec says:
5030 * "Anonymous structures are not supported. Embedded structures are not
5033 * struct S { float f; };
5035 * S; // Error: anonymous structures disallowed
5036 * struct { ... }; // Error: embedded structures disallowed
5037 * S s; // Okay: nested structures with name are allowed
5040 * The GLSL ES 1.00 and 3.00 specs have similar langauge and examples. So,
5041 * we allow embedded structures in 1.10 only.
5043 if (state
->language_version
!= 110 && state
->struct_specifier_depth
!= 0)
5044 _mesa_glsl_error(&loc
, state
,
5045 "embedded structure declartions are not allowed");
5047 state
->struct_specifier_depth
++;
5049 glsl_struct_field
*fields
;
5050 unsigned decl_count
=
5051 ast_process_structure_or_interface_block(instructions
,
5053 &this->declarations
,
5058 false /* allow_reserved_names */,
5061 validate_identifier(this->name
, loc
, state
);
5063 const glsl_type
*t
=
5064 glsl_type::get_record_instance(fields
, decl_count
, this->name
);
5066 if (!state
->symbols
->add_type(name
, t
)) {
5067 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
5069 const glsl_type
**s
= reralloc(state
, state
->user_structures
,
5071 state
->num_user_structures
+ 1);
5073 s
[state
->num_user_structures
] = t
;
5074 state
->user_structures
= s
;
5075 state
->num_user_structures
++;
5079 state
->struct_specifier_depth
--;
5081 /* Structure type definitions do not have r-values.
5088 * Visitor class which detects whether a given interface block has been used.
5090 class interface_block_usage_visitor
: public ir_hierarchical_visitor
5093 interface_block_usage_visitor(ir_variable_mode mode
, const glsl_type
*block
)
5094 : mode(mode
), block(block
), found(false)
5098 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
5100 if (ir
->var
->data
.mode
== mode
&& ir
->var
->get_interface_type() == block
) {
5104 return visit_continue
;
5107 bool usage_found() const
5113 ir_variable_mode mode
;
5114 const glsl_type
*block
;
5120 ast_interface_block::hir(exec_list
*instructions
,
5121 struct _mesa_glsl_parse_state
*state
)
5123 YYLTYPE loc
= this->get_location();
5125 /* The ast_interface_block has a list of ast_declarator_lists. We
5126 * need to turn those into ir_variables with an association
5127 * with this uniform block.
5129 enum glsl_interface_packing packing
;
5130 if (this->layout
.flags
.q
.shared
) {
5131 packing
= GLSL_INTERFACE_PACKING_SHARED
;
5132 } else if (this->layout
.flags
.q
.packed
) {
5133 packing
= GLSL_INTERFACE_PACKING_PACKED
;
5135 /* The default layout is std140.
5137 packing
= GLSL_INTERFACE_PACKING_STD140
;
5140 ir_variable_mode var_mode
;
5141 const char *iface_type_name
;
5142 if (this->layout
.flags
.q
.in
) {
5143 var_mode
= ir_var_shader_in
;
5144 iface_type_name
= "in";
5145 } else if (this->layout
.flags
.q
.out
) {
5146 var_mode
= ir_var_shader_out
;
5147 iface_type_name
= "out";
5148 } else if (this->layout
.flags
.q
.uniform
) {
5149 var_mode
= ir_var_uniform
;
5150 iface_type_name
= "uniform";
5152 var_mode
= ir_var_auto
;
5153 iface_type_name
= "UNKNOWN";
5154 assert(!"interface block layout qualifier not found!");
5157 bool redeclaring_per_vertex
= strcmp(this->block_name
, "gl_PerVertex") == 0;
5158 bool block_row_major
= this->layout
.flags
.q
.row_major
;
5159 exec_list declared_variables
;
5160 glsl_struct_field
*fields
;
5161 unsigned int num_variables
=
5162 ast_process_structure_or_interface_block(&declared_variables
,
5164 &this->declarations
,
5169 redeclaring_per_vertex
,
5172 if (!redeclaring_per_vertex
)
5173 validate_identifier(this->block_name
, loc
, state
);
5175 const glsl_type
*earlier_per_vertex
= NULL
;
5176 if (redeclaring_per_vertex
) {
5177 /* Find the previous declaration of gl_PerVertex. If we're redeclaring
5178 * the named interface block gl_in, we can find it by looking at the
5179 * previous declaration of gl_in. Otherwise we can find it by looking
5180 * at the previous decalartion of any of the built-in outputs,
5183 * Also check that the instance name and array-ness of the redeclaration
5187 case ir_var_shader_in
:
5188 if (ir_variable
*earlier_gl_in
=
5189 state
->symbols
->get_variable("gl_in")) {
5190 earlier_per_vertex
= earlier_gl_in
->get_interface_type();
5192 _mesa_glsl_error(&loc
, state
,
5193 "redeclaration of gl_PerVertex input not allowed "
5195 _mesa_shader_stage_to_string(state
->stage
));
5197 if (this->instance_name
== NULL
||
5198 strcmp(this->instance_name
, "gl_in") != 0 || this->array_specifier
== NULL
) {
5199 _mesa_glsl_error(&loc
, state
,
5200 "gl_PerVertex input must be redeclared as "
5204 case ir_var_shader_out
:
5205 if (ir_variable
*earlier_gl_Position
=
5206 state
->symbols
->get_variable("gl_Position")) {
5207 earlier_per_vertex
= earlier_gl_Position
->get_interface_type();
5209 _mesa_glsl_error(&loc
, state
,
5210 "redeclaration of gl_PerVertex output not "
5211 "allowed in the %s shader",
5212 _mesa_shader_stage_to_string(state
->stage
));
5214 if (this->instance_name
!= NULL
) {
5215 _mesa_glsl_error(&loc
, state
,
5216 "gl_PerVertex input may not be redeclared with "
5217 "an instance name");
5221 _mesa_glsl_error(&loc
, state
,
5222 "gl_PerVertex must be declared as an input or an "
5227 if (earlier_per_vertex
== NULL
) {
5228 /* An error has already been reported. Bail out to avoid null
5229 * dereferences later in this function.
5234 /* Copy locations from the old gl_PerVertex interface block. */
5235 for (unsigned i
= 0; i
< num_variables
; i
++) {
5236 int j
= earlier_per_vertex
->field_index(fields
[i
].name
);
5238 _mesa_glsl_error(&loc
, state
,
5239 "redeclaration of gl_PerVertex must be a subset "
5240 "of the built-in members of gl_PerVertex");
5242 fields
[i
].location
=
5243 earlier_per_vertex
->fields
.structure
[j
].location
;
5244 fields
[i
].interpolation
=
5245 earlier_per_vertex
->fields
.structure
[j
].interpolation
;
5246 fields
[i
].centroid
=
5247 earlier_per_vertex
->fields
.structure
[j
].centroid
;
5249 earlier_per_vertex
->fields
.structure
[j
].sample
;
5253 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10
5256 * If a built-in interface block is redeclared, it must appear in
5257 * the shader before any use of any member included in the built-in
5258 * declaration, or a compilation error will result.
5260 * This appears to be a clarification to the behaviour established for
5261 * gl_PerVertex by GLSL 1.50, therefore we implement this behaviour
5262 * regardless of GLSL version.
5264 interface_block_usage_visitor
v(var_mode
, earlier_per_vertex
);
5265 v
.run(instructions
);
5266 if (v
.usage_found()) {
5267 _mesa_glsl_error(&loc
, state
,
5268 "redeclaration of a built-in interface block must "
5269 "appear before any use of any member of the "
5274 const glsl_type
*block_type
=
5275 glsl_type::get_interface_instance(fields
,
5280 if (!state
->symbols
->add_interface(block_type
->name
, block_type
, var_mode
)) {
5281 YYLTYPE loc
= this->get_location();
5282 _mesa_glsl_error(&loc
, state
, "interface block `%s' with type `%s' "
5283 "already taken in the current scope",
5284 this->block_name
, iface_type_name
);
5287 /* Since interface blocks cannot contain statements, it should be
5288 * impossible for the block to generate any instructions.
5290 assert(declared_variables
.is_empty());
5292 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
5294 * Geometry shader input variables get the per-vertex values written
5295 * out by vertex shader output variables of the same names. Since a
5296 * geometry shader operates on a set of vertices, each input varying
5297 * variable (or input block, see interface blocks below) needs to be
5298 * declared as an array.
5300 if (state
->stage
== MESA_SHADER_GEOMETRY
&& this->array_specifier
== NULL
&&
5301 var_mode
== ir_var_shader_in
) {
5302 _mesa_glsl_error(&loc
, state
, "geometry shader inputs must be arrays");
5305 /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
5308 * "If an instance name (instance-name) is used, then it puts all the
5309 * members inside a scope within its own name space, accessed with the
5310 * field selector ( . ) operator (analogously to structures)."
5312 if (this->instance_name
) {
5313 if (redeclaring_per_vertex
) {
5314 /* When a built-in in an unnamed interface block is redeclared,
5315 * get_variable_being_redeclared() calls
5316 * check_builtin_array_max_size() to make sure that built-in array
5317 * variables aren't redeclared to illegal sizes. But we're looking
5318 * at a redeclaration of a named built-in interface block. So we
5319 * have to manually call check_builtin_array_max_size() for all parts
5320 * of the interface that are arrays.
5322 for (unsigned i
= 0; i
< num_variables
; i
++) {
5323 if (fields
[i
].type
->is_array()) {
5324 const unsigned size
= fields
[i
].type
->array_size();
5325 check_builtin_array_max_size(fields
[i
].name
, size
, loc
, state
);
5329 validate_identifier(this->instance_name
, loc
, state
);
5334 if (this->array_specifier
!= NULL
) {
5335 /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
5337 * For uniform blocks declared an array, each individual array
5338 * element corresponds to a separate buffer object backing one
5339 * instance of the block. As the array size indicates the number
5340 * of buffer objects needed, uniform block array declarations
5341 * must specify an array size.
5343 * And a few paragraphs later:
5345 * Geometry shader input blocks must be declared as arrays and
5346 * follow the array declaration and linking rules for all
5347 * geometry shader inputs. All other input and output block
5348 * arrays must specify an array size.
5350 * The upshot of this is that the only circumstance where an
5351 * interface array size *doesn't* need to be specified is on a
5352 * geometry shader input.
5354 if (this->array_specifier
->is_unsized_array
&&
5355 (state
->stage
!= MESA_SHADER_GEOMETRY
|| !this->layout
.flags
.q
.in
)) {
5356 _mesa_glsl_error(&loc
, state
,
5357 "only geometry shader inputs may be unsized "
5358 "instance block arrays");
5362 const glsl_type
*block_array_type
=
5363 process_array_type(&loc
, block_type
, this->array_specifier
, state
);
5365 var
= new(state
) ir_variable(block_array_type
,
5366 this->instance_name
,
5369 var
= new(state
) ir_variable(block_type
,
5370 this->instance_name
,
5374 if (state
->stage
== MESA_SHADER_GEOMETRY
&& var_mode
== ir_var_shader_in
)
5375 handle_geometry_shader_input_decl(state
, loc
, var
);
5377 if (ir_variable
*earlier
=
5378 state
->symbols
->get_variable(this->instance_name
)) {
5379 if (!redeclaring_per_vertex
) {
5380 _mesa_glsl_error(&loc
, state
, "`%s' redeclared",
5381 this->instance_name
);
5383 earlier
->data
.how_declared
= ir_var_declared_normally
;
5384 earlier
->type
= var
->type
;
5385 earlier
->reinit_interface_type(block_type
);
5388 /* Propagate the "binding" keyword into this UBO's fields;
5389 * the UBO declaration itself doesn't get an ir_variable unless it
5390 * has an instance name. This is ugly.
5392 var
->data
.explicit_binding
= this->layout
.flags
.q
.explicit_binding
;
5393 var
->data
.binding
= this->layout
.binding
;
5395 state
->symbols
->add_variable(var
);
5396 instructions
->push_tail(var
);
5399 /* In order to have an array size, the block must also be declared with
5402 assert(this->array_specifier
== NULL
);
5404 for (unsigned i
= 0; i
< num_variables
; i
++) {
5406 new(state
) ir_variable(fields
[i
].type
,
5407 ralloc_strdup(state
, fields
[i
].name
),
5409 var
->data
.interpolation
= fields
[i
].interpolation
;
5410 var
->data
.centroid
= fields
[i
].centroid
;
5411 var
->data
.sample
= fields
[i
].sample
;
5412 var
->init_interface_type(block_type
);
5414 if (redeclaring_per_vertex
) {
5415 ir_variable
*earlier
=
5416 get_variable_being_redeclared(var
, loc
, state
,
5417 true /* allow_all_redeclarations */);
5418 if (!is_gl_identifier(var
->name
) || earlier
== NULL
) {
5419 _mesa_glsl_error(&loc
, state
,
5420 "redeclaration of gl_PerVertex can only "
5421 "include built-in variables");
5422 } else if (earlier
->data
.how_declared
== ir_var_declared_normally
) {
5423 _mesa_glsl_error(&loc
, state
,
5424 "`%s' has already been redeclared", var
->name
);
5426 earlier
->data
.how_declared
= ir_var_declared_in_block
;
5427 earlier
->reinit_interface_type(block_type
);
5432 if (state
->symbols
->get_variable(var
->name
) != NULL
)
5433 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
5435 /* Propagate the "binding" keyword into this UBO's fields;
5436 * the UBO declaration itself doesn't get an ir_variable unless it
5437 * has an instance name. This is ugly.
5439 var
->data
.explicit_binding
= this->layout
.flags
.q
.explicit_binding
;
5440 var
->data
.binding
= this->layout
.binding
;
5442 state
->symbols
->add_variable(var
);
5443 instructions
->push_tail(var
);
5446 if (redeclaring_per_vertex
&& block_type
!= earlier_per_vertex
) {
5447 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec:
5449 * It is also a compilation error ... to redeclare a built-in
5450 * block and then use a member from that built-in block that was
5451 * not included in the redeclaration.
5453 * This appears to be a clarification to the behaviour established
5454 * for gl_PerVertex by GLSL 1.50, therefore we implement this
5455 * behaviour regardless of GLSL version.
5457 * To prevent the shader from using a member that was not included in
5458 * the redeclaration, we disable any ir_variables that are still
5459 * associated with the old declaration of gl_PerVertex (since we've
5460 * already updated all of the variables contained in the new
5461 * gl_PerVertex to point to it).
5463 * As a side effect this will prevent
5464 * validate_intrastage_interface_blocks() from getting confused and
5465 * thinking there are conflicting definitions of gl_PerVertex in the
5468 foreach_list_safe(node
, instructions
) {
5469 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
5471 var
->get_interface_type() == earlier_per_vertex
&&
5472 var
->data
.mode
== var_mode
) {
5473 if (var
->data
.how_declared
== ir_var_declared_normally
) {
5474 _mesa_glsl_error(&loc
, state
,
5475 "redeclaration of gl_PerVertex cannot "
5476 "follow a redeclaration of `%s'",
5479 state
->symbols
->disable_variable(var
->name
);
5491 ast_gs_input_layout::hir(exec_list
*instructions
,
5492 struct _mesa_glsl_parse_state
*state
)
5494 YYLTYPE loc
= this->get_location();
5496 /* If any geometry input layout declaration preceded this one, make sure it
5497 * was consistent with this one.
5499 if (state
->gs_input_prim_type_specified
&&
5500 state
->in_qualifier
->prim_type
!= this->prim_type
) {
5501 _mesa_glsl_error(&loc
, state
,
5502 "geometry shader input layout does not match"
5503 " previous declaration");
5507 /* If any shader inputs occurred before this declaration and specified an
5508 * array size, make sure the size they specified is consistent with the
5511 unsigned num_vertices
= vertices_per_prim(this->prim_type
);
5512 if (state
->gs_input_size
!= 0 && state
->gs_input_size
!= num_vertices
) {
5513 _mesa_glsl_error(&loc
, state
,
5514 "this geometry shader input layout implies %u vertices"
5515 " per primitive, but a previous input is declared"
5516 " with size %u", num_vertices
, state
->gs_input_size
);
5520 state
->gs_input_prim_type_specified
= true;
5522 /* If any shader inputs occurred before this declaration and did not
5523 * specify an array size, their size is determined now.
5525 foreach_list (node
, instructions
) {
5526 ir_variable
*var
= ((ir_instruction
*) node
)->as_variable();
5527 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_in
)
5530 /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
5534 if (var
->type
->is_unsized_array()) {
5535 if (var
->data
.max_array_access
>= num_vertices
) {
5536 _mesa_glsl_error(&loc
, state
,
5537 "this geometry shader input layout implies %u"
5538 " vertices, but an access to element %u of input"
5539 " `%s' already exists", num_vertices
,
5540 var
->data
.max_array_access
, var
->name
);
5542 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
5553 ast_cs_input_layout::hir(exec_list
*instructions
,
5554 struct _mesa_glsl_parse_state
*state
)
5556 YYLTYPE loc
= this->get_location();
5558 /* If any compute input layout declaration preceded this one, make sure it
5559 * was consistent with this one.
5561 if (state
->cs_input_local_size_specified
) {
5562 for (int i
= 0; i
< 3; i
++) {
5563 if (state
->cs_input_local_size
[i
] != this->local_size
[i
]) {
5564 _mesa_glsl_error(&loc
, state
,
5565 "compute shader input layout does not match"
5566 " previous declaration");
5572 /* From the ARB_compute_shader specification:
5574 * If the local size of the shader in any dimension is greater
5575 * than the maximum size supported by the implementation for that
5576 * dimension, a compile-time error results.
5578 * It is not clear from the spec how the error should be reported if
5579 * the total size of the work group exceeds
5580 * MAX_COMPUTE_WORK_GROUP_INVOCATIONS, but it seems reasonable to
5581 * report it at compile time as well.
5583 GLuint64 total_invocations
= 1;
5584 for (int i
= 0; i
< 3; i
++) {
5585 if (this->local_size
[i
] > state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]) {
5586 _mesa_glsl_error(&loc
, state
,
5587 "local_size_%c exceeds MAX_COMPUTE_WORK_GROUP_SIZE"
5589 state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]);
5592 total_invocations
*= this->local_size
[i
];
5593 if (total_invocations
>
5594 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
) {
5595 _mesa_glsl_error(&loc
, state
,
5596 "product of local_sizes exceeds "
5597 "MAX_COMPUTE_WORK_GROUP_INVOCATIONS (%d)",
5598 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
);
5603 state
->cs_input_local_size_specified
= true;
5604 for (int i
= 0; i
< 3; i
++)
5605 state
->cs_input_local_size
[i
] = this->local_size
[i
];
5607 /* We may now declare the built-in constant gl_WorkGroupSize (see
5608 * builtin_variable_generator::generate_constants() for why we didn't
5609 * declare it earlier).
5611 ir_variable
*var
= new(state
->symbols
)
5612 ir_variable(glsl_type::ivec3_type
, "gl_WorkGroupSize", ir_var_auto
);
5613 var
->data
.how_declared
= ir_var_declared_implicitly
;
5614 var
->data
.read_only
= true;
5615 instructions
->push_tail(var
);
5616 state
->symbols
->add_variable(var
);
5617 ir_constant_data data
;
5618 memset(&data
, 0, sizeof(data
));
5619 for (int i
= 0; i
< 3; i
++)
5620 data
.i
[i
] = this->local_size
[i
];
5621 var
->constant_value
= new(var
) ir_constant(glsl_type::ivec3_type
, &data
);
5622 var
->constant_initializer
=
5623 new(var
) ir_constant(glsl_type::ivec3_type
, &data
);
5624 var
->data
.has_initializer
= true;
5631 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
5632 exec_list
*instructions
)
5634 bool gl_FragColor_assigned
= false;
5635 bool gl_FragData_assigned
= false;
5636 bool user_defined_fs_output_assigned
= false;
5637 ir_variable
*user_defined_fs_output
= NULL
;
5639 /* It would be nice to have proper location information. */
5641 memset(&loc
, 0, sizeof(loc
));
5643 foreach_list(node
, instructions
) {
5644 ir_variable
*var
= ((ir_instruction
*)node
)->as_variable();
5646 if (!var
|| !var
->data
.assigned
)
5649 if (strcmp(var
->name
, "gl_FragColor") == 0)
5650 gl_FragColor_assigned
= true;
5651 else if (strcmp(var
->name
, "gl_FragData") == 0)
5652 gl_FragData_assigned
= true;
5653 else if (!is_gl_identifier(var
->name
)) {
5654 if (state
->stage
== MESA_SHADER_FRAGMENT
&&
5655 var
->data
.mode
== ir_var_shader_out
) {
5656 user_defined_fs_output_assigned
= true;
5657 user_defined_fs_output
= var
;
5662 /* From the GLSL 1.30 spec:
5664 * "If a shader statically assigns a value to gl_FragColor, it
5665 * may not assign a value to any element of gl_FragData. If a
5666 * shader statically writes a value to any element of
5667 * gl_FragData, it may not assign a value to
5668 * gl_FragColor. That is, a shader may assign values to either
5669 * gl_FragColor or gl_FragData, but not both. Multiple shaders
5670 * linked together must also consistently write just one of
5671 * these variables. Similarly, if user declared output
5672 * variables are in use (statically assigned to), then the
5673 * built-in variables gl_FragColor and gl_FragData may not be
5674 * assigned to. These incorrect usages all generate compile
5677 if (gl_FragColor_assigned
&& gl_FragData_assigned
) {
5678 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
5679 "`gl_FragColor' and `gl_FragData'");
5680 } else if (gl_FragColor_assigned
&& user_defined_fs_output_assigned
) {
5681 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
5682 "`gl_FragColor' and `%s'",
5683 user_defined_fs_output
->name
);
5684 } else if (gl_FragData_assigned
&& user_defined_fs_output_assigned
) {
5685 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
5686 "`gl_FragData' and `%s'",
5687 user_defined_fs_output
->name
);
5693 remove_per_vertex_blocks(exec_list
*instructions
,
5694 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
)
5696 /* Find the gl_PerVertex interface block of the appropriate (in/out) mode,
5697 * if it exists in this shader type.
5699 const glsl_type
*per_vertex
= NULL
;
5701 case ir_var_shader_in
:
5702 if (ir_variable
*gl_in
= state
->symbols
->get_variable("gl_in"))
5703 per_vertex
= gl_in
->get_interface_type();
5705 case ir_var_shader_out
:
5706 if (ir_variable
*gl_Position
=
5707 state
->symbols
->get_variable("gl_Position")) {
5708 per_vertex
= gl_Position
->get_interface_type();
5712 assert(!"Unexpected mode");
5716 /* If we didn't find a built-in gl_PerVertex interface block, then we don't
5717 * need to do anything.
5719 if (per_vertex
== NULL
)
5722 /* If the interface block is used by the shader, then we don't need to do
5725 interface_block_usage_visitor
v(mode
, per_vertex
);
5726 v
.run(instructions
);
5727 if (v
.usage_found())
5730 /* Remove any ir_variable declarations that refer to the interface block
5733 foreach_list_safe(node
, instructions
) {
5734 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
5735 if (var
!= NULL
&& var
->get_interface_type() == per_vertex
&&
5736 var
->data
.mode
== mode
) {
5737 state
->symbols
->disable_variable(var
->name
);