2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
26 * Convert abstract syntax to to high-level intermediate reprensentation (HIR).
28 * During the conversion to HIR, the majority of the symantic checking is
29 * preformed on the program. This includes:
31 * * Symbol table management
35 * The majority of this work could be done during parsing, and the parser could
36 * probably generate HIR directly. However, this results in frequent changes
37 * to the parser code. Since we do not assume that every system this complier
38 * is built on will have Flex and Bison installed, we have to store the code
39 * generated by these tools in our version control system. In other parts of
40 * the system we've seen problems where a parser was changed but the generated
41 * code was not committed, merge conflicts where created because two developers
42 * had slightly different versions of Bison installed, etc.
44 * I have also noticed that running Bison generated parsers in GDB is very
45 * irritating. When you get a segfault on '$$ = $1->foo', you can't very
46 * well 'print $1' in GDB.
48 * As a result, my preference is to put as little C code as possible in the
49 * parser (and lexer) sources.
52 #include "glsl_symbol_table.h"
53 #include "glsl_parser_extras.h"
55 #include "glsl_types.h"
56 #include "program/hash_table.h"
58 #include "ir_builder.h"
60 using namespace ir_builder
;
63 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
64 exec_list
*instructions
);
66 remove_per_vertex_blocks(exec_list
*instructions
,
67 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
);
71 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
73 _mesa_glsl_initialize_variables(instructions
, state
);
75 state
->symbols
->separate_function_namespace
= state
->language_version
== 110;
77 state
->current_function
= NULL
;
79 state
->toplevel_ir
= instructions
;
81 state
->gs_input_prim_type_specified
= false;
82 state
->cs_input_local_size_specified
= false;
84 /* Section 4.2 of the GLSL 1.20 specification states:
85 * "The built-in functions are scoped in a scope outside the global scope
86 * users declare global variables in. That is, a shader's global scope,
87 * available for user-defined functions and global variables, is nested
88 * inside the scope containing the built-in functions."
90 * Since built-in functions like ftransform() access built-in variables,
91 * it follows that those must be in the outer scope as well.
93 * We push scope here to create this nesting effect...but don't pop.
94 * This way, a shader's globals are still in the symbol table for use
97 state
->symbols
->push_scope();
99 foreach_list_typed (ast_node
, ast
, link
, & state
->translation_unit
)
100 ast
->hir(instructions
, state
);
102 detect_recursion_unlinked(state
, instructions
);
103 detect_conflicting_assignments(state
, instructions
);
105 state
->toplevel_ir
= NULL
;
107 /* Move all of the variable declarations to the front of the IR list, and
108 * reverse the order. This has the (intended!) side effect that vertex
109 * shader inputs and fragment shader outputs will appear in the IR in the
110 * same order that they appeared in the shader code. This results in the
111 * locations being assigned in the declared order. Many (arguably buggy)
112 * applications depend on this behavior, and it matches what nearly all
115 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
116 ir_variable
*const var
= node
->as_variable();
122 instructions
->push_head(var
);
125 /* Figure out if gl_FragCoord is actually used in fragment shader */
126 ir_variable
*const var
= state
->symbols
->get_variable("gl_FragCoord");
128 state
->fs_uses_gl_fragcoord
= var
->data
.used
;
130 /* From section 7.1 (Built-In Language Variables) of the GLSL 4.10 spec:
132 * If multiple shaders using members of a built-in block belonging to
133 * the same interface are linked together in the same program, they
134 * must all redeclare the built-in block in the same way, as described
135 * in section 4.3.7 "Interface Blocks" for interface block matching, or
136 * a link error will result.
138 * The phrase "using members of a built-in block" implies that if two
139 * shaders are linked together and one of them *does not use* any members
140 * of the built-in block, then that shader does not need to have a matching
141 * redeclaration of the built-in block.
143 * This appears to be a clarification to the behaviour established for
144 * gl_PerVertex by GLSL 1.50, therefore implement it regardless of GLSL
147 * The definition of "interface" in section 4.3.7 that applies here is as
150 * The boundary between adjacent programmable pipeline stages: This
151 * spans all the outputs in all compilation units of the first stage
152 * and all the inputs in all compilation units of the second stage.
154 * Therefore this rule applies to both inter- and intra-stage linking.
156 * The easiest way to implement this is to check whether the shader uses
157 * gl_PerVertex right after ast-to-ir conversion, and if it doesn't, simply
158 * remove all the relevant variable declaration from the IR, so that the
159 * linker won't see them and complain about mismatches.
161 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_in
);
162 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_out
);
166 static ir_expression_operation
167 get_conversion_operation(const glsl_type
*to
, const glsl_type
*from
,
168 struct _mesa_glsl_parse_state
*state
)
170 switch (to
->base_type
) {
171 case GLSL_TYPE_FLOAT
:
172 switch (from
->base_type
) {
173 case GLSL_TYPE_INT
: return ir_unop_i2f
;
174 case GLSL_TYPE_UINT
: return ir_unop_u2f
;
175 default: return (ir_expression_operation
)0;
179 if (!state
->is_version(400, 0) && !state
->ARB_gpu_shader5_enable
)
180 return (ir_expression_operation
)0;
181 switch (from
->base_type
) {
182 case GLSL_TYPE_INT
: return ir_unop_i2u
;
183 default: return (ir_expression_operation
)0;
186 default: return (ir_expression_operation
)0;
192 * If a conversion is available, convert one operand to a different type
194 * The \c from \c ir_rvalue is converted "in place".
196 * \param to Type that the operand it to be converted to
197 * \param from Operand that is being converted
198 * \param state GLSL compiler state
201 * If a conversion is possible (or unnecessary), \c true is returned.
202 * Otherwise \c false is returned.
205 apply_implicit_conversion(const glsl_type
*to
, ir_rvalue
* &from
,
206 struct _mesa_glsl_parse_state
*state
)
209 if (to
->base_type
== from
->type
->base_type
)
212 /* Prior to GLSL 1.20, there are no implicit conversions */
213 if (!state
->is_version(120, 0))
216 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
218 * "There are no implicit array or structure conversions. For
219 * example, an array of int cannot be implicitly converted to an
222 if (!to
->is_numeric() || !from
->type
->is_numeric())
225 /* We don't actually want the specific type `to`, we want a type
226 * with the same base type as `to`, but the same vector width as
229 to
= glsl_type::get_instance(to
->base_type
, from
->type
->vector_elements
,
230 from
->type
->matrix_columns
);
232 ir_expression_operation op
= get_conversion_operation(to
, from
->type
, state
);
234 from
= new(ctx
) ir_expression(op
, to
, from
, NULL
);
242 static const struct glsl_type
*
243 arithmetic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
245 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
247 const glsl_type
*type_a
= value_a
->type
;
248 const glsl_type
*type_b
= value_b
->type
;
250 /* From GLSL 1.50 spec, page 56:
252 * "The arithmetic binary operators add (+), subtract (-),
253 * multiply (*), and divide (/) operate on integer and
254 * floating-point scalars, vectors, and matrices."
256 if (!type_a
->is_numeric() || !type_b
->is_numeric()) {
257 _mesa_glsl_error(loc
, state
,
258 "operands to arithmetic operators must be numeric");
259 return glsl_type::error_type
;
263 /* "If one operand is floating-point based and the other is
264 * not, then the conversions from Section 4.1.10 "Implicit
265 * Conversions" are applied to the non-floating-point-based operand."
267 if (!apply_implicit_conversion(type_a
, value_b
, state
)
268 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
269 _mesa_glsl_error(loc
, state
,
270 "could not implicitly convert operands to "
271 "arithmetic operator");
272 return glsl_type::error_type
;
274 type_a
= value_a
->type
;
275 type_b
= value_b
->type
;
277 /* "If the operands are integer types, they must both be signed or
280 * From this rule and the preceeding conversion it can be inferred that
281 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
282 * The is_numeric check above already filtered out the case where either
283 * type is not one of these, so now the base types need only be tested for
286 if (type_a
->base_type
!= type_b
->base_type
) {
287 _mesa_glsl_error(loc
, state
,
288 "base type mismatch for arithmetic operator");
289 return glsl_type::error_type
;
292 /* "All arithmetic binary operators result in the same fundamental type
293 * (signed integer, unsigned integer, or floating-point) as the
294 * operands they operate on, after operand type conversion. After
295 * conversion, the following cases are valid
297 * * The two operands are scalars. In this case the operation is
298 * applied, resulting in a scalar."
300 if (type_a
->is_scalar() && type_b
->is_scalar())
303 /* "* One operand is a scalar, and the other is a vector or matrix.
304 * In this case, the scalar operation is applied independently to each
305 * component of the vector or matrix, resulting in the same size
308 if (type_a
->is_scalar()) {
309 if (!type_b
->is_scalar())
311 } else if (type_b
->is_scalar()) {
315 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
316 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
319 assert(!type_a
->is_scalar());
320 assert(!type_b
->is_scalar());
322 /* "* The two operands are vectors of the same size. In this case, the
323 * operation is done component-wise resulting in the same size
326 if (type_a
->is_vector() && type_b
->is_vector()) {
327 if (type_a
== type_b
) {
330 _mesa_glsl_error(loc
, state
,
331 "vector size mismatch for arithmetic operator");
332 return glsl_type::error_type
;
336 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
337 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
338 * <vector, vector> have been handled. At least one of the operands must
339 * be matrix. Further, since there are no integer matrix types, the base
340 * type of both operands must be float.
342 assert(type_a
->is_matrix() || type_b
->is_matrix());
343 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
);
344 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
);
346 /* "* The operator is add (+), subtract (-), or divide (/), and the
347 * operands are matrices with the same number of rows and the same
348 * number of columns. In this case, the operation is done component-
349 * wise resulting in the same size matrix."
350 * * The operator is multiply (*), where both operands are matrices or
351 * one operand is a vector and the other a matrix. A right vector
352 * operand is treated as a column vector and a left vector operand as a
353 * row vector. In all these cases, it is required that the number of
354 * columns of the left operand is equal to the number of rows of the
355 * right operand. Then, the multiply (*) operation does a linear
356 * algebraic multiply, yielding an object that has the same number of
357 * rows as the left operand and the same number of columns as the right
358 * operand. Section 5.10 "Vector and Matrix Operations" explains in
359 * more detail how vectors and matrices are operated on."
362 if (type_a
== type_b
)
365 if (type_a
->is_matrix() && type_b
->is_matrix()) {
366 /* Matrix multiply. The columns of A must match the rows of B. Given
367 * the other previously tested constraints, this means the vector type
368 * of a row from A must be the same as the vector type of a column from
371 if (type_a
->row_type() == type_b
->column_type()) {
372 /* The resulting matrix has the number of columns of matrix B and
373 * the number of rows of matrix A. We get the row count of A by
374 * looking at the size of a vector that makes up a column. The
375 * transpose (size of a row) is done for B.
377 const glsl_type
*const type
=
378 glsl_type::get_instance(type_a
->base_type
,
379 type_a
->column_type()->vector_elements
,
380 type_b
->row_type()->vector_elements
);
381 assert(type
!= glsl_type::error_type
);
385 } else if (type_a
->is_matrix()) {
386 /* A is a matrix and B is a column vector. Columns of A must match
387 * rows of B. Given the other previously tested constraints, this
388 * means the vector type of a row from A must be the same as the
389 * vector the type of B.
391 if (type_a
->row_type() == type_b
) {
392 /* The resulting vector has a number of elements equal to
393 * the number of rows of matrix A. */
394 const glsl_type
*const type
=
395 glsl_type::get_instance(type_a
->base_type
,
396 type_a
->column_type()->vector_elements
,
398 assert(type
!= glsl_type::error_type
);
403 assert(type_b
->is_matrix());
405 /* A is a row vector and B is a matrix. Columns of A must match rows
406 * of B. Given the other previously tested constraints, this means
407 * the type of A must be the same as the vector type of a column from
410 if (type_a
== type_b
->column_type()) {
411 /* The resulting vector has a number of elements equal to
412 * the number of columns of matrix B. */
413 const glsl_type
*const type
=
414 glsl_type::get_instance(type_a
->base_type
,
415 type_b
->row_type()->vector_elements
,
417 assert(type
!= glsl_type::error_type
);
423 _mesa_glsl_error(loc
, state
, "size mismatch for matrix multiplication");
424 return glsl_type::error_type
;
428 /* "All other cases are illegal."
430 _mesa_glsl_error(loc
, state
, "type mismatch");
431 return glsl_type::error_type
;
435 static const struct glsl_type
*
436 unary_arithmetic_result_type(const struct glsl_type
*type
,
437 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
439 /* From GLSL 1.50 spec, page 57:
441 * "The arithmetic unary operators negate (-), post- and pre-increment
442 * and decrement (-- and ++) operate on integer or floating-point
443 * values (including vectors and matrices). All unary operators work
444 * component-wise on their operands. These result with the same type
447 if (!type
->is_numeric()) {
448 _mesa_glsl_error(loc
, state
,
449 "operands to arithmetic operators must be numeric");
450 return glsl_type::error_type
;
457 * \brief Return the result type of a bit-logic operation.
459 * If the given types to the bit-logic operator are invalid, return
460 * glsl_type::error_type.
462 * \param type_a Type of LHS of bit-logic op
463 * \param type_b Type of RHS of bit-logic op
465 static const struct glsl_type
*
466 bit_logic_result_type(const struct glsl_type
*type_a
,
467 const struct glsl_type
*type_b
,
469 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
471 if (!state
->check_bitwise_operations_allowed(loc
)) {
472 return glsl_type::error_type
;
475 /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
477 * "The bitwise operators and (&), exclusive-or (^), and inclusive-or
478 * (|). The operands must be of type signed or unsigned integers or
481 if (!type_a
->is_integer()) {
482 _mesa_glsl_error(loc
, state
, "LHS of `%s' must be an integer",
483 ast_expression::operator_string(op
));
484 return glsl_type::error_type
;
486 if (!type_b
->is_integer()) {
487 _mesa_glsl_error(loc
, state
, "RHS of `%s' must be an integer",
488 ast_expression::operator_string(op
));
489 return glsl_type::error_type
;
492 /* "The fundamental types of the operands (signed or unsigned) must
495 if (type_a
->base_type
!= type_b
->base_type
) {
496 _mesa_glsl_error(loc
, state
, "operands of `%s' must have the same "
497 "base type", ast_expression::operator_string(op
));
498 return glsl_type::error_type
;
501 /* "The operands cannot be vectors of differing size." */
502 if (type_a
->is_vector() &&
503 type_b
->is_vector() &&
504 type_a
->vector_elements
!= type_b
->vector_elements
) {
505 _mesa_glsl_error(loc
, state
, "operands of `%s' cannot be vectors of "
506 "different sizes", ast_expression::operator_string(op
));
507 return glsl_type::error_type
;
510 /* "If one operand is a scalar and the other a vector, the scalar is
511 * applied component-wise to the vector, resulting in the same type as
512 * the vector. The fundamental types of the operands [...] will be the
513 * resulting fundamental type."
515 if (type_a
->is_scalar())
521 static const struct glsl_type
*
522 modulus_result_type(const struct glsl_type
*type_a
,
523 const struct glsl_type
*type_b
,
524 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
526 if (!state
->check_version(130, 300, loc
, "operator '%%' is reserved")) {
527 return glsl_type::error_type
;
530 /* From GLSL 1.50 spec, page 56:
531 * "The operator modulus (%) operates on signed or unsigned integers or
532 * integer vectors. The operand types must both be signed or both be
535 if (!type_a
->is_integer()) {
536 _mesa_glsl_error(loc
, state
, "LHS of operator %% must be an integer");
537 return glsl_type::error_type
;
539 if (!type_b
->is_integer()) {
540 _mesa_glsl_error(loc
, state
, "RHS of operator %% must be an integer");
541 return glsl_type::error_type
;
543 if (type_a
->base_type
!= type_b
->base_type
) {
544 _mesa_glsl_error(loc
, state
,
545 "operands of %% must have the same base type");
546 return glsl_type::error_type
;
549 /* "The operands cannot be vectors of differing size. If one operand is
550 * a scalar and the other vector, then the scalar is applied component-
551 * wise to the vector, resulting in the same type as the vector. If both
552 * are vectors of the same size, the result is computed component-wise."
554 if (type_a
->is_vector()) {
555 if (!type_b
->is_vector()
556 || (type_a
->vector_elements
== type_b
->vector_elements
))
561 /* "The operator modulus (%) is not defined for any other data types
562 * (non-integer types)."
564 _mesa_glsl_error(loc
, state
, "type mismatch");
565 return glsl_type::error_type
;
569 static const struct glsl_type
*
570 relational_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
571 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
573 const glsl_type
*type_a
= value_a
->type
;
574 const glsl_type
*type_b
= value_b
->type
;
576 /* From GLSL 1.50 spec, page 56:
577 * "The relational operators greater than (>), less than (<), greater
578 * than or equal (>=), and less than or equal (<=) operate only on
579 * scalar integer and scalar floating-point expressions."
581 if (!type_a
->is_numeric()
582 || !type_b
->is_numeric()
583 || !type_a
->is_scalar()
584 || !type_b
->is_scalar()) {
585 _mesa_glsl_error(loc
, state
,
586 "operands to relational operators must be scalar and "
588 return glsl_type::error_type
;
591 /* "Either the operands' types must match, or the conversions from
592 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
593 * operand, after which the types must match."
595 if (!apply_implicit_conversion(type_a
, value_b
, state
)
596 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
597 _mesa_glsl_error(loc
, state
,
598 "could not implicitly convert operands to "
599 "relational operator");
600 return glsl_type::error_type
;
602 type_a
= value_a
->type
;
603 type_b
= value_b
->type
;
605 if (type_a
->base_type
!= type_b
->base_type
) {
606 _mesa_glsl_error(loc
, state
, "base type mismatch");
607 return glsl_type::error_type
;
610 /* "The result is scalar Boolean."
612 return glsl_type::bool_type
;
616 * \brief Return the result type of a bit-shift operation.
618 * If the given types to the bit-shift operator are invalid, return
619 * glsl_type::error_type.
621 * \param type_a Type of LHS of bit-shift op
622 * \param type_b Type of RHS of bit-shift op
624 static const struct glsl_type
*
625 shift_result_type(const struct glsl_type
*type_a
,
626 const struct glsl_type
*type_b
,
628 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
630 if (!state
->check_bitwise_operations_allowed(loc
)) {
631 return glsl_type::error_type
;
634 /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
636 * "The shift operators (<<) and (>>). For both operators, the operands
637 * must be signed or unsigned integers or integer vectors. One operand
638 * can be signed while the other is unsigned."
640 if (!type_a
->is_integer()) {
641 _mesa_glsl_error(loc
, state
, "LHS of operator %s must be an integer or "
642 "integer vector", ast_expression::operator_string(op
));
643 return glsl_type::error_type
;
646 if (!type_b
->is_integer()) {
647 _mesa_glsl_error(loc
, state
, "RHS of operator %s must be an integer or "
648 "integer vector", ast_expression::operator_string(op
));
649 return glsl_type::error_type
;
652 /* "If the first operand is a scalar, the second operand has to be
655 if (type_a
->is_scalar() && !type_b
->is_scalar()) {
656 _mesa_glsl_error(loc
, state
, "if the first operand of %s is scalar, the "
657 "second must be scalar as well",
658 ast_expression::operator_string(op
));
659 return glsl_type::error_type
;
662 /* If both operands are vectors, check that they have same number of
665 if (type_a
->is_vector() &&
666 type_b
->is_vector() &&
667 type_a
->vector_elements
!= type_b
->vector_elements
) {
668 _mesa_glsl_error(loc
, state
, "vector operands to operator %s must "
669 "have same number of elements",
670 ast_expression::operator_string(op
));
671 return glsl_type::error_type
;
674 /* "In all cases, the resulting type will be the same type as the left
681 * Validates that a value can be assigned to a location with a specified type
683 * Validates that \c rhs can be assigned to some location. If the types are
684 * not an exact match but an automatic conversion is possible, \c rhs will be
688 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
689 * Otherwise the actual RHS to be assigned will be returned. This may be
690 * \c rhs, or it may be \c rhs after some type conversion.
693 * In addition to being used for assignments, this function is used to
694 * type-check return values.
697 validate_assignment(struct _mesa_glsl_parse_state
*state
,
698 YYLTYPE loc
, const glsl_type
*lhs_type
,
699 ir_rvalue
*rhs
, bool is_initializer
)
701 /* If there is already some error in the RHS, just return it. Anything
702 * else will lead to an avalanche of error message back to the user.
704 if (rhs
->type
->is_error())
707 /* If the types are identical, the assignment can trivially proceed.
709 if (rhs
->type
== lhs_type
)
712 /* If the array element types are the same and the LHS is unsized,
713 * the assignment is okay for initializers embedded in variable
716 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
717 * is handled by ir_dereference::is_lvalue.
719 if (lhs_type
->is_unsized_array() && rhs
->type
->is_array()
720 && (lhs_type
->element_type() == rhs
->type
->element_type())) {
721 if (is_initializer
) {
724 _mesa_glsl_error(&loc
, state
,
725 "implicitly sized arrays cannot be assigned");
730 /* Check for implicit conversion in GLSL 1.20 */
731 if (apply_implicit_conversion(lhs_type
, rhs
, state
)) {
732 if (rhs
->type
== lhs_type
)
736 _mesa_glsl_error(&loc
, state
,
737 "%s of type %s cannot be assigned to "
738 "variable of type %s",
739 is_initializer
? "initializer" : "value",
740 rhs
->type
->name
, lhs_type
->name
);
746 mark_whole_array_access(ir_rvalue
*access
)
748 ir_dereference_variable
*deref
= access
->as_dereference_variable();
750 if (deref
&& deref
->var
) {
751 deref
->var
->data
.max_array_access
= deref
->type
->length
- 1;
756 do_assignment(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
757 const char *non_lvalue_description
,
758 ir_rvalue
*lhs
, ir_rvalue
*rhs
,
759 ir_rvalue
**out_rvalue
, bool needs_rvalue
,
764 bool error_emitted
= (lhs
->type
->is_error() || rhs
->type
->is_error());
765 ir_rvalue
*extract_channel
= NULL
;
767 /* If the assignment LHS comes back as an ir_binop_vector_extract
768 * expression, move it to the RHS as an ir_triop_vector_insert.
770 if (lhs
->ir_type
== ir_type_expression
) {
771 ir_expression
*const lhs_expr
= lhs
->as_expression();
773 if (unlikely(lhs_expr
->operation
== ir_binop_vector_extract
)) {
775 validate_assignment(state
, lhs_loc
, lhs
->type
,
776 rhs
, is_initializer
);
778 if (new_rhs
== NULL
) {
782 * - LHS: (expression float vector_extract <vec> <channel>)
786 * - RHS: (expression vec2 vector_insert <vec> <channel> <scalar>)
788 * The LHS type is now a vector instead of a scalar. Since GLSL
789 * allows assignments to be used as rvalues, we need to re-extract
790 * the channel from assignment_temp when returning the rvalue.
792 extract_channel
= lhs_expr
->operands
[1];
793 rhs
= new(ctx
) ir_expression(ir_triop_vector_insert
,
794 lhs_expr
->operands
[0]->type
,
795 lhs_expr
->operands
[0],
798 lhs
= lhs_expr
->operands
[0]->clone(ctx
, NULL
);
803 ir_variable
*lhs_var
= lhs
->variable_referenced();
805 lhs_var
->data
.assigned
= true;
807 if (!error_emitted
) {
808 if (non_lvalue_description
!= NULL
) {
809 _mesa_glsl_error(&lhs_loc
, state
,
811 non_lvalue_description
);
812 error_emitted
= true;
813 } else if (lhs_var
!= NULL
&& lhs_var
->data
.read_only
) {
814 _mesa_glsl_error(&lhs_loc
, state
,
815 "assignment to read-only variable '%s'",
817 error_emitted
= true;
818 } else if (lhs
->type
->is_array() &&
819 !state
->check_version(120, 300, &lhs_loc
,
820 "whole array assignment forbidden")) {
821 /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
823 * "Other binary or unary expressions, non-dereferenced
824 * arrays, function names, swizzles with repeated fields,
825 * and constants cannot be l-values."
827 * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00.
829 error_emitted
= true;
830 } else if (!lhs
->is_lvalue()) {
831 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
832 error_emitted
= true;
837 validate_assignment(state
, lhs_loc
, lhs
->type
, rhs
, is_initializer
);
838 if (new_rhs
!= NULL
) {
841 /* If the LHS array was not declared with a size, it takes it size from
842 * the RHS. If the LHS is an l-value and a whole array, it must be a
843 * dereference of a variable. Any other case would require that the LHS
844 * is either not an l-value or not a whole array.
846 if (lhs
->type
->is_unsized_array()) {
847 ir_dereference
*const d
= lhs
->as_dereference();
851 ir_variable
*const var
= d
->variable_referenced();
855 if (var
->data
.max_array_access
>= unsigned(rhs
->type
->array_size())) {
856 /* FINISHME: This should actually log the location of the RHS. */
857 _mesa_glsl_error(& lhs_loc
, state
, "array size must be > %u due to "
859 var
->data
.max_array_access
);
862 var
->type
= glsl_type::get_array_instance(lhs
->type
->element_type(),
863 rhs
->type
->array_size());
866 if (lhs
->type
->is_array()) {
867 mark_whole_array_access(rhs
);
868 mark_whole_array_access(lhs
);
872 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
873 * but not post_inc) need the converted assigned value as an rvalue
874 * to handle things like:
879 ir_variable
*var
= new(ctx
) ir_variable(rhs
->type
, "assignment_tmp",
881 instructions
->push_tail(var
);
882 instructions
->push_tail(assign(var
, rhs
));
884 if (!error_emitted
) {
885 ir_dereference_variable
*deref_var
= new(ctx
) ir_dereference_variable(var
);
886 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, deref_var
));
888 ir_rvalue
*rvalue
= new(ctx
) ir_dereference_variable(var
);
890 if (extract_channel
) {
891 rvalue
= new(ctx
) ir_expression(ir_binop_vector_extract
,
893 extract_channel
->clone(ctx
, NULL
));
896 *out_rvalue
= rvalue
;
899 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, rhs
));
903 return error_emitted
;
907 get_lvalue_copy(exec_list
*instructions
, ir_rvalue
*lvalue
)
909 void *ctx
= ralloc_parent(lvalue
);
912 var
= new(ctx
) ir_variable(lvalue
->type
, "_post_incdec_tmp",
914 instructions
->push_tail(var
);
916 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
919 return new(ctx
) ir_dereference_variable(var
);
924 ast_node::hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
933 ast_function_expression::hir_no_rvalue(exec_list
*instructions
,
934 struct _mesa_glsl_parse_state
*state
)
936 (void)hir(instructions
, state
);
940 ast_aggregate_initializer::hir_no_rvalue(exec_list
*instructions
,
941 struct _mesa_glsl_parse_state
*state
)
943 (void)hir(instructions
, state
);
947 do_comparison(void *mem_ctx
, int operation
, ir_rvalue
*op0
, ir_rvalue
*op1
)
950 ir_rvalue
*cmp
= NULL
;
952 if (operation
== ir_binop_all_equal
)
953 join_op
= ir_binop_logic_and
;
955 join_op
= ir_binop_logic_or
;
957 switch (op0
->type
->base_type
) {
958 case GLSL_TYPE_FLOAT
:
962 return new(mem_ctx
) ir_expression(operation
, op0
, op1
);
964 case GLSL_TYPE_ARRAY
: {
965 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
966 ir_rvalue
*e0
, *e1
, *result
;
968 e0
= new(mem_ctx
) ir_dereference_array(op0
->clone(mem_ctx
, NULL
),
969 new(mem_ctx
) ir_constant(i
));
970 e1
= new(mem_ctx
) ir_dereference_array(op1
->clone(mem_ctx
, NULL
),
971 new(mem_ctx
) ir_constant(i
));
972 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
975 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
981 mark_whole_array_access(op0
);
982 mark_whole_array_access(op1
);
986 case GLSL_TYPE_STRUCT
: {
987 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
988 ir_rvalue
*e0
, *e1
, *result
;
989 const char *field_name
= op0
->type
->fields
.structure
[i
].name
;
991 e0
= new(mem_ctx
) ir_dereference_record(op0
->clone(mem_ctx
, NULL
),
993 e1
= new(mem_ctx
) ir_dereference_record(op1
->clone(mem_ctx
, NULL
),
995 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
998 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
1006 case GLSL_TYPE_ERROR
:
1007 case GLSL_TYPE_VOID
:
1008 case GLSL_TYPE_SAMPLER
:
1009 case GLSL_TYPE_IMAGE
:
1010 case GLSL_TYPE_INTERFACE
:
1011 case GLSL_TYPE_ATOMIC_UINT
:
1012 /* I assume a comparison of a struct containing a sampler just
1013 * ignores the sampler present in the type.
1019 cmp
= new(mem_ctx
) ir_constant(true);
1024 /* For logical operations, we want to ensure that the operands are
1025 * scalar booleans. If it isn't, emit an error and return a constant
1026 * boolean to avoid triggering cascading error messages.
1029 get_scalar_boolean_operand(exec_list
*instructions
,
1030 struct _mesa_glsl_parse_state
*state
,
1031 ast_expression
*parent_expr
,
1033 const char *operand_name
,
1034 bool *error_emitted
)
1036 ast_expression
*expr
= parent_expr
->subexpressions
[operand
];
1038 ir_rvalue
*val
= expr
->hir(instructions
, state
);
1040 if (val
->type
->is_boolean() && val
->type
->is_scalar())
1043 if (!*error_emitted
) {
1044 YYLTYPE loc
= expr
->get_location();
1045 _mesa_glsl_error(&loc
, state
, "%s of `%s' must be scalar boolean",
1047 parent_expr
->operator_string(parent_expr
->oper
));
1048 *error_emitted
= true;
1051 return new(ctx
) ir_constant(true);
1055 * If name refers to a builtin array whose maximum allowed size is less than
1056 * size, report an error and return true. Otherwise return false.
1059 check_builtin_array_max_size(const char *name
, unsigned size
,
1060 YYLTYPE loc
, struct _mesa_glsl_parse_state
*state
)
1062 if ((strcmp("gl_TexCoord", name
) == 0)
1063 && (size
> state
->Const
.MaxTextureCoords
)) {
1064 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1066 * "The size [of gl_TexCoord] can be at most
1067 * gl_MaxTextureCoords."
1069 _mesa_glsl_error(&loc
, state
, "`gl_TexCoord' array size cannot "
1070 "be larger than gl_MaxTextureCoords (%u)",
1071 state
->Const
.MaxTextureCoords
);
1072 } else if (strcmp("gl_ClipDistance", name
) == 0
1073 && size
> state
->Const
.MaxClipPlanes
) {
1074 /* From section 7.1 (Vertex Shader Special Variables) of the
1077 * "The gl_ClipDistance array is predeclared as unsized and
1078 * must be sized by the shader either redeclaring it with a
1079 * size or indexing it only with integral constant
1080 * expressions. ... The size can be at most
1081 * gl_MaxClipDistances."
1083 _mesa_glsl_error(&loc
, state
, "`gl_ClipDistance' array size cannot "
1084 "be larger than gl_MaxClipDistances (%u)",
1085 state
->Const
.MaxClipPlanes
);
1090 * Create the constant 1, of a which is appropriate for incrementing and
1091 * decrementing values of the given GLSL type. For example, if type is vec4,
1092 * this creates a constant value of 1.0 having type float.
1094 * If the given type is invalid for increment and decrement operators, return
1095 * a floating point 1--the error will be detected later.
1098 constant_one_for_inc_dec(void *ctx
, const glsl_type
*type
)
1100 switch (type
->base_type
) {
1101 case GLSL_TYPE_UINT
:
1102 return new(ctx
) ir_constant((unsigned) 1);
1104 return new(ctx
) ir_constant(1);
1106 case GLSL_TYPE_FLOAT
:
1107 return new(ctx
) ir_constant(1.0f
);
1112 ast_expression::hir(exec_list
*instructions
,
1113 struct _mesa_glsl_parse_state
*state
)
1115 return do_hir(instructions
, state
, true);
1119 ast_expression::hir_no_rvalue(exec_list
*instructions
,
1120 struct _mesa_glsl_parse_state
*state
)
1122 do_hir(instructions
, state
, false);
1126 ast_expression::do_hir(exec_list
*instructions
,
1127 struct _mesa_glsl_parse_state
*state
,
1131 static const int operations
[AST_NUM_OPERATORS
] = {
1132 -1, /* ast_assign doesn't convert to ir_expression. */
1133 -1, /* ast_plus doesn't convert to ir_expression. */
1147 ir_binop_any_nequal
,
1157 /* Note: The following block of expression types actually convert
1158 * to multiple IR instructions.
1160 ir_binop_mul
, /* ast_mul_assign */
1161 ir_binop_div
, /* ast_div_assign */
1162 ir_binop_mod
, /* ast_mod_assign */
1163 ir_binop_add
, /* ast_add_assign */
1164 ir_binop_sub
, /* ast_sub_assign */
1165 ir_binop_lshift
, /* ast_ls_assign */
1166 ir_binop_rshift
, /* ast_rs_assign */
1167 ir_binop_bit_and
, /* ast_and_assign */
1168 ir_binop_bit_xor
, /* ast_xor_assign */
1169 ir_binop_bit_or
, /* ast_or_assign */
1171 -1, /* ast_conditional doesn't convert to ir_expression. */
1172 ir_binop_add
, /* ast_pre_inc. */
1173 ir_binop_sub
, /* ast_pre_dec. */
1174 ir_binop_add
, /* ast_post_inc. */
1175 ir_binop_sub
, /* ast_post_dec. */
1176 -1, /* ast_field_selection doesn't conv to ir_expression. */
1177 -1, /* ast_array_index doesn't convert to ir_expression. */
1178 -1, /* ast_function_call doesn't conv to ir_expression. */
1179 -1, /* ast_identifier doesn't convert to ir_expression. */
1180 -1, /* ast_int_constant doesn't convert to ir_expression. */
1181 -1, /* ast_uint_constant doesn't conv to ir_expression. */
1182 -1, /* ast_float_constant doesn't conv to ir_expression. */
1183 -1, /* ast_bool_constant doesn't conv to ir_expression. */
1184 -1, /* ast_sequence doesn't convert to ir_expression. */
1186 ir_rvalue
*result
= NULL
;
1188 const struct glsl_type
*type
; /* a temporary variable for switch cases */
1189 bool error_emitted
= false;
1192 loc
= this->get_location();
1194 switch (this->oper
) {
1196 assert(!"ast_aggregate: Should never get here.");
1200 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1201 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1204 do_assignment(instructions
, state
,
1205 this->subexpressions
[0]->non_lvalue_description
,
1206 op
[0], op
[1], &result
, needs_rvalue
, false,
1207 this->subexpressions
[0]->get_location());
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();
1222 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1224 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1226 error_emitted
= type
->is_error();
1228 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1236 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1237 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1239 type
= arithmetic_result_type(op
[0], op
[1],
1240 (this->oper
== ast_mul
),
1242 error_emitted
= type
->is_error();
1244 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1249 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1250 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1252 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1254 assert(operations
[this->oper
] == ir_binop_mod
);
1256 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1258 error_emitted
= type
->is_error();
1263 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1264 error_emitted
= true;
1267 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1268 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1269 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1271 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1273 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1280 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1281 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1283 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
1285 /* The relational operators must either generate an error or result
1286 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1288 assert(type
->is_error()
1289 || ((type
->base_type
== GLSL_TYPE_BOOL
)
1290 && type
->is_scalar()));
1292 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1294 error_emitted
= type
->is_error();
1299 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1300 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1302 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1304 * "The equality operators equal (==), and not equal (!=)
1305 * operate on all types. They result in a scalar Boolean. If
1306 * the operand types do not match, then there must be a
1307 * conversion from Section 4.1.10 "Implicit Conversions"
1308 * applied to one operand that can make them match, in which
1309 * case this conversion is done."
1311 if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
1312 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
1313 || (op
[0]->type
!= op
[1]->type
)) {
1314 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
1315 "type", (this->oper
== ast_equal
) ? "==" : "!=");
1316 error_emitted
= true;
1317 } else if ((op
[0]->type
->is_array() || op
[1]->type
->is_array()) &&
1318 !state
->check_version(120, 300, &loc
,
1319 "array comparisons forbidden")) {
1320 error_emitted
= true;
1321 } else if ((op
[0]->type
->contains_opaque() ||
1322 op
[1]->type
->contains_opaque())) {
1323 _mesa_glsl_error(&loc
, state
, "opaque type comparisons forbidden");
1324 error_emitted
= true;
1327 if (error_emitted
) {
1328 result
= new(ctx
) ir_constant(false);
1330 result
= do_comparison(ctx
, operations
[this->oper
], op
[0], op
[1]);
1331 assert(result
->type
== glsl_type::bool_type
);
1338 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1339 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1340 type
= bit_logic_result_type(op
[0]->type
, op
[1]->type
, this->oper
,
1342 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1344 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1348 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1350 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1351 error_emitted
= true;
1354 if (!op
[0]->type
->is_integer()) {
1355 _mesa_glsl_error(&loc
, state
, "operand of `~' must be an integer");
1356 error_emitted
= true;
1359 type
= error_emitted
? glsl_type::error_type
: op
[0]->type
;
1360 result
= new(ctx
) ir_expression(ir_unop_bit_not
, type
, op
[0], NULL
);
1363 case ast_logic_and
: {
1364 exec_list rhs_instructions
;
1365 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1366 "LHS", &error_emitted
);
1367 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1368 "RHS", &error_emitted
);
1370 if (rhs_instructions
.is_empty()) {
1371 result
= new(ctx
) ir_expression(ir_binop_logic_and
, op
[0], op
[1]);
1372 type
= result
->type
;
1374 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1377 instructions
->push_tail(tmp
);
1379 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1380 instructions
->push_tail(stmt
);
1382 stmt
->then_instructions
.append_list(&rhs_instructions
);
1383 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1384 ir_assignment
*const then_assign
=
1385 new(ctx
) ir_assignment(then_deref
, op
[1]);
1386 stmt
->then_instructions
.push_tail(then_assign
);
1388 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1389 ir_assignment
*const else_assign
=
1390 new(ctx
) ir_assignment(else_deref
, new(ctx
) ir_constant(false));
1391 stmt
->else_instructions
.push_tail(else_assign
);
1393 result
= new(ctx
) ir_dereference_variable(tmp
);
1399 case ast_logic_or
: {
1400 exec_list rhs_instructions
;
1401 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1402 "LHS", &error_emitted
);
1403 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1404 "RHS", &error_emitted
);
1406 if (rhs_instructions
.is_empty()) {
1407 result
= new(ctx
) ir_expression(ir_binop_logic_or
, op
[0], op
[1]);
1408 type
= result
->type
;
1410 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1413 instructions
->push_tail(tmp
);
1415 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1416 instructions
->push_tail(stmt
);
1418 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1419 ir_assignment
*const then_assign
=
1420 new(ctx
) ir_assignment(then_deref
, new(ctx
) ir_constant(true));
1421 stmt
->then_instructions
.push_tail(then_assign
);
1423 stmt
->else_instructions
.append_list(&rhs_instructions
);
1424 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1425 ir_assignment
*const else_assign
=
1426 new(ctx
) ir_assignment(else_deref
, op
[1]);
1427 stmt
->else_instructions
.push_tail(else_assign
);
1429 result
= new(ctx
) ir_dereference_variable(tmp
);
1436 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1438 * "The logical binary operators and (&&), or ( | | ), and
1439 * exclusive or (^^). They operate only on two Boolean
1440 * expressions and result in a Boolean expression."
1442 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0, "LHS",
1444 op
[1] = get_scalar_boolean_operand(instructions
, state
, this, 1, "RHS",
1447 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1452 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1453 "operand", &error_emitted
);
1455 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1459 case ast_mul_assign
:
1460 case ast_div_assign
:
1461 case ast_add_assign
:
1462 case ast_sub_assign
: {
1463 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1464 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1466 type
= arithmetic_result_type(op
[0], op
[1],
1467 (this->oper
== ast_mul_assign
),
1470 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1474 do_assignment(instructions
, state
,
1475 this->subexpressions
[0]->non_lvalue_description
,
1476 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1477 &result
, needs_rvalue
, false,
1478 this->subexpressions
[0]->get_location());
1480 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1481 * explicitly test for this because none of the binary expression
1482 * operators allow array operands either.
1488 case ast_mod_assign
: {
1489 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1490 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1492 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1494 assert(operations
[this->oper
] == ir_binop_mod
);
1496 ir_rvalue
*temp_rhs
;
1497 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1501 do_assignment(instructions
, state
,
1502 this->subexpressions
[0]->non_lvalue_description
,
1503 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1504 &result
, needs_rvalue
, false,
1505 this->subexpressions
[0]->get_location());
1510 case ast_rs_assign
: {
1511 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1512 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1513 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1515 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1516 type
, op
[0], op
[1]);
1518 do_assignment(instructions
, state
,
1519 this->subexpressions
[0]->non_lvalue_description
,
1520 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1521 &result
, needs_rvalue
, false,
1522 this->subexpressions
[0]->get_location());
1526 case ast_and_assign
:
1527 case ast_xor_assign
:
1528 case ast_or_assign
: {
1529 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1530 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1531 type
= bit_logic_result_type(op
[0]->type
, op
[1]->type
, this->oper
,
1533 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1534 type
, op
[0], op
[1]);
1536 do_assignment(instructions
, state
,
1537 this->subexpressions
[0]->non_lvalue_description
,
1538 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1539 &result
, needs_rvalue
, false,
1540 this->subexpressions
[0]->get_location());
1544 case ast_conditional
: {
1545 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1547 * "The ternary selection operator (?:). It operates on three
1548 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1549 * first expression, which must result in a scalar Boolean."
1551 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1552 "condition", &error_emitted
);
1554 /* The :? operator is implemented by generating an anonymous temporary
1555 * followed by an if-statement. The last instruction in each branch of
1556 * the if-statement assigns a value to the anonymous temporary. This
1557 * temporary is the r-value of the expression.
1559 exec_list then_instructions
;
1560 exec_list else_instructions
;
1562 op
[1] = this->subexpressions
[1]->hir(&then_instructions
, state
);
1563 op
[2] = this->subexpressions
[2]->hir(&else_instructions
, state
);
1565 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1567 * "The second and third expressions can be any type, as
1568 * long their types match, or there is a conversion in
1569 * Section 4.1.10 "Implicit Conversions" that can be applied
1570 * to one of the expressions to make their types match. This
1571 * resulting matching type is the type of the entire
1574 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1575 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1576 || (op
[1]->type
!= op
[2]->type
)) {
1577 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1579 _mesa_glsl_error(& loc
, state
, "second and third operands of ?: "
1580 "operator must have matching types");
1581 error_emitted
= true;
1582 type
= glsl_type::error_type
;
1587 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1589 * "The second and third expressions must be the same type, but can
1590 * be of any type other than an array."
1592 if (type
->is_array() &&
1593 !state
->check_version(120, 300, &loc
,
1594 "second and third operands of ?: operator "
1595 "cannot be arrays")) {
1596 error_emitted
= true;
1599 ir_constant
*cond_val
= op
[0]->constant_expression_value();
1600 ir_constant
*then_val
= op
[1]->constant_expression_value();
1601 ir_constant
*else_val
= op
[2]->constant_expression_value();
1603 if (then_instructions
.is_empty()
1604 && else_instructions
.is_empty()
1605 && (cond_val
!= NULL
) && (then_val
!= NULL
) && (else_val
!= NULL
)) {
1606 result
= (cond_val
->value
.b
[0]) ? then_val
: else_val
;
1608 ir_variable
*const tmp
=
1609 new(ctx
) ir_variable(type
, "conditional_tmp", ir_var_temporary
);
1610 instructions
->push_tail(tmp
);
1612 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1613 instructions
->push_tail(stmt
);
1615 then_instructions
.move_nodes_to(& stmt
->then_instructions
);
1616 ir_dereference
*const then_deref
=
1617 new(ctx
) ir_dereference_variable(tmp
);
1618 ir_assignment
*const then_assign
=
1619 new(ctx
) ir_assignment(then_deref
, op
[1]);
1620 stmt
->then_instructions
.push_tail(then_assign
);
1622 else_instructions
.move_nodes_to(& stmt
->else_instructions
);
1623 ir_dereference
*const else_deref
=
1624 new(ctx
) ir_dereference_variable(tmp
);
1625 ir_assignment
*const else_assign
=
1626 new(ctx
) ir_assignment(else_deref
, op
[2]);
1627 stmt
->else_instructions
.push_tail(else_assign
);
1629 result
= new(ctx
) ir_dereference_variable(tmp
);
1636 this->non_lvalue_description
= (this->oper
== ast_pre_inc
)
1637 ? "pre-increment operation" : "pre-decrement operation";
1639 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1640 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1642 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1644 ir_rvalue
*temp_rhs
;
1645 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1649 do_assignment(instructions
, state
,
1650 this->subexpressions
[0]->non_lvalue_description
,
1651 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1652 &result
, needs_rvalue
, false,
1653 this->subexpressions
[0]->get_location());
1658 case ast_post_dec
: {
1659 this->non_lvalue_description
= (this->oper
== ast_post_inc
)
1660 ? "post-increment operation" : "post-decrement operation";
1661 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1662 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1664 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1666 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1668 ir_rvalue
*temp_rhs
;
1669 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1672 /* Get a temporary of a copy of the lvalue before it's modified.
1673 * This may get thrown away later.
1675 result
= get_lvalue_copy(instructions
, op
[0]->clone(ctx
, NULL
));
1677 ir_rvalue
*junk_rvalue
;
1679 do_assignment(instructions
, state
,
1680 this->subexpressions
[0]->non_lvalue_description
,
1681 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1682 &junk_rvalue
, false, false,
1683 this->subexpressions
[0]->get_location());
1688 case ast_field_selection
:
1689 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1692 case ast_array_index
: {
1693 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1695 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1696 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1698 result
= _mesa_ast_array_index_to_hir(ctx
, state
, op
[0], op
[1],
1701 if (result
->type
->is_error())
1702 error_emitted
= true;
1707 case ast_function_call
:
1708 /* Should *NEVER* get here. ast_function_call should always be handled
1709 * by ast_function_expression::hir.
1714 case ast_identifier
: {
1715 /* ast_identifier can appear several places in a full abstract syntax
1716 * tree. This particular use must be at location specified in the grammar
1717 * as 'variable_identifier'.
1720 state
->symbols
->get_variable(this->primary_expression
.identifier
);
1723 var
->data
.used
= true;
1724 result
= new(ctx
) ir_dereference_variable(var
);
1726 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
1727 this->primary_expression
.identifier
);
1729 result
= ir_rvalue::error_value(ctx
);
1730 error_emitted
= true;
1735 case ast_int_constant
:
1736 result
= new(ctx
) ir_constant(this->primary_expression
.int_constant
);
1739 case ast_uint_constant
:
1740 result
= new(ctx
) ir_constant(this->primary_expression
.uint_constant
);
1743 case ast_float_constant
:
1744 result
= new(ctx
) ir_constant(this->primary_expression
.float_constant
);
1747 case ast_bool_constant
:
1748 result
= new(ctx
) ir_constant(bool(this->primary_expression
.bool_constant
));
1751 case ast_sequence
: {
1752 /* It should not be possible to generate a sequence in the AST without
1753 * any expressions in it.
1755 assert(!this->expressions
.is_empty());
1757 /* The r-value of a sequence is the last expression in the sequence. If
1758 * the other expressions in the sequence do not have side-effects (and
1759 * therefore add instructions to the instruction list), they get dropped
1762 exec_node
*previous_tail_pred
= NULL
;
1763 YYLTYPE previous_operand_loc
= loc
;
1765 foreach_list_typed (ast_node
, ast
, link
, &this->expressions
) {
1766 /* If one of the operands of comma operator does not generate any
1767 * code, we want to emit a warning. At each pass through the loop
1768 * previous_tail_pred will point to the last instruction in the
1769 * stream *before* processing the previous operand. Naturally,
1770 * instructions->tail_pred will point to the last instruction in the
1771 * stream *after* processing the previous operand. If the two
1772 * pointers match, then the previous operand had no effect.
1774 * The warning behavior here differs slightly from GCC. GCC will
1775 * only emit a warning if none of the left-hand operands have an
1776 * effect. However, it will emit a warning for each. I believe that
1777 * there are some cases in C (especially with GCC extensions) where
1778 * it is useful to have an intermediate step in a sequence have no
1779 * effect, but I don't think these cases exist in GLSL. Either way,
1780 * it would be a giant hassle to replicate that behavior.
1782 if (previous_tail_pred
== instructions
->tail_pred
) {
1783 _mesa_glsl_warning(&previous_operand_loc
, state
,
1784 "left-hand operand of comma expression has "
1788 /* tail_pred is directly accessed instead of using the get_tail()
1789 * method for performance reasons. get_tail() has extra code to
1790 * return NULL when the list is empty. We don't care about that
1791 * here, so using tail_pred directly is fine.
1793 previous_tail_pred
= instructions
->tail_pred
;
1794 previous_operand_loc
= ast
->get_location();
1796 result
= ast
->hir(instructions
, state
);
1799 /* Any errors should have already been emitted in the loop above.
1801 error_emitted
= true;
1805 type
= NULL
; /* use result->type, not type. */
1806 assert(result
!= NULL
|| !needs_rvalue
);
1808 if (result
&& result
->type
->is_error() && !error_emitted
)
1809 _mesa_glsl_error(& loc
, state
, "type mismatch");
1816 ast_expression_statement::hir(exec_list
*instructions
,
1817 struct _mesa_glsl_parse_state
*state
)
1819 /* It is possible to have expression statements that don't have an
1820 * expression. This is the solitary semicolon:
1822 * for (i = 0; i < 5; i++)
1825 * In this case the expression will be NULL. Test for NULL and don't do
1826 * anything in that case.
1828 if (expression
!= NULL
)
1829 expression
->hir_no_rvalue(instructions
, state
);
1831 /* Statements do not have r-values.
1838 ast_compound_statement::hir(exec_list
*instructions
,
1839 struct _mesa_glsl_parse_state
*state
)
1842 state
->symbols
->push_scope();
1844 foreach_list_typed (ast_node
, ast
, link
, &this->statements
)
1845 ast
->hir(instructions
, state
);
1848 state
->symbols
->pop_scope();
1850 /* Compound statements do not have r-values.
1856 * Evaluate the given exec_node (which should be an ast_node representing
1857 * a single array dimension) and return its integer value.
1860 process_array_size(exec_node
*node
,
1861 struct _mesa_glsl_parse_state
*state
)
1863 exec_list dummy_instructions
;
1865 ast_node
*array_size
= exec_node_data(ast_node
, node
, link
);
1866 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
1867 YYLTYPE loc
= array_size
->get_location();
1870 _mesa_glsl_error(& loc
, state
,
1871 "array size could not be resolved");
1875 if (!ir
->type
->is_integer()) {
1876 _mesa_glsl_error(& loc
, state
,
1877 "array size must be integer type");
1881 if (!ir
->type
->is_scalar()) {
1882 _mesa_glsl_error(& loc
, state
,
1883 "array size must be scalar type");
1887 ir_constant
*const size
= ir
->constant_expression_value();
1889 _mesa_glsl_error(& loc
, state
, "array size must be a "
1890 "constant valued expression");
1894 if (size
->value
.i
[0] <= 0) {
1895 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
1899 assert(size
->type
== ir
->type
);
1901 /* If the array size is const (and we've verified that
1902 * it is) then no instructions should have been emitted
1903 * when we converted it to HIR. If they were emitted,
1904 * then either the array size isn't const after all, or
1905 * we are emitting unnecessary instructions.
1907 assert(dummy_instructions
.is_empty());
1909 return size
->value
.u
[0];
1912 static const glsl_type
*
1913 process_array_type(YYLTYPE
*loc
, const glsl_type
*base
,
1914 ast_array_specifier
*array_specifier
,
1915 struct _mesa_glsl_parse_state
*state
)
1917 const glsl_type
*array_type
= base
;
1919 if (array_specifier
!= NULL
) {
1920 if (base
->is_array()) {
1922 /* From page 19 (page 25) of the GLSL 1.20 spec:
1924 * "Only one-dimensional arrays may be declared."
1926 if (!state
->ARB_arrays_of_arrays_enable
) {
1927 _mesa_glsl_error(loc
, state
,
1928 "invalid array of `%s'"
1929 "GL_ARB_arrays_of_arrays "
1930 "required for defining arrays of arrays",
1932 return glsl_type::error_type
;
1935 if (base
->length
== 0) {
1936 _mesa_glsl_error(loc
, state
,
1937 "only the outermost array dimension can "
1940 return glsl_type::error_type
;
1944 for (exec_node
*node
= array_specifier
->array_dimensions
.tail_pred
;
1945 !node
->is_head_sentinel(); node
= node
->prev
) {
1946 unsigned array_size
= process_array_size(node
, state
);
1947 array_type
= glsl_type::get_array_instance(array_type
, array_size
);
1950 if (array_specifier
->is_unsized_array
)
1951 array_type
= glsl_type::get_array_instance(array_type
, 0);
1959 ast_type_specifier::glsl_type(const char **name
,
1960 struct _mesa_glsl_parse_state
*state
) const
1962 const struct glsl_type
*type
;
1964 type
= state
->symbols
->get_type(this->type_name
);
1965 *name
= this->type_name
;
1967 YYLTYPE loc
= this->get_location();
1968 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
1974 ast_fully_specified_type::glsl_type(const char **name
,
1975 struct _mesa_glsl_parse_state
*state
) const
1977 const struct glsl_type
*type
= this->specifier
->glsl_type(name
, state
);
1982 if (type
->base_type
== GLSL_TYPE_FLOAT
1984 && state
->stage
== MESA_SHADER_FRAGMENT
1985 && this->qualifier
.precision
== ast_precision_none
1986 && state
->symbols
->get_variable("#default precision") == NULL
) {
1987 YYLTYPE loc
= this->get_location();
1988 _mesa_glsl_error(&loc
, state
,
1989 "no precision specified this scope for type `%s'",
1997 * Determine whether a toplevel variable declaration declares a varying. This
1998 * function operates by examining the variable's mode and the shader target,
1999 * so it correctly identifies linkage variables regardless of whether they are
2000 * declared using the deprecated "varying" syntax or the new "in/out" syntax.
2002 * Passing a non-toplevel variable declaration (e.g. a function parameter) to
2003 * this function will produce undefined results.
2006 is_varying_var(ir_variable
*var
, gl_shader_stage target
)
2009 case MESA_SHADER_VERTEX
:
2010 return var
->data
.mode
== ir_var_shader_out
;
2011 case MESA_SHADER_FRAGMENT
:
2012 return var
->data
.mode
== ir_var_shader_in
;
2014 return var
->data
.mode
== ir_var_shader_out
|| var
->data
.mode
== ir_var_shader_in
;
2020 * Matrix layout qualifiers are only allowed on certain types
2023 validate_matrix_layout_for_type(struct _mesa_glsl_parse_state
*state
,
2025 const glsl_type
*type
,
2028 if (var
&& !var
->is_in_uniform_block()) {
2029 /* Layout qualifiers may only apply to interface blocks and fields in
2032 _mesa_glsl_error(loc
, state
,
2033 "uniform block layout qualifiers row_major and "
2034 "column_major may not be applied to variables "
2035 "outside of uniform blocks");
2036 } else if (!type
->is_matrix()) {
2037 /* The OpenGL ES 3.0 conformance tests did not originally allow
2038 * matrix layout qualifiers on non-matrices. However, the OpenGL
2039 * 4.4 and OpenGL ES 3.0 (revision TBD) specifications were
2040 * amended to specifically allow these layouts on all types. Emit
2041 * a warning so that people know their code may not be portable.
2043 _mesa_glsl_warning(loc
, state
,
2044 "uniform block layout qualifiers row_major and "
2045 "column_major applied to non-matrix types may "
2046 "be rejected by older compilers");
2047 } else if (type
->is_record()) {
2048 /* We allow 'layout(row_major)' on structure types because it's the only
2049 * way to get row-major layouts on matrices contained in structures.
2051 _mesa_glsl_warning(loc
, state
,
2052 "uniform block layout qualifiers row_major and "
2053 "column_major applied to structure types is not "
2054 "strictly conformant and may be rejected by other "
2060 validate_binding_qualifier(struct _mesa_glsl_parse_state
*state
,
2063 const ast_type_qualifier
*qual
)
2065 if (var
->data
.mode
!= ir_var_uniform
) {
2066 _mesa_glsl_error(loc
, state
,
2067 "the \"binding\" qualifier only applies to uniforms");
2071 if (qual
->binding
< 0) {
2072 _mesa_glsl_error(loc
, state
, "binding values must be >= 0");
2076 const struct gl_context
*const ctx
= state
->ctx
;
2077 unsigned elements
= var
->type
->is_array() ? var
->type
->length
: 1;
2078 unsigned max_index
= qual
->binding
+ elements
- 1;
2080 if (var
->type
->is_interface()) {
2081 /* UBOs. From page 60 of the GLSL 4.20 specification:
2082 * "If the binding point for any uniform block instance is less than zero,
2083 * or greater than or equal to the implementation-dependent maximum
2084 * number of uniform buffer bindings, a compilation error will occur.
2085 * When the binding identifier is used with a uniform block instanced as
2086 * an array of size N, all elements of the array from binding through
2087 * binding + N – 1 must be within this range."
2089 * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
2091 if (max_index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2092 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d UBOs exceeds "
2093 "the maximum number of UBO binding points (%d)",
2094 qual
->binding
, elements
,
2095 ctx
->Const
.MaxUniformBufferBindings
);
2098 } else if (var
->type
->is_sampler() ||
2099 (var
->type
->is_array() && var
->type
->fields
.array
->is_sampler())) {
2100 /* Samplers. From page 63 of the GLSL 4.20 specification:
2101 * "If the binding is less than zero, or greater than or equal to the
2102 * implementation-dependent maximum supported number of units, a
2103 * compilation error will occur. When the binding identifier is used
2104 * with an array of size N, all elements of the array from binding
2105 * through binding + N - 1 must be within this range."
2107 unsigned limit
= ctx
->Const
.Program
[state
->stage
].MaxTextureImageUnits
;
2109 if (max_index
>= limit
) {
2110 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d samplers "
2111 "exceeds the maximum number of texture image units "
2112 "(%d)", qual
->binding
, elements
, limit
);
2116 } else if (var
->type
->contains_atomic()) {
2117 assert(ctx
->Const
.MaxAtomicBufferBindings
<= MAX_COMBINED_ATOMIC_BUFFERS
);
2118 if (unsigned(qual
->binding
) >= ctx
->Const
.MaxAtomicBufferBindings
) {
2119 _mesa_glsl_error(loc
, state
, "layout(binding = %d) exceeds the "
2120 " maximum number of atomic counter buffer bindings"
2121 "(%d)", qual
->binding
,
2122 ctx
->Const
.MaxAtomicBufferBindings
);
2127 _mesa_glsl_error(loc
, state
,
2128 "the \"binding\" qualifier only applies to uniform "
2129 "blocks, samplers, atomic counters, or arrays thereof");
2137 static glsl_interp_qualifier
2138 interpret_interpolation_qualifier(const struct ast_type_qualifier
*qual
,
2139 ir_variable_mode mode
,
2140 struct _mesa_glsl_parse_state
*state
,
2143 glsl_interp_qualifier interpolation
;
2144 if (qual
->flags
.q
.flat
)
2145 interpolation
= INTERP_QUALIFIER_FLAT
;
2146 else if (qual
->flags
.q
.noperspective
)
2147 interpolation
= INTERP_QUALIFIER_NOPERSPECTIVE
;
2148 else if (qual
->flags
.q
.smooth
)
2149 interpolation
= INTERP_QUALIFIER_SMOOTH
;
2151 interpolation
= INTERP_QUALIFIER_NONE
;
2153 if (interpolation
!= INTERP_QUALIFIER_NONE
) {
2154 if (mode
!= ir_var_shader_in
&& mode
!= ir_var_shader_out
) {
2155 _mesa_glsl_error(loc
, state
,
2156 "interpolation qualifier `%s' can only be applied to "
2157 "shader inputs or outputs.",
2158 interpolation_string(interpolation
));
2162 if ((state
->stage
== MESA_SHADER_VERTEX
&& mode
== ir_var_shader_in
) ||
2163 (state
->stage
== MESA_SHADER_FRAGMENT
&& mode
== ir_var_shader_out
)) {
2164 _mesa_glsl_error(loc
, state
,
2165 "interpolation qualifier `%s' cannot be applied to "
2166 "vertex shader inputs or fragment shader outputs",
2167 interpolation_string(interpolation
));
2171 return interpolation
;
2176 validate_explicit_location(const struct ast_type_qualifier
*qual
,
2178 struct _mesa_glsl_parse_state
*state
,
2183 /* Checks for GL_ARB_explicit_uniform_location. */
2184 if (qual
->flags
.q
.uniform
) {
2185 if (!state
->check_explicit_uniform_location_allowed(loc
, var
))
2188 const struct gl_context
*const ctx
= state
->ctx
;
2189 unsigned max_loc
= qual
->location
+ var
->type
->uniform_locations() - 1;
2191 /* ARB_explicit_uniform_location specification states:
2193 * "The explicitly defined locations and the generated locations
2194 * must be in the range of 0 to MAX_UNIFORM_LOCATIONS minus one."
2196 * "Valid locations for default-block uniform variable locations
2197 * are in the range of 0 to the implementation-defined maximum
2198 * number of uniform locations."
2200 if (qual
->location
< 0) {
2201 _mesa_glsl_error(loc
, state
,
2202 "explicit location < 0 for uniform %s", var
->name
);
2206 if (max_loc
>= ctx
->Const
.MaxUserAssignableUniformLocations
) {
2207 _mesa_glsl_error(loc
, state
, "location(s) consumed by uniform %s "
2208 ">= MAX_UNIFORM_LOCATIONS (%u)", var
->name
,
2209 ctx
->Const
.MaxUserAssignableUniformLocations
);
2213 var
->data
.explicit_location
= true;
2214 var
->data
.location
= qual
->location
;
2218 /* Between GL_ARB_explicit_attrib_location an
2219 * GL_ARB_separate_shader_objects, the inputs and outputs of any shader
2220 * stage can be assigned explicit locations. The checking here associates
2221 * the correct extension with the correct stage's input / output:
2225 * vertex explicit_loc sso
2227 * fragment sso explicit_loc
2229 switch (state
->stage
) {
2230 case MESA_SHADER_VERTEX
:
2231 if (var
->data
.mode
== ir_var_shader_in
) {
2232 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
2238 if (var
->data
.mode
== ir_var_shader_out
) {
2239 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
2248 case MESA_SHADER_GEOMETRY
:
2249 if (var
->data
.mode
== ir_var_shader_in
|| var
->data
.mode
== ir_var_shader_out
) {
2250 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
2259 case MESA_SHADER_FRAGMENT
:
2260 if (var
->data
.mode
== ir_var_shader_in
) {
2261 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
2267 if (var
->data
.mode
== ir_var_shader_out
) {
2268 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
2277 case MESA_SHADER_COMPUTE
:
2278 _mesa_glsl_error(loc
, state
,
2279 "compute shader variables cannot be given "
2280 "explicit locations");
2285 _mesa_glsl_error(loc
, state
,
2286 "%s cannot be given an explicit location in %s shader",
2288 _mesa_shader_stage_to_string(state
->stage
));
2290 var
->data
.explicit_location
= true;
2292 /* This bit of silliness is needed because invalid explicit locations
2293 * are supposed to be flagged during linking. Small negative values
2294 * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
2295 * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
2296 * The linker needs to be able to differentiate these cases. This
2297 * ensures that negative values stay negative.
2299 if (qual
->location
>= 0) {
2300 switch (state
->stage
) {
2301 case MESA_SHADER_VERTEX
:
2302 var
->data
.location
= (var
->data
.mode
== ir_var_shader_in
)
2303 ? (qual
->location
+ VERT_ATTRIB_GENERIC0
)
2304 : (qual
->location
+ VARYING_SLOT_VAR0
);
2307 case MESA_SHADER_GEOMETRY
:
2308 var
->data
.location
= qual
->location
+ VARYING_SLOT_VAR0
;
2311 case MESA_SHADER_FRAGMENT
:
2312 var
->data
.location
= (var
->data
.mode
== ir_var_shader_out
)
2313 ? (qual
->location
+ FRAG_RESULT_DATA0
)
2314 : (qual
->location
+ VARYING_SLOT_VAR0
);
2316 case MESA_SHADER_COMPUTE
:
2317 assert(!"Unexpected shader type");
2321 var
->data
.location
= qual
->location
;
2324 if (qual
->flags
.q
.explicit_index
) {
2325 /* From the GLSL 4.30 specification, section 4.4.2 (Output
2326 * Layout Qualifiers):
2328 * "It is also a compile-time error if a fragment shader
2329 * sets a layout index to less than 0 or greater than 1."
2331 * Older specifications don't mandate a behavior; we take
2332 * this as a clarification and always generate the error.
2334 if (qual
->index
< 0 || qual
->index
> 1) {
2335 _mesa_glsl_error(loc
, state
,
2336 "explicit index may only be 0 or 1");
2338 var
->data
.explicit_index
= true;
2339 var
->data
.index
= qual
->index
;
2346 apply_image_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
2348 struct _mesa_glsl_parse_state
*state
,
2351 const glsl_type
*base_type
=
2352 (var
->type
->is_array() ? var
->type
->element_type() : var
->type
);
2354 if (base_type
->is_image()) {
2355 if (var
->data
.mode
!= ir_var_uniform
&&
2356 var
->data
.mode
!= ir_var_function_in
) {
2357 _mesa_glsl_error(loc
, state
, "image variables may only be declared as "
2358 "function parameters or uniform-qualified "
2359 "global variables");
2362 var
->data
.image_read_only
|= qual
->flags
.q
.read_only
;
2363 var
->data
.image_write_only
|= qual
->flags
.q
.write_only
;
2364 var
->data
.image_coherent
|= qual
->flags
.q
.coherent
;
2365 var
->data
.image_volatile
|= qual
->flags
.q
._volatile
;
2366 var
->data
.image_restrict
|= qual
->flags
.q
.restrict_flag
;
2367 var
->data
.read_only
= true;
2369 if (qual
->flags
.q
.explicit_image_format
) {
2370 if (var
->data
.mode
== ir_var_function_in
) {
2371 _mesa_glsl_error(loc
, state
, "format qualifiers cannot be "
2372 "used on image function parameters");
2375 if (qual
->image_base_type
!= base_type
->sampler_type
) {
2376 _mesa_glsl_error(loc
, state
, "format qualifier doesn't match the "
2377 "base data type of the image");
2380 var
->data
.image_format
= qual
->image_format
;
2382 if (var
->data
.mode
== ir_var_uniform
&& !qual
->flags
.q
.write_only
) {
2383 _mesa_glsl_error(loc
, state
, "uniforms not qualified with "
2384 "`writeonly' must have a format layout "
2388 var
->data
.image_format
= GL_NONE
;
2393 static inline const char*
2394 get_layout_qualifier_string(bool origin_upper_left
, bool pixel_center_integer
)
2396 if (origin_upper_left
&& pixel_center_integer
)
2397 return "origin_upper_left, pixel_center_integer";
2398 else if (origin_upper_left
)
2399 return "origin_upper_left";
2400 else if (pixel_center_integer
)
2401 return "pixel_center_integer";
2407 is_conflicting_fragcoord_redeclaration(struct _mesa_glsl_parse_state
*state
,
2408 const struct ast_type_qualifier
*qual
)
2410 /* If gl_FragCoord was previously declared, and the qualifiers were
2411 * different in any way, return true.
2413 if (state
->fs_redeclares_gl_fragcoord
) {
2414 return (state
->fs_pixel_center_integer
!= qual
->flags
.q
.pixel_center_integer
2415 || state
->fs_origin_upper_left
!= qual
->flags
.q
.origin_upper_left
);
2422 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
2424 struct _mesa_glsl_parse_state
*state
,
2428 STATIC_ASSERT(sizeof(qual
->flags
.q
) <= sizeof(qual
->flags
.i
));
2430 if (qual
->flags
.q
.invariant
) {
2431 if (var
->data
.used
) {
2432 _mesa_glsl_error(loc
, state
,
2433 "variable `%s' may not be redeclared "
2434 "`invariant' after being used",
2437 var
->data
.invariant
= 1;
2441 if (qual
->flags
.q
.precise
) {
2442 if (var
->data
.used
) {
2443 _mesa_glsl_error(loc
, state
,
2444 "variable `%s' may not be redeclared "
2445 "`precise' after being used",
2448 var
->data
.precise
= 1;
2452 if (qual
->flags
.q
.constant
|| qual
->flags
.q
.attribute
2453 || qual
->flags
.q
.uniform
2454 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
2455 var
->data
.read_only
= 1;
2457 if (qual
->flags
.q
.centroid
)
2458 var
->data
.centroid
= 1;
2460 if (qual
->flags
.q
.sample
)
2461 var
->data
.sample
= 1;
2463 if (state
->stage
== MESA_SHADER_GEOMETRY
&&
2464 qual
->flags
.q
.out
&& qual
->flags
.q
.stream
) {
2465 var
->data
.stream
= qual
->stream
;
2468 if (qual
->flags
.q
.attribute
&& state
->stage
!= MESA_SHADER_VERTEX
) {
2469 var
->type
= glsl_type::error_type
;
2470 _mesa_glsl_error(loc
, state
,
2471 "`attribute' variables may not be declared in the "
2473 _mesa_shader_stage_to_string(state
->stage
));
2476 /* Disallow layout qualifiers which may only appear on layout declarations. */
2477 if (qual
->flags
.q
.prim_type
) {
2478 _mesa_glsl_error(loc
, state
,
2479 "Primitive type may only be specified on GS input or output "
2480 "layout declaration, not on variables.");
2483 /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
2485 * "However, the const qualifier cannot be used with out or inout."
2487 * The same section of the GLSL 4.40 spec further clarifies this saying:
2489 * "The const qualifier cannot be used with out or inout, or a
2490 * compile-time error results."
2492 if (is_parameter
&& qual
->flags
.q
.constant
&& qual
->flags
.q
.out
) {
2493 _mesa_glsl_error(loc
, state
,
2494 "`const' may not be applied to `out' or `inout' "
2495 "function parameters");
2498 /* If there is no qualifier that changes the mode of the variable, leave
2499 * the setting alone.
2501 assert(var
->data
.mode
!= ir_var_temporary
);
2502 if (qual
->flags
.q
.in
&& qual
->flags
.q
.out
)
2503 var
->data
.mode
= ir_var_function_inout
;
2504 else if (qual
->flags
.q
.in
)
2505 var
->data
.mode
= is_parameter
? ir_var_function_in
: ir_var_shader_in
;
2506 else if (qual
->flags
.q
.attribute
2507 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
2508 var
->data
.mode
= ir_var_shader_in
;
2509 else if (qual
->flags
.q
.out
)
2510 var
->data
.mode
= is_parameter
? ir_var_function_out
: ir_var_shader_out
;
2511 else if (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_VERTEX
))
2512 var
->data
.mode
= ir_var_shader_out
;
2513 else if (qual
->flags
.q
.uniform
)
2514 var
->data
.mode
= ir_var_uniform
;
2516 if (!is_parameter
&& is_varying_var(var
, state
->stage
)) {
2517 /* User-defined ins/outs are not permitted in compute shaders. */
2518 if (state
->stage
== MESA_SHADER_COMPUTE
) {
2519 _mesa_glsl_error(loc
, state
,
2520 "user-defined input and output variables are not "
2521 "permitted in compute shaders");
2524 /* This variable is being used to link data between shader stages (in
2525 * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
2526 * that is allowed for such purposes.
2528 * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
2530 * "The varying qualifier can be used only with the data types
2531 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
2534 * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
2535 * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
2537 * "Fragment inputs can only be signed and unsigned integers and
2538 * integer vectors, float, floating-point vectors, matrices, or
2539 * arrays of these. Structures cannot be input.
2541 * Similar text exists in the section on vertex shader outputs.
2543 * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
2544 * 3.00 spec allows structs as well. Varying structs are also allowed
2547 switch (var
->type
->get_scalar_type()->base_type
) {
2548 case GLSL_TYPE_FLOAT
:
2549 /* Ok in all GLSL versions */
2551 case GLSL_TYPE_UINT
:
2553 if (state
->is_version(130, 300))
2555 _mesa_glsl_error(loc
, state
,
2556 "varying variables must be of base type float in %s",
2557 state
->get_version_string());
2559 case GLSL_TYPE_STRUCT
:
2560 if (state
->is_version(150, 300))
2562 _mesa_glsl_error(loc
, state
,
2563 "varying variables may not be of type struct");
2566 _mesa_glsl_error(loc
, state
, "illegal type for a varying variable");
2571 if (state
->all_invariant
&& (state
->current_function
== NULL
)) {
2572 switch (state
->stage
) {
2573 case MESA_SHADER_VERTEX
:
2574 if (var
->data
.mode
== ir_var_shader_out
)
2575 var
->data
.invariant
= true;
2577 case MESA_SHADER_GEOMETRY
:
2578 if ((var
->data
.mode
== ir_var_shader_in
)
2579 || (var
->data
.mode
== ir_var_shader_out
))
2580 var
->data
.invariant
= true;
2582 case MESA_SHADER_FRAGMENT
:
2583 if (var
->data
.mode
== ir_var_shader_in
)
2584 var
->data
.invariant
= true;
2586 case MESA_SHADER_COMPUTE
:
2587 /* Invariance isn't meaningful in compute shaders. */
2592 var
->data
.interpolation
=
2593 interpret_interpolation_qualifier(qual
, (ir_variable_mode
) var
->data
.mode
,
2596 var
->data
.pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
2597 var
->data
.origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
2598 if ((qual
->flags
.q
.origin_upper_left
|| qual
->flags
.q
.pixel_center_integer
)
2599 && (strcmp(var
->name
, "gl_FragCoord") != 0)) {
2600 const char *const qual_string
= (qual
->flags
.q
.origin_upper_left
)
2601 ? "origin_upper_left" : "pixel_center_integer";
2603 _mesa_glsl_error(loc
, state
,
2604 "layout qualifier `%s' can only be applied to "
2605 "fragment shader input `gl_FragCoord'",
2609 if (var
->name
!= NULL
&& strcmp(var
->name
, "gl_FragCoord") == 0) {
2611 /* Section 4.3.8.1, page 39 of GLSL 1.50 spec says:
2613 * "Within any shader, the first redeclarations of gl_FragCoord
2614 * must appear before any use of gl_FragCoord."
2616 * Generate a compiler error if above condition is not met by the
2619 ir_variable
*earlier
= state
->symbols
->get_variable("gl_FragCoord");
2620 if (earlier
!= NULL
&&
2621 earlier
->data
.used
&&
2622 !state
->fs_redeclares_gl_fragcoord
) {
2623 _mesa_glsl_error(loc
, state
,
2624 "gl_FragCoord used before its first redeclaration "
2625 "in fragment shader");
2628 /* Make sure all gl_FragCoord redeclarations specify the same layout
2631 if (is_conflicting_fragcoord_redeclaration(state
, qual
)) {
2632 const char *const qual_string
=
2633 get_layout_qualifier_string(qual
->flags
.q
.origin_upper_left
,
2634 qual
->flags
.q
.pixel_center_integer
);
2636 const char *const state_string
=
2637 get_layout_qualifier_string(state
->fs_origin_upper_left
,
2638 state
->fs_pixel_center_integer
);
2640 _mesa_glsl_error(loc
, state
,
2641 "gl_FragCoord redeclared with different layout "
2642 "qualifiers (%s) and (%s) ",
2646 state
->fs_origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
2647 state
->fs_pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
2648 state
->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers
=
2649 !qual
->flags
.q
.origin_upper_left
&& !qual
->flags
.q
.pixel_center_integer
;
2650 state
->fs_redeclares_gl_fragcoord
=
2651 state
->fs_origin_upper_left
||
2652 state
->fs_pixel_center_integer
||
2653 state
->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers
;
2656 if (qual
->flags
.q
.explicit_location
) {
2657 validate_explicit_location(qual
, var
, state
, loc
);
2658 } else if (qual
->flags
.q
.explicit_index
) {
2659 _mesa_glsl_error(loc
, state
, "explicit index requires explicit location");
2662 if (qual
->flags
.q
.explicit_binding
&&
2663 validate_binding_qualifier(state
, loc
, var
, qual
)) {
2664 var
->data
.explicit_binding
= true;
2665 var
->data
.binding
= qual
->binding
;
2668 if (var
->type
->contains_atomic()) {
2669 if (var
->data
.mode
== ir_var_uniform
) {
2670 if (var
->data
.explicit_binding
) {
2672 &state
->atomic_counter_offsets
[var
->data
.binding
];
2674 if (*offset
% ATOMIC_COUNTER_SIZE
)
2675 _mesa_glsl_error(loc
, state
,
2676 "misaligned atomic counter offset");
2678 var
->data
.atomic
.offset
= *offset
;
2679 *offset
+= var
->type
->atomic_size();
2682 _mesa_glsl_error(loc
, state
,
2683 "atomic counters require explicit binding point");
2685 } else if (var
->data
.mode
!= ir_var_function_in
) {
2686 _mesa_glsl_error(loc
, state
, "atomic counters may only be declared as "
2687 "function parameters or uniform-qualified "
2688 "global variables");
2692 /* Does the declaration use the deprecated 'attribute' or 'varying'
2695 const bool uses_deprecated_qualifier
= qual
->flags
.q
.attribute
2696 || qual
->flags
.q
.varying
;
2699 /* Validate auxiliary storage qualifiers */
2701 /* From section 4.3.4 of the GLSL 1.30 spec:
2702 * "It is an error to use centroid in in a vertex shader."
2704 * From section 4.3.4 of the GLSL ES 3.00 spec:
2705 * "It is an error to use centroid in or interpolation qualifiers in
2706 * a vertex shader input."
2709 /* Section 4.3.6 of the GLSL 1.30 specification states:
2710 * "It is an error to use centroid out in a fragment shader."
2712 * The GL_ARB_shading_language_420pack extension specification states:
2713 * "It is an error to use auxiliary storage qualifiers or interpolation
2714 * qualifiers on an output in a fragment shader."
2716 if (qual
->flags
.q
.sample
&& (!is_varying_var(var
, state
->stage
) || uses_deprecated_qualifier
)) {
2717 _mesa_glsl_error(loc
, state
,
2718 "sample qualifier may only be used on `in` or `out` "
2719 "variables between shader stages");
2721 if (qual
->flags
.q
.centroid
&& !is_varying_var(var
, state
->stage
)) {
2722 _mesa_glsl_error(loc
, state
,
2723 "centroid qualifier may only be used with `in', "
2724 "`out' or `varying' variables between shader stages");
2728 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
2729 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
2730 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
2731 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
2732 * These extensions and all following extensions that add the 'layout'
2733 * keyword have been modified to require the use of 'in' or 'out'.
2735 * The following extension do not allow the deprecated keywords:
2737 * GL_AMD_conservative_depth
2738 * GL_ARB_conservative_depth
2739 * GL_ARB_gpu_shader5
2740 * GL_ARB_separate_shader_objects
2741 * GL_ARB_tesselation_shader
2742 * GL_ARB_transform_feedback3
2743 * GL_ARB_uniform_buffer_object
2745 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
2746 * allow layout with the deprecated keywords.
2748 const bool relaxed_layout_qualifier_checking
=
2749 state
->ARB_fragment_coord_conventions_enable
;
2751 if (qual
->has_layout() && uses_deprecated_qualifier
) {
2752 if (relaxed_layout_qualifier_checking
) {
2753 _mesa_glsl_warning(loc
, state
,
2754 "`layout' qualifier may not be used with "
2755 "`attribute' or `varying'");
2757 _mesa_glsl_error(loc
, state
,
2758 "`layout' qualifier may not be used with "
2759 "`attribute' or `varying'");
2763 /* Layout qualifiers for gl_FragDepth, which are enabled by extension
2764 * AMD_conservative_depth.
2766 int depth_layout_count
= qual
->flags
.q
.depth_any
2767 + qual
->flags
.q
.depth_greater
2768 + qual
->flags
.q
.depth_less
2769 + qual
->flags
.q
.depth_unchanged
;
2770 if (depth_layout_count
> 0
2771 && !state
->AMD_conservative_depth_enable
2772 && !state
->ARB_conservative_depth_enable
) {
2773 _mesa_glsl_error(loc
, state
,
2774 "extension GL_AMD_conservative_depth or "
2775 "GL_ARB_conservative_depth must be enabled "
2776 "to use depth layout qualifiers");
2777 } else if (depth_layout_count
> 0
2778 && strcmp(var
->name
, "gl_FragDepth") != 0) {
2779 _mesa_glsl_error(loc
, state
,
2780 "depth layout qualifiers can be applied only to "
2782 } else if (depth_layout_count
> 1
2783 && strcmp(var
->name
, "gl_FragDepth") == 0) {
2784 _mesa_glsl_error(loc
, state
,
2785 "at most one depth layout qualifier can be applied to "
2788 if (qual
->flags
.q
.depth_any
)
2789 var
->data
.depth_layout
= ir_depth_layout_any
;
2790 else if (qual
->flags
.q
.depth_greater
)
2791 var
->data
.depth_layout
= ir_depth_layout_greater
;
2792 else if (qual
->flags
.q
.depth_less
)
2793 var
->data
.depth_layout
= ir_depth_layout_less
;
2794 else if (qual
->flags
.q
.depth_unchanged
)
2795 var
->data
.depth_layout
= ir_depth_layout_unchanged
;
2797 var
->data
.depth_layout
= ir_depth_layout_none
;
2799 if (qual
->flags
.q
.std140
||
2800 qual
->flags
.q
.packed
||
2801 qual
->flags
.q
.shared
) {
2802 _mesa_glsl_error(loc
, state
,
2803 "uniform block layout qualifiers std140, packed, and "
2804 "shared can only be applied to uniform blocks, not "
2808 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
2809 validate_matrix_layout_for_type(state
, loc
, var
->type
, var
);
2812 if (var
->type
->contains_image())
2813 apply_image_qualifier_to_variable(qual
, var
, state
, loc
);
2817 * Get the variable that is being redeclared by this declaration
2819 * Semantic checks to verify the validity of the redeclaration are also
2820 * performed. If semantic checks fail, compilation error will be emitted via
2821 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
2824 * A pointer to an existing variable in the current scope if the declaration
2825 * is a redeclaration, \c NULL otherwise.
2827 static ir_variable
*
2828 get_variable_being_redeclared(ir_variable
*var
, YYLTYPE loc
,
2829 struct _mesa_glsl_parse_state
*state
,
2830 bool allow_all_redeclarations
)
2832 /* Check if this declaration is actually a re-declaration, either to
2833 * resize an array or add qualifiers to an existing variable.
2835 * This is allowed for variables in the current scope, or when at
2836 * global scope (for built-ins in the implicit outer scope).
2838 ir_variable
*earlier
= state
->symbols
->get_variable(var
->name
);
2839 if (earlier
== NULL
||
2840 (state
->current_function
!= NULL
&&
2841 !state
->symbols
->name_declared_this_scope(var
->name
))) {
2846 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
2848 * "It is legal to declare an array without a size and then
2849 * later re-declare the same name as an array of the same
2850 * type and specify a size."
2852 if (earlier
->type
->is_unsized_array() && var
->type
->is_array()
2853 && (var
->type
->element_type() == earlier
->type
->element_type())) {
2854 /* FINISHME: This doesn't match the qualifiers on the two
2855 * FINISHME: declarations. It's not 100% clear whether this is
2856 * FINISHME: required or not.
2859 const unsigned size
= unsigned(var
->type
->array_size());
2860 check_builtin_array_max_size(var
->name
, size
, loc
, state
);
2861 if ((size
> 0) && (size
<= earlier
->data
.max_array_access
)) {
2862 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
2864 earlier
->data
.max_array_access
);
2867 earlier
->type
= var
->type
;
2870 } else if ((state
->ARB_fragment_coord_conventions_enable
||
2871 state
->is_version(150, 0))
2872 && strcmp(var
->name
, "gl_FragCoord") == 0
2873 && earlier
->type
== var
->type
2874 && earlier
->data
.mode
== var
->data
.mode
) {
2875 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
2878 earlier
->data
.origin_upper_left
= var
->data
.origin_upper_left
;
2879 earlier
->data
.pixel_center_integer
= var
->data
.pixel_center_integer
;
2881 /* According to section 4.3.7 of the GLSL 1.30 spec,
2882 * the following built-in varaibles can be redeclared with an
2883 * interpolation qualifier:
2886 * * gl_FrontSecondaryColor
2887 * * gl_BackSecondaryColor
2889 * * gl_SecondaryColor
2891 } else if (state
->is_version(130, 0)
2892 && (strcmp(var
->name
, "gl_FrontColor") == 0
2893 || strcmp(var
->name
, "gl_BackColor") == 0
2894 || strcmp(var
->name
, "gl_FrontSecondaryColor") == 0
2895 || strcmp(var
->name
, "gl_BackSecondaryColor") == 0
2896 || strcmp(var
->name
, "gl_Color") == 0
2897 || strcmp(var
->name
, "gl_SecondaryColor") == 0)
2898 && earlier
->type
== var
->type
2899 && earlier
->data
.mode
== var
->data
.mode
) {
2900 earlier
->data
.interpolation
= var
->data
.interpolation
;
2902 /* Layout qualifiers for gl_FragDepth. */
2903 } else if ((state
->AMD_conservative_depth_enable
||
2904 state
->ARB_conservative_depth_enable
)
2905 && strcmp(var
->name
, "gl_FragDepth") == 0
2906 && earlier
->type
== var
->type
2907 && earlier
->data
.mode
== var
->data
.mode
) {
2909 /** From the AMD_conservative_depth spec:
2910 * Within any shader, the first redeclarations of gl_FragDepth
2911 * must appear before any use of gl_FragDepth.
2913 if (earlier
->data
.used
) {
2914 _mesa_glsl_error(&loc
, state
,
2915 "the first redeclaration of gl_FragDepth "
2916 "must appear before any use of gl_FragDepth");
2919 /* Prevent inconsistent redeclaration of depth layout qualifier. */
2920 if (earlier
->data
.depth_layout
!= ir_depth_layout_none
2921 && earlier
->data
.depth_layout
!= var
->data
.depth_layout
) {
2922 _mesa_glsl_error(&loc
, state
,
2923 "gl_FragDepth: depth layout is declared here "
2924 "as '%s, but it was previously declared as "
2926 depth_layout_string(var
->data
.depth_layout
),
2927 depth_layout_string(earlier
->data
.depth_layout
));
2930 earlier
->data
.depth_layout
= var
->data
.depth_layout
;
2932 } else if (allow_all_redeclarations
) {
2933 if (earlier
->data
.mode
!= var
->data
.mode
) {
2934 _mesa_glsl_error(&loc
, state
,
2935 "redeclaration of `%s' with incorrect qualifiers",
2937 } else if (earlier
->type
!= var
->type
) {
2938 _mesa_glsl_error(&loc
, state
,
2939 "redeclaration of `%s' has incorrect type",
2943 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
2950 * Generate the IR for an initializer in a variable declaration
2953 process_initializer(ir_variable
*var
, ast_declaration
*decl
,
2954 ast_fully_specified_type
*type
,
2955 exec_list
*initializer_instructions
,
2956 struct _mesa_glsl_parse_state
*state
)
2958 ir_rvalue
*result
= NULL
;
2960 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
2962 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
2964 * "All uniform variables are read-only and are initialized either
2965 * directly by an application via API commands, or indirectly by
2968 if (var
->data
.mode
== ir_var_uniform
) {
2969 state
->check_version(120, 0, &initializer_loc
,
2970 "cannot initialize uniforms");
2973 /* From section 4.1.7 of the GLSL 4.40 spec:
2975 * "Opaque variables [...] are initialized only through the
2976 * OpenGL API; they cannot be declared with an initializer in a
2979 if (var
->type
->contains_opaque()) {
2980 _mesa_glsl_error(& initializer_loc
, state
,
2981 "cannot initialize opaque variable");
2984 if ((var
->data
.mode
== ir_var_shader_in
) && (state
->current_function
== NULL
)) {
2985 _mesa_glsl_error(& initializer_loc
, state
,
2986 "cannot initialize %s shader input / %s",
2987 _mesa_shader_stage_to_string(state
->stage
),
2988 (state
->stage
== MESA_SHADER_VERTEX
)
2989 ? "attribute" : "varying");
2992 /* If the initializer is an ast_aggregate_initializer, recursively store
2993 * type information from the LHS into it, so that its hir() function can do
2996 if (decl
->initializer
->oper
== ast_aggregate
)
2997 _mesa_ast_set_aggregate_type(var
->type
, decl
->initializer
);
2999 ir_dereference
*const lhs
= new(state
) ir_dereference_variable(var
);
3000 ir_rvalue
*rhs
= decl
->initializer
->hir(initializer_instructions
, state
);
3002 /* Calculate the constant value if this is a const or uniform
3005 if (type
->qualifier
.flags
.q
.constant
3006 || type
->qualifier
.flags
.q
.uniform
) {
3007 ir_rvalue
*new_rhs
= validate_assignment(state
, initializer_loc
,
3008 var
->type
, rhs
, true);
3009 if (new_rhs
!= NULL
) {
3012 ir_constant
*constant_value
= rhs
->constant_expression_value();
3013 if (!constant_value
) {
3014 /* If ARB_shading_language_420pack is enabled, initializers of
3015 * const-qualified local variables do not have to be constant
3016 * expressions. Const-qualified global variables must still be
3017 * initialized with constant expressions.
3019 if (!state
->ARB_shading_language_420pack_enable
3020 || state
->current_function
== NULL
) {
3021 _mesa_glsl_error(& initializer_loc
, state
,
3022 "initializer of %s variable `%s' must be a "
3023 "constant expression",
3024 (type
->qualifier
.flags
.q
.constant
)
3025 ? "const" : "uniform",
3027 if (var
->type
->is_numeric()) {
3028 /* Reduce cascading errors. */
3029 var
->constant_value
= ir_constant::zero(state
, var
->type
);
3033 rhs
= constant_value
;
3034 var
->constant_value
= constant_value
;
3037 if (var
->type
->is_numeric()) {
3038 /* Reduce cascading errors. */
3039 var
->constant_value
= ir_constant::zero(state
, var
->type
);
3044 if (rhs
&& !rhs
->type
->is_error()) {
3045 bool temp
= var
->data
.read_only
;
3046 if (type
->qualifier
.flags
.q
.constant
)
3047 var
->data
.read_only
= false;
3049 /* Never emit code to initialize a uniform.
3051 const glsl_type
*initializer_type
;
3052 if (!type
->qualifier
.flags
.q
.uniform
) {
3053 do_assignment(initializer_instructions
, state
,
3058 type
->get_location());
3059 initializer_type
= result
->type
;
3061 initializer_type
= rhs
->type
;
3063 var
->constant_initializer
= rhs
->constant_expression_value();
3064 var
->data
.has_initializer
= true;
3066 /* If the declared variable is an unsized array, it must inherrit
3067 * its full type from the initializer. A declaration such as
3069 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
3073 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
3075 * The assignment generated in the if-statement (below) will also
3076 * automatically handle this case for non-uniforms.
3078 * If the declared variable is not an array, the types must
3079 * already match exactly. As a result, the type assignment
3080 * here can be done unconditionally. For non-uniforms the call
3081 * to do_assignment can change the type of the initializer (via
3082 * the implicit conversion rules). For uniforms the initializer
3083 * must be a constant expression, and the type of that expression
3084 * was validated above.
3086 var
->type
= initializer_type
;
3088 var
->data
.read_only
= temp
;
3096 * Do additional processing necessary for geometry shader input declarations
3097 * (this covers both interface blocks arrays and bare input variables).
3100 handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
3101 YYLTYPE loc
, ir_variable
*var
)
3103 unsigned num_vertices
= 0;
3104 if (state
->gs_input_prim_type_specified
) {
3105 num_vertices
= vertices_per_prim(state
->in_qualifier
->prim_type
);
3108 /* Geometry shader input variables must be arrays. Caller should have
3109 * reported an error for this.
3111 if (!var
->type
->is_array()) {
3112 assert(state
->error
);
3114 /* To avoid cascading failures, short circuit the checks below. */
3118 if (var
->type
->is_unsized_array()) {
3119 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says:
3121 * All geometry shader input unsized array declarations will be
3122 * sized by an earlier input layout qualifier, when present, as per
3123 * the following table.
3125 * Followed by a table mapping each allowed input layout qualifier to
3126 * the corresponding input length.
3128 if (num_vertices
!= 0)
3129 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
3132 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec
3133 * includes the following examples of compile-time errors:
3135 * // code sequence within one shader...
3136 * in vec4 Color1[]; // size unknown
3137 * ...Color1.length()...// illegal, length() unknown
3138 * in vec4 Color2[2]; // size is 2
3139 * ...Color1.length()...// illegal, Color1 still has no size
3140 * in vec4 Color3[3]; // illegal, input sizes are inconsistent
3141 * layout(lines) in; // legal, input size is 2, matching
3142 * in vec4 Color4[3]; // illegal, contradicts layout
3145 * To detect the case illustrated by Color3, we verify that the size of
3146 * an explicitly-sized array matches the size of any previously declared
3147 * explicitly-sized array. To detect the case illustrated by Color4, we
3148 * verify that the size of an explicitly-sized array is consistent with
3149 * any previously declared input layout.
3151 if (num_vertices
!= 0 && var
->type
->length
!= num_vertices
) {
3152 _mesa_glsl_error(&loc
, state
,
3153 "geometry shader input size contradicts previously"
3154 " declared layout (size is %u, but layout requires a"
3155 " size of %u)", var
->type
->length
, num_vertices
);
3156 } else if (state
->gs_input_size
!= 0 &&
3157 var
->type
->length
!= state
->gs_input_size
) {
3158 _mesa_glsl_error(&loc
, state
,
3159 "geometry shader input sizes are "
3160 "inconsistent (size is %u, but a previous "
3161 "declaration has size %u)",
3162 var
->type
->length
, state
->gs_input_size
);
3164 state
->gs_input_size
= var
->type
->length
;
3171 validate_identifier(const char *identifier
, YYLTYPE loc
,
3172 struct _mesa_glsl_parse_state
*state
)
3174 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
3176 * "Identifiers starting with "gl_" are reserved for use by
3177 * OpenGL, and may not be declared in a shader as either a
3178 * variable or a function."
3180 if (is_gl_identifier(identifier
)) {
3181 _mesa_glsl_error(&loc
, state
,
3182 "identifier `%s' uses reserved `gl_' prefix",
3184 } else if (strstr(identifier
, "__")) {
3185 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
3188 * "In addition, all identifiers containing two
3189 * consecutive underscores (__) are reserved as
3190 * possible future keywords."
3192 * The intention is that names containing __ are reserved for internal
3193 * use by the implementation, and names prefixed with GL_ are reserved
3194 * for use by Khronos. Names simply containing __ are dangerous to use,
3195 * but should be allowed.
3197 * A future version of the GLSL specification will clarify this.
3199 _mesa_glsl_warning(&loc
, state
,
3200 "identifier `%s' uses reserved `__' string",
3206 precision_qualifier_allowed(const glsl_type
*type
)
3208 /* Precision qualifiers apply to floating point, integer and sampler
3211 * Section 4.5.2 (Precision Qualifiers) of the GLSL 1.30 spec says:
3212 * "Any floating point or any integer declaration can have the type
3213 * preceded by one of these precision qualifiers [...] Literal
3214 * constants do not have precision qualifiers. Neither do Boolean
3217 * Section 4.5 (Precision and Precision Qualifiers) of the GLSL 1.30
3220 * "Precision qualifiers are added for code portability with OpenGL
3221 * ES, not for functionality. They have the same syntax as in OpenGL
3224 * Section 8 (Built-In Functions) of the GLSL ES 1.00 spec says:
3226 * "uniform lowp sampler2D sampler;
3229 * lowp vec4 col = texture2D (sampler, coord);
3230 * // texture2D returns lowp"
3232 * From this, we infer that GLSL 1.30 (and later) should allow precision
3233 * qualifiers on sampler types just like float and integer types.
3235 return type
->is_float()
3236 || type
->is_integer()
3237 || type
->is_record()
3238 || type
->is_sampler();
3242 ast_declarator_list::hir(exec_list
*instructions
,
3243 struct _mesa_glsl_parse_state
*state
)
3246 const struct glsl_type
*decl_type
;
3247 const char *type_name
= NULL
;
3248 ir_rvalue
*result
= NULL
;
3249 YYLTYPE loc
= this->get_location();
3251 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
3253 * "To ensure that a particular output variable is invariant, it is
3254 * necessary to use the invariant qualifier. It can either be used to
3255 * qualify a previously declared variable as being invariant
3257 * invariant gl_Position; // make existing gl_Position be invariant"
3259 * In these cases the parser will set the 'invariant' flag in the declarator
3260 * list, and the type will be NULL.
3262 if (this->invariant
) {
3263 assert(this->type
== NULL
);
3265 if (state
->current_function
!= NULL
) {
3266 _mesa_glsl_error(& loc
, state
,
3267 "all uses of `invariant' keyword must be at global "
3271 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
3272 assert(decl
->array_specifier
== NULL
);
3273 assert(decl
->initializer
== NULL
);
3275 ir_variable
*const earlier
=
3276 state
->symbols
->get_variable(decl
->identifier
);
3277 if (earlier
== NULL
) {
3278 _mesa_glsl_error(& loc
, state
,
3279 "undeclared variable `%s' cannot be marked "
3280 "invariant", decl
->identifier
);
3281 } else if (!is_varying_var(earlier
, state
->stage
)) {
3282 _mesa_glsl_error(&loc
, state
,
3283 "`%s' cannot be marked invariant; interfaces between "
3284 "shader stages only.", decl
->identifier
);
3285 } else if (earlier
->data
.used
) {
3286 _mesa_glsl_error(& loc
, state
,
3287 "variable `%s' may not be redeclared "
3288 "`invariant' after being used",
3291 earlier
->data
.invariant
= true;
3295 /* Invariant redeclarations do not have r-values.
3300 if (this->precise
) {
3301 assert(this->type
== NULL
);
3303 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
3304 assert(decl
->array_specifier
== NULL
);
3305 assert(decl
->initializer
== NULL
);
3307 ir_variable
*const earlier
=
3308 state
->symbols
->get_variable(decl
->identifier
);
3309 if (earlier
== NULL
) {
3310 _mesa_glsl_error(& loc
, state
,
3311 "undeclared variable `%s' cannot be marked "
3312 "precise", decl
->identifier
);
3313 } else if (state
->current_function
!= NULL
&&
3314 !state
->symbols
->name_declared_this_scope(decl
->identifier
)) {
3315 /* Note: we have to check if we're in a function, since
3316 * builtins are treated as having come from another scope.
3318 _mesa_glsl_error(& loc
, state
,
3319 "variable `%s' from an outer scope may not be "
3320 "redeclared `precise' in this scope",
3322 } else if (earlier
->data
.used
) {
3323 _mesa_glsl_error(& loc
, state
,
3324 "variable `%s' may not be redeclared "
3325 "`precise' after being used",
3328 earlier
->data
.precise
= true;
3332 /* Precise redeclarations do not have r-values either. */
3336 assert(this->type
!= NULL
);
3337 assert(!this->invariant
);
3338 assert(!this->precise
);
3340 /* The type specifier may contain a structure definition. Process that
3341 * before any of the variable declarations.
3343 (void) this->type
->specifier
->hir(instructions
, state
);
3345 decl_type
= this->type
->glsl_type(& type_name
, state
);
3347 /* An offset-qualified atomic counter declaration sets the default
3348 * offset for the next declaration within the same atomic counter
3351 if (decl_type
&& decl_type
->contains_atomic()) {
3352 if (type
->qualifier
.flags
.q
.explicit_binding
&&
3353 type
->qualifier
.flags
.q
.explicit_offset
)
3354 state
->atomic_counter_offsets
[type
->qualifier
.binding
] =
3355 type
->qualifier
.offset
;
3358 if (this->declarations
.is_empty()) {
3359 /* If there is no structure involved in the program text, there are two
3360 * possible scenarios:
3362 * - The program text contained something like 'vec4;'. This is an
3363 * empty declaration. It is valid but weird. Emit a warning.
3365 * - The program text contained something like 'S;' and 'S' is not the
3366 * name of a known structure type. This is both invalid and weird.
3369 * - The program text contained something like 'mediump float;'
3370 * when the programmer probably meant 'precision mediump
3371 * float;' Emit a warning with a description of what they
3372 * probably meant to do.
3374 * Note that if decl_type is NULL and there is a structure involved,
3375 * there must have been some sort of error with the structure. In this
3376 * case we assume that an error was already generated on this line of
3377 * code for the structure. There is no need to generate an additional,
3380 assert(this->type
->specifier
->structure
== NULL
|| decl_type
!= NULL
3383 if (decl_type
== NULL
) {
3384 _mesa_glsl_error(&loc
, state
,
3385 "invalid type `%s' in empty declaration",
3387 } else if (decl_type
->base_type
== GLSL_TYPE_ATOMIC_UINT
) {
3388 /* Empty atomic counter declarations are allowed and useful
3389 * to set the default offset qualifier.
3392 } else if (this->type
->qualifier
.precision
!= ast_precision_none
) {
3393 if (this->type
->specifier
->structure
!= NULL
) {
3394 _mesa_glsl_error(&loc
, state
,
3395 "precision qualifiers can't be applied "
3398 static const char *const precision_names
[] = {
3405 _mesa_glsl_warning(&loc
, state
,
3406 "empty declaration with precision qualifier, "
3407 "to set the default precision, use "
3408 "`precision %s %s;'",
3409 precision_names
[this->type
->qualifier
.precision
],
3412 } else if (this->type
->specifier
->structure
== NULL
) {
3413 _mesa_glsl_warning(&loc
, state
, "empty declaration");
3417 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
3418 const struct glsl_type
*var_type
;
3421 /* FINISHME: Emit a warning if a variable declaration shadows a
3422 * FINISHME: declaration at a higher scope.
3425 if ((decl_type
== NULL
) || decl_type
->is_void()) {
3426 if (type_name
!= NULL
) {
3427 _mesa_glsl_error(& loc
, state
,
3428 "invalid type `%s' in declaration of `%s'",
3429 type_name
, decl
->identifier
);
3431 _mesa_glsl_error(& loc
, state
,
3432 "invalid type in declaration of `%s'",
3438 var_type
= process_array_type(&loc
, decl_type
, decl
->array_specifier
,
3441 var
= new(ctx
) ir_variable(var_type
, decl
->identifier
, ir_var_auto
);
3443 /* The 'varying in' and 'varying out' qualifiers can only be used with
3444 * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
3447 if (this->type
->qualifier
.flags
.q
.varying
) {
3448 if (this->type
->qualifier
.flags
.q
.in
) {
3449 _mesa_glsl_error(& loc
, state
,
3450 "`varying in' qualifier in declaration of "
3451 "`%s' only valid for geometry shaders using "
3452 "ARB_geometry_shader4 or EXT_geometry_shader4",
3454 } else if (this->type
->qualifier
.flags
.q
.out
) {
3455 _mesa_glsl_error(& loc
, state
,
3456 "`varying out' qualifier in declaration of "
3457 "`%s' only valid for geometry shaders using "
3458 "ARB_geometry_shader4 or EXT_geometry_shader4",
3463 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
3465 * "Global variables can only use the qualifiers const,
3466 * attribute, uniform, or varying. Only one may be
3469 * Local variables can only use the qualifier const."
3471 * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
3472 * any extension that adds the 'layout' keyword.
3474 if (!state
->is_version(130, 300)
3475 && !state
->has_explicit_attrib_location()
3476 && !state
->has_separate_shader_objects()
3477 && !state
->ARB_fragment_coord_conventions_enable
) {
3478 if (this->type
->qualifier
.flags
.q
.out
) {
3479 _mesa_glsl_error(& loc
, state
,
3480 "`out' qualifier in declaration of `%s' "
3481 "only valid for function parameters in %s",
3482 decl
->identifier
, state
->get_version_string());
3484 if (this->type
->qualifier
.flags
.q
.in
) {
3485 _mesa_glsl_error(& loc
, state
,
3486 "`in' qualifier in declaration of `%s' "
3487 "only valid for function parameters in %s",
3488 decl
->identifier
, state
->get_version_string());
3490 /* FINISHME: Test for other invalid qualifiers. */
3493 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
3496 if (this->type
->qualifier
.flags
.q
.invariant
) {
3497 if (!is_varying_var(var
, state
->stage
)) {
3498 _mesa_glsl_error(&loc
, state
,
3499 "`%s' cannot be marked invariant; interfaces between "
3500 "shader stages only", var
->name
);
3504 if (state
->current_function
!= NULL
) {
3505 const char *mode
= NULL
;
3506 const char *extra
= "";
3508 /* There is no need to check for 'inout' here because the parser will
3509 * only allow that in function parameter lists.
3511 if (this->type
->qualifier
.flags
.q
.attribute
) {
3513 } else if (this->type
->qualifier
.flags
.q
.uniform
) {
3515 } else if (this->type
->qualifier
.flags
.q
.varying
) {
3517 } else if (this->type
->qualifier
.flags
.q
.in
) {
3519 extra
= " or in function parameter list";
3520 } else if (this->type
->qualifier
.flags
.q
.out
) {
3522 extra
= " or in function parameter list";
3526 _mesa_glsl_error(& loc
, state
,
3527 "%s variable `%s' must be declared at "
3529 mode
, var
->name
, extra
);
3531 } else if (var
->data
.mode
== ir_var_shader_in
) {
3532 var
->data
.read_only
= true;
3534 if (state
->stage
== MESA_SHADER_VERTEX
) {
3535 bool error_emitted
= false;
3537 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
3539 * "Vertex shader inputs can only be float, floating-point
3540 * vectors, matrices, signed and unsigned integers and integer
3541 * vectors. Vertex shader inputs can also form arrays of these
3542 * types, but not structures."
3544 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
3546 * "Vertex shader inputs can only be float, floating-point
3547 * vectors, matrices, signed and unsigned integers and integer
3548 * vectors. They cannot be arrays or structures."
3550 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
3552 * "The attribute qualifier can be used only with float,
3553 * floating-point vectors, and matrices. Attribute variables
3554 * cannot be declared as arrays or structures."
3556 * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
3558 * "Vertex shader inputs can only be float, floating-point
3559 * vectors, matrices, signed and unsigned integers and integer
3560 * vectors. Vertex shader inputs cannot be arrays or
3563 const glsl_type
*check_type
= var
->type
;
3564 while (check_type
->is_array())
3565 check_type
= check_type
->element_type();
3567 switch (check_type
->base_type
) {
3568 case GLSL_TYPE_FLOAT
:
3570 case GLSL_TYPE_UINT
:
3572 if (state
->is_version(120, 300))
3576 _mesa_glsl_error(& loc
, state
,
3577 "vertex shader input / attribute cannot have "
3579 var
->type
->is_array() ? "array of " : "",
3581 error_emitted
= true;
3584 if (!error_emitted
&& var
->type
->is_array() &&
3585 !state
->check_version(150, 0, &loc
,
3586 "vertex shader input / attribute "
3587 "cannot have array type")) {
3588 error_emitted
= true;
3590 } else if (state
->stage
== MESA_SHADER_GEOMETRY
) {
3591 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
3593 * Geometry shader input variables get the per-vertex values
3594 * written out by vertex shader output variables of the same
3595 * names. Since a geometry shader operates on a set of
3596 * vertices, each input varying variable (or input block, see
3597 * interface blocks below) needs to be declared as an array.
3599 if (!var
->type
->is_array()) {
3600 _mesa_glsl_error(&loc
, state
,
3601 "geometry shader inputs must be arrays");
3604 handle_geometry_shader_input_decl(state
, loc
, var
);
3608 /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
3609 * so must integer vertex outputs.
3611 * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
3612 * "Fragment shader inputs that are signed or unsigned integers or
3613 * integer vectors must be qualified with the interpolation qualifier
3616 * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
3617 * "Fragment shader inputs that are, or contain, signed or unsigned
3618 * integers or integer vectors must be qualified with the
3619 * interpolation qualifier flat."
3621 * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
3622 * "Vertex shader outputs that are, or contain, signed or unsigned
3623 * integers or integer vectors must be qualified with the
3624 * interpolation qualifier flat."
3626 * Note that prior to GLSL 1.50, this requirement applied to vertex
3627 * outputs rather than fragment inputs. That creates problems in the
3628 * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
3629 * desktop GL shaders. For GLSL ES shaders, we follow the spec and
3630 * apply the restriction to both vertex outputs and fragment inputs.
3632 * Note also that the desktop GLSL specs are missing the text "or
3633 * contain"; this is presumably an oversight, since there is no
3634 * reasonable way to interpolate a fragment shader input that contains
3637 if (state
->is_version(130, 300) &&
3638 var
->type
->contains_integer() &&
3639 var
->data
.interpolation
!= INTERP_QUALIFIER_FLAT
&&
3640 ((state
->stage
== MESA_SHADER_FRAGMENT
&& var
->data
.mode
== ir_var_shader_in
)
3641 || (state
->stage
== MESA_SHADER_VERTEX
&& var
->data
.mode
== ir_var_shader_out
3642 && state
->es_shader
))) {
3643 const char *var_type
= (state
->stage
== MESA_SHADER_VERTEX
) ?
3644 "vertex output" : "fragment input";
3645 _mesa_glsl_error(&loc
, state
, "if a %s is (or contains) "
3646 "an integer, then it must be qualified with 'flat'",
3651 /* Interpolation qualifiers cannot be applied to 'centroid' and
3652 * 'centroid varying'.
3654 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
3655 * "interpolation qualifiers may only precede the qualifiers in,
3656 * centroid in, out, or centroid out in a declaration. They do not apply
3657 * to the deprecated storage qualifiers varying or centroid varying."
3659 * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
3661 if (state
->is_version(130, 0)
3662 && this->type
->qualifier
.has_interpolation()
3663 && this->type
->qualifier
.flags
.q
.varying
) {
3665 const char *i
= this->type
->qualifier
.interpolation_string();
3668 if (this->type
->qualifier
.flags
.q
.centroid
)
3669 s
= "centroid varying";
3673 _mesa_glsl_error(&loc
, state
,
3674 "qualifier '%s' cannot be applied to the "
3675 "deprecated storage qualifier '%s'", i
, s
);
3679 /* Interpolation qualifiers can only apply to vertex shader outputs and
3680 * fragment shader inputs.
3682 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
3683 * "Outputs from a vertex shader (out) and inputs to a fragment
3684 * shader (in) can be further qualified with one or more of these
3685 * interpolation qualifiers"
3687 * From page 31 (page 37 of the PDF) of the GLSL ES 3.00 spec:
3688 * "These interpolation qualifiers may only precede the qualifiers
3689 * in, centroid in, out, or centroid out in a declaration. They do
3690 * not apply to inputs into a vertex shader or outputs from a
3693 if (state
->is_version(130, 300)
3694 && this->type
->qualifier
.has_interpolation()) {
3696 const char *i
= this->type
->qualifier
.interpolation_string();
3699 switch (state
->stage
) {
3700 case MESA_SHADER_VERTEX
:
3701 if (this->type
->qualifier
.flags
.q
.in
) {
3702 _mesa_glsl_error(&loc
, state
,
3703 "qualifier '%s' cannot be applied to vertex "
3704 "shader inputs", i
);
3707 case MESA_SHADER_FRAGMENT
:
3708 if (this->type
->qualifier
.flags
.q
.out
) {
3709 _mesa_glsl_error(&loc
, state
,
3710 "qualifier '%s' cannot be applied to fragment "
3711 "shader outputs", i
);
3720 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
3722 if (this->type
->qualifier
.precision
!= ast_precision_none
) {
3723 state
->check_precision_qualifiers_allowed(&loc
);
3727 /* If a precision qualifier is allowed on a type, it is allowed on
3728 * an array of that type.
3730 if (!(this->type
->qualifier
.precision
== ast_precision_none
3731 || precision_qualifier_allowed(var
->type
)
3732 || (var
->type
->is_array()
3733 && precision_qualifier_allowed(var
->type
->fields
.array
)))) {
3735 _mesa_glsl_error(&loc
, state
,
3736 "precision qualifiers apply only to floating point"
3737 ", integer and sampler types");
3740 /* From section 4.1.7 of the GLSL 4.40 spec:
3742 * "[Opaque types] can only be declared as function
3743 * parameters or uniform-qualified variables."
3745 if (var_type
->contains_opaque() &&
3746 !this->type
->qualifier
.flags
.q
.uniform
) {
3747 _mesa_glsl_error(&loc
, state
,
3748 "opaque variables must be declared uniform");
3751 /* Process the initializer and add its instructions to a temporary
3752 * list. This list will be added to the instruction stream (below) after
3753 * the declaration is added. This is done because in some cases (such as
3754 * redeclarations) the declaration may not actually be added to the
3755 * instruction stream.
3757 exec_list initializer_instructions
;
3759 /* Examine var name here since var may get deleted in the next call */
3760 bool var_is_gl_id
= is_gl_identifier(var
->name
);
3762 ir_variable
*earlier
=
3763 get_variable_being_redeclared(var
, decl
->get_location(), state
,
3764 false /* allow_all_redeclarations */);
3765 if (earlier
!= NULL
) {
3767 earlier
->data
.how_declared
== ir_var_declared_in_block
) {
3768 _mesa_glsl_error(&loc
, state
,
3769 "`%s' has already been redeclared using "
3770 "gl_PerVertex", earlier
->name
);
3772 earlier
->data
.how_declared
= ir_var_declared_normally
;
3775 if (decl
->initializer
!= NULL
) {
3776 result
= process_initializer((earlier
== NULL
) ? var
: earlier
,
3778 &initializer_instructions
, state
);
3781 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
3783 * "It is an error to write to a const variable outside of
3784 * its declaration, so they must be initialized when
3787 if (this->type
->qualifier
.flags
.q
.constant
&& decl
->initializer
== NULL
) {
3788 _mesa_glsl_error(& loc
, state
,
3789 "const declaration of `%s' must be initialized",
3793 if (state
->es_shader
) {
3794 const glsl_type
*const t
= (earlier
== NULL
)
3795 ? var
->type
: earlier
->type
;
3797 if (t
->is_unsized_array())
3798 /* Section 10.17 of the GLSL ES 1.00 specification states that
3799 * unsized array declarations have been removed from the language.
3800 * Arrays that are sized using an initializer are still explicitly
3801 * sized. However, GLSL ES 1.00 does not allow array
3802 * initializers. That is only allowed in GLSL ES 3.00.
3804 * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
3806 * "An array type can also be formed without specifying a size
3807 * if the definition includes an initializer:
3809 * float x[] = float[2] (1.0, 2.0); // declares an array of size 2
3810 * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
3815 _mesa_glsl_error(& loc
, state
,
3816 "unsized array declarations are not allowed in "
3820 /* If the declaration is not a redeclaration, there are a few additional
3821 * semantic checks that must be applied. In addition, variable that was
3822 * created for the declaration should be added to the IR stream.
3824 if (earlier
== NULL
) {
3825 validate_identifier(decl
->identifier
, loc
, state
);
3827 /* Add the variable to the symbol table. Note that the initializer's
3828 * IR was already processed earlier (though it hasn't been emitted
3829 * yet), without the variable in scope.
3831 * This differs from most C-like languages, but it follows the GLSL
3832 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
3835 * "Within a declaration, the scope of a name starts immediately
3836 * after the initializer if present or immediately after the name
3837 * being declared if not."
3839 if (!state
->symbols
->add_variable(var
)) {
3840 YYLTYPE loc
= this->get_location();
3841 _mesa_glsl_error(&loc
, state
, "name `%s' already taken in the "
3842 "current scope", decl
->identifier
);
3846 /* Push the variable declaration to the top. It means that all the
3847 * variable declarations will appear in a funny last-to-first order,
3848 * but otherwise we run into trouble if a function is prototyped, a
3849 * global var is decled, then the function is defined with usage of
3850 * the global var. See glslparsertest's CorrectModule.frag.
3852 instructions
->push_head(var
);
3855 instructions
->append_list(&initializer_instructions
);
3859 /* Generally, variable declarations do not have r-values. However,
3860 * one is used for the declaration in
3862 * while (bool b = some_condition()) {
3866 * so we return the rvalue from the last seen declaration here.
3873 ast_parameter_declarator::hir(exec_list
*instructions
,
3874 struct _mesa_glsl_parse_state
*state
)
3877 const struct glsl_type
*type
;
3878 const char *name
= NULL
;
3879 YYLTYPE loc
= this->get_location();
3881 type
= this->type
->glsl_type(& name
, state
);
3885 _mesa_glsl_error(& loc
, state
,
3886 "invalid type `%s' in declaration of `%s'",
3887 name
, this->identifier
);
3889 _mesa_glsl_error(& loc
, state
,
3890 "invalid type in declaration of `%s'",
3894 type
= glsl_type::error_type
;
3897 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
3899 * "Functions that accept no input arguments need not use void in the
3900 * argument list because prototypes (or definitions) are required and
3901 * therefore there is no ambiguity when an empty argument list "( )" is
3902 * declared. The idiom "(void)" as a parameter list is provided for
3905 * Placing this check here prevents a void parameter being set up
3906 * for a function, which avoids tripping up checks for main taking
3907 * parameters and lookups of an unnamed symbol.
3909 if (type
->is_void()) {
3910 if (this->identifier
!= NULL
)
3911 _mesa_glsl_error(& loc
, state
,
3912 "named parameter cannot have type `void'");
3918 if (formal_parameter
&& (this->identifier
== NULL
)) {
3919 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
3923 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
3924 * call already handled the "vec4[..] foo" case.
3926 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
3928 if (!type
->is_error() && type
->is_unsized_array()) {
3929 _mesa_glsl_error(&loc
, state
, "arrays passed as parameters must have "
3931 type
= glsl_type::error_type
;
3935 ir_variable
*var
= new(ctx
)
3936 ir_variable(type
, this->identifier
, ir_var_function_in
);
3938 /* Apply any specified qualifiers to the parameter declaration. Note that
3939 * for function parameters the default mode is 'in'.
3941 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
,
3944 /* From section 4.1.7 of the GLSL 4.40 spec:
3946 * "Opaque variables cannot be treated as l-values; hence cannot
3947 * be used as out or inout function parameters, nor can they be
3950 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
3951 && type
->contains_opaque()) {
3952 _mesa_glsl_error(&loc
, state
, "out and inout parameters cannot "
3953 "contain opaque variables");
3954 type
= glsl_type::error_type
;
3957 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
3959 * "When calling a function, expressions that do not evaluate to
3960 * l-values cannot be passed to parameters declared as out or inout."
3962 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
3964 * "Other binary or unary expressions, non-dereferenced arrays,
3965 * function names, swizzles with repeated fields, and constants
3966 * cannot be l-values."
3968 * So for GLSL 1.10, passing an array as an out or inout parameter is not
3969 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
3971 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
3973 && !state
->check_version(120, 100, &loc
,
3974 "arrays cannot be out or inout parameters")) {
3975 type
= glsl_type::error_type
;
3978 instructions
->push_tail(var
);
3980 /* Parameter declarations do not have r-values.
3987 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
3989 exec_list
*ir_parameters
,
3990 _mesa_glsl_parse_state
*state
)
3992 ast_parameter_declarator
*void_param
= NULL
;
3995 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
3996 param
->formal_parameter
= formal
;
3997 param
->hir(ir_parameters
, state
);
4005 if ((void_param
!= NULL
) && (count
> 1)) {
4006 YYLTYPE loc
= void_param
->get_location();
4008 _mesa_glsl_error(& loc
, state
,
4009 "`void' parameter must be only parameter");
4015 emit_function(_mesa_glsl_parse_state
*state
, ir_function
*f
)
4017 /* IR invariants disallow function declarations or definitions
4018 * nested within other function definitions. But there is no
4019 * requirement about the relative order of function declarations
4020 * and definitions with respect to one another. So simply insert
4021 * the new ir_function block at the end of the toplevel instruction
4024 state
->toplevel_ir
->push_tail(f
);
4029 ast_function::hir(exec_list
*instructions
,
4030 struct _mesa_glsl_parse_state
*state
)
4033 ir_function
*f
= NULL
;
4034 ir_function_signature
*sig
= NULL
;
4035 exec_list hir_parameters
;
4037 const char *const name
= identifier
;
4039 /* New functions are always added to the top-level IR instruction stream,
4040 * so this instruction list pointer is ignored. See also emit_function
4043 (void) instructions
;
4045 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
4047 * "Function declarations (prototypes) cannot occur inside of functions;
4048 * they must be at global scope, or for the built-in functions, outside
4049 * the global scope."
4051 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
4053 * "User defined functions may only be defined within the global scope."
4055 * Note that this language does not appear in GLSL 1.10.
4057 if ((state
->current_function
!= NULL
) &&
4058 state
->is_version(120, 100)) {
4059 YYLTYPE loc
= this->get_location();
4060 _mesa_glsl_error(&loc
, state
,
4061 "declaration of function `%s' not allowed within "
4062 "function body", name
);
4065 validate_identifier(name
, this->get_location(), state
);
4067 /* Convert the list of function parameters to HIR now so that they can be
4068 * used below to compare this function's signature with previously seen
4069 * signatures for functions with the same name.
4071 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
4073 & hir_parameters
, state
);
4075 const char *return_type_name
;
4076 const glsl_type
*return_type
=
4077 this->return_type
->glsl_type(& return_type_name
, state
);
4080 YYLTYPE loc
= this->get_location();
4081 _mesa_glsl_error(&loc
, state
,
4082 "function `%s' has undeclared return type `%s'",
4083 name
, return_type_name
);
4084 return_type
= glsl_type::error_type
;
4087 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
4088 * "No qualifier is allowed on the return type of a function."
4090 if (this->return_type
->has_qualifiers()) {
4091 YYLTYPE loc
= this->get_location();
4092 _mesa_glsl_error(& loc
, state
,
4093 "function `%s' return type has qualifiers", name
);
4096 /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
4098 * "Arrays are allowed as arguments and as the return type. In both
4099 * cases, the array must be explicitly sized."
4101 if (return_type
->is_unsized_array()) {
4102 YYLTYPE loc
= this->get_location();
4103 _mesa_glsl_error(& loc
, state
,
4104 "function `%s' return type array must be explicitly "
4108 /* From section 4.1.7 of the GLSL 4.40 spec:
4110 * "[Opaque types] can only be declared as function parameters
4111 * or uniform-qualified variables."
4113 if (return_type
->contains_opaque()) {
4114 YYLTYPE loc
= this->get_location();
4115 _mesa_glsl_error(&loc
, state
,
4116 "function `%s' return type can't contain an opaque type",
4120 /* Create an ir_function if one doesn't already exist. */
4121 f
= state
->symbols
->get_function(name
);
4123 f
= new(ctx
) ir_function(name
);
4124 if (!state
->symbols
->add_function(f
)) {
4125 /* This function name shadows a non-function use of the same name. */
4126 YYLTYPE loc
= this->get_location();
4128 _mesa_glsl_error(&loc
, state
, "function name `%s' conflicts with "
4129 "non-function", name
);
4133 emit_function(state
, f
);
4136 /* Verify that this function's signature either doesn't match a previously
4137 * seen signature for a function with the same name, or, if a match is found,
4138 * that the previously seen signature does not have an associated definition.
4140 if (state
->es_shader
|| f
->has_user_signature()) {
4141 sig
= f
->exact_matching_signature(state
, &hir_parameters
);
4143 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
4144 if (badvar
!= NULL
) {
4145 YYLTYPE loc
= this->get_location();
4147 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
4148 "qualifiers don't match prototype", name
, badvar
);
4151 if (sig
->return_type
!= return_type
) {
4152 YYLTYPE loc
= this->get_location();
4154 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
4155 "match prototype", name
);
4158 if (sig
->is_defined
) {
4159 if (is_definition
) {
4160 YYLTYPE loc
= this->get_location();
4161 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
4163 /* We just encountered a prototype that exactly matches a
4164 * function that's already been defined. This is redundant,
4165 * and we should ignore it.
4173 /* Verify the return type of main() */
4174 if (strcmp(name
, "main") == 0) {
4175 if (! return_type
->is_void()) {
4176 YYLTYPE loc
= this->get_location();
4178 _mesa_glsl_error(& loc
, state
, "main() must return void");
4181 if (!hir_parameters
.is_empty()) {
4182 YYLTYPE loc
= this->get_location();
4184 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
4188 /* Finish storing the information about this new function in its signature.
4191 sig
= new(ctx
) ir_function_signature(return_type
);
4192 f
->add_signature(sig
);
4195 sig
->replace_parameters(&hir_parameters
);
4198 /* Function declarations (prototypes) do not have r-values.
4205 ast_function_definition::hir(exec_list
*instructions
,
4206 struct _mesa_glsl_parse_state
*state
)
4208 prototype
->is_definition
= true;
4209 prototype
->hir(instructions
, state
);
4211 ir_function_signature
*signature
= prototype
->signature
;
4212 if (signature
== NULL
)
4215 assert(state
->current_function
== NULL
);
4216 state
->current_function
= signature
;
4217 state
->found_return
= false;
4219 /* Duplicate parameters declared in the prototype as concrete variables.
4220 * Add these to the symbol table.
4222 state
->symbols
->push_scope();
4223 foreach_in_list(ir_variable
, var
, &signature
->parameters
) {
4224 assert(var
->as_variable() != NULL
);
4226 /* The only way a parameter would "exist" is if two parameters have
4229 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
4230 YYLTYPE loc
= this->get_location();
4232 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
4234 state
->symbols
->add_variable(var
);
4238 /* Convert the body of the function to HIR. */
4239 this->body
->hir(&signature
->body
, state
);
4240 signature
->is_defined
= true;
4242 state
->symbols
->pop_scope();
4244 assert(state
->current_function
== signature
);
4245 state
->current_function
= NULL
;
4247 if (!signature
->return_type
->is_void() && !state
->found_return
) {
4248 YYLTYPE loc
= this->get_location();
4249 _mesa_glsl_error(& loc
, state
, "function `%s' has non-void return type "
4250 "%s, but no return statement",
4251 signature
->function_name(),
4252 signature
->return_type
->name
);
4255 /* Function definitions do not have r-values.
4262 ast_jump_statement::hir(exec_list
*instructions
,
4263 struct _mesa_glsl_parse_state
*state
)
4270 assert(state
->current_function
);
4272 if (opt_return_value
) {
4273 ir_rvalue
*ret
= opt_return_value
->hir(instructions
, state
);
4275 /* The value of the return type can be NULL if the shader says
4276 * 'return foo();' and foo() is a function that returns void.
4278 * NOTE: The GLSL spec doesn't say that this is an error. The type
4279 * of the return value is void. If the return type of the function is
4280 * also void, then this should compile without error. Seriously.
4282 const glsl_type
*const ret_type
=
4283 (ret
== NULL
) ? glsl_type::void_type
: ret
->type
;
4285 /* Implicit conversions are not allowed for return values prior to
4286 * ARB_shading_language_420pack.
4288 if (state
->current_function
->return_type
!= ret_type
) {
4289 YYLTYPE loc
= this->get_location();
4291 if (state
->ARB_shading_language_420pack_enable
) {
4292 if (!apply_implicit_conversion(state
->current_function
->return_type
,
4294 _mesa_glsl_error(& loc
, state
,
4295 "could not implicitly convert return value "
4296 "to %s, in function `%s'",
4297 state
->current_function
->return_type
->name
,
4298 state
->current_function
->function_name());
4301 _mesa_glsl_error(& loc
, state
,
4302 "`return' with wrong type %s, in function `%s' "
4305 state
->current_function
->function_name(),
4306 state
->current_function
->return_type
->name
);
4308 } else if (state
->current_function
->return_type
->base_type
==
4310 YYLTYPE loc
= this->get_location();
4312 /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
4313 * specs add a clarification:
4315 * "A void function can only use return without a return argument, even if
4316 * the return argument has void type. Return statements only accept values:
4319 * void func2() { return func1(); } // illegal return statement"
4321 _mesa_glsl_error(& loc
, state
,
4322 "void functions can only use `return' without a "
4326 inst
= new(ctx
) ir_return(ret
);
4328 if (state
->current_function
->return_type
->base_type
!=
4330 YYLTYPE loc
= this->get_location();
4332 _mesa_glsl_error(& loc
, state
,
4333 "`return' with no value, in function %s returning "
4335 state
->current_function
->function_name());
4337 inst
= new(ctx
) ir_return
;
4340 state
->found_return
= true;
4341 instructions
->push_tail(inst
);
4346 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
4347 YYLTYPE loc
= this->get_location();
4349 _mesa_glsl_error(& loc
, state
,
4350 "`discard' may only appear in a fragment shader");
4352 instructions
->push_tail(new(ctx
) ir_discard
);
4357 if (mode
== ast_continue
&&
4358 state
->loop_nesting_ast
== NULL
) {
4359 YYLTYPE loc
= this->get_location();
4361 _mesa_glsl_error(& loc
, state
, "continue may only appear in a loop");
4362 } else if (mode
== ast_break
&&
4363 state
->loop_nesting_ast
== NULL
&&
4364 state
->switch_state
.switch_nesting_ast
== NULL
) {
4365 YYLTYPE loc
= this->get_location();
4367 _mesa_glsl_error(& loc
, state
,
4368 "break may only appear in a loop or a switch");
4370 /* For a loop, inline the for loop expression again, since we don't
4371 * know where near the end of the loop body the normal copy of it is
4372 * going to be placed. Same goes for the condition for a do-while
4375 if (state
->loop_nesting_ast
!= NULL
&&
4376 mode
== ast_continue
&& !state
->switch_state
.is_switch_innermost
) {
4377 if (state
->loop_nesting_ast
->rest_expression
) {
4378 state
->loop_nesting_ast
->rest_expression
->hir(instructions
,
4381 if (state
->loop_nesting_ast
->mode
==
4382 ast_iteration_statement::ast_do_while
) {
4383 state
->loop_nesting_ast
->condition_to_hir(instructions
, state
);
4387 if (state
->switch_state
.is_switch_innermost
&&
4388 mode
== ast_continue
) {
4389 /* Set 'continue_inside' to true. */
4390 ir_rvalue
*const true_val
= new (ctx
) ir_constant(true);
4391 ir_dereference_variable
*deref_continue_inside_var
=
4392 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
4393 instructions
->push_tail(new(ctx
) ir_assignment(deref_continue_inside_var
,
4396 /* Break out from the switch, continue for the loop will
4397 * be called right after switch. */
4398 ir_loop_jump
*const jump
=
4399 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
4400 instructions
->push_tail(jump
);
4402 } else if (state
->switch_state
.is_switch_innermost
&&
4403 mode
== ast_break
) {
4404 /* Force break out of switch by inserting a break. */
4405 ir_loop_jump
*const jump
=
4406 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
4407 instructions
->push_tail(jump
);
4409 ir_loop_jump
*const jump
=
4410 new(ctx
) ir_loop_jump((mode
== ast_break
)
4411 ? ir_loop_jump::jump_break
4412 : ir_loop_jump::jump_continue
);
4413 instructions
->push_tail(jump
);
4420 /* Jump instructions do not have r-values.
4427 ast_selection_statement::hir(exec_list
*instructions
,
4428 struct _mesa_glsl_parse_state
*state
)
4432 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
4434 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
4436 * "Any expression whose type evaluates to a Boolean can be used as the
4437 * conditional expression bool-expression. Vector types are not accepted
4438 * as the expression to if."
4440 * The checks are separated so that higher quality diagnostics can be
4441 * generated for cases where both rules are violated.
4443 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
4444 YYLTYPE loc
= this->condition
->get_location();
4446 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
4450 ir_if
*const stmt
= new(ctx
) ir_if(condition
);
4452 if (then_statement
!= NULL
) {
4453 state
->symbols
->push_scope();
4454 then_statement
->hir(& stmt
->then_instructions
, state
);
4455 state
->symbols
->pop_scope();
4458 if (else_statement
!= NULL
) {
4459 state
->symbols
->push_scope();
4460 else_statement
->hir(& stmt
->else_instructions
, state
);
4461 state
->symbols
->pop_scope();
4464 instructions
->push_tail(stmt
);
4466 /* if-statements do not have r-values.
4473 ast_switch_statement::hir(exec_list
*instructions
,
4474 struct _mesa_glsl_parse_state
*state
)
4478 ir_rvalue
*const test_expression
=
4479 this->test_expression
->hir(instructions
, state
);
4481 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
4483 * "The type of init-expression in a switch statement must be a
4486 if (!test_expression
->type
->is_scalar() ||
4487 !test_expression
->type
->is_integer()) {
4488 YYLTYPE loc
= this->test_expression
->get_location();
4490 _mesa_glsl_error(& loc
,
4492 "switch-statement expression must be scalar "
4496 /* Track the switch-statement nesting in a stack-like manner.
4498 struct glsl_switch_state saved
= state
->switch_state
;
4500 state
->switch_state
.is_switch_innermost
= true;
4501 state
->switch_state
.switch_nesting_ast
= this;
4502 state
->switch_state
.labels_ht
= hash_table_ctor(0, hash_table_pointer_hash
,
4503 hash_table_pointer_compare
);
4504 state
->switch_state
.previous_default
= NULL
;
4506 /* Initalize is_fallthru state to false.
4508 ir_rvalue
*const is_fallthru_val
= new (ctx
) ir_constant(false);
4509 state
->switch_state
.is_fallthru_var
=
4510 new(ctx
) ir_variable(glsl_type::bool_type
,
4511 "switch_is_fallthru_tmp",
4513 instructions
->push_tail(state
->switch_state
.is_fallthru_var
);
4515 ir_dereference_variable
*deref_is_fallthru_var
=
4516 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4517 instructions
->push_tail(new(ctx
) ir_assignment(deref_is_fallthru_var
,
4520 /* Initialize continue_inside state to false.
4522 state
->switch_state
.continue_inside
=
4523 new(ctx
) ir_variable(glsl_type::bool_type
,
4524 "continue_inside_tmp",
4526 instructions
->push_tail(state
->switch_state
.continue_inside
);
4528 ir_rvalue
*const false_val
= new (ctx
) ir_constant(false);
4529 ir_dereference_variable
*deref_continue_inside_var
=
4530 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
4531 instructions
->push_tail(new(ctx
) ir_assignment(deref_continue_inside_var
,
4534 state
->switch_state
.run_default
=
4535 new(ctx
) ir_variable(glsl_type::bool_type
,
4538 instructions
->push_tail(state
->switch_state
.run_default
);
4540 /* Loop around the switch is used for flow control. */
4541 ir_loop
* loop
= new(ctx
) ir_loop();
4542 instructions
->push_tail(loop
);
4544 /* Cache test expression.
4546 test_to_hir(&loop
->body_instructions
, state
);
4548 /* Emit code for body of switch stmt.
4550 body
->hir(&loop
->body_instructions
, state
);
4552 /* Insert a break at the end to exit loop. */
4553 ir_loop_jump
*jump
= new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
4554 loop
->body_instructions
.push_tail(jump
);
4556 /* If we are inside loop, check if continue got called inside switch. */
4557 if (state
->loop_nesting_ast
!= NULL
) {
4558 ir_dereference_variable
*deref_continue_inside
=
4559 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
4560 ir_if
*irif
= new(ctx
) ir_if(deref_continue_inside
);
4561 ir_loop_jump
*jump
= new(ctx
) ir_loop_jump(ir_loop_jump::jump_continue
);
4563 if (state
->loop_nesting_ast
!= NULL
) {
4564 if (state
->loop_nesting_ast
->rest_expression
) {
4565 state
->loop_nesting_ast
->rest_expression
->hir(&irif
->then_instructions
,
4568 if (state
->loop_nesting_ast
->mode
==
4569 ast_iteration_statement::ast_do_while
) {
4570 state
->loop_nesting_ast
->condition_to_hir(&irif
->then_instructions
, state
);
4573 irif
->then_instructions
.push_tail(jump
);
4574 instructions
->push_tail(irif
);
4577 hash_table_dtor(state
->switch_state
.labels_ht
);
4579 state
->switch_state
= saved
;
4581 /* Switch statements do not have r-values. */
4587 ast_switch_statement::test_to_hir(exec_list
*instructions
,
4588 struct _mesa_glsl_parse_state
*state
)
4592 /* Cache value of test expression. */
4593 ir_rvalue
*const test_val
=
4594 test_expression
->hir(instructions
,
4597 state
->switch_state
.test_var
= new(ctx
) ir_variable(test_val
->type
,
4600 ir_dereference_variable
*deref_test_var
=
4601 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
4603 instructions
->push_tail(state
->switch_state
.test_var
);
4604 instructions
->push_tail(new(ctx
) ir_assignment(deref_test_var
, test_val
));
4609 ast_switch_body::hir(exec_list
*instructions
,
4610 struct _mesa_glsl_parse_state
*state
)
4613 stmts
->hir(instructions
, state
);
4615 /* Switch bodies do not have r-values. */
4620 ast_case_statement_list::hir(exec_list
*instructions
,
4621 struct _mesa_glsl_parse_state
*state
)
4623 exec_list default_case
, after_default
, tmp
;
4625 foreach_list_typed (ast_case_statement
, case_stmt
, link
, & this->cases
) {
4626 case_stmt
->hir(&tmp
, state
);
4629 if (state
->switch_state
.previous_default
&& default_case
.is_empty()) {
4630 default_case
.append_list(&tmp
);
4634 /* If default case found, append 'after_default' list. */
4635 if (!default_case
.is_empty())
4636 after_default
.append_list(&tmp
);
4638 instructions
->append_list(&tmp
);
4641 /* Handle the default case. This is done here because default might not be
4642 * the last case. We need to add checks against following cases first to see
4643 * if default should be chosen or not.
4645 if (!default_case
.is_empty()) {
4647 ir_rvalue
*const true_val
= new (state
) ir_constant(true);
4648 ir_dereference_variable
*deref_run_default_var
=
4649 new(state
) ir_dereference_variable(state
->switch_state
.run_default
);
4651 /* Choose to run default case initially, following conditional
4652 * assignments might change this.
4654 ir_assignment
*const init_var
=
4655 new(state
) ir_assignment(deref_run_default_var
, true_val
);
4656 instructions
->push_tail(init_var
);
4658 /* Default case was the last one, no checks required. */
4659 if (after_default
.is_empty()) {
4660 instructions
->append_list(&default_case
);
4664 foreach_in_list(ir_instruction
, ir
, &after_default
) {
4665 ir_assignment
*assign
= ir
->as_assignment();
4670 /* Clone the check between case label and init expression. */
4671 ir_expression
*exp
= (ir_expression
*) assign
->condition
;
4672 ir_expression
*clone
= exp
->clone(state
, NULL
);
4674 ir_dereference_variable
*deref_var
=
4675 new(state
) ir_dereference_variable(state
->switch_state
.run_default
);
4676 ir_rvalue
*const false_val
= new (state
) ir_constant(false);
4678 ir_assignment
*const set_false
=
4679 new(state
) ir_assignment(deref_var
, false_val
, clone
);
4681 instructions
->push_tail(set_false
);
4684 /* Append default case and all cases after it. */
4685 instructions
->append_list(&default_case
);
4686 instructions
->append_list(&after_default
);
4689 /* Case statements do not have r-values. */
4694 ast_case_statement::hir(exec_list
*instructions
,
4695 struct _mesa_glsl_parse_state
*state
)
4697 labels
->hir(instructions
, state
);
4699 /* Guard case statements depending on fallthru state. */
4700 ir_dereference_variable
*const deref_fallthru_guard
=
4701 new(state
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4702 ir_if
*const test_fallthru
= new(state
) ir_if(deref_fallthru_guard
);
4704 foreach_list_typed (ast_node
, stmt
, link
, & this->stmts
)
4705 stmt
->hir(& test_fallthru
->then_instructions
, state
);
4707 instructions
->push_tail(test_fallthru
);
4709 /* Case statements do not have r-values. */
4715 ast_case_label_list::hir(exec_list
*instructions
,
4716 struct _mesa_glsl_parse_state
*state
)
4718 foreach_list_typed (ast_case_label
, label
, link
, & this->labels
)
4719 label
->hir(instructions
, state
);
4721 /* Case labels do not have r-values. */
4726 ast_case_label::hir(exec_list
*instructions
,
4727 struct _mesa_glsl_parse_state
*state
)
4731 ir_dereference_variable
*deref_fallthru_var
=
4732 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4734 ir_rvalue
*const true_val
= new(ctx
) ir_constant(true);
4736 /* If not default case, ... */
4737 if (this->test_value
!= NULL
) {
4738 /* Conditionally set fallthru state based on
4739 * comparison of cached test expression value to case label.
4741 ir_rvalue
*const label_rval
= this->test_value
->hir(instructions
, state
);
4742 ir_constant
*label_const
= label_rval
->constant_expression_value();
4745 YYLTYPE loc
= this->test_value
->get_location();
4747 _mesa_glsl_error(& loc
, state
,
4748 "switch statement case label must be a "
4749 "constant expression");
4751 /* Stuff a dummy value in to allow processing to continue. */
4752 label_const
= new(ctx
) ir_constant(0);
4754 ast_expression
*previous_label
= (ast_expression
*)
4755 hash_table_find(state
->switch_state
.labels_ht
,
4756 (void *)(uintptr_t)label_const
->value
.u
[0]);
4758 if (previous_label
) {
4759 YYLTYPE loc
= this->test_value
->get_location();
4760 _mesa_glsl_error(& loc
, state
, "duplicate case value");
4762 loc
= previous_label
->get_location();
4763 _mesa_glsl_error(& loc
, state
, "this is the previous case label");
4765 hash_table_insert(state
->switch_state
.labels_ht
,
4767 (void *)(uintptr_t)label_const
->value
.u
[0]);
4771 ir_dereference_variable
*deref_test_var
=
4772 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
4774 ir_expression
*test_cond
= new(ctx
) ir_expression(ir_binop_all_equal
,
4779 * From GLSL 4.40 specification section 6.2 ("Selection"):
4781 * "The type of the init-expression value in a switch statement must
4782 * be a scalar int or uint. The type of the constant-expression value
4783 * in a case label also must be a scalar int or uint. When any pair
4784 * of these values is tested for "equal value" and the types do not
4785 * match, an implicit conversion will be done to convert the int to a
4786 * uint (see section 4.1.10 “Implicit Conversions”) before the compare
4789 if (label_const
->type
!= state
->switch_state
.test_var
->type
) {
4790 YYLTYPE loc
= this->test_value
->get_location();
4792 const glsl_type
*type_a
= label_const
->type
;
4793 const glsl_type
*type_b
= state
->switch_state
.test_var
->type
;
4795 /* Check if int->uint implicit conversion is supported. */
4796 bool integer_conversion_supported
=
4797 glsl_type::int_type
->can_implicitly_convert_to(glsl_type::uint_type
,
4800 if ((!type_a
->is_integer() || !type_b
->is_integer()) ||
4801 !integer_conversion_supported
) {
4802 _mesa_glsl_error(&loc
, state
, "type mismatch with switch "
4803 "init-expression and case label (%s != %s)",
4804 type_a
->name
, type_b
->name
);
4806 /* Conversion of the case label. */
4807 if (type_a
->base_type
== GLSL_TYPE_INT
) {
4808 if (!apply_implicit_conversion(glsl_type::uint_type
,
4809 test_cond
->operands
[0], state
))
4810 _mesa_glsl_error(&loc
, state
, "implicit type conversion error");
4812 /* Conversion of the init-expression value. */
4813 if (!apply_implicit_conversion(glsl_type::uint_type
,
4814 test_cond
->operands
[1], state
))
4815 _mesa_glsl_error(&loc
, state
, "implicit type conversion error");
4820 ir_assignment
*set_fallthru_on_test
=
4821 new(ctx
) ir_assignment(deref_fallthru_var
, true_val
, test_cond
);
4823 instructions
->push_tail(set_fallthru_on_test
);
4824 } else { /* default case */
4825 if (state
->switch_state
.previous_default
) {
4826 YYLTYPE loc
= this->get_location();
4827 _mesa_glsl_error(& loc
, state
,
4828 "multiple default labels in one switch");
4830 loc
= state
->switch_state
.previous_default
->get_location();
4831 _mesa_glsl_error(& loc
, state
, "this is the first default label");
4833 state
->switch_state
.previous_default
= this;
4835 /* Set fallthru condition on 'run_default' bool. */
4836 ir_dereference_variable
*deref_run_default
=
4837 new(ctx
) ir_dereference_variable(state
->switch_state
.run_default
);
4838 ir_rvalue
*const cond_true
= new(ctx
) ir_constant(true);
4839 ir_expression
*test_cond
= new(ctx
) ir_expression(ir_binop_all_equal
,
4843 /* Set falltrhu state. */
4844 ir_assignment
*set_fallthru
=
4845 new(ctx
) ir_assignment(deref_fallthru_var
, true_val
, test_cond
);
4847 instructions
->push_tail(set_fallthru
);
4850 /* Case statements do not have r-values. */
4855 ast_iteration_statement::condition_to_hir(exec_list
*instructions
,
4856 struct _mesa_glsl_parse_state
*state
)
4860 if (condition
!= NULL
) {
4861 ir_rvalue
*const cond
=
4862 condition
->hir(instructions
, state
);
4865 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
4866 YYLTYPE loc
= condition
->get_location();
4868 _mesa_glsl_error(& loc
, state
,
4869 "loop condition must be scalar boolean");
4871 /* As the first code in the loop body, generate a block that looks
4872 * like 'if (!condition) break;' as the loop termination condition.
4874 ir_rvalue
*const not_cond
=
4875 new(ctx
) ir_expression(ir_unop_logic_not
, cond
);
4877 ir_if
*const if_stmt
= new(ctx
) ir_if(not_cond
);
4879 ir_jump
*const break_stmt
=
4880 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
4882 if_stmt
->then_instructions
.push_tail(break_stmt
);
4883 instructions
->push_tail(if_stmt
);
4890 ast_iteration_statement::hir(exec_list
*instructions
,
4891 struct _mesa_glsl_parse_state
*state
)
4895 /* For-loops and while-loops start a new scope, but do-while loops do not.
4897 if (mode
!= ast_do_while
)
4898 state
->symbols
->push_scope();
4900 if (init_statement
!= NULL
)
4901 init_statement
->hir(instructions
, state
);
4903 ir_loop
*const stmt
= new(ctx
) ir_loop();
4904 instructions
->push_tail(stmt
);
4906 /* Track the current loop nesting. */
4907 ast_iteration_statement
*nesting_ast
= state
->loop_nesting_ast
;
4909 state
->loop_nesting_ast
= this;
4911 /* Likewise, indicate that following code is closest to a loop,
4912 * NOT closest to a switch.
4914 bool saved_is_switch_innermost
= state
->switch_state
.is_switch_innermost
;
4915 state
->switch_state
.is_switch_innermost
= false;
4917 if (mode
!= ast_do_while
)
4918 condition_to_hir(&stmt
->body_instructions
, state
);
4921 body
->hir(& stmt
->body_instructions
, state
);
4923 if (rest_expression
!= NULL
)
4924 rest_expression
->hir(& stmt
->body_instructions
, state
);
4926 if (mode
== ast_do_while
)
4927 condition_to_hir(&stmt
->body_instructions
, state
);
4929 if (mode
!= ast_do_while
)
4930 state
->symbols
->pop_scope();
4932 /* Restore previous nesting before returning. */
4933 state
->loop_nesting_ast
= nesting_ast
;
4934 state
->switch_state
.is_switch_innermost
= saved_is_switch_innermost
;
4936 /* Loops do not have r-values.
4943 * Determine if the given type is valid for establishing a default precision
4946 * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
4948 * "The precision statement
4950 * precision precision-qualifier type;
4952 * can be used to establish a default precision qualifier. The type field
4953 * can be either int or float or any of the sampler types, and the
4954 * precision-qualifier can be lowp, mediump, or highp."
4956 * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
4957 * qualifiers on sampler types, but this seems like an oversight (since the
4958 * intention of including these in GLSL 1.30 is to allow compatibility with ES
4959 * shaders). So we allow int, float, and all sampler types regardless of GLSL
4963 is_valid_default_precision_type(const struct glsl_type
*const type
)
4968 switch (type
->base_type
) {
4970 case GLSL_TYPE_FLOAT
:
4971 /* "int" and "float" are valid, but vectors and matrices are not. */
4972 return type
->vector_elements
== 1 && type
->matrix_columns
== 1;
4973 case GLSL_TYPE_SAMPLER
:
4982 ast_type_specifier::hir(exec_list
*instructions
,
4983 struct _mesa_glsl_parse_state
*state
)
4985 if (this->default_precision
== ast_precision_none
&& this->structure
== NULL
)
4988 YYLTYPE loc
= this->get_location();
4990 /* If this is a precision statement, check that the type to which it is
4991 * applied is either float or int.
4993 * From section 4.5.3 of the GLSL 1.30 spec:
4994 * "The precision statement
4995 * precision precision-qualifier type;
4996 * can be used to establish a default precision qualifier. The type
4997 * field can be either int or float [...]. Any other types or
4998 * qualifiers will result in an error.
5000 if (this->default_precision
!= ast_precision_none
) {
5001 if (!state
->check_precision_qualifiers_allowed(&loc
))
5004 if (this->structure
!= NULL
) {
5005 _mesa_glsl_error(&loc
, state
,
5006 "precision qualifiers do not apply to structures");
5010 if (this->array_specifier
!= NULL
) {
5011 _mesa_glsl_error(&loc
, state
,
5012 "default precision statements do not apply to "
5017 const struct glsl_type
*const type
=
5018 state
->symbols
->get_type(this->type_name
);
5019 if (!is_valid_default_precision_type(type
)) {
5020 _mesa_glsl_error(&loc
, state
,
5021 "default precision statements apply only to "
5022 "float, int, and sampler types");
5026 if (type
->base_type
== GLSL_TYPE_FLOAT
5028 && state
->stage
== MESA_SHADER_FRAGMENT
) {
5029 /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
5032 * "The fragment language has no default precision qualifier for
5033 * floating point types."
5035 * As a result, we have to track whether or not default precision has
5036 * been specified for float in GLSL ES fragment shaders.
5038 * Earlier in that same section, the spec says:
5040 * "Non-precision qualified declarations will use the precision
5041 * qualifier specified in the most recent precision statement
5042 * that is still in scope. The precision statement has the same
5043 * scoping rules as variable declarations. If it is declared
5044 * inside a compound statement, its effect stops at the end of
5045 * the innermost statement it was declared in. Precision
5046 * statements in nested scopes override precision statements in
5047 * outer scopes. Multiple precision statements for the same basic
5048 * type can appear inside the same scope, with later statements
5049 * overriding earlier statements within that scope."
5051 * Default precision specifications follow the same scope rules as
5052 * variables. So, we can track the state of the default float
5053 * precision in the symbol table, and the rules will just work. This
5054 * is a slight abuse of the symbol table, but it has the semantics
5057 ir_variable
*const junk
=
5058 new(state
) ir_variable(type
, "#default precision",
5061 state
->symbols
->add_variable(junk
);
5064 /* FINISHME: Translate precision statements into IR. */
5068 /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
5069 * process_record_constructor() can do type-checking on C-style initializer
5070 * expressions of structs, but ast_struct_specifier should only be translated
5071 * to HIR if it is declaring the type of a structure.
5073 * The ->is_declaration field is false for initializers of variables
5074 * declared separately from the struct's type definition.
5076 * struct S { ... }; (is_declaration = true)
5077 * struct T { ... } t = { ... }; (is_declaration = true)
5078 * S s = { ... }; (is_declaration = false)
5080 if (this->structure
!= NULL
&& this->structure
->is_declaration
)
5081 return this->structure
->hir(instructions
, state
);
5088 * Process a structure or interface block tree into an array of structure fields
5090 * After parsing, where there are some syntax differnces, structures and
5091 * interface blocks are almost identical. They are similar enough that the
5092 * AST for each can be processed the same way into a set of
5093 * \c glsl_struct_field to describe the members.
5095 * If we're processing an interface block, var_mode should be the type of the
5096 * interface block (ir_var_shader_in, ir_var_shader_out, or ir_var_uniform).
5097 * If we're processing a structure, var_mode should be ir_var_auto.
5100 * The number of fields processed. A pointer to the array structure fields is
5101 * stored in \c *fields_ret.
5104 ast_process_structure_or_interface_block(exec_list
*instructions
,
5105 struct _mesa_glsl_parse_state
*state
,
5106 exec_list
*declarations
,
5108 glsl_struct_field
**fields_ret
,
5110 enum glsl_matrix_layout matrix_layout
,
5111 bool allow_reserved_names
,
5112 ir_variable_mode var_mode
)
5114 unsigned decl_count
= 0;
5116 /* Make an initial pass over the list of fields to determine how
5117 * many there are. Each element in this list is an ast_declarator_list.
5118 * This means that we actually need to count the number of elements in the
5119 * 'declarations' list in each of the elements.
5121 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
5122 decl_count
+= decl_list
->declarations
.length();
5125 /* Allocate storage for the fields and process the field
5126 * declarations. As the declarations are processed, try to also convert
5127 * the types to HIR. This ensures that structure definitions embedded in
5128 * other structure definitions or in interface blocks are processed.
5130 glsl_struct_field
*const fields
= ralloc_array(state
, glsl_struct_field
,
5134 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
5135 const char *type_name
;
5137 decl_list
->type
->specifier
->hir(instructions
, state
);
5139 /* Section 10.9 of the GLSL ES 1.00 specification states that
5140 * embedded structure definitions have been removed from the language.
5142 if (state
->es_shader
&& decl_list
->type
->specifier
->structure
!= NULL
) {
5143 _mesa_glsl_error(&loc
, state
, "embedded structure definitions are "
5144 "not allowed in GLSL ES 1.00");
5147 const glsl_type
*decl_type
=
5148 decl_list
->type
->glsl_type(& type_name
, state
);
5150 foreach_list_typed (ast_declaration
, decl
, link
,
5151 &decl_list
->declarations
) {
5152 if (!allow_reserved_names
)
5153 validate_identifier(decl
->identifier
, loc
, state
);
5155 /* From section 4.3.9 of the GLSL 4.40 spec:
5157 * "[In interface blocks] opaque types are not allowed."
5159 * It should be impossible for decl_type to be NULL here. Cases that
5160 * might naturally lead to decl_type being NULL, especially for the
5161 * is_interface case, will have resulted in compilation having
5162 * already halted due to a syntax error.
5164 const struct glsl_type
*field_type
=
5165 decl_type
!= NULL
? decl_type
: glsl_type::error_type
;
5167 if (is_interface
&& field_type
->contains_opaque()) {
5168 YYLTYPE loc
= decl_list
->get_location();
5169 _mesa_glsl_error(&loc
, state
,
5170 "uniform in non-default uniform block contains "
5174 if (field_type
->contains_atomic()) {
5175 /* FINISHME: Add a spec quotation here once updated spec
5176 * FINISHME: language is available. See Khronos bug #10903
5177 * FINISHME: on whether atomic counters are allowed in
5178 * FINISHME: structures.
5180 YYLTYPE loc
= decl_list
->get_location();
5181 _mesa_glsl_error(&loc
, state
, "atomic counter in structure or "
5185 if (field_type
->contains_image()) {
5186 /* FINISHME: Same problem as with atomic counters.
5187 * FINISHME: Request clarification from Khronos and add
5188 * FINISHME: spec quotation here.
5190 YYLTYPE loc
= decl_list
->get_location();
5191 _mesa_glsl_error(&loc
, state
,
5192 "image in structure or uniform block");
5195 const struct ast_type_qualifier
*const qual
=
5196 & decl_list
->type
->qualifier
;
5197 if (qual
->flags
.q
.std140
||
5198 qual
->flags
.q
.packed
||
5199 qual
->flags
.q
.shared
) {
5200 _mesa_glsl_error(&loc
, state
,
5201 "uniform block layout qualifiers std140, packed, and "
5202 "shared can only be applied to uniform blocks, not "
5206 field_type
= process_array_type(&loc
, decl_type
,
5207 decl
->array_specifier
, state
);
5208 fields
[i
].type
= field_type
;
5209 fields
[i
].name
= decl
->identifier
;
5210 fields
[i
].location
= -1;
5211 fields
[i
].interpolation
=
5212 interpret_interpolation_qualifier(qual
, var_mode
, state
, &loc
);
5213 fields
[i
].centroid
= qual
->flags
.q
.centroid
? 1 : 0;
5214 fields
[i
].sample
= qual
->flags
.q
.sample
? 1 : 0;
5216 /* Only save explicitly defined streams in block's field */
5217 fields
[i
].stream
= qual
->flags
.q
.explicit_stream
? qual
->stream
: -1;
5219 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
5220 if (!qual
->flags
.q
.uniform
) {
5221 _mesa_glsl_error(&loc
, state
,
5222 "row_major and column_major can only be "
5223 "applied to uniform interface blocks");
5225 validate_matrix_layout_for_type(state
, &loc
, field_type
, NULL
);
5228 if (qual
->flags
.q
.uniform
&& qual
->has_interpolation()) {
5229 _mesa_glsl_error(&loc
, state
,
5230 "interpolation qualifiers cannot be used "
5231 "with uniform interface blocks");
5234 if ((qual
->flags
.q
.uniform
|| !is_interface
) &&
5235 qual
->has_auxiliary_storage()) {
5236 _mesa_glsl_error(&loc
, state
,
5237 "auxiliary storage qualifiers cannot be used "
5238 "in uniform blocks or structures.");
5241 /* Propogate row- / column-major information down the fields of the
5242 * structure or interface block. Structures need this data because
5243 * the structure may contain a structure that contains ... a matrix
5244 * that need the proper layout.
5246 if (field_type
->without_array()->is_matrix()
5247 || field_type
->without_array()->is_record()) {
5248 /* If no layout is specified for the field, inherit the layout
5251 fields
[i
].matrix_layout
= matrix_layout
;
5253 if (qual
->flags
.q
.row_major
)
5254 fields
[i
].matrix_layout
= GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
5255 else if (qual
->flags
.q
.column_major
)
5256 fields
[i
].matrix_layout
= GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
;
5258 /* If we're processing an interface block, the matrix layout must
5259 * be decided by this point.
5261 assert(!is_interface
5262 || fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
5263 || fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
);
5270 assert(i
== decl_count
);
5272 *fields_ret
= fields
;
5278 ast_struct_specifier::hir(exec_list
*instructions
,
5279 struct _mesa_glsl_parse_state
*state
)
5281 YYLTYPE loc
= this->get_location();
5283 /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
5285 * "Anonymous structures are not supported; so embedded structures must
5286 * have a declarator. A name given to an embedded struct is scoped at
5287 * the same level as the struct it is embedded in."
5289 * The same section of the GLSL 1.20 spec says:
5291 * "Anonymous structures are not supported. Embedded structures are not
5294 * struct S { float f; };
5296 * S; // Error: anonymous structures disallowed
5297 * struct { ... }; // Error: embedded structures disallowed
5298 * S s; // Okay: nested structures with name are allowed
5301 * The GLSL ES 1.00 and 3.00 specs have similar langauge and examples. So,
5302 * we allow embedded structures in 1.10 only.
5304 if (state
->language_version
!= 110 && state
->struct_specifier_depth
!= 0)
5305 _mesa_glsl_error(&loc
, state
,
5306 "embedded structure declarations are not allowed");
5308 state
->struct_specifier_depth
++;
5310 glsl_struct_field
*fields
;
5311 unsigned decl_count
=
5312 ast_process_structure_or_interface_block(instructions
,
5314 &this->declarations
,
5318 GLSL_MATRIX_LAYOUT_INHERITED
,
5319 false /* allow_reserved_names */,
5322 validate_identifier(this->name
, loc
, state
);
5324 const glsl_type
*t
=
5325 glsl_type::get_record_instance(fields
, decl_count
, this->name
);
5327 if (!state
->symbols
->add_type(name
, t
)) {
5328 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
5330 const glsl_type
**s
= reralloc(state
, state
->user_structures
,
5332 state
->num_user_structures
+ 1);
5334 s
[state
->num_user_structures
] = t
;
5335 state
->user_structures
= s
;
5336 state
->num_user_structures
++;
5340 state
->struct_specifier_depth
--;
5342 /* Structure type definitions do not have r-values.
5349 * Visitor class which detects whether a given interface block has been used.
5351 class interface_block_usage_visitor
: public ir_hierarchical_visitor
5354 interface_block_usage_visitor(ir_variable_mode mode
, const glsl_type
*block
)
5355 : mode(mode
), block(block
), found(false)
5359 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
5361 if (ir
->var
->data
.mode
== mode
&& ir
->var
->get_interface_type() == block
) {
5365 return visit_continue
;
5368 bool usage_found() const
5374 ir_variable_mode mode
;
5375 const glsl_type
*block
;
5381 ast_interface_block::hir(exec_list
*instructions
,
5382 struct _mesa_glsl_parse_state
*state
)
5384 YYLTYPE loc
= this->get_location();
5386 /* The ast_interface_block has a list of ast_declarator_lists. We
5387 * need to turn those into ir_variables with an association
5388 * with this uniform block.
5390 enum glsl_interface_packing packing
;
5391 if (this->layout
.flags
.q
.shared
) {
5392 packing
= GLSL_INTERFACE_PACKING_SHARED
;
5393 } else if (this->layout
.flags
.q
.packed
) {
5394 packing
= GLSL_INTERFACE_PACKING_PACKED
;
5396 /* The default layout is std140.
5398 packing
= GLSL_INTERFACE_PACKING_STD140
;
5401 ir_variable_mode var_mode
;
5402 const char *iface_type_name
;
5403 if (this->layout
.flags
.q
.in
) {
5404 var_mode
= ir_var_shader_in
;
5405 iface_type_name
= "in";
5406 } else if (this->layout
.flags
.q
.out
) {
5407 var_mode
= ir_var_shader_out
;
5408 iface_type_name
= "out";
5409 } else if (this->layout
.flags
.q
.uniform
) {
5410 var_mode
= ir_var_uniform
;
5411 iface_type_name
= "uniform";
5413 var_mode
= ir_var_auto
;
5414 iface_type_name
= "UNKNOWN";
5415 assert(!"interface block layout qualifier not found!");
5418 enum glsl_matrix_layout matrix_layout
= GLSL_MATRIX_LAYOUT_INHERITED
;
5419 if (this->layout
.flags
.q
.row_major
)
5420 matrix_layout
= GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
5421 else if (this->layout
.flags
.q
.column_major
)
5422 matrix_layout
= GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
;
5424 bool redeclaring_per_vertex
= strcmp(this->block_name
, "gl_PerVertex") == 0;
5425 exec_list declared_variables
;
5426 glsl_struct_field
*fields
;
5428 /* Treat an interface block as one level of nesting, so that embedded struct
5429 * specifiers will be disallowed.
5431 state
->struct_specifier_depth
++;
5433 unsigned int num_variables
=
5434 ast_process_structure_or_interface_block(&declared_variables
,
5436 &this->declarations
,
5441 redeclaring_per_vertex
,
5444 state
->struct_specifier_depth
--;
5446 if (!redeclaring_per_vertex
)
5447 validate_identifier(this->block_name
, loc
, state
);
5449 const glsl_type
*earlier_per_vertex
= NULL
;
5450 if (redeclaring_per_vertex
) {
5451 /* Find the previous declaration of gl_PerVertex. If we're redeclaring
5452 * the named interface block gl_in, we can find it by looking at the
5453 * previous declaration of gl_in. Otherwise we can find it by looking
5454 * at the previous decalartion of any of the built-in outputs,
5457 * Also check that the instance name and array-ness of the redeclaration
5461 case ir_var_shader_in
:
5462 if (ir_variable
*earlier_gl_in
=
5463 state
->symbols
->get_variable("gl_in")) {
5464 earlier_per_vertex
= earlier_gl_in
->get_interface_type();
5466 _mesa_glsl_error(&loc
, state
,
5467 "redeclaration of gl_PerVertex input not allowed "
5469 _mesa_shader_stage_to_string(state
->stage
));
5471 if (this->instance_name
== NULL
||
5472 strcmp(this->instance_name
, "gl_in") != 0 || this->array_specifier
== NULL
) {
5473 _mesa_glsl_error(&loc
, state
,
5474 "gl_PerVertex input must be redeclared as "
5478 case ir_var_shader_out
:
5479 if (ir_variable
*earlier_gl_Position
=
5480 state
->symbols
->get_variable("gl_Position")) {
5481 earlier_per_vertex
= earlier_gl_Position
->get_interface_type();
5483 _mesa_glsl_error(&loc
, state
,
5484 "redeclaration of gl_PerVertex output not "
5485 "allowed in the %s shader",
5486 _mesa_shader_stage_to_string(state
->stage
));
5488 if (this->instance_name
!= NULL
) {
5489 _mesa_glsl_error(&loc
, state
,
5490 "gl_PerVertex output may not be redeclared with "
5491 "an instance name");
5495 _mesa_glsl_error(&loc
, state
,
5496 "gl_PerVertex must be declared as an input or an "
5501 if (earlier_per_vertex
== NULL
) {
5502 /* An error has already been reported. Bail out to avoid null
5503 * dereferences later in this function.
5508 /* Copy locations from the old gl_PerVertex interface block. */
5509 for (unsigned i
= 0; i
< num_variables
; i
++) {
5510 int j
= earlier_per_vertex
->field_index(fields
[i
].name
);
5512 _mesa_glsl_error(&loc
, state
,
5513 "redeclaration of gl_PerVertex must be a subset "
5514 "of the built-in members of gl_PerVertex");
5516 fields
[i
].location
=
5517 earlier_per_vertex
->fields
.structure
[j
].location
;
5518 fields
[i
].interpolation
=
5519 earlier_per_vertex
->fields
.structure
[j
].interpolation
;
5520 fields
[i
].centroid
=
5521 earlier_per_vertex
->fields
.structure
[j
].centroid
;
5523 earlier_per_vertex
->fields
.structure
[j
].sample
;
5527 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10
5530 * If a built-in interface block is redeclared, it must appear in
5531 * the shader before any use of any member included in the built-in
5532 * declaration, or a compilation error will result.
5534 * This appears to be a clarification to the behaviour established for
5535 * gl_PerVertex by GLSL 1.50, therefore we implement this behaviour
5536 * regardless of GLSL version.
5538 interface_block_usage_visitor
v(var_mode
, earlier_per_vertex
);
5539 v
.run(instructions
);
5540 if (v
.usage_found()) {
5541 _mesa_glsl_error(&loc
, state
,
5542 "redeclaration of a built-in interface block must "
5543 "appear before any use of any member of the "
5548 const glsl_type
*block_type
=
5549 glsl_type::get_interface_instance(fields
,
5554 if (!state
->symbols
->add_interface(block_type
->name
, block_type
, var_mode
)) {
5555 YYLTYPE loc
= this->get_location();
5556 _mesa_glsl_error(&loc
, state
, "interface block `%s' with type `%s' "
5557 "already taken in the current scope",
5558 this->block_name
, iface_type_name
);
5561 /* Since interface blocks cannot contain statements, it should be
5562 * impossible for the block to generate any instructions.
5564 assert(declared_variables
.is_empty());
5566 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
5568 * Geometry shader input variables get the per-vertex values written
5569 * out by vertex shader output variables of the same names. Since a
5570 * geometry shader operates on a set of vertices, each input varying
5571 * variable (or input block, see interface blocks below) needs to be
5572 * declared as an array.
5574 if (state
->stage
== MESA_SHADER_GEOMETRY
&& this->array_specifier
== NULL
&&
5575 var_mode
== ir_var_shader_in
) {
5576 _mesa_glsl_error(&loc
, state
, "geometry shader inputs must be arrays");
5579 /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
5582 * "If an instance name (instance-name) is used, then it puts all the
5583 * members inside a scope within its own name space, accessed with the
5584 * field selector ( . ) operator (analogously to structures)."
5586 if (this->instance_name
) {
5587 if (redeclaring_per_vertex
) {
5588 /* When a built-in in an unnamed interface block is redeclared,
5589 * get_variable_being_redeclared() calls
5590 * check_builtin_array_max_size() to make sure that built-in array
5591 * variables aren't redeclared to illegal sizes. But we're looking
5592 * at a redeclaration of a named built-in interface block. So we
5593 * have to manually call check_builtin_array_max_size() for all parts
5594 * of the interface that are arrays.
5596 for (unsigned i
= 0; i
< num_variables
; i
++) {
5597 if (fields
[i
].type
->is_array()) {
5598 const unsigned size
= fields
[i
].type
->array_size();
5599 check_builtin_array_max_size(fields
[i
].name
, size
, loc
, state
);
5603 validate_identifier(this->instance_name
, loc
, state
);
5608 if (this->array_specifier
!= NULL
) {
5609 /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
5611 * For uniform blocks declared an array, each individual array
5612 * element corresponds to a separate buffer object backing one
5613 * instance of the block. As the array size indicates the number
5614 * of buffer objects needed, uniform block array declarations
5615 * must specify an array size.
5617 * And a few paragraphs later:
5619 * Geometry shader input blocks must be declared as arrays and
5620 * follow the array declaration and linking rules for all
5621 * geometry shader inputs. All other input and output block
5622 * arrays must specify an array size.
5624 * The upshot of this is that the only circumstance where an
5625 * interface array size *doesn't* need to be specified is on a
5626 * geometry shader input.
5628 if (this->array_specifier
->is_unsized_array
&&
5629 (state
->stage
!= MESA_SHADER_GEOMETRY
|| !this->layout
.flags
.q
.in
)) {
5630 _mesa_glsl_error(&loc
, state
,
5631 "only geometry shader inputs may be unsized "
5632 "instance block arrays");
5636 const glsl_type
*block_array_type
=
5637 process_array_type(&loc
, block_type
, this->array_specifier
, state
);
5639 var
= new(state
) ir_variable(block_array_type
,
5640 this->instance_name
,
5643 var
= new(state
) ir_variable(block_type
,
5644 this->instance_name
,
5648 var
->data
.matrix_layout
= matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
5649 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
: matrix_layout
;
5651 if (state
->stage
== MESA_SHADER_GEOMETRY
&& var_mode
== ir_var_shader_in
)
5652 handle_geometry_shader_input_decl(state
, loc
, var
);
5654 if (ir_variable
*earlier
=
5655 state
->symbols
->get_variable(this->instance_name
)) {
5656 if (!redeclaring_per_vertex
) {
5657 _mesa_glsl_error(&loc
, state
, "`%s' redeclared",
5658 this->instance_name
);
5660 earlier
->data
.how_declared
= ir_var_declared_normally
;
5661 earlier
->type
= var
->type
;
5662 earlier
->reinit_interface_type(block_type
);
5665 /* Propagate the "binding" keyword into this UBO's fields;
5666 * the UBO declaration itself doesn't get an ir_variable unless it
5667 * has an instance name. This is ugly.
5669 var
->data
.explicit_binding
= this->layout
.flags
.q
.explicit_binding
;
5670 var
->data
.binding
= this->layout
.binding
;
5672 state
->symbols
->add_variable(var
);
5673 instructions
->push_tail(var
);
5676 /* In order to have an array size, the block must also be declared with
5679 assert(this->array_specifier
== NULL
);
5681 for (unsigned i
= 0; i
< num_variables
; i
++) {
5683 new(state
) ir_variable(fields
[i
].type
,
5684 ralloc_strdup(state
, fields
[i
].name
),
5686 var
->data
.interpolation
= fields
[i
].interpolation
;
5687 var
->data
.centroid
= fields
[i
].centroid
;
5688 var
->data
.sample
= fields
[i
].sample
;
5689 var
->init_interface_type(block_type
);
5691 if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
) {
5692 var
->data
.matrix_layout
= matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
5693 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
: matrix_layout
;
5695 var
->data
.matrix_layout
= fields
[i
].matrix_layout
;
5698 if (fields
[i
].stream
!= -1 &&
5699 ((unsigned)fields
[i
].stream
) != this->layout
.stream
) {
5700 _mesa_glsl_error(&loc
, state
,
5701 "stream layout qualifier on "
5702 "interface block member `%s' does not match "
5703 "the interface block (%d vs %d)",
5704 var
->name
, fields
[i
].stream
, this->layout
.stream
);
5707 var
->data
.stream
= this->layout
.stream
;
5709 /* Examine var name here since var may get deleted in the next call */
5710 bool var_is_gl_id
= is_gl_identifier(var
->name
);
5712 if (redeclaring_per_vertex
) {
5713 ir_variable
*earlier
=
5714 get_variable_being_redeclared(var
, loc
, state
,
5715 true /* allow_all_redeclarations */);
5716 if (!var_is_gl_id
|| earlier
== NULL
) {
5717 _mesa_glsl_error(&loc
, state
,
5718 "redeclaration of gl_PerVertex can only "
5719 "include built-in variables");
5720 } else if (earlier
->data
.how_declared
== ir_var_declared_normally
) {
5721 _mesa_glsl_error(&loc
, state
,
5722 "`%s' has already been redeclared",
5725 earlier
->data
.how_declared
= ir_var_declared_in_block
;
5726 earlier
->reinit_interface_type(block_type
);
5731 if (state
->symbols
->get_variable(var
->name
) != NULL
)
5732 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
5734 /* Propagate the "binding" keyword into this UBO's fields;
5735 * the UBO declaration itself doesn't get an ir_variable unless it
5736 * has an instance name. This is ugly.
5738 var
->data
.explicit_binding
= this->layout
.flags
.q
.explicit_binding
;
5739 var
->data
.binding
= this->layout
.binding
;
5741 state
->symbols
->add_variable(var
);
5742 instructions
->push_tail(var
);
5745 if (redeclaring_per_vertex
&& block_type
!= earlier_per_vertex
) {
5746 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec:
5748 * It is also a compilation error ... to redeclare a built-in
5749 * block and then use a member from that built-in block that was
5750 * not included in the redeclaration.
5752 * This appears to be a clarification to the behaviour established
5753 * for gl_PerVertex by GLSL 1.50, therefore we implement this
5754 * behaviour regardless of GLSL version.
5756 * To prevent the shader from using a member that was not included in
5757 * the redeclaration, we disable any ir_variables that are still
5758 * associated with the old declaration of gl_PerVertex (since we've
5759 * already updated all of the variables contained in the new
5760 * gl_PerVertex to point to it).
5762 * As a side effect this will prevent
5763 * validate_intrastage_interface_blocks() from getting confused and
5764 * thinking there are conflicting definitions of gl_PerVertex in the
5767 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
5768 ir_variable
*const var
= node
->as_variable();
5770 var
->get_interface_type() == earlier_per_vertex
&&
5771 var
->data
.mode
== var_mode
) {
5772 if (var
->data
.how_declared
== ir_var_declared_normally
) {
5773 _mesa_glsl_error(&loc
, state
,
5774 "redeclaration of gl_PerVertex cannot "
5775 "follow a redeclaration of `%s'",
5778 state
->symbols
->disable_variable(var
->name
);
5790 ast_gs_input_layout::hir(exec_list
*instructions
,
5791 struct _mesa_glsl_parse_state
*state
)
5793 YYLTYPE loc
= this->get_location();
5795 /* If any geometry input layout declaration preceded this one, make sure it
5796 * was consistent with this one.
5798 if (state
->gs_input_prim_type_specified
&&
5799 state
->in_qualifier
->prim_type
!= this->prim_type
) {
5800 _mesa_glsl_error(&loc
, state
,
5801 "geometry shader input layout does not match"
5802 " previous declaration");
5806 /* If any shader inputs occurred before this declaration and specified an
5807 * array size, make sure the size they specified is consistent with the
5810 unsigned num_vertices
= vertices_per_prim(this->prim_type
);
5811 if (state
->gs_input_size
!= 0 && state
->gs_input_size
!= num_vertices
) {
5812 _mesa_glsl_error(&loc
, state
,
5813 "this geometry shader input layout implies %u vertices"
5814 " per primitive, but a previous input is declared"
5815 " with size %u", num_vertices
, state
->gs_input_size
);
5819 state
->gs_input_prim_type_specified
= true;
5821 /* If any shader inputs occurred before this declaration and did not
5822 * specify an array size, their size is determined now.
5824 foreach_in_list(ir_instruction
, node
, instructions
) {
5825 ir_variable
*var
= node
->as_variable();
5826 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_in
)
5829 /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
5833 if (var
->type
->is_unsized_array()) {
5834 if (var
->data
.max_array_access
>= num_vertices
) {
5835 _mesa_glsl_error(&loc
, state
,
5836 "this geometry shader input layout implies %u"
5837 " vertices, but an access to element %u of input"
5838 " `%s' already exists", num_vertices
,
5839 var
->data
.max_array_access
, var
->name
);
5841 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
5852 ast_cs_input_layout::hir(exec_list
*instructions
,
5853 struct _mesa_glsl_parse_state
*state
)
5855 YYLTYPE loc
= this->get_location();
5857 /* If any compute input layout declaration preceded this one, make sure it
5858 * was consistent with this one.
5860 if (state
->cs_input_local_size_specified
) {
5861 for (int i
= 0; i
< 3; i
++) {
5862 if (state
->cs_input_local_size
[i
] != this->local_size
[i
]) {
5863 _mesa_glsl_error(&loc
, state
,
5864 "compute shader input layout does not match"
5865 " previous declaration");
5871 /* From the ARB_compute_shader specification:
5873 * If the local size of the shader in any dimension is greater
5874 * than the maximum size supported by the implementation for that
5875 * dimension, a compile-time error results.
5877 * It is not clear from the spec how the error should be reported if
5878 * the total size of the work group exceeds
5879 * MAX_COMPUTE_WORK_GROUP_INVOCATIONS, but it seems reasonable to
5880 * report it at compile time as well.
5882 GLuint64 total_invocations
= 1;
5883 for (int i
= 0; i
< 3; i
++) {
5884 if (this->local_size
[i
] > state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]) {
5885 _mesa_glsl_error(&loc
, state
,
5886 "local_size_%c exceeds MAX_COMPUTE_WORK_GROUP_SIZE"
5888 state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]);
5891 total_invocations
*= this->local_size
[i
];
5892 if (total_invocations
>
5893 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
) {
5894 _mesa_glsl_error(&loc
, state
,
5895 "product of local_sizes exceeds "
5896 "MAX_COMPUTE_WORK_GROUP_INVOCATIONS (%d)",
5897 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
);
5902 state
->cs_input_local_size_specified
= true;
5903 for (int i
= 0; i
< 3; i
++)
5904 state
->cs_input_local_size
[i
] = this->local_size
[i
];
5906 /* We may now declare the built-in constant gl_WorkGroupSize (see
5907 * builtin_variable_generator::generate_constants() for why we didn't
5908 * declare it earlier).
5910 ir_variable
*var
= new(state
->symbols
)
5911 ir_variable(glsl_type::ivec3_type
, "gl_WorkGroupSize", ir_var_auto
);
5912 var
->data
.how_declared
= ir_var_declared_implicitly
;
5913 var
->data
.read_only
= true;
5914 instructions
->push_tail(var
);
5915 state
->symbols
->add_variable(var
);
5916 ir_constant_data data
;
5917 memset(&data
, 0, sizeof(data
));
5918 for (int i
= 0; i
< 3; i
++)
5919 data
.i
[i
] = this->local_size
[i
];
5920 var
->constant_value
= new(var
) ir_constant(glsl_type::ivec3_type
, &data
);
5921 var
->constant_initializer
=
5922 new(var
) ir_constant(glsl_type::ivec3_type
, &data
);
5923 var
->data
.has_initializer
= true;
5930 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
5931 exec_list
*instructions
)
5933 bool gl_FragColor_assigned
= false;
5934 bool gl_FragData_assigned
= false;
5935 bool user_defined_fs_output_assigned
= false;
5936 ir_variable
*user_defined_fs_output
= NULL
;
5938 /* It would be nice to have proper location information. */
5940 memset(&loc
, 0, sizeof(loc
));
5942 foreach_in_list(ir_instruction
, node
, instructions
) {
5943 ir_variable
*var
= node
->as_variable();
5945 if (!var
|| !var
->data
.assigned
)
5948 if (strcmp(var
->name
, "gl_FragColor") == 0)
5949 gl_FragColor_assigned
= true;
5950 else if (strcmp(var
->name
, "gl_FragData") == 0)
5951 gl_FragData_assigned
= true;
5952 else if (!is_gl_identifier(var
->name
)) {
5953 if (state
->stage
== MESA_SHADER_FRAGMENT
&&
5954 var
->data
.mode
== ir_var_shader_out
) {
5955 user_defined_fs_output_assigned
= true;
5956 user_defined_fs_output
= var
;
5961 /* From the GLSL 1.30 spec:
5963 * "If a shader statically assigns a value to gl_FragColor, it
5964 * may not assign a value to any element of gl_FragData. If a
5965 * shader statically writes a value to any element of
5966 * gl_FragData, it may not assign a value to
5967 * gl_FragColor. That is, a shader may assign values to either
5968 * gl_FragColor or gl_FragData, but not both. Multiple shaders
5969 * linked together must also consistently write just one of
5970 * these variables. Similarly, if user declared output
5971 * variables are in use (statically assigned to), then the
5972 * built-in variables gl_FragColor and gl_FragData may not be
5973 * assigned to. These incorrect usages all generate compile
5976 if (gl_FragColor_assigned
&& gl_FragData_assigned
) {
5977 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
5978 "`gl_FragColor' and `gl_FragData'");
5979 } else if (gl_FragColor_assigned
&& user_defined_fs_output_assigned
) {
5980 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
5981 "`gl_FragColor' and `%s'",
5982 user_defined_fs_output
->name
);
5983 } else if (gl_FragData_assigned
&& user_defined_fs_output_assigned
) {
5984 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
5985 "`gl_FragData' and `%s'",
5986 user_defined_fs_output
->name
);
5992 remove_per_vertex_blocks(exec_list
*instructions
,
5993 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
)
5995 /* Find the gl_PerVertex interface block of the appropriate (in/out) mode,
5996 * if it exists in this shader type.
5998 const glsl_type
*per_vertex
= NULL
;
6000 case ir_var_shader_in
:
6001 if (ir_variable
*gl_in
= state
->symbols
->get_variable("gl_in"))
6002 per_vertex
= gl_in
->get_interface_type();
6004 case ir_var_shader_out
:
6005 if (ir_variable
*gl_Position
=
6006 state
->symbols
->get_variable("gl_Position")) {
6007 per_vertex
= gl_Position
->get_interface_type();
6011 assert(!"Unexpected mode");
6015 /* If we didn't find a built-in gl_PerVertex interface block, then we don't
6016 * need to do anything.
6018 if (per_vertex
== NULL
)
6021 /* If the interface block is used by the shader, then we don't need to do
6024 interface_block_usage_visitor
v(mode
, per_vertex
);
6025 v
.run(instructions
);
6026 if (v
.usage_found())
6029 /* Remove any ir_variable declarations that refer to the interface block
6032 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
6033 ir_variable
*const var
= node
->as_variable();
6034 if (var
!= NULL
&& var
->get_interface_type() == per_vertex
&&
6035 var
->data
.mode
== mode
) {
6036 state
->symbols
->disable_variable(var
->name
);