2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
26 * Convert abstract syntax to to high-level intermediate reprensentation (HIR).
28 * During the conversion to HIR, the majority of the symantic checking is
29 * preformed on the program. This includes:
31 * * Symbol table management
35 * The majority of this work could be done during parsing, and the parser could
36 * probably generate HIR directly. However, this results in frequent changes
37 * to the parser code. Since we do not assume that every system this complier
38 * is built on will have Flex and Bison installed, we have to store the code
39 * generated by these tools in our version control system. In other parts of
40 * the system we've seen problems where a parser was changed but the generated
41 * code was not committed, merge conflicts where created because two developers
42 * had slightly different versions of Bison installed, etc.
44 * I have also noticed that running Bison generated parsers in GDB is very
45 * irritating. When you get a segfault on '$$ = $1->foo', you can't very
46 * well 'print $1' in GDB.
48 * As a result, my preference is to put as little C code as possible in the
49 * parser (and lexer) sources.
52 #include "glsl_symbol_table.h"
53 #include "glsl_parser_extras.h"
55 #include "glsl_types.h"
56 #include "program/hash_table.h"
58 #include "ir_builder.h"
60 using namespace ir_builder
;
63 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
64 exec_list
*instructions
);
66 remove_per_vertex_blocks(exec_list
*instructions
,
67 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
);
71 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
73 _mesa_glsl_initialize_variables(instructions
, state
);
75 state
->symbols
->separate_function_namespace
= state
->language_version
== 110;
77 state
->current_function
= NULL
;
79 state
->toplevel_ir
= instructions
;
81 state
->gs_input_prim_type_specified
= false;
82 state
->cs_input_local_size_specified
= false;
84 /* Section 4.2 of the GLSL 1.20 specification states:
85 * "The built-in functions are scoped in a scope outside the global scope
86 * users declare global variables in. That is, a shader's global scope,
87 * available for user-defined functions and global variables, is nested
88 * inside the scope containing the built-in functions."
90 * Since built-in functions like ftransform() access built-in variables,
91 * it follows that those must be in the outer scope as well.
93 * We push scope here to create this nesting effect...but don't pop.
94 * This way, a shader's globals are still in the symbol table for use
97 state
->symbols
->push_scope();
99 foreach_list_typed (ast_node
, ast
, link
, & state
->translation_unit
)
100 ast
->hir(instructions
, state
);
102 detect_recursion_unlinked(state
, instructions
);
103 detect_conflicting_assignments(state
, instructions
);
105 state
->toplevel_ir
= NULL
;
107 /* Move all of the variable declarations to the front of the IR list, and
108 * reverse the order. This has the (intended!) side effect that vertex
109 * shader inputs and fragment shader outputs will appear in the IR in the
110 * same order that they appeared in the shader code. This results in the
111 * locations being assigned in the declared order. Many (arguably buggy)
112 * applications depend on this behavior, and it matches what nearly all
115 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
116 ir_variable
*const var
= node
->as_variable();
122 instructions
->push_head(var
);
125 /* Figure out if gl_FragCoord is actually used in fragment shader */
126 ir_variable
*const var
= state
->symbols
->get_variable("gl_FragCoord");
128 state
->fs_uses_gl_fragcoord
= var
->data
.used
;
130 /* From section 7.1 (Built-In Language Variables) of the GLSL 4.10 spec:
132 * If multiple shaders using members of a built-in block belonging to
133 * the same interface are linked together in the same program, they
134 * must all redeclare the built-in block in the same way, as described
135 * in section 4.3.7 "Interface Blocks" for interface block matching, or
136 * a link error will result.
138 * The phrase "using members of a built-in block" implies that if two
139 * shaders are linked together and one of them *does not use* any members
140 * of the built-in block, then that shader does not need to have a matching
141 * redeclaration of the built-in block.
143 * This appears to be a clarification to the behaviour established for
144 * gl_PerVertex by GLSL 1.50, therefore implement it regardless of GLSL
147 * The definition of "interface" in section 4.3.7 that applies here is as
150 * The boundary between adjacent programmable pipeline stages: This
151 * spans all the outputs in all compilation units of the first stage
152 * and all the inputs in all compilation units of the second stage.
154 * Therefore this rule applies to both inter- and intra-stage linking.
156 * The easiest way to implement this is to check whether the shader uses
157 * gl_PerVertex right after ast-to-ir conversion, and if it doesn't, simply
158 * remove all the relevant variable declaration from the IR, so that the
159 * linker won't see them and complain about mismatches.
161 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_in
);
162 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_out
);
166 static ir_expression_operation
167 get_conversion_operation(const glsl_type
*to
, const glsl_type
*from
,
168 struct _mesa_glsl_parse_state
*state
)
170 switch (to
->base_type
) {
171 case GLSL_TYPE_FLOAT
:
172 switch (from
->base_type
) {
173 case GLSL_TYPE_INT
: return ir_unop_i2f
;
174 case GLSL_TYPE_UINT
: return ir_unop_u2f
;
175 case GLSL_TYPE_DOUBLE
: return ir_unop_d2f
;
176 default: return (ir_expression_operation
)0;
180 if (!state
->is_version(400, 0) && !state
->ARB_gpu_shader5_enable
)
181 return (ir_expression_operation
)0;
182 switch (from
->base_type
) {
183 case GLSL_TYPE_INT
: return ir_unop_i2u
;
184 default: return (ir_expression_operation
)0;
187 case GLSL_TYPE_DOUBLE
:
188 if (!state
->has_double())
189 return (ir_expression_operation
)0;
190 switch (from
->base_type
) {
191 case GLSL_TYPE_INT
: return ir_unop_i2d
;
192 case GLSL_TYPE_UINT
: return ir_unop_u2d
;
193 case GLSL_TYPE_FLOAT
: return ir_unop_f2d
;
194 default: return (ir_expression_operation
)0;
197 default: return (ir_expression_operation
)0;
203 * If a conversion is available, convert one operand to a different type
205 * The \c from \c ir_rvalue is converted "in place".
207 * \param to Type that the operand it to be converted to
208 * \param from Operand that is being converted
209 * \param state GLSL compiler state
212 * If a conversion is possible (or unnecessary), \c true is returned.
213 * Otherwise \c false is returned.
216 apply_implicit_conversion(const glsl_type
*to
, ir_rvalue
* &from
,
217 struct _mesa_glsl_parse_state
*state
)
220 if (to
->base_type
== from
->type
->base_type
)
223 /* Prior to GLSL 1.20, there are no implicit conversions */
224 if (!state
->is_version(120, 0))
227 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
229 * "There are no implicit array or structure conversions. For
230 * example, an array of int cannot be implicitly converted to an
233 if (!to
->is_numeric() || !from
->type
->is_numeric())
236 /* We don't actually want the specific type `to`, we want a type
237 * with the same base type as `to`, but the same vector width as
240 to
= glsl_type::get_instance(to
->base_type
, from
->type
->vector_elements
,
241 from
->type
->matrix_columns
);
243 ir_expression_operation op
= get_conversion_operation(to
, from
->type
, state
);
245 from
= new(ctx
) ir_expression(op
, to
, from
, NULL
);
253 static const struct glsl_type
*
254 arithmetic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
256 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
258 const glsl_type
*type_a
= value_a
->type
;
259 const glsl_type
*type_b
= value_b
->type
;
261 /* From GLSL 1.50 spec, page 56:
263 * "The arithmetic binary operators add (+), subtract (-),
264 * multiply (*), and divide (/) operate on integer and
265 * floating-point scalars, vectors, and matrices."
267 if (!type_a
->is_numeric() || !type_b
->is_numeric()) {
268 _mesa_glsl_error(loc
, state
,
269 "operands to arithmetic operators must be numeric");
270 return glsl_type::error_type
;
274 /* "If one operand is floating-point based and the other is
275 * not, then the conversions from Section 4.1.10 "Implicit
276 * Conversions" are applied to the non-floating-point-based operand."
278 if (!apply_implicit_conversion(type_a
, value_b
, state
)
279 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
280 _mesa_glsl_error(loc
, state
,
281 "could not implicitly convert operands to "
282 "arithmetic operator");
283 return glsl_type::error_type
;
285 type_a
= value_a
->type
;
286 type_b
= value_b
->type
;
288 /* "If the operands are integer types, they must both be signed or
291 * From this rule and the preceeding conversion it can be inferred that
292 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
293 * The is_numeric check above already filtered out the case where either
294 * type is not one of these, so now the base types need only be tested for
297 if (type_a
->base_type
!= type_b
->base_type
) {
298 _mesa_glsl_error(loc
, state
,
299 "base type mismatch for arithmetic operator");
300 return glsl_type::error_type
;
303 /* "All arithmetic binary operators result in the same fundamental type
304 * (signed integer, unsigned integer, or floating-point) as the
305 * operands they operate on, after operand type conversion. After
306 * conversion, the following cases are valid
308 * * The two operands are scalars. In this case the operation is
309 * applied, resulting in a scalar."
311 if (type_a
->is_scalar() && type_b
->is_scalar())
314 /* "* One operand is a scalar, and the other is a vector or matrix.
315 * In this case, the scalar operation is applied independently to each
316 * component of the vector or matrix, resulting in the same size
319 if (type_a
->is_scalar()) {
320 if (!type_b
->is_scalar())
322 } else if (type_b
->is_scalar()) {
326 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
327 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
330 assert(!type_a
->is_scalar());
331 assert(!type_b
->is_scalar());
333 /* "* The two operands are vectors of the same size. In this case, the
334 * operation is done component-wise resulting in the same size
337 if (type_a
->is_vector() && type_b
->is_vector()) {
338 if (type_a
== type_b
) {
341 _mesa_glsl_error(loc
, state
,
342 "vector size mismatch for arithmetic operator");
343 return glsl_type::error_type
;
347 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
348 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
349 * <vector, vector> have been handled. At least one of the operands must
350 * be matrix. Further, since there are no integer matrix types, the base
351 * type of both operands must be float.
353 assert(type_a
->is_matrix() || type_b
->is_matrix());
354 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
||
355 type_a
->base_type
== GLSL_TYPE_DOUBLE
);
356 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
||
357 type_b
->base_type
== GLSL_TYPE_DOUBLE
);
359 /* "* The operator is add (+), subtract (-), or divide (/), and the
360 * operands are matrices with the same number of rows and the same
361 * number of columns. In this case, the operation is done component-
362 * wise resulting in the same size matrix."
363 * * The operator is multiply (*), where both operands are matrices or
364 * one operand is a vector and the other a matrix. A right vector
365 * operand is treated as a column vector and a left vector operand as a
366 * row vector. In all these cases, it is required that the number of
367 * columns of the left operand is equal to the number of rows of the
368 * right operand. Then, the multiply (*) operation does a linear
369 * algebraic multiply, yielding an object that has the same number of
370 * rows as the left operand and the same number of columns as the right
371 * operand. Section 5.10 "Vector and Matrix Operations" explains in
372 * more detail how vectors and matrices are operated on."
375 if (type_a
== type_b
)
378 const glsl_type
*type
= glsl_type::get_mul_type(type_a
, type_b
);
380 if (type
== glsl_type::error_type
) {
381 _mesa_glsl_error(loc
, state
,
382 "size mismatch for matrix multiplication");
389 /* "All other cases are illegal."
391 _mesa_glsl_error(loc
, state
, "type mismatch");
392 return glsl_type::error_type
;
396 static const struct glsl_type
*
397 unary_arithmetic_result_type(const struct glsl_type
*type
,
398 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
400 /* From GLSL 1.50 spec, page 57:
402 * "The arithmetic unary operators negate (-), post- and pre-increment
403 * and decrement (-- and ++) operate on integer or floating-point
404 * values (including vectors and matrices). All unary operators work
405 * component-wise on their operands. These result with the same type
408 if (!type
->is_numeric()) {
409 _mesa_glsl_error(loc
, state
,
410 "operands to arithmetic operators must be numeric");
411 return glsl_type::error_type
;
418 * \brief Return the result type of a bit-logic operation.
420 * If the given types to the bit-logic operator are invalid, return
421 * glsl_type::error_type.
423 * \param type_a Type of LHS of bit-logic op
424 * \param type_b Type of RHS of bit-logic op
426 static const struct glsl_type
*
427 bit_logic_result_type(const struct glsl_type
*type_a
,
428 const struct glsl_type
*type_b
,
430 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
432 if (!state
->check_bitwise_operations_allowed(loc
)) {
433 return glsl_type::error_type
;
436 /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
438 * "The bitwise operators and (&), exclusive-or (^), and inclusive-or
439 * (|). The operands must be of type signed or unsigned integers or
442 if (!type_a
->is_integer()) {
443 _mesa_glsl_error(loc
, state
, "LHS of `%s' must be an integer",
444 ast_expression::operator_string(op
));
445 return glsl_type::error_type
;
447 if (!type_b
->is_integer()) {
448 _mesa_glsl_error(loc
, state
, "RHS of `%s' must be an integer",
449 ast_expression::operator_string(op
));
450 return glsl_type::error_type
;
453 /* "The fundamental types of the operands (signed or unsigned) must
456 if (type_a
->base_type
!= type_b
->base_type
) {
457 _mesa_glsl_error(loc
, state
, "operands of `%s' must have the same "
458 "base type", ast_expression::operator_string(op
));
459 return glsl_type::error_type
;
462 /* "The operands cannot be vectors of differing size." */
463 if (type_a
->is_vector() &&
464 type_b
->is_vector() &&
465 type_a
->vector_elements
!= type_b
->vector_elements
) {
466 _mesa_glsl_error(loc
, state
, "operands of `%s' cannot be vectors of "
467 "different sizes", ast_expression::operator_string(op
));
468 return glsl_type::error_type
;
471 /* "If one operand is a scalar and the other a vector, the scalar is
472 * applied component-wise to the vector, resulting in the same type as
473 * the vector. The fundamental types of the operands [...] will be the
474 * resulting fundamental type."
476 if (type_a
->is_scalar())
482 static const struct glsl_type
*
483 modulus_result_type(const struct glsl_type
*type_a
,
484 const struct glsl_type
*type_b
,
485 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
487 if (!state
->check_version(130, 300, loc
, "operator '%%' is reserved")) {
488 return glsl_type::error_type
;
491 /* From GLSL 1.50 spec, page 56:
492 * "The operator modulus (%) operates on signed or unsigned integers or
493 * integer vectors. The operand types must both be signed or both be
496 if (!type_a
->is_integer()) {
497 _mesa_glsl_error(loc
, state
, "LHS of operator %% must be an integer");
498 return glsl_type::error_type
;
500 if (!type_b
->is_integer()) {
501 _mesa_glsl_error(loc
, state
, "RHS of operator %% must be an integer");
502 return glsl_type::error_type
;
504 if (type_a
->base_type
!= type_b
->base_type
) {
505 _mesa_glsl_error(loc
, state
,
506 "operands of %% must have the same base type");
507 return glsl_type::error_type
;
510 /* "The operands cannot be vectors of differing size. If one operand is
511 * a scalar and the other vector, then the scalar is applied component-
512 * wise to the vector, resulting in the same type as the vector. If both
513 * are vectors of the same size, the result is computed component-wise."
515 if (type_a
->is_vector()) {
516 if (!type_b
->is_vector()
517 || (type_a
->vector_elements
== type_b
->vector_elements
))
522 /* "The operator modulus (%) is not defined for any other data types
523 * (non-integer types)."
525 _mesa_glsl_error(loc
, state
, "type mismatch");
526 return glsl_type::error_type
;
530 static const struct glsl_type
*
531 relational_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
532 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
534 const glsl_type
*type_a
= value_a
->type
;
535 const glsl_type
*type_b
= value_b
->type
;
537 /* From GLSL 1.50 spec, page 56:
538 * "The relational operators greater than (>), less than (<), greater
539 * than or equal (>=), and less than or equal (<=) operate only on
540 * scalar integer and scalar floating-point expressions."
542 if (!type_a
->is_numeric()
543 || !type_b
->is_numeric()
544 || !type_a
->is_scalar()
545 || !type_b
->is_scalar()) {
546 _mesa_glsl_error(loc
, state
,
547 "operands to relational operators must be scalar and "
549 return glsl_type::error_type
;
552 /* "Either the operands' types must match, or the conversions from
553 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
554 * operand, after which the types must match."
556 if (!apply_implicit_conversion(type_a
, value_b
, state
)
557 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
558 _mesa_glsl_error(loc
, state
,
559 "could not implicitly convert operands to "
560 "relational operator");
561 return glsl_type::error_type
;
563 type_a
= value_a
->type
;
564 type_b
= value_b
->type
;
566 if (type_a
->base_type
!= type_b
->base_type
) {
567 _mesa_glsl_error(loc
, state
, "base type mismatch");
568 return glsl_type::error_type
;
571 /* "The result is scalar Boolean."
573 return glsl_type::bool_type
;
577 * \brief Return the result type of a bit-shift operation.
579 * If the given types to the bit-shift operator are invalid, return
580 * glsl_type::error_type.
582 * \param type_a Type of LHS of bit-shift op
583 * \param type_b Type of RHS of bit-shift op
585 static const struct glsl_type
*
586 shift_result_type(const struct glsl_type
*type_a
,
587 const struct glsl_type
*type_b
,
589 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
591 if (!state
->check_bitwise_operations_allowed(loc
)) {
592 return glsl_type::error_type
;
595 /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
597 * "The shift operators (<<) and (>>). For both operators, the operands
598 * must be signed or unsigned integers or integer vectors. One operand
599 * can be signed while the other is unsigned."
601 if (!type_a
->is_integer()) {
602 _mesa_glsl_error(loc
, state
, "LHS of operator %s must be an integer or "
603 "integer vector", ast_expression::operator_string(op
));
604 return glsl_type::error_type
;
607 if (!type_b
->is_integer()) {
608 _mesa_glsl_error(loc
, state
, "RHS of operator %s must be an integer or "
609 "integer vector", ast_expression::operator_string(op
));
610 return glsl_type::error_type
;
613 /* "If the first operand is a scalar, the second operand has to be
616 if (type_a
->is_scalar() && !type_b
->is_scalar()) {
617 _mesa_glsl_error(loc
, state
, "if the first operand of %s is scalar, the "
618 "second must be scalar as well",
619 ast_expression::operator_string(op
));
620 return glsl_type::error_type
;
623 /* If both operands are vectors, check that they have same number of
626 if (type_a
->is_vector() &&
627 type_b
->is_vector() &&
628 type_a
->vector_elements
!= type_b
->vector_elements
) {
629 _mesa_glsl_error(loc
, state
, "vector operands to operator %s must "
630 "have same number of elements",
631 ast_expression::operator_string(op
));
632 return glsl_type::error_type
;
635 /* "In all cases, the resulting type will be the same type as the left
642 * Validates that a value can be assigned to a location with a specified type
644 * Validates that \c rhs can be assigned to some location. If the types are
645 * not an exact match but an automatic conversion is possible, \c rhs will be
649 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
650 * Otherwise the actual RHS to be assigned will be returned. This may be
651 * \c rhs, or it may be \c rhs after some type conversion.
654 * In addition to being used for assignments, this function is used to
655 * type-check return values.
658 validate_assignment(struct _mesa_glsl_parse_state
*state
,
659 YYLTYPE loc
, const glsl_type
*lhs_type
,
660 ir_rvalue
*rhs
, bool is_initializer
)
662 /* If there is already some error in the RHS, just return it. Anything
663 * else will lead to an avalanche of error message back to the user.
665 if (rhs
->type
->is_error())
668 /* If the types are identical, the assignment can trivially proceed.
670 if (rhs
->type
== lhs_type
)
673 /* If the array element types are the same and the LHS is unsized,
674 * the assignment is okay for initializers embedded in variable
677 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
678 * is handled by ir_dereference::is_lvalue.
680 if (lhs_type
->is_unsized_array() && rhs
->type
->is_array()
681 && (lhs_type
->element_type() == rhs
->type
->element_type())) {
682 if (is_initializer
) {
685 _mesa_glsl_error(&loc
, state
,
686 "implicitly sized arrays cannot be assigned");
691 /* Check for implicit conversion in GLSL 1.20 */
692 if (apply_implicit_conversion(lhs_type
, rhs
, state
)) {
693 if (rhs
->type
== lhs_type
)
697 _mesa_glsl_error(&loc
, state
,
698 "%s of type %s cannot be assigned to "
699 "variable of type %s",
700 is_initializer
? "initializer" : "value",
701 rhs
->type
->name
, lhs_type
->name
);
707 mark_whole_array_access(ir_rvalue
*access
)
709 ir_dereference_variable
*deref
= access
->as_dereference_variable();
711 if (deref
&& deref
->var
) {
712 deref
->var
->data
.max_array_access
= deref
->type
->length
- 1;
717 do_assignment(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
718 const char *non_lvalue_description
,
719 ir_rvalue
*lhs
, ir_rvalue
*rhs
,
720 ir_rvalue
**out_rvalue
, bool needs_rvalue
,
725 bool error_emitted
= (lhs
->type
->is_error() || rhs
->type
->is_error());
726 ir_rvalue
*extract_channel
= NULL
;
728 /* If the assignment LHS comes back as an ir_binop_vector_extract
729 * expression, move it to the RHS as an ir_triop_vector_insert.
731 if (lhs
->ir_type
== ir_type_expression
) {
732 ir_expression
*const lhs_expr
= lhs
->as_expression();
734 if (unlikely(lhs_expr
->operation
== ir_binop_vector_extract
)) {
736 validate_assignment(state
, lhs_loc
, lhs
->type
,
737 rhs
, is_initializer
);
739 if (new_rhs
== NULL
) {
743 * - LHS: (expression float vector_extract <vec> <channel>)
747 * - RHS: (expression vec2 vector_insert <vec> <channel> <scalar>)
749 * The LHS type is now a vector instead of a scalar. Since GLSL
750 * allows assignments to be used as rvalues, we need to re-extract
751 * the channel from assignment_temp when returning the rvalue.
753 extract_channel
= lhs_expr
->operands
[1];
754 rhs
= new(ctx
) ir_expression(ir_triop_vector_insert
,
755 lhs_expr
->operands
[0]->type
,
756 lhs_expr
->operands
[0],
759 lhs
= lhs_expr
->operands
[0]->clone(ctx
, NULL
);
764 ir_variable
*lhs_var
= lhs
->variable_referenced();
766 lhs_var
->data
.assigned
= true;
768 if (!error_emitted
) {
769 if (non_lvalue_description
!= NULL
) {
770 _mesa_glsl_error(&lhs_loc
, state
,
772 non_lvalue_description
);
773 error_emitted
= true;
774 } else if (lhs_var
!= NULL
&& lhs_var
->data
.read_only
) {
775 _mesa_glsl_error(&lhs_loc
, state
,
776 "assignment to read-only variable '%s'",
778 error_emitted
= true;
779 } else if (lhs
->type
->is_array() &&
780 !state
->check_version(120, 300, &lhs_loc
,
781 "whole array assignment forbidden")) {
782 /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
784 * "Other binary or unary expressions, non-dereferenced
785 * arrays, function names, swizzles with repeated fields,
786 * and constants cannot be l-values."
788 * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00.
790 error_emitted
= true;
791 } else if (!lhs
->is_lvalue()) {
792 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
793 error_emitted
= true;
798 validate_assignment(state
, lhs_loc
, lhs
->type
, rhs
, is_initializer
);
799 if (new_rhs
!= NULL
) {
802 /* If the LHS array was not declared with a size, it takes it size from
803 * the RHS. If the LHS is an l-value and a whole array, it must be a
804 * dereference of a variable. Any other case would require that the LHS
805 * is either not an l-value or not a whole array.
807 if (lhs
->type
->is_unsized_array()) {
808 ir_dereference
*const d
= lhs
->as_dereference();
812 ir_variable
*const var
= d
->variable_referenced();
816 if (var
->data
.max_array_access
>= unsigned(rhs
->type
->array_size())) {
817 /* FINISHME: This should actually log the location of the RHS. */
818 _mesa_glsl_error(& lhs_loc
, state
, "array size must be > %u due to "
820 var
->data
.max_array_access
);
823 var
->type
= glsl_type::get_array_instance(lhs
->type
->element_type(),
824 rhs
->type
->array_size());
827 if (lhs
->type
->is_array()) {
828 mark_whole_array_access(rhs
);
829 mark_whole_array_access(lhs
);
833 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
834 * but not post_inc) need the converted assigned value as an rvalue
835 * to handle things like:
840 ir_variable
*var
= new(ctx
) ir_variable(rhs
->type
, "assignment_tmp",
842 instructions
->push_tail(var
);
843 instructions
->push_tail(assign(var
, rhs
));
845 if (!error_emitted
) {
846 ir_dereference_variable
*deref_var
= new(ctx
) ir_dereference_variable(var
);
847 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, deref_var
));
849 ir_rvalue
*rvalue
= new(ctx
) ir_dereference_variable(var
);
851 if (extract_channel
) {
852 rvalue
= new(ctx
) ir_expression(ir_binop_vector_extract
,
854 extract_channel
->clone(ctx
, NULL
));
857 *out_rvalue
= rvalue
;
860 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, rhs
));
864 return error_emitted
;
868 get_lvalue_copy(exec_list
*instructions
, ir_rvalue
*lvalue
)
870 void *ctx
= ralloc_parent(lvalue
);
873 var
= new(ctx
) ir_variable(lvalue
->type
, "_post_incdec_tmp",
875 instructions
->push_tail(var
);
877 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
880 return new(ctx
) ir_dereference_variable(var
);
885 ast_node::hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
894 ast_function_expression::hir_no_rvalue(exec_list
*instructions
,
895 struct _mesa_glsl_parse_state
*state
)
897 (void)hir(instructions
, state
);
901 ast_aggregate_initializer::hir_no_rvalue(exec_list
*instructions
,
902 struct _mesa_glsl_parse_state
*state
)
904 (void)hir(instructions
, state
);
908 do_comparison(void *mem_ctx
, int operation
, ir_rvalue
*op0
, ir_rvalue
*op1
)
911 ir_rvalue
*cmp
= NULL
;
913 if (operation
== ir_binop_all_equal
)
914 join_op
= ir_binop_logic_and
;
916 join_op
= ir_binop_logic_or
;
918 switch (op0
->type
->base_type
) {
919 case GLSL_TYPE_FLOAT
:
923 case GLSL_TYPE_DOUBLE
:
924 return new(mem_ctx
) ir_expression(operation
, op0
, op1
);
926 case GLSL_TYPE_ARRAY
: {
927 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
928 ir_rvalue
*e0
, *e1
, *result
;
930 e0
= new(mem_ctx
) ir_dereference_array(op0
->clone(mem_ctx
, NULL
),
931 new(mem_ctx
) ir_constant(i
));
932 e1
= new(mem_ctx
) ir_dereference_array(op1
->clone(mem_ctx
, NULL
),
933 new(mem_ctx
) ir_constant(i
));
934 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
937 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
943 mark_whole_array_access(op0
);
944 mark_whole_array_access(op1
);
948 case GLSL_TYPE_STRUCT
: {
949 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
950 ir_rvalue
*e0
, *e1
, *result
;
951 const char *field_name
= op0
->type
->fields
.structure
[i
].name
;
953 e0
= new(mem_ctx
) ir_dereference_record(op0
->clone(mem_ctx
, NULL
),
955 e1
= new(mem_ctx
) ir_dereference_record(op1
->clone(mem_ctx
, NULL
),
957 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
960 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
968 case GLSL_TYPE_ERROR
:
970 case GLSL_TYPE_SAMPLER
:
971 case GLSL_TYPE_IMAGE
:
972 case GLSL_TYPE_INTERFACE
:
973 case GLSL_TYPE_ATOMIC_UINT
:
974 /* I assume a comparison of a struct containing a sampler just
975 * ignores the sampler present in the type.
981 cmp
= new(mem_ctx
) ir_constant(true);
986 /* For logical operations, we want to ensure that the operands are
987 * scalar booleans. If it isn't, emit an error and return a constant
988 * boolean to avoid triggering cascading error messages.
991 get_scalar_boolean_operand(exec_list
*instructions
,
992 struct _mesa_glsl_parse_state
*state
,
993 ast_expression
*parent_expr
,
995 const char *operand_name
,
998 ast_expression
*expr
= parent_expr
->subexpressions
[operand
];
1000 ir_rvalue
*val
= expr
->hir(instructions
, state
);
1002 if (val
->type
->is_boolean() && val
->type
->is_scalar())
1005 if (!*error_emitted
) {
1006 YYLTYPE loc
= expr
->get_location();
1007 _mesa_glsl_error(&loc
, state
, "%s of `%s' must be scalar boolean",
1009 parent_expr
->operator_string(parent_expr
->oper
));
1010 *error_emitted
= true;
1013 return new(ctx
) ir_constant(true);
1017 * If name refers to a builtin array whose maximum allowed size is less than
1018 * size, report an error and return true. Otherwise return false.
1021 check_builtin_array_max_size(const char *name
, unsigned size
,
1022 YYLTYPE loc
, struct _mesa_glsl_parse_state
*state
)
1024 if ((strcmp("gl_TexCoord", name
) == 0)
1025 && (size
> state
->Const
.MaxTextureCoords
)) {
1026 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1028 * "The size [of gl_TexCoord] can be at most
1029 * gl_MaxTextureCoords."
1031 _mesa_glsl_error(&loc
, state
, "`gl_TexCoord' array size cannot "
1032 "be larger than gl_MaxTextureCoords (%u)",
1033 state
->Const
.MaxTextureCoords
);
1034 } else if (strcmp("gl_ClipDistance", name
) == 0
1035 && size
> state
->Const
.MaxClipPlanes
) {
1036 /* From section 7.1 (Vertex Shader Special Variables) of the
1039 * "The gl_ClipDistance array is predeclared as unsized and
1040 * must be sized by the shader either redeclaring it with a
1041 * size or indexing it only with integral constant
1042 * expressions. ... The size can be at most
1043 * gl_MaxClipDistances."
1045 _mesa_glsl_error(&loc
, state
, "`gl_ClipDistance' array size cannot "
1046 "be larger than gl_MaxClipDistances (%u)",
1047 state
->Const
.MaxClipPlanes
);
1052 * Create the constant 1, of a which is appropriate for incrementing and
1053 * decrementing values of the given GLSL type. For example, if type is vec4,
1054 * this creates a constant value of 1.0 having type float.
1056 * If the given type is invalid for increment and decrement operators, return
1057 * a floating point 1--the error will be detected later.
1060 constant_one_for_inc_dec(void *ctx
, const glsl_type
*type
)
1062 switch (type
->base_type
) {
1063 case GLSL_TYPE_UINT
:
1064 return new(ctx
) ir_constant((unsigned) 1);
1066 return new(ctx
) ir_constant(1);
1068 case GLSL_TYPE_FLOAT
:
1069 return new(ctx
) ir_constant(1.0f
);
1074 ast_expression::hir(exec_list
*instructions
,
1075 struct _mesa_glsl_parse_state
*state
)
1077 return do_hir(instructions
, state
, true);
1081 ast_expression::hir_no_rvalue(exec_list
*instructions
,
1082 struct _mesa_glsl_parse_state
*state
)
1084 do_hir(instructions
, state
, false);
1088 ast_expression::do_hir(exec_list
*instructions
,
1089 struct _mesa_glsl_parse_state
*state
,
1093 static const int operations
[AST_NUM_OPERATORS
] = {
1094 -1, /* ast_assign doesn't convert to ir_expression. */
1095 -1, /* ast_plus doesn't convert to ir_expression. */
1109 ir_binop_any_nequal
,
1119 /* Note: The following block of expression types actually convert
1120 * to multiple IR instructions.
1122 ir_binop_mul
, /* ast_mul_assign */
1123 ir_binop_div
, /* ast_div_assign */
1124 ir_binop_mod
, /* ast_mod_assign */
1125 ir_binop_add
, /* ast_add_assign */
1126 ir_binop_sub
, /* ast_sub_assign */
1127 ir_binop_lshift
, /* ast_ls_assign */
1128 ir_binop_rshift
, /* ast_rs_assign */
1129 ir_binop_bit_and
, /* ast_and_assign */
1130 ir_binop_bit_xor
, /* ast_xor_assign */
1131 ir_binop_bit_or
, /* ast_or_assign */
1133 -1, /* ast_conditional doesn't convert to ir_expression. */
1134 ir_binop_add
, /* ast_pre_inc. */
1135 ir_binop_sub
, /* ast_pre_dec. */
1136 ir_binop_add
, /* ast_post_inc. */
1137 ir_binop_sub
, /* ast_post_dec. */
1138 -1, /* ast_field_selection doesn't conv to ir_expression. */
1139 -1, /* ast_array_index doesn't convert to ir_expression. */
1140 -1, /* ast_function_call doesn't conv to ir_expression. */
1141 -1, /* ast_identifier doesn't convert to ir_expression. */
1142 -1, /* ast_int_constant doesn't convert to ir_expression. */
1143 -1, /* ast_uint_constant doesn't conv to ir_expression. */
1144 -1, /* ast_float_constant doesn't conv to ir_expression. */
1145 -1, /* ast_bool_constant doesn't conv to ir_expression. */
1146 -1, /* ast_sequence doesn't convert to ir_expression. */
1148 ir_rvalue
*result
= NULL
;
1150 const struct glsl_type
*type
; /* a temporary variable for switch cases */
1151 bool error_emitted
= false;
1154 loc
= this->get_location();
1156 switch (this->oper
) {
1158 assert(!"ast_aggregate: Should never get here.");
1162 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1163 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1166 do_assignment(instructions
, state
,
1167 this->subexpressions
[0]->non_lvalue_description
,
1168 op
[0], op
[1], &result
, needs_rvalue
, false,
1169 this->subexpressions
[0]->get_location());
1174 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1176 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1178 error_emitted
= type
->is_error();
1184 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1186 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1188 error_emitted
= type
->is_error();
1190 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1198 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1199 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1201 type
= arithmetic_result_type(op
[0], op
[1],
1202 (this->oper
== ast_mul
),
1204 error_emitted
= type
->is_error();
1206 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1211 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1212 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1214 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1216 assert(operations
[this->oper
] == ir_binop_mod
);
1218 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1220 error_emitted
= type
->is_error();
1225 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1226 error_emitted
= true;
1229 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1230 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1231 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1233 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1235 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1242 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1243 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1245 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
1247 /* The relational operators must either generate an error or result
1248 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1250 assert(type
->is_error()
1251 || ((type
->base_type
== GLSL_TYPE_BOOL
)
1252 && type
->is_scalar()));
1254 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1256 error_emitted
= type
->is_error();
1261 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1262 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1264 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1266 * "The equality operators equal (==), and not equal (!=)
1267 * operate on all types. They result in a scalar Boolean. If
1268 * the operand types do not match, then there must be a
1269 * conversion from Section 4.1.10 "Implicit Conversions"
1270 * applied to one operand that can make them match, in which
1271 * case this conversion is done."
1273 if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
1274 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
1275 || (op
[0]->type
!= op
[1]->type
)) {
1276 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
1277 "type", (this->oper
== ast_equal
) ? "==" : "!=");
1278 error_emitted
= true;
1279 } else if ((op
[0]->type
->is_array() || op
[1]->type
->is_array()) &&
1280 !state
->check_version(120, 300, &loc
,
1281 "array comparisons forbidden")) {
1282 error_emitted
= true;
1283 } else if ((op
[0]->type
->contains_opaque() ||
1284 op
[1]->type
->contains_opaque())) {
1285 _mesa_glsl_error(&loc
, state
, "opaque type comparisons forbidden");
1286 error_emitted
= true;
1289 if (error_emitted
) {
1290 result
= new(ctx
) ir_constant(false);
1292 result
= do_comparison(ctx
, operations
[this->oper
], op
[0], op
[1]);
1293 assert(result
->type
== glsl_type::bool_type
);
1300 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1301 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1302 type
= bit_logic_result_type(op
[0]->type
, op
[1]->type
, this->oper
,
1304 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1306 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1310 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1312 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1313 error_emitted
= true;
1316 if (!op
[0]->type
->is_integer()) {
1317 _mesa_glsl_error(&loc
, state
, "operand of `~' must be an integer");
1318 error_emitted
= true;
1321 type
= error_emitted
? glsl_type::error_type
: op
[0]->type
;
1322 result
= new(ctx
) ir_expression(ir_unop_bit_not
, type
, op
[0], NULL
);
1325 case ast_logic_and
: {
1326 exec_list rhs_instructions
;
1327 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1328 "LHS", &error_emitted
);
1329 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1330 "RHS", &error_emitted
);
1332 if (rhs_instructions
.is_empty()) {
1333 result
= new(ctx
) ir_expression(ir_binop_logic_and
, op
[0], op
[1]);
1334 type
= result
->type
;
1336 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1339 instructions
->push_tail(tmp
);
1341 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1342 instructions
->push_tail(stmt
);
1344 stmt
->then_instructions
.append_list(&rhs_instructions
);
1345 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1346 ir_assignment
*const then_assign
=
1347 new(ctx
) ir_assignment(then_deref
, op
[1]);
1348 stmt
->then_instructions
.push_tail(then_assign
);
1350 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1351 ir_assignment
*const else_assign
=
1352 new(ctx
) ir_assignment(else_deref
, new(ctx
) ir_constant(false));
1353 stmt
->else_instructions
.push_tail(else_assign
);
1355 result
= new(ctx
) ir_dereference_variable(tmp
);
1361 case ast_logic_or
: {
1362 exec_list rhs_instructions
;
1363 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1364 "LHS", &error_emitted
);
1365 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1366 "RHS", &error_emitted
);
1368 if (rhs_instructions
.is_empty()) {
1369 result
= new(ctx
) ir_expression(ir_binop_logic_or
, op
[0], op
[1]);
1370 type
= result
->type
;
1372 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1375 instructions
->push_tail(tmp
);
1377 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1378 instructions
->push_tail(stmt
);
1380 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1381 ir_assignment
*const then_assign
=
1382 new(ctx
) ir_assignment(then_deref
, new(ctx
) ir_constant(true));
1383 stmt
->then_instructions
.push_tail(then_assign
);
1385 stmt
->else_instructions
.append_list(&rhs_instructions
);
1386 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1387 ir_assignment
*const else_assign
=
1388 new(ctx
) ir_assignment(else_deref
, op
[1]);
1389 stmt
->else_instructions
.push_tail(else_assign
);
1391 result
= new(ctx
) ir_dereference_variable(tmp
);
1398 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1400 * "The logical binary operators and (&&), or ( | | ), and
1401 * exclusive or (^^). They operate only on two Boolean
1402 * expressions and result in a Boolean expression."
1404 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0, "LHS",
1406 op
[1] = get_scalar_boolean_operand(instructions
, state
, this, 1, "RHS",
1409 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1414 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1415 "operand", &error_emitted
);
1417 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1421 case ast_mul_assign
:
1422 case ast_div_assign
:
1423 case ast_add_assign
:
1424 case ast_sub_assign
: {
1425 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1426 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1428 type
= arithmetic_result_type(op
[0], op
[1],
1429 (this->oper
== ast_mul_assign
),
1432 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1436 do_assignment(instructions
, state
,
1437 this->subexpressions
[0]->non_lvalue_description
,
1438 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1439 &result
, needs_rvalue
, false,
1440 this->subexpressions
[0]->get_location());
1442 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1443 * explicitly test for this because none of the binary expression
1444 * operators allow array operands either.
1450 case ast_mod_assign
: {
1451 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1452 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1454 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1456 assert(operations
[this->oper
] == ir_binop_mod
);
1458 ir_rvalue
*temp_rhs
;
1459 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1463 do_assignment(instructions
, state
,
1464 this->subexpressions
[0]->non_lvalue_description
,
1465 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1466 &result
, needs_rvalue
, false,
1467 this->subexpressions
[0]->get_location());
1472 case ast_rs_assign
: {
1473 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1474 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1475 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1477 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1478 type
, op
[0], op
[1]);
1480 do_assignment(instructions
, state
,
1481 this->subexpressions
[0]->non_lvalue_description
,
1482 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1483 &result
, needs_rvalue
, false,
1484 this->subexpressions
[0]->get_location());
1488 case ast_and_assign
:
1489 case ast_xor_assign
:
1490 case ast_or_assign
: {
1491 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1492 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1493 type
= bit_logic_result_type(op
[0]->type
, op
[1]->type
, this->oper
,
1495 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1496 type
, op
[0], op
[1]);
1498 do_assignment(instructions
, state
,
1499 this->subexpressions
[0]->non_lvalue_description
,
1500 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1501 &result
, needs_rvalue
, false,
1502 this->subexpressions
[0]->get_location());
1506 case ast_conditional
: {
1507 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1509 * "The ternary selection operator (?:). It operates on three
1510 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1511 * first expression, which must result in a scalar Boolean."
1513 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1514 "condition", &error_emitted
);
1516 /* The :? operator is implemented by generating an anonymous temporary
1517 * followed by an if-statement. The last instruction in each branch of
1518 * the if-statement assigns a value to the anonymous temporary. This
1519 * temporary is the r-value of the expression.
1521 exec_list then_instructions
;
1522 exec_list else_instructions
;
1524 op
[1] = this->subexpressions
[1]->hir(&then_instructions
, state
);
1525 op
[2] = this->subexpressions
[2]->hir(&else_instructions
, state
);
1527 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1529 * "The second and third expressions can be any type, as
1530 * long their types match, or there is a conversion in
1531 * Section 4.1.10 "Implicit Conversions" that can be applied
1532 * to one of the expressions to make their types match. This
1533 * resulting matching type is the type of the entire
1536 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1537 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1538 || (op
[1]->type
!= op
[2]->type
)) {
1539 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1541 _mesa_glsl_error(& loc
, state
, "second and third operands of ?: "
1542 "operator must have matching types");
1543 error_emitted
= true;
1544 type
= glsl_type::error_type
;
1549 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1551 * "The second and third expressions must be the same type, but can
1552 * be of any type other than an array."
1554 if (type
->is_array() &&
1555 !state
->check_version(120, 300, &loc
,
1556 "second and third operands of ?: operator "
1557 "cannot be arrays")) {
1558 error_emitted
= true;
1561 /* From section 4.1.7 of the GLSL 4.50 spec (Opaque Types):
1563 * "Except for array indexing, structure member selection, and
1564 * parentheses, opaque variables are not allowed to be operands in
1565 * expressions; such use results in a compile-time error."
1567 if (type
->contains_opaque()) {
1568 _mesa_glsl_error(&loc
, state
, "opaque variables cannot be operands "
1569 "of the ?: operator");
1570 error_emitted
= true;
1573 ir_constant
*cond_val
= op
[0]->constant_expression_value();
1575 if (then_instructions
.is_empty()
1576 && else_instructions
.is_empty()
1577 && cond_val
!= NULL
) {
1578 result
= cond_val
->value
.b
[0] ? op
[1] : op
[2];
1580 /* The copy to conditional_tmp reads the whole array. */
1581 if (type
->is_array()) {
1582 mark_whole_array_access(op
[1]);
1583 mark_whole_array_access(op
[2]);
1586 ir_variable
*const tmp
=
1587 new(ctx
) ir_variable(type
, "conditional_tmp", ir_var_temporary
);
1588 instructions
->push_tail(tmp
);
1590 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1591 instructions
->push_tail(stmt
);
1593 then_instructions
.move_nodes_to(& stmt
->then_instructions
);
1594 ir_dereference
*const then_deref
=
1595 new(ctx
) ir_dereference_variable(tmp
);
1596 ir_assignment
*const then_assign
=
1597 new(ctx
) ir_assignment(then_deref
, op
[1]);
1598 stmt
->then_instructions
.push_tail(then_assign
);
1600 else_instructions
.move_nodes_to(& stmt
->else_instructions
);
1601 ir_dereference
*const else_deref
=
1602 new(ctx
) ir_dereference_variable(tmp
);
1603 ir_assignment
*const else_assign
=
1604 new(ctx
) ir_assignment(else_deref
, op
[2]);
1605 stmt
->else_instructions
.push_tail(else_assign
);
1607 result
= new(ctx
) ir_dereference_variable(tmp
);
1614 this->non_lvalue_description
= (this->oper
== ast_pre_inc
)
1615 ? "pre-increment operation" : "pre-decrement operation";
1617 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1618 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1620 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1622 ir_rvalue
*temp_rhs
;
1623 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1627 do_assignment(instructions
, state
,
1628 this->subexpressions
[0]->non_lvalue_description
,
1629 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1630 &result
, needs_rvalue
, false,
1631 this->subexpressions
[0]->get_location());
1636 case ast_post_dec
: {
1637 this->non_lvalue_description
= (this->oper
== ast_post_inc
)
1638 ? "post-increment operation" : "post-decrement operation";
1639 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1640 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1642 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1644 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1646 ir_rvalue
*temp_rhs
;
1647 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1650 /* Get a temporary of a copy of the lvalue before it's modified.
1651 * This may get thrown away later.
1653 result
= get_lvalue_copy(instructions
, op
[0]->clone(ctx
, NULL
));
1655 ir_rvalue
*junk_rvalue
;
1657 do_assignment(instructions
, state
,
1658 this->subexpressions
[0]->non_lvalue_description
,
1659 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1660 &junk_rvalue
, false, false,
1661 this->subexpressions
[0]->get_location());
1666 case ast_field_selection
:
1667 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1670 case ast_array_index
: {
1671 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1673 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1674 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1676 result
= _mesa_ast_array_index_to_hir(ctx
, state
, op
[0], op
[1],
1679 if (result
->type
->is_error())
1680 error_emitted
= true;
1685 case ast_function_call
:
1686 /* Should *NEVER* get here. ast_function_call should always be handled
1687 * by ast_function_expression::hir.
1692 case ast_identifier
: {
1693 /* ast_identifier can appear several places in a full abstract syntax
1694 * tree. This particular use must be at location specified in the grammar
1695 * as 'variable_identifier'.
1698 state
->symbols
->get_variable(this->primary_expression
.identifier
);
1701 var
->data
.used
= true;
1702 result
= new(ctx
) ir_dereference_variable(var
);
1704 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
1705 this->primary_expression
.identifier
);
1707 result
= ir_rvalue::error_value(ctx
);
1708 error_emitted
= true;
1713 case ast_int_constant
:
1714 result
= new(ctx
) ir_constant(this->primary_expression
.int_constant
);
1717 case ast_uint_constant
:
1718 result
= new(ctx
) ir_constant(this->primary_expression
.uint_constant
);
1721 case ast_float_constant
:
1722 result
= new(ctx
) ir_constant(this->primary_expression
.float_constant
);
1725 case ast_bool_constant
:
1726 result
= new(ctx
) ir_constant(bool(this->primary_expression
.bool_constant
));
1729 case ast_double_constant
:
1730 result
= new(ctx
) ir_constant(this->primary_expression
.double_constant
);
1733 case ast_sequence
: {
1734 /* It should not be possible to generate a sequence in the AST without
1735 * any expressions in it.
1737 assert(!this->expressions
.is_empty());
1739 /* The r-value of a sequence is the last expression in the sequence. If
1740 * the other expressions in the sequence do not have side-effects (and
1741 * therefore add instructions to the instruction list), they get dropped
1744 exec_node
*previous_tail_pred
= NULL
;
1745 YYLTYPE previous_operand_loc
= loc
;
1747 foreach_list_typed (ast_node
, ast
, link
, &this->expressions
) {
1748 /* If one of the operands of comma operator does not generate any
1749 * code, we want to emit a warning. At each pass through the loop
1750 * previous_tail_pred will point to the last instruction in the
1751 * stream *before* processing the previous operand. Naturally,
1752 * instructions->tail_pred will point to the last instruction in the
1753 * stream *after* processing the previous operand. If the two
1754 * pointers match, then the previous operand had no effect.
1756 * The warning behavior here differs slightly from GCC. GCC will
1757 * only emit a warning if none of the left-hand operands have an
1758 * effect. However, it will emit a warning for each. I believe that
1759 * there are some cases in C (especially with GCC extensions) where
1760 * it is useful to have an intermediate step in a sequence have no
1761 * effect, but I don't think these cases exist in GLSL. Either way,
1762 * it would be a giant hassle to replicate that behavior.
1764 if (previous_tail_pred
== instructions
->tail_pred
) {
1765 _mesa_glsl_warning(&previous_operand_loc
, state
,
1766 "left-hand operand of comma expression has "
1770 /* tail_pred is directly accessed instead of using the get_tail()
1771 * method for performance reasons. get_tail() has extra code to
1772 * return NULL when the list is empty. We don't care about that
1773 * here, so using tail_pred directly is fine.
1775 previous_tail_pred
= instructions
->tail_pred
;
1776 previous_operand_loc
= ast
->get_location();
1778 result
= ast
->hir(instructions
, state
);
1781 /* Any errors should have already been emitted in the loop above.
1783 error_emitted
= true;
1787 type
= NULL
; /* use result->type, not type. */
1788 assert(result
!= NULL
|| !needs_rvalue
);
1790 if (result
&& result
->type
->is_error() && !error_emitted
)
1791 _mesa_glsl_error(& loc
, state
, "type mismatch");
1798 ast_expression_statement::hir(exec_list
*instructions
,
1799 struct _mesa_glsl_parse_state
*state
)
1801 /* It is possible to have expression statements that don't have an
1802 * expression. This is the solitary semicolon:
1804 * for (i = 0; i < 5; i++)
1807 * In this case the expression will be NULL. Test for NULL and don't do
1808 * anything in that case.
1810 if (expression
!= NULL
)
1811 expression
->hir_no_rvalue(instructions
, state
);
1813 /* Statements do not have r-values.
1820 ast_compound_statement::hir(exec_list
*instructions
,
1821 struct _mesa_glsl_parse_state
*state
)
1824 state
->symbols
->push_scope();
1826 foreach_list_typed (ast_node
, ast
, link
, &this->statements
)
1827 ast
->hir(instructions
, state
);
1830 state
->symbols
->pop_scope();
1832 /* Compound statements do not have r-values.
1838 * Evaluate the given exec_node (which should be an ast_node representing
1839 * a single array dimension) and return its integer value.
1842 process_array_size(exec_node
*node
,
1843 struct _mesa_glsl_parse_state
*state
)
1845 exec_list dummy_instructions
;
1847 ast_node
*array_size
= exec_node_data(ast_node
, node
, link
);
1848 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
1849 YYLTYPE loc
= array_size
->get_location();
1852 _mesa_glsl_error(& loc
, state
,
1853 "array size could not be resolved");
1857 if (!ir
->type
->is_integer()) {
1858 _mesa_glsl_error(& loc
, state
,
1859 "array size must be integer type");
1863 if (!ir
->type
->is_scalar()) {
1864 _mesa_glsl_error(& loc
, state
,
1865 "array size must be scalar type");
1869 ir_constant
*const size
= ir
->constant_expression_value();
1871 _mesa_glsl_error(& loc
, state
, "array size must be a "
1872 "constant valued expression");
1876 if (size
->value
.i
[0] <= 0) {
1877 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
1881 assert(size
->type
== ir
->type
);
1883 /* If the array size is const (and we've verified that
1884 * it is) then no instructions should have been emitted
1885 * when we converted it to HIR. If they were emitted,
1886 * then either the array size isn't const after all, or
1887 * we are emitting unnecessary instructions.
1889 assert(dummy_instructions
.is_empty());
1891 return size
->value
.u
[0];
1894 static const glsl_type
*
1895 process_array_type(YYLTYPE
*loc
, const glsl_type
*base
,
1896 ast_array_specifier
*array_specifier
,
1897 struct _mesa_glsl_parse_state
*state
)
1899 const glsl_type
*array_type
= base
;
1901 if (array_specifier
!= NULL
) {
1902 if (base
->is_array()) {
1904 /* From page 19 (page 25) of the GLSL 1.20 spec:
1906 * "Only one-dimensional arrays may be declared."
1908 if (!state
->ARB_arrays_of_arrays_enable
) {
1909 _mesa_glsl_error(loc
, state
,
1910 "invalid array of `%s'"
1911 "GL_ARB_arrays_of_arrays "
1912 "required for defining arrays of arrays",
1914 return glsl_type::error_type
;
1917 if (base
->length
== 0) {
1918 _mesa_glsl_error(loc
, state
,
1919 "only the outermost array dimension can "
1922 return glsl_type::error_type
;
1926 for (exec_node
*node
= array_specifier
->array_dimensions
.tail_pred
;
1927 !node
->is_head_sentinel(); node
= node
->prev
) {
1928 unsigned array_size
= process_array_size(node
, state
);
1929 array_type
= glsl_type::get_array_instance(array_type
, array_size
);
1932 if (array_specifier
->is_unsized_array
)
1933 array_type
= glsl_type::get_array_instance(array_type
, 0);
1941 ast_type_specifier::glsl_type(const char **name
,
1942 struct _mesa_glsl_parse_state
*state
) const
1944 const struct glsl_type
*type
;
1946 type
= state
->symbols
->get_type(this->type_name
);
1947 *name
= this->type_name
;
1949 YYLTYPE loc
= this->get_location();
1950 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
1956 ast_fully_specified_type::glsl_type(const char **name
,
1957 struct _mesa_glsl_parse_state
*state
) const
1959 const struct glsl_type
*type
= this->specifier
->glsl_type(name
, state
);
1964 if (type
->base_type
== GLSL_TYPE_FLOAT
1966 && state
->stage
== MESA_SHADER_FRAGMENT
1967 && this->qualifier
.precision
== ast_precision_none
1968 && state
->symbols
->get_variable("#default precision") == NULL
) {
1969 YYLTYPE loc
= this->get_location();
1970 _mesa_glsl_error(&loc
, state
,
1971 "no precision specified this scope for type `%s'",
1979 * Determine whether a toplevel variable declaration declares a varying. This
1980 * function operates by examining the variable's mode and the shader target,
1981 * so it correctly identifies linkage variables regardless of whether they are
1982 * declared using the deprecated "varying" syntax or the new "in/out" syntax.
1984 * Passing a non-toplevel variable declaration (e.g. a function parameter) to
1985 * this function will produce undefined results.
1988 is_varying_var(ir_variable
*var
, gl_shader_stage target
)
1991 case MESA_SHADER_VERTEX
:
1992 return var
->data
.mode
== ir_var_shader_out
;
1993 case MESA_SHADER_FRAGMENT
:
1994 return var
->data
.mode
== ir_var_shader_in
;
1996 return var
->data
.mode
== ir_var_shader_out
|| var
->data
.mode
== ir_var_shader_in
;
2002 * Matrix layout qualifiers are only allowed on certain types
2005 validate_matrix_layout_for_type(struct _mesa_glsl_parse_state
*state
,
2007 const glsl_type
*type
,
2010 if (var
&& !var
->is_in_uniform_block()) {
2011 /* Layout qualifiers may only apply to interface blocks and fields in
2014 _mesa_glsl_error(loc
, state
,
2015 "uniform block layout qualifiers row_major and "
2016 "column_major may not be applied to variables "
2017 "outside of uniform blocks");
2018 } else if (!type
->is_matrix()) {
2019 /* The OpenGL ES 3.0 conformance tests did not originally allow
2020 * matrix layout qualifiers on non-matrices. However, the OpenGL
2021 * 4.4 and OpenGL ES 3.0 (revision TBD) specifications were
2022 * amended to specifically allow these layouts on all types. Emit
2023 * a warning so that people know their code may not be portable.
2025 _mesa_glsl_warning(loc
, state
,
2026 "uniform block layout qualifiers row_major and "
2027 "column_major applied to non-matrix types may "
2028 "be rejected by older compilers");
2029 } else if (type
->is_record()) {
2030 /* We allow 'layout(row_major)' on structure types because it's the only
2031 * way to get row-major layouts on matrices contained in structures.
2033 _mesa_glsl_warning(loc
, state
,
2034 "uniform block layout qualifiers row_major and "
2035 "column_major applied to structure types is not "
2036 "strictly conformant and may be rejected by other "
2042 validate_binding_qualifier(struct _mesa_glsl_parse_state
*state
,
2045 const ast_type_qualifier
*qual
)
2047 if (var
->data
.mode
!= ir_var_uniform
) {
2048 _mesa_glsl_error(loc
, state
,
2049 "the \"binding\" qualifier only applies to uniforms");
2053 if (qual
->binding
< 0) {
2054 _mesa_glsl_error(loc
, state
, "binding values must be >= 0");
2058 const struct gl_context
*const ctx
= state
->ctx
;
2059 unsigned elements
= var
->type
->is_array() ? var
->type
->length
: 1;
2060 unsigned max_index
= qual
->binding
+ elements
- 1;
2062 if (var
->type
->is_interface()) {
2063 /* UBOs. From page 60 of the GLSL 4.20 specification:
2064 * "If the binding point for any uniform block instance is less than zero,
2065 * or greater than or equal to the implementation-dependent maximum
2066 * number of uniform buffer bindings, a compilation error will occur.
2067 * When the binding identifier is used with a uniform block instanced as
2068 * an array of size N, all elements of the array from binding through
2069 * binding + N – 1 must be within this range."
2071 * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
2073 if (max_index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2074 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d UBOs exceeds "
2075 "the maximum number of UBO binding points (%d)",
2076 qual
->binding
, elements
,
2077 ctx
->Const
.MaxUniformBufferBindings
);
2080 } else if (var
->type
->is_sampler() ||
2081 (var
->type
->is_array() && var
->type
->fields
.array
->is_sampler())) {
2082 /* Samplers. From page 63 of the GLSL 4.20 specification:
2083 * "If the binding is less than zero, or greater than or equal to the
2084 * implementation-dependent maximum supported number of units, a
2085 * compilation error will occur. When the binding identifier is used
2086 * with an array of size N, all elements of the array from binding
2087 * through binding + N - 1 must be within this range."
2089 unsigned limit
= ctx
->Const
.Program
[state
->stage
].MaxTextureImageUnits
;
2091 if (max_index
>= limit
) {
2092 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d samplers "
2093 "exceeds the maximum number of texture image units "
2094 "(%d)", qual
->binding
, elements
, limit
);
2098 } else if (var
->type
->contains_atomic()) {
2099 assert(ctx
->Const
.MaxAtomicBufferBindings
<= MAX_COMBINED_ATOMIC_BUFFERS
);
2100 if (unsigned(qual
->binding
) >= ctx
->Const
.MaxAtomicBufferBindings
) {
2101 _mesa_glsl_error(loc
, state
, "layout(binding = %d) exceeds the "
2102 " maximum number of atomic counter buffer bindings"
2103 "(%d)", qual
->binding
,
2104 ctx
->Const
.MaxAtomicBufferBindings
);
2109 _mesa_glsl_error(loc
, state
,
2110 "the \"binding\" qualifier only applies to uniform "
2111 "blocks, samplers, atomic counters, or arrays thereof");
2119 static glsl_interp_qualifier
2120 interpret_interpolation_qualifier(const struct ast_type_qualifier
*qual
,
2121 ir_variable_mode mode
,
2122 struct _mesa_glsl_parse_state
*state
,
2125 glsl_interp_qualifier interpolation
;
2126 if (qual
->flags
.q
.flat
)
2127 interpolation
= INTERP_QUALIFIER_FLAT
;
2128 else if (qual
->flags
.q
.noperspective
)
2129 interpolation
= INTERP_QUALIFIER_NOPERSPECTIVE
;
2130 else if (qual
->flags
.q
.smooth
)
2131 interpolation
= INTERP_QUALIFIER_SMOOTH
;
2133 interpolation
= INTERP_QUALIFIER_NONE
;
2135 if (interpolation
!= INTERP_QUALIFIER_NONE
) {
2136 if (mode
!= ir_var_shader_in
&& mode
!= ir_var_shader_out
) {
2137 _mesa_glsl_error(loc
, state
,
2138 "interpolation qualifier `%s' can only be applied to "
2139 "shader inputs or outputs.",
2140 interpolation_string(interpolation
));
2144 if ((state
->stage
== MESA_SHADER_VERTEX
&& mode
== ir_var_shader_in
) ||
2145 (state
->stage
== MESA_SHADER_FRAGMENT
&& mode
== ir_var_shader_out
)) {
2146 _mesa_glsl_error(loc
, state
,
2147 "interpolation qualifier `%s' cannot be applied to "
2148 "vertex shader inputs or fragment shader outputs",
2149 interpolation_string(interpolation
));
2153 return interpolation
;
2158 validate_explicit_location(const struct ast_type_qualifier
*qual
,
2160 struct _mesa_glsl_parse_state
*state
,
2165 /* Checks for GL_ARB_explicit_uniform_location. */
2166 if (qual
->flags
.q
.uniform
) {
2167 if (!state
->check_explicit_uniform_location_allowed(loc
, var
))
2170 const struct gl_context
*const ctx
= state
->ctx
;
2171 unsigned max_loc
= qual
->location
+ var
->type
->uniform_locations() - 1;
2173 /* ARB_explicit_uniform_location specification states:
2175 * "The explicitly defined locations and the generated locations
2176 * must be in the range of 0 to MAX_UNIFORM_LOCATIONS minus one."
2178 * "Valid locations for default-block uniform variable locations
2179 * are in the range of 0 to the implementation-defined maximum
2180 * number of uniform locations."
2182 if (qual
->location
< 0) {
2183 _mesa_glsl_error(loc
, state
,
2184 "explicit location < 0 for uniform %s", var
->name
);
2188 if (max_loc
>= ctx
->Const
.MaxUserAssignableUniformLocations
) {
2189 _mesa_glsl_error(loc
, state
, "location(s) consumed by uniform %s "
2190 ">= MAX_UNIFORM_LOCATIONS (%u)", var
->name
,
2191 ctx
->Const
.MaxUserAssignableUniformLocations
);
2195 var
->data
.explicit_location
= true;
2196 var
->data
.location
= qual
->location
;
2200 /* Between GL_ARB_explicit_attrib_location an
2201 * GL_ARB_separate_shader_objects, the inputs and outputs of any shader
2202 * stage can be assigned explicit locations. The checking here associates
2203 * the correct extension with the correct stage's input / output:
2207 * vertex explicit_loc sso
2209 * fragment sso explicit_loc
2211 switch (state
->stage
) {
2212 case MESA_SHADER_VERTEX
:
2213 if (var
->data
.mode
== ir_var_shader_in
) {
2214 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
2220 if (var
->data
.mode
== ir_var_shader_out
) {
2221 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
2230 case MESA_SHADER_GEOMETRY
:
2231 if (var
->data
.mode
== ir_var_shader_in
|| var
->data
.mode
== ir_var_shader_out
) {
2232 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
2241 case MESA_SHADER_FRAGMENT
:
2242 if (var
->data
.mode
== ir_var_shader_in
) {
2243 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
2249 if (var
->data
.mode
== ir_var_shader_out
) {
2250 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
2259 case MESA_SHADER_COMPUTE
:
2260 _mesa_glsl_error(loc
, state
,
2261 "compute shader variables cannot be given "
2262 "explicit locations");
2267 _mesa_glsl_error(loc
, state
,
2268 "%s cannot be given an explicit location in %s shader",
2270 _mesa_shader_stage_to_string(state
->stage
));
2272 var
->data
.explicit_location
= true;
2274 /* This bit of silliness is needed because invalid explicit locations
2275 * are supposed to be flagged during linking. Small negative values
2276 * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
2277 * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
2278 * The linker needs to be able to differentiate these cases. This
2279 * ensures that negative values stay negative.
2281 if (qual
->location
>= 0) {
2282 switch (state
->stage
) {
2283 case MESA_SHADER_VERTEX
:
2284 var
->data
.location
= (var
->data
.mode
== ir_var_shader_in
)
2285 ? (qual
->location
+ VERT_ATTRIB_GENERIC0
)
2286 : (qual
->location
+ VARYING_SLOT_VAR0
);
2289 case MESA_SHADER_GEOMETRY
:
2290 var
->data
.location
= qual
->location
+ VARYING_SLOT_VAR0
;
2293 case MESA_SHADER_FRAGMENT
:
2294 var
->data
.location
= (var
->data
.mode
== ir_var_shader_out
)
2295 ? (qual
->location
+ FRAG_RESULT_DATA0
)
2296 : (qual
->location
+ VARYING_SLOT_VAR0
);
2298 case MESA_SHADER_COMPUTE
:
2299 assert(!"Unexpected shader type");
2303 var
->data
.location
= qual
->location
;
2306 if (qual
->flags
.q
.explicit_index
) {
2307 /* From the GLSL 4.30 specification, section 4.4.2 (Output
2308 * Layout Qualifiers):
2310 * "It is also a compile-time error if a fragment shader
2311 * sets a layout index to less than 0 or greater than 1."
2313 * Older specifications don't mandate a behavior; we take
2314 * this as a clarification and always generate the error.
2316 if (qual
->index
< 0 || qual
->index
> 1) {
2317 _mesa_glsl_error(loc
, state
,
2318 "explicit index may only be 0 or 1");
2320 var
->data
.explicit_index
= true;
2321 var
->data
.index
= qual
->index
;
2328 apply_image_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
2330 struct _mesa_glsl_parse_state
*state
,
2333 const glsl_type
*base_type
=
2334 (var
->type
->is_array() ? var
->type
->element_type() : var
->type
);
2336 if (base_type
->is_image()) {
2337 if (var
->data
.mode
!= ir_var_uniform
&&
2338 var
->data
.mode
!= ir_var_function_in
) {
2339 _mesa_glsl_error(loc
, state
, "image variables may only be declared as "
2340 "function parameters or uniform-qualified "
2341 "global variables");
2344 var
->data
.image_read_only
|= qual
->flags
.q
.read_only
;
2345 var
->data
.image_write_only
|= qual
->flags
.q
.write_only
;
2346 var
->data
.image_coherent
|= qual
->flags
.q
.coherent
;
2347 var
->data
.image_volatile
|= qual
->flags
.q
._volatile
;
2348 var
->data
.image_restrict
|= qual
->flags
.q
.restrict_flag
;
2349 var
->data
.read_only
= true;
2351 if (qual
->flags
.q
.explicit_image_format
) {
2352 if (var
->data
.mode
== ir_var_function_in
) {
2353 _mesa_glsl_error(loc
, state
, "format qualifiers cannot be "
2354 "used on image function parameters");
2357 if (qual
->image_base_type
!= base_type
->sampler_type
) {
2358 _mesa_glsl_error(loc
, state
, "format qualifier doesn't match the "
2359 "base data type of the image");
2362 var
->data
.image_format
= qual
->image_format
;
2364 if (var
->data
.mode
== ir_var_uniform
&& !qual
->flags
.q
.write_only
) {
2365 _mesa_glsl_error(loc
, state
, "uniforms not qualified with "
2366 "`writeonly' must have a format layout "
2370 var
->data
.image_format
= GL_NONE
;
2375 static inline const char*
2376 get_layout_qualifier_string(bool origin_upper_left
, bool pixel_center_integer
)
2378 if (origin_upper_left
&& pixel_center_integer
)
2379 return "origin_upper_left, pixel_center_integer";
2380 else if (origin_upper_left
)
2381 return "origin_upper_left";
2382 else if (pixel_center_integer
)
2383 return "pixel_center_integer";
2389 is_conflicting_fragcoord_redeclaration(struct _mesa_glsl_parse_state
*state
,
2390 const struct ast_type_qualifier
*qual
)
2392 /* If gl_FragCoord was previously declared, and the qualifiers were
2393 * different in any way, return true.
2395 if (state
->fs_redeclares_gl_fragcoord
) {
2396 return (state
->fs_pixel_center_integer
!= qual
->flags
.q
.pixel_center_integer
2397 || state
->fs_origin_upper_left
!= qual
->flags
.q
.origin_upper_left
);
2404 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
2406 struct _mesa_glsl_parse_state
*state
,
2410 STATIC_ASSERT(sizeof(qual
->flags
.q
) <= sizeof(qual
->flags
.i
));
2412 if (qual
->flags
.q
.invariant
) {
2413 if (var
->data
.used
) {
2414 _mesa_glsl_error(loc
, state
,
2415 "variable `%s' may not be redeclared "
2416 "`invariant' after being used",
2419 var
->data
.invariant
= 1;
2423 if (qual
->flags
.q
.precise
) {
2424 if (var
->data
.used
) {
2425 _mesa_glsl_error(loc
, state
,
2426 "variable `%s' may not be redeclared "
2427 "`precise' after being used",
2430 var
->data
.precise
= 1;
2434 if (qual
->flags
.q
.constant
|| qual
->flags
.q
.attribute
2435 || qual
->flags
.q
.uniform
2436 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
2437 var
->data
.read_only
= 1;
2439 if (qual
->flags
.q
.centroid
)
2440 var
->data
.centroid
= 1;
2442 if (qual
->flags
.q
.sample
)
2443 var
->data
.sample
= 1;
2445 if (state
->stage
== MESA_SHADER_GEOMETRY
&&
2446 qual
->flags
.q
.out
&& qual
->flags
.q
.stream
) {
2447 var
->data
.stream
= qual
->stream
;
2450 if (qual
->flags
.q
.attribute
&& state
->stage
!= MESA_SHADER_VERTEX
) {
2451 var
->type
= glsl_type::error_type
;
2452 _mesa_glsl_error(loc
, state
,
2453 "`attribute' variables may not be declared in the "
2455 _mesa_shader_stage_to_string(state
->stage
));
2458 /* Disallow layout qualifiers which may only appear on layout declarations. */
2459 if (qual
->flags
.q
.prim_type
) {
2460 _mesa_glsl_error(loc
, state
,
2461 "Primitive type may only be specified on GS input or output "
2462 "layout declaration, not on variables.");
2465 /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
2467 * "However, the const qualifier cannot be used with out or inout."
2469 * The same section of the GLSL 4.40 spec further clarifies this saying:
2471 * "The const qualifier cannot be used with out or inout, or a
2472 * compile-time error results."
2474 if (is_parameter
&& qual
->flags
.q
.constant
&& qual
->flags
.q
.out
) {
2475 _mesa_glsl_error(loc
, state
,
2476 "`const' may not be applied to `out' or `inout' "
2477 "function parameters");
2480 /* If there is no qualifier that changes the mode of the variable, leave
2481 * the setting alone.
2483 assert(var
->data
.mode
!= ir_var_temporary
);
2484 if (qual
->flags
.q
.in
&& qual
->flags
.q
.out
)
2485 var
->data
.mode
= ir_var_function_inout
;
2486 else if (qual
->flags
.q
.in
)
2487 var
->data
.mode
= is_parameter
? ir_var_function_in
: ir_var_shader_in
;
2488 else if (qual
->flags
.q
.attribute
2489 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
2490 var
->data
.mode
= ir_var_shader_in
;
2491 else if (qual
->flags
.q
.out
)
2492 var
->data
.mode
= is_parameter
? ir_var_function_out
: ir_var_shader_out
;
2493 else if (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_VERTEX
))
2494 var
->data
.mode
= ir_var_shader_out
;
2495 else if (qual
->flags
.q
.uniform
)
2496 var
->data
.mode
= ir_var_uniform
;
2498 if (!is_parameter
&& is_varying_var(var
, state
->stage
)) {
2499 /* User-defined ins/outs are not permitted in compute shaders. */
2500 if (state
->stage
== MESA_SHADER_COMPUTE
) {
2501 _mesa_glsl_error(loc
, state
,
2502 "user-defined input and output variables are not "
2503 "permitted in compute shaders");
2506 /* This variable is being used to link data between shader stages (in
2507 * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
2508 * that is allowed for such purposes.
2510 * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
2512 * "The varying qualifier can be used only with the data types
2513 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
2516 * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
2517 * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
2519 * "Fragment inputs can only be signed and unsigned integers and
2520 * integer vectors, float, floating-point vectors, matrices, or
2521 * arrays of these. Structures cannot be input.
2523 * Similar text exists in the section on vertex shader outputs.
2525 * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
2526 * 3.00 spec allows structs as well. Varying structs are also allowed
2529 switch (var
->type
->get_scalar_type()->base_type
) {
2530 case GLSL_TYPE_FLOAT
:
2531 /* Ok in all GLSL versions */
2533 case GLSL_TYPE_UINT
:
2535 if (state
->is_version(130, 300))
2537 _mesa_glsl_error(loc
, state
,
2538 "varying variables must be of base type float in %s",
2539 state
->get_version_string());
2541 case GLSL_TYPE_STRUCT
:
2542 if (state
->is_version(150, 300))
2544 _mesa_glsl_error(loc
, state
,
2545 "varying variables may not be of type struct");
2547 case GLSL_TYPE_DOUBLE
:
2550 _mesa_glsl_error(loc
, state
, "illegal type for a varying variable");
2555 if (state
->all_invariant
&& (state
->current_function
== NULL
)) {
2556 switch (state
->stage
) {
2557 case MESA_SHADER_VERTEX
:
2558 if (var
->data
.mode
== ir_var_shader_out
)
2559 var
->data
.invariant
= true;
2561 case MESA_SHADER_GEOMETRY
:
2562 if ((var
->data
.mode
== ir_var_shader_in
)
2563 || (var
->data
.mode
== ir_var_shader_out
))
2564 var
->data
.invariant
= true;
2566 case MESA_SHADER_FRAGMENT
:
2567 if (var
->data
.mode
== ir_var_shader_in
)
2568 var
->data
.invariant
= true;
2570 case MESA_SHADER_COMPUTE
:
2571 /* Invariance isn't meaningful in compute shaders. */
2576 var
->data
.interpolation
=
2577 interpret_interpolation_qualifier(qual
, (ir_variable_mode
) var
->data
.mode
,
2580 var
->data
.pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
2581 var
->data
.origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
2582 if ((qual
->flags
.q
.origin_upper_left
|| qual
->flags
.q
.pixel_center_integer
)
2583 && (strcmp(var
->name
, "gl_FragCoord") != 0)) {
2584 const char *const qual_string
= (qual
->flags
.q
.origin_upper_left
)
2585 ? "origin_upper_left" : "pixel_center_integer";
2587 _mesa_glsl_error(loc
, state
,
2588 "layout qualifier `%s' can only be applied to "
2589 "fragment shader input `gl_FragCoord'",
2593 if (var
->name
!= NULL
&& strcmp(var
->name
, "gl_FragCoord") == 0) {
2595 /* Section 4.3.8.1, page 39 of GLSL 1.50 spec says:
2597 * "Within any shader, the first redeclarations of gl_FragCoord
2598 * must appear before any use of gl_FragCoord."
2600 * Generate a compiler error if above condition is not met by the
2603 ir_variable
*earlier
= state
->symbols
->get_variable("gl_FragCoord");
2604 if (earlier
!= NULL
&&
2605 earlier
->data
.used
&&
2606 !state
->fs_redeclares_gl_fragcoord
) {
2607 _mesa_glsl_error(loc
, state
,
2608 "gl_FragCoord used before its first redeclaration "
2609 "in fragment shader");
2612 /* Make sure all gl_FragCoord redeclarations specify the same layout
2615 if (is_conflicting_fragcoord_redeclaration(state
, qual
)) {
2616 const char *const qual_string
=
2617 get_layout_qualifier_string(qual
->flags
.q
.origin_upper_left
,
2618 qual
->flags
.q
.pixel_center_integer
);
2620 const char *const state_string
=
2621 get_layout_qualifier_string(state
->fs_origin_upper_left
,
2622 state
->fs_pixel_center_integer
);
2624 _mesa_glsl_error(loc
, state
,
2625 "gl_FragCoord redeclared with different layout "
2626 "qualifiers (%s) and (%s) ",
2630 state
->fs_origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
2631 state
->fs_pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
2632 state
->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers
=
2633 !qual
->flags
.q
.origin_upper_left
&& !qual
->flags
.q
.pixel_center_integer
;
2634 state
->fs_redeclares_gl_fragcoord
=
2635 state
->fs_origin_upper_left
||
2636 state
->fs_pixel_center_integer
||
2637 state
->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers
;
2640 if (qual
->flags
.q
.explicit_location
) {
2641 validate_explicit_location(qual
, var
, state
, loc
);
2642 } else if (qual
->flags
.q
.explicit_index
) {
2643 _mesa_glsl_error(loc
, state
, "explicit index requires explicit location");
2646 if (qual
->flags
.q
.explicit_binding
&&
2647 validate_binding_qualifier(state
, loc
, var
, qual
)) {
2648 var
->data
.explicit_binding
= true;
2649 var
->data
.binding
= qual
->binding
;
2652 if (var
->type
->contains_atomic()) {
2653 if (var
->data
.mode
== ir_var_uniform
) {
2654 if (var
->data
.explicit_binding
) {
2656 &state
->atomic_counter_offsets
[var
->data
.binding
];
2658 if (*offset
% ATOMIC_COUNTER_SIZE
)
2659 _mesa_glsl_error(loc
, state
,
2660 "misaligned atomic counter offset");
2662 var
->data
.atomic
.offset
= *offset
;
2663 *offset
+= var
->type
->atomic_size();
2666 _mesa_glsl_error(loc
, state
,
2667 "atomic counters require explicit binding point");
2669 } else if (var
->data
.mode
!= ir_var_function_in
) {
2670 _mesa_glsl_error(loc
, state
, "atomic counters may only be declared as "
2671 "function parameters or uniform-qualified "
2672 "global variables");
2676 /* Does the declaration use the deprecated 'attribute' or 'varying'
2679 const bool uses_deprecated_qualifier
= qual
->flags
.q
.attribute
2680 || qual
->flags
.q
.varying
;
2683 /* Validate auxiliary storage qualifiers */
2685 /* From section 4.3.4 of the GLSL 1.30 spec:
2686 * "It is an error to use centroid in in a vertex shader."
2688 * From section 4.3.4 of the GLSL ES 3.00 spec:
2689 * "It is an error to use centroid in or interpolation qualifiers in
2690 * a vertex shader input."
2693 /* Section 4.3.6 of the GLSL 1.30 specification states:
2694 * "It is an error to use centroid out in a fragment shader."
2696 * The GL_ARB_shading_language_420pack extension specification states:
2697 * "It is an error to use auxiliary storage qualifiers or interpolation
2698 * qualifiers on an output in a fragment shader."
2700 if (qual
->flags
.q
.sample
&& (!is_varying_var(var
, state
->stage
) || uses_deprecated_qualifier
)) {
2701 _mesa_glsl_error(loc
, state
,
2702 "sample qualifier may only be used on `in` or `out` "
2703 "variables between shader stages");
2705 if (qual
->flags
.q
.centroid
&& !is_varying_var(var
, state
->stage
)) {
2706 _mesa_glsl_error(loc
, state
,
2707 "centroid qualifier may only be used with `in', "
2708 "`out' or `varying' variables between shader stages");
2712 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
2713 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
2714 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
2715 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
2716 * These extensions and all following extensions that add the 'layout'
2717 * keyword have been modified to require the use of 'in' or 'out'.
2719 * The following extension do not allow the deprecated keywords:
2721 * GL_AMD_conservative_depth
2722 * GL_ARB_conservative_depth
2723 * GL_ARB_gpu_shader5
2724 * GL_ARB_separate_shader_objects
2725 * GL_ARB_tesselation_shader
2726 * GL_ARB_transform_feedback3
2727 * GL_ARB_uniform_buffer_object
2729 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
2730 * allow layout with the deprecated keywords.
2732 const bool relaxed_layout_qualifier_checking
=
2733 state
->ARB_fragment_coord_conventions_enable
;
2735 if (qual
->has_layout() && uses_deprecated_qualifier
) {
2736 if (relaxed_layout_qualifier_checking
) {
2737 _mesa_glsl_warning(loc
, state
,
2738 "`layout' qualifier may not be used with "
2739 "`attribute' or `varying'");
2741 _mesa_glsl_error(loc
, state
,
2742 "`layout' qualifier may not be used with "
2743 "`attribute' or `varying'");
2747 /* Layout qualifiers for gl_FragDepth, which are enabled by extension
2748 * AMD_conservative_depth.
2750 int depth_layout_count
= qual
->flags
.q
.depth_any
2751 + qual
->flags
.q
.depth_greater
2752 + qual
->flags
.q
.depth_less
2753 + qual
->flags
.q
.depth_unchanged
;
2754 if (depth_layout_count
> 0
2755 && !state
->AMD_conservative_depth_enable
2756 && !state
->ARB_conservative_depth_enable
) {
2757 _mesa_glsl_error(loc
, state
,
2758 "extension GL_AMD_conservative_depth or "
2759 "GL_ARB_conservative_depth must be enabled "
2760 "to use depth layout qualifiers");
2761 } else if (depth_layout_count
> 0
2762 && strcmp(var
->name
, "gl_FragDepth") != 0) {
2763 _mesa_glsl_error(loc
, state
,
2764 "depth layout qualifiers can be applied only to "
2766 } else if (depth_layout_count
> 1
2767 && strcmp(var
->name
, "gl_FragDepth") == 0) {
2768 _mesa_glsl_error(loc
, state
,
2769 "at most one depth layout qualifier can be applied to "
2772 if (qual
->flags
.q
.depth_any
)
2773 var
->data
.depth_layout
= ir_depth_layout_any
;
2774 else if (qual
->flags
.q
.depth_greater
)
2775 var
->data
.depth_layout
= ir_depth_layout_greater
;
2776 else if (qual
->flags
.q
.depth_less
)
2777 var
->data
.depth_layout
= ir_depth_layout_less
;
2778 else if (qual
->flags
.q
.depth_unchanged
)
2779 var
->data
.depth_layout
= ir_depth_layout_unchanged
;
2781 var
->data
.depth_layout
= ir_depth_layout_none
;
2783 if (qual
->flags
.q
.std140
||
2784 qual
->flags
.q
.packed
||
2785 qual
->flags
.q
.shared
) {
2786 _mesa_glsl_error(loc
, state
,
2787 "uniform block layout qualifiers std140, packed, and "
2788 "shared can only be applied to uniform blocks, not "
2792 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
2793 validate_matrix_layout_for_type(state
, loc
, var
->type
, var
);
2796 if (var
->type
->contains_image())
2797 apply_image_qualifier_to_variable(qual
, var
, state
, loc
);
2801 * Get the variable that is being redeclared by this declaration
2803 * Semantic checks to verify the validity of the redeclaration are also
2804 * performed. If semantic checks fail, compilation error will be emitted via
2805 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
2808 * A pointer to an existing variable in the current scope if the declaration
2809 * is a redeclaration, \c NULL otherwise.
2811 static ir_variable
*
2812 get_variable_being_redeclared(ir_variable
*var
, YYLTYPE loc
,
2813 struct _mesa_glsl_parse_state
*state
,
2814 bool allow_all_redeclarations
)
2816 /* Check if this declaration is actually a re-declaration, either to
2817 * resize an array or add qualifiers to an existing variable.
2819 * This is allowed for variables in the current scope, or when at
2820 * global scope (for built-ins in the implicit outer scope).
2822 ir_variable
*earlier
= state
->symbols
->get_variable(var
->name
);
2823 if (earlier
== NULL
||
2824 (state
->current_function
!= NULL
&&
2825 !state
->symbols
->name_declared_this_scope(var
->name
))) {
2830 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
2832 * "It is legal to declare an array without a size and then
2833 * later re-declare the same name as an array of the same
2834 * type and specify a size."
2836 if (earlier
->type
->is_unsized_array() && var
->type
->is_array()
2837 && (var
->type
->element_type() == earlier
->type
->element_type())) {
2838 /* FINISHME: This doesn't match the qualifiers on the two
2839 * FINISHME: declarations. It's not 100% clear whether this is
2840 * FINISHME: required or not.
2843 const unsigned size
= unsigned(var
->type
->array_size());
2844 check_builtin_array_max_size(var
->name
, size
, loc
, state
);
2845 if ((size
> 0) && (size
<= earlier
->data
.max_array_access
)) {
2846 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
2848 earlier
->data
.max_array_access
);
2851 earlier
->type
= var
->type
;
2854 } else if ((state
->ARB_fragment_coord_conventions_enable
||
2855 state
->is_version(150, 0))
2856 && strcmp(var
->name
, "gl_FragCoord") == 0
2857 && earlier
->type
== var
->type
2858 && earlier
->data
.mode
== var
->data
.mode
) {
2859 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
2862 earlier
->data
.origin_upper_left
= var
->data
.origin_upper_left
;
2863 earlier
->data
.pixel_center_integer
= var
->data
.pixel_center_integer
;
2865 /* According to section 4.3.7 of the GLSL 1.30 spec,
2866 * the following built-in varaibles can be redeclared with an
2867 * interpolation qualifier:
2870 * * gl_FrontSecondaryColor
2871 * * gl_BackSecondaryColor
2873 * * gl_SecondaryColor
2875 } else if (state
->is_version(130, 0)
2876 && (strcmp(var
->name
, "gl_FrontColor") == 0
2877 || strcmp(var
->name
, "gl_BackColor") == 0
2878 || strcmp(var
->name
, "gl_FrontSecondaryColor") == 0
2879 || strcmp(var
->name
, "gl_BackSecondaryColor") == 0
2880 || strcmp(var
->name
, "gl_Color") == 0
2881 || strcmp(var
->name
, "gl_SecondaryColor") == 0)
2882 && earlier
->type
== var
->type
2883 && earlier
->data
.mode
== var
->data
.mode
) {
2884 earlier
->data
.interpolation
= var
->data
.interpolation
;
2886 /* Layout qualifiers for gl_FragDepth. */
2887 } else if ((state
->AMD_conservative_depth_enable
||
2888 state
->ARB_conservative_depth_enable
)
2889 && strcmp(var
->name
, "gl_FragDepth") == 0
2890 && earlier
->type
== var
->type
2891 && earlier
->data
.mode
== var
->data
.mode
) {
2893 /** From the AMD_conservative_depth spec:
2894 * Within any shader, the first redeclarations of gl_FragDepth
2895 * must appear before any use of gl_FragDepth.
2897 if (earlier
->data
.used
) {
2898 _mesa_glsl_error(&loc
, state
,
2899 "the first redeclaration of gl_FragDepth "
2900 "must appear before any use of gl_FragDepth");
2903 /* Prevent inconsistent redeclaration of depth layout qualifier. */
2904 if (earlier
->data
.depth_layout
!= ir_depth_layout_none
2905 && earlier
->data
.depth_layout
!= var
->data
.depth_layout
) {
2906 _mesa_glsl_error(&loc
, state
,
2907 "gl_FragDepth: depth layout is declared here "
2908 "as '%s, but it was previously declared as "
2910 depth_layout_string(var
->data
.depth_layout
),
2911 depth_layout_string(earlier
->data
.depth_layout
));
2914 earlier
->data
.depth_layout
= var
->data
.depth_layout
;
2916 } else if (allow_all_redeclarations
) {
2917 if (earlier
->data
.mode
!= var
->data
.mode
) {
2918 _mesa_glsl_error(&loc
, state
,
2919 "redeclaration of `%s' with incorrect qualifiers",
2921 } else if (earlier
->type
!= var
->type
) {
2922 _mesa_glsl_error(&loc
, state
,
2923 "redeclaration of `%s' has incorrect type",
2927 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
2934 * Generate the IR for an initializer in a variable declaration
2937 process_initializer(ir_variable
*var
, ast_declaration
*decl
,
2938 ast_fully_specified_type
*type
,
2939 exec_list
*initializer_instructions
,
2940 struct _mesa_glsl_parse_state
*state
)
2942 ir_rvalue
*result
= NULL
;
2944 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
2946 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
2948 * "All uniform variables are read-only and are initialized either
2949 * directly by an application via API commands, or indirectly by
2952 if (var
->data
.mode
== ir_var_uniform
) {
2953 state
->check_version(120, 0, &initializer_loc
,
2954 "cannot initialize uniforms");
2957 /* From section 4.1.7 of the GLSL 4.40 spec:
2959 * "Opaque variables [...] are initialized only through the
2960 * OpenGL API; they cannot be declared with an initializer in a
2963 if (var
->type
->contains_opaque()) {
2964 _mesa_glsl_error(& initializer_loc
, state
,
2965 "cannot initialize opaque variable");
2968 if ((var
->data
.mode
== ir_var_shader_in
) && (state
->current_function
== NULL
)) {
2969 _mesa_glsl_error(& initializer_loc
, state
,
2970 "cannot initialize %s shader input / %s",
2971 _mesa_shader_stage_to_string(state
->stage
),
2972 (state
->stage
== MESA_SHADER_VERTEX
)
2973 ? "attribute" : "varying");
2976 /* If the initializer is an ast_aggregate_initializer, recursively store
2977 * type information from the LHS into it, so that its hir() function can do
2980 if (decl
->initializer
->oper
== ast_aggregate
)
2981 _mesa_ast_set_aggregate_type(var
->type
, decl
->initializer
);
2983 ir_dereference
*const lhs
= new(state
) ir_dereference_variable(var
);
2984 ir_rvalue
*rhs
= decl
->initializer
->hir(initializer_instructions
, state
);
2986 /* Calculate the constant value if this is a const or uniform
2989 if (type
->qualifier
.flags
.q
.constant
2990 || type
->qualifier
.flags
.q
.uniform
) {
2991 ir_rvalue
*new_rhs
= validate_assignment(state
, initializer_loc
,
2992 var
->type
, rhs
, true);
2993 if (new_rhs
!= NULL
) {
2996 ir_constant
*constant_value
= rhs
->constant_expression_value();
2997 if (!constant_value
) {
2998 /* If ARB_shading_language_420pack is enabled, initializers of
2999 * const-qualified local variables do not have to be constant
3000 * expressions. Const-qualified global variables must still be
3001 * initialized with constant expressions.
3003 if (!state
->ARB_shading_language_420pack_enable
3004 || state
->current_function
== NULL
) {
3005 _mesa_glsl_error(& initializer_loc
, state
,
3006 "initializer of %s variable `%s' must be a "
3007 "constant expression",
3008 (type
->qualifier
.flags
.q
.constant
)
3009 ? "const" : "uniform",
3011 if (var
->type
->is_numeric()) {
3012 /* Reduce cascading errors. */
3013 var
->constant_value
= ir_constant::zero(state
, var
->type
);
3017 rhs
= constant_value
;
3018 var
->constant_value
= constant_value
;
3021 if (var
->type
->is_numeric()) {
3022 /* Reduce cascading errors. */
3023 var
->constant_value
= ir_constant::zero(state
, var
->type
);
3028 if (rhs
&& !rhs
->type
->is_error()) {
3029 bool temp
= var
->data
.read_only
;
3030 if (type
->qualifier
.flags
.q
.constant
)
3031 var
->data
.read_only
= false;
3033 /* Never emit code to initialize a uniform.
3035 const glsl_type
*initializer_type
;
3036 if (!type
->qualifier
.flags
.q
.uniform
) {
3037 do_assignment(initializer_instructions
, state
,
3042 type
->get_location());
3043 initializer_type
= result
->type
;
3045 initializer_type
= rhs
->type
;
3047 var
->constant_initializer
= rhs
->constant_expression_value();
3048 var
->data
.has_initializer
= true;
3050 /* If the declared variable is an unsized array, it must inherrit
3051 * its full type from the initializer. A declaration such as
3053 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
3057 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
3059 * The assignment generated in the if-statement (below) will also
3060 * automatically handle this case for non-uniforms.
3062 * If the declared variable is not an array, the types must
3063 * already match exactly. As a result, the type assignment
3064 * here can be done unconditionally. For non-uniforms the call
3065 * to do_assignment can change the type of the initializer (via
3066 * the implicit conversion rules). For uniforms the initializer
3067 * must be a constant expression, and the type of that expression
3068 * was validated above.
3070 var
->type
= initializer_type
;
3072 var
->data
.read_only
= temp
;
3080 * Do additional processing necessary for geometry shader input declarations
3081 * (this covers both interface blocks arrays and bare input variables).
3084 handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
3085 YYLTYPE loc
, ir_variable
*var
)
3087 unsigned num_vertices
= 0;
3088 if (state
->gs_input_prim_type_specified
) {
3089 num_vertices
= vertices_per_prim(state
->in_qualifier
->prim_type
);
3092 /* Geometry shader input variables must be arrays. Caller should have
3093 * reported an error for this.
3095 if (!var
->type
->is_array()) {
3096 assert(state
->error
);
3098 /* To avoid cascading failures, short circuit the checks below. */
3102 if (var
->type
->is_unsized_array()) {
3103 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says:
3105 * All geometry shader input unsized array declarations will be
3106 * sized by an earlier input layout qualifier, when present, as per
3107 * the following table.
3109 * Followed by a table mapping each allowed input layout qualifier to
3110 * the corresponding input length.
3112 if (num_vertices
!= 0)
3113 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
3116 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec
3117 * includes the following examples of compile-time errors:
3119 * // code sequence within one shader...
3120 * in vec4 Color1[]; // size unknown
3121 * ...Color1.length()...// illegal, length() unknown
3122 * in vec4 Color2[2]; // size is 2
3123 * ...Color1.length()...// illegal, Color1 still has no size
3124 * in vec4 Color3[3]; // illegal, input sizes are inconsistent
3125 * layout(lines) in; // legal, input size is 2, matching
3126 * in vec4 Color4[3]; // illegal, contradicts layout
3129 * To detect the case illustrated by Color3, we verify that the size of
3130 * an explicitly-sized array matches the size of any previously declared
3131 * explicitly-sized array. To detect the case illustrated by Color4, we
3132 * verify that the size of an explicitly-sized array is consistent with
3133 * any previously declared input layout.
3135 if (num_vertices
!= 0 && var
->type
->length
!= num_vertices
) {
3136 _mesa_glsl_error(&loc
, state
,
3137 "geometry shader input size contradicts previously"
3138 " declared layout (size is %u, but layout requires a"
3139 " size of %u)", var
->type
->length
, num_vertices
);
3140 } else if (state
->gs_input_size
!= 0 &&
3141 var
->type
->length
!= state
->gs_input_size
) {
3142 _mesa_glsl_error(&loc
, state
,
3143 "geometry shader input sizes are "
3144 "inconsistent (size is %u, but a previous "
3145 "declaration has size %u)",
3146 var
->type
->length
, state
->gs_input_size
);
3148 state
->gs_input_size
= var
->type
->length
;
3155 validate_identifier(const char *identifier
, YYLTYPE loc
,
3156 struct _mesa_glsl_parse_state
*state
)
3158 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
3160 * "Identifiers starting with "gl_" are reserved for use by
3161 * OpenGL, and may not be declared in a shader as either a
3162 * variable or a function."
3164 if (is_gl_identifier(identifier
)) {
3165 _mesa_glsl_error(&loc
, state
,
3166 "identifier `%s' uses reserved `gl_' prefix",
3168 } else if (strstr(identifier
, "__")) {
3169 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
3172 * "In addition, all identifiers containing two
3173 * consecutive underscores (__) are reserved as
3174 * possible future keywords."
3176 * The intention is that names containing __ are reserved for internal
3177 * use by the implementation, and names prefixed with GL_ are reserved
3178 * for use by Khronos. Names simply containing __ are dangerous to use,
3179 * but should be allowed.
3181 * A future version of the GLSL specification will clarify this.
3183 _mesa_glsl_warning(&loc
, state
,
3184 "identifier `%s' uses reserved `__' string",
3190 precision_qualifier_allowed(const glsl_type
*type
)
3192 /* Precision qualifiers apply to floating point, integer and sampler
3195 * Section 4.5.2 (Precision Qualifiers) of the GLSL 1.30 spec says:
3196 * "Any floating point or any integer declaration can have the type
3197 * preceded by one of these precision qualifiers [...] Literal
3198 * constants do not have precision qualifiers. Neither do Boolean
3201 * Section 4.5 (Precision and Precision Qualifiers) of the GLSL 1.30
3204 * "Precision qualifiers are added for code portability with OpenGL
3205 * ES, not for functionality. They have the same syntax as in OpenGL
3208 * Section 8 (Built-In Functions) of the GLSL ES 1.00 spec says:
3210 * "uniform lowp sampler2D sampler;
3213 * lowp vec4 col = texture2D (sampler, coord);
3214 * // texture2D returns lowp"
3216 * From this, we infer that GLSL 1.30 (and later) should allow precision
3217 * qualifiers on sampler types just like float and integer types.
3219 return type
->is_float()
3220 || type
->is_integer()
3221 || type
->is_record()
3222 || type
->is_sampler();
3226 ast_declarator_list::hir(exec_list
*instructions
,
3227 struct _mesa_glsl_parse_state
*state
)
3230 const struct glsl_type
*decl_type
;
3231 const char *type_name
= NULL
;
3232 ir_rvalue
*result
= NULL
;
3233 YYLTYPE loc
= this->get_location();
3235 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
3237 * "To ensure that a particular output variable is invariant, it is
3238 * necessary to use the invariant qualifier. It can either be used to
3239 * qualify a previously declared variable as being invariant
3241 * invariant gl_Position; // make existing gl_Position be invariant"
3243 * In these cases the parser will set the 'invariant' flag in the declarator
3244 * list, and the type will be NULL.
3246 if (this->invariant
) {
3247 assert(this->type
== NULL
);
3249 if (state
->current_function
!= NULL
) {
3250 _mesa_glsl_error(& loc
, state
,
3251 "all uses of `invariant' keyword must be at global "
3255 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
3256 assert(decl
->array_specifier
== NULL
);
3257 assert(decl
->initializer
== NULL
);
3259 ir_variable
*const earlier
=
3260 state
->symbols
->get_variable(decl
->identifier
);
3261 if (earlier
== NULL
) {
3262 _mesa_glsl_error(& loc
, state
,
3263 "undeclared variable `%s' cannot be marked "
3264 "invariant", decl
->identifier
);
3265 } else if (!is_varying_var(earlier
, state
->stage
)) {
3266 _mesa_glsl_error(&loc
, state
,
3267 "`%s' cannot be marked invariant; interfaces between "
3268 "shader stages only.", decl
->identifier
);
3269 } else if (earlier
->data
.used
) {
3270 _mesa_glsl_error(& loc
, state
,
3271 "variable `%s' may not be redeclared "
3272 "`invariant' after being used",
3275 earlier
->data
.invariant
= true;
3279 /* Invariant redeclarations do not have r-values.
3284 if (this->precise
) {
3285 assert(this->type
== NULL
);
3287 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
3288 assert(decl
->array_specifier
== NULL
);
3289 assert(decl
->initializer
== NULL
);
3291 ir_variable
*const earlier
=
3292 state
->symbols
->get_variable(decl
->identifier
);
3293 if (earlier
== NULL
) {
3294 _mesa_glsl_error(& loc
, state
,
3295 "undeclared variable `%s' cannot be marked "
3296 "precise", decl
->identifier
);
3297 } else if (state
->current_function
!= NULL
&&
3298 !state
->symbols
->name_declared_this_scope(decl
->identifier
)) {
3299 /* Note: we have to check if we're in a function, since
3300 * builtins are treated as having come from another scope.
3302 _mesa_glsl_error(& loc
, state
,
3303 "variable `%s' from an outer scope may not be "
3304 "redeclared `precise' in this scope",
3306 } else if (earlier
->data
.used
) {
3307 _mesa_glsl_error(& loc
, state
,
3308 "variable `%s' may not be redeclared "
3309 "`precise' after being used",
3312 earlier
->data
.precise
= true;
3316 /* Precise redeclarations do not have r-values either. */
3320 assert(this->type
!= NULL
);
3321 assert(!this->invariant
);
3322 assert(!this->precise
);
3324 /* The type specifier may contain a structure definition. Process that
3325 * before any of the variable declarations.
3327 (void) this->type
->specifier
->hir(instructions
, state
);
3329 decl_type
= this->type
->glsl_type(& type_name
, state
);
3331 /* An offset-qualified atomic counter declaration sets the default
3332 * offset for the next declaration within the same atomic counter
3335 if (decl_type
&& decl_type
->contains_atomic()) {
3336 if (type
->qualifier
.flags
.q
.explicit_binding
&&
3337 type
->qualifier
.flags
.q
.explicit_offset
)
3338 state
->atomic_counter_offsets
[type
->qualifier
.binding
] =
3339 type
->qualifier
.offset
;
3342 if (this->declarations
.is_empty()) {
3343 /* If there is no structure involved in the program text, there are two
3344 * possible scenarios:
3346 * - The program text contained something like 'vec4;'. This is an
3347 * empty declaration. It is valid but weird. Emit a warning.
3349 * - The program text contained something like 'S;' and 'S' is not the
3350 * name of a known structure type. This is both invalid and weird.
3353 * - The program text contained something like 'mediump float;'
3354 * when the programmer probably meant 'precision mediump
3355 * float;' Emit a warning with a description of what they
3356 * probably meant to do.
3358 * Note that if decl_type is NULL and there is a structure involved,
3359 * there must have been some sort of error with the structure. In this
3360 * case we assume that an error was already generated on this line of
3361 * code for the structure. There is no need to generate an additional,
3364 assert(this->type
->specifier
->structure
== NULL
|| decl_type
!= NULL
3367 if (decl_type
== NULL
) {
3368 _mesa_glsl_error(&loc
, state
,
3369 "invalid type `%s' in empty declaration",
3371 } else if (decl_type
->base_type
== GLSL_TYPE_ATOMIC_UINT
) {
3372 /* Empty atomic counter declarations are allowed and useful
3373 * to set the default offset qualifier.
3376 } else if (this->type
->qualifier
.precision
!= ast_precision_none
) {
3377 if (this->type
->specifier
->structure
!= NULL
) {
3378 _mesa_glsl_error(&loc
, state
,
3379 "precision qualifiers can't be applied "
3382 static const char *const precision_names
[] = {
3389 _mesa_glsl_warning(&loc
, state
,
3390 "empty declaration with precision qualifier, "
3391 "to set the default precision, use "
3392 "`precision %s %s;'",
3393 precision_names
[this->type
->qualifier
.precision
],
3396 } else if (this->type
->specifier
->structure
== NULL
) {
3397 _mesa_glsl_warning(&loc
, state
, "empty declaration");
3401 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
3402 const struct glsl_type
*var_type
;
3405 /* FINISHME: Emit a warning if a variable declaration shadows a
3406 * FINISHME: declaration at a higher scope.
3409 if ((decl_type
== NULL
) || decl_type
->is_void()) {
3410 if (type_name
!= NULL
) {
3411 _mesa_glsl_error(& loc
, state
,
3412 "invalid type `%s' in declaration of `%s'",
3413 type_name
, decl
->identifier
);
3415 _mesa_glsl_error(& loc
, state
,
3416 "invalid type in declaration of `%s'",
3422 var_type
= process_array_type(&loc
, decl_type
, decl
->array_specifier
,
3425 var
= new(ctx
) ir_variable(var_type
, decl
->identifier
, ir_var_auto
);
3427 /* The 'varying in' and 'varying out' qualifiers can only be used with
3428 * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
3431 if (this->type
->qualifier
.flags
.q
.varying
) {
3432 if (this->type
->qualifier
.flags
.q
.in
) {
3433 _mesa_glsl_error(& loc
, state
,
3434 "`varying in' qualifier in declaration of "
3435 "`%s' only valid for geometry shaders using "
3436 "ARB_geometry_shader4 or EXT_geometry_shader4",
3438 } else if (this->type
->qualifier
.flags
.q
.out
) {
3439 _mesa_glsl_error(& loc
, state
,
3440 "`varying out' qualifier in declaration of "
3441 "`%s' only valid for geometry shaders using "
3442 "ARB_geometry_shader4 or EXT_geometry_shader4",
3447 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
3449 * "Global variables can only use the qualifiers const,
3450 * attribute, uniform, or varying. Only one may be
3453 * Local variables can only use the qualifier const."
3455 * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
3456 * any extension that adds the 'layout' keyword.
3458 if (!state
->is_version(130, 300)
3459 && !state
->has_explicit_attrib_location()
3460 && !state
->has_separate_shader_objects()
3461 && !state
->ARB_fragment_coord_conventions_enable
) {
3462 if (this->type
->qualifier
.flags
.q
.out
) {
3463 _mesa_glsl_error(& loc
, state
,
3464 "`out' qualifier in declaration of `%s' "
3465 "only valid for function parameters in %s",
3466 decl
->identifier
, state
->get_version_string());
3468 if (this->type
->qualifier
.flags
.q
.in
) {
3469 _mesa_glsl_error(& loc
, state
,
3470 "`in' qualifier in declaration of `%s' "
3471 "only valid for function parameters in %s",
3472 decl
->identifier
, state
->get_version_string());
3474 /* FINISHME: Test for other invalid qualifiers. */
3477 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
3480 if (this->type
->qualifier
.flags
.q
.invariant
) {
3481 if (!is_varying_var(var
, state
->stage
)) {
3482 _mesa_glsl_error(&loc
, state
,
3483 "`%s' cannot be marked invariant; interfaces between "
3484 "shader stages only", var
->name
);
3488 if (state
->current_function
!= NULL
) {
3489 const char *mode
= NULL
;
3490 const char *extra
= "";
3492 /* There is no need to check for 'inout' here because the parser will
3493 * only allow that in function parameter lists.
3495 if (this->type
->qualifier
.flags
.q
.attribute
) {
3497 } else if (this->type
->qualifier
.flags
.q
.uniform
) {
3499 } else if (this->type
->qualifier
.flags
.q
.varying
) {
3501 } else if (this->type
->qualifier
.flags
.q
.in
) {
3503 extra
= " or in function parameter list";
3504 } else if (this->type
->qualifier
.flags
.q
.out
) {
3506 extra
= " or in function parameter list";
3510 _mesa_glsl_error(& loc
, state
,
3511 "%s variable `%s' must be declared at "
3513 mode
, var
->name
, extra
);
3515 } else if (var
->data
.mode
== ir_var_shader_in
) {
3516 var
->data
.read_only
= true;
3518 if (state
->stage
== MESA_SHADER_VERTEX
) {
3519 bool error_emitted
= false;
3521 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
3523 * "Vertex shader inputs can only be float, floating-point
3524 * vectors, matrices, signed and unsigned integers and integer
3525 * vectors. Vertex shader inputs can also form arrays of these
3526 * types, but not structures."
3528 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
3530 * "Vertex shader inputs can only be float, floating-point
3531 * vectors, matrices, signed and unsigned integers and integer
3532 * vectors. They cannot be arrays or structures."
3534 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
3536 * "The attribute qualifier can be used only with float,
3537 * floating-point vectors, and matrices. Attribute variables
3538 * cannot be declared as arrays or structures."
3540 * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
3542 * "Vertex shader inputs can only be float, floating-point
3543 * vectors, matrices, signed and unsigned integers and integer
3544 * vectors. Vertex shader inputs cannot be arrays or
3547 const glsl_type
*check_type
= var
->type
->without_array();
3549 switch (check_type
->base_type
) {
3550 case GLSL_TYPE_FLOAT
:
3552 case GLSL_TYPE_UINT
:
3554 if (state
->is_version(120, 300))
3558 _mesa_glsl_error(& loc
, state
,
3559 "vertex shader input / attribute cannot have "
3561 var
->type
->is_array() ? "array of " : "",
3563 error_emitted
= true;
3566 if (!error_emitted
&& var
->type
->is_array() &&
3567 !state
->check_version(150, 0, &loc
,
3568 "vertex shader input / attribute "
3569 "cannot have array type")) {
3570 error_emitted
= true;
3572 } else if (state
->stage
== MESA_SHADER_GEOMETRY
) {
3573 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
3575 * Geometry shader input variables get the per-vertex values
3576 * written out by vertex shader output variables of the same
3577 * names. Since a geometry shader operates on a set of
3578 * vertices, each input varying variable (or input block, see
3579 * interface blocks below) needs to be declared as an array.
3581 if (!var
->type
->is_array()) {
3582 _mesa_glsl_error(&loc
, state
,
3583 "geometry shader inputs must be arrays");
3586 handle_geometry_shader_input_decl(state
, loc
, var
);
3588 } else if (var
->data
.mode
== ir_var_shader_out
) {
3589 const glsl_type
*check_type
= var
->type
->without_array();
3591 /* From section 4.3.6 (Output variables) of the GLSL 4.40 spec:
3593 * It is a compile-time error to declare a vertex, tessellation
3594 * evaluation, tessellation control, or geometry shader output
3595 * that contains any of the following:
3597 * * A Boolean type (bool, bvec2 ...)
3600 if (check_type
->is_boolean() || check_type
->contains_opaque())
3601 _mesa_glsl_error(&loc
, state
,
3602 "%s shader output cannot have type %s",
3603 _mesa_shader_stage_to_string(state
->stage
),
3606 /* From section 4.3.6 (Output variables) of the GLSL 4.40 spec:
3608 * It is a compile-time error to declare a fragment shader output
3609 * that contains any of the following:
3611 * * A Boolean type (bool, bvec2 ...)
3612 * * A double-precision scalar or vector (double, dvec2 ...)
3617 if (state
->stage
== MESA_SHADER_FRAGMENT
) {
3618 if (check_type
->is_record() || check_type
->is_matrix())
3619 _mesa_glsl_error(&loc
, state
,
3620 "fragment shader output "
3621 "cannot have struct or array type");
3622 switch (check_type
->base_type
) {
3623 case GLSL_TYPE_UINT
:
3625 case GLSL_TYPE_FLOAT
:
3628 _mesa_glsl_error(&loc
, state
,
3629 "fragment shader output cannot have "
3630 "type %s", check_type
->name
);
3635 /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
3636 * so must integer vertex outputs.
3638 * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
3639 * "Fragment shader inputs that are signed or unsigned integers or
3640 * integer vectors must be qualified with the interpolation qualifier
3643 * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
3644 * "Fragment shader inputs that are, or contain, signed or unsigned
3645 * integers or integer vectors must be qualified with the
3646 * interpolation qualifier flat."
3648 * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
3649 * "Vertex shader outputs that are, or contain, signed or unsigned
3650 * integers or integer vectors must be qualified with the
3651 * interpolation qualifier flat."
3653 * Note that prior to GLSL 1.50, this requirement applied to vertex
3654 * outputs rather than fragment inputs. That creates problems in the
3655 * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
3656 * desktop GL shaders. For GLSL ES shaders, we follow the spec and
3657 * apply the restriction to both vertex outputs and fragment inputs.
3659 * Note also that the desktop GLSL specs are missing the text "or
3660 * contain"; this is presumably an oversight, since there is no
3661 * reasonable way to interpolate a fragment shader input that contains
3664 if (state
->is_version(130, 300) &&
3665 var
->type
->contains_integer() &&
3666 var
->data
.interpolation
!= INTERP_QUALIFIER_FLAT
&&
3667 ((state
->stage
== MESA_SHADER_FRAGMENT
&& var
->data
.mode
== ir_var_shader_in
)
3668 || (state
->stage
== MESA_SHADER_VERTEX
&& var
->data
.mode
== ir_var_shader_out
3669 && state
->es_shader
))) {
3670 const char *var_type
= (state
->stage
== MESA_SHADER_VERTEX
) ?
3671 "vertex output" : "fragment input";
3672 _mesa_glsl_error(&loc
, state
, "if a %s is (or contains) "
3673 "an integer, then it must be qualified with 'flat'",
3677 /* Double fragment inputs must be qualified with 'flat'. */
3678 if (var
->type
->contains_double() &&
3679 var
->data
.interpolation
!= INTERP_QUALIFIER_FLAT
&&
3680 state
->stage
== MESA_SHADER_FRAGMENT
&&
3681 var
->data
.mode
== ir_var_shader_in
) {
3682 _mesa_glsl_error(&loc
, state
, "if a fragment input is (or contains) "
3683 "a double, then it must be qualified with 'flat'",
3687 /* Interpolation qualifiers cannot be applied to 'centroid' and
3688 * 'centroid varying'.
3690 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
3691 * "interpolation qualifiers may only precede the qualifiers in,
3692 * centroid in, out, or centroid out in a declaration. They do not apply
3693 * to the deprecated storage qualifiers varying or centroid varying."
3695 * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
3697 if (state
->is_version(130, 0)
3698 && this->type
->qualifier
.has_interpolation()
3699 && this->type
->qualifier
.flags
.q
.varying
) {
3701 const char *i
= this->type
->qualifier
.interpolation_string();
3704 if (this->type
->qualifier
.flags
.q
.centroid
)
3705 s
= "centroid varying";
3709 _mesa_glsl_error(&loc
, state
,
3710 "qualifier '%s' cannot be applied to the "
3711 "deprecated storage qualifier '%s'", i
, s
);
3715 /* Interpolation qualifiers can only apply to vertex shader outputs and
3716 * fragment shader inputs.
3718 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
3719 * "Outputs from a vertex shader (out) and inputs to a fragment
3720 * shader (in) can be further qualified with one or more of these
3721 * interpolation qualifiers"
3723 * From page 31 (page 37 of the PDF) of the GLSL ES 3.00 spec:
3724 * "These interpolation qualifiers may only precede the qualifiers
3725 * in, centroid in, out, or centroid out in a declaration. They do
3726 * not apply to inputs into a vertex shader or outputs from a
3729 if (state
->is_version(130, 300)
3730 && this->type
->qualifier
.has_interpolation()) {
3732 const char *i
= this->type
->qualifier
.interpolation_string();
3735 switch (state
->stage
) {
3736 case MESA_SHADER_VERTEX
:
3737 if (this->type
->qualifier
.flags
.q
.in
) {
3738 _mesa_glsl_error(&loc
, state
,
3739 "qualifier '%s' cannot be applied to vertex "
3740 "shader inputs", i
);
3743 case MESA_SHADER_FRAGMENT
:
3744 if (this->type
->qualifier
.flags
.q
.out
) {
3745 _mesa_glsl_error(&loc
, state
,
3746 "qualifier '%s' cannot be applied to fragment "
3747 "shader outputs", i
);
3756 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
3758 if (this->type
->qualifier
.precision
!= ast_precision_none
) {
3759 state
->check_precision_qualifiers_allowed(&loc
);
3763 /* If a precision qualifier is allowed on a type, it is allowed on
3764 * an array of that type.
3766 if (!(this->type
->qualifier
.precision
== ast_precision_none
3767 || precision_qualifier_allowed(var
->type
)
3768 || (var
->type
->is_array()
3769 && precision_qualifier_allowed(var
->type
->fields
.array
)))) {
3771 _mesa_glsl_error(&loc
, state
,
3772 "precision qualifiers apply only to floating point"
3773 ", integer and sampler types");
3776 /* From section 4.1.7 of the GLSL 4.40 spec:
3778 * "[Opaque types] can only be declared as function
3779 * parameters or uniform-qualified variables."
3781 if (var_type
->contains_opaque() &&
3782 !this->type
->qualifier
.flags
.q
.uniform
) {
3783 _mesa_glsl_error(&loc
, state
,
3784 "opaque variables must be declared uniform");
3787 /* Process the initializer and add its instructions to a temporary
3788 * list. This list will be added to the instruction stream (below) after
3789 * the declaration is added. This is done because in some cases (such as
3790 * redeclarations) the declaration may not actually be added to the
3791 * instruction stream.
3793 exec_list initializer_instructions
;
3795 /* Examine var name here since var may get deleted in the next call */
3796 bool var_is_gl_id
= is_gl_identifier(var
->name
);
3798 ir_variable
*earlier
=
3799 get_variable_being_redeclared(var
, decl
->get_location(), state
,
3800 false /* allow_all_redeclarations */);
3801 if (earlier
!= NULL
) {
3803 earlier
->data
.how_declared
== ir_var_declared_in_block
) {
3804 _mesa_glsl_error(&loc
, state
,
3805 "`%s' has already been redeclared using "
3806 "gl_PerVertex", earlier
->name
);
3808 earlier
->data
.how_declared
= ir_var_declared_normally
;
3811 if (decl
->initializer
!= NULL
) {
3812 result
= process_initializer((earlier
== NULL
) ? var
: earlier
,
3814 &initializer_instructions
, state
);
3817 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
3819 * "It is an error to write to a const variable outside of
3820 * its declaration, so they must be initialized when
3823 if (this->type
->qualifier
.flags
.q
.constant
&& decl
->initializer
== NULL
) {
3824 _mesa_glsl_error(& loc
, state
,
3825 "const declaration of `%s' must be initialized",
3829 if (state
->es_shader
) {
3830 const glsl_type
*const t
= (earlier
== NULL
)
3831 ? var
->type
: earlier
->type
;
3833 if (t
->is_unsized_array())
3834 /* Section 10.17 of the GLSL ES 1.00 specification states that
3835 * unsized array declarations have been removed from the language.
3836 * Arrays that are sized using an initializer are still explicitly
3837 * sized. However, GLSL ES 1.00 does not allow array
3838 * initializers. That is only allowed in GLSL ES 3.00.
3840 * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
3842 * "An array type can also be formed without specifying a size
3843 * if the definition includes an initializer:
3845 * float x[] = float[2] (1.0, 2.0); // declares an array of size 2
3846 * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
3851 _mesa_glsl_error(& loc
, state
,
3852 "unsized array declarations are not allowed in "
3856 /* If the declaration is not a redeclaration, there are a few additional
3857 * semantic checks that must be applied. In addition, variable that was
3858 * created for the declaration should be added to the IR stream.
3860 if (earlier
== NULL
) {
3861 validate_identifier(decl
->identifier
, loc
, state
);
3863 /* Add the variable to the symbol table. Note that the initializer's
3864 * IR was already processed earlier (though it hasn't been emitted
3865 * yet), without the variable in scope.
3867 * This differs from most C-like languages, but it follows the GLSL
3868 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
3871 * "Within a declaration, the scope of a name starts immediately
3872 * after the initializer if present or immediately after the name
3873 * being declared if not."
3875 if (!state
->symbols
->add_variable(var
)) {
3876 YYLTYPE loc
= this->get_location();
3877 _mesa_glsl_error(&loc
, state
, "name `%s' already taken in the "
3878 "current scope", decl
->identifier
);
3882 /* Push the variable declaration to the top. It means that all the
3883 * variable declarations will appear in a funny last-to-first order,
3884 * but otherwise we run into trouble if a function is prototyped, a
3885 * global var is decled, then the function is defined with usage of
3886 * the global var. See glslparsertest's CorrectModule.frag.
3888 instructions
->push_head(var
);
3891 instructions
->append_list(&initializer_instructions
);
3895 /* Generally, variable declarations do not have r-values. However,
3896 * one is used for the declaration in
3898 * while (bool b = some_condition()) {
3902 * so we return the rvalue from the last seen declaration here.
3909 ast_parameter_declarator::hir(exec_list
*instructions
,
3910 struct _mesa_glsl_parse_state
*state
)
3913 const struct glsl_type
*type
;
3914 const char *name
= NULL
;
3915 YYLTYPE loc
= this->get_location();
3917 type
= this->type
->glsl_type(& name
, state
);
3921 _mesa_glsl_error(& loc
, state
,
3922 "invalid type `%s' in declaration of `%s'",
3923 name
, this->identifier
);
3925 _mesa_glsl_error(& loc
, state
,
3926 "invalid type in declaration of `%s'",
3930 type
= glsl_type::error_type
;
3933 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
3935 * "Functions that accept no input arguments need not use void in the
3936 * argument list because prototypes (or definitions) are required and
3937 * therefore there is no ambiguity when an empty argument list "( )" is
3938 * declared. The idiom "(void)" as a parameter list is provided for
3941 * Placing this check here prevents a void parameter being set up
3942 * for a function, which avoids tripping up checks for main taking
3943 * parameters and lookups of an unnamed symbol.
3945 if (type
->is_void()) {
3946 if (this->identifier
!= NULL
)
3947 _mesa_glsl_error(& loc
, state
,
3948 "named parameter cannot have type `void'");
3954 if (formal_parameter
&& (this->identifier
== NULL
)) {
3955 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
3959 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
3960 * call already handled the "vec4[..] foo" case.
3962 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
3964 if (!type
->is_error() && type
->is_unsized_array()) {
3965 _mesa_glsl_error(&loc
, state
, "arrays passed as parameters must have "
3967 type
= glsl_type::error_type
;
3971 ir_variable
*var
= new(ctx
)
3972 ir_variable(type
, this->identifier
, ir_var_function_in
);
3974 /* Apply any specified qualifiers to the parameter declaration. Note that
3975 * for function parameters the default mode is 'in'.
3977 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
,
3980 /* From section 4.1.7 of the GLSL 4.40 spec:
3982 * "Opaque variables cannot be treated as l-values; hence cannot
3983 * be used as out or inout function parameters, nor can they be
3986 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
3987 && type
->contains_opaque()) {
3988 _mesa_glsl_error(&loc
, state
, "out and inout parameters cannot "
3989 "contain opaque variables");
3990 type
= glsl_type::error_type
;
3993 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
3995 * "When calling a function, expressions that do not evaluate to
3996 * l-values cannot be passed to parameters declared as out or inout."
3998 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
4000 * "Other binary or unary expressions, non-dereferenced arrays,
4001 * function names, swizzles with repeated fields, and constants
4002 * cannot be l-values."
4004 * So for GLSL 1.10, passing an array as an out or inout parameter is not
4005 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
4007 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
4009 && !state
->check_version(120, 100, &loc
,
4010 "arrays cannot be out or inout parameters")) {
4011 type
= glsl_type::error_type
;
4014 instructions
->push_tail(var
);
4016 /* Parameter declarations do not have r-values.
4023 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
4025 exec_list
*ir_parameters
,
4026 _mesa_glsl_parse_state
*state
)
4028 ast_parameter_declarator
*void_param
= NULL
;
4031 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
4032 param
->formal_parameter
= formal
;
4033 param
->hir(ir_parameters
, state
);
4041 if ((void_param
!= NULL
) && (count
> 1)) {
4042 YYLTYPE loc
= void_param
->get_location();
4044 _mesa_glsl_error(& loc
, state
,
4045 "`void' parameter must be only parameter");
4051 emit_function(_mesa_glsl_parse_state
*state
, ir_function
*f
)
4053 /* IR invariants disallow function declarations or definitions
4054 * nested within other function definitions. But there is no
4055 * requirement about the relative order of function declarations
4056 * and definitions with respect to one another. So simply insert
4057 * the new ir_function block at the end of the toplevel instruction
4060 state
->toplevel_ir
->push_tail(f
);
4065 ast_function::hir(exec_list
*instructions
,
4066 struct _mesa_glsl_parse_state
*state
)
4069 ir_function
*f
= NULL
;
4070 ir_function_signature
*sig
= NULL
;
4071 exec_list hir_parameters
;
4073 const char *const name
= identifier
;
4075 /* New functions are always added to the top-level IR instruction stream,
4076 * so this instruction list pointer is ignored. See also emit_function
4079 (void) instructions
;
4081 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
4083 * "Function declarations (prototypes) cannot occur inside of functions;
4084 * they must be at global scope, or for the built-in functions, outside
4085 * the global scope."
4087 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
4089 * "User defined functions may only be defined within the global scope."
4091 * Note that this language does not appear in GLSL 1.10.
4093 if ((state
->current_function
!= NULL
) &&
4094 state
->is_version(120, 100)) {
4095 YYLTYPE loc
= this->get_location();
4096 _mesa_glsl_error(&loc
, state
,
4097 "declaration of function `%s' not allowed within "
4098 "function body", name
);
4101 validate_identifier(name
, this->get_location(), state
);
4103 /* Convert the list of function parameters to HIR now so that they can be
4104 * used below to compare this function's signature with previously seen
4105 * signatures for functions with the same name.
4107 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
4109 & hir_parameters
, state
);
4111 const char *return_type_name
;
4112 const glsl_type
*return_type
=
4113 this->return_type
->glsl_type(& return_type_name
, state
);
4116 YYLTYPE loc
= this->get_location();
4117 _mesa_glsl_error(&loc
, state
,
4118 "function `%s' has undeclared return type `%s'",
4119 name
, return_type_name
);
4120 return_type
= glsl_type::error_type
;
4123 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
4124 * "No qualifier is allowed on the return type of a function."
4126 if (this->return_type
->has_qualifiers()) {
4127 YYLTYPE loc
= this->get_location();
4128 _mesa_glsl_error(& loc
, state
,
4129 "function `%s' return type has qualifiers", name
);
4132 /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
4134 * "Arrays are allowed as arguments and as the return type. In both
4135 * cases, the array must be explicitly sized."
4137 if (return_type
->is_unsized_array()) {
4138 YYLTYPE loc
= this->get_location();
4139 _mesa_glsl_error(& loc
, state
,
4140 "function `%s' return type array must be explicitly "
4144 /* From section 4.1.7 of the GLSL 4.40 spec:
4146 * "[Opaque types] can only be declared as function parameters
4147 * or uniform-qualified variables."
4149 if (return_type
->contains_opaque()) {
4150 YYLTYPE loc
= this->get_location();
4151 _mesa_glsl_error(&loc
, state
,
4152 "function `%s' return type can't contain an opaque type",
4156 /* Create an ir_function if one doesn't already exist. */
4157 f
= state
->symbols
->get_function(name
);
4159 f
= new(ctx
) ir_function(name
);
4160 if (!state
->symbols
->add_function(f
)) {
4161 /* This function name shadows a non-function use of the same name. */
4162 YYLTYPE loc
= this->get_location();
4164 _mesa_glsl_error(&loc
, state
, "function name `%s' conflicts with "
4165 "non-function", name
);
4169 emit_function(state
, f
);
4172 /* From GLSL ES 3.0 spec, chapter 6.1 "Function Definitions", page 71:
4174 * "A shader cannot redefine or overload built-in functions."
4176 * While in GLSL ES 1.0 specification, chapter 8 "Built-in Functions":
4178 * "User code can overload the built-in functions but cannot redefine
4181 if (state
->es_shader
&& state
->language_version
>= 300) {
4182 /* Local shader has no exact candidates; check the built-ins. */
4183 _mesa_glsl_initialize_builtin_functions();
4184 if (_mesa_glsl_find_builtin_function_by_name(state
, name
)) {
4185 YYLTYPE loc
= this->get_location();
4186 _mesa_glsl_error(& loc
, state
,
4187 "A shader cannot redefine or overload built-in "
4188 "function `%s' in GLSL ES 3.00", name
);
4193 /* Verify that this function's signature either doesn't match a previously
4194 * seen signature for a function with the same name, or, if a match is found,
4195 * that the previously seen signature does not have an associated definition.
4197 if (state
->es_shader
|| f
->has_user_signature()) {
4198 sig
= f
->exact_matching_signature(state
, &hir_parameters
);
4200 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
4201 if (badvar
!= NULL
) {
4202 YYLTYPE loc
= this->get_location();
4204 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
4205 "qualifiers don't match prototype", name
, badvar
);
4208 if (sig
->return_type
!= return_type
) {
4209 YYLTYPE loc
= this->get_location();
4211 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
4212 "match prototype", name
);
4215 if (sig
->is_defined
) {
4216 if (is_definition
) {
4217 YYLTYPE loc
= this->get_location();
4218 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
4220 /* We just encountered a prototype that exactly matches a
4221 * function that's already been defined. This is redundant,
4222 * and we should ignore it.
4230 /* Verify the return type of main() */
4231 if (strcmp(name
, "main") == 0) {
4232 if (! return_type
->is_void()) {
4233 YYLTYPE loc
= this->get_location();
4235 _mesa_glsl_error(& loc
, state
, "main() must return void");
4238 if (!hir_parameters
.is_empty()) {
4239 YYLTYPE loc
= this->get_location();
4241 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
4245 /* Finish storing the information about this new function in its signature.
4248 sig
= new(ctx
) ir_function_signature(return_type
);
4249 f
->add_signature(sig
);
4252 sig
->replace_parameters(&hir_parameters
);
4255 /* Function declarations (prototypes) do not have r-values.
4262 ast_function_definition::hir(exec_list
*instructions
,
4263 struct _mesa_glsl_parse_state
*state
)
4265 prototype
->is_definition
= true;
4266 prototype
->hir(instructions
, state
);
4268 ir_function_signature
*signature
= prototype
->signature
;
4269 if (signature
== NULL
)
4272 assert(state
->current_function
== NULL
);
4273 state
->current_function
= signature
;
4274 state
->found_return
= false;
4276 /* Duplicate parameters declared in the prototype as concrete variables.
4277 * Add these to the symbol table.
4279 state
->symbols
->push_scope();
4280 foreach_in_list(ir_variable
, var
, &signature
->parameters
) {
4281 assert(var
->as_variable() != NULL
);
4283 /* The only way a parameter would "exist" is if two parameters have
4286 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
4287 YYLTYPE loc
= this->get_location();
4289 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
4291 state
->symbols
->add_variable(var
);
4295 /* Convert the body of the function to HIR. */
4296 this->body
->hir(&signature
->body
, state
);
4297 signature
->is_defined
= true;
4299 state
->symbols
->pop_scope();
4301 assert(state
->current_function
== signature
);
4302 state
->current_function
= NULL
;
4304 if (!signature
->return_type
->is_void() && !state
->found_return
) {
4305 YYLTYPE loc
= this->get_location();
4306 _mesa_glsl_error(& loc
, state
, "function `%s' has non-void return type "
4307 "%s, but no return statement",
4308 signature
->function_name(),
4309 signature
->return_type
->name
);
4312 /* Function definitions do not have r-values.
4319 ast_jump_statement::hir(exec_list
*instructions
,
4320 struct _mesa_glsl_parse_state
*state
)
4327 assert(state
->current_function
);
4329 if (opt_return_value
) {
4330 ir_rvalue
*ret
= opt_return_value
->hir(instructions
, state
);
4332 /* The value of the return type can be NULL if the shader says
4333 * 'return foo();' and foo() is a function that returns void.
4335 * NOTE: The GLSL spec doesn't say that this is an error. The type
4336 * of the return value is void. If the return type of the function is
4337 * also void, then this should compile without error. Seriously.
4339 const glsl_type
*const ret_type
=
4340 (ret
== NULL
) ? glsl_type::void_type
: ret
->type
;
4342 /* Implicit conversions are not allowed for return values prior to
4343 * ARB_shading_language_420pack.
4345 if (state
->current_function
->return_type
!= ret_type
) {
4346 YYLTYPE loc
= this->get_location();
4348 if (state
->ARB_shading_language_420pack_enable
) {
4349 if (!apply_implicit_conversion(state
->current_function
->return_type
,
4351 _mesa_glsl_error(& loc
, state
,
4352 "could not implicitly convert return value "
4353 "to %s, in function `%s'",
4354 state
->current_function
->return_type
->name
,
4355 state
->current_function
->function_name());
4358 _mesa_glsl_error(& loc
, state
,
4359 "`return' with wrong type %s, in function `%s' "
4362 state
->current_function
->function_name(),
4363 state
->current_function
->return_type
->name
);
4365 } else if (state
->current_function
->return_type
->base_type
==
4367 YYLTYPE loc
= this->get_location();
4369 /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
4370 * specs add a clarification:
4372 * "A void function can only use return without a return argument, even if
4373 * the return argument has void type. Return statements only accept values:
4376 * void func2() { return func1(); } // illegal return statement"
4378 _mesa_glsl_error(& loc
, state
,
4379 "void functions can only use `return' without a "
4383 inst
= new(ctx
) ir_return(ret
);
4385 if (state
->current_function
->return_type
->base_type
!=
4387 YYLTYPE loc
= this->get_location();
4389 _mesa_glsl_error(& loc
, state
,
4390 "`return' with no value, in function %s returning "
4392 state
->current_function
->function_name());
4394 inst
= new(ctx
) ir_return
;
4397 state
->found_return
= true;
4398 instructions
->push_tail(inst
);
4403 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
4404 YYLTYPE loc
= this->get_location();
4406 _mesa_glsl_error(& loc
, state
,
4407 "`discard' may only appear in a fragment shader");
4409 instructions
->push_tail(new(ctx
) ir_discard
);
4414 if (mode
== ast_continue
&&
4415 state
->loop_nesting_ast
== NULL
) {
4416 YYLTYPE loc
= this->get_location();
4418 _mesa_glsl_error(& loc
, state
, "continue may only appear in a loop");
4419 } else if (mode
== ast_break
&&
4420 state
->loop_nesting_ast
== NULL
&&
4421 state
->switch_state
.switch_nesting_ast
== NULL
) {
4422 YYLTYPE loc
= this->get_location();
4424 _mesa_glsl_error(& loc
, state
,
4425 "break may only appear in a loop or a switch");
4427 /* For a loop, inline the for loop expression again, since we don't
4428 * know where near the end of the loop body the normal copy of it is
4429 * going to be placed. Same goes for the condition for a do-while
4432 if (state
->loop_nesting_ast
!= NULL
&&
4433 mode
== ast_continue
&& !state
->switch_state
.is_switch_innermost
) {
4434 if (state
->loop_nesting_ast
->rest_expression
) {
4435 state
->loop_nesting_ast
->rest_expression
->hir(instructions
,
4438 if (state
->loop_nesting_ast
->mode
==
4439 ast_iteration_statement::ast_do_while
) {
4440 state
->loop_nesting_ast
->condition_to_hir(instructions
, state
);
4444 if (state
->switch_state
.is_switch_innermost
&&
4445 mode
== ast_continue
) {
4446 /* Set 'continue_inside' to true. */
4447 ir_rvalue
*const true_val
= new (ctx
) ir_constant(true);
4448 ir_dereference_variable
*deref_continue_inside_var
=
4449 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
4450 instructions
->push_tail(new(ctx
) ir_assignment(deref_continue_inside_var
,
4453 /* Break out from the switch, continue for the loop will
4454 * be called right after switch. */
4455 ir_loop_jump
*const jump
=
4456 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
4457 instructions
->push_tail(jump
);
4459 } else if (state
->switch_state
.is_switch_innermost
&&
4460 mode
== ast_break
) {
4461 /* Force break out of switch by inserting a break. */
4462 ir_loop_jump
*const jump
=
4463 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
4464 instructions
->push_tail(jump
);
4466 ir_loop_jump
*const jump
=
4467 new(ctx
) ir_loop_jump((mode
== ast_break
)
4468 ? ir_loop_jump::jump_break
4469 : ir_loop_jump::jump_continue
);
4470 instructions
->push_tail(jump
);
4477 /* Jump instructions do not have r-values.
4484 ast_selection_statement::hir(exec_list
*instructions
,
4485 struct _mesa_glsl_parse_state
*state
)
4489 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
4491 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
4493 * "Any expression whose type evaluates to a Boolean can be used as the
4494 * conditional expression bool-expression. Vector types are not accepted
4495 * as the expression to if."
4497 * The checks are separated so that higher quality diagnostics can be
4498 * generated for cases where both rules are violated.
4500 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
4501 YYLTYPE loc
= this->condition
->get_location();
4503 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
4507 ir_if
*const stmt
= new(ctx
) ir_if(condition
);
4509 if (then_statement
!= NULL
) {
4510 state
->symbols
->push_scope();
4511 then_statement
->hir(& stmt
->then_instructions
, state
);
4512 state
->symbols
->pop_scope();
4515 if (else_statement
!= NULL
) {
4516 state
->symbols
->push_scope();
4517 else_statement
->hir(& stmt
->else_instructions
, state
);
4518 state
->symbols
->pop_scope();
4521 instructions
->push_tail(stmt
);
4523 /* if-statements do not have r-values.
4530 ast_switch_statement::hir(exec_list
*instructions
,
4531 struct _mesa_glsl_parse_state
*state
)
4535 ir_rvalue
*const test_expression
=
4536 this->test_expression
->hir(instructions
, state
);
4538 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
4540 * "The type of init-expression in a switch statement must be a
4543 if (!test_expression
->type
->is_scalar() ||
4544 !test_expression
->type
->is_integer()) {
4545 YYLTYPE loc
= this->test_expression
->get_location();
4547 _mesa_glsl_error(& loc
,
4549 "switch-statement expression must be scalar "
4553 /* Track the switch-statement nesting in a stack-like manner.
4555 struct glsl_switch_state saved
= state
->switch_state
;
4557 state
->switch_state
.is_switch_innermost
= true;
4558 state
->switch_state
.switch_nesting_ast
= this;
4559 state
->switch_state
.labels_ht
= hash_table_ctor(0, hash_table_pointer_hash
,
4560 hash_table_pointer_compare
);
4561 state
->switch_state
.previous_default
= NULL
;
4563 /* Initalize is_fallthru state to false.
4565 ir_rvalue
*const is_fallthru_val
= new (ctx
) ir_constant(false);
4566 state
->switch_state
.is_fallthru_var
=
4567 new(ctx
) ir_variable(glsl_type::bool_type
,
4568 "switch_is_fallthru_tmp",
4570 instructions
->push_tail(state
->switch_state
.is_fallthru_var
);
4572 ir_dereference_variable
*deref_is_fallthru_var
=
4573 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4574 instructions
->push_tail(new(ctx
) ir_assignment(deref_is_fallthru_var
,
4577 /* Initialize continue_inside state to false.
4579 state
->switch_state
.continue_inside
=
4580 new(ctx
) ir_variable(glsl_type::bool_type
,
4581 "continue_inside_tmp",
4583 instructions
->push_tail(state
->switch_state
.continue_inside
);
4585 ir_rvalue
*const false_val
= new (ctx
) ir_constant(false);
4586 ir_dereference_variable
*deref_continue_inside_var
=
4587 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
4588 instructions
->push_tail(new(ctx
) ir_assignment(deref_continue_inside_var
,
4591 state
->switch_state
.run_default
=
4592 new(ctx
) ir_variable(glsl_type::bool_type
,
4595 instructions
->push_tail(state
->switch_state
.run_default
);
4597 /* Loop around the switch is used for flow control. */
4598 ir_loop
* loop
= new(ctx
) ir_loop();
4599 instructions
->push_tail(loop
);
4601 /* Cache test expression.
4603 test_to_hir(&loop
->body_instructions
, state
);
4605 /* Emit code for body of switch stmt.
4607 body
->hir(&loop
->body_instructions
, state
);
4609 /* Insert a break at the end to exit loop. */
4610 ir_loop_jump
*jump
= new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
4611 loop
->body_instructions
.push_tail(jump
);
4613 /* If we are inside loop, check if continue got called inside switch. */
4614 if (state
->loop_nesting_ast
!= NULL
) {
4615 ir_dereference_variable
*deref_continue_inside
=
4616 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
4617 ir_if
*irif
= new(ctx
) ir_if(deref_continue_inside
);
4618 ir_loop_jump
*jump
= new(ctx
) ir_loop_jump(ir_loop_jump::jump_continue
);
4620 if (state
->loop_nesting_ast
!= NULL
) {
4621 if (state
->loop_nesting_ast
->rest_expression
) {
4622 state
->loop_nesting_ast
->rest_expression
->hir(&irif
->then_instructions
,
4625 if (state
->loop_nesting_ast
->mode
==
4626 ast_iteration_statement::ast_do_while
) {
4627 state
->loop_nesting_ast
->condition_to_hir(&irif
->then_instructions
, state
);
4630 irif
->then_instructions
.push_tail(jump
);
4631 instructions
->push_tail(irif
);
4634 hash_table_dtor(state
->switch_state
.labels_ht
);
4636 state
->switch_state
= saved
;
4638 /* Switch statements do not have r-values. */
4644 ast_switch_statement::test_to_hir(exec_list
*instructions
,
4645 struct _mesa_glsl_parse_state
*state
)
4649 /* Cache value of test expression. */
4650 ir_rvalue
*const test_val
=
4651 test_expression
->hir(instructions
,
4654 state
->switch_state
.test_var
= new(ctx
) ir_variable(test_val
->type
,
4657 ir_dereference_variable
*deref_test_var
=
4658 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
4660 instructions
->push_tail(state
->switch_state
.test_var
);
4661 instructions
->push_tail(new(ctx
) ir_assignment(deref_test_var
, test_val
));
4666 ast_switch_body::hir(exec_list
*instructions
,
4667 struct _mesa_glsl_parse_state
*state
)
4670 stmts
->hir(instructions
, state
);
4672 /* Switch bodies do not have r-values. */
4677 ast_case_statement_list::hir(exec_list
*instructions
,
4678 struct _mesa_glsl_parse_state
*state
)
4680 exec_list default_case
, after_default
, tmp
;
4682 foreach_list_typed (ast_case_statement
, case_stmt
, link
, & this->cases
) {
4683 case_stmt
->hir(&tmp
, state
);
4686 if (state
->switch_state
.previous_default
&& default_case
.is_empty()) {
4687 default_case
.append_list(&tmp
);
4691 /* If default case found, append 'after_default' list. */
4692 if (!default_case
.is_empty())
4693 after_default
.append_list(&tmp
);
4695 instructions
->append_list(&tmp
);
4698 /* Handle the default case. This is done here because default might not be
4699 * the last case. We need to add checks against following cases first to see
4700 * if default should be chosen or not.
4702 if (!default_case
.is_empty()) {
4704 ir_rvalue
*const true_val
= new (state
) ir_constant(true);
4705 ir_dereference_variable
*deref_run_default_var
=
4706 new(state
) ir_dereference_variable(state
->switch_state
.run_default
);
4708 /* Choose to run default case initially, following conditional
4709 * assignments might change this.
4711 ir_assignment
*const init_var
=
4712 new(state
) ir_assignment(deref_run_default_var
, true_val
);
4713 instructions
->push_tail(init_var
);
4715 /* Default case was the last one, no checks required. */
4716 if (after_default
.is_empty()) {
4717 instructions
->append_list(&default_case
);
4721 foreach_in_list(ir_instruction
, ir
, &after_default
) {
4722 ir_assignment
*assign
= ir
->as_assignment();
4727 /* Clone the check between case label and init expression. */
4728 ir_expression
*exp
= (ir_expression
*) assign
->condition
;
4729 ir_expression
*clone
= exp
->clone(state
, NULL
);
4731 ir_dereference_variable
*deref_var
=
4732 new(state
) ir_dereference_variable(state
->switch_state
.run_default
);
4733 ir_rvalue
*const false_val
= new (state
) ir_constant(false);
4735 ir_assignment
*const set_false
=
4736 new(state
) ir_assignment(deref_var
, false_val
, clone
);
4738 instructions
->push_tail(set_false
);
4741 /* Append default case and all cases after it. */
4742 instructions
->append_list(&default_case
);
4743 instructions
->append_list(&after_default
);
4746 /* Case statements do not have r-values. */
4751 ast_case_statement::hir(exec_list
*instructions
,
4752 struct _mesa_glsl_parse_state
*state
)
4754 labels
->hir(instructions
, state
);
4756 /* Guard case statements depending on fallthru state. */
4757 ir_dereference_variable
*const deref_fallthru_guard
=
4758 new(state
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4759 ir_if
*const test_fallthru
= new(state
) ir_if(deref_fallthru_guard
);
4761 foreach_list_typed (ast_node
, stmt
, link
, & this->stmts
)
4762 stmt
->hir(& test_fallthru
->then_instructions
, state
);
4764 instructions
->push_tail(test_fallthru
);
4766 /* Case statements do not have r-values. */
4772 ast_case_label_list::hir(exec_list
*instructions
,
4773 struct _mesa_glsl_parse_state
*state
)
4775 foreach_list_typed (ast_case_label
, label
, link
, & this->labels
)
4776 label
->hir(instructions
, state
);
4778 /* Case labels do not have r-values. */
4783 ast_case_label::hir(exec_list
*instructions
,
4784 struct _mesa_glsl_parse_state
*state
)
4788 ir_dereference_variable
*deref_fallthru_var
=
4789 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4791 ir_rvalue
*const true_val
= new(ctx
) ir_constant(true);
4793 /* If not default case, ... */
4794 if (this->test_value
!= NULL
) {
4795 /* Conditionally set fallthru state based on
4796 * comparison of cached test expression value to case label.
4798 ir_rvalue
*const label_rval
= this->test_value
->hir(instructions
, state
);
4799 ir_constant
*label_const
= label_rval
->constant_expression_value();
4802 YYLTYPE loc
= this->test_value
->get_location();
4804 _mesa_glsl_error(& loc
, state
,
4805 "switch statement case label must be a "
4806 "constant expression");
4808 /* Stuff a dummy value in to allow processing to continue. */
4809 label_const
= new(ctx
) ir_constant(0);
4811 ast_expression
*previous_label
= (ast_expression
*)
4812 hash_table_find(state
->switch_state
.labels_ht
,
4813 (void *)(uintptr_t)label_const
->value
.u
[0]);
4815 if (previous_label
) {
4816 YYLTYPE loc
= this->test_value
->get_location();
4817 _mesa_glsl_error(& loc
, state
, "duplicate case value");
4819 loc
= previous_label
->get_location();
4820 _mesa_glsl_error(& loc
, state
, "this is the previous case label");
4822 hash_table_insert(state
->switch_state
.labels_ht
,
4824 (void *)(uintptr_t)label_const
->value
.u
[0]);
4828 ir_dereference_variable
*deref_test_var
=
4829 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
4831 ir_expression
*test_cond
= new(ctx
) ir_expression(ir_binop_all_equal
,
4836 * From GLSL 4.40 specification section 6.2 ("Selection"):
4838 * "The type of the init-expression value in a switch statement must
4839 * be a scalar int or uint. The type of the constant-expression value
4840 * in a case label also must be a scalar int or uint. When any pair
4841 * of these values is tested for "equal value" and the types do not
4842 * match, an implicit conversion will be done to convert the int to a
4843 * uint (see section 4.1.10 “Implicit Conversions”) before the compare
4846 if (label_const
->type
!= state
->switch_state
.test_var
->type
) {
4847 YYLTYPE loc
= this->test_value
->get_location();
4849 const glsl_type
*type_a
= label_const
->type
;
4850 const glsl_type
*type_b
= state
->switch_state
.test_var
->type
;
4852 /* Check if int->uint implicit conversion is supported. */
4853 bool integer_conversion_supported
=
4854 glsl_type::int_type
->can_implicitly_convert_to(glsl_type::uint_type
,
4857 if ((!type_a
->is_integer() || !type_b
->is_integer()) ||
4858 !integer_conversion_supported
) {
4859 _mesa_glsl_error(&loc
, state
, "type mismatch with switch "
4860 "init-expression and case label (%s != %s)",
4861 type_a
->name
, type_b
->name
);
4863 /* Conversion of the case label. */
4864 if (type_a
->base_type
== GLSL_TYPE_INT
) {
4865 if (!apply_implicit_conversion(glsl_type::uint_type
,
4866 test_cond
->operands
[0], state
))
4867 _mesa_glsl_error(&loc
, state
, "implicit type conversion error");
4869 /* Conversion of the init-expression value. */
4870 if (!apply_implicit_conversion(glsl_type::uint_type
,
4871 test_cond
->operands
[1], state
))
4872 _mesa_glsl_error(&loc
, state
, "implicit type conversion error");
4877 ir_assignment
*set_fallthru_on_test
=
4878 new(ctx
) ir_assignment(deref_fallthru_var
, true_val
, test_cond
);
4880 instructions
->push_tail(set_fallthru_on_test
);
4881 } else { /* default case */
4882 if (state
->switch_state
.previous_default
) {
4883 YYLTYPE loc
= this->get_location();
4884 _mesa_glsl_error(& loc
, state
,
4885 "multiple default labels in one switch");
4887 loc
= state
->switch_state
.previous_default
->get_location();
4888 _mesa_glsl_error(& loc
, state
, "this is the first default label");
4890 state
->switch_state
.previous_default
= this;
4892 /* Set fallthru condition on 'run_default' bool. */
4893 ir_dereference_variable
*deref_run_default
=
4894 new(ctx
) ir_dereference_variable(state
->switch_state
.run_default
);
4895 ir_rvalue
*const cond_true
= new(ctx
) ir_constant(true);
4896 ir_expression
*test_cond
= new(ctx
) ir_expression(ir_binop_all_equal
,
4900 /* Set falltrhu state. */
4901 ir_assignment
*set_fallthru
=
4902 new(ctx
) ir_assignment(deref_fallthru_var
, true_val
, test_cond
);
4904 instructions
->push_tail(set_fallthru
);
4907 /* Case statements do not have r-values. */
4912 ast_iteration_statement::condition_to_hir(exec_list
*instructions
,
4913 struct _mesa_glsl_parse_state
*state
)
4917 if (condition
!= NULL
) {
4918 ir_rvalue
*const cond
=
4919 condition
->hir(instructions
, state
);
4922 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
4923 YYLTYPE loc
= condition
->get_location();
4925 _mesa_glsl_error(& loc
, state
,
4926 "loop condition must be scalar boolean");
4928 /* As the first code in the loop body, generate a block that looks
4929 * like 'if (!condition) break;' as the loop termination condition.
4931 ir_rvalue
*const not_cond
=
4932 new(ctx
) ir_expression(ir_unop_logic_not
, cond
);
4934 ir_if
*const if_stmt
= new(ctx
) ir_if(not_cond
);
4936 ir_jump
*const break_stmt
=
4937 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
4939 if_stmt
->then_instructions
.push_tail(break_stmt
);
4940 instructions
->push_tail(if_stmt
);
4947 ast_iteration_statement::hir(exec_list
*instructions
,
4948 struct _mesa_glsl_parse_state
*state
)
4952 /* For-loops and while-loops start a new scope, but do-while loops do not.
4954 if (mode
!= ast_do_while
)
4955 state
->symbols
->push_scope();
4957 if (init_statement
!= NULL
)
4958 init_statement
->hir(instructions
, state
);
4960 ir_loop
*const stmt
= new(ctx
) ir_loop();
4961 instructions
->push_tail(stmt
);
4963 /* Track the current loop nesting. */
4964 ast_iteration_statement
*nesting_ast
= state
->loop_nesting_ast
;
4966 state
->loop_nesting_ast
= this;
4968 /* Likewise, indicate that following code is closest to a loop,
4969 * NOT closest to a switch.
4971 bool saved_is_switch_innermost
= state
->switch_state
.is_switch_innermost
;
4972 state
->switch_state
.is_switch_innermost
= false;
4974 if (mode
!= ast_do_while
)
4975 condition_to_hir(&stmt
->body_instructions
, state
);
4978 body
->hir(& stmt
->body_instructions
, state
);
4980 if (rest_expression
!= NULL
)
4981 rest_expression
->hir(& stmt
->body_instructions
, state
);
4983 if (mode
== ast_do_while
)
4984 condition_to_hir(&stmt
->body_instructions
, state
);
4986 if (mode
!= ast_do_while
)
4987 state
->symbols
->pop_scope();
4989 /* Restore previous nesting before returning. */
4990 state
->loop_nesting_ast
= nesting_ast
;
4991 state
->switch_state
.is_switch_innermost
= saved_is_switch_innermost
;
4993 /* Loops do not have r-values.
5000 * Determine if the given type is valid for establishing a default precision
5003 * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
5005 * "The precision statement
5007 * precision precision-qualifier type;
5009 * can be used to establish a default precision qualifier. The type field
5010 * can be either int or float or any of the sampler types, and the
5011 * precision-qualifier can be lowp, mediump, or highp."
5013 * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
5014 * qualifiers on sampler types, but this seems like an oversight (since the
5015 * intention of including these in GLSL 1.30 is to allow compatibility with ES
5016 * shaders). So we allow int, float, and all sampler types regardless of GLSL
5020 is_valid_default_precision_type(const struct glsl_type
*const type
)
5025 switch (type
->base_type
) {
5027 case GLSL_TYPE_FLOAT
:
5028 /* "int" and "float" are valid, but vectors and matrices are not. */
5029 return type
->vector_elements
== 1 && type
->matrix_columns
== 1;
5030 case GLSL_TYPE_SAMPLER
:
5039 ast_type_specifier::hir(exec_list
*instructions
,
5040 struct _mesa_glsl_parse_state
*state
)
5042 if (this->default_precision
== ast_precision_none
&& this->structure
== NULL
)
5045 YYLTYPE loc
= this->get_location();
5047 /* If this is a precision statement, check that the type to which it is
5048 * applied is either float or int.
5050 * From section 4.5.3 of the GLSL 1.30 spec:
5051 * "The precision statement
5052 * precision precision-qualifier type;
5053 * can be used to establish a default precision qualifier. The type
5054 * field can be either int or float [...]. Any other types or
5055 * qualifiers will result in an error.
5057 if (this->default_precision
!= ast_precision_none
) {
5058 if (!state
->check_precision_qualifiers_allowed(&loc
))
5061 if (this->structure
!= NULL
) {
5062 _mesa_glsl_error(&loc
, state
,
5063 "precision qualifiers do not apply to structures");
5067 if (this->array_specifier
!= NULL
) {
5068 _mesa_glsl_error(&loc
, state
,
5069 "default precision statements do not apply to "
5074 const struct glsl_type
*const type
=
5075 state
->symbols
->get_type(this->type_name
);
5076 if (!is_valid_default_precision_type(type
)) {
5077 _mesa_glsl_error(&loc
, state
,
5078 "default precision statements apply only to "
5079 "float, int, and sampler types");
5083 if (type
->base_type
== GLSL_TYPE_FLOAT
5085 && state
->stage
== MESA_SHADER_FRAGMENT
) {
5086 /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
5089 * "The fragment language has no default precision qualifier for
5090 * floating point types."
5092 * As a result, we have to track whether or not default precision has
5093 * been specified for float in GLSL ES fragment shaders.
5095 * Earlier in that same section, the spec says:
5097 * "Non-precision qualified declarations will use the precision
5098 * qualifier specified in the most recent precision statement
5099 * that is still in scope. The precision statement has the same
5100 * scoping rules as variable declarations. If it is declared
5101 * inside a compound statement, its effect stops at the end of
5102 * the innermost statement it was declared in. Precision
5103 * statements in nested scopes override precision statements in
5104 * outer scopes. Multiple precision statements for the same basic
5105 * type can appear inside the same scope, with later statements
5106 * overriding earlier statements within that scope."
5108 * Default precision specifications follow the same scope rules as
5109 * variables. So, we can track the state of the default float
5110 * precision in the symbol table, and the rules will just work. This
5111 * is a slight abuse of the symbol table, but it has the semantics
5114 ir_variable
*const junk
=
5115 new(state
) ir_variable(type
, "#default precision",
5118 state
->symbols
->add_variable(junk
);
5121 /* FINISHME: Translate precision statements into IR. */
5125 /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
5126 * process_record_constructor() can do type-checking on C-style initializer
5127 * expressions of structs, but ast_struct_specifier should only be translated
5128 * to HIR if it is declaring the type of a structure.
5130 * The ->is_declaration field is false for initializers of variables
5131 * declared separately from the struct's type definition.
5133 * struct S { ... }; (is_declaration = true)
5134 * struct T { ... } t = { ... }; (is_declaration = true)
5135 * S s = { ... }; (is_declaration = false)
5137 if (this->structure
!= NULL
&& this->structure
->is_declaration
)
5138 return this->structure
->hir(instructions
, state
);
5145 * Process a structure or interface block tree into an array of structure fields
5147 * After parsing, where there are some syntax differnces, structures and
5148 * interface blocks are almost identical. They are similar enough that the
5149 * AST for each can be processed the same way into a set of
5150 * \c glsl_struct_field to describe the members.
5152 * If we're processing an interface block, var_mode should be the type of the
5153 * interface block (ir_var_shader_in, ir_var_shader_out, or ir_var_uniform).
5154 * If we're processing a structure, var_mode should be ir_var_auto.
5157 * The number of fields processed. A pointer to the array structure fields is
5158 * stored in \c *fields_ret.
5161 ast_process_structure_or_interface_block(exec_list
*instructions
,
5162 struct _mesa_glsl_parse_state
*state
,
5163 exec_list
*declarations
,
5165 glsl_struct_field
**fields_ret
,
5167 enum glsl_matrix_layout matrix_layout
,
5168 bool allow_reserved_names
,
5169 ir_variable_mode var_mode
)
5171 unsigned decl_count
= 0;
5173 /* Make an initial pass over the list of fields to determine how
5174 * many there are. Each element in this list is an ast_declarator_list.
5175 * This means that we actually need to count the number of elements in the
5176 * 'declarations' list in each of the elements.
5178 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
5179 decl_count
+= decl_list
->declarations
.length();
5182 /* Allocate storage for the fields and process the field
5183 * declarations. As the declarations are processed, try to also convert
5184 * the types to HIR. This ensures that structure definitions embedded in
5185 * other structure definitions or in interface blocks are processed.
5187 glsl_struct_field
*const fields
= ralloc_array(state
, glsl_struct_field
,
5191 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
5192 const char *type_name
;
5194 decl_list
->type
->specifier
->hir(instructions
, state
);
5196 /* Section 10.9 of the GLSL ES 1.00 specification states that
5197 * embedded structure definitions have been removed from the language.
5199 if (state
->es_shader
&& decl_list
->type
->specifier
->structure
!= NULL
) {
5200 _mesa_glsl_error(&loc
, state
, "embedded structure definitions are "
5201 "not allowed in GLSL ES 1.00");
5204 const glsl_type
*decl_type
=
5205 decl_list
->type
->glsl_type(& type_name
, state
);
5207 foreach_list_typed (ast_declaration
, decl
, link
,
5208 &decl_list
->declarations
) {
5209 if (!allow_reserved_names
)
5210 validate_identifier(decl
->identifier
, loc
, state
);
5212 /* From section 4.3.9 of the GLSL 4.40 spec:
5214 * "[In interface blocks] opaque types are not allowed."
5216 * It should be impossible for decl_type to be NULL here. Cases that
5217 * might naturally lead to decl_type being NULL, especially for the
5218 * is_interface case, will have resulted in compilation having
5219 * already halted due to a syntax error.
5221 const struct glsl_type
*field_type
=
5222 decl_type
!= NULL
? decl_type
: glsl_type::error_type
;
5224 if (is_interface
&& field_type
->contains_opaque()) {
5225 YYLTYPE loc
= decl_list
->get_location();
5226 _mesa_glsl_error(&loc
, state
,
5227 "uniform in non-default uniform block contains "
5231 if (field_type
->contains_atomic()) {
5232 /* FINISHME: Add a spec quotation here once updated spec
5233 * FINISHME: language is available. See Khronos bug #10903
5234 * FINISHME: on whether atomic counters are allowed in
5235 * FINISHME: structures.
5237 YYLTYPE loc
= decl_list
->get_location();
5238 _mesa_glsl_error(&loc
, state
, "atomic counter in structure or "
5242 if (field_type
->contains_image()) {
5243 /* FINISHME: Same problem as with atomic counters.
5244 * FINISHME: Request clarification from Khronos and add
5245 * FINISHME: spec quotation here.
5247 YYLTYPE loc
= decl_list
->get_location();
5248 _mesa_glsl_error(&loc
, state
,
5249 "image in structure or uniform block");
5252 const struct ast_type_qualifier
*const qual
=
5253 & decl_list
->type
->qualifier
;
5254 if (qual
->flags
.q
.std140
||
5255 qual
->flags
.q
.packed
||
5256 qual
->flags
.q
.shared
) {
5257 _mesa_glsl_error(&loc
, state
,
5258 "uniform block layout qualifiers std140, packed, and "
5259 "shared can only be applied to uniform blocks, not "
5263 if (qual
->flags
.q
.constant
) {
5264 YYLTYPE loc
= decl_list
->get_location();
5265 _mesa_glsl_error(&loc
, state
,
5266 "const storage qualifier cannot be applied "
5267 "to struct or interface block members");
5270 field_type
= process_array_type(&loc
, decl_type
,
5271 decl
->array_specifier
, state
);
5272 fields
[i
].type
= field_type
;
5273 fields
[i
].name
= decl
->identifier
;
5274 fields
[i
].location
= -1;
5275 fields
[i
].interpolation
=
5276 interpret_interpolation_qualifier(qual
, var_mode
, state
, &loc
);
5277 fields
[i
].centroid
= qual
->flags
.q
.centroid
? 1 : 0;
5278 fields
[i
].sample
= qual
->flags
.q
.sample
? 1 : 0;
5280 /* Only save explicitly defined streams in block's field */
5281 fields
[i
].stream
= qual
->flags
.q
.explicit_stream
? qual
->stream
: -1;
5283 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
5284 if (!qual
->flags
.q
.uniform
) {
5285 _mesa_glsl_error(&loc
, state
,
5286 "row_major and column_major can only be "
5287 "applied to uniform interface blocks");
5289 validate_matrix_layout_for_type(state
, &loc
, field_type
, NULL
);
5292 if (qual
->flags
.q
.uniform
&& qual
->has_interpolation()) {
5293 _mesa_glsl_error(&loc
, state
,
5294 "interpolation qualifiers cannot be used "
5295 "with uniform interface blocks");
5298 if ((qual
->flags
.q
.uniform
|| !is_interface
) &&
5299 qual
->has_auxiliary_storage()) {
5300 _mesa_glsl_error(&loc
, state
,
5301 "auxiliary storage qualifiers cannot be used "
5302 "in uniform blocks or structures.");
5305 /* Propogate row- / column-major information down the fields of the
5306 * structure or interface block. Structures need this data because
5307 * the structure may contain a structure that contains ... a matrix
5308 * that need the proper layout.
5310 if (field_type
->without_array()->is_matrix()
5311 || field_type
->without_array()->is_record()) {
5312 /* If no layout is specified for the field, inherit the layout
5315 fields
[i
].matrix_layout
= matrix_layout
;
5317 if (qual
->flags
.q
.row_major
)
5318 fields
[i
].matrix_layout
= GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
5319 else if (qual
->flags
.q
.column_major
)
5320 fields
[i
].matrix_layout
= GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
;
5322 /* If we're processing an interface block, the matrix layout must
5323 * be decided by this point.
5325 assert(!is_interface
5326 || fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
5327 || fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
);
5334 assert(i
== decl_count
);
5336 *fields_ret
= fields
;
5342 ast_struct_specifier::hir(exec_list
*instructions
,
5343 struct _mesa_glsl_parse_state
*state
)
5345 YYLTYPE loc
= this->get_location();
5347 /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
5349 * "Anonymous structures are not supported; so embedded structures must
5350 * have a declarator. A name given to an embedded struct is scoped at
5351 * the same level as the struct it is embedded in."
5353 * The same section of the GLSL 1.20 spec says:
5355 * "Anonymous structures are not supported. Embedded structures are not
5358 * struct S { float f; };
5360 * S; // Error: anonymous structures disallowed
5361 * struct { ... }; // Error: embedded structures disallowed
5362 * S s; // Okay: nested structures with name are allowed
5365 * The GLSL ES 1.00 and 3.00 specs have similar langauge and examples. So,
5366 * we allow embedded structures in 1.10 only.
5368 if (state
->language_version
!= 110 && state
->struct_specifier_depth
!= 0)
5369 _mesa_glsl_error(&loc
, state
,
5370 "embedded structure declarations are not allowed");
5372 state
->struct_specifier_depth
++;
5374 glsl_struct_field
*fields
;
5375 unsigned decl_count
=
5376 ast_process_structure_or_interface_block(instructions
,
5378 &this->declarations
,
5382 GLSL_MATRIX_LAYOUT_INHERITED
,
5383 false /* allow_reserved_names */,
5386 validate_identifier(this->name
, loc
, state
);
5388 const glsl_type
*t
=
5389 glsl_type::get_record_instance(fields
, decl_count
, this->name
);
5391 if (!state
->symbols
->add_type(name
, t
)) {
5392 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
5394 const glsl_type
**s
= reralloc(state
, state
->user_structures
,
5396 state
->num_user_structures
+ 1);
5398 s
[state
->num_user_structures
] = t
;
5399 state
->user_structures
= s
;
5400 state
->num_user_structures
++;
5404 state
->struct_specifier_depth
--;
5406 /* Structure type definitions do not have r-values.
5413 * Visitor class which detects whether a given interface block has been used.
5415 class interface_block_usage_visitor
: public ir_hierarchical_visitor
5418 interface_block_usage_visitor(ir_variable_mode mode
, const glsl_type
*block
)
5419 : mode(mode
), block(block
), found(false)
5423 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
5425 if (ir
->var
->data
.mode
== mode
&& ir
->var
->get_interface_type() == block
) {
5429 return visit_continue
;
5432 bool usage_found() const
5438 ir_variable_mode mode
;
5439 const glsl_type
*block
;
5445 ast_interface_block::hir(exec_list
*instructions
,
5446 struct _mesa_glsl_parse_state
*state
)
5448 YYLTYPE loc
= this->get_location();
5450 /* Interface blocks must be declared at global scope */
5451 if (state
->current_function
!= NULL
) {
5452 _mesa_glsl_error(&loc
, state
,
5453 "Interface block `%s' must be declared "
5458 /* The ast_interface_block has a list of ast_declarator_lists. We
5459 * need to turn those into ir_variables with an association
5460 * with this uniform block.
5462 enum glsl_interface_packing packing
;
5463 if (this->layout
.flags
.q
.shared
) {
5464 packing
= GLSL_INTERFACE_PACKING_SHARED
;
5465 } else if (this->layout
.flags
.q
.packed
) {
5466 packing
= GLSL_INTERFACE_PACKING_PACKED
;
5468 /* The default layout is std140.
5470 packing
= GLSL_INTERFACE_PACKING_STD140
;
5473 ir_variable_mode var_mode
;
5474 const char *iface_type_name
;
5475 if (this->layout
.flags
.q
.in
) {
5476 var_mode
= ir_var_shader_in
;
5477 iface_type_name
= "in";
5478 } else if (this->layout
.flags
.q
.out
) {
5479 var_mode
= ir_var_shader_out
;
5480 iface_type_name
= "out";
5481 } else if (this->layout
.flags
.q
.uniform
) {
5482 var_mode
= ir_var_uniform
;
5483 iface_type_name
= "uniform";
5485 var_mode
= ir_var_auto
;
5486 iface_type_name
= "UNKNOWN";
5487 assert(!"interface block layout qualifier not found!");
5490 enum glsl_matrix_layout matrix_layout
= GLSL_MATRIX_LAYOUT_INHERITED
;
5491 if (this->layout
.flags
.q
.row_major
)
5492 matrix_layout
= GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
5493 else if (this->layout
.flags
.q
.column_major
)
5494 matrix_layout
= GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
;
5496 bool redeclaring_per_vertex
= strcmp(this->block_name
, "gl_PerVertex") == 0;
5497 exec_list declared_variables
;
5498 glsl_struct_field
*fields
;
5500 /* Treat an interface block as one level of nesting, so that embedded struct
5501 * specifiers will be disallowed.
5503 state
->struct_specifier_depth
++;
5505 unsigned int num_variables
=
5506 ast_process_structure_or_interface_block(&declared_variables
,
5508 &this->declarations
,
5513 redeclaring_per_vertex
,
5516 state
->struct_specifier_depth
--;
5518 if (!redeclaring_per_vertex
) {
5519 validate_identifier(this->block_name
, loc
, state
);
5521 /* From section 4.3.9 ("Interface Blocks") of the GLSL 4.50 spec:
5523 * "Block names have no other use within a shader beyond interface
5524 * matching; it is a compile-time error to use a block name at global
5525 * scope for anything other than as a block name."
5527 ir_variable
*var
= state
->symbols
->get_variable(this->block_name
);
5528 if (var
&& !var
->type
->is_interface()) {
5529 _mesa_glsl_error(&loc
, state
, "Block name `%s' is "
5530 "already used in the scope.",
5535 const glsl_type
*earlier_per_vertex
= NULL
;
5536 if (redeclaring_per_vertex
) {
5537 /* Find the previous declaration of gl_PerVertex. If we're redeclaring
5538 * the named interface block gl_in, we can find it by looking at the
5539 * previous declaration of gl_in. Otherwise we can find it by looking
5540 * at the previous decalartion of any of the built-in outputs,
5543 * Also check that the instance name and array-ness of the redeclaration
5547 case ir_var_shader_in
:
5548 if (ir_variable
*earlier_gl_in
=
5549 state
->symbols
->get_variable("gl_in")) {
5550 earlier_per_vertex
= earlier_gl_in
->get_interface_type();
5552 _mesa_glsl_error(&loc
, state
,
5553 "redeclaration of gl_PerVertex input not allowed "
5555 _mesa_shader_stage_to_string(state
->stage
));
5557 if (this->instance_name
== NULL
||
5558 strcmp(this->instance_name
, "gl_in") != 0 || this->array_specifier
== NULL
) {
5559 _mesa_glsl_error(&loc
, state
,
5560 "gl_PerVertex input must be redeclared as "
5564 case ir_var_shader_out
:
5565 if (ir_variable
*earlier_gl_Position
=
5566 state
->symbols
->get_variable("gl_Position")) {
5567 earlier_per_vertex
= earlier_gl_Position
->get_interface_type();
5569 _mesa_glsl_error(&loc
, state
,
5570 "redeclaration of gl_PerVertex output not "
5571 "allowed in the %s shader",
5572 _mesa_shader_stage_to_string(state
->stage
));
5574 if (this->instance_name
!= NULL
) {
5575 _mesa_glsl_error(&loc
, state
,
5576 "gl_PerVertex output may not be redeclared with "
5577 "an instance name");
5581 _mesa_glsl_error(&loc
, state
,
5582 "gl_PerVertex must be declared as an input or an "
5587 if (earlier_per_vertex
== NULL
) {
5588 /* An error has already been reported. Bail out to avoid null
5589 * dereferences later in this function.
5594 /* Copy locations from the old gl_PerVertex interface block. */
5595 for (unsigned i
= 0; i
< num_variables
; i
++) {
5596 int j
= earlier_per_vertex
->field_index(fields
[i
].name
);
5598 _mesa_glsl_error(&loc
, state
,
5599 "redeclaration of gl_PerVertex must be a subset "
5600 "of the built-in members of gl_PerVertex");
5602 fields
[i
].location
=
5603 earlier_per_vertex
->fields
.structure
[j
].location
;
5604 fields
[i
].interpolation
=
5605 earlier_per_vertex
->fields
.structure
[j
].interpolation
;
5606 fields
[i
].centroid
=
5607 earlier_per_vertex
->fields
.structure
[j
].centroid
;
5609 earlier_per_vertex
->fields
.structure
[j
].sample
;
5613 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10
5616 * If a built-in interface block is redeclared, it must appear in
5617 * the shader before any use of any member included in the built-in
5618 * declaration, or a compilation error will result.
5620 * This appears to be a clarification to the behaviour established for
5621 * gl_PerVertex by GLSL 1.50, therefore we implement this behaviour
5622 * regardless of GLSL version.
5624 interface_block_usage_visitor
v(var_mode
, earlier_per_vertex
);
5625 v
.run(instructions
);
5626 if (v
.usage_found()) {
5627 _mesa_glsl_error(&loc
, state
,
5628 "redeclaration of a built-in interface block must "
5629 "appear before any use of any member of the "
5634 const glsl_type
*block_type
=
5635 glsl_type::get_interface_instance(fields
,
5640 if (!state
->symbols
->add_interface(block_type
->name
, block_type
, var_mode
)) {
5641 YYLTYPE loc
= this->get_location();
5642 _mesa_glsl_error(&loc
, state
, "interface block `%s' with type `%s' "
5643 "already taken in the current scope",
5644 this->block_name
, iface_type_name
);
5647 /* Since interface blocks cannot contain statements, it should be
5648 * impossible for the block to generate any instructions.
5650 assert(declared_variables
.is_empty());
5652 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
5654 * Geometry shader input variables get the per-vertex values written
5655 * out by vertex shader output variables of the same names. Since a
5656 * geometry shader operates on a set of vertices, each input varying
5657 * variable (or input block, see interface blocks below) needs to be
5658 * declared as an array.
5660 if (state
->stage
== MESA_SHADER_GEOMETRY
&& this->array_specifier
== NULL
&&
5661 var_mode
== ir_var_shader_in
) {
5662 _mesa_glsl_error(&loc
, state
, "geometry shader inputs must be arrays");
5665 /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
5668 * "If an instance name (instance-name) is used, then it puts all the
5669 * members inside a scope within its own name space, accessed with the
5670 * field selector ( . ) operator (analogously to structures)."
5672 if (this->instance_name
) {
5673 if (redeclaring_per_vertex
) {
5674 /* When a built-in in an unnamed interface block is redeclared,
5675 * get_variable_being_redeclared() calls
5676 * check_builtin_array_max_size() to make sure that built-in array
5677 * variables aren't redeclared to illegal sizes. But we're looking
5678 * at a redeclaration of a named built-in interface block. So we
5679 * have to manually call check_builtin_array_max_size() for all parts
5680 * of the interface that are arrays.
5682 for (unsigned i
= 0; i
< num_variables
; i
++) {
5683 if (fields
[i
].type
->is_array()) {
5684 const unsigned size
= fields
[i
].type
->array_size();
5685 check_builtin_array_max_size(fields
[i
].name
, size
, loc
, state
);
5689 validate_identifier(this->instance_name
, loc
, state
);
5694 if (this->array_specifier
!= NULL
) {
5695 /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
5697 * For uniform blocks declared an array, each individual array
5698 * element corresponds to a separate buffer object backing one
5699 * instance of the block. As the array size indicates the number
5700 * of buffer objects needed, uniform block array declarations
5701 * must specify an array size.
5703 * And a few paragraphs later:
5705 * Geometry shader input blocks must be declared as arrays and
5706 * follow the array declaration and linking rules for all
5707 * geometry shader inputs. All other input and output block
5708 * arrays must specify an array size.
5710 * The upshot of this is that the only circumstance where an
5711 * interface array size *doesn't* need to be specified is on a
5712 * geometry shader input.
5714 if (this->array_specifier
->is_unsized_array
&&
5715 (state
->stage
!= MESA_SHADER_GEOMETRY
|| !this->layout
.flags
.q
.in
)) {
5716 _mesa_glsl_error(&loc
, state
,
5717 "only geometry shader inputs may be unsized "
5718 "instance block arrays");
5722 const glsl_type
*block_array_type
=
5723 process_array_type(&loc
, block_type
, this->array_specifier
, state
);
5725 var
= new(state
) ir_variable(block_array_type
,
5726 this->instance_name
,
5729 var
= new(state
) ir_variable(block_type
,
5730 this->instance_name
,
5734 var
->data
.matrix_layout
= matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
5735 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
: matrix_layout
;
5737 if (var_mode
== ir_var_shader_in
|| var_mode
== ir_var_uniform
)
5738 var
->data
.read_only
= true;
5740 if (state
->stage
== MESA_SHADER_GEOMETRY
&& var_mode
== ir_var_shader_in
)
5741 handle_geometry_shader_input_decl(state
, loc
, var
);
5743 if (ir_variable
*earlier
=
5744 state
->symbols
->get_variable(this->instance_name
)) {
5745 if (!redeclaring_per_vertex
) {
5746 _mesa_glsl_error(&loc
, state
, "`%s' redeclared",
5747 this->instance_name
);
5749 earlier
->data
.how_declared
= ir_var_declared_normally
;
5750 earlier
->type
= var
->type
;
5751 earlier
->reinit_interface_type(block_type
);
5754 /* Propagate the "binding" keyword into this UBO's fields;
5755 * the UBO declaration itself doesn't get an ir_variable unless it
5756 * has an instance name. This is ugly.
5758 var
->data
.explicit_binding
= this->layout
.flags
.q
.explicit_binding
;
5759 var
->data
.binding
= this->layout
.binding
;
5761 state
->symbols
->add_variable(var
);
5762 instructions
->push_tail(var
);
5765 /* In order to have an array size, the block must also be declared with
5768 assert(this->array_specifier
== NULL
);
5770 for (unsigned i
= 0; i
< num_variables
; i
++) {
5772 new(state
) ir_variable(fields
[i
].type
,
5773 ralloc_strdup(state
, fields
[i
].name
),
5775 var
->data
.interpolation
= fields
[i
].interpolation
;
5776 var
->data
.centroid
= fields
[i
].centroid
;
5777 var
->data
.sample
= fields
[i
].sample
;
5778 var
->init_interface_type(block_type
);
5780 if (var_mode
== ir_var_shader_in
|| var_mode
== ir_var_uniform
)
5781 var
->data
.read_only
= true;
5783 if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
) {
5784 var
->data
.matrix_layout
= matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
5785 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
: matrix_layout
;
5787 var
->data
.matrix_layout
= fields
[i
].matrix_layout
;
5790 if (fields
[i
].stream
!= -1 &&
5791 ((unsigned)fields
[i
].stream
) != this->layout
.stream
) {
5792 _mesa_glsl_error(&loc
, state
,
5793 "stream layout qualifier on "
5794 "interface block member `%s' does not match "
5795 "the interface block (%d vs %d)",
5796 var
->name
, fields
[i
].stream
, this->layout
.stream
);
5799 var
->data
.stream
= this->layout
.stream
;
5801 /* Examine var name here since var may get deleted in the next call */
5802 bool var_is_gl_id
= is_gl_identifier(var
->name
);
5804 if (redeclaring_per_vertex
) {
5805 ir_variable
*earlier
=
5806 get_variable_being_redeclared(var
, loc
, state
,
5807 true /* allow_all_redeclarations */);
5808 if (!var_is_gl_id
|| earlier
== NULL
) {
5809 _mesa_glsl_error(&loc
, state
,
5810 "redeclaration of gl_PerVertex can only "
5811 "include built-in variables");
5812 } else if (earlier
->data
.how_declared
== ir_var_declared_normally
) {
5813 _mesa_glsl_error(&loc
, state
,
5814 "`%s' has already been redeclared",
5817 earlier
->data
.how_declared
= ir_var_declared_in_block
;
5818 earlier
->reinit_interface_type(block_type
);
5823 if (state
->symbols
->get_variable(var
->name
) != NULL
)
5824 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
5826 /* Propagate the "binding" keyword into this UBO's fields;
5827 * the UBO declaration itself doesn't get an ir_variable unless it
5828 * has an instance name. This is ugly.
5830 var
->data
.explicit_binding
= this->layout
.flags
.q
.explicit_binding
;
5831 var
->data
.binding
= this->layout
.binding
;
5833 state
->symbols
->add_variable(var
);
5834 instructions
->push_tail(var
);
5837 if (redeclaring_per_vertex
&& block_type
!= earlier_per_vertex
) {
5838 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec:
5840 * It is also a compilation error ... to redeclare a built-in
5841 * block and then use a member from that built-in block that was
5842 * not included in the redeclaration.
5844 * This appears to be a clarification to the behaviour established
5845 * for gl_PerVertex by GLSL 1.50, therefore we implement this
5846 * behaviour regardless of GLSL version.
5848 * To prevent the shader from using a member that was not included in
5849 * the redeclaration, we disable any ir_variables that are still
5850 * associated with the old declaration of gl_PerVertex (since we've
5851 * already updated all of the variables contained in the new
5852 * gl_PerVertex to point to it).
5854 * As a side effect this will prevent
5855 * validate_intrastage_interface_blocks() from getting confused and
5856 * thinking there are conflicting definitions of gl_PerVertex in the
5859 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
5860 ir_variable
*const var
= node
->as_variable();
5862 var
->get_interface_type() == earlier_per_vertex
&&
5863 var
->data
.mode
== var_mode
) {
5864 if (var
->data
.how_declared
== ir_var_declared_normally
) {
5865 _mesa_glsl_error(&loc
, state
,
5866 "redeclaration of gl_PerVertex cannot "
5867 "follow a redeclaration of `%s'",
5870 state
->symbols
->disable_variable(var
->name
);
5882 ast_gs_input_layout::hir(exec_list
*instructions
,
5883 struct _mesa_glsl_parse_state
*state
)
5885 YYLTYPE loc
= this->get_location();
5887 /* If any geometry input layout declaration preceded this one, make sure it
5888 * was consistent with this one.
5890 if (state
->gs_input_prim_type_specified
&&
5891 state
->in_qualifier
->prim_type
!= this->prim_type
) {
5892 _mesa_glsl_error(&loc
, state
,
5893 "geometry shader input layout does not match"
5894 " previous declaration");
5898 /* If any shader inputs occurred before this declaration and specified an
5899 * array size, make sure the size they specified is consistent with the
5902 unsigned num_vertices
= vertices_per_prim(this->prim_type
);
5903 if (state
->gs_input_size
!= 0 && state
->gs_input_size
!= num_vertices
) {
5904 _mesa_glsl_error(&loc
, state
,
5905 "this geometry shader input layout implies %u vertices"
5906 " per primitive, but a previous input is declared"
5907 " with size %u", num_vertices
, state
->gs_input_size
);
5911 state
->gs_input_prim_type_specified
= true;
5913 /* If any shader inputs occurred before this declaration and did not
5914 * specify an array size, their size is determined now.
5916 foreach_in_list(ir_instruction
, node
, instructions
) {
5917 ir_variable
*var
= node
->as_variable();
5918 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_in
)
5921 /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
5925 if (var
->type
->is_unsized_array()) {
5926 if (var
->data
.max_array_access
>= num_vertices
) {
5927 _mesa_glsl_error(&loc
, state
,
5928 "this geometry shader input layout implies %u"
5929 " vertices, but an access to element %u of input"
5930 " `%s' already exists", num_vertices
,
5931 var
->data
.max_array_access
, var
->name
);
5933 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
5944 ast_cs_input_layout::hir(exec_list
*instructions
,
5945 struct _mesa_glsl_parse_state
*state
)
5947 YYLTYPE loc
= this->get_location();
5949 /* If any compute input layout declaration preceded this one, make sure it
5950 * was consistent with this one.
5952 if (state
->cs_input_local_size_specified
) {
5953 for (int i
= 0; i
< 3; i
++) {
5954 if (state
->cs_input_local_size
[i
] != this->local_size
[i
]) {
5955 _mesa_glsl_error(&loc
, state
,
5956 "compute shader input layout does not match"
5957 " previous declaration");
5963 /* From the ARB_compute_shader specification:
5965 * If the local size of the shader in any dimension is greater
5966 * than the maximum size supported by the implementation for that
5967 * dimension, a compile-time error results.
5969 * It is not clear from the spec how the error should be reported if
5970 * the total size of the work group exceeds
5971 * MAX_COMPUTE_WORK_GROUP_INVOCATIONS, but it seems reasonable to
5972 * report it at compile time as well.
5974 GLuint64 total_invocations
= 1;
5975 for (int i
= 0; i
< 3; i
++) {
5976 if (this->local_size
[i
] > state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]) {
5977 _mesa_glsl_error(&loc
, state
,
5978 "local_size_%c exceeds MAX_COMPUTE_WORK_GROUP_SIZE"
5980 state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]);
5983 total_invocations
*= this->local_size
[i
];
5984 if (total_invocations
>
5985 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
) {
5986 _mesa_glsl_error(&loc
, state
,
5987 "product of local_sizes exceeds "
5988 "MAX_COMPUTE_WORK_GROUP_INVOCATIONS (%d)",
5989 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
);
5994 state
->cs_input_local_size_specified
= true;
5995 for (int i
= 0; i
< 3; i
++)
5996 state
->cs_input_local_size
[i
] = this->local_size
[i
];
5998 /* We may now declare the built-in constant gl_WorkGroupSize (see
5999 * builtin_variable_generator::generate_constants() for why we didn't
6000 * declare it earlier).
6002 ir_variable
*var
= new(state
->symbols
)
6003 ir_variable(glsl_type::uvec3_type
, "gl_WorkGroupSize", ir_var_auto
);
6004 var
->data
.how_declared
= ir_var_declared_implicitly
;
6005 var
->data
.read_only
= true;
6006 instructions
->push_tail(var
);
6007 state
->symbols
->add_variable(var
);
6008 ir_constant_data data
;
6009 memset(&data
, 0, sizeof(data
));
6010 for (int i
= 0; i
< 3; i
++)
6011 data
.u
[i
] = this->local_size
[i
];
6012 var
->constant_value
= new(var
) ir_constant(glsl_type::uvec3_type
, &data
);
6013 var
->constant_initializer
=
6014 new(var
) ir_constant(glsl_type::uvec3_type
, &data
);
6015 var
->data
.has_initializer
= true;
6022 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
6023 exec_list
*instructions
)
6025 bool gl_FragColor_assigned
= false;
6026 bool gl_FragData_assigned
= false;
6027 bool user_defined_fs_output_assigned
= false;
6028 ir_variable
*user_defined_fs_output
= NULL
;
6030 /* It would be nice to have proper location information. */
6032 memset(&loc
, 0, sizeof(loc
));
6034 foreach_in_list(ir_instruction
, node
, instructions
) {
6035 ir_variable
*var
= node
->as_variable();
6037 if (!var
|| !var
->data
.assigned
)
6040 if (strcmp(var
->name
, "gl_FragColor") == 0)
6041 gl_FragColor_assigned
= true;
6042 else if (strcmp(var
->name
, "gl_FragData") == 0)
6043 gl_FragData_assigned
= true;
6044 else if (!is_gl_identifier(var
->name
)) {
6045 if (state
->stage
== MESA_SHADER_FRAGMENT
&&
6046 var
->data
.mode
== ir_var_shader_out
) {
6047 user_defined_fs_output_assigned
= true;
6048 user_defined_fs_output
= var
;
6053 /* From the GLSL 1.30 spec:
6055 * "If a shader statically assigns a value to gl_FragColor, it
6056 * may not assign a value to any element of gl_FragData. If a
6057 * shader statically writes a value to any element of
6058 * gl_FragData, it may not assign a value to
6059 * gl_FragColor. That is, a shader may assign values to either
6060 * gl_FragColor or gl_FragData, but not both. Multiple shaders
6061 * linked together must also consistently write just one of
6062 * these variables. Similarly, if user declared output
6063 * variables are in use (statically assigned to), then the
6064 * built-in variables gl_FragColor and gl_FragData may not be
6065 * assigned to. These incorrect usages all generate compile
6068 if (gl_FragColor_assigned
&& gl_FragData_assigned
) {
6069 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
6070 "`gl_FragColor' and `gl_FragData'");
6071 } else if (gl_FragColor_assigned
&& user_defined_fs_output_assigned
) {
6072 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
6073 "`gl_FragColor' and `%s'",
6074 user_defined_fs_output
->name
);
6075 } else if (gl_FragData_assigned
&& user_defined_fs_output_assigned
) {
6076 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
6077 "`gl_FragData' and `%s'",
6078 user_defined_fs_output
->name
);
6084 remove_per_vertex_blocks(exec_list
*instructions
,
6085 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
)
6087 /* Find the gl_PerVertex interface block of the appropriate (in/out) mode,
6088 * if it exists in this shader type.
6090 const glsl_type
*per_vertex
= NULL
;
6092 case ir_var_shader_in
:
6093 if (ir_variable
*gl_in
= state
->symbols
->get_variable("gl_in"))
6094 per_vertex
= gl_in
->get_interface_type();
6096 case ir_var_shader_out
:
6097 if (ir_variable
*gl_Position
=
6098 state
->symbols
->get_variable("gl_Position")) {
6099 per_vertex
= gl_Position
->get_interface_type();
6103 assert(!"Unexpected mode");
6107 /* If we didn't find a built-in gl_PerVertex interface block, then we don't
6108 * need to do anything.
6110 if (per_vertex
== NULL
)
6113 /* If the interface block is used by the shader, then we don't need to do
6116 interface_block_usage_visitor
v(mode
, per_vertex
);
6117 v
.run(instructions
);
6118 if (v
.usage_found())
6121 /* Remove any ir_variable declarations that refer to the interface block
6124 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
6125 ir_variable
*const var
= node
->as_variable();
6126 if (var
!= NULL
&& var
->get_interface_type() == per_vertex
&&
6127 var
->data
.mode
== mode
) {
6128 state
->symbols
->disable_variable(var
->name
);