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_IMAGE
:
973 case GLSL_TYPE_INTERFACE
:
974 case GLSL_TYPE_ATOMIC_UINT
:
975 /* I assume a comparison of a struct containing a sampler just
976 * ignores the sampler present in the type.
982 cmp
= new(mem_ctx
) ir_constant(true);
987 /* For logical operations, we want to ensure that the operands are
988 * scalar booleans. If it isn't, emit an error and return a constant
989 * boolean to avoid triggering cascading error messages.
992 get_scalar_boolean_operand(exec_list
*instructions
,
993 struct _mesa_glsl_parse_state
*state
,
994 ast_expression
*parent_expr
,
996 const char *operand_name
,
999 ast_expression
*expr
= parent_expr
->subexpressions
[operand
];
1001 ir_rvalue
*val
= expr
->hir(instructions
, state
);
1003 if (val
->type
->is_boolean() && val
->type
->is_scalar())
1006 if (!*error_emitted
) {
1007 YYLTYPE loc
= expr
->get_location();
1008 _mesa_glsl_error(&loc
, state
, "%s of `%s' must be scalar boolean",
1010 parent_expr
->operator_string(parent_expr
->oper
));
1011 *error_emitted
= true;
1014 return new(ctx
) ir_constant(true);
1018 * If name refers to a builtin array whose maximum allowed size is less than
1019 * size, report an error and return true. Otherwise return false.
1022 check_builtin_array_max_size(const char *name
, unsigned size
,
1023 YYLTYPE loc
, struct _mesa_glsl_parse_state
*state
)
1025 if ((strcmp("gl_TexCoord", name
) == 0)
1026 && (size
> state
->Const
.MaxTextureCoords
)) {
1027 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1029 * "The size [of gl_TexCoord] can be at most
1030 * gl_MaxTextureCoords."
1032 _mesa_glsl_error(&loc
, state
, "`gl_TexCoord' array size cannot "
1033 "be larger than gl_MaxTextureCoords (%u)",
1034 state
->Const
.MaxTextureCoords
);
1035 } else if (strcmp("gl_ClipDistance", name
) == 0
1036 && size
> state
->Const
.MaxClipPlanes
) {
1037 /* From section 7.1 (Vertex Shader Special Variables) of the
1040 * "The gl_ClipDistance array is predeclared as unsized and
1041 * must be sized by the shader either redeclaring it with a
1042 * size or indexing it only with integral constant
1043 * expressions. ... The size can be at most
1044 * gl_MaxClipDistances."
1046 _mesa_glsl_error(&loc
, state
, "`gl_ClipDistance' array size cannot "
1047 "be larger than gl_MaxClipDistances (%u)",
1048 state
->Const
.MaxClipPlanes
);
1053 * Create the constant 1, of a which is appropriate for incrementing and
1054 * decrementing values of the given GLSL type. For example, if type is vec4,
1055 * this creates a constant value of 1.0 having type float.
1057 * If the given type is invalid for increment and decrement operators, return
1058 * a floating point 1--the error will be detected later.
1061 constant_one_for_inc_dec(void *ctx
, const glsl_type
*type
)
1063 switch (type
->base_type
) {
1064 case GLSL_TYPE_UINT
:
1065 return new(ctx
) ir_constant((unsigned) 1);
1067 return new(ctx
) ir_constant(1);
1069 case GLSL_TYPE_FLOAT
:
1070 return new(ctx
) ir_constant(1.0f
);
1075 ast_expression::hir(exec_list
*instructions
,
1076 struct _mesa_glsl_parse_state
*state
)
1079 static const int operations
[AST_NUM_OPERATORS
] = {
1080 -1, /* ast_assign doesn't convert to ir_expression. */
1081 -1, /* ast_plus doesn't convert to ir_expression. */
1095 ir_binop_any_nequal
,
1105 /* Note: The following block of expression types actually convert
1106 * to multiple IR instructions.
1108 ir_binop_mul
, /* ast_mul_assign */
1109 ir_binop_div
, /* ast_div_assign */
1110 ir_binop_mod
, /* ast_mod_assign */
1111 ir_binop_add
, /* ast_add_assign */
1112 ir_binop_sub
, /* ast_sub_assign */
1113 ir_binop_lshift
, /* ast_ls_assign */
1114 ir_binop_rshift
, /* ast_rs_assign */
1115 ir_binop_bit_and
, /* ast_and_assign */
1116 ir_binop_bit_xor
, /* ast_xor_assign */
1117 ir_binop_bit_or
, /* ast_or_assign */
1119 -1, /* ast_conditional doesn't convert to ir_expression. */
1120 ir_binop_add
, /* ast_pre_inc. */
1121 ir_binop_sub
, /* ast_pre_dec. */
1122 ir_binop_add
, /* ast_post_inc. */
1123 ir_binop_sub
, /* ast_post_dec. */
1124 -1, /* ast_field_selection doesn't conv to ir_expression. */
1125 -1, /* ast_array_index doesn't convert to ir_expression. */
1126 -1, /* ast_function_call doesn't conv to ir_expression. */
1127 -1, /* ast_identifier doesn't convert to ir_expression. */
1128 -1, /* ast_int_constant doesn't convert to ir_expression. */
1129 -1, /* ast_uint_constant doesn't conv to ir_expression. */
1130 -1, /* ast_float_constant doesn't conv to ir_expression. */
1131 -1, /* ast_bool_constant doesn't conv to ir_expression. */
1132 -1, /* ast_sequence doesn't convert to ir_expression. */
1134 ir_rvalue
*result
= NULL
;
1136 const struct glsl_type
*type
; /* a temporary variable for switch cases */
1137 bool error_emitted
= false;
1140 loc
= this->get_location();
1142 switch (this->oper
) {
1144 assert(!"ast_aggregate: Should never get here.");
1148 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1149 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1151 result
= do_assignment(instructions
, state
,
1152 this->subexpressions
[0]->non_lvalue_description
,
1153 op
[0], op
[1], false,
1154 this->subexpressions
[0]->get_location());
1155 error_emitted
= result
->type
->is_error();
1160 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1162 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1164 error_emitted
= type
->is_error();
1170 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1172 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1174 error_emitted
= type
->is_error();
1176 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1184 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1185 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1187 type
= arithmetic_result_type(op
[0], op
[1],
1188 (this->oper
== ast_mul
),
1190 error_emitted
= type
->is_error();
1192 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1197 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1198 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1200 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1202 assert(operations
[this->oper
] == ir_binop_mod
);
1204 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1206 error_emitted
= type
->is_error();
1211 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1212 error_emitted
= true;
1215 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1216 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1217 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1219 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1221 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1228 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1229 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1231 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
1233 /* The relational operators must either generate an error or result
1234 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1236 assert(type
->is_error()
1237 || ((type
->base_type
== GLSL_TYPE_BOOL
)
1238 && type
->is_scalar()));
1240 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1242 error_emitted
= type
->is_error();
1247 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1248 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1250 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1252 * "The equality operators equal (==), and not equal (!=)
1253 * operate on all types. They result in a scalar Boolean. If
1254 * the operand types do not match, then there must be a
1255 * conversion from Section 4.1.10 "Implicit Conversions"
1256 * applied to one operand that can make them match, in which
1257 * case this conversion is done."
1259 if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
1260 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
1261 || (op
[0]->type
!= op
[1]->type
)) {
1262 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
1263 "type", (this->oper
== ast_equal
) ? "==" : "!=");
1264 error_emitted
= true;
1265 } else if ((op
[0]->type
->is_array() || op
[1]->type
->is_array()) &&
1266 !state
->check_version(120, 300, &loc
,
1267 "array comparisons forbidden")) {
1268 error_emitted
= true;
1269 } else if ((op
[0]->type
->contains_opaque() ||
1270 op
[1]->type
->contains_opaque())) {
1271 _mesa_glsl_error(&loc
, state
, "opaque type comparisons forbidden");
1272 error_emitted
= true;
1275 if (error_emitted
) {
1276 result
= new(ctx
) ir_constant(false);
1278 result
= do_comparison(ctx
, operations
[this->oper
], op
[0], op
[1]);
1279 assert(result
->type
== glsl_type::bool_type
);
1286 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1287 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1288 type
= bit_logic_result_type(op
[0]->type
, op
[1]->type
, this->oper
,
1290 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1292 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1296 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1298 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1299 error_emitted
= true;
1302 if (!op
[0]->type
->is_integer()) {
1303 _mesa_glsl_error(&loc
, state
, "operand of `~' must be an integer");
1304 error_emitted
= true;
1307 type
= error_emitted
? glsl_type::error_type
: op
[0]->type
;
1308 result
= new(ctx
) ir_expression(ir_unop_bit_not
, type
, op
[0], NULL
);
1311 case ast_logic_and
: {
1312 exec_list rhs_instructions
;
1313 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1314 "LHS", &error_emitted
);
1315 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1316 "RHS", &error_emitted
);
1318 if (rhs_instructions
.is_empty()) {
1319 result
= new(ctx
) ir_expression(ir_binop_logic_and
, op
[0], op
[1]);
1320 type
= result
->type
;
1322 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1325 instructions
->push_tail(tmp
);
1327 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1328 instructions
->push_tail(stmt
);
1330 stmt
->then_instructions
.append_list(&rhs_instructions
);
1331 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1332 ir_assignment
*const then_assign
=
1333 new(ctx
) ir_assignment(then_deref
, op
[1]);
1334 stmt
->then_instructions
.push_tail(then_assign
);
1336 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1337 ir_assignment
*const else_assign
=
1338 new(ctx
) ir_assignment(else_deref
, new(ctx
) ir_constant(false));
1339 stmt
->else_instructions
.push_tail(else_assign
);
1341 result
= new(ctx
) ir_dereference_variable(tmp
);
1347 case ast_logic_or
: {
1348 exec_list rhs_instructions
;
1349 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1350 "LHS", &error_emitted
);
1351 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1352 "RHS", &error_emitted
);
1354 if (rhs_instructions
.is_empty()) {
1355 result
= new(ctx
) ir_expression(ir_binop_logic_or
, op
[0], op
[1]);
1356 type
= result
->type
;
1358 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1361 instructions
->push_tail(tmp
);
1363 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1364 instructions
->push_tail(stmt
);
1366 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1367 ir_assignment
*const then_assign
=
1368 new(ctx
) ir_assignment(then_deref
, new(ctx
) ir_constant(true));
1369 stmt
->then_instructions
.push_tail(then_assign
);
1371 stmt
->else_instructions
.append_list(&rhs_instructions
);
1372 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1373 ir_assignment
*const else_assign
=
1374 new(ctx
) ir_assignment(else_deref
, op
[1]);
1375 stmt
->else_instructions
.push_tail(else_assign
);
1377 result
= new(ctx
) ir_dereference_variable(tmp
);
1384 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1386 * "The logical binary operators and (&&), or ( | | ), and
1387 * exclusive or (^^). They operate only on two Boolean
1388 * expressions and result in a Boolean expression."
1390 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0, "LHS",
1392 op
[1] = get_scalar_boolean_operand(instructions
, state
, this, 1, "RHS",
1395 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1400 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1401 "operand", &error_emitted
);
1403 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1407 case ast_mul_assign
:
1408 case ast_div_assign
:
1409 case ast_add_assign
:
1410 case ast_sub_assign
: {
1411 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1412 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1414 type
= arithmetic_result_type(op
[0], op
[1],
1415 (this->oper
== ast_mul_assign
),
1418 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1421 result
= do_assignment(instructions
, state
,
1422 this->subexpressions
[0]->non_lvalue_description
,
1423 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1424 this->subexpressions
[0]->get_location());
1425 error_emitted
= (op
[0]->type
->is_error());
1427 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1428 * explicitly test for this because none of the binary expression
1429 * operators allow array operands either.
1435 case ast_mod_assign
: {
1436 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1437 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1439 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1441 assert(operations
[this->oper
] == ir_binop_mod
);
1443 ir_rvalue
*temp_rhs
;
1444 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1447 result
= do_assignment(instructions
, state
,
1448 this->subexpressions
[0]->non_lvalue_description
,
1449 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1450 this->subexpressions
[0]->get_location());
1451 error_emitted
= type
->is_error();
1456 case ast_rs_assign
: {
1457 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1458 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1459 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1461 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1462 type
, op
[0], op
[1]);
1463 result
= do_assignment(instructions
, state
,
1464 this->subexpressions
[0]->non_lvalue_description
,
1465 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1466 this->subexpressions
[0]->get_location());
1467 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1471 case ast_and_assign
:
1472 case ast_xor_assign
:
1473 case ast_or_assign
: {
1474 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1475 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1476 type
= bit_logic_result_type(op
[0]->type
, op
[1]->type
, this->oper
,
1478 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1479 type
, op
[0], op
[1]);
1480 result
= do_assignment(instructions
, state
,
1481 this->subexpressions
[0]->non_lvalue_description
,
1482 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1483 this->subexpressions
[0]->get_location());
1484 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1488 case ast_conditional
: {
1489 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1491 * "The ternary selection operator (?:). It operates on three
1492 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1493 * first expression, which must result in a scalar Boolean."
1495 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1496 "condition", &error_emitted
);
1498 /* The :? operator is implemented by generating an anonymous temporary
1499 * followed by an if-statement. The last instruction in each branch of
1500 * the if-statement assigns a value to the anonymous temporary. This
1501 * temporary is the r-value of the expression.
1503 exec_list then_instructions
;
1504 exec_list else_instructions
;
1506 op
[1] = this->subexpressions
[1]->hir(&then_instructions
, state
);
1507 op
[2] = this->subexpressions
[2]->hir(&else_instructions
, state
);
1509 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1511 * "The second and third expressions can be any type, as
1512 * long their types match, or there is a conversion in
1513 * Section 4.1.10 "Implicit Conversions" that can be applied
1514 * to one of the expressions to make their types match. This
1515 * resulting matching type is the type of the entire
1518 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1519 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1520 || (op
[1]->type
!= op
[2]->type
)) {
1521 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1523 _mesa_glsl_error(& loc
, state
, "second and third operands of ?: "
1524 "operator must have matching types");
1525 error_emitted
= true;
1526 type
= glsl_type::error_type
;
1531 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1533 * "The second and third expressions must be the same type, but can
1534 * be of any type other than an array."
1536 if (type
->is_array() &&
1537 !state
->check_version(120, 300, &loc
,
1538 "second and third operands of ?: operator "
1539 "cannot be arrays")) {
1540 error_emitted
= true;
1543 ir_constant
*cond_val
= op
[0]->constant_expression_value();
1544 ir_constant
*then_val
= op
[1]->constant_expression_value();
1545 ir_constant
*else_val
= op
[2]->constant_expression_value();
1547 if (then_instructions
.is_empty()
1548 && else_instructions
.is_empty()
1549 && (cond_val
!= NULL
) && (then_val
!= NULL
) && (else_val
!= NULL
)) {
1550 result
= (cond_val
->value
.b
[0]) ? then_val
: else_val
;
1552 ir_variable
*const tmp
=
1553 new(ctx
) ir_variable(type
, "conditional_tmp", ir_var_temporary
);
1554 instructions
->push_tail(tmp
);
1556 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1557 instructions
->push_tail(stmt
);
1559 then_instructions
.move_nodes_to(& stmt
->then_instructions
);
1560 ir_dereference
*const then_deref
=
1561 new(ctx
) ir_dereference_variable(tmp
);
1562 ir_assignment
*const then_assign
=
1563 new(ctx
) ir_assignment(then_deref
, op
[1]);
1564 stmt
->then_instructions
.push_tail(then_assign
);
1566 else_instructions
.move_nodes_to(& stmt
->else_instructions
);
1567 ir_dereference
*const else_deref
=
1568 new(ctx
) ir_dereference_variable(tmp
);
1569 ir_assignment
*const else_assign
=
1570 new(ctx
) ir_assignment(else_deref
, op
[2]);
1571 stmt
->else_instructions
.push_tail(else_assign
);
1573 result
= new(ctx
) ir_dereference_variable(tmp
);
1580 this->non_lvalue_description
= (this->oper
== ast_pre_inc
)
1581 ? "pre-increment operation" : "pre-decrement operation";
1583 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1584 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1586 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1588 ir_rvalue
*temp_rhs
;
1589 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1592 result
= do_assignment(instructions
, state
,
1593 this->subexpressions
[0]->non_lvalue_description
,
1594 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1595 this->subexpressions
[0]->get_location());
1596 error_emitted
= op
[0]->type
->is_error();
1601 case ast_post_dec
: {
1602 this->non_lvalue_description
= (this->oper
== ast_post_inc
)
1603 ? "post-increment operation" : "post-decrement operation";
1604 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1605 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1607 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1609 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1611 ir_rvalue
*temp_rhs
;
1612 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1615 /* Get a temporary of a copy of the lvalue before it's modified.
1616 * This may get thrown away later.
1618 result
= get_lvalue_copy(instructions
, op
[0]->clone(ctx
, NULL
));
1620 (void)do_assignment(instructions
, state
,
1621 this->subexpressions
[0]->non_lvalue_description
,
1622 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1623 this->subexpressions
[0]->get_location());
1625 error_emitted
= op
[0]->type
->is_error();
1629 case ast_field_selection
:
1630 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1633 case ast_array_index
: {
1634 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1636 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1637 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1639 result
= _mesa_ast_array_index_to_hir(ctx
, state
, op
[0], op
[1],
1642 if (result
->type
->is_error())
1643 error_emitted
= true;
1648 case ast_function_call
:
1649 /* Should *NEVER* get here. ast_function_call should always be handled
1650 * by ast_function_expression::hir.
1655 case ast_identifier
: {
1656 /* ast_identifier can appear several places in a full abstract syntax
1657 * tree. This particular use must be at location specified in the grammar
1658 * as 'variable_identifier'.
1661 state
->symbols
->get_variable(this->primary_expression
.identifier
);
1664 var
->data
.used
= true;
1665 result
= new(ctx
) ir_dereference_variable(var
);
1667 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
1668 this->primary_expression
.identifier
);
1670 result
= ir_rvalue::error_value(ctx
);
1671 error_emitted
= true;
1676 case ast_int_constant
:
1677 result
= new(ctx
) ir_constant(this->primary_expression
.int_constant
);
1680 case ast_uint_constant
:
1681 result
= new(ctx
) ir_constant(this->primary_expression
.uint_constant
);
1684 case ast_float_constant
:
1685 result
= new(ctx
) ir_constant(this->primary_expression
.float_constant
);
1688 case ast_bool_constant
:
1689 result
= new(ctx
) ir_constant(bool(this->primary_expression
.bool_constant
));
1692 case ast_sequence
: {
1693 /* It should not be possible to generate a sequence in the AST without
1694 * any expressions in it.
1696 assert(!this->expressions
.is_empty());
1698 /* The r-value of a sequence is the last expression in the sequence. If
1699 * the other expressions in the sequence do not have side-effects (and
1700 * therefore add instructions to the instruction list), they get dropped
1703 exec_node
*previous_tail_pred
= NULL
;
1704 YYLTYPE previous_operand_loc
= loc
;
1706 foreach_list_typed (ast_node
, ast
, link
, &this->expressions
) {
1707 /* If one of the operands of comma operator does not generate any
1708 * code, we want to emit a warning. At each pass through the loop
1709 * previous_tail_pred will point to the last instruction in the
1710 * stream *before* processing the previous operand. Naturally,
1711 * instructions->tail_pred will point to the last instruction in the
1712 * stream *after* processing the previous operand. If the two
1713 * pointers match, then the previous operand had no effect.
1715 * The warning behavior here differs slightly from GCC. GCC will
1716 * only emit a warning if none of the left-hand operands have an
1717 * effect. However, it will emit a warning for each. I believe that
1718 * there are some cases in C (especially with GCC extensions) where
1719 * it is useful to have an intermediate step in a sequence have no
1720 * effect, but I don't think these cases exist in GLSL. Either way,
1721 * it would be a giant hassle to replicate that behavior.
1723 if (previous_tail_pred
== instructions
->tail_pred
) {
1724 _mesa_glsl_warning(&previous_operand_loc
, state
,
1725 "left-hand operand of comma expression has "
1729 /* tail_pred is directly accessed instead of using the get_tail()
1730 * method for performance reasons. get_tail() has extra code to
1731 * return NULL when the list is empty. We don't care about that
1732 * here, so using tail_pred directly is fine.
1734 previous_tail_pred
= instructions
->tail_pred
;
1735 previous_operand_loc
= ast
->get_location();
1737 result
= ast
->hir(instructions
, state
);
1740 /* Any errors should have already been emitted in the loop above.
1742 error_emitted
= true;
1746 type
= NULL
; /* use result->type, not type. */
1747 assert(result
!= NULL
);
1749 if (result
->type
->is_error() && !error_emitted
)
1750 _mesa_glsl_error(& loc
, state
, "type mismatch");
1757 ast_expression_statement::hir(exec_list
*instructions
,
1758 struct _mesa_glsl_parse_state
*state
)
1760 /* It is possible to have expression statements that don't have an
1761 * expression. This is the solitary semicolon:
1763 * for (i = 0; i < 5; i++)
1766 * In this case the expression will be NULL. Test for NULL and don't do
1767 * anything in that case.
1769 if (expression
!= NULL
)
1770 expression
->hir(instructions
, state
);
1772 /* Statements do not have r-values.
1779 ast_compound_statement::hir(exec_list
*instructions
,
1780 struct _mesa_glsl_parse_state
*state
)
1783 state
->symbols
->push_scope();
1785 foreach_list_typed (ast_node
, ast
, link
, &this->statements
)
1786 ast
->hir(instructions
, state
);
1789 state
->symbols
->pop_scope();
1791 /* Compound statements do not have r-values.
1797 * Evaluate the given exec_node (which should be an ast_node representing
1798 * a single array dimension) and return its integer value.
1801 process_array_size(exec_node
*node
,
1802 struct _mesa_glsl_parse_state
*state
)
1804 exec_list dummy_instructions
;
1806 ast_node
*array_size
= exec_node_data(ast_node
, node
, link
);
1807 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
,
1809 YYLTYPE loc
= array_size
->get_location();
1812 _mesa_glsl_error(& loc
, state
,
1813 "array size could not be resolved");
1817 if (!ir
->type
->is_integer()) {
1818 _mesa_glsl_error(& loc
, state
,
1819 "array size must be integer type");
1823 if (!ir
->type
->is_scalar()) {
1824 _mesa_glsl_error(& loc
, state
,
1825 "array size must be scalar type");
1829 ir_constant
*const size
= ir
->constant_expression_value();
1831 _mesa_glsl_error(& loc
, state
, "array size must be a "
1832 "constant valued expression");
1836 if (size
->value
.i
[0] <= 0) {
1837 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
1841 assert(size
->type
== ir
->type
);
1843 /* If the array size is const (and we've verified that
1844 * it is) then no instructions should have been emitted
1845 * when we converted it to HIR. If they were emitted,
1846 * then either the array size isn't const after all, or
1847 * we are emitting unnecessary instructions.
1849 assert(dummy_instructions
.is_empty());
1851 return size
->value
.u
[0];
1854 static const glsl_type
*
1855 process_array_type(YYLTYPE
*loc
, const glsl_type
*base
,
1856 ast_array_specifier
*array_specifier
,
1857 struct _mesa_glsl_parse_state
*state
)
1859 const glsl_type
*array_type
= base
;
1861 if (array_specifier
!= NULL
) {
1862 if (base
->is_array()) {
1864 /* From page 19 (page 25) of the GLSL 1.20 spec:
1866 * "Only one-dimensional arrays may be declared."
1868 if (!state
->ARB_arrays_of_arrays_enable
) {
1869 _mesa_glsl_error(loc
, state
,
1870 "invalid array of `%s'"
1871 "GL_ARB_arrays_of_arrays "
1872 "required for defining arrays of arrays",
1874 return glsl_type::error_type
;
1877 if (base
->length
== 0) {
1878 _mesa_glsl_error(loc
, state
,
1879 "only the outermost array dimension can "
1882 return glsl_type::error_type
;
1886 for (exec_node
*node
= array_specifier
->array_dimensions
.tail_pred
;
1887 !node
->is_head_sentinel(); node
= node
->prev
) {
1888 unsigned array_size
= process_array_size(node
, state
);
1889 array_type
= glsl_type::get_array_instance(array_type
,
1893 if (array_specifier
->is_unsized_array
)
1894 array_type
= glsl_type::get_array_instance(array_type
, 0);
1902 ast_type_specifier::glsl_type(const char **name
,
1903 struct _mesa_glsl_parse_state
*state
) const
1905 const struct glsl_type
*type
;
1907 type
= state
->symbols
->get_type(this->type_name
);
1908 *name
= this->type_name
;
1910 YYLTYPE loc
= this->get_location();
1911 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
1917 ast_fully_specified_type::glsl_type(const char **name
,
1918 struct _mesa_glsl_parse_state
*state
) const
1920 const struct glsl_type
*type
= this->specifier
->glsl_type(name
, state
);
1925 if (type
->base_type
== GLSL_TYPE_FLOAT
1927 && state
->stage
== MESA_SHADER_FRAGMENT
1928 && this->qualifier
.precision
== ast_precision_none
1929 && state
->symbols
->get_variable("#default precision") == NULL
) {
1930 YYLTYPE loc
= this->get_location();
1931 _mesa_glsl_error(&loc
, state
,
1932 "no precision specified this scope for type `%s'",
1940 * Determine whether a toplevel variable declaration declares a varying. This
1941 * function operates by examining the variable's mode and the shader target,
1942 * so it correctly identifies linkage variables regardless of whether they are
1943 * declared using the deprecated "varying" syntax or the new "in/out" syntax.
1945 * Passing a non-toplevel variable declaration (e.g. a function parameter) to
1946 * this function will produce undefined results.
1949 is_varying_var(ir_variable
*var
, gl_shader_stage target
)
1952 case MESA_SHADER_VERTEX
:
1953 return var
->data
.mode
== ir_var_shader_out
;
1954 case MESA_SHADER_FRAGMENT
:
1955 return var
->data
.mode
== ir_var_shader_in
;
1957 return var
->data
.mode
== ir_var_shader_out
|| var
->data
.mode
== ir_var_shader_in
;
1963 * Matrix layout qualifiers are only allowed on certain types
1966 validate_matrix_layout_for_type(struct _mesa_glsl_parse_state
*state
,
1968 const glsl_type
*type
,
1971 if (var
&& !var
->is_in_uniform_block()) {
1972 /* Layout qualifiers may only apply to interface blocks and fields in
1975 _mesa_glsl_error(loc
, state
,
1976 "uniform block layout qualifiers row_major and "
1977 "column_major may not be applied to variables "
1978 "outside of uniform blocks");
1979 } else if (!type
->is_matrix()) {
1980 /* The OpenGL ES 3.0 conformance tests did not originally allow
1981 * matrix layout qualifiers on non-matrices. However, the OpenGL
1982 * 4.4 and OpenGL ES 3.0 (revision TBD) specifications were
1983 * amended to specifically allow these layouts on all types. Emit
1984 * a warning so that people know their code may not be portable.
1986 _mesa_glsl_warning(loc
, state
,
1987 "uniform block layout qualifiers row_major and "
1988 "column_major applied to non-matrix types may "
1989 "be rejected by older compilers");
1990 } else if (type
->is_record()) {
1991 /* We allow 'layout(row_major)' on structure types because it's the only
1992 * way to get row-major layouts on matrices contained in structures.
1994 _mesa_glsl_warning(loc
, state
,
1995 "uniform block layout qualifiers row_major and "
1996 "column_major applied to structure types is not "
1997 "strictly conformant and may be rejected by other "
2003 validate_binding_qualifier(struct _mesa_glsl_parse_state
*state
,
2006 const ast_type_qualifier
*qual
)
2008 if (var
->data
.mode
!= ir_var_uniform
) {
2009 _mesa_glsl_error(loc
, state
,
2010 "the \"binding\" qualifier only applies to uniforms");
2014 if (qual
->binding
< 0) {
2015 _mesa_glsl_error(loc
, state
, "binding values must be >= 0");
2019 const struct gl_context
*const ctx
= state
->ctx
;
2020 unsigned elements
= var
->type
->is_array() ? var
->type
->length
: 1;
2021 unsigned max_index
= qual
->binding
+ elements
- 1;
2023 if (var
->type
->is_interface()) {
2024 /* UBOs. From page 60 of the GLSL 4.20 specification:
2025 * "If the binding point for any uniform block instance is less than zero,
2026 * or greater than or equal to the implementation-dependent maximum
2027 * number of uniform buffer bindings, a compilation error will occur.
2028 * When the binding identifier is used with a uniform block instanced as
2029 * an array of size N, all elements of the array from binding through
2030 * binding + N – 1 must be within this range."
2032 * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
2034 if (max_index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2035 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d UBOs exceeds "
2036 "the maximum number of UBO binding points (%d)",
2037 qual
->binding
, elements
,
2038 ctx
->Const
.MaxUniformBufferBindings
);
2041 } else if (var
->type
->is_sampler() ||
2042 (var
->type
->is_array() && var
->type
->fields
.array
->is_sampler())) {
2043 /* Samplers. From page 63 of the GLSL 4.20 specification:
2044 * "If the binding is less than zero, or greater than or equal to the
2045 * implementation-dependent maximum supported number of units, a
2046 * compilation error will occur. When the binding identifier is used
2047 * with an array of size N, all elements of the array from binding
2048 * through binding + N - 1 must be within this range."
2050 unsigned limit
= ctx
->Const
.Program
[state
->stage
].MaxTextureImageUnits
;
2052 if (max_index
>= limit
) {
2053 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d samplers "
2054 "exceeds the maximum number of texture image units "
2055 "(%d)", qual
->binding
, elements
, limit
);
2059 } else if (var
->type
->contains_atomic()) {
2060 assert(ctx
->Const
.MaxAtomicBufferBindings
<= MAX_COMBINED_ATOMIC_BUFFERS
);
2061 if (unsigned(qual
->binding
) >= ctx
->Const
.MaxAtomicBufferBindings
) {
2062 _mesa_glsl_error(loc
, state
, "layout(binding = %d) exceeds the "
2063 " maximum number of atomic counter buffer bindings"
2064 "(%d)", qual
->binding
,
2065 ctx
->Const
.MaxAtomicBufferBindings
);
2070 _mesa_glsl_error(loc
, state
,
2071 "the \"binding\" qualifier only applies to uniform "
2072 "blocks, samplers, atomic counters, or arrays thereof");
2080 static glsl_interp_qualifier
2081 interpret_interpolation_qualifier(const struct ast_type_qualifier
*qual
,
2082 ir_variable_mode mode
,
2083 struct _mesa_glsl_parse_state
*state
,
2086 glsl_interp_qualifier interpolation
;
2087 if (qual
->flags
.q
.flat
)
2088 interpolation
= INTERP_QUALIFIER_FLAT
;
2089 else if (qual
->flags
.q
.noperspective
)
2090 interpolation
= INTERP_QUALIFIER_NOPERSPECTIVE
;
2091 else if (qual
->flags
.q
.smooth
)
2092 interpolation
= INTERP_QUALIFIER_SMOOTH
;
2094 interpolation
= INTERP_QUALIFIER_NONE
;
2096 if (interpolation
!= INTERP_QUALIFIER_NONE
) {
2097 if (mode
!= ir_var_shader_in
&& mode
!= ir_var_shader_out
) {
2098 _mesa_glsl_error(loc
, state
,
2099 "interpolation qualifier `%s' can only be applied to "
2100 "shader inputs or outputs.",
2101 interpolation_string(interpolation
));
2105 if ((state
->stage
== MESA_SHADER_VERTEX
&& mode
== ir_var_shader_in
) ||
2106 (state
->stage
== MESA_SHADER_FRAGMENT
&& mode
== ir_var_shader_out
)) {
2107 _mesa_glsl_error(loc
, state
,
2108 "interpolation qualifier `%s' cannot be applied to "
2109 "vertex shader inputs or fragment shader outputs",
2110 interpolation_string(interpolation
));
2114 return interpolation
;
2119 validate_explicit_location(const struct ast_type_qualifier
*qual
,
2121 struct _mesa_glsl_parse_state
*state
,
2126 /* In the vertex shader only shader inputs can be given explicit
2129 * In the fragment shader only shader outputs can be given explicit
2132 switch (state
->stage
) {
2133 case MESA_SHADER_VERTEX
:
2134 if (var
->data
.mode
== ir_var_shader_in
) {
2135 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
2144 case MESA_SHADER_GEOMETRY
:
2145 _mesa_glsl_error(loc
, state
,
2146 "geometry shader variables cannot be given "
2147 "explicit locations");
2150 case MESA_SHADER_FRAGMENT
:
2151 if (var
->data
.mode
== ir_var_shader_out
) {
2152 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
2161 case MESA_SHADER_COMPUTE
:
2162 _mesa_glsl_error(loc
, state
,
2163 "compute shader variables cannot be given "
2164 "explicit locations");
2169 _mesa_glsl_error(loc
, state
,
2170 "%s cannot be given an explicit location in %s shader",
2172 _mesa_shader_stage_to_string(state
->stage
));
2174 var
->data
.explicit_location
= true;
2176 /* This bit of silliness is needed because invalid explicit locations
2177 * are supposed to be flagged during linking. Small negative values
2178 * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
2179 * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
2180 * The linker needs to be able to differentiate these cases. This
2181 * ensures that negative values stay negative.
2183 if (qual
->location
>= 0) {
2184 var
->data
.location
= (state
->stage
== MESA_SHADER_VERTEX
)
2185 ? (qual
->location
+ VERT_ATTRIB_GENERIC0
)
2186 : (qual
->location
+ FRAG_RESULT_DATA0
);
2188 var
->data
.location
= qual
->location
;
2191 if (qual
->flags
.q
.explicit_index
) {
2192 /* From the GLSL 4.30 specification, section 4.4.2 (Output
2193 * Layout Qualifiers):
2195 * "It is also a compile-time error if a fragment shader
2196 * sets a layout index to less than 0 or greater than 1."
2198 * Older specifications don't mandate a behavior; we take
2199 * this as a clarification and always generate the error.
2201 if (qual
->index
< 0 || qual
->index
> 1) {
2202 _mesa_glsl_error(loc
, state
,
2203 "explicit index may only be 0 or 1");
2205 var
->data
.explicit_index
= true;
2206 var
->data
.index
= qual
->index
;
2215 apply_image_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
2217 struct _mesa_glsl_parse_state
*state
,
2220 const glsl_type
*base_type
=
2221 (var
->type
->is_array() ? var
->type
->element_type() : var
->type
);
2223 if (base_type
->is_image()) {
2224 if (var
->data
.mode
!= ir_var_uniform
&&
2225 var
->data
.mode
!= ir_var_function_in
) {
2226 _mesa_glsl_error(loc
, state
, "image variables may only be declared as "
2227 "function parameters or uniform-qualified "
2228 "global variables");
2231 var
->data
.image
.read_only
|= qual
->flags
.q
.read_only
;
2232 var
->data
.image
.write_only
|= qual
->flags
.q
.write_only
;
2233 var
->data
.image
.coherent
|= qual
->flags
.q
.coherent
;
2234 var
->data
.image
._volatile
|= qual
->flags
.q
._volatile
;
2235 var
->data
.image
.restrict_flag
|= qual
->flags
.q
.restrict_flag
;
2236 var
->data
.read_only
= true;
2238 if (qual
->flags
.q
.explicit_image_format
) {
2239 if (var
->data
.mode
== ir_var_function_in
) {
2240 _mesa_glsl_error(loc
, state
, "format qualifiers cannot be "
2241 "used on image function parameters");
2244 if (qual
->image_base_type
!= base_type
->sampler_type
) {
2245 _mesa_glsl_error(loc
, state
, "format qualifier doesn't match the "
2246 "base data type of the image");
2249 var
->data
.image
.format
= qual
->image_format
;
2251 if (var
->data
.mode
== ir_var_uniform
&& !qual
->flags
.q
.write_only
) {
2252 _mesa_glsl_error(loc
, state
, "uniforms not qualified with "
2253 "`writeonly' must have a format layout "
2257 var
->data
.image
.format
= GL_NONE
;
2263 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
2265 struct _mesa_glsl_parse_state
*state
,
2269 STATIC_ASSERT(sizeof(qual
->flags
.q
) <= sizeof(qual
->flags
.i
));
2271 if (qual
->flags
.q
.invariant
) {
2272 if (var
->data
.used
) {
2273 _mesa_glsl_error(loc
, state
,
2274 "variable `%s' may not be redeclared "
2275 "`invariant' after being used",
2278 var
->data
.invariant
= 1;
2282 if (qual
->flags
.q
.constant
|| qual
->flags
.q
.attribute
2283 || qual
->flags
.q
.uniform
2284 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
2285 var
->data
.read_only
= 1;
2287 if (qual
->flags
.q
.centroid
)
2288 var
->data
.centroid
= 1;
2290 if (qual
->flags
.q
.sample
)
2291 var
->data
.sample
= 1;
2293 if (qual
->flags
.q
.attribute
&& state
->stage
!= MESA_SHADER_VERTEX
) {
2294 var
->type
= glsl_type::error_type
;
2295 _mesa_glsl_error(loc
, state
,
2296 "`attribute' variables may not be declared in the "
2298 _mesa_shader_stage_to_string(state
->stage
));
2301 /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
2303 * "However, the const qualifier cannot be used with out or inout."
2305 * The same section of the GLSL 4.40 spec further clarifies this saying:
2307 * "The const qualifier cannot be used with out or inout, or a
2308 * compile-time error results."
2310 if (is_parameter
&& qual
->flags
.q
.constant
&& qual
->flags
.q
.out
) {
2311 _mesa_glsl_error(loc
, state
,
2312 "`const' may not be applied to `out' or `inout' "
2313 "function parameters");
2316 /* If there is no qualifier that changes the mode of the variable, leave
2317 * the setting alone.
2319 if (qual
->flags
.q
.in
&& qual
->flags
.q
.out
)
2320 var
->data
.mode
= ir_var_function_inout
;
2321 else if (qual
->flags
.q
.in
)
2322 var
->data
.mode
= is_parameter
? ir_var_function_in
: ir_var_shader_in
;
2323 else if (qual
->flags
.q
.attribute
2324 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
2325 var
->data
.mode
= ir_var_shader_in
;
2326 else if (qual
->flags
.q
.out
)
2327 var
->data
.mode
= is_parameter
? ir_var_function_out
: ir_var_shader_out
;
2328 else if (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_VERTEX
))
2329 var
->data
.mode
= ir_var_shader_out
;
2330 else if (qual
->flags
.q
.uniform
)
2331 var
->data
.mode
= ir_var_uniform
;
2333 if (!is_parameter
&& is_varying_var(var
, state
->stage
)) {
2334 /* User-defined ins/outs are not permitted in compute shaders. */
2335 if (state
->stage
== MESA_SHADER_COMPUTE
) {
2336 _mesa_glsl_error(loc
, state
,
2337 "user-defined input and output variables are not "
2338 "permitted in compute shaders");
2341 /* This variable is being used to link data between shader stages (in
2342 * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
2343 * that is allowed for such purposes.
2345 * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
2347 * "The varying qualifier can be used only with the data types
2348 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
2351 * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
2352 * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
2354 * "Fragment inputs can only be signed and unsigned integers and
2355 * integer vectors, float, floating-point vectors, matrices, or
2356 * arrays of these. Structures cannot be input.
2358 * Similar text exists in the section on vertex shader outputs.
2360 * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
2361 * 3.00 spec allows structs as well. Varying structs are also allowed
2364 switch (var
->type
->get_scalar_type()->base_type
) {
2365 case GLSL_TYPE_FLOAT
:
2366 /* Ok in all GLSL versions */
2368 case GLSL_TYPE_UINT
:
2370 if (state
->is_version(130, 300))
2372 _mesa_glsl_error(loc
, state
,
2373 "varying variables must be of base type float in %s",
2374 state
->get_version_string());
2376 case GLSL_TYPE_STRUCT
:
2377 if (state
->is_version(150, 300))
2379 _mesa_glsl_error(loc
, state
,
2380 "varying variables may not be of type struct");
2383 _mesa_glsl_error(loc
, state
, "illegal type for a varying variable");
2388 if (state
->all_invariant
&& (state
->current_function
== NULL
)) {
2389 switch (state
->stage
) {
2390 case MESA_SHADER_VERTEX
:
2391 if (var
->data
.mode
== ir_var_shader_out
)
2392 var
->data
.invariant
= true;
2394 case MESA_SHADER_GEOMETRY
:
2395 if ((var
->data
.mode
== ir_var_shader_in
)
2396 || (var
->data
.mode
== ir_var_shader_out
))
2397 var
->data
.invariant
= true;
2399 case MESA_SHADER_FRAGMENT
:
2400 if (var
->data
.mode
== ir_var_shader_in
)
2401 var
->data
.invariant
= true;
2403 case MESA_SHADER_COMPUTE
:
2404 /* Invariance isn't meaningful in compute shaders. */
2409 var
->data
.interpolation
=
2410 interpret_interpolation_qualifier(qual
, (ir_variable_mode
) var
->data
.mode
,
2413 var
->data
.pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
2414 var
->data
.origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
2415 if ((qual
->flags
.q
.origin_upper_left
|| qual
->flags
.q
.pixel_center_integer
)
2416 && (strcmp(var
->name
, "gl_FragCoord") != 0)) {
2417 const char *const qual_string
= (qual
->flags
.q
.origin_upper_left
)
2418 ? "origin_upper_left" : "pixel_center_integer";
2420 _mesa_glsl_error(loc
, state
,
2421 "layout qualifier `%s' can only be applied to "
2422 "fragment shader input `gl_FragCoord'",
2426 if (qual
->flags
.q
.explicit_location
) {
2427 validate_explicit_location(qual
, var
, state
, loc
);
2428 } else if (qual
->flags
.q
.explicit_index
) {
2429 _mesa_glsl_error(loc
, state
,
2430 "explicit index requires explicit location");
2433 if (qual
->flags
.q
.explicit_binding
&&
2434 validate_binding_qualifier(state
, loc
, var
, qual
)) {
2435 var
->data
.explicit_binding
= true;
2436 var
->data
.binding
= qual
->binding
;
2439 if (var
->type
->contains_atomic()) {
2440 if (var
->data
.mode
== ir_var_uniform
) {
2441 if (var
->data
.explicit_binding
) {
2443 &state
->atomic_counter_offsets
[var
->data
.binding
];
2445 if (*offset
% ATOMIC_COUNTER_SIZE
)
2446 _mesa_glsl_error(loc
, state
,
2447 "misaligned atomic counter offset");
2449 var
->data
.atomic
.offset
= *offset
;
2450 *offset
+= var
->type
->atomic_size();
2453 _mesa_glsl_error(loc
, state
,
2454 "atomic counters require explicit binding point");
2456 } else if (var
->data
.mode
!= ir_var_function_in
) {
2457 _mesa_glsl_error(loc
, state
, "atomic counters may only be declared as "
2458 "function parameters or uniform-qualified "
2459 "global variables");
2463 /* Does the declaration use the deprecated 'attribute' or 'varying'
2466 const bool uses_deprecated_qualifier
= qual
->flags
.q
.attribute
2467 || qual
->flags
.q
.varying
;
2469 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
2470 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
2471 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
2472 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
2473 * These extensions and all following extensions that add the 'layout'
2474 * keyword have been modified to require the use of 'in' or 'out'.
2476 * The following extension do not allow the deprecated keywords:
2478 * GL_AMD_conservative_depth
2479 * GL_ARB_conservative_depth
2480 * GL_ARB_gpu_shader5
2481 * GL_ARB_separate_shader_objects
2482 * GL_ARB_tesselation_shader
2483 * GL_ARB_transform_feedback3
2484 * GL_ARB_uniform_buffer_object
2486 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
2487 * allow layout with the deprecated keywords.
2489 const bool relaxed_layout_qualifier_checking
=
2490 state
->ARB_fragment_coord_conventions_enable
;
2492 if (qual
->has_layout() && uses_deprecated_qualifier
) {
2493 if (relaxed_layout_qualifier_checking
) {
2494 _mesa_glsl_warning(loc
, state
,
2495 "`layout' qualifier may not be used with "
2496 "`attribute' or `varying'");
2498 _mesa_glsl_error(loc
, state
,
2499 "`layout' qualifier may not be used with "
2500 "`attribute' or `varying'");
2504 /* Layout qualifiers for gl_FragDepth, which are enabled by extension
2505 * AMD_conservative_depth.
2507 int depth_layout_count
= qual
->flags
.q
.depth_any
2508 + qual
->flags
.q
.depth_greater
2509 + qual
->flags
.q
.depth_less
2510 + qual
->flags
.q
.depth_unchanged
;
2511 if (depth_layout_count
> 0
2512 && !state
->AMD_conservative_depth_enable
2513 && !state
->ARB_conservative_depth_enable
) {
2514 _mesa_glsl_error(loc
, state
,
2515 "extension GL_AMD_conservative_depth or "
2516 "GL_ARB_conservative_depth must be enabled "
2517 "to use depth layout qualifiers");
2518 } else if (depth_layout_count
> 0
2519 && strcmp(var
->name
, "gl_FragDepth") != 0) {
2520 _mesa_glsl_error(loc
, state
,
2521 "depth layout qualifiers can be applied only to "
2523 } else if (depth_layout_count
> 1
2524 && strcmp(var
->name
, "gl_FragDepth") == 0) {
2525 _mesa_glsl_error(loc
, state
,
2526 "at most one depth layout qualifier can be applied to "
2529 if (qual
->flags
.q
.depth_any
)
2530 var
->data
.depth_layout
= ir_depth_layout_any
;
2531 else if (qual
->flags
.q
.depth_greater
)
2532 var
->data
.depth_layout
= ir_depth_layout_greater
;
2533 else if (qual
->flags
.q
.depth_less
)
2534 var
->data
.depth_layout
= ir_depth_layout_less
;
2535 else if (qual
->flags
.q
.depth_unchanged
)
2536 var
->data
.depth_layout
= ir_depth_layout_unchanged
;
2538 var
->data
.depth_layout
= ir_depth_layout_none
;
2540 if (qual
->flags
.q
.std140
||
2541 qual
->flags
.q
.packed
||
2542 qual
->flags
.q
.shared
) {
2543 _mesa_glsl_error(loc
, state
,
2544 "uniform block layout qualifiers std140, packed, and "
2545 "shared can only be applied to uniform blocks, not "
2549 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
2550 validate_matrix_layout_for_type(state
, loc
, var
->type
, var
);
2553 if (var
->type
->contains_image())
2554 apply_image_qualifier_to_variable(qual
, var
, state
, loc
);
2558 * Get the variable that is being redeclared by this declaration
2560 * Semantic checks to verify the validity of the redeclaration are also
2561 * performed. If semantic checks fail, compilation error will be emitted via
2562 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
2565 * A pointer to an existing variable in the current scope if the declaration
2566 * is a redeclaration, \c NULL otherwise.
2568 static ir_variable
*
2569 get_variable_being_redeclared(ir_variable
*var
, YYLTYPE loc
,
2570 struct _mesa_glsl_parse_state
*state
,
2571 bool allow_all_redeclarations
)
2573 /* Check if this declaration is actually a re-declaration, either to
2574 * resize an array or add qualifiers to an existing variable.
2576 * This is allowed for variables in the current scope, or when at
2577 * global scope (for built-ins in the implicit outer scope).
2579 ir_variable
*earlier
= state
->symbols
->get_variable(var
->name
);
2580 if (earlier
== NULL
||
2581 (state
->current_function
!= NULL
&&
2582 !state
->symbols
->name_declared_this_scope(var
->name
))) {
2587 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
2589 * "It is legal to declare an array without a size and then
2590 * later re-declare the same name as an array of the same
2591 * type and specify a size."
2593 if (earlier
->type
->is_unsized_array() && var
->type
->is_array()
2594 && (var
->type
->element_type() == earlier
->type
->element_type())) {
2595 /* FINISHME: This doesn't match the qualifiers on the two
2596 * FINISHME: declarations. It's not 100% clear whether this is
2597 * FINISHME: required or not.
2600 const unsigned size
= unsigned(var
->type
->array_size());
2601 check_builtin_array_max_size(var
->name
, size
, loc
, state
);
2602 if ((size
> 0) && (size
<= earlier
->data
.max_array_access
)) {
2603 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
2605 earlier
->data
.max_array_access
);
2608 earlier
->type
= var
->type
;
2611 } else if ((state
->ARB_fragment_coord_conventions_enable
||
2612 state
->is_version(150, 0))
2613 && strcmp(var
->name
, "gl_FragCoord") == 0
2614 && earlier
->type
== var
->type
2615 && earlier
->data
.mode
== var
->data
.mode
) {
2616 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
2619 earlier
->data
.origin_upper_left
= var
->data
.origin_upper_left
;
2620 earlier
->data
.pixel_center_integer
= var
->data
.pixel_center_integer
;
2622 /* According to section 4.3.7 of the GLSL 1.30 spec,
2623 * the following built-in varaibles can be redeclared with an
2624 * interpolation qualifier:
2627 * * gl_FrontSecondaryColor
2628 * * gl_BackSecondaryColor
2630 * * gl_SecondaryColor
2632 } else if (state
->is_version(130, 0)
2633 && (strcmp(var
->name
, "gl_FrontColor") == 0
2634 || strcmp(var
->name
, "gl_BackColor") == 0
2635 || strcmp(var
->name
, "gl_FrontSecondaryColor") == 0
2636 || strcmp(var
->name
, "gl_BackSecondaryColor") == 0
2637 || strcmp(var
->name
, "gl_Color") == 0
2638 || strcmp(var
->name
, "gl_SecondaryColor") == 0)
2639 && earlier
->type
== var
->type
2640 && earlier
->data
.mode
== var
->data
.mode
) {
2641 earlier
->data
.interpolation
= var
->data
.interpolation
;
2643 /* Layout qualifiers for gl_FragDepth. */
2644 } else if ((state
->AMD_conservative_depth_enable
||
2645 state
->ARB_conservative_depth_enable
)
2646 && strcmp(var
->name
, "gl_FragDepth") == 0
2647 && earlier
->type
== var
->type
2648 && earlier
->data
.mode
== var
->data
.mode
) {
2650 /** From the AMD_conservative_depth spec:
2651 * Within any shader, the first redeclarations of gl_FragDepth
2652 * must appear before any use of gl_FragDepth.
2654 if (earlier
->data
.used
) {
2655 _mesa_glsl_error(&loc
, state
,
2656 "the first redeclaration of gl_FragDepth "
2657 "must appear before any use of gl_FragDepth");
2660 /* Prevent inconsistent redeclaration of depth layout qualifier. */
2661 if (earlier
->data
.depth_layout
!= ir_depth_layout_none
2662 && earlier
->data
.depth_layout
!= var
->data
.depth_layout
) {
2663 _mesa_glsl_error(&loc
, state
,
2664 "gl_FragDepth: depth layout is declared here "
2665 "as '%s, but it was previously declared as "
2667 depth_layout_string(var
->data
.depth_layout
),
2668 depth_layout_string(earlier
->data
.depth_layout
));
2671 earlier
->data
.depth_layout
= var
->data
.depth_layout
;
2673 } else if (allow_all_redeclarations
) {
2674 if (earlier
->data
.mode
!= var
->data
.mode
) {
2675 _mesa_glsl_error(&loc
, state
,
2676 "redeclaration of `%s' with incorrect qualifiers",
2678 } else if (earlier
->type
!= var
->type
) {
2679 _mesa_glsl_error(&loc
, state
,
2680 "redeclaration of `%s' has incorrect type",
2684 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
2691 * Generate the IR for an initializer in a variable declaration
2694 process_initializer(ir_variable
*var
, ast_declaration
*decl
,
2695 ast_fully_specified_type
*type
,
2696 exec_list
*initializer_instructions
,
2697 struct _mesa_glsl_parse_state
*state
)
2699 ir_rvalue
*result
= NULL
;
2701 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
2703 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
2705 * "All uniform variables are read-only and are initialized either
2706 * directly by an application via API commands, or indirectly by
2709 if (var
->data
.mode
== ir_var_uniform
) {
2710 state
->check_version(120, 0, &initializer_loc
,
2711 "cannot initialize uniforms");
2714 /* From section 4.1.7 of the GLSL 4.40 spec:
2716 * "Opaque variables [...] are initialized only through the
2717 * OpenGL API; they cannot be declared with an initializer in a
2720 if (var
->type
->contains_opaque()) {
2721 _mesa_glsl_error(& initializer_loc
, state
,
2722 "cannot initialize opaque variable");
2725 if ((var
->data
.mode
== ir_var_shader_in
) && (state
->current_function
== NULL
)) {
2726 _mesa_glsl_error(& initializer_loc
, state
,
2727 "cannot initialize %s shader input / %s",
2728 _mesa_shader_stage_to_string(state
->stage
),
2729 (state
->stage
== MESA_SHADER_VERTEX
)
2730 ? "attribute" : "varying");
2733 /* If the initializer is an ast_aggregate_initializer, recursively store
2734 * type information from the LHS into it, so that its hir() function can do
2737 if (decl
->initializer
->oper
== ast_aggregate
)
2738 _mesa_ast_set_aggregate_type(var
->type
, decl
->initializer
);
2740 ir_dereference
*const lhs
= new(state
) ir_dereference_variable(var
);
2741 ir_rvalue
*rhs
= decl
->initializer
->hir(initializer_instructions
,
2744 /* Calculate the constant value if this is a const or uniform
2747 if (type
->qualifier
.flags
.q
.constant
2748 || type
->qualifier
.flags
.q
.uniform
) {
2749 ir_rvalue
*new_rhs
= validate_assignment(state
, initializer_loc
,
2750 var
->type
, rhs
, true);
2751 if (new_rhs
!= NULL
) {
2754 ir_constant
*constant_value
= rhs
->constant_expression_value();
2755 if (!constant_value
) {
2756 /* If ARB_shading_language_420pack is enabled, initializers of
2757 * const-qualified local variables do not have to be constant
2758 * expressions. Const-qualified global variables must still be
2759 * initialized with constant expressions.
2761 if (!state
->ARB_shading_language_420pack_enable
2762 || state
->current_function
== NULL
) {
2763 _mesa_glsl_error(& initializer_loc
, state
,
2764 "initializer of %s variable `%s' must be a "
2765 "constant expression",
2766 (type
->qualifier
.flags
.q
.constant
)
2767 ? "const" : "uniform",
2769 if (var
->type
->is_numeric()) {
2770 /* Reduce cascading errors. */
2771 var
->constant_value
= ir_constant::zero(state
, var
->type
);
2775 rhs
= constant_value
;
2776 var
->constant_value
= constant_value
;
2779 if (var
->type
->is_numeric()) {
2780 /* Reduce cascading errors. */
2781 var
->constant_value
= ir_constant::zero(state
, var
->type
);
2786 if (rhs
&& !rhs
->type
->is_error()) {
2787 bool temp
= var
->data
.read_only
;
2788 if (type
->qualifier
.flags
.q
.constant
)
2789 var
->data
.read_only
= false;
2791 /* Never emit code to initialize a uniform.
2793 const glsl_type
*initializer_type
;
2794 if (!type
->qualifier
.flags
.q
.uniform
) {
2795 result
= do_assignment(initializer_instructions
, state
,
2798 type
->get_location());
2799 initializer_type
= result
->type
;
2801 initializer_type
= rhs
->type
;
2803 var
->constant_initializer
= rhs
->constant_expression_value();
2804 var
->data
.has_initializer
= true;
2806 /* If the declared variable is an unsized array, it must inherrit
2807 * its full type from the initializer. A declaration such as
2809 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
2813 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
2815 * The assignment generated in the if-statement (below) will also
2816 * automatically handle this case for non-uniforms.
2818 * If the declared variable is not an array, the types must
2819 * already match exactly. As a result, the type assignment
2820 * here can be done unconditionally. For non-uniforms the call
2821 * to do_assignment can change the type of the initializer (via
2822 * the implicit conversion rules). For uniforms the initializer
2823 * must be a constant expression, and the type of that expression
2824 * was validated above.
2826 var
->type
= initializer_type
;
2828 var
->data
.read_only
= temp
;
2836 * Do additional processing necessary for geometry shader input declarations
2837 * (this covers both interface blocks arrays and bare input variables).
2840 handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
2841 YYLTYPE loc
, ir_variable
*var
)
2843 unsigned num_vertices
= 0;
2844 if (state
->gs_input_prim_type_specified
) {
2845 num_vertices
= vertices_per_prim(state
->gs_input_prim_type
);
2848 /* Geometry shader input variables must be arrays. Caller should have
2849 * reported an error for this.
2851 if (!var
->type
->is_array()) {
2852 assert(state
->error
);
2854 /* To avoid cascading failures, short circuit the checks below. */
2858 if (var
->type
->is_unsized_array()) {
2859 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says:
2861 * All geometry shader input unsized array declarations will be
2862 * sized by an earlier input layout qualifier, when present, as per
2863 * the following table.
2865 * Followed by a table mapping each allowed input layout qualifier to
2866 * the corresponding input length.
2868 if (num_vertices
!= 0)
2869 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
2872 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec
2873 * includes the following examples of compile-time errors:
2875 * // code sequence within one shader...
2876 * in vec4 Color1[]; // size unknown
2877 * ...Color1.length()...// illegal, length() unknown
2878 * in vec4 Color2[2]; // size is 2
2879 * ...Color1.length()...// illegal, Color1 still has no size
2880 * in vec4 Color3[3]; // illegal, input sizes are inconsistent
2881 * layout(lines) in; // legal, input size is 2, matching
2882 * in vec4 Color4[3]; // illegal, contradicts layout
2885 * To detect the case illustrated by Color3, we verify that the size of
2886 * an explicitly-sized array matches the size of any previously declared
2887 * explicitly-sized array. To detect the case illustrated by Color4, we
2888 * verify that the size of an explicitly-sized array is consistent with
2889 * any previously declared input layout.
2891 if (num_vertices
!= 0 && var
->type
->length
!= num_vertices
) {
2892 _mesa_glsl_error(&loc
, state
,
2893 "geometry shader input size contradicts previously"
2894 " declared layout (size is %u, but layout requires a"
2895 " size of %u)", var
->type
->length
, num_vertices
);
2896 } else if (state
->gs_input_size
!= 0 &&
2897 var
->type
->length
!= state
->gs_input_size
) {
2898 _mesa_glsl_error(&loc
, state
,
2899 "geometry shader input sizes are "
2900 "inconsistent (size is %u, but a previous "
2901 "declaration has size %u)",
2902 var
->type
->length
, state
->gs_input_size
);
2904 state
->gs_input_size
= var
->type
->length
;
2911 validate_identifier(const char *identifier
, YYLTYPE loc
,
2912 struct _mesa_glsl_parse_state
*state
)
2914 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
2916 * "Identifiers starting with "gl_" are reserved for use by
2917 * OpenGL, and may not be declared in a shader as either a
2918 * variable or a function."
2920 if (strncmp(identifier
, "gl_", 3) == 0) {
2921 _mesa_glsl_error(&loc
, state
,
2922 "identifier `%s' uses reserved `gl_' prefix",
2924 } else if (strstr(identifier
, "__")) {
2925 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
2928 * "In addition, all identifiers containing two
2929 * consecutive underscores (__) are reserved as
2930 * possible future keywords."
2932 * The intention is that names containing __ are reserved for internal
2933 * use by the implementation, and names prefixed with GL_ are reserved
2934 * for use by Khronos. Names simply containing __ are dangerous to use,
2935 * but should be allowed.
2937 * A future version of the GLSL specification will clarify this.
2939 _mesa_glsl_warning(&loc
, state
,
2940 "identifier `%s' uses reserved `__' string",
2947 ast_declarator_list::hir(exec_list
*instructions
,
2948 struct _mesa_glsl_parse_state
*state
)
2951 const struct glsl_type
*decl_type
;
2952 const char *type_name
= NULL
;
2953 ir_rvalue
*result
= NULL
;
2954 YYLTYPE loc
= this->get_location();
2956 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
2958 * "To ensure that a particular output variable is invariant, it is
2959 * necessary to use the invariant qualifier. It can either be used to
2960 * qualify a previously declared variable as being invariant
2962 * invariant gl_Position; // make existing gl_Position be invariant"
2964 * In these cases the parser will set the 'invariant' flag in the declarator
2965 * list, and the type will be NULL.
2967 if (this->invariant
) {
2968 assert(this->type
== NULL
);
2970 if (state
->current_function
!= NULL
) {
2971 _mesa_glsl_error(& loc
, state
,
2972 "all uses of `invariant' keyword must be at global "
2976 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
2977 assert(decl
->array_specifier
== NULL
);
2978 assert(decl
->initializer
== NULL
);
2980 ir_variable
*const earlier
=
2981 state
->symbols
->get_variable(decl
->identifier
);
2982 if (earlier
== NULL
) {
2983 _mesa_glsl_error(& loc
, state
,
2984 "undeclared variable `%s' cannot be marked "
2985 "invariant", decl
->identifier
);
2986 } else if ((state
->stage
== MESA_SHADER_VERTEX
)
2987 && (earlier
->data
.mode
!= ir_var_shader_out
)) {
2988 _mesa_glsl_error(& loc
, state
,
2989 "`%s' cannot be marked invariant, vertex shader "
2990 "outputs only", decl
->identifier
);
2991 } else if ((state
->stage
== MESA_SHADER_FRAGMENT
)
2992 && (earlier
->data
.mode
!= ir_var_shader_in
)) {
2993 _mesa_glsl_error(& loc
, state
,
2994 "`%s' cannot be marked invariant, fragment shader "
2995 "inputs only", decl
->identifier
);
2996 } else if (earlier
->data
.used
) {
2997 _mesa_glsl_error(& loc
, state
,
2998 "variable `%s' may not be redeclared "
2999 "`invariant' after being used",
3002 earlier
->data
.invariant
= true;
3006 /* Invariant redeclarations do not have r-values.
3011 assert(this->type
!= NULL
);
3012 assert(!this->invariant
);
3014 /* The type specifier may contain a structure definition. Process that
3015 * before any of the variable declarations.
3017 (void) this->type
->specifier
->hir(instructions
, state
);
3019 decl_type
= this->type
->glsl_type(& type_name
, state
);
3021 /* An offset-qualified atomic counter declaration sets the default
3022 * offset for the next declaration within the same atomic counter
3025 if (decl_type
&& decl_type
->contains_atomic()) {
3026 if (type
->qualifier
.flags
.q
.explicit_binding
&&
3027 type
->qualifier
.flags
.q
.explicit_offset
)
3028 state
->atomic_counter_offsets
[type
->qualifier
.binding
] =
3029 type
->qualifier
.offset
;
3032 if (this->declarations
.is_empty()) {
3033 /* If there is no structure involved in the program text, there are two
3034 * possible scenarios:
3036 * - The program text contained something like 'vec4;'. This is an
3037 * empty declaration. It is valid but weird. Emit a warning.
3039 * - The program text contained something like 'S;' and 'S' is not the
3040 * name of a known structure type. This is both invalid and weird.
3043 * - The program text contained something like 'mediump float;'
3044 * when the programmer probably meant 'precision mediump
3045 * float;' Emit a warning with a description of what they
3046 * probably meant to do.
3048 * Note that if decl_type is NULL and there is a structure involved,
3049 * there must have been some sort of error with the structure. In this
3050 * case we assume that an error was already generated on this line of
3051 * code for the structure. There is no need to generate an additional,
3054 assert(this->type
->specifier
->structure
== NULL
|| decl_type
!= NULL
3057 if (decl_type
== NULL
) {
3058 _mesa_glsl_error(&loc
, state
,
3059 "invalid type `%s' in empty declaration",
3061 } else if (decl_type
->base_type
== GLSL_TYPE_ATOMIC_UINT
) {
3062 /* Empty atomic counter declarations are allowed and useful
3063 * to set the default offset qualifier.
3066 } else if (this->type
->qualifier
.precision
!= ast_precision_none
) {
3067 if (this->type
->specifier
->structure
!= NULL
) {
3068 _mesa_glsl_error(&loc
, state
,
3069 "precision qualifiers can't be applied "
3072 static const char *const precision_names
[] = {
3079 _mesa_glsl_warning(&loc
, state
,
3080 "empty declaration with precision qualifier, "
3081 "to set the default precision, use "
3082 "`precision %s %s;'",
3083 precision_names
[this->type
->qualifier
.precision
],
3086 } else if (this->type
->specifier
->structure
== NULL
) {
3087 _mesa_glsl_warning(&loc
, state
, "empty declaration");
3091 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
3092 const struct glsl_type
*var_type
;
3095 /* FINISHME: Emit a warning if a variable declaration shadows a
3096 * FINISHME: declaration at a higher scope.
3099 if ((decl_type
== NULL
) || decl_type
->is_void()) {
3100 if (type_name
!= NULL
) {
3101 _mesa_glsl_error(& loc
, state
,
3102 "invalid type `%s' in declaration of `%s'",
3103 type_name
, decl
->identifier
);
3105 _mesa_glsl_error(& loc
, state
,
3106 "invalid type in declaration of `%s'",
3112 var_type
= process_array_type(&loc
, decl_type
, decl
->array_specifier
,
3115 var
= new(ctx
) ir_variable(var_type
, decl
->identifier
, ir_var_auto
);
3117 /* The 'varying in' and 'varying out' qualifiers can only be used with
3118 * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
3121 if (this->type
->qualifier
.flags
.q
.varying
) {
3122 if (this->type
->qualifier
.flags
.q
.in
) {
3123 _mesa_glsl_error(& loc
, state
,
3124 "`varying in' qualifier in declaration of "
3125 "`%s' only valid for geometry shaders using "
3126 "ARB_geometry_shader4 or EXT_geometry_shader4",
3128 } else if (this->type
->qualifier
.flags
.q
.out
) {
3129 _mesa_glsl_error(& loc
, state
,
3130 "`varying out' qualifier in declaration of "
3131 "`%s' only valid for geometry shaders using "
3132 "ARB_geometry_shader4 or EXT_geometry_shader4",
3137 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
3139 * "Global variables can only use the qualifiers const,
3140 * attribute, uni form, or varying. Only one may be
3143 * Local variables can only use the qualifier const."
3145 * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
3146 * any extension that adds the 'layout' keyword.
3148 if (!state
->is_version(130, 300)
3149 && !state
->has_explicit_attrib_location()
3150 && !state
->ARB_fragment_coord_conventions_enable
) {
3151 if (this->type
->qualifier
.flags
.q
.out
) {
3152 _mesa_glsl_error(& loc
, state
,
3153 "`out' qualifier in declaration of `%s' "
3154 "only valid for function parameters in %s",
3155 decl
->identifier
, state
->get_version_string());
3157 if (this->type
->qualifier
.flags
.q
.in
) {
3158 _mesa_glsl_error(& loc
, state
,
3159 "`in' qualifier in declaration of `%s' "
3160 "only valid for function parameters in %s",
3161 decl
->identifier
, state
->get_version_string());
3163 /* FINISHME: Test for other invalid qualifiers. */
3166 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
3169 if (this->type
->qualifier
.flags
.q
.invariant
) {
3170 if ((state
->stage
== MESA_SHADER_VERTEX
) &&
3171 var
->data
.mode
!= ir_var_shader_out
) {
3172 _mesa_glsl_error(& loc
, state
,
3173 "`%s' cannot be marked invariant, vertex shader "
3174 "outputs only", var
->name
);
3175 } else if ((state
->stage
== MESA_SHADER_FRAGMENT
) &&
3176 var
->data
.mode
!= ir_var_shader_in
) {
3177 /* FINISHME: Note that this doesn't work for invariant on
3178 * a function signature inval
3180 _mesa_glsl_error(& loc
, state
,
3181 "`%s' cannot be marked invariant, fragment shader "
3182 "inputs only", var
->name
);
3186 if (state
->current_function
!= NULL
) {
3187 const char *mode
= NULL
;
3188 const char *extra
= "";
3190 /* There is no need to check for 'inout' here because the parser will
3191 * only allow that in function parameter lists.
3193 if (this->type
->qualifier
.flags
.q
.attribute
) {
3195 } else if (this->type
->qualifier
.flags
.q
.uniform
) {
3197 } else if (this->type
->qualifier
.flags
.q
.varying
) {
3199 } else if (this->type
->qualifier
.flags
.q
.in
) {
3201 extra
= " or in function parameter list";
3202 } else if (this->type
->qualifier
.flags
.q
.out
) {
3204 extra
= " or in function parameter list";
3208 _mesa_glsl_error(& loc
, state
,
3209 "%s variable `%s' must be declared at "
3211 mode
, var
->name
, extra
);
3213 } else if (var
->data
.mode
== ir_var_shader_in
) {
3214 var
->data
.read_only
= true;
3216 if (state
->stage
== MESA_SHADER_VERTEX
) {
3217 bool error_emitted
= false;
3219 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
3221 * "Vertex shader inputs can only be float, floating-point
3222 * vectors, matrices, signed and unsigned integers and integer
3223 * vectors. Vertex shader inputs can also form arrays of these
3224 * types, but not structures."
3226 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
3228 * "Vertex shader inputs can only be float, floating-point
3229 * vectors, matrices, signed and unsigned integers and integer
3230 * vectors. They cannot be arrays or structures."
3232 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
3234 * "The attribute qualifier can be used only with float,
3235 * floating-point vectors, and matrices. Attribute variables
3236 * cannot be declared as arrays or structures."
3238 * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
3240 * "Vertex shader inputs can only be float, floating-point
3241 * vectors, matrices, signed and unsigned integers and integer
3242 * vectors. Vertex shader inputs cannot be arrays or
3245 const glsl_type
*check_type
= var
->type
;
3246 while (check_type
->is_array())
3247 check_type
= check_type
->element_type();
3249 switch (check_type
->base_type
) {
3250 case GLSL_TYPE_FLOAT
:
3252 case GLSL_TYPE_UINT
:
3254 if (state
->is_version(120, 300))
3258 _mesa_glsl_error(& loc
, state
,
3259 "vertex shader input / attribute cannot have "
3261 var
->type
->is_array() ? "array of " : "",
3263 error_emitted
= true;
3266 if (!error_emitted
&& var
->type
->is_array() &&
3267 !state
->check_version(150, 0, &loc
,
3268 "vertex shader input / attribute "
3269 "cannot have array type")) {
3270 error_emitted
= true;
3272 } else if (state
->stage
== MESA_SHADER_GEOMETRY
) {
3273 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
3275 * Geometry shader input variables get the per-vertex values
3276 * written out by vertex shader output variables of the same
3277 * names. Since a geometry shader operates on a set of
3278 * vertices, each input varying variable (or input block, see
3279 * interface blocks below) needs to be declared as an array.
3281 if (!var
->type
->is_array()) {
3282 _mesa_glsl_error(&loc
, state
,
3283 "geometry shader inputs must be arrays");
3286 handle_geometry_shader_input_decl(state
, loc
, var
);
3290 /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
3291 * so must integer vertex outputs.
3293 * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
3294 * "Fragment shader inputs that are signed or unsigned integers or
3295 * integer vectors must be qualified with the interpolation qualifier
3298 * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
3299 * "Fragment shader inputs that are, or contain, signed or unsigned
3300 * integers or integer vectors must be qualified with the
3301 * interpolation qualifier flat."
3303 * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
3304 * "Vertex shader outputs that are, or contain, signed or unsigned
3305 * integers or integer vectors must be qualified with the
3306 * interpolation qualifier flat."
3308 * Note that prior to GLSL 1.50, this requirement applied to vertex
3309 * outputs rather than fragment inputs. That creates problems in the
3310 * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
3311 * desktop GL shaders. For GLSL ES shaders, we follow the spec and
3312 * apply the restriction to both vertex outputs and fragment inputs.
3314 * Note also that the desktop GLSL specs are missing the text "or
3315 * contain"; this is presumably an oversight, since there is no
3316 * reasonable way to interpolate a fragment shader input that contains
3319 if (state
->is_version(130, 300) &&
3320 var
->type
->contains_integer() &&
3321 var
->data
.interpolation
!= INTERP_QUALIFIER_FLAT
&&
3322 ((state
->stage
== MESA_SHADER_FRAGMENT
&& var
->data
.mode
== ir_var_shader_in
)
3323 || (state
->stage
== MESA_SHADER_VERTEX
&& var
->data
.mode
== ir_var_shader_out
3324 && state
->es_shader
))) {
3325 const char *var_type
= (state
->stage
== MESA_SHADER_VERTEX
) ?
3326 "vertex output" : "fragment input";
3327 _mesa_glsl_error(&loc
, state
, "if a %s is (or contains) "
3328 "an integer, then it must be qualified with 'flat'",
3333 /* Interpolation qualifiers cannot be applied to 'centroid' and
3334 * 'centroid varying'.
3336 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
3337 * "interpolation qualifiers may only precede the qualifiers in,
3338 * centroid in, out, or centroid out in a declaration. They do not apply
3339 * to the deprecated storage qualifiers varying or centroid varying."
3341 * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
3343 if (state
->is_version(130, 0)
3344 && this->type
->qualifier
.has_interpolation()
3345 && this->type
->qualifier
.flags
.q
.varying
) {
3347 const char *i
= this->type
->qualifier
.interpolation_string();
3350 if (this->type
->qualifier
.flags
.q
.centroid
)
3351 s
= "centroid varying";
3355 _mesa_glsl_error(&loc
, state
,
3356 "qualifier '%s' cannot be applied to the "
3357 "deprecated storage qualifier '%s'", i
, s
);
3361 /* Interpolation qualifiers can only apply to vertex shader outputs and
3362 * fragment shader inputs.
3364 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
3365 * "Outputs from a vertex shader (out) and inputs to a fragment
3366 * shader (in) can be further qualified with one or more of these
3367 * interpolation qualifiers"
3369 * From page 31 (page 37 of the PDF) of the GLSL ES 3.00 spec:
3370 * "These interpolation qualifiers may only precede the qualifiers
3371 * in, centroid in, out, or centroid out in a declaration. They do
3372 * not apply to inputs into a vertex shader or outputs from a
3375 if (state
->is_version(130, 300)
3376 && this->type
->qualifier
.has_interpolation()) {
3378 const char *i
= this->type
->qualifier
.interpolation_string();
3381 switch (state
->stage
) {
3382 case MESA_SHADER_VERTEX
:
3383 if (this->type
->qualifier
.flags
.q
.in
) {
3384 _mesa_glsl_error(&loc
, state
,
3385 "qualifier '%s' cannot be applied to vertex "
3386 "shader inputs", i
);
3389 case MESA_SHADER_FRAGMENT
:
3390 if (this->type
->qualifier
.flags
.q
.out
) {
3391 _mesa_glsl_error(&loc
, state
,
3392 "qualifier '%s' cannot be applied to fragment "
3393 "shader outputs", i
);
3402 /* From section 4.3.4 of the GLSL 1.30 spec:
3403 * "It is an error to use centroid in in a vertex shader."
3405 * From section 4.3.4 of the GLSL ES 3.00 spec:
3406 * "It is an error to use centroid in or interpolation qualifiers in
3407 * a vertex shader input."
3409 if (state
->is_version(130, 300)
3410 && this->type
->qualifier
.flags
.q
.centroid
3411 && this->type
->qualifier
.flags
.q
.in
3412 && state
->stage
== MESA_SHADER_VERTEX
) {
3414 _mesa_glsl_error(&loc
, state
,
3415 "'centroid in' cannot be used in a vertex shader");
3418 if (state
->stage
== MESA_SHADER_VERTEX
3419 && this->type
->qualifier
.flags
.q
.sample
3420 && this->type
->qualifier
.flags
.q
.in
) {
3422 _mesa_glsl_error(&loc
, state
,
3423 "'sample in' cannot be used in a vertex shader");
3426 /* Section 4.3.6 of the GLSL 1.30 specification states:
3427 * "It is an error to use centroid out in a fragment shader."
3429 * The GL_ARB_shading_language_420pack extension specification states:
3430 * "It is an error to use auxiliary storage qualifiers or interpolation
3431 * qualifiers on an output in a fragment shader."
3433 if (state
->stage
== MESA_SHADER_FRAGMENT
&&
3434 this->type
->qualifier
.flags
.q
.out
&&
3435 this->type
->qualifier
.has_auxiliary_storage()) {
3436 _mesa_glsl_error(&loc
, state
,
3437 "auxiliary storage qualifiers cannot be used on "
3438 "fragment shader outputs");
3441 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
3443 if (this->type
->qualifier
.precision
!= ast_precision_none
) {
3444 state
->check_precision_qualifiers_allowed(&loc
);
3448 /* Precision qualifiers apply to floating point, integer and sampler
3451 * Section 4.5.2 (Precision Qualifiers) of the GLSL 1.30 spec says:
3452 * "Any floating point or any integer declaration can have the type
3453 * preceded by one of these precision qualifiers [...] Literal
3454 * constants do not have precision qualifiers. Neither do Boolean
3457 * Section 4.5 (Precision and Precision Qualifiers) of the GLSL 1.30
3460 * "Precision qualifiers are added for code portability with OpenGL
3461 * ES, not for functionality. They have the same syntax as in OpenGL
3464 * Section 8 (Built-In Functions) of the GLSL ES 1.00 spec says:
3466 * "uniform lowp sampler2D sampler;
3469 * lowp vec4 col = texture2D (sampler, coord);
3470 * // texture2D returns lowp"
3472 * From this, we infer that GLSL 1.30 (and later) should allow precision
3473 * qualifiers on sampler types just like float and integer types.
3475 if (this->type
->qualifier
.precision
!= ast_precision_none
3476 && !var
->type
->is_float()
3477 && !var
->type
->is_integer()
3478 && !var
->type
->is_record()
3479 && !var
->type
->is_sampler()
3480 && !(var
->type
->is_array()
3481 && (var
->type
->fields
.array
->is_float()
3482 || var
->type
->fields
.array
->is_integer()))) {
3484 _mesa_glsl_error(&loc
, state
,
3485 "precision qualifiers apply only to floating point"
3486 ", integer and sampler types");
3489 /* From section 4.1.7 of the GLSL 4.40 spec:
3491 * "[Opaque types] can only be declared as function
3492 * parameters or uniform-qualified variables."
3494 if (var_type
->contains_opaque() &&
3495 !this->type
->qualifier
.flags
.q
.uniform
) {
3496 _mesa_glsl_error(&loc
, state
,
3497 "opaque variables must be declared uniform");
3500 /* Process the initializer and add its instructions to a temporary
3501 * list. This list will be added to the instruction stream (below) after
3502 * the declaration is added. This is done because in some cases (such as
3503 * redeclarations) the declaration may not actually be added to the
3504 * instruction stream.
3506 exec_list initializer_instructions
;
3507 ir_variable
*earlier
=
3508 get_variable_being_redeclared(var
, decl
->get_location(), state
,
3509 false /* allow_all_redeclarations */);
3510 if (earlier
!= NULL
) {
3511 if (strncmp(var
->name
, "gl_", 3) == 0 &&
3512 earlier
->data
.how_declared
== ir_var_declared_in_block
) {
3513 _mesa_glsl_error(&loc
, state
,
3514 "`%s' has already been redeclared using "
3515 "gl_PerVertex", var
->name
);
3517 earlier
->data
.how_declared
= ir_var_declared_normally
;
3520 if (decl
->initializer
!= NULL
) {
3521 result
= process_initializer((earlier
== NULL
) ? var
: earlier
,
3523 &initializer_instructions
, state
);
3526 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
3528 * "It is an error to write to a const variable outside of
3529 * its declaration, so they must be initialized when
3532 if (this->type
->qualifier
.flags
.q
.constant
&& decl
->initializer
== NULL
) {
3533 _mesa_glsl_error(& loc
, state
,
3534 "const declaration of `%s' must be initialized",
3538 if (state
->es_shader
) {
3539 const glsl_type
*const t
= (earlier
== NULL
)
3540 ? var
->type
: earlier
->type
;
3542 if (t
->is_unsized_array())
3543 /* Section 10.17 of the GLSL ES 1.00 specification states that
3544 * unsized array declarations have been removed from the language.
3545 * Arrays that are sized using an initializer are still explicitly
3546 * sized. However, GLSL ES 1.00 does not allow array
3547 * initializers. That is only allowed in GLSL ES 3.00.
3549 * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
3551 * "An array type can also be formed without specifying a size
3552 * if the definition includes an initializer:
3554 * float x[] = float[2] (1.0, 2.0); // declares an array of size 2
3555 * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
3560 _mesa_glsl_error(& loc
, state
,
3561 "unsized array declarations are not allowed in "
3565 /* If the declaration is not a redeclaration, there are a few additional
3566 * semantic checks that must be applied. In addition, variable that was
3567 * created for the declaration should be added to the IR stream.
3569 if (earlier
== NULL
) {
3570 validate_identifier(decl
->identifier
, loc
, state
);
3572 /* Add the variable to the symbol table. Note that the initializer's
3573 * IR was already processed earlier (though it hasn't been emitted
3574 * yet), without the variable in scope.
3576 * This differs from most C-like languages, but it follows the GLSL
3577 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
3580 * "Within a declaration, the scope of a name starts immediately
3581 * after the initializer if present or immediately after the name
3582 * being declared if not."
3584 if (!state
->symbols
->add_variable(var
)) {
3585 YYLTYPE loc
= this->get_location();
3586 _mesa_glsl_error(&loc
, state
, "name `%s' already taken in the "
3587 "current scope", decl
->identifier
);
3591 /* Push the variable declaration to the top. It means that all the
3592 * variable declarations will appear in a funny last-to-first order,
3593 * but otherwise we run into trouble if a function is prototyped, a
3594 * global var is decled, then the function is defined with usage of
3595 * the global var. See glslparsertest's CorrectModule.frag.
3597 instructions
->push_head(var
);
3600 instructions
->append_list(&initializer_instructions
);
3604 /* Generally, variable declarations do not have r-values. However,
3605 * one is used for the declaration in
3607 * while (bool b = some_condition()) {
3611 * so we return the rvalue from the last seen declaration here.
3618 ast_parameter_declarator::hir(exec_list
*instructions
,
3619 struct _mesa_glsl_parse_state
*state
)
3622 const struct glsl_type
*type
;
3623 const char *name
= NULL
;
3624 YYLTYPE loc
= this->get_location();
3626 type
= this->type
->glsl_type(& name
, state
);
3630 _mesa_glsl_error(& loc
, state
,
3631 "invalid type `%s' in declaration of `%s'",
3632 name
, this->identifier
);
3634 _mesa_glsl_error(& loc
, state
,
3635 "invalid type in declaration of `%s'",
3639 type
= glsl_type::error_type
;
3642 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
3644 * "Functions that accept no input arguments need not use void in the
3645 * argument list because prototypes (or definitions) are required and
3646 * therefore there is no ambiguity when an empty argument list "( )" is
3647 * declared. The idiom "(void)" as a parameter list is provided for
3650 * Placing this check here prevents a void parameter being set up
3651 * for a function, which avoids tripping up checks for main taking
3652 * parameters and lookups of an unnamed symbol.
3654 if (type
->is_void()) {
3655 if (this->identifier
!= NULL
)
3656 _mesa_glsl_error(& loc
, state
,
3657 "named parameter cannot have type `void'");
3663 if (formal_parameter
&& (this->identifier
== NULL
)) {
3664 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
3668 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
3669 * call already handled the "vec4[..] foo" case.
3671 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
3673 if (!type
->is_error() && type
->is_unsized_array()) {
3674 _mesa_glsl_error(&loc
, state
, "arrays passed as parameters must have "
3676 type
= glsl_type::error_type
;
3680 ir_variable
*var
= new(ctx
)
3681 ir_variable(type
, this->identifier
, ir_var_function_in
);
3683 /* Apply any specified qualifiers to the parameter declaration. Note that
3684 * for function parameters the default mode is 'in'.
3686 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
,
3689 /* From section 4.1.7 of the GLSL 4.40 spec:
3691 * "Opaque variables cannot be treated as l-values; hence cannot
3692 * be used as out or inout function parameters, nor can they be
3695 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
3696 && type
->contains_opaque()) {
3697 _mesa_glsl_error(&loc
, state
, "out and inout parameters cannot "
3698 "contain opaque variables");
3699 type
= glsl_type::error_type
;
3702 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
3704 * "When calling a function, expressions that do not evaluate to
3705 * l-values cannot be passed to parameters declared as out or inout."
3707 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
3709 * "Other binary or unary expressions, non-dereferenced arrays,
3710 * function names, swizzles with repeated fields, and constants
3711 * cannot be l-values."
3713 * So for GLSL 1.10, passing an array as an out or inout parameter is not
3714 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
3716 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
3718 && !state
->check_version(120, 100, &loc
,
3719 "arrays cannot be out or inout parameters")) {
3720 type
= glsl_type::error_type
;
3723 instructions
->push_tail(var
);
3725 /* Parameter declarations do not have r-values.
3732 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
3734 exec_list
*ir_parameters
,
3735 _mesa_glsl_parse_state
*state
)
3737 ast_parameter_declarator
*void_param
= NULL
;
3740 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
3741 param
->formal_parameter
= formal
;
3742 param
->hir(ir_parameters
, state
);
3750 if ((void_param
!= NULL
) && (count
> 1)) {
3751 YYLTYPE loc
= void_param
->get_location();
3753 _mesa_glsl_error(& loc
, state
,
3754 "`void' parameter must be only parameter");
3760 emit_function(_mesa_glsl_parse_state
*state
, ir_function
*f
)
3762 /* IR invariants disallow function declarations or definitions
3763 * nested within other function definitions. But there is no
3764 * requirement about the relative order of function declarations
3765 * and definitions with respect to one another. So simply insert
3766 * the new ir_function block at the end of the toplevel instruction
3769 state
->toplevel_ir
->push_tail(f
);
3774 ast_function::hir(exec_list
*instructions
,
3775 struct _mesa_glsl_parse_state
*state
)
3778 ir_function
*f
= NULL
;
3779 ir_function_signature
*sig
= NULL
;
3780 exec_list hir_parameters
;
3782 const char *const name
= identifier
;
3784 /* New functions are always added to the top-level IR instruction stream,
3785 * so this instruction list pointer is ignored. See also emit_function
3788 (void) instructions
;
3790 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
3792 * "Function declarations (prototypes) cannot occur inside of functions;
3793 * they must be at global scope, or for the built-in functions, outside
3794 * the global scope."
3796 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
3798 * "User defined functions may only be defined within the global scope."
3800 * Note that this language does not appear in GLSL 1.10.
3802 if ((state
->current_function
!= NULL
) &&
3803 state
->is_version(120, 100)) {
3804 YYLTYPE loc
= this->get_location();
3805 _mesa_glsl_error(&loc
, state
,
3806 "declaration of function `%s' not allowed within "
3807 "function body", name
);
3810 validate_identifier(name
, this->get_location(), state
);
3812 /* Convert the list of function parameters to HIR now so that they can be
3813 * used below to compare this function's signature with previously seen
3814 * signatures for functions with the same name.
3816 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
3818 & hir_parameters
, state
);
3820 const char *return_type_name
;
3821 const glsl_type
*return_type
=
3822 this->return_type
->glsl_type(& return_type_name
, state
);
3825 YYLTYPE loc
= this->get_location();
3826 _mesa_glsl_error(&loc
, state
,
3827 "function `%s' has undeclared return type `%s'",
3828 name
, return_type_name
);
3829 return_type
= glsl_type::error_type
;
3832 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
3833 * "No qualifier is allowed on the return type of a function."
3835 if (this->return_type
->has_qualifiers()) {
3836 YYLTYPE loc
= this->get_location();
3837 _mesa_glsl_error(& loc
, state
,
3838 "function `%s' return type has qualifiers", name
);
3841 /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
3843 * "Arrays are allowed as arguments and as the return type. In both
3844 * cases, the array must be explicitly sized."
3846 if (return_type
->is_unsized_array()) {
3847 YYLTYPE loc
= this->get_location();
3848 _mesa_glsl_error(& loc
, state
,
3849 "function `%s' return type array must be explicitly "
3853 /* From section 4.1.7 of the GLSL 4.40 spec:
3855 * "[Opaque types] can only be declared as function parameters
3856 * or uniform-qualified variables."
3858 if (return_type
->contains_opaque()) {
3859 YYLTYPE loc
= this->get_location();
3860 _mesa_glsl_error(&loc
, state
,
3861 "function `%s' return type can't contain an opaque type",
3865 /* Verify that this function's signature either doesn't match a previously
3866 * seen signature for a function with the same name, or, if a match is found,
3867 * that the previously seen signature does not have an associated definition.
3869 f
= state
->symbols
->get_function(name
);
3870 if (f
!= NULL
&& (state
->es_shader
|| f
->has_user_signature())) {
3871 sig
= f
->exact_matching_signature(state
, &hir_parameters
);
3873 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
3874 if (badvar
!= NULL
) {
3875 YYLTYPE loc
= this->get_location();
3877 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
3878 "qualifiers don't match prototype", name
, badvar
);
3881 if (sig
->return_type
!= return_type
) {
3882 YYLTYPE loc
= this->get_location();
3884 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
3885 "match prototype", name
);
3888 if (sig
->is_defined
) {
3889 if (is_definition
) {
3890 YYLTYPE loc
= this->get_location();
3891 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
3893 /* We just encountered a prototype that exactly matches a
3894 * function that's already been defined. This is redundant,
3895 * and we should ignore it.
3902 f
= new(ctx
) ir_function(name
);
3903 if (!state
->symbols
->add_function(f
)) {
3904 /* This function name shadows a non-function use of the same name. */
3905 YYLTYPE loc
= this->get_location();
3907 _mesa_glsl_error(&loc
, state
, "function name `%s' conflicts with "
3908 "non-function", name
);
3912 emit_function(state
, f
);
3915 /* Verify the return type of main() */
3916 if (strcmp(name
, "main") == 0) {
3917 if (! return_type
->is_void()) {
3918 YYLTYPE loc
= this->get_location();
3920 _mesa_glsl_error(& loc
, state
, "main() must return void");
3923 if (!hir_parameters
.is_empty()) {
3924 YYLTYPE loc
= this->get_location();
3926 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
3930 /* Finish storing the information about this new function in its signature.
3933 sig
= new(ctx
) ir_function_signature(return_type
);
3934 f
->add_signature(sig
);
3937 sig
->replace_parameters(&hir_parameters
);
3940 /* Function declarations (prototypes) do not have r-values.
3947 ast_function_definition::hir(exec_list
*instructions
,
3948 struct _mesa_glsl_parse_state
*state
)
3950 prototype
->is_definition
= true;
3951 prototype
->hir(instructions
, state
);
3953 ir_function_signature
*signature
= prototype
->signature
;
3954 if (signature
== NULL
)
3957 assert(state
->current_function
== NULL
);
3958 state
->current_function
= signature
;
3959 state
->found_return
= false;
3961 /* Duplicate parameters declared in the prototype as concrete variables.
3962 * Add these to the symbol table.
3964 state
->symbols
->push_scope();
3965 foreach_list(n
, &signature
->parameters
) {
3966 ir_variable
*const var
= ((ir_instruction
*) n
)->as_variable();
3968 assert(var
!= NULL
);
3970 /* The only way a parameter would "exist" is if two parameters have
3973 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
3974 YYLTYPE loc
= this->get_location();
3976 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
3978 state
->symbols
->add_variable(var
);
3982 /* Convert the body of the function to HIR. */
3983 this->body
->hir(&signature
->body
, state
);
3984 signature
->is_defined
= true;
3986 state
->symbols
->pop_scope();
3988 assert(state
->current_function
== signature
);
3989 state
->current_function
= NULL
;
3991 if (!signature
->return_type
->is_void() && !state
->found_return
) {
3992 YYLTYPE loc
= this->get_location();
3993 _mesa_glsl_error(& loc
, state
, "function `%s' has non-void return type "
3994 "%s, but no return statement",
3995 signature
->function_name(),
3996 signature
->return_type
->name
);
3999 /* Function definitions do not have r-values.
4006 ast_jump_statement::hir(exec_list
*instructions
,
4007 struct _mesa_glsl_parse_state
*state
)
4014 assert(state
->current_function
);
4016 if (opt_return_value
) {
4017 ir_rvalue
*ret
= opt_return_value
->hir(instructions
, state
);
4019 /* The value of the return type can be NULL if the shader says
4020 * 'return foo();' and foo() is a function that returns void.
4022 * NOTE: The GLSL spec doesn't say that this is an error. The type
4023 * of the return value is void. If the return type of the function is
4024 * also void, then this should compile without error. Seriously.
4026 const glsl_type
*const ret_type
=
4027 (ret
== NULL
) ? glsl_type::void_type
: ret
->type
;
4029 /* Implicit conversions are not allowed for return values prior to
4030 * ARB_shading_language_420pack.
4032 if (state
->current_function
->return_type
!= ret_type
) {
4033 YYLTYPE loc
= this->get_location();
4035 if (state
->ARB_shading_language_420pack_enable
) {
4036 if (!apply_implicit_conversion(state
->current_function
->return_type
,
4038 _mesa_glsl_error(& loc
, state
,
4039 "could not implicitly convert return value "
4040 "to %s, in function `%s'",
4041 state
->current_function
->return_type
->name
,
4042 state
->current_function
->function_name());
4045 _mesa_glsl_error(& loc
, state
,
4046 "`return' with wrong type %s, in function `%s' "
4049 state
->current_function
->function_name(),
4050 state
->current_function
->return_type
->name
);
4052 } else if (state
->current_function
->return_type
->base_type
==
4054 YYLTYPE loc
= this->get_location();
4056 /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
4057 * specs add a clarification:
4059 * "A void function can only use return without a return argument, even if
4060 * the return argument has void type. Return statements only accept values:
4063 * void func2() { return func1(); } // illegal return statement"
4065 _mesa_glsl_error(& loc
, state
,
4066 "void functions can only use `return' without a "
4070 inst
= new(ctx
) ir_return(ret
);
4072 if (state
->current_function
->return_type
->base_type
!=
4074 YYLTYPE loc
= this->get_location();
4076 _mesa_glsl_error(& loc
, state
,
4077 "`return' with no value, in function %s returning "
4079 state
->current_function
->function_name());
4081 inst
= new(ctx
) ir_return
;
4084 state
->found_return
= true;
4085 instructions
->push_tail(inst
);
4090 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
4091 YYLTYPE loc
= this->get_location();
4093 _mesa_glsl_error(& loc
, state
,
4094 "`discard' may only appear in a fragment shader");
4096 instructions
->push_tail(new(ctx
) ir_discard
);
4101 if (mode
== ast_continue
&&
4102 state
->loop_nesting_ast
== NULL
) {
4103 YYLTYPE loc
= this->get_location();
4105 _mesa_glsl_error(& loc
, state
,
4106 "continue may only appear in a loop");
4107 } else if (mode
== ast_break
&&
4108 state
->loop_nesting_ast
== NULL
&&
4109 state
->switch_state
.switch_nesting_ast
== NULL
) {
4110 YYLTYPE loc
= this->get_location();
4112 _mesa_glsl_error(& loc
, state
,
4113 "break may only appear in a loop or a switch");
4115 /* For a loop, inline the for loop expression again, since we don't
4116 * know where near the end of the loop body the normal copy of it is
4117 * going to be placed. Same goes for the condition for a do-while
4120 if (state
->loop_nesting_ast
!= NULL
&&
4121 mode
== ast_continue
) {
4122 if (state
->loop_nesting_ast
->rest_expression
) {
4123 state
->loop_nesting_ast
->rest_expression
->hir(instructions
,
4126 if (state
->loop_nesting_ast
->mode
==
4127 ast_iteration_statement::ast_do_while
) {
4128 state
->loop_nesting_ast
->condition_to_hir(instructions
, state
);
4132 if (state
->switch_state
.is_switch_innermost
&&
4133 mode
== ast_break
) {
4134 /* Force break out of switch by setting is_break switch state.
4136 ir_variable
*const is_break_var
= state
->switch_state
.is_break_var
;
4137 ir_dereference_variable
*const deref_is_break_var
=
4138 new(ctx
) ir_dereference_variable(is_break_var
);
4139 ir_constant
*const true_val
= new(ctx
) ir_constant(true);
4140 ir_assignment
*const set_break_var
=
4141 new(ctx
) ir_assignment(deref_is_break_var
, true_val
);
4143 instructions
->push_tail(set_break_var
);
4146 ir_loop_jump
*const jump
=
4147 new(ctx
) ir_loop_jump((mode
== ast_break
)
4148 ? ir_loop_jump::jump_break
4149 : ir_loop_jump::jump_continue
);
4150 instructions
->push_tail(jump
);
4157 /* Jump instructions do not have r-values.
4164 ast_selection_statement::hir(exec_list
*instructions
,
4165 struct _mesa_glsl_parse_state
*state
)
4169 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
4171 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
4173 * "Any expression whose type evaluates to a Boolean can be used as the
4174 * conditional expression bool-expression. Vector types are not accepted
4175 * as the expression to if."
4177 * The checks are separated so that higher quality diagnostics can be
4178 * generated for cases where both rules are violated.
4180 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
4181 YYLTYPE loc
= this->condition
->get_location();
4183 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
4187 ir_if
*const stmt
= new(ctx
) ir_if(condition
);
4189 if (then_statement
!= NULL
) {
4190 state
->symbols
->push_scope();
4191 then_statement
->hir(& stmt
->then_instructions
, state
);
4192 state
->symbols
->pop_scope();
4195 if (else_statement
!= NULL
) {
4196 state
->symbols
->push_scope();
4197 else_statement
->hir(& stmt
->else_instructions
, state
);
4198 state
->symbols
->pop_scope();
4201 instructions
->push_tail(stmt
);
4203 /* if-statements do not have r-values.
4210 ast_switch_statement::hir(exec_list
*instructions
,
4211 struct _mesa_glsl_parse_state
*state
)
4215 ir_rvalue
*const test_expression
=
4216 this->test_expression
->hir(instructions
, state
);
4218 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
4220 * "The type of init-expression in a switch statement must be a
4223 if (!test_expression
->type
->is_scalar() ||
4224 !test_expression
->type
->is_integer()) {
4225 YYLTYPE loc
= this->test_expression
->get_location();
4227 _mesa_glsl_error(& loc
,
4229 "switch-statement expression must be scalar "
4233 /* Track the switch-statement nesting in a stack-like manner.
4235 struct glsl_switch_state saved
= state
->switch_state
;
4237 state
->switch_state
.is_switch_innermost
= true;
4238 state
->switch_state
.switch_nesting_ast
= this;
4239 state
->switch_state
.labels_ht
= hash_table_ctor(0, hash_table_pointer_hash
,
4240 hash_table_pointer_compare
);
4241 state
->switch_state
.previous_default
= NULL
;
4243 /* Initalize is_fallthru state to false.
4245 ir_rvalue
*const is_fallthru_val
= new (ctx
) ir_constant(false);
4246 state
->switch_state
.is_fallthru_var
=
4247 new(ctx
) ir_variable(glsl_type::bool_type
,
4248 "switch_is_fallthru_tmp",
4250 instructions
->push_tail(state
->switch_state
.is_fallthru_var
);
4252 ir_dereference_variable
*deref_is_fallthru_var
=
4253 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4254 instructions
->push_tail(new(ctx
) ir_assignment(deref_is_fallthru_var
,
4257 /* Initalize is_break state to false.
4259 ir_rvalue
*const is_break_val
= new (ctx
) ir_constant(false);
4260 state
->switch_state
.is_break_var
= new(ctx
) ir_variable(glsl_type::bool_type
,
4261 "switch_is_break_tmp",
4263 instructions
->push_tail(state
->switch_state
.is_break_var
);
4265 ir_dereference_variable
*deref_is_break_var
=
4266 new(ctx
) ir_dereference_variable(state
->switch_state
.is_break_var
);
4267 instructions
->push_tail(new(ctx
) ir_assignment(deref_is_break_var
,
4270 /* Cache test expression.
4272 test_to_hir(instructions
, state
);
4274 /* Emit code for body of switch stmt.
4276 body
->hir(instructions
, state
);
4278 hash_table_dtor(state
->switch_state
.labels_ht
);
4280 state
->switch_state
= saved
;
4282 /* Switch statements do not have r-values. */
4288 ast_switch_statement::test_to_hir(exec_list
*instructions
,
4289 struct _mesa_glsl_parse_state
*state
)
4293 /* Cache value of test expression. */
4294 ir_rvalue
*const test_val
=
4295 test_expression
->hir(instructions
,
4298 state
->switch_state
.test_var
= new(ctx
) ir_variable(test_val
->type
,
4301 ir_dereference_variable
*deref_test_var
=
4302 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
4304 instructions
->push_tail(state
->switch_state
.test_var
);
4305 instructions
->push_tail(new(ctx
) ir_assignment(deref_test_var
, test_val
));
4310 ast_switch_body::hir(exec_list
*instructions
,
4311 struct _mesa_glsl_parse_state
*state
)
4314 stmts
->hir(instructions
, state
);
4316 /* Switch bodies do not have r-values. */
4321 ast_case_statement_list::hir(exec_list
*instructions
,
4322 struct _mesa_glsl_parse_state
*state
)
4324 foreach_list_typed (ast_case_statement
, case_stmt
, link
, & this->cases
)
4325 case_stmt
->hir(instructions
, state
);
4327 /* Case statements do not have r-values. */
4332 ast_case_statement::hir(exec_list
*instructions
,
4333 struct _mesa_glsl_parse_state
*state
)
4335 labels
->hir(instructions
, state
);
4337 /* Conditionally set fallthru state based on break state. */
4338 ir_constant
*const false_val
= new(state
) ir_constant(false);
4339 ir_dereference_variable
*const deref_is_fallthru_var
=
4340 new(state
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4341 ir_dereference_variable
*const deref_is_break_var
=
4342 new(state
) ir_dereference_variable(state
->switch_state
.is_break_var
);
4343 ir_assignment
*const reset_fallthru_on_break
=
4344 new(state
) ir_assignment(deref_is_fallthru_var
,
4346 deref_is_break_var
);
4347 instructions
->push_tail(reset_fallthru_on_break
);
4349 /* Guard case statements depending on fallthru state. */
4350 ir_dereference_variable
*const deref_fallthru_guard
=
4351 new(state
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4352 ir_if
*const test_fallthru
= new(state
) ir_if(deref_fallthru_guard
);
4354 foreach_list_typed (ast_node
, stmt
, link
, & this->stmts
)
4355 stmt
->hir(& test_fallthru
->then_instructions
, state
);
4357 instructions
->push_tail(test_fallthru
);
4359 /* Case statements do not have r-values. */
4365 ast_case_label_list::hir(exec_list
*instructions
,
4366 struct _mesa_glsl_parse_state
*state
)
4368 foreach_list_typed (ast_case_label
, label
, link
, & this->labels
)
4369 label
->hir(instructions
, state
);
4371 /* Case labels do not have r-values. */
4376 ast_case_label::hir(exec_list
*instructions
,
4377 struct _mesa_glsl_parse_state
*state
)
4381 ir_dereference_variable
*deref_fallthru_var
=
4382 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4384 ir_rvalue
*const true_val
= new(ctx
) ir_constant(true);
4386 /* If not default case, ... */
4387 if (this->test_value
!= NULL
) {
4388 /* Conditionally set fallthru state based on
4389 * comparison of cached test expression value to case label.
4391 ir_rvalue
*const label_rval
= this->test_value
->hir(instructions
, state
);
4392 ir_constant
*label_const
= label_rval
->constant_expression_value();
4395 YYLTYPE loc
= this->test_value
->get_location();
4397 _mesa_glsl_error(& loc
, state
,
4398 "switch statement case label must be a "
4399 "constant expression");
4401 /* Stuff a dummy value in to allow processing to continue. */
4402 label_const
= new(ctx
) ir_constant(0);
4404 ast_expression
*previous_label
= (ast_expression
*)
4405 hash_table_find(state
->switch_state
.labels_ht
,
4406 (void *)(uintptr_t)label_const
->value
.u
[0]);
4408 if (previous_label
) {
4409 YYLTYPE loc
= this->test_value
->get_location();
4410 _mesa_glsl_error(& loc
, state
,
4411 "duplicate case value");
4413 loc
= previous_label
->get_location();
4414 _mesa_glsl_error(& loc
, state
,
4415 "this is the previous case label");
4417 hash_table_insert(state
->switch_state
.labels_ht
,
4419 (void *)(uintptr_t)label_const
->value
.u
[0]);
4423 ir_dereference_variable
*deref_test_var
=
4424 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
4426 ir_rvalue
*const test_cond
= new(ctx
) ir_expression(ir_binop_all_equal
,
4430 ir_assignment
*set_fallthru_on_test
=
4431 new(ctx
) ir_assignment(deref_fallthru_var
,
4435 instructions
->push_tail(set_fallthru_on_test
);
4436 } else { /* default case */
4437 if (state
->switch_state
.previous_default
) {
4438 YYLTYPE loc
= this->get_location();
4439 _mesa_glsl_error(& loc
, state
,
4440 "multiple default labels in one switch");
4442 loc
= state
->switch_state
.previous_default
->get_location();
4443 _mesa_glsl_error(& loc
, state
,
4444 "this is the first default label");
4446 state
->switch_state
.previous_default
= this;
4448 /* Set falltrhu state. */
4449 ir_assignment
*set_fallthru
=
4450 new(ctx
) ir_assignment(deref_fallthru_var
, true_val
);
4452 instructions
->push_tail(set_fallthru
);
4455 /* Case statements do not have r-values. */
4460 ast_iteration_statement::condition_to_hir(exec_list
*instructions
,
4461 struct _mesa_glsl_parse_state
*state
)
4465 if (condition
!= NULL
) {
4466 ir_rvalue
*const cond
=
4467 condition
->hir(instructions
, state
);
4470 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
4471 YYLTYPE loc
= condition
->get_location();
4473 _mesa_glsl_error(& loc
, state
,
4474 "loop condition must be scalar boolean");
4476 /* As the first code in the loop body, generate a block that looks
4477 * like 'if (!condition) break;' as the loop termination condition.
4479 ir_rvalue
*const not_cond
=
4480 new(ctx
) ir_expression(ir_unop_logic_not
, cond
);
4482 ir_if
*const if_stmt
= new(ctx
) ir_if(not_cond
);
4484 ir_jump
*const break_stmt
=
4485 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
4487 if_stmt
->then_instructions
.push_tail(break_stmt
);
4488 instructions
->push_tail(if_stmt
);
4495 ast_iteration_statement::hir(exec_list
*instructions
,
4496 struct _mesa_glsl_parse_state
*state
)
4500 /* For-loops and while-loops start a new scope, but do-while loops do not.
4502 if (mode
!= ast_do_while
)
4503 state
->symbols
->push_scope();
4505 if (init_statement
!= NULL
)
4506 init_statement
->hir(instructions
, state
);
4508 ir_loop
*const stmt
= new(ctx
) ir_loop();
4509 instructions
->push_tail(stmt
);
4511 /* Track the current loop nesting. */
4512 ast_iteration_statement
*nesting_ast
= state
->loop_nesting_ast
;
4514 state
->loop_nesting_ast
= this;
4516 /* Likewise, indicate that following code is closest to a loop,
4517 * NOT closest to a switch.
4519 bool saved_is_switch_innermost
= state
->switch_state
.is_switch_innermost
;
4520 state
->switch_state
.is_switch_innermost
= false;
4522 if (mode
!= ast_do_while
)
4523 condition_to_hir(&stmt
->body_instructions
, state
);
4526 body
->hir(& stmt
->body_instructions
, state
);
4528 if (rest_expression
!= NULL
)
4529 rest_expression
->hir(& stmt
->body_instructions
, state
);
4531 if (mode
== ast_do_while
)
4532 condition_to_hir(&stmt
->body_instructions
, state
);
4534 if (mode
!= ast_do_while
)
4535 state
->symbols
->pop_scope();
4537 /* Restore previous nesting before returning. */
4538 state
->loop_nesting_ast
= nesting_ast
;
4539 state
->switch_state
.is_switch_innermost
= saved_is_switch_innermost
;
4541 /* Loops do not have r-values.
4548 * Determine if the given type is valid for establishing a default precision
4551 * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
4553 * "The precision statement
4555 * precision precision-qualifier type;
4557 * can be used to establish a default precision qualifier. The type field
4558 * can be either int or float or any of the sampler types, and the
4559 * precision-qualifier can be lowp, mediump, or highp."
4561 * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
4562 * qualifiers on sampler types, but this seems like an oversight (since the
4563 * intention of including these in GLSL 1.30 is to allow compatibility with ES
4564 * shaders). So we allow int, float, and all sampler types regardless of GLSL
4568 is_valid_default_precision_type(const struct glsl_type
*const type
)
4573 switch (type
->base_type
) {
4575 case GLSL_TYPE_FLOAT
:
4576 /* "int" and "float" are valid, but vectors and matrices are not. */
4577 return type
->vector_elements
== 1 && type
->matrix_columns
== 1;
4578 case GLSL_TYPE_SAMPLER
:
4587 ast_type_specifier::hir(exec_list
*instructions
,
4588 struct _mesa_glsl_parse_state
*state
)
4590 if (this->default_precision
== ast_precision_none
&& this->structure
== NULL
)
4593 YYLTYPE loc
= this->get_location();
4595 /* If this is a precision statement, check that the type to which it is
4596 * applied is either float or int.
4598 * From section 4.5.3 of the GLSL 1.30 spec:
4599 * "The precision statement
4600 * precision precision-qualifier type;
4601 * can be used to establish a default precision qualifier. The type
4602 * field can be either int or float [...]. Any other types or
4603 * qualifiers will result in an error.
4605 if (this->default_precision
!= ast_precision_none
) {
4606 if (!state
->check_precision_qualifiers_allowed(&loc
))
4609 if (this->structure
!= NULL
) {
4610 _mesa_glsl_error(&loc
, state
,
4611 "precision qualifiers do not apply to structures");
4615 if (this->array_specifier
!= NULL
) {
4616 _mesa_glsl_error(&loc
, state
,
4617 "default precision statements do not apply to "
4622 const struct glsl_type
*const type
=
4623 state
->symbols
->get_type(this->type_name
);
4624 if (!is_valid_default_precision_type(type
)) {
4625 _mesa_glsl_error(&loc
, state
,
4626 "default precision statements apply only to "
4627 "float, int, and sampler types");
4631 if (type
->base_type
== GLSL_TYPE_FLOAT
4633 && state
->stage
== MESA_SHADER_FRAGMENT
) {
4634 /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
4637 * "The fragment language has no default precision qualifier for
4638 * floating point types."
4640 * As a result, we have to track whether or not default precision has
4641 * been specified for float in GLSL ES fragment shaders.
4643 * Earlier in that same section, the spec says:
4645 * "Non-precision qualified declarations will use the precision
4646 * qualifier specified in the most recent precision statement
4647 * that is still in scope. The precision statement has the same
4648 * scoping rules as variable declarations. If it is declared
4649 * inside a compound statement, its effect stops at the end of
4650 * the innermost statement it was declared in. Precision
4651 * statements in nested scopes override precision statements in
4652 * outer scopes. Multiple precision statements for the same basic
4653 * type can appear inside the same scope, with later statements
4654 * overriding earlier statements within that scope."
4656 * Default precision specifications follow the same scope rules as
4657 * variables. So, we can track the state of the default float
4658 * precision in the symbol table, and the rules will just work. This
4659 * is a slight abuse of the symbol table, but it has the semantics
4662 ir_variable
*const junk
=
4663 new(state
) ir_variable(type
, "#default precision",
4666 state
->symbols
->add_variable(junk
);
4669 /* FINISHME: Translate precision statements into IR. */
4673 /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
4674 * process_record_constructor() can do type-checking on C-style initializer
4675 * expressions of structs, but ast_struct_specifier should only be translated
4676 * to HIR if it is declaring the type of a structure.
4678 * The ->is_declaration field is false for initializers of variables
4679 * declared separately from the struct's type definition.
4681 * struct S { ... }; (is_declaration = true)
4682 * struct T { ... } t = { ... }; (is_declaration = true)
4683 * S s = { ... }; (is_declaration = false)
4685 if (this->structure
!= NULL
&& this->structure
->is_declaration
)
4686 return this->structure
->hir(instructions
, state
);
4693 * Process a structure or interface block tree into an array of structure fields
4695 * After parsing, where there are some syntax differnces, structures and
4696 * interface blocks are almost identical. They are similar enough that the
4697 * AST for each can be processed the same way into a set of
4698 * \c glsl_struct_field to describe the members.
4700 * If we're processing an interface block, var_mode should be the type of the
4701 * interface block (ir_var_shader_in, ir_var_shader_out, or ir_var_uniform).
4702 * If we're processing a structure, var_mode should be ir_var_auto.
4705 * The number of fields processed. A pointer to the array structure fields is
4706 * stored in \c *fields_ret.
4709 ast_process_structure_or_interface_block(exec_list
*instructions
,
4710 struct _mesa_glsl_parse_state
*state
,
4711 exec_list
*declarations
,
4713 glsl_struct_field
**fields_ret
,
4715 bool block_row_major
,
4716 bool allow_reserved_names
,
4717 ir_variable_mode var_mode
)
4719 unsigned decl_count
= 0;
4721 /* Make an initial pass over the list of fields to determine how
4722 * many there are. Each element in this list is an ast_declarator_list.
4723 * This means that we actually need to count the number of elements in the
4724 * 'declarations' list in each of the elements.
4726 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
4727 foreach_list_const (decl_ptr
, & decl_list
->declarations
) {
4732 /* Allocate storage for the fields and process the field
4733 * declarations. As the declarations are processed, try to also convert
4734 * the types to HIR. This ensures that structure definitions embedded in
4735 * other structure definitions or in interface blocks are processed.
4737 glsl_struct_field
*const fields
= ralloc_array(state
, glsl_struct_field
,
4741 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
4742 const char *type_name
;
4744 decl_list
->type
->specifier
->hir(instructions
, state
);
4746 /* Section 10.9 of the GLSL ES 1.00 specification states that
4747 * embedded structure definitions have been removed from the language.
4749 if (state
->es_shader
&& decl_list
->type
->specifier
->structure
!= NULL
) {
4750 _mesa_glsl_error(&loc
, state
, "embedded structure definitions are "
4751 "not allowed in GLSL ES 1.00");
4754 const glsl_type
*decl_type
=
4755 decl_list
->type
->glsl_type(& type_name
, state
);
4757 foreach_list_typed (ast_declaration
, decl
, link
,
4758 &decl_list
->declarations
) {
4759 if (!allow_reserved_names
)
4760 validate_identifier(decl
->identifier
, loc
, state
);
4762 /* From section 4.3.9 of the GLSL 4.40 spec:
4764 * "[In interface blocks] opaque types are not allowed."
4766 * It should be impossible for decl_type to be NULL here. Cases that
4767 * might naturally lead to decl_type being NULL, especially for the
4768 * is_interface case, will have resulted in compilation having
4769 * already halted due to a syntax error.
4771 const struct glsl_type
*field_type
=
4772 decl_type
!= NULL
? decl_type
: glsl_type::error_type
;
4774 if (is_interface
&& field_type
->contains_opaque()) {
4775 YYLTYPE loc
= decl_list
->get_location();
4776 _mesa_glsl_error(&loc
, state
,
4777 "uniform in non-default uniform block contains "
4781 if (field_type
->contains_atomic()) {
4782 /* FINISHME: Add a spec quotation here once updated spec
4783 * FINISHME: language is available. See Khronos bug #10903
4784 * FINISHME: on whether atomic counters are allowed in
4785 * FINISHME: structures.
4787 YYLTYPE loc
= decl_list
->get_location();
4788 _mesa_glsl_error(&loc
, state
, "atomic counter in structure or "
4792 if (field_type
->contains_image()) {
4793 /* FINISHME: Same problem as with atomic counters.
4794 * FINISHME: Request clarification from Khronos and add
4795 * FINISHME: spec quotation here.
4797 YYLTYPE loc
= decl_list
->get_location();
4798 _mesa_glsl_error(&loc
, state
,
4799 "image in structure or uniform block");
4802 const struct ast_type_qualifier
*const qual
=
4803 & decl_list
->type
->qualifier
;
4804 if (qual
->flags
.q
.std140
||
4805 qual
->flags
.q
.packed
||
4806 qual
->flags
.q
.shared
) {
4807 _mesa_glsl_error(&loc
, state
,
4808 "uniform block layout qualifiers std140, packed, and "
4809 "shared can only be applied to uniform blocks, not "
4813 field_type
= process_array_type(&loc
, decl_type
,
4814 decl
->array_specifier
, state
);
4815 fields
[i
].type
= field_type
;
4816 fields
[i
].name
= decl
->identifier
;
4817 fields
[i
].location
= -1;
4818 fields
[i
].interpolation
=
4819 interpret_interpolation_qualifier(qual
, var_mode
, state
, &loc
);
4820 fields
[i
].centroid
= qual
->flags
.q
.centroid
? 1 : 0;
4821 fields
[i
].sample
= qual
->flags
.q
.sample
? 1 : 0;
4823 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
4824 if (!qual
->flags
.q
.uniform
) {
4825 _mesa_glsl_error(&loc
, state
,
4826 "row_major and column_major can only be "
4827 "applied to uniform interface blocks");
4829 validate_matrix_layout_for_type(state
, &loc
, field_type
, NULL
);
4832 if (qual
->flags
.q
.uniform
&& qual
->has_interpolation()) {
4833 _mesa_glsl_error(&loc
, state
,
4834 "interpolation qualifiers cannot be used "
4835 "with uniform interface blocks");
4838 if (field_type
->is_matrix() ||
4839 (field_type
->is_array() && field_type
->fields
.array
->is_matrix())) {
4840 fields
[i
].row_major
= block_row_major
;
4841 if (qual
->flags
.q
.row_major
)
4842 fields
[i
].row_major
= true;
4843 else if (qual
->flags
.q
.column_major
)
4844 fields
[i
].row_major
= false;
4851 assert(i
== decl_count
);
4853 *fields_ret
= fields
;
4859 ast_struct_specifier::hir(exec_list
*instructions
,
4860 struct _mesa_glsl_parse_state
*state
)
4862 YYLTYPE loc
= this->get_location();
4864 /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
4866 * "Anonymous structures are not supported; so embedded structures must
4867 * have a declarator. A name given to an embedded struct is scoped at
4868 * the same level as the struct it is embedded in."
4870 * The same section of the GLSL 1.20 spec says:
4872 * "Anonymous structures are not supported. Embedded structures are not
4875 * struct S { float f; };
4877 * S; // Error: anonymous structures disallowed
4878 * struct { ... }; // Error: embedded structures disallowed
4879 * S s; // Okay: nested structures with name are allowed
4882 * The GLSL ES 1.00 and 3.00 specs have similar langauge and examples. So,
4883 * we allow embedded structures in 1.10 only.
4885 if (state
->language_version
!= 110 && state
->struct_specifier_depth
!= 0)
4886 _mesa_glsl_error(&loc
, state
,
4887 "embedded structure declartions are not allowed");
4889 state
->struct_specifier_depth
++;
4891 glsl_struct_field
*fields
;
4892 unsigned decl_count
=
4893 ast_process_structure_or_interface_block(instructions
,
4895 &this->declarations
,
4900 false /* allow_reserved_names */,
4903 validate_identifier(this->name
, loc
, state
);
4905 const glsl_type
*t
=
4906 glsl_type::get_record_instance(fields
, decl_count
, this->name
);
4908 if (!state
->symbols
->add_type(name
, t
)) {
4909 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
4911 const glsl_type
**s
= reralloc(state
, state
->user_structures
,
4913 state
->num_user_structures
+ 1);
4915 s
[state
->num_user_structures
] = t
;
4916 state
->user_structures
= s
;
4917 state
->num_user_structures
++;
4921 state
->struct_specifier_depth
--;
4923 /* Structure type definitions do not have r-values.
4930 * Visitor class which detects whether a given interface block has been used.
4932 class interface_block_usage_visitor
: public ir_hierarchical_visitor
4935 interface_block_usage_visitor(ir_variable_mode mode
, const glsl_type
*block
)
4936 : mode(mode
), block(block
), found(false)
4940 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
4942 if (ir
->var
->data
.mode
== mode
&& ir
->var
->get_interface_type() == block
) {
4946 return visit_continue
;
4949 bool usage_found() const
4955 ir_variable_mode mode
;
4956 const glsl_type
*block
;
4962 ast_interface_block::hir(exec_list
*instructions
,
4963 struct _mesa_glsl_parse_state
*state
)
4965 YYLTYPE loc
= this->get_location();
4967 /* The ast_interface_block has a list of ast_declarator_lists. We
4968 * need to turn those into ir_variables with an association
4969 * with this uniform block.
4971 enum glsl_interface_packing packing
;
4972 if (this->layout
.flags
.q
.shared
) {
4973 packing
= GLSL_INTERFACE_PACKING_SHARED
;
4974 } else if (this->layout
.flags
.q
.packed
) {
4975 packing
= GLSL_INTERFACE_PACKING_PACKED
;
4977 /* The default layout is std140.
4979 packing
= GLSL_INTERFACE_PACKING_STD140
;
4982 ir_variable_mode var_mode
;
4983 const char *iface_type_name
;
4984 if (this->layout
.flags
.q
.in
) {
4985 var_mode
= ir_var_shader_in
;
4986 iface_type_name
= "in";
4987 } else if (this->layout
.flags
.q
.out
) {
4988 var_mode
= ir_var_shader_out
;
4989 iface_type_name
= "out";
4990 } else if (this->layout
.flags
.q
.uniform
) {
4991 var_mode
= ir_var_uniform
;
4992 iface_type_name
= "uniform";
4994 var_mode
= ir_var_auto
;
4995 iface_type_name
= "UNKNOWN";
4996 assert(!"interface block layout qualifier not found!");
4999 bool redeclaring_per_vertex
= strcmp(this->block_name
, "gl_PerVertex") == 0;
5000 bool block_row_major
= this->layout
.flags
.q
.row_major
;
5001 exec_list declared_variables
;
5002 glsl_struct_field
*fields
;
5003 unsigned int num_variables
=
5004 ast_process_structure_or_interface_block(&declared_variables
,
5006 &this->declarations
,
5011 redeclaring_per_vertex
,
5014 if (!redeclaring_per_vertex
)
5015 validate_identifier(this->block_name
, loc
, state
);
5017 const glsl_type
*earlier_per_vertex
= NULL
;
5018 if (redeclaring_per_vertex
) {
5019 /* Find the previous declaration of gl_PerVertex. If we're redeclaring
5020 * the named interface block gl_in, we can find it by looking at the
5021 * previous declaration of gl_in. Otherwise we can find it by looking
5022 * at the previous decalartion of any of the built-in outputs,
5025 * Also check that the instance name and array-ness of the redeclaration
5029 case ir_var_shader_in
:
5030 if (ir_variable
*earlier_gl_in
=
5031 state
->symbols
->get_variable("gl_in")) {
5032 earlier_per_vertex
= earlier_gl_in
->get_interface_type();
5034 _mesa_glsl_error(&loc
, state
,
5035 "redeclaration of gl_PerVertex input not allowed "
5037 _mesa_shader_stage_to_string(state
->stage
));
5039 if (this->instance_name
== NULL
||
5040 strcmp(this->instance_name
, "gl_in") != 0 || this->array_specifier
== NULL
) {
5041 _mesa_glsl_error(&loc
, state
,
5042 "gl_PerVertex input must be redeclared as "
5046 case ir_var_shader_out
:
5047 if (ir_variable
*earlier_gl_Position
=
5048 state
->symbols
->get_variable("gl_Position")) {
5049 earlier_per_vertex
= earlier_gl_Position
->get_interface_type();
5051 _mesa_glsl_error(&loc
, state
,
5052 "redeclaration of gl_PerVertex output not "
5053 "allowed in the %s shader",
5054 _mesa_shader_stage_to_string(state
->stage
));
5056 if (this->instance_name
!= NULL
) {
5057 _mesa_glsl_error(&loc
, state
,
5058 "gl_PerVertex input may not be redeclared with "
5059 "an instance name");
5063 _mesa_glsl_error(&loc
, state
,
5064 "gl_PerVertex must be declared as an input or an "
5069 if (earlier_per_vertex
== NULL
) {
5070 /* An error has already been reported. Bail out to avoid null
5071 * dereferences later in this function.
5076 /* Copy locations from the old gl_PerVertex interface block. */
5077 for (unsigned i
= 0; i
< num_variables
; i
++) {
5078 int j
= earlier_per_vertex
->field_index(fields
[i
].name
);
5080 _mesa_glsl_error(&loc
, state
,
5081 "redeclaration of gl_PerVertex must be a subset "
5082 "of the built-in members of gl_PerVertex");
5084 fields
[i
].location
=
5085 earlier_per_vertex
->fields
.structure
[j
].location
;
5086 fields
[i
].interpolation
=
5087 earlier_per_vertex
->fields
.structure
[j
].interpolation
;
5088 fields
[i
].centroid
=
5089 earlier_per_vertex
->fields
.structure
[j
].centroid
;
5091 earlier_per_vertex
->fields
.structure
[j
].sample
;
5095 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10
5098 * If a built-in interface block is redeclared, it must appear in
5099 * the shader before any use of any member included in the built-in
5100 * declaration, or a compilation error will result.
5102 * This appears to be a clarification to the behaviour established for
5103 * gl_PerVertex by GLSL 1.50, therefore we implement this behaviour
5104 * regardless of GLSL version.
5106 interface_block_usage_visitor
v(var_mode
, earlier_per_vertex
);
5107 v
.run(instructions
);
5108 if (v
.usage_found()) {
5109 _mesa_glsl_error(&loc
, state
,
5110 "redeclaration of a built-in interface block must "
5111 "appear before any use of any member of the "
5116 const glsl_type
*block_type
=
5117 glsl_type::get_interface_instance(fields
,
5122 if (!state
->symbols
->add_interface(block_type
->name
, block_type
, var_mode
)) {
5123 YYLTYPE loc
= this->get_location();
5124 _mesa_glsl_error(&loc
, state
, "interface block `%s' with type `%s' "
5125 "already taken in the current scope",
5126 this->block_name
, iface_type_name
);
5129 /* Since interface blocks cannot contain statements, it should be
5130 * impossible for the block to generate any instructions.
5132 assert(declared_variables
.is_empty());
5134 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
5136 * Geometry shader input variables get the per-vertex values written
5137 * out by vertex shader output variables of the same names. Since a
5138 * geometry shader operates on a set of vertices, each input varying
5139 * variable (or input block, see interface blocks below) needs to be
5140 * declared as an array.
5142 if (state
->stage
== MESA_SHADER_GEOMETRY
&& this->array_specifier
== NULL
&&
5143 var_mode
== ir_var_shader_in
) {
5144 _mesa_glsl_error(&loc
, state
, "geometry shader inputs must be arrays");
5147 /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
5150 * "If an instance name (instance-name) is used, then it puts all the
5151 * members inside a scope within its own name space, accessed with the
5152 * field selector ( . ) operator (analogously to structures)."
5154 if (this->instance_name
) {
5155 if (redeclaring_per_vertex
) {
5156 /* When a built-in in an unnamed interface block is redeclared,
5157 * get_variable_being_redeclared() calls
5158 * check_builtin_array_max_size() to make sure that built-in array
5159 * variables aren't redeclared to illegal sizes. But we're looking
5160 * at a redeclaration of a named built-in interface block. So we
5161 * have to manually call check_builtin_array_max_size() for all parts
5162 * of the interface that are arrays.
5164 for (unsigned i
= 0; i
< num_variables
; i
++) {
5165 if (fields
[i
].type
->is_array()) {
5166 const unsigned size
= fields
[i
].type
->array_size();
5167 check_builtin_array_max_size(fields
[i
].name
, size
, loc
, state
);
5171 validate_identifier(this->instance_name
, loc
, state
);
5176 if (this->array_specifier
!= NULL
) {
5177 /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
5179 * For uniform blocks declared an array, each individual array
5180 * element corresponds to a separate buffer object backing one
5181 * instance of the block. As the array size indicates the number
5182 * of buffer objects needed, uniform block array declarations
5183 * must specify an array size.
5185 * And a few paragraphs later:
5187 * Geometry shader input blocks must be declared as arrays and
5188 * follow the array declaration and linking rules for all
5189 * geometry shader inputs. All other input and output block
5190 * arrays must specify an array size.
5192 * The upshot of this is that the only circumstance where an
5193 * interface array size *doesn't* need to be specified is on a
5194 * geometry shader input.
5196 if (this->array_specifier
->is_unsized_array
&&
5197 (state
->stage
!= MESA_SHADER_GEOMETRY
|| !this->layout
.flags
.q
.in
)) {
5198 _mesa_glsl_error(&loc
, state
,
5199 "only geometry shader inputs may be unsized "
5200 "instance block arrays");
5204 const glsl_type
*block_array_type
=
5205 process_array_type(&loc
, block_type
, this->array_specifier
, state
);
5207 var
= new(state
) ir_variable(block_array_type
,
5208 this->instance_name
,
5211 var
= new(state
) ir_variable(block_type
,
5212 this->instance_name
,
5216 if (state
->stage
== MESA_SHADER_GEOMETRY
&& var_mode
== ir_var_shader_in
)
5217 handle_geometry_shader_input_decl(state
, loc
, var
);
5219 if (ir_variable
*earlier
=
5220 state
->symbols
->get_variable(this->instance_name
)) {
5221 if (!redeclaring_per_vertex
) {
5222 _mesa_glsl_error(&loc
, state
, "`%s' redeclared",
5223 this->instance_name
);
5225 earlier
->data
.how_declared
= ir_var_declared_normally
;
5226 earlier
->type
= var
->type
;
5227 earlier
->reinit_interface_type(block_type
);
5230 state
->symbols
->add_variable(var
);
5231 instructions
->push_tail(var
);
5234 /* In order to have an array size, the block must also be declared with
5237 assert(this->array_specifier
== NULL
);
5239 for (unsigned i
= 0; i
< num_variables
; i
++) {
5241 new(state
) ir_variable(fields
[i
].type
,
5242 ralloc_strdup(state
, fields
[i
].name
),
5244 var
->data
.interpolation
= fields
[i
].interpolation
;
5245 var
->data
.centroid
= fields
[i
].centroid
;
5246 var
->data
.sample
= fields
[i
].sample
;
5247 var
->init_interface_type(block_type
);
5249 if (redeclaring_per_vertex
) {
5250 ir_variable
*earlier
=
5251 get_variable_being_redeclared(var
, loc
, state
,
5252 true /* allow_all_redeclarations */);
5253 if (strncmp(var
->name
, "gl_", 3) != 0 || earlier
== NULL
) {
5254 _mesa_glsl_error(&loc
, state
,
5255 "redeclaration of gl_PerVertex can only "
5256 "include built-in variables");
5257 } else if (earlier
->data
.how_declared
== ir_var_declared_normally
) {
5258 _mesa_glsl_error(&loc
, state
,
5259 "`%s' has already been redeclared", var
->name
);
5261 earlier
->data
.how_declared
= ir_var_declared_in_block
;
5262 earlier
->reinit_interface_type(block_type
);
5267 if (state
->symbols
->get_variable(var
->name
) != NULL
)
5268 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
5270 /* Propagate the "binding" keyword into this UBO's fields;
5271 * the UBO declaration itself doesn't get an ir_variable unless it
5272 * has an instance name. This is ugly.
5274 var
->data
.explicit_binding
= this->layout
.flags
.q
.explicit_binding
;
5275 var
->data
.binding
= this->layout
.binding
;
5277 state
->symbols
->add_variable(var
);
5278 instructions
->push_tail(var
);
5281 if (redeclaring_per_vertex
&& block_type
!= earlier_per_vertex
) {
5282 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec:
5284 * It is also a compilation error ... to redeclare a built-in
5285 * block and then use a member from that built-in block that was
5286 * not included in the redeclaration.
5288 * This appears to be a clarification to the behaviour established
5289 * for gl_PerVertex by GLSL 1.50, therefore we implement this
5290 * behaviour regardless of GLSL version.
5292 * To prevent the shader from using a member that was not included in
5293 * the redeclaration, we disable any ir_variables that are still
5294 * associated with the old declaration of gl_PerVertex (since we've
5295 * already updated all of the variables contained in the new
5296 * gl_PerVertex to point to it).
5298 * As a side effect this will prevent
5299 * validate_intrastage_interface_blocks() from getting confused and
5300 * thinking there are conflicting definitions of gl_PerVertex in the
5303 foreach_list_safe(node
, instructions
) {
5304 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
5306 var
->get_interface_type() == earlier_per_vertex
&&
5307 var
->data
.mode
== var_mode
) {
5308 if (var
->data
.how_declared
== ir_var_declared_normally
) {
5309 _mesa_glsl_error(&loc
, state
,
5310 "redeclaration of gl_PerVertex cannot "
5311 "follow a redeclaration of `%s'",
5314 state
->symbols
->disable_variable(var
->name
);
5326 ast_gs_input_layout::hir(exec_list
*instructions
,
5327 struct _mesa_glsl_parse_state
*state
)
5329 YYLTYPE loc
= this->get_location();
5331 /* If any geometry input layout declaration preceded this one, make sure it
5332 * was consistent with this one.
5334 if (state
->gs_input_prim_type_specified
&&
5335 state
->gs_input_prim_type
!= this->prim_type
) {
5336 _mesa_glsl_error(&loc
, state
,
5337 "geometry shader input layout does not match"
5338 " previous declaration");
5342 /* If any shader inputs occurred before this declaration and specified an
5343 * array size, make sure the size they specified is consistent with the
5346 unsigned num_vertices
= vertices_per_prim(this->prim_type
);
5347 if (state
->gs_input_size
!= 0 && state
->gs_input_size
!= num_vertices
) {
5348 _mesa_glsl_error(&loc
, state
,
5349 "this geometry shader input layout implies %u vertices"
5350 " per primitive, but a previous input is declared"
5351 " with size %u", num_vertices
, state
->gs_input_size
);
5355 state
->gs_input_prim_type_specified
= true;
5356 state
->gs_input_prim_type
= this->prim_type
;
5358 /* If any shader inputs occurred before this declaration and did not
5359 * specify an array size, their size is determined now.
5361 foreach_list (node
, instructions
) {
5362 ir_variable
*var
= ((ir_instruction
*) node
)->as_variable();
5363 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_in
)
5366 /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
5370 if (var
->type
->is_unsized_array()) {
5371 if (var
->data
.max_array_access
>= num_vertices
) {
5372 _mesa_glsl_error(&loc
, state
,
5373 "this geometry shader input layout implies %u"
5374 " vertices, but an access to element %u of input"
5375 " `%s' already exists", num_vertices
,
5376 var
->data
.max_array_access
, var
->name
);
5378 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
5389 ast_cs_input_layout::hir(exec_list
*instructions
,
5390 struct _mesa_glsl_parse_state
*state
)
5392 YYLTYPE loc
= this->get_location();
5394 /* If any compute input layout declaration preceded this one, make sure it
5395 * was consistent with this one.
5397 if (state
->cs_input_local_size_specified
) {
5398 for (int i
= 0; i
< 3; i
++) {
5399 if (state
->cs_input_local_size
[i
] != this->local_size
[i
]) {
5400 _mesa_glsl_error(&loc
, state
,
5401 "compute shader input layout does not match"
5402 " previous declaration");
5408 /* From the ARB_compute_shader specification:
5410 * If the local size of the shader in any dimension is greater
5411 * than the maximum size supported by the implementation for that
5412 * dimension, a compile-time error results.
5414 * It is not clear from the spec how the error should be reported if
5415 * the total size of the work group exceeds
5416 * MAX_COMPUTE_WORK_GROUP_INVOCATIONS, but it seems reasonable to
5417 * report it at compile time as well.
5419 GLuint64 total_invocations
= 1;
5420 for (int i
= 0; i
< 3; i
++) {
5421 if (this->local_size
[i
] > state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]) {
5422 _mesa_glsl_error(&loc
, state
,
5423 "local_size_%c exceeds MAX_COMPUTE_WORK_GROUP_SIZE"
5425 state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]);
5428 total_invocations
*= this->local_size
[i
];
5429 if (total_invocations
>
5430 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
) {
5431 _mesa_glsl_error(&loc
, state
,
5432 "product of local_sizes exceeds "
5433 "MAX_COMPUTE_WORK_GROUP_INVOCATIONS (%d)",
5434 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
);
5439 state
->cs_input_local_size_specified
= true;
5440 for (int i
= 0; i
< 3; i
++)
5441 state
->cs_input_local_size
[i
] = this->local_size
[i
];
5443 /* We may now declare the built-in constant gl_WorkGroupSize (see
5444 * builtin_variable_generator::generate_constants() for why we didn't
5445 * declare it earlier).
5447 ir_variable
*var
= new(state
->symbols
)
5448 ir_variable(glsl_type::ivec3_type
, "gl_WorkGroupSize", ir_var_auto
);
5449 var
->data
.how_declared
= ir_var_declared_implicitly
;
5450 var
->data
.read_only
= true;
5451 instructions
->push_tail(var
);
5452 state
->symbols
->add_variable(var
);
5453 ir_constant_data data
;
5454 memset(&data
, 0, sizeof(data
));
5455 for (int i
= 0; i
< 3; i
++)
5456 data
.i
[i
] = this->local_size
[i
];
5457 var
->constant_value
= new(var
) ir_constant(glsl_type::ivec3_type
, &data
);
5458 var
->constant_initializer
=
5459 new(var
) ir_constant(glsl_type::ivec3_type
, &data
);
5460 var
->data
.has_initializer
= true;
5467 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
5468 exec_list
*instructions
)
5470 bool gl_FragColor_assigned
= false;
5471 bool gl_FragData_assigned
= false;
5472 bool user_defined_fs_output_assigned
= false;
5473 ir_variable
*user_defined_fs_output
= NULL
;
5475 /* It would be nice to have proper location information. */
5477 memset(&loc
, 0, sizeof(loc
));
5479 foreach_list(node
, instructions
) {
5480 ir_variable
*var
= ((ir_instruction
*)node
)->as_variable();
5482 if (!var
|| !var
->data
.assigned
)
5485 if (strcmp(var
->name
, "gl_FragColor") == 0)
5486 gl_FragColor_assigned
= true;
5487 else if (strcmp(var
->name
, "gl_FragData") == 0)
5488 gl_FragData_assigned
= true;
5489 else if (strncmp(var
->name
, "gl_", 3) != 0) {
5490 if (state
->stage
== MESA_SHADER_FRAGMENT
&&
5491 var
->data
.mode
== ir_var_shader_out
) {
5492 user_defined_fs_output_assigned
= true;
5493 user_defined_fs_output
= var
;
5498 /* From the GLSL 1.30 spec:
5500 * "If a shader statically assigns a value to gl_FragColor, it
5501 * may not assign a value to any element of gl_FragData. If a
5502 * shader statically writes a value to any element of
5503 * gl_FragData, it may not assign a value to
5504 * gl_FragColor. That is, a shader may assign values to either
5505 * gl_FragColor or gl_FragData, but not both. Multiple shaders
5506 * linked together must also consistently write just one of
5507 * these variables. Similarly, if user declared output
5508 * variables are in use (statically assigned to), then the
5509 * built-in variables gl_FragColor and gl_FragData may not be
5510 * assigned to. These incorrect usages all generate compile
5513 if (gl_FragColor_assigned
&& gl_FragData_assigned
) {
5514 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
5515 "`gl_FragColor' and `gl_FragData'");
5516 } else if (gl_FragColor_assigned
&& user_defined_fs_output_assigned
) {
5517 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
5518 "`gl_FragColor' and `%s'",
5519 user_defined_fs_output
->name
);
5520 } else if (gl_FragData_assigned
&& user_defined_fs_output_assigned
) {
5521 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
5522 "`gl_FragData' and `%s'",
5523 user_defined_fs_output
->name
);
5529 remove_per_vertex_blocks(exec_list
*instructions
,
5530 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
)
5532 /* Find the gl_PerVertex interface block of the appropriate (in/out) mode,
5533 * if it exists in this shader type.
5535 const glsl_type
*per_vertex
= NULL
;
5537 case ir_var_shader_in
:
5538 if (ir_variable
*gl_in
= state
->symbols
->get_variable("gl_in"))
5539 per_vertex
= gl_in
->get_interface_type();
5541 case ir_var_shader_out
:
5542 if (ir_variable
*gl_Position
=
5543 state
->symbols
->get_variable("gl_Position")) {
5544 per_vertex
= gl_Position
->get_interface_type();
5548 assert(!"Unexpected mode");
5552 /* If we didn't find a built-in gl_PerVertex interface block, then we don't
5553 * need to do anything.
5555 if (per_vertex
== NULL
)
5558 /* If the interface block is used by the shader, then we don't need to do
5561 interface_block_usage_visitor
v(mode
, per_vertex
);
5562 v
.run(instructions
);
5563 if (v
.usage_found())
5566 /* Remove any ir_variable declarations that refer to the interface block
5569 foreach_list_safe(node
, instructions
) {
5570 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
5571 if (var
!= NULL
&& var
->get_interface_type() == per_vertex
&&
5572 var
->data
.mode
== mode
) {
5573 state
->symbols
->disable_variable(var
->name
);