2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
26 * Convert abstract syntax to to high-level intermediate reprensentation (HIR).
28 * During the conversion to HIR, the majority of the symantic checking is
29 * preformed on the program. This includes:
31 * * Symbol table management
35 * The majority of this work could be done during parsing, and the parser could
36 * probably generate HIR directly. However, this results in frequent changes
37 * to the parser code. Since we do not assume that every system this complier
38 * is built on will have Flex and Bison installed, we have to store the code
39 * generated by these tools in our version control system. In other parts of
40 * the system we've seen problems where a parser was changed but the generated
41 * code was not committed, merge conflicts where created because two developers
42 * had slightly different versions of Bison installed, etc.
44 * I have also noticed that running Bison generated parsers in GDB is very
45 * irritating. When you get a segfault on '$$ = $1->foo', you can't very
46 * well 'print $1' in GDB.
48 * As a result, my preference is to put as little C code as possible in the
49 * parser (and lexer) sources.
52 #include "main/core.h" /* for struct gl_extensions */
53 #include "glsl_symbol_table.h"
54 #include "glsl_parser_extras.h"
56 #include "glsl_types.h"
57 #include "program/hash_table.h"
61 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
62 exec_list
*instructions
);
64 remove_per_vertex_blocks(exec_list
*instructions
,
65 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
);
69 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
71 _mesa_glsl_initialize_variables(instructions
, state
);
73 state
->symbols
->separate_function_namespace
= state
->language_version
== 110;
75 state
->current_function
= NULL
;
77 state
->toplevel_ir
= instructions
;
79 state
->gs_input_prim_type_specified
= false;
81 /* Section 4.2 of the GLSL 1.20 specification states:
82 * "The built-in functions are scoped in a scope outside the global scope
83 * users declare global variables in. That is, a shader's global scope,
84 * available for user-defined functions and global variables, is nested
85 * inside the scope containing the built-in functions."
87 * Since built-in functions like ftransform() access built-in variables,
88 * it follows that those must be in the outer scope as well.
90 * We push scope here to create this nesting effect...but don't pop.
91 * This way, a shader's globals are still in the symbol table for use
94 state
->symbols
->push_scope();
96 foreach_list_typed (ast_node
, ast
, link
, & state
->translation_unit
)
97 ast
->hir(instructions
, state
);
99 detect_recursion_unlinked(state
, instructions
);
100 detect_conflicting_assignments(state
, instructions
);
102 state
->toplevel_ir
= NULL
;
104 /* Move all of the variable declarations to the front of the IR list, and
105 * reverse the order. This has the (intended!) side effect that vertex
106 * shader inputs and fragment shader outputs will appear in the IR in the
107 * same order that they appeared in the shader code. This results in the
108 * locations being assigned in the declared order. Many (arguably buggy)
109 * applications depend on this behavior, and it matches what nearly all
112 foreach_list_safe(node
, instructions
) {
113 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
119 instructions
->push_head(var
);
122 /* From section 7.1 (Built-In Language Variables) of the GLSL 4.10 spec:
124 * If multiple shaders using members of a built-in block belonging to
125 * the same interface are linked together in the same program, they
126 * must all redeclare the built-in block in the same way, as described
127 * in section 4.3.7 "Interface Blocks" for interface block matching, or
128 * a link error will result.
130 * The phrase "using members of a built-in block" implies that if two
131 * shaders are linked together and one of them *does not use* any members
132 * of the built-in block, then that shader does not need to have a matching
133 * redeclaration of the built-in block.
135 * This appears to be a clarification to the behaviour established for
136 * gl_PerVertex by GLSL 1.50, therefore implement it regardless of GLSL
139 * The definition of "interface" in section 4.3.7 that applies here is as
142 * The boundary between adjacent programmable pipeline stages: This
143 * spans all the outputs in all compilation units of the first stage
144 * and all the inputs in all compilation units of the second stage.
146 * Therefore this rule applies to both inter- and intra-stage linking.
148 * The easiest way to implement this is to check whether the shader uses
149 * gl_PerVertex right after ast-to-ir conversion, and if it doesn't, simply
150 * remove all the relevant variable declaration from the IR, so that the
151 * linker won't see them and complain about mismatches.
153 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_in
);
154 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_out
);
159 * If a conversion is available, convert one operand to a different type
161 * The \c from \c ir_rvalue is converted "in place".
163 * \param to Type that the operand it to be converted to
164 * \param from Operand that is being converted
165 * \param state GLSL compiler state
168 * If a conversion is possible (or unnecessary), \c true is returned.
169 * Otherwise \c false is returned.
172 apply_implicit_conversion(const glsl_type
*to
, ir_rvalue
* &from
,
173 struct _mesa_glsl_parse_state
*state
)
176 if (to
->base_type
== from
->type
->base_type
)
179 /* This conversion was added in GLSL 1.20. If the compilation mode is
180 * GLSL 1.10, the conversion is skipped.
182 if (!state
->is_version(120, 0))
185 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
187 * "There are no implicit array or structure conversions. For
188 * example, an array of int cannot be implicitly converted to an
189 * array of float. There are no implicit conversions between
190 * signed and unsigned integers."
192 /* FINISHME: The above comment is partially a lie. There is int/uint
193 * FINISHME: conversion for immediate constants.
195 if (!to
->is_float() || !from
->type
->is_numeric())
198 /* Convert to a floating point type with the same number of components
199 * as the original type - i.e. int to float, not int to vec4.
201 to
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, from
->type
->vector_elements
,
202 from
->type
->matrix_columns
);
204 switch (from
->type
->base_type
) {
206 from
= new(ctx
) ir_expression(ir_unop_i2f
, to
, from
, NULL
);
209 from
= new(ctx
) ir_expression(ir_unop_u2f
, to
, from
, NULL
);
212 from
= new(ctx
) ir_expression(ir_unop_b2f
, to
, from
, NULL
);
222 static const struct glsl_type
*
223 arithmetic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
225 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
227 const glsl_type
*type_a
= value_a
->type
;
228 const glsl_type
*type_b
= value_b
->type
;
230 /* From GLSL 1.50 spec, page 56:
232 * "The arithmetic binary operators add (+), subtract (-),
233 * multiply (*), and divide (/) operate on integer and
234 * floating-point scalars, vectors, and matrices."
236 if (!type_a
->is_numeric() || !type_b
->is_numeric()) {
237 _mesa_glsl_error(loc
, state
,
238 "operands to arithmetic operators must be numeric");
239 return glsl_type::error_type
;
243 /* "If one operand is floating-point based and the other is
244 * not, then the conversions from Section 4.1.10 "Implicit
245 * Conversions" are applied to the non-floating-point-based operand."
247 if (!apply_implicit_conversion(type_a
, value_b
, state
)
248 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
249 _mesa_glsl_error(loc
, state
,
250 "could not implicitly convert operands to "
251 "arithmetic operator");
252 return glsl_type::error_type
;
254 type_a
= value_a
->type
;
255 type_b
= value_b
->type
;
257 /* "If the operands are integer types, they must both be signed or
260 * From this rule and the preceeding conversion it can be inferred that
261 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
262 * The is_numeric check above already filtered out the case where either
263 * type is not one of these, so now the base types need only be tested for
266 if (type_a
->base_type
!= type_b
->base_type
) {
267 _mesa_glsl_error(loc
, state
,
268 "base type mismatch for arithmetic operator");
269 return glsl_type::error_type
;
272 /* "All arithmetic binary operators result in the same fundamental type
273 * (signed integer, unsigned integer, or floating-point) as the
274 * operands they operate on, after operand type conversion. After
275 * conversion, the following cases are valid
277 * * The two operands are scalars. In this case the operation is
278 * applied, resulting in a scalar."
280 if (type_a
->is_scalar() && type_b
->is_scalar())
283 /* "* One operand is a scalar, and the other is a vector or matrix.
284 * In this case, the scalar operation is applied independently to each
285 * component of the vector or matrix, resulting in the same size
288 if (type_a
->is_scalar()) {
289 if (!type_b
->is_scalar())
291 } else if (type_b
->is_scalar()) {
295 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
296 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
299 assert(!type_a
->is_scalar());
300 assert(!type_b
->is_scalar());
302 /* "* The two operands are vectors of the same size. In this case, the
303 * operation is done component-wise resulting in the same size
306 if (type_a
->is_vector() && type_b
->is_vector()) {
307 if (type_a
== type_b
) {
310 _mesa_glsl_error(loc
, state
,
311 "vector size mismatch for arithmetic operator");
312 return glsl_type::error_type
;
316 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
317 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
318 * <vector, vector> have been handled. At least one of the operands must
319 * be matrix. Further, since there are no integer matrix types, the base
320 * type of both operands must be float.
322 assert(type_a
->is_matrix() || type_b
->is_matrix());
323 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
);
324 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
);
326 /* "* The operator is add (+), subtract (-), or divide (/), and the
327 * operands are matrices with the same number of rows and the same
328 * number of columns. In this case, the operation is done component-
329 * wise resulting in the same size matrix."
330 * * The operator is multiply (*), where both operands are matrices or
331 * one operand is a vector and the other a matrix. A right vector
332 * operand is treated as a column vector and a left vector operand as a
333 * row vector. In all these cases, it is required that the number of
334 * columns of the left operand is equal to the number of rows of the
335 * right operand. Then, the multiply (*) operation does a linear
336 * algebraic multiply, yielding an object that has the same number of
337 * rows as the left operand and the same number of columns as the right
338 * operand. Section 5.10 "Vector and Matrix Operations" explains in
339 * more detail how vectors and matrices are operated on."
342 if (type_a
== type_b
)
345 if (type_a
->is_matrix() && type_b
->is_matrix()) {
346 /* Matrix multiply. The columns of A must match the rows of B. Given
347 * the other previously tested constraints, this means the vector type
348 * of a row from A must be the same as the vector type of a column from
351 if (type_a
->row_type() == type_b
->column_type()) {
352 /* The resulting matrix has the number of columns of matrix B and
353 * the number of rows of matrix A. We get the row count of A by
354 * looking at the size of a vector that makes up a column. The
355 * transpose (size of a row) is done for B.
357 const glsl_type
*const type
=
358 glsl_type::get_instance(type_a
->base_type
,
359 type_a
->column_type()->vector_elements
,
360 type_b
->row_type()->vector_elements
);
361 assert(type
!= glsl_type::error_type
);
365 } else if (type_a
->is_matrix()) {
366 /* A is a matrix and B is a column vector. Columns of A must match
367 * rows of B. Given the other previously tested constraints, this
368 * means the vector type of a row from A must be the same as the
369 * vector the type of B.
371 if (type_a
->row_type() == type_b
) {
372 /* The resulting vector has a number of elements equal to
373 * the number of rows of matrix A. */
374 const glsl_type
*const type
=
375 glsl_type::get_instance(type_a
->base_type
,
376 type_a
->column_type()->vector_elements
,
378 assert(type
!= glsl_type::error_type
);
383 assert(type_b
->is_matrix());
385 /* A is a row vector and B is a matrix. Columns of A must match rows
386 * of B. Given the other previously tested constraints, this means
387 * the type of A must be the same as the vector type of a column from
390 if (type_a
== type_b
->column_type()) {
391 /* The resulting vector has a number of elements equal to
392 * the number of columns of matrix B. */
393 const glsl_type
*const type
=
394 glsl_type::get_instance(type_a
->base_type
,
395 type_b
->row_type()->vector_elements
,
397 assert(type
!= glsl_type::error_type
);
403 _mesa_glsl_error(loc
, state
, "size mismatch for matrix multiplication");
404 return glsl_type::error_type
;
408 /* "All other cases are illegal."
410 _mesa_glsl_error(loc
, state
, "type mismatch");
411 return glsl_type::error_type
;
415 static const struct glsl_type
*
416 unary_arithmetic_result_type(const struct glsl_type
*type
,
417 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
419 /* From GLSL 1.50 spec, page 57:
421 * "The arithmetic unary operators negate (-), post- and pre-increment
422 * and decrement (-- and ++) operate on integer or floating-point
423 * values (including vectors and matrices). All unary operators work
424 * component-wise on their operands. These result with the same type
427 if (!type
->is_numeric()) {
428 _mesa_glsl_error(loc
, state
,
429 "operands to arithmetic operators must be numeric");
430 return glsl_type::error_type
;
437 * \brief Return the result type of a bit-logic operation.
439 * If the given types to the bit-logic operator are invalid, return
440 * glsl_type::error_type.
442 * \param type_a Type of LHS of bit-logic op
443 * \param type_b Type of RHS of bit-logic op
445 static const struct glsl_type
*
446 bit_logic_result_type(const struct glsl_type
*type_a
,
447 const struct glsl_type
*type_b
,
449 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
451 if (!state
->check_bitwise_operations_allowed(loc
)) {
452 return glsl_type::error_type
;
455 /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
457 * "The bitwise operators and (&), exclusive-or (^), and inclusive-or
458 * (|). The operands must be of type signed or unsigned integers or
461 if (!type_a
->is_integer()) {
462 _mesa_glsl_error(loc
, state
, "LHS of `%s' must be an integer",
463 ast_expression::operator_string(op
));
464 return glsl_type::error_type
;
466 if (!type_b
->is_integer()) {
467 _mesa_glsl_error(loc
, state
, "RHS of `%s' must be an integer",
468 ast_expression::operator_string(op
));
469 return glsl_type::error_type
;
472 /* "The fundamental types of the operands (signed or unsigned) must
475 if (type_a
->base_type
!= type_b
->base_type
) {
476 _mesa_glsl_error(loc
, state
, "operands of `%s' must have the same "
477 "base type", ast_expression::operator_string(op
));
478 return glsl_type::error_type
;
481 /* "The operands cannot be vectors of differing size." */
482 if (type_a
->is_vector() &&
483 type_b
->is_vector() &&
484 type_a
->vector_elements
!= type_b
->vector_elements
) {
485 _mesa_glsl_error(loc
, state
, "operands of `%s' cannot be vectors of "
486 "different sizes", ast_expression::operator_string(op
));
487 return glsl_type::error_type
;
490 /* "If one operand is a scalar and the other a vector, the scalar is
491 * applied component-wise to the vector, resulting in the same type as
492 * the vector. The fundamental types of the operands [...] will be the
493 * resulting fundamental type."
495 if (type_a
->is_scalar())
501 static const struct glsl_type
*
502 modulus_result_type(const struct glsl_type
*type_a
,
503 const struct glsl_type
*type_b
,
504 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
506 if (!state
->check_version(130, 300, loc
, "operator '%%' is reserved")) {
507 return glsl_type::error_type
;
510 /* From GLSL 1.50 spec, page 56:
511 * "The operator modulus (%) operates on signed or unsigned integers or
512 * integer vectors. The operand types must both be signed or both be
515 if (!type_a
->is_integer()) {
516 _mesa_glsl_error(loc
, state
, "LHS of operator %% must be an integer");
517 return glsl_type::error_type
;
519 if (!type_b
->is_integer()) {
520 _mesa_glsl_error(loc
, state
, "RHS of operator %% must be an integer");
521 return glsl_type::error_type
;
523 if (type_a
->base_type
!= type_b
->base_type
) {
524 _mesa_glsl_error(loc
, state
,
525 "operands of %% must have the same base type");
526 return glsl_type::error_type
;
529 /* "The operands cannot be vectors of differing size. If one operand is
530 * a scalar and the other vector, then the scalar is applied component-
531 * wise to the vector, resulting in the same type as the vector. If both
532 * are vectors of the same size, the result is computed component-wise."
534 if (type_a
->is_vector()) {
535 if (!type_b
->is_vector()
536 || (type_a
->vector_elements
== type_b
->vector_elements
))
541 /* "The operator modulus (%) is not defined for any other data types
542 * (non-integer types)."
544 _mesa_glsl_error(loc
, state
, "type mismatch");
545 return glsl_type::error_type
;
549 static const struct glsl_type
*
550 relational_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
551 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
553 const glsl_type
*type_a
= value_a
->type
;
554 const glsl_type
*type_b
= value_b
->type
;
556 /* From GLSL 1.50 spec, page 56:
557 * "The relational operators greater than (>), less than (<), greater
558 * than or equal (>=), and less than or equal (<=) operate only on
559 * scalar integer and scalar floating-point expressions."
561 if (!type_a
->is_numeric()
562 || !type_b
->is_numeric()
563 || !type_a
->is_scalar()
564 || !type_b
->is_scalar()) {
565 _mesa_glsl_error(loc
, state
,
566 "operands to relational operators must be scalar and "
568 return glsl_type::error_type
;
571 /* "Either the operands' types must match, or the conversions from
572 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
573 * operand, after which the types must match."
575 if (!apply_implicit_conversion(type_a
, value_b
, state
)
576 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
577 _mesa_glsl_error(loc
, state
,
578 "could not implicitly convert operands to "
579 "relational operator");
580 return glsl_type::error_type
;
582 type_a
= value_a
->type
;
583 type_b
= value_b
->type
;
585 if (type_a
->base_type
!= type_b
->base_type
) {
586 _mesa_glsl_error(loc
, state
, "base type mismatch");
587 return glsl_type::error_type
;
590 /* "The result is scalar Boolean."
592 return glsl_type::bool_type
;
596 * \brief Return the result type of a bit-shift operation.
598 * If the given types to the bit-shift operator are invalid, return
599 * glsl_type::error_type.
601 * \param type_a Type of LHS of bit-shift op
602 * \param type_b Type of RHS of bit-shift op
604 static const struct glsl_type
*
605 shift_result_type(const struct glsl_type
*type_a
,
606 const struct glsl_type
*type_b
,
608 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
610 if (!state
->check_bitwise_operations_allowed(loc
)) {
611 return glsl_type::error_type
;
614 /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
616 * "The shift operators (<<) and (>>). For both operators, the operands
617 * must be signed or unsigned integers or integer vectors. One operand
618 * can be signed while the other is unsigned."
620 if (!type_a
->is_integer()) {
621 _mesa_glsl_error(loc
, state
, "LHS of operator %s must be an integer or "
622 "integer vector", ast_expression::operator_string(op
));
623 return glsl_type::error_type
;
626 if (!type_b
->is_integer()) {
627 _mesa_glsl_error(loc
, state
, "RHS of operator %s must be an integer or "
628 "integer vector", ast_expression::operator_string(op
));
629 return glsl_type::error_type
;
632 /* "If the first operand is a scalar, the second operand has to be
635 if (type_a
->is_scalar() && !type_b
->is_scalar()) {
636 _mesa_glsl_error(loc
, state
, "if the first operand of %s is scalar, the "
637 "second must be scalar as well",
638 ast_expression::operator_string(op
));
639 return glsl_type::error_type
;
642 /* If both operands are vectors, check that they have same number of
645 if (type_a
->is_vector() &&
646 type_b
->is_vector() &&
647 type_a
->vector_elements
!= type_b
->vector_elements
) {
648 _mesa_glsl_error(loc
, state
, "vector operands to operator %s must "
649 "have same number of elements",
650 ast_expression::operator_string(op
));
651 return glsl_type::error_type
;
654 /* "In all cases, the resulting type will be the same type as the left
661 * Validates that a value can be assigned to a location with a specified type
663 * Validates that \c rhs can be assigned to some location. If the types are
664 * not an exact match but an automatic conversion is possible, \c rhs will be
668 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
669 * Otherwise the actual RHS to be assigned will be returned. This may be
670 * \c rhs, or it may be \c rhs after some type conversion.
673 * In addition to being used for assignments, this function is used to
674 * type-check return values.
677 validate_assignment(struct _mesa_glsl_parse_state
*state
,
678 YYLTYPE loc
, const glsl_type
*lhs_type
,
679 ir_rvalue
*rhs
, bool is_initializer
)
681 /* If there is already some error in the RHS, just return it. Anything
682 * else will lead to an avalanche of error message back to the user.
684 if (rhs
->type
->is_error())
687 /* If the types are identical, the assignment can trivially proceed.
689 if (rhs
->type
== lhs_type
)
692 /* If the array element types are the same and the LHS is unsized,
693 * the assignment is okay for initializers embedded in variable
696 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
697 * is handled by ir_dereference::is_lvalue.
699 if (lhs_type
->is_unsized_array() && rhs
->type
->is_array()
700 && (lhs_type
->element_type() == rhs
->type
->element_type())) {
701 if (is_initializer
) {
704 _mesa_glsl_error(&loc
, state
,
705 "implicitly sized arrays cannot be assigned");
710 /* Check for implicit conversion in GLSL 1.20 */
711 if (apply_implicit_conversion(lhs_type
, rhs
, state
)) {
712 if (rhs
->type
== lhs_type
)
716 _mesa_glsl_error(&loc
, state
,
717 "%s of type %s cannot be assigned to "
718 "variable of type %s",
719 is_initializer
? "initializer" : "value",
720 rhs
->type
->name
, lhs_type
->name
);
726 mark_whole_array_access(ir_rvalue
*access
)
728 ir_dereference_variable
*deref
= access
->as_dereference_variable();
730 if (deref
&& deref
->var
) {
731 deref
->var
->data
.max_array_access
= deref
->type
->length
- 1;
736 do_assignment(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
737 const char *non_lvalue_description
,
738 ir_rvalue
*lhs
, ir_rvalue
*rhs
, bool is_initializer
,
742 bool error_emitted
= (lhs
->type
->is_error() || rhs
->type
->is_error());
744 /* If the assignment LHS comes back as an ir_binop_vector_extract
745 * expression, move it to the RHS as an ir_triop_vector_insert.
747 if (lhs
->ir_type
== ir_type_expression
) {
748 ir_expression
*const expr
= lhs
->as_expression();
750 if (unlikely(expr
->operation
== ir_binop_vector_extract
)) {
752 validate_assignment(state
, lhs_loc
, lhs
->type
,
753 rhs
, is_initializer
);
755 if (new_rhs
== NULL
) {
758 rhs
= new(ctx
) ir_expression(ir_triop_vector_insert
,
759 expr
->operands
[0]->type
,
763 lhs
= expr
->operands
[0]->clone(ctx
, NULL
);
768 ir_variable
*lhs_var
= lhs
->variable_referenced();
770 lhs_var
->data
.assigned
= true;
772 if (!error_emitted
) {
773 if (non_lvalue_description
!= NULL
) {
774 _mesa_glsl_error(&lhs_loc
, state
,
776 non_lvalue_description
);
777 error_emitted
= true;
778 } else if (lhs
->variable_referenced() != NULL
779 && lhs
->variable_referenced()->data
.read_only
) {
780 _mesa_glsl_error(&lhs_loc
, state
,
781 "assignment to read-only variable '%s'",
782 lhs
->variable_referenced()->name
);
783 error_emitted
= true;
785 } else if (lhs
->type
->is_array() &&
786 !state
->check_version(120, 300, &lhs_loc
,
787 "whole array assignment forbidden")) {
788 /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
790 * "Other binary or unary expressions, non-dereferenced
791 * arrays, function names, swizzles with repeated fields,
792 * and constants cannot be l-values."
794 * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00.
796 error_emitted
= true;
797 } else if (!lhs
->is_lvalue()) {
798 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
799 error_emitted
= true;
804 validate_assignment(state
, lhs_loc
, lhs
->type
, rhs
, is_initializer
);
805 if (new_rhs
!= NULL
) {
808 /* If the LHS array was not declared with a size, it takes it size from
809 * the RHS. If the LHS is an l-value and a whole array, it must be a
810 * dereference of a variable. Any other case would require that the LHS
811 * is either not an l-value or not a whole array.
813 if (lhs
->type
->is_unsized_array()) {
814 ir_dereference
*const d
= lhs
->as_dereference();
818 ir_variable
*const var
= d
->variable_referenced();
822 if (var
->data
.max_array_access
>= unsigned(rhs
->type
->array_size())) {
823 /* FINISHME: This should actually log the location of the RHS. */
824 _mesa_glsl_error(& lhs_loc
, state
, "array size must be > %u due to "
826 var
->data
.max_array_access
);
829 var
->type
= glsl_type::get_array_instance(lhs
->type
->element_type(),
830 rhs
->type
->array_size());
833 mark_whole_array_access(rhs
);
834 mark_whole_array_access(lhs
);
837 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
838 * but not post_inc) need the converted assigned value as an rvalue
839 * to handle things like:
843 * So we always just store the computed value being assigned to a
844 * temporary and return a deref of that temporary. If the rvalue
845 * ends up not being used, the temp will get copy-propagated out.
847 ir_variable
*var
= new(ctx
) ir_variable(rhs
->type
, "assignment_tmp",
849 ir_dereference_variable
*deref_var
= new(ctx
) ir_dereference_variable(var
);
850 instructions
->push_tail(var
);
851 instructions
->push_tail(new(ctx
) ir_assignment(deref_var
, rhs
));
852 deref_var
= new(ctx
) ir_dereference_variable(var
);
855 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, deref_var
));
857 return new(ctx
) ir_dereference_variable(var
);
861 get_lvalue_copy(exec_list
*instructions
, ir_rvalue
*lvalue
)
863 void *ctx
= ralloc_parent(lvalue
);
866 var
= new(ctx
) ir_variable(lvalue
->type
, "_post_incdec_tmp",
868 instructions
->push_tail(var
);
869 var
->data
.mode
= ir_var_auto
;
871 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
874 return new(ctx
) ir_dereference_variable(var
);
879 ast_node::hir(exec_list
*instructions
,
880 struct _mesa_glsl_parse_state
*state
)
889 do_comparison(void *mem_ctx
, int operation
, ir_rvalue
*op0
, ir_rvalue
*op1
)
892 ir_rvalue
*cmp
= NULL
;
894 if (operation
== ir_binop_all_equal
)
895 join_op
= ir_binop_logic_and
;
897 join_op
= ir_binop_logic_or
;
899 switch (op0
->type
->base_type
) {
900 case GLSL_TYPE_FLOAT
:
904 return new(mem_ctx
) ir_expression(operation
, op0
, op1
);
906 case GLSL_TYPE_ARRAY
: {
907 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
908 ir_rvalue
*e0
, *e1
, *result
;
910 e0
= new(mem_ctx
) ir_dereference_array(op0
->clone(mem_ctx
, NULL
),
911 new(mem_ctx
) ir_constant(i
));
912 e1
= new(mem_ctx
) ir_dereference_array(op1
->clone(mem_ctx
, NULL
),
913 new(mem_ctx
) ir_constant(i
));
914 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
917 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
923 mark_whole_array_access(op0
);
924 mark_whole_array_access(op1
);
928 case GLSL_TYPE_STRUCT
: {
929 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
930 ir_rvalue
*e0
, *e1
, *result
;
931 const char *field_name
= op0
->type
->fields
.structure
[i
].name
;
933 e0
= new(mem_ctx
) ir_dereference_record(op0
->clone(mem_ctx
, NULL
),
935 e1
= new(mem_ctx
) ir_dereference_record(op1
->clone(mem_ctx
, NULL
),
937 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
940 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
948 case GLSL_TYPE_ERROR
:
950 case GLSL_TYPE_SAMPLER
:
951 case GLSL_TYPE_INTERFACE
:
952 case GLSL_TYPE_ATOMIC_UINT
:
953 /* I assume a comparison of a struct containing a sampler just
954 * ignores the sampler present in the type.
960 cmp
= new(mem_ctx
) ir_constant(true);
965 /* For logical operations, we want to ensure that the operands are
966 * scalar booleans. If it isn't, emit an error and return a constant
967 * boolean to avoid triggering cascading error messages.
970 get_scalar_boolean_operand(exec_list
*instructions
,
971 struct _mesa_glsl_parse_state
*state
,
972 ast_expression
*parent_expr
,
974 const char *operand_name
,
977 ast_expression
*expr
= parent_expr
->subexpressions
[operand
];
979 ir_rvalue
*val
= expr
->hir(instructions
, state
);
981 if (val
->type
->is_boolean() && val
->type
->is_scalar())
984 if (!*error_emitted
) {
985 YYLTYPE loc
= expr
->get_location();
986 _mesa_glsl_error(&loc
, state
, "%s of `%s' must be scalar boolean",
988 parent_expr
->operator_string(parent_expr
->oper
));
989 *error_emitted
= true;
992 return new(ctx
) ir_constant(true);
996 * If name refers to a builtin array whose maximum allowed size is less than
997 * size, report an error and return true. Otherwise return false.
1000 check_builtin_array_max_size(const char *name
, unsigned size
,
1001 YYLTYPE loc
, struct _mesa_glsl_parse_state
*state
)
1003 if ((strcmp("gl_TexCoord", name
) == 0)
1004 && (size
> state
->Const
.MaxTextureCoords
)) {
1005 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1007 * "The size [of gl_TexCoord] can be at most
1008 * gl_MaxTextureCoords."
1010 _mesa_glsl_error(&loc
, state
, "`gl_TexCoord' array size cannot "
1011 "be larger than gl_MaxTextureCoords (%u)",
1012 state
->Const
.MaxTextureCoords
);
1013 } else if (strcmp("gl_ClipDistance", name
) == 0
1014 && size
> state
->Const
.MaxClipPlanes
) {
1015 /* From section 7.1 (Vertex Shader Special Variables) of the
1018 * "The gl_ClipDistance array is predeclared as unsized and
1019 * must be sized by the shader either redeclaring it with a
1020 * size or indexing it only with integral constant
1021 * expressions. ... The size can be at most
1022 * gl_MaxClipDistances."
1024 _mesa_glsl_error(&loc
, state
, "`gl_ClipDistance' array size cannot "
1025 "be larger than gl_MaxClipDistances (%u)",
1026 state
->Const
.MaxClipPlanes
);
1031 * Create the constant 1, of a which is appropriate for incrementing and
1032 * decrementing values of the given GLSL type. For example, if type is vec4,
1033 * this creates a constant value of 1.0 having type float.
1035 * If the given type is invalid for increment and decrement operators, return
1036 * a floating point 1--the error will be detected later.
1039 constant_one_for_inc_dec(void *ctx
, const glsl_type
*type
)
1041 switch (type
->base_type
) {
1042 case GLSL_TYPE_UINT
:
1043 return new(ctx
) ir_constant((unsigned) 1);
1045 return new(ctx
) ir_constant(1);
1047 case GLSL_TYPE_FLOAT
:
1048 return new(ctx
) ir_constant(1.0f
);
1053 ast_expression::hir(exec_list
*instructions
,
1054 struct _mesa_glsl_parse_state
*state
)
1057 static const int operations
[AST_NUM_OPERATORS
] = {
1058 -1, /* ast_assign doesn't convert to ir_expression. */
1059 -1, /* ast_plus doesn't convert to ir_expression. */
1073 ir_binop_any_nequal
,
1083 /* Note: The following block of expression types actually convert
1084 * to multiple IR instructions.
1086 ir_binop_mul
, /* ast_mul_assign */
1087 ir_binop_div
, /* ast_div_assign */
1088 ir_binop_mod
, /* ast_mod_assign */
1089 ir_binop_add
, /* ast_add_assign */
1090 ir_binop_sub
, /* ast_sub_assign */
1091 ir_binop_lshift
, /* ast_ls_assign */
1092 ir_binop_rshift
, /* ast_rs_assign */
1093 ir_binop_bit_and
, /* ast_and_assign */
1094 ir_binop_bit_xor
, /* ast_xor_assign */
1095 ir_binop_bit_or
, /* ast_or_assign */
1097 -1, /* ast_conditional doesn't convert to ir_expression. */
1098 ir_binop_add
, /* ast_pre_inc. */
1099 ir_binop_sub
, /* ast_pre_dec. */
1100 ir_binop_add
, /* ast_post_inc. */
1101 ir_binop_sub
, /* ast_post_dec. */
1102 -1, /* ast_field_selection doesn't conv to ir_expression. */
1103 -1, /* ast_array_index doesn't convert to ir_expression. */
1104 -1, /* ast_function_call doesn't conv to ir_expression. */
1105 -1, /* ast_identifier doesn't convert to ir_expression. */
1106 -1, /* ast_int_constant doesn't convert to ir_expression. */
1107 -1, /* ast_uint_constant doesn't conv to ir_expression. */
1108 -1, /* ast_float_constant doesn't conv to ir_expression. */
1109 -1, /* ast_bool_constant doesn't conv to ir_expression. */
1110 -1, /* ast_sequence doesn't convert to ir_expression. */
1112 ir_rvalue
*result
= NULL
;
1114 const struct glsl_type
*type
; /* a temporary variable for switch cases */
1115 bool error_emitted
= false;
1118 loc
= this->get_location();
1120 switch (this->oper
) {
1122 assert(!"ast_aggregate: Should never get here.");
1126 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1127 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1129 result
= do_assignment(instructions
, state
,
1130 this->subexpressions
[0]->non_lvalue_description
,
1131 op
[0], op
[1], false,
1132 this->subexpressions
[0]->get_location());
1133 error_emitted
= result
->type
->is_error();
1138 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1140 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1142 error_emitted
= type
->is_error();
1148 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1150 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1152 error_emitted
= type
->is_error();
1154 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1162 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1163 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1165 type
= arithmetic_result_type(op
[0], op
[1],
1166 (this->oper
== ast_mul
),
1168 error_emitted
= type
->is_error();
1170 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1175 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1176 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1178 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1180 assert(operations
[this->oper
] == ir_binop_mod
);
1182 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1184 error_emitted
= type
->is_error();
1189 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1190 error_emitted
= true;
1193 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1194 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1195 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1197 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1199 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1206 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1207 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1209 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
1211 /* The relational operators must either generate an error or result
1212 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1214 assert(type
->is_error()
1215 || ((type
->base_type
== GLSL_TYPE_BOOL
)
1216 && type
->is_scalar()));
1218 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1220 error_emitted
= type
->is_error();
1225 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1226 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1228 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1230 * "The equality operators equal (==), and not equal (!=)
1231 * operate on all types. They result in a scalar Boolean. If
1232 * the operand types do not match, then there must be a
1233 * conversion from Section 4.1.10 "Implicit Conversions"
1234 * applied to one operand that can make them match, in which
1235 * case this conversion is done."
1237 if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
1238 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
1239 || (op
[0]->type
!= op
[1]->type
)) {
1240 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
1241 "type", (this->oper
== ast_equal
) ? "==" : "!=");
1242 error_emitted
= true;
1243 } else if ((op
[0]->type
->is_array() || op
[1]->type
->is_array()) &&
1244 !state
->check_version(120, 300, &loc
,
1245 "array comparisons forbidden")) {
1246 error_emitted
= true;
1247 } else if ((op
[0]->type
->contains_opaque() ||
1248 op
[1]->type
->contains_opaque())) {
1249 _mesa_glsl_error(&loc
, state
, "opaque type comparisons forbidden");
1250 error_emitted
= true;
1253 if (error_emitted
) {
1254 result
= new(ctx
) ir_constant(false);
1256 result
= do_comparison(ctx
, operations
[this->oper
], op
[0], op
[1]);
1257 assert(result
->type
== glsl_type::bool_type
);
1264 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1265 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1266 type
= bit_logic_result_type(op
[0]->type
, op
[1]->type
, this->oper
,
1268 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1270 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1274 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1276 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1277 error_emitted
= true;
1280 if (!op
[0]->type
->is_integer()) {
1281 _mesa_glsl_error(&loc
, state
, "operand of `~' must be an integer");
1282 error_emitted
= true;
1285 type
= error_emitted
? glsl_type::error_type
: op
[0]->type
;
1286 result
= new(ctx
) ir_expression(ir_unop_bit_not
, type
, op
[0], NULL
);
1289 case ast_logic_and
: {
1290 exec_list rhs_instructions
;
1291 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1292 "LHS", &error_emitted
);
1293 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1294 "RHS", &error_emitted
);
1296 if (rhs_instructions
.is_empty()) {
1297 result
= new(ctx
) ir_expression(ir_binop_logic_and
, op
[0], op
[1]);
1298 type
= result
->type
;
1300 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1303 instructions
->push_tail(tmp
);
1305 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1306 instructions
->push_tail(stmt
);
1308 stmt
->then_instructions
.append_list(&rhs_instructions
);
1309 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1310 ir_assignment
*const then_assign
=
1311 new(ctx
) ir_assignment(then_deref
, op
[1]);
1312 stmt
->then_instructions
.push_tail(then_assign
);
1314 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1315 ir_assignment
*const else_assign
=
1316 new(ctx
) ir_assignment(else_deref
, new(ctx
) ir_constant(false));
1317 stmt
->else_instructions
.push_tail(else_assign
);
1319 result
= new(ctx
) ir_dereference_variable(tmp
);
1325 case ast_logic_or
: {
1326 exec_list rhs_instructions
;
1327 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1328 "LHS", &error_emitted
);
1329 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1330 "RHS", &error_emitted
);
1332 if (rhs_instructions
.is_empty()) {
1333 result
= new(ctx
) ir_expression(ir_binop_logic_or
, op
[0], op
[1]);
1334 type
= result
->type
;
1336 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1339 instructions
->push_tail(tmp
);
1341 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1342 instructions
->push_tail(stmt
);
1344 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1345 ir_assignment
*const then_assign
=
1346 new(ctx
) ir_assignment(then_deref
, new(ctx
) ir_constant(true));
1347 stmt
->then_instructions
.push_tail(then_assign
);
1349 stmt
->else_instructions
.append_list(&rhs_instructions
);
1350 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1351 ir_assignment
*const else_assign
=
1352 new(ctx
) ir_assignment(else_deref
, op
[1]);
1353 stmt
->else_instructions
.push_tail(else_assign
);
1355 result
= new(ctx
) ir_dereference_variable(tmp
);
1362 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1364 * "The logical binary operators and (&&), or ( | | ), and
1365 * exclusive or (^^). They operate only on two Boolean
1366 * expressions and result in a Boolean expression."
1368 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0, "LHS",
1370 op
[1] = get_scalar_boolean_operand(instructions
, state
, this, 1, "RHS",
1373 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1378 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1379 "operand", &error_emitted
);
1381 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1385 case ast_mul_assign
:
1386 case ast_div_assign
:
1387 case ast_add_assign
:
1388 case ast_sub_assign
: {
1389 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1390 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1392 type
= arithmetic_result_type(op
[0], op
[1],
1393 (this->oper
== ast_mul_assign
),
1396 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1399 result
= do_assignment(instructions
, state
,
1400 this->subexpressions
[0]->non_lvalue_description
,
1401 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1402 this->subexpressions
[0]->get_location());
1403 error_emitted
= (op
[0]->type
->is_error());
1405 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1406 * explicitly test for this because none of the binary expression
1407 * operators allow array operands either.
1413 case ast_mod_assign
: {
1414 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1415 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1417 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1419 assert(operations
[this->oper
] == ir_binop_mod
);
1421 ir_rvalue
*temp_rhs
;
1422 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1425 result
= do_assignment(instructions
, state
,
1426 this->subexpressions
[0]->non_lvalue_description
,
1427 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1428 this->subexpressions
[0]->get_location());
1429 error_emitted
= type
->is_error();
1434 case ast_rs_assign
: {
1435 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1436 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1437 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1439 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1440 type
, op
[0], op
[1]);
1441 result
= do_assignment(instructions
, state
,
1442 this->subexpressions
[0]->non_lvalue_description
,
1443 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1444 this->subexpressions
[0]->get_location());
1445 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1449 case ast_and_assign
:
1450 case ast_xor_assign
:
1451 case ast_or_assign
: {
1452 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1453 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1454 type
= bit_logic_result_type(op
[0]->type
, op
[1]->type
, this->oper
,
1456 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1457 type
, op
[0], op
[1]);
1458 result
= do_assignment(instructions
, state
,
1459 this->subexpressions
[0]->non_lvalue_description
,
1460 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1461 this->subexpressions
[0]->get_location());
1462 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1466 case ast_conditional
: {
1467 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1469 * "The ternary selection operator (?:). It operates on three
1470 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1471 * first expression, which must result in a scalar Boolean."
1473 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1474 "condition", &error_emitted
);
1476 /* The :? operator is implemented by generating an anonymous temporary
1477 * followed by an if-statement. The last instruction in each branch of
1478 * the if-statement assigns a value to the anonymous temporary. This
1479 * temporary is the r-value of the expression.
1481 exec_list then_instructions
;
1482 exec_list else_instructions
;
1484 op
[1] = this->subexpressions
[1]->hir(&then_instructions
, state
);
1485 op
[2] = this->subexpressions
[2]->hir(&else_instructions
, state
);
1487 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1489 * "The second and third expressions can be any type, as
1490 * long their types match, or there is a conversion in
1491 * Section 4.1.10 "Implicit Conversions" that can be applied
1492 * to one of the expressions to make their types match. This
1493 * resulting matching type is the type of the entire
1496 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1497 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1498 || (op
[1]->type
!= op
[2]->type
)) {
1499 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1501 _mesa_glsl_error(& loc
, state
, "second and third operands of ?: "
1502 "operator must have matching types");
1503 error_emitted
= true;
1504 type
= glsl_type::error_type
;
1509 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1511 * "The second and third expressions must be the same type, but can
1512 * be of any type other than an array."
1514 if (type
->is_array() &&
1515 !state
->check_version(120, 300, &loc
,
1516 "second and third operands of ?: operator "
1517 "cannot be arrays")) {
1518 error_emitted
= true;
1521 ir_constant
*cond_val
= op
[0]->constant_expression_value();
1522 ir_constant
*then_val
= op
[1]->constant_expression_value();
1523 ir_constant
*else_val
= op
[2]->constant_expression_value();
1525 if (then_instructions
.is_empty()
1526 && else_instructions
.is_empty()
1527 && (cond_val
!= NULL
) && (then_val
!= NULL
) && (else_val
!= NULL
)) {
1528 result
= (cond_val
->value
.b
[0]) ? then_val
: else_val
;
1530 ir_variable
*const tmp
=
1531 new(ctx
) ir_variable(type
, "conditional_tmp", ir_var_temporary
);
1532 instructions
->push_tail(tmp
);
1534 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1535 instructions
->push_tail(stmt
);
1537 then_instructions
.move_nodes_to(& stmt
->then_instructions
);
1538 ir_dereference
*const then_deref
=
1539 new(ctx
) ir_dereference_variable(tmp
);
1540 ir_assignment
*const then_assign
=
1541 new(ctx
) ir_assignment(then_deref
, op
[1]);
1542 stmt
->then_instructions
.push_tail(then_assign
);
1544 else_instructions
.move_nodes_to(& stmt
->else_instructions
);
1545 ir_dereference
*const else_deref
=
1546 new(ctx
) ir_dereference_variable(tmp
);
1547 ir_assignment
*const else_assign
=
1548 new(ctx
) ir_assignment(else_deref
, op
[2]);
1549 stmt
->else_instructions
.push_tail(else_assign
);
1551 result
= new(ctx
) ir_dereference_variable(tmp
);
1558 this->non_lvalue_description
= (this->oper
== ast_pre_inc
)
1559 ? "pre-increment operation" : "pre-decrement operation";
1561 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1562 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1564 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1566 ir_rvalue
*temp_rhs
;
1567 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1570 result
= do_assignment(instructions
, state
,
1571 this->subexpressions
[0]->non_lvalue_description
,
1572 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1573 this->subexpressions
[0]->get_location());
1574 error_emitted
= op
[0]->type
->is_error();
1579 case ast_post_dec
: {
1580 this->non_lvalue_description
= (this->oper
== ast_post_inc
)
1581 ? "post-increment operation" : "post-decrement operation";
1582 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1583 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1585 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1587 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1589 ir_rvalue
*temp_rhs
;
1590 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1593 /* Get a temporary of a copy of the lvalue before it's modified.
1594 * This may get thrown away later.
1596 result
= get_lvalue_copy(instructions
, op
[0]->clone(ctx
, NULL
));
1598 (void)do_assignment(instructions
, state
,
1599 this->subexpressions
[0]->non_lvalue_description
,
1600 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1601 this->subexpressions
[0]->get_location());
1603 error_emitted
= op
[0]->type
->is_error();
1607 case ast_field_selection
:
1608 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1611 case ast_array_index
: {
1612 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1614 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1615 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1617 result
= _mesa_ast_array_index_to_hir(ctx
, state
, op
[0], op
[1],
1620 if (result
->type
->is_error())
1621 error_emitted
= true;
1626 case ast_function_call
:
1627 /* Should *NEVER* get here. ast_function_call should always be handled
1628 * by ast_function_expression::hir.
1633 case ast_identifier
: {
1634 /* ast_identifier can appear several places in a full abstract syntax
1635 * tree. This particular use must be at location specified in the grammar
1636 * as 'variable_identifier'.
1639 state
->symbols
->get_variable(this->primary_expression
.identifier
);
1642 var
->data
.used
= true;
1643 result
= new(ctx
) ir_dereference_variable(var
);
1645 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
1646 this->primary_expression
.identifier
);
1648 result
= ir_rvalue::error_value(ctx
);
1649 error_emitted
= true;
1654 case ast_int_constant
:
1655 result
= new(ctx
) ir_constant(this->primary_expression
.int_constant
);
1658 case ast_uint_constant
:
1659 result
= new(ctx
) ir_constant(this->primary_expression
.uint_constant
);
1662 case ast_float_constant
:
1663 result
= new(ctx
) ir_constant(this->primary_expression
.float_constant
);
1666 case ast_bool_constant
:
1667 result
= new(ctx
) ir_constant(bool(this->primary_expression
.bool_constant
));
1670 case ast_sequence
: {
1671 /* It should not be possible to generate a sequence in the AST without
1672 * any expressions in it.
1674 assert(!this->expressions
.is_empty());
1676 /* The r-value of a sequence is the last expression in the sequence. If
1677 * the other expressions in the sequence do not have side-effects (and
1678 * therefore add instructions to the instruction list), they get dropped
1681 exec_node
*previous_tail_pred
= NULL
;
1682 YYLTYPE previous_operand_loc
= loc
;
1684 foreach_list_typed (ast_node
, ast
, link
, &this->expressions
) {
1685 /* If one of the operands of comma operator does not generate any
1686 * code, we want to emit a warning. At each pass through the loop
1687 * previous_tail_pred will point to the last instruction in the
1688 * stream *before* processing the previous operand. Naturally,
1689 * instructions->tail_pred will point to the last instruction in the
1690 * stream *after* processing the previous operand. If the two
1691 * pointers match, then the previous operand had no effect.
1693 * The warning behavior here differs slightly from GCC. GCC will
1694 * only emit a warning if none of the left-hand operands have an
1695 * effect. However, it will emit a warning for each. I believe that
1696 * there are some cases in C (especially with GCC extensions) where
1697 * it is useful to have an intermediate step in a sequence have no
1698 * effect, but I don't think these cases exist in GLSL. Either way,
1699 * it would be a giant hassle to replicate that behavior.
1701 if (previous_tail_pred
== instructions
->tail_pred
) {
1702 _mesa_glsl_warning(&previous_operand_loc
, state
,
1703 "left-hand operand of comma expression has "
1707 /* tail_pred is directly accessed instead of using the get_tail()
1708 * method for performance reasons. get_tail() has extra code to
1709 * return NULL when the list is empty. We don't care about that
1710 * here, so using tail_pred directly is fine.
1712 previous_tail_pred
= instructions
->tail_pred
;
1713 previous_operand_loc
= ast
->get_location();
1715 result
= ast
->hir(instructions
, state
);
1718 /* Any errors should have already been emitted in the loop above.
1720 error_emitted
= true;
1724 type
= NULL
; /* use result->type, not type. */
1725 assert(result
!= NULL
);
1727 if (result
->type
->is_error() && !error_emitted
)
1728 _mesa_glsl_error(& loc
, state
, "type mismatch");
1735 ast_expression_statement::hir(exec_list
*instructions
,
1736 struct _mesa_glsl_parse_state
*state
)
1738 /* It is possible to have expression statements that don't have an
1739 * expression. This is the solitary semicolon:
1741 * for (i = 0; i < 5; i++)
1744 * In this case the expression will be NULL. Test for NULL and don't do
1745 * anything in that case.
1747 if (expression
!= NULL
)
1748 expression
->hir(instructions
, state
);
1750 /* Statements do not have r-values.
1757 ast_compound_statement::hir(exec_list
*instructions
,
1758 struct _mesa_glsl_parse_state
*state
)
1761 state
->symbols
->push_scope();
1763 foreach_list_typed (ast_node
, ast
, link
, &this->statements
)
1764 ast
->hir(instructions
, state
);
1767 state
->symbols
->pop_scope();
1769 /* Compound statements do not have r-values.
1775 static const glsl_type
*
1776 process_array_type(YYLTYPE
*loc
, const glsl_type
*base
, ast_node
*array_size
,
1777 struct _mesa_glsl_parse_state
*state
)
1779 unsigned length
= 0;
1782 return glsl_type::error_type
;
1784 /* From page 19 (page 25) of the GLSL 1.20 spec:
1786 * "Only one-dimensional arrays may be declared."
1788 if (base
->is_array()) {
1789 _mesa_glsl_error(loc
, state
,
1790 "invalid array of `%s' (only one-dimensional arrays "
1793 return glsl_type::error_type
;
1796 if (array_size
!= NULL
) {
1797 exec_list dummy_instructions
;
1798 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
1799 YYLTYPE loc
= array_size
->get_location();
1802 if (!ir
->type
->is_integer()) {
1803 _mesa_glsl_error(& loc
, state
, "array size must be integer type");
1804 } else if (!ir
->type
->is_scalar()) {
1805 _mesa_glsl_error(& loc
, state
, "array size must be scalar type");
1807 ir_constant
*const size
= ir
->constant_expression_value();
1810 _mesa_glsl_error(& loc
, state
, "array size must be a "
1811 "constant valued expression");
1812 } else if (size
->value
.i
[0] <= 0) {
1813 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
1815 assert(size
->type
== ir
->type
);
1816 length
= size
->value
.u
[0];
1818 /* If the array size is const (and we've verified that
1819 * it is) then no instructions should have been emitted
1820 * when we converted it to HIR. If they were emitted,
1821 * then either the array size isn't const after all, or
1822 * we are emitting unnecessary instructions.
1824 assert(dummy_instructions
.is_empty());
1830 const glsl_type
*array_type
= glsl_type::get_array_instance(base
, length
);
1831 return array_type
!= NULL
? array_type
: glsl_type::error_type
;
1836 ast_type_specifier::glsl_type(const char **name
,
1837 struct _mesa_glsl_parse_state
*state
) const
1839 const struct glsl_type
*type
;
1841 type
= state
->symbols
->get_type(this->type_name
);
1842 *name
= this->type_name
;
1844 if (this->is_array
) {
1845 YYLTYPE loc
= this->get_location();
1846 type
= process_array_type(&loc
, type
, this->array_size
, state
);
1853 ast_fully_specified_type::glsl_type(const char **name
,
1854 struct _mesa_glsl_parse_state
*state
) const
1856 const struct glsl_type
*type
= this->specifier
->glsl_type(name
, state
);
1861 if (type
->base_type
== GLSL_TYPE_FLOAT
1863 && state
->target
== fragment_shader
1864 && this->qualifier
.precision
== ast_precision_none
1865 && state
->symbols
->get_variable("#default precision") == NULL
) {
1866 YYLTYPE loc
= this->get_location();
1867 _mesa_glsl_error(&loc
, state
,
1868 "no precision specified this scope for type `%s'",
1876 * Determine whether a toplevel variable declaration declares a varying. This
1877 * function operates by examining the variable's mode and the shader target,
1878 * so it correctly identifies linkage variables regardless of whether they are
1879 * declared using the deprecated "varying" syntax or the new "in/out" syntax.
1881 * Passing a non-toplevel variable declaration (e.g. a function parameter) to
1882 * this function will produce undefined results.
1885 is_varying_var(ir_variable
*var
, _mesa_glsl_parser_targets target
)
1889 return var
->data
.mode
== ir_var_shader_out
;
1890 case fragment_shader
:
1891 return var
->data
.mode
== ir_var_shader_in
;
1893 return var
->data
.mode
== ir_var_shader_out
|| var
->data
.mode
== ir_var_shader_in
;
1899 * Matrix layout qualifiers are only allowed on certain types
1902 validate_matrix_layout_for_type(struct _mesa_glsl_parse_state
*state
,
1904 const glsl_type
*type
,
1907 if (var
&& !var
->is_in_uniform_block()) {
1908 /* Layout qualifiers may only apply to interface blocks and fields in
1911 _mesa_glsl_error(loc
, state
,
1912 "uniform block layout qualifiers row_major and "
1913 "column_major may not be applied to variables "
1914 "outside of uniform blocks");
1915 } else if (!type
->is_matrix()) {
1916 /* The OpenGL ES 3.0 conformance tests did not originally allow
1917 * matrix layout qualifiers on non-matrices. However, the OpenGL
1918 * 4.4 and OpenGL ES 3.0 (revision TBD) specifications were
1919 * amended to specifically allow these layouts on all types. Emit
1920 * a warning so that people know their code may not be portable.
1922 _mesa_glsl_warning(loc
, state
,
1923 "uniform block layout qualifiers row_major and "
1924 "column_major applied to non-matrix types may "
1925 "be rejected by older compilers");
1926 } else if (type
->is_record()) {
1927 /* We allow 'layout(row_major)' on structure types because it's the only
1928 * way to get row-major layouts on matrices contained in structures.
1930 _mesa_glsl_warning(loc
, state
,
1931 "uniform block layout qualifiers row_major and "
1932 "column_major applied to structure types is not "
1933 "strictly conformant and may be rejected by other "
1939 validate_binding_qualifier(struct _mesa_glsl_parse_state
*state
,
1942 const ast_type_qualifier
*qual
)
1944 if (var
->data
.mode
!= ir_var_uniform
) {
1945 _mesa_glsl_error(loc
, state
,
1946 "the \"binding\" qualifier only applies to uniforms");
1950 if (qual
->binding
< 0) {
1951 _mesa_glsl_error(loc
, state
, "binding values must be >= 0");
1955 const struct gl_context
*const ctx
= state
->ctx
;
1956 unsigned elements
= var
->type
->is_array() ? var
->type
->length
: 1;
1957 unsigned max_index
= qual
->binding
+ elements
- 1;
1959 if (var
->type
->is_interface()) {
1960 /* UBOs. From page 60 of the GLSL 4.20 specification:
1961 * "If the binding point for any uniform block instance is less than zero,
1962 * or greater than or equal to the implementation-dependent maximum
1963 * number of uniform buffer bindings, a compilation error will occur.
1964 * When the binding identifier is used with a uniform block instanced as
1965 * an array of size N, all elements of the array from binding through
1966 * binding + N – 1 must be within this range."
1968 * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
1970 if (max_index
>= ctx
->Const
.MaxUniformBufferBindings
) {
1971 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d UBOs exceeds "
1972 "the maximum number of UBO binding points (%d)",
1973 qual
->binding
, elements
,
1974 ctx
->Const
.MaxUniformBufferBindings
);
1977 } else if (var
->type
->is_sampler() ||
1978 (var
->type
->is_array() && var
->type
->fields
.array
->is_sampler())) {
1979 /* Samplers. From page 63 of the GLSL 4.20 specification:
1980 * "If the binding is less than zero, or greater than or equal to the
1981 * implementation-dependent maximum supported number of units, a
1982 * compilation error will occur. When the binding identifier is used
1983 * with an array of size N, all elements of the array from binding
1984 * through binding + N - 1 must be within this range."
1987 switch (state
->target
) {
1989 limit
= ctx
->Const
.VertexProgram
.MaxTextureImageUnits
;
1991 case geometry_shader
:
1992 limit
= ctx
->Const
.GeometryProgram
.MaxTextureImageUnits
;
1994 case fragment_shader
:
1995 limit
= ctx
->Const
.FragmentProgram
.MaxTextureImageUnits
;
1999 if (max_index
>= limit
) {
2000 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d samplers "
2001 "exceeds the maximum number of texture image units "
2002 "(%d)", qual
->binding
, elements
, limit
);
2006 } else if (var
->type
->contains_atomic()) {
2007 assert(ctx
->Const
.MaxAtomicBufferBindings
<= MAX_COMBINED_ATOMIC_BUFFERS
);
2008 if (unsigned(qual
->binding
) >= ctx
->Const
.MaxAtomicBufferBindings
) {
2009 _mesa_glsl_error(loc
, state
, "layout(binding = %d) exceeds the "
2010 " maximum number of atomic counter buffer bindings"
2011 "(%d)", qual
->binding
,
2012 ctx
->Const
.MaxAtomicBufferBindings
);
2017 _mesa_glsl_error(loc
, state
,
2018 "the \"binding\" qualifier only applies to uniform "
2019 "blocks, samplers, atomic counters, or arrays thereof");
2027 static glsl_interp_qualifier
2028 interpret_interpolation_qualifier(const struct ast_type_qualifier
*qual
,
2029 ir_variable_mode mode
,
2030 struct _mesa_glsl_parse_state
*state
,
2033 glsl_interp_qualifier interpolation
;
2034 if (qual
->flags
.q
.flat
)
2035 interpolation
= INTERP_QUALIFIER_FLAT
;
2036 else if (qual
->flags
.q
.noperspective
)
2037 interpolation
= INTERP_QUALIFIER_NOPERSPECTIVE
;
2038 else if (qual
->flags
.q
.smooth
)
2039 interpolation
= INTERP_QUALIFIER_SMOOTH
;
2041 interpolation
= INTERP_QUALIFIER_NONE
;
2043 if (interpolation
!= INTERP_QUALIFIER_NONE
) {
2044 if (mode
!= ir_var_shader_in
&& mode
!= ir_var_shader_out
) {
2045 _mesa_glsl_error(loc
, state
,
2046 "interpolation qualifier `%s' can only be applied to "
2047 "shader inputs or outputs.",
2048 interpolation_string(interpolation
));
2052 if ((state
->target
== vertex_shader
&& mode
== ir_var_shader_in
) ||
2053 (state
->target
== fragment_shader
&& mode
== ir_var_shader_out
)) {
2054 _mesa_glsl_error(loc
, state
,
2055 "interpolation qualifier `%s' cannot be applied to "
2056 "vertex shader inputs or fragment shader outputs",
2057 interpolation_string(interpolation
));
2061 return interpolation
;
2066 validate_explicit_location(const struct ast_type_qualifier
*qual
,
2068 struct _mesa_glsl_parse_state
*state
,
2073 /* In the vertex shader only shader inputs can be given explicit
2076 * In the fragment shader only shader outputs can be given explicit
2079 switch (state
->target
) {
2081 if (var
->data
.mode
== ir_var_shader_in
) {
2082 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
2091 case geometry_shader
:
2092 _mesa_glsl_error(loc
, state
,
2093 "geometry shader variables cannot be given "
2094 "explicit locations");
2097 case fragment_shader
:
2098 if (var
->data
.mode
== ir_var_shader_out
) {
2099 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
2110 _mesa_glsl_error(loc
, state
,
2111 "%s cannot be given an explicit location in %s shader",
2113 _mesa_glsl_shader_target_name(state
->target
));
2115 var
->data
.explicit_location
= true;
2117 /* This bit of silliness is needed because invalid explicit locations
2118 * are supposed to be flagged during linking. Small negative values
2119 * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
2120 * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
2121 * The linker needs to be able to differentiate these cases. This
2122 * ensures that negative values stay negative.
2124 if (qual
->location
>= 0) {
2125 var
->data
.location
= (state
->target
== vertex_shader
)
2126 ? (qual
->location
+ VERT_ATTRIB_GENERIC0
)
2127 : (qual
->location
+ FRAG_RESULT_DATA0
);
2129 var
->data
.location
= qual
->location
;
2132 if (qual
->flags
.q
.explicit_index
) {
2133 /* From the GLSL 4.30 specification, section 4.4.2 (Output
2134 * Layout Qualifiers):
2136 * "It is also a compile-time error if a fragment shader
2137 * sets a layout index to less than 0 or greater than 1."
2139 * Older specifications don't mandate a behavior; we take
2140 * this as a clarification and always generate the error.
2142 if (qual
->index
< 0 || qual
->index
> 1) {
2143 _mesa_glsl_error(loc
, state
,
2144 "explicit index may only be 0 or 1");
2146 var
->data
.explicit_index
= true;
2147 var
->data
.index
= qual
->index
;
2156 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
2158 struct _mesa_glsl_parse_state
*state
,
2162 STATIC_ASSERT(sizeof(qual
->flags
.q
) <= sizeof(qual
->flags
.i
));
2164 if (qual
->flags
.q
.invariant
) {
2165 if (var
->data
.used
) {
2166 _mesa_glsl_error(loc
, state
,
2167 "variable `%s' may not be redeclared "
2168 "`invariant' after being used",
2171 var
->data
.invariant
= 1;
2175 if (qual
->flags
.q
.constant
|| qual
->flags
.q
.attribute
2176 || qual
->flags
.q
.uniform
2177 || (qual
->flags
.q
.varying
&& (state
->target
== fragment_shader
)))
2178 var
->data
.read_only
= 1;
2180 if (qual
->flags
.q
.centroid
)
2181 var
->data
.centroid
= 1;
2183 if (qual
->flags
.q
.sample
)
2184 var
->data
.sample
= 1;
2186 if (qual
->flags
.q
.attribute
&& state
->target
!= vertex_shader
) {
2187 var
->type
= glsl_type::error_type
;
2188 _mesa_glsl_error(loc
, state
,
2189 "`attribute' variables may not be declared in the "
2191 _mesa_glsl_shader_target_name(state
->target
));
2194 /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
2196 * "However, the const qualifier cannot be used with out or inout."
2198 * The same section of the GLSL 4.40 spec further clarifies this saying:
2200 * "The const qualifier cannot be used with out or inout, or a
2201 * compile-time error results."
2203 if (is_parameter
&& qual
->flags
.q
.constant
&& qual
->flags
.q
.out
) {
2204 _mesa_glsl_error(loc
, state
,
2205 "`const' may not be applied to `out' or `inout' "
2206 "function parameters");
2209 /* If there is no qualifier that changes the mode of the variable, leave
2210 * the setting alone.
2212 if (qual
->flags
.q
.in
&& qual
->flags
.q
.out
)
2213 var
->data
.mode
= ir_var_function_inout
;
2214 else if (qual
->flags
.q
.in
)
2215 var
->data
.mode
= is_parameter
? ir_var_function_in
: ir_var_shader_in
;
2216 else if (qual
->flags
.q
.attribute
2217 || (qual
->flags
.q
.varying
&& (state
->target
== fragment_shader
)))
2218 var
->data
.mode
= ir_var_shader_in
;
2219 else if (qual
->flags
.q
.out
)
2220 var
->data
.mode
= is_parameter
? ir_var_function_out
: ir_var_shader_out
;
2221 else if (qual
->flags
.q
.varying
&& (state
->target
== vertex_shader
))
2222 var
->data
.mode
= ir_var_shader_out
;
2223 else if (qual
->flags
.q
.uniform
)
2224 var
->data
.mode
= ir_var_uniform
;
2226 if (!is_parameter
&& is_varying_var(var
, state
->target
)) {
2227 /* This variable is being used to link data between shader stages (in
2228 * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
2229 * that is allowed for such purposes.
2231 * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
2233 * "The varying qualifier can be used only with the data types
2234 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
2237 * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
2238 * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
2240 * "Fragment inputs can only be signed and unsigned integers and
2241 * integer vectors, float, floating-point vectors, matrices, or
2242 * arrays of these. Structures cannot be input.
2244 * Similar text exists in the section on vertex shader outputs.
2246 * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
2247 * 3.00 spec allows structs as well. Varying structs are also allowed
2250 switch (var
->type
->get_scalar_type()->base_type
) {
2251 case GLSL_TYPE_FLOAT
:
2252 /* Ok in all GLSL versions */
2254 case GLSL_TYPE_UINT
:
2256 if (state
->is_version(130, 300))
2258 _mesa_glsl_error(loc
, state
,
2259 "varying variables must be of base type float in %s",
2260 state
->get_version_string());
2262 case GLSL_TYPE_STRUCT
:
2263 if (state
->is_version(150, 300))
2265 _mesa_glsl_error(loc
, state
,
2266 "varying variables may not be of type struct");
2269 _mesa_glsl_error(loc
, state
, "illegal type for a varying variable");
2274 if (state
->all_invariant
&& (state
->current_function
== NULL
)) {
2275 switch (state
->target
) {
2277 if (var
->data
.mode
== ir_var_shader_out
)
2278 var
->data
.invariant
= true;
2280 case geometry_shader
:
2281 if ((var
->data
.mode
== ir_var_shader_in
)
2282 || (var
->data
.mode
== ir_var_shader_out
))
2283 var
->data
.invariant
= true;
2285 case fragment_shader
:
2286 if (var
->data
.mode
== ir_var_shader_in
)
2287 var
->data
.invariant
= true;
2292 var
->data
.interpolation
=
2293 interpret_interpolation_qualifier(qual
, (ir_variable_mode
) var
->data
.mode
,
2296 var
->data
.pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
2297 var
->data
.origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
2298 if ((qual
->flags
.q
.origin_upper_left
|| qual
->flags
.q
.pixel_center_integer
)
2299 && (strcmp(var
->name
, "gl_FragCoord") != 0)) {
2300 const char *const qual_string
= (qual
->flags
.q
.origin_upper_left
)
2301 ? "origin_upper_left" : "pixel_center_integer";
2303 _mesa_glsl_error(loc
, state
,
2304 "layout qualifier `%s' can only be applied to "
2305 "fragment shader input `gl_FragCoord'",
2309 if (qual
->flags
.q
.explicit_location
) {
2310 validate_explicit_location(qual
, var
, state
, loc
);
2311 } else if (qual
->flags
.q
.explicit_index
) {
2312 _mesa_glsl_error(loc
, state
,
2313 "explicit index requires explicit location");
2316 if (qual
->flags
.q
.explicit_binding
&&
2317 validate_binding_qualifier(state
, loc
, var
, qual
)) {
2318 var
->data
.explicit_binding
= true;
2319 var
->data
.binding
= qual
->binding
;
2322 if (var
->type
->contains_atomic()) {
2323 if (var
->data
.mode
== ir_var_uniform
) {
2324 if (var
->data
.explicit_binding
) {
2326 &state
->atomic_counter_offsets
[var
->data
.binding
];
2328 if (*offset
% ATOMIC_COUNTER_SIZE
)
2329 _mesa_glsl_error(loc
, state
,
2330 "misaligned atomic counter offset");
2332 var
->data
.atomic
.offset
= *offset
;
2333 *offset
+= var
->type
->atomic_size();
2336 _mesa_glsl_error(loc
, state
,
2337 "atomic counters require explicit binding point");
2339 } else if (var
->data
.mode
!= ir_var_function_in
) {
2340 _mesa_glsl_error(loc
, state
, "atomic counters may only be declared as "
2341 "function parameters or uniform-qualified "
2342 "global variables");
2346 /* Does the declaration use the deprecated 'attribute' or 'varying'
2349 const bool uses_deprecated_qualifier
= qual
->flags
.q
.attribute
2350 || qual
->flags
.q
.varying
;
2352 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
2353 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
2354 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
2355 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
2356 * These extensions and all following extensions that add the 'layout'
2357 * keyword have been modified to require the use of 'in' or 'out'.
2359 * The following extension do not allow the deprecated keywords:
2361 * GL_AMD_conservative_depth
2362 * GL_ARB_conservative_depth
2363 * GL_ARB_gpu_shader5
2364 * GL_ARB_separate_shader_objects
2365 * GL_ARB_tesselation_shader
2366 * GL_ARB_transform_feedback3
2367 * GL_ARB_uniform_buffer_object
2369 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
2370 * allow layout with the deprecated keywords.
2372 const bool relaxed_layout_qualifier_checking
=
2373 state
->ARB_fragment_coord_conventions_enable
;
2375 if (qual
->has_layout() && uses_deprecated_qualifier
) {
2376 if (relaxed_layout_qualifier_checking
) {
2377 _mesa_glsl_warning(loc
, state
,
2378 "`layout' qualifier may not be used with "
2379 "`attribute' or `varying'");
2381 _mesa_glsl_error(loc
, state
,
2382 "`layout' qualifier may not be used with "
2383 "`attribute' or `varying'");
2387 /* Layout qualifiers for gl_FragDepth, which are enabled by extension
2388 * AMD_conservative_depth.
2390 int depth_layout_count
= qual
->flags
.q
.depth_any
2391 + qual
->flags
.q
.depth_greater
2392 + qual
->flags
.q
.depth_less
2393 + qual
->flags
.q
.depth_unchanged
;
2394 if (depth_layout_count
> 0
2395 && !state
->AMD_conservative_depth_enable
2396 && !state
->ARB_conservative_depth_enable
) {
2397 _mesa_glsl_error(loc
, state
,
2398 "extension GL_AMD_conservative_depth or "
2399 "GL_ARB_conservative_depth must be enabled "
2400 "to use depth layout qualifiers");
2401 } else if (depth_layout_count
> 0
2402 && strcmp(var
->name
, "gl_FragDepth") != 0) {
2403 _mesa_glsl_error(loc
, state
,
2404 "depth layout qualifiers can be applied only to "
2406 } else if (depth_layout_count
> 1
2407 && strcmp(var
->name
, "gl_FragDepth") == 0) {
2408 _mesa_glsl_error(loc
, state
,
2409 "at most one depth layout qualifier can be applied to "
2412 if (qual
->flags
.q
.depth_any
)
2413 var
->data
.depth_layout
= ir_depth_layout_any
;
2414 else if (qual
->flags
.q
.depth_greater
)
2415 var
->data
.depth_layout
= ir_depth_layout_greater
;
2416 else if (qual
->flags
.q
.depth_less
)
2417 var
->data
.depth_layout
= ir_depth_layout_less
;
2418 else if (qual
->flags
.q
.depth_unchanged
)
2419 var
->data
.depth_layout
= ir_depth_layout_unchanged
;
2421 var
->data
.depth_layout
= ir_depth_layout_none
;
2423 if (qual
->flags
.q
.std140
||
2424 qual
->flags
.q
.packed
||
2425 qual
->flags
.q
.shared
) {
2426 _mesa_glsl_error(loc
, state
,
2427 "uniform block layout qualifiers std140, packed, and "
2428 "shared can only be applied to uniform blocks, not "
2432 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
2433 validate_matrix_layout_for_type(state
, loc
, var
->type
, var
);
2438 * Get the variable that is being redeclared by this declaration
2440 * Semantic checks to verify the validity of the redeclaration are also
2441 * performed. If semantic checks fail, compilation error will be emitted via
2442 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
2445 * A pointer to an existing variable in the current scope if the declaration
2446 * is a redeclaration, \c NULL otherwise.
2448 static ir_variable
*
2449 get_variable_being_redeclared(ir_variable
*var
, YYLTYPE loc
,
2450 struct _mesa_glsl_parse_state
*state
,
2451 bool allow_all_redeclarations
)
2453 /* Check if this declaration is actually a re-declaration, either to
2454 * resize an array or add qualifiers to an existing variable.
2456 * This is allowed for variables in the current scope, or when at
2457 * global scope (for built-ins in the implicit outer scope).
2459 ir_variable
*earlier
= state
->symbols
->get_variable(var
->name
);
2460 if (earlier
== NULL
||
2461 (state
->current_function
!= NULL
&&
2462 !state
->symbols
->name_declared_this_scope(var
->name
))) {
2467 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
2469 * "It is legal to declare an array without a size and then
2470 * later re-declare the same name as an array of the same
2471 * type and specify a size."
2473 if (earlier
->type
->is_unsized_array() && var
->type
->is_array()
2474 && (var
->type
->element_type() == earlier
->type
->element_type())) {
2475 /* FINISHME: This doesn't match the qualifiers on the two
2476 * FINISHME: declarations. It's not 100% clear whether this is
2477 * FINISHME: required or not.
2480 const unsigned size
= unsigned(var
->type
->array_size());
2481 check_builtin_array_max_size(var
->name
, size
, loc
, state
);
2482 if ((size
> 0) && (size
<= earlier
->data
.max_array_access
)) {
2483 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
2485 earlier
->data
.max_array_access
);
2488 earlier
->type
= var
->type
;
2491 } else if ((state
->ARB_fragment_coord_conventions_enable
||
2492 state
->is_version(150, 0))
2493 && strcmp(var
->name
, "gl_FragCoord") == 0
2494 && earlier
->type
== var
->type
2495 && earlier
->data
.mode
== var
->data
.mode
) {
2496 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
2499 earlier
->data
.origin_upper_left
= var
->data
.origin_upper_left
;
2500 earlier
->data
.pixel_center_integer
= var
->data
.pixel_center_integer
;
2502 /* According to section 4.3.7 of the GLSL 1.30 spec,
2503 * the following built-in varaibles can be redeclared with an
2504 * interpolation qualifier:
2507 * * gl_FrontSecondaryColor
2508 * * gl_BackSecondaryColor
2510 * * gl_SecondaryColor
2512 } else if (state
->is_version(130, 0)
2513 && (strcmp(var
->name
, "gl_FrontColor") == 0
2514 || strcmp(var
->name
, "gl_BackColor") == 0
2515 || strcmp(var
->name
, "gl_FrontSecondaryColor") == 0
2516 || strcmp(var
->name
, "gl_BackSecondaryColor") == 0
2517 || strcmp(var
->name
, "gl_Color") == 0
2518 || strcmp(var
->name
, "gl_SecondaryColor") == 0)
2519 && earlier
->type
== var
->type
2520 && earlier
->data
.mode
== var
->data
.mode
) {
2521 earlier
->data
.interpolation
= var
->data
.interpolation
;
2523 /* Layout qualifiers for gl_FragDepth. */
2524 } else if ((state
->AMD_conservative_depth_enable
||
2525 state
->ARB_conservative_depth_enable
)
2526 && strcmp(var
->name
, "gl_FragDepth") == 0
2527 && earlier
->type
== var
->type
2528 && earlier
->data
.mode
== var
->data
.mode
) {
2530 /** From the AMD_conservative_depth spec:
2531 * Within any shader, the first redeclarations of gl_FragDepth
2532 * must appear before any use of gl_FragDepth.
2534 if (earlier
->data
.used
) {
2535 _mesa_glsl_error(&loc
, state
,
2536 "the first redeclaration of gl_FragDepth "
2537 "must appear before any use of gl_FragDepth");
2540 /* Prevent inconsistent redeclaration of depth layout qualifier. */
2541 if (earlier
->data
.depth_layout
!= ir_depth_layout_none
2542 && earlier
->data
.depth_layout
!= var
->data
.depth_layout
) {
2543 _mesa_glsl_error(&loc
, state
,
2544 "gl_FragDepth: depth layout is declared here "
2545 "as '%s, but it was previously declared as "
2547 depth_layout_string(var
->data
.depth_layout
),
2548 depth_layout_string(earlier
->data
.depth_layout
));
2551 earlier
->data
.depth_layout
= var
->data
.depth_layout
;
2553 } else if (allow_all_redeclarations
) {
2554 if (earlier
->data
.mode
!= var
->data
.mode
) {
2555 _mesa_glsl_error(&loc
, state
,
2556 "redeclaration of `%s' with incorrect qualifiers",
2558 } else if (earlier
->type
!= var
->type
) {
2559 _mesa_glsl_error(&loc
, state
,
2560 "redeclaration of `%s' has incorrect type",
2564 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
2571 * Generate the IR for an initializer in a variable declaration
2574 process_initializer(ir_variable
*var
, ast_declaration
*decl
,
2575 ast_fully_specified_type
*type
,
2576 exec_list
*initializer_instructions
,
2577 struct _mesa_glsl_parse_state
*state
)
2579 ir_rvalue
*result
= NULL
;
2581 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
2583 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
2585 * "All uniform variables are read-only and are initialized either
2586 * directly by an application via API commands, or indirectly by
2589 if (var
->data
.mode
== ir_var_uniform
) {
2590 state
->check_version(120, 0, &initializer_loc
,
2591 "cannot initialize uniforms");
2594 if (var
->type
->is_sampler()) {
2595 _mesa_glsl_error(& initializer_loc
, state
,
2596 "cannot initialize samplers");
2599 if ((var
->data
.mode
== ir_var_shader_in
) && (state
->current_function
== NULL
)) {
2600 _mesa_glsl_error(& initializer_loc
, state
,
2601 "cannot initialize %s shader input / %s",
2602 _mesa_glsl_shader_target_name(state
->target
),
2603 (state
->target
== vertex_shader
)
2604 ? "attribute" : "varying");
2607 ir_dereference
*const lhs
= new(state
) ir_dereference_variable(var
);
2608 ir_rvalue
*rhs
= decl
->initializer
->hir(initializer_instructions
,
2611 /* Calculate the constant value if this is a const or uniform
2614 if (type
->qualifier
.flags
.q
.constant
2615 || type
->qualifier
.flags
.q
.uniform
) {
2616 ir_rvalue
*new_rhs
= validate_assignment(state
, initializer_loc
,
2617 var
->type
, rhs
, true);
2618 if (new_rhs
!= NULL
) {
2621 ir_constant
*constant_value
= rhs
->constant_expression_value();
2622 if (!constant_value
) {
2623 /* If ARB_shading_language_420pack is enabled, initializers of
2624 * const-qualified local variables do not have to be constant
2625 * expressions. Const-qualified global variables must still be
2626 * initialized with constant expressions.
2628 if (!state
->ARB_shading_language_420pack_enable
2629 || state
->current_function
== NULL
) {
2630 _mesa_glsl_error(& initializer_loc
, state
,
2631 "initializer of %s variable `%s' must be a "
2632 "constant expression",
2633 (type
->qualifier
.flags
.q
.constant
)
2634 ? "const" : "uniform",
2636 if (var
->type
->is_numeric()) {
2637 /* Reduce cascading errors. */
2638 var
->constant_value
= ir_constant::zero(state
, var
->type
);
2642 rhs
= constant_value
;
2643 var
->constant_value
= constant_value
;
2646 if (var
->type
->is_numeric()) {
2647 /* Reduce cascading errors. */
2648 var
->constant_value
= ir_constant::zero(state
, var
->type
);
2653 if (rhs
&& !rhs
->type
->is_error()) {
2654 bool temp
= var
->data
.read_only
;
2655 if (type
->qualifier
.flags
.q
.constant
)
2656 var
->data
.read_only
= false;
2658 /* Never emit code to initialize a uniform.
2660 const glsl_type
*initializer_type
;
2661 if (!type
->qualifier
.flags
.q
.uniform
) {
2662 result
= do_assignment(initializer_instructions
, state
,
2665 type
->get_location());
2666 initializer_type
= result
->type
;
2668 initializer_type
= rhs
->type
;
2670 var
->constant_initializer
= rhs
->constant_expression_value();
2671 var
->data
.has_initializer
= true;
2673 /* If the declared variable is an unsized array, it must inherrit
2674 * its full type from the initializer. A declaration such as
2676 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
2680 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
2682 * The assignment generated in the if-statement (below) will also
2683 * automatically handle this case for non-uniforms.
2685 * If the declared variable is not an array, the types must
2686 * already match exactly. As a result, the type assignment
2687 * here can be done unconditionally. For non-uniforms the call
2688 * to do_assignment can change the type of the initializer (via
2689 * the implicit conversion rules). For uniforms the initializer
2690 * must be a constant expression, and the type of that expression
2691 * was validated above.
2693 var
->type
= initializer_type
;
2695 var
->data
.read_only
= temp
;
2703 * Do additional processing necessary for geometry shader input declarations
2704 * (this covers both interface blocks arrays and bare input variables).
2707 handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
2708 YYLTYPE loc
, ir_variable
*var
)
2710 unsigned num_vertices
= 0;
2711 if (state
->gs_input_prim_type_specified
) {
2712 num_vertices
= vertices_per_prim(state
->gs_input_prim_type
);
2715 /* Geometry shader input variables must be arrays. Caller should have
2716 * reported an error for this.
2718 if (!var
->type
->is_array()) {
2719 assert(state
->error
);
2721 /* To avoid cascading failures, short circuit the checks below. */
2725 if (var
->type
->is_unsized_array()) {
2726 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says:
2728 * All geometry shader input unsized array declarations will be
2729 * sized by an earlier input layout qualifier, when present, as per
2730 * the following table.
2732 * Followed by a table mapping each allowed input layout qualifier to
2733 * the corresponding input length.
2735 if (num_vertices
!= 0)
2736 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
2739 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec
2740 * includes the following examples of compile-time errors:
2742 * // code sequence within one shader...
2743 * in vec4 Color1[]; // size unknown
2744 * ...Color1.length()...// illegal, length() unknown
2745 * in vec4 Color2[2]; // size is 2
2746 * ...Color1.length()...// illegal, Color1 still has no size
2747 * in vec4 Color3[3]; // illegal, input sizes are inconsistent
2748 * layout(lines) in; // legal, input size is 2, matching
2749 * in vec4 Color4[3]; // illegal, contradicts layout
2752 * To detect the case illustrated by Color3, we verify that the size of
2753 * an explicitly-sized array matches the size of any previously declared
2754 * explicitly-sized array. To detect the case illustrated by Color4, we
2755 * verify that the size of an explicitly-sized array is consistent with
2756 * any previously declared input layout.
2758 if (num_vertices
!= 0 && var
->type
->length
!= num_vertices
) {
2759 _mesa_glsl_error(&loc
, state
,
2760 "geometry shader input size contradicts previously"
2761 " declared layout (size is %u, but layout requires a"
2762 " size of %u)", var
->type
->length
, num_vertices
);
2763 } else if (state
->gs_input_size
!= 0 &&
2764 var
->type
->length
!= state
->gs_input_size
) {
2765 _mesa_glsl_error(&loc
, state
,
2766 "geometry shader input sizes are "
2767 "inconsistent (size is %u, but a previous "
2768 "declaration has size %u)",
2769 var
->type
->length
, state
->gs_input_size
);
2771 state
->gs_input_size
= var
->type
->length
;
2778 validate_identifier(const char *identifier
, YYLTYPE loc
,
2779 struct _mesa_glsl_parse_state
*state
)
2781 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
2783 * "Identifiers starting with "gl_" are reserved for use by
2784 * OpenGL, and may not be declared in a shader as either a
2785 * variable or a function."
2787 if (strncmp(identifier
, "gl_", 3) == 0) {
2788 _mesa_glsl_error(&loc
, state
,
2789 "identifier `%s' uses reserved `gl_' prefix",
2791 } else if (strstr(identifier
, "__")) {
2792 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
2795 * "In addition, all identifiers containing two
2796 * consecutive underscores (__) are reserved as
2797 * possible future keywords."
2799 _mesa_glsl_error(&loc
, state
,
2800 "identifier `%s' uses reserved `__' string",
2807 ast_declarator_list::hir(exec_list
*instructions
,
2808 struct _mesa_glsl_parse_state
*state
)
2811 const struct glsl_type
*decl_type
;
2812 const char *type_name
= NULL
;
2813 ir_rvalue
*result
= NULL
;
2814 YYLTYPE loc
= this->get_location();
2816 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
2818 * "To ensure that a particular output variable is invariant, it is
2819 * necessary to use the invariant qualifier. It can either be used to
2820 * qualify a previously declared variable as being invariant
2822 * invariant gl_Position; // make existing gl_Position be invariant"
2824 * In these cases the parser will set the 'invariant' flag in the declarator
2825 * list, and the type will be NULL.
2827 if (this->invariant
) {
2828 assert(this->type
== NULL
);
2830 if (state
->current_function
!= NULL
) {
2831 _mesa_glsl_error(& loc
, state
,
2832 "all uses of `invariant' keyword must be at global "
2836 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
2837 assert(!decl
->is_array
);
2838 assert(decl
->array_size
== NULL
);
2839 assert(decl
->initializer
== NULL
);
2841 ir_variable
*const earlier
=
2842 state
->symbols
->get_variable(decl
->identifier
);
2843 if (earlier
== NULL
) {
2844 _mesa_glsl_error(& loc
, state
,
2845 "undeclared variable `%s' cannot be marked "
2846 "invariant", decl
->identifier
);
2847 } else if ((state
->target
== vertex_shader
)
2848 && (earlier
->data
.mode
!= ir_var_shader_out
)) {
2849 _mesa_glsl_error(& loc
, state
,
2850 "`%s' cannot be marked invariant, vertex shader "
2851 "outputs only", decl
->identifier
);
2852 } else if ((state
->target
== fragment_shader
)
2853 && (earlier
->data
.mode
!= ir_var_shader_in
)) {
2854 _mesa_glsl_error(& loc
, state
,
2855 "`%s' cannot be marked invariant, fragment shader "
2856 "inputs only", decl
->identifier
);
2857 } else if (earlier
->data
.used
) {
2858 _mesa_glsl_error(& loc
, state
,
2859 "variable `%s' may not be redeclared "
2860 "`invariant' after being used",
2863 earlier
->data
.invariant
= true;
2867 /* Invariant redeclarations do not have r-values.
2872 assert(this->type
!= NULL
);
2873 assert(!this->invariant
);
2875 /* The type specifier may contain a structure definition. Process that
2876 * before any of the variable declarations.
2878 (void) this->type
->specifier
->hir(instructions
, state
);
2880 decl_type
= this->type
->glsl_type(& type_name
, state
);
2882 /* An offset-qualified atomic counter declaration sets the default
2883 * offset for the next declaration within the same atomic counter
2886 if (decl_type
&& decl_type
->contains_atomic()) {
2887 if (type
->qualifier
.flags
.q
.explicit_binding
&&
2888 type
->qualifier
.flags
.q
.explicit_offset
)
2889 state
->atomic_counter_offsets
[type
->qualifier
.binding
] =
2890 type
->qualifier
.offset
;
2893 if (this->declarations
.is_empty()) {
2894 /* If there is no structure involved in the program text, there are two
2895 * possible scenarios:
2897 * - The program text contained something like 'vec4;'. This is an
2898 * empty declaration. It is valid but weird. Emit a warning.
2900 * - The program text contained something like 'S;' and 'S' is not the
2901 * name of a known structure type. This is both invalid and weird.
2904 * - The program text contained something like 'mediump float;'
2905 * when the programmer probably meant 'precision mediump
2906 * float;' Emit a warning with a description of what they
2907 * probably meant to do.
2909 * Note that if decl_type is NULL and there is a structure involved,
2910 * there must have been some sort of error with the structure. In this
2911 * case we assume that an error was already generated on this line of
2912 * code for the structure. There is no need to generate an additional,
2915 assert(this->type
->specifier
->structure
== NULL
|| decl_type
!= NULL
2918 if (decl_type
== NULL
) {
2919 _mesa_glsl_error(&loc
, state
,
2920 "invalid type `%s' in empty declaration",
2922 } else if (decl_type
->base_type
== GLSL_TYPE_ATOMIC_UINT
) {
2923 /* Empty atomic counter declarations are allowed and useful
2924 * to set the default offset qualifier.
2927 } else if (this->type
->qualifier
.precision
!= ast_precision_none
) {
2928 if (this->type
->specifier
->structure
!= NULL
) {
2929 _mesa_glsl_error(&loc
, state
,
2930 "precision qualifiers can't be applied "
2933 static const char *const precision_names
[] = {
2940 _mesa_glsl_warning(&loc
, state
,
2941 "empty declaration with precision qualifier, "
2942 "to set the default precision, use "
2943 "`precision %s %s;'",
2944 precision_names
[this->type
->qualifier
.precision
],
2947 } else if (this->type
->specifier
->structure
== NULL
) {
2948 _mesa_glsl_warning(&loc
, state
, "empty declaration");
2952 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
2953 const struct glsl_type
*var_type
;
2956 /* FINISHME: Emit a warning if a variable declaration shadows a
2957 * FINISHME: declaration at a higher scope.
2960 if ((decl_type
== NULL
) || decl_type
->is_void()) {
2961 if (type_name
!= NULL
) {
2962 _mesa_glsl_error(& loc
, state
,
2963 "invalid type `%s' in declaration of `%s'",
2964 type_name
, decl
->identifier
);
2966 _mesa_glsl_error(& loc
, state
,
2967 "invalid type in declaration of `%s'",
2973 if (decl
->is_array
) {
2974 var_type
= process_array_type(&loc
, decl_type
, decl
->array_size
,
2976 if (var_type
->is_error())
2979 var_type
= decl_type
;
2982 var
= new(ctx
) ir_variable(var_type
, decl
->identifier
, ir_var_auto
);
2984 /* The 'varying in' and 'varying out' qualifiers can only be used with
2985 * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
2988 if (this->type
->qualifier
.flags
.q
.varying
) {
2989 if (this->type
->qualifier
.flags
.q
.in
) {
2990 _mesa_glsl_error(& loc
, state
,
2991 "`varying in' qualifier in declaration of "
2992 "`%s' only valid for geometry shaders using "
2993 "ARB_geometry_shader4 or EXT_geometry_shader4",
2995 } else if (this->type
->qualifier
.flags
.q
.out
) {
2996 _mesa_glsl_error(& loc
, state
,
2997 "`varying out' qualifier in declaration of "
2998 "`%s' only valid for geometry shaders using "
2999 "ARB_geometry_shader4 or EXT_geometry_shader4",
3004 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
3006 * "Global variables can only use the qualifiers const,
3007 * attribute, uni form, or varying. Only one may be
3010 * Local variables can only use the qualifier const."
3012 * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
3013 * any extension that adds the 'layout' keyword.
3015 if (!state
->is_version(130, 300)
3016 && !state
->has_explicit_attrib_location()
3017 && !state
->ARB_fragment_coord_conventions_enable
) {
3018 if (this->type
->qualifier
.flags
.q
.out
) {
3019 _mesa_glsl_error(& loc
, state
,
3020 "`out' qualifier in declaration of `%s' "
3021 "only valid for function parameters in %s",
3022 decl
->identifier
, state
->get_version_string());
3024 if (this->type
->qualifier
.flags
.q
.in
) {
3025 _mesa_glsl_error(& loc
, state
,
3026 "`in' qualifier in declaration of `%s' "
3027 "only valid for function parameters in %s",
3028 decl
->identifier
, state
->get_version_string());
3030 /* FINISHME: Test for other invalid qualifiers. */
3033 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
3036 if (this->type
->qualifier
.flags
.q
.invariant
) {
3037 if ((state
->target
== vertex_shader
) &&
3038 var
->data
.mode
!= ir_var_shader_out
) {
3039 _mesa_glsl_error(& loc
, state
,
3040 "`%s' cannot be marked invariant, vertex shader "
3041 "outputs only", var
->name
);
3042 } else if ((state
->target
== fragment_shader
) &&
3043 var
->data
.mode
!= ir_var_shader_in
) {
3044 /* FINISHME: Note that this doesn't work for invariant on
3045 * a function signature inval
3047 _mesa_glsl_error(& loc
, state
,
3048 "`%s' cannot be marked invariant, fragment shader "
3049 "inputs only", var
->name
);
3053 if (state
->current_function
!= NULL
) {
3054 const char *mode
= NULL
;
3055 const char *extra
= "";
3057 /* There is no need to check for 'inout' here because the parser will
3058 * only allow that in function parameter lists.
3060 if (this->type
->qualifier
.flags
.q
.attribute
) {
3062 } else if (this->type
->qualifier
.flags
.q
.uniform
) {
3064 } else if (this->type
->qualifier
.flags
.q
.varying
) {
3066 } else if (this->type
->qualifier
.flags
.q
.in
) {
3068 extra
= " or in function parameter list";
3069 } else if (this->type
->qualifier
.flags
.q
.out
) {
3071 extra
= " or in function parameter list";
3075 _mesa_glsl_error(& loc
, state
,
3076 "%s variable `%s' must be declared at "
3078 mode
, var
->name
, extra
);
3080 } else if (var
->data
.mode
== ir_var_shader_in
) {
3081 var
->data
.read_only
= true;
3083 if (state
->target
== vertex_shader
) {
3084 bool error_emitted
= false;
3086 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
3088 * "Vertex shader inputs can only be float, floating-point
3089 * vectors, matrices, signed and unsigned integers and integer
3090 * vectors. Vertex shader inputs can also form arrays of these
3091 * types, but not structures."
3093 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
3095 * "Vertex shader inputs can only be float, floating-point
3096 * vectors, matrices, signed and unsigned integers and integer
3097 * vectors. They cannot be arrays or structures."
3099 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
3101 * "The attribute qualifier can be used only with float,
3102 * floating-point vectors, and matrices. Attribute variables
3103 * cannot be declared as arrays or structures."
3105 * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
3107 * "Vertex shader inputs can only be float, floating-point
3108 * vectors, matrices, signed and unsigned integers and integer
3109 * vectors. Vertex shader inputs cannot be arrays or
3112 const glsl_type
*check_type
= var
->type
->is_array()
3113 ? var
->type
->fields
.array
: var
->type
;
3115 switch (check_type
->base_type
) {
3116 case GLSL_TYPE_FLOAT
:
3118 case GLSL_TYPE_UINT
:
3120 if (state
->is_version(120, 300))
3124 _mesa_glsl_error(& loc
, state
,
3125 "vertex shader input / attribute cannot have "
3127 var
->type
->is_array() ? "array of " : "",
3129 error_emitted
= true;
3132 if (!error_emitted
&& var
->type
->is_array() &&
3133 !state
->check_version(150, 0, &loc
,
3134 "vertex shader input / attribute "
3135 "cannot have array type")) {
3136 error_emitted
= true;
3138 } else if (state
->target
== geometry_shader
) {
3139 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
3141 * Geometry shader input variables get the per-vertex values
3142 * written out by vertex shader output variables of the same
3143 * names. Since a geometry shader operates on a set of
3144 * vertices, each input varying variable (or input block, see
3145 * interface blocks below) needs to be declared as an array.
3147 if (!var
->type
->is_array()) {
3148 _mesa_glsl_error(&loc
, state
,
3149 "geometry shader inputs must be arrays");
3152 handle_geometry_shader_input_decl(state
, loc
, var
);
3156 /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
3157 * so must integer vertex outputs.
3159 * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
3160 * "Fragment shader inputs that are signed or unsigned integers or
3161 * integer vectors must be qualified with the interpolation qualifier
3164 * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
3165 * "Fragment shader inputs that are, or contain, signed or unsigned
3166 * integers or integer vectors must be qualified with the
3167 * interpolation qualifier flat."
3169 * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
3170 * "Vertex shader outputs that are, or contain, signed or unsigned
3171 * integers or integer vectors must be qualified with the
3172 * interpolation qualifier flat."
3174 * Note that prior to GLSL 1.50, this requirement applied to vertex
3175 * outputs rather than fragment inputs. That creates problems in the
3176 * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
3177 * desktop GL shaders. For GLSL ES shaders, we follow the spec and
3178 * apply the restriction to both vertex outputs and fragment inputs.
3180 * Note also that the desktop GLSL specs are missing the text "or
3181 * contain"; this is presumably an oversight, since there is no
3182 * reasonable way to interpolate a fragment shader input that contains
3185 if (state
->is_version(130, 300) &&
3186 var
->type
->contains_integer() &&
3187 var
->data
.interpolation
!= INTERP_QUALIFIER_FLAT
&&
3188 ((state
->target
== fragment_shader
&& var
->data
.mode
== ir_var_shader_in
)
3189 || (state
->target
== vertex_shader
&& var
->data
.mode
== ir_var_shader_out
3190 && state
->es_shader
))) {
3191 const char *var_type
= (state
->target
== vertex_shader
) ?
3192 "vertex output" : "fragment input";
3193 _mesa_glsl_error(&loc
, state
, "if a %s is (or contains) "
3194 "an integer, then it must be qualified with 'flat'",
3199 /* Interpolation qualifiers cannot be applied to 'centroid' and
3200 * 'centroid varying'.
3202 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
3203 * "interpolation qualifiers may only precede the qualifiers in,
3204 * centroid in, out, or centroid out in a declaration. They do not apply
3205 * to the deprecated storage qualifiers varying or centroid varying."
3207 * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
3209 if (state
->is_version(130, 0)
3210 && this->type
->qualifier
.has_interpolation()
3211 && this->type
->qualifier
.flags
.q
.varying
) {
3213 const char *i
= this->type
->qualifier
.interpolation_string();
3216 if (this->type
->qualifier
.flags
.q
.centroid
)
3217 s
= "centroid varying";
3221 _mesa_glsl_error(&loc
, state
,
3222 "qualifier '%s' cannot be applied to the "
3223 "deprecated storage qualifier '%s'", i
, s
);
3227 /* Interpolation qualifiers can only apply to vertex shader outputs and
3228 * fragment shader inputs.
3230 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
3231 * "Outputs from a vertex shader (out) and inputs to a fragment
3232 * shader (in) can be further qualified with one or more of these
3233 * interpolation qualifiers"
3235 * From page 31 (page 37 of the PDF) of the GLSL ES 3.00 spec:
3236 * "These interpolation qualifiers may only precede the qualifiers
3237 * in, centroid in, out, or centroid out in a declaration. They do
3238 * not apply to inputs into a vertex shader or outputs from a
3241 if (state
->is_version(130, 300)
3242 && this->type
->qualifier
.has_interpolation()) {
3244 const char *i
= this->type
->qualifier
.interpolation_string();
3247 switch (state
->target
) {
3249 if (this->type
->qualifier
.flags
.q
.in
) {
3250 _mesa_glsl_error(&loc
, state
,
3251 "qualifier '%s' cannot be applied to vertex "
3252 "shader inputs", i
);
3255 case fragment_shader
:
3256 if (this->type
->qualifier
.flags
.q
.out
) {
3257 _mesa_glsl_error(&loc
, state
,
3258 "qualifier '%s' cannot be applied to fragment "
3259 "shader outputs", i
);
3268 /* From section 4.3.4 of the GLSL 1.30 spec:
3269 * "It is an error to use centroid in in a vertex shader."
3271 * From section 4.3.4 of the GLSL ES 3.00 spec:
3272 * "It is an error to use centroid in or interpolation qualifiers in
3273 * a vertex shader input."
3275 if (state
->is_version(130, 300)
3276 && this->type
->qualifier
.flags
.q
.centroid
3277 && this->type
->qualifier
.flags
.q
.in
3278 && state
->target
== vertex_shader
) {
3280 _mesa_glsl_error(&loc
, state
,
3281 "'centroid in' cannot be used in a vertex shader");
3284 if (state
->target
== vertex_shader
3285 && this->type
->qualifier
.flags
.q
.sample
3286 && this->type
->qualifier
.flags
.q
.in
) {
3288 _mesa_glsl_error(&loc
, state
,
3289 "'sample in' cannot be used in a vertex shader");
3292 /* Section 4.3.6 of the GLSL 1.30 specification states:
3293 * "It is an error to use centroid out in a fragment shader."
3295 * The GL_ARB_shading_language_420pack extension specification states:
3296 * "It is an error to use auxiliary storage qualifiers or interpolation
3297 * qualifiers on an output in a fragment shader."
3299 if (state
->target
== fragment_shader
&&
3300 this->type
->qualifier
.flags
.q
.out
&&
3301 this->type
->qualifier
.has_auxiliary_storage()) {
3302 _mesa_glsl_error(&loc
, state
,
3303 "auxiliary storage qualifiers cannot be used on "
3304 "fragment shader outputs");
3307 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
3309 if (this->type
->qualifier
.precision
!= ast_precision_none
) {
3310 state
->check_precision_qualifiers_allowed(&loc
);
3314 /* Precision qualifiers apply to floating point, integer and sampler
3317 * Section 4.5.2 (Precision Qualifiers) of the GLSL 1.30 spec says:
3318 * "Any floating point or any integer declaration can have the type
3319 * preceded by one of these precision qualifiers [...] Literal
3320 * constants do not have precision qualifiers. Neither do Boolean
3323 * Section 4.5 (Precision and Precision Qualifiers) of the GLSL 1.30
3326 * "Precision qualifiers are added for code portability with OpenGL
3327 * ES, not for functionality. They have the same syntax as in OpenGL
3330 * Section 8 (Built-In Functions) of the GLSL ES 1.00 spec says:
3332 * "uniform lowp sampler2D sampler;
3335 * lowp vec4 col = texture2D (sampler, coord);
3336 * // texture2D returns lowp"
3338 * From this, we infer that GLSL 1.30 (and later) should allow precision
3339 * qualifiers on sampler types just like float and integer types.
3341 if (this->type
->qualifier
.precision
!= ast_precision_none
3342 && !var
->type
->is_float()
3343 && !var
->type
->is_integer()
3344 && !var
->type
->is_record()
3345 && !var
->type
->is_sampler()
3346 && !(var
->type
->is_array()
3347 && (var
->type
->fields
.array
->is_float()
3348 || var
->type
->fields
.array
->is_integer()))) {
3350 _mesa_glsl_error(&loc
, state
,
3351 "precision qualifiers apply only to floating point"
3352 ", integer and sampler types");
3355 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3357 * "[Sampler types] can only be declared as function
3358 * parameters or uniform variables (see Section 4.3.5
3361 if (var_type
->contains_sampler() &&
3362 !this->type
->qualifier
.flags
.q
.uniform
) {
3363 _mesa_glsl_error(&loc
, state
, "samplers must be declared uniform");
3366 /* Process the initializer and add its instructions to a temporary
3367 * list. This list will be added to the instruction stream (below) after
3368 * the declaration is added. This is done because in some cases (such as
3369 * redeclarations) the declaration may not actually be added to the
3370 * instruction stream.
3372 exec_list initializer_instructions
;
3373 ir_variable
*earlier
=
3374 get_variable_being_redeclared(var
, decl
->get_location(), state
,
3375 false /* allow_all_redeclarations */);
3376 if (earlier
!= NULL
) {
3377 if (strncmp(var
->name
, "gl_", 3) == 0 &&
3378 earlier
->data
.how_declared
== ir_var_declared_in_block
) {
3379 _mesa_glsl_error(&loc
, state
,
3380 "`%s' has already been redeclared using "
3381 "gl_PerVertex", var
->name
);
3383 earlier
->data
.how_declared
= ir_var_declared_normally
;
3386 if (decl
->initializer
!= NULL
) {
3387 result
= process_initializer((earlier
== NULL
) ? var
: earlier
,
3389 &initializer_instructions
, state
);
3392 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
3394 * "It is an error to write to a const variable outside of
3395 * its declaration, so they must be initialized when
3398 if (this->type
->qualifier
.flags
.q
.constant
&& decl
->initializer
== NULL
) {
3399 _mesa_glsl_error(& loc
, state
,
3400 "const declaration of `%s' must be initialized",
3404 if (state
->es_shader
) {
3405 const glsl_type
*const t
= (earlier
== NULL
)
3406 ? var
->type
: earlier
->type
;
3408 if (t
->is_unsized_array())
3409 /* Section 10.17 of the GLSL ES 1.00 specification states that
3410 * unsized array declarations have been removed from the language.
3411 * Arrays that are sized using an initializer are still explicitly
3412 * sized. However, GLSL ES 1.00 does not allow array
3413 * initializers. That is only allowed in GLSL ES 3.00.
3415 * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
3417 * "An array type can also be formed without specifying a size
3418 * if the definition includes an initializer:
3420 * float x[] = float[2] (1.0, 2.0); // declares an array of size 2
3421 * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
3426 _mesa_glsl_error(& loc
, state
,
3427 "unsized array declarations are not allowed in "
3431 /* If the declaration is not a redeclaration, there are a few additional
3432 * semantic checks that must be applied. In addition, variable that was
3433 * created for the declaration should be added to the IR stream.
3435 if (earlier
== NULL
) {
3436 validate_identifier(decl
->identifier
, loc
, state
);
3438 /* Add the variable to the symbol table. Note that the initializer's
3439 * IR was already processed earlier (though it hasn't been emitted
3440 * yet), without the variable in scope.
3442 * This differs from most C-like languages, but it follows the GLSL
3443 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
3446 * "Within a declaration, the scope of a name starts immediately
3447 * after the initializer if present or immediately after the name
3448 * being declared if not."
3450 if (!state
->symbols
->add_variable(var
)) {
3451 YYLTYPE loc
= this->get_location();
3452 _mesa_glsl_error(&loc
, state
, "name `%s' already taken in the "
3453 "current scope", decl
->identifier
);
3457 /* Push the variable declaration to the top. It means that all the
3458 * variable declarations will appear in a funny last-to-first order,
3459 * but otherwise we run into trouble if a function is prototyped, a
3460 * global var is decled, then the function is defined with usage of
3461 * the global var. See glslparsertest's CorrectModule.frag.
3463 instructions
->push_head(var
);
3466 instructions
->append_list(&initializer_instructions
);
3470 /* Generally, variable declarations do not have r-values. However,
3471 * one is used for the declaration in
3473 * while (bool b = some_condition()) {
3477 * so we return the rvalue from the last seen declaration here.
3484 ast_parameter_declarator::hir(exec_list
*instructions
,
3485 struct _mesa_glsl_parse_state
*state
)
3488 const struct glsl_type
*type
;
3489 const char *name
= NULL
;
3490 YYLTYPE loc
= this->get_location();
3492 type
= this->type
->glsl_type(& name
, state
);
3496 _mesa_glsl_error(& loc
, state
,
3497 "invalid type `%s' in declaration of `%s'",
3498 name
, this->identifier
);
3500 _mesa_glsl_error(& loc
, state
,
3501 "invalid type in declaration of `%s'",
3505 type
= glsl_type::error_type
;
3508 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
3510 * "Functions that accept no input arguments need not use void in the
3511 * argument list because prototypes (or definitions) are required and
3512 * therefore there is no ambiguity when an empty argument list "( )" is
3513 * declared. The idiom "(void)" as a parameter list is provided for
3516 * Placing this check here prevents a void parameter being set up
3517 * for a function, which avoids tripping up checks for main taking
3518 * parameters and lookups of an unnamed symbol.
3520 if (type
->is_void()) {
3521 if (this->identifier
!= NULL
)
3522 _mesa_glsl_error(& loc
, state
,
3523 "named parameter cannot have type `void'");
3529 if (formal_parameter
&& (this->identifier
== NULL
)) {
3530 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
3534 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
3535 * call already handled the "vec4[..] foo" case.
3537 if (this->is_array
) {
3538 type
= process_array_type(&loc
, type
, this->array_size
, state
);
3541 if (!type
->is_error() && type
->is_unsized_array()) {
3542 _mesa_glsl_error(&loc
, state
, "arrays passed as parameters must have "
3544 type
= glsl_type::error_type
;
3548 ir_variable
*var
= new(ctx
)
3549 ir_variable(type
, this->identifier
, ir_var_function_in
);
3551 /* Apply any specified qualifiers to the parameter declaration. Note that
3552 * for function parameters the default mode is 'in'.
3554 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
,
3557 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3559 * "Samplers cannot be treated as l-values; hence cannot be used
3560 * as out or inout function parameters, nor can they be assigned
3563 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
3564 && type
->contains_sampler()) {
3565 _mesa_glsl_error(&loc
, state
, "out and inout parameters cannot contain samplers");
3566 type
= glsl_type::error_type
;
3569 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
3571 * "When calling a function, expressions that do not evaluate to
3572 * l-values cannot be passed to parameters declared as out or inout."
3574 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
3576 * "Other binary or unary expressions, non-dereferenced arrays,
3577 * function names, swizzles with repeated fields, and constants
3578 * cannot be l-values."
3580 * So for GLSL 1.10, passing an array as an out or inout parameter is not
3581 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
3583 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
3585 && !state
->check_version(120, 100, &loc
,
3586 "arrays cannot be out or inout parameters")) {
3587 type
= glsl_type::error_type
;
3590 instructions
->push_tail(var
);
3592 /* Parameter declarations do not have r-values.
3599 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
3601 exec_list
*ir_parameters
,
3602 _mesa_glsl_parse_state
*state
)
3604 ast_parameter_declarator
*void_param
= NULL
;
3607 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
3608 param
->formal_parameter
= formal
;
3609 param
->hir(ir_parameters
, state
);
3617 if ((void_param
!= NULL
) && (count
> 1)) {
3618 YYLTYPE loc
= void_param
->get_location();
3620 _mesa_glsl_error(& loc
, state
,
3621 "`void' parameter must be only parameter");
3627 emit_function(_mesa_glsl_parse_state
*state
, ir_function
*f
)
3629 /* IR invariants disallow function declarations or definitions
3630 * nested within other function definitions. But there is no
3631 * requirement about the relative order of function declarations
3632 * and definitions with respect to one another. So simply insert
3633 * the new ir_function block at the end of the toplevel instruction
3636 state
->toplevel_ir
->push_tail(f
);
3641 ast_function::hir(exec_list
*instructions
,
3642 struct _mesa_glsl_parse_state
*state
)
3645 ir_function
*f
= NULL
;
3646 ir_function_signature
*sig
= NULL
;
3647 exec_list hir_parameters
;
3649 const char *const name
= identifier
;
3651 /* New functions are always added to the top-level IR instruction stream,
3652 * so this instruction list pointer is ignored. See also emit_function
3655 (void) instructions
;
3657 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
3659 * "Function declarations (prototypes) cannot occur inside of functions;
3660 * they must be at global scope, or for the built-in functions, outside
3661 * the global scope."
3663 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
3665 * "User defined functions may only be defined within the global scope."
3667 * Note that this language does not appear in GLSL 1.10.
3669 if ((state
->current_function
!= NULL
) &&
3670 state
->is_version(120, 100)) {
3671 YYLTYPE loc
= this->get_location();
3672 _mesa_glsl_error(&loc
, state
,
3673 "declaration of function `%s' not allowed within "
3674 "function body", name
);
3677 validate_identifier(name
, this->get_location(), state
);
3679 /* Convert the list of function parameters to HIR now so that they can be
3680 * used below to compare this function's signature with previously seen
3681 * signatures for functions with the same name.
3683 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
3685 & hir_parameters
, state
);
3687 const char *return_type_name
;
3688 const glsl_type
*return_type
=
3689 this->return_type
->glsl_type(& return_type_name
, state
);
3692 YYLTYPE loc
= this->get_location();
3693 _mesa_glsl_error(&loc
, state
,
3694 "function `%s' has undeclared return type `%s'",
3695 name
, return_type_name
);
3696 return_type
= glsl_type::error_type
;
3699 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
3700 * "No qualifier is allowed on the return type of a function."
3702 if (this->return_type
->has_qualifiers()) {
3703 YYLTYPE loc
= this->get_location();
3704 _mesa_glsl_error(& loc
, state
,
3705 "function `%s' return type has qualifiers", name
);
3708 /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
3710 * "Arrays are allowed as arguments and as the return type. In both
3711 * cases, the array must be explicitly sized."
3713 if (return_type
->is_unsized_array()) {
3714 YYLTYPE loc
= this->get_location();
3715 _mesa_glsl_error(& loc
, state
,
3716 "function `%s' return type array must be explicitly "
3720 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3722 * "[Sampler types] can only be declared as function parameters
3723 * or uniform variables (see Section 4.3.5 "Uniform")".
3725 if (return_type
->contains_sampler()) {
3726 YYLTYPE loc
= this->get_location();
3727 _mesa_glsl_error(&loc
, state
,
3728 "function `%s' return type can't contain a sampler",
3732 /* Verify that this function's signature either doesn't match a previously
3733 * seen signature for a function with the same name, or, if a match is found,
3734 * that the previously seen signature does not have an associated definition.
3736 f
= state
->symbols
->get_function(name
);
3737 if (f
!= NULL
&& (state
->es_shader
|| f
->has_user_signature())) {
3738 sig
= f
->exact_matching_signature(state
, &hir_parameters
);
3740 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
3741 if (badvar
!= NULL
) {
3742 YYLTYPE loc
= this->get_location();
3744 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
3745 "qualifiers don't match prototype", name
, badvar
);
3748 if (sig
->return_type
!= return_type
) {
3749 YYLTYPE loc
= this->get_location();
3751 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
3752 "match prototype", name
);
3755 if (sig
->is_defined
) {
3756 if (is_definition
) {
3757 YYLTYPE loc
= this->get_location();
3758 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
3760 /* We just encountered a prototype that exactly matches a
3761 * function that's already been defined. This is redundant,
3762 * and we should ignore it.
3769 f
= new(ctx
) ir_function(name
);
3770 if (!state
->symbols
->add_function(f
)) {
3771 /* This function name shadows a non-function use of the same name. */
3772 YYLTYPE loc
= this->get_location();
3774 _mesa_glsl_error(&loc
, state
, "function name `%s' conflicts with "
3775 "non-function", name
);
3779 emit_function(state
, f
);
3782 /* Verify the return type of main() */
3783 if (strcmp(name
, "main") == 0) {
3784 if (! return_type
->is_void()) {
3785 YYLTYPE loc
= this->get_location();
3787 _mesa_glsl_error(& loc
, state
, "main() must return void");
3790 if (!hir_parameters
.is_empty()) {
3791 YYLTYPE loc
= this->get_location();
3793 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
3797 /* Finish storing the information about this new function in its signature.
3800 sig
= new(ctx
) ir_function_signature(return_type
);
3801 f
->add_signature(sig
);
3804 sig
->replace_parameters(&hir_parameters
);
3807 /* Function declarations (prototypes) do not have r-values.
3814 ast_function_definition::hir(exec_list
*instructions
,
3815 struct _mesa_glsl_parse_state
*state
)
3817 prototype
->is_definition
= true;
3818 prototype
->hir(instructions
, state
);
3820 ir_function_signature
*signature
= prototype
->signature
;
3821 if (signature
== NULL
)
3824 assert(state
->current_function
== NULL
);
3825 state
->current_function
= signature
;
3826 state
->found_return
= false;
3828 /* Duplicate parameters declared in the prototype as concrete variables.
3829 * Add these to the symbol table.
3831 state
->symbols
->push_scope();
3832 foreach_iter(exec_list_iterator
, iter
, signature
->parameters
) {
3833 ir_variable
*const var
= ((ir_instruction
*) iter
.get())->as_variable();
3835 assert(var
!= NULL
);
3837 /* The only way a parameter would "exist" is if two parameters have
3840 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
3841 YYLTYPE loc
= this->get_location();
3843 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
3845 state
->symbols
->add_variable(var
);
3849 /* Convert the body of the function to HIR. */
3850 this->body
->hir(&signature
->body
, state
);
3851 signature
->is_defined
= true;
3853 state
->symbols
->pop_scope();
3855 assert(state
->current_function
== signature
);
3856 state
->current_function
= NULL
;
3858 if (!signature
->return_type
->is_void() && !state
->found_return
) {
3859 YYLTYPE loc
= this->get_location();
3860 _mesa_glsl_error(& loc
, state
, "function `%s' has non-void return type "
3861 "%s, but no return statement",
3862 signature
->function_name(),
3863 signature
->return_type
->name
);
3866 /* Function definitions do not have r-values.
3873 ast_jump_statement::hir(exec_list
*instructions
,
3874 struct _mesa_glsl_parse_state
*state
)
3881 assert(state
->current_function
);
3883 if (opt_return_value
) {
3884 ir_rvalue
*ret
= opt_return_value
->hir(instructions
, state
);
3886 /* The value of the return type can be NULL if the shader says
3887 * 'return foo();' and foo() is a function that returns void.
3889 * NOTE: The GLSL spec doesn't say that this is an error. The type
3890 * of the return value is void. If the return type of the function is
3891 * also void, then this should compile without error. Seriously.
3893 const glsl_type
*const ret_type
=
3894 (ret
== NULL
) ? glsl_type::void_type
: ret
->type
;
3896 /* Implicit conversions are not allowed for return values prior to
3897 * ARB_shading_language_420pack.
3899 if (state
->current_function
->return_type
!= ret_type
) {
3900 YYLTYPE loc
= this->get_location();
3902 if (state
->ARB_shading_language_420pack_enable
) {
3903 if (!apply_implicit_conversion(state
->current_function
->return_type
,
3905 _mesa_glsl_error(& loc
, state
,
3906 "could not implicitly convert return value "
3907 "to %s, in function `%s'",
3908 state
->current_function
->return_type
->name
,
3909 state
->current_function
->function_name());
3912 _mesa_glsl_error(& loc
, state
,
3913 "`return' with wrong type %s, in function `%s' "
3916 state
->current_function
->function_name(),
3917 state
->current_function
->return_type
->name
);
3919 } else if (state
->current_function
->return_type
->base_type
==
3921 YYLTYPE loc
= this->get_location();
3923 /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
3924 * specs add a clarification:
3926 * "A void function can only use return without a return argument, even if
3927 * the return argument has void type. Return statements only accept values:
3930 * void func2() { return func1(); } // illegal return statement"
3932 _mesa_glsl_error(& loc
, state
,
3933 "void functions can only use `return' without a "
3937 inst
= new(ctx
) ir_return(ret
);
3939 if (state
->current_function
->return_type
->base_type
!=
3941 YYLTYPE loc
= this->get_location();
3943 _mesa_glsl_error(& loc
, state
,
3944 "`return' with no value, in function %s returning "
3946 state
->current_function
->function_name());
3948 inst
= new(ctx
) ir_return
;
3951 state
->found_return
= true;
3952 instructions
->push_tail(inst
);
3957 if (state
->target
!= fragment_shader
) {
3958 YYLTYPE loc
= this->get_location();
3960 _mesa_glsl_error(& loc
, state
,
3961 "`discard' may only appear in a fragment shader");
3963 instructions
->push_tail(new(ctx
) ir_discard
);
3968 if (mode
== ast_continue
&&
3969 state
->loop_nesting_ast
== NULL
) {
3970 YYLTYPE loc
= this->get_location();
3972 _mesa_glsl_error(& loc
, state
,
3973 "continue may only appear in a loop");
3974 } else if (mode
== ast_break
&&
3975 state
->loop_nesting_ast
== NULL
&&
3976 state
->switch_state
.switch_nesting_ast
== NULL
) {
3977 YYLTYPE loc
= this->get_location();
3979 _mesa_glsl_error(& loc
, state
,
3980 "break may only appear in a loop or a switch");
3982 /* For a loop, inline the for loop expression again,
3983 * since we don't know where near the end of
3984 * the loop body the normal copy of it
3985 * is going to be placed.
3987 if (state
->loop_nesting_ast
!= NULL
&&
3988 mode
== ast_continue
&&
3989 state
->loop_nesting_ast
->rest_expression
) {
3990 state
->loop_nesting_ast
->rest_expression
->hir(instructions
,
3994 if (state
->switch_state
.is_switch_innermost
&&
3995 mode
== ast_break
) {
3996 /* Force break out of switch by setting is_break switch state.
3998 ir_variable
*const is_break_var
= state
->switch_state
.is_break_var
;
3999 ir_dereference_variable
*const deref_is_break_var
=
4000 new(ctx
) ir_dereference_variable(is_break_var
);
4001 ir_constant
*const true_val
= new(ctx
) ir_constant(true);
4002 ir_assignment
*const set_break_var
=
4003 new(ctx
) ir_assignment(deref_is_break_var
, true_val
);
4005 instructions
->push_tail(set_break_var
);
4008 ir_loop_jump
*const jump
=
4009 new(ctx
) ir_loop_jump((mode
== ast_break
)
4010 ? ir_loop_jump::jump_break
4011 : ir_loop_jump::jump_continue
);
4012 instructions
->push_tail(jump
);
4019 /* Jump instructions do not have r-values.
4026 ast_selection_statement::hir(exec_list
*instructions
,
4027 struct _mesa_glsl_parse_state
*state
)
4031 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
4033 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
4035 * "Any expression whose type evaluates to a Boolean can be used as the
4036 * conditional expression bool-expression. Vector types are not accepted
4037 * as the expression to if."
4039 * The checks are separated so that higher quality diagnostics can be
4040 * generated for cases where both rules are violated.
4042 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
4043 YYLTYPE loc
= this->condition
->get_location();
4045 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
4049 ir_if
*const stmt
= new(ctx
) ir_if(condition
);
4051 if (then_statement
!= NULL
) {
4052 state
->symbols
->push_scope();
4053 then_statement
->hir(& stmt
->then_instructions
, state
);
4054 state
->symbols
->pop_scope();
4057 if (else_statement
!= NULL
) {
4058 state
->symbols
->push_scope();
4059 else_statement
->hir(& stmt
->else_instructions
, state
);
4060 state
->symbols
->pop_scope();
4063 instructions
->push_tail(stmt
);
4065 /* if-statements do not have r-values.
4072 ast_switch_statement::hir(exec_list
*instructions
,
4073 struct _mesa_glsl_parse_state
*state
)
4077 ir_rvalue
*const test_expression
=
4078 this->test_expression
->hir(instructions
, state
);
4080 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
4082 * "The type of init-expression in a switch statement must be a
4085 if (!test_expression
->type
->is_scalar() ||
4086 !test_expression
->type
->is_integer()) {
4087 YYLTYPE loc
= this->test_expression
->get_location();
4089 _mesa_glsl_error(& loc
,
4091 "switch-statement expression must be scalar "
4095 /* Track the switch-statement nesting in a stack-like manner.
4097 struct glsl_switch_state saved
= state
->switch_state
;
4099 state
->switch_state
.is_switch_innermost
= true;
4100 state
->switch_state
.switch_nesting_ast
= this;
4101 state
->switch_state
.labels_ht
= hash_table_ctor(0, hash_table_pointer_hash
,
4102 hash_table_pointer_compare
);
4103 state
->switch_state
.previous_default
= NULL
;
4105 /* Initalize is_fallthru state to false.
4107 ir_rvalue
*const is_fallthru_val
= new (ctx
) ir_constant(false);
4108 state
->switch_state
.is_fallthru_var
=
4109 new(ctx
) ir_variable(glsl_type::bool_type
,
4110 "switch_is_fallthru_tmp",
4112 instructions
->push_tail(state
->switch_state
.is_fallthru_var
);
4114 ir_dereference_variable
*deref_is_fallthru_var
=
4115 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4116 instructions
->push_tail(new(ctx
) ir_assignment(deref_is_fallthru_var
,
4119 /* Initalize is_break state to false.
4121 ir_rvalue
*const is_break_val
= new (ctx
) ir_constant(false);
4122 state
->switch_state
.is_break_var
= new(ctx
) ir_variable(glsl_type::bool_type
,
4123 "switch_is_break_tmp",
4125 instructions
->push_tail(state
->switch_state
.is_break_var
);
4127 ir_dereference_variable
*deref_is_break_var
=
4128 new(ctx
) ir_dereference_variable(state
->switch_state
.is_break_var
);
4129 instructions
->push_tail(new(ctx
) ir_assignment(deref_is_break_var
,
4132 /* Cache test expression.
4134 test_to_hir(instructions
, state
);
4136 /* Emit code for body of switch stmt.
4138 body
->hir(instructions
, state
);
4140 hash_table_dtor(state
->switch_state
.labels_ht
);
4142 state
->switch_state
= saved
;
4144 /* Switch statements do not have r-values. */
4150 ast_switch_statement::test_to_hir(exec_list
*instructions
,
4151 struct _mesa_glsl_parse_state
*state
)
4155 /* Cache value of test expression. */
4156 ir_rvalue
*const test_val
=
4157 test_expression
->hir(instructions
,
4160 state
->switch_state
.test_var
= new(ctx
) ir_variable(test_val
->type
,
4163 ir_dereference_variable
*deref_test_var
=
4164 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
4166 instructions
->push_tail(state
->switch_state
.test_var
);
4167 instructions
->push_tail(new(ctx
) ir_assignment(deref_test_var
, test_val
));
4172 ast_switch_body::hir(exec_list
*instructions
,
4173 struct _mesa_glsl_parse_state
*state
)
4176 stmts
->hir(instructions
, state
);
4178 /* Switch bodies do not have r-values. */
4183 ast_case_statement_list::hir(exec_list
*instructions
,
4184 struct _mesa_glsl_parse_state
*state
)
4186 foreach_list_typed (ast_case_statement
, case_stmt
, link
, & this->cases
)
4187 case_stmt
->hir(instructions
, state
);
4189 /* Case statements do not have r-values. */
4194 ast_case_statement::hir(exec_list
*instructions
,
4195 struct _mesa_glsl_parse_state
*state
)
4197 labels
->hir(instructions
, state
);
4199 /* Conditionally set fallthru state based on break state. */
4200 ir_constant
*const false_val
= new(state
) ir_constant(false);
4201 ir_dereference_variable
*const deref_is_fallthru_var
=
4202 new(state
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4203 ir_dereference_variable
*const deref_is_break_var
=
4204 new(state
) ir_dereference_variable(state
->switch_state
.is_break_var
);
4205 ir_assignment
*const reset_fallthru_on_break
=
4206 new(state
) ir_assignment(deref_is_fallthru_var
,
4208 deref_is_break_var
);
4209 instructions
->push_tail(reset_fallthru_on_break
);
4211 /* Guard case statements depending on fallthru state. */
4212 ir_dereference_variable
*const deref_fallthru_guard
=
4213 new(state
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4214 ir_if
*const test_fallthru
= new(state
) ir_if(deref_fallthru_guard
);
4216 foreach_list_typed (ast_node
, stmt
, link
, & this->stmts
)
4217 stmt
->hir(& test_fallthru
->then_instructions
, state
);
4219 instructions
->push_tail(test_fallthru
);
4221 /* Case statements do not have r-values. */
4227 ast_case_label_list::hir(exec_list
*instructions
,
4228 struct _mesa_glsl_parse_state
*state
)
4230 foreach_list_typed (ast_case_label
, label
, link
, & this->labels
)
4231 label
->hir(instructions
, state
);
4233 /* Case labels do not have r-values. */
4238 ast_case_label::hir(exec_list
*instructions
,
4239 struct _mesa_glsl_parse_state
*state
)
4243 ir_dereference_variable
*deref_fallthru_var
=
4244 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4246 ir_rvalue
*const true_val
= new(ctx
) ir_constant(true);
4248 /* If not default case, ... */
4249 if (this->test_value
!= NULL
) {
4250 /* Conditionally set fallthru state based on
4251 * comparison of cached test expression value to case label.
4253 ir_rvalue
*const label_rval
= this->test_value
->hir(instructions
, state
);
4254 ir_constant
*label_const
= label_rval
->constant_expression_value();
4257 YYLTYPE loc
= this->test_value
->get_location();
4259 _mesa_glsl_error(& loc
, state
,
4260 "switch statement case label must be a "
4261 "constant expression");
4263 /* Stuff a dummy value in to allow processing to continue. */
4264 label_const
= new(ctx
) ir_constant(0);
4266 ast_expression
*previous_label
= (ast_expression
*)
4267 hash_table_find(state
->switch_state
.labels_ht
,
4268 (void *)(uintptr_t)label_const
->value
.u
[0]);
4270 if (previous_label
) {
4271 YYLTYPE loc
= this->test_value
->get_location();
4272 _mesa_glsl_error(& loc
, state
,
4273 "duplicate case value");
4275 loc
= previous_label
->get_location();
4276 _mesa_glsl_error(& loc
, state
,
4277 "this is the previous case label");
4279 hash_table_insert(state
->switch_state
.labels_ht
,
4281 (void *)(uintptr_t)label_const
->value
.u
[0]);
4285 ir_dereference_variable
*deref_test_var
=
4286 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
4288 ir_rvalue
*const test_cond
= new(ctx
) ir_expression(ir_binop_all_equal
,
4292 ir_assignment
*set_fallthru_on_test
=
4293 new(ctx
) ir_assignment(deref_fallthru_var
,
4297 instructions
->push_tail(set_fallthru_on_test
);
4298 } else { /* default case */
4299 if (state
->switch_state
.previous_default
) {
4300 YYLTYPE loc
= this->get_location();
4301 _mesa_glsl_error(& loc
, state
,
4302 "multiple default labels in one switch");
4304 loc
= state
->switch_state
.previous_default
->get_location();
4305 _mesa_glsl_error(& loc
, state
,
4306 "this is the first default label");
4308 state
->switch_state
.previous_default
= this;
4310 /* Set falltrhu state. */
4311 ir_assignment
*set_fallthru
=
4312 new(ctx
) ir_assignment(deref_fallthru_var
, true_val
);
4314 instructions
->push_tail(set_fallthru
);
4317 /* Case statements do not have r-values. */
4322 ast_iteration_statement::condition_to_hir(ir_loop
*stmt
,
4323 struct _mesa_glsl_parse_state
*state
)
4327 if (condition
!= NULL
) {
4328 ir_rvalue
*const cond
=
4329 condition
->hir(& stmt
->body_instructions
, state
);
4332 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
4333 YYLTYPE loc
= condition
->get_location();
4335 _mesa_glsl_error(& loc
, state
,
4336 "loop condition must be scalar boolean");
4338 /* As the first code in the loop body, generate a block that looks
4339 * like 'if (!condition) break;' as the loop termination condition.
4341 ir_rvalue
*const not_cond
=
4342 new(ctx
) ir_expression(ir_unop_logic_not
, cond
);
4344 ir_if
*const if_stmt
= new(ctx
) ir_if(not_cond
);
4346 ir_jump
*const break_stmt
=
4347 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
4349 if_stmt
->then_instructions
.push_tail(break_stmt
);
4350 stmt
->body_instructions
.push_tail(if_stmt
);
4357 ast_iteration_statement::hir(exec_list
*instructions
,
4358 struct _mesa_glsl_parse_state
*state
)
4362 /* For-loops and while-loops start a new scope, but do-while loops do not.
4364 if (mode
!= ast_do_while
)
4365 state
->symbols
->push_scope();
4367 if (init_statement
!= NULL
)
4368 init_statement
->hir(instructions
, state
);
4370 ir_loop
*const stmt
= new(ctx
) ir_loop();
4371 instructions
->push_tail(stmt
);
4373 /* Track the current loop nesting. */
4374 ast_iteration_statement
*nesting_ast
= state
->loop_nesting_ast
;
4376 state
->loop_nesting_ast
= this;
4378 /* Likewise, indicate that following code is closest to a loop,
4379 * NOT closest to a switch.
4381 bool saved_is_switch_innermost
= state
->switch_state
.is_switch_innermost
;
4382 state
->switch_state
.is_switch_innermost
= false;
4384 if (mode
!= ast_do_while
)
4385 condition_to_hir(stmt
, state
);
4388 body
->hir(& stmt
->body_instructions
, state
);
4390 if (rest_expression
!= NULL
)
4391 rest_expression
->hir(& stmt
->body_instructions
, state
);
4393 if (mode
== ast_do_while
)
4394 condition_to_hir(stmt
, state
);
4396 if (mode
!= ast_do_while
)
4397 state
->symbols
->pop_scope();
4399 /* Restore previous nesting before returning. */
4400 state
->loop_nesting_ast
= nesting_ast
;
4401 state
->switch_state
.is_switch_innermost
= saved_is_switch_innermost
;
4403 /* Loops do not have r-values.
4410 * Determine if the given type is valid for establishing a default precision
4413 * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
4415 * "The precision statement
4417 * precision precision-qualifier type;
4419 * can be used to establish a default precision qualifier. The type field
4420 * can be either int or float or any of the sampler types, and the
4421 * precision-qualifier can be lowp, mediump, or highp."
4423 * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
4424 * qualifiers on sampler types, but this seems like an oversight (since the
4425 * intention of including these in GLSL 1.30 is to allow compatibility with ES
4426 * shaders). So we allow int, float, and all sampler types regardless of GLSL
4430 is_valid_default_precision_type(const struct glsl_type
*const type
)
4435 switch (type
->base_type
) {
4437 case GLSL_TYPE_FLOAT
:
4438 /* "int" and "float" are valid, but vectors and matrices are not. */
4439 return type
->vector_elements
== 1 && type
->matrix_columns
== 1;
4440 case GLSL_TYPE_SAMPLER
:
4449 ast_type_specifier::hir(exec_list
*instructions
,
4450 struct _mesa_glsl_parse_state
*state
)
4452 if (this->default_precision
== ast_precision_none
&& this->structure
== NULL
)
4455 YYLTYPE loc
= this->get_location();
4457 /* If this is a precision statement, check that the type to which it is
4458 * applied is either float or int.
4460 * From section 4.5.3 of the GLSL 1.30 spec:
4461 * "The precision statement
4462 * precision precision-qualifier type;
4463 * can be used to establish a default precision qualifier. The type
4464 * field can be either int or float [...]. Any other types or
4465 * qualifiers will result in an error.
4467 if (this->default_precision
!= ast_precision_none
) {
4468 if (!state
->check_precision_qualifiers_allowed(&loc
))
4471 if (this->structure
!= NULL
) {
4472 _mesa_glsl_error(&loc
, state
,
4473 "precision qualifiers do not apply to structures");
4477 if (this->is_array
) {
4478 _mesa_glsl_error(&loc
, state
,
4479 "default precision statements do not apply to "
4484 const struct glsl_type
*const type
=
4485 state
->symbols
->get_type(this->type_name
);
4486 if (!is_valid_default_precision_type(type
)) {
4487 _mesa_glsl_error(&loc
, state
,
4488 "default precision statements apply only to "
4489 "float, int, and sampler types");
4493 if (type
->base_type
== GLSL_TYPE_FLOAT
4495 && state
->target
== fragment_shader
) {
4496 /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
4499 * "The fragment language has no default precision qualifier for
4500 * floating point types."
4502 * As a result, we have to track whether or not default precision has
4503 * been specified for float in GLSL ES fragment shaders.
4505 * Earlier in that same section, the spec says:
4507 * "Non-precision qualified declarations will use the precision
4508 * qualifier specified in the most recent precision statement
4509 * that is still in scope. The precision statement has the same
4510 * scoping rules as variable declarations. If it is declared
4511 * inside a compound statement, its effect stops at the end of
4512 * the innermost statement it was declared in. Precision
4513 * statements in nested scopes override precision statements in
4514 * outer scopes. Multiple precision statements for the same basic
4515 * type can appear inside the same scope, with later statements
4516 * overriding earlier statements within that scope."
4518 * Default precision specifications follow the same scope rules as
4519 * variables. So, we can track the state of the default float
4520 * precision in the symbol table, and the rules will just work. This
4521 * is a slight abuse of the symbol table, but it has the semantics
4524 ir_variable
*const junk
=
4525 new(state
) ir_variable(type
, "#default precision",
4528 state
->symbols
->add_variable(junk
);
4531 /* FINISHME: Translate precision statements into IR. */
4535 /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
4536 * process_record_constructor() can do type-checking on C-style initializer
4537 * expressions of structs, but ast_struct_specifier should only be translated
4538 * to HIR if it is declaring the type of a structure.
4540 * The ->is_declaration field is false for initializers of variables
4541 * declared separately from the struct's type definition.
4543 * struct S { ... }; (is_declaration = true)
4544 * struct T { ... } t = { ... }; (is_declaration = true)
4545 * S s = { ... }; (is_declaration = false)
4547 if (this->structure
!= NULL
&& this->structure
->is_declaration
)
4548 return this->structure
->hir(instructions
, state
);
4555 * Process a structure or interface block tree into an array of structure fields
4557 * After parsing, where there are some syntax differnces, structures and
4558 * interface blocks are almost identical. They are similar enough that the
4559 * AST for each can be processed the same way into a set of
4560 * \c glsl_struct_field to describe the members.
4562 * If we're processing an interface block, var_mode should be the type of the
4563 * interface block (ir_var_shader_in, ir_var_shader_out, or ir_var_uniform).
4564 * If we're processing a structure, var_mode should be ir_var_auto.
4567 * The number of fields processed. A pointer to the array structure fields is
4568 * stored in \c *fields_ret.
4571 ast_process_structure_or_interface_block(exec_list
*instructions
,
4572 struct _mesa_glsl_parse_state
*state
,
4573 exec_list
*declarations
,
4575 glsl_struct_field
**fields_ret
,
4577 bool block_row_major
,
4578 bool allow_reserved_names
,
4579 ir_variable_mode var_mode
)
4581 unsigned decl_count
= 0;
4583 /* Make an initial pass over the list of fields to determine how
4584 * many there are. Each element in this list is an ast_declarator_list.
4585 * This means that we actually need to count the number of elements in the
4586 * 'declarations' list in each of the elements.
4588 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
4589 foreach_list_const (decl_ptr
, & decl_list
->declarations
) {
4594 /* Allocate storage for the fields and process the field
4595 * declarations. As the declarations are processed, try to also convert
4596 * the types to HIR. This ensures that structure definitions embedded in
4597 * other structure definitions or in interface blocks are processed.
4599 glsl_struct_field
*const fields
= ralloc_array(state
, glsl_struct_field
,
4603 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
4604 const char *type_name
;
4606 decl_list
->type
->specifier
->hir(instructions
, state
);
4608 /* Section 10.9 of the GLSL ES 1.00 specification states that
4609 * embedded structure definitions have been removed from the language.
4611 if (state
->es_shader
&& decl_list
->type
->specifier
->structure
!= NULL
) {
4612 _mesa_glsl_error(&loc
, state
, "embedded structure definitions are "
4613 "not allowed in GLSL ES 1.00");
4616 const glsl_type
*decl_type
=
4617 decl_list
->type
->glsl_type(& type_name
, state
);
4619 foreach_list_typed (ast_declaration
, decl
, link
,
4620 &decl_list
->declarations
) {
4621 if (!allow_reserved_names
)
4622 validate_identifier(decl
->identifier
, loc
, state
);
4624 /* From the GL_ARB_uniform_buffer_object spec:
4626 * "Sampler types are not allowed inside of uniform
4627 * blocks. All other types, arrays, and structures
4628 * allowed for uniforms are allowed within a uniform
4631 * It should be impossible for decl_type to be NULL here. Cases that
4632 * might naturally lead to decl_type being NULL, especially for the
4633 * is_interface case, will have resulted in compilation having
4634 * already halted due to a syntax error.
4636 const struct glsl_type
*field_type
=
4637 decl_type
!= NULL
? decl_type
: glsl_type::error_type
;
4639 if (is_interface
&& field_type
->contains_sampler()) {
4640 YYLTYPE loc
= decl_list
->get_location();
4641 _mesa_glsl_error(&loc
, state
,
4642 "uniform in non-default uniform block contains sampler");
4645 if (field_type
->contains_atomic()) {
4646 /* FINISHME: Add a spec quotation here once updated spec
4647 * FINISHME: language is available. See Khronos bug #10903
4648 * FINISHME: on whether atomic counters are allowed in
4649 * FINISHME: structures.
4651 YYLTYPE loc
= decl_list
->get_location();
4652 _mesa_glsl_error(&loc
, state
, "atomic counter in structure or "
4656 const struct ast_type_qualifier
*const qual
=
4657 & decl_list
->type
->qualifier
;
4658 if (qual
->flags
.q
.std140
||
4659 qual
->flags
.q
.packed
||
4660 qual
->flags
.q
.shared
) {
4661 _mesa_glsl_error(&loc
, state
,
4662 "uniform block layout qualifiers std140, packed, and "
4663 "shared can only be applied to uniform blocks, not "
4667 if (decl
->is_array
) {
4668 field_type
= process_array_type(&loc
, decl_type
, decl
->array_size
,
4671 fields
[i
].type
= field_type
;
4672 fields
[i
].name
= decl
->identifier
;
4673 fields
[i
].location
= -1;
4674 fields
[i
].interpolation
=
4675 interpret_interpolation_qualifier(qual
, var_mode
, state
, &loc
);
4676 fields
[i
].centroid
= qual
->flags
.q
.centroid
? 1 : 0;
4677 fields
[i
].sample
= qual
->flags
.q
.sample
? 1 : 0;
4679 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
4680 if (!qual
->flags
.q
.uniform
) {
4681 _mesa_glsl_error(&loc
, state
,
4682 "row_major and column_major can only be "
4683 "applied to uniform interface blocks");
4685 validate_matrix_layout_for_type(state
, &loc
, field_type
, NULL
);
4688 if (qual
->flags
.q
.uniform
&& qual
->has_interpolation()) {
4689 _mesa_glsl_error(&loc
, state
,
4690 "interpolation qualifiers cannot be used "
4691 "with uniform interface blocks");
4694 if (field_type
->is_matrix() ||
4695 (field_type
->is_array() && field_type
->fields
.array
->is_matrix())) {
4696 fields
[i
].row_major
= block_row_major
;
4697 if (qual
->flags
.q
.row_major
)
4698 fields
[i
].row_major
= true;
4699 else if (qual
->flags
.q
.column_major
)
4700 fields
[i
].row_major
= false;
4707 assert(i
== decl_count
);
4709 *fields_ret
= fields
;
4715 ast_struct_specifier::hir(exec_list
*instructions
,
4716 struct _mesa_glsl_parse_state
*state
)
4718 YYLTYPE loc
= this->get_location();
4720 /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
4722 * "Anonymous structures are not supported; so embedded structures must
4723 * have a declarator. A name given to an embedded struct is scoped at
4724 * the same level as the struct it is embedded in."
4726 * The same section of the GLSL 1.20 spec says:
4728 * "Anonymous structures are not supported. Embedded structures are not
4731 * struct S { float f; };
4733 * S; // Error: anonymous structures disallowed
4734 * struct { ... }; // Error: embedded structures disallowed
4735 * S s; // Okay: nested structures with name are allowed
4738 * The GLSL ES 1.00 and 3.00 specs have similar langauge and examples. So,
4739 * we allow embedded structures in 1.10 only.
4741 if (state
->language_version
!= 110 && state
->struct_specifier_depth
!= 0)
4742 _mesa_glsl_error(&loc
, state
,
4743 "embedded structure declartions are not allowed");
4745 state
->struct_specifier_depth
++;
4747 glsl_struct_field
*fields
;
4748 unsigned decl_count
=
4749 ast_process_structure_or_interface_block(instructions
,
4751 &this->declarations
,
4756 false /* allow_reserved_names */,
4759 validate_identifier(this->name
, loc
, state
);
4761 const glsl_type
*t
=
4762 glsl_type::get_record_instance(fields
, decl_count
, this->name
);
4764 if (!state
->symbols
->add_type(name
, t
)) {
4765 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
4767 const glsl_type
**s
= reralloc(state
, state
->user_structures
,
4769 state
->num_user_structures
+ 1);
4771 s
[state
->num_user_structures
] = t
;
4772 state
->user_structures
= s
;
4773 state
->num_user_structures
++;
4777 state
->struct_specifier_depth
--;
4779 /* Structure type definitions do not have r-values.
4786 * Visitor class which detects whether a given interface block has been used.
4788 class interface_block_usage_visitor
: public ir_hierarchical_visitor
4791 interface_block_usage_visitor(ir_variable_mode mode
, const glsl_type
*block
)
4792 : mode(mode
), block(block
), found(false)
4796 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
4798 if (ir
->var
->data
.mode
== mode
&& ir
->var
->get_interface_type() == block
) {
4802 return visit_continue
;
4805 bool usage_found() const
4811 ir_variable_mode mode
;
4812 const glsl_type
*block
;
4818 ast_interface_block::hir(exec_list
*instructions
,
4819 struct _mesa_glsl_parse_state
*state
)
4821 YYLTYPE loc
= this->get_location();
4823 /* The ast_interface_block has a list of ast_declarator_lists. We
4824 * need to turn those into ir_variables with an association
4825 * with this uniform block.
4827 enum glsl_interface_packing packing
;
4828 if (this->layout
.flags
.q
.shared
) {
4829 packing
= GLSL_INTERFACE_PACKING_SHARED
;
4830 } else if (this->layout
.flags
.q
.packed
) {
4831 packing
= GLSL_INTERFACE_PACKING_PACKED
;
4833 /* The default layout is std140.
4835 packing
= GLSL_INTERFACE_PACKING_STD140
;
4838 ir_variable_mode var_mode
;
4839 const char *iface_type_name
;
4840 if (this->layout
.flags
.q
.in
) {
4841 var_mode
= ir_var_shader_in
;
4842 iface_type_name
= "in";
4843 } else if (this->layout
.flags
.q
.out
) {
4844 var_mode
= ir_var_shader_out
;
4845 iface_type_name
= "out";
4846 } else if (this->layout
.flags
.q
.uniform
) {
4847 var_mode
= ir_var_uniform
;
4848 iface_type_name
= "uniform";
4850 var_mode
= ir_var_auto
;
4851 iface_type_name
= "UNKNOWN";
4852 assert(!"interface block layout qualifier not found!");
4855 bool redeclaring_per_vertex
= strcmp(this->block_name
, "gl_PerVertex") == 0;
4856 bool block_row_major
= this->layout
.flags
.q
.row_major
;
4857 exec_list declared_variables
;
4858 glsl_struct_field
*fields
;
4859 unsigned int num_variables
=
4860 ast_process_structure_or_interface_block(&declared_variables
,
4862 &this->declarations
,
4867 redeclaring_per_vertex
,
4870 if (!redeclaring_per_vertex
)
4871 validate_identifier(this->block_name
, loc
, state
);
4873 const glsl_type
*earlier_per_vertex
= NULL
;
4874 if (redeclaring_per_vertex
) {
4875 /* Find the previous declaration of gl_PerVertex. If we're redeclaring
4876 * the named interface block gl_in, we can find it by looking at the
4877 * previous declaration of gl_in. Otherwise we can find it by looking
4878 * at the previous decalartion of any of the built-in outputs,
4881 * Also check that the instance name and array-ness of the redeclaration
4885 case ir_var_shader_in
:
4886 if (ir_variable
*earlier_gl_in
=
4887 state
->symbols
->get_variable("gl_in")) {
4888 earlier_per_vertex
= earlier_gl_in
->get_interface_type();
4890 _mesa_glsl_error(&loc
, state
,
4891 "redeclaration of gl_PerVertex input not allowed "
4893 _mesa_glsl_shader_target_name(state
->target
));
4895 if (this->instance_name
== NULL
||
4896 strcmp(this->instance_name
, "gl_in") != 0 || !this->is_array
) {
4897 _mesa_glsl_error(&loc
, state
,
4898 "gl_PerVertex input must be redeclared as "
4902 case ir_var_shader_out
:
4903 if (ir_variable
*earlier_gl_Position
=
4904 state
->symbols
->get_variable("gl_Position")) {
4905 earlier_per_vertex
= earlier_gl_Position
->get_interface_type();
4907 _mesa_glsl_error(&loc
, state
,
4908 "redeclaration of gl_PerVertex output not "
4909 "allowed in the %s shader",
4910 _mesa_glsl_shader_target_name(state
->target
));
4912 if (this->instance_name
!= NULL
) {
4913 _mesa_glsl_error(&loc
, state
,
4914 "gl_PerVertex input may not be redeclared with "
4915 "an instance name");
4919 _mesa_glsl_error(&loc
, state
,
4920 "gl_PerVertex must be declared as an input or an "
4925 if (earlier_per_vertex
== NULL
) {
4926 /* An error has already been reported. Bail out to avoid null
4927 * dereferences later in this function.
4932 /* Copy locations from the old gl_PerVertex interface block. */
4933 for (unsigned i
= 0; i
< num_variables
; i
++) {
4934 int j
= earlier_per_vertex
->field_index(fields
[i
].name
);
4936 _mesa_glsl_error(&loc
, state
,
4937 "redeclaration of gl_PerVertex must be a subset "
4938 "of the built-in members of gl_PerVertex");
4940 fields
[i
].location
=
4941 earlier_per_vertex
->fields
.structure
[j
].location
;
4942 fields
[i
].interpolation
=
4943 earlier_per_vertex
->fields
.structure
[j
].interpolation
;
4944 fields
[i
].centroid
=
4945 earlier_per_vertex
->fields
.structure
[j
].centroid
;
4947 earlier_per_vertex
->fields
.structure
[j
].sample
;
4951 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10
4954 * If a built-in interface block is redeclared, it must appear in
4955 * the shader before any use of any member included in the built-in
4956 * declaration, or a compilation error will result.
4958 * This appears to be a clarification to the behaviour established for
4959 * gl_PerVertex by GLSL 1.50, therefore we implement this behaviour
4960 * regardless of GLSL version.
4962 interface_block_usage_visitor
v(var_mode
, earlier_per_vertex
);
4963 v
.run(instructions
);
4964 if (v
.usage_found()) {
4965 _mesa_glsl_error(&loc
, state
,
4966 "redeclaration of a built-in interface block must "
4967 "appear before any use of any member of the "
4972 const glsl_type
*block_type
=
4973 glsl_type::get_interface_instance(fields
,
4978 if (!state
->symbols
->add_interface(block_type
->name
, block_type
, var_mode
)) {
4979 YYLTYPE loc
= this->get_location();
4980 _mesa_glsl_error(&loc
, state
, "interface block `%s' with type `%s' "
4981 "already taken in the current scope",
4982 this->block_name
, iface_type_name
);
4985 /* Since interface blocks cannot contain statements, it should be
4986 * impossible for the block to generate any instructions.
4988 assert(declared_variables
.is_empty());
4990 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
4992 * Geometry shader input variables get the per-vertex values written
4993 * out by vertex shader output variables of the same names. Since a
4994 * geometry shader operates on a set of vertices, each input varying
4995 * variable (or input block, see interface blocks below) needs to be
4996 * declared as an array.
4998 if (state
->target
== geometry_shader
&& !this->is_array
&&
4999 var_mode
== ir_var_shader_in
) {
5000 _mesa_glsl_error(&loc
, state
, "geometry shader inputs must be arrays");
5003 /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
5006 * "If an instance name (instance-name) is used, then it puts all the
5007 * members inside a scope within its own name space, accessed with the
5008 * field selector ( . ) operator (analogously to structures)."
5010 if (this->instance_name
) {
5011 if (redeclaring_per_vertex
) {
5012 /* When a built-in in an unnamed interface block is redeclared,
5013 * get_variable_being_redeclared() calls
5014 * check_builtin_array_max_size() to make sure that built-in array
5015 * variables aren't redeclared to illegal sizes. But we're looking
5016 * at a redeclaration of a named built-in interface block. So we
5017 * have to manually call check_builtin_array_max_size() for all parts
5018 * of the interface that are arrays.
5020 for (unsigned i
= 0; i
< num_variables
; i
++) {
5021 if (fields
[i
].type
->is_array()) {
5022 const unsigned size
= fields
[i
].type
->array_size();
5023 check_builtin_array_max_size(fields
[i
].name
, size
, loc
, state
);
5027 validate_identifier(this->instance_name
, loc
, state
);
5032 if (this->is_array
) {
5033 /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
5035 * For uniform blocks declared an array, each individual array
5036 * element corresponds to a separate buffer object backing one
5037 * instance of the block. As the array size indicates the number
5038 * of buffer objects needed, uniform block array declarations
5039 * must specify an array size.
5041 * And a few paragraphs later:
5043 * Geometry shader input blocks must be declared as arrays and
5044 * follow the array declaration and linking rules for all
5045 * geometry shader inputs. All other input and output block
5046 * arrays must specify an array size.
5048 * The upshot of this is that the only circumstance where an
5049 * interface array size *doesn't* need to be specified is on a
5050 * geometry shader input.
5052 if (this->array_size
== NULL
&&
5053 (state
->target
!= geometry_shader
|| !this->layout
.flags
.q
.in
)) {
5054 _mesa_glsl_error(&loc
, state
,
5055 "only geometry shader inputs may be unsized "
5056 "instance block arrays");
5060 const glsl_type
*block_array_type
=
5061 process_array_type(&loc
, block_type
, this->array_size
, state
);
5063 var
= new(state
) ir_variable(block_array_type
,
5064 this->instance_name
,
5067 var
= new(state
) ir_variable(block_type
,
5068 this->instance_name
,
5072 if (state
->target
== geometry_shader
&& var_mode
== ir_var_shader_in
)
5073 handle_geometry_shader_input_decl(state
, loc
, var
);
5075 if (ir_variable
*earlier
=
5076 state
->symbols
->get_variable(this->instance_name
)) {
5077 if (!redeclaring_per_vertex
) {
5078 _mesa_glsl_error(&loc
, state
, "`%s' redeclared",
5079 this->instance_name
);
5081 earlier
->data
.how_declared
= ir_var_declared_normally
;
5082 earlier
->type
= var
->type
;
5083 earlier
->reinit_interface_type(block_type
);
5086 state
->symbols
->add_variable(var
);
5087 instructions
->push_tail(var
);
5090 /* In order to have an array size, the block must also be declared with
5093 assert(!this->is_array
);
5095 for (unsigned i
= 0; i
< num_variables
; i
++) {
5097 new(state
) ir_variable(fields
[i
].type
,
5098 ralloc_strdup(state
, fields
[i
].name
),
5100 var
->data
.interpolation
= fields
[i
].interpolation
;
5101 var
->data
.centroid
= fields
[i
].centroid
;
5102 var
->data
.sample
= fields
[i
].sample
;
5103 var
->init_interface_type(block_type
);
5105 if (redeclaring_per_vertex
) {
5106 ir_variable
*earlier
=
5107 get_variable_being_redeclared(var
, loc
, state
,
5108 true /* allow_all_redeclarations */);
5109 if (strncmp(var
->name
, "gl_", 3) != 0 || earlier
== NULL
) {
5110 _mesa_glsl_error(&loc
, state
,
5111 "redeclaration of gl_PerVertex can only "
5112 "include built-in variables");
5113 } else if (earlier
->data
.how_declared
== ir_var_declared_normally
) {
5114 _mesa_glsl_error(&loc
, state
,
5115 "`%s' has already been redeclared", var
->name
);
5117 earlier
->data
.how_declared
= ir_var_declared_in_block
;
5118 earlier
->reinit_interface_type(block_type
);
5123 if (state
->symbols
->get_variable(var
->name
) != NULL
)
5124 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
5126 /* Propagate the "binding" keyword into this UBO's fields;
5127 * the UBO declaration itself doesn't get an ir_variable unless it
5128 * has an instance name. This is ugly.
5130 var
->data
.explicit_binding
= this->layout
.flags
.q
.explicit_binding
;
5131 var
->data
.binding
= this->layout
.binding
;
5133 state
->symbols
->add_variable(var
);
5134 instructions
->push_tail(var
);
5137 if (redeclaring_per_vertex
&& block_type
!= earlier_per_vertex
) {
5138 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec:
5140 * It is also a compilation error ... to redeclare a built-in
5141 * block and then use a member from that built-in block that was
5142 * not included in the redeclaration.
5144 * This appears to be a clarification to the behaviour established
5145 * for gl_PerVertex by GLSL 1.50, therefore we implement this
5146 * behaviour regardless of GLSL version.
5148 * To prevent the shader from using a member that was not included in
5149 * the redeclaration, we disable any ir_variables that are still
5150 * associated with the old declaration of gl_PerVertex (since we've
5151 * already updated all of the variables contained in the new
5152 * gl_PerVertex to point to it).
5154 * As a side effect this will prevent
5155 * validate_intrastage_interface_blocks() from getting confused and
5156 * thinking there are conflicting definitions of gl_PerVertex in the
5159 foreach_list_safe(node
, instructions
) {
5160 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
5162 var
->get_interface_type() == earlier_per_vertex
&&
5163 var
->data
.mode
== var_mode
) {
5164 if (var
->data
.how_declared
== ir_var_declared_normally
) {
5165 _mesa_glsl_error(&loc
, state
,
5166 "redeclaration of gl_PerVertex cannot "
5167 "follow a redeclaration of `%s'",
5170 state
->symbols
->disable_variable(var
->name
);
5182 ast_gs_input_layout::hir(exec_list
*instructions
,
5183 struct _mesa_glsl_parse_state
*state
)
5185 YYLTYPE loc
= this->get_location();
5187 /* If any geometry input layout declaration preceded this one, make sure it
5188 * was consistent with this one.
5190 if (state
->gs_input_prim_type_specified
&&
5191 state
->gs_input_prim_type
!= this->prim_type
) {
5192 _mesa_glsl_error(&loc
, state
,
5193 "geometry shader input layout does not match"
5194 " previous declaration");
5198 /* If any shader inputs occurred before this declaration and specified an
5199 * array size, make sure the size they specified is consistent with the
5202 unsigned num_vertices
= vertices_per_prim(this->prim_type
);
5203 if (state
->gs_input_size
!= 0 && state
->gs_input_size
!= num_vertices
) {
5204 _mesa_glsl_error(&loc
, state
,
5205 "this geometry shader input layout implies %u vertices"
5206 " per primitive, but a previous input is declared"
5207 " with size %u", num_vertices
, state
->gs_input_size
);
5211 state
->gs_input_prim_type_specified
= true;
5212 state
->gs_input_prim_type
= this->prim_type
;
5214 /* If any shader inputs occurred before this declaration and did not
5215 * specify an array size, their size is determined now.
5217 foreach_list (node
, instructions
) {
5218 ir_variable
*var
= ((ir_instruction
*) node
)->as_variable();
5219 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_in
)
5222 /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
5226 if (var
->type
->is_unsized_array()) {
5227 if (var
->data
.max_array_access
>= num_vertices
) {
5228 _mesa_glsl_error(&loc
, state
,
5229 "this geometry shader input layout implies %u"
5230 " vertices, but an access to element %u of input"
5231 " `%s' already exists", num_vertices
,
5232 var
->data
.max_array_access
, var
->name
);
5234 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
5245 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
5246 exec_list
*instructions
)
5248 bool gl_FragColor_assigned
= false;
5249 bool gl_FragData_assigned
= false;
5250 bool user_defined_fs_output_assigned
= false;
5251 ir_variable
*user_defined_fs_output
= NULL
;
5253 /* It would be nice to have proper location information. */
5255 memset(&loc
, 0, sizeof(loc
));
5257 foreach_list(node
, instructions
) {
5258 ir_variable
*var
= ((ir_instruction
*)node
)->as_variable();
5260 if (!var
|| !var
->data
.assigned
)
5263 if (strcmp(var
->name
, "gl_FragColor") == 0)
5264 gl_FragColor_assigned
= true;
5265 else if (strcmp(var
->name
, "gl_FragData") == 0)
5266 gl_FragData_assigned
= true;
5267 else if (strncmp(var
->name
, "gl_", 3) != 0) {
5268 if (state
->target
== fragment_shader
&&
5269 var
->data
.mode
== ir_var_shader_out
) {
5270 user_defined_fs_output_assigned
= true;
5271 user_defined_fs_output
= var
;
5276 /* From the GLSL 1.30 spec:
5278 * "If a shader statically assigns a value to gl_FragColor, it
5279 * may not assign a value to any element of gl_FragData. If a
5280 * shader statically writes a value to any element of
5281 * gl_FragData, it may not assign a value to
5282 * gl_FragColor. That is, a shader may assign values to either
5283 * gl_FragColor or gl_FragData, but not both. Multiple shaders
5284 * linked together must also consistently write just one of
5285 * these variables. Similarly, if user declared output
5286 * variables are in use (statically assigned to), then the
5287 * built-in variables gl_FragColor and gl_FragData may not be
5288 * assigned to. These incorrect usages all generate compile
5291 if (gl_FragColor_assigned
&& gl_FragData_assigned
) {
5292 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
5293 "`gl_FragColor' and `gl_FragData'");
5294 } else if (gl_FragColor_assigned
&& user_defined_fs_output_assigned
) {
5295 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
5296 "`gl_FragColor' and `%s'",
5297 user_defined_fs_output
->name
);
5298 } else if (gl_FragData_assigned
&& user_defined_fs_output_assigned
) {
5299 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
5300 "`gl_FragData' and `%s'",
5301 user_defined_fs_output
->name
);
5307 remove_per_vertex_blocks(exec_list
*instructions
,
5308 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
)
5310 /* Find the gl_PerVertex interface block of the appropriate (in/out) mode,
5311 * if it exists in this shader type.
5313 const glsl_type
*per_vertex
= NULL
;
5315 case ir_var_shader_in
:
5316 if (ir_variable
*gl_in
= state
->symbols
->get_variable("gl_in"))
5317 per_vertex
= gl_in
->get_interface_type();
5319 case ir_var_shader_out
:
5320 if (ir_variable
*gl_Position
=
5321 state
->symbols
->get_variable("gl_Position")) {
5322 per_vertex
= gl_Position
->get_interface_type();
5326 assert(!"Unexpected mode");
5330 /* If we didn't find a built-in gl_PerVertex interface block, then we don't
5331 * need to do anything.
5333 if (per_vertex
== NULL
)
5336 /* If the interface block is used by the shader, then we don't need to do
5339 interface_block_usage_visitor
v(mode
, per_vertex
);
5340 v
.run(instructions
);
5341 if (v
.usage_found())
5344 /* Remove any ir_variable declarations that refer to the interface block
5347 foreach_list_safe(node
, instructions
) {
5348 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
5349 if (var
!= NULL
&& var
->get_interface_type() == per_vertex
&&
5350 var
->data
.mode
== mode
) {
5351 state
->symbols
->disable_variable(var
->name
);