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;
80 state
->cs_input_local_size_specified
= false;
82 /* Section 4.2 of the GLSL 1.20 specification states:
83 * "The built-in functions are scoped in a scope outside the global scope
84 * users declare global variables in. That is, a shader's global scope,
85 * available for user-defined functions and global variables, is nested
86 * inside the scope containing the built-in functions."
88 * Since built-in functions like ftransform() access built-in variables,
89 * it follows that those must be in the outer scope as well.
91 * We push scope here to create this nesting effect...but don't pop.
92 * This way, a shader's globals are still in the symbol table for use
95 state
->symbols
->push_scope();
97 foreach_list_typed (ast_node
, ast
, link
, & state
->translation_unit
)
98 ast
->hir(instructions
, state
);
100 detect_recursion_unlinked(state
, instructions
);
101 detect_conflicting_assignments(state
, instructions
);
103 state
->toplevel_ir
= NULL
;
105 /* Move all of the variable declarations to the front of the IR list, and
106 * reverse the order. This has the (intended!) side effect that vertex
107 * shader inputs and fragment shader outputs will appear in the IR in the
108 * same order that they appeared in the shader code. This results in the
109 * locations being assigned in the declared order. Many (arguably buggy)
110 * applications depend on this behavior, and it matches what nearly all
113 foreach_list_safe(node
, instructions
) {
114 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
120 instructions
->push_head(var
);
123 /* From section 7.1 (Built-In Language Variables) of the GLSL 4.10 spec:
125 * If multiple shaders using members of a built-in block belonging to
126 * the same interface are linked together in the same program, they
127 * must all redeclare the built-in block in the same way, as described
128 * in section 4.3.7 "Interface Blocks" for interface block matching, or
129 * a link error will result.
131 * The phrase "using members of a built-in block" implies that if two
132 * shaders are linked together and one of them *does not use* any members
133 * of the built-in block, then that shader does not need to have a matching
134 * redeclaration of the built-in block.
136 * This appears to be a clarification to the behaviour established for
137 * gl_PerVertex by GLSL 1.50, therefore implement it regardless of GLSL
140 * The definition of "interface" in section 4.3.7 that applies here is as
143 * The boundary between adjacent programmable pipeline stages: This
144 * spans all the outputs in all compilation units of the first stage
145 * and all the inputs in all compilation units of the second stage.
147 * Therefore this rule applies to both inter- and intra-stage linking.
149 * The easiest way to implement this is to check whether the shader uses
150 * gl_PerVertex right after ast-to-ir conversion, and if it doesn't, simply
151 * remove all the relevant variable declaration from the IR, so that the
152 * linker won't see them and complain about mismatches.
154 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_in
);
155 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_out
);
160 * If a conversion is available, convert one operand to a different type
162 * The \c from \c ir_rvalue is converted "in place".
164 * \param to Type that the operand it to be converted to
165 * \param from Operand that is being converted
166 * \param state GLSL compiler state
169 * If a conversion is possible (or unnecessary), \c true is returned.
170 * Otherwise \c false is returned.
173 apply_implicit_conversion(const glsl_type
*to
, ir_rvalue
* &from
,
174 struct _mesa_glsl_parse_state
*state
)
177 if (to
->base_type
== from
->type
->base_type
)
180 /* This conversion was added in GLSL 1.20. If the compilation mode is
181 * GLSL 1.10, the conversion is skipped.
183 if (!state
->is_version(120, 0))
186 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
188 * "There are no implicit array or structure conversions. For
189 * example, an array of int cannot be implicitly converted to an
190 * array of float. There are no implicit conversions between
191 * signed and unsigned integers."
193 /* FINISHME: The above comment is partially a lie. There is int/uint
194 * FINISHME: conversion for immediate constants.
196 if (!to
->is_float() || !from
->type
->is_numeric())
199 /* Convert to a floating point type with the same number of components
200 * as the original type - i.e. int to float, not int to vec4.
202 to
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, from
->type
->vector_elements
,
203 from
->type
->matrix_columns
);
205 switch (from
->type
->base_type
) {
207 from
= new(ctx
) ir_expression(ir_unop_i2f
, to
, from
, NULL
);
210 from
= new(ctx
) ir_expression(ir_unop_u2f
, to
, from
, NULL
);
213 from
= new(ctx
) ir_expression(ir_unop_b2f
, to
, from
, NULL
);
223 static const struct glsl_type
*
224 arithmetic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
226 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
228 const glsl_type
*type_a
= value_a
->type
;
229 const glsl_type
*type_b
= value_b
->type
;
231 /* From GLSL 1.50 spec, page 56:
233 * "The arithmetic binary operators add (+), subtract (-),
234 * multiply (*), and divide (/) operate on integer and
235 * floating-point scalars, vectors, and matrices."
237 if (!type_a
->is_numeric() || !type_b
->is_numeric()) {
238 _mesa_glsl_error(loc
, state
,
239 "operands to arithmetic operators must be numeric");
240 return glsl_type::error_type
;
244 /* "If one operand is floating-point based and the other is
245 * not, then the conversions from Section 4.1.10 "Implicit
246 * Conversions" are applied to the non-floating-point-based operand."
248 if (!apply_implicit_conversion(type_a
, value_b
, state
)
249 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
250 _mesa_glsl_error(loc
, state
,
251 "could not implicitly convert operands to "
252 "arithmetic operator");
253 return glsl_type::error_type
;
255 type_a
= value_a
->type
;
256 type_b
= value_b
->type
;
258 /* "If the operands are integer types, they must both be signed or
261 * From this rule and the preceeding conversion it can be inferred that
262 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
263 * The is_numeric check above already filtered out the case where either
264 * type is not one of these, so now the base types need only be tested for
267 if (type_a
->base_type
!= type_b
->base_type
) {
268 _mesa_glsl_error(loc
, state
,
269 "base type mismatch for arithmetic operator");
270 return glsl_type::error_type
;
273 /* "All arithmetic binary operators result in the same fundamental type
274 * (signed integer, unsigned integer, or floating-point) as the
275 * operands they operate on, after operand type conversion. After
276 * conversion, the following cases are valid
278 * * The two operands are scalars. In this case the operation is
279 * applied, resulting in a scalar."
281 if (type_a
->is_scalar() && type_b
->is_scalar())
284 /* "* One operand is a scalar, and the other is a vector or matrix.
285 * In this case, the scalar operation is applied independently to each
286 * component of the vector or matrix, resulting in the same size
289 if (type_a
->is_scalar()) {
290 if (!type_b
->is_scalar())
292 } else if (type_b
->is_scalar()) {
296 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
297 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
300 assert(!type_a
->is_scalar());
301 assert(!type_b
->is_scalar());
303 /* "* The two operands are vectors of the same size. In this case, the
304 * operation is done component-wise resulting in the same size
307 if (type_a
->is_vector() && type_b
->is_vector()) {
308 if (type_a
== type_b
) {
311 _mesa_glsl_error(loc
, state
,
312 "vector size mismatch for arithmetic operator");
313 return glsl_type::error_type
;
317 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
318 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
319 * <vector, vector> have been handled. At least one of the operands must
320 * be matrix. Further, since there are no integer matrix types, the base
321 * type of both operands must be float.
323 assert(type_a
->is_matrix() || type_b
->is_matrix());
324 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
);
325 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
);
327 /* "* The operator is add (+), subtract (-), or divide (/), and the
328 * operands are matrices with the same number of rows and the same
329 * number of columns. In this case, the operation is done component-
330 * wise resulting in the same size matrix."
331 * * The operator is multiply (*), where both operands are matrices or
332 * one operand is a vector and the other a matrix. A right vector
333 * operand is treated as a column vector and a left vector operand as a
334 * row vector. In all these cases, it is required that the number of
335 * columns of the left operand is equal to the number of rows of the
336 * right operand. Then, the multiply (*) operation does a linear
337 * algebraic multiply, yielding an object that has the same number of
338 * rows as the left operand and the same number of columns as the right
339 * operand. Section 5.10 "Vector and Matrix Operations" explains in
340 * more detail how vectors and matrices are operated on."
343 if (type_a
== type_b
)
346 if (type_a
->is_matrix() && type_b
->is_matrix()) {
347 /* Matrix multiply. The columns of A must match the rows of B. Given
348 * the other previously tested constraints, this means the vector type
349 * of a row from A must be the same as the vector type of a column from
352 if (type_a
->row_type() == type_b
->column_type()) {
353 /* The resulting matrix has the number of columns of matrix B and
354 * the number of rows of matrix A. We get the row count of A by
355 * looking at the size of a vector that makes up a column. The
356 * transpose (size of a row) is done for B.
358 const glsl_type
*const type
=
359 glsl_type::get_instance(type_a
->base_type
,
360 type_a
->column_type()->vector_elements
,
361 type_b
->row_type()->vector_elements
);
362 assert(type
!= glsl_type::error_type
);
366 } else if (type_a
->is_matrix()) {
367 /* A is a matrix and B is a column vector. Columns of A must match
368 * rows of B. Given the other previously tested constraints, this
369 * means the vector type of a row from A must be the same as the
370 * vector the type of B.
372 if (type_a
->row_type() == type_b
) {
373 /* The resulting vector has a number of elements equal to
374 * the number of rows of matrix A. */
375 const glsl_type
*const type
=
376 glsl_type::get_instance(type_a
->base_type
,
377 type_a
->column_type()->vector_elements
,
379 assert(type
!= glsl_type::error_type
);
384 assert(type_b
->is_matrix());
386 /* A is a row vector and B is a matrix. Columns of A must match rows
387 * of B. Given the other previously tested constraints, this means
388 * the type of A must be the same as the vector type of a column from
391 if (type_a
== type_b
->column_type()) {
392 /* The resulting vector has a number of elements equal to
393 * the number of columns of matrix B. */
394 const glsl_type
*const type
=
395 glsl_type::get_instance(type_a
->base_type
,
396 type_b
->row_type()->vector_elements
,
398 assert(type
!= glsl_type::error_type
);
404 _mesa_glsl_error(loc
, state
, "size mismatch for matrix multiplication");
405 return glsl_type::error_type
;
409 /* "All other cases are illegal."
411 _mesa_glsl_error(loc
, state
, "type mismatch");
412 return glsl_type::error_type
;
416 static const struct glsl_type
*
417 unary_arithmetic_result_type(const struct glsl_type
*type
,
418 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
420 /* From GLSL 1.50 spec, page 57:
422 * "The arithmetic unary operators negate (-), post- and pre-increment
423 * and decrement (-- and ++) operate on integer or floating-point
424 * values (including vectors and matrices). All unary operators work
425 * component-wise on their operands. These result with the same type
428 if (!type
->is_numeric()) {
429 _mesa_glsl_error(loc
, state
,
430 "operands to arithmetic operators must be numeric");
431 return glsl_type::error_type
;
438 * \brief Return the result type of a bit-logic operation.
440 * If the given types to the bit-logic operator are invalid, return
441 * glsl_type::error_type.
443 * \param type_a Type of LHS of bit-logic op
444 * \param type_b Type of RHS of bit-logic op
446 static const struct glsl_type
*
447 bit_logic_result_type(const struct glsl_type
*type_a
,
448 const struct glsl_type
*type_b
,
450 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
452 if (!state
->check_bitwise_operations_allowed(loc
)) {
453 return glsl_type::error_type
;
456 /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
458 * "The bitwise operators and (&), exclusive-or (^), and inclusive-or
459 * (|). The operands must be of type signed or unsigned integers or
462 if (!type_a
->is_integer()) {
463 _mesa_glsl_error(loc
, state
, "LHS of `%s' must be an integer",
464 ast_expression::operator_string(op
));
465 return glsl_type::error_type
;
467 if (!type_b
->is_integer()) {
468 _mesa_glsl_error(loc
, state
, "RHS of `%s' must be an integer",
469 ast_expression::operator_string(op
));
470 return glsl_type::error_type
;
473 /* "The fundamental types of the operands (signed or unsigned) must
476 if (type_a
->base_type
!= type_b
->base_type
) {
477 _mesa_glsl_error(loc
, state
, "operands of `%s' must have the same "
478 "base type", ast_expression::operator_string(op
));
479 return glsl_type::error_type
;
482 /* "The operands cannot be vectors of differing size." */
483 if (type_a
->is_vector() &&
484 type_b
->is_vector() &&
485 type_a
->vector_elements
!= type_b
->vector_elements
) {
486 _mesa_glsl_error(loc
, state
, "operands of `%s' cannot be vectors of "
487 "different sizes", ast_expression::operator_string(op
));
488 return glsl_type::error_type
;
491 /* "If one operand is a scalar and the other a vector, the scalar is
492 * applied component-wise to the vector, resulting in the same type as
493 * the vector. The fundamental types of the operands [...] will be the
494 * resulting fundamental type."
496 if (type_a
->is_scalar())
502 static const struct glsl_type
*
503 modulus_result_type(const struct glsl_type
*type_a
,
504 const struct glsl_type
*type_b
,
505 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
507 if (!state
->check_version(130, 300, loc
, "operator '%%' is reserved")) {
508 return glsl_type::error_type
;
511 /* From GLSL 1.50 spec, page 56:
512 * "The operator modulus (%) operates on signed or unsigned integers or
513 * integer vectors. The operand types must both be signed or both be
516 if (!type_a
->is_integer()) {
517 _mesa_glsl_error(loc
, state
, "LHS of operator %% must be an integer");
518 return glsl_type::error_type
;
520 if (!type_b
->is_integer()) {
521 _mesa_glsl_error(loc
, state
, "RHS of operator %% must be an integer");
522 return glsl_type::error_type
;
524 if (type_a
->base_type
!= type_b
->base_type
) {
525 _mesa_glsl_error(loc
, state
,
526 "operands of %% must have the same base type");
527 return glsl_type::error_type
;
530 /* "The operands cannot be vectors of differing size. If one operand is
531 * a scalar and the other vector, then the scalar is applied component-
532 * wise to the vector, resulting in the same type as the vector. If both
533 * are vectors of the same size, the result is computed component-wise."
535 if (type_a
->is_vector()) {
536 if (!type_b
->is_vector()
537 || (type_a
->vector_elements
== type_b
->vector_elements
))
542 /* "The operator modulus (%) is not defined for any other data types
543 * (non-integer types)."
545 _mesa_glsl_error(loc
, state
, "type mismatch");
546 return glsl_type::error_type
;
550 static const struct glsl_type
*
551 relational_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
552 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
554 const glsl_type
*type_a
= value_a
->type
;
555 const glsl_type
*type_b
= value_b
->type
;
557 /* From GLSL 1.50 spec, page 56:
558 * "The relational operators greater than (>), less than (<), greater
559 * than or equal (>=), and less than or equal (<=) operate only on
560 * scalar integer and scalar floating-point expressions."
562 if (!type_a
->is_numeric()
563 || !type_b
->is_numeric()
564 || !type_a
->is_scalar()
565 || !type_b
->is_scalar()) {
566 _mesa_glsl_error(loc
, state
,
567 "operands to relational operators must be scalar and "
569 return glsl_type::error_type
;
572 /* "Either the operands' types must match, or the conversions from
573 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
574 * operand, after which the types must match."
576 if (!apply_implicit_conversion(type_a
, value_b
, state
)
577 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
578 _mesa_glsl_error(loc
, state
,
579 "could not implicitly convert operands to "
580 "relational operator");
581 return glsl_type::error_type
;
583 type_a
= value_a
->type
;
584 type_b
= value_b
->type
;
586 if (type_a
->base_type
!= type_b
->base_type
) {
587 _mesa_glsl_error(loc
, state
, "base type mismatch");
588 return glsl_type::error_type
;
591 /* "The result is scalar Boolean."
593 return glsl_type::bool_type
;
597 * \brief Return the result type of a bit-shift operation.
599 * If the given types to the bit-shift operator are invalid, return
600 * glsl_type::error_type.
602 * \param type_a Type of LHS of bit-shift op
603 * \param type_b Type of RHS of bit-shift op
605 static const struct glsl_type
*
606 shift_result_type(const struct glsl_type
*type_a
,
607 const struct glsl_type
*type_b
,
609 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
611 if (!state
->check_bitwise_operations_allowed(loc
)) {
612 return glsl_type::error_type
;
615 /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
617 * "The shift operators (<<) and (>>). For both operators, the operands
618 * must be signed or unsigned integers or integer vectors. One operand
619 * can be signed while the other is unsigned."
621 if (!type_a
->is_integer()) {
622 _mesa_glsl_error(loc
, state
, "LHS of operator %s must be an integer or "
623 "integer vector", ast_expression::operator_string(op
));
624 return glsl_type::error_type
;
627 if (!type_b
->is_integer()) {
628 _mesa_glsl_error(loc
, state
, "RHS of operator %s must be an integer or "
629 "integer vector", ast_expression::operator_string(op
));
630 return glsl_type::error_type
;
633 /* "If the first operand is a scalar, the second operand has to be
636 if (type_a
->is_scalar() && !type_b
->is_scalar()) {
637 _mesa_glsl_error(loc
, state
, "if the first operand of %s is scalar, the "
638 "second must be scalar as well",
639 ast_expression::operator_string(op
));
640 return glsl_type::error_type
;
643 /* If both operands are vectors, check that they have same number of
646 if (type_a
->is_vector() &&
647 type_b
->is_vector() &&
648 type_a
->vector_elements
!= type_b
->vector_elements
) {
649 _mesa_glsl_error(loc
, state
, "vector operands to operator %s must "
650 "have same number of elements",
651 ast_expression::operator_string(op
));
652 return glsl_type::error_type
;
655 /* "In all cases, the resulting type will be the same type as the left
662 * Validates that a value can be assigned to a location with a specified type
664 * Validates that \c rhs can be assigned to some location. If the types are
665 * not an exact match but an automatic conversion is possible, \c rhs will be
669 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
670 * Otherwise the actual RHS to be assigned will be returned. This may be
671 * \c rhs, or it may be \c rhs after some type conversion.
674 * In addition to being used for assignments, this function is used to
675 * type-check return values.
678 validate_assignment(struct _mesa_glsl_parse_state
*state
,
679 YYLTYPE loc
, const glsl_type
*lhs_type
,
680 ir_rvalue
*rhs
, bool is_initializer
)
682 /* If there is already some error in the RHS, just return it. Anything
683 * else will lead to an avalanche of error message back to the user.
685 if (rhs
->type
->is_error())
688 /* If the types are identical, the assignment can trivially proceed.
690 if (rhs
->type
== lhs_type
)
693 /* If the array element types are the same and the LHS is unsized,
694 * the assignment is okay for initializers embedded in variable
697 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
698 * is handled by ir_dereference::is_lvalue.
700 if (lhs_type
->is_unsized_array() && rhs
->type
->is_array()
701 && (lhs_type
->element_type() == rhs
->type
->element_type())) {
702 if (is_initializer
) {
705 _mesa_glsl_error(&loc
, state
,
706 "implicitly sized arrays cannot be assigned");
711 /* Check for implicit conversion in GLSL 1.20 */
712 if (apply_implicit_conversion(lhs_type
, rhs
, state
)) {
713 if (rhs
->type
== lhs_type
)
717 _mesa_glsl_error(&loc
, state
,
718 "%s of type %s cannot be assigned to "
719 "variable of type %s",
720 is_initializer
? "initializer" : "value",
721 rhs
->type
->name
, lhs_type
->name
);
727 mark_whole_array_access(ir_rvalue
*access
)
729 ir_dereference_variable
*deref
= access
->as_dereference_variable();
731 if (deref
&& deref
->var
) {
732 deref
->var
->data
.max_array_access
= deref
->type
->length
- 1;
737 do_assignment(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
738 const char *non_lvalue_description
,
739 ir_rvalue
*lhs
, ir_rvalue
*rhs
, bool is_initializer
,
743 bool error_emitted
= (lhs
->type
->is_error() || rhs
->type
->is_error());
744 ir_rvalue
*extract_channel
= NULL
;
746 /* If the assignment LHS comes back as an ir_binop_vector_extract
747 * expression, move it to the RHS as an ir_triop_vector_insert.
749 if (lhs
->ir_type
== ir_type_expression
) {
750 ir_expression
*const lhs_expr
= lhs
->as_expression();
752 if (unlikely(lhs_expr
->operation
== ir_binop_vector_extract
)) {
754 validate_assignment(state
, lhs_loc
, lhs
->type
,
755 rhs
, is_initializer
);
757 if (new_rhs
== NULL
) {
761 * - LHS: (expression float vector_extract <vec> <channel>)
765 * - RHS: (expression vec2 vector_insert <vec> <channel> <scalar>)
767 * The LHS type is now a vector instead of a scalar. Since GLSL
768 * allows assignments to be used as rvalues, we need to re-extract
769 * the channel from assignment_temp when returning the rvalue.
771 extract_channel
= lhs_expr
->operands
[1];
772 rhs
= new(ctx
) ir_expression(ir_triop_vector_insert
,
773 lhs_expr
->operands
[0]->type
,
774 lhs_expr
->operands
[0],
777 lhs
= lhs_expr
->operands
[0]->clone(ctx
, NULL
);
782 ir_variable
*lhs_var
= lhs
->variable_referenced();
784 lhs_var
->data
.assigned
= true;
786 if (!error_emitted
) {
787 if (non_lvalue_description
!= NULL
) {
788 _mesa_glsl_error(&lhs_loc
, state
,
790 non_lvalue_description
);
791 error_emitted
= true;
792 } else if (lhs
->variable_referenced() != NULL
793 && lhs
->variable_referenced()->data
.read_only
) {
794 _mesa_glsl_error(&lhs_loc
, state
,
795 "assignment to read-only variable '%s'",
796 lhs
->variable_referenced()->name
);
797 error_emitted
= true;
799 } else if (lhs
->type
->is_array() &&
800 !state
->check_version(120, 300, &lhs_loc
,
801 "whole array assignment forbidden")) {
802 /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
804 * "Other binary or unary expressions, non-dereferenced
805 * arrays, function names, swizzles with repeated fields,
806 * and constants cannot be l-values."
808 * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00.
810 error_emitted
= true;
811 } else if (!lhs
->is_lvalue()) {
812 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
813 error_emitted
= true;
818 validate_assignment(state
, lhs_loc
, lhs
->type
, rhs
, is_initializer
);
819 if (new_rhs
!= NULL
) {
822 /* If the LHS array was not declared with a size, it takes it size from
823 * the RHS. If the LHS is an l-value and a whole array, it must be a
824 * dereference of a variable. Any other case would require that the LHS
825 * is either not an l-value or not a whole array.
827 if (lhs
->type
->is_unsized_array()) {
828 ir_dereference
*const d
= lhs
->as_dereference();
832 ir_variable
*const var
= d
->variable_referenced();
836 if (var
->data
.max_array_access
>= unsigned(rhs
->type
->array_size())) {
837 /* FINISHME: This should actually log the location of the RHS. */
838 _mesa_glsl_error(& lhs_loc
, state
, "array size must be > %u due to "
840 var
->data
.max_array_access
);
843 var
->type
= glsl_type::get_array_instance(lhs
->type
->element_type(),
844 rhs
->type
->array_size());
847 if (lhs
->type
->is_array()) {
848 mark_whole_array_access(rhs
);
849 mark_whole_array_access(lhs
);
853 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
854 * but not post_inc) need the converted assigned value as an rvalue
855 * to handle things like:
859 * So we always just store the computed value being assigned to a
860 * temporary and return a deref of that temporary. If the rvalue
861 * ends up not being used, the temp will get copy-propagated out.
863 ir_variable
*var
= new(ctx
) ir_variable(rhs
->type
, "assignment_tmp",
865 ir_dereference_variable
*deref_var
= new(ctx
) ir_dereference_variable(var
);
866 instructions
->push_tail(var
);
867 instructions
->push_tail(new(ctx
) ir_assignment(deref_var
, rhs
));
868 deref_var
= new(ctx
) ir_dereference_variable(var
);
871 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, deref_var
));
873 if (extract_channel
) {
874 return new(ctx
) ir_expression(ir_binop_vector_extract
,
875 new(ctx
) ir_dereference_variable(var
),
876 extract_channel
->clone(ctx
, NULL
));
878 return new(ctx
) ir_dereference_variable(var
);
882 get_lvalue_copy(exec_list
*instructions
, ir_rvalue
*lvalue
)
884 void *ctx
= ralloc_parent(lvalue
);
887 var
= new(ctx
) ir_variable(lvalue
->type
, "_post_incdec_tmp",
889 instructions
->push_tail(var
);
890 var
->data
.mode
= ir_var_auto
;
892 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
895 return new(ctx
) ir_dereference_variable(var
);
900 ast_node::hir(exec_list
*instructions
,
901 struct _mesa_glsl_parse_state
*state
)
910 do_comparison(void *mem_ctx
, int operation
, ir_rvalue
*op0
, ir_rvalue
*op1
)
913 ir_rvalue
*cmp
= NULL
;
915 if (operation
== ir_binop_all_equal
)
916 join_op
= ir_binop_logic_and
;
918 join_op
= ir_binop_logic_or
;
920 switch (op0
->type
->base_type
) {
921 case GLSL_TYPE_FLOAT
:
925 return new(mem_ctx
) ir_expression(operation
, op0
, op1
);
927 case GLSL_TYPE_ARRAY
: {
928 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
929 ir_rvalue
*e0
, *e1
, *result
;
931 e0
= new(mem_ctx
) ir_dereference_array(op0
->clone(mem_ctx
, NULL
),
932 new(mem_ctx
) ir_constant(i
));
933 e1
= new(mem_ctx
) ir_dereference_array(op1
->clone(mem_ctx
, NULL
),
934 new(mem_ctx
) ir_constant(i
));
935 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
938 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
944 mark_whole_array_access(op0
);
945 mark_whole_array_access(op1
);
949 case GLSL_TYPE_STRUCT
: {
950 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
951 ir_rvalue
*e0
, *e1
, *result
;
952 const char *field_name
= op0
->type
->fields
.structure
[i
].name
;
954 e0
= new(mem_ctx
) ir_dereference_record(op0
->clone(mem_ctx
, NULL
),
956 e1
= new(mem_ctx
) ir_dereference_record(op1
->clone(mem_ctx
, NULL
),
958 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
961 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
969 case GLSL_TYPE_ERROR
:
971 case GLSL_TYPE_SAMPLER
:
972 case GLSL_TYPE_INTERFACE
:
973 case GLSL_TYPE_ATOMIC_UINT
:
974 /* I assume a comparison of a struct containing a sampler just
975 * ignores the sampler present in the type.
981 cmp
= new(mem_ctx
) ir_constant(true);
986 /* For logical operations, we want to ensure that the operands are
987 * scalar booleans. If it isn't, emit an error and return a constant
988 * boolean to avoid triggering cascading error messages.
991 get_scalar_boolean_operand(exec_list
*instructions
,
992 struct _mesa_glsl_parse_state
*state
,
993 ast_expression
*parent_expr
,
995 const char *operand_name
,
998 ast_expression
*expr
= parent_expr
->subexpressions
[operand
];
1000 ir_rvalue
*val
= expr
->hir(instructions
, state
);
1002 if (val
->type
->is_boolean() && val
->type
->is_scalar())
1005 if (!*error_emitted
) {
1006 YYLTYPE loc
= expr
->get_location();
1007 _mesa_glsl_error(&loc
, state
, "%s of `%s' must be scalar boolean",
1009 parent_expr
->operator_string(parent_expr
->oper
));
1010 *error_emitted
= true;
1013 return new(ctx
) ir_constant(true);
1017 * If name refers to a builtin array whose maximum allowed size is less than
1018 * size, report an error and return true. Otherwise return false.
1021 check_builtin_array_max_size(const char *name
, unsigned size
,
1022 YYLTYPE loc
, struct _mesa_glsl_parse_state
*state
)
1024 if ((strcmp("gl_TexCoord", name
) == 0)
1025 && (size
> state
->Const
.MaxTextureCoords
)) {
1026 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1028 * "The size [of gl_TexCoord] can be at most
1029 * gl_MaxTextureCoords."
1031 _mesa_glsl_error(&loc
, state
, "`gl_TexCoord' array size cannot "
1032 "be larger than gl_MaxTextureCoords (%u)",
1033 state
->Const
.MaxTextureCoords
);
1034 } else if (strcmp("gl_ClipDistance", name
) == 0
1035 && size
> state
->Const
.MaxClipPlanes
) {
1036 /* From section 7.1 (Vertex Shader Special Variables) of the
1039 * "The gl_ClipDistance array is predeclared as unsized and
1040 * must be sized by the shader either redeclaring it with a
1041 * size or indexing it only with integral constant
1042 * expressions. ... The size can be at most
1043 * gl_MaxClipDistances."
1045 _mesa_glsl_error(&loc
, state
, "`gl_ClipDistance' array size cannot "
1046 "be larger than gl_MaxClipDistances (%u)",
1047 state
->Const
.MaxClipPlanes
);
1052 * Create the constant 1, of a which is appropriate for incrementing and
1053 * decrementing values of the given GLSL type. For example, if type is vec4,
1054 * this creates a constant value of 1.0 having type float.
1056 * If the given type is invalid for increment and decrement operators, return
1057 * a floating point 1--the error will be detected later.
1060 constant_one_for_inc_dec(void *ctx
, const glsl_type
*type
)
1062 switch (type
->base_type
) {
1063 case GLSL_TYPE_UINT
:
1064 return new(ctx
) ir_constant((unsigned) 1);
1066 return new(ctx
) ir_constant(1);
1068 case GLSL_TYPE_FLOAT
:
1069 return new(ctx
) ir_constant(1.0f
);
1074 ast_expression::hir(exec_list
*instructions
,
1075 struct _mesa_glsl_parse_state
*state
)
1078 static const int operations
[AST_NUM_OPERATORS
] = {
1079 -1, /* ast_assign doesn't convert to ir_expression. */
1080 -1, /* ast_plus doesn't convert to ir_expression. */
1094 ir_binop_any_nequal
,
1104 /* Note: The following block of expression types actually convert
1105 * to multiple IR instructions.
1107 ir_binop_mul
, /* ast_mul_assign */
1108 ir_binop_div
, /* ast_div_assign */
1109 ir_binop_mod
, /* ast_mod_assign */
1110 ir_binop_add
, /* ast_add_assign */
1111 ir_binop_sub
, /* ast_sub_assign */
1112 ir_binop_lshift
, /* ast_ls_assign */
1113 ir_binop_rshift
, /* ast_rs_assign */
1114 ir_binop_bit_and
, /* ast_and_assign */
1115 ir_binop_bit_xor
, /* ast_xor_assign */
1116 ir_binop_bit_or
, /* ast_or_assign */
1118 -1, /* ast_conditional doesn't convert to ir_expression. */
1119 ir_binop_add
, /* ast_pre_inc. */
1120 ir_binop_sub
, /* ast_pre_dec. */
1121 ir_binop_add
, /* ast_post_inc. */
1122 ir_binop_sub
, /* ast_post_dec. */
1123 -1, /* ast_field_selection doesn't conv to ir_expression. */
1124 -1, /* ast_array_index doesn't convert to ir_expression. */
1125 -1, /* ast_function_call doesn't conv to ir_expression. */
1126 -1, /* ast_identifier doesn't convert to ir_expression. */
1127 -1, /* ast_int_constant doesn't convert to ir_expression. */
1128 -1, /* ast_uint_constant doesn't conv to ir_expression. */
1129 -1, /* ast_float_constant doesn't conv to ir_expression. */
1130 -1, /* ast_bool_constant doesn't conv to ir_expression. */
1131 -1, /* ast_sequence doesn't convert to ir_expression. */
1133 ir_rvalue
*result
= NULL
;
1135 const struct glsl_type
*type
; /* a temporary variable for switch cases */
1136 bool error_emitted
= false;
1139 loc
= this->get_location();
1141 switch (this->oper
) {
1143 assert(!"ast_aggregate: Should never get here.");
1147 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1148 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1150 result
= do_assignment(instructions
, state
,
1151 this->subexpressions
[0]->non_lvalue_description
,
1152 op
[0], op
[1], false,
1153 this->subexpressions
[0]->get_location());
1154 error_emitted
= result
->type
->is_error();
1159 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1161 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1163 error_emitted
= type
->is_error();
1169 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1171 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1173 error_emitted
= type
->is_error();
1175 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1183 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1184 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1186 type
= arithmetic_result_type(op
[0], op
[1],
1187 (this->oper
== ast_mul
),
1189 error_emitted
= type
->is_error();
1191 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1196 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1197 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1199 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1201 assert(operations
[this->oper
] == ir_binop_mod
);
1203 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1205 error_emitted
= type
->is_error();
1210 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1211 error_emitted
= true;
1214 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1215 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1216 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1218 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1220 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1227 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1228 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1230 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
1232 /* The relational operators must either generate an error or result
1233 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1235 assert(type
->is_error()
1236 || ((type
->base_type
== GLSL_TYPE_BOOL
)
1237 && type
->is_scalar()));
1239 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1241 error_emitted
= type
->is_error();
1246 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1247 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1249 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1251 * "The equality operators equal (==), and not equal (!=)
1252 * operate on all types. They result in a scalar Boolean. If
1253 * the operand types do not match, then there must be a
1254 * conversion from Section 4.1.10 "Implicit Conversions"
1255 * applied to one operand that can make them match, in which
1256 * case this conversion is done."
1258 if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
1259 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
1260 || (op
[0]->type
!= op
[1]->type
)) {
1261 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
1262 "type", (this->oper
== ast_equal
) ? "==" : "!=");
1263 error_emitted
= true;
1264 } else if ((op
[0]->type
->is_array() || op
[1]->type
->is_array()) &&
1265 !state
->check_version(120, 300, &loc
,
1266 "array comparisons forbidden")) {
1267 error_emitted
= true;
1268 } else if ((op
[0]->type
->contains_opaque() ||
1269 op
[1]->type
->contains_opaque())) {
1270 _mesa_glsl_error(&loc
, state
, "opaque type comparisons forbidden");
1271 error_emitted
= true;
1274 if (error_emitted
) {
1275 result
= new(ctx
) ir_constant(false);
1277 result
= do_comparison(ctx
, operations
[this->oper
], op
[0], op
[1]);
1278 assert(result
->type
== glsl_type::bool_type
);
1285 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1286 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1287 type
= bit_logic_result_type(op
[0]->type
, op
[1]->type
, this->oper
,
1289 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1291 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1295 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1297 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1298 error_emitted
= true;
1301 if (!op
[0]->type
->is_integer()) {
1302 _mesa_glsl_error(&loc
, state
, "operand of `~' must be an integer");
1303 error_emitted
= true;
1306 type
= error_emitted
? glsl_type::error_type
: op
[0]->type
;
1307 result
= new(ctx
) ir_expression(ir_unop_bit_not
, type
, op
[0], NULL
);
1310 case ast_logic_and
: {
1311 exec_list rhs_instructions
;
1312 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1313 "LHS", &error_emitted
);
1314 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1315 "RHS", &error_emitted
);
1317 if (rhs_instructions
.is_empty()) {
1318 result
= new(ctx
) ir_expression(ir_binop_logic_and
, op
[0], op
[1]);
1319 type
= result
->type
;
1321 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1324 instructions
->push_tail(tmp
);
1326 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1327 instructions
->push_tail(stmt
);
1329 stmt
->then_instructions
.append_list(&rhs_instructions
);
1330 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1331 ir_assignment
*const then_assign
=
1332 new(ctx
) ir_assignment(then_deref
, op
[1]);
1333 stmt
->then_instructions
.push_tail(then_assign
);
1335 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1336 ir_assignment
*const else_assign
=
1337 new(ctx
) ir_assignment(else_deref
, new(ctx
) ir_constant(false));
1338 stmt
->else_instructions
.push_tail(else_assign
);
1340 result
= new(ctx
) ir_dereference_variable(tmp
);
1346 case ast_logic_or
: {
1347 exec_list rhs_instructions
;
1348 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1349 "LHS", &error_emitted
);
1350 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1351 "RHS", &error_emitted
);
1353 if (rhs_instructions
.is_empty()) {
1354 result
= new(ctx
) ir_expression(ir_binop_logic_or
, op
[0], op
[1]);
1355 type
= result
->type
;
1357 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1360 instructions
->push_tail(tmp
);
1362 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1363 instructions
->push_tail(stmt
);
1365 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1366 ir_assignment
*const then_assign
=
1367 new(ctx
) ir_assignment(then_deref
, new(ctx
) ir_constant(true));
1368 stmt
->then_instructions
.push_tail(then_assign
);
1370 stmt
->else_instructions
.append_list(&rhs_instructions
);
1371 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1372 ir_assignment
*const else_assign
=
1373 new(ctx
) ir_assignment(else_deref
, op
[1]);
1374 stmt
->else_instructions
.push_tail(else_assign
);
1376 result
= new(ctx
) ir_dereference_variable(tmp
);
1383 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1385 * "The logical binary operators and (&&), or ( | | ), and
1386 * exclusive or (^^). They operate only on two Boolean
1387 * expressions and result in a Boolean expression."
1389 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0, "LHS",
1391 op
[1] = get_scalar_boolean_operand(instructions
, state
, this, 1, "RHS",
1394 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1399 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1400 "operand", &error_emitted
);
1402 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1406 case ast_mul_assign
:
1407 case ast_div_assign
:
1408 case ast_add_assign
:
1409 case ast_sub_assign
: {
1410 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1411 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1413 type
= arithmetic_result_type(op
[0], op
[1],
1414 (this->oper
== ast_mul_assign
),
1417 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1420 result
= do_assignment(instructions
, state
,
1421 this->subexpressions
[0]->non_lvalue_description
,
1422 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1423 this->subexpressions
[0]->get_location());
1424 error_emitted
= (op
[0]->type
->is_error());
1426 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1427 * explicitly test for this because none of the binary expression
1428 * operators allow array operands either.
1434 case ast_mod_assign
: {
1435 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1436 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1438 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1440 assert(operations
[this->oper
] == ir_binop_mod
);
1442 ir_rvalue
*temp_rhs
;
1443 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1446 result
= do_assignment(instructions
, state
,
1447 this->subexpressions
[0]->non_lvalue_description
,
1448 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1449 this->subexpressions
[0]->get_location());
1450 error_emitted
= type
->is_error();
1455 case ast_rs_assign
: {
1456 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1457 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1458 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1460 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1461 type
, op
[0], op
[1]);
1462 result
= do_assignment(instructions
, state
,
1463 this->subexpressions
[0]->non_lvalue_description
,
1464 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1465 this->subexpressions
[0]->get_location());
1466 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1470 case ast_and_assign
:
1471 case ast_xor_assign
:
1472 case ast_or_assign
: {
1473 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1474 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1475 type
= bit_logic_result_type(op
[0]->type
, op
[1]->type
, this->oper
,
1477 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1478 type
, op
[0], op
[1]);
1479 result
= do_assignment(instructions
, state
,
1480 this->subexpressions
[0]->non_lvalue_description
,
1481 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1482 this->subexpressions
[0]->get_location());
1483 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1487 case ast_conditional
: {
1488 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1490 * "The ternary selection operator (?:). It operates on three
1491 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1492 * first expression, which must result in a scalar Boolean."
1494 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1495 "condition", &error_emitted
);
1497 /* The :? operator is implemented by generating an anonymous temporary
1498 * followed by an if-statement. The last instruction in each branch of
1499 * the if-statement assigns a value to the anonymous temporary. This
1500 * temporary is the r-value of the expression.
1502 exec_list then_instructions
;
1503 exec_list else_instructions
;
1505 op
[1] = this->subexpressions
[1]->hir(&then_instructions
, state
);
1506 op
[2] = this->subexpressions
[2]->hir(&else_instructions
, state
);
1508 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1510 * "The second and third expressions can be any type, as
1511 * long their types match, or there is a conversion in
1512 * Section 4.1.10 "Implicit Conversions" that can be applied
1513 * to one of the expressions to make their types match. This
1514 * resulting matching type is the type of the entire
1517 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1518 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1519 || (op
[1]->type
!= op
[2]->type
)) {
1520 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1522 _mesa_glsl_error(& loc
, state
, "second and third operands of ?: "
1523 "operator must have matching types");
1524 error_emitted
= true;
1525 type
= glsl_type::error_type
;
1530 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1532 * "The second and third expressions must be the same type, but can
1533 * be of any type other than an array."
1535 if (type
->is_array() &&
1536 !state
->check_version(120, 300, &loc
,
1537 "second and third operands of ?: operator "
1538 "cannot be arrays")) {
1539 error_emitted
= true;
1542 ir_constant
*cond_val
= op
[0]->constant_expression_value();
1543 ir_constant
*then_val
= op
[1]->constant_expression_value();
1544 ir_constant
*else_val
= op
[2]->constant_expression_value();
1546 if (then_instructions
.is_empty()
1547 && else_instructions
.is_empty()
1548 && (cond_val
!= NULL
) && (then_val
!= NULL
) && (else_val
!= NULL
)) {
1549 result
= (cond_val
->value
.b
[0]) ? then_val
: else_val
;
1551 ir_variable
*const tmp
=
1552 new(ctx
) ir_variable(type
, "conditional_tmp", ir_var_temporary
);
1553 instructions
->push_tail(tmp
);
1555 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1556 instructions
->push_tail(stmt
);
1558 then_instructions
.move_nodes_to(& stmt
->then_instructions
);
1559 ir_dereference
*const then_deref
=
1560 new(ctx
) ir_dereference_variable(tmp
);
1561 ir_assignment
*const then_assign
=
1562 new(ctx
) ir_assignment(then_deref
, op
[1]);
1563 stmt
->then_instructions
.push_tail(then_assign
);
1565 else_instructions
.move_nodes_to(& stmt
->else_instructions
);
1566 ir_dereference
*const else_deref
=
1567 new(ctx
) ir_dereference_variable(tmp
);
1568 ir_assignment
*const else_assign
=
1569 new(ctx
) ir_assignment(else_deref
, op
[2]);
1570 stmt
->else_instructions
.push_tail(else_assign
);
1572 result
= new(ctx
) ir_dereference_variable(tmp
);
1579 this->non_lvalue_description
= (this->oper
== ast_pre_inc
)
1580 ? "pre-increment operation" : "pre-decrement operation";
1582 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1583 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1585 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1587 ir_rvalue
*temp_rhs
;
1588 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1591 result
= do_assignment(instructions
, state
,
1592 this->subexpressions
[0]->non_lvalue_description
,
1593 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1594 this->subexpressions
[0]->get_location());
1595 error_emitted
= op
[0]->type
->is_error();
1600 case ast_post_dec
: {
1601 this->non_lvalue_description
= (this->oper
== ast_post_inc
)
1602 ? "post-increment operation" : "post-decrement operation";
1603 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1604 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1606 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1608 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1610 ir_rvalue
*temp_rhs
;
1611 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1614 /* Get a temporary of a copy of the lvalue before it's modified.
1615 * This may get thrown away later.
1617 result
= get_lvalue_copy(instructions
, op
[0]->clone(ctx
, NULL
));
1619 (void)do_assignment(instructions
, state
,
1620 this->subexpressions
[0]->non_lvalue_description
,
1621 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1622 this->subexpressions
[0]->get_location());
1624 error_emitted
= op
[0]->type
->is_error();
1628 case ast_field_selection
:
1629 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1632 case ast_array_index
: {
1633 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1635 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1636 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1638 result
= _mesa_ast_array_index_to_hir(ctx
, state
, op
[0], op
[1],
1641 if (result
->type
->is_error())
1642 error_emitted
= true;
1647 case ast_function_call
:
1648 /* Should *NEVER* get here. ast_function_call should always be handled
1649 * by ast_function_expression::hir.
1654 case ast_identifier
: {
1655 /* ast_identifier can appear several places in a full abstract syntax
1656 * tree. This particular use must be at location specified in the grammar
1657 * as 'variable_identifier'.
1660 state
->symbols
->get_variable(this->primary_expression
.identifier
);
1663 var
->data
.used
= true;
1664 result
= new(ctx
) ir_dereference_variable(var
);
1666 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
1667 this->primary_expression
.identifier
);
1669 result
= ir_rvalue::error_value(ctx
);
1670 error_emitted
= true;
1675 case ast_int_constant
:
1676 result
= new(ctx
) ir_constant(this->primary_expression
.int_constant
);
1679 case ast_uint_constant
:
1680 result
= new(ctx
) ir_constant(this->primary_expression
.uint_constant
);
1683 case ast_float_constant
:
1684 result
= new(ctx
) ir_constant(this->primary_expression
.float_constant
);
1687 case ast_bool_constant
:
1688 result
= new(ctx
) ir_constant(bool(this->primary_expression
.bool_constant
));
1691 case ast_sequence
: {
1692 /* It should not be possible to generate a sequence in the AST without
1693 * any expressions in it.
1695 assert(!this->expressions
.is_empty());
1697 /* The r-value of a sequence is the last expression in the sequence. If
1698 * the other expressions in the sequence do not have side-effects (and
1699 * therefore add instructions to the instruction list), they get dropped
1702 exec_node
*previous_tail_pred
= NULL
;
1703 YYLTYPE previous_operand_loc
= loc
;
1705 foreach_list_typed (ast_node
, ast
, link
, &this->expressions
) {
1706 /* If one of the operands of comma operator does not generate any
1707 * code, we want to emit a warning. At each pass through the loop
1708 * previous_tail_pred will point to the last instruction in the
1709 * stream *before* processing the previous operand. Naturally,
1710 * instructions->tail_pred will point to the last instruction in the
1711 * stream *after* processing the previous operand. If the two
1712 * pointers match, then the previous operand had no effect.
1714 * The warning behavior here differs slightly from GCC. GCC will
1715 * only emit a warning if none of the left-hand operands have an
1716 * effect. However, it will emit a warning for each. I believe that
1717 * there are some cases in C (especially with GCC extensions) where
1718 * it is useful to have an intermediate step in a sequence have no
1719 * effect, but I don't think these cases exist in GLSL. Either way,
1720 * it would be a giant hassle to replicate that behavior.
1722 if (previous_tail_pred
== instructions
->tail_pred
) {
1723 _mesa_glsl_warning(&previous_operand_loc
, state
,
1724 "left-hand operand of comma expression has "
1728 /* tail_pred is directly accessed instead of using the get_tail()
1729 * method for performance reasons. get_tail() has extra code to
1730 * return NULL when the list is empty. We don't care about that
1731 * here, so using tail_pred directly is fine.
1733 previous_tail_pred
= instructions
->tail_pred
;
1734 previous_operand_loc
= ast
->get_location();
1736 result
= ast
->hir(instructions
, state
);
1739 /* Any errors should have already been emitted in the loop above.
1741 error_emitted
= true;
1745 type
= NULL
; /* use result->type, not type. */
1746 assert(result
!= NULL
);
1748 if (result
->type
->is_error() && !error_emitted
)
1749 _mesa_glsl_error(& loc
, state
, "type mismatch");
1756 ast_expression_statement::hir(exec_list
*instructions
,
1757 struct _mesa_glsl_parse_state
*state
)
1759 /* It is possible to have expression statements that don't have an
1760 * expression. This is the solitary semicolon:
1762 * for (i = 0; i < 5; i++)
1765 * In this case the expression will be NULL. Test for NULL and don't do
1766 * anything in that case.
1768 if (expression
!= NULL
)
1769 expression
->hir(instructions
, state
);
1771 /* Statements do not have r-values.
1778 ast_compound_statement::hir(exec_list
*instructions
,
1779 struct _mesa_glsl_parse_state
*state
)
1782 state
->symbols
->push_scope();
1784 foreach_list_typed (ast_node
, ast
, link
, &this->statements
)
1785 ast
->hir(instructions
, state
);
1788 state
->symbols
->pop_scope();
1790 /* Compound statements do not have r-values.
1796 * Evaluate the given exec_node (which should be an ast_node representing
1797 * a single array dimension) and return its integer value.
1799 static const unsigned
1800 process_array_size(exec_node
*node
,
1801 struct _mesa_glsl_parse_state
*state
)
1803 exec_list dummy_instructions
;
1805 ast_node
*array_size
= exec_node_data(ast_node
, node
, link
);
1806 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
,
1808 YYLTYPE loc
= array_size
->get_location();
1811 _mesa_glsl_error(& loc
, state
,
1812 "array size could not be resolved");
1816 if (!ir
->type
->is_integer()) {
1817 _mesa_glsl_error(& loc
, state
,
1818 "array size must be integer type");
1822 if (!ir
->type
->is_scalar()) {
1823 _mesa_glsl_error(& loc
, state
,
1824 "array size must be scalar type");
1828 ir_constant
*const size
= ir
->constant_expression_value();
1830 _mesa_glsl_error(& loc
, state
, "array size must be a "
1831 "constant valued expression");
1835 if (size
->value
.i
[0] <= 0) {
1836 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
1840 assert(size
->type
== ir
->type
);
1842 /* If the array size is const (and we've verified that
1843 * it is) then no instructions should have been emitted
1844 * when we converted it to HIR. If they were emitted,
1845 * then either the array size isn't const after all, or
1846 * we are emitting unnecessary instructions.
1848 assert(dummy_instructions
.is_empty());
1850 return size
->value
.u
[0];
1853 static const glsl_type
*
1854 process_array_type(YYLTYPE
*loc
, const glsl_type
*base
,
1855 ast_array_specifier
*array_specifier
,
1856 struct _mesa_glsl_parse_state
*state
)
1858 const glsl_type
*array_type
= base
;
1860 if (array_specifier
!= NULL
) {
1861 if (base
->is_array()) {
1863 /* From page 19 (page 25) of the GLSL 1.20 spec:
1865 * "Only one-dimensional arrays may be declared."
1867 if (!state
->ARB_arrays_of_arrays_enable
) {
1868 _mesa_glsl_error(loc
, state
,
1869 "invalid array of `%s'"
1870 "GL_ARB_arrays_of_arrays "
1871 "required for defining arrays of arrays",
1873 return glsl_type::error_type
;
1876 if (base
->length
== 0) {
1877 _mesa_glsl_error(loc
, state
,
1878 "only the outermost array dimension can "
1881 return glsl_type::error_type
;
1885 for (exec_node
*node
= array_specifier
->array_dimensions
.tail_pred
;
1886 !node
->is_head_sentinel(); node
= node
->prev
) {
1887 unsigned array_size
= process_array_size(node
, state
);
1888 array_type
= glsl_type::get_array_instance(array_type
,
1892 if (array_specifier
->is_unsized_array
)
1893 array_type
= glsl_type::get_array_instance(array_type
, 0);
1901 ast_type_specifier::glsl_type(const char **name
,
1902 struct _mesa_glsl_parse_state
*state
) const
1904 const struct glsl_type
*type
;
1906 type
= state
->symbols
->get_type(this->type_name
);
1907 *name
= this->type_name
;
1909 YYLTYPE loc
= this->get_location();
1910 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
1916 ast_fully_specified_type::glsl_type(const char **name
,
1917 struct _mesa_glsl_parse_state
*state
) const
1919 const struct glsl_type
*type
= this->specifier
->glsl_type(name
, state
);
1924 if (type
->base_type
== GLSL_TYPE_FLOAT
1926 && state
->stage
== MESA_SHADER_FRAGMENT
1927 && this->qualifier
.precision
== ast_precision_none
1928 && state
->symbols
->get_variable("#default precision") == NULL
) {
1929 YYLTYPE loc
= this->get_location();
1930 _mesa_glsl_error(&loc
, state
,
1931 "no precision specified this scope for type `%s'",
1939 * Determine whether a toplevel variable declaration declares a varying. This
1940 * function operates by examining the variable's mode and the shader target,
1941 * so it correctly identifies linkage variables regardless of whether they are
1942 * declared using the deprecated "varying" syntax or the new "in/out" syntax.
1944 * Passing a non-toplevel variable declaration (e.g. a function parameter) to
1945 * this function will produce undefined results.
1948 is_varying_var(ir_variable
*var
, gl_shader_stage target
)
1951 case MESA_SHADER_VERTEX
:
1952 return var
->data
.mode
== ir_var_shader_out
;
1953 case MESA_SHADER_FRAGMENT
:
1954 return var
->data
.mode
== ir_var_shader_in
;
1956 return var
->data
.mode
== ir_var_shader_out
|| var
->data
.mode
== ir_var_shader_in
;
1962 * Matrix layout qualifiers are only allowed on certain types
1965 validate_matrix_layout_for_type(struct _mesa_glsl_parse_state
*state
,
1967 const glsl_type
*type
,
1970 if (var
&& !var
->is_in_uniform_block()) {
1971 /* Layout qualifiers may only apply to interface blocks and fields in
1974 _mesa_glsl_error(loc
, state
,
1975 "uniform block layout qualifiers row_major and "
1976 "column_major may not be applied to variables "
1977 "outside of uniform blocks");
1978 } else if (!type
->is_matrix()) {
1979 /* The OpenGL ES 3.0 conformance tests did not originally allow
1980 * matrix layout qualifiers on non-matrices. However, the OpenGL
1981 * 4.4 and OpenGL ES 3.0 (revision TBD) specifications were
1982 * amended to specifically allow these layouts on all types. Emit
1983 * a warning so that people know their code may not be portable.
1985 _mesa_glsl_warning(loc
, state
,
1986 "uniform block layout qualifiers row_major and "
1987 "column_major applied to non-matrix types may "
1988 "be rejected by older compilers");
1989 } else if (type
->is_record()) {
1990 /* We allow 'layout(row_major)' on structure types because it's the only
1991 * way to get row-major layouts on matrices contained in structures.
1993 _mesa_glsl_warning(loc
, state
,
1994 "uniform block layout qualifiers row_major and "
1995 "column_major applied to structure types is not "
1996 "strictly conformant and may be rejected by other "
2002 validate_binding_qualifier(struct _mesa_glsl_parse_state
*state
,
2005 const ast_type_qualifier
*qual
)
2007 if (var
->data
.mode
!= ir_var_uniform
) {
2008 _mesa_glsl_error(loc
, state
,
2009 "the \"binding\" qualifier only applies to uniforms");
2013 if (qual
->binding
< 0) {
2014 _mesa_glsl_error(loc
, state
, "binding values must be >= 0");
2018 const struct gl_context
*const ctx
= state
->ctx
;
2019 unsigned elements
= var
->type
->is_array() ? var
->type
->length
: 1;
2020 unsigned max_index
= qual
->binding
+ elements
- 1;
2022 if (var
->type
->is_interface()) {
2023 /* UBOs. From page 60 of the GLSL 4.20 specification:
2024 * "If the binding point for any uniform block instance is less than zero,
2025 * or greater than or equal to the implementation-dependent maximum
2026 * number of uniform buffer bindings, a compilation error will occur.
2027 * When the binding identifier is used with a uniform block instanced as
2028 * an array of size N, all elements of the array from binding through
2029 * binding + N – 1 must be within this range."
2031 * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
2033 if (max_index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2034 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d UBOs exceeds "
2035 "the maximum number of UBO binding points (%d)",
2036 qual
->binding
, elements
,
2037 ctx
->Const
.MaxUniformBufferBindings
);
2040 } else if (var
->type
->is_sampler() ||
2041 (var
->type
->is_array() && var
->type
->fields
.array
->is_sampler())) {
2042 /* Samplers. From page 63 of the GLSL 4.20 specification:
2043 * "If the binding is less than zero, or greater than or equal to the
2044 * implementation-dependent maximum supported number of units, a
2045 * compilation error will occur. When the binding identifier is used
2046 * with an array of size N, all elements of the array from binding
2047 * through binding + N - 1 must be within this range."
2049 unsigned limit
= ctx
->Const
.Program
[state
->stage
].MaxTextureImageUnits
;
2051 if (max_index
>= limit
) {
2052 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d samplers "
2053 "exceeds the maximum number of texture image units "
2054 "(%d)", qual
->binding
, elements
, limit
);
2058 } else if (var
->type
->contains_atomic()) {
2059 assert(ctx
->Const
.MaxAtomicBufferBindings
<= MAX_COMBINED_ATOMIC_BUFFERS
);
2060 if (unsigned(qual
->binding
) >= ctx
->Const
.MaxAtomicBufferBindings
) {
2061 _mesa_glsl_error(loc
, state
, "layout(binding = %d) exceeds the "
2062 " maximum number of atomic counter buffer bindings"
2063 "(%d)", qual
->binding
,
2064 ctx
->Const
.MaxAtomicBufferBindings
);
2069 _mesa_glsl_error(loc
, state
,
2070 "the \"binding\" qualifier only applies to uniform "
2071 "blocks, samplers, atomic counters, or arrays thereof");
2079 static glsl_interp_qualifier
2080 interpret_interpolation_qualifier(const struct ast_type_qualifier
*qual
,
2081 ir_variable_mode mode
,
2082 struct _mesa_glsl_parse_state
*state
,
2085 glsl_interp_qualifier interpolation
;
2086 if (qual
->flags
.q
.flat
)
2087 interpolation
= INTERP_QUALIFIER_FLAT
;
2088 else if (qual
->flags
.q
.noperspective
)
2089 interpolation
= INTERP_QUALIFIER_NOPERSPECTIVE
;
2090 else if (qual
->flags
.q
.smooth
)
2091 interpolation
= INTERP_QUALIFIER_SMOOTH
;
2093 interpolation
= INTERP_QUALIFIER_NONE
;
2095 if (interpolation
!= INTERP_QUALIFIER_NONE
) {
2096 if (mode
!= ir_var_shader_in
&& mode
!= ir_var_shader_out
) {
2097 _mesa_glsl_error(loc
, state
,
2098 "interpolation qualifier `%s' can only be applied to "
2099 "shader inputs or outputs.",
2100 interpolation_string(interpolation
));
2104 if ((state
->stage
== MESA_SHADER_VERTEX
&& mode
== ir_var_shader_in
) ||
2105 (state
->stage
== MESA_SHADER_FRAGMENT
&& mode
== ir_var_shader_out
)) {
2106 _mesa_glsl_error(loc
, state
,
2107 "interpolation qualifier `%s' cannot be applied to "
2108 "vertex shader inputs or fragment shader outputs",
2109 interpolation_string(interpolation
));
2113 return interpolation
;
2118 validate_explicit_location(const struct ast_type_qualifier
*qual
,
2120 struct _mesa_glsl_parse_state
*state
,
2125 /* In the vertex shader only shader inputs can be given explicit
2128 * In the fragment shader only shader outputs can be given explicit
2131 switch (state
->stage
) {
2132 case MESA_SHADER_VERTEX
:
2133 if (var
->data
.mode
== ir_var_shader_in
) {
2134 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
2143 case MESA_SHADER_GEOMETRY
:
2144 _mesa_glsl_error(loc
, state
,
2145 "geometry shader variables cannot be given "
2146 "explicit locations");
2149 case MESA_SHADER_FRAGMENT
:
2150 if (var
->data
.mode
== ir_var_shader_out
) {
2151 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
2160 case MESA_SHADER_COMPUTE
:
2161 _mesa_glsl_error(loc
, state
,
2162 "compute shader variables cannot be given "
2163 "explicit locations");
2168 _mesa_glsl_error(loc
, state
,
2169 "%s cannot be given an explicit location in %s shader",
2171 _mesa_shader_stage_to_string(state
->stage
));
2173 var
->data
.explicit_location
= true;
2175 /* This bit of silliness is needed because invalid explicit locations
2176 * are supposed to be flagged during linking. Small negative values
2177 * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
2178 * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
2179 * The linker needs to be able to differentiate these cases. This
2180 * ensures that negative values stay negative.
2182 if (qual
->location
>= 0) {
2183 var
->data
.location
= (state
->stage
== MESA_SHADER_VERTEX
)
2184 ? (qual
->location
+ VERT_ATTRIB_GENERIC0
)
2185 : (qual
->location
+ FRAG_RESULT_DATA0
);
2187 var
->data
.location
= qual
->location
;
2190 if (qual
->flags
.q
.explicit_index
) {
2191 /* From the GLSL 4.30 specification, section 4.4.2 (Output
2192 * Layout Qualifiers):
2194 * "It is also a compile-time error if a fragment shader
2195 * sets a layout index to less than 0 or greater than 1."
2197 * Older specifications don't mandate a behavior; we take
2198 * this as a clarification and always generate the error.
2200 if (qual
->index
< 0 || qual
->index
> 1) {
2201 _mesa_glsl_error(loc
, state
,
2202 "explicit index may only be 0 or 1");
2204 var
->data
.explicit_index
= true;
2205 var
->data
.index
= qual
->index
;
2214 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
2216 struct _mesa_glsl_parse_state
*state
,
2220 STATIC_ASSERT(sizeof(qual
->flags
.q
) <= sizeof(qual
->flags
.i
));
2222 if (qual
->flags
.q
.invariant
) {
2223 if (var
->data
.used
) {
2224 _mesa_glsl_error(loc
, state
,
2225 "variable `%s' may not be redeclared "
2226 "`invariant' after being used",
2229 var
->data
.invariant
= 1;
2233 if (qual
->flags
.q
.constant
|| qual
->flags
.q
.attribute
2234 || qual
->flags
.q
.uniform
2235 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
2236 var
->data
.read_only
= 1;
2238 if (qual
->flags
.q
.centroid
)
2239 var
->data
.centroid
= 1;
2241 if (qual
->flags
.q
.sample
)
2242 var
->data
.sample
= 1;
2244 if (qual
->flags
.q
.attribute
&& state
->stage
!= MESA_SHADER_VERTEX
) {
2245 var
->type
= glsl_type::error_type
;
2246 _mesa_glsl_error(loc
, state
,
2247 "`attribute' variables may not be declared in the "
2249 _mesa_shader_stage_to_string(state
->stage
));
2252 /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
2254 * "However, the const qualifier cannot be used with out or inout."
2256 * The same section of the GLSL 4.40 spec further clarifies this saying:
2258 * "The const qualifier cannot be used with out or inout, or a
2259 * compile-time error results."
2261 if (is_parameter
&& qual
->flags
.q
.constant
&& qual
->flags
.q
.out
) {
2262 _mesa_glsl_error(loc
, state
,
2263 "`const' may not be applied to `out' or `inout' "
2264 "function parameters");
2267 /* If there is no qualifier that changes the mode of the variable, leave
2268 * the setting alone.
2270 if (qual
->flags
.q
.in
&& qual
->flags
.q
.out
)
2271 var
->data
.mode
= ir_var_function_inout
;
2272 else if (qual
->flags
.q
.in
)
2273 var
->data
.mode
= is_parameter
? ir_var_function_in
: ir_var_shader_in
;
2274 else if (qual
->flags
.q
.attribute
2275 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
2276 var
->data
.mode
= ir_var_shader_in
;
2277 else if (qual
->flags
.q
.out
)
2278 var
->data
.mode
= is_parameter
? ir_var_function_out
: ir_var_shader_out
;
2279 else if (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_VERTEX
))
2280 var
->data
.mode
= ir_var_shader_out
;
2281 else if (qual
->flags
.q
.uniform
)
2282 var
->data
.mode
= ir_var_uniform
;
2284 if (!is_parameter
&& is_varying_var(var
, state
->stage
)) {
2285 /* User-defined ins/outs are not permitted in compute shaders. */
2286 if (state
->stage
== MESA_SHADER_COMPUTE
) {
2287 _mesa_glsl_error(loc
, state
,
2288 "user-defined input and output variables are not "
2289 "permitted in compute shaders");
2292 /* This variable is being used to link data between shader stages (in
2293 * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
2294 * that is allowed for such purposes.
2296 * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
2298 * "The varying qualifier can be used only with the data types
2299 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
2302 * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
2303 * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
2305 * "Fragment inputs can only be signed and unsigned integers and
2306 * integer vectors, float, floating-point vectors, matrices, or
2307 * arrays of these. Structures cannot be input.
2309 * Similar text exists in the section on vertex shader outputs.
2311 * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
2312 * 3.00 spec allows structs as well. Varying structs are also allowed
2315 switch (var
->type
->get_scalar_type()->base_type
) {
2316 case GLSL_TYPE_FLOAT
:
2317 /* Ok in all GLSL versions */
2319 case GLSL_TYPE_UINT
:
2321 if (state
->is_version(130, 300))
2323 _mesa_glsl_error(loc
, state
,
2324 "varying variables must be of base type float in %s",
2325 state
->get_version_string());
2327 case GLSL_TYPE_STRUCT
:
2328 if (state
->is_version(150, 300))
2330 _mesa_glsl_error(loc
, state
,
2331 "varying variables may not be of type struct");
2334 _mesa_glsl_error(loc
, state
, "illegal type for a varying variable");
2339 if (state
->all_invariant
&& (state
->current_function
== NULL
)) {
2340 switch (state
->stage
) {
2341 case MESA_SHADER_VERTEX
:
2342 if (var
->data
.mode
== ir_var_shader_out
)
2343 var
->data
.invariant
= true;
2345 case MESA_SHADER_GEOMETRY
:
2346 if ((var
->data
.mode
== ir_var_shader_in
)
2347 || (var
->data
.mode
== ir_var_shader_out
))
2348 var
->data
.invariant
= true;
2350 case MESA_SHADER_FRAGMENT
:
2351 if (var
->data
.mode
== ir_var_shader_in
)
2352 var
->data
.invariant
= true;
2354 case MESA_SHADER_COMPUTE
:
2355 /* Invariance isn't meaningful in compute shaders. */
2360 var
->data
.interpolation
=
2361 interpret_interpolation_qualifier(qual
, (ir_variable_mode
) var
->data
.mode
,
2364 var
->data
.pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
2365 var
->data
.origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
2366 if ((qual
->flags
.q
.origin_upper_left
|| qual
->flags
.q
.pixel_center_integer
)
2367 && (strcmp(var
->name
, "gl_FragCoord") != 0)) {
2368 const char *const qual_string
= (qual
->flags
.q
.origin_upper_left
)
2369 ? "origin_upper_left" : "pixel_center_integer";
2371 _mesa_glsl_error(loc
, state
,
2372 "layout qualifier `%s' can only be applied to "
2373 "fragment shader input `gl_FragCoord'",
2377 if (qual
->flags
.q
.explicit_location
) {
2378 validate_explicit_location(qual
, var
, state
, loc
);
2379 } else if (qual
->flags
.q
.explicit_index
) {
2380 _mesa_glsl_error(loc
, state
,
2381 "explicit index requires explicit location");
2384 if (qual
->flags
.q
.explicit_binding
&&
2385 validate_binding_qualifier(state
, loc
, var
, qual
)) {
2386 var
->data
.explicit_binding
= true;
2387 var
->data
.binding
= qual
->binding
;
2390 if (var
->type
->contains_atomic()) {
2391 if (var
->data
.mode
== ir_var_uniform
) {
2392 if (var
->data
.explicit_binding
) {
2394 &state
->atomic_counter_offsets
[var
->data
.binding
];
2396 if (*offset
% ATOMIC_COUNTER_SIZE
)
2397 _mesa_glsl_error(loc
, state
,
2398 "misaligned atomic counter offset");
2400 var
->data
.atomic
.offset
= *offset
;
2401 *offset
+= var
->type
->atomic_size();
2404 _mesa_glsl_error(loc
, state
,
2405 "atomic counters require explicit binding point");
2407 } else if (var
->data
.mode
!= ir_var_function_in
) {
2408 _mesa_glsl_error(loc
, state
, "atomic counters may only be declared as "
2409 "function parameters or uniform-qualified "
2410 "global variables");
2414 /* Does the declaration use the deprecated 'attribute' or 'varying'
2417 const bool uses_deprecated_qualifier
= qual
->flags
.q
.attribute
2418 || qual
->flags
.q
.varying
;
2420 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
2421 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
2422 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
2423 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
2424 * These extensions and all following extensions that add the 'layout'
2425 * keyword have been modified to require the use of 'in' or 'out'.
2427 * The following extension do not allow the deprecated keywords:
2429 * GL_AMD_conservative_depth
2430 * GL_ARB_conservative_depth
2431 * GL_ARB_gpu_shader5
2432 * GL_ARB_separate_shader_objects
2433 * GL_ARB_tesselation_shader
2434 * GL_ARB_transform_feedback3
2435 * GL_ARB_uniform_buffer_object
2437 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
2438 * allow layout with the deprecated keywords.
2440 const bool relaxed_layout_qualifier_checking
=
2441 state
->ARB_fragment_coord_conventions_enable
;
2443 if (qual
->has_layout() && uses_deprecated_qualifier
) {
2444 if (relaxed_layout_qualifier_checking
) {
2445 _mesa_glsl_warning(loc
, state
,
2446 "`layout' qualifier may not be used with "
2447 "`attribute' or `varying'");
2449 _mesa_glsl_error(loc
, state
,
2450 "`layout' qualifier may not be used with "
2451 "`attribute' or `varying'");
2455 /* Layout qualifiers for gl_FragDepth, which are enabled by extension
2456 * AMD_conservative_depth.
2458 int depth_layout_count
= qual
->flags
.q
.depth_any
2459 + qual
->flags
.q
.depth_greater
2460 + qual
->flags
.q
.depth_less
2461 + qual
->flags
.q
.depth_unchanged
;
2462 if (depth_layout_count
> 0
2463 && !state
->AMD_conservative_depth_enable
2464 && !state
->ARB_conservative_depth_enable
) {
2465 _mesa_glsl_error(loc
, state
,
2466 "extension GL_AMD_conservative_depth or "
2467 "GL_ARB_conservative_depth must be enabled "
2468 "to use depth layout qualifiers");
2469 } else if (depth_layout_count
> 0
2470 && strcmp(var
->name
, "gl_FragDepth") != 0) {
2471 _mesa_glsl_error(loc
, state
,
2472 "depth layout qualifiers can be applied only to "
2474 } else if (depth_layout_count
> 1
2475 && strcmp(var
->name
, "gl_FragDepth") == 0) {
2476 _mesa_glsl_error(loc
, state
,
2477 "at most one depth layout qualifier can be applied to "
2480 if (qual
->flags
.q
.depth_any
)
2481 var
->data
.depth_layout
= ir_depth_layout_any
;
2482 else if (qual
->flags
.q
.depth_greater
)
2483 var
->data
.depth_layout
= ir_depth_layout_greater
;
2484 else if (qual
->flags
.q
.depth_less
)
2485 var
->data
.depth_layout
= ir_depth_layout_less
;
2486 else if (qual
->flags
.q
.depth_unchanged
)
2487 var
->data
.depth_layout
= ir_depth_layout_unchanged
;
2489 var
->data
.depth_layout
= ir_depth_layout_none
;
2491 if (qual
->flags
.q
.std140
||
2492 qual
->flags
.q
.packed
||
2493 qual
->flags
.q
.shared
) {
2494 _mesa_glsl_error(loc
, state
,
2495 "uniform block layout qualifiers std140, packed, and "
2496 "shared can only be applied to uniform blocks, not "
2500 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
2501 validate_matrix_layout_for_type(state
, loc
, var
->type
, var
);
2506 * Get the variable that is being redeclared by this declaration
2508 * Semantic checks to verify the validity of the redeclaration are also
2509 * performed. If semantic checks fail, compilation error will be emitted via
2510 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
2513 * A pointer to an existing variable in the current scope if the declaration
2514 * is a redeclaration, \c NULL otherwise.
2516 static ir_variable
*
2517 get_variable_being_redeclared(ir_variable
*var
, YYLTYPE loc
,
2518 struct _mesa_glsl_parse_state
*state
,
2519 bool allow_all_redeclarations
)
2521 /* Check if this declaration is actually a re-declaration, either to
2522 * resize an array or add qualifiers to an existing variable.
2524 * This is allowed for variables in the current scope, or when at
2525 * global scope (for built-ins in the implicit outer scope).
2527 ir_variable
*earlier
= state
->symbols
->get_variable(var
->name
);
2528 if (earlier
== NULL
||
2529 (state
->current_function
!= NULL
&&
2530 !state
->symbols
->name_declared_this_scope(var
->name
))) {
2535 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
2537 * "It is legal to declare an array without a size and then
2538 * later re-declare the same name as an array of the same
2539 * type and specify a size."
2541 if (earlier
->type
->is_unsized_array() && var
->type
->is_array()
2542 && (var
->type
->element_type() == earlier
->type
->element_type())) {
2543 /* FINISHME: This doesn't match the qualifiers on the two
2544 * FINISHME: declarations. It's not 100% clear whether this is
2545 * FINISHME: required or not.
2548 const unsigned size
= unsigned(var
->type
->array_size());
2549 check_builtin_array_max_size(var
->name
, size
, loc
, state
);
2550 if ((size
> 0) && (size
<= earlier
->data
.max_array_access
)) {
2551 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
2553 earlier
->data
.max_array_access
);
2556 earlier
->type
= var
->type
;
2559 } else if ((state
->ARB_fragment_coord_conventions_enable
||
2560 state
->is_version(150, 0))
2561 && strcmp(var
->name
, "gl_FragCoord") == 0
2562 && earlier
->type
== var
->type
2563 && earlier
->data
.mode
== var
->data
.mode
) {
2564 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
2567 earlier
->data
.origin_upper_left
= var
->data
.origin_upper_left
;
2568 earlier
->data
.pixel_center_integer
= var
->data
.pixel_center_integer
;
2570 /* According to section 4.3.7 of the GLSL 1.30 spec,
2571 * the following built-in varaibles can be redeclared with an
2572 * interpolation qualifier:
2575 * * gl_FrontSecondaryColor
2576 * * gl_BackSecondaryColor
2578 * * gl_SecondaryColor
2580 } else if (state
->is_version(130, 0)
2581 && (strcmp(var
->name
, "gl_FrontColor") == 0
2582 || strcmp(var
->name
, "gl_BackColor") == 0
2583 || strcmp(var
->name
, "gl_FrontSecondaryColor") == 0
2584 || strcmp(var
->name
, "gl_BackSecondaryColor") == 0
2585 || strcmp(var
->name
, "gl_Color") == 0
2586 || strcmp(var
->name
, "gl_SecondaryColor") == 0)
2587 && earlier
->type
== var
->type
2588 && earlier
->data
.mode
== var
->data
.mode
) {
2589 earlier
->data
.interpolation
= var
->data
.interpolation
;
2591 /* Layout qualifiers for gl_FragDepth. */
2592 } else if ((state
->AMD_conservative_depth_enable
||
2593 state
->ARB_conservative_depth_enable
)
2594 && strcmp(var
->name
, "gl_FragDepth") == 0
2595 && earlier
->type
== var
->type
2596 && earlier
->data
.mode
== var
->data
.mode
) {
2598 /** From the AMD_conservative_depth spec:
2599 * Within any shader, the first redeclarations of gl_FragDepth
2600 * must appear before any use of gl_FragDepth.
2602 if (earlier
->data
.used
) {
2603 _mesa_glsl_error(&loc
, state
,
2604 "the first redeclaration of gl_FragDepth "
2605 "must appear before any use of gl_FragDepth");
2608 /* Prevent inconsistent redeclaration of depth layout qualifier. */
2609 if (earlier
->data
.depth_layout
!= ir_depth_layout_none
2610 && earlier
->data
.depth_layout
!= var
->data
.depth_layout
) {
2611 _mesa_glsl_error(&loc
, state
,
2612 "gl_FragDepth: depth layout is declared here "
2613 "as '%s, but it was previously declared as "
2615 depth_layout_string(var
->data
.depth_layout
),
2616 depth_layout_string(earlier
->data
.depth_layout
));
2619 earlier
->data
.depth_layout
= var
->data
.depth_layout
;
2621 } else if (allow_all_redeclarations
) {
2622 if (earlier
->data
.mode
!= var
->data
.mode
) {
2623 _mesa_glsl_error(&loc
, state
,
2624 "redeclaration of `%s' with incorrect qualifiers",
2626 } else if (earlier
->type
!= var
->type
) {
2627 _mesa_glsl_error(&loc
, state
,
2628 "redeclaration of `%s' has incorrect type",
2632 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
2639 * Generate the IR for an initializer in a variable declaration
2642 process_initializer(ir_variable
*var
, ast_declaration
*decl
,
2643 ast_fully_specified_type
*type
,
2644 exec_list
*initializer_instructions
,
2645 struct _mesa_glsl_parse_state
*state
)
2647 ir_rvalue
*result
= NULL
;
2649 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
2651 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
2653 * "All uniform variables are read-only and are initialized either
2654 * directly by an application via API commands, or indirectly by
2657 if (var
->data
.mode
== ir_var_uniform
) {
2658 state
->check_version(120, 0, &initializer_loc
,
2659 "cannot initialize uniforms");
2662 if (var
->type
->is_sampler()) {
2663 _mesa_glsl_error(& initializer_loc
, state
,
2664 "cannot initialize samplers");
2667 if ((var
->data
.mode
== ir_var_shader_in
) && (state
->current_function
== NULL
)) {
2668 _mesa_glsl_error(& initializer_loc
, state
,
2669 "cannot initialize %s shader input / %s",
2670 _mesa_shader_stage_to_string(state
->stage
),
2671 (state
->stage
== MESA_SHADER_VERTEX
)
2672 ? "attribute" : "varying");
2675 /* If the initializer is an ast_aggregate_initializer, recursively store
2676 * type information from the LHS into it, so that its hir() function can do
2679 if (decl
->initializer
->oper
== ast_aggregate
)
2680 _mesa_ast_set_aggregate_type(var
->type
, decl
->initializer
);
2682 ir_dereference
*const lhs
= new(state
) ir_dereference_variable(var
);
2683 ir_rvalue
*rhs
= decl
->initializer
->hir(initializer_instructions
,
2686 /* Calculate the constant value if this is a const or uniform
2689 if (type
->qualifier
.flags
.q
.constant
2690 || type
->qualifier
.flags
.q
.uniform
) {
2691 ir_rvalue
*new_rhs
= validate_assignment(state
, initializer_loc
,
2692 var
->type
, rhs
, true);
2693 if (new_rhs
!= NULL
) {
2696 ir_constant
*constant_value
= rhs
->constant_expression_value();
2697 if (!constant_value
) {
2698 /* If ARB_shading_language_420pack is enabled, initializers of
2699 * const-qualified local variables do not have to be constant
2700 * expressions. Const-qualified global variables must still be
2701 * initialized with constant expressions.
2703 if (!state
->ARB_shading_language_420pack_enable
2704 || state
->current_function
== NULL
) {
2705 _mesa_glsl_error(& initializer_loc
, state
,
2706 "initializer of %s variable `%s' must be a "
2707 "constant expression",
2708 (type
->qualifier
.flags
.q
.constant
)
2709 ? "const" : "uniform",
2711 if (var
->type
->is_numeric()) {
2712 /* Reduce cascading errors. */
2713 var
->constant_value
= ir_constant::zero(state
, var
->type
);
2717 rhs
= constant_value
;
2718 var
->constant_value
= constant_value
;
2721 if (var
->type
->is_numeric()) {
2722 /* Reduce cascading errors. */
2723 var
->constant_value
= ir_constant::zero(state
, var
->type
);
2728 if (rhs
&& !rhs
->type
->is_error()) {
2729 bool temp
= var
->data
.read_only
;
2730 if (type
->qualifier
.flags
.q
.constant
)
2731 var
->data
.read_only
= false;
2733 /* Never emit code to initialize a uniform.
2735 const glsl_type
*initializer_type
;
2736 if (!type
->qualifier
.flags
.q
.uniform
) {
2737 result
= do_assignment(initializer_instructions
, state
,
2740 type
->get_location());
2741 initializer_type
= result
->type
;
2743 initializer_type
= rhs
->type
;
2745 var
->constant_initializer
= rhs
->constant_expression_value();
2746 var
->data
.has_initializer
= true;
2748 /* If the declared variable is an unsized array, it must inherrit
2749 * its full type from the initializer. A declaration such as
2751 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
2755 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
2757 * The assignment generated in the if-statement (below) will also
2758 * automatically handle this case for non-uniforms.
2760 * If the declared variable is not an array, the types must
2761 * already match exactly. As a result, the type assignment
2762 * here can be done unconditionally. For non-uniforms the call
2763 * to do_assignment can change the type of the initializer (via
2764 * the implicit conversion rules). For uniforms the initializer
2765 * must be a constant expression, and the type of that expression
2766 * was validated above.
2768 var
->type
= initializer_type
;
2770 var
->data
.read_only
= temp
;
2778 * Do additional processing necessary for geometry shader input declarations
2779 * (this covers both interface blocks arrays and bare input variables).
2782 handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
2783 YYLTYPE loc
, ir_variable
*var
)
2785 unsigned num_vertices
= 0;
2786 if (state
->gs_input_prim_type_specified
) {
2787 num_vertices
= vertices_per_prim(state
->gs_input_prim_type
);
2790 /* Geometry shader input variables must be arrays. Caller should have
2791 * reported an error for this.
2793 if (!var
->type
->is_array()) {
2794 assert(state
->error
);
2796 /* To avoid cascading failures, short circuit the checks below. */
2800 if (var
->type
->is_unsized_array()) {
2801 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says:
2803 * All geometry shader input unsized array declarations will be
2804 * sized by an earlier input layout qualifier, when present, as per
2805 * the following table.
2807 * Followed by a table mapping each allowed input layout qualifier to
2808 * the corresponding input length.
2810 if (num_vertices
!= 0)
2811 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
2814 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec
2815 * includes the following examples of compile-time errors:
2817 * // code sequence within one shader...
2818 * in vec4 Color1[]; // size unknown
2819 * ...Color1.length()...// illegal, length() unknown
2820 * in vec4 Color2[2]; // size is 2
2821 * ...Color1.length()...// illegal, Color1 still has no size
2822 * in vec4 Color3[3]; // illegal, input sizes are inconsistent
2823 * layout(lines) in; // legal, input size is 2, matching
2824 * in vec4 Color4[3]; // illegal, contradicts layout
2827 * To detect the case illustrated by Color3, we verify that the size of
2828 * an explicitly-sized array matches the size of any previously declared
2829 * explicitly-sized array. To detect the case illustrated by Color4, we
2830 * verify that the size of an explicitly-sized array is consistent with
2831 * any previously declared input layout.
2833 if (num_vertices
!= 0 && var
->type
->length
!= num_vertices
) {
2834 _mesa_glsl_error(&loc
, state
,
2835 "geometry shader input size contradicts previously"
2836 " declared layout (size is %u, but layout requires a"
2837 " size of %u)", var
->type
->length
, num_vertices
);
2838 } else if (state
->gs_input_size
!= 0 &&
2839 var
->type
->length
!= state
->gs_input_size
) {
2840 _mesa_glsl_error(&loc
, state
,
2841 "geometry shader input sizes are "
2842 "inconsistent (size is %u, but a previous "
2843 "declaration has size %u)",
2844 var
->type
->length
, state
->gs_input_size
);
2846 state
->gs_input_size
= var
->type
->length
;
2853 validate_identifier(const char *identifier
, YYLTYPE loc
,
2854 struct _mesa_glsl_parse_state
*state
)
2856 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
2858 * "Identifiers starting with "gl_" are reserved for use by
2859 * OpenGL, and may not be declared in a shader as either a
2860 * variable or a function."
2862 if (strncmp(identifier
, "gl_", 3) == 0) {
2863 _mesa_glsl_error(&loc
, state
,
2864 "identifier `%s' uses reserved `gl_' prefix",
2866 } else if (strstr(identifier
, "__")) {
2867 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
2870 * "In addition, all identifiers containing two
2871 * consecutive underscores (__) are reserved as
2872 * possible future keywords."
2874 _mesa_glsl_error(&loc
, state
,
2875 "identifier `%s' uses reserved `__' string",
2882 ast_declarator_list::hir(exec_list
*instructions
,
2883 struct _mesa_glsl_parse_state
*state
)
2886 const struct glsl_type
*decl_type
;
2887 const char *type_name
= NULL
;
2888 ir_rvalue
*result
= NULL
;
2889 YYLTYPE loc
= this->get_location();
2891 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
2893 * "To ensure that a particular output variable is invariant, it is
2894 * necessary to use the invariant qualifier. It can either be used to
2895 * qualify a previously declared variable as being invariant
2897 * invariant gl_Position; // make existing gl_Position be invariant"
2899 * In these cases the parser will set the 'invariant' flag in the declarator
2900 * list, and the type will be NULL.
2902 if (this->invariant
) {
2903 assert(this->type
== NULL
);
2905 if (state
->current_function
!= NULL
) {
2906 _mesa_glsl_error(& loc
, state
,
2907 "all uses of `invariant' keyword must be at global "
2911 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
2912 assert(decl
->array_specifier
== NULL
);
2913 assert(decl
->initializer
== NULL
);
2915 ir_variable
*const earlier
=
2916 state
->symbols
->get_variable(decl
->identifier
);
2917 if (earlier
== NULL
) {
2918 _mesa_glsl_error(& loc
, state
,
2919 "undeclared variable `%s' cannot be marked "
2920 "invariant", decl
->identifier
);
2921 } else if ((state
->stage
== MESA_SHADER_VERTEX
)
2922 && (earlier
->data
.mode
!= ir_var_shader_out
)) {
2923 _mesa_glsl_error(& loc
, state
,
2924 "`%s' cannot be marked invariant, vertex shader "
2925 "outputs only", decl
->identifier
);
2926 } else if ((state
->stage
== MESA_SHADER_FRAGMENT
)
2927 && (earlier
->data
.mode
!= ir_var_shader_in
)) {
2928 _mesa_glsl_error(& loc
, state
,
2929 "`%s' cannot be marked invariant, fragment shader "
2930 "inputs only", decl
->identifier
);
2931 } else if (earlier
->data
.used
) {
2932 _mesa_glsl_error(& loc
, state
,
2933 "variable `%s' may not be redeclared "
2934 "`invariant' after being used",
2937 earlier
->data
.invariant
= true;
2941 /* Invariant redeclarations do not have r-values.
2946 assert(this->type
!= NULL
);
2947 assert(!this->invariant
);
2949 /* The type specifier may contain a structure definition. Process that
2950 * before any of the variable declarations.
2952 (void) this->type
->specifier
->hir(instructions
, state
);
2954 decl_type
= this->type
->glsl_type(& type_name
, state
);
2956 /* An offset-qualified atomic counter declaration sets the default
2957 * offset for the next declaration within the same atomic counter
2960 if (decl_type
&& decl_type
->contains_atomic()) {
2961 if (type
->qualifier
.flags
.q
.explicit_binding
&&
2962 type
->qualifier
.flags
.q
.explicit_offset
)
2963 state
->atomic_counter_offsets
[type
->qualifier
.binding
] =
2964 type
->qualifier
.offset
;
2967 if (this->declarations
.is_empty()) {
2968 /* If there is no structure involved in the program text, there are two
2969 * possible scenarios:
2971 * - The program text contained something like 'vec4;'. This is an
2972 * empty declaration. It is valid but weird. Emit a warning.
2974 * - The program text contained something like 'S;' and 'S' is not the
2975 * name of a known structure type. This is both invalid and weird.
2978 * - The program text contained something like 'mediump float;'
2979 * when the programmer probably meant 'precision mediump
2980 * float;' Emit a warning with a description of what they
2981 * probably meant to do.
2983 * Note that if decl_type is NULL and there is a structure involved,
2984 * there must have been some sort of error with the structure. In this
2985 * case we assume that an error was already generated on this line of
2986 * code for the structure. There is no need to generate an additional,
2989 assert(this->type
->specifier
->structure
== NULL
|| decl_type
!= NULL
2992 if (decl_type
== NULL
) {
2993 _mesa_glsl_error(&loc
, state
,
2994 "invalid type `%s' in empty declaration",
2996 } else if (decl_type
->base_type
== GLSL_TYPE_ATOMIC_UINT
) {
2997 /* Empty atomic counter declarations are allowed and useful
2998 * to set the default offset qualifier.
3001 } else if (this->type
->qualifier
.precision
!= ast_precision_none
) {
3002 if (this->type
->specifier
->structure
!= NULL
) {
3003 _mesa_glsl_error(&loc
, state
,
3004 "precision qualifiers can't be applied "
3007 static const char *const precision_names
[] = {
3014 _mesa_glsl_warning(&loc
, state
,
3015 "empty declaration with precision qualifier, "
3016 "to set the default precision, use "
3017 "`precision %s %s;'",
3018 precision_names
[this->type
->qualifier
.precision
],
3021 } else if (this->type
->specifier
->structure
== NULL
) {
3022 _mesa_glsl_warning(&loc
, state
, "empty declaration");
3026 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
3027 const struct glsl_type
*var_type
;
3030 /* FINISHME: Emit a warning if a variable declaration shadows a
3031 * FINISHME: declaration at a higher scope.
3034 if ((decl_type
== NULL
) || decl_type
->is_void()) {
3035 if (type_name
!= NULL
) {
3036 _mesa_glsl_error(& loc
, state
,
3037 "invalid type `%s' in declaration of `%s'",
3038 type_name
, decl
->identifier
);
3040 _mesa_glsl_error(& loc
, state
,
3041 "invalid type in declaration of `%s'",
3047 var_type
= process_array_type(&loc
, decl_type
, decl
->array_specifier
,
3050 var
= new(ctx
) ir_variable(var_type
, decl
->identifier
, ir_var_auto
);
3052 /* The 'varying in' and 'varying out' qualifiers can only be used with
3053 * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
3056 if (this->type
->qualifier
.flags
.q
.varying
) {
3057 if (this->type
->qualifier
.flags
.q
.in
) {
3058 _mesa_glsl_error(& loc
, state
,
3059 "`varying in' qualifier in declaration of "
3060 "`%s' only valid for geometry shaders using "
3061 "ARB_geometry_shader4 or EXT_geometry_shader4",
3063 } else if (this->type
->qualifier
.flags
.q
.out
) {
3064 _mesa_glsl_error(& loc
, state
,
3065 "`varying out' qualifier in declaration of "
3066 "`%s' only valid for geometry shaders using "
3067 "ARB_geometry_shader4 or EXT_geometry_shader4",
3072 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
3074 * "Global variables can only use the qualifiers const,
3075 * attribute, uni form, or varying. Only one may be
3078 * Local variables can only use the qualifier const."
3080 * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
3081 * any extension that adds the 'layout' keyword.
3083 if (!state
->is_version(130, 300)
3084 && !state
->has_explicit_attrib_location()
3085 && !state
->ARB_fragment_coord_conventions_enable
) {
3086 if (this->type
->qualifier
.flags
.q
.out
) {
3087 _mesa_glsl_error(& loc
, state
,
3088 "`out' qualifier in declaration of `%s' "
3089 "only valid for function parameters in %s",
3090 decl
->identifier
, state
->get_version_string());
3092 if (this->type
->qualifier
.flags
.q
.in
) {
3093 _mesa_glsl_error(& loc
, state
,
3094 "`in' qualifier in declaration of `%s' "
3095 "only valid for function parameters in %s",
3096 decl
->identifier
, state
->get_version_string());
3098 /* FINISHME: Test for other invalid qualifiers. */
3101 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
3104 if (this->type
->qualifier
.flags
.q
.invariant
) {
3105 if ((state
->stage
== MESA_SHADER_VERTEX
) &&
3106 var
->data
.mode
!= ir_var_shader_out
) {
3107 _mesa_glsl_error(& loc
, state
,
3108 "`%s' cannot be marked invariant, vertex shader "
3109 "outputs only", var
->name
);
3110 } else if ((state
->stage
== MESA_SHADER_FRAGMENT
) &&
3111 var
->data
.mode
!= ir_var_shader_in
) {
3112 /* FINISHME: Note that this doesn't work for invariant on
3113 * a function signature inval
3115 _mesa_glsl_error(& loc
, state
,
3116 "`%s' cannot be marked invariant, fragment shader "
3117 "inputs only", var
->name
);
3121 if (state
->current_function
!= NULL
) {
3122 const char *mode
= NULL
;
3123 const char *extra
= "";
3125 /* There is no need to check for 'inout' here because the parser will
3126 * only allow that in function parameter lists.
3128 if (this->type
->qualifier
.flags
.q
.attribute
) {
3130 } else if (this->type
->qualifier
.flags
.q
.uniform
) {
3132 } else if (this->type
->qualifier
.flags
.q
.varying
) {
3134 } else if (this->type
->qualifier
.flags
.q
.in
) {
3136 extra
= " or in function parameter list";
3137 } else if (this->type
->qualifier
.flags
.q
.out
) {
3139 extra
= " or in function parameter list";
3143 _mesa_glsl_error(& loc
, state
,
3144 "%s variable `%s' must be declared at "
3146 mode
, var
->name
, extra
);
3148 } else if (var
->data
.mode
== ir_var_shader_in
) {
3149 var
->data
.read_only
= true;
3151 if (state
->stage
== MESA_SHADER_VERTEX
) {
3152 bool error_emitted
= false;
3154 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
3156 * "Vertex shader inputs can only be float, floating-point
3157 * vectors, matrices, signed and unsigned integers and integer
3158 * vectors. Vertex shader inputs can also form arrays of these
3159 * types, but not structures."
3161 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
3163 * "Vertex shader inputs can only be float, floating-point
3164 * vectors, matrices, signed and unsigned integers and integer
3165 * vectors. They cannot be arrays or structures."
3167 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
3169 * "The attribute qualifier can be used only with float,
3170 * floating-point vectors, and matrices. Attribute variables
3171 * cannot be declared as arrays or structures."
3173 * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
3175 * "Vertex shader inputs can only be float, floating-point
3176 * vectors, matrices, signed and unsigned integers and integer
3177 * vectors. Vertex shader inputs cannot be arrays or
3180 const glsl_type
*check_type
= var
->type
;
3181 while (check_type
->is_array())
3182 check_type
= check_type
->element_type();
3184 switch (check_type
->base_type
) {
3185 case GLSL_TYPE_FLOAT
:
3187 case GLSL_TYPE_UINT
:
3189 if (state
->is_version(120, 300))
3193 _mesa_glsl_error(& loc
, state
,
3194 "vertex shader input / attribute cannot have "
3196 var
->type
->is_array() ? "array of " : "",
3198 error_emitted
= true;
3201 if (!error_emitted
&& var
->type
->is_array() &&
3202 !state
->check_version(150, 0, &loc
,
3203 "vertex shader input / attribute "
3204 "cannot have array type")) {
3205 error_emitted
= true;
3207 } else if (state
->stage
== MESA_SHADER_GEOMETRY
) {
3208 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
3210 * Geometry shader input variables get the per-vertex values
3211 * written out by vertex shader output variables of the same
3212 * names. Since a geometry shader operates on a set of
3213 * vertices, each input varying variable (or input block, see
3214 * interface blocks below) needs to be declared as an array.
3216 if (!var
->type
->is_array()) {
3217 _mesa_glsl_error(&loc
, state
,
3218 "geometry shader inputs must be arrays");
3221 handle_geometry_shader_input_decl(state
, loc
, var
);
3225 /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
3226 * so must integer vertex outputs.
3228 * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
3229 * "Fragment shader inputs that are signed or unsigned integers or
3230 * integer vectors must be qualified with the interpolation qualifier
3233 * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
3234 * "Fragment shader inputs that are, or contain, signed or unsigned
3235 * integers or integer vectors must be qualified with the
3236 * interpolation qualifier flat."
3238 * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
3239 * "Vertex shader outputs that are, or contain, signed or unsigned
3240 * integers or integer vectors must be qualified with the
3241 * interpolation qualifier flat."
3243 * Note that prior to GLSL 1.50, this requirement applied to vertex
3244 * outputs rather than fragment inputs. That creates problems in the
3245 * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
3246 * desktop GL shaders. For GLSL ES shaders, we follow the spec and
3247 * apply the restriction to both vertex outputs and fragment inputs.
3249 * Note also that the desktop GLSL specs are missing the text "or
3250 * contain"; this is presumably an oversight, since there is no
3251 * reasonable way to interpolate a fragment shader input that contains
3254 if (state
->is_version(130, 300) &&
3255 var
->type
->contains_integer() &&
3256 var
->data
.interpolation
!= INTERP_QUALIFIER_FLAT
&&
3257 ((state
->stage
== MESA_SHADER_FRAGMENT
&& var
->data
.mode
== ir_var_shader_in
)
3258 || (state
->stage
== MESA_SHADER_VERTEX
&& var
->data
.mode
== ir_var_shader_out
3259 && state
->es_shader
))) {
3260 const char *var_type
= (state
->stage
== MESA_SHADER_VERTEX
) ?
3261 "vertex output" : "fragment input";
3262 _mesa_glsl_error(&loc
, state
, "if a %s is (or contains) "
3263 "an integer, then it must be qualified with 'flat'",
3268 /* Interpolation qualifiers cannot be applied to 'centroid' and
3269 * 'centroid varying'.
3271 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
3272 * "interpolation qualifiers may only precede the qualifiers in,
3273 * centroid in, out, or centroid out in a declaration. They do not apply
3274 * to the deprecated storage qualifiers varying or centroid varying."
3276 * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
3278 if (state
->is_version(130, 0)
3279 && this->type
->qualifier
.has_interpolation()
3280 && this->type
->qualifier
.flags
.q
.varying
) {
3282 const char *i
= this->type
->qualifier
.interpolation_string();
3285 if (this->type
->qualifier
.flags
.q
.centroid
)
3286 s
= "centroid varying";
3290 _mesa_glsl_error(&loc
, state
,
3291 "qualifier '%s' cannot be applied to the "
3292 "deprecated storage qualifier '%s'", i
, s
);
3296 /* Interpolation qualifiers can only apply to vertex shader outputs and
3297 * fragment shader inputs.
3299 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
3300 * "Outputs from a vertex shader (out) and inputs to a fragment
3301 * shader (in) can be further qualified with one or more of these
3302 * interpolation qualifiers"
3304 * From page 31 (page 37 of the PDF) of the GLSL ES 3.00 spec:
3305 * "These interpolation qualifiers may only precede the qualifiers
3306 * in, centroid in, out, or centroid out in a declaration. They do
3307 * not apply to inputs into a vertex shader or outputs from a
3310 if (state
->is_version(130, 300)
3311 && this->type
->qualifier
.has_interpolation()) {
3313 const char *i
= this->type
->qualifier
.interpolation_string();
3316 switch (state
->stage
) {
3317 case MESA_SHADER_VERTEX
:
3318 if (this->type
->qualifier
.flags
.q
.in
) {
3319 _mesa_glsl_error(&loc
, state
,
3320 "qualifier '%s' cannot be applied to vertex "
3321 "shader inputs", i
);
3324 case MESA_SHADER_FRAGMENT
:
3325 if (this->type
->qualifier
.flags
.q
.out
) {
3326 _mesa_glsl_error(&loc
, state
,
3327 "qualifier '%s' cannot be applied to fragment "
3328 "shader outputs", i
);
3337 /* From section 4.3.4 of the GLSL 1.30 spec:
3338 * "It is an error to use centroid in in a vertex shader."
3340 * From section 4.3.4 of the GLSL ES 3.00 spec:
3341 * "It is an error to use centroid in or interpolation qualifiers in
3342 * a vertex shader input."
3344 if (state
->is_version(130, 300)
3345 && this->type
->qualifier
.flags
.q
.centroid
3346 && this->type
->qualifier
.flags
.q
.in
3347 && state
->stage
== MESA_SHADER_VERTEX
) {
3349 _mesa_glsl_error(&loc
, state
,
3350 "'centroid in' cannot be used in a vertex shader");
3353 if (state
->stage
== MESA_SHADER_VERTEX
3354 && this->type
->qualifier
.flags
.q
.sample
3355 && this->type
->qualifier
.flags
.q
.in
) {
3357 _mesa_glsl_error(&loc
, state
,
3358 "'sample in' cannot be used in a vertex shader");
3361 /* Section 4.3.6 of the GLSL 1.30 specification states:
3362 * "It is an error to use centroid out in a fragment shader."
3364 * The GL_ARB_shading_language_420pack extension specification states:
3365 * "It is an error to use auxiliary storage qualifiers or interpolation
3366 * qualifiers on an output in a fragment shader."
3368 if (state
->stage
== MESA_SHADER_FRAGMENT
&&
3369 this->type
->qualifier
.flags
.q
.out
&&
3370 this->type
->qualifier
.has_auxiliary_storage()) {
3371 _mesa_glsl_error(&loc
, state
,
3372 "auxiliary storage qualifiers cannot be used on "
3373 "fragment shader outputs");
3376 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
3378 if (this->type
->qualifier
.precision
!= ast_precision_none
) {
3379 state
->check_precision_qualifiers_allowed(&loc
);
3383 /* Precision qualifiers apply to floating point, integer and sampler
3386 * Section 4.5.2 (Precision Qualifiers) of the GLSL 1.30 spec says:
3387 * "Any floating point or any integer declaration can have the type
3388 * preceded by one of these precision qualifiers [...] Literal
3389 * constants do not have precision qualifiers. Neither do Boolean
3392 * Section 4.5 (Precision and Precision Qualifiers) of the GLSL 1.30
3395 * "Precision qualifiers are added for code portability with OpenGL
3396 * ES, not for functionality. They have the same syntax as in OpenGL
3399 * Section 8 (Built-In Functions) of the GLSL ES 1.00 spec says:
3401 * "uniform lowp sampler2D sampler;
3404 * lowp vec4 col = texture2D (sampler, coord);
3405 * // texture2D returns lowp"
3407 * From this, we infer that GLSL 1.30 (and later) should allow precision
3408 * qualifiers on sampler types just like float and integer types.
3410 if (this->type
->qualifier
.precision
!= ast_precision_none
3411 && !var
->type
->is_float()
3412 && !var
->type
->is_integer()
3413 && !var
->type
->is_record()
3414 && !var
->type
->is_sampler()
3415 && !(var
->type
->is_array()
3416 && (var
->type
->fields
.array
->is_float()
3417 || var
->type
->fields
.array
->is_integer()))) {
3419 _mesa_glsl_error(&loc
, state
,
3420 "precision qualifiers apply only to floating point"
3421 ", integer and sampler types");
3424 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3426 * "[Sampler types] can only be declared as function
3427 * parameters or uniform variables (see Section 4.3.5
3430 if (var_type
->contains_sampler() &&
3431 !this->type
->qualifier
.flags
.q
.uniform
) {
3432 _mesa_glsl_error(&loc
, state
, "samplers must be declared uniform");
3435 /* Process the initializer and add its instructions to a temporary
3436 * list. This list will be added to the instruction stream (below) after
3437 * the declaration is added. This is done because in some cases (such as
3438 * redeclarations) the declaration may not actually be added to the
3439 * instruction stream.
3441 exec_list initializer_instructions
;
3442 ir_variable
*earlier
=
3443 get_variable_being_redeclared(var
, decl
->get_location(), state
,
3444 false /* allow_all_redeclarations */);
3445 if (earlier
!= NULL
) {
3446 if (strncmp(var
->name
, "gl_", 3) == 0 &&
3447 earlier
->data
.how_declared
== ir_var_declared_in_block
) {
3448 _mesa_glsl_error(&loc
, state
,
3449 "`%s' has already been redeclared using "
3450 "gl_PerVertex", var
->name
);
3452 earlier
->data
.how_declared
= ir_var_declared_normally
;
3455 if (decl
->initializer
!= NULL
) {
3456 result
= process_initializer((earlier
== NULL
) ? var
: earlier
,
3458 &initializer_instructions
, state
);
3461 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
3463 * "It is an error to write to a const variable outside of
3464 * its declaration, so they must be initialized when
3467 if (this->type
->qualifier
.flags
.q
.constant
&& decl
->initializer
== NULL
) {
3468 _mesa_glsl_error(& loc
, state
,
3469 "const declaration of `%s' must be initialized",
3473 if (state
->es_shader
) {
3474 const glsl_type
*const t
= (earlier
== NULL
)
3475 ? var
->type
: earlier
->type
;
3477 if (t
->is_unsized_array())
3478 /* Section 10.17 of the GLSL ES 1.00 specification states that
3479 * unsized array declarations have been removed from the language.
3480 * Arrays that are sized using an initializer are still explicitly
3481 * sized. However, GLSL ES 1.00 does not allow array
3482 * initializers. That is only allowed in GLSL ES 3.00.
3484 * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
3486 * "An array type can also be formed without specifying a size
3487 * if the definition includes an initializer:
3489 * float x[] = float[2] (1.0, 2.0); // declares an array of size 2
3490 * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
3495 _mesa_glsl_error(& loc
, state
,
3496 "unsized array declarations are not allowed in "
3500 /* If the declaration is not a redeclaration, there are a few additional
3501 * semantic checks that must be applied. In addition, variable that was
3502 * created for the declaration should be added to the IR stream.
3504 if (earlier
== NULL
) {
3505 validate_identifier(decl
->identifier
, loc
, state
);
3507 /* Add the variable to the symbol table. Note that the initializer's
3508 * IR was already processed earlier (though it hasn't been emitted
3509 * yet), without the variable in scope.
3511 * This differs from most C-like languages, but it follows the GLSL
3512 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
3515 * "Within a declaration, the scope of a name starts immediately
3516 * after the initializer if present or immediately after the name
3517 * being declared if not."
3519 if (!state
->symbols
->add_variable(var
)) {
3520 YYLTYPE loc
= this->get_location();
3521 _mesa_glsl_error(&loc
, state
, "name `%s' already taken in the "
3522 "current scope", decl
->identifier
);
3526 /* Push the variable declaration to the top. It means that all the
3527 * variable declarations will appear in a funny last-to-first order,
3528 * but otherwise we run into trouble if a function is prototyped, a
3529 * global var is decled, then the function is defined with usage of
3530 * the global var. See glslparsertest's CorrectModule.frag.
3532 instructions
->push_head(var
);
3535 instructions
->append_list(&initializer_instructions
);
3539 /* Generally, variable declarations do not have r-values. However,
3540 * one is used for the declaration in
3542 * while (bool b = some_condition()) {
3546 * so we return the rvalue from the last seen declaration here.
3553 ast_parameter_declarator::hir(exec_list
*instructions
,
3554 struct _mesa_glsl_parse_state
*state
)
3557 const struct glsl_type
*type
;
3558 const char *name
= NULL
;
3559 YYLTYPE loc
= this->get_location();
3561 type
= this->type
->glsl_type(& name
, state
);
3565 _mesa_glsl_error(& loc
, state
,
3566 "invalid type `%s' in declaration of `%s'",
3567 name
, this->identifier
);
3569 _mesa_glsl_error(& loc
, state
,
3570 "invalid type in declaration of `%s'",
3574 type
= glsl_type::error_type
;
3577 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
3579 * "Functions that accept no input arguments need not use void in the
3580 * argument list because prototypes (or definitions) are required and
3581 * therefore there is no ambiguity when an empty argument list "( )" is
3582 * declared. The idiom "(void)" as a parameter list is provided for
3585 * Placing this check here prevents a void parameter being set up
3586 * for a function, which avoids tripping up checks for main taking
3587 * parameters and lookups of an unnamed symbol.
3589 if (type
->is_void()) {
3590 if (this->identifier
!= NULL
)
3591 _mesa_glsl_error(& loc
, state
,
3592 "named parameter cannot have type `void'");
3598 if (formal_parameter
&& (this->identifier
== NULL
)) {
3599 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
3603 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
3604 * call already handled the "vec4[..] foo" case.
3606 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
3608 if (!type
->is_error() && type
->is_unsized_array()) {
3609 _mesa_glsl_error(&loc
, state
, "arrays passed as parameters must have "
3611 type
= glsl_type::error_type
;
3615 ir_variable
*var
= new(ctx
)
3616 ir_variable(type
, this->identifier
, ir_var_function_in
);
3618 /* Apply any specified qualifiers to the parameter declaration. Note that
3619 * for function parameters the default mode is 'in'.
3621 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
,
3624 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3626 * "Samplers cannot be treated as l-values; hence cannot be used
3627 * as out or inout function parameters, nor can they be assigned
3630 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
3631 && type
->contains_sampler()) {
3632 _mesa_glsl_error(&loc
, state
, "out and inout parameters cannot contain samplers");
3633 type
= glsl_type::error_type
;
3636 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
3638 * "When calling a function, expressions that do not evaluate to
3639 * l-values cannot be passed to parameters declared as out or inout."
3641 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
3643 * "Other binary or unary expressions, non-dereferenced arrays,
3644 * function names, swizzles with repeated fields, and constants
3645 * cannot be l-values."
3647 * So for GLSL 1.10, passing an array as an out or inout parameter is not
3648 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
3650 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
3652 && !state
->check_version(120, 100, &loc
,
3653 "arrays cannot be out or inout parameters")) {
3654 type
= glsl_type::error_type
;
3657 instructions
->push_tail(var
);
3659 /* Parameter declarations do not have r-values.
3666 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
3668 exec_list
*ir_parameters
,
3669 _mesa_glsl_parse_state
*state
)
3671 ast_parameter_declarator
*void_param
= NULL
;
3674 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
3675 param
->formal_parameter
= formal
;
3676 param
->hir(ir_parameters
, state
);
3684 if ((void_param
!= NULL
) && (count
> 1)) {
3685 YYLTYPE loc
= void_param
->get_location();
3687 _mesa_glsl_error(& loc
, state
,
3688 "`void' parameter must be only parameter");
3694 emit_function(_mesa_glsl_parse_state
*state
, ir_function
*f
)
3696 /* IR invariants disallow function declarations or definitions
3697 * nested within other function definitions. But there is no
3698 * requirement about the relative order of function declarations
3699 * and definitions with respect to one another. So simply insert
3700 * the new ir_function block at the end of the toplevel instruction
3703 state
->toplevel_ir
->push_tail(f
);
3708 ast_function::hir(exec_list
*instructions
,
3709 struct _mesa_glsl_parse_state
*state
)
3712 ir_function
*f
= NULL
;
3713 ir_function_signature
*sig
= NULL
;
3714 exec_list hir_parameters
;
3716 const char *const name
= identifier
;
3718 /* New functions are always added to the top-level IR instruction stream,
3719 * so this instruction list pointer is ignored. See also emit_function
3722 (void) instructions
;
3724 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
3726 * "Function declarations (prototypes) cannot occur inside of functions;
3727 * they must be at global scope, or for the built-in functions, outside
3728 * the global scope."
3730 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
3732 * "User defined functions may only be defined within the global scope."
3734 * Note that this language does not appear in GLSL 1.10.
3736 if ((state
->current_function
!= NULL
) &&
3737 state
->is_version(120, 100)) {
3738 YYLTYPE loc
= this->get_location();
3739 _mesa_glsl_error(&loc
, state
,
3740 "declaration of function `%s' not allowed within "
3741 "function body", name
);
3744 validate_identifier(name
, this->get_location(), state
);
3746 /* Convert the list of function parameters to HIR now so that they can be
3747 * used below to compare this function's signature with previously seen
3748 * signatures for functions with the same name.
3750 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
3752 & hir_parameters
, state
);
3754 const char *return_type_name
;
3755 const glsl_type
*return_type
=
3756 this->return_type
->glsl_type(& return_type_name
, state
);
3759 YYLTYPE loc
= this->get_location();
3760 _mesa_glsl_error(&loc
, state
,
3761 "function `%s' has undeclared return type `%s'",
3762 name
, return_type_name
);
3763 return_type
= glsl_type::error_type
;
3766 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
3767 * "No qualifier is allowed on the return type of a function."
3769 if (this->return_type
->has_qualifiers()) {
3770 YYLTYPE loc
= this->get_location();
3771 _mesa_glsl_error(& loc
, state
,
3772 "function `%s' return type has qualifiers", name
);
3775 /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
3777 * "Arrays are allowed as arguments and as the return type. In both
3778 * cases, the array must be explicitly sized."
3780 if (return_type
->is_unsized_array()) {
3781 YYLTYPE loc
= this->get_location();
3782 _mesa_glsl_error(& loc
, state
,
3783 "function `%s' return type array must be explicitly "
3787 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3789 * "[Sampler types] can only be declared as function parameters
3790 * or uniform variables (see Section 4.3.5 "Uniform")".
3792 if (return_type
->contains_sampler()) {
3793 YYLTYPE loc
= this->get_location();
3794 _mesa_glsl_error(&loc
, state
,
3795 "function `%s' return type can't contain a sampler",
3799 /* Verify that this function's signature either doesn't match a previously
3800 * seen signature for a function with the same name, or, if a match is found,
3801 * that the previously seen signature does not have an associated definition.
3803 f
= state
->symbols
->get_function(name
);
3804 if (f
!= NULL
&& (state
->es_shader
|| f
->has_user_signature())) {
3805 sig
= f
->exact_matching_signature(state
, &hir_parameters
);
3807 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
3808 if (badvar
!= NULL
) {
3809 YYLTYPE loc
= this->get_location();
3811 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
3812 "qualifiers don't match prototype", name
, badvar
);
3815 if (sig
->return_type
!= return_type
) {
3816 YYLTYPE loc
= this->get_location();
3818 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
3819 "match prototype", name
);
3822 if (sig
->is_defined
) {
3823 if (is_definition
) {
3824 YYLTYPE loc
= this->get_location();
3825 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
3827 /* We just encountered a prototype that exactly matches a
3828 * function that's already been defined. This is redundant,
3829 * and we should ignore it.
3836 f
= new(ctx
) ir_function(name
);
3837 if (!state
->symbols
->add_function(f
)) {
3838 /* This function name shadows a non-function use of the same name. */
3839 YYLTYPE loc
= this->get_location();
3841 _mesa_glsl_error(&loc
, state
, "function name `%s' conflicts with "
3842 "non-function", name
);
3846 emit_function(state
, f
);
3849 /* Verify the return type of main() */
3850 if (strcmp(name
, "main") == 0) {
3851 if (! return_type
->is_void()) {
3852 YYLTYPE loc
= this->get_location();
3854 _mesa_glsl_error(& loc
, state
, "main() must return void");
3857 if (!hir_parameters
.is_empty()) {
3858 YYLTYPE loc
= this->get_location();
3860 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
3864 /* Finish storing the information about this new function in its signature.
3867 sig
= new(ctx
) ir_function_signature(return_type
);
3868 f
->add_signature(sig
);
3871 sig
->replace_parameters(&hir_parameters
);
3874 /* Function declarations (prototypes) do not have r-values.
3881 ast_function_definition::hir(exec_list
*instructions
,
3882 struct _mesa_glsl_parse_state
*state
)
3884 prototype
->is_definition
= true;
3885 prototype
->hir(instructions
, state
);
3887 ir_function_signature
*signature
= prototype
->signature
;
3888 if (signature
== NULL
)
3891 assert(state
->current_function
== NULL
);
3892 state
->current_function
= signature
;
3893 state
->found_return
= false;
3895 /* Duplicate parameters declared in the prototype as concrete variables.
3896 * Add these to the symbol table.
3898 state
->symbols
->push_scope();
3899 foreach_list(n
, &signature
->parameters
) {
3900 ir_variable
*const var
= ((ir_instruction
*) n
)->as_variable();
3902 assert(var
!= NULL
);
3904 /* The only way a parameter would "exist" is if two parameters have
3907 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
3908 YYLTYPE loc
= this->get_location();
3910 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
3912 state
->symbols
->add_variable(var
);
3916 /* Convert the body of the function to HIR. */
3917 this->body
->hir(&signature
->body
, state
);
3918 signature
->is_defined
= true;
3920 state
->symbols
->pop_scope();
3922 assert(state
->current_function
== signature
);
3923 state
->current_function
= NULL
;
3925 if (!signature
->return_type
->is_void() && !state
->found_return
) {
3926 YYLTYPE loc
= this->get_location();
3927 _mesa_glsl_error(& loc
, state
, "function `%s' has non-void return type "
3928 "%s, but no return statement",
3929 signature
->function_name(),
3930 signature
->return_type
->name
);
3933 /* Function definitions do not have r-values.
3940 ast_jump_statement::hir(exec_list
*instructions
,
3941 struct _mesa_glsl_parse_state
*state
)
3948 assert(state
->current_function
);
3950 if (opt_return_value
) {
3951 ir_rvalue
*ret
= opt_return_value
->hir(instructions
, state
);
3953 /* The value of the return type can be NULL if the shader says
3954 * 'return foo();' and foo() is a function that returns void.
3956 * NOTE: The GLSL spec doesn't say that this is an error. The type
3957 * of the return value is void. If the return type of the function is
3958 * also void, then this should compile without error. Seriously.
3960 const glsl_type
*const ret_type
=
3961 (ret
== NULL
) ? glsl_type::void_type
: ret
->type
;
3963 /* Implicit conversions are not allowed for return values prior to
3964 * ARB_shading_language_420pack.
3966 if (state
->current_function
->return_type
!= ret_type
) {
3967 YYLTYPE loc
= this->get_location();
3969 if (state
->ARB_shading_language_420pack_enable
) {
3970 if (!apply_implicit_conversion(state
->current_function
->return_type
,
3972 _mesa_glsl_error(& loc
, state
,
3973 "could not implicitly convert return value "
3974 "to %s, in function `%s'",
3975 state
->current_function
->return_type
->name
,
3976 state
->current_function
->function_name());
3979 _mesa_glsl_error(& loc
, state
,
3980 "`return' with wrong type %s, in function `%s' "
3983 state
->current_function
->function_name(),
3984 state
->current_function
->return_type
->name
);
3986 } else if (state
->current_function
->return_type
->base_type
==
3988 YYLTYPE loc
= this->get_location();
3990 /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
3991 * specs add a clarification:
3993 * "A void function can only use return without a return argument, even if
3994 * the return argument has void type. Return statements only accept values:
3997 * void func2() { return func1(); } // illegal return statement"
3999 _mesa_glsl_error(& loc
, state
,
4000 "void functions can only use `return' without a "
4004 inst
= new(ctx
) ir_return(ret
);
4006 if (state
->current_function
->return_type
->base_type
!=
4008 YYLTYPE loc
= this->get_location();
4010 _mesa_glsl_error(& loc
, state
,
4011 "`return' with no value, in function %s returning "
4013 state
->current_function
->function_name());
4015 inst
= new(ctx
) ir_return
;
4018 state
->found_return
= true;
4019 instructions
->push_tail(inst
);
4024 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
4025 YYLTYPE loc
= this->get_location();
4027 _mesa_glsl_error(& loc
, state
,
4028 "`discard' may only appear in a fragment shader");
4030 instructions
->push_tail(new(ctx
) ir_discard
);
4035 if (mode
== ast_continue
&&
4036 state
->loop_nesting_ast
== NULL
) {
4037 YYLTYPE loc
= this->get_location();
4039 _mesa_glsl_error(& loc
, state
,
4040 "continue may only appear in a loop");
4041 } else if (mode
== ast_break
&&
4042 state
->loop_nesting_ast
== NULL
&&
4043 state
->switch_state
.switch_nesting_ast
== NULL
) {
4044 YYLTYPE loc
= this->get_location();
4046 _mesa_glsl_error(& loc
, state
,
4047 "break may only appear in a loop or a switch");
4049 /* For a loop, inline the for loop expression again, since we don't
4050 * know where near the end of the loop body the normal copy of it is
4051 * going to be placed. Same goes for the condition for a do-while
4054 if (state
->loop_nesting_ast
!= NULL
&&
4055 mode
== ast_continue
) {
4056 if (state
->loop_nesting_ast
->rest_expression
) {
4057 state
->loop_nesting_ast
->rest_expression
->hir(instructions
,
4060 if (state
->loop_nesting_ast
->mode
==
4061 ast_iteration_statement::ast_do_while
) {
4062 state
->loop_nesting_ast
->condition_to_hir(instructions
, state
);
4066 if (state
->switch_state
.is_switch_innermost
&&
4067 mode
== ast_break
) {
4068 /* Force break out of switch by setting is_break switch state.
4070 ir_variable
*const is_break_var
= state
->switch_state
.is_break_var
;
4071 ir_dereference_variable
*const deref_is_break_var
=
4072 new(ctx
) ir_dereference_variable(is_break_var
);
4073 ir_constant
*const true_val
= new(ctx
) ir_constant(true);
4074 ir_assignment
*const set_break_var
=
4075 new(ctx
) ir_assignment(deref_is_break_var
, true_val
);
4077 instructions
->push_tail(set_break_var
);
4080 ir_loop_jump
*const jump
=
4081 new(ctx
) ir_loop_jump((mode
== ast_break
)
4082 ? ir_loop_jump::jump_break
4083 : ir_loop_jump::jump_continue
);
4084 instructions
->push_tail(jump
);
4091 /* Jump instructions do not have r-values.
4098 ast_selection_statement::hir(exec_list
*instructions
,
4099 struct _mesa_glsl_parse_state
*state
)
4103 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
4105 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
4107 * "Any expression whose type evaluates to a Boolean can be used as the
4108 * conditional expression bool-expression. Vector types are not accepted
4109 * as the expression to if."
4111 * The checks are separated so that higher quality diagnostics can be
4112 * generated for cases where both rules are violated.
4114 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
4115 YYLTYPE loc
= this->condition
->get_location();
4117 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
4121 ir_if
*const stmt
= new(ctx
) ir_if(condition
);
4123 if (then_statement
!= NULL
) {
4124 state
->symbols
->push_scope();
4125 then_statement
->hir(& stmt
->then_instructions
, state
);
4126 state
->symbols
->pop_scope();
4129 if (else_statement
!= NULL
) {
4130 state
->symbols
->push_scope();
4131 else_statement
->hir(& stmt
->else_instructions
, state
);
4132 state
->symbols
->pop_scope();
4135 instructions
->push_tail(stmt
);
4137 /* if-statements do not have r-values.
4144 ast_switch_statement::hir(exec_list
*instructions
,
4145 struct _mesa_glsl_parse_state
*state
)
4149 ir_rvalue
*const test_expression
=
4150 this->test_expression
->hir(instructions
, state
);
4152 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
4154 * "The type of init-expression in a switch statement must be a
4157 if (!test_expression
->type
->is_scalar() ||
4158 !test_expression
->type
->is_integer()) {
4159 YYLTYPE loc
= this->test_expression
->get_location();
4161 _mesa_glsl_error(& loc
,
4163 "switch-statement expression must be scalar "
4167 /* Track the switch-statement nesting in a stack-like manner.
4169 struct glsl_switch_state saved
= state
->switch_state
;
4171 state
->switch_state
.is_switch_innermost
= true;
4172 state
->switch_state
.switch_nesting_ast
= this;
4173 state
->switch_state
.labels_ht
= hash_table_ctor(0, hash_table_pointer_hash
,
4174 hash_table_pointer_compare
);
4175 state
->switch_state
.previous_default
= NULL
;
4177 /* Initalize is_fallthru state to false.
4179 ir_rvalue
*const is_fallthru_val
= new (ctx
) ir_constant(false);
4180 state
->switch_state
.is_fallthru_var
=
4181 new(ctx
) ir_variable(glsl_type::bool_type
,
4182 "switch_is_fallthru_tmp",
4184 instructions
->push_tail(state
->switch_state
.is_fallthru_var
);
4186 ir_dereference_variable
*deref_is_fallthru_var
=
4187 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4188 instructions
->push_tail(new(ctx
) ir_assignment(deref_is_fallthru_var
,
4191 /* Initalize is_break state to false.
4193 ir_rvalue
*const is_break_val
= new (ctx
) ir_constant(false);
4194 state
->switch_state
.is_break_var
= new(ctx
) ir_variable(glsl_type::bool_type
,
4195 "switch_is_break_tmp",
4197 instructions
->push_tail(state
->switch_state
.is_break_var
);
4199 ir_dereference_variable
*deref_is_break_var
=
4200 new(ctx
) ir_dereference_variable(state
->switch_state
.is_break_var
);
4201 instructions
->push_tail(new(ctx
) ir_assignment(deref_is_break_var
,
4204 /* Cache test expression.
4206 test_to_hir(instructions
, state
);
4208 /* Emit code for body of switch stmt.
4210 body
->hir(instructions
, state
);
4212 hash_table_dtor(state
->switch_state
.labels_ht
);
4214 state
->switch_state
= saved
;
4216 /* Switch statements do not have r-values. */
4222 ast_switch_statement::test_to_hir(exec_list
*instructions
,
4223 struct _mesa_glsl_parse_state
*state
)
4227 /* Cache value of test expression. */
4228 ir_rvalue
*const test_val
=
4229 test_expression
->hir(instructions
,
4232 state
->switch_state
.test_var
= new(ctx
) ir_variable(test_val
->type
,
4235 ir_dereference_variable
*deref_test_var
=
4236 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
4238 instructions
->push_tail(state
->switch_state
.test_var
);
4239 instructions
->push_tail(new(ctx
) ir_assignment(deref_test_var
, test_val
));
4244 ast_switch_body::hir(exec_list
*instructions
,
4245 struct _mesa_glsl_parse_state
*state
)
4248 stmts
->hir(instructions
, state
);
4250 /* Switch bodies do not have r-values. */
4255 ast_case_statement_list::hir(exec_list
*instructions
,
4256 struct _mesa_glsl_parse_state
*state
)
4258 foreach_list_typed (ast_case_statement
, case_stmt
, link
, & this->cases
)
4259 case_stmt
->hir(instructions
, state
);
4261 /* Case statements do not have r-values. */
4266 ast_case_statement::hir(exec_list
*instructions
,
4267 struct _mesa_glsl_parse_state
*state
)
4269 labels
->hir(instructions
, state
);
4271 /* Conditionally set fallthru state based on break state. */
4272 ir_constant
*const false_val
= new(state
) ir_constant(false);
4273 ir_dereference_variable
*const deref_is_fallthru_var
=
4274 new(state
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4275 ir_dereference_variable
*const deref_is_break_var
=
4276 new(state
) ir_dereference_variable(state
->switch_state
.is_break_var
);
4277 ir_assignment
*const reset_fallthru_on_break
=
4278 new(state
) ir_assignment(deref_is_fallthru_var
,
4280 deref_is_break_var
);
4281 instructions
->push_tail(reset_fallthru_on_break
);
4283 /* Guard case statements depending on fallthru state. */
4284 ir_dereference_variable
*const deref_fallthru_guard
=
4285 new(state
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4286 ir_if
*const test_fallthru
= new(state
) ir_if(deref_fallthru_guard
);
4288 foreach_list_typed (ast_node
, stmt
, link
, & this->stmts
)
4289 stmt
->hir(& test_fallthru
->then_instructions
, state
);
4291 instructions
->push_tail(test_fallthru
);
4293 /* Case statements do not have r-values. */
4299 ast_case_label_list::hir(exec_list
*instructions
,
4300 struct _mesa_glsl_parse_state
*state
)
4302 foreach_list_typed (ast_case_label
, label
, link
, & this->labels
)
4303 label
->hir(instructions
, state
);
4305 /* Case labels do not have r-values. */
4310 ast_case_label::hir(exec_list
*instructions
,
4311 struct _mesa_glsl_parse_state
*state
)
4315 ir_dereference_variable
*deref_fallthru_var
=
4316 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4318 ir_rvalue
*const true_val
= new(ctx
) ir_constant(true);
4320 /* If not default case, ... */
4321 if (this->test_value
!= NULL
) {
4322 /* Conditionally set fallthru state based on
4323 * comparison of cached test expression value to case label.
4325 ir_rvalue
*const label_rval
= this->test_value
->hir(instructions
, state
);
4326 ir_constant
*label_const
= label_rval
->constant_expression_value();
4329 YYLTYPE loc
= this->test_value
->get_location();
4331 _mesa_glsl_error(& loc
, state
,
4332 "switch statement case label must be a "
4333 "constant expression");
4335 /* Stuff a dummy value in to allow processing to continue. */
4336 label_const
= new(ctx
) ir_constant(0);
4338 ast_expression
*previous_label
= (ast_expression
*)
4339 hash_table_find(state
->switch_state
.labels_ht
,
4340 (void *)(uintptr_t)label_const
->value
.u
[0]);
4342 if (previous_label
) {
4343 YYLTYPE loc
= this->test_value
->get_location();
4344 _mesa_glsl_error(& loc
, state
,
4345 "duplicate case value");
4347 loc
= previous_label
->get_location();
4348 _mesa_glsl_error(& loc
, state
,
4349 "this is the previous case label");
4351 hash_table_insert(state
->switch_state
.labels_ht
,
4353 (void *)(uintptr_t)label_const
->value
.u
[0]);
4357 ir_dereference_variable
*deref_test_var
=
4358 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
4360 ir_rvalue
*const test_cond
= new(ctx
) ir_expression(ir_binop_all_equal
,
4364 ir_assignment
*set_fallthru_on_test
=
4365 new(ctx
) ir_assignment(deref_fallthru_var
,
4369 instructions
->push_tail(set_fallthru_on_test
);
4370 } else { /* default case */
4371 if (state
->switch_state
.previous_default
) {
4372 YYLTYPE loc
= this->get_location();
4373 _mesa_glsl_error(& loc
, state
,
4374 "multiple default labels in one switch");
4376 loc
= state
->switch_state
.previous_default
->get_location();
4377 _mesa_glsl_error(& loc
, state
,
4378 "this is the first default label");
4380 state
->switch_state
.previous_default
= this;
4382 /* Set falltrhu state. */
4383 ir_assignment
*set_fallthru
=
4384 new(ctx
) ir_assignment(deref_fallthru_var
, true_val
);
4386 instructions
->push_tail(set_fallthru
);
4389 /* Case statements do not have r-values. */
4394 ast_iteration_statement::condition_to_hir(exec_list
*instructions
,
4395 struct _mesa_glsl_parse_state
*state
)
4399 if (condition
!= NULL
) {
4400 ir_rvalue
*const cond
=
4401 condition
->hir(instructions
, state
);
4404 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
4405 YYLTYPE loc
= condition
->get_location();
4407 _mesa_glsl_error(& loc
, state
,
4408 "loop condition must be scalar boolean");
4410 /* As the first code in the loop body, generate a block that looks
4411 * like 'if (!condition) break;' as the loop termination condition.
4413 ir_rvalue
*const not_cond
=
4414 new(ctx
) ir_expression(ir_unop_logic_not
, cond
);
4416 ir_if
*const if_stmt
= new(ctx
) ir_if(not_cond
);
4418 ir_jump
*const break_stmt
=
4419 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
4421 if_stmt
->then_instructions
.push_tail(break_stmt
);
4422 instructions
->push_tail(if_stmt
);
4429 ast_iteration_statement::hir(exec_list
*instructions
,
4430 struct _mesa_glsl_parse_state
*state
)
4434 /* For-loops and while-loops start a new scope, but do-while loops do not.
4436 if (mode
!= ast_do_while
)
4437 state
->symbols
->push_scope();
4439 if (init_statement
!= NULL
)
4440 init_statement
->hir(instructions
, state
);
4442 ir_loop
*const stmt
= new(ctx
) ir_loop();
4443 instructions
->push_tail(stmt
);
4445 /* Track the current loop nesting. */
4446 ast_iteration_statement
*nesting_ast
= state
->loop_nesting_ast
;
4448 state
->loop_nesting_ast
= this;
4450 /* Likewise, indicate that following code is closest to a loop,
4451 * NOT closest to a switch.
4453 bool saved_is_switch_innermost
= state
->switch_state
.is_switch_innermost
;
4454 state
->switch_state
.is_switch_innermost
= false;
4456 if (mode
!= ast_do_while
)
4457 condition_to_hir(&stmt
->body_instructions
, state
);
4460 body
->hir(& stmt
->body_instructions
, state
);
4462 if (rest_expression
!= NULL
)
4463 rest_expression
->hir(& stmt
->body_instructions
, state
);
4465 if (mode
== ast_do_while
)
4466 condition_to_hir(&stmt
->body_instructions
, state
);
4468 if (mode
!= ast_do_while
)
4469 state
->symbols
->pop_scope();
4471 /* Restore previous nesting before returning. */
4472 state
->loop_nesting_ast
= nesting_ast
;
4473 state
->switch_state
.is_switch_innermost
= saved_is_switch_innermost
;
4475 /* Loops do not have r-values.
4482 * Determine if the given type is valid for establishing a default precision
4485 * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
4487 * "The precision statement
4489 * precision precision-qualifier type;
4491 * can be used to establish a default precision qualifier. The type field
4492 * can be either int or float or any of the sampler types, and the
4493 * precision-qualifier can be lowp, mediump, or highp."
4495 * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
4496 * qualifiers on sampler types, but this seems like an oversight (since the
4497 * intention of including these in GLSL 1.30 is to allow compatibility with ES
4498 * shaders). So we allow int, float, and all sampler types regardless of GLSL
4502 is_valid_default_precision_type(const struct glsl_type
*const type
)
4507 switch (type
->base_type
) {
4509 case GLSL_TYPE_FLOAT
:
4510 /* "int" and "float" are valid, but vectors and matrices are not. */
4511 return type
->vector_elements
== 1 && type
->matrix_columns
== 1;
4512 case GLSL_TYPE_SAMPLER
:
4521 ast_type_specifier::hir(exec_list
*instructions
,
4522 struct _mesa_glsl_parse_state
*state
)
4524 if (this->default_precision
== ast_precision_none
&& this->structure
== NULL
)
4527 YYLTYPE loc
= this->get_location();
4529 /* If this is a precision statement, check that the type to which it is
4530 * applied is either float or int.
4532 * From section 4.5.3 of the GLSL 1.30 spec:
4533 * "The precision statement
4534 * precision precision-qualifier type;
4535 * can be used to establish a default precision qualifier. The type
4536 * field can be either int or float [...]. Any other types or
4537 * qualifiers will result in an error.
4539 if (this->default_precision
!= ast_precision_none
) {
4540 if (!state
->check_precision_qualifiers_allowed(&loc
))
4543 if (this->structure
!= NULL
) {
4544 _mesa_glsl_error(&loc
, state
,
4545 "precision qualifiers do not apply to structures");
4549 if (this->array_specifier
!= NULL
) {
4550 _mesa_glsl_error(&loc
, state
,
4551 "default precision statements do not apply to "
4556 const struct glsl_type
*const type
=
4557 state
->symbols
->get_type(this->type_name
);
4558 if (!is_valid_default_precision_type(type
)) {
4559 _mesa_glsl_error(&loc
, state
,
4560 "default precision statements apply only to "
4561 "float, int, and sampler types");
4565 if (type
->base_type
== GLSL_TYPE_FLOAT
4567 && state
->stage
== MESA_SHADER_FRAGMENT
) {
4568 /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
4571 * "The fragment language has no default precision qualifier for
4572 * floating point types."
4574 * As a result, we have to track whether or not default precision has
4575 * been specified for float in GLSL ES fragment shaders.
4577 * Earlier in that same section, the spec says:
4579 * "Non-precision qualified declarations will use the precision
4580 * qualifier specified in the most recent precision statement
4581 * that is still in scope. The precision statement has the same
4582 * scoping rules as variable declarations. If it is declared
4583 * inside a compound statement, its effect stops at the end of
4584 * the innermost statement it was declared in. Precision
4585 * statements in nested scopes override precision statements in
4586 * outer scopes. Multiple precision statements for the same basic
4587 * type can appear inside the same scope, with later statements
4588 * overriding earlier statements within that scope."
4590 * Default precision specifications follow the same scope rules as
4591 * variables. So, we can track the state of the default float
4592 * precision in the symbol table, and the rules will just work. This
4593 * is a slight abuse of the symbol table, but it has the semantics
4596 ir_variable
*const junk
=
4597 new(state
) ir_variable(type
, "#default precision",
4600 state
->symbols
->add_variable(junk
);
4603 /* FINISHME: Translate precision statements into IR. */
4607 /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
4608 * process_record_constructor() can do type-checking on C-style initializer
4609 * expressions of structs, but ast_struct_specifier should only be translated
4610 * to HIR if it is declaring the type of a structure.
4612 * The ->is_declaration field is false for initializers of variables
4613 * declared separately from the struct's type definition.
4615 * struct S { ... }; (is_declaration = true)
4616 * struct T { ... } t = { ... }; (is_declaration = true)
4617 * S s = { ... }; (is_declaration = false)
4619 if (this->structure
!= NULL
&& this->structure
->is_declaration
)
4620 return this->structure
->hir(instructions
, state
);
4627 * Process a structure or interface block tree into an array of structure fields
4629 * After parsing, where there are some syntax differnces, structures and
4630 * interface blocks are almost identical. They are similar enough that the
4631 * AST for each can be processed the same way into a set of
4632 * \c glsl_struct_field to describe the members.
4634 * If we're processing an interface block, var_mode should be the type of the
4635 * interface block (ir_var_shader_in, ir_var_shader_out, or ir_var_uniform).
4636 * If we're processing a structure, var_mode should be ir_var_auto.
4639 * The number of fields processed. A pointer to the array structure fields is
4640 * stored in \c *fields_ret.
4643 ast_process_structure_or_interface_block(exec_list
*instructions
,
4644 struct _mesa_glsl_parse_state
*state
,
4645 exec_list
*declarations
,
4647 glsl_struct_field
**fields_ret
,
4649 bool block_row_major
,
4650 bool allow_reserved_names
,
4651 ir_variable_mode var_mode
)
4653 unsigned decl_count
= 0;
4655 /* Make an initial pass over the list of fields to determine how
4656 * many there are. Each element in this list is an ast_declarator_list.
4657 * This means that we actually need to count the number of elements in the
4658 * 'declarations' list in each of the elements.
4660 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
4661 foreach_list_const (decl_ptr
, & decl_list
->declarations
) {
4666 /* Allocate storage for the fields and process the field
4667 * declarations. As the declarations are processed, try to also convert
4668 * the types to HIR. This ensures that structure definitions embedded in
4669 * other structure definitions or in interface blocks are processed.
4671 glsl_struct_field
*const fields
= ralloc_array(state
, glsl_struct_field
,
4675 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
4676 const char *type_name
;
4678 decl_list
->type
->specifier
->hir(instructions
, state
);
4680 /* Section 10.9 of the GLSL ES 1.00 specification states that
4681 * embedded structure definitions have been removed from the language.
4683 if (state
->es_shader
&& decl_list
->type
->specifier
->structure
!= NULL
) {
4684 _mesa_glsl_error(&loc
, state
, "embedded structure definitions are "
4685 "not allowed in GLSL ES 1.00");
4688 const glsl_type
*decl_type
=
4689 decl_list
->type
->glsl_type(& type_name
, state
);
4691 foreach_list_typed (ast_declaration
, decl
, link
,
4692 &decl_list
->declarations
) {
4693 if (!allow_reserved_names
)
4694 validate_identifier(decl
->identifier
, loc
, state
);
4696 /* From the GL_ARB_uniform_buffer_object spec:
4698 * "Sampler types are not allowed inside of uniform
4699 * blocks. All other types, arrays, and structures
4700 * allowed for uniforms are allowed within a uniform
4703 * It should be impossible for decl_type to be NULL here. Cases that
4704 * might naturally lead to decl_type being NULL, especially for the
4705 * is_interface case, will have resulted in compilation having
4706 * already halted due to a syntax error.
4708 const struct glsl_type
*field_type
=
4709 decl_type
!= NULL
? decl_type
: glsl_type::error_type
;
4711 if (is_interface
&& field_type
->contains_sampler()) {
4712 YYLTYPE loc
= decl_list
->get_location();
4713 _mesa_glsl_error(&loc
, state
,
4714 "uniform in non-default uniform block contains sampler");
4717 if (field_type
->contains_atomic()) {
4718 /* FINISHME: Add a spec quotation here once updated spec
4719 * FINISHME: language is available. See Khronos bug #10903
4720 * FINISHME: on whether atomic counters are allowed in
4721 * FINISHME: structures.
4723 YYLTYPE loc
= decl_list
->get_location();
4724 _mesa_glsl_error(&loc
, state
, "atomic counter in structure or "
4728 const struct ast_type_qualifier
*const qual
=
4729 & decl_list
->type
->qualifier
;
4730 if (qual
->flags
.q
.std140
||
4731 qual
->flags
.q
.packed
||
4732 qual
->flags
.q
.shared
) {
4733 _mesa_glsl_error(&loc
, state
,
4734 "uniform block layout qualifiers std140, packed, and "
4735 "shared can only be applied to uniform blocks, not "
4739 field_type
= process_array_type(&loc
, decl_type
,
4740 decl
->array_specifier
, state
);
4741 fields
[i
].type
= field_type
;
4742 fields
[i
].name
= decl
->identifier
;
4743 fields
[i
].location
= -1;
4744 fields
[i
].interpolation
=
4745 interpret_interpolation_qualifier(qual
, var_mode
, state
, &loc
);
4746 fields
[i
].centroid
= qual
->flags
.q
.centroid
? 1 : 0;
4747 fields
[i
].sample
= qual
->flags
.q
.sample
? 1 : 0;
4749 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
4750 if (!qual
->flags
.q
.uniform
) {
4751 _mesa_glsl_error(&loc
, state
,
4752 "row_major and column_major can only be "
4753 "applied to uniform interface blocks");
4755 validate_matrix_layout_for_type(state
, &loc
, field_type
, NULL
);
4758 if (qual
->flags
.q
.uniform
&& qual
->has_interpolation()) {
4759 _mesa_glsl_error(&loc
, state
,
4760 "interpolation qualifiers cannot be used "
4761 "with uniform interface blocks");
4764 if (field_type
->is_matrix() ||
4765 (field_type
->is_array() && field_type
->fields
.array
->is_matrix())) {
4766 fields
[i
].row_major
= block_row_major
;
4767 if (qual
->flags
.q
.row_major
)
4768 fields
[i
].row_major
= true;
4769 else if (qual
->flags
.q
.column_major
)
4770 fields
[i
].row_major
= false;
4777 assert(i
== decl_count
);
4779 *fields_ret
= fields
;
4785 ast_struct_specifier::hir(exec_list
*instructions
,
4786 struct _mesa_glsl_parse_state
*state
)
4788 YYLTYPE loc
= this->get_location();
4790 /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
4792 * "Anonymous structures are not supported; so embedded structures must
4793 * have a declarator. A name given to an embedded struct is scoped at
4794 * the same level as the struct it is embedded in."
4796 * The same section of the GLSL 1.20 spec says:
4798 * "Anonymous structures are not supported. Embedded structures are not
4801 * struct S { float f; };
4803 * S; // Error: anonymous structures disallowed
4804 * struct { ... }; // Error: embedded structures disallowed
4805 * S s; // Okay: nested structures with name are allowed
4808 * The GLSL ES 1.00 and 3.00 specs have similar langauge and examples. So,
4809 * we allow embedded structures in 1.10 only.
4811 if (state
->language_version
!= 110 && state
->struct_specifier_depth
!= 0)
4812 _mesa_glsl_error(&loc
, state
,
4813 "embedded structure declartions are not allowed");
4815 state
->struct_specifier_depth
++;
4817 glsl_struct_field
*fields
;
4818 unsigned decl_count
=
4819 ast_process_structure_or_interface_block(instructions
,
4821 &this->declarations
,
4826 false /* allow_reserved_names */,
4829 validate_identifier(this->name
, loc
, state
);
4831 const glsl_type
*t
=
4832 glsl_type::get_record_instance(fields
, decl_count
, this->name
);
4834 if (!state
->symbols
->add_type(name
, t
)) {
4835 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
4837 const glsl_type
**s
= reralloc(state
, state
->user_structures
,
4839 state
->num_user_structures
+ 1);
4841 s
[state
->num_user_structures
] = t
;
4842 state
->user_structures
= s
;
4843 state
->num_user_structures
++;
4847 state
->struct_specifier_depth
--;
4849 /* Structure type definitions do not have r-values.
4856 * Visitor class which detects whether a given interface block has been used.
4858 class interface_block_usage_visitor
: public ir_hierarchical_visitor
4861 interface_block_usage_visitor(ir_variable_mode mode
, const glsl_type
*block
)
4862 : mode(mode
), block(block
), found(false)
4866 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
4868 if (ir
->var
->data
.mode
== mode
&& ir
->var
->get_interface_type() == block
) {
4872 return visit_continue
;
4875 bool usage_found() const
4881 ir_variable_mode mode
;
4882 const glsl_type
*block
;
4888 ast_interface_block::hir(exec_list
*instructions
,
4889 struct _mesa_glsl_parse_state
*state
)
4891 YYLTYPE loc
= this->get_location();
4893 /* The ast_interface_block has a list of ast_declarator_lists. We
4894 * need to turn those into ir_variables with an association
4895 * with this uniform block.
4897 enum glsl_interface_packing packing
;
4898 if (this->layout
.flags
.q
.shared
) {
4899 packing
= GLSL_INTERFACE_PACKING_SHARED
;
4900 } else if (this->layout
.flags
.q
.packed
) {
4901 packing
= GLSL_INTERFACE_PACKING_PACKED
;
4903 /* The default layout is std140.
4905 packing
= GLSL_INTERFACE_PACKING_STD140
;
4908 ir_variable_mode var_mode
;
4909 const char *iface_type_name
;
4910 if (this->layout
.flags
.q
.in
) {
4911 var_mode
= ir_var_shader_in
;
4912 iface_type_name
= "in";
4913 } else if (this->layout
.flags
.q
.out
) {
4914 var_mode
= ir_var_shader_out
;
4915 iface_type_name
= "out";
4916 } else if (this->layout
.flags
.q
.uniform
) {
4917 var_mode
= ir_var_uniform
;
4918 iface_type_name
= "uniform";
4920 var_mode
= ir_var_auto
;
4921 iface_type_name
= "UNKNOWN";
4922 assert(!"interface block layout qualifier not found!");
4925 bool redeclaring_per_vertex
= strcmp(this->block_name
, "gl_PerVertex") == 0;
4926 bool block_row_major
= this->layout
.flags
.q
.row_major
;
4927 exec_list declared_variables
;
4928 glsl_struct_field
*fields
;
4929 unsigned int num_variables
=
4930 ast_process_structure_or_interface_block(&declared_variables
,
4932 &this->declarations
,
4937 redeclaring_per_vertex
,
4940 if (!redeclaring_per_vertex
)
4941 validate_identifier(this->block_name
, loc
, state
);
4943 const glsl_type
*earlier_per_vertex
= NULL
;
4944 if (redeclaring_per_vertex
) {
4945 /* Find the previous declaration of gl_PerVertex. If we're redeclaring
4946 * the named interface block gl_in, we can find it by looking at the
4947 * previous declaration of gl_in. Otherwise we can find it by looking
4948 * at the previous decalartion of any of the built-in outputs,
4951 * Also check that the instance name and array-ness of the redeclaration
4955 case ir_var_shader_in
:
4956 if (ir_variable
*earlier_gl_in
=
4957 state
->symbols
->get_variable("gl_in")) {
4958 earlier_per_vertex
= earlier_gl_in
->get_interface_type();
4960 _mesa_glsl_error(&loc
, state
,
4961 "redeclaration of gl_PerVertex input not allowed "
4963 _mesa_shader_stage_to_string(state
->stage
));
4965 if (this->instance_name
== NULL
||
4966 strcmp(this->instance_name
, "gl_in") != 0 || this->array_specifier
== NULL
) {
4967 _mesa_glsl_error(&loc
, state
,
4968 "gl_PerVertex input must be redeclared as "
4972 case ir_var_shader_out
:
4973 if (ir_variable
*earlier_gl_Position
=
4974 state
->symbols
->get_variable("gl_Position")) {
4975 earlier_per_vertex
= earlier_gl_Position
->get_interface_type();
4977 _mesa_glsl_error(&loc
, state
,
4978 "redeclaration of gl_PerVertex output not "
4979 "allowed in the %s shader",
4980 _mesa_shader_stage_to_string(state
->stage
));
4982 if (this->instance_name
!= NULL
) {
4983 _mesa_glsl_error(&loc
, state
,
4984 "gl_PerVertex input may not be redeclared with "
4985 "an instance name");
4989 _mesa_glsl_error(&loc
, state
,
4990 "gl_PerVertex must be declared as an input or an "
4995 if (earlier_per_vertex
== NULL
) {
4996 /* An error has already been reported. Bail out to avoid null
4997 * dereferences later in this function.
5002 /* Copy locations from the old gl_PerVertex interface block. */
5003 for (unsigned i
= 0; i
< num_variables
; i
++) {
5004 int j
= earlier_per_vertex
->field_index(fields
[i
].name
);
5006 _mesa_glsl_error(&loc
, state
,
5007 "redeclaration of gl_PerVertex must be a subset "
5008 "of the built-in members of gl_PerVertex");
5010 fields
[i
].location
=
5011 earlier_per_vertex
->fields
.structure
[j
].location
;
5012 fields
[i
].interpolation
=
5013 earlier_per_vertex
->fields
.structure
[j
].interpolation
;
5014 fields
[i
].centroid
=
5015 earlier_per_vertex
->fields
.structure
[j
].centroid
;
5017 earlier_per_vertex
->fields
.structure
[j
].sample
;
5021 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10
5024 * If a built-in interface block is redeclared, it must appear in
5025 * the shader before any use of any member included in the built-in
5026 * declaration, or a compilation error will result.
5028 * This appears to be a clarification to the behaviour established for
5029 * gl_PerVertex by GLSL 1.50, therefore we implement this behaviour
5030 * regardless of GLSL version.
5032 interface_block_usage_visitor
v(var_mode
, earlier_per_vertex
);
5033 v
.run(instructions
);
5034 if (v
.usage_found()) {
5035 _mesa_glsl_error(&loc
, state
,
5036 "redeclaration of a built-in interface block must "
5037 "appear before any use of any member of the "
5042 const glsl_type
*block_type
=
5043 glsl_type::get_interface_instance(fields
,
5048 if (!state
->symbols
->add_interface(block_type
->name
, block_type
, var_mode
)) {
5049 YYLTYPE loc
= this->get_location();
5050 _mesa_glsl_error(&loc
, state
, "interface block `%s' with type `%s' "
5051 "already taken in the current scope",
5052 this->block_name
, iface_type_name
);
5055 /* Since interface blocks cannot contain statements, it should be
5056 * impossible for the block to generate any instructions.
5058 assert(declared_variables
.is_empty());
5060 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
5062 * Geometry shader input variables get the per-vertex values written
5063 * out by vertex shader output variables of the same names. Since a
5064 * geometry shader operates on a set of vertices, each input varying
5065 * variable (or input block, see interface blocks below) needs to be
5066 * declared as an array.
5068 if (state
->stage
== MESA_SHADER_GEOMETRY
&& this->array_specifier
== NULL
&&
5069 var_mode
== ir_var_shader_in
) {
5070 _mesa_glsl_error(&loc
, state
, "geometry shader inputs must be arrays");
5073 /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
5076 * "If an instance name (instance-name) is used, then it puts all the
5077 * members inside a scope within its own name space, accessed with the
5078 * field selector ( . ) operator (analogously to structures)."
5080 if (this->instance_name
) {
5081 if (redeclaring_per_vertex
) {
5082 /* When a built-in in an unnamed interface block is redeclared,
5083 * get_variable_being_redeclared() calls
5084 * check_builtin_array_max_size() to make sure that built-in array
5085 * variables aren't redeclared to illegal sizes. But we're looking
5086 * at a redeclaration of a named built-in interface block. So we
5087 * have to manually call check_builtin_array_max_size() for all parts
5088 * of the interface that are arrays.
5090 for (unsigned i
= 0; i
< num_variables
; i
++) {
5091 if (fields
[i
].type
->is_array()) {
5092 const unsigned size
= fields
[i
].type
->array_size();
5093 check_builtin_array_max_size(fields
[i
].name
, size
, loc
, state
);
5097 validate_identifier(this->instance_name
, loc
, state
);
5102 if (this->array_specifier
!= NULL
) {
5103 /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
5105 * For uniform blocks declared an array, each individual array
5106 * element corresponds to a separate buffer object backing one
5107 * instance of the block. As the array size indicates the number
5108 * of buffer objects needed, uniform block array declarations
5109 * must specify an array size.
5111 * And a few paragraphs later:
5113 * Geometry shader input blocks must be declared as arrays and
5114 * follow the array declaration and linking rules for all
5115 * geometry shader inputs. All other input and output block
5116 * arrays must specify an array size.
5118 * The upshot of this is that the only circumstance where an
5119 * interface array size *doesn't* need to be specified is on a
5120 * geometry shader input.
5122 if (this->array_specifier
->is_unsized_array
&&
5123 (state
->stage
!= MESA_SHADER_GEOMETRY
|| !this->layout
.flags
.q
.in
)) {
5124 _mesa_glsl_error(&loc
, state
,
5125 "only geometry shader inputs may be unsized "
5126 "instance block arrays");
5130 const glsl_type
*block_array_type
=
5131 process_array_type(&loc
, block_type
, this->array_specifier
, state
);
5133 var
= new(state
) ir_variable(block_array_type
,
5134 this->instance_name
,
5137 var
= new(state
) ir_variable(block_type
,
5138 this->instance_name
,
5142 if (state
->stage
== MESA_SHADER_GEOMETRY
&& var_mode
== ir_var_shader_in
)
5143 handle_geometry_shader_input_decl(state
, loc
, var
);
5145 if (ir_variable
*earlier
=
5146 state
->symbols
->get_variable(this->instance_name
)) {
5147 if (!redeclaring_per_vertex
) {
5148 _mesa_glsl_error(&loc
, state
, "`%s' redeclared",
5149 this->instance_name
);
5151 earlier
->data
.how_declared
= ir_var_declared_normally
;
5152 earlier
->type
= var
->type
;
5153 earlier
->reinit_interface_type(block_type
);
5156 state
->symbols
->add_variable(var
);
5157 instructions
->push_tail(var
);
5160 /* In order to have an array size, the block must also be declared with
5163 assert(this->array_specifier
== NULL
);
5165 for (unsigned i
= 0; i
< num_variables
; i
++) {
5167 new(state
) ir_variable(fields
[i
].type
,
5168 ralloc_strdup(state
, fields
[i
].name
),
5170 var
->data
.interpolation
= fields
[i
].interpolation
;
5171 var
->data
.centroid
= fields
[i
].centroid
;
5172 var
->data
.sample
= fields
[i
].sample
;
5173 var
->init_interface_type(block_type
);
5175 if (redeclaring_per_vertex
) {
5176 ir_variable
*earlier
=
5177 get_variable_being_redeclared(var
, loc
, state
,
5178 true /* allow_all_redeclarations */);
5179 if (strncmp(var
->name
, "gl_", 3) != 0 || earlier
== NULL
) {
5180 _mesa_glsl_error(&loc
, state
,
5181 "redeclaration of gl_PerVertex can only "
5182 "include built-in variables");
5183 } else if (earlier
->data
.how_declared
== ir_var_declared_normally
) {
5184 _mesa_glsl_error(&loc
, state
,
5185 "`%s' has already been redeclared", var
->name
);
5187 earlier
->data
.how_declared
= ir_var_declared_in_block
;
5188 earlier
->reinit_interface_type(block_type
);
5193 if (state
->symbols
->get_variable(var
->name
) != NULL
)
5194 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
5196 /* Propagate the "binding" keyword into this UBO's fields;
5197 * the UBO declaration itself doesn't get an ir_variable unless it
5198 * has an instance name. This is ugly.
5200 var
->data
.explicit_binding
= this->layout
.flags
.q
.explicit_binding
;
5201 var
->data
.binding
= this->layout
.binding
;
5203 state
->symbols
->add_variable(var
);
5204 instructions
->push_tail(var
);
5207 if (redeclaring_per_vertex
&& block_type
!= earlier_per_vertex
) {
5208 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec:
5210 * It is also a compilation error ... to redeclare a built-in
5211 * block and then use a member from that built-in block that was
5212 * not included in the redeclaration.
5214 * This appears to be a clarification to the behaviour established
5215 * for gl_PerVertex by GLSL 1.50, therefore we implement this
5216 * behaviour regardless of GLSL version.
5218 * To prevent the shader from using a member that was not included in
5219 * the redeclaration, we disable any ir_variables that are still
5220 * associated with the old declaration of gl_PerVertex (since we've
5221 * already updated all of the variables contained in the new
5222 * gl_PerVertex to point to it).
5224 * As a side effect this will prevent
5225 * validate_intrastage_interface_blocks() from getting confused and
5226 * thinking there are conflicting definitions of gl_PerVertex in the
5229 foreach_list_safe(node
, instructions
) {
5230 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
5232 var
->get_interface_type() == earlier_per_vertex
&&
5233 var
->data
.mode
== var_mode
) {
5234 if (var
->data
.how_declared
== ir_var_declared_normally
) {
5235 _mesa_glsl_error(&loc
, state
,
5236 "redeclaration of gl_PerVertex cannot "
5237 "follow a redeclaration of `%s'",
5240 state
->symbols
->disable_variable(var
->name
);
5252 ast_gs_input_layout::hir(exec_list
*instructions
,
5253 struct _mesa_glsl_parse_state
*state
)
5255 YYLTYPE loc
= this->get_location();
5257 /* If any geometry input layout declaration preceded this one, make sure it
5258 * was consistent with this one.
5260 if (state
->gs_input_prim_type_specified
&&
5261 state
->gs_input_prim_type
!= this->prim_type
) {
5262 _mesa_glsl_error(&loc
, state
,
5263 "geometry shader input layout does not match"
5264 " previous declaration");
5268 /* If any shader inputs occurred before this declaration and specified an
5269 * array size, make sure the size they specified is consistent with the
5272 unsigned num_vertices
= vertices_per_prim(this->prim_type
);
5273 if (state
->gs_input_size
!= 0 && state
->gs_input_size
!= num_vertices
) {
5274 _mesa_glsl_error(&loc
, state
,
5275 "this geometry shader input layout implies %u vertices"
5276 " per primitive, but a previous input is declared"
5277 " with size %u", num_vertices
, state
->gs_input_size
);
5281 state
->gs_input_prim_type_specified
= true;
5282 state
->gs_input_prim_type
= this->prim_type
;
5284 /* If any shader inputs occurred before this declaration and did not
5285 * specify an array size, their size is determined now.
5287 foreach_list (node
, instructions
) {
5288 ir_variable
*var
= ((ir_instruction
*) node
)->as_variable();
5289 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_in
)
5292 /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
5296 if (var
->type
->is_unsized_array()) {
5297 if (var
->data
.max_array_access
>= num_vertices
) {
5298 _mesa_glsl_error(&loc
, state
,
5299 "this geometry shader input layout implies %u"
5300 " vertices, but an access to element %u of input"
5301 " `%s' already exists", num_vertices
,
5302 var
->data
.max_array_access
, var
->name
);
5304 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
5315 ast_cs_input_layout::hir(exec_list
*instructions
,
5316 struct _mesa_glsl_parse_state
*state
)
5318 YYLTYPE loc
= this->get_location();
5320 /* If any compute input layout declaration preceded this one, make sure it
5321 * was consistent with this one.
5323 if (state
->cs_input_local_size_specified
) {
5324 for (int i
= 0; i
< 3; i
++) {
5325 if (state
->cs_input_local_size
[i
] != this->local_size
[i
]) {
5326 _mesa_glsl_error(&loc
, state
,
5327 "compute shader input layout does not match"
5328 " previous declaration");
5334 /* From the ARB_compute_shader specification:
5336 * If the local size of the shader in any dimension is greater
5337 * than the maximum size supported by the implementation for that
5338 * dimension, a compile-time error results.
5340 * It is not clear from the spec how the error should be reported if
5341 * the total size of the work group exceeds
5342 * MAX_COMPUTE_WORK_GROUP_INVOCATIONS, but it seems reasonable to
5343 * report it at compile time as well.
5345 GLuint64 total_invocations
= 1;
5346 for (int i
= 0; i
< 3; i
++) {
5347 if (this->local_size
[i
] > state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]) {
5348 _mesa_glsl_error(&loc
, state
,
5349 "local_size_%c exceeds MAX_COMPUTE_WORK_GROUP_SIZE"
5351 state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]);
5354 total_invocations
*= this->local_size
[i
];
5355 if (total_invocations
>
5356 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
) {
5357 _mesa_glsl_error(&loc
, state
,
5358 "product of local_sizes exceeds "
5359 "MAX_COMPUTE_WORK_GROUP_INVOCATIONS (%d)",
5360 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
);
5365 state
->cs_input_local_size_specified
= true;
5366 for (int i
= 0; i
< 3; i
++)
5367 state
->cs_input_local_size
[i
] = this->local_size
[i
];
5369 /* We may now declare the built-in constant gl_WorkGroupSize (see
5370 * builtin_variable_generator::generate_constants() for why we didn't
5371 * declare it earlier).
5373 ir_variable
*var
= new(state
->symbols
)
5374 ir_variable(glsl_type::ivec3_type
, "gl_WorkGroupSize", ir_var_auto
);
5375 var
->data
.how_declared
= ir_var_declared_implicitly
;
5376 var
->data
.read_only
= true;
5377 instructions
->push_tail(var
);
5378 state
->symbols
->add_variable(var
);
5379 ir_constant_data data
;
5380 memset(&data
, 0, sizeof(data
));
5381 for (int i
= 0; i
< 3; i
++)
5382 data
.i
[i
] = this->local_size
[i
];
5383 var
->constant_value
= new(var
) ir_constant(glsl_type::ivec3_type
, &data
);
5384 var
->constant_initializer
=
5385 new(var
) ir_constant(glsl_type::ivec3_type
, &data
);
5386 var
->data
.has_initializer
= true;
5393 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
5394 exec_list
*instructions
)
5396 bool gl_FragColor_assigned
= false;
5397 bool gl_FragData_assigned
= false;
5398 bool user_defined_fs_output_assigned
= false;
5399 ir_variable
*user_defined_fs_output
= NULL
;
5401 /* It would be nice to have proper location information. */
5403 memset(&loc
, 0, sizeof(loc
));
5405 foreach_list(node
, instructions
) {
5406 ir_variable
*var
= ((ir_instruction
*)node
)->as_variable();
5408 if (!var
|| !var
->data
.assigned
)
5411 if (strcmp(var
->name
, "gl_FragColor") == 0)
5412 gl_FragColor_assigned
= true;
5413 else if (strcmp(var
->name
, "gl_FragData") == 0)
5414 gl_FragData_assigned
= true;
5415 else if (strncmp(var
->name
, "gl_", 3) != 0) {
5416 if (state
->stage
== MESA_SHADER_FRAGMENT
&&
5417 var
->data
.mode
== ir_var_shader_out
) {
5418 user_defined_fs_output_assigned
= true;
5419 user_defined_fs_output
= var
;
5424 /* From the GLSL 1.30 spec:
5426 * "If a shader statically assigns a value to gl_FragColor, it
5427 * may not assign a value to any element of gl_FragData. If a
5428 * shader statically writes a value to any element of
5429 * gl_FragData, it may not assign a value to
5430 * gl_FragColor. That is, a shader may assign values to either
5431 * gl_FragColor or gl_FragData, but not both. Multiple shaders
5432 * linked together must also consistently write just one of
5433 * these variables. Similarly, if user declared output
5434 * variables are in use (statically assigned to), then the
5435 * built-in variables gl_FragColor and gl_FragData may not be
5436 * assigned to. These incorrect usages all generate compile
5439 if (gl_FragColor_assigned
&& gl_FragData_assigned
) {
5440 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
5441 "`gl_FragColor' and `gl_FragData'");
5442 } else if (gl_FragColor_assigned
&& user_defined_fs_output_assigned
) {
5443 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
5444 "`gl_FragColor' and `%s'",
5445 user_defined_fs_output
->name
);
5446 } else if (gl_FragData_assigned
&& user_defined_fs_output_assigned
) {
5447 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
5448 "`gl_FragData' and `%s'",
5449 user_defined_fs_output
->name
);
5455 remove_per_vertex_blocks(exec_list
*instructions
,
5456 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
)
5458 /* Find the gl_PerVertex interface block of the appropriate (in/out) mode,
5459 * if it exists in this shader type.
5461 const glsl_type
*per_vertex
= NULL
;
5463 case ir_var_shader_in
:
5464 if (ir_variable
*gl_in
= state
->symbols
->get_variable("gl_in"))
5465 per_vertex
= gl_in
->get_interface_type();
5467 case ir_var_shader_out
:
5468 if (ir_variable
*gl_Position
=
5469 state
->symbols
->get_variable("gl_Position")) {
5470 per_vertex
= gl_Position
->get_interface_type();
5474 assert(!"Unexpected mode");
5478 /* If we didn't find a built-in gl_PerVertex interface block, then we don't
5479 * need to do anything.
5481 if (per_vertex
== NULL
)
5484 /* If the interface block is used by the shader, then we don't need to do
5487 interface_block_usage_visitor
v(mode
, per_vertex
);
5488 v
.run(instructions
);
5489 if (v
.usage_found())
5492 /* Remove any ir_variable declarations that refer to the interface block
5495 foreach_list_safe(node
, instructions
) {
5496 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
5497 if (var
!= NULL
&& var
->get_interface_type() == per_vertex
&&
5498 var
->data
.mode
== mode
) {
5499 state
->symbols
->disable_variable(var
->name
);