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());
743 ir_rvalue
*extract_channel
= NULL
;
745 /* If the assignment LHS comes back as an ir_binop_vector_extract
746 * expression, move it to the RHS as an ir_triop_vector_insert.
748 if (lhs
->ir_type
== ir_type_expression
) {
749 ir_expression
*const lhs_expr
= lhs
->as_expression();
751 if (unlikely(lhs_expr
->operation
== ir_binop_vector_extract
)) {
753 validate_assignment(state
, lhs_loc
, lhs
->type
,
754 rhs
, is_initializer
);
756 if (new_rhs
== NULL
) {
760 * - LHS: (expression float vector_extract <vec> <channel>)
764 * - RHS: (expression vec2 vector_insert <vec> <channel> <scalar>)
766 * The LHS type is now a vector instead of a scalar. Since GLSL
767 * allows assignments to be used as rvalues, we need to re-extract
768 * the channel from assignment_temp when returning the rvalue.
770 extract_channel
= lhs_expr
->operands
[1];
771 rhs
= new(ctx
) ir_expression(ir_triop_vector_insert
,
772 lhs_expr
->operands
[0]->type
,
773 lhs_expr
->operands
[0],
776 lhs
= lhs_expr
->operands
[0]->clone(ctx
, NULL
);
781 ir_variable
*lhs_var
= lhs
->variable_referenced();
783 lhs_var
->data
.assigned
= true;
785 if (!error_emitted
) {
786 if (non_lvalue_description
!= NULL
) {
787 _mesa_glsl_error(&lhs_loc
, state
,
789 non_lvalue_description
);
790 error_emitted
= true;
791 } else if (lhs
->variable_referenced() != NULL
792 && lhs
->variable_referenced()->data
.read_only
) {
793 _mesa_glsl_error(&lhs_loc
, state
,
794 "assignment to read-only variable '%s'",
795 lhs
->variable_referenced()->name
);
796 error_emitted
= true;
798 } else if (lhs
->type
->is_array() &&
799 !state
->check_version(120, 300, &lhs_loc
,
800 "whole array assignment forbidden")) {
801 /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
803 * "Other binary or unary expressions, non-dereferenced
804 * arrays, function names, swizzles with repeated fields,
805 * and constants cannot be l-values."
807 * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00.
809 error_emitted
= true;
810 } else if (!lhs
->is_lvalue()) {
811 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
812 error_emitted
= true;
817 validate_assignment(state
, lhs_loc
, lhs
->type
, rhs
, is_initializer
);
818 if (new_rhs
!= NULL
) {
821 /* If the LHS array was not declared with a size, it takes it size from
822 * the RHS. If the LHS is an l-value and a whole array, it must be a
823 * dereference of a variable. Any other case would require that the LHS
824 * is either not an l-value or not a whole array.
826 if (lhs
->type
->is_unsized_array()) {
827 ir_dereference
*const d
= lhs
->as_dereference();
831 ir_variable
*const var
= d
->variable_referenced();
835 if (var
->data
.max_array_access
>= unsigned(rhs
->type
->array_size())) {
836 /* FINISHME: This should actually log the location of the RHS. */
837 _mesa_glsl_error(& lhs_loc
, state
, "array size must be > %u due to "
839 var
->data
.max_array_access
);
842 var
->type
= glsl_type::get_array_instance(lhs
->type
->element_type(),
843 rhs
->type
->array_size());
846 if (lhs
->type
->is_array()) {
847 mark_whole_array_access(rhs
);
848 mark_whole_array_access(lhs
);
852 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
853 * but not post_inc) need the converted assigned value as an rvalue
854 * to handle things like:
858 * So we always just store the computed value being assigned to a
859 * temporary and return a deref of that temporary. If the rvalue
860 * ends up not being used, the temp will get copy-propagated out.
862 ir_variable
*var
= new(ctx
) ir_variable(rhs
->type
, "assignment_tmp",
864 ir_dereference_variable
*deref_var
= new(ctx
) ir_dereference_variable(var
);
865 instructions
->push_tail(var
);
866 instructions
->push_tail(new(ctx
) ir_assignment(deref_var
, rhs
));
867 deref_var
= new(ctx
) ir_dereference_variable(var
);
870 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, deref_var
));
872 if (extract_channel
) {
873 return new(ctx
) ir_expression(ir_binop_vector_extract
,
874 new(ctx
) ir_dereference_variable(var
),
875 extract_channel
->clone(ctx
, NULL
));
877 return new(ctx
) ir_dereference_variable(var
);
881 get_lvalue_copy(exec_list
*instructions
, ir_rvalue
*lvalue
)
883 void *ctx
= ralloc_parent(lvalue
);
886 var
= new(ctx
) ir_variable(lvalue
->type
, "_post_incdec_tmp",
888 instructions
->push_tail(var
);
889 var
->data
.mode
= ir_var_auto
;
891 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
894 return new(ctx
) ir_dereference_variable(var
);
899 ast_node::hir(exec_list
*instructions
,
900 struct _mesa_glsl_parse_state
*state
)
909 do_comparison(void *mem_ctx
, int operation
, ir_rvalue
*op0
, ir_rvalue
*op1
)
912 ir_rvalue
*cmp
= NULL
;
914 if (operation
== ir_binop_all_equal
)
915 join_op
= ir_binop_logic_and
;
917 join_op
= ir_binop_logic_or
;
919 switch (op0
->type
->base_type
) {
920 case GLSL_TYPE_FLOAT
:
924 return new(mem_ctx
) ir_expression(operation
, op0
, op1
);
926 case GLSL_TYPE_ARRAY
: {
927 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
928 ir_rvalue
*e0
, *e1
, *result
;
930 e0
= new(mem_ctx
) ir_dereference_array(op0
->clone(mem_ctx
, NULL
),
931 new(mem_ctx
) ir_constant(i
));
932 e1
= new(mem_ctx
) ir_dereference_array(op1
->clone(mem_ctx
, NULL
),
933 new(mem_ctx
) ir_constant(i
));
934 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
937 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
943 mark_whole_array_access(op0
);
944 mark_whole_array_access(op1
);
948 case GLSL_TYPE_STRUCT
: {
949 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
950 ir_rvalue
*e0
, *e1
, *result
;
951 const char *field_name
= op0
->type
->fields
.structure
[i
].name
;
953 e0
= new(mem_ctx
) ir_dereference_record(op0
->clone(mem_ctx
, NULL
),
955 e1
= new(mem_ctx
) ir_dereference_record(op1
->clone(mem_ctx
, NULL
),
957 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
960 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
968 case GLSL_TYPE_ERROR
:
970 case GLSL_TYPE_SAMPLER
:
971 case GLSL_TYPE_INTERFACE
:
972 case GLSL_TYPE_ATOMIC_UINT
:
973 /* I assume a comparison of a struct containing a sampler just
974 * ignores the sampler present in the type.
980 cmp
= new(mem_ctx
) ir_constant(true);
985 /* For logical operations, we want to ensure that the operands are
986 * scalar booleans. If it isn't, emit an error and return a constant
987 * boolean to avoid triggering cascading error messages.
990 get_scalar_boolean_operand(exec_list
*instructions
,
991 struct _mesa_glsl_parse_state
*state
,
992 ast_expression
*parent_expr
,
994 const char *operand_name
,
997 ast_expression
*expr
= parent_expr
->subexpressions
[operand
];
999 ir_rvalue
*val
= expr
->hir(instructions
, state
);
1001 if (val
->type
->is_boolean() && val
->type
->is_scalar())
1004 if (!*error_emitted
) {
1005 YYLTYPE loc
= expr
->get_location();
1006 _mesa_glsl_error(&loc
, state
, "%s of `%s' must be scalar boolean",
1008 parent_expr
->operator_string(parent_expr
->oper
));
1009 *error_emitted
= true;
1012 return new(ctx
) ir_constant(true);
1016 * If name refers to a builtin array whose maximum allowed size is less than
1017 * size, report an error and return true. Otherwise return false.
1020 check_builtin_array_max_size(const char *name
, unsigned size
,
1021 YYLTYPE loc
, struct _mesa_glsl_parse_state
*state
)
1023 if ((strcmp("gl_TexCoord", name
) == 0)
1024 && (size
> state
->Const
.MaxTextureCoords
)) {
1025 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1027 * "The size [of gl_TexCoord] can be at most
1028 * gl_MaxTextureCoords."
1030 _mesa_glsl_error(&loc
, state
, "`gl_TexCoord' array size cannot "
1031 "be larger than gl_MaxTextureCoords (%u)",
1032 state
->Const
.MaxTextureCoords
);
1033 } else if (strcmp("gl_ClipDistance", name
) == 0
1034 && size
> state
->Const
.MaxClipPlanes
) {
1035 /* From section 7.1 (Vertex Shader Special Variables) of the
1038 * "The gl_ClipDistance array is predeclared as unsized and
1039 * must be sized by the shader either redeclaring it with a
1040 * size or indexing it only with integral constant
1041 * expressions. ... The size can be at most
1042 * gl_MaxClipDistances."
1044 _mesa_glsl_error(&loc
, state
, "`gl_ClipDistance' array size cannot "
1045 "be larger than gl_MaxClipDistances (%u)",
1046 state
->Const
.MaxClipPlanes
);
1051 * Create the constant 1, of a which is appropriate for incrementing and
1052 * decrementing values of the given GLSL type. For example, if type is vec4,
1053 * this creates a constant value of 1.0 having type float.
1055 * If the given type is invalid for increment and decrement operators, return
1056 * a floating point 1--the error will be detected later.
1059 constant_one_for_inc_dec(void *ctx
, const glsl_type
*type
)
1061 switch (type
->base_type
) {
1062 case GLSL_TYPE_UINT
:
1063 return new(ctx
) ir_constant((unsigned) 1);
1065 return new(ctx
) ir_constant(1);
1067 case GLSL_TYPE_FLOAT
:
1068 return new(ctx
) ir_constant(1.0f
);
1073 ast_expression::hir(exec_list
*instructions
,
1074 struct _mesa_glsl_parse_state
*state
)
1077 static const int operations
[AST_NUM_OPERATORS
] = {
1078 -1, /* ast_assign doesn't convert to ir_expression. */
1079 -1, /* ast_plus doesn't convert to ir_expression. */
1093 ir_binop_any_nequal
,
1103 /* Note: The following block of expression types actually convert
1104 * to multiple IR instructions.
1106 ir_binop_mul
, /* ast_mul_assign */
1107 ir_binop_div
, /* ast_div_assign */
1108 ir_binop_mod
, /* ast_mod_assign */
1109 ir_binop_add
, /* ast_add_assign */
1110 ir_binop_sub
, /* ast_sub_assign */
1111 ir_binop_lshift
, /* ast_ls_assign */
1112 ir_binop_rshift
, /* ast_rs_assign */
1113 ir_binop_bit_and
, /* ast_and_assign */
1114 ir_binop_bit_xor
, /* ast_xor_assign */
1115 ir_binop_bit_or
, /* ast_or_assign */
1117 -1, /* ast_conditional doesn't convert to ir_expression. */
1118 ir_binop_add
, /* ast_pre_inc. */
1119 ir_binop_sub
, /* ast_pre_dec. */
1120 ir_binop_add
, /* ast_post_inc. */
1121 ir_binop_sub
, /* ast_post_dec. */
1122 -1, /* ast_field_selection doesn't conv to ir_expression. */
1123 -1, /* ast_array_index doesn't convert to ir_expression. */
1124 -1, /* ast_function_call doesn't conv to ir_expression. */
1125 -1, /* ast_identifier doesn't convert to ir_expression. */
1126 -1, /* ast_int_constant doesn't convert to ir_expression. */
1127 -1, /* ast_uint_constant doesn't conv to ir_expression. */
1128 -1, /* ast_float_constant doesn't conv to ir_expression. */
1129 -1, /* ast_bool_constant doesn't conv to ir_expression. */
1130 -1, /* ast_sequence doesn't convert to ir_expression. */
1132 ir_rvalue
*result
= NULL
;
1134 const struct glsl_type
*type
; /* a temporary variable for switch cases */
1135 bool error_emitted
= false;
1138 loc
= this->get_location();
1140 switch (this->oper
) {
1142 assert(!"ast_aggregate: Should never get here.");
1146 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1147 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1149 result
= do_assignment(instructions
, state
,
1150 this->subexpressions
[0]->non_lvalue_description
,
1151 op
[0], op
[1], false,
1152 this->subexpressions
[0]->get_location());
1153 error_emitted
= result
->type
->is_error();
1158 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1160 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1162 error_emitted
= type
->is_error();
1168 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1170 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1172 error_emitted
= type
->is_error();
1174 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1182 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1183 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1185 type
= arithmetic_result_type(op
[0], op
[1],
1186 (this->oper
== ast_mul
),
1188 error_emitted
= type
->is_error();
1190 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1195 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1196 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1198 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1200 assert(operations
[this->oper
] == ir_binop_mod
);
1202 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1204 error_emitted
= type
->is_error();
1209 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1210 error_emitted
= true;
1213 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1214 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1215 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1217 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1219 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1226 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1227 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1229 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
1231 /* The relational operators must either generate an error or result
1232 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1234 assert(type
->is_error()
1235 || ((type
->base_type
== GLSL_TYPE_BOOL
)
1236 && type
->is_scalar()));
1238 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1240 error_emitted
= type
->is_error();
1245 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1246 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1248 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1250 * "The equality operators equal (==), and not equal (!=)
1251 * operate on all types. They result in a scalar Boolean. If
1252 * the operand types do not match, then there must be a
1253 * conversion from Section 4.1.10 "Implicit Conversions"
1254 * applied to one operand that can make them match, in which
1255 * case this conversion is done."
1257 if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
1258 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
1259 || (op
[0]->type
!= op
[1]->type
)) {
1260 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
1261 "type", (this->oper
== ast_equal
) ? "==" : "!=");
1262 error_emitted
= true;
1263 } else if ((op
[0]->type
->is_array() || op
[1]->type
->is_array()) &&
1264 !state
->check_version(120, 300, &loc
,
1265 "array comparisons forbidden")) {
1266 error_emitted
= true;
1267 } else if ((op
[0]->type
->contains_opaque() ||
1268 op
[1]->type
->contains_opaque())) {
1269 _mesa_glsl_error(&loc
, state
, "opaque type comparisons forbidden");
1270 error_emitted
= true;
1273 if (error_emitted
) {
1274 result
= new(ctx
) ir_constant(false);
1276 result
= do_comparison(ctx
, operations
[this->oper
], op
[0], op
[1]);
1277 assert(result
->type
== glsl_type::bool_type
);
1284 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1285 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1286 type
= bit_logic_result_type(op
[0]->type
, op
[1]->type
, this->oper
,
1288 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1290 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1294 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1296 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1297 error_emitted
= true;
1300 if (!op
[0]->type
->is_integer()) {
1301 _mesa_glsl_error(&loc
, state
, "operand of `~' must be an integer");
1302 error_emitted
= true;
1305 type
= error_emitted
? glsl_type::error_type
: op
[0]->type
;
1306 result
= new(ctx
) ir_expression(ir_unop_bit_not
, type
, op
[0], NULL
);
1309 case ast_logic_and
: {
1310 exec_list rhs_instructions
;
1311 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1312 "LHS", &error_emitted
);
1313 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1314 "RHS", &error_emitted
);
1316 if (rhs_instructions
.is_empty()) {
1317 result
= new(ctx
) ir_expression(ir_binop_logic_and
, op
[0], op
[1]);
1318 type
= result
->type
;
1320 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1323 instructions
->push_tail(tmp
);
1325 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1326 instructions
->push_tail(stmt
);
1328 stmt
->then_instructions
.append_list(&rhs_instructions
);
1329 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1330 ir_assignment
*const then_assign
=
1331 new(ctx
) ir_assignment(then_deref
, op
[1]);
1332 stmt
->then_instructions
.push_tail(then_assign
);
1334 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1335 ir_assignment
*const else_assign
=
1336 new(ctx
) ir_assignment(else_deref
, new(ctx
) ir_constant(false));
1337 stmt
->else_instructions
.push_tail(else_assign
);
1339 result
= new(ctx
) ir_dereference_variable(tmp
);
1345 case ast_logic_or
: {
1346 exec_list rhs_instructions
;
1347 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1348 "LHS", &error_emitted
);
1349 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1350 "RHS", &error_emitted
);
1352 if (rhs_instructions
.is_empty()) {
1353 result
= new(ctx
) ir_expression(ir_binop_logic_or
, op
[0], op
[1]);
1354 type
= result
->type
;
1356 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1359 instructions
->push_tail(tmp
);
1361 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1362 instructions
->push_tail(stmt
);
1364 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1365 ir_assignment
*const then_assign
=
1366 new(ctx
) ir_assignment(then_deref
, new(ctx
) ir_constant(true));
1367 stmt
->then_instructions
.push_tail(then_assign
);
1369 stmt
->else_instructions
.append_list(&rhs_instructions
);
1370 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1371 ir_assignment
*const else_assign
=
1372 new(ctx
) ir_assignment(else_deref
, op
[1]);
1373 stmt
->else_instructions
.push_tail(else_assign
);
1375 result
= new(ctx
) ir_dereference_variable(tmp
);
1382 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1384 * "The logical binary operators and (&&), or ( | | ), and
1385 * exclusive or (^^). They operate only on two Boolean
1386 * expressions and result in a Boolean expression."
1388 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0, "LHS",
1390 op
[1] = get_scalar_boolean_operand(instructions
, state
, this, 1, "RHS",
1393 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1398 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1399 "operand", &error_emitted
);
1401 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1405 case ast_mul_assign
:
1406 case ast_div_assign
:
1407 case ast_add_assign
:
1408 case ast_sub_assign
: {
1409 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1410 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1412 type
= arithmetic_result_type(op
[0], op
[1],
1413 (this->oper
== ast_mul_assign
),
1416 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1419 result
= do_assignment(instructions
, state
,
1420 this->subexpressions
[0]->non_lvalue_description
,
1421 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1422 this->subexpressions
[0]->get_location());
1423 error_emitted
= (op
[0]->type
->is_error());
1425 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1426 * explicitly test for this because none of the binary expression
1427 * operators allow array operands either.
1433 case ast_mod_assign
: {
1434 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1435 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1437 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1439 assert(operations
[this->oper
] == ir_binop_mod
);
1441 ir_rvalue
*temp_rhs
;
1442 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1445 result
= do_assignment(instructions
, state
,
1446 this->subexpressions
[0]->non_lvalue_description
,
1447 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1448 this->subexpressions
[0]->get_location());
1449 error_emitted
= type
->is_error();
1454 case ast_rs_assign
: {
1455 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1456 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1457 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1459 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1460 type
, op
[0], op
[1]);
1461 result
= do_assignment(instructions
, state
,
1462 this->subexpressions
[0]->non_lvalue_description
,
1463 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1464 this->subexpressions
[0]->get_location());
1465 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1469 case ast_and_assign
:
1470 case ast_xor_assign
:
1471 case ast_or_assign
: {
1472 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1473 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1474 type
= bit_logic_result_type(op
[0]->type
, op
[1]->type
, this->oper
,
1476 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1477 type
, op
[0], op
[1]);
1478 result
= do_assignment(instructions
, state
,
1479 this->subexpressions
[0]->non_lvalue_description
,
1480 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1481 this->subexpressions
[0]->get_location());
1482 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1486 case ast_conditional
: {
1487 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1489 * "The ternary selection operator (?:). It operates on three
1490 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1491 * first expression, which must result in a scalar Boolean."
1493 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1494 "condition", &error_emitted
);
1496 /* The :? operator is implemented by generating an anonymous temporary
1497 * followed by an if-statement. The last instruction in each branch of
1498 * the if-statement assigns a value to the anonymous temporary. This
1499 * temporary is the r-value of the expression.
1501 exec_list then_instructions
;
1502 exec_list else_instructions
;
1504 op
[1] = this->subexpressions
[1]->hir(&then_instructions
, state
);
1505 op
[2] = this->subexpressions
[2]->hir(&else_instructions
, state
);
1507 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1509 * "The second and third expressions can be any type, as
1510 * long their types match, or there is a conversion in
1511 * Section 4.1.10 "Implicit Conversions" that can be applied
1512 * to one of the expressions to make their types match. This
1513 * resulting matching type is the type of the entire
1516 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1517 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1518 || (op
[1]->type
!= op
[2]->type
)) {
1519 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1521 _mesa_glsl_error(& loc
, state
, "second and third operands of ?: "
1522 "operator must have matching types");
1523 error_emitted
= true;
1524 type
= glsl_type::error_type
;
1529 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1531 * "The second and third expressions must be the same type, but can
1532 * be of any type other than an array."
1534 if (type
->is_array() &&
1535 !state
->check_version(120, 300, &loc
,
1536 "second and third operands of ?: operator "
1537 "cannot be arrays")) {
1538 error_emitted
= true;
1541 ir_constant
*cond_val
= op
[0]->constant_expression_value();
1542 ir_constant
*then_val
= op
[1]->constant_expression_value();
1543 ir_constant
*else_val
= op
[2]->constant_expression_value();
1545 if (then_instructions
.is_empty()
1546 && else_instructions
.is_empty()
1547 && (cond_val
!= NULL
) && (then_val
!= NULL
) && (else_val
!= NULL
)) {
1548 result
= (cond_val
->value
.b
[0]) ? then_val
: else_val
;
1550 ir_variable
*const tmp
=
1551 new(ctx
) ir_variable(type
, "conditional_tmp", ir_var_temporary
);
1552 instructions
->push_tail(tmp
);
1554 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1555 instructions
->push_tail(stmt
);
1557 then_instructions
.move_nodes_to(& stmt
->then_instructions
);
1558 ir_dereference
*const then_deref
=
1559 new(ctx
) ir_dereference_variable(tmp
);
1560 ir_assignment
*const then_assign
=
1561 new(ctx
) ir_assignment(then_deref
, op
[1]);
1562 stmt
->then_instructions
.push_tail(then_assign
);
1564 else_instructions
.move_nodes_to(& stmt
->else_instructions
);
1565 ir_dereference
*const else_deref
=
1566 new(ctx
) ir_dereference_variable(tmp
);
1567 ir_assignment
*const else_assign
=
1568 new(ctx
) ir_assignment(else_deref
, op
[2]);
1569 stmt
->else_instructions
.push_tail(else_assign
);
1571 result
= new(ctx
) ir_dereference_variable(tmp
);
1578 this->non_lvalue_description
= (this->oper
== ast_pre_inc
)
1579 ? "pre-increment operation" : "pre-decrement operation";
1581 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1582 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1584 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1586 ir_rvalue
*temp_rhs
;
1587 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1590 result
= do_assignment(instructions
, state
,
1591 this->subexpressions
[0]->non_lvalue_description
,
1592 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1593 this->subexpressions
[0]->get_location());
1594 error_emitted
= op
[0]->type
->is_error();
1599 case ast_post_dec
: {
1600 this->non_lvalue_description
= (this->oper
== ast_post_inc
)
1601 ? "post-increment operation" : "post-decrement operation";
1602 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1603 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1605 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1607 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1609 ir_rvalue
*temp_rhs
;
1610 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1613 /* Get a temporary of a copy of the lvalue before it's modified.
1614 * This may get thrown away later.
1616 result
= get_lvalue_copy(instructions
, op
[0]->clone(ctx
, NULL
));
1618 (void)do_assignment(instructions
, state
,
1619 this->subexpressions
[0]->non_lvalue_description
,
1620 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1621 this->subexpressions
[0]->get_location());
1623 error_emitted
= op
[0]->type
->is_error();
1627 case ast_field_selection
:
1628 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1631 case ast_array_index
: {
1632 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1634 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1635 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1637 result
= _mesa_ast_array_index_to_hir(ctx
, state
, op
[0], op
[1],
1640 if (result
->type
->is_error())
1641 error_emitted
= true;
1646 case ast_function_call
:
1647 /* Should *NEVER* get here. ast_function_call should always be handled
1648 * by ast_function_expression::hir.
1653 case ast_identifier
: {
1654 /* ast_identifier can appear several places in a full abstract syntax
1655 * tree. This particular use must be at location specified in the grammar
1656 * as 'variable_identifier'.
1659 state
->symbols
->get_variable(this->primary_expression
.identifier
);
1662 var
->data
.used
= true;
1663 result
= new(ctx
) ir_dereference_variable(var
);
1665 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
1666 this->primary_expression
.identifier
);
1668 result
= ir_rvalue::error_value(ctx
);
1669 error_emitted
= true;
1674 case ast_int_constant
:
1675 result
= new(ctx
) ir_constant(this->primary_expression
.int_constant
);
1678 case ast_uint_constant
:
1679 result
= new(ctx
) ir_constant(this->primary_expression
.uint_constant
);
1682 case ast_float_constant
:
1683 result
= new(ctx
) ir_constant(this->primary_expression
.float_constant
);
1686 case ast_bool_constant
:
1687 result
= new(ctx
) ir_constant(bool(this->primary_expression
.bool_constant
));
1690 case ast_sequence
: {
1691 /* It should not be possible to generate a sequence in the AST without
1692 * any expressions in it.
1694 assert(!this->expressions
.is_empty());
1696 /* The r-value of a sequence is the last expression in the sequence. If
1697 * the other expressions in the sequence do not have side-effects (and
1698 * therefore add instructions to the instruction list), they get dropped
1701 exec_node
*previous_tail_pred
= NULL
;
1702 YYLTYPE previous_operand_loc
= loc
;
1704 foreach_list_typed (ast_node
, ast
, link
, &this->expressions
) {
1705 /* If one of the operands of comma operator does not generate any
1706 * code, we want to emit a warning. At each pass through the loop
1707 * previous_tail_pred will point to the last instruction in the
1708 * stream *before* processing the previous operand. Naturally,
1709 * instructions->tail_pred will point to the last instruction in the
1710 * stream *after* processing the previous operand. If the two
1711 * pointers match, then the previous operand had no effect.
1713 * The warning behavior here differs slightly from GCC. GCC will
1714 * only emit a warning if none of the left-hand operands have an
1715 * effect. However, it will emit a warning for each. I believe that
1716 * there are some cases in C (especially with GCC extensions) where
1717 * it is useful to have an intermediate step in a sequence have no
1718 * effect, but I don't think these cases exist in GLSL. Either way,
1719 * it would be a giant hassle to replicate that behavior.
1721 if (previous_tail_pred
== instructions
->tail_pred
) {
1722 _mesa_glsl_warning(&previous_operand_loc
, state
,
1723 "left-hand operand of comma expression has "
1727 /* tail_pred is directly accessed instead of using the get_tail()
1728 * method for performance reasons. get_tail() has extra code to
1729 * return NULL when the list is empty. We don't care about that
1730 * here, so using tail_pred directly is fine.
1732 previous_tail_pred
= instructions
->tail_pred
;
1733 previous_operand_loc
= ast
->get_location();
1735 result
= ast
->hir(instructions
, state
);
1738 /* Any errors should have already been emitted in the loop above.
1740 error_emitted
= true;
1744 type
= NULL
; /* use result->type, not type. */
1745 assert(result
!= NULL
);
1747 if (result
->type
->is_error() && !error_emitted
)
1748 _mesa_glsl_error(& loc
, state
, "type mismatch");
1755 ast_expression_statement::hir(exec_list
*instructions
,
1756 struct _mesa_glsl_parse_state
*state
)
1758 /* It is possible to have expression statements that don't have an
1759 * expression. This is the solitary semicolon:
1761 * for (i = 0; i < 5; i++)
1764 * In this case the expression will be NULL. Test for NULL and don't do
1765 * anything in that case.
1767 if (expression
!= NULL
)
1768 expression
->hir(instructions
, state
);
1770 /* Statements do not have r-values.
1777 ast_compound_statement::hir(exec_list
*instructions
,
1778 struct _mesa_glsl_parse_state
*state
)
1781 state
->symbols
->push_scope();
1783 foreach_list_typed (ast_node
, ast
, link
, &this->statements
)
1784 ast
->hir(instructions
, state
);
1787 state
->symbols
->pop_scope();
1789 /* Compound statements do not have r-values.
1795 * Evaluate the given exec_node (which should be an ast_node representing
1796 * a single array dimension) and return its integer value.
1798 static const unsigned
1799 process_array_size(exec_node
*node
,
1800 struct _mesa_glsl_parse_state
*state
)
1802 exec_list dummy_instructions
;
1804 ast_node
*array_size
= exec_node_data(ast_node
, node
, link
);
1805 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
,
1807 YYLTYPE loc
= array_size
->get_location();
1810 _mesa_glsl_error(& loc
, state
,
1811 "array size could not be resolved");
1815 if (!ir
->type
->is_integer()) {
1816 _mesa_glsl_error(& loc
, state
,
1817 "array size must be integer type");
1821 if (!ir
->type
->is_scalar()) {
1822 _mesa_glsl_error(& loc
, state
,
1823 "array size must be scalar type");
1827 ir_constant
*const size
= ir
->constant_expression_value();
1829 _mesa_glsl_error(& loc
, state
, "array size must be a "
1830 "constant valued expression");
1834 if (size
->value
.i
[0] <= 0) {
1835 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
1839 assert(size
->type
== ir
->type
);
1841 /* If the array size is const (and we've verified that
1842 * it is) then no instructions should have been emitted
1843 * when we converted it to HIR. If they were emitted,
1844 * then either the array size isn't const after all, or
1845 * we are emitting unnecessary instructions.
1847 assert(dummy_instructions
.is_empty());
1849 return size
->value
.u
[0];
1852 static const glsl_type
*
1853 process_array_type(YYLTYPE
*loc
, const glsl_type
*base
,
1854 ast_array_specifier
*array_specifier
,
1855 struct _mesa_glsl_parse_state
*state
)
1857 const glsl_type
*array_type
= base
;
1859 if (array_specifier
!= NULL
) {
1860 if (base
->is_array()) {
1862 /* From page 19 (page 25) of the GLSL 1.20 spec:
1864 * "Only one-dimensional arrays may be declared."
1866 if (!state
->ARB_arrays_of_arrays_enable
) {
1867 _mesa_glsl_error(loc
, state
,
1868 "invalid array of `%s'"
1869 "GL_ARB_arrays_of_arrays "
1870 "required for defining arrays of arrays",
1872 return glsl_type::error_type
;
1875 if (base
->length
== 0) {
1876 _mesa_glsl_error(loc
, state
,
1877 "only the outermost array dimension can "
1880 return glsl_type::error_type
;
1884 for (exec_node
*node
= array_specifier
->array_dimensions
.tail_pred
;
1885 !node
->is_head_sentinel(); node
= node
->prev
) {
1886 unsigned array_size
= process_array_size(node
, state
);
1887 array_type
= glsl_type::get_array_instance(array_type
,
1891 if (array_specifier
->is_unsized_array
)
1892 array_type
= glsl_type::get_array_instance(array_type
, 0);
1900 ast_type_specifier::glsl_type(const char **name
,
1901 struct _mesa_glsl_parse_state
*state
) const
1903 const struct glsl_type
*type
;
1905 type
= state
->symbols
->get_type(this->type_name
);
1906 *name
= this->type_name
;
1908 YYLTYPE loc
= this->get_location();
1909 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
1915 ast_fully_specified_type::glsl_type(const char **name
,
1916 struct _mesa_glsl_parse_state
*state
) const
1918 const struct glsl_type
*type
= this->specifier
->glsl_type(name
, state
);
1923 if (type
->base_type
== GLSL_TYPE_FLOAT
1925 && state
->stage
== MESA_SHADER_FRAGMENT
1926 && this->qualifier
.precision
== ast_precision_none
1927 && state
->symbols
->get_variable("#default precision") == NULL
) {
1928 YYLTYPE loc
= this->get_location();
1929 _mesa_glsl_error(&loc
, state
,
1930 "no precision specified this scope for type `%s'",
1938 * Determine whether a toplevel variable declaration declares a varying. This
1939 * function operates by examining the variable's mode and the shader target,
1940 * so it correctly identifies linkage variables regardless of whether they are
1941 * declared using the deprecated "varying" syntax or the new "in/out" syntax.
1943 * Passing a non-toplevel variable declaration (e.g. a function parameter) to
1944 * this function will produce undefined results.
1947 is_varying_var(ir_variable
*var
, gl_shader_stage target
)
1950 case MESA_SHADER_VERTEX
:
1951 return var
->data
.mode
== ir_var_shader_out
;
1952 case MESA_SHADER_FRAGMENT
:
1953 return var
->data
.mode
== ir_var_shader_in
;
1955 return var
->data
.mode
== ir_var_shader_out
|| var
->data
.mode
== ir_var_shader_in
;
1961 * Matrix layout qualifiers are only allowed on certain types
1964 validate_matrix_layout_for_type(struct _mesa_glsl_parse_state
*state
,
1966 const glsl_type
*type
,
1969 if (var
&& !var
->is_in_uniform_block()) {
1970 /* Layout qualifiers may only apply to interface blocks and fields in
1973 _mesa_glsl_error(loc
, state
,
1974 "uniform block layout qualifiers row_major and "
1975 "column_major may not be applied to variables "
1976 "outside of uniform blocks");
1977 } else if (!type
->is_matrix()) {
1978 /* The OpenGL ES 3.0 conformance tests did not originally allow
1979 * matrix layout qualifiers on non-matrices. However, the OpenGL
1980 * 4.4 and OpenGL ES 3.0 (revision TBD) specifications were
1981 * amended to specifically allow these layouts on all types. Emit
1982 * a warning so that people know their code may not be portable.
1984 _mesa_glsl_warning(loc
, state
,
1985 "uniform block layout qualifiers row_major and "
1986 "column_major applied to non-matrix types may "
1987 "be rejected by older compilers");
1988 } else if (type
->is_record()) {
1989 /* We allow 'layout(row_major)' on structure types because it's the only
1990 * way to get row-major layouts on matrices contained in structures.
1992 _mesa_glsl_warning(loc
, state
,
1993 "uniform block layout qualifiers row_major and "
1994 "column_major applied to structure types is not "
1995 "strictly conformant and may be rejected by other "
2001 validate_binding_qualifier(struct _mesa_glsl_parse_state
*state
,
2004 const ast_type_qualifier
*qual
)
2006 if (var
->data
.mode
!= ir_var_uniform
) {
2007 _mesa_glsl_error(loc
, state
,
2008 "the \"binding\" qualifier only applies to uniforms");
2012 if (qual
->binding
< 0) {
2013 _mesa_glsl_error(loc
, state
, "binding values must be >= 0");
2017 const struct gl_context
*const ctx
= state
->ctx
;
2018 unsigned elements
= var
->type
->is_array() ? var
->type
->length
: 1;
2019 unsigned max_index
= qual
->binding
+ elements
- 1;
2021 if (var
->type
->is_interface()) {
2022 /* UBOs. From page 60 of the GLSL 4.20 specification:
2023 * "If the binding point for any uniform block instance is less than zero,
2024 * or greater than or equal to the implementation-dependent maximum
2025 * number of uniform buffer bindings, a compilation error will occur.
2026 * When the binding identifier is used with a uniform block instanced as
2027 * an array of size N, all elements of the array from binding through
2028 * binding + N – 1 must be within this range."
2030 * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
2032 if (max_index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2033 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d UBOs exceeds "
2034 "the maximum number of UBO binding points (%d)",
2035 qual
->binding
, elements
,
2036 ctx
->Const
.MaxUniformBufferBindings
);
2039 } else if (var
->type
->is_sampler() ||
2040 (var
->type
->is_array() && var
->type
->fields
.array
->is_sampler())) {
2041 /* Samplers. From page 63 of the GLSL 4.20 specification:
2042 * "If the binding is less than zero, or greater than or equal to the
2043 * implementation-dependent maximum supported number of units, a
2044 * compilation error will occur. When the binding identifier is used
2045 * with an array of size N, all elements of the array from binding
2046 * through binding + N - 1 must be within this range."
2048 unsigned limit
= ctx
->Const
.Program
[state
->stage
].MaxTextureImageUnits
;
2050 if (max_index
>= limit
) {
2051 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d samplers "
2052 "exceeds the maximum number of texture image units "
2053 "(%d)", qual
->binding
, elements
, limit
);
2057 } else if (var
->type
->contains_atomic()) {
2058 assert(ctx
->Const
.MaxAtomicBufferBindings
<= MAX_COMBINED_ATOMIC_BUFFERS
);
2059 if (unsigned(qual
->binding
) >= ctx
->Const
.MaxAtomicBufferBindings
) {
2060 _mesa_glsl_error(loc
, state
, "layout(binding = %d) exceeds the "
2061 " maximum number of atomic counter buffer bindings"
2062 "(%d)", qual
->binding
,
2063 ctx
->Const
.MaxAtomicBufferBindings
);
2068 _mesa_glsl_error(loc
, state
,
2069 "the \"binding\" qualifier only applies to uniform "
2070 "blocks, samplers, atomic counters, or arrays thereof");
2078 static glsl_interp_qualifier
2079 interpret_interpolation_qualifier(const struct ast_type_qualifier
*qual
,
2080 ir_variable_mode mode
,
2081 struct _mesa_glsl_parse_state
*state
,
2084 glsl_interp_qualifier interpolation
;
2085 if (qual
->flags
.q
.flat
)
2086 interpolation
= INTERP_QUALIFIER_FLAT
;
2087 else if (qual
->flags
.q
.noperspective
)
2088 interpolation
= INTERP_QUALIFIER_NOPERSPECTIVE
;
2089 else if (qual
->flags
.q
.smooth
)
2090 interpolation
= INTERP_QUALIFIER_SMOOTH
;
2092 interpolation
= INTERP_QUALIFIER_NONE
;
2094 if (interpolation
!= INTERP_QUALIFIER_NONE
) {
2095 if (mode
!= ir_var_shader_in
&& mode
!= ir_var_shader_out
) {
2096 _mesa_glsl_error(loc
, state
,
2097 "interpolation qualifier `%s' can only be applied to "
2098 "shader inputs or outputs.",
2099 interpolation_string(interpolation
));
2103 if ((state
->stage
== MESA_SHADER_VERTEX
&& mode
== ir_var_shader_in
) ||
2104 (state
->stage
== MESA_SHADER_FRAGMENT
&& mode
== ir_var_shader_out
)) {
2105 _mesa_glsl_error(loc
, state
,
2106 "interpolation qualifier `%s' cannot be applied to "
2107 "vertex shader inputs or fragment shader outputs",
2108 interpolation_string(interpolation
));
2112 return interpolation
;
2117 validate_explicit_location(const struct ast_type_qualifier
*qual
,
2119 struct _mesa_glsl_parse_state
*state
,
2124 /* In the vertex shader only shader inputs can be given explicit
2127 * In the fragment shader only shader outputs can be given explicit
2130 switch (state
->stage
) {
2131 case MESA_SHADER_VERTEX
:
2132 if (var
->data
.mode
== ir_var_shader_in
) {
2133 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
2142 case MESA_SHADER_GEOMETRY
:
2143 _mesa_glsl_error(loc
, state
,
2144 "geometry shader variables cannot be given "
2145 "explicit locations");
2148 case MESA_SHADER_FRAGMENT
:
2149 if (var
->data
.mode
== ir_var_shader_out
) {
2150 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
2161 _mesa_glsl_error(loc
, state
,
2162 "%s cannot be given an explicit location in %s shader",
2164 _mesa_shader_stage_to_string(state
->stage
));
2166 var
->data
.explicit_location
= true;
2168 /* This bit of silliness is needed because invalid explicit locations
2169 * are supposed to be flagged during linking. Small negative values
2170 * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
2171 * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
2172 * The linker needs to be able to differentiate these cases. This
2173 * ensures that negative values stay negative.
2175 if (qual
->location
>= 0) {
2176 var
->data
.location
= (state
->stage
== MESA_SHADER_VERTEX
)
2177 ? (qual
->location
+ VERT_ATTRIB_GENERIC0
)
2178 : (qual
->location
+ FRAG_RESULT_DATA0
);
2180 var
->data
.location
= qual
->location
;
2183 if (qual
->flags
.q
.explicit_index
) {
2184 /* From the GLSL 4.30 specification, section 4.4.2 (Output
2185 * Layout Qualifiers):
2187 * "It is also a compile-time error if a fragment shader
2188 * sets a layout index to less than 0 or greater than 1."
2190 * Older specifications don't mandate a behavior; we take
2191 * this as a clarification and always generate the error.
2193 if (qual
->index
< 0 || qual
->index
> 1) {
2194 _mesa_glsl_error(loc
, state
,
2195 "explicit index may only be 0 or 1");
2197 var
->data
.explicit_index
= true;
2198 var
->data
.index
= qual
->index
;
2207 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
2209 struct _mesa_glsl_parse_state
*state
,
2213 STATIC_ASSERT(sizeof(qual
->flags
.q
) <= sizeof(qual
->flags
.i
));
2215 if (qual
->flags
.q
.invariant
) {
2216 if (var
->data
.used
) {
2217 _mesa_glsl_error(loc
, state
,
2218 "variable `%s' may not be redeclared "
2219 "`invariant' after being used",
2222 var
->data
.invariant
= 1;
2226 if (qual
->flags
.q
.constant
|| qual
->flags
.q
.attribute
2227 || qual
->flags
.q
.uniform
2228 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
2229 var
->data
.read_only
= 1;
2231 if (qual
->flags
.q
.centroid
)
2232 var
->data
.centroid
= 1;
2234 if (qual
->flags
.q
.sample
)
2235 var
->data
.sample
= 1;
2237 if (qual
->flags
.q
.attribute
&& state
->stage
!= MESA_SHADER_VERTEX
) {
2238 var
->type
= glsl_type::error_type
;
2239 _mesa_glsl_error(loc
, state
,
2240 "`attribute' variables may not be declared in the "
2242 _mesa_shader_stage_to_string(state
->stage
));
2245 /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
2247 * "However, the const qualifier cannot be used with out or inout."
2249 * The same section of the GLSL 4.40 spec further clarifies this saying:
2251 * "The const qualifier cannot be used with out or inout, or a
2252 * compile-time error results."
2254 if (is_parameter
&& qual
->flags
.q
.constant
&& qual
->flags
.q
.out
) {
2255 _mesa_glsl_error(loc
, state
,
2256 "`const' may not be applied to `out' or `inout' "
2257 "function parameters");
2260 /* If there is no qualifier that changes the mode of the variable, leave
2261 * the setting alone.
2263 if (qual
->flags
.q
.in
&& qual
->flags
.q
.out
)
2264 var
->data
.mode
= ir_var_function_inout
;
2265 else if (qual
->flags
.q
.in
)
2266 var
->data
.mode
= is_parameter
? ir_var_function_in
: ir_var_shader_in
;
2267 else if (qual
->flags
.q
.attribute
2268 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
2269 var
->data
.mode
= ir_var_shader_in
;
2270 else if (qual
->flags
.q
.out
)
2271 var
->data
.mode
= is_parameter
? ir_var_function_out
: ir_var_shader_out
;
2272 else if (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_VERTEX
))
2273 var
->data
.mode
= ir_var_shader_out
;
2274 else if (qual
->flags
.q
.uniform
)
2275 var
->data
.mode
= ir_var_uniform
;
2277 if (!is_parameter
&& is_varying_var(var
, state
->stage
)) {
2278 /* This variable is being used to link data between shader stages (in
2279 * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
2280 * that is allowed for such purposes.
2282 * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
2284 * "The varying qualifier can be used only with the data types
2285 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
2288 * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
2289 * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
2291 * "Fragment inputs can only be signed and unsigned integers and
2292 * integer vectors, float, floating-point vectors, matrices, or
2293 * arrays of these. Structures cannot be input.
2295 * Similar text exists in the section on vertex shader outputs.
2297 * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
2298 * 3.00 spec allows structs as well. Varying structs are also allowed
2301 switch (var
->type
->get_scalar_type()->base_type
) {
2302 case GLSL_TYPE_FLOAT
:
2303 /* Ok in all GLSL versions */
2305 case GLSL_TYPE_UINT
:
2307 if (state
->is_version(130, 300))
2309 _mesa_glsl_error(loc
, state
,
2310 "varying variables must be of base type float in %s",
2311 state
->get_version_string());
2313 case GLSL_TYPE_STRUCT
:
2314 if (state
->is_version(150, 300))
2316 _mesa_glsl_error(loc
, state
,
2317 "varying variables may not be of type struct");
2320 _mesa_glsl_error(loc
, state
, "illegal type for a varying variable");
2325 if (state
->all_invariant
&& (state
->current_function
== NULL
)) {
2326 switch (state
->stage
) {
2327 case MESA_SHADER_VERTEX
:
2328 if (var
->data
.mode
== ir_var_shader_out
)
2329 var
->data
.invariant
= true;
2331 case MESA_SHADER_GEOMETRY
:
2332 if ((var
->data
.mode
== ir_var_shader_in
)
2333 || (var
->data
.mode
== ir_var_shader_out
))
2334 var
->data
.invariant
= true;
2336 case MESA_SHADER_FRAGMENT
:
2337 if (var
->data
.mode
== ir_var_shader_in
)
2338 var
->data
.invariant
= true;
2343 var
->data
.interpolation
=
2344 interpret_interpolation_qualifier(qual
, (ir_variable_mode
) var
->data
.mode
,
2347 var
->data
.pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
2348 var
->data
.origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
2349 if ((qual
->flags
.q
.origin_upper_left
|| qual
->flags
.q
.pixel_center_integer
)
2350 && (strcmp(var
->name
, "gl_FragCoord") != 0)) {
2351 const char *const qual_string
= (qual
->flags
.q
.origin_upper_left
)
2352 ? "origin_upper_left" : "pixel_center_integer";
2354 _mesa_glsl_error(loc
, state
,
2355 "layout qualifier `%s' can only be applied to "
2356 "fragment shader input `gl_FragCoord'",
2360 if (qual
->flags
.q
.explicit_location
) {
2361 validate_explicit_location(qual
, var
, state
, loc
);
2362 } else if (qual
->flags
.q
.explicit_index
) {
2363 _mesa_glsl_error(loc
, state
,
2364 "explicit index requires explicit location");
2367 if (qual
->flags
.q
.explicit_binding
&&
2368 validate_binding_qualifier(state
, loc
, var
, qual
)) {
2369 var
->data
.explicit_binding
= true;
2370 var
->data
.binding
= qual
->binding
;
2373 if (var
->type
->contains_atomic()) {
2374 if (var
->data
.mode
== ir_var_uniform
) {
2375 if (var
->data
.explicit_binding
) {
2377 &state
->atomic_counter_offsets
[var
->data
.binding
];
2379 if (*offset
% ATOMIC_COUNTER_SIZE
)
2380 _mesa_glsl_error(loc
, state
,
2381 "misaligned atomic counter offset");
2383 var
->data
.atomic
.offset
= *offset
;
2384 *offset
+= var
->type
->atomic_size();
2387 _mesa_glsl_error(loc
, state
,
2388 "atomic counters require explicit binding point");
2390 } else if (var
->data
.mode
!= ir_var_function_in
) {
2391 _mesa_glsl_error(loc
, state
, "atomic counters may only be declared as "
2392 "function parameters or uniform-qualified "
2393 "global variables");
2397 /* Does the declaration use the deprecated 'attribute' or 'varying'
2400 const bool uses_deprecated_qualifier
= qual
->flags
.q
.attribute
2401 || qual
->flags
.q
.varying
;
2403 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
2404 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
2405 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
2406 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
2407 * These extensions and all following extensions that add the 'layout'
2408 * keyword have been modified to require the use of 'in' or 'out'.
2410 * The following extension do not allow the deprecated keywords:
2412 * GL_AMD_conservative_depth
2413 * GL_ARB_conservative_depth
2414 * GL_ARB_gpu_shader5
2415 * GL_ARB_separate_shader_objects
2416 * GL_ARB_tesselation_shader
2417 * GL_ARB_transform_feedback3
2418 * GL_ARB_uniform_buffer_object
2420 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
2421 * allow layout with the deprecated keywords.
2423 const bool relaxed_layout_qualifier_checking
=
2424 state
->ARB_fragment_coord_conventions_enable
;
2426 if (qual
->has_layout() && uses_deprecated_qualifier
) {
2427 if (relaxed_layout_qualifier_checking
) {
2428 _mesa_glsl_warning(loc
, state
,
2429 "`layout' qualifier may not be used with "
2430 "`attribute' or `varying'");
2432 _mesa_glsl_error(loc
, state
,
2433 "`layout' qualifier may not be used with "
2434 "`attribute' or `varying'");
2438 /* Layout qualifiers for gl_FragDepth, which are enabled by extension
2439 * AMD_conservative_depth.
2441 int depth_layout_count
= qual
->flags
.q
.depth_any
2442 + qual
->flags
.q
.depth_greater
2443 + qual
->flags
.q
.depth_less
2444 + qual
->flags
.q
.depth_unchanged
;
2445 if (depth_layout_count
> 0
2446 && !state
->AMD_conservative_depth_enable
2447 && !state
->ARB_conservative_depth_enable
) {
2448 _mesa_glsl_error(loc
, state
,
2449 "extension GL_AMD_conservative_depth or "
2450 "GL_ARB_conservative_depth must be enabled "
2451 "to use depth layout qualifiers");
2452 } else if (depth_layout_count
> 0
2453 && strcmp(var
->name
, "gl_FragDepth") != 0) {
2454 _mesa_glsl_error(loc
, state
,
2455 "depth layout qualifiers can be applied only to "
2457 } else if (depth_layout_count
> 1
2458 && strcmp(var
->name
, "gl_FragDepth") == 0) {
2459 _mesa_glsl_error(loc
, state
,
2460 "at most one depth layout qualifier can be applied to "
2463 if (qual
->flags
.q
.depth_any
)
2464 var
->data
.depth_layout
= ir_depth_layout_any
;
2465 else if (qual
->flags
.q
.depth_greater
)
2466 var
->data
.depth_layout
= ir_depth_layout_greater
;
2467 else if (qual
->flags
.q
.depth_less
)
2468 var
->data
.depth_layout
= ir_depth_layout_less
;
2469 else if (qual
->flags
.q
.depth_unchanged
)
2470 var
->data
.depth_layout
= ir_depth_layout_unchanged
;
2472 var
->data
.depth_layout
= ir_depth_layout_none
;
2474 if (qual
->flags
.q
.std140
||
2475 qual
->flags
.q
.packed
||
2476 qual
->flags
.q
.shared
) {
2477 _mesa_glsl_error(loc
, state
,
2478 "uniform block layout qualifiers std140, packed, and "
2479 "shared can only be applied to uniform blocks, not "
2483 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
2484 validate_matrix_layout_for_type(state
, loc
, var
->type
, var
);
2489 * Get the variable that is being redeclared by this declaration
2491 * Semantic checks to verify the validity of the redeclaration are also
2492 * performed. If semantic checks fail, compilation error will be emitted via
2493 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
2496 * A pointer to an existing variable in the current scope if the declaration
2497 * is a redeclaration, \c NULL otherwise.
2499 static ir_variable
*
2500 get_variable_being_redeclared(ir_variable
*var
, YYLTYPE loc
,
2501 struct _mesa_glsl_parse_state
*state
,
2502 bool allow_all_redeclarations
)
2504 /* Check if this declaration is actually a re-declaration, either to
2505 * resize an array or add qualifiers to an existing variable.
2507 * This is allowed for variables in the current scope, or when at
2508 * global scope (for built-ins in the implicit outer scope).
2510 ir_variable
*earlier
= state
->symbols
->get_variable(var
->name
);
2511 if (earlier
== NULL
||
2512 (state
->current_function
!= NULL
&&
2513 !state
->symbols
->name_declared_this_scope(var
->name
))) {
2518 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
2520 * "It is legal to declare an array without a size and then
2521 * later re-declare the same name as an array of the same
2522 * type and specify a size."
2524 if (earlier
->type
->is_unsized_array() && var
->type
->is_array()
2525 && (var
->type
->element_type() == earlier
->type
->element_type())) {
2526 /* FINISHME: This doesn't match the qualifiers on the two
2527 * FINISHME: declarations. It's not 100% clear whether this is
2528 * FINISHME: required or not.
2531 const unsigned size
= unsigned(var
->type
->array_size());
2532 check_builtin_array_max_size(var
->name
, size
, loc
, state
);
2533 if ((size
> 0) && (size
<= earlier
->data
.max_array_access
)) {
2534 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
2536 earlier
->data
.max_array_access
);
2539 earlier
->type
= var
->type
;
2542 } else if ((state
->ARB_fragment_coord_conventions_enable
||
2543 state
->is_version(150, 0))
2544 && strcmp(var
->name
, "gl_FragCoord") == 0
2545 && earlier
->type
== var
->type
2546 && earlier
->data
.mode
== var
->data
.mode
) {
2547 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
2550 earlier
->data
.origin_upper_left
= var
->data
.origin_upper_left
;
2551 earlier
->data
.pixel_center_integer
= var
->data
.pixel_center_integer
;
2553 /* According to section 4.3.7 of the GLSL 1.30 spec,
2554 * the following built-in varaibles can be redeclared with an
2555 * interpolation qualifier:
2558 * * gl_FrontSecondaryColor
2559 * * gl_BackSecondaryColor
2561 * * gl_SecondaryColor
2563 } else if (state
->is_version(130, 0)
2564 && (strcmp(var
->name
, "gl_FrontColor") == 0
2565 || strcmp(var
->name
, "gl_BackColor") == 0
2566 || strcmp(var
->name
, "gl_FrontSecondaryColor") == 0
2567 || strcmp(var
->name
, "gl_BackSecondaryColor") == 0
2568 || strcmp(var
->name
, "gl_Color") == 0
2569 || strcmp(var
->name
, "gl_SecondaryColor") == 0)
2570 && earlier
->type
== var
->type
2571 && earlier
->data
.mode
== var
->data
.mode
) {
2572 earlier
->data
.interpolation
= var
->data
.interpolation
;
2574 /* Layout qualifiers for gl_FragDepth. */
2575 } else if ((state
->AMD_conservative_depth_enable
||
2576 state
->ARB_conservative_depth_enable
)
2577 && strcmp(var
->name
, "gl_FragDepth") == 0
2578 && earlier
->type
== var
->type
2579 && earlier
->data
.mode
== var
->data
.mode
) {
2581 /** From the AMD_conservative_depth spec:
2582 * Within any shader, the first redeclarations of gl_FragDepth
2583 * must appear before any use of gl_FragDepth.
2585 if (earlier
->data
.used
) {
2586 _mesa_glsl_error(&loc
, state
,
2587 "the first redeclaration of gl_FragDepth "
2588 "must appear before any use of gl_FragDepth");
2591 /* Prevent inconsistent redeclaration of depth layout qualifier. */
2592 if (earlier
->data
.depth_layout
!= ir_depth_layout_none
2593 && earlier
->data
.depth_layout
!= var
->data
.depth_layout
) {
2594 _mesa_glsl_error(&loc
, state
,
2595 "gl_FragDepth: depth layout is declared here "
2596 "as '%s, but it was previously declared as "
2598 depth_layout_string(var
->data
.depth_layout
),
2599 depth_layout_string(earlier
->data
.depth_layout
));
2602 earlier
->data
.depth_layout
= var
->data
.depth_layout
;
2604 } else if (allow_all_redeclarations
) {
2605 if (earlier
->data
.mode
!= var
->data
.mode
) {
2606 _mesa_glsl_error(&loc
, state
,
2607 "redeclaration of `%s' with incorrect qualifiers",
2609 } else if (earlier
->type
!= var
->type
) {
2610 _mesa_glsl_error(&loc
, state
,
2611 "redeclaration of `%s' has incorrect type",
2615 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
2622 * Generate the IR for an initializer in a variable declaration
2625 process_initializer(ir_variable
*var
, ast_declaration
*decl
,
2626 ast_fully_specified_type
*type
,
2627 exec_list
*initializer_instructions
,
2628 struct _mesa_glsl_parse_state
*state
)
2630 ir_rvalue
*result
= NULL
;
2632 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
2634 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
2636 * "All uniform variables are read-only and are initialized either
2637 * directly by an application via API commands, or indirectly by
2640 if (var
->data
.mode
== ir_var_uniform
) {
2641 state
->check_version(120, 0, &initializer_loc
,
2642 "cannot initialize uniforms");
2645 if (var
->type
->is_sampler()) {
2646 _mesa_glsl_error(& initializer_loc
, state
,
2647 "cannot initialize samplers");
2650 if ((var
->data
.mode
== ir_var_shader_in
) && (state
->current_function
== NULL
)) {
2651 _mesa_glsl_error(& initializer_loc
, state
,
2652 "cannot initialize %s shader input / %s",
2653 _mesa_shader_stage_to_string(state
->stage
),
2654 (state
->stage
== MESA_SHADER_VERTEX
)
2655 ? "attribute" : "varying");
2658 /* If the initializer is an ast_aggregate_initializer, recursively store
2659 * type information from the LHS into it, so that its hir() function can do
2662 if (decl
->initializer
->oper
== ast_aggregate
)
2663 _mesa_ast_set_aggregate_type(var
->type
, decl
->initializer
);
2665 ir_dereference
*const lhs
= new(state
) ir_dereference_variable(var
);
2666 ir_rvalue
*rhs
= decl
->initializer
->hir(initializer_instructions
,
2669 /* Calculate the constant value if this is a const or uniform
2672 if (type
->qualifier
.flags
.q
.constant
2673 || type
->qualifier
.flags
.q
.uniform
) {
2674 ir_rvalue
*new_rhs
= validate_assignment(state
, initializer_loc
,
2675 var
->type
, rhs
, true);
2676 if (new_rhs
!= NULL
) {
2679 ir_constant
*constant_value
= rhs
->constant_expression_value();
2680 if (!constant_value
) {
2681 /* If ARB_shading_language_420pack is enabled, initializers of
2682 * const-qualified local variables do not have to be constant
2683 * expressions. Const-qualified global variables must still be
2684 * initialized with constant expressions.
2686 if (!state
->ARB_shading_language_420pack_enable
2687 || state
->current_function
== NULL
) {
2688 _mesa_glsl_error(& initializer_loc
, state
,
2689 "initializer of %s variable `%s' must be a "
2690 "constant expression",
2691 (type
->qualifier
.flags
.q
.constant
)
2692 ? "const" : "uniform",
2694 if (var
->type
->is_numeric()) {
2695 /* Reduce cascading errors. */
2696 var
->constant_value
= ir_constant::zero(state
, var
->type
);
2700 rhs
= constant_value
;
2701 var
->constant_value
= constant_value
;
2704 if (var
->type
->is_numeric()) {
2705 /* Reduce cascading errors. */
2706 var
->constant_value
= ir_constant::zero(state
, var
->type
);
2711 if (rhs
&& !rhs
->type
->is_error()) {
2712 bool temp
= var
->data
.read_only
;
2713 if (type
->qualifier
.flags
.q
.constant
)
2714 var
->data
.read_only
= false;
2716 /* Never emit code to initialize a uniform.
2718 const glsl_type
*initializer_type
;
2719 if (!type
->qualifier
.flags
.q
.uniform
) {
2720 result
= do_assignment(initializer_instructions
, state
,
2723 type
->get_location());
2724 initializer_type
= result
->type
;
2726 initializer_type
= rhs
->type
;
2728 var
->constant_initializer
= rhs
->constant_expression_value();
2729 var
->data
.has_initializer
= true;
2731 /* If the declared variable is an unsized array, it must inherrit
2732 * its full type from the initializer. A declaration such as
2734 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
2738 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
2740 * The assignment generated in the if-statement (below) will also
2741 * automatically handle this case for non-uniforms.
2743 * If the declared variable is not an array, the types must
2744 * already match exactly. As a result, the type assignment
2745 * here can be done unconditionally. For non-uniforms the call
2746 * to do_assignment can change the type of the initializer (via
2747 * the implicit conversion rules). For uniforms the initializer
2748 * must be a constant expression, and the type of that expression
2749 * was validated above.
2751 var
->type
= initializer_type
;
2753 var
->data
.read_only
= temp
;
2761 * Do additional processing necessary for geometry shader input declarations
2762 * (this covers both interface blocks arrays and bare input variables).
2765 handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
2766 YYLTYPE loc
, ir_variable
*var
)
2768 unsigned num_vertices
= 0;
2769 if (state
->gs_input_prim_type_specified
) {
2770 num_vertices
= vertices_per_prim(state
->gs_input_prim_type
);
2773 /* Geometry shader input variables must be arrays. Caller should have
2774 * reported an error for this.
2776 if (!var
->type
->is_array()) {
2777 assert(state
->error
);
2779 /* To avoid cascading failures, short circuit the checks below. */
2783 if (var
->type
->is_unsized_array()) {
2784 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says:
2786 * All geometry shader input unsized array declarations will be
2787 * sized by an earlier input layout qualifier, when present, as per
2788 * the following table.
2790 * Followed by a table mapping each allowed input layout qualifier to
2791 * the corresponding input length.
2793 if (num_vertices
!= 0)
2794 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
2797 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec
2798 * includes the following examples of compile-time errors:
2800 * // code sequence within one shader...
2801 * in vec4 Color1[]; // size unknown
2802 * ...Color1.length()...// illegal, length() unknown
2803 * in vec4 Color2[2]; // size is 2
2804 * ...Color1.length()...// illegal, Color1 still has no size
2805 * in vec4 Color3[3]; // illegal, input sizes are inconsistent
2806 * layout(lines) in; // legal, input size is 2, matching
2807 * in vec4 Color4[3]; // illegal, contradicts layout
2810 * To detect the case illustrated by Color3, we verify that the size of
2811 * an explicitly-sized array matches the size of any previously declared
2812 * explicitly-sized array. To detect the case illustrated by Color4, we
2813 * verify that the size of an explicitly-sized array is consistent with
2814 * any previously declared input layout.
2816 if (num_vertices
!= 0 && var
->type
->length
!= num_vertices
) {
2817 _mesa_glsl_error(&loc
, state
,
2818 "geometry shader input size contradicts previously"
2819 " declared layout (size is %u, but layout requires a"
2820 " size of %u)", var
->type
->length
, num_vertices
);
2821 } else if (state
->gs_input_size
!= 0 &&
2822 var
->type
->length
!= state
->gs_input_size
) {
2823 _mesa_glsl_error(&loc
, state
,
2824 "geometry shader input sizes are "
2825 "inconsistent (size is %u, but a previous "
2826 "declaration has size %u)",
2827 var
->type
->length
, state
->gs_input_size
);
2829 state
->gs_input_size
= var
->type
->length
;
2836 validate_identifier(const char *identifier
, YYLTYPE loc
,
2837 struct _mesa_glsl_parse_state
*state
)
2839 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
2841 * "Identifiers starting with "gl_" are reserved for use by
2842 * OpenGL, and may not be declared in a shader as either a
2843 * variable or a function."
2845 if (strncmp(identifier
, "gl_", 3) == 0) {
2846 _mesa_glsl_error(&loc
, state
,
2847 "identifier `%s' uses reserved `gl_' prefix",
2849 } else if (strstr(identifier
, "__")) {
2850 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
2853 * "In addition, all identifiers containing two
2854 * consecutive underscores (__) are reserved as
2855 * possible future keywords."
2857 _mesa_glsl_error(&loc
, state
,
2858 "identifier `%s' uses reserved `__' string",
2865 ast_declarator_list::hir(exec_list
*instructions
,
2866 struct _mesa_glsl_parse_state
*state
)
2869 const struct glsl_type
*decl_type
;
2870 const char *type_name
= NULL
;
2871 ir_rvalue
*result
= NULL
;
2872 YYLTYPE loc
= this->get_location();
2874 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
2876 * "To ensure that a particular output variable is invariant, it is
2877 * necessary to use the invariant qualifier. It can either be used to
2878 * qualify a previously declared variable as being invariant
2880 * invariant gl_Position; // make existing gl_Position be invariant"
2882 * In these cases the parser will set the 'invariant' flag in the declarator
2883 * list, and the type will be NULL.
2885 if (this->invariant
) {
2886 assert(this->type
== NULL
);
2888 if (state
->current_function
!= NULL
) {
2889 _mesa_glsl_error(& loc
, state
,
2890 "all uses of `invariant' keyword must be at global "
2894 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
2895 assert(decl
->array_specifier
== NULL
);
2896 assert(decl
->initializer
== NULL
);
2898 ir_variable
*const earlier
=
2899 state
->symbols
->get_variable(decl
->identifier
);
2900 if (earlier
== NULL
) {
2901 _mesa_glsl_error(& loc
, state
,
2902 "undeclared variable `%s' cannot be marked "
2903 "invariant", decl
->identifier
);
2904 } else if ((state
->stage
== MESA_SHADER_VERTEX
)
2905 && (earlier
->data
.mode
!= ir_var_shader_out
)) {
2906 _mesa_glsl_error(& loc
, state
,
2907 "`%s' cannot be marked invariant, vertex shader "
2908 "outputs only", decl
->identifier
);
2909 } else if ((state
->stage
== MESA_SHADER_FRAGMENT
)
2910 && (earlier
->data
.mode
!= ir_var_shader_in
)) {
2911 _mesa_glsl_error(& loc
, state
,
2912 "`%s' cannot be marked invariant, fragment shader "
2913 "inputs only", decl
->identifier
);
2914 } else if (earlier
->data
.used
) {
2915 _mesa_glsl_error(& loc
, state
,
2916 "variable `%s' may not be redeclared "
2917 "`invariant' after being used",
2920 earlier
->data
.invariant
= true;
2924 /* Invariant redeclarations do not have r-values.
2929 assert(this->type
!= NULL
);
2930 assert(!this->invariant
);
2932 /* The type specifier may contain a structure definition. Process that
2933 * before any of the variable declarations.
2935 (void) this->type
->specifier
->hir(instructions
, state
);
2937 decl_type
= this->type
->glsl_type(& type_name
, state
);
2939 /* An offset-qualified atomic counter declaration sets the default
2940 * offset for the next declaration within the same atomic counter
2943 if (decl_type
&& decl_type
->contains_atomic()) {
2944 if (type
->qualifier
.flags
.q
.explicit_binding
&&
2945 type
->qualifier
.flags
.q
.explicit_offset
)
2946 state
->atomic_counter_offsets
[type
->qualifier
.binding
] =
2947 type
->qualifier
.offset
;
2950 if (this->declarations
.is_empty()) {
2951 /* If there is no structure involved in the program text, there are two
2952 * possible scenarios:
2954 * - The program text contained something like 'vec4;'. This is an
2955 * empty declaration. It is valid but weird. Emit a warning.
2957 * - The program text contained something like 'S;' and 'S' is not the
2958 * name of a known structure type. This is both invalid and weird.
2961 * - The program text contained something like 'mediump float;'
2962 * when the programmer probably meant 'precision mediump
2963 * float;' Emit a warning with a description of what they
2964 * probably meant to do.
2966 * Note that if decl_type is NULL and there is a structure involved,
2967 * there must have been some sort of error with the structure. In this
2968 * case we assume that an error was already generated on this line of
2969 * code for the structure. There is no need to generate an additional,
2972 assert(this->type
->specifier
->structure
== NULL
|| decl_type
!= NULL
2975 if (decl_type
== NULL
) {
2976 _mesa_glsl_error(&loc
, state
,
2977 "invalid type `%s' in empty declaration",
2979 } else if (decl_type
->base_type
== GLSL_TYPE_ATOMIC_UINT
) {
2980 /* Empty atomic counter declarations are allowed and useful
2981 * to set the default offset qualifier.
2984 } else if (this->type
->qualifier
.precision
!= ast_precision_none
) {
2985 if (this->type
->specifier
->structure
!= NULL
) {
2986 _mesa_glsl_error(&loc
, state
,
2987 "precision qualifiers can't be applied "
2990 static const char *const precision_names
[] = {
2997 _mesa_glsl_warning(&loc
, state
,
2998 "empty declaration with precision qualifier, "
2999 "to set the default precision, use "
3000 "`precision %s %s;'",
3001 precision_names
[this->type
->qualifier
.precision
],
3004 } else if (this->type
->specifier
->structure
== NULL
) {
3005 _mesa_glsl_warning(&loc
, state
, "empty declaration");
3009 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
3010 const struct glsl_type
*var_type
;
3013 /* FINISHME: Emit a warning if a variable declaration shadows a
3014 * FINISHME: declaration at a higher scope.
3017 if ((decl_type
== NULL
) || decl_type
->is_void()) {
3018 if (type_name
!= NULL
) {
3019 _mesa_glsl_error(& loc
, state
,
3020 "invalid type `%s' in declaration of `%s'",
3021 type_name
, decl
->identifier
);
3023 _mesa_glsl_error(& loc
, state
,
3024 "invalid type in declaration of `%s'",
3030 var_type
= process_array_type(&loc
, decl_type
, decl
->array_specifier
,
3033 var
= new(ctx
) ir_variable(var_type
, decl
->identifier
, ir_var_auto
);
3035 /* The 'varying in' and 'varying out' qualifiers can only be used with
3036 * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
3039 if (this->type
->qualifier
.flags
.q
.varying
) {
3040 if (this->type
->qualifier
.flags
.q
.in
) {
3041 _mesa_glsl_error(& loc
, state
,
3042 "`varying in' qualifier in declaration of "
3043 "`%s' only valid for geometry shaders using "
3044 "ARB_geometry_shader4 or EXT_geometry_shader4",
3046 } else if (this->type
->qualifier
.flags
.q
.out
) {
3047 _mesa_glsl_error(& loc
, state
,
3048 "`varying out' qualifier in declaration of "
3049 "`%s' only valid for geometry shaders using "
3050 "ARB_geometry_shader4 or EXT_geometry_shader4",
3055 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
3057 * "Global variables can only use the qualifiers const,
3058 * attribute, uni form, or varying. Only one may be
3061 * Local variables can only use the qualifier const."
3063 * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
3064 * any extension that adds the 'layout' keyword.
3066 if (!state
->is_version(130, 300)
3067 && !state
->has_explicit_attrib_location()
3068 && !state
->ARB_fragment_coord_conventions_enable
) {
3069 if (this->type
->qualifier
.flags
.q
.out
) {
3070 _mesa_glsl_error(& loc
, state
,
3071 "`out' qualifier in declaration of `%s' "
3072 "only valid for function parameters in %s",
3073 decl
->identifier
, state
->get_version_string());
3075 if (this->type
->qualifier
.flags
.q
.in
) {
3076 _mesa_glsl_error(& loc
, state
,
3077 "`in' qualifier in declaration of `%s' "
3078 "only valid for function parameters in %s",
3079 decl
->identifier
, state
->get_version_string());
3081 /* FINISHME: Test for other invalid qualifiers. */
3084 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
3087 if (this->type
->qualifier
.flags
.q
.invariant
) {
3088 if ((state
->stage
== MESA_SHADER_VERTEX
) &&
3089 var
->data
.mode
!= ir_var_shader_out
) {
3090 _mesa_glsl_error(& loc
, state
,
3091 "`%s' cannot be marked invariant, vertex shader "
3092 "outputs only", var
->name
);
3093 } else if ((state
->stage
== MESA_SHADER_FRAGMENT
) &&
3094 var
->data
.mode
!= ir_var_shader_in
) {
3095 /* FINISHME: Note that this doesn't work for invariant on
3096 * a function signature inval
3098 _mesa_glsl_error(& loc
, state
,
3099 "`%s' cannot be marked invariant, fragment shader "
3100 "inputs only", var
->name
);
3104 if (state
->current_function
!= NULL
) {
3105 const char *mode
= NULL
;
3106 const char *extra
= "";
3108 /* There is no need to check for 'inout' here because the parser will
3109 * only allow that in function parameter lists.
3111 if (this->type
->qualifier
.flags
.q
.attribute
) {
3113 } else if (this->type
->qualifier
.flags
.q
.uniform
) {
3115 } else if (this->type
->qualifier
.flags
.q
.varying
) {
3117 } else if (this->type
->qualifier
.flags
.q
.in
) {
3119 extra
= " or in function parameter list";
3120 } else if (this->type
->qualifier
.flags
.q
.out
) {
3122 extra
= " or in function parameter list";
3126 _mesa_glsl_error(& loc
, state
,
3127 "%s variable `%s' must be declared at "
3129 mode
, var
->name
, extra
);
3131 } else if (var
->data
.mode
== ir_var_shader_in
) {
3132 var
->data
.read_only
= true;
3134 if (state
->stage
== MESA_SHADER_VERTEX
) {
3135 bool error_emitted
= false;
3137 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
3139 * "Vertex shader inputs can only be float, floating-point
3140 * vectors, matrices, signed and unsigned integers and integer
3141 * vectors. Vertex shader inputs can also form arrays of these
3142 * types, but not structures."
3144 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
3146 * "Vertex shader inputs can only be float, floating-point
3147 * vectors, matrices, signed and unsigned integers and integer
3148 * vectors. They cannot be arrays or structures."
3150 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
3152 * "The attribute qualifier can be used only with float,
3153 * floating-point vectors, and matrices. Attribute variables
3154 * cannot be declared as arrays or structures."
3156 * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
3158 * "Vertex shader inputs can only be float, floating-point
3159 * vectors, matrices, signed and unsigned integers and integer
3160 * vectors. Vertex shader inputs cannot be arrays or
3163 const glsl_type
*check_type
= var
->type
;
3164 while (check_type
->is_array())
3165 check_type
= check_type
->element_type();
3167 switch (check_type
->base_type
) {
3168 case GLSL_TYPE_FLOAT
:
3170 case GLSL_TYPE_UINT
:
3172 if (state
->is_version(120, 300))
3176 _mesa_glsl_error(& loc
, state
,
3177 "vertex shader input / attribute cannot have "
3179 var
->type
->is_array() ? "array of " : "",
3181 error_emitted
= true;
3184 if (!error_emitted
&& var
->type
->is_array() &&
3185 !state
->check_version(150, 0, &loc
,
3186 "vertex shader input / attribute "
3187 "cannot have array type")) {
3188 error_emitted
= true;
3190 } else if (state
->stage
== MESA_SHADER_GEOMETRY
) {
3191 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
3193 * Geometry shader input variables get the per-vertex values
3194 * written out by vertex shader output variables of the same
3195 * names. Since a geometry shader operates on a set of
3196 * vertices, each input varying variable (or input block, see
3197 * interface blocks below) needs to be declared as an array.
3199 if (!var
->type
->is_array()) {
3200 _mesa_glsl_error(&loc
, state
,
3201 "geometry shader inputs must be arrays");
3204 handle_geometry_shader_input_decl(state
, loc
, var
);
3208 /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
3209 * so must integer vertex outputs.
3211 * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
3212 * "Fragment shader inputs that are signed or unsigned integers or
3213 * integer vectors must be qualified with the interpolation qualifier
3216 * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
3217 * "Fragment shader inputs that are, or contain, signed or unsigned
3218 * integers or integer vectors must be qualified with the
3219 * interpolation qualifier flat."
3221 * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
3222 * "Vertex shader outputs that are, or contain, signed or unsigned
3223 * integers or integer vectors must be qualified with the
3224 * interpolation qualifier flat."
3226 * Note that prior to GLSL 1.50, this requirement applied to vertex
3227 * outputs rather than fragment inputs. That creates problems in the
3228 * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
3229 * desktop GL shaders. For GLSL ES shaders, we follow the spec and
3230 * apply the restriction to both vertex outputs and fragment inputs.
3232 * Note also that the desktop GLSL specs are missing the text "or
3233 * contain"; this is presumably an oversight, since there is no
3234 * reasonable way to interpolate a fragment shader input that contains
3237 if (state
->is_version(130, 300) &&
3238 var
->type
->contains_integer() &&
3239 var
->data
.interpolation
!= INTERP_QUALIFIER_FLAT
&&
3240 ((state
->stage
== MESA_SHADER_FRAGMENT
&& var
->data
.mode
== ir_var_shader_in
)
3241 || (state
->stage
== MESA_SHADER_VERTEX
&& var
->data
.mode
== ir_var_shader_out
3242 && state
->es_shader
))) {
3243 const char *var_type
= (state
->stage
== MESA_SHADER_VERTEX
) ?
3244 "vertex output" : "fragment input";
3245 _mesa_glsl_error(&loc
, state
, "if a %s is (or contains) "
3246 "an integer, then it must be qualified with 'flat'",
3251 /* Interpolation qualifiers cannot be applied to 'centroid' and
3252 * 'centroid varying'.
3254 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
3255 * "interpolation qualifiers may only precede the qualifiers in,
3256 * centroid in, out, or centroid out in a declaration. They do not apply
3257 * to the deprecated storage qualifiers varying or centroid varying."
3259 * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
3261 if (state
->is_version(130, 0)
3262 && this->type
->qualifier
.has_interpolation()
3263 && this->type
->qualifier
.flags
.q
.varying
) {
3265 const char *i
= this->type
->qualifier
.interpolation_string();
3268 if (this->type
->qualifier
.flags
.q
.centroid
)
3269 s
= "centroid varying";
3273 _mesa_glsl_error(&loc
, state
,
3274 "qualifier '%s' cannot be applied to the "
3275 "deprecated storage qualifier '%s'", i
, s
);
3279 /* Interpolation qualifiers can only apply to vertex shader outputs and
3280 * fragment shader inputs.
3282 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
3283 * "Outputs from a vertex shader (out) and inputs to a fragment
3284 * shader (in) can be further qualified with one or more of these
3285 * interpolation qualifiers"
3287 * From page 31 (page 37 of the PDF) of the GLSL ES 3.00 spec:
3288 * "These interpolation qualifiers may only precede the qualifiers
3289 * in, centroid in, out, or centroid out in a declaration. They do
3290 * not apply to inputs into a vertex shader or outputs from a
3293 if (state
->is_version(130, 300)
3294 && this->type
->qualifier
.has_interpolation()) {
3296 const char *i
= this->type
->qualifier
.interpolation_string();
3299 switch (state
->stage
) {
3300 case MESA_SHADER_VERTEX
:
3301 if (this->type
->qualifier
.flags
.q
.in
) {
3302 _mesa_glsl_error(&loc
, state
,
3303 "qualifier '%s' cannot be applied to vertex "
3304 "shader inputs", i
);
3307 case MESA_SHADER_FRAGMENT
:
3308 if (this->type
->qualifier
.flags
.q
.out
) {
3309 _mesa_glsl_error(&loc
, state
,
3310 "qualifier '%s' cannot be applied to fragment "
3311 "shader outputs", i
);
3320 /* From section 4.3.4 of the GLSL 1.30 spec:
3321 * "It is an error to use centroid in in a vertex shader."
3323 * From section 4.3.4 of the GLSL ES 3.00 spec:
3324 * "It is an error to use centroid in or interpolation qualifiers in
3325 * a vertex shader input."
3327 if (state
->is_version(130, 300)
3328 && this->type
->qualifier
.flags
.q
.centroid
3329 && this->type
->qualifier
.flags
.q
.in
3330 && state
->stage
== MESA_SHADER_VERTEX
) {
3332 _mesa_glsl_error(&loc
, state
,
3333 "'centroid in' cannot be used in a vertex shader");
3336 if (state
->stage
== MESA_SHADER_VERTEX
3337 && this->type
->qualifier
.flags
.q
.sample
3338 && this->type
->qualifier
.flags
.q
.in
) {
3340 _mesa_glsl_error(&loc
, state
,
3341 "'sample in' cannot be used in a vertex shader");
3344 /* Section 4.3.6 of the GLSL 1.30 specification states:
3345 * "It is an error to use centroid out in a fragment shader."
3347 * The GL_ARB_shading_language_420pack extension specification states:
3348 * "It is an error to use auxiliary storage qualifiers or interpolation
3349 * qualifiers on an output in a fragment shader."
3351 if (state
->stage
== MESA_SHADER_FRAGMENT
&&
3352 this->type
->qualifier
.flags
.q
.out
&&
3353 this->type
->qualifier
.has_auxiliary_storage()) {
3354 _mesa_glsl_error(&loc
, state
,
3355 "auxiliary storage qualifiers cannot be used on "
3356 "fragment shader outputs");
3359 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
3361 if (this->type
->qualifier
.precision
!= ast_precision_none
) {
3362 state
->check_precision_qualifiers_allowed(&loc
);
3366 /* Precision qualifiers apply to floating point, integer and sampler
3369 * Section 4.5.2 (Precision Qualifiers) of the GLSL 1.30 spec says:
3370 * "Any floating point or any integer declaration can have the type
3371 * preceded by one of these precision qualifiers [...] Literal
3372 * constants do not have precision qualifiers. Neither do Boolean
3375 * Section 4.5 (Precision and Precision Qualifiers) of the GLSL 1.30
3378 * "Precision qualifiers are added for code portability with OpenGL
3379 * ES, not for functionality. They have the same syntax as in OpenGL
3382 * Section 8 (Built-In Functions) of the GLSL ES 1.00 spec says:
3384 * "uniform lowp sampler2D sampler;
3387 * lowp vec4 col = texture2D (sampler, coord);
3388 * // texture2D returns lowp"
3390 * From this, we infer that GLSL 1.30 (and later) should allow precision
3391 * qualifiers on sampler types just like float and integer types.
3393 if (this->type
->qualifier
.precision
!= ast_precision_none
3394 && !var
->type
->is_float()
3395 && !var
->type
->is_integer()
3396 && !var
->type
->is_record()
3397 && !var
->type
->is_sampler()
3398 && !(var
->type
->is_array()
3399 && (var
->type
->fields
.array
->is_float()
3400 || var
->type
->fields
.array
->is_integer()))) {
3402 _mesa_glsl_error(&loc
, state
,
3403 "precision qualifiers apply only to floating point"
3404 ", integer and sampler types");
3407 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3409 * "[Sampler types] can only be declared as function
3410 * parameters or uniform variables (see Section 4.3.5
3413 if (var_type
->contains_sampler() &&
3414 !this->type
->qualifier
.flags
.q
.uniform
) {
3415 _mesa_glsl_error(&loc
, state
, "samplers must be declared uniform");
3418 /* Process the initializer and add its instructions to a temporary
3419 * list. This list will be added to the instruction stream (below) after
3420 * the declaration is added. This is done because in some cases (such as
3421 * redeclarations) the declaration may not actually be added to the
3422 * instruction stream.
3424 exec_list initializer_instructions
;
3425 ir_variable
*earlier
=
3426 get_variable_being_redeclared(var
, decl
->get_location(), state
,
3427 false /* allow_all_redeclarations */);
3428 if (earlier
!= NULL
) {
3429 if (strncmp(var
->name
, "gl_", 3) == 0 &&
3430 earlier
->data
.how_declared
== ir_var_declared_in_block
) {
3431 _mesa_glsl_error(&loc
, state
,
3432 "`%s' has already been redeclared using "
3433 "gl_PerVertex", var
->name
);
3435 earlier
->data
.how_declared
= ir_var_declared_normally
;
3438 if (decl
->initializer
!= NULL
) {
3439 result
= process_initializer((earlier
== NULL
) ? var
: earlier
,
3441 &initializer_instructions
, state
);
3444 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
3446 * "It is an error to write to a const variable outside of
3447 * its declaration, so they must be initialized when
3450 if (this->type
->qualifier
.flags
.q
.constant
&& decl
->initializer
== NULL
) {
3451 _mesa_glsl_error(& loc
, state
,
3452 "const declaration of `%s' must be initialized",
3456 if (state
->es_shader
) {
3457 const glsl_type
*const t
= (earlier
== NULL
)
3458 ? var
->type
: earlier
->type
;
3460 if (t
->is_unsized_array())
3461 /* Section 10.17 of the GLSL ES 1.00 specification states that
3462 * unsized array declarations have been removed from the language.
3463 * Arrays that are sized using an initializer are still explicitly
3464 * sized. However, GLSL ES 1.00 does not allow array
3465 * initializers. That is only allowed in GLSL ES 3.00.
3467 * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
3469 * "An array type can also be formed without specifying a size
3470 * if the definition includes an initializer:
3472 * float x[] = float[2] (1.0, 2.0); // declares an array of size 2
3473 * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
3478 _mesa_glsl_error(& loc
, state
,
3479 "unsized array declarations are not allowed in "
3483 /* If the declaration is not a redeclaration, there are a few additional
3484 * semantic checks that must be applied. In addition, variable that was
3485 * created for the declaration should be added to the IR stream.
3487 if (earlier
== NULL
) {
3488 validate_identifier(decl
->identifier
, loc
, state
);
3490 /* Add the variable to the symbol table. Note that the initializer's
3491 * IR was already processed earlier (though it hasn't been emitted
3492 * yet), without the variable in scope.
3494 * This differs from most C-like languages, but it follows the GLSL
3495 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
3498 * "Within a declaration, the scope of a name starts immediately
3499 * after the initializer if present or immediately after the name
3500 * being declared if not."
3502 if (!state
->symbols
->add_variable(var
)) {
3503 YYLTYPE loc
= this->get_location();
3504 _mesa_glsl_error(&loc
, state
, "name `%s' already taken in the "
3505 "current scope", decl
->identifier
);
3509 /* Push the variable declaration to the top. It means that all the
3510 * variable declarations will appear in a funny last-to-first order,
3511 * but otherwise we run into trouble if a function is prototyped, a
3512 * global var is decled, then the function is defined with usage of
3513 * the global var. See glslparsertest's CorrectModule.frag.
3515 instructions
->push_head(var
);
3518 instructions
->append_list(&initializer_instructions
);
3522 /* Generally, variable declarations do not have r-values. However,
3523 * one is used for the declaration in
3525 * while (bool b = some_condition()) {
3529 * so we return the rvalue from the last seen declaration here.
3536 ast_parameter_declarator::hir(exec_list
*instructions
,
3537 struct _mesa_glsl_parse_state
*state
)
3540 const struct glsl_type
*type
;
3541 const char *name
= NULL
;
3542 YYLTYPE loc
= this->get_location();
3544 type
= this->type
->glsl_type(& name
, state
);
3548 _mesa_glsl_error(& loc
, state
,
3549 "invalid type `%s' in declaration of `%s'",
3550 name
, this->identifier
);
3552 _mesa_glsl_error(& loc
, state
,
3553 "invalid type in declaration of `%s'",
3557 type
= glsl_type::error_type
;
3560 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
3562 * "Functions that accept no input arguments need not use void in the
3563 * argument list because prototypes (or definitions) are required and
3564 * therefore there is no ambiguity when an empty argument list "( )" is
3565 * declared. The idiom "(void)" as a parameter list is provided for
3568 * Placing this check here prevents a void parameter being set up
3569 * for a function, which avoids tripping up checks for main taking
3570 * parameters and lookups of an unnamed symbol.
3572 if (type
->is_void()) {
3573 if (this->identifier
!= NULL
)
3574 _mesa_glsl_error(& loc
, state
,
3575 "named parameter cannot have type `void'");
3581 if (formal_parameter
&& (this->identifier
== NULL
)) {
3582 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
3586 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
3587 * call already handled the "vec4[..] foo" case.
3589 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
3591 if (!type
->is_error() && type
->is_unsized_array()) {
3592 _mesa_glsl_error(&loc
, state
, "arrays passed as parameters must have "
3594 type
= glsl_type::error_type
;
3598 ir_variable
*var
= new(ctx
)
3599 ir_variable(type
, this->identifier
, ir_var_function_in
);
3601 /* Apply any specified qualifiers to the parameter declaration. Note that
3602 * for function parameters the default mode is 'in'.
3604 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
,
3607 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3609 * "Samplers cannot be treated as l-values; hence cannot be used
3610 * as out or inout function parameters, nor can they be assigned
3613 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
3614 && type
->contains_sampler()) {
3615 _mesa_glsl_error(&loc
, state
, "out and inout parameters cannot contain samplers");
3616 type
= glsl_type::error_type
;
3619 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
3621 * "When calling a function, expressions that do not evaluate to
3622 * l-values cannot be passed to parameters declared as out or inout."
3624 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
3626 * "Other binary or unary expressions, non-dereferenced arrays,
3627 * function names, swizzles with repeated fields, and constants
3628 * cannot be l-values."
3630 * So for GLSL 1.10, passing an array as an out or inout parameter is not
3631 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
3633 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
3635 && !state
->check_version(120, 100, &loc
,
3636 "arrays cannot be out or inout parameters")) {
3637 type
= glsl_type::error_type
;
3640 instructions
->push_tail(var
);
3642 /* Parameter declarations do not have r-values.
3649 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
3651 exec_list
*ir_parameters
,
3652 _mesa_glsl_parse_state
*state
)
3654 ast_parameter_declarator
*void_param
= NULL
;
3657 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
3658 param
->formal_parameter
= formal
;
3659 param
->hir(ir_parameters
, state
);
3667 if ((void_param
!= NULL
) && (count
> 1)) {
3668 YYLTYPE loc
= void_param
->get_location();
3670 _mesa_glsl_error(& loc
, state
,
3671 "`void' parameter must be only parameter");
3677 emit_function(_mesa_glsl_parse_state
*state
, ir_function
*f
)
3679 /* IR invariants disallow function declarations or definitions
3680 * nested within other function definitions. But there is no
3681 * requirement about the relative order of function declarations
3682 * and definitions with respect to one another. So simply insert
3683 * the new ir_function block at the end of the toplevel instruction
3686 state
->toplevel_ir
->push_tail(f
);
3691 ast_function::hir(exec_list
*instructions
,
3692 struct _mesa_glsl_parse_state
*state
)
3695 ir_function
*f
= NULL
;
3696 ir_function_signature
*sig
= NULL
;
3697 exec_list hir_parameters
;
3699 const char *const name
= identifier
;
3701 /* New functions are always added to the top-level IR instruction stream,
3702 * so this instruction list pointer is ignored. See also emit_function
3705 (void) instructions
;
3707 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
3709 * "Function declarations (prototypes) cannot occur inside of functions;
3710 * they must be at global scope, or for the built-in functions, outside
3711 * the global scope."
3713 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
3715 * "User defined functions may only be defined within the global scope."
3717 * Note that this language does not appear in GLSL 1.10.
3719 if ((state
->current_function
!= NULL
) &&
3720 state
->is_version(120, 100)) {
3721 YYLTYPE loc
= this->get_location();
3722 _mesa_glsl_error(&loc
, state
,
3723 "declaration of function `%s' not allowed within "
3724 "function body", name
);
3727 validate_identifier(name
, this->get_location(), state
);
3729 /* Convert the list of function parameters to HIR now so that they can be
3730 * used below to compare this function's signature with previously seen
3731 * signatures for functions with the same name.
3733 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
3735 & hir_parameters
, state
);
3737 const char *return_type_name
;
3738 const glsl_type
*return_type
=
3739 this->return_type
->glsl_type(& return_type_name
, state
);
3742 YYLTYPE loc
= this->get_location();
3743 _mesa_glsl_error(&loc
, state
,
3744 "function `%s' has undeclared return type `%s'",
3745 name
, return_type_name
);
3746 return_type
= glsl_type::error_type
;
3749 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
3750 * "No qualifier is allowed on the return type of a function."
3752 if (this->return_type
->has_qualifiers()) {
3753 YYLTYPE loc
= this->get_location();
3754 _mesa_glsl_error(& loc
, state
,
3755 "function `%s' return type has qualifiers", name
);
3758 /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
3760 * "Arrays are allowed as arguments and as the return type. In both
3761 * cases, the array must be explicitly sized."
3763 if (return_type
->is_unsized_array()) {
3764 YYLTYPE loc
= this->get_location();
3765 _mesa_glsl_error(& loc
, state
,
3766 "function `%s' return type array must be explicitly "
3770 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3772 * "[Sampler types] can only be declared as function parameters
3773 * or uniform variables (see Section 4.3.5 "Uniform")".
3775 if (return_type
->contains_sampler()) {
3776 YYLTYPE loc
= this->get_location();
3777 _mesa_glsl_error(&loc
, state
,
3778 "function `%s' return type can't contain a sampler",
3782 /* Verify that this function's signature either doesn't match a previously
3783 * seen signature for a function with the same name, or, if a match is found,
3784 * that the previously seen signature does not have an associated definition.
3786 f
= state
->symbols
->get_function(name
);
3787 if (f
!= NULL
&& (state
->es_shader
|| f
->has_user_signature())) {
3788 sig
= f
->exact_matching_signature(state
, &hir_parameters
);
3790 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
3791 if (badvar
!= NULL
) {
3792 YYLTYPE loc
= this->get_location();
3794 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
3795 "qualifiers don't match prototype", name
, badvar
);
3798 if (sig
->return_type
!= return_type
) {
3799 YYLTYPE loc
= this->get_location();
3801 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
3802 "match prototype", name
);
3805 if (sig
->is_defined
) {
3806 if (is_definition
) {
3807 YYLTYPE loc
= this->get_location();
3808 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
3810 /* We just encountered a prototype that exactly matches a
3811 * function that's already been defined. This is redundant,
3812 * and we should ignore it.
3819 f
= new(ctx
) ir_function(name
);
3820 if (!state
->symbols
->add_function(f
)) {
3821 /* This function name shadows a non-function use of the same name. */
3822 YYLTYPE loc
= this->get_location();
3824 _mesa_glsl_error(&loc
, state
, "function name `%s' conflicts with "
3825 "non-function", name
);
3829 emit_function(state
, f
);
3832 /* Verify the return type of main() */
3833 if (strcmp(name
, "main") == 0) {
3834 if (! return_type
->is_void()) {
3835 YYLTYPE loc
= this->get_location();
3837 _mesa_glsl_error(& loc
, state
, "main() must return void");
3840 if (!hir_parameters
.is_empty()) {
3841 YYLTYPE loc
= this->get_location();
3843 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
3847 /* Finish storing the information about this new function in its signature.
3850 sig
= new(ctx
) ir_function_signature(return_type
);
3851 f
->add_signature(sig
);
3854 sig
->replace_parameters(&hir_parameters
);
3857 /* Function declarations (prototypes) do not have r-values.
3864 ast_function_definition::hir(exec_list
*instructions
,
3865 struct _mesa_glsl_parse_state
*state
)
3867 prototype
->is_definition
= true;
3868 prototype
->hir(instructions
, state
);
3870 ir_function_signature
*signature
= prototype
->signature
;
3871 if (signature
== NULL
)
3874 assert(state
->current_function
== NULL
);
3875 state
->current_function
= signature
;
3876 state
->found_return
= false;
3878 /* Duplicate parameters declared in the prototype as concrete variables.
3879 * Add these to the symbol table.
3881 state
->symbols
->push_scope();
3882 foreach_list(n
, &signature
->parameters
) {
3883 ir_variable
*const var
= ((ir_instruction
*) n
)->as_variable();
3885 assert(var
!= NULL
);
3887 /* The only way a parameter would "exist" is if two parameters have
3890 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
3891 YYLTYPE loc
= this->get_location();
3893 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
3895 state
->symbols
->add_variable(var
);
3899 /* Convert the body of the function to HIR. */
3900 this->body
->hir(&signature
->body
, state
);
3901 signature
->is_defined
= true;
3903 state
->symbols
->pop_scope();
3905 assert(state
->current_function
== signature
);
3906 state
->current_function
= NULL
;
3908 if (!signature
->return_type
->is_void() && !state
->found_return
) {
3909 YYLTYPE loc
= this->get_location();
3910 _mesa_glsl_error(& loc
, state
, "function `%s' has non-void return type "
3911 "%s, but no return statement",
3912 signature
->function_name(),
3913 signature
->return_type
->name
);
3916 /* Function definitions do not have r-values.
3923 ast_jump_statement::hir(exec_list
*instructions
,
3924 struct _mesa_glsl_parse_state
*state
)
3931 assert(state
->current_function
);
3933 if (opt_return_value
) {
3934 ir_rvalue
*ret
= opt_return_value
->hir(instructions
, state
);
3936 /* The value of the return type can be NULL if the shader says
3937 * 'return foo();' and foo() is a function that returns void.
3939 * NOTE: The GLSL spec doesn't say that this is an error. The type
3940 * of the return value is void. If the return type of the function is
3941 * also void, then this should compile without error. Seriously.
3943 const glsl_type
*const ret_type
=
3944 (ret
== NULL
) ? glsl_type::void_type
: ret
->type
;
3946 /* Implicit conversions are not allowed for return values prior to
3947 * ARB_shading_language_420pack.
3949 if (state
->current_function
->return_type
!= ret_type
) {
3950 YYLTYPE loc
= this->get_location();
3952 if (state
->ARB_shading_language_420pack_enable
) {
3953 if (!apply_implicit_conversion(state
->current_function
->return_type
,
3955 _mesa_glsl_error(& loc
, state
,
3956 "could not implicitly convert return value "
3957 "to %s, in function `%s'",
3958 state
->current_function
->return_type
->name
,
3959 state
->current_function
->function_name());
3962 _mesa_glsl_error(& loc
, state
,
3963 "`return' with wrong type %s, in function `%s' "
3966 state
->current_function
->function_name(),
3967 state
->current_function
->return_type
->name
);
3969 } else if (state
->current_function
->return_type
->base_type
==
3971 YYLTYPE loc
= this->get_location();
3973 /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
3974 * specs add a clarification:
3976 * "A void function can only use return without a return argument, even if
3977 * the return argument has void type. Return statements only accept values:
3980 * void func2() { return func1(); } // illegal return statement"
3982 _mesa_glsl_error(& loc
, state
,
3983 "void functions can only use `return' without a "
3987 inst
= new(ctx
) ir_return(ret
);
3989 if (state
->current_function
->return_type
->base_type
!=
3991 YYLTYPE loc
= this->get_location();
3993 _mesa_glsl_error(& loc
, state
,
3994 "`return' with no value, in function %s returning "
3996 state
->current_function
->function_name());
3998 inst
= new(ctx
) ir_return
;
4001 state
->found_return
= true;
4002 instructions
->push_tail(inst
);
4007 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
4008 YYLTYPE loc
= this->get_location();
4010 _mesa_glsl_error(& loc
, state
,
4011 "`discard' may only appear in a fragment shader");
4013 instructions
->push_tail(new(ctx
) ir_discard
);
4018 if (mode
== ast_continue
&&
4019 state
->loop_nesting_ast
== NULL
) {
4020 YYLTYPE loc
= this->get_location();
4022 _mesa_glsl_error(& loc
, state
,
4023 "continue may only appear in a loop");
4024 } else if (mode
== ast_break
&&
4025 state
->loop_nesting_ast
== NULL
&&
4026 state
->switch_state
.switch_nesting_ast
== NULL
) {
4027 YYLTYPE loc
= this->get_location();
4029 _mesa_glsl_error(& loc
, state
,
4030 "break may only appear in a loop or a switch");
4032 /* For a loop, inline the for loop expression again,
4033 * since we don't know where near the end of
4034 * the loop body the normal copy of it
4035 * is going to be placed.
4037 if (state
->loop_nesting_ast
!= NULL
&&
4038 mode
== ast_continue
&&
4039 state
->loop_nesting_ast
->rest_expression
) {
4040 state
->loop_nesting_ast
->rest_expression
->hir(instructions
,
4044 if (state
->switch_state
.is_switch_innermost
&&
4045 mode
== ast_break
) {
4046 /* Force break out of switch by setting is_break switch state.
4048 ir_variable
*const is_break_var
= state
->switch_state
.is_break_var
;
4049 ir_dereference_variable
*const deref_is_break_var
=
4050 new(ctx
) ir_dereference_variable(is_break_var
);
4051 ir_constant
*const true_val
= new(ctx
) ir_constant(true);
4052 ir_assignment
*const set_break_var
=
4053 new(ctx
) ir_assignment(deref_is_break_var
, true_val
);
4055 instructions
->push_tail(set_break_var
);
4058 ir_loop_jump
*const jump
=
4059 new(ctx
) ir_loop_jump((mode
== ast_break
)
4060 ? ir_loop_jump::jump_break
4061 : ir_loop_jump::jump_continue
);
4062 instructions
->push_tail(jump
);
4069 /* Jump instructions do not have r-values.
4076 ast_selection_statement::hir(exec_list
*instructions
,
4077 struct _mesa_glsl_parse_state
*state
)
4081 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
4083 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
4085 * "Any expression whose type evaluates to a Boolean can be used as the
4086 * conditional expression bool-expression. Vector types are not accepted
4087 * as the expression to if."
4089 * The checks are separated so that higher quality diagnostics can be
4090 * generated for cases where both rules are violated.
4092 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
4093 YYLTYPE loc
= this->condition
->get_location();
4095 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
4099 ir_if
*const stmt
= new(ctx
) ir_if(condition
);
4101 if (then_statement
!= NULL
) {
4102 state
->symbols
->push_scope();
4103 then_statement
->hir(& stmt
->then_instructions
, state
);
4104 state
->symbols
->pop_scope();
4107 if (else_statement
!= NULL
) {
4108 state
->symbols
->push_scope();
4109 else_statement
->hir(& stmt
->else_instructions
, state
);
4110 state
->symbols
->pop_scope();
4113 instructions
->push_tail(stmt
);
4115 /* if-statements do not have r-values.
4122 ast_switch_statement::hir(exec_list
*instructions
,
4123 struct _mesa_glsl_parse_state
*state
)
4127 ir_rvalue
*const test_expression
=
4128 this->test_expression
->hir(instructions
, state
);
4130 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
4132 * "The type of init-expression in a switch statement must be a
4135 if (!test_expression
->type
->is_scalar() ||
4136 !test_expression
->type
->is_integer()) {
4137 YYLTYPE loc
= this->test_expression
->get_location();
4139 _mesa_glsl_error(& loc
,
4141 "switch-statement expression must be scalar "
4145 /* Track the switch-statement nesting in a stack-like manner.
4147 struct glsl_switch_state saved
= state
->switch_state
;
4149 state
->switch_state
.is_switch_innermost
= true;
4150 state
->switch_state
.switch_nesting_ast
= this;
4151 state
->switch_state
.labels_ht
= hash_table_ctor(0, hash_table_pointer_hash
,
4152 hash_table_pointer_compare
);
4153 state
->switch_state
.previous_default
= NULL
;
4155 /* Initalize is_fallthru state to false.
4157 ir_rvalue
*const is_fallthru_val
= new (ctx
) ir_constant(false);
4158 state
->switch_state
.is_fallthru_var
=
4159 new(ctx
) ir_variable(glsl_type::bool_type
,
4160 "switch_is_fallthru_tmp",
4162 instructions
->push_tail(state
->switch_state
.is_fallthru_var
);
4164 ir_dereference_variable
*deref_is_fallthru_var
=
4165 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4166 instructions
->push_tail(new(ctx
) ir_assignment(deref_is_fallthru_var
,
4169 /* Initalize is_break state to false.
4171 ir_rvalue
*const is_break_val
= new (ctx
) ir_constant(false);
4172 state
->switch_state
.is_break_var
= new(ctx
) ir_variable(glsl_type::bool_type
,
4173 "switch_is_break_tmp",
4175 instructions
->push_tail(state
->switch_state
.is_break_var
);
4177 ir_dereference_variable
*deref_is_break_var
=
4178 new(ctx
) ir_dereference_variable(state
->switch_state
.is_break_var
);
4179 instructions
->push_tail(new(ctx
) ir_assignment(deref_is_break_var
,
4182 /* Cache test expression.
4184 test_to_hir(instructions
, state
);
4186 /* Emit code for body of switch stmt.
4188 body
->hir(instructions
, state
);
4190 hash_table_dtor(state
->switch_state
.labels_ht
);
4192 state
->switch_state
= saved
;
4194 /* Switch statements do not have r-values. */
4200 ast_switch_statement::test_to_hir(exec_list
*instructions
,
4201 struct _mesa_glsl_parse_state
*state
)
4205 /* Cache value of test expression. */
4206 ir_rvalue
*const test_val
=
4207 test_expression
->hir(instructions
,
4210 state
->switch_state
.test_var
= new(ctx
) ir_variable(test_val
->type
,
4213 ir_dereference_variable
*deref_test_var
=
4214 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
4216 instructions
->push_tail(state
->switch_state
.test_var
);
4217 instructions
->push_tail(new(ctx
) ir_assignment(deref_test_var
, test_val
));
4222 ast_switch_body::hir(exec_list
*instructions
,
4223 struct _mesa_glsl_parse_state
*state
)
4226 stmts
->hir(instructions
, state
);
4228 /* Switch bodies do not have r-values. */
4233 ast_case_statement_list::hir(exec_list
*instructions
,
4234 struct _mesa_glsl_parse_state
*state
)
4236 foreach_list_typed (ast_case_statement
, case_stmt
, link
, & this->cases
)
4237 case_stmt
->hir(instructions
, state
);
4239 /* Case statements do not have r-values. */
4244 ast_case_statement::hir(exec_list
*instructions
,
4245 struct _mesa_glsl_parse_state
*state
)
4247 labels
->hir(instructions
, state
);
4249 /* Conditionally set fallthru state based on break state. */
4250 ir_constant
*const false_val
= new(state
) ir_constant(false);
4251 ir_dereference_variable
*const deref_is_fallthru_var
=
4252 new(state
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4253 ir_dereference_variable
*const deref_is_break_var
=
4254 new(state
) ir_dereference_variable(state
->switch_state
.is_break_var
);
4255 ir_assignment
*const reset_fallthru_on_break
=
4256 new(state
) ir_assignment(deref_is_fallthru_var
,
4258 deref_is_break_var
);
4259 instructions
->push_tail(reset_fallthru_on_break
);
4261 /* Guard case statements depending on fallthru state. */
4262 ir_dereference_variable
*const deref_fallthru_guard
=
4263 new(state
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4264 ir_if
*const test_fallthru
= new(state
) ir_if(deref_fallthru_guard
);
4266 foreach_list_typed (ast_node
, stmt
, link
, & this->stmts
)
4267 stmt
->hir(& test_fallthru
->then_instructions
, state
);
4269 instructions
->push_tail(test_fallthru
);
4271 /* Case statements do not have r-values. */
4277 ast_case_label_list::hir(exec_list
*instructions
,
4278 struct _mesa_glsl_parse_state
*state
)
4280 foreach_list_typed (ast_case_label
, label
, link
, & this->labels
)
4281 label
->hir(instructions
, state
);
4283 /* Case labels do not have r-values. */
4288 ast_case_label::hir(exec_list
*instructions
,
4289 struct _mesa_glsl_parse_state
*state
)
4293 ir_dereference_variable
*deref_fallthru_var
=
4294 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4296 ir_rvalue
*const true_val
= new(ctx
) ir_constant(true);
4298 /* If not default case, ... */
4299 if (this->test_value
!= NULL
) {
4300 /* Conditionally set fallthru state based on
4301 * comparison of cached test expression value to case label.
4303 ir_rvalue
*const label_rval
= this->test_value
->hir(instructions
, state
);
4304 ir_constant
*label_const
= label_rval
->constant_expression_value();
4307 YYLTYPE loc
= this->test_value
->get_location();
4309 _mesa_glsl_error(& loc
, state
,
4310 "switch statement case label must be a "
4311 "constant expression");
4313 /* Stuff a dummy value in to allow processing to continue. */
4314 label_const
= new(ctx
) ir_constant(0);
4316 ast_expression
*previous_label
= (ast_expression
*)
4317 hash_table_find(state
->switch_state
.labels_ht
,
4318 (void *)(uintptr_t)label_const
->value
.u
[0]);
4320 if (previous_label
) {
4321 YYLTYPE loc
= this->test_value
->get_location();
4322 _mesa_glsl_error(& loc
, state
,
4323 "duplicate case value");
4325 loc
= previous_label
->get_location();
4326 _mesa_glsl_error(& loc
, state
,
4327 "this is the previous case label");
4329 hash_table_insert(state
->switch_state
.labels_ht
,
4331 (void *)(uintptr_t)label_const
->value
.u
[0]);
4335 ir_dereference_variable
*deref_test_var
=
4336 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
4338 ir_rvalue
*const test_cond
= new(ctx
) ir_expression(ir_binop_all_equal
,
4342 ir_assignment
*set_fallthru_on_test
=
4343 new(ctx
) ir_assignment(deref_fallthru_var
,
4347 instructions
->push_tail(set_fallthru_on_test
);
4348 } else { /* default case */
4349 if (state
->switch_state
.previous_default
) {
4350 YYLTYPE loc
= this->get_location();
4351 _mesa_glsl_error(& loc
, state
,
4352 "multiple default labels in one switch");
4354 loc
= state
->switch_state
.previous_default
->get_location();
4355 _mesa_glsl_error(& loc
, state
,
4356 "this is the first default label");
4358 state
->switch_state
.previous_default
= this;
4360 /* Set falltrhu state. */
4361 ir_assignment
*set_fallthru
=
4362 new(ctx
) ir_assignment(deref_fallthru_var
, true_val
);
4364 instructions
->push_tail(set_fallthru
);
4367 /* Case statements do not have r-values. */
4372 ast_iteration_statement::condition_to_hir(ir_loop
*stmt
,
4373 struct _mesa_glsl_parse_state
*state
)
4377 if (condition
!= NULL
) {
4378 ir_rvalue
*const cond
=
4379 condition
->hir(& stmt
->body_instructions
, state
);
4382 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
4383 YYLTYPE loc
= condition
->get_location();
4385 _mesa_glsl_error(& loc
, state
,
4386 "loop condition must be scalar boolean");
4388 /* As the first code in the loop body, generate a block that looks
4389 * like 'if (!condition) break;' as the loop termination condition.
4391 ir_rvalue
*const not_cond
=
4392 new(ctx
) ir_expression(ir_unop_logic_not
, cond
);
4394 ir_if
*const if_stmt
= new(ctx
) ir_if(not_cond
);
4396 ir_jump
*const break_stmt
=
4397 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
4399 if_stmt
->then_instructions
.push_tail(break_stmt
);
4400 stmt
->body_instructions
.push_tail(if_stmt
);
4407 ast_iteration_statement::hir(exec_list
*instructions
,
4408 struct _mesa_glsl_parse_state
*state
)
4412 /* For-loops and while-loops start a new scope, but do-while loops do not.
4414 if (mode
!= ast_do_while
)
4415 state
->symbols
->push_scope();
4417 if (init_statement
!= NULL
)
4418 init_statement
->hir(instructions
, state
);
4420 ir_loop
*const stmt
= new(ctx
) ir_loop();
4421 instructions
->push_tail(stmt
);
4423 /* Track the current loop nesting. */
4424 ast_iteration_statement
*nesting_ast
= state
->loop_nesting_ast
;
4426 state
->loop_nesting_ast
= this;
4428 /* Likewise, indicate that following code is closest to a loop,
4429 * NOT closest to a switch.
4431 bool saved_is_switch_innermost
= state
->switch_state
.is_switch_innermost
;
4432 state
->switch_state
.is_switch_innermost
= false;
4434 if (mode
!= ast_do_while
)
4435 condition_to_hir(stmt
, state
);
4438 body
->hir(& stmt
->body_instructions
, state
);
4440 if (rest_expression
!= NULL
)
4441 rest_expression
->hir(& stmt
->body_instructions
, state
);
4443 if (mode
== ast_do_while
)
4444 condition_to_hir(stmt
, state
);
4446 if (mode
!= ast_do_while
)
4447 state
->symbols
->pop_scope();
4449 /* Restore previous nesting before returning. */
4450 state
->loop_nesting_ast
= nesting_ast
;
4451 state
->switch_state
.is_switch_innermost
= saved_is_switch_innermost
;
4453 /* Loops do not have r-values.
4460 * Determine if the given type is valid for establishing a default precision
4463 * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
4465 * "The precision statement
4467 * precision precision-qualifier type;
4469 * can be used to establish a default precision qualifier. The type field
4470 * can be either int or float or any of the sampler types, and the
4471 * precision-qualifier can be lowp, mediump, or highp."
4473 * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
4474 * qualifiers on sampler types, but this seems like an oversight (since the
4475 * intention of including these in GLSL 1.30 is to allow compatibility with ES
4476 * shaders). So we allow int, float, and all sampler types regardless of GLSL
4480 is_valid_default_precision_type(const struct glsl_type
*const type
)
4485 switch (type
->base_type
) {
4487 case GLSL_TYPE_FLOAT
:
4488 /* "int" and "float" are valid, but vectors and matrices are not. */
4489 return type
->vector_elements
== 1 && type
->matrix_columns
== 1;
4490 case GLSL_TYPE_SAMPLER
:
4499 ast_type_specifier::hir(exec_list
*instructions
,
4500 struct _mesa_glsl_parse_state
*state
)
4502 if (this->default_precision
== ast_precision_none
&& this->structure
== NULL
)
4505 YYLTYPE loc
= this->get_location();
4507 /* If this is a precision statement, check that the type to which it is
4508 * applied is either float or int.
4510 * From section 4.5.3 of the GLSL 1.30 spec:
4511 * "The precision statement
4512 * precision precision-qualifier type;
4513 * can be used to establish a default precision qualifier. The type
4514 * field can be either int or float [...]. Any other types or
4515 * qualifiers will result in an error.
4517 if (this->default_precision
!= ast_precision_none
) {
4518 if (!state
->check_precision_qualifiers_allowed(&loc
))
4521 if (this->structure
!= NULL
) {
4522 _mesa_glsl_error(&loc
, state
,
4523 "precision qualifiers do not apply to structures");
4527 if (this->array_specifier
!= NULL
) {
4528 _mesa_glsl_error(&loc
, state
,
4529 "default precision statements do not apply to "
4534 const struct glsl_type
*const type
=
4535 state
->symbols
->get_type(this->type_name
);
4536 if (!is_valid_default_precision_type(type
)) {
4537 _mesa_glsl_error(&loc
, state
,
4538 "default precision statements apply only to "
4539 "float, int, and sampler types");
4543 if (type
->base_type
== GLSL_TYPE_FLOAT
4545 && state
->stage
== MESA_SHADER_FRAGMENT
) {
4546 /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
4549 * "The fragment language has no default precision qualifier for
4550 * floating point types."
4552 * As a result, we have to track whether or not default precision has
4553 * been specified for float in GLSL ES fragment shaders.
4555 * Earlier in that same section, the spec says:
4557 * "Non-precision qualified declarations will use the precision
4558 * qualifier specified in the most recent precision statement
4559 * that is still in scope. The precision statement has the same
4560 * scoping rules as variable declarations. If it is declared
4561 * inside a compound statement, its effect stops at the end of
4562 * the innermost statement it was declared in. Precision
4563 * statements in nested scopes override precision statements in
4564 * outer scopes. Multiple precision statements for the same basic
4565 * type can appear inside the same scope, with later statements
4566 * overriding earlier statements within that scope."
4568 * Default precision specifications follow the same scope rules as
4569 * variables. So, we can track the state of the default float
4570 * precision in the symbol table, and the rules will just work. This
4571 * is a slight abuse of the symbol table, but it has the semantics
4574 ir_variable
*const junk
=
4575 new(state
) ir_variable(type
, "#default precision",
4578 state
->symbols
->add_variable(junk
);
4581 /* FINISHME: Translate precision statements into IR. */
4585 /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
4586 * process_record_constructor() can do type-checking on C-style initializer
4587 * expressions of structs, but ast_struct_specifier should only be translated
4588 * to HIR if it is declaring the type of a structure.
4590 * The ->is_declaration field is false for initializers of variables
4591 * declared separately from the struct's type definition.
4593 * struct S { ... }; (is_declaration = true)
4594 * struct T { ... } t = { ... }; (is_declaration = true)
4595 * S s = { ... }; (is_declaration = false)
4597 if (this->structure
!= NULL
&& this->structure
->is_declaration
)
4598 return this->structure
->hir(instructions
, state
);
4605 * Process a structure or interface block tree into an array of structure fields
4607 * After parsing, where there are some syntax differnces, structures and
4608 * interface blocks are almost identical. They are similar enough that the
4609 * AST for each can be processed the same way into a set of
4610 * \c glsl_struct_field to describe the members.
4612 * If we're processing an interface block, var_mode should be the type of the
4613 * interface block (ir_var_shader_in, ir_var_shader_out, or ir_var_uniform).
4614 * If we're processing a structure, var_mode should be ir_var_auto.
4617 * The number of fields processed. A pointer to the array structure fields is
4618 * stored in \c *fields_ret.
4621 ast_process_structure_or_interface_block(exec_list
*instructions
,
4622 struct _mesa_glsl_parse_state
*state
,
4623 exec_list
*declarations
,
4625 glsl_struct_field
**fields_ret
,
4627 bool block_row_major
,
4628 bool allow_reserved_names
,
4629 ir_variable_mode var_mode
)
4631 unsigned decl_count
= 0;
4633 /* Make an initial pass over the list of fields to determine how
4634 * many there are. Each element in this list is an ast_declarator_list.
4635 * This means that we actually need to count the number of elements in the
4636 * 'declarations' list in each of the elements.
4638 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
4639 foreach_list_const (decl_ptr
, & decl_list
->declarations
) {
4644 /* Allocate storage for the fields and process the field
4645 * declarations. As the declarations are processed, try to also convert
4646 * the types to HIR. This ensures that structure definitions embedded in
4647 * other structure definitions or in interface blocks are processed.
4649 glsl_struct_field
*const fields
= ralloc_array(state
, glsl_struct_field
,
4653 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
4654 const char *type_name
;
4656 decl_list
->type
->specifier
->hir(instructions
, state
);
4658 /* Section 10.9 of the GLSL ES 1.00 specification states that
4659 * embedded structure definitions have been removed from the language.
4661 if (state
->es_shader
&& decl_list
->type
->specifier
->structure
!= NULL
) {
4662 _mesa_glsl_error(&loc
, state
, "embedded structure definitions are "
4663 "not allowed in GLSL ES 1.00");
4666 const glsl_type
*decl_type
=
4667 decl_list
->type
->glsl_type(& type_name
, state
);
4669 foreach_list_typed (ast_declaration
, decl
, link
,
4670 &decl_list
->declarations
) {
4671 if (!allow_reserved_names
)
4672 validate_identifier(decl
->identifier
, loc
, state
);
4674 /* From the GL_ARB_uniform_buffer_object spec:
4676 * "Sampler types are not allowed inside of uniform
4677 * blocks. All other types, arrays, and structures
4678 * allowed for uniforms are allowed within a uniform
4681 * It should be impossible for decl_type to be NULL here. Cases that
4682 * might naturally lead to decl_type being NULL, especially for the
4683 * is_interface case, will have resulted in compilation having
4684 * already halted due to a syntax error.
4686 const struct glsl_type
*field_type
=
4687 decl_type
!= NULL
? decl_type
: glsl_type::error_type
;
4689 if (is_interface
&& field_type
->contains_sampler()) {
4690 YYLTYPE loc
= decl_list
->get_location();
4691 _mesa_glsl_error(&loc
, state
,
4692 "uniform in non-default uniform block contains sampler");
4695 if (field_type
->contains_atomic()) {
4696 /* FINISHME: Add a spec quotation here once updated spec
4697 * FINISHME: language is available. See Khronos bug #10903
4698 * FINISHME: on whether atomic counters are allowed in
4699 * FINISHME: structures.
4701 YYLTYPE loc
= decl_list
->get_location();
4702 _mesa_glsl_error(&loc
, state
, "atomic counter in structure or "
4706 const struct ast_type_qualifier
*const qual
=
4707 & decl_list
->type
->qualifier
;
4708 if (qual
->flags
.q
.std140
||
4709 qual
->flags
.q
.packed
||
4710 qual
->flags
.q
.shared
) {
4711 _mesa_glsl_error(&loc
, state
,
4712 "uniform block layout qualifiers std140, packed, and "
4713 "shared can only be applied to uniform blocks, not "
4717 field_type
= process_array_type(&loc
, decl_type
,
4718 decl
->array_specifier
, state
);
4719 fields
[i
].type
= field_type
;
4720 fields
[i
].name
= decl
->identifier
;
4721 fields
[i
].location
= -1;
4722 fields
[i
].interpolation
=
4723 interpret_interpolation_qualifier(qual
, var_mode
, state
, &loc
);
4724 fields
[i
].centroid
= qual
->flags
.q
.centroid
? 1 : 0;
4725 fields
[i
].sample
= qual
->flags
.q
.sample
? 1 : 0;
4727 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
4728 if (!qual
->flags
.q
.uniform
) {
4729 _mesa_glsl_error(&loc
, state
,
4730 "row_major and column_major can only be "
4731 "applied to uniform interface blocks");
4733 validate_matrix_layout_for_type(state
, &loc
, field_type
, NULL
);
4736 if (qual
->flags
.q
.uniform
&& qual
->has_interpolation()) {
4737 _mesa_glsl_error(&loc
, state
,
4738 "interpolation qualifiers cannot be used "
4739 "with uniform interface blocks");
4742 if (field_type
->is_matrix() ||
4743 (field_type
->is_array() && field_type
->fields
.array
->is_matrix())) {
4744 fields
[i
].row_major
= block_row_major
;
4745 if (qual
->flags
.q
.row_major
)
4746 fields
[i
].row_major
= true;
4747 else if (qual
->flags
.q
.column_major
)
4748 fields
[i
].row_major
= false;
4755 assert(i
== decl_count
);
4757 *fields_ret
= fields
;
4763 ast_struct_specifier::hir(exec_list
*instructions
,
4764 struct _mesa_glsl_parse_state
*state
)
4766 YYLTYPE loc
= this->get_location();
4768 /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
4770 * "Anonymous structures are not supported; so embedded structures must
4771 * have a declarator. A name given to an embedded struct is scoped at
4772 * the same level as the struct it is embedded in."
4774 * The same section of the GLSL 1.20 spec says:
4776 * "Anonymous structures are not supported. Embedded structures are not
4779 * struct S { float f; };
4781 * S; // Error: anonymous structures disallowed
4782 * struct { ... }; // Error: embedded structures disallowed
4783 * S s; // Okay: nested structures with name are allowed
4786 * The GLSL ES 1.00 and 3.00 specs have similar langauge and examples. So,
4787 * we allow embedded structures in 1.10 only.
4789 if (state
->language_version
!= 110 && state
->struct_specifier_depth
!= 0)
4790 _mesa_glsl_error(&loc
, state
,
4791 "embedded structure declartions are not allowed");
4793 state
->struct_specifier_depth
++;
4795 glsl_struct_field
*fields
;
4796 unsigned decl_count
=
4797 ast_process_structure_or_interface_block(instructions
,
4799 &this->declarations
,
4804 false /* allow_reserved_names */,
4807 validate_identifier(this->name
, loc
, state
);
4809 const glsl_type
*t
=
4810 glsl_type::get_record_instance(fields
, decl_count
, this->name
);
4812 if (!state
->symbols
->add_type(name
, t
)) {
4813 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
4815 const glsl_type
**s
= reralloc(state
, state
->user_structures
,
4817 state
->num_user_structures
+ 1);
4819 s
[state
->num_user_structures
] = t
;
4820 state
->user_structures
= s
;
4821 state
->num_user_structures
++;
4825 state
->struct_specifier_depth
--;
4827 /* Structure type definitions do not have r-values.
4834 * Visitor class which detects whether a given interface block has been used.
4836 class interface_block_usage_visitor
: public ir_hierarchical_visitor
4839 interface_block_usage_visitor(ir_variable_mode mode
, const glsl_type
*block
)
4840 : mode(mode
), block(block
), found(false)
4844 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
4846 if (ir
->var
->data
.mode
== mode
&& ir
->var
->get_interface_type() == block
) {
4850 return visit_continue
;
4853 bool usage_found() const
4859 ir_variable_mode mode
;
4860 const glsl_type
*block
;
4866 ast_interface_block::hir(exec_list
*instructions
,
4867 struct _mesa_glsl_parse_state
*state
)
4869 YYLTYPE loc
= this->get_location();
4871 /* The ast_interface_block has a list of ast_declarator_lists. We
4872 * need to turn those into ir_variables with an association
4873 * with this uniform block.
4875 enum glsl_interface_packing packing
;
4876 if (this->layout
.flags
.q
.shared
) {
4877 packing
= GLSL_INTERFACE_PACKING_SHARED
;
4878 } else if (this->layout
.flags
.q
.packed
) {
4879 packing
= GLSL_INTERFACE_PACKING_PACKED
;
4881 /* The default layout is std140.
4883 packing
= GLSL_INTERFACE_PACKING_STD140
;
4886 ir_variable_mode var_mode
;
4887 const char *iface_type_name
;
4888 if (this->layout
.flags
.q
.in
) {
4889 var_mode
= ir_var_shader_in
;
4890 iface_type_name
= "in";
4891 } else if (this->layout
.flags
.q
.out
) {
4892 var_mode
= ir_var_shader_out
;
4893 iface_type_name
= "out";
4894 } else if (this->layout
.flags
.q
.uniform
) {
4895 var_mode
= ir_var_uniform
;
4896 iface_type_name
= "uniform";
4898 var_mode
= ir_var_auto
;
4899 iface_type_name
= "UNKNOWN";
4900 assert(!"interface block layout qualifier not found!");
4903 bool redeclaring_per_vertex
= strcmp(this->block_name
, "gl_PerVertex") == 0;
4904 bool block_row_major
= this->layout
.flags
.q
.row_major
;
4905 exec_list declared_variables
;
4906 glsl_struct_field
*fields
;
4907 unsigned int num_variables
=
4908 ast_process_structure_or_interface_block(&declared_variables
,
4910 &this->declarations
,
4915 redeclaring_per_vertex
,
4918 if (!redeclaring_per_vertex
)
4919 validate_identifier(this->block_name
, loc
, state
);
4921 const glsl_type
*earlier_per_vertex
= NULL
;
4922 if (redeclaring_per_vertex
) {
4923 /* Find the previous declaration of gl_PerVertex. If we're redeclaring
4924 * the named interface block gl_in, we can find it by looking at the
4925 * previous declaration of gl_in. Otherwise we can find it by looking
4926 * at the previous decalartion of any of the built-in outputs,
4929 * Also check that the instance name and array-ness of the redeclaration
4933 case ir_var_shader_in
:
4934 if (ir_variable
*earlier_gl_in
=
4935 state
->symbols
->get_variable("gl_in")) {
4936 earlier_per_vertex
= earlier_gl_in
->get_interface_type();
4938 _mesa_glsl_error(&loc
, state
,
4939 "redeclaration of gl_PerVertex input not allowed "
4941 _mesa_shader_stage_to_string(state
->stage
));
4943 if (this->instance_name
== NULL
||
4944 strcmp(this->instance_name
, "gl_in") != 0 || this->array_specifier
== NULL
) {
4945 _mesa_glsl_error(&loc
, state
,
4946 "gl_PerVertex input must be redeclared as "
4950 case ir_var_shader_out
:
4951 if (ir_variable
*earlier_gl_Position
=
4952 state
->symbols
->get_variable("gl_Position")) {
4953 earlier_per_vertex
= earlier_gl_Position
->get_interface_type();
4955 _mesa_glsl_error(&loc
, state
,
4956 "redeclaration of gl_PerVertex output not "
4957 "allowed in the %s shader",
4958 _mesa_shader_stage_to_string(state
->stage
));
4960 if (this->instance_name
!= NULL
) {
4961 _mesa_glsl_error(&loc
, state
,
4962 "gl_PerVertex input may not be redeclared with "
4963 "an instance name");
4967 _mesa_glsl_error(&loc
, state
,
4968 "gl_PerVertex must be declared as an input or an "
4973 if (earlier_per_vertex
== NULL
) {
4974 /* An error has already been reported. Bail out to avoid null
4975 * dereferences later in this function.
4980 /* Copy locations from the old gl_PerVertex interface block. */
4981 for (unsigned i
= 0; i
< num_variables
; i
++) {
4982 int j
= earlier_per_vertex
->field_index(fields
[i
].name
);
4984 _mesa_glsl_error(&loc
, state
,
4985 "redeclaration of gl_PerVertex must be a subset "
4986 "of the built-in members of gl_PerVertex");
4988 fields
[i
].location
=
4989 earlier_per_vertex
->fields
.structure
[j
].location
;
4990 fields
[i
].interpolation
=
4991 earlier_per_vertex
->fields
.structure
[j
].interpolation
;
4992 fields
[i
].centroid
=
4993 earlier_per_vertex
->fields
.structure
[j
].centroid
;
4995 earlier_per_vertex
->fields
.structure
[j
].sample
;
4999 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10
5002 * If a built-in interface block is redeclared, it must appear in
5003 * the shader before any use of any member included in the built-in
5004 * declaration, or a compilation error will result.
5006 * This appears to be a clarification to the behaviour established for
5007 * gl_PerVertex by GLSL 1.50, therefore we implement this behaviour
5008 * regardless of GLSL version.
5010 interface_block_usage_visitor
v(var_mode
, earlier_per_vertex
);
5011 v
.run(instructions
);
5012 if (v
.usage_found()) {
5013 _mesa_glsl_error(&loc
, state
,
5014 "redeclaration of a built-in interface block must "
5015 "appear before any use of any member of the "
5020 const glsl_type
*block_type
=
5021 glsl_type::get_interface_instance(fields
,
5026 if (!state
->symbols
->add_interface(block_type
->name
, block_type
, var_mode
)) {
5027 YYLTYPE loc
= this->get_location();
5028 _mesa_glsl_error(&loc
, state
, "interface block `%s' with type `%s' "
5029 "already taken in the current scope",
5030 this->block_name
, iface_type_name
);
5033 /* Since interface blocks cannot contain statements, it should be
5034 * impossible for the block to generate any instructions.
5036 assert(declared_variables
.is_empty());
5038 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
5040 * Geometry shader input variables get the per-vertex values written
5041 * out by vertex shader output variables of the same names. Since a
5042 * geometry shader operates on a set of vertices, each input varying
5043 * variable (or input block, see interface blocks below) needs to be
5044 * declared as an array.
5046 if (state
->stage
== MESA_SHADER_GEOMETRY
&& this->array_specifier
== NULL
&&
5047 var_mode
== ir_var_shader_in
) {
5048 _mesa_glsl_error(&loc
, state
, "geometry shader inputs must be arrays");
5051 /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
5054 * "If an instance name (instance-name) is used, then it puts all the
5055 * members inside a scope within its own name space, accessed with the
5056 * field selector ( . ) operator (analogously to structures)."
5058 if (this->instance_name
) {
5059 if (redeclaring_per_vertex
) {
5060 /* When a built-in in an unnamed interface block is redeclared,
5061 * get_variable_being_redeclared() calls
5062 * check_builtin_array_max_size() to make sure that built-in array
5063 * variables aren't redeclared to illegal sizes. But we're looking
5064 * at a redeclaration of a named built-in interface block. So we
5065 * have to manually call check_builtin_array_max_size() for all parts
5066 * of the interface that are arrays.
5068 for (unsigned i
= 0; i
< num_variables
; i
++) {
5069 if (fields
[i
].type
->is_array()) {
5070 const unsigned size
= fields
[i
].type
->array_size();
5071 check_builtin_array_max_size(fields
[i
].name
, size
, loc
, state
);
5075 validate_identifier(this->instance_name
, loc
, state
);
5080 if (this->array_specifier
!= NULL
) {
5081 /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
5083 * For uniform blocks declared an array, each individual array
5084 * element corresponds to a separate buffer object backing one
5085 * instance of the block. As the array size indicates the number
5086 * of buffer objects needed, uniform block array declarations
5087 * must specify an array size.
5089 * And a few paragraphs later:
5091 * Geometry shader input blocks must be declared as arrays and
5092 * follow the array declaration and linking rules for all
5093 * geometry shader inputs. All other input and output block
5094 * arrays must specify an array size.
5096 * The upshot of this is that the only circumstance where an
5097 * interface array size *doesn't* need to be specified is on a
5098 * geometry shader input.
5100 if (this->array_specifier
->is_unsized_array
&&
5101 (state
->stage
!= MESA_SHADER_GEOMETRY
|| !this->layout
.flags
.q
.in
)) {
5102 _mesa_glsl_error(&loc
, state
,
5103 "only geometry shader inputs may be unsized "
5104 "instance block arrays");
5108 const glsl_type
*block_array_type
=
5109 process_array_type(&loc
, block_type
, this->array_specifier
, state
);
5111 var
= new(state
) ir_variable(block_array_type
,
5112 this->instance_name
,
5115 var
= new(state
) ir_variable(block_type
,
5116 this->instance_name
,
5120 if (state
->stage
== MESA_SHADER_GEOMETRY
&& var_mode
== ir_var_shader_in
)
5121 handle_geometry_shader_input_decl(state
, loc
, var
);
5123 if (ir_variable
*earlier
=
5124 state
->symbols
->get_variable(this->instance_name
)) {
5125 if (!redeclaring_per_vertex
) {
5126 _mesa_glsl_error(&loc
, state
, "`%s' redeclared",
5127 this->instance_name
);
5129 earlier
->data
.how_declared
= ir_var_declared_normally
;
5130 earlier
->type
= var
->type
;
5131 earlier
->reinit_interface_type(block_type
);
5134 state
->symbols
->add_variable(var
);
5135 instructions
->push_tail(var
);
5138 /* In order to have an array size, the block must also be declared with
5141 assert(this->array_specifier
== NULL
);
5143 for (unsigned i
= 0; i
< num_variables
; i
++) {
5145 new(state
) ir_variable(fields
[i
].type
,
5146 ralloc_strdup(state
, fields
[i
].name
),
5148 var
->data
.interpolation
= fields
[i
].interpolation
;
5149 var
->data
.centroid
= fields
[i
].centroid
;
5150 var
->data
.sample
= fields
[i
].sample
;
5151 var
->init_interface_type(block_type
);
5153 if (redeclaring_per_vertex
) {
5154 ir_variable
*earlier
=
5155 get_variable_being_redeclared(var
, loc
, state
,
5156 true /* allow_all_redeclarations */);
5157 if (strncmp(var
->name
, "gl_", 3) != 0 || earlier
== NULL
) {
5158 _mesa_glsl_error(&loc
, state
,
5159 "redeclaration of gl_PerVertex can only "
5160 "include built-in variables");
5161 } else if (earlier
->data
.how_declared
== ir_var_declared_normally
) {
5162 _mesa_glsl_error(&loc
, state
,
5163 "`%s' has already been redeclared", var
->name
);
5165 earlier
->data
.how_declared
= ir_var_declared_in_block
;
5166 earlier
->reinit_interface_type(block_type
);
5171 if (state
->symbols
->get_variable(var
->name
) != NULL
)
5172 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
5174 /* Propagate the "binding" keyword into this UBO's fields;
5175 * the UBO declaration itself doesn't get an ir_variable unless it
5176 * has an instance name. This is ugly.
5178 var
->data
.explicit_binding
= this->layout
.flags
.q
.explicit_binding
;
5179 var
->data
.binding
= this->layout
.binding
;
5181 state
->symbols
->add_variable(var
);
5182 instructions
->push_tail(var
);
5185 if (redeclaring_per_vertex
&& block_type
!= earlier_per_vertex
) {
5186 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec:
5188 * It is also a compilation error ... to redeclare a built-in
5189 * block and then use a member from that built-in block that was
5190 * not included in the redeclaration.
5192 * This appears to be a clarification to the behaviour established
5193 * for gl_PerVertex by GLSL 1.50, therefore we implement this
5194 * behaviour regardless of GLSL version.
5196 * To prevent the shader from using a member that was not included in
5197 * the redeclaration, we disable any ir_variables that are still
5198 * associated with the old declaration of gl_PerVertex (since we've
5199 * already updated all of the variables contained in the new
5200 * gl_PerVertex to point to it).
5202 * As a side effect this will prevent
5203 * validate_intrastage_interface_blocks() from getting confused and
5204 * thinking there are conflicting definitions of gl_PerVertex in the
5207 foreach_list_safe(node
, instructions
) {
5208 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
5210 var
->get_interface_type() == earlier_per_vertex
&&
5211 var
->data
.mode
== var_mode
) {
5212 if (var
->data
.how_declared
== ir_var_declared_normally
) {
5213 _mesa_glsl_error(&loc
, state
,
5214 "redeclaration of gl_PerVertex cannot "
5215 "follow a redeclaration of `%s'",
5218 state
->symbols
->disable_variable(var
->name
);
5230 ast_gs_input_layout::hir(exec_list
*instructions
,
5231 struct _mesa_glsl_parse_state
*state
)
5233 YYLTYPE loc
= this->get_location();
5235 /* If any geometry input layout declaration preceded this one, make sure it
5236 * was consistent with this one.
5238 if (state
->gs_input_prim_type_specified
&&
5239 state
->gs_input_prim_type
!= this->prim_type
) {
5240 _mesa_glsl_error(&loc
, state
,
5241 "geometry shader input layout does not match"
5242 " previous declaration");
5246 /* If any shader inputs occurred before this declaration and specified an
5247 * array size, make sure the size they specified is consistent with the
5250 unsigned num_vertices
= vertices_per_prim(this->prim_type
);
5251 if (state
->gs_input_size
!= 0 && state
->gs_input_size
!= num_vertices
) {
5252 _mesa_glsl_error(&loc
, state
,
5253 "this geometry shader input layout implies %u vertices"
5254 " per primitive, but a previous input is declared"
5255 " with size %u", num_vertices
, state
->gs_input_size
);
5259 state
->gs_input_prim_type_specified
= true;
5260 state
->gs_input_prim_type
= this->prim_type
;
5262 /* If any shader inputs occurred before this declaration and did not
5263 * specify an array size, their size is determined now.
5265 foreach_list (node
, instructions
) {
5266 ir_variable
*var
= ((ir_instruction
*) node
)->as_variable();
5267 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_in
)
5270 /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
5274 if (var
->type
->is_unsized_array()) {
5275 if (var
->data
.max_array_access
>= num_vertices
) {
5276 _mesa_glsl_error(&loc
, state
,
5277 "this geometry shader input layout implies %u"
5278 " vertices, but an access to element %u of input"
5279 " `%s' already exists", num_vertices
,
5280 var
->data
.max_array_access
, var
->name
);
5282 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
5293 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
5294 exec_list
*instructions
)
5296 bool gl_FragColor_assigned
= false;
5297 bool gl_FragData_assigned
= false;
5298 bool user_defined_fs_output_assigned
= false;
5299 ir_variable
*user_defined_fs_output
= NULL
;
5301 /* It would be nice to have proper location information. */
5303 memset(&loc
, 0, sizeof(loc
));
5305 foreach_list(node
, instructions
) {
5306 ir_variable
*var
= ((ir_instruction
*)node
)->as_variable();
5308 if (!var
|| !var
->data
.assigned
)
5311 if (strcmp(var
->name
, "gl_FragColor") == 0)
5312 gl_FragColor_assigned
= true;
5313 else if (strcmp(var
->name
, "gl_FragData") == 0)
5314 gl_FragData_assigned
= true;
5315 else if (strncmp(var
->name
, "gl_", 3) != 0) {
5316 if (state
->stage
== MESA_SHADER_FRAGMENT
&&
5317 var
->data
.mode
== ir_var_shader_out
) {
5318 user_defined_fs_output_assigned
= true;
5319 user_defined_fs_output
= var
;
5324 /* From the GLSL 1.30 spec:
5326 * "If a shader statically assigns a value to gl_FragColor, it
5327 * may not assign a value to any element of gl_FragData. If a
5328 * shader statically writes a value to any element of
5329 * gl_FragData, it may not assign a value to
5330 * gl_FragColor. That is, a shader may assign values to either
5331 * gl_FragColor or gl_FragData, but not both. Multiple shaders
5332 * linked together must also consistently write just one of
5333 * these variables. Similarly, if user declared output
5334 * variables are in use (statically assigned to), then the
5335 * built-in variables gl_FragColor and gl_FragData may not be
5336 * assigned to. These incorrect usages all generate compile
5339 if (gl_FragColor_assigned
&& gl_FragData_assigned
) {
5340 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
5341 "`gl_FragColor' and `gl_FragData'");
5342 } else if (gl_FragColor_assigned
&& user_defined_fs_output_assigned
) {
5343 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
5344 "`gl_FragColor' and `%s'",
5345 user_defined_fs_output
->name
);
5346 } else if (gl_FragData_assigned
&& user_defined_fs_output_assigned
) {
5347 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
5348 "`gl_FragData' and `%s'",
5349 user_defined_fs_output
->name
);
5355 remove_per_vertex_blocks(exec_list
*instructions
,
5356 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
)
5358 /* Find the gl_PerVertex interface block of the appropriate (in/out) mode,
5359 * if it exists in this shader type.
5361 const glsl_type
*per_vertex
= NULL
;
5363 case ir_var_shader_in
:
5364 if (ir_variable
*gl_in
= state
->symbols
->get_variable("gl_in"))
5365 per_vertex
= gl_in
->get_interface_type();
5367 case ir_var_shader_out
:
5368 if (ir_variable
*gl_Position
=
5369 state
->symbols
->get_variable("gl_Position")) {
5370 per_vertex
= gl_Position
->get_interface_type();
5374 assert(!"Unexpected mode");
5378 /* If we didn't find a built-in gl_PerVertex interface block, then we don't
5379 * need to do anything.
5381 if (per_vertex
== NULL
)
5384 /* If the interface block is used by the shader, then we don't need to do
5387 interface_block_usage_visitor
v(mode
, per_vertex
);
5388 v
.run(instructions
);
5389 if (v
.usage_found())
5392 /* Remove any ir_variable declarations that refer to the interface block
5395 foreach_list_safe(node
, instructions
) {
5396 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
5397 if (var
!= NULL
&& var
->get_interface_type() == per_vertex
&&
5398 var
->data
.mode
== mode
) {
5399 state
->symbols
->disable_variable(var
->name
);