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
->tcs_output_vertices_specified
= false;
83 state
->cs_input_local_size_specified
= false;
85 /* Section 4.2 of the GLSL 1.20 specification states:
86 * "The built-in functions are scoped in a scope outside the global scope
87 * users declare global variables in. That is, a shader's global scope,
88 * available for user-defined functions and global variables, is nested
89 * inside the scope containing the built-in functions."
91 * Since built-in functions like ftransform() access built-in variables,
92 * it follows that those must be in the outer scope as well.
94 * We push scope here to create this nesting effect...but don't pop.
95 * This way, a shader's globals are still in the symbol table for use
98 state
->symbols
->push_scope();
100 foreach_list_typed (ast_node
, ast
, link
, & state
->translation_unit
)
101 ast
->hir(instructions
, state
);
103 detect_recursion_unlinked(state
, instructions
);
104 detect_conflicting_assignments(state
, instructions
);
106 state
->toplevel_ir
= NULL
;
108 /* Move all of the variable declarations to the front of the IR list, and
109 * reverse the order. This has the (intended!) side effect that vertex
110 * shader inputs and fragment shader outputs will appear in the IR in the
111 * same order that they appeared in the shader code. This results in the
112 * locations being assigned in the declared order. Many (arguably buggy)
113 * applications depend on this behavior, and it matches what nearly all
116 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
117 ir_variable
*const var
= node
->as_variable();
123 instructions
->push_head(var
);
126 /* Figure out if gl_FragCoord is actually used in fragment shader */
127 ir_variable
*const var
= state
->symbols
->get_variable("gl_FragCoord");
129 state
->fs_uses_gl_fragcoord
= var
->data
.used
;
131 /* From section 7.1 (Built-In Language Variables) of the GLSL 4.10 spec:
133 * If multiple shaders using members of a built-in block belonging to
134 * the same interface are linked together in the same program, they
135 * must all redeclare the built-in block in the same way, as described
136 * in section 4.3.7 "Interface Blocks" for interface block matching, or
137 * a link error will result.
139 * The phrase "using members of a built-in block" implies that if two
140 * shaders are linked together and one of them *does not use* any members
141 * of the built-in block, then that shader does not need to have a matching
142 * redeclaration of the built-in block.
144 * This appears to be a clarification to the behaviour established for
145 * gl_PerVertex by GLSL 1.50, therefore implement it regardless of GLSL
148 * The definition of "interface" in section 4.3.7 that applies here is as
151 * The boundary between adjacent programmable pipeline stages: This
152 * spans all the outputs in all compilation units of the first stage
153 * and all the inputs in all compilation units of the second stage.
155 * Therefore this rule applies to both inter- and intra-stage linking.
157 * The easiest way to implement this is to check whether the shader uses
158 * gl_PerVertex right after ast-to-ir conversion, and if it doesn't, simply
159 * remove all the relevant variable declaration from the IR, so that the
160 * linker won't see them and complain about mismatches.
162 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_in
);
163 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_out
);
167 static ir_expression_operation
168 get_conversion_operation(const glsl_type
*to
, const glsl_type
*from
,
169 struct _mesa_glsl_parse_state
*state
)
171 switch (to
->base_type
) {
172 case GLSL_TYPE_FLOAT
:
173 switch (from
->base_type
) {
174 case GLSL_TYPE_INT
: return ir_unop_i2f
;
175 case GLSL_TYPE_UINT
: return ir_unop_u2f
;
176 case GLSL_TYPE_DOUBLE
: return ir_unop_d2f
;
177 default: return (ir_expression_operation
)0;
181 if (!state
->is_version(400, 0) && !state
->ARB_gpu_shader5_enable
)
182 return (ir_expression_operation
)0;
183 switch (from
->base_type
) {
184 case GLSL_TYPE_INT
: return ir_unop_i2u
;
185 default: return (ir_expression_operation
)0;
188 case GLSL_TYPE_DOUBLE
:
189 if (!state
->has_double())
190 return (ir_expression_operation
)0;
191 switch (from
->base_type
) {
192 case GLSL_TYPE_INT
: return ir_unop_i2d
;
193 case GLSL_TYPE_UINT
: return ir_unop_u2d
;
194 case GLSL_TYPE_FLOAT
: return ir_unop_f2d
;
195 default: return (ir_expression_operation
)0;
198 default: return (ir_expression_operation
)0;
204 * If a conversion is available, convert one operand to a different type
206 * The \c from \c ir_rvalue is converted "in place".
208 * \param to Type that the operand it to be converted to
209 * \param from Operand that is being converted
210 * \param state GLSL compiler state
213 * If a conversion is possible (or unnecessary), \c true is returned.
214 * Otherwise \c false is returned.
217 apply_implicit_conversion(const glsl_type
*to
, ir_rvalue
* &from
,
218 struct _mesa_glsl_parse_state
*state
)
221 if (to
->base_type
== from
->type
->base_type
)
224 /* Prior to GLSL 1.20, there are no implicit conversions */
225 if (!state
->is_version(120, 0))
228 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
230 * "There are no implicit array or structure conversions. For
231 * example, an array of int cannot be implicitly converted to an
234 if (!to
->is_numeric() || !from
->type
->is_numeric())
237 /* We don't actually want the specific type `to`, we want a type
238 * with the same base type as `to`, but the same vector width as
241 to
= glsl_type::get_instance(to
->base_type
, from
->type
->vector_elements
,
242 from
->type
->matrix_columns
);
244 ir_expression_operation op
= get_conversion_operation(to
, from
->type
, state
);
246 from
= new(ctx
) ir_expression(op
, to
, from
, NULL
);
254 static const struct glsl_type
*
255 arithmetic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
257 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
259 const glsl_type
*type_a
= value_a
->type
;
260 const glsl_type
*type_b
= value_b
->type
;
262 /* From GLSL 1.50 spec, page 56:
264 * "The arithmetic binary operators add (+), subtract (-),
265 * multiply (*), and divide (/) operate on integer and
266 * floating-point scalars, vectors, and matrices."
268 if (!type_a
->is_numeric() || !type_b
->is_numeric()) {
269 _mesa_glsl_error(loc
, state
,
270 "operands to arithmetic operators must be numeric");
271 return glsl_type::error_type
;
275 /* "If one operand is floating-point based and the other is
276 * not, then the conversions from Section 4.1.10 "Implicit
277 * Conversions" are applied to the non-floating-point-based operand."
279 if (!apply_implicit_conversion(type_a
, value_b
, state
)
280 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
281 _mesa_glsl_error(loc
, state
,
282 "could not implicitly convert operands to "
283 "arithmetic operator");
284 return glsl_type::error_type
;
286 type_a
= value_a
->type
;
287 type_b
= value_b
->type
;
289 /* "If the operands are integer types, they must both be signed or
292 * From this rule and the preceeding conversion it can be inferred that
293 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
294 * The is_numeric check above already filtered out the case where either
295 * type is not one of these, so now the base types need only be tested for
298 if (type_a
->base_type
!= type_b
->base_type
) {
299 _mesa_glsl_error(loc
, state
,
300 "base type mismatch for arithmetic operator");
301 return glsl_type::error_type
;
304 /* "All arithmetic binary operators result in the same fundamental type
305 * (signed integer, unsigned integer, or floating-point) as the
306 * operands they operate on, after operand type conversion. After
307 * conversion, the following cases are valid
309 * * The two operands are scalars. In this case the operation is
310 * applied, resulting in a scalar."
312 if (type_a
->is_scalar() && type_b
->is_scalar())
315 /* "* One operand is a scalar, and the other is a vector or matrix.
316 * In this case, the scalar operation is applied independently to each
317 * component of the vector or matrix, resulting in the same size
320 if (type_a
->is_scalar()) {
321 if (!type_b
->is_scalar())
323 } else if (type_b
->is_scalar()) {
327 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
328 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
331 assert(!type_a
->is_scalar());
332 assert(!type_b
->is_scalar());
334 /* "* The two operands are vectors of the same size. In this case, the
335 * operation is done component-wise resulting in the same size
338 if (type_a
->is_vector() && type_b
->is_vector()) {
339 if (type_a
== type_b
) {
342 _mesa_glsl_error(loc
, state
,
343 "vector size mismatch for arithmetic operator");
344 return glsl_type::error_type
;
348 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
349 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
350 * <vector, vector> have been handled. At least one of the operands must
351 * be matrix. Further, since there are no integer matrix types, the base
352 * type of both operands must be float.
354 assert(type_a
->is_matrix() || type_b
->is_matrix());
355 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
||
356 type_a
->base_type
== GLSL_TYPE_DOUBLE
);
357 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
||
358 type_b
->base_type
== GLSL_TYPE_DOUBLE
);
360 /* "* The operator is add (+), subtract (-), or divide (/), and the
361 * operands are matrices with the same number of rows and the same
362 * number of columns. In this case, the operation is done component-
363 * wise resulting in the same size matrix."
364 * * The operator is multiply (*), where both operands are matrices or
365 * one operand is a vector and the other a matrix. A right vector
366 * operand is treated as a column vector and a left vector operand as a
367 * row vector. In all these cases, it is required that the number of
368 * columns of the left operand is equal to the number of rows of the
369 * right operand. Then, the multiply (*) operation does a linear
370 * algebraic multiply, yielding an object that has the same number of
371 * rows as the left operand and the same number of columns as the right
372 * operand. Section 5.10 "Vector and Matrix Operations" explains in
373 * more detail how vectors and matrices are operated on."
376 if (type_a
== type_b
)
379 const glsl_type
*type
= glsl_type::get_mul_type(type_a
, type_b
);
381 if (type
== glsl_type::error_type
) {
382 _mesa_glsl_error(loc
, state
,
383 "size mismatch for matrix multiplication");
390 /* "All other cases are illegal."
392 _mesa_glsl_error(loc
, state
, "type mismatch");
393 return glsl_type::error_type
;
397 static const struct glsl_type
*
398 unary_arithmetic_result_type(const struct glsl_type
*type
,
399 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
401 /* From GLSL 1.50 spec, page 57:
403 * "The arithmetic unary operators negate (-), post- and pre-increment
404 * and decrement (-- and ++) operate on integer or floating-point
405 * values (including vectors and matrices). All unary operators work
406 * component-wise on their operands. These result with the same type
409 if (!type
->is_numeric()) {
410 _mesa_glsl_error(loc
, state
,
411 "operands to arithmetic operators must be numeric");
412 return glsl_type::error_type
;
419 * \brief Return the result type of a bit-logic operation.
421 * If the given types to the bit-logic operator are invalid, return
422 * glsl_type::error_type.
424 * \param type_a Type of LHS of bit-logic op
425 * \param type_b Type of RHS of bit-logic op
427 static const struct glsl_type
*
428 bit_logic_result_type(const struct glsl_type
*type_a
,
429 const struct glsl_type
*type_b
,
431 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
433 if (!state
->check_bitwise_operations_allowed(loc
)) {
434 return glsl_type::error_type
;
437 /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
439 * "The bitwise operators and (&), exclusive-or (^), and inclusive-or
440 * (|). The operands must be of type signed or unsigned integers or
443 if (!type_a
->is_integer()) {
444 _mesa_glsl_error(loc
, state
, "LHS of `%s' must be an integer",
445 ast_expression::operator_string(op
));
446 return glsl_type::error_type
;
448 if (!type_b
->is_integer()) {
449 _mesa_glsl_error(loc
, state
, "RHS of `%s' must be an integer",
450 ast_expression::operator_string(op
));
451 return glsl_type::error_type
;
454 /* "The fundamental types of the operands (signed or unsigned) must
457 if (type_a
->base_type
!= type_b
->base_type
) {
458 _mesa_glsl_error(loc
, state
, "operands of `%s' must have the same "
459 "base type", ast_expression::operator_string(op
));
460 return glsl_type::error_type
;
463 /* "The operands cannot be vectors of differing size." */
464 if (type_a
->is_vector() &&
465 type_b
->is_vector() &&
466 type_a
->vector_elements
!= type_b
->vector_elements
) {
467 _mesa_glsl_error(loc
, state
, "operands of `%s' cannot be vectors of "
468 "different sizes", ast_expression::operator_string(op
));
469 return glsl_type::error_type
;
472 /* "If one operand is a scalar and the other a vector, the scalar is
473 * applied component-wise to the vector, resulting in the same type as
474 * the vector. The fundamental types of the operands [...] will be the
475 * resulting fundamental type."
477 if (type_a
->is_scalar())
483 static const struct glsl_type
*
484 modulus_result_type(const struct glsl_type
*type_a
,
485 const struct glsl_type
*type_b
,
486 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
488 if (!state
->check_version(130, 300, loc
, "operator '%%' is reserved")) {
489 return glsl_type::error_type
;
492 /* From GLSL 1.50 spec, page 56:
493 * "The operator modulus (%) operates on signed or unsigned integers or
494 * integer vectors. The operand types must both be signed or both be
497 if (!type_a
->is_integer()) {
498 _mesa_glsl_error(loc
, state
, "LHS of operator %% must be an integer");
499 return glsl_type::error_type
;
501 if (!type_b
->is_integer()) {
502 _mesa_glsl_error(loc
, state
, "RHS of operator %% must be an integer");
503 return glsl_type::error_type
;
505 if (type_a
->base_type
!= type_b
->base_type
) {
506 _mesa_glsl_error(loc
, state
,
507 "operands of %% must have the same base type");
508 return glsl_type::error_type
;
511 /* "The operands cannot be vectors of differing size. If one operand is
512 * a scalar and the other vector, then the scalar is applied component-
513 * wise to the vector, resulting in the same type as the vector. If both
514 * are vectors of the same size, the result is computed component-wise."
516 if (type_a
->is_vector()) {
517 if (!type_b
->is_vector()
518 || (type_a
->vector_elements
== type_b
->vector_elements
))
523 /* "The operator modulus (%) is not defined for any other data types
524 * (non-integer types)."
526 _mesa_glsl_error(loc
, state
, "type mismatch");
527 return glsl_type::error_type
;
531 static const struct glsl_type
*
532 relational_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
533 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
535 const glsl_type
*type_a
= value_a
->type
;
536 const glsl_type
*type_b
= value_b
->type
;
538 /* From GLSL 1.50 spec, page 56:
539 * "The relational operators greater than (>), less than (<), greater
540 * than or equal (>=), and less than or equal (<=) operate only on
541 * scalar integer and scalar floating-point expressions."
543 if (!type_a
->is_numeric()
544 || !type_b
->is_numeric()
545 || !type_a
->is_scalar()
546 || !type_b
->is_scalar()) {
547 _mesa_glsl_error(loc
, state
,
548 "operands to relational operators must be scalar and "
550 return glsl_type::error_type
;
553 /* "Either the operands' types must match, or the conversions from
554 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
555 * operand, after which the types must match."
557 if (!apply_implicit_conversion(type_a
, value_b
, state
)
558 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
559 _mesa_glsl_error(loc
, state
,
560 "could not implicitly convert operands to "
561 "relational operator");
562 return glsl_type::error_type
;
564 type_a
= value_a
->type
;
565 type_b
= value_b
->type
;
567 if (type_a
->base_type
!= type_b
->base_type
) {
568 _mesa_glsl_error(loc
, state
, "base type mismatch");
569 return glsl_type::error_type
;
572 /* "The result is scalar Boolean."
574 return glsl_type::bool_type
;
578 * \brief Return the result type of a bit-shift operation.
580 * If the given types to the bit-shift operator are invalid, return
581 * glsl_type::error_type.
583 * \param type_a Type of LHS of bit-shift op
584 * \param type_b Type of RHS of bit-shift op
586 static const struct glsl_type
*
587 shift_result_type(const struct glsl_type
*type_a
,
588 const struct glsl_type
*type_b
,
590 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
592 if (!state
->check_bitwise_operations_allowed(loc
)) {
593 return glsl_type::error_type
;
596 /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
598 * "The shift operators (<<) and (>>). For both operators, the operands
599 * must be signed or unsigned integers or integer vectors. One operand
600 * can be signed while the other is unsigned."
602 if (!type_a
->is_integer()) {
603 _mesa_glsl_error(loc
, state
, "LHS of operator %s must be an integer or "
604 "integer vector", ast_expression::operator_string(op
));
605 return glsl_type::error_type
;
608 if (!type_b
->is_integer()) {
609 _mesa_glsl_error(loc
, state
, "RHS of operator %s must be an integer or "
610 "integer vector", ast_expression::operator_string(op
));
611 return glsl_type::error_type
;
614 /* "If the first operand is a scalar, the second operand has to be
617 if (type_a
->is_scalar() && !type_b
->is_scalar()) {
618 _mesa_glsl_error(loc
, state
, "if the first operand of %s is scalar, the "
619 "second must be scalar as well",
620 ast_expression::operator_string(op
));
621 return glsl_type::error_type
;
624 /* If both operands are vectors, check that they have same number of
627 if (type_a
->is_vector() &&
628 type_b
->is_vector() &&
629 type_a
->vector_elements
!= type_b
->vector_elements
) {
630 _mesa_glsl_error(loc
, state
, "vector operands to operator %s must "
631 "have same number of elements",
632 ast_expression::operator_string(op
));
633 return glsl_type::error_type
;
636 /* "In all cases, the resulting type will be the same type as the left
643 * Returns the innermost array index expression in an rvalue tree.
644 * This is the largest indexing level -- if an array of blocks, then
645 * it is the block index rather than an indexing expression for an
646 * array-typed member of an array of blocks.
649 find_innermost_array_index(ir_rvalue
*rv
)
651 ir_dereference_array
*last
= NULL
;
653 if (rv
->as_dereference_array()) {
654 last
= rv
->as_dereference_array();
656 } else if (rv
->as_dereference_record())
657 rv
= rv
->as_dereference_record()->record
;
658 else if (rv
->as_swizzle())
659 rv
= rv
->as_swizzle()->val
;
665 return last
->array_index
;
671 * Validates that a value can be assigned to a location with a specified type
673 * Validates that \c rhs can be assigned to some location. If the types are
674 * not an exact match but an automatic conversion is possible, \c rhs will be
678 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
679 * Otherwise the actual RHS to be assigned will be returned. This may be
680 * \c rhs, or it may be \c rhs after some type conversion.
683 * In addition to being used for assignments, this function is used to
684 * type-check return values.
687 validate_assignment(struct _mesa_glsl_parse_state
*state
,
688 YYLTYPE loc
, ir_rvalue
*lhs
,
689 ir_rvalue
*rhs
, bool is_initializer
)
691 /* If there is already some error in the RHS, just return it. Anything
692 * else will lead to an avalanche of error message back to the user.
694 if (rhs
->type
->is_error())
697 /* In the Tessellation Control Shader:
698 * If a per-vertex output variable is used as an l-value, it is an error
699 * if the expression indicating the vertex number is not the identifier
702 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
703 ir_variable
*var
= lhs
->variable_referenced();
704 if (var
->data
.mode
== ir_var_shader_out
&& !var
->data
.patch
) {
705 ir_rvalue
*index
= find_innermost_array_index(lhs
);
706 ir_variable
*index_var
= index
? index
->variable_referenced() : NULL
;
707 if (!index_var
|| strcmp(index_var
->name
, "gl_InvocationID") != 0) {
708 _mesa_glsl_error(&loc
, state
,
709 "Tessellation control shader outputs can only "
710 "be indexed by gl_InvocationID");
716 /* If the types are identical, the assignment can trivially proceed.
718 if (rhs
->type
== lhs
->type
)
721 /* If the array element types are the same and the LHS is unsized,
722 * the assignment is okay for initializers embedded in variable
725 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
726 * is handled by ir_dereference::is_lvalue.
728 if (lhs
->type
->is_unsized_array() && rhs
->type
->is_array()
729 && (lhs
->type
->fields
.array
== rhs
->type
->fields
.array
)) {
730 if (is_initializer
) {
733 _mesa_glsl_error(&loc
, state
,
734 "implicitly sized arrays cannot be assigned");
739 /* Check for implicit conversion in GLSL 1.20 */
740 if (apply_implicit_conversion(lhs
->type
, rhs
, state
)) {
741 if (rhs
->type
== lhs
->type
)
745 _mesa_glsl_error(&loc
, state
,
746 "%s of type %s cannot be assigned to "
747 "variable of type %s",
748 is_initializer
? "initializer" : "value",
749 rhs
->type
->name
, lhs
->type
->name
);
755 mark_whole_array_access(ir_rvalue
*access
)
757 ir_dereference_variable
*deref
= access
->as_dereference_variable();
759 if (deref
&& deref
->var
) {
760 deref
->var
->data
.max_array_access
= deref
->type
->length
- 1;
765 do_assignment(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
766 const char *non_lvalue_description
,
767 ir_rvalue
*lhs
, ir_rvalue
*rhs
,
768 ir_rvalue
**out_rvalue
, bool needs_rvalue
,
773 bool error_emitted
= (lhs
->type
->is_error() || rhs
->type
->is_error());
774 ir_rvalue
*extract_channel
= NULL
;
776 /* If the assignment LHS comes back as an ir_binop_vector_extract
777 * expression, move it to the RHS as an ir_triop_vector_insert.
779 if (lhs
->ir_type
== ir_type_expression
) {
780 ir_expression
*const lhs_expr
= lhs
->as_expression();
782 if (unlikely(lhs_expr
->operation
== ir_binop_vector_extract
)) {
784 validate_assignment(state
, lhs_loc
, lhs
,
785 rhs
, is_initializer
);
787 if (new_rhs
== NULL
) {
791 * - LHS: (expression float vector_extract <vec> <channel>)
795 * - RHS: (expression vec2 vector_insert <vec> <channel> <scalar>)
797 * The LHS type is now a vector instead of a scalar. Since GLSL
798 * allows assignments to be used as rvalues, we need to re-extract
799 * the channel from assignment_temp when returning the rvalue.
801 extract_channel
= lhs_expr
->operands
[1];
802 rhs
= new(ctx
) ir_expression(ir_triop_vector_insert
,
803 lhs_expr
->operands
[0]->type
,
804 lhs_expr
->operands
[0],
807 lhs
= lhs_expr
->operands
[0]->clone(ctx
, NULL
);
812 ir_variable
*lhs_var
= lhs
->variable_referenced();
814 lhs_var
->data
.assigned
= true;
816 if (!error_emitted
) {
817 if (non_lvalue_description
!= NULL
) {
818 _mesa_glsl_error(&lhs_loc
, state
,
820 non_lvalue_description
);
821 error_emitted
= true;
822 } else if (lhs_var
!= NULL
&& lhs_var
->data
.read_only
) {
823 _mesa_glsl_error(&lhs_loc
, state
,
824 "assignment to read-only variable '%s'",
826 error_emitted
= true;
827 } else if (lhs
->type
->is_array() &&
828 !state
->check_version(120, 300, &lhs_loc
,
829 "whole array assignment forbidden")) {
830 /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
832 * "Other binary or unary expressions, non-dereferenced
833 * arrays, function names, swizzles with repeated fields,
834 * and constants cannot be l-values."
836 * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00.
838 error_emitted
= true;
839 } else if (!lhs
->is_lvalue()) {
840 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
841 error_emitted
= true;
846 validate_assignment(state
, lhs_loc
, lhs
, rhs
, is_initializer
);
847 if (new_rhs
!= NULL
) {
850 /* If the LHS array was not declared with a size, it takes it size from
851 * the RHS. If the LHS is an l-value and a whole array, it must be a
852 * dereference of a variable. Any other case would require that the LHS
853 * is either not an l-value or not a whole array.
855 if (lhs
->type
->is_unsized_array()) {
856 ir_dereference
*const d
= lhs
->as_dereference();
860 ir_variable
*const var
= d
->variable_referenced();
864 if (var
->data
.max_array_access
>= unsigned(rhs
->type
->array_size())) {
865 /* FINISHME: This should actually log the location of the RHS. */
866 _mesa_glsl_error(& lhs_loc
, state
, "array size must be > %u due to "
868 var
->data
.max_array_access
);
871 var
->type
= glsl_type::get_array_instance(lhs
->type
->fields
.array
,
872 rhs
->type
->array_size());
875 if (lhs
->type
->is_array()) {
876 mark_whole_array_access(rhs
);
877 mark_whole_array_access(lhs
);
881 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
882 * but not post_inc) need the converted assigned value as an rvalue
883 * to handle things like:
888 ir_variable
*var
= new(ctx
) ir_variable(rhs
->type
, "assignment_tmp",
890 instructions
->push_tail(var
);
891 instructions
->push_tail(assign(var
, rhs
));
893 if (!error_emitted
) {
894 ir_dereference_variable
*deref_var
= new(ctx
) ir_dereference_variable(var
);
895 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, deref_var
));
897 ir_rvalue
*rvalue
= new(ctx
) ir_dereference_variable(var
);
899 if (extract_channel
) {
900 rvalue
= new(ctx
) ir_expression(ir_binop_vector_extract
,
902 extract_channel
->clone(ctx
, NULL
));
905 *out_rvalue
= rvalue
;
908 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, rhs
));
912 return error_emitted
;
916 get_lvalue_copy(exec_list
*instructions
, ir_rvalue
*lvalue
)
918 void *ctx
= ralloc_parent(lvalue
);
921 var
= new(ctx
) ir_variable(lvalue
->type
, "_post_incdec_tmp",
923 instructions
->push_tail(var
);
925 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
928 return new(ctx
) ir_dereference_variable(var
);
933 ast_node::hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
942 ast_function_expression::hir_no_rvalue(exec_list
*instructions
,
943 struct _mesa_glsl_parse_state
*state
)
945 (void)hir(instructions
, state
);
949 ast_aggregate_initializer::hir_no_rvalue(exec_list
*instructions
,
950 struct _mesa_glsl_parse_state
*state
)
952 (void)hir(instructions
, state
);
956 do_comparison(void *mem_ctx
, int operation
, ir_rvalue
*op0
, ir_rvalue
*op1
)
959 ir_rvalue
*cmp
= NULL
;
961 if (operation
== ir_binop_all_equal
)
962 join_op
= ir_binop_logic_and
;
964 join_op
= ir_binop_logic_or
;
966 switch (op0
->type
->base_type
) {
967 case GLSL_TYPE_FLOAT
:
971 case GLSL_TYPE_DOUBLE
:
972 return new(mem_ctx
) ir_expression(operation
, op0
, op1
);
974 case GLSL_TYPE_ARRAY
: {
975 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
976 ir_rvalue
*e0
, *e1
, *result
;
978 e0
= new(mem_ctx
) ir_dereference_array(op0
->clone(mem_ctx
, NULL
),
979 new(mem_ctx
) ir_constant(i
));
980 e1
= new(mem_ctx
) ir_dereference_array(op1
->clone(mem_ctx
, NULL
),
981 new(mem_ctx
) ir_constant(i
));
982 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
985 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
991 mark_whole_array_access(op0
);
992 mark_whole_array_access(op1
);
996 case GLSL_TYPE_STRUCT
: {
997 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
998 ir_rvalue
*e0
, *e1
, *result
;
999 const char *field_name
= op0
->type
->fields
.structure
[i
].name
;
1001 e0
= new(mem_ctx
) ir_dereference_record(op0
->clone(mem_ctx
, NULL
),
1003 e1
= new(mem_ctx
) ir_dereference_record(op1
->clone(mem_ctx
, NULL
),
1005 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
1008 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
1016 case GLSL_TYPE_ERROR
:
1017 case GLSL_TYPE_VOID
:
1018 case GLSL_TYPE_SAMPLER
:
1019 case GLSL_TYPE_IMAGE
:
1020 case GLSL_TYPE_INTERFACE
:
1021 case GLSL_TYPE_ATOMIC_UINT
:
1022 /* I assume a comparison of a struct containing a sampler just
1023 * ignores the sampler present in the type.
1029 cmp
= new(mem_ctx
) ir_constant(true);
1034 /* For logical operations, we want to ensure that the operands are
1035 * scalar booleans. If it isn't, emit an error and return a constant
1036 * boolean to avoid triggering cascading error messages.
1039 get_scalar_boolean_operand(exec_list
*instructions
,
1040 struct _mesa_glsl_parse_state
*state
,
1041 ast_expression
*parent_expr
,
1043 const char *operand_name
,
1044 bool *error_emitted
)
1046 ast_expression
*expr
= parent_expr
->subexpressions
[operand
];
1048 ir_rvalue
*val
= expr
->hir(instructions
, state
);
1050 if (val
->type
->is_boolean() && val
->type
->is_scalar())
1053 if (!*error_emitted
) {
1054 YYLTYPE loc
= expr
->get_location();
1055 _mesa_glsl_error(&loc
, state
, "%s of `%s' must be scalar boolean",
1057 parent_expr
->operator_string(parent_expr
->oper
));
1058 *error_emitted
= true;
1061 return new(ctx
) ir_constant(true);
1065 * If name refers to a builtin array whose maximum allowed size is less than
1066 * size, report an error and return true. Otherwise return false.
1069 check_builtin_array_max_size(const char *name
, unsigned size
,
1070 YYLTYPE loc
, struct _mesa_glsl_parse_state
*state
)
1072 if ((strcmp("gl_TexCoord", name
) == 0)
1073 && (size
> state
->Const
.MaxTextureCoords
)) {
1074 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1076 * "The size [of gl_TexCoord] can be at most
1077 * gl_MaxTextureCoords."
1079 _mesa_glsl_error(&loc
, state
, "`gl_TexCoord' array size cannot "
1080 "be larger than gl_MaxTextureCoords (%u)",
1081 state
->Const
.MaxTextureCoords
);
1082 } else if (strcmp("gl_ClipDistance", name
) == 0
1083 && size
> state
->Const
.MaxClipPlanes
) {
1084 /* From section 7.1 (Vertex Shader Special Variables) of the
1087 * "The gl_ClipDistance array is predeclared as unsized and
1088 * must be sized by the shader either redeclaring it with a
1089 * size or indexing it only with integral constant
1090 * expressions. ... The size can be at most
1091 * gl_MaxClipDistances."
1093 _mesa_glsl_error(&loc
, state
, "`gl_ClipDistance' array size cannot "
1094 "be larger than gl_MaxClipDistances (%u)",
1095 state
->Const
.MaxClipPlanes
);
1100 * Create the constant 1, of a which is appropriate for incrementing and
1101 * decrementing values of the given GLSL type. For example, if type is vec4,
1102 * this creates a constant value of 1.0 having type float.
1104 * If the given type is invalid for increment and decrement operators, return
1105 * a floating point 1--the error will be detected later.
1108 constant_one_for_inc_dec(void *ctx
, const glsl_type
*type
)
1110 switch (type
->base_type
) {
1111 case GLSL_TYPE_UINT
:
1112 return new(ctx
) ir_constant((unsigned) 1);
1114 return new(ctx
) ir_constant(1);
1116 case GLSL_TYPE_FLOAT
:
1117 return new(ctx
) ir_constant(1.0f
);
1122 ast_expression::hir(exec_list
*instructions
,
1123 struct _mesa_glsl_parse_state
*state
)
1125 return do_hir(instructions
, state
, true);
1129 ast_expression::hir_no_rvalue(exec_list
*instructions
,
1130 struct _mesa_glsl_parse_state
*state
)
1132 do_hir(instructions
, state
, false);
1136 ast_expression::do_hir(exec_list
*instructions
,
1137 struct _mesa_glsl_parse_state
*state
,
1141 static const int operations
[AST_NUM_OPERATORS
] = {
1142 -1, /* ast_assign doesn't convert to ir_expression. */
1143 -1, /* ast_plus doesn't convert to ir_expression. */
1157 ir_binop_any_nequal
,
1167 /* Note: The following block of expression types actually convert
1168 * to multiple IR instructions.
1170 ir_binop_mul
, /* ast_mul_assign */
1171 ir_binop_div
, /* ast_div_assign */
1172 ir_binop_mod
, /* ast_mod_assign */
1173 ir_binop_add
, /* ast_add_assign */
1174 ir_binop_sub
, /* ast_sub_assign */
1175 ir_binop_lshift
, /* ast_ls_assign */
1176 ir_binop_rshift
, /* ast_rs_assign */
1177 ir_binop_bit_and
, /* ast_and_assign */
1178 ir_binop_bit_xor
, /* ast_xor_assign */
1179 ir_binop_bit_or
, /* ast_or_assign */
1181 -1, /* ast_conditional doesn't convert to ir_expression. */
1182 ir_binop_add
, /* ast_pre_inc. */
1183 ir_binop_sub
, /* ast_pre_dec. */
1184 ir_binop_add
, /* ast_post_inc. */
1185 ir_binop_sub
, /* ast_post_dec. */
1186 -1, /* ast_field_selection doesn't conv to ir_expression. */
1187 -1, /* ast_array_index doesn't convert to ir_expression. */
1188 -1, /* ast_function_call doesn't conv to ir_expression. */
1189 -1, /* ast_identifier doesn't convert to ir_expression. */
1190 -1, /* ast_int_constant doesn't convert to ir_expression. */
1191 -1, /* ast_uint_constant doesn't conv to ir_expression. */
1192 -1, /* ast_float_constant doesn't conv to ir_expression. */
1193 -1, /* ast_bool_constant doesn't conv to ir_expression. */
1194 -1, /* ast_sequence doesn't convert to ir_expression. */
1196 ir_rvalue
*result
= NULL
;
1198 const struct glsl_type
*type
; /* a temporary variable for switch cases */
1199 bool error_emitted
= false;
1202 loc
= this->get_location();
1204 switch (this->oper
) {
1206 assert(!"ast_aggregate: Should never get here.");
1210 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1211 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1214 do_assignment(instructions
, state
,
1215 this->subexpressions
[0]->non_lvalue_description
,
1216 op
[0], op
[1], &result
, needs_rvalue
, false,
1217 this->subexpressions
[0]->get_location());
1222 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1224 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1226 error_emitted
= type
->is_error();
1232 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1234 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1236 error_emitted
= type
->is_error();
1238 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1246 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1247 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1249 type
= arithmetic_result_type(op
[0], op
[1],
1250 (this->oper
== ast_mul
),
1252 error_emitted
= type
->is_error();
1254 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1259 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1260 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1262 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1264 assert(operations
[this->oper
] == ir_binop_mod
);
1266 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1268 error_emitted
= type
->is_error();
1273 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1274 error_emitted
= true;
1277 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1278 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1279 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1281 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1283 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1290 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1291 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1293 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
1295 /* The relational operators must either generate an error or result
1296 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1298 assert(type
->is_error()
1299 || ((type
->base_type
== GLSL_TYPE_BOOL
)
1300 && type
->is_scalar()));
1302 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1304 error_emitted
= type
->is_error();
1309 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1310 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1312 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1314 * "The equality operators equal (==), and not equal (!=)
1315 * operate on all types. They result in a scalar Boolean. If
1316 * the operand types do not match, then there must be a
1317 * conversion from Section 4.1.10 "Implicit Conversions"
1318 * applied to one operand that can make them match, in which
1319 * case this conversion is done."
1322 if (op
[0]->type
== glsl_type::void_type
|| op
[1]->type
== glsl_type::void_type
) {
1323 _mesa_glsl_error(& loc
, state
, "`%s': wrong operand types: "
1324 "no operation `%1$s' exists that takes a left-hand "
1325 "operand of type 'void' or a right operand of type "
1326 "'void'", (this->oper
== ast_equal
) ? "==" : "!=");
1327 error_emitted
= true;
1328 } else if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
1329 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
1330 || (op
[0]->type
!= op
[1]->type
)) {
1331 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
1332 "type", (this->oper
== ast_equal
) ? "==" : "!=");
1333 error_emitted
= true;
1334 } else if ((op
[0]->type
->is_array() || op
[1]->type
->is_array()) &&
1335 !state
->check_version(120, 300, &loc
,
1336 "array comparisons forbidden")) {
1337 error_emitted
= true;
1338 } else if ((op
[0]->type
->contains_opaque() ||
1339 op
[1]->type
->contains_opaque())) {
1340 _mesa_glsl_error(&loc
, state
, "opaque type comparisons forbidden");
1341 error_emitted
= true;
1344 if (error_emitted
) {
1345 result
= new(ctx
) ir_constant(false);
1347 result
= do_comparison(ctx
, operations
[this->oper
], op
[0], op
[1]);
1348 assert(result
->type
== glsl_type::bool_type
);
1355 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1356 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1357 type
= bit_logic_result_type(op
[0]->type
, op
[1]->type
, this->oper
,
1359 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1361 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1365 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1367 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1368 error_emitted
= true;
1371 if (!op
[0]->type
->is_integer()) {
1372 _mesa_glsl_error(&loc
, state
, "operand of `~' must be an integer");
1373 error_emitted
= true;
1376 type
= error_emitted
? glsl_type::error_type
: op
[0]->type
;
1377 result
= new(ctx
) ir_expression(ir_unop_bit_not
, type
, op
[0], NULL
);
1380 case ast_logic_and
: {
1381 exec_list rhs_instructions
;
1382 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1383 "LHS", &error_emitted
);
1384 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1385 "RHS", &error_emitted
);
1387 if (rhs_instructions
.is_empty()) {
1388 result
= new(ctx
) ir_expression(ir_binop_logic_and
, op
[0], op
[1]);
1389 type
= result
->type
;
1391 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1394 instructions
->push_tail(tmp
);
1396 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1397 instructions
->push_tail(stmt
);
1399 stmt
->then_instructions
.append_list(&rhs_instructions
);
1400 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1401 ir_assignment
*const then_assign
=
1402 new(ctx
) ir_assignment(then_deref
, op
[1]);
1403 stmt
->then_instructions
.push_tail(then_assign
);
1405 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1406 ir_assignment
*const else_assign
=
1407 new(ctx
) ir_assignment(else_deref
, new(ctx
) ir_constant(false));
1408 stmt
->else_instructions
.push_tail(else_assign
);
1410 result
= new(ctx
) ir_dereference_variable(tmp
);
1416 case ast_logic_or
: {
1417 exec_list rhs_instructions
;
1418 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1419 "LHS", &error_emitted
);
1420 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1421 "RHS", &error_emitted
);
1423 if (rhs_instructions
.is_empty()) {
1424 result
= new(ctx
) ir_expression(ir_binop_logic_or
, op
[0], op
[1]);
1425 type
= result
->type
;
1427 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1430 instructions
->push_tail(tmp
);
1432 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1433 instructions
->push_tail(stmt
);
1435 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1436 ir_assignment
*const then_assign
=
1437 new(ctx
) ir_assignment(then_deref
, new(ctx
) ir_constant(true));
1438 stmt
->then_instructions
.push_tail(then_assign
);
1440 stmt
->else_instructions
.append_list(&rhs_instructions
);
1441 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1442 ir_assignment
*const else_assign
=
1443 new(ctx
) ir_assignment(else_deref
, op
[1]);
1444 stmt
->else_instructions
.push_tail(else_assign
);
1446 result
= new(ctx
) ir_dereference_variable(tmp
);
1453 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1455 * "The logical binary operators and (&&), or ( | | ), and
1456 * exclusive or (^^). They operate only on two Boolean
1457 * expressions and result in a Boolean expression."
1459 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0, "LHS",
1461 op
[1] = get_scalar_boolean_operand(instructions
, state
, this, 1, "RHS",
1464 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1469 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1470 "operand", &error_emitted
);
1472 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1476 case ast_mul_assign
:
1477 case ast_div_assign
:
1478 case ast_add_assign
:
1479 case ast_sub_assign
: {
1480 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1481 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1483 type
= arithmetic_result_type(op
[0], op
[1],
1484 (this->oper
== ast_mul_assign
),
1487 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1491 do_assignment(instructions
, state
,
1492 this->subexpressions
[0]->non_lvalue_description
,
1493 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1494 &result
, needs_rvalue
, false,
1495 this->subexpressions
[0]->get_location());
1497 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1498 * explicitly test for this because none of the binary expression
1499 * operators allow array operands either.
1505 case ast_mod_assign
: {
1506 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1507 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1509 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1511 assert(operations
[this->oper
] == ir_binop_mod
);
1513 ir_rvalue
*temp_rhs
;
1514 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1518 do_assignment(instructions
, state
,
1519 this->subexpressions
[0]->non_lvalue_description
,
1520 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1521 &result
, needs_rvalue
, false,
1522 this->subexpressions
[0]->get_location());
1527 case ast_rs_assign
: {
1528 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1529 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1530 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1532 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1533 type
, op
[0], op
[1]);
1535 do_assignment(instructions
, state
,
1536 this->subexpressions
[0]->non_lvalue_description
,
1537 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1538 &result
, needs_rvalue
, false,
1539 this->subexpressions
[0]->get_location());
1543 case ast_and_assign
:
1544 case ast_xor_assign
:
1545 case ast_or_assign
: {
1546 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1547 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1548 type
= bit_logic_result_type(op
[0]->type
, op
[1]->type
, this->oper
,
1550 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1551 type
, op
[0], op
[1]);
1553 do_assignment(instructions
, state
,
1554 this->subexpressions
[0]->non_lvalue_description
,
1555 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1556 &result
, needs_rvalue
, false,
1557 this->subexpressions
[0]->get_location());
1561 case ast_conditional
: {
1562 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1564 * "The ternary selection operator (?:). It operates on three
1565 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1566 * first expression, which must result in a scalar Boolean."
1568 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1569 "condition", &error_emitted
);
1571 /* The :? operator is implemented by generating an anonymous temporary
1572 * followed by an if-statement. The last instruction in each branch of
1573 * the if-statement assigns a value to the anonymous temporary. This
1574 * temporary is the r-value of the expression.
1576 exec_list then_instructions
;
1577 exec_list else_instructions
;
1579 op
[1] = this->subexpressions
[1]->hir(&then_instructions
, state
);
1580 op
[2] = this->subexpressions
[2]->hir(&else_instructions
, state
);
1582 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1584 * "The second and third expressions can be any type, as
1585 * long their types match, or there is a conversion in
1586 * Section 4.1.10 "Implicit Conversions" that can be applied
1587 * to one of the expressions to make their types match. This
1588 * resulting matching type is the type of the entire
1591 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1592 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1593 || (op
[1]->type
!= op
[2]->type
)) {
1594 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1596 _mesa_glsl_error(& loc
, state
, "second and third operands of ?: "
1597 "operator must have matching types");
1598 error_emitted
= true;
1599 type
= glsl_type::error_type
;
1604 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1606 * "The second and third expressions must be the same type, but can
1607 * be of any type other than an array."
1609 if (type
->is_array() &&
1610 !state
->check_version(120, 300, &loc
,
1611 "second and third operands of ?: operator "
1612 "cannot be arrays")) {
1613 error_emitted
= true;
1616 /* From section 4.1.7 of the GLSL 4.50 spec (Opaque Types):
1618 * "Except for array indexing, structure member selection, and
1619 * parentheses, opaque variables are not allowed to be operands in
1620 * expressions; such use results in a compile-time error."
1622 if (type
->contains_opaque()) {
1623 _mesa_glsl_error(&loc
, state
, "opaque variables cannot be operands "
1624 "of the ?: operator");
1625 error_emitted
= true;
1628 ir_constant
*cond_val
= op
[0]->constant_expression_value();
1630 if (then_instructions
.is_empty()
1631 && else_instructions
.is_empty()
1632 && cond_val
!= NULL
) {
1633 result
= cond_val
->value
.b
[0] ? op
[1] : op
[2];
1635 /* The copy to conditional_tmp reads the whole array. */
1636 if (type
->is_array()) {
1637 mark_whole_array_access(op
[1]);
1638 mark_whole_array_access(op
[2]);
1641 ir_variable
*const tmp
=
1642 new(ctx
) ir_variable(type
, "conditional_tmp", ir_var_temporary
);
1643 instructions
->push_tail(tmp
);
1645 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1646 instructions
->push_tail(stmt
);
1648 then_instructions
.move_nodes_to(& stmt
->then_instructions
);
1649 ir_dereference
*const then_deref
=
1650 new(ctx
) ir_dereference_variable(tmp
);
1651 ir_assignment
*const then_assign
=
1652 new(ctx
) ir_assignment(then_deref
, op
[1]);
1653 stmt
->then_instructions
.push_tail(then_assign
);
1655 else_instructions
.move_nodes_to(& stmt
->else_instructions
);
1656 ir_dereference
*const else_deref
=
1657 new(ctx
) ir_dereference_variable(tmp
);
1658 ir_assignment
*const else_assign
=
1659 new(ctx
) ir_assignment(else_deref
, op
[2]);
1660 stmt
->else_instructions
.push_tail(else_assign
);
1662 result
= new(ctx
) ir_dereference_variable(tmp
);
1669 this->non_lvalue_description
= (this->oper
== ast_pre_inc
)
1670 ? "pre-increment operation" : "pre-decrement operation";
1672 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1673 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1675 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1677 ir_rvalue
*temp_rhs
;
1678 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1682 do_assignment(instructions
, state
,
1683 this->subexpressions
[0]->non_lvalue_description
,
1684 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1685 &result
, needs_rvalue
, false,
1686 this->subexpressions
[0]->get_location());
1691 case ast_post_dec
: {
1692 this->non_lvalue_description
= (this->oper
== ast_post_inc
)
1693 ? "post-increment operation" : "post-decrement operation";
1694 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1695 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1697 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1699 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1701 ir_rvalue
*temp_rhs
;
1702 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1705 /* Get a temporary of a copy of the lvalue before it's modified.
1706 * This may get thrown away later.
1708 result
= get_lvalue_copy(instructions
, op
[0]->clone(ctx
, NULL
));
1710 ir_rvalue
*junk_rvalue
;
1712 do_assignment(instructions
, state
,
1713 this->subexpressions
[0]->non_lvalue_description
,
1714 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1715 &junk_rvalue
, false, false,
1716 this->subexpressions
[0]->get_location());
1721 case ast_field_selection
:
1722 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1725 case ast_array_index
: {
1726 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1728 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1729 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1731 result
= _mesa_ast_array_index_to_hir(ctx
, state
, op
[0], op
[1],
1734 if (result
->type
->is_error())
1735 error_emitted
= true;
1740 case ast_function_call
:
1741 /* Should *NEVER* get here. ast_function_call should always be handled
1742 * by ast_function_expression::hir.
1747 case ast_identifier
: {
1748 /* ast_identifier can appear several places in a full abstract syntax
1749 * tree. This particular use must be at location specified in the grammar
1750 * as 'variable_identifier'.
1753 state
->symbols
->get_variable(this->primary_expression
.identifier
);
1756 var
->data
.used
= true;
1757 result
= new(ctx
) ir_dereference_variable(var
);
1759 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
1760 this->primary_expression
.identifier
);
1762 result
= ir_rvalue::error_value(ctx
);
1763 error_emitted
= true;
1768 case ast_int_constant
:
1769 result
= new(ctx
) ir_constant(this->primary_expression
.int_constant
);
1772 case ast_uint_constant
:
1773 result
= new(ctx
) ir_constant(this->primary_expression
.uint_constant
);
1776 case ast_float_constant
:
1777 result
= new(ctx
) ir_constant(this->primary_expression
.float_constant
);
1780 case ast_bool_constant
:
1781 result
= new(ctx
) ir_constant(bool(this->primary_expression
.bool_constant
));
1784 case ast_double_constant
:
1785 result
= new(ctx
) ir_constant(this->primary_expression
.double_constant
);
1788 case ast_sequence
: {
1789 /* It should not be possible to generate a sequence in the AST without
1790 * any expressions in it.
1792 assert(!this->expressions
.is_empty());
1794 /* The r-value of a sequence is the last expression in the sequence. If
1795 * the other expressions in the sequence do not have side-effects (and
1796 * therefore add instructions to the instruction list), they get dropped
1799 exec_node
*previous_tail_pred
= NULL
;
1800 YYLTYPE previous_operand_loc
= loc
;
1802 foreach_list_typed (ast_node
, ast
, link
, &this->expressions
) {
1803 /* If one of the operands of comma operator does not generate any
1804 * code, we want to emit a warning. At each pass through the loop
1805 * previous_tail_pred will point to the last instruction in the
1806 * stream *before* processing the previous operand. Naturally,
1807 * instructions->tail_pred will point to the last instruction in the
1808 * stream *after* processing the previous operand. If the two
1809 * pointers match, then the previous operand had no effect.
1811 * The warning behavior here differs slightly from GCC. GCC will
1812 * only emit a warning if none of the left-hand operands have an
1813 * effect. However, it will emit a warning for each. I believe that
1814 * there are some cases in C (especially with GCC extensions) where
1815 * it is useful to have an intermediate step in a sequence have no
1816 * effect, but I don't think these cases exist in GLSL. Either way,
1817 * it would be a giant hassle to replicate that behavior.
1819 if (previous_tail_pred
== instructions
->tail_pred
) {
1820 _mesa_glsl_warning(&previous_operand_loc
, state
,
1821 "left-hand operand of comma expression has "
1825 /* tail_pred is directly accessed instead of using the get_tail()
1826 * method for performance reasons. get_tail() has extra code to
1827 * return NULL when the list is empty. We don't care about that
1828 * here, so using tail_pred directly is fine.
1830 previous_tail_pred
= instructions
->tail_pred
;
1831 previous_operand_loc
= ast
->get_location();
1833 result
= ast
->hir(instructions
, state
);
1836 /* Any errors should have already been emitted in the loop above.
1838 error_emitted
= true;
1842 type
= NULL
; /* use result->type, not type. */
1843 assert(result
!= NULL
|| !needs_rvalue
);
1845 if (result
&& result
->type
->is_error() && !error_emitted
)
1846 _mesa_glsl_error(& loc
, state
, "type mismatch");
1853 ast_expression_statement::hir(exec_list
*instructions
,
1854 struct _mesa_glsl_parse_state
*state
)
1856 /* It is possible to have expression statements that don't have an
1857 * expression. This is the solitary semicolon:
1859 * for (i = 0; i < 5; i++)
1862 * In this case the expression will be NULL. Test for NULL and don't do
1863 * anything in that case.
1865 if (expression
!= NULL
)
1866 expression
->hir_no_rvalue(instructions
, state
);
1868 /* Statements do not have r-values.
1875 ast_compound_statement::hir(exec_list
*instructions
,
1876 struct _mesa_glsl_parse_state
*state
)
1879 state
->symbols
->push_scope();
1881 foreach_list_typed (ast_node
, ast
, link
, &this->statements
)
1882 ast
->hir(instructions
, state
);
1885 state
->symbols
->pop_scope();
1887 /* Compound statements do not have r-values.
1893 * Evaluate the given exec_node (which should be an ast_node representing
1894 * a single array dimension) and return its integer value.
1897 process_array_size(exec_node
*node
,
1898 struct _mesa_glsl_parse_state
*state
)
1900 exec_list dummy_instructions
;
1902 ast_node
*array_size
= exec_node_data(ast_node
, node
, link
);
1903 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
1904 YYLTYPE loc
= array_size
->get_location();
1907 _mesa_glsl_error(& loc
, state
,
1908 "array size could not be resolved");
1912 if (!ir
->type
->is_integer()) {
1913 _mesa_glsl_error(& loc
, state
,
1914 "array size must be integer type");
1918 if (!ir
->type
->is_scalar()) {
1919 _mesa_glsl_error(& loc
, state
,
1920 "array size must be scalar type");
1924 ir_constant
*const size
= ir
->constant_expression_value();
1926 _mesa_glsl_error(& loc
, state
, "array size must be a "
1927 "constant valued expression");
1931 if (size
->value
.i
[0] <= 0) {
1932 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
1936 assert(size
->type
== ir
->type
);
1938 /* If the array size is const (and we've verified that
1939 * it is) then no instructions should have been emitted
1940 * when we converted it to HIR. If they were emitted,
1941 * then either the array size isn't const after all, or
1942 * we are emitting unnecessary instructions.
1944 assert(dummy_instructions
.is_empty());
1946 return size
->value
.u
[0];
1949 static const glsl_type
*
1950 process_array_type(YYLTYPE
*loc
, const glsl_type
*base
,
1951 ast_array_specifier
*array_specifier
,
1952 struct _mesa_glsl_parse_state
*state
)
1954 const glsl_type
*array_type
= base
;
1956 if (array_specifier
!= NULL
) {
1957 if (base
->is_array()) {
1959 /* From page 19 (page 25) of the GLSL 1.20 spec:
1961 * "Only one-dimensional arrays may be declared."
1963 if (!state
->ARB_arrays_of_arrays_enable
) {
1964 _mesa_glsl_error(loc
, state
,
1965 "invalid array of `%s'"
1966 "GL_ARB_arrays_of_arrays "
1967 "required for defining arrays of arrays",
1969 return glsl_type::error_type
;
1972 if (base
->length
== 0) {
1973 _mesa_glsl_error(loc
, state
,
1974 "only the outermost array dimension can "
1977 return glsl_type::error_type
;
1981 for (exec_node
*node
= array_specifier
->array_dimensions
.tail_pred
;
1982 !node
->is_head_sentinel(); node
= node
->prev
) {
1983 unsigned array_size
= process_array_size(node
, state
);
1984 array_type
= glsl_type::get_array_instance(array_type
, array_size
);
1987 if (array_specifier
->is_unsized_array
)
1988 array_type
= glsl_type::get_array_instance(array_type
, 0);
1996 ast_type_specifier::glsl_type(const char **name
,
1997 struct _mesa_glsl_parse_state
*state
) const
1999 const struct glsl_type
*type
;
2001 type
= state
->symbols
->get_type(this->type_name
);
2002 *name
= this->type_name
;
2004 YYLTYPE loc
= this->get_location();
2005 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
2011 ast_fully_specified_type::glsl_type(const char **name
,
2012 struct _mesa_glsl_parse_state
*state
) const
2014 const struct glsl_type
*type
= this->specifier
->glsl_type(name
, state
);
2019 if (type
->base_type
== GLSL_TYPE_FLOAT
2021 && state
->stage
== MESA_SHADER_FRAGMENT
2022 && this->qualifier
.precision
== ast_precision_none
2023 && state
->symbols
->get_variable("#default precision") == NULL
) {
2024 YYLTYPE loc
= this->get_location();
2025 _mesa_glsl_error(&loc
, state
,
2026 "no precision specified this scope for type `%s'",
2034 * Determine whether a toplevel variable declaration declares a varying. This
2035 * function operates by examining the variable's mode and the shader target,
2036 * so it correctly identifies linkage variables regardless of whether they are
2037 * declared using the deprecated "varying" syntax or the new "in/out" syntax.
2039 * Passing a non-toplevel variable declaration (e.g. a function parameter) to
2040 * this function will produce undefined results.
2043 is_varying_var(ir_variable
*var
, gl_shader_stage target
)
2046 case MESA_SHADER_VERTEX
:
2047 return var
->data
.mode
== ir_var_shader_out
;
2048 case MESA_SHADER_FRAGMENT
:
2049 return var
->data
.mode
== ir_var_shader_in
;
2051 return var
->data
.mode
== ir_var_shader_out
|| var
->data
.mode
== ir_var_shader_in
;
2057 * Matrix layout qualifiers are only allowed on certain types
2060 validate_matrix_layout_for_type(struct _mesa_glsl_parse_state
*state
,
2062 const glsl_type
*type
,
2065 if (var
&& !var
->is_in_buffer_block()) {
2066 /* Layout qualifiers may only apply to interface blocks and fields in
2069 _mesa_glsl_error(loc
, state
,
2070 "uniform block layout qualifiers row_major and "
2071 "column_major may not be applied to variables "
2072 "outside of uniform blocks");
2073 } else if (!type
->is_matrix()) {
2074 /* The OpenGL ES 3.0 conformance tests did not originally allow
2075 * matrix layout qualifiers on non-matrices. However, the OpenGL
2076 * 4.4 and OpenGL ES 3.0 (revision TBD) specifications were
2077 * amended to specifically allow these layouts on all types. Emit
2078 * a warning so that people know their code may not be portable.
2080 _mesa_glsl_warning(loc
, state
,
2081 "uniform block layout qualifiers row_major and "
2082 "column_major applied to non-matrix types may "
2083 "be rejected by older compilers");
2084 } else if (type
->is_record()) {
2085 /* We allow 'layout(row_major)' on structure types because it's the only
2086 * way to get row-major layouts on matrices contained in structures.
2088 _mesa_glsl_warning(loc
, state
,
2089 "uniform block layout qualifiers row_major and "
2090 "column_major applied to structure types is not "
2091 "strictly conformant and may be rejected by other "
2097 validate_binding_qualifier(struct _mesa_glsl_parse_state
*state
,
2100 const ast_type_qualifier
*qual
)
2102 if (var
->data
.mode
!= ir_var_uniform
&& var
->data
.mode
!= ir_var_shader_storage
) {
2103 _mesa_glsl_error(loc
, state
,
2104 "the \"binding\" qualifier only applies to uniforms and "
2105 "shader storage buffer objects");
2109 if (qual
->binding
< 0) {
2110 _mesa_glsl_error(loc
, state
, "binding values must be >= 0");
2114 const struct gl_context
*const ctx
= state
->ctx
;
2115 unsigned elements
= var
->type
->is_array() ? var
->type
->length
: 1;
2116 unsigned max_index
= qual
->binding
+ elements
- 1;
2118 if (var
->type
->is_interface()) {
2119 /* UBOs. From page 60 of the GLSL 4.20 specification:
2120 * "If the binding point for any uniform block instance is less than zero,
2121 * or greater than or equal to the implementation-dependent maximum
2122 * number of uniform buffer bindings, a compilation error will occur.
2123 * When the binding identifier is used with a uniform block instanced as
2124 * an array of size N, all elements of the array from binding through
2125 * binding + N – 1 must be within this range."
2127 * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
2129 if (var
->data
.mode
== ir_var_uniform
&&
2130 max_index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2131 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d UBOs exceeds "
2132 "the maximum number of UBO binding points (%d)",
2133 qual
->binding
, elements
,
2134 ctx
->Const
.MaxUniformBufferBindings
);
2137 /* SSBOs. From page 67 of the GLSL 4.30 specification:
2138 * "If the binding point for any uniform or shader storage block instance
2139 * is less than zero, or greater than or equal to the
2140 * implementation-dependent maximum number of uniform buffer bindings, a
2141 * compile-time error will occur. When the binding identifier is used
2142 * with a uniform or shader storage block instanced as an array of size
2143 * N, all elements of the array from binding through binding + N – 1 must
2144 * be within this range."
2146 if (var
->data
.mode
== ir_var_shader_storage
&&
2147 max_index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2148 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d SSBOs exceeds "
2149 "the maximum number of SSBO binding points (%d)",
2150 qual
->binding
, elements
,
2151 ctx
->Const
.MaxShaderStorageBufferBindings
);
2154 } else if (var
->type
->is_sampler() ||
2155 (var
->type
->is_array() && var
->type
->fields
.array
->is_sampler())) {
2156 /* Samplers. From page 63 of the GLSL 4.20 specification:
2157 * "If the binding is less than zero, or greater than or equal to the
2158 * implementation-dependent maximum supported number of units, a
2159 * compilation error will occur. When the binding identifier is used
2160 * with an array of size N, all elements of the array from binding
2161 * through binding + N - 1 must be within this range."
2163 unsigned limit
= ctx
->Const
.MaxCombinedTextureImageUnits
;
2165 if (max_index
>= limit
) {
2166 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d samplers "
2167 "exceeds the maximum number of texture image units "
2168 "(%d)", qual
->binding
, elements
, limit
);
2172 } else if (var
->type
->contains_atomic()) {
2173 assert(ctx
->Const
.MaxAtomicBufferBindings
<= MAX_COMBINED_ATOMIC_BUFFERS
);
2174 if (unsigned(qual
->binding
) >= ctx
->Const
.MaxAtomicBufferBindings
) {
2175 _mesa_glsl_error(loc
, state
, "layout(binding = %d) exceeds the "
2176 " maximum number of atomic counter buffer bindings"
2177 "(%d)", qual
->binding
,
2178 ctx
->Const
.MaxAtomicBufferBindings
);
2183 _mesa_glsl_error(loc
, state
,
2184 "the \"binding\" qualifier only applies to uniform "
2185 "blocks, samplers, atomic counters, or arrays thereof");
2193 static glsl_interp_qualifier
2194 interpret_interpolation_qualifier(const struct ast_type_qualifier
*qual
,
2195 ir_variable_mode mode
,
2196 struct _mesa_glsl_parse_state
*state
,
2199 glsl_interp_qualifier interpolation
;
2200 if (qual
->flags
.q
.flat
)
2201 interpolation
= INTERP_QUALIFIER_FLAT
;
2202 else if (qual
->flags
.q
.noperspective
)
2203 interpolation
= INTERP_QUALIFIER_NOPERSPECTIVE
;
2204 else if (qual
->flags
.q
.smooth
)
2205 interpolation
= INTERP_QUALIFIER_SMOOTH
;
2207 interpolation
= INTERP_QUALIFIER_NONE
;
2209 if (interpolation
!= INTERP_QUALIFIER_NONE
) {
2210 if (mode
!= ir_var_shader_in
&& mode
!= ir_var_shader_out
) {
2211 _mesa_glsl_error(loc
, state
,
2212 "interpolation qualifier `%s' can only be applied to "
2213 "shader inputs or outputs.",
2214 interpolation_string(interpolation
));
2218 if ((state
->stage
== MESA_SHADER_VERTEX
&& mode
== ir_var_shader_in
) ||
2219 (state
->stage
== MESA_SHADER_FRAGMENT
&& mode
== ir_var_shader_out
)) {
2220 _mesa_glsl_error(loc
, state
,
2221 "interpolation qualifier `%s' cannot be applied to "
2222 "vertex shader inputs or fragment shader outputs",
2223 interpolation_string(interpolation
));
2227 return interpolation
;
2232 validate_explicit_location(const struct ast_type_qualifier
*qual
,
2234 struct _mesa_glsl_parse_state
*state
,
2239 /* Checks for GL_ARB_explicit_uniform_location. */
2240 if (qual
->flags
.q
.uniform
) {
2241 if (!state
->check_explicit_uniform_location_allowed(loc
, var
))
2244 const struct gl_context
*const ctx
= state
->ctx
;
2245 unsigned max_loc
= qual
->location
+ var
->type
->uniform_locations() - 1;
2247 /* ARB_explicit_uniform_location specification states:
2249 * "The explicitly defined locations and the generated locations
2250 * must be in the range of 0 to MAX_UNIFORM_LOCATIONS minus one."
2252 * "Valid locations for default-block uniform variable locations
2253 * are in the range of 0 to the implementation-defined maximum
2254 * number of uniform locations."
2256 if (qual
->location
< 0) {
2257 _mesa_glsl_error(loc
, state
,
2258 "explicit location < 0 for uniform %s", var
->name
);
2262 if (max_loc
>= ctx
->Const
.MaxUserAssignableUniformLocations
) {
2263 _mesa_glsl_error(loc
, state
, "location(s) consumed by uniform %s "
2264 ">= MAX_UNIFORM_LOCATIONS (%u)", var
->name
,
2265 ctx
->Const
.MaxUserAssignableUniformLocations
);
2269 var
->data
.explicit_location
= true;
2270 var
->data
.location
= qual
->location
;
2274 /* Between GL_ARB_explicit_attrib_location an
2275 * GL_ARB_separate_shader_objects, the inputs and outputs of any shader
2276 * stage can be assigned explicit locations. The checking here associates
2277 * the correct extension with the correct stage's input / output:
2281 * vertex explicit_loc sso
2282 * tess control sso sso
2285 * fragment sso explicit_loc
2287 switch (state
->stage
) {
2288 case MESA_SHADER_VERTEX
:
2289 if (var
->data
.mode
== ir_var_shader_in
) {
2290 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
2296 if (var
->data
.mode
== ir_var_shader_out
) {
2297 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
2306 case MESA_SHADER_TESS_CTRL
:
2307 case MESA_SHADER_TESS_EVAL
:
2308 case MESA_SHADER_GEOMETRY
:
2309 if (var
->data
.mode
== ir_var_shader_in
|| var
->data
.mode
== ir_var_shader_out
) {
2310 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
2319 case MESA_SHADER_FRAGMENT
:
2320 if (var
->data
.mode
== ir_var_shader_in
) {
2321 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
2327 if (var
->data
.mode
== ir_var_shader_out
) {
2328 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
2337 case MESA_SHADER_COMPUTE
:
2338 _mesa_glsl_error(loc
, state
,
2339 "compute shader variables cannot be given "
2340 "explicit locations");
2345 _mesa_glsl_error(loc
, state
,
2346 "%s cannot be given an explicit location in %s shader",
2348 _mesa_shader_stage_to_string(state
->stage
));
2350 var
->data
.explicit_location
= true;
2352 /* This bit of silliness is needed because invalid explicit locations
2353 * are supposed to be flagged during linking. Small negative values
2354 * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
2355 * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
2356 * The linker needs to be able to differentiate these cases. This
2357 * ensures that negative values stay negative.
2359 if (qual
->location
>= 0) {
2360 switch (state
->stage
) {
2361 case MESA_SHADER_VERTEX
:
2362 var
->data
.location
= (var
->data
.mode
== ir_var_shader_in
)
2363 ? (qual
->location
+ VERT_ATTRIB_GENERIC0
)
2364 : (qual
->location
+ VARYING_SLOT_VAR0
);
2367 case MESA_SHADER_TESS_CTRL
:
2368 case MESA_SHADER_TESS_EVAL
:
2369 case MESA_SHADER_GEOMETRY
:
2370 var
->data
.location
= qual
->location
+ VARYING_SLOT_VAR0
;
2373 case MESA_SHADER_FRAGMENT
:
2374 var
->data
.location
= (var
->data
.mode
== ir_var_shader_out
)
2375 ? (qual
->location
+ FRAG_RESULT_DATA0
)
2376 : (qual
->location
+ VARYING_SLOT_VAR0
);
2378 case MESA_SHADER_COMPUTE
:
2379 assert(!"Unexpected shader type");
2383 var
->data
.location
= qual
->location
;
2386 if (qual
->flags
.q
.explicit_index
) {
2387 /* From the GLSL 4.30 specification, section 4.4.2 (Output
2388 * Layout Qualifiers):
2390 * "It is also a compile-time error if a fragment shader
2391 * sets a layout index to less than 0 or greater than 1."
2393 * Older specifications don't mandate a behavior; we take
2394 * this as a clarification and always generate the error.
2396 if (qual
->index
< 0 || qual
->index
> 1) {
2397 _mesa_glsl_error(loc
, state
,
2398 "explicit index may only be 0 or 1");
2400 var
->data
.explicit_index
= true;
2401 var
->data
.index
= qual
->index
;
2408 apply_image_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
2410 struct _mesa_glsl_parse_state
*state
,
2413 const glsl_type
*base_type
= var
->type
->without_array();
2415 if (base_type
->is_image()) {
2416 if (var
->data
.mode
!= ir_var_uniform
&&
2417 var
->data
.mode
!= ir_var_function_in
) {
2418 _mesa_glsl_error(loc
, state
, "image variables may only be declared as "
2419 "function parameters or uniform-qualified "
2420 "global variables");
2423 var
->data
.image_read_only
|= qual
->flags
.q
.read_only
;
2424 var
->data
.image_write_only
|= qual
->flags
.q
.write_only
;
2425 var
->data
.image_coherent
|= qual
->flags
.q
.coherent
;
2426 var
->data
.image_volatile
|= qual
->flags
.q
._volatile
;
2427 var
->data
.image_restrict
|= qual
->flags
.q
.restrict_flag
;
2428 var
->data
.read_only
= true;
2430 if (qual
->flags
.q
.explicit_image_format
) {
2431 if (var
->data
.mode
== ir_var_function_in
) {
2432 _mesa_glsl_error(loc
, state
, "format qualifiers cannot be "
2433 "used on image function parameters");
2436 if (qual
->image_base_type
!= base_type
->sampler_type
) {
2437 _mesa_glsl_error(loc
, state
, "format qualifier doesn't match the "
2438 "base data type of the image");
2441 var
->data
.image_format
= qual
->image_format
;
2443 if (var
->data
.mode
== ir_var_uniform
&& !qual
->flags
.q
.write_only
) {
2444 _mesa_glsl_error(loc
, state
, "uniforms not qualified with "
2445 "`writeonly' must have a format layout "
2449 var
->data
.image_format
= GL_NONE
;
2451 } else if (qual
->flags
.q
.read_only
||
2452 qual
->flags
.q
.write_only
||
2453 qual
->flags
.q
.coherent
||
2454 qual
->flags
.q
._volatile
||
2455 qual
->flags
.q
.restrict_flag
||
2456 qual
->flags
.q
.explicit_image_format
) {
2457 _mesa_glsl_error(loc
, state
, "memory qualifiers may only be applied to "
2462 static inline const char*
2463 get_layout_qualifier_string(bool origin_upper_left
, bool pixel_center_integer
)
2465 if (origin_upper_left
&& pixel_center_integer
)
2466 return "origin_upper_left, pixel_center_integer";
2467 else if (origin_upper_left
)
2468 return "origin_upper_left";
2469 else if (pixel_center_integer
)
2470 return "pixel_center_integer";
2476 is_conflicting_fragcoord_redeclaration(struct _mesa_glsl_parse_state
*state
,
2477 const struct ast_type_qualifier
*qual
)
2479 /* If gl_FragCoord was previously declared, and the qualifiers were
2480 * different in any way, return true.
2482 if (state
->fs_redeclares_gl_fragcoord
) {
2483 return (state
->fs_pixel_center_integer
!= qual
->flags
.q
.pixel_center_integer
2484 || state
->fs_origin_upper_left
!= qual
->flags
.q
.origin_upper_left
);
2491 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
2493 struct _mesa_glsl_parse_state
*state
,
2497 STATIC_ASSERT(sizeof(qual
->flags
.q
) <= sizeof(qual
->flags
.i
));
2499 if (qual
->flags
.q
.invariant
) {
2500 if (var
->data
.used
) {
2501 _mesa_glsl_error(loc
, state
,
2502 "variable `%s' may not be redeclared "
2503 "`invariant' after being used",
2506 var
->data
.invariant
= 1;
2510 if (qual
->flags
.q
.precise
) {
2511 if (var
->data
.used
) {
2512 _mesa_glsl_error(loc
, state
,
2513 "variable `%s' may not be redeclared "
2514 "`precise' after being used",
2517 var
->data
.precise
= 1;
2521 if (qual
->flags
.q
.constant
|| qual
->flags
.q
.attribute
2522 || qual
->flags
.q
.uniform
2523 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
2524 var
->data
.read_only
= 1;
2526 if (qual
->flags
.q
.centroid
)
2527 var
->data
.centroid
= 1;
2529 if (qual
->flags
.q
.sample
)
2530 var
->data
.sample
= 1;
2532 if (state
->stage
== MESA_SHADER_GEOMETRY
&&
2533 qual
->flags
.q
.out
&& qual
->flags
.q
.stream
) {
2534 var
->data
.stream
= qual
->stream
;
2537 if (qual
->flags
.q
.patch
)
2538 var
->data
.patch
= 1;
2540 if (qual
->flags
.q
.attribute
&& state
->stage
!= MESA_SHADER_VERTEX
) {
2541 var
->type
= glsl_type::error_type
;
2542 _mesa_glsl_error(loc
, state
,
2543 "`attribute' variables may not be declared in the "
2545 _mesa_shader_stage_to_string(state
->stage
));
2548 /* Disallow layout qualifiers which may only appear on layout declarations. */
2549 if (qual
->flags
.q
.prim_type
) {
2550 _mesa_glsl_error(loc
, state
,
2551 "Primitive type may only be specified on GS input or output "
2552 "layout declaration, not on variables.");
2555 /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
2557 * "However, the const qualifier cannot be used with out or inout."
2559 * The same section of the GLSL 4.40 spec further clarifies this saying:
2561 * "The const qualifier cannot be used with out or inout, or a
2562 * compile-time error results."
2564 if (is_parameter
&& qual
->flags
.q
.constant
&& qual
->flags
.q
.out
) {
2565 _mesa_glsl_error(loc
, state
,
2566 "`const' may not be applied to `out' or `inout' "
2567 "function parameters");
2570 /* If there is no qualifier that changes the mode of the variable, leave
2571 * the setting alone.
2573 assert(var
->data
.mode
!= ir_var_temporary
);
2574 if (qual
->flags
.q
.in
&& qual
->flags
.q
.out
)
2575 var
->data
.mode
= ir_var_function_inout
;
2576 else if (qual
->flags
.q
.in
)
2577 var
->data
.mode
= is_parameter
? ir_var_function_in
: ir_var_shader_in
;
2578 else if (qual
->flags
.q
.attribute
2579 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
2580 var
->data
.mode
= ir_var_shader_in
;
2581 else if (qual
->flags
.q
.out
)
2582 var
->data
.mode
= is_parameter
? ir_var_function_out
: ir_var_shader_out
;
2583 else if (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_VERTEX
))
2584 var
->data
.mode
= ir_var_shader_out
;
2585 else if (qual
->flags
.q
.uniform
)
2586 var
->data
.mode
= ir_var_uniform
;
2587 else if (qual
->flags
.q
.buffer
)
2588 var
->data
.mode
= ir_var_shader_storage
;
2590 if (!is_parameter
&& is_varying_var(var
, state
->stage
)) {
2591 /* User-defined ins/outs are not permitted in compute shaders. */
2592 if (state
->stage
== MESA_SHADER_COMPUTE
) {
2593 _mesa_glsl_error(loc
, state
,
2594 "user-defined input and output variables are not "
2595 "permitted in compute shaders");
2598 /* This variable is being used to link data between shader stages (in
2599 * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
2600 * that is allowed for such purposes.
2602 * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
2604 * "The varying qualifier can be used only with the data types
2605 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
2608 * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
2609 * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
2611 * "Fragment inputs can only be signed and unsigned integers and
2612 * integer vectors, float, floating-point vectors, matrices, or
2613 * arrays of these. Structures cannot be input.
2615 * Similar text exists in the section on vertex shader outputs.
2617 * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
2618 * 3.00 spec allows structs as well. Varying structs are also allowed
2621 switch (var
->type
->get_scalar_type()->base_type
) {
2622 case GLSL_TYPE_FLOAT
:
2623 /* Ok in all GLSL versions */
2625 case GLSL_TYPE_UINT
:
2627 if (state
->is_version(130, 300))
2629 _mesa_glsl_error(loc
, state
,
2630 "varying variables must be of base type float in %s",
2631 state
->get_version_string());
2633 case GLSL_TYPE_STRUCT
:
2634 if (state
->is_version(150, 300))
2636 _mesa_glsl_error(loc
, state
,
2637 "varying variables may not be of type struct");
2639 case GLSL_TYPE_DOUBLE
:
2642 _mesa_glsl_error(loc
, state
, "illegal type for a varying variable");
2647 if (state
->all_invariant
&& (state
->current_function
== NULL
)) {
2648 switch (state
->stage
) {
2649 case MESA_SHADER_VERTEX
:
2650 if (var
->data
.mode
== ir_var_shader_out
)
2651 var
->data
.invariant
= true;
2653 case MESA_SHADER_TESS_CTRL
:
2654 case MESA_SHADER_TESS_EVAL
:
2655 case MESA_SHADER_GEOMETRY
:
2656 if ((var
->data
.mode
== ir_var_shader_in
)
2657 || (var
->data
.mode
== ir_var_shader_out
))
2658 var
->data
.invariant
= true;
2660 case MESA_SHADER_FRAGMENT
:
2661 if (var
->data
.mode
== ir_var_shader_in
)
2662 var
->data
.invariant
= true;
2664 case MESA_SHADER_COMPUTE
:
2665 /* Invariance isn't meaningful in compute shaders. */
2670 var
->data
.interpolation
=
2671 interpret_interpolation_qualifier(qual
, (ir_variable_mode
) var
->data
.mode
,
2674 var
->data
.pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
2675 var
->data
.origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
2676 if ((qual
->flags
.q
.origin_upper_left
|| qual
->flags
.q
.pixel_center_integer
)
2677 && (strcmp(var
->name
, "gl_FragCoord") != 0)) {
2678 const char *const qual_string
= (qual
->flags
.q
.origin_upper_left
)
2679 ? "origin_upper_left" : "pixel_center_integer";
2681 _mesa_glsl_error(loc
, state
,
2682 "layout qualifier `%s' can only be applied to "
2683 "fragment shader input `gl_FragCoord'",
2687 if (var
->name
!= NULL
&& strcmp(var
->name
, "gl_FragCoord") == 0) {
2689 /* Section 4.3.8.1, page 39 of GLSL 1.50 spec says:
2691 * "Within any shader, the first redeclarations of gl_FragCoord
2692 * must appear before any use of gl_FragCoord."
2694 * Generate a compiler error if above condition is not met by the
2697 ir_variable
*earlier
= state
->symbols
->get_variable("gl_FragCoord");
2698 if (earlier
!= NULL
&&
2699 earlier
->data
.used
&&
2700 !state
->fs_redeclares_gl_fragcoord
) {
2701 _mesa_glsl_error(loc
, state
,
2702 "gl_FragCoord used before its first redeclaration "
2703 "in fragment shader");
2706 /* Make sure all gl_FragCoord redeclarations specify the same layout
2709 if (is_conflicting_fragcoord_redeclaration(state
, qual
)) {
2710 const char *const qual_string
=
2711 get_layout_qualifier_string(qual
->flags
.q
.origin_upper_left
,
2712 qual
->flags
.q
.pixel_center_integer
);
2714 const char *const state_string
=
2715 get_layout_qualifier_string(state
->fs_origin_upper_left
,
2716 state
->fs_pixel_center_integer
);
2718 _mesa_glsl_error(loc
, state
,
2719 "gl_FragCoord redeclared with different layout "
2720 "qualifiers (%s) and (%s) ",
2724 state
->fs_origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
2725 state
->fs_pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
2726 state
->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers
=
2727 !qual
->flags
.q
.origin_upper_left
&& !qual
->flags
.q
.pixel_center_integer
;
2728 state
->fs_redeclares_gl_fragcoord
=
2729 state
->fs_origin_upper_left
||
2730 state
->fs_pixel_center_integer
||
2731 state
->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers
;
2734 if (qual
->flags
.q
.explicit_location
) {
2735 validate_explicit_location(qual
, var
, state
, loc
);
2736 } else if (qual
->flags
.q
.explicit_index
) {
2737 _mesa_glsl_error(loc
, state
, "explicit index requires explicit location");
2740 if (qual
->flags
.q
.explicit_binding
&&
2741 validate_binding_qualifier(state
, loc
, var
, qual
)) {
2742 var
->data
.explicit_binding
= true;
2743 var
->data
.binding
= qual
->binding
;
2746 if (var
->type
->contains_atomic()) {
2747 if (var
->data
.mode
== ir_var_uniform
) {
2748 if (var
->data
.explicit_binding
) {
2750 &state
->atomic_counter_offsets
[var
->data
.binding
];
2752 if (*offset
% ATOMIC_COUNTER_SIZE
)
2753 _mesa_glsl_error(loc
, state
,
2754 "misaligned atomic counter offset");
2756 var
->data
.atomic
.offset
= *offset
;
2757 *offset
+= var
->type
->atomic_size();
2760 _mesa_glsl_error(loc
, state
,
2761 "atomic counters require explicit binding point");
2763 } else if (var
->data
.mode
!= ir_var_function_in
) {
2764 _mesa_glsl_error(loc
, state
, "atomic counters may only be declared as "
2765 "function parameters or uniform-qualified "
2766 "global variables");
2770 /* Does the declaration use the deprecated 'attribute' or 'varying'
2773 const bool uses_deprecated_qualifier
= qual
->flags
.q
.attribute
2774 || qual
->flags
.q
.varying
;
2777 /* Validate auxiliary storage qualifiers */
2779 /* From section 4.3.4 of the GLSL 1.30 spec:
2780 * "It is an error to use centroid in in a vertex shader."
2782 * From section 4.3.4 of the GLSL ES 3.00 spec:
2783 * "It is an error to use centroid in or interpolation qualifiers in
2784 * a vertex shader input."
2787 /* Section 4.3.6 of the GLSL 1.30 specification states:
2788 * "It is an error to use centroid out in a fragment shader."
2790 * The GL_ARB_shading_language_420pack extension specification states:
2791 * "It is an error to use auxiliary storage qualifiers or interpolation
2792 * qualifiers on an output in a fragment shader."
2794 if (qual
->flags
.q
.sample
&& (!is_varying_var(var
, state
->stage
) || uses_deprecated_qualifier
)) {
2795 _mesa_glsl_error(loc
, state
,
2796 "sample qualifier may only be used on `in` or `out` "
2797 "variables between shader stages");
2799 if (qual
->flags
.q
.centroid
&& !is_varying_var(var
, state
->stage
)) {
2800 _mesa_glsl_error(loc
, state
,
2801 "centroid qualifier may only be used with `in', "
2802 "`out' or `varying' variables between shader stages");
2806 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
2807 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
2808 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
2809 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
2810 * These extensions and all following extensions that add the 'layout'
2811 * keyword have been modified to require the use of 'in' or 'out'.
2813 * The following extension do not allow the deprecated keywords:
2815 * GL_AMD_conservative_depth
2816 * GL_ARB_conservative_depth
2817 * GL_ARB_gpu_shader5
2818 * GL_ARB_separate_shader_objects
2819 * GL_ARB_tessellation_shader
2820 * GL_ARB_transform_feedback3
2821 * GL_ARB_uniform_buffer_object
2823 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
2824 * allow layout with the deprecated keywords.
2826 const bool relaxed_layout_qualifier_checking
=
2827 state
->ARB_fragment_coord_conventions_enable
;
2829 if (qual
->has_layout() && uses_deprecated_qualifier
) {
2830 if (relaxed_layout_qualifier_checking
) {
2831 _mesa_glsl_warning(loc
, state
,
2832 "`layout' qualifier may not be used with "
2833 "`attribute' or `varying'");
2835 _mesa_glsl_error(loc
, state
,
2836 "`layout' qualifier may not be used with "
2837 "`attribute' or `varying'");
2841 /* Layout qualifiers for gl_FragDepth, which are enabled by extension
2842 * AMD_conservative_depth.
2844 int depth_layout_count
= qual
->flags
.q
.depth_any
2845 + qual
->flags
.q
.depth_greater
2846 + qual
->flags
.q
.depth_less
2847 + qual
->flags
.q
.depth_unchanged
;
2848 if (depth_layout_count
> 0
2849 && !state
->AMD_conservative_depth_enable
2850 && !state
->ARB_conservative_depth_enable
) {
2851 _mesa_glsl_error(loc
, state
,
2852 "extension GL_AMD_conservative_depth or "
2853 "GL_ARB_conservative_depth must be enabled "
2854 "to use depth layout qualifiers");
2855 } else if (depth_layout_count
> 0
2856 && strcmp(var
->name
, "gl_FragDepth") != 0) {
2857 _mesa_glsl_error(loc
, state
,
2858 "depth layout qualifiers can be applied only to "
2860 } else if (depth_layout_count
> 1
2861 && strcmp(var
->name
, "gl_FragDepth") == 0) {
2862 _mesa_glsl_error(loc
, state
,
2863 "at most one depth layout qualifier can be applied to "
2866 if (qual
->flags
.q
.depth_any
)
2867 var
->data
.depth_layout
= ir_depth_layout_any
;
2868 else if (qual
->flags
.q
.depth_greater
)
2869 var
->data
.depth_layout
= ir_depth_layout_greater
;
2870 else if (qual
->flags
.q
.depth_less
)
2871 var
->data
.depth_layout
= ir_depth_layout_less
;
2872 else if (qual
->flags
.q
.depth_unchanged
)
2873 var
->data
.depth_layout
= ir_depth_layout_unchanged
;
2875 var
->data
.depth_layout
= ir_depth_layout_none
;
2877 if (qual
->flags
.q
.std140
||
2878 qual
->flags
.q
.packed
||
2879 qual
->flags
.q
.shared
) {
2880 _mesa_glsl_error(loc
, state
,
2881 "uniform block layout qualifiers std140, packed, and "
2882 "shared can only be applied to uniform blocks, not "
2886 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
2887 validate_matrix_layout_for_type(state
, loc
, var
->type
, var
);
2890 apply_image_qualifier_to_variable(qual
, var
, state
, loc
);
2892 /* From section 4.4.1.3 of the GLSL 4.50 specification (Fragment Shader
2895 * "Fragment shaders also allow the following layout qualifier on in only
2896 * (not with variable declarations)
2897 * layout-qualifier-id
2898 * early_fragment_tests
2901 if (qual
->flags
.q
.early_fragment_tests
) {
2902 _mesa_glsl_error(loc
, state
, "early_fragment_tests layout qualifier only "
2903 "valid in fragment shader input layout declaration.");
2908 * Get the variable that is being redeclared by this declaration
2910 * Semantic checks to verify the validity of the redeclaration are also
2911 * performed. If semantic checks fail, compilation error will be emitted via
2912 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
2915 * A pointer to an existing variable in the current scope if the declaration
2916 * is a redeclaration, \c NULL otherwise.
2918 static ir_variable
*
2919 get_variable_being_redeclared(ir_variable
*var
, YYLTYPE loc
,
2920 struct _mesa_glsl_parse_state
*state
,
2921 bool allow_all_redeclarations
)
2923 /* Check if this declaration is actually a re-declaration, either to
2924 * resize an array or add qualifiers to an existing variable.
2926 * This is allowed for variables in the current scope, or when at
2927 * global scope (for built-ins in the implicit outer scope).
2929 ir_variable
*earlier
= state
->symbols
->get_variable(var
->name
);
2930 if (earlier
== NULL
||
2931 (state
->current_function
!= NULL
&&
2932 !state
->symbols
->name_declared_this_scope(var
->name
))) {
2937 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
2939 * "It is legal to declare an array without a size and then
2940 * later re-declare the same name as an array of the same
2941 * type and specify a size."
2943 if (earlier
->type
->is_unsized_array() && var
->type
->is_array()
2944 && (var
->type
->fields
.array
== earlier
->type
->fields
.array
)) {
2945 /* FINISHME: This doesn't match the qualifiers on the two
2946 * FINISHME: declarations. It's not 100% clear whether this is
2947 * FINISHME: required or not.
2950 const unsigned size
= unsigned(var
->type
->array_size());
2951 check_builtin_array_max_size(var
->name
, size
, loc
, state
);
2952 if ((size
> 0) && (size
<= earlier
->data
.max_array_access
)) {
2953 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
2955 earlier
->data
.max_array_access
);
2958 earlier
->type
= var
->type
;
2961 } else if ((state
->ARB_fragment_coord_conventions_enable
||
2962 state
->is_version(150, 0))
2963 && strcmp(var
->name
, "gl_FragCoord") == 0
2964 && earlier
->type
== var
->type
2965 && earlier
->data
.mode
== var
->data
.mode
) {
2966 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
2969 earlier
->data
.origin_upper_left
= var
->data
.origin_upper_left
;
2970 earlier
->data
.pixel_center_integer
= var
->data
.pixel_center_integer
;
2972 /* According to section 4.3.7 of the GLSL 1.30 spec,
2973 * the following built-in varaibles can be redeclared with an
2974 * interpolation qualifier:
2977 * * gl_FrontSecondaryColor
2978 * * gl_BackSecondaryColor
2980 * * gl_SecondaryColor
2982 } else if (state
->is_version(130, 0)
2983 && (strcmp(var
->name
, "gl_FrontColor") == 0
2984 || strcmp(var
->name
, "gl_BackColor") == 0
2985 || strcmp(var
->name
, "gl_FrontSecondaryColor") == 0
2986 || strcmp(var
->name
, "gl_BackSecondaryColor") == 0
2987 || strcmp(var
->name
, "gl_Color") == 0
2988 || strcmp(var
->name
, "gl_SecondaryColor") == 0)
2989 && earlier
->type
== var
->type
2990 && earlier
->data
.mode
== var
->data
.mode
) {
2991 earlier
->data
.interpolation
= var
->data
.interpolation
;
2993 /* Layout qualifiers for gl_FragDepth. */
2994 } else if ((state
->AMD_conservative_depth_enable
||
2995 state
->ARB_conservative_depth_enable
)
2996 && strcmp(var
->name
, "gl_FragDepth") == 0
2997 && earlier
->type
== var
->type
2998 && earlier
->data
.mode
== var
->data
.mode
) {
3000 /** From the AMD_conservative_depth spec:
3001 * Within any shader, the first redeclarations of gl_FragDepth
3002 * must appear before any use of gl_FragDepth.
3004 if (earlier
->data
.used
) {
3005 _mesa_glsl_error(&loc
, state
,
3006 "the first redeclaration of gl_FragDepth "
3007 "must appear before any use of gl_FragDepth");
3010 /* Prevent inconsistent redeclaration of depth layout qualifier. */
3011 if (earlier
->data
.depth_layout
!= ir_depth_layout_none
3012 && earlier
->data
.depth_layout
!= var
->data
.depth_layout
) {
3013 _mesa_glsl_error(&loc
, state
,
3014 "gl_FragDepth: depth layout is declared here "
3015 "as '%s, but it was previously declared as "
3017 depth_layout_string(var
->data
.depth_layout
),
3018 depth_layout_string(earlier
->data
.depth_layout
));
3021 earlier
->data
.depth_layout
= var
->data
.depth_layout
;
3023 } else if (allow_all_redeclarations
) {
3024 if (earlier
->data
.mode
!= var
->data
.mode
) {
3025 _mesa_glsl_error(&loc
, state
,
3026 "redeclaration of `%s' with incorrect qualifiers",
3028 } else if (earlier
->type
!= var
->type
) {
3029 _mesa_glsl_error(&loc
, state
,
3030 "redeclaration of `%s' has incorrect type",
3034 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
3041 * Generate the IR for an initializer in a variable declaration
3044 process_initializer(ir_variable
*var
, ast_declaration
*decl
,
3045 ast_fully_specified_type
*type
,
3046 exec_list
*initializer_instructions
,
3047 struct _mesa_glsl_parse_state
*state
)
3049 ir_rvalue
*result
= NULL
;
3051 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
3053 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
3055 * "All uniform variables are read-only and are initialized either
3056 * directly by an application via API commands, or indirectly by
3059 if (var
->data
.mode
== ir_var_uniform
) {
3060 state
->check_version(120, 0, &initializer_loc
,
3061 "cannot initialize uniforms");
3064 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
3066 * "Buffer variables cannot have initializers."
3068 if (var
->data
.mode
== ir_var_shader_storage
) {
3069 _mesa_glsl_error(& initializer_loc
, state
,
3070 "SSBO variables cannot have initializers");
3073 /* From section 4.1.7 of the GLSL 4.40 spec:
3075 * "Opaque variables [...] are initialized only through the
3076 * OpenGL API; they cannot be declared with an initializer in a
3079 if (var
->type
->contains_opaque()) {
3080 _mesa_glsl_error(& initializer_loc
, state
,
3081 "cannot initialize opaque variable");
3084 if ((var
->data
.mode
== ir_var_shader_in
) && (state
->current_function
== NULL
)) {
3085 _mesa_glsl_error(& initializer_loc
, state
,
3086 "cannot initialize %s shader input / %s",
3087 _mesa_shader_stage_to_string(state
->stage
),
3088 (state
->stage
== MESA_SHADER_VERTEX
)
3089 ? "attribute" : "varying");
3092 /* If the initializer is an ast_aggregate_initializer, recursively store
3093 * type information from the LHS into it, so that its hir() function can do
3096 if (decl
->initializer
->oper
== ast_aggregate
)
3097 _mesa_ast_set_aggregate_type(var
->type
, decl
->initializer
);
3099 ir_dereference
*const lhs
= new(state
) ir_dereference_variable(var
);
3100 ir_rvalue
*rhs
= decl
->initializer
->hir(initializer_instructions
, state
);
3102 /* Calculate the constant value if this is a const or uniform
3105 if (type
->qualifier
.flags
.q
.constant
3106 || type
->qualifier
.flags
.q
.uniform
) {
3107 ir_rvalue
*new_rhs
= validate_assignment(state
, initializer_loc
,
3109 if (new_rhs
!= NULL
) {
3112 ir_constant
*constant_value
= rhs
->constant_expression_value();
3113 if (!constant_value
) {
3114 /* If ARB_shading_language_420pack is enabled, initializers of
3115 * const-qualified local variables do not have to be constant
3116 * expressions. Const-qualified global variables must still be
3117 * initialized with constant expressions.
3119 if (!state
->ARB_shading_language_420pack_enable
3120 || state
->current_function
== NULL
) {
3121 _mesa_glsl_error(& initializer_loc
, state
,
3122 "initializer of %s variable `%s' must be a "
3123 "constant expression",
3124 (type
->qualifier
.flags
.q
.constant
)
3125 ? "const" : "uniform",
3127 if (var
->type
->is_numeric()) {
3128 /* Reduce cascading errors. */
3129 var
->constant_value
= ir_constant::zero(state
, var
->type
);
3133 rhs
= constant_value
;
3134 var
->constant_value
= constant_value
;
3137 if (var
->type
->is_numeric()) {
3138 /* Reduce cascading errors. */
3139 var
->constant_value
= ir_constant::zero(state
, var
->type
);
3144 if (rhs
&& !rhs
->type
->is_error()) {
3145 bool temp
= var
->data
.read_only
;
3146 if (type
->qualifier
.flags
.q
.constant
)
3147 var
->data
.read_only
= false;
3149 /* Never emit code to initialize a uniform.
3151 const glsl_type
*initializer_type
;
3152 if (!type
->qualifier
.flags
.q
.uniform
) {
3153 do_assignment(initializer_instructions
, state
,
3158 type
->get_location());
3159 initializer_type
= result
->type
;
3161 initializer_type
= rhs
->type
;
3163 var
->constant_initializer
= rhs
->constant_expression_value();
3164 var
->data
.has_initializer
= true;
3166 /* If the declared variable is an unsized array, it must inherrit
3167 * its full type from the initializer. A declaration such as
3169 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
3173 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
3175 * The assignment generated in the if-statement (below) will also
3176 * automatically handle this case for non-uniforms.
3178 * If the declared variable is not an array, the types must
3179 * already match exactly. As a result, the type assignment
3180 * here can be done unconditionally. For non-uniforms the call
3181 * to do_assignment can change the type of the initializer (via
3182 * the implicit conversion rules). For uniforms the initializer
3183 * must be a constant expression, and the type of that expression
3184 * was validated above.
3186 var
->type
= initializer_type
;
3188 var
->data
.read_only
= temp
;
3195 validate_layout_qualifier_vertex_count(struct _mesa_glsl_parse_state
*state
,
3196 YYLTYPE loc
, ir_variable
*var
,
3197 unsigned num_vertices
,
3199 const char *var_category
)
3201 if (var
->type
->is_unsized_array()) {
3202 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says:
3204 * All geometry shader input unsized array declarations will be
3205 * sized by an earlier input layout qualifier, when present, as per
3206 * the following table.
3208 * Followed by a table mapping each allowed input layout qualifier to
3209 * the corresponding input length.
3211 * Similarly for tessellation control shader outputs.
3213 if (num_vertices
!= 0)
3214 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
3217 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec
3218 * includes the following examples of compile-time errors:
3220 * // code sequence within one shader...
3221 * in vec4 Color1[]; // size unknown
3222 * ...Color1.length()...// illegal, length() unknown
3223 * in vec4 Color2[2]; // size is 2
3224 * ...Color1.length()...// illegal, Color1 still has no size
3225 * in vec4 Color3[3]; // illegal, input sizes are inconsistent
3226 * layout(lines) in; // legal, input size is 2, matching
3227 * in vec4 Color4[3]; // illegal, contradicts layout
3230 * To detect the case illustrated by Color3, we verify that the size of
3231 * an explicitly-sized array matches the size of any previously declared
3232 * explicitly-sized array. To detect the case illustrated by Color4, we
3233 * verify that the size of an explicitly-sized array is consistent with
3234 * any previously declared input layout.
3236 if (num_vertices
!= 0 && var
->type
->length
!= num_vertices
) {
3237 _mesa_glsl_error(&loc
, state
,
3238 "%s size contradicts previously declared layout "
3239 "(size is %u, but layout requires a size of %u)",
3240 var_category
, var
->type
->length
, num_vertices
);
3241 } else if (*size
!= 0 && var
->type
->length
!= *size
) {
3242 _mesa_glsl_error(&loc
, state
,
3243 "%s sizes are inconsistent (size is %u, but a "
3244 "previous declaration has size %u)",
3245 var_category
, var
->type
->length
, *size
);
3247 *size
= var
->type
->length
;
3253 handle_tess_ctrl_shader_output_decl(struct _mesa_glsl_parse_state
*state
,
3254 YYLTYPE loc
, ir_variable
*var
)
3256 unsigned num_vertices
= 0;
3258 if (state
->tcs_output_vertices_specified
) {
3259 num_vertices
= state
->out_qualifier
->vertices
;
3262 if (!var
->type
->is_array() && !var
->data
.patch
) {
3263 _mesa_glsl_error(&loc
, state
,
3264 "tessellation control shader outputs must be arrays");
3266 /* To avoid cascading failures, short circuit the checks below. */
3270 if (var
->data
.patch
)
3273 validate_layout_qualifier_vertex_count(state
, loc
, var
, num_vertices
,
3274 &state
->tcs_output_size
,
3275 "geometry shader input");
3279 * Do additional processing necessary for tessellation control/evaluation shader
3280 * input declarations. This covers both interface block arrays and bare input
3284 handle_tess_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
3285 YYLTYPE loc
, ir_variable
*var
)
3287 if (!var
->type
->is_array() && !var
->data
.patch
) {
3288 _mesa_glsl_error(&loc
, state
,
3289 "per-vertex tessellation shader inputs must be arrays");
3290 /* Avoid cascading failures. */
3294 if (var
->data
.patch
)
3297 /* Unsized arrays are implicitly sized to gl_MaxPatchVertices. */
3298 if (var
->type
->is_unsized_array()) {
3299 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
3300 state
->Const
.MaxPatchVertices
);
3306 * Do additional processing necessary for geometry shader input declarations
3307 * (this covers both interface blocks arrays and bare input variables).
3310 handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
3311 YYLTYPE loc
, ir_variable
*var
)
3313 unsigned num_vertices
= 0;
3315 if (state
->gs_input_prim_type_specified
) {
3316 num_vertices
= vertices_per_prim(state
->in_qualifier
->prim_type
);
3319 /* Geometry shader input variables must be arrays. Caller should have
3320 * reported an error for this.
3322 if (!var
->type
->is_array()) {
3323 assert(state
->error
);
3325 /* To avoid cascading failures, short circuit the checks below. */
3329 validate_layout_qualifier_vertex_count(state
, loc
, var
, num_vertices
,
3330 &state
->gs_input_size
,
3331 "geometry shader input");
3335 validate_identifier(const char *identifier
, YYLTYPE loc
,
3336 struct _mesa_glsl_parse_state
*state
)
3338 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
3340 * "Identifiers starting with "gl_" are reserved for use by
3341 * OpenGL, and may not be declared in a shader as either a
3342 * variable or a function."
3344 if (is_gl_identifier(identifier
)) {
3345 _mesa_glsl_error(&loc
, state
,
3346 "identifier `%s' uses reserved `gl_' prefix",
3348 } else if (strstr(identifier
, "__")) {
3349 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
3352 * "In addition, all identifiers containing two
3353 * consecutive underscores (__) are reserved as
3354 * possible future keywords."
3356 * The intention is that names containing __ are reserved for internal
3357 * use by the implementation, and names prefixed with GL_ are reserved
3358 * for use by Khronos. Names simply containing __ are dangerous to use,
3359 * but should be allowed.
3361 * A future version of the GLSL specification will clarify this.
3363 _mesa_glsl_warning(&loc
, state
,
3364 "identifier `%s' uses reserved `__' string",
3370 precision_qualifier_allowed(const glsl_type
*type
)
3372 /* Precision qualifiers apply to floating point, integer and sampler
3375 * Section 4.5.2 (Precision Qualifiers) of the GLSL 1.30 spec says:
3376 * "Any floating point or any integer declaration can have the type
3377 * preceded by one of these precision qualifiers [...] Literal
3378 * constants do not have precision qualifiers. Neither do Boolean
3381 * Section 4.5 (Precision and Precision Qualifiers) of the GLSL 1.30
3384 * "Precision qualifiers are added for code portability with OpenGL
3385 * ES, not for functionality. They have the same syntax as in OpenGL
3388 * Section 8 (Built-In Functions) of the GLSL ES 1.00 spec says:
3390 * "uniform lowp sampler2D sampler;
3393 * lowp vec4 col = texture2D (sampler, coord);
3394 * // texture2D returns lowp"
3396 * From this, we infer that GLSL 1.30 (and later) should allow precision
3397 * qualifiers on sampler types just like float and integer types.
3399 return type
->is_float()
3400 || type
->is_integer()
3401 || type
->is_record()
3402 || type
->is_sampler();
3406 ast_declarator_list::hir(exec_list
*instructions
,
3407 struct _mesa_glsl_parse_state
*state
)
3410 const struct glsl_type
*decl_type
;
3411 const char *type_name
= NULL
;
3412 ir_rvalue
*result
= NULL
;
3413 YYLTYPE loc
= this->get_location();
3415 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
3417 * "To ensure that a particular output variable is invariant, it is
3418 * necessary to use the invariant qualifier. It can either be used to
3419 * qualify a previously declared variable as being invariant
3421 * invariant gl_Position; // make existing gl_Position be invariant"
3423 * In these cases the parser will set the 'invariant' flag in the declarator
3424 * list, and the type will be NULL.
3426 if (this->invariant
) {
3427 assert(this->type
== NULL
);
3429 if (state
->current_function
!= NULL
) {
3430 _mesa_glsl_error(& loc
, state
,
3431 "all uses of `invariant' keyword must be at global "
3435 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
3436 assert(decl
->array_specifier
== NULL
);
3437 assert(decl
->initializer
== NULL
);
3439 ir_variable
*const earlier
=
3440 state
->symbols
->get_variable(decl
->identifier
);
3441 if (earlier
== NULL
) {
3442 _mesa_glsl_error(& loc
, state
,
3443 "undeclared variable `%s' cannot be marked "
3444 "invariant", decl
->identifier
);
3445 } else if (!is_varying_var(earlier
, state
->stage
)) {
3446 _mesa_glsl_error(&loc
, state
,
3447 "`%s' cannot be marked invariant; interfaces between "
3448 "shader stages only.", decl
->identifier
);
3449 } else if (earlier
->data
.used
) {
3450 _mesa_glsl_error(& loc
, state
,
3451 "variable `%s' may not be redeclared "
3452 "`invariant' after being used",
3455 earlier
->data
.invariant
= true;
3459 /* Invariant redeclarations do not have r-values.
3464 if (this->precise
) {
3465 assert(this->type
== NULL
);
3467 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
3468 assert(decl
->array_specifier
== NULL
);
3469 assert(decl
->initializer
== NULL
);
3471 ir_variable
*const earlier
=
3472 state
->symbols
->get_variable(decl
->identifier
);
3473 if (earlier
== NULL
) {
3474 _mesa_glsl_error(& loc
, state
,
3475 "undeclared variable `%s' cannot be marked "
3476 "precise", decl
->identifier
);
3477 } else if (state
->current_function
!= NULL
&&
3478 !state
->symbols
->name_declared_this_scope(decl
->identifier
)) {
3479 /* Note: we have to check if we're in a function, since
3480 * builtins are treated as having come from another scope.
3482 _mesa_glsl_error(& loc
, state
,
3483 "variable `%s' from an outer scope may not be "
3484 "redeclared `precise' in this scope",
3486 } else if (earlier
->data
.used
) {
3487 _mesa_glsl_error(& loc
, state
,
3488 "variable `%s' may not be redeclared "
3489 "`precise' after being used",
3492 earlier
->data
.precise
= true;
3496 /* Precise redeclarations do not have r-values either. */
3500 assert(this->type
!= NULL
);
3501 assert(!this->invariant
);
3502 assert(!this->precise
);
3504 /* The type specifier may contain a structure definition. Process that
3505 * before any of the variable declarations.
3507 (void) this->type
->specifier
->hir(instructions
, state
);
3509 decl_type
= this->type
->glsl_type(& type_name
, state
);
3511 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
3512 * "Buffer variables may only be declared inside interface blocks
3513 * (section 4.3.9 “Interface Blocks”), which are then referred to as
3514 * shader storage blocks. It is a compile-time error to declare buffer
3515 * variables at global scope (outside a block)."
3517 if (type
->qualifier
.flags
.q
.buffer
&& !decl_type
->is_interface()) {
3518 _mesa_glsl_error(&loc
, state
,
3519 "buffer variables cannot be declared outside "
3520 "interface blocks");
3523 /* An offset-qualified atomic counter declaration sets the default
3524 * offset for the next declaration within the same atomic counter
3527 if (decl_type
&& decl_type
->contains_atomic()) {
3528 if (type
->qualifier
.flags
.q
.explicit_binding
&&
3529 type
->qualifier
.flags
.q
.explicit_offset
)
3530 state
->atomic_counter_offsets
[type
->qualifier
.binding
] =
3531 type
->qualifier
.offset
;
3534 if (this->declarations
.is_empty()) {
3535 /* If there is no structure involved in the program text, there are two
3536 * possible scenarios:
3538 * - The program text contained something like 'vec4;'. This is an
3539 * empty declaration. It is valid but weird. Emit a warning.
3541 * - The program text contained something like 'S;' and 'S' is not the
3542 * name of a known structure type. This is both invalid and weird.
3545 * - The program text contained something like 'mediump float;'
3546 * when the programmer probably meant 'precision mediump
3547 * float;' Emit a warning with a description of what they
3548 * probably meant to do.
3550 * Note that if decl_type is NULL and there is a structure involved,
3551 * there must have been some sort of error with the structure. In this
3552 * case we assume that an error was already generated on this line of
3553 * code for the structure. There is no need to generate an additional,
3556 assert(this->type
->specifier
->structure
== NULL
|| decl_type
!= NULL
3559 if (decl_type
== NULL
) {
3560 _mesa_glsl_error(&loc
, state
,
3561 "invalid type `%s' in empty declaration",
3563 } else if (decl_type
->base_type
== GLSL_TYPE_ATOMIC_UINT
) {
3564 /* Empty atomic counter declarations are allowed and useful
3565 * to set the default offset qualifier.
3568 } else if (this->type
->qualifier
.precision
!= ast_precision_none
) {
3569 if (this->type
->specifier
->structure
!= NULL
) {
3570 _mesa_glsl_error(&loc
, state
,
3571 "precision qualifiers can't be applied "
3574 static const char *const precision_names
[] = {
3581 _mesa_glsl_warning(&loc
, state
,
3582 "empty declaration with precision qualifier, "
3583 "to set the default precision, use "
3584 "`precision %s %s;'",
3585 precision_names
[this->type
->qualifier
.precision
],
3588 } else if (this->type
->specifier
->structure
== NULL
) {
3589 _mesa_glsl_warning(&loc
, state
, "empty declaration");
3593 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
3594 const struct glsl_type
*var_type
;
3597 /* FINISHME: Emit a warning if a variable declaration shadows a
3598 * FINISHME: declaration at a higher scope.
3601 if ((decl_type
== NULL
) || decl_type
->is_void()) {
3602 if (type_name
!= NULL
) {
3603 _mesa_glsl_error(& loc
, state
,
3604 "invalid type `%s' in declaration of `%s'",
3605 type_name
, decl
->identifier
);
3607 _mesa_glsl_error(& loc
, state
,
3608 "invalid type in declaration of `%s'",
3614 var_type
= process_array_type(&loc
, decl_type
, decl
->array_specifier
,
3617 var
= new(ctx
) ir_variable(var_type
, decl
->identifier
, ir_var_auto
);
3619 /* The 'varying in' and 'varying out' qualifiers can only be used with
3620 * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
3623 if (this->type
->qualifier
.flags
.q
.varying
) {
3624 if (this->type
->qualifier
.flags
.q
.in
) {
3625 _mesa_glsl_error(& loc
, state
,
3626 "`varying in' qualifier in declaration of "
3627 "`%s' only valid for geometry shaders using "
3628 "ARB_geometry_shader4 or EXT_geometry_shader4",
3630 } else if (this->type
->qualifier
.flags
.q
.out
) {
3631 _mesa_glsl_error(& loc
, state
,
3632 "`varying out' qualifier in declaration of "
3633 "`%s' only valid for geometry shaders using "
3634 "ARB_geometry_shader4 or EXT_geometry_shader4",
3639 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
3641 * "Global variables can only use the qualifiers const,
3642 * attribute, uniform, or varying. Only one may be
3645 * Local variables can only use the qualifier const."
3647 * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
3648 * any extension that adds the 'layout' keyword.
3650 if (!state
->is_version(130, 300)
3651 && !state
->has_explicit_attrib_location()
3652 && !state
->has_separate_shader_objects()
3653 && !state
->ARB_fragment_coord_conventions_enable
) {
3654 if (this->type
->qualifier
.flags
.q
.out
) {
3655 _mesa_glsl_error(& loc
, state
,
3656 "`out' qualifier in declaration of `%s' "
3657 "only valid for function parameters in %s",
3658 decl
->identifier
, state
->get_version_string());
3660 if (this->type
->qualifier
.flags
.q
.in
) {
3661 _mesa_glsl_error(& loc
, state
,
3662 "`in' qualifier in declaration of `%s' "
3663 "only valid for function parameters in %s",
3664 decl
->identifier
, state
->get_version_string());
3666 /* FINISHME: Test for other invalid qualifiers. */
3669 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
3672 if (this->type
->qualifier
.flags
.q
.invariant
) {
3673 if (!is_varying_var(var
, state
->stage
)) {
3674 _mesa_glsl_error(&loc
, state
,
3675 "`%s' cannot be marked invariant; interfaces between "
3676 "shader stages only", var
->name
);
3680 if (state
->current_function
!= NULL
) {
3681 const char *mode
= NULL
;
3682 const char *extra
= "";
3684 /* There is no need to check for 'inout' here because the parser will
3685 * only allow that in function parameter lists.
3687 if (this->type
->qualifier
.flags
.q
.attribute
) {
3689 } else if (this->type
->qualifier
.flags
.q
.uniform
) {
3691 } else if (this->type
->qualifier
.flags
.q
.varying
) {
3693 } else if (this->type
->qualifier
.flags
.q
.in
) {
3695 extra
= " or in function parameter list";
3696 } else if (this->type
->qualifier
.flags
.q
.out
) {
3698 extra
= " or in function parameter list";
3702 _mesa_glsl_error(& loc
, state
,
3703 "%s variable `%s' must be declared at "
3705 mode
, var
->name
, extra
);
3707 } else if (var
->data
.mode
== ir_var_shader_in
) {
3708 var
->data
.read_only
= true;
3710 if (state
->stage
== MESA_SHADER_VERTEX
) {
3711 bool error_emitted
= false;
3713 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
3715 * "Vertex shader inputs can only be float, floating-point
3716 * vectors, matrices, signed and unsigned integers and integer
3717 * vectors. Vertex shader inputs can also form arrays of these
3718 * types, but not structures."
3720 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
3722 * "Vertex shader inputs can only be float, floating-point
3723 * vectors, matrices, signed and unsigned integers and integer
3724 * vectors. They cannot be arrays or structures."
3726 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
3728 * "The attribute qualifier can be used only with float,
3729 * floating-point vectors, and matrices. Attribute variables
3730 * cannot be declared as arrays or structures."
3732 * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
3734 * "Vertex shader inputs can only be float, floating-point
3735 * vectors, matrices, signed and unsigned integers and integer
3736 * vectors. Vertex shader inputs cannot be arrays or
3739 const glsl_type
*check_type
= var
->type
->without_array();
3741 switch (check_type
->base_type
) {
3742 case GLSL_TYPE_FLOAT
:
3744 case GLSL_TYPE_UINT
:
3746 if (state
->is_version(120, 300))
3748 case GLSL_TYPE_DOUBLE
:
3749 if (check_type
->base_type
== GLSL_TYPE_DOUBLE
&& (state
->is_version(410, 0) || state
->ARB_vertex_attrib_64bit_enable
))
3753 _mesa_glsl_error(& loc
, state
,
3754 "vertex shader input / attribute cannot have "
3756 var
->type
->is_array() ? "array of " : "",
3758 error_emitted
= true;
3761 if (!error_emitted
&& var
->type
->is_array() &&
3762 !state
->check_version(150, 0, &loc
,
3763 "vertex shader input / attribute "
3764 "cannot have array type")) {
3765 error_emitted
= true;
3767 } else if (state
->stage
== MESA_SHADER_GEOMETRY
) {
3768 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
3770 * Geometry shader input variables get the per-vertex values
3771 * written out by vertex shader output variables of the same
3772 * names. Since a geometry shader operates on a set of
3773 * vertices, each input varying variable (or input block, see
3774 * interface blocks below) needs to be declared as an array.
3776 if (!var
->type
->is_array()) {
3777 _mesa_glsl_error(&loc
, state
,
3778 "geometry shader inputs must be arrays");
3781 handle_geometry_shader_input_decl(state
, loc
, var
);
3782 } else if (state
->stage
== MESA_SHADER_FRAGMENT
) {
3783 /* From section 4.3.4 (Input Variables) of the GLSL ES 3.10 spec:
3785 * It is a compile-time error to declare a fragment shader
3786 * input with, or that contains, any of the following types:
3790 * * An array of arrays
3791 * * An array of structures
3792 * * A structure containing an array
3793 * * A structure containing a structure
3795 if (state
->es_shader
) {
3796 const glsl_type
*check_type
= var
->type
->without_array();
3797 if (check_type
->is_boolean() ||
3798 check_type
->contains_opaque()) {
3799 _mesa_glsl_error(&loc
, state
,
3800 "fragment shader input cannot have type %s",
3803 if (var
->type
->is_array() &&
3804 var
->type
->fields
.array
->is_array()) {
3805 _mesa_glsl_error(&loc
, state
,
3807 "cannot have an array of arrays",
3808 _mesa_shader_stage_to_string(state
->stage
));
3810 if (var
->type
->is_array() &&
3811 var
->type
->fields
.array
->is_record()) {
3812 _mesa_glsl_error(&loc
, state
,
3813 "fragment shader input "
3814 "cannot have an array of structs");
3816 if (var
->type
->is_record()) {
3817 for (unsigned i
= 0; i
< var
->type
->length
; i
++) {
3818 if (var
->type
->fields
.structure
[i
].type
->is_array() ||
3819 var
->type
->fields
.structure
[i
].type
->is_record())
3820 _mesa_glsl_error(&loc
, state
,
3821 "fragement shader input cannot have "
3822 "a struct that contains an "
3827 } else if (state
->stage
== MESA_SHADER_TESS_CTRL
||
3828 state
->stage
== MESA_SHADER_TESS_EVAL
) {
3829 handle_tess_shader_input_decl(state
, loc
, var
);
3831 } else if (var
->data
.mode
== ir_var_shader_out
) {
3832 const glsl_type
*check_type
= var
->type
->without_array();
3834 /* From section 4.3.6 (Output variables) of the GLSL 4.40 spec:
3836 * It is a compile-time error to declare a vertex, tessellation
3837 * evaluation, tessellation control, or geometry shader output
3838 * that contains any of the following:
3840 * * A Boolean type (bool, bvec2 ...)
3843 if (check_type
->is_boolean() || check_type
->contains_opaque())
3844 _mesa_glsl_error(&loc
, state
,
3845 "%s shader output cannot have type %s",
3846 _mesa_shader_stage_to_string(state
->stage
),
3849 /* From section 4.3.6 (Output variables) of the GLSL 4.40 spec:
3851 * It is a compile-time error to declare a fragment shader output
3852 * that contains any of the following:
3854 * * A Boolean type (bool, bvec2 ...)
3855 * * A double-precision scalar or vector (double, dvec2 ...)
3860 if (state
->stage
== MESA_SHADER_FRAGMENT
) {
3861 if (check_type
->is_record() || check_type
->is_matrix())
3862 _mesa_glsl_error(&loc
, state
,
3863 "fragment shader output "
3864 "cannot have struct or matrix type");
3865 switch (check_type
->base_type
) {
3866 case GLSL_TYPE_UINT
:
3868 case GLSL_TYPE_FLOAT
:
3871 _mesa_glsl_error(&loc
, state
,
3872 "fragment shader output cannot have "
3873 "type %s", check_type
->name
);
3877 /* From section 4.3.6 (Output Variables) of the GLSL ES 3.10 spec:
3879 * It is a compile-time error to declare a vertex shader output
3880 * with, or that contains, any of the following types:
3884 * * An array of arrays
3885 * * An array of structures
3886 * * A structure containing an array
3887 * * A structure containing a structure
3889 * It is a compile-time error to declare a fragment shader output
3890 * with, or that contains, any of the following types:
3896 * * An array of array
3898 if (state
->es_shader
) {
3899 if (var
->type
->is_array() &&
3900 var
->type
->fields
.array
->is_array()) {
3901 _mesa_glsl_error(&loc
, state
,
3903 "cannot have an array of arrays",
3904 _mesa_shader_stage_to_string(state
->stage
));
3906 if (state
->stage
== MESA_SHADER_VERTEX
) {
3907 if (var
->type
->is_array() &&
3908 var
->type
->fields
.array
->is_record()) {
3909 _mesa_glsl_error(&loc
, state
,
3910 "vertex shader output "
3911 "cannot have an array of structs");
3913 if (var
->type
->is_record()) {
3914 for (unsigned i
= 0; i
< var
->type
->length
; i
++) {
3915 if (var
->type
->fields
.structure
[i
].type
->is_array() ||
3916 var
->type
->fields
.structure
[i
].type
->is_record())
3917 _mesa_glsl_error(&loc
, state
,
3918 "vertex shader output cannot have a "
3919 "struct that contains an "
3926 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
3927 handle_tess_ctrl_shader_output_decl(state
, loc
, var
);
3931 /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
3932 * so must integer vertex outputs.
3934 * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
3935 * "Fragment shader inputs that are signed or unsigned integers or
3936 * integer vectors must be qualified with the interpolation qualifier
3939 * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
3940 * "Fragment shader inputs that are, or contain, signed or unsigned
3941 * integers or integer vectors must be qualified with the
3942 * interpolation qualifier flat."
3944 * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
3945 * "Vertex shader outputs that are, or contain, signed or unsigned
3946 * integers or integer vectors must be qualified with the
3947 * interpolation qualifier flat."
3949 * Note that prior to GLSL 1.50, this requirement applied to vertex
3950 * outputs rather than fragment inputs. That creates problems in the
3951 * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
3952 * desktop GL shaders. For GLSL ES shaders, we follow the spec and
3953 * apply the restriction to both vertex outputs and fragment inputs.
3955 * Note also that the desktop GLSL specs are missing the text "or
3956 * contain"; this is presumably an oversight, since there is no
3957 * reasonable way to interpolate a fragment shader input that contains
3960 if (state
->is_version(130, 300) &&
3961 var
->type
->contains_integer() &&
3962 var
->data
.interpolation
!= INTERP_QUALIFIER_FLAT
&&
3963 ((state
->stage
== MESA_SHADER_FRAGMENT
&& var
->data
.mode
== ir_var_shader_in
)
3964 || (state
->stage
== MESA_SHADER_VERTEX
&& var
->data
.mode
== ir_var_shader_out
3965 && state
->es_shader
))) {
3966 const char *var_type
= (state
->stage
== MESA_SHADER_VERTEX
) ?
3967 "vertex output" : "fragment input";
3968 _mesa_glsl_error(&loc
, state
, "if a %s is (or contains) "
3969 "an integer, then it must be qualified with 'flat'",
3973 /* Double fragment inputs must be qualified with 'flat'. */
3974 if (var
->type
->contains_double() &&
3975 var
->data
.interpolation
!= INTERP_QUALIFIER_FLAT
&&
3976 state
->stage
== MESA_SHADER_FRAGMENT
&&
3977 var
->data
.mode
== ir_var_shader_in
) {
3978 _mesa_glsl_error(&loc
, state
, "if a fragment input is (or contains) "
3979 "a double, then it must be qualified with 'flat'",
3983 /* Interpolation qualifiers cannot be applied to 'centroid' and
3984 * 'centroid varying'.
3986 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
3987 * "interpolation qualifiers may only precede the qualifiers in,
3988 * centroid in, out, or centroid out in a declaration. They do not apply
3989 * to the deprecated storage qualifiers varying or centroid varying."
3991 * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
3993 if (state
->is_version(130, 0)
3994 && this->type
->qualifier
.has_interpolation()
3995 && this->type
->qualifier
.flags
.q
.varying
) {
3997 const char *i
= this->type
->qualifier
.interpolation_string();
4000 if (this->type
->qualifier
.flags
.q
.centroid
)
4001 s
= "centroid varying";
4005 _mesa_glsl_error(&loc
, state
,
4006 "qualifier '%s' cannot be applied to the "
4007 "deprecated storage qualifier '%s'", i
, s
);
4011 /* Interpolation qualifiers can only apply to vertex shader outputs and
4012 * fragment shader inputs.
4014 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
4015 * "Outputs from a vertex shader (out) and inputs to a fragment
4016 * shader (in) can be further qualified with one or more of these
4017 * interpolation qualifiers"
4019 * From page 31 (page 37 of the PDF) of the GLSL ES 3.00 spec:
4020 * "These interpolation qualifiers may only precede the qualifiers
4021 * in, centroid in, out, or centroid out in a declaration. They do
4022 * not apply to inputs into a vertex shader or outputs from a
4025 if (state
->is_version(130, 300)
4026 && this->type
->qualifier
.has_interpolation()) {
4028 const char *i
= this->type
->qualifier
.interpolation_string();
4031 switch (state
->stage
) {
4032 case MESA_SHADER_VERTEX
:
4033 if (this->type
->qualifier
.flags
.q
.in
) {
4034 _mesa_glsl_error(&loc
, state
,
4035 "qualifier '%s' cannot be applied to vertex "
4036 "shader inputs", i
);
4039 case MESA_SHADER_FRAGMENT
:
4040 if (this->type
->qualifier
.flags
.q
.out
) {
4041 _mesa_glsl_error(&loc
, state
,
4042 "qualifier '%s' cannot be applied to fragment "
4043 "shader outputs", i
);
4052 /* From section 4.3.4 of the GLSL 4.00 spec:
4053 * "Input variables may not be declared using the patch in qualifier
4054 * in tessellation control or geometry shaders."
4056 * From section 4.3.6 of the GLSL 4.00 spec:
4057 * "It is an error to use patch out in a vertex, tessellation
4058 * evaluation, or geometry shader."
4060 * This doesn't explicitly forbid using them in a fragment shader, but
4061 * that's probably just an oversight.
4063 if (state
->stage
!= MESA_SHADER_TESS_EVAL
4064 && this->type
->qualifier
.flags
.q
.patch
4065 && this->type
->qualifier
.flags
.q
.in
) {
4067 _mesa_glsl_error(&loc
, state
, "'patch in' can only be used in a "
4068 "tessellation evaluation shader");
4071 if (state
->stage
!= MESA_SHADER_TESS_CTRL
4072 && this->type
->qualifier
.flags
.q
.patch
4073 && this->type
->qualifier
.flags
.q
.out
) {
4075 _mesa_glsl_error(&loc
, state
, "'patch out' can only be used in a "
4076 "tessellation control shader");
4079 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
4081 if (this->type
->qualifier
.precision
!= ast_precision_none
) {
4082 state
->check_precision_qualifiers_allowed(&loc
);
4086 /* If a precision qualifier is allowed on a type, it is allowed on
4087 * an array of that type.
4089 if (!(this->type
->qualifier
.precision
== ast_precision_none
4090 || precision_qualifier_allowed(var
->type
->without_array()))) {
4092 _mesa_glsl_error(&loc
, state
,
4093 "precision qualifiers apply only to floating point"
4094 ", integer and sampler types");
4097 /* From section 4.1.7 of the GLSL 4.40 spec:
4099 * "[Opaque types] can only be declared as function
4100 * parameters or uniform-qualified variables."
4102 if (var_type
->contains_opaque() &&
4103 !this->type
->qualifier
.flags
.q
.uniform
) {
4104 _mesa_glsl_error(&loc
, state
,
4105 "opaque variables must be declared uniform");
4108 /* Process the initializer and add its instructions to a temporary
4109 * list. This list will be added to the instruction stream (below) after
4110 * the declaration is added. This is done because in some cases (such as
4111 * redeclarations) the declaration may not actually be added to the
4112 * instruction stream.
4114 exec_list initializer_instructions
;
4116 /* Examine var name here since var may get deleted in the next call */
4117 bool var_is_gl_id
= is_gl_identifier(var
->name
);
4119 ir_variable
*earlier
=
4120 get_variable_being_redeclared(var
, decl
->get_location(), state
,
4121 false /* allow_all_redeclarations */);
4122 if (earlier
!= NULL
) {
4124 earlier
->data
.how_declared
== ir_var_declared_in_block
) {
4125 _mesa_glsl_error(&loc
, state
,
4126 "`%s' has already been redeclared using "
4127 "gl_PerVertex", earlier
->name
);
4129 earlier
->data
.how_declared
= ir_var_declared_normally
;
4132 if (decl
->initializer
!= NULL
) {
4133 result
= process_initializer((earlier
== NULL
) ? var
: earlier
,
4135 &initializer_instructions
, state
);
4138 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
4140 * "It is an error to write to a const variable outside of
4141 * its declaration, so they must be initialized when
4144 if (this->type
->qualifier
.flags
.q
.constant
&& decl
->initializer
== NULL
) {
4145 _mesa_glsl_error(& loc
, state
,
4146 "const declaration of `%s' must be initialized",
4150 if (state
->es_shader
) {
4151 const glsl_type
*const t
= (earlier
== NULL
)
4152 ? var
->type
: earlier
->type
;
4154 if (t
->is_unsized_array())
4155 /* Section 10.17 of the GLSL ES 1.00 specification states that
4156 * unsized array declarations have been removed from the language.
4157 * Arrays that are sized using an initializer are still explicitly
4158 * sized. However, GLSL ES 1.00 does not allow array
4159 * initializers. That is only allowed in GLSL ES 3.00.
4161 * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
4163 * "An array type can also be formed without specifying a size
4164 * if the definition includes an initializer:
4166 * float x[] = float[2] (1.0, 2.0); // declares an array of size 2
4167 * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
4172 _mesa_glsl_error(& loc
, state
,
4173 "unsized array declarations are not allowed in "
4177 /* If the declaration is not a redeclaration, there are a few additional
4178 * semantic checks that must be applied. In addition, variable that was
4179 * created for the declaration should be added to the IR stream.
4181 if (earlier
== NULL
) {
4182 validate_identifier(decl
->identifier
, loc
, state
);
4184 /* Add the variable to the symbol table. Note that the initializer's
4185 * IR was already processed earlier (though it hasn't been emitted
4186 * yet), without the variable in scope.
4188 * This differs from most C-like languages, but it follows the GLSL
4189 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
4192 * "Within a declaration, the scope of a name starts immediately
4193 * after the initializer if present or immediately after the name
4194 * being declared if not."
4196 if (!state
->symbols
->add_variable(var
)) {
4197 YYLTYPE loc
= this->get_location();
4198 _mesa_glsl_error(&loc
, state
, "name `%s' already taken in the "
4199 "current scope", decl
->identifier
);
4203 /* Push the variable declaration to the top. It means that all the
4204 * variable declarations will appear in a funny last-to-first order,
4205 * but otherwise we run into trouble if a function is prototyped, a
4206 * global var is decled, then the function is defined with usage of
4207 * the global var. See glslparsertest's CorrectModule.frag.
4209 instructions
->push_head(var
);
4212 instructions
->append_list(&initializer_instructions
);
4216 /* Generally, variable declarations do not have r-values. However,
4217 * one is used for the declaration in
4219 * while (bool b = some_condition()) {
4223 * so we return the rvalue from the last seen declaration here.
4230 ast_parameter_declarator::hir(exec_list
*instructions
,
4231 struct _mesa_glsl_parse_state
*state
)
4234 const struct glsl_type
*type
;
4235 const char *name
= NULL
;
4236 YYLTYPE loc
= this->get_location();
4238 type
= this->type
->glsl_type(& name
, state
);
4242 _mesa_glsl_error(& loc
, state
,
4243 "invalid type `%s' in declaration of `%s'",
4244 name
, this->identifier
);
4246 _mesa_glsl_error(& loc
, state
,
4247 "invalid type in declaration of `%s'",
4251 type
= glsl_type::error_type
;
4254 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
4256 * "Functions that accept no input arguments need not use void in the
4257 * argument list because prototypes (or definitions) are required and
4258 * therefore there is no ambiguity when an empty argument list "( )" is
4259 * declared. The idiom "(void)" as a parameter list is provided for
4262 * Placing this check here prevents a void parameter being set up
4263 * for a function, which avoids tripping up checks for main taking
4264 * parameters and lookups of an unnamed symbol.
4266 if (type
->is_void()) {
4267 if (this->identifier
!= NULL
)
4268 _mesa_glsl_error(& loc
, state
,
4269 "named parameter cannot have type `void'");
4275 if (formal_parameter
&& (this->identifier
== NULL
)) {
4276 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
4280 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
4281 * call already handled the "vec4[..] foo" case.
4283 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
4285 if (!type
->is_error() && type
->is_unsized_array()) {
4286 _mesa_glsl_error(&loc
, state
, "arrays passed as parameters must have "
4288 type
= glsl_type::error_type
;
4292 ir_variable
*var
= new(ctx
)
4293 ir_variable(type
, this->identifier
, ir_var_function_in
);
4295 /* Apply any specified qualifiers to the parameter declaration. Note that
4296 * for function parameters the default mode is 'in'.
4298 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
,
4301 /* From section 4.1.7 of the GLSL 4.40 spec:
4303 * "Opaque variables cannot be treated as l-values; hence cannot
4304 * be used as out or inout function parameters, nor can they be
4307 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
4308 && type
->contains_opaque()) {
4309 _mesa_glsl_error(&loc
, state
, "out and inout parameters cannot "
4310 "contain opaque variables");
4311 type
= glsl_type::error_type
;
4314 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
4316 * "When calling a function, expressions that do not evaluate to
4317 * l-values cannot be passed to parameters declared as out or inout."
4319 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
4321 * "Other binary or unary expressions, non-dereferenced arrays,
4322 * function names, swizzles with repeated fields, and constants
4323 * cannot be l-values."
4325 * So for GLSL 1.10, passing an array as an out or inout parameter is not
4326 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
4328 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
4330 && !state
->check_version(120, 100, &loc
,
4331 "arrays cannot be out or inout parameters")) {
4332 type
= glsl_type::error_type
;
4335 instructions
->push_tail(var
);
4337 /* Parameter declarations do not have r-values.
4344 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
4346 exec_list
*ir_parameters
,
4347 _mesa_glsl_parse_state
*state
)
4349 ast_parameter_declarator
*void_param
= NULL
;
4352 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
4353 param
->formal_parameter
= formal
;
4354 param
->hir(ir_parameters
, state
);
4362 if ((void_param
!= NULL
) && (count
> 1)) {
4363 YYLTYPE loc
= void_param
->get_location();
4365 _mesa_glsl_error(& loc
, state
,
4366 "`void' parameter must be only parameter");
4372 emit_function(_mesa_glsl_parse_state
*state
, ir_function
*f
)
4374 /* IR invariants disallow function declarations or definitions
4375 * nested within other function definitions. But there is no
4376 * requirement about the relative order of function declarations
4377 * and definitions with respect to one another. So simply insert
4378 * the new ir_function block at the end of the toplevel instruction
4381 state
->toplevel_ir
->push_tail(f
);
4386 ast_function::hir(exec_list
*instructions
,
4387 struct _mesa_glsl_parse_state
*state
)
4390 ir_function
*f
= NULL
;
4391 ir_function_signature
*sig
= NULL
;
4392 exec_list hir_parameters
;
4394 const char *const name
= identifier
;
4396 /* New functions are always added to the top-level IR instruction stream,
4397 * so this instruction list pointer is ignored. See also emit_function
4400 (void) instructions
;
4402 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
4404 * "Function declarations (prototypes) cannot occur inside of functions;
4405 * they must be at global scope, or for the built-in functions, outside
4406 * the global scope."
4408 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
4410 * "User defined functions may only be defined within the global scope."
4412 * Note that this language does not appear in GLSL 1.10.
4414 if ((state
->current_function
!= NULL
) &&
4415 state
->is_version(120, 100)) {
4416 YYLTYPE loc
= this->get_location();
4417 _mesa_glsl_error(&loc
, state
,
4418 "declaration of function `%s' not allowed within "
4419 "function body", name
);
4422 validate_identifier(name
, this->get_location(), state
);
4424 /* Convert the list of function parameters to HIR now so that they can be
4425 * used below to compare this function's signature with previously seen
4426 * signatures for functions with the same name.
4428 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
4430 & hir_parameters
, state
);
4432 const char *return_type_name
;
4433 const glsl_type
*return_type
=
4434 this->return_type
->glsl_type(& return_type_name
, state
);
4437 YYLTYPE loc
= this->get_location();
4438 _mesa_glsl_error(&loc
, state
,
4439 "function `%s' has undeclared return type `%s'",
4440 name
, return_type_name
);
4441 return_type
= glsl_type::error_type
;
4444 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
4445 * "No qualifier is allowed on the return type of a function."
4447 if (this->return_type
->has_qualifiers()) {
4448 YYLTYPE loc
= this->get_location();
4449 _mesa_glsl_error(& loc
, state
,
4450 "function `%s' return type has qualifiers", name
);
4453 /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
4455 * "Arrays are allowed as arguments and as the return type. In both
4456 * cases, the array must be explicitly sized."
4458 if (return_type
->is_unsized_array()) {
4459 YYLTYPE loc
= this->get_location();
4460 _mesa_glsl_error(& loc
, state
,
4461 "function `%s' return type array must be explicitly "
4465 /* From section 4.1.7 of the GLSL 4.40 spec:
4467 * "[Opaque types] can only be declared as function parameters
4468 * or uniform-qualified variables."
4470 if (return_type
->contains_opaque()) {
4471 YYLTYPE loc
= this->get_location();
4472 _mesa_glsl_error(&loc
, state
,
4473 "function `%s' return type can't contain an opaque type",
4477 /* Create an ir_function if one doesn't already exist. */
4478 f
= state
->symbols
->get_function(name
);
4480 f
= new(ctx
) ir_function(name
);
4481 if (!state
->symbols
->add_function(f
)) {
4482 /* This function name shadows a non-function use of the same name. */
4483 YYLTYPE loc
= this->get_location();
4485 _mesa_glsl_error(&loc
, state
, "function name `%s' conflicts with "
4486 "non-function", name
);
4490 emit_function(state
, f
);
4493 /* From GLSL ES 3.0 spec, chapter 6.1 "Function Definitions", page 71:
4495 * "A shader cannot redefine or overload built-in functions."
4497 * While in GLSL ES 1.0 specification, chapter 8 "Built-in Functions":
4499 * "User code can overload the built-in functions but cannot redefine
4502 if (state
->es_shader
&& state
->language_version
>= 300) {
4503 /* Local shader has no exact candidates; check the built-ins. */
4504 _mesa_glsl_initialize_builtin_functions();
4505 if (_mesa_glsl_find_builtin_function_by_name(state
, name
)) {
4506 YYLTYPE loc
= this->get_location();
4507 _mesa_glsl_error(& loc
, state
,
4508 "A shader cannot redefine or overload built-in "
4509 "function `%s' in GLSL ES 3.00", name
);
4514 /* Verify that this function's signature either doesn't match a previously
4515 * seen signature for a function with the same name, or, if a match is found,
4516 * that the previously seen signature does not have an associated definition.
4518 if (state
->es_shader
|| f
->has_user_signature()) {
4519 sig
= f
->exact_matching_signature(state
, &hir_parameters
);
4521 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
4522 if (badvar
!= NULL
) {
4523 YYLTYPE loc
= this->get_location();
4525 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
4526 "qualifiers don't match prototype", name
, badvar
);
4529 if (sig
->return_type
!= return_type
) {
4530 YYLTYPE loc
= this->get_location();
4532 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
4533 "match prototype", name
);
4536 if (sig
->is_defined
) {
4537 if (is_definition
) {
4538 YYLTYPE loc
= this->get_location();
4539 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
4541 /* We just encountered a prototype that exactly matches a
4542 * function that's already been defined. This is redundant,
4543 * and we should ignore it.
4551 /* Verify the return type of main() */
4552 if (strcmp(name
, "main") == 0) {
4553 if (! return_type
->is_void()) {
4554 YYLTYPE loc
= this->get_location();
4556 _mesa_glsl_error(& loc
, state
, "main() must return void");
4559 if (!hir_parameters
.is_empty()) {
4560 YYLTYPE loc
= this->get_location();
4562 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
4566 /* Finish storing the information about this new function in its signature.
4569 sig
= new(ctx
) ir_function_signature(return_type
);
4570 f
->add_signature(sig
);
4573 sig
->replace_parameters(&hir_parameters
);
4576 /* Function declarations (prototypes) do not have r-values.
4583 ast_function_definition::hir(exec_list
*instructions
,
4584 struct _mesa_glsl_parse_state
*state
)
4586 prototype
->is_definition
= true;
4587 prototype
->hir(instructions
, state
);
4589 ir_function_signature
*signature
= prototype
->signature
;
4590 if (signature
== NULL
)
4593 assert(state
->current_function
== NULL
);
4594 state
->current_function
= signature
;
4595 state
->found_return
= false;
4597 /* Duplicate parameters declared in the prototype as concrete variables.
4598 * Add these to the symbol table.
4600 state
->symbols
->push_scope();
4601 foreach_in_list(ir_variable
, var
, &signature
->parameters
) {
4602 assert(var
->as_variable() != NULL
);
4604 /* The only way a parameter would "exist" is if two parameters have
4607 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
4608 YYLTYPE loc
= this->get_location();
4610 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
4612 state
->symbols
->add_variable(var
);
4616 /* Convert the body of the function to HIR. */
4617 this->body
->hir(&signature
->body
, state
);
4618 signature
->is_defined
= true;
4620 state
->symbols
->pop_scope();
4622 assert(state
->current_function
== signature
);
4623 state
->current_function
= NULL
;
4625 if (!signature
->return_type
->is_void() && !state
->found_return
) {
4626 YYLTYPE loc
= this->get_location();
4627 _mesa_glsl_error(& loc
, state
, "function `%s' has non-void return type "
4628 "%s, but no return statement",
4629 signature
->function_name(),
4630 signature
->return_type
->name
);
4633 /* Function definitions do not have r-values.
4640 ast_jump_statement::hir(exec_list
*instructions
,
4641 struct _mesa_glsl_parse_state
*state
)
4648 assert(state
->current_function
);
4650 if (opt_return_value
) {
4651 ir_rvalue
*ret
= opt_return_value
->hir(instructions
, state
);
4653 /* The value of the return type can be NULL if the shader says
4654 * 'return foo();' and foo() is a function that returns void.
4656 * NOTE: The GLSL spec doesn't say that this is an error. The type
4657 * of the return value is void. If the return type of the function is
4658 * also void, then this should compile without error. Seriously.
4660 const glsl_type
*const ret_type
=
4661 (ret
== NULL
) ? glsl_type::void_type
: ret
->type
;
4663 /* Implicit conversions are not allowed for return values prior to
4664 * ARB_shading_language_420pack.
4666 if (state
->current_function
->return_type
!= ret_type
) {
4667 YYLTYPE loc
= this->get_location();
4669 if (state
->ARB_shading_language_420pack_enable
) {
4670 if (!apply_implicit_conversion(state
->current_function
->return_type
,
4672 _mesa_glsl_error(& loc
, state
,
4673 "could not implicitly convert return value "
4674 "to %s, in function `%s'",
4675 state
->current_function
->return_type
->name
,
4676 state
->current_function
->function_name());
4679 _mesa_glsl_error(& loc
, state
,
4680 "`return' with wrong type %s, in function `%s' "
4683 state
->current_function
->function_name(),
4684 state
->current_function
->return_type
->name
);
4686 } else if (state
->current_function
->return_type
->base_type
==
4688 YYLTYPE loc
= this->get_location();
4690 /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
4691 * specs add a clarification:
4693 * "A void function can only use return without a return argument, even if
4694 * the return argument has void type. Return statements only accept values:
4697 * void func2() { return func1(); } // illegal return statement"
4699 _mesa_glsl_error(& loc
, state
,
4700 "void functions can only use `return' without a "
4704 inst
= new(ctx
) ir_return(ret
);
4706 if (state
->current_function
->return_type
->base_type
!=
4708 YYLTYPE loc
= this->get_location();
4710 _mesa_glsl_error(& loc
, state
,
4711 "`return' with no value, in function %s returning "
4713 state
->current_function
->function_name());
4715 inst
= new(ctx
) ir_return
;
4718 state
->found_return
= true;
4719 instructions
->push_tail(inst
);
4724 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
4725 YYLTYPE loc
= this->get_location();
4727 _mesa_glsl_error(& loc
, state
,
4728 "`discard' may only appear in a fragment shader");
4730 instructions
->push_tail(new(ctx
) ir_discard
);
4735 if (mode
== ast_continue
&&
4736 state
->loop_nesting_ast
== NULL
) {
4737 YYLTYPE loc
= this->get_location();
4739 _mesa_glsl_error(& loc
, state
, "continue may only appear in a loop");
4740 } else if (mode
== ast_break
&&
4741 state
->loop_nesting_ast
== NULL
&&
4742 state
->switch_state
.switch_nesting_ast
== NULL
) {
4743 YYLTYPE loc
= this->get_location();
4745 _mesa_glsl_error(& loc
, state
,
4746 "break may only appear in a loop or a switch");
4748 /* For a loop, inline the for loop expression again, since we don't
4749 * know where near the end of the loop body the normal copy of it is
4750 * going to be placed. Same goes for the condition for a do-while
4753 if (state
->loop_nesting_ast
!= NULL
&&
4754 mode
== ast_continue
&& !state
->switch_state
.is_switch_innermost
) {
4755 if (state
->loop_nesting_ast
->rest_expression
) {
4756 state
->loop_nesting_ast
->rest_expression
->hir(instructions
,
4759 if (state
->loop_nesting_ast
->mode
==
4760 ast_iteration_statement::ast_do_while
) {
4761 state
->loop_nesting_ast
->condition_to_hir(instructions
, state
);
4765 if (state
->switch_state
.is_switch_innermost
&&
4766 mode
== ast_continue
) {
4767 /* Set 'continue_inside' to true. */
4768 ir_rvalue
*const true_val
= new (ctx
) ir_constant(true);
4769 ir_dereference_variable
*deref_continue_inside_var
=
4770 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
4771 instructions
->push_tail(new(ctx
) ir_assignment(deref_continue_inside_var
,
4774 /* Break out from the switch, continue for the loop will
4775 * be called right after switch. */
4776 ir_loop_jump
*const jump
=
4777 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
4778 instructions
->push_tail(jump
);
4780 } else if (state
->switch_state
.is_switch_innermost
&&
4781 mode
== ast_break
) {
4782 /* Force break out of switch by inserting a break. */
4783 ir_loop_jump
*const jump
=
4784 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
4785 instructions
->push_tail(jump
);
4787 ir_loop_jump
*const jump
=
4788 new(ctx
) ir_loop_jump((mode
== ast_break
)
4789 ? ir_loop_jump::jump_break
4790 : ir_loop_jump::jump_continue
);
4791 instructions
->push_tail(jump
);
4798 /* Jump instructions do not have r-values.
4805 ast_selection_statement::hir(exec_list
*instructions
,
4806 struct _mesa_glsl_parse_state
*state
)
4810 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
4812 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
4814 * "Any expression whose type evaluates to a Boolean can be used as the
4815 * conditional expression bool-expression. Vector types are not accepted
4816 * as the expression to if."
4818 * The checks are separated so that higher quality diagnostics can be
4819 * generated for cases where both rules are violated.
4821 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
4822 YYLTYPE loc
= this->condition
->get_location();
4824 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
4828 ir_if
*const stmt
= new(ctx
) ir_if(condition
);
4830 if (then_statement
!= NULL
) {
4831 state
->symbols
->push_scope();
4832 then_statement
->hir(& stmt
->then_instructions
, state
);
4833 state
->symbols
->pop_scope();
4836 if (else_statement
!= NULL
) {
4837 state
->symbols
->push_scope();
4838 else_statement
->hir(& stmt
->else_instructions
, state
);
4839 state
->symbols
->pop_scope();
4842 instructions
->push_tail(stmt
);
4844 /* if-statements do not have r-values.
4851 ast_switch_statement::hir(exec_list
*instructions
,
4852 struct _mesa_glsl_parse_state
*state
)
4856 ir_rvalue
*const test_expression
=
4857 this->test_expression
->hir(instructions
, state
);
4859 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
4861 * "The type of init-expression in a switch statement must be a
4864 if (!test_expression
->type
->is_scalar() ||
4865 !test_expression
->type
->is_integer()) {
4866 YYLTYPE loc
= this->test_expression
->get_location();
4868 _mesa_glsl_error(& loc
,
4870 "switch-statement expression must be scalar "
4874 /* Track the switch-statement nesting in a stack-like manner.
4876 struct glsl_switch_state saved
= state
->switch_state
;
4878 state
->switch_state
.is_switch_innermost
= true;
4879 state
->switch_state
.switch_nesting_ast
= this;
4880 state
->switch_state
.labels_ht
= hash_table_ctor(0, hash_table_pointer_hash
,
4881 hash_table_pointer_compare
);
4882 state
->switch_state
.previous_default
= NULL
;
4884 /* Initalize is_fallthru state to false.
4886 ir_rvalue
*const is_fallthru_val
= new (ctx
) ir_constant(false);
4887 state
->switch_state
.is_fallthru_var
=
4888 new(ctx
) ir_variable(glsl_type::bool_type
,
4889 "switch_is_fallthru_tmp",
4891 instructions
->push_tail(state
->switch_state
.is_fallthru_var
);
4893 ir_dereference_variable
*deref_is_fallthru_var
=
4894 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4895 instructions
->push_tail(new(ctx
) ir_assignment(deref_is_fallthru_var
,
4898 /* Initialize continue_inside state to false.
4900 state
->switch_state
.continue_inside
=
4901 new(ctx
) ir_variable(glsl_type::bool_type
,
4902 "continue_inside_tmp",
4904 instructions
->push_tail(state
->switch_state
.continue_inside
);
4906 ir_rvalue
*const false_val
= new (ctx
) ir_constant(false);
4907 ir_dereference_variable
*deref_continue_inside_var
=
4908 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
4909 instructions
->push_tail(new(ctx
) ir_assignment(deref_continue_inside_var
,
4912 state
->switch_state
.run_default
=
4913 new(ctx
) ir_variable(glsl_type::bool_type
,
4916 instructions
->push_tail(state
->switch_state
.run_default
);
4918 /* Loop around the switch is used for flow control. */
4919 ir_loop
* loop
= new(ctx
) ir_loop();
4920 instructions
->push_tail(loop
);
4922 /* Cache test expression.
4924 test_to_hir(&loop
->body_instructions
, state
);
4926 /* Emit code for body of switch stmt.
4928 body
->hir(&loop
->body_instructions
, state
);
4930 /* Insert a break at the end to exit loop. */
4931 ir_loop_jump
*jump
= new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
4932 loop
->body_instructions
.push_tail(jump
);
4934 /* If we are inside loop, check if continue got called inside switch. */
4935 if (state
->loop_nesting_ast
!= NULL
) {
4936 ir_dereference_variable
*deref_continue_inside
=
4937 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
4938 ir_if
*irif
= new(ctx
) ir_if(deref_continue_inside
);
4939 ir_loop_jump
*jump
= new(ctx
) ir_loop_jump(ir_loop_jump::jump_continue
);
4941 if (state
->loop_nesting_ast
!= NULL
) {
4942 if (state
->loop_nesting_ast
->rest_expression
) {
4943 state
->loop_nesting_ast
->rest_expression
->hir(&irif
->then_instructions
,
4946 if (state
->loop_nesting_ast
->mode
==
4947 ast_iteration_statement::ast_do_while
) {
4948 state
->loop_nesting_ast
->condition_to_hir(&irif
->then_instructions
, state
);
4951 irif
->then_instructions
.push_tail(jump
);
4952 instructions
->push_tail(irif
);
4955 hash_table_dtor(state
->switch_state
.labels_ht
);
4957 state
->switch_state
= saved
;
4959 /* Switch statements do not have r-values. */
4965 ast_switch_statement::test_to_hir(exec_list
*instructions
,
4966 struct _mesa_glsl_parse_state
*state
)
4970 /* Cache value of test expression. */
4971 ir_rvalue
*const test_val
=
4972 test_expression
->hir(instructions
,
4975 state
->switch_state
.test_var
= new(ctx
) ir_variable(test_val
->type
,
4978 ir_dereference_variable
*deref_test_var
=
4979 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
4981 instructions
->push_tail(state
->switch_state
.test_var
);
4982 instructions
->push_tail(new(ctx
) ir_assignment(deref_test_var
, test_val
));
4987 ast_switch_body::hir(exec_list
*instructions
,
4988 struct _mesa_glsl_parse_state
*state
)
4991 stmts
->hir(instructions
, state
);
4993 /* Switch bodies do not have r-values. */
4998 ast_case_statement_list::hir(exec_list
*instructions
,
4999 struct _mesa_glsl_parse_state
*state
)
5001 exec_list default_case
, after_default
, tmp
;
5003 foreach_list_typed (ast_case_statement
, case_stmt
, link
, & this->cases
) {
5004 case_stmt
->hir(&tmp
, state
);
5007 if (state
->switch_state
.previous_default
&& default_case
.is_empty()) {
5008 default_case
.append_list(&tmp
);
5012 /* If default case found, append 'after_default' list. */
5013 if (!default_case
.is_empty())
5014 after_default
.append_list(&tmp
);
5016 instructions
->append_list(&tmp
);
5019 /* Handle the default case. This is done here because default might not be
5020 * the last case. We need to add checks against following cases first to see
5021 * if default should be chosen or not.
5023 if (!default_case
.is_empty()) {
5025 ir_rvalue
*const true_val
= new (state
) ir_constant(true);
5026 ir_dereference_variable
*deref_run_default_var
=
5027 new(state
) ir_dereference_variable(state
->switch_state
.run_default
);
5029 /* Choose to run default case initially, following conditional
5030 * assignments might change this.
5032 ir_assignment
*const init_var
=
5033 new(state
) ir_assignment(deref_run_default_var
, true_val
);
5034 instructions
->push_tail(init_var
);
5036 /* Default case was the last one, no checks required. */
5037 if (after_default
.is_empty()) {
5038 instructions
->append_list(&default_case
);
5042 foreach_in_list(ir_instruction
, ir
, &after_default
) {
5043 ir_assignment
*assign
= ir
->as_assignment();
5048 /* Clone the check between case label and init expression. */
5049 ir_expression
*exp
= (ir_expression
*) assign
->condition
;
5050 ir_expression
*clone
= exp
->clone(state
, NULL
);
5052 ir_dereference_variable
*deref_var
=
5053 new(state
) ir_dereference_variable(state
->switch_state
.run_default
);
5054 ir_rvalue
*const false_val
= new (state
) ir_constant(false);
5056 ir_assignment
*const set_false
=
5057 new(state
) ir_assignment(deref_var
, false_val
, clone
);
5059 instructions
->push_tail(set_false
);
5062 /* Append default case and all cases after it. */
5063 instructions
->append_list(&default_case
);
5064 instructions
->append_list(&after_default
);
5067 /* Case statements do not have r-values. */
5072 ast_case_statement::hir(exec_list
*instructions
,
5073 struct _mesa_glsl_parse_state
*state
)
5075 labels
->hir(instructions
, state
);
5077 /* Guard case statements depending on fallthru state. */
5078 ir_dereference_variable
*const deref_fallthru_guard
=
5079 new(state
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
5080 ir_if
*const test_fallthru
= new(state
) ir_if(deref_fallthru_guard
);
5082 foreach_list_typed (ast_node
, stmt
, link
, & this->stmts
)
5083 stmt
->hir(& test_fallthru
->then_instructions
, state
);
5085 instructions
->push_tail(test_fallthru
);
5087 /* Case statements do not have r-values. */
5093 ast_case_label_list::hir(exec_list
*instructions
,
5094 struct _mesa_glsl_parse_state
*state
)
5096 foreach_list_typed (ast_case_label
, label
, link
, & this->labels
)
5097 label
->hir(instructions
, state
);
5099 /* Case labels do not have r-values. */
5104 ast_case_label::hir(exec_list
*instructions
,
5105 struct _mesa_glsl_parse_state
*state
)
5109 ir_dereference_variable
*deref_fallthru_var
=
5110 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
5112 ir_rvalue
*const true_val
= new(ctx
) ir_constant(true);
5114 /* If not default case, ... */
5115 if (this->test_value
!= NULL
) {
5116 /* Conditionally set fallthru state based on
5117 * comparison of cached test expression value to case label.
5119 ir_rvalue
*const label_rval
= this->test_value
->hir(instructions
, state
);
5120 ir_constant
*label_const
= label_rval
->constant_expression_value();
5123 YYLTYPE loc
= this->test_value
->get_location();
5125 _mesa_glsl_error(& loc
, state
,
5126 "switch statement case label must be a "
5127 "constant expression");
5129 /* Stuff a dummy value in to allow processing to continue. */
5130 label_const
= new(ctx
) ir_constant(0);
5132 ast_expression
*previous_label
= (ast_expression
*)
5133 hash_table_find(state
->switch_state
.labels_ht
,
5134 (void *)(uintptr_t)label_const
->value
.u
[0]);
5136 if (previous_label
) {
5137 YYLTYPE loc
= this->test_value
->get_location();
5138 _mesa_glsl_error(& loc
, state
, "duplicate case value");
5140 loc
= previous_label
->get_location();
5141 _mesa_glsl_error(& loc
, state
, "this is the previous case label");
5143 hash_table_insert(state
->switch_state
.labels_ht
,
5145 (void *)(uintptr_t)label_const
->value
.u
[0]);
5149 ir_dereference_variable
*deref_test_var
=
5150 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
5152 ir_expression
*test_cond
= new(ctx
) ir_expression(ir_binop_all_equal
,
5157 * From GLSL 4.40 specification section 6.2 ("Selection"):
5159 * "The type of the init-expression value in a switch statement must
5160 * be a scalar int or uint. The type of the constant-expression value
5161 * in a case label also must be a scalar int or uint. When any pair
5162 * of these values is tested for "equal value" and the types do not
5163 * match, an implicit conversion will be done to convert the int to a
5164 * uint (see section 4.1.10 “Implicit Conversions”) before the compare
5167 if (label_const
->type
!= state
->switch_state
.test_var
->type
) {
5168 YYLTYPE loc
= this->test_value
->get_location();
5170 const glsl_type
*type_a
= label_const
->type
;
5171 const glsl_type
*type_b
= state
->switch_state
.test_var
->type
;
5173 /* Check if int->uint implicit conversion is supported. */
5174 bool integer_conversion_supported
=
5175 glsl_type::int_type
->can_implicitly_convert_to(glsl_type::uint_type
,
5178 if ((!type_a
->is_integer() || !type_b
->is_integer()) ||
5179 !integer_conversion_supported
) {
5180 _mesa_glsl_error(&loc
, state
, "type mismatch with switch "
5181 "init-expression and case label (%s != %s)",
5182 type_a
->name
, type_b
->name
);
5184 /* Conversion of the case label. */
5185 if (type_a
->base_type
== GLSL_TYPE_INT
) {
5186 if (!apply_implicit_conversion(glsl_type::uint_type
,
5187 test_cond
->operands
[0], state
))
5188 _mesa_glsl_error(&loc
, state
, "implicit type conversion error");
5190 /* Conversion of the init-expression value. */
5191 if (!apply_implicit_conversion(glsl_type::uint_type
,
5192 test_cond
->operands
[1], state
))
5193 _mesa_glsl_error(&loc
, state
, "implicit type conversion error");
5198 ir_assignment
*set_fallthru_on_test
=
5199 new(ctx
) ir_assignment(deref_fallthru_var
, true_val
, test_cond
);
5201 instructions
->push_tail(set_fallthru_on_test
);
5202 } else { /* default case */
5203 if (state
->switch_state
.previous_default
) {
5204 YYLTYPE loc
= this->get_location();
5205 _mesa_glsl_error(& loc
, state
,
5206 "multiple default labels in one switch");
5208 loc
= state
->switch_state
.previous_default
->get_location();
5209 _mesa_glsl_error(& loc
, state
, "this is the first default label");
5211 state
->switch_state
.previous_default
= this;
5213 /* Set fallthru condition on 'run_default' bool. */
5214 ir_dereference_variable
*deref_run_default
=
5215 new(ctx
) ir_dereference_variable(state
->switch_state
.run_default
);
5216 ir_rvalue
*const cond_true
= new(ctx
) ir_constant(true);
5217 ir_expression
*test_cond
= new(ctx
) ir_expression(ir_binop_all_equal
,
5221 /* Set falltrhu state. */
5222 ir_assignment
*set_fallthru
=
5223 new(ctx
) ir_assignment(deref_fallthru_var
, true_val
, test_cond
);
5225 instructions
->push_tail(set_fallthru
);
5228 /* Case statements do not have r-values. */
5233 ast_iteration_statement::condition_to_hir(exec_list
*instructions
,
5234 struct _mesa_glsl_parse_state
*state
)
5238 if (condition
!= NULL
) {
5239 ir_rvalue
*const cond
=
5240 condition
->hir(instructions
, state
);
5243 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
5244 YYLTYPE loc
= condition
->get_location();
5246 _mesa_glsl_error(& loc
, state
,
5247 "loop condition must be scalar boolean");
5249 /* As the first code in the loop body, generate a block that looks
5250 * like 'if (!condition) break;' as the loop termination condition.
5252 ir_rvalue
*const not_cond
=
5253 new(ctx
) ir_expression(ir_unop_logic_not
, cond
);
5255 ir_if
*const if_stmt
= new(ctx
) ir_if(not_cond
);
5257 ir_jump
*const break_stmt
=
5258 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
5260 if_stmt
->then_instructions
.push_tail(break_stmt
);
5261 instructions
->push_tail(if_stmt
);
5268 ast_iteration_statement::hir(exec_list
*instructions
,
5269 struct _mesa_glsl_parse_state
*state
)
5273 /* For-loops and while-loops start a new scope, but do-while loops do not.
5275 if (mode
!= ast_do_while
)
5276 state
->symbols
->push_scope();
5278 if (init_statement
!= NULL
)
5279 init_statement
->hir(instructions
, state
);
5281 ir_loop
*const stmt
= new(ctx
) ir_loop();
5282 instructions
->push_tail(stmt
);
5284 /* Track the current loop nesting. */
5285 ast_iteration_statement
*nesting_ast
= state
->loop_nesting_ast
;
5287 state
->loop_nesting_ast
= this;
5289 /* Likewise, indicate that following code is closest to a loop,
5290 * NOT closest to a switch.
5292 bool saved_is_switch_innermost
= state
->switch_state
.is_switch_innermost
;
5293 state
->switch_state
.is_switch_innermost
= false;
5295 if (mode
!= ast_do_while
)
5296 condition_to_hir(&stmt
->body_instructions
, state
);
5299 body
->hir(& stmt
->body_instructions
, state
);
5301 if (rest_expression
!= NULL
)
5302 rest_expression
->hir(& stmt
->body_instructions
, state
);
5304 if (mode
== ast_do_while
)
5305 condition_to_hir(&stmt
->body_instructions
, state
);
5307 if (mode
!= ast_do_while
)
5308 state
->symbols
->pop_scope();
5310 /* Restore previous nesting before returning. */
5311 state
->loop_nesting_ast
= nesting_ast
;
5312 state
->switch_state
.is_switch_innermost
= saved_is_switch_innermost
;
5314 /* Loops do not have r-values.
5321 * Determine if the given type is valid for establishing a default precision
5324 * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
5326 * "The precision statement
5328 * precision precision-qualifier type;
5330 * can be used to establish a default precision qualifier. The type field
5331 * can be either int or float or any of the sampler types, and the
5332 * precision-qualifier can be lowp, mediump, or highp."
5334 * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
5335 * qualifiers on sampler types, but this seems like an oversight (since the
5336 * intention of including these in GLSL 1.30 is to allow compatibility with ES
5337 * shaders). So we allow int, float, and all sampler types regardless of GLSL
5341 is_valid_default_precision_type(const struct glsl_type
*const type
)
5346 switch (type
->base_type
) {
5348 case GLSL_TYPE_FLOAT
:
5349 /* "int" and "float" are valid, but vectors and matrices are not. */
5350 return type
->vector_elements
== 1 && type
->matrix_columns
== 1;
5351 case GLSL_TYPE_SAMPLER
:
5360 ast_type_specifier::hir(exec_list
*instructions
,
5361 struct _mesa_glsl_parse_state
*state
)
5363 if (this->default_precision
== ast_precision_none
&& this->structure
== NULL
)
5366 YYLTYPE loc
= this->get_location();
5368 /* If this is a precision statement, check that the type to which it is
5369 * applied is either float or int.
5371 * From section 4.5.3 of the GLSL 1.30 spec:
5372 * "The precision statement
5373 * precision precision-qualifier type;
5374 * can be used to establish a default precision qualifier. The type
5375 * field can be either int or float [...]. Any other types or
5376 * qualifiers will result in an error.
5378 if (this->default_precision
!= ast_precision_none
) {
5379 if (!state
->check_precision_qualifiers_allowed(&loc
))
5382 if (this->structure
!= NULL
) {
5383 _mesa_glsl_error(&loc
, state
,
5384 "precision qualifiers do not apply to structures");
5388 if (this->array_specifier
!= NULL
) {
5389 _mesa_glsl_error(&loc
, state
,
5390 "default precision statements do not apply to "
5395 const struct glsl_type
*const type
=
5396 state
->symbols
->get_type(this->type_name
);
5397 if (!is_valid_default_precision_type(type
)) {
5398 _mesa_glsl_error(&loc
, state
,
5399 "default precision statements apply only to "
5400 "float, int, and sampler types");
5404 if (type
->base_type
== GLSL_TYPE_FLOAT
5406 && state
->stage
== MESA_SHADER_FRAGMENT
) {
5407 /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
5410 * "The fragment language has no default precision qualifier for
5411 * floating point types."
5413 * As a result, we have to track whether or not default precision has
5414 * been specified for float in GLSL ES fragment shaders.
5416 * Earlier in that same section, the spec says:
5418 * "Non-precision qualified declarations will use the precision
5419 * qualifier specified in the most recent precision statement
5420 * that is still in scope. The precision statement has the same
5421 * scoping rules as variable declarations. If it is declared
5422 * inside a compound statement, its effect stops at the end of
5423 * the innermost statement it was declared in. Precision
5424 * statements in nested scopes override precision statements in
5425 * outer scopes. Multiple precision statements for the same basic
5426 * type can appear inside the same scope, with later statements
5427 * overriding earlier statements within that scope."
5429 * Default precision specifications follow the same scope rules as
5430 * variables. So, we can track the state of the default float
5431 * precision in the symbol table, and the rules will just work. This
5432 * is a slight abuse of the symbol table, but it has the semantics
5435 ir_variable
*const junk
=
5436 new(state
) ir_variable(type
, "#default precision",
5439 state
->symbols
->add_variable(junk
);
5442 /* FINISHME: Translate precision statements into IR. */
5446 /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
5447 * process_record_constructor() can do type-checking on C-style initializer
5448 * expressions of structs, but ast_struct_specifier should only be translated
5449 * to HIR if it is declaring the type of a structure.
5451 * The ->is_declaration field is false for initializers of variables
5452 * declared separately from the struct's type definition.
5454 * struct S { ... }; (is_declaration = true)
5455 * struct T { ... } t = { ... }; (is_declaration = true)
5456 * S s = { ... }; (is_declaration = false)
5458 if (this->structure
!= NULL
&& this->structure
->is_declaration
)
5459 return this->structure
->hir(instructions
, state
);
5466 * Process a structure or interface block tree into an array of structure fields
5468 * After parsing, where there are some syntax differnces, structures and
5469 * interface blocks are almost identical. They are similar enough that the
5470 * AST for each can be processed the same way into a set of
5471 * \c glsl_struct_field to describe the members.
5473 * If we're processing an interface block, var_mode should be the type of the
5474 * interface block (ir_var_shader_in, ir_var_shader_out, ir_var_uniform or
5475 * ir_var_shader_storage). If we're processing a structure, var_mode should be
5479 * The number of fields processed. A pointer to the array structure fields is
5480 * stored in \c *fields_ret.
5483 ast_process_structure_or_interface_block(exec_list
*instructions
,
5484 struct _mesa_glsl_parse_state
*state
,
5485 exec_list
*declarations
,
5487 glsl_struct_field
**fields_ret
,
5489 enum glsl_matrix_layout matrix_layout
,
5490 bool allow_reserved_names
,
5491 ir_variable_mode var_mode
)
5493 unsigned decl_count
= 0;
5495 /* Make an initial pass over the list of fields to determine how
5496 * many there are. Each element in this list is an ast_declarator_list.
5497 * This means that we actually need to count the number of elements in the
5498 * 'declarations' list in each of the elements.
5500 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
5501 decl_count
+= decl_list
->declarations
.length();
5504 /* Allocate storage for the fields and process the field
5505 * declarations. As the declarations are processed, try to also convert
5506 * the types to HIR. This ensures that structure definitions embedded in
5507 * other structure definitions or in interface blocks are processed.
5509 glsl_struct_field
*const fields
= ralloc_array(state
, glsl_struct_field
,
5513 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
5514 const char *type_name
;
5516 decl_list
->type
->specifier
->hir(instructions
, state
);
5518 /* Section 10.9 of the GLSL ES 1.00 specification states that
5519 * embedded structure definitions have been removed from the language.
5521 if (state
->es_shader
&& decl_list
->type
->specifier
->structure
!= NULL
) {
5522 _mesa_glsl_error(&loc
, state
, "embedded structure definitions are "
5523 "not allowed in GLSL ES 1.00");
5526 const glsl_type
*decl_type
=
5527 decl_list
->type
->glsl_type(& type_name
, state
);
5529 foreach_list_typed (ast_declaration
, decl
, link
,
5530 &decl_list
->declarations
) {
5531 if (!allow_reserved_names
)
5532 validate_identifier(decl
->identifier
, loc
, state
);
5534 /* From section 4.3.9 of the GLSL 4.40 spec:
5536 * "[In interface blocks] opaque types are not allowed."
5538 * It should be impossible for decl_type to be NULL here. Cases that
5539 * might naturally lead to decl_type being NULL, especially for the
5540 * is_interface case, will have resulted in compilation having
5541 * already halted due to a syntax error.
5543 const struct glsl_type
*field_type
=
5544 decl_type
!= NULL
? decl_type
: glsl_type::error_type
;
5546 if (is_interface
&& field_type
->contains_opaque()) {
5547 YYLTYPE loc
= decl_list
->get_location();
5548 _mesa_glsl_error(&loc
, state
,
5549 "uniform/buffer in non-default interface block contains "
5553 if (field_type
->contains_atomic()) {
5554 /* FINISHME: Add a spec quotation here once updated spec
5555 * FINISHME: language is available. See Khronos bug #10903
5556 * FINISHME: on whether atomic counters are allowed in
5557 * FINISHME: structures.
5559 YYLTYPE loc
= decl_list
->get_location();
5560 _mesa_glsl_error(&loc
, state
, "atomic counter in structure, "
5561 "shader storage block or uniform block");
5564 if (field_type
->contains_image()) {
5565 /* FINISHME: Same problem as with atomic counters.
5566 * FINISHME: Request clarification from Khronos and add
5567 * FINISHME: spec quotation here.
5569 YYLTYPE loc
= decl_list
->get_location();
5570 _mesa_glsl_error(&loc
, state
,
5571 "image in structure, shader storage block or "
5575 const struct ast_type_qualifier
*const qual
=
5576 & decl_list
->type
->qualifier
;
5577 if (qual
->flags
.q
.std140
||
5578 qual
->flags
.q
.packed
||
5579 qual
->flags
.q
.shared
) {
5580 _mesa_glsl_error(&loc
, state
,
5581 "uniform/shader storage block layout qualifiers "
5582 "std140, packed, and shared can only be applied "
5583 "to uniform/shader storage blocks, not members");
5586 if (qual
->flags
.q
.constant
) {
5587 YYLTYPE loc
= decl_list
->get_location();
5588 _mesa_glsl_error(&loc
, state
,
5589 "const storage qualifier cannot be applied "
5590 "to struct or interface block members");
5593 field_type
= process_array_type(&loc
, decl_type
,
5594 decl
->array_specifier
, state
);
5595 fields
[i
].type
= field_type
;
5596 fields
[i
].name
= decl
->identifier
;
5597 fields
[i
].location
= -1;
5598 fields
[i
].interpolation
=
5599 interpret_interpolation_qualifier(qual
, var_mode
, state
, &loc
);
5600 fields
[i
].centroid
= qual
->flags
.q
.centroid
? 1 : 0;
5601 fields
[i
].sample
= qual
->flags
.q
.sample
? 1 : 0;
5602 fields
[i
].patch
= qual
->flags
.q
.patch
? 1 : 0;
5604 /* Only save explicitly defined streams in block's field */
5605 fields
[i
].stream
= qual
->flags
.q
.explicit_stream
? qual
->stream
: -1;
5607 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
5608 if (!qual
->flags
.q
.uniform
&& !qual
->flags
.q
.buffer
) {
5609 _mesa_glsl_error(&loc
, state
,
5610 "row_major and column_major can only be "
5611 "applied to interface blocks");
5613 validate_matrix_layout_for_type(state
, &loc
, field_type
, NULL
);
5616 if (qual
->flags
.q
.uniform
&& qual
->has_interpolation()) {
5617 _mesa_glsl_error(&loc
, state
,
5618 "interpolation qualifiers cannot be used "
5619 "with uniform interface blocks");
5622 if ((qual
->flags
.q
.uniform
|| !is_interface
) &&
5623 qual
->has_auxiliary_storage()) {
5624 _mesa_glsl_error(&loc
, state
,
5625 "auxiliary storage qualifiers cannot be used "
5626 "in uniform blocks or structures.");
5629 /* Propogate row- / column-major information down the fields of the
5630 * structure or interface block. Structures need this data because
5631 * the structure may contain a structure that contains ... a matrix
5632 * that need the proper layout.
5634 if (field_type
->without_array()->is_matrix()
5635 || field_type
->without_array()->is_record()) {
5636 /* If no layout is specified for the field, inherit the layout
5639 fields
[i
].matrix_layout
= matrix_layout
;
5641 if (qual
->flags
.q
.row_major
)
5642 fields
[i
].matrix_layout
= GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
5643 else if (qual
->flags
.q
.column_major
)
5644 fields
[i
].matrix_layout
= GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
;
5646 /* If we're processing an interface block, the matrix layout must
5647 * be decided by this point.
5649 assert(!is_interface
5650 || fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
5651 || fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
);
5658 assert(i
== decl_count
);
5660 *fields_ret
= fields
;
5666 ast_struct_specifier::hir(exec_list
*instructions
,
5667 struct _mesa_glsl_parse_state
*state
)
5669 YYLTYPE loc
= this->get_location();
5671 /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
5673 * "Anonymous structures are not supported; so embedded structures must
5674 * have a declarator. A name given to an embedded struct is scoped at
5675 * the same level as the struct it is embedded in."
5677 * The same section of the GLSL 1.20 spec says:
5679 * "Anonymous structures are not supported. Embedded structures are not
5682 * struct S { float f; };
5684 * S; // Error: anonymous structures disallowed
5685 * struct { ... }; // Error: embedded structures disallowed
5686 * S s; // Okay: nested structures with name are allowed
5689 * The GLSL ES 1.00 and 3.00 specs have similar langauge and examples. So,
5690 * we allow embedded structures in 1.10 only.
5692 if (state
->language_version
!= 110 && state
->struct_specifier_depth
!= 0)
5693 _mesa_glsl_error(&loc
, state
,
5694 "embedded structure declarations are not allowed");
5696 state
->struct_specifier_depth
++;
5698 glsl_struct_field
*fields
;
5699 unsigned decl_count
=
5700 ast_process_structure_or_interface_block(instructions
,
5702 &this->declarations
,
5706 GLSL_MATRIX_LAYOUT_INHERITED
,
5707 false /* allow_reserved_names */,
5710 validate_identifier(this->name
, loc
, state
);
5712 const glsl_type
*t
=
5713 glsl_type::get_record_instance(fields
, decl_count
, this->name
);
5715 if (!state
->symbols
->add_type(name
, t
)) {
5716 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
5718 const glsl_type
**s
= reralloc(state
, state
->user_structures
,
5720 state
->num_user_structures
+ 1);
5722 s
[state
->num_user_structures
] = t
;
5723 state
->user_structures
= s
;
5724 state
->num_user_structures
++;
5728 state
->struct_specifier_depth
--;
5730 /* Structure type definitions do not have r-values.
5737 * Visitor class which detects whether a given interface block has been used.
5739 class interface_block_usage_visitor
: public ir_hierarchical_visitor
5742 interface_block_usage_visitor(ir_variable_mode mode
, const glsl_type
*block
)
5743 : mode(mode
), block(block
), found(false)
5747 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
5749 if (ir
->var
->data
.mode
== mode
&& ir
->var
->get_interface_type() == block
) {
5753 return visit_continue
;
5756 bool usage_found() const
5762 ir_variable_mode mode
;
5763 const glsl_type
*block
;
5769 ast_interface_block::hir(exec_list
*instructions
,
5770 struct _mesa_glsl_parse_state
*state
)
5772 YYLTYPE loc
= this->get_location();
5774 /* Interface blocks must be declared at global scope */
5775 if (state
->current_function
!= NULL
) {
5776 _mesa_glsl_error(&loc
, state
,
5777 "Interface block `%s' must be declared "
5782 /* The ast_interface_block has a list of ast_declarator_lists. We
5783 * need to turn those into ir_variables with an association
5784 * with this uniform block.
5786 enum glsl_interface_packing packing
;
5787 if (this->layout
.flags
.q
.shared
) {
5788 packing
= GLSL_INTERFACE_PACKING_SHARED
;
5789 } else if (this->layout
.flags
.q
.packed
) {
5790 packing
= GLSL_INTERFACE_PACKING_PACKED
;
5792 /* The default layout is std140.
5794 packing
= GLSL_INTERFACE_PACKING_STD140
;
5797 ir_variable_mode var_mode
;
5798 const char *iface_type_name
;
5799 if (this->layout
.flags
.q
.in
) {
5800 var_mode
= ir_var_shader_in
;
5801 iface_type_name
= "in";
5802 } else if (this->layout
.flags
.q
.out
) {
5803 var_mode
= ir_var_shader_out
;
5804 iface_type_name
= "out";
5805 } else if (this->layout
.flags
.q
.uniform
) {
5806 var_mode
= ir_var_uniform
;
5807 iface_type_name
= "uniform";
5808 } else if (this->layout
.flags
.q
.buffer
) {
5809 var_mode
= ir_var_shader_storage
;
5810 iface_type_name
= "buffer";
5812 var_mode
= ir_var_auto
;
5813 iface_type_name
= "UNKNOWN";
5814 assert(!"interface block layout qualifier not found!");
5817 enum glsl_matrix_layout matrix_layout
= GLSL_MATRIX_LAYOUT_INHERITED
;
5818 if (this->layout
.flags
.q
.row_major
)
5819 matrix_layout
= GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
5820 else if (this->layout
.flags
.q
.column_major
)
5821 matrix_layout
= GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
;
5823 bool redeclaring_per_vertex
= strcmp(this->block_name
, "gl_PerVertex") == 0;
5824 exec_list declared_variables
;
5825 glsl_struct_field
*fields
;
5827 /* Treat an interface block as one level of nesting, so that embedded struct
5828 * specifiers will be disallowed.
5830 state
->struct_specifier_depth
++;
5832 unsigned int num_variables
=
5833 ast_process_structure_or_interface_block(&declared_variables
,
5835 &this->declarations
,
5840 redeclaring_per_vertex
,
5843 state
->struct_specifier_depth
--;
5845 if (!redeclaring_per_vertex
) {
5846 validate_identifier(this->block_name
, loc
, state
);
5848 /* From section 4.3.9 ("Interface Blocks") of the GLSL 4.50 spec:
5850 * "Block names have no other use within a shader beyond interface
5851 * matching; it is a compile-time error to use a block name at global
5852 * scope for anything other than as a block name."
5854 ir_variable
*var
= state
->symbols
->get_variable(this->block_name
);
5855 if (var
&& !var
->type
->is_interface()) {
5856 _mesa_glsl_error(&loc
, state
, "Block name `%s' is "
5857 "already used in the scope.",
5862 const glsl_type
*earlier_per_vertex
= NULL
;
5863 if (redeclaring_per_vertex
) {
5864 /* Find the previous declaration of gl_PerVertex. If we're redeclaring
5865 * the named interface block gl_in, we can find it by looking at the
5866 * previous declaration of gl_in. Otherwise we can find it by looking
5867 * at the previous decalartion of any of the built-in outputs,
5870 * Also check that the instance name and array-ness of the redeclaration
5874 case ir_var_shader_in
:
5875 if (ir_variable
*earlier_gl_in
=
5876 state
->symbols
->get_variable("gl_in")) {
5877 earlier_per_vertex
= earlier_gl_in
->get_interface_type();
5879 _mesa_glsl_error(&loc
, state
,
5880 "redeclaration of gl_PerVertex input not allowed "
5882 _mesa_shader_stage_to_string(state
->stage
));
5884 if (this->instance_name
== NULL
||
5885 strcmp(this->instance_name
, "gl_in") != 0 || this->array_specifier
== NULL
) {
5886 _mesa_glsl_error(&loc
, state
,
5887 "gl_PerVertex input must be redeclared as "
5891 case ir_var_shader_out
:
5892 if (ir_variable
*earlier_gl_Position
=
5893 state
->symbols
->get_variable("gl_Position")) {
5894 earlier_per_vertex
= earlier_gl_Position
->get_interface_type();
5896 _mesa_glsl_error(&loc
, state
,
5897 "redeclaration of gl_PerVertex output not "
5898 "allowed in the %s shader",
5899 _mesa_shader_stage_to_string(state
->stage
));
5901 if (this->instance_name
!= NULL
) {
5902 _mesa_glsl_error(&loc
, state
,
5903 "gl_PerVertex output may not be redeclared with "
5904 "an instance name");
5908 _mesa_glsl_error(&loc
, state
,
5909 "gl_PerVertex must be declared as an input or an "
5914 if (earlier_per_vertex
== NULL
) {
5915 /* An error has already been reported. Bail out to avoid null
5916 * dereferences later in this function.
5921 /* Copy locations from the old gl_PerVertex interface block. */
5922 for (unsigned i
= 0; i
< num_variables
; i
++) {
5923 int j
= earlier_per_vertex
->field_index(fields
[i
].name
);
5925 _mesa_glsl_error(&loc
, state
,
5926 "redeclaration of gl_PerVertex must be a subset "
5927 "of the built-in members of gl_PerVertex");
5929 fields
[i
].location
=
5930 earlier_per_vertex
->fields
.structure
[j
].location
;
5931 fields
[i
].interpolation
=
5932 earlier_per_vertex
->fields
.structure
[j
].interpolation
;
5933 fields
[i
].centroid
=
5934 earlier_per_vertex
->fields
.structure
[j
].centroid
;
5936 earlier_per_vertex
->fields
.structure
[j
].sample
;
5938 earlier_per_vertex
->fields
.structure
[j
].patch
;
5942 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10
5945 * If a built-in interface block is redeclared, it must appear in
5946 * the shader before any use of any member included in the built-in
5947 * declaration, or a compilation error will result.
5949 * This appears to be a clarification to the behaviour established for
5950 * gl_PerVertex by GLSL 1.50, therefore we implement this behaviour
5951 * regardless of GLSL version.
5953 interface_block_usage_visitor
v(var_mode
, earlier_per_vertex
);
5954 v
.run(instructions
);
5955 if (v
.usage_found()) {
5956 _mesa_glsl_error(&loc
, state
,
5957 "redeclaration of a built-in interface block must "
5958 "appear before any use of any member of the "
5963 const glsl_type
*block_type
=
5964 glsl_type::get_interface_instance(fields
,
5969 if (!state
->symbols
->add_interface(block_type
->name
, block_type
, var_mode
)) {
5970 YYLTYPE loc
= this->get_location();
5971 _mesa_glsl_error(&loc
, state
, "interface block `%s' with type `%s' "
5972 "already taken in the current scope",
5973 this->block_name
, iface_type_name
);
5976 /* Since interface blocks cannot contain statements, it should be
5977 * impossible for the block to generate any instructions.
5979 assert(declared_variables
.is_empty());
5981 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
5983 * Geometry shader input variables get the per-vertex values written
5984 * out by vertex shader output variables of the same names. Since a
5985 * geometry shader operates on a set of vertices, each input varying
5986 * variable (or input block, see interface blocks below) needs to be
5987 * declared as an array.
5989 if (state
->stage
== MESA_SHADER_GEOMETRY
&& this->array_specifier
== NULL
&&
5990 var_mode
== ir_var_shader_in
) {
5991 _mesa_glsl_error(&loc
, state
, "geometry shader inputs must be arrays");
5992 } else if ((state
->stage
== MESA_SHADER_TESS_CTRL
||
5993 state
->stage
== MESA_SHADER_TESS_EVAL
) &&
5994 this->array_specifier
== NULL
&&
5995 var_mode
== ir_var_shader_in
) {
5996 _mesa_glsl_error(&loc
, state
, "per-vertex tessellation shader inputs must be arrays");
5997 } else if (state
->stage
== MESA_SHADER_TESS_CTRL
&&
5998 this->array_specifier
== NULL
&&
5999 var_mode
== ir_var_shader_out
) {
6000 _mesa_glsl_error(&loc
, state
, "tessellation control shader outputs must be arrays");
6004 /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
6007 * "If an instance name (instance-name) is used, then it puts all the
6008 * members inside a scope within its own name space, accessed with the
6009 * field selector ( . ) operator (analogously to structures)."
6011 if (this->instance_name
) {
6012 if (redeclaring_per_vertex
) {
6013 /* When a built-in in an unnamed interface block is redeclared,
6014 * get_variable_being_redeclared() calls
6015 * check_builtin_array_max_size() to make sure that built-in array
6016 * variables aren't redeclared to illegal sizes. But we're looking
6017 * at a redeclaration of a named built-in interface block. So we
6018 * have to manually call check_builtin_array_max_size() for all parts
6019 * of the interface that are arrays.
6021 for (unsigned i
= 0; i
< num_variables
; i
++) {
6022 if (fields
[i
].type
->is_array()) {
6023 const unsigned size
= fields
[i
].type
->array_size();
6024 check_builtin_array_max_size(fields
[i
].name
, size
, loc
, state
);
6028 validate_identifier(this->instance_name
, loc
, state
);
6033 if (this->array_specifier
!= NULL
) {
6034 /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
6036 * For uniform blocks declared an array, each individual array
6037 * element corresponds to a separate buffer object backing one
6038 * instance of the block. As the array size indicates the number
6039 * of buffer objects needed, uniform block array declarations
6040 * must specify an array size.
6042 * And a few paragraphs later:
6044 * Geometry shader input blocks must be declared as arrays and
6045 * follow the array declaration and linking rules for all
6046 * geometry shader inputs. All other input and output block
6047 * arrays must specify an array size.
6049 * The same applies to tessellation shaders.
6051 * The upshot of this is that the only circumstance where an
6052 * interface array size *doesn't* need to be specified is on a
6053 * geometry shader input, tessellation control shader input,
6054 * tessellation control shader output, and tessellation evaluation
6057 if (this->array_specifier
->is_unsized_array
) {
6058 bool allow_inputs
= state
->stage
== MESA_SHADER_GEOMETRY
||
6059 state
->stage
== MESA_SHADER_TESS_CTRL
||
6060 state
->stage
== MESA_SHADER_TESS_EVAL
;
6061 bool allow_outputs
= state
->stage
== MESA_SHADER_TESS_CTRL
;
6063 if (this->layout
.flags
.q
.in
) {
6065 _mesa_glsl_error(&loc
, state
,
6066 "unsized input block arrays not allowed in "
6068 _mesa_shader_stage_to_string(state
->stage
));
6069 } else if (this->layout
.flags
.q
.out
) {
6071 _mesa_glsl_error(&loc
, state
,
6072 "unsized output block arrays not allowed in "
6074 _mesa_shader_stage_to_string(state
->stage
));
6076 /* by elimination, this is a uniform block array */
6077 _mesa_glsl_error(&loc
, state
,
6078 "unsized uniform block arrays not allowed in "
6080 _mesa_shader_stage_to_string(state
->stage
));
6084 const glsl_type
*block_array_type
=
6085 process_array_type(&loc
, block_type
, this->array_specifier
, state
);
6087 /* From section 4.3.9 (Interface Blocks) of the GLSL ES 3.10 spec:
6089 * * Arrays of arrays of blocks are not allowed
6091 if (state
->es_shader
&& block_array_type
->is_array() &&
6092 block_array_type
->fields
.array
->is_array()) {
6093 _mesa_glsl_error(&loc
, state
,
6094 "arrays of arrays interface blocks are "
6098 var
= new(state
) ir_variable(block_array_type
,
6099 this->instance_name
,
6102 var
= new(state
) ir_variable(block_type
,
6103 this->instance_name
,
6107 var
->data
.matrix_layout
= matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
6108 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
: matrix_layout
;
6110 if (var_mode
== ir_var_shader_in
|| var_mode
== ir_var_uniform
)
6111 var
->data
.read_only
= true;
6113 if (state
->stage
== MESA_SHADER_GEOMETRY
&& var_mode
== ir_var_shader_in
)
6114 handle_geometry_shader_input_decl(state
, loc
, var
);
6115 else if ((state
->stage
== MESA_SHADER_TESS_CTRL
||
6116 state
->stage
== MESA_SHADER_TESS_EVAL
) && var_mode
== ir_var_shader_in
)
6117 handle_tess_shader_input_decl(state
, loc
, var
);
6118 else if (state
->stage
== MESA_SHADER_TESS_CTRL
&& var_mode
== ir_var_shader_out
)
6119 handle_tess_ctrl_shader_output_decl(state
, loc
, var
);
6121 if (ir_variable
*earlier
=
6122 state
->symbols
->get_variable(this->instance_name
)) {
6123 if (!redeclaring_per_vertex
) {
6124 _mesa_glsl_error(&loc
, state
, "`%s' redeclared",
6125 this->instance_name
);
6127 earlier
->data
.how_declared
= ir_var_declared_normally
;
6128 earlier
->type
= var
->type
;
6129 earlier
->reinit_interface_type(block_type
);
6132 /* Propagate the "binding" keyword into this UBO's fields;
6133 * the UBO declaration itself doesn't get an ir_variable unless it
6134 * has an instance name. This is ugly.
6136 var
->data
.explicit_binding
= this->layout
.flags
.q
.explicit_binding
;
6137 var
->data
.binding
= this->layout
.binding
;
6139 state
->symbols
->add_variable(var
);
6140 instructions
->push_tail(var
);
6143 /* In order to have an array size, the block must also be declared with
6146 assert(this->array_specifier
== NULL
);
6148 for (unsigned i
= 0; i
< num_variables
; i
++) {
6150 new(state
) ir_variable(fields
[i
].type
,
6151 ralloc_strdup(state
, fields
[i
].name
),
6153 var
->data
.interpolation
= fields
[i
].interpolation
;
6154 var
->data
.centroid
= fields
[i
].centroid
;
6155 var
->data
.sample
= fields
[i
].sample
;
6156 var
->data
.patch
= fields
[i
].patch
;
6157 var
->init_interface_type(block_type
);
6159 if (var_mode
== ir_var_shader_in
|| var_mode
== ir_var_uniform
)
6160 var
->data
.read_only
= true;
6162 if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
) {
6163 var
->data
.matrix_layout
= matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
6164 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
: matrix_layout
;
6166 var
->data
.matrix_layout
= fields
[i
].matrix_layout
;
6169 if (fields
[i
].stream
!= -1 &&
6170 ((unsigned)fields
[i
].stream
) != this->layout
.stream
) {
6171 _mesa_glsl_error(&loc
, state
,
6172 "stream layout qualifier on "
6173 "interface block member `%s' does not match "
6174 "the interface block (%d vs %d)",
6175 var
->name
, fields
[i
].stream
, this->layout
.stream
);
6178 var
->data
.stream
= this->layout
.stream
;
6180 /* Examine var name here since var may get deleted in the next call */
6181 bool var_is_gl_id
= is_gl_identifier(var
->name
);
6183 if (redeclaring_per_vertex
) {
6184 ir_variable
*earlier
=
6185 get_variable_being_redeclared(var
, loc
, state
,
6186 true /* allow_all_redeclarations */);
6187 if (!var_is_gl_id
|| earlier
== NULL
) {
6188 _mesa_glsl_error(&loc
, state
,
6189 "redeclaration of gl_PerVertex can only "
6190 "include built-in variables");
6191 } else if (earlier
->data
.how_declared
== ir_var_declared_normally
) {
6192 _mesa_glsl_error(&loc
, state
,
6193 "`%s' has already been redeclared",
6196 earlier
->data
.how_declared
= ir_var_declared_in_block
;
6197 earlier
->reinit_interface_type(block_type
);
6202 if (state
->symbols
->get_variable(var
->name
) != NULL
)
6203 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
6205 /* Propagate the "binding" keyword into this UBO/SSBO's fields.
6206 * The UBO declaration itself doesn't get an ir_variable unless it
6207 * has an instance name. This is ugly.
6209 var
->data
.explicit_binding
= this->layout
.flags
.q
.explicit_binding
;
6210 var
->data
.binding
= this->layout
.binding
;
6212 state
->symbols
->add_variable(var
);
6213 instructions
->push_tail(var
);
6216 if (redeclaring_per_vertex
&& block_type
!= earlier_per_vertex
) {
6217 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec:
6219 * It is also a compilation error ... to redeclare a built-in
6220 * block and then use a member from that built-in block that was
6221 * not included in the redeclaration.
6223 * This appears to be a clarification to the behaviour established
6224 * for gl_PerVertex by GLSL 1.50, therefore we implement this
6225 * behaviour regardless of GLSL version.
6227 * To prevent the shader from using a member that was not included in
6228 * the redeclaration, we disable any ir_variables that are still
6229 * associated with the old declaration of gl_PerVertex (since we've
6230 * already updated all of the variables contained in the new
6231 * gl_PerVertex to point to it).
6233 * As a side effect this will prevent
6234 * validate_intrastage_interface_blocks() from getting confused and
6235 * thinking there are conflicting definitions of gl_PerVertex in the
6238 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
6239 ir_variable
*const var
= node
->as_variable();
6241 var
->get_interface_type() == earlier_per_vertex
&&
6242 var
->data
.mode
== var_mode
) {
6243 if (var
->data
.how_declared
== ir_var_declared_normally
) {
6244 _mesa_glsl_error(&loc
, state
,
6245 "redeclaration of gl_PerVertex cannot "
6246 "follow a redeclaration of `%s'",
6249 state
->symbols
->disable_variable(var
->name
);
6261 ast_tcs_output_layout::hir(exec_list
*instructions
,
6262 struct _mesa_glsl_parse_state
*state
)
6264 YYLTYPE loc
= this->get_location();
6266 /* If any tessellation control output layout declaration preceded this
6267 * one, make sure it was consistent with this one.
6269 if (state
->tcs_output_vertices_specified
&&
6270 state
->out_qualifier
->vertices
!= this->vertices
) {
6271 _mesa_glsl_error(&loc
, state
,
6272 "tessellation control shader output layout does not "
6273 "match previous declaration");
6277 /* If any shader outputs occurred before this declaration and specified an
6278 * array size, make sure the size they specified is consistent with the
6281 unsigned num_vertices
= this->vertices
;
6282 if (state
->tcs_output_size
!= 0 && state
->tcs_output_size
!= num_vertices
) {
6283 _mesa_glsl_error(&loc
, state
,
6284 "this tessellation control shader output layout "
6285 "specifies %u vertices, but a previous output "
6286 "is declared with size %u",
6287 num_vertices
, state
->tcs_output_size
);
6291 state
->tcs_output_vertices_specified
= true;
6293 /* If any shader outputs occurred before this declaration and did not
6294 * specify an array size, their size is determined now.
6296 foreach_in_list (ir_instruction
, node
, instructions
) {
6297 ir_variable
*var
= node
->as_variable();
6298 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_out
)
6301 /* Note: Not all tessellation control shader output are arrays. */
6302 if (!var
->type
->is_unsized_array() || var
->data
.patch
)
6305 if (var
->data
.max_array_access
>= num_vertices
) {
6306 _mesa_glsl_error(&loc
, state
,
6307 "this tessellation control shader output layout "
6308 "specifies %u vertices, but an access to element "
6309 "%u of output `%s' already exists", num_vertices
,
6310 var
->data
.max_array_access
, var
->name
);
6312 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
6322 ast_gs_input_layout::hir(exec_list
*instructions
,
6323 struct _mesa_glsl_parse_state
*state
)
6325 YYLTYPE loc
= this->get_location();
6327 /* If any geometry input layout declaration preceded this one, make sure it
6328 * was consistent with this one.
6330 if (state
->gs_input_prim_type_specified
&&
6331 state
->in_qualifier
->prim_type
!= this->prim_type
) {
6332 _mesa_glsl_error(&loc
, state
,
6333 "geometry shader input layout does not match"
6334 " previous declaration");
6338 /* If any shader inputs occurred before this declaration and specified an
6339 * array size, make sure the size they specified is consistent with the
6342 unsigned num_vertices
= vertices_per_prim(this->prim_type
);
6343 if (state
->gs_input_size
!= 0 && state
->gs_input_size
!= num_vertices
) {
6344 _mesa_glsl_error(&loc
, state
,
6345 "this geometry shader input layout implies %u vertices"
6346 " per primitive, but a previous input is declared"
6347 " with size %u", num_vertices
, state
->gs_input_size
);
6351 state
->gs_input_prim_type_specified
= true;
6353 /* If any shader inputs occurred before this declaration and did not
6354 * specify an array size, their size is determined now.
6356 foreach_in_list(ir_instruction
, node
, instructions
) {
6357 ir_variable
*var
= node
->as_variable();
6358 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_in
)
6361 /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
6365 if (var
->type
->is_unsized_array()) {
6366 if (var
->data
.max_array_access
>= num_vertices
) {
6367 _mesa_glsl_error(&loc
, state
,
6368 "this geometry shader input layout implies %u"
6369 " vertices, but an access to element %u of input"
6370 " `%s' already exists", num_vertices
,
6371 var
->data
.max_array_access
, var
->name
);
6373 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
6384 ast_cs_input_layout::hir(exec_list
*instructions
,
6385 struct _mesa_glsl_parse_state
*state
)
6387 YYLTYPE loc
= this->get_location();
6389 /* If any compute input layout declaration preceded this one, make sure it
6390 * was consistent with this one.
6392 if (state
->cs_input_local_size_specified
) {
6393 for (int i
= 0; i
< 3; i
++) {
6394 if (state
->cs_input_local_size
[i
] != this->local_size
[i
]) {
6395 _mesa_glsl_error(&loc
, state
,
6396 "compute shader input layout does not match"
6397 " previous declaration");
6403 /* From the ARB_compute_shader specification:
6405 * If the local size of the shader in any dimension is greater
6406 * than the maximum size supported by the implementation for that
6407 * dimension, a compile-time error results.
6409 * It is not clear from the spec how the error should be reported if
6410 * the total size of the work group exceeds
6411 * MAX_COMPUTE_WORK_GROUP_INVOCATIONS, but it seems reasonable to
6412 * report it at compile time as well.
6414 GLuint64 total_invocations
= 1;
6415 for (int i
= 0; i
< 3; i
++) {
6416 if (this->local_size
[i
] > state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]) {
6417 _mesa_glsl_error(&loc
, state
,
6418 "local_size_%c exceeds MAX_COMPUTE_WORK_GROUP_SIZE"
6420 state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]);
6423 total_invocations
*= this->local_size
[i
];
6424 if (total_invocations
>
6425 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
) {
6426 _mesa_glsl_error(&loc
, state
,
6427 "product of local_sizes exceeds "
6428 "MAX_COMPUTE_WORK_GROUP_INVOCATIONS (%d)",
6429 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
);
6434 state
->cs_input_local_size_specified
= true;
6435 for (int i
= 0; i
< 3; i
++)
6436 state
->cs_input_local_size
[i
] = this->local_size
[i
];
6438 /* We may now declare the built-in constant gl_WorkGroupSize (see
6439 * builtin_variable_generator::generate_constants() for why we didn't
6440 * declare it earlier).
6442 ir_variable
*var
= new(state
->symbols
)
6443 ir_variable(glsl_type::uvec3_type
, "gl_WorkGroupSize", ir_var_auto
);
6444 var
->data
.how_declared
= ir_var_declared_implicitly
;
6445 var
->data
.read_only
= true;
6446 instructions
->push_tail(var
);
6447 state
->symbols
->add_variable(var
);
6448 ir_constant_data data
;
6449 memset(&data
, 0, sizeof(data
));
6450 for (int i
= 0; i
< 3; i
++)
6451 data
.u
[i
] = this->local_size
[i
];
6452 var
->constant_value
= new(var
) ir_constant(glsl_type::uvec3_type
, &data
);
6453 var
->constant_initializer
=
6454 new(var
) ir_constant(glsl_type::uvec3_type
, &data
);
6455 var
->data
.has_initializer
= true;
6462 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
6463 exec_list
*instructions
)
6465 bool gl_FragColor_assigned
= false;
6466 bool gl_FragData_assigned
= false;
6467 bool user_defined_fs_output_assigned
= false;
6468 ir_variable
*user_defined_fs_output
= NULL
;
6470 /* It would be nice to have proper location information. */
6472 memset(&loc
, 0, sizeof(loc
));
6474 foreach_in_list(ir_instruction
, node
, instructions
) {
6475 ir_variable
*var
= node
->as_variable();
6477 if (!var
|| !var
->data
.assigned
)
6480 if (strcmp(var
->name
, "gl_FragColor") == 0)
6481 gl_FragColor_assigned
= true;
6482 else if (strcmp(var
->name
, "gl_FragData") == 0)
6483 gl_FragData_assigned
= true;
6484 else if (!is_gl_identifier(var
->name
)) {
6485 if (state
->stage
== MESA_SHADER_FRAGMENT
&&
6486 var
->data
.mode
== ir_var_shader_out
) {
6487 user_defined_fs_output_assigned
= true;
6488 user_defined_fs_output
= var
;
6493 /* From the GLSL 1.30 spec:
6495 * "If a shader statically assigns a value to gl_FragColor, it
6496 * may not assign a value to any element of gl_FragData. If a
6497 * shader statically writes a value to any element of
6498 * gl_FragData, it may not assign a value to
6499 * gl_FragColor. That is, a shader may assign values to either
6500 * gl_FragColor or gl_FragData, but not both. Multiple shaders
6501 * linked together must also consistently write just one of
6502 * these variables. Similarly, if user declared output
6503 * variables are in use (statically assigned to), then the
6504 * built-in variables gl_FragColor and gl_FragData may not be
6505 * assigned to. These incorrect usages all generate compile
6508 if (gl_FragColor_assigned
&& gl_FragData_assigned
) {
6509 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
6510 "`gl_FragColor' and `gl_FragData'");
6511 } else if (gl_FragColor_assigned
&& user_defined_fs_output_assigned
) {
6512 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
6513 "`gl_FragColor' and `%s'",
6514 user_defined_fs_output
->name
);
6515 } else if (gl_FragData_assigned
&& user_defined_fs_output_assigned
) {
6516 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
6517 "`gl_FragData' and `%s'",
6518 user_defined_fs_output
->name
);
6524 remove_per_vertex_blocks(exec_list
*instructions
,
6525 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
)
6527 /* Find the gl_PerVertex interface block of the appropriate (in/out) mode,
6528 * if it exists in this shader type.
6530 const glsl_type
*per_vertex
= NULL
;
6532 case ir_var_shader_in
:
6533 if (ir_variable
*gl_in
= state
->symbols
->get_variable("gl_in"))
6534 per_vertex
= gl_in
->get_interface_type();
6536 case ir_var_shader_out
:
6537 if (ir_variable
*gl_Position
=
6538 state
->symbols
->get_variable("gl_Position")) {
6539 per_vertex
= gl_Position
->get_interface_type();
6543 assert(!"Unexpected mode");
6547 /* If we didn't find a built-in gl_PerVertex interface block, then we don't
6548 * need to do anything.
6550 if (per_vertex
== NULL
)
6553 /* If the interface block is used by the shader, then we don't need to do
6556 interface_block_usage_visitor
v(mode
, per_vertex
);
6557 v
.run(instructions
);
6558 if (v
.usage_found())
6561 /* Remove any ir_variable declarations that refer to the interface block
6564 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
6565 ir_variable
*const var
= node
->as_variable();
6566 if (var
!= NULL
&& var
->get_interface_type() == per_vertex
&&
6567 var
->data
.mode
== mode
) {
6568 state
->symbols
->disable_variable(var
->name
);