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"
57 #include "main/shaderobj.h"
59 #include "ir_builder.h"
61 using namespace ir_builder
;
64 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
65 exec_list
*instructions
);
67 remove_per_vertex_blocks(exec_list
*instructions
,
68 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
);
72 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
74 _mesa_glsl_initialize_variables(instructions
, state
);
76 state
->symbols
->separate_function_namespace
= state
->language_version
== 110;
78 state
->current_function
= NULL
;
80 state
->toplevel_ir
= instructions
;
82 state
->gs_input_prim_type_specified
= false;
83 state
->tcs_output_vertices_specified
= false;
84 state
->cs_input_local_size_specified
= false;
86 /* Section 4.2 of the GLSL 1.20 specification states:
87 * "The built-in functions are scoped in a scope outside the global scope
88 * users declare global variables in. That is, a shader's global scope,
89 * available for user-defined functions and global variables, is nested
90 * inside the scope containing the built-in functions."
92 * Since built-in functions like ftransform() access built-in variables,
93 * it follows that those must be in the outer scope as well.
95 * We push scope here to create this nesting effect...but don't pop.
96 * This way, a shader's globals are still in the symbol table for use
99 state
->symbols
->push_scope();
101 foreach_list_typed (ast_node
, ast
, link
, & state
->translation_unit
)
102 ast
->hir(instructions
, state
);
104 detect_recursion_unlinked(state
, instructions
);
105 detect_conflicting_assignments(state
, instructions
);
107 state
->toplevel_ir
= NULL
;
109 /* Move all of the variable declarations to the front of the IR list, and
110 * reverse the order. This has the (intended!) side effect that vertex
111 * shader inputs and fragment shader outputs will appear in the IR in the
112 * same order that they appeared in the shader code. This results in the
113 * locations being assigned in the declared order. Many (arguably buggy)
114 * applications depend on this behavior, and it matches what nearly all
117 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
118 ir_variable
*const var
= node
->as_variable();
124 instructions
->push_head(var
);
127 /* Figure out if gl_FragCoord is actually used in fragment shader */
128 ir_variable
*const var
= state
->symbols
->get_variable("gl_FragCoord");
130 state
->fs_uses_gl_fragcoord
= var
->data
.used
;
132 /* From section 7.1 (Built-In Language Variables) of the GLSL 4.10 spec:
134 * If multiple shaders using members of a built-in block belonging to
135 * the same interface are linked together in the same program, they
136 * must all redeclare the built-in block in the same way, as described
137 * in section 4.3.7 "Interface Blocks" for interface block matching, or
138 * a link error will result.
140 * The phrase "using members of a built-in block" implies that if two
141 * shaders are linked together and one of them *does not use* any members
142 * of the built-in block, then that shader does not need to have a matching
143 * redeclaration of the built-in block.
145 * This appears to be a clarification to the behaviour established for
146 * gl_PerVertex by GLSL 1.50, therefore implement it regardless of GLSL
149 * The definition of "interface" in section 4.3.7 that applies here is as
152 * The boundary between adjacent programmable pipeline stages: This
153 * spans all the outputs in all compilation units of the first stage
154 * and all the inputs in all compilation units of the second stage.
156 * Therefore this rule applies to both inter- and intra-stage linking.
158 * The easiest way to implement this is to check whether the shader uses
159 * gl_PerVertex right after ast-to-ir conversion, and if it doesn't, simply
160 * remove all the relevant variable declaration from the IR, so that the
161 * linker won't see them and complain about mismatches.
163 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_in
);
164 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_out
);
168 static ir_expression_operation
169 get_conversion_operation(const glsl_type
*to
, const glsl_type
*from
,
170 struct _mesa_glsl_parse_state
*state
)
172 switch (to
->base_type
) {
173 case GLSL_TYPE_FLOAT
:
174 switch (from
->base_type
) {
175 case GLSL_TYPE_INT
: return ir_unop_i2f
;
176 case GLSL_TYPE_UINT
: return ir_unop_u2f
;
177 case GLSL_TYPE_DOUBLE
: return ir_unop_d2f
;
178 default: return (ir_expression_operation
)0;
182 if (!state
->is_version(400, 0) && !state
->ARB_gpu_shader5_enable
)
183 return (ir_expression_operation
)0;
184 switch (from
->base_type
) {
185 case GLSL_TYPE_INT
: return ir_unop_i2u
;
186 default: return (ir_expression_operation
)0;
189 case GLSL_TYPE_DOUBLE
:
190 if (!state
->has_double())
191 return (ir_expression_operation
)0;
192 switch (from
->base_type
) {
193 case GLSL_TYPE_INT
: return ir_unop_i2d
;
194 case GLSL_TYPE_UINT
: return ir_unop_u2d
;
195 case GLSL_TYPE_FLOAT
: return ir_unop_f2d
;
196 default: return (ir_expression_operation
)0;
199 default: return (ir_expression_operation
)0;
205 * If a conversion is available, convert one operand to a different type
207 * The \c from \c ir_rvalue is converted "in place".
209 * \param to Type that the operand it to be converted to
210 * \param from Operand that is being converted
211 * \param state GLSL compiler state
214 * If a conversion is possible (or unnecessary), \c true is returned.
215 * Otherwise \c false is returned.
218 apply_implicit_conversion(const glsl_type
*to
, ir_rvalue
* &from
,
219 struct _mesa_glsl_parse_state
*state
)
222 if (to
->base_type
== from
->type
->base_type
)
225 /* Prior to GLSL 1.20, there are no implicit conversions */
226 if (!state
->is_version(120, 0))
229 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
231 * "There are no implicit array or structure conversions. For
232 * example, an array of int cannot be implicitly converted to an
235 if (!to
->is_numeric() || !from
->type
->is_numeric())
238 /* We don't actually want the specific type `to`, we want a type
239 * with the same base type as `to`, but the same vector width as
242 to
= glsl_type::get_instance(to
->base_type
, from
->type
->vector_elements
,
243 from
->type
->matrix_columns
);
245 ir_expression_operation op
= get_conversion_operation(to
, from
->type
, state
);
247 from
= new(ctx
) ir_expression(op
, to
, from
, NULL
);
255 static const struct glsl_type
*
256 arithmetic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
258 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
260 const glsl_type
*type_a
= value_a
->type
;
261 const glsl_type
*type_b
= value_b
->type
;
263 /* From GLSL 1.50 spec, page 56:
265 * "The arithmetic binary operators add (+), subtract (-),
266 * multiply (*), and divide (/) operate on integer and
267 * floating-point scalars, vectors, and matrices."
269 if (!type_a
->is_numeric() || !type_b
->is_numeric()) {
270 _mesa_glsl_error(loc
, state
,
271 "operands to arithmetic operators must be numeric");
272 return glsl_type::error_type
;
276 /* "If one operand is floating-point based and the other is
277 * not, then the conversions from Section 4.1.10 "Implicit
278 * Conversions" are applied to the non-floating-point-based operand."
280 if (!apply_implicit_conversion(type_a
, value_b
, state
)
281 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
282 _mesa_glsl_error(loc
, state
,
283 "could not implicitly convert operands to "
284 "arithmetic operator");
285 return glsl_type::error_type
;
287 type_a
= value_a
->type
;
288 type_b
= value_b
->type
;
290 /* "If the operands are integer types, they must both be signed or
293 * From this rule and the preceeding conversion it can be inferred that
294 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
295 * The is_numeric check above already filtered out the case where either
296 * type is not one of these, so now the base types need only be tested for
299 if (type_a
->base_type
!= type_b
->base_type
) {
300 _mesa_glsl_error(loc
, state
,
301 "base type mismatch for arithmetic operator");
302 return glsl_type::error_type
;
305 /* "All arithmetic binary operators result in the same fundamental type
306 * (signed integer, unsigned integer, or floating-point) as the
307 * operands they operate on, after operand type conversion. After
308 * conversion, the following cases are valid
310 * * The two operands are scalars. In this case the operation is
311 * applied, resulting in a scalar."
313 if (type_a
->is_scalar() && type_b
->is_scalar())
316 /* "* One operand is a scalar, and the other is a vector or matrix.
317 * In this case, the scalar operation is applied independently to each
318 * component of the vector or matrix, resulting in the same size
321 if (type_a
->is_scalar()) {
322 if (!type_b
->is_scalar())
324 } else if (type_b
->is_scalar()) {
328 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
329 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
332 assert(!type_a
->is_scalar());
333 assert(!type_b
->is_scalar());
335 /* "* The two operands are vectors of the same size. In this case, the
336 * operation is done component-wise resulting in the same size
339 if (type_a
->is_vector() && type_b
->is_vector()) {
340 if (type_a
== type_b
) {
343 _mesa_glsl_error(loc
, state
,
344 "vector size mismatch for arithmetic operator");
345 return glsl_type::error_type
;
349 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
350 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
351 * <vector, vector> have been handled. At least one of the operands must
352 * be matrix. Further, since there are no integer matrix types, the base
353 * type of both operands must be float.
355 assert(type_a
->is_matrix() || type_b
->is_matrix());
356 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
||
357 type_a
->base_type
== GLSL_TYPE_DOUBLE
);
358 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
||
359 type_b
->base_type
== GLSL_TYPE_DOUBLE
);
361 /* "* The operator is add (+), subtract (-), or divide (/), and the
362 * operands are matrices with the same number of rows and the same
363 * number of columns. In this case, the operation is done component-
364 * wise resulting in the same size matrix."
365 * * The operator is multiply (*), where both operands are matrices or
366 * one operand is a vector and the other a matrix. A right vector
367 * operand is treated as a column vector and a left vector operand as a
368 * row vector. In all these cases, it is required that the number of
369 * columns of the left operand is equal to the number of rows of the
370 * right operand. Then, the multiply (*) operation does a linear
371 * algebraic multiply, yielding an object that has the same number of
372 * rows as the left operand and the same number of columns as the right
373 * operand. Section 5.10 "Vector and Matrix Operations" explains in
374 * more detail how vectors and matrices are operated on."
377 if (type_a
== type_b
)
380 const glsl_type
*type
= glsl_type::get_mul_type(type_a
, type_b
);
382 if (type
== glsl_type::error_type
) {
383 _mesa_glsl_error(loc
, state
,
384 "size mismatch for matrix multiplication");
391 /* "All other cases are illegal."
393 _mesa_glsl_error(loc
, state
, "type mismatch");
394 return glsl_type::error_type
;
398 static const struct glsl_type
*
399 unary_arithmetic_result_type(const struct glsl_type
*type
,
400 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
402 /* From GLSL 1.50 spec, page 57:
404 * "The arithmetic unary operators negate (-), post- and pre-increment
405 * and decrement (-- and ++) operate on integer or floating-point
406 * values (including vectors and matrices). All unary operators work
407 * component-wise on their operands. These result with the same type
410 if (!type
->is_numeric()) {
411 _mesa_glsl_error(loc
, state
,
412 "operands to arithmetic operators must be numeric");
413 return glsl_type::error_type
;
420 * \brief Return the result type of a bit-logic operation.
422 * If the given types to the bit-logic operator are invalid, return
423 * glsl_type::error_type.
425 * \param type_a Type of LHS of bit-logic op
426 * \param type_b Type of RHS of bit-logic op
428 static const struct glsl_type
*
429 bit_logic_result_type(const struct glsl_type
*type_a
,
430 const struct glsl_type
*type_b
,
432 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
434 if (!state
->check_bitwise_operations_allowed(loc
)) {
435 return glsl_type::error_type
;
438 /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
440 * "The bitwise operators and (&), exclusive-or (^), and inclusive-or
441 * (|). The operands must be of type signed or unsigned integers or
444 if (!type_a
->is_integer()) {
445 _mesa_glsl_error(loc
, state
, "LHS of `%s' must be an integer",
446 ast_expression::operator_string(op
));
447 return glsl_type::error_type
;
449 if (!type_b
->is_integer()) {
450 _mesa_glsl_error(loc
, state
, "RHS of `%s' must be an integer",
451 ast_expression::operator_string(op
));
452 return glsl_type::error_type
;
455 /* "The fundamental types of the operands (signed or unsigned) must
458 if (type_a
->base_type
!= type_b
->base_type
) {
459 _mesa_glsl_error(loc
, state
, "operands of `%s' must have the same "
460 "base type", ast_expression::operator_string(op
));
461 return glsl_type::error_type
;
464 /* "The operands cannot be vectors of differing size." */
465 if (type_a
->is_vector() &&
466 type_b
->is_vector() &&
467 type_a
->vector_elements
!= type_b
->vector_elements
) {
468 _mesa_glsl_error(loc
, state
, "operands of `%s' cannot be vectors of "
469 "different sizes", ast_expression::operator_string(op
));
470 return glsl_type::error_type
;
473 /* "If one operand is a scalar and the other a vector, the scalar is
474 * applied component-wise to the vector, resulting in the same type as
475 * the vector. The fundamental types of the operands [...] will be the
476 * resulting fundamental type."
478 if (type_a
->is_scalar())
484 static const struct glsl_type
*
485 modulus_result_type(const struct glsl_type
*type_a
,
486 const struct glsl_type
*type_b
,
487 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
489 if (!state
->check_version(130, 300, loc
, "operator '%%' is reserved")) {
490 return glsl_type::error_type
;
493 /* From GLSL 1.50 spec, page 56:
494 * "The operator modulus (%) operates on signed or unsigned integers or
495 * integer vectors. The operand types must both be signed or both be
498 if (!type_a
->is_integer()) {
499 _mesa_glsl_error(loc
, state
, "LHS of operator %% must be an integer");
500 return glsl_type::error_type
;
502 if (!type_b
->is_integer()) {
503 _mesa_glsl_error(loc
, state
, "RHS of operator %% must be an integer");
504 return glsl_type::error_type
;
506 if (type_a
->base_type
!= type_b
->base_type
) {
507 _mesa_glsl_error(loc
, state
,
508 "operands of %% must have the same base type");
509 return glsl_type::error_type
;
512 /* "The operands cannot be vectors of differing size. If one operand is
513 * a scalar and the other vector, then the scalar is applied component-
514 * wise to the vector, resulting in the same type as the vector. If both
515 * are vectors of the same size, the result is computed component-wise."
517 if (type_a
->is_vector()) {
518 if (!type_b
->is_vector()
519 || (type_a
->vector_elements
== type_b
->vector_elements
))
524 /* "The operator modulus (%) is not defined for any other data types
525 * (non-integer types)."
527 _mesa_glsl_error(loc
, state
, "type mismatch");
528 return glsl_type::error_type
;
532 static const struct glsl_type
*
533 relational_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
534 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
536 const glsl_type
*type_a
= value_a
->type
;
537 const glsl_type
*type_b
= value_b
->type
;
539 /* From GLSL 1.50 spec, page 56:
540 * "The relational operators greater than (>), less than (<), greater
541 * than or equal (>=), and less than or equal (<=) operate only on
542 * scalar integer and scalar floating-point expressions."
544 if (!type_a
->is_numeric()
545 || !type_b
->is_numeric()
546 || !type_a
->is_scalar()
547 || !type_b
->is_scalar()) {
548 _mesa_glsl_error(loc
, state
,
549 "operands to relational operators must be scalar and "
551 return glsl_type::error_type
;
554 /* "Either the operands' types must match, or the conversions from
555 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
556 * operand, after which the types must match."
558 if (!apply_implicit_conversion(type_a
, value_b
, state
)
559 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
560 _mesa_glsl_error(loc
, state
,
561 "could not implicitly convert operands to "
562 "relational operator");
563 return glsl_type::error_type
;
565 type_a
= value_a
->type
;
566 type_b
= value_b
->type
;
568 if (type_a
->base_type
!= type_b
->base_type
) {
569 _mesa_glsl_error(loc
, state
, "base type mismatch");
570 return glsl_type::error_type
;
573 /* "The result is scalar Boolean."
575 return glsl_type::bool_type
;
579 * \brief Return the result type of a bit-shift operation.
581 * If the given types to the bit-shift operator are invalid, return
582 * glsl_type::error_type.
584 * \param type_a Type of LHS of bit-shift op
585 * \param type_b Type of RHS of bit-shift op
587 static const struct glsl_type
*
588 shift_result_type(const struct glsl_type
*type_a
,
589 const struct glsl_type
*type_b
,
591 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
593 if (!state
->check_bitwise_operations_allowed(loc
)) {
594 return glsl_type::error_type
;
597 /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
599 * "The shift operators (<<) and (>>). For both operators, the operands
600 * must be signed or unsigned integers or integer vectors. One operand
601 * can be signed while the other is unsigned."
603 if (!type_a
->is_integer()) {
604 _mesa_glsl_error(loc
, state
, "LHS of operator %s must be an integer or "
605 "integer vector", ast_expression::operator_string(op
));
606 return glsl_type::error_type
;
609 if (!type_b
->is_integer()) {
610 _mesa_glsl_error(loc
, state
, "RHS of operator %s must be an integer or "
611 "integer vector", ast_expression::operator_string(op
));
612 return glsl_type::error_type
;
615 /* "If the first operand is a scalar, the second operand has to be
618 if (type_a
->is_scalar() && !type_b
->is_scalar()) {
619 _mesa_glsl_error(loc
, state
, "if the first operand of %s is scalar, the "
620 "second must be scalar as well",
621 ast_expression::operator_string(op
));
622 return glsl_type::error_type
;
625 /* If both operands are vectors, check that they have same number of
628 if (type_a
->is_vector() &&
629 type_b
->is_vector() &&
630 type_a
->vector_elements
!= type_b
->vector_elements
) {
631 _mesa_glsl_error(loc
, state
, "vector operands to operator %s must "
632 "have same number of elements",
633 ast_expression::operator_string(op
));
634 return glsl_type::error_type
;
637 /* "In all cases, the resulting type will be the same type as the left
644 * Returns the innermost array index expression in an rvalue tree.
645 * This is the largest indexing level -- if an array of blocks, then
646 * it is the block index rather than an indexing expression for an
647 * array-typed member of an array of blocks.
650 find_innermost_array_index(ir_rvalue
*rv
)
652 ir_dereference_array
*last
= NULL
;
654 if (rv
->as_dereference_array()) {
655 last
= rv
->as_dereference_array();
657 } else if (rv
->as_dereference_record())
658 rv
= rv
->as_dereference_record()->record
;
659 else if (rv
->as_swizzle())
660 rv
= rv
->as_swizzle()->val
;
666 return last
->array_index
;
672 * Validates that a value can be assigned to a location with a specified type
674 * Validates that \c rhs can be assigned to some location. If the types are
675 * not an exact match but an automatic conversion is possible, \c rhs will be
679 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
680 * Otherwise the actual RHS to be assigned will be returned. This may be
681 * \c rhs, or it may be \c rhs after some type conversion.
684 * In addition to being used for assignments, this function is used to
685 * type-check return values.
688 validate_assignment(struct _mesa_glsl_parse_state
*state
,
689 YYLTYPE loc
, ir_rvalue
*lhs
,
690 ir_rvalue
*rhs
, bool is_initializer
)
692 /* If there is already some error in the RHS, just return it. Anything
693 * else will lead to an avalanche of error message back to the user.
695 if (rhs
->type
->is_error())
698 /* In the Tessellation Control Shader:
699 * If a per-vertex output variable is used as an l-value, it is an error
700 * if the expression indicating the vertex number is not the identifier
703 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
704 ir_variable
*var
= lhs
->variable_referenced();
705 if (var
->data
.mode
== ir_var_shader_out
&& !var
->data
.patch
) {
706 ir_rvalue
*index
= find_innermost_array_index(lhs
);
707 ir_variable
*index_var
= index
? index
->variable_referenced() : NULL
;
708 if (!index_var
|| strcmp(index_var
->name
, "gl_InvocationID") != 0) {
709 _mesa_glsl_error(&loc
, state
,
710 "Tessellation control shader outputs can only "
711 "be indexed by gl_InvocationID");
717 /* If the types are identical, the assignment can trivially proceed.
719 if (rhs
->type
== lhs
->type
)
722 /* If the array element types are the same and the LHS is unsized,
723 * the assignment is okay for initializers embedded in variable
726 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
727 * is handled by ir_dereference::is_lvalue.
729 if (lhs
->type
->is_unsized_array() && rhs
->type
->is_array()
730 && (lhs
->type
->fields
.array
== rhs
->type
->fields
.array
)) {
731 if (is_initializer
) {
734 _mesa_glsl_error(&loc
, state
,
735 "implicitly sized arrays cannot be assigned");
740 /* Check for implicit conversion in GLSL 1.20 */
741 if (apply_implicit_conversion(lhs
->type
, rhs
, state
)) {
742 if (rhs
->type
== lhs
->type
)
746 _mesa_glsl_error(&loc
, state
,
747 "%s of type %s cannot be assigned to "
748 "variable of type %s",
749 is_initializer
? "initializer" : "value",
750 rhs
->type
->name
, lhs
->type
->name
);
756 mark_whole_array_access(ir_rvalue
*access
)
758 ir_dereference_variable
*deref
= access
->as_dereference_variable();
760 if (deref
&& deref
->var
) {
761 deref
->var
->data
.max_array_access
= deref
->type
->length
- 1;
766 do_assignment(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
767 const char *non_lvalue_description
,
768 ir_rvalue
*lhs
, ir_rvalue
*rhs
,
769 ir_rvalue
**out_rvalue
, bool needs_rvalue
,
774 bool error_emitted
= (lhs
->type
->is_error() || rhs
->type
->is_error());
775 ir_rvalue
*extract_channel
= NULL
;
777 /* If the assignment LHS comes back as an ir_binop_vector_extract
778 * expression, move it to the RHS as an ir_triop_vector_insert.
780 if (lhs
->ir_type
== ir_type_expression
) {
781 ir_expression
*const lhs_expr
= lhs
->as_expression();
783 if (unlikely(lhs_expr
->operation
== ir_binop_vector_extract
)) {
785 validate_assignment(state
, lhs_loc
, lhs
,
786 rhs
, is_initializer
);
788 if (new_rhs
== NULL
) {
792 * - LHS: (expression float vector_extract <vec> <channel>)
796 * - RHS: (expression vec2 vector_insert <vec> <channel> <scalar>)
798 * The LHS type is now a vector instead of a scalar. Since GLSL
799 * allows assignments to be used as rvalues, we need to re-extract
800 * the channel from assignment_temp when returning the rvalue.
802 extract_channel
= lhs_expr
->operands
[1];
803 rhs
= new(ctx
) ir_expression(ir_triop_vector_insert
,
804 lhs_expr
->operands
[0]->type
,
805 lhs_expr
->operands
[0],
808 lhs
= lhs_expr
->operands
[0]->clone(ctx
, NULL
);
813 ir_variable
*lhs_var
= lhs
->variable_referenced();
815 lhs_var
->data
.assigned
= true;
817 if (!error_emitted
) {
818 if (non_lvalue_description
!= NULL
) {
819 _mesa_glsl_error(&lhs_loc
, state
,
821 non_lvalue_description
);
822 error_emitted
= true;
823 } else if (lhs_var
!= NULL
&& lhs_var
->data
.read_only
) {
824 _mesa_glsl_error(&lhs_loc
, state
,
825 "assignment to read-only variable '%s'",
827 error_emitted
= true;
828 } else if (lhs
->type
->is_array() &&
829 !state
->check_version(120, 300, &lhs_loc
,
830 "whole array assignment forbidden")) {
831 /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
833 * "Other binary or unary expressions, non-dereferenced
834 * arrays, function names, swizzles with repeated fields,
835 * and constants cannot be l-values."
837 * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00.
839 error_emitted
= true;
840 } else if (!lhs
->is_lvalue()) {
841 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
842 error_emitted
= true;
847 validate_assignment(state
, lhs_loc
, lhs
, rhs
, is_initializer
);
848 if (new_rhs
!= NULL
) {
851 /* If the LHS array was not declared with a size, it takes it size from
852 * the RHS. If the LHS is an l-value and a whole array, it must be a
853 * dereference of a variable. Any other case would require that the LHS
854 * is either not an l-value or not a whole array.
856 if (lhs
->type
->is_unsized_array()) {
857 ir_dereference
*const d
= lhs
->as_dereference();
861 ir_variable
*const var
= d
->variable_referenced();
865 if (var
->data
.max_array_access
>= unsigned(rhs
->type
->array_size())) {
866 /* FINISHME: This should actually log the location of the RHS. */
867 _mesa_glsl_error(& lhs_loc
, state
, "array size must be > %u due to "
869 var
->data
.max_array_access
);
872 var
->type
= glsl_type::get_array_instance(lhs
->type
->fields
.array
,
873 rhs
->type
->array_size());
876 if (lhs
->type
->is_array()) {
877 mark_whole_array_access(rhs
);
878 mark_whole_array_access(lhs
);
882 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
883 * but not post_inc) need the converted assigned value as an rvalue
884 * to handle things like:
889 ir_variable
*var
= new(ctx
) ir_variable(rhs
->type
, "assignment_tmp",
891 instructions
->push_tail(var
);
892 instructions
->push_tail(assign(var
, rhs
));
894 if (!error_emitted
) {
895 ir_dereference_variable
*deref_var
= new(ctx
) ir_dereference_variable(var
);
896 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, deref_var
));
898 ir_rvalue
*rvalue
= new(ctx
) ir_dereference_variable(var
);
900 if (extract_channel
) {
901 rvalue
= new(ctx
) ir_expression(ir_binop_vector_extract
,
903 extract_channel
->clone(ctx
, NULL
));
906 *out_rvalue
= rvalue
;
909 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, rhs
));
913 return error_emitted
;
917 get_lvalue_copy(exec_list
*instructions
, ir_rvalue
*lvalue
)
919 void *ctx
= ralloc_parent(lvalue
);
922 var
= new(ctx
) ir_variable(lvalue
->type
, "_post_incdec_tmp",
924 instructions
->push_tail(var
);
926 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
929 return new(ctx
) ir_dereference_variable(var
);
934 ast_node::hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
943 ast_function_expression::hir_no_rvalue(exec_list
*instructions
,
944 struct _mesa_glsl_parse_state
*state
)
946 (void)hir(instructions
, state
);
950 ast_aggregate_initializer::hir_no_rvalue(exec_list
*instructions
,
951 struct _mesa_glsl_parse_state
*state
)
953 (void)hir(instructions
, state
);
957 do_comparison(void *mem_ctx
, int operation
, ir_rvalue
*op0
, ir_rvalue
*op1
)
960 ir_rvalue
*cmp
= NULL
;
962 if (operation
== ir_binop_all_equal
)
963 join_op
= ir_binop_logic_and
;
965 join_op
= ir_binop_logic_or
;
967 switch (op0
->type
->base_type
) {
968 case GLSL_TYPE_FLOAT
:
972 case GLSL_TYPE_DOUBLE
:
973 return new(mem_ctx
) ir_expression(operation
, op0
, op1
);
975 case GLSL_TYPE_ARRAY
: {
976 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
977 ir_rvalue
*e0
, *e1
, *result
;
979 e0
= new(mem_ctx
) ir_dereference_array(op0
->clone(mem_ctx
, NULL
),
980 new(mem_ctx
) ir_constant(i
));
981 e1
= new(mem_ctx
) ir_dereference_array(op1
->clone(mem_ctx
, NULL
),
982 new(mem_ctx
) ir_constant(i
));
983 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
986 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
992 mark_whole_array_access(op0
);
993 mark_whole_array_access(op1
);
997 case GLSL_TYPE_STRUCT
: {
998 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
999 ir_rvalue
*e0
, *e1
, *result
;
1000 const char *field_name
= op0
->type
->fields
.structure
[i
].name
;
1002 e0
= new(mem_ctx
) ir_dereference_record(op0
->clone(mem_ctx
, NULL
),
1004 e1
= new(mem_ctx
) ir_dereference_record(op1
->clone(mem_ctx
, NULL
),
1006 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
1009 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
1017 case GLSL_TYPE_ERROR
:
1018 case GLSL_TYPE_VOID
:
1019 case GLSL_TYPE_SAMPLER
:
1020 case GLSL_TYPE_IMAGE
:
1021 case GLSL_TYPE_INTERFACE
:
1022 case GLSL_TYPE_ATOMIC_UINT
:
1023 case GLSL_TYPE_SUBROUTINE
:
1024 /* I assume a comparison of a struct containing a sampler just
1025 * ignores the sampler present in the type.
1031 cmp
= new(mem_ctx
) ir_constant(true);
1036 /* For logical operations, we want to ensure that the operands are
1037 * scalar booleans. If it isn't, emit an error and return a constant
1038 * boolean to avoid triggering cascading error messages.
1041 get_scalar_boolean_operand(exec_list
*instructions
,
1042 struct _mesa_glsl_parse_state
*state
,
1043 ast_expression
*parent_expr
,
1045 const char *operand_name
,
1046 bool *error_emitted
)
1048 ast_expression
*expr
= parent_expr
->subexpressions
[operand
];
1050 ir_rvalue
*val
= expr
->hir(instructions
, state
);
1052 if (val
->type
->is_boolean() && val
->type
->is_scalar())
1055 if (!*error_emitted
) {
1056 YYLTYPE loc
= expr
->get_location();
1057 _mesa_glsl_error(&loc
, state
, "%s of `%s' must be scalar boolean",
1059 parent_expr
->operator_string(parent_expr
->oper
));
1060 *error_emitted
= true;
1063 return new(ctx
) ir_constant(true);
1067 * If name refers to a builtin array whose maximum allowed size is less than
1068 * size, report an error and return true. Otherwise return false.
1071 check_builtin_array_max_size(const char *name
, unsigned size
,
1072 YYLTYPE loc
, struct _mesa_glsl_parse_state
*state
)
1074 if ((strcmp("gl_TexCoord", name
) == 0)
1075 && (size
> state
->Const
.MaxTextureCoords
)) {
1076 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1078 * "The size [of gl_TexCoord] can be at most
1079 * gl_MaxTextureCoords."
1081 _mesa_glsl_error(&loc
, state
, "`gl_TexCoord' array size cannot "
1082 "be larger than gl_MaxTextureCoords (%u)",
1083 state
->Const
.MaxTextureCoords
);
1084 } else if (strcmp("gl_ClipDistance", name
) == 0
1085 && size
> state
->Const
.MaxClipPlanes
) {
1086 /* From section 7.1 (Vertex Shader Special Variables) of the
1089 * "The gl_ClipDistance array is predeclared as unsized and
1090 * must be sized by the shader either redeclaring it with a
1091 * size or indexing it only with integral constant
1092 * expressions. ... The size can be at most
1093 * gl_MaxClipDistances."
1095 _mesa_glsl_error(&loc
, state
, "`gl_ClipDistance' array size cannot "
1096 "be larger than gl_MaxClipDistances (%u)",
1097 state
->Const
.MaxClipPlanes
);
1102 * Create the constant 1, of a which is appropriate for incrementing and
1103 * decrementing values of the given GLSL type. For example, if type is vec4,
1104 * this creates a constant value of 1.0 having type float.
1106 * If the given type is invalid for increment and decrement operators, return
1107 * a floating point 1--the error will be detected later.
1110 constant_one_for_inc_dec(void *ctx
, const glsl_type
*type
)
1112 switch (type
->base_type
) {
1113 case GLSL_TYPE_UINT
:
1114 return new(ctx
) ir_constant((unsigned) 1);
1116 return new(ctx
) ir_constant(1);
1118 case GLSL_TYPE_FLOAT
:
1119 return new(ctx
) ir_constant(1.0f
);
1124 ast_expression::hir(exec_list
*instructions
,
1125 struct _mesa_glsl_parse_state
*state
)
1127 return do_hir(instructions
, state
, true);
1131 ast_expression::hir_no_rvalue(exec_list
*instructions
,
1132 struct _mesa_glsl_parse_state
*state
)
1134 do_hir(instructions
, state
, false);
1138 ast_expression::do_hir(exec_list
*instructions
,
1139 struct _mesa_glsl_parse_state
*state
,
1143 static const int operations
[AST_NUM_OPERATORS
] = {
1144 -1, /* ast_assign doesn't convert to ir_expression. */
1145 -1, /* ast_plus doesn't convert to ir_expression. */
1159 ir_binop_any_nequal
,
1169 /* Note: The following block of expression types actually convert
1170 * to multiple IR instructions.
1172 ir_binop_mul
, /* ast_mul_assign */
1173 ir_binop_div
, /* ast_div_assign */
1174 ir_binop_mod
, /* ast_mod_assign */
1175 ir_binop_add
, /* ast_add_assign */
1176 ir_binop_sub
, /* ast_sub_assign */
1177 ir_binop_lshift
, /* ast_ls_assign */
1178 ir_binop_rshift
, /* ast_rs_assign */
1179 ir_binop_bit_and
, /* ast_and_assign */
1180 ir_binop_bit_xor
, /* ast_xor_assign */
1181 ir_binop_bit_or
, /* ast_or_assign */
1183 -1, /* ast_conditional doesn't convert to ir_expression. */
1184 ir_binop_add
, /* ast_pre_inc. */
1185 ir_binop_sub
, /* ast_pre_dec. */
1186 ir_binop_add
, /* ast_post_inc. */
1187 ir_binop_sub
, /* ast_post_dec. */
1188 -1, /* ast_field_selection doesn't conv to ir_expression. */
1189 -1, /* ast_array_index doesn't convert to ir_expression. */
1190 -1, /* ast_function_call doesn't conv to ir_expression. */
1191 -1, /* ast_identifier doesn't convert to ir_expression. */
1192 -1, /* ast_int_constant doesn't convert to ir_expression. */
1193 -1, /* ast_uint_constant doesn't conv to ir_expression. */
1194 -1, /* ast_float_constant doesn't conv to ir_expression. */
1195 -1, /* ast_bool_constant doesn't conv to ir_expression. */
1196 -1, /* ast_sequence doesn't convert to ir_expression. */
1198 ir_rvalue
*result
= NULL
;
1200 const struct glsl_type
*type
; /* a temporary variable for switch cases */
1201 bool error_emitted
= false;
1204 loc
= this->get_location();
1206 switch (this->oper
) {
1208 assert(!"ast_aggregate: Should never get here.");
1212 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1213 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1216 do_assignment(instructions
, state
,
1217 this->subexpressions
[0]->non_lvalue_description
,
1218 op
[0], op
[1], &result
, needs_rvalue
, false,
1219 this->subexpressions
[0]->get_location());
1224 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1226 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1228 error_emitted
= type
->is_error();
1234 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1236 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1238 error_emitted
= type
->is_error();
1240 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1248 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1249 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1251 type
= arithmetic_result_type(op
[0], op
[1],
1252 (this->oper
== ast_mul
),
1254 error_emitted
= type
->is_error();
1256 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1261 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1262 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1264 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1266 assert(operations
[this->oper
] == ir_binop_mod
);
1268 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1270 error_emitted
= type
->is_error();
1275 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1276 error_emitted
= true;
1279 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1280 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1281 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1283 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1285 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1292 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1293 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1295 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
1297 /* The relational operators must either generate an error or result
1298 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1300 assert(type
->is_error()
1301 || ((type
->base_type
== GLSL_TYPE_BOOL
)
1302 && type
->is_scalar()));
1304 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1306 error_emitted
= type
->is_error();
1311 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1312 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1314 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1316 * "The equality operators equal (==), and not equal (!=)
1317 * operate on all types. They result in a scalar Boolean. If
1318 * the operand types do not match, then there must be a
1319 * conversion from Section 4.1.10 "Implicit Conversions"
1320 * applied to one operand that can make them match, in which
1321 * case this conversion is done."
1324 if (op
[0]->type
== glsl_type::void_type
|| op
[1]->type
== glsl_type::void_type
) {
1325 _mesa_glsl_error(& loc
, state
, "`%s': wrong operand types: "
1326 "no operation `%1$s' exists that takes a left-hand "
1327 "operand of type 'void' or a right operand of type "
1328 "'void'", (this->oper
== ast_equal
) ? "==" : "!=");
1329 error_emitted
= true;
1330 } else if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
1331 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
1332 || (op
[0]->type
!= op
[1]->type
)) {
1333 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
1334 "type", (this->oper
== ast_equal
) ? "==" : "!=");
1335 error_emitted
= true;
1336 } else if ((op
[0]->type
->is_array() || op
[1]->type
->is_array()) &&
1337 !state
->check_version(120, 300, &loc
,
1338 "array comparisons forbidden")) {
1339 error_emitted
= true;
1340 } else if ((op
[0]->type
->contains_opaque() ||
1341 op
[1]->type
->contains_opaque())) {
1342 _mesa_glsl_error(&loc
, state
, "opaque type comparisons forbidden");
1343 error_emitted
= true;
1346 if (error_emitted
) {
1347 result
= new(ctx
) ir_constant(false);
1349 result
= do_comparison(ctx
, operations
[this->oper
], op
[0], op
[1]);
1350 assert(result
->type
== glsl_type::bool_type
);
1357 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1358 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1359 type
= bit_logic_result_type(op
[0]->type
, op
[1]->type
, this->oper
,
1361 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1363 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1367 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1369 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1370 error_emitted
= true;
1373 if (!op
[0]->type
->is_integer()) {
1374 _mesa_glsl_error(&loc
, state
, "operand of `~' must be an integer");
1375 error_emitted
= true;
1378 type
= error_emitted
? glsl_type::error_type
: op
[0]->type
;
1379 result
= new(ctx
) ir_expression(ir_unop_bit_not
, type
, op
[0], NULL
);
1382 case ast_logic_and
: {
1383 exec_list rhs_instructions
;
1384 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1385 "LHS", &error_emitted
);
1386 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1387 "RHS", &error_emitted
);
1389 if (rhs_instructions
.is_empty()) {
1390 result
= new(ctx
) ir_expression(ir_binop_logic_and
, op
[0], op
[1]);
1391 type
= result
->type
;
1393 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1396 instructions
->push_tail(tmp
);
1398 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1399 instructions
->push_tail(stmt
);
1401 stmt
->then_instructions
.append_list(&rhs_instructions
);
1402 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1403 ir_assignment
*const then_assign
=
1404 new(ctx
) ir_assignment(then_deref
, op
[1]);
1405 stmt
->then_instructions
.push_tail(then_assign
);
1407 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1408 ir_assignment
*const else_assign
=
1409 new(ctx
) ir_assignment(else_deref
, new(ctx
) ir_constant(false));
1410 stmt
->else_instructions
.push_tail(else_assign
);
1412 result
= new(ctx
) ir_dereference_variable(tmp
);
1418 case ast_logic_or
: {
1419 exec_list rhs_instructions
;
1420 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1421 "LHS", &error_emitted
);
1422 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1423 "RHS", &error_emitted
);
1425 if (rhs_instructions
.is_empty()) {
1426 result
= new(ctx
) ir_expression(ir_binop_logic_or
, op
[0], op
[1]);
1427 type
= result
->type
;
1429 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1432 instructions
->push_tail(tmp
);
1434 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1435 instructions
->push_tail(stmt
);
1437 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1438 ir_assignment
*const then_assign
=
1439 new(ctx
) ir_assignment(then_deref
, new(ctx
) ir_constant(true));
1440 stmt
->then_instructions
.push_tail(then_assign
);
1442 stmt
->else_instructions
.append_list(&rhs_instructions
);
1443 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1444 ir_assignment
*const else_assign
=
1445 new(ctx
) ir_assignment(else_deref
, op
[1]);
1446 stmt
->else_instructions
.push_tail(else_assign
);
1448 result
= new(ctx
) ir_dereference_variable(tmp
);
1455 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1457 * "The logical binary operators and (&&), or ( | | ), and
1458 * exclusive or (^^). They operate only on two Boolean
1459 * expressions and result in a Boolean expression."
1461 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0, "LHS",
1463 op
[1] = get_scalar_boolean_operand(instructions
, state
, this, 1, "RHS",
1466 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1471 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1472 "operand", &error_emitted
);
1474 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1478 case ast_mul_assign
:
1479 case ast_div_assign
:
1480 case ast_add_assign
:
1481 case ast_sub_assign
: {
1482 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1483 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1485 type
= arithmetic_result_type(op
[0], op
[1],
1486 (this->oper
== ast_mul_assign
),
1489 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1493 do_assignment(instructions
, state
,
1494 this->subexpressions
[0]->non_lvalue_description
,
1495 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1496 &result
, needs_rvalue
, false,
1497 this->subexpressions
[0]->get_location());
1499 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1500 * explicitly test for this because none of the binary expression
1501 * operators allow array operands either.
1507 case ast_mod_assign
: {
1508 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1509 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1511 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1513 assert(operations
[this->oper
] == ir_binop_mod
);
1515 ir_rvalue
*temp_rhs
;
1516 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1520 do_assignment(instructions
, state
,
1521 this->subexpressions
[0]->non_lvalue_description
,
1522 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1523 &result
, needs_rvalue
, false,
1524 this->subexpressions
[0]->get_location());
1529 case ast_rs_assign
: {
1530 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1531 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1532 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1534 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1535 type
, op
[0], op
[1]);
1537 do_assignment(instructions
, state
,
1538 this->subexpressions
[0]->non_lvalue_description
,
1539 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1540 &result
, needs_rvalue
, false,
1541 this->subexpressions
[0]->get_location());
1545 case ast_and_assign
:
1546 case ast_xor_assign
:
1547 case ast_or_assign
: {
1548 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1549 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1550 type
= bit_logic_result_type(op
[0]->type
, op
[1]->type
, this->oper
,
1552 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1553 type
, op
[0], op
[1]);
1555 do_assignment(instructions
, state
,
1556 this->subexpressions
[0]->non_lvalue_description
,
1557 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1558 &result
, needs_rvalue
, false,
1559 this->subexpressions
[0]->get_location());
1563 case ast_conditional
: {
1564 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1566 * "The ternary selection operator (?:). It operates on three
1567 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1568 * first expression, which must result in a scalar Boolean."
1570 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1571 "condition", &error_emitted
);
1573 /* The :? operator is implemented by generating an anonymous temporary
1574 * followed by an if-statement. The last instruction in each branch of
1575 * the if-statement assigns a value to the anonymous temporary. This
1576 * temporary is the r-value of the expression.
1578 exec_list then_instructions
;
1579 exec_list else_instructions
;
1581 op
[1] = this->subexpressions
[1]->hir(&then_instructions
, state
);
1582 op
[2] = this->subexpressions
[2]->hir(&else_instructions
, state
);
1584 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1586 * "The second and third expressions can be any type, as
1587 * long their types match, or there is a conversion in
1588 * Section 4.1.10 "Implicit Conversions" that can be applied
1589 * to one of the expressions to make their types match. This
1590 * resulting matching type is the type of the entire
1593 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1594 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1595 || (op
[1]->type
!= op
[2]->type
)) {
1596 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1598 _mesa_glsl_error(& loc
, state
, "second and third operands of ?: "
1599 "operator must have matching types");
1600 error_emitted
= true;
1601 type
= glsl_type::error_type
;
1606 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1608 * "The second and third expressions must be the same type, but can
1609 * be of any type other than an array."
1611 if (type
->is_array() &&
1612 !state
->check_version(120, 300, &loc
,
1613 "second and third operands of ?: operator "
1614 "cannot be arrays")) {
1615 error_emitted
= true;
1618 /* From section 4.1.7 of the GLSL 4.50 spec (Opaque Types):
1620 * "Except for array indexing, structure member selection, and
1621 * parentheses, opaque variables are not allowed to be operands in
1622 * expressions; such use results in a compile-time error."
1624 if (type
->contains_opaque()) {
1625 _mesa_glsl_error(&loc
, state
, "opaque variables cannot be operands "
1626 "of the ?: operator");
1627 error_emitted
= true;
1630 ir_constant
*cond_val
= op
[0]->constant_expression_value();
1632 if (then_instructions
.is_empty()
1633 && else_instructions
.is_empty()
1634 && cond_val
!= NULL
) {
1635 result
= cond_val
->value
.b
[0] ? op
[1] : op
[2];
1637 /* The copy to conditional_tmp reads the whole array. */
1638 if (type
->is_array()) {
1639 mark_whole_array_access(op
[1]);
1640 mark_whole_array_access(op
[2]);
1643 ir_variable
*const tmp
=
1644 new(ctx
) ir_variable(type
, "conditional_tmp", ir_var_temporary
);
1645 instructions
->push_tail(tmp
);
1647 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1648 instructions
->push_tail(stmt
);
1650 then_instructions
.move_nodes_to(& stmt
->then_instructions
);
1651 ir_dereference
*const then_deref
=
1652 new(ctx
) ir_dereference_variable(tmp
);
1653 ir_assignment
*const then_assign
=
1654 new(ctx
) ir_assignment(then_deref
, op
[1]);
1655 stmt
->then_instructions
.push_tail(then_assign
);
1657 else_instructions
.move_nodes_to(& stmt
->else_instructions
);
1658 ir_dereference
*const else_deref
=
1659 new(ctx
) ir_dereference_variable(tmp
);
1660 ir_assignment
*const else_assign
=
1661 new(ctx
) ir_assignment(else_deref
, op
[2]);
1662 stmt
->else_instructions
.push_tail(else_assign
);
1664 result
= new(ctx
) ir_dereference_variable(tmp
);
1671 this->non_lvalue_description
= (this->oper
== ast_pre_inc
)
1672 ? "pre-increment operation" : "pre-decrement operation";
1674 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1675 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1677 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1679 ir_rvalue
*temp_rhs
;
1680 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1684 do_assignment(instructions
, state
,
1685 this->subexpressions
[0]->non_lvalue_description
,
1686 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1687 &result
, needs_rvalue
, false,
1688 this->subexpressions
[0]->get_location());
1693 case ast_post_dec
: {
1694 this->non_lvalue_description
= (this->oper
== ast_post_inc
)
1695 ? "post-increment operation" : "post-decrement operation";
1696 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1697 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1699 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1701 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1703 ir_rvalue
*temp_rhs
;
1704 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1707 /* Get a temporary of a copy of the lvalue before it's modified.
1708 * This may get thrown away later.
1710 result
= get_lvalue_copy(instructions
, op
[0]->clone(ctx
, NULL
));
1712 ir_rvalue
*junk_rvalue
;
1714 do_assignment(instructions
, state
,
1715 this->subexpressions
[0]->non_lvalue_description
,
1716 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1717 &junk_rvalue
, false, false,
1718 this->subexpressions
[0]->get_location());
1723 case ast_field_selection
:
1724 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1727 case ast_array_index
: {
1728 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1730 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1731 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1733 result
= _mesa_ast_array_index_to_hir(ctx
, state
, op
[0], op
[1],
1736 if (result
->type
->is_error())
1737 error_emitted
= true;
1742 case ast_function_call
:
1743 /* Should *NEVER* get here. ast_function_call should always be handled
1744 * by ast_function_expression::hir.
1749 case ast_identifier
: {
1750 /* ast_identifier can appear several places in a full abstract syntax
1751 * tree. This particular use must be at location specified in the grammar
1752 * as 'variable_identifier'.
1755 state
->symbols
->get_variable(this->primary_expression
.identifier
);
1758 var
->data
.used
= true;
1759 result
= new(ctx
) ir_dereference_variable(var
);
1761 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
1762 this->primary_expression
.identifier
);
1764 result
= ir_rvalue::error_value(ctx
);
1765 error_emitted
= true;
1770 case ast_int_constant
:
1771 result
= new(ctx
) ir_constant(this->primary_expression
.int_constant
);
1774 case ast_uint_constant
:
1775 result
= new(ctx
) ir_constant(this->primary_expression
.uint_constant
);
1778 case ast_float_constant
:
1779 result
= new(ctx
) ir_constant(this->primary_expression
.float_constant
);
1782 case ast_bool_constant
:
1783 result
= new(ctx
) ir_constant(bool(this->primary_expression
.bool_constant
));
1786 case ast_double_constant
:
1787 result
= new(ctx
) ir_constant(this->primary_expression
.double_constant
);
1790 case ast_sequence
: {
1791 /* It should not be possible to generate a sequence in the AST without
1792 * any expressions in it.
1794 assert(!this->expressions
.is_empty());
1796 /* The r-value of a sequence is the last expression in the sequence. If
1797 * the other expressions in the sequence do not have side-effects (and
1798 * therefore add instructions to the instruction list), they get dropped
1801 exec_node
*previous_tail_pred
= NULL
;
1802 YYLTYPE previous_operand_loc
= loc
;
1804 foreach_list_typed (ast_node
, ast
, link
, &this->expressions
) {
1805 /* If one of the operands of comma operator does not generate any
1806 * code, we want to emit a warning. At each pass through the loop
1807 * previous_tail_pred will point to the last instruction in the
1808 * stream *before* processing the previous operand. Naturally,
1809 * instructions->tail_pred will point to the last instruction in the
1810 * stream *after* processing the previous operand. If the two
1811 * pointers match, then the previous operand had no effect.
1813 * The warning behavior here differs slightly from GCC. GCC will
1814 * only emit a warning if none of the left-hand operands have an
1815 * effect. However, it will emit a warning for each. I believe that
1816 * there are some cases in C (especially with GCC extensions) where
1817 * it is useful to have an intermediate step in a sequence have no
1818 * effect, but I don't think these cases exist in GLSL. Either way,
1819 * it would be a giant hassle to replicate that behavior.
1821 if (previous_tail_pred
== instructions
->tail_pred
) {
1822 _mesa_glsl_warning(&previous_operand_loc
, state
,
1823 "left-hand operand of comma expression has "
1827 /* tail_pred is directly accessed instead of using the get_tail()
1828 * method for performance reasons. get_tail() has extra code to
1829 * return NULL when the list is empty. We don't care about that
1830 * here, so using tail_pred directly is fine.
1832 previous_tail_pred
= instructions
->tail_pred
;
1833 previous_operand_loc
= ast
->get_location();
1835 result
= ast
->hir(instructions
, state
);
1838 /* Any errors should have already been emitted in the loop above.
1840 error_emitted
= true;
1844 type
= NULL
; /* use result->type, not type. */
1845 assert(result
!= NULL
|| !needs_rvalue
);
1847 if (result
&& result
->type
->is_error() && !error_emitted
)
1848 _mesa_glsl_error(& loc
, state
, "type mismatch");
1855 ast_expression_statement::hir(exec_list
*instructions
,
1856 struct _mesa_glsl_parse_state
*state
)
1858 /* It is possible to have expression statements that don't have an
1859 * expression. This is the solitary semicolon:
1861 * for (i = 0; i < 5; i++)
1864 * In this case the expression will be NULL. Test for NULL and don't do
1865 * anything in that case.
1867 if (expression
!= NULL
)
1868 expression
->hir_no_rvalue(instructions
, state
);
1870 /* Statements do not have r-values.
1877 ast_compound_statement::hir(exec_list
*instructions
,
1878 struct _mesa_glsl_parse_state
*state
)
1881 state
->symbols
->push_scope();
1883 foreach_list_typed (ast_node
, ast
, link
, &this->statements
)
1884 ast
->hir(instructions
, state
);
1887 state
->symbols
->pop_scope();
1889 /* Compound statements do not have r-values.
1895 * Evaluate the given exec_node (which should be an ast_node representing
1896 * a single array dimension) and return its integer value.
1899 process_array_size(exec_node
*node
,
1900 struct _mesa_glsl_parse_state
*state
)
1902 exec_list dummy_instructions
;
1904 ast_node
*array_size
= exec_node_data(ast_node
, node
, link
);
1905 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
1906 YYLTYPE loc
= array_size
->get_location();
1909 _mesa_glsl_error(& loc
, state
,
1910 "array size could not be resolved");
1914 if (!ir
->type
->is_integer()) {
1915 _mesa_glsl_error(& loc
, state
,
1916 "array size must be integer type");
1920 if (!ir
->type
->is_scalar()) {
1921 _mesa_glsl_error(& loc
, state
,
1922 "array size must be scalar type");
1926 ir_constant
*const size
= ir
->constant_expression_value();
1928 _mesa_glsl_error(& loc
, state
, "array size must be a "
1929 "constant valued expression");
1933 if (size
->value
.i
[0] <= 0) {
1934 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
1938 assert(size
->type
== ir
->type
);
1940 /* If the array size is const (and we've verified that
1941 * it is) then no instructions should have been emitted
1942 * when we converted it to HIR. If they were emitted,
1943 * then either the array size isn't const after all, or
1944 * we are emitting unnecessary instructions.
1946 assert(dummy_instructions
.is_empty());
1948 return size
->value
.u
[0];
1951 static const glsl_type
*
1952 process_array_type(YYLTYPE
*loc
, const glsl_type
*base
,
1953 ast_array_specifier
*array_specifier
,
1954 struct _mesa_glsl_parse_state
*state
)
1956 const glsl_type
*array_type
= base
;
1958 if (array_specifier
!= NULL
) {
1959 if (base
->is_array()) {
1961 /* From page 19 (page 25) of the GLSL 1.20 spec:
1963 * "Only one-dimensional arrays may be declared."
1965 if (!state
->ARB_arrays_of_arrays_enable
) {
1966 _mesa_glsl_error(loc
, state
,
1967 "invalid array of `%s'"
1968 "GL_ARB_arrays_of_arrays "
1969 "required for defining arrays of arrays",
1971 return glsl_type::error_type
;
1974 if (base
->length
== 0) {
1975 _mesa_glsl_error(loc
, state
,
1976 "only the outermost array dimension can "
1979 return glsl_type::error_type
;
1983 for (exec_node
*node
= array_specifier
->array_dimensions
.tail_pred
;
1984 !node
->is_head_sentinel(); node
= node
->prev
) {
1985 unsigned array_size
= process_array_size(node
, state
);
1986 array_type
= glsl_type::get_array_instance(array_type
, array_size
);
1989 if (array_specifier
->is_unsized_array
)
1990 array_type
= glsl_type::get_array_instance(array_type
, 0);
1998 ast_type_specifier::glsl_type(const char **name
,
1999 struct _mesa_glsl_parse_state
*state
) const
2001 const struct glsl_type
*type
;
2003 type
= state
->symbols
->get_type(this->type_name
);
2004 *name
= this->type_name
;
2006 YYLTYPE loc
= this->get_location();
2007 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
2013 ast_fully_specified_type::glsl_type(const char **name
,
2014 struct _mesa_glsl_parse_state
*state
) const
2016 const struct glsl_type
*type
= this->specifier
->glsl_type(name
, state
);
2021 if (type
->base_type
== GLSL_TYPE_FLOAT
2023 && state
->stage
== MESA_SHADER_FRAGMENT
2024 && this->qualifier
.precision
== ast_precision_none
2025 && state
->symbols
->get_variable("#default precision") == NULL
) {
2026 YYLTYPE loc
= this->get_location();
2027 _mesa_glsl_error(&loc
, state
,
2028 "no precision specified this scope for type `%s'",
2036 * Determine whether a toplevel variable declaration declares a varying. This
2037 * function operates by examining the variable's mode and the shader target,
2038 * so it correctly identifies linkage variables regardless of whether they are
2039 * declared using the deprecated "varying" syntax or the new "in/out" syntax.
2041 * Passing a non-toplevel variable declaration (e.g. a function parameter) to
2042 * this function will produce undefined results.
2045 is_varying_var(ir_variable
*var
, gl_shader_stage target
)
2048 case MESA_SHADER_VERTEX
:
2049 return var
->data
.mode
== ir_var_shader_out
;
2050 case MESA_SHADER_FRAGMENT
:
2051 return var
->data
.mode
== ir_var_shader_in
;
2053 return var
->data
.mode
== ir_var_shader_out
|| var
->data
.mode
== ir_var_shader_in
;
2059 * Matrix layout qualifiers are only allowed on certain types
2062 validate_matrix_layout_for_type(struct _mesa_glsl_parse_state
*state
,
2064 const glsl_type
*type
,
2067 if (var
&& !var
->is_in_buffer_block()) {
2068 /* Layout qualifiers may only apply to interface blocks and fields in
2071 _mesa_glsl_error(loc
, state
,
2072 "uniform block layout qualifiers row_major and "
2073 "column_major may not be applied to variables "
2074 "outside of uniform blocks");
2075 } else if (!type
->is_matrix()) {
2076 /* The OpenGL ES 3.0 conformance tests did not originally allow
2077 * matrix layout qualifiers on non-matrices. However, the OpenGL
2078 * 4.4 and OpenGL ES 3.0 (revision TBD) specifications were
2079 * amended to specifically allow these layouts on all types. Emit
2080 * a warning so that people know their code may not be portable.
2082 _mesa_glsl_warning(loc
, state
,
2083 "uniform block layout qualifiers row_major and "
2084 "column_major applied to non-matrix types may "
2085 "be rejected by older compilers");
2086 } else if (type
->is_record()) {
2087 /* We allow 'layout(row_major)' on structure types because it's the only
2088 * way to get row-major layouts on matrices contained in structures.
2090 _mesa_glsl_warning(loc
, state
,
2091 "uniform block layout qualifiers row_major and "
2092 "column_major applied to structure types is not "
2093 "strictly conformant and may be rejected by other "
2099 validate_binding_qualifier(struct _mesa_glsl_parse_state
*state
,
2101 const glsl_type
*type
,
2102 const ast_type_qualifier
*qual
)
2104 if (!qual
->flags
.q
.uniform
&& !qual
->flags
.q
.buffer
) {
2105 _mesa_glsl_error(loc
, state
,
2106 "the \"binding\" qualifier only applies to uniforms and "
2107 "shader storage buffer objects");
2111 if (qual
->binding
< 0) {
2112 _mesa_glsl_error(loc
, state
, "binding values must be >= 0");
2116 const struct gl_context
*const ctx
= state
->ctx
;
2117 unsigned elements
= type
->is_array() ? type
->length
: 1;
2118 unsigned max_index
= qual
->binding
+ elements
- 1;
2119 const glsl_type
*base_type
= type
->without_array();
2121 if (base_type
->is_interface()) {
2122 /* UBOs. From page 60 of the GLSL 4.20 specification:
2123 * "If the binding point for any uniform block instance is less than zero,
2124 * or greater than or equal to the implementation-dependent maximum
2125 * number of uniform buffer bindings, a compilation error will occur.
2126 * When the binding identifier is used with a uniform block instanced as
2127 * an array of size N, all elements of the array from binding through
2128 * binding + N – 1 must be within this range."
2130 * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
2132 if (qual
->flags
.q
.uniform
&&
2133 max_index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2134 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d UBOs exceeds "
2135 "the maximum number of UBO binding points (%d)",
2136 qual
->binding
, elements
,
2137 ctx
->Const
.MaxUniformBufferBindings
);
2141 /* SSBOs. From page 67 of the GLSL 4.30 specification:
2142 * "If the binding point for any uniform or shader storage block instance
2143 * is less than zero, or greater than or equal to the
2144 * implementation-dependent maximum number of uniform buffer bindings, a
2145 * compile-time error will occur. When the binding identifier is used
2146 * with a uniform or shader storage block instanced as an array of size
2147 * N, all elements of the array from binding through binding + N – 1 must
2148 * be within this range."
2150 if (qual
->flags
.q
.buffer
&&
2151 max_index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2152 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d SSBOs exceeds "
2153 "the maximum number of SSBO binding points (%d)",
2154 qual
->binding
, elements
,
2155 ctx
->Const
.MaxShaderStorageBufferBindings
);
2158 } else if (base_type
->is_sampler()) {
2159 /* Samplers. From page 63 of the GLSL 4.20 specification:
2160 * "If the binding is less than zero, or greater than or equal to the
2161 * implementation-dependent maximum supported number of units, a
2162 * compilation error will occur. When the binding identifier is used
2163 * with an array of size N, all elements of the array from binding
2164 * through binding + N - 1 must be within this range."
2166 unsigned limit
= ctx
->Const
.MaxCombinedTextureImageUnits
;
2168 if (max_index
>= limit
) {
2169 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d samplers "
2170 "exceeds the maximum number of texture image units "
2171 "(%d)", qual
->binding
, elements
, limit
);
2175 } else if (base_type
->contains_atomic()) {
2176 assert(ctx
->Const
.MaxAtomicBufferBindings
<= MAX_COMBINED_ATOMIC_BUFFERS
);
2177 if (unsigned(qual
->binding
) >= ctx
->Const
.MaxAtomicBufferBindings
) {
2178 _mesa_glsl_error(loc
, state
, "layout(binding = %d) exceeds the "
2179 " maximum number of atomic counter buffer bindings"
2180 "(%d)", qual
->binding
,
2181 ctx
->Const
.MaxAtomicBufferBindings
);
2185 } else if (state
->is_version(420, 310) && base_type
->is_image()) {
2186 assert(ctx
->Const
.MaxImageUnits
<= MAX_IMAGE_UNITS
);
2187 if (max_index
>= ctx
->Const
.MaxImageUnits
) {
2188 _mesa_glsl_error(loc
, state
, "Image binding %d exceeds the "
2189 " maximum number of image units (%d)", max_index
,
2190 ctx
->Const
.MaxImageUnits
);
2195 _mesa_glsl_error(loc
, state
,
2196 "the \"binding\" qualifier only applies to uniform "
2197 "blocks, opaque variables, or arrays thereof");
2205 static glsl_interp_qualifier
2206 interpret_interpolation_qualifier(const struct ast_type_qualifier
*qual
,
2207 ir_variable_mode mode
,
2208 struct _mesa_glsl_parse_state
*state
,
2211 glsl_interp_qualifier interpolation
;
2212 if (qual
->flags
.q
.flat
)
2213 interpolation
= INTERP_QUALIFIER_FLAT
;
2214 else if (qual
->flags
.q
.noperspective
)
2215 interpolation
= INTERP_QUALIFIER_NOPERSPECTIVE
;
2216 else if (qual
->flags
.q
.smooth
)
2217 interpolation
= INTERP_QUALIFIER_SMOOTH
;
2219 interpolation
= INTERP_QUALIFIER_NONE
;
2221 if (interpolation
!= INTERP_QUALIFIER_NONE
) {
2222 if (mode
!= ir_var_shader_in
&& mode
!= ir_var_shader_out
) {
2223 _mesa_glsl_error(loc
, state
,
2224 "interpolation qualifier `%s' can only be applied to "
2225 "shader inputs or outputs.",
2226 interpolation_string(interpolation
));
2230 if ((state
->stage
== MESA_SHADER_VERTEX
&& mode
== ir_var_shader_in
) ||
2231 (state
->stage
== MESA_SHADER_FRAGMENT
&& mode
== ir_var_shader_out
)) {
2232 _mesa_glsl_error(loc
, state
,
2233 "interpolation qualifier `%s' cannot be applied to "
2234 "vertex shader inputs or fragment shader outputs",
2235 interpolation_string(interpolation
));
2239 return interpolation
;
2244 validate_explicit_location(const struct ast_type_qualifier
*qual
,
2246 struct _mesa_glsl_parse_state
*state
,
2251 /* Checks for GL_ARB_explicit_uniform_location. */
2252 if (qual
->flags
.q
.uniform
) {
2253 if (!state
->check_explicit_uniform_location_allowed(loc
, var
))
2256 const struct gl_context
*const ctx
= state
->ctx
;
2257 unsigned max_loc
= qual
->location
+ var
->type
->uniform_locations() - 1;
2259 /* ARB_explicit_uniform_location specification states:
2261 * "The explicitly defined locations and the generated locations
2262 * must be in the range of 0 to MAX_UNIFORM_LOCATIONS minus one."
2264 * "Valid locations for default-block uniform variable locations
2265 * are in the range of 0 to the implementation-defined maximum
2266 * number of uniform locations."
2268 if (qual
->location
< 0) {
2269 _mesa_glsl_error(loc
, state
,
2270 "explicit location < 0 for uniform %s", var
->name
);
2274 if (max_loc
>= ctx
->Const
.MaxUserAssignableUniformLocations
) {
2275 _mesa_glsl_error(loc
, state
, "location(s) consumed by uniform %s "
2276 ">= MAX_UNIFORM_LOCATIONS (%u)", var
->name
,
2277 ctx
->Const
.MaxUserAssignableUniformLocations
);
2281 var
->data
.explicit_location
= true;
2282 var
->data
.location
= qual
->location
;
2286 /* Between GL_ARB_explicit_attrib_location an
2287 * GL_ARB_separate_shader_objects, the inputs and outputs of any shader
2288 * stage can be assigned explicit locations. The checking here associates
2289 * the correct extension with the correct stage's input / output:
2293 * vertex explicit_loc sso
2294 * tess control sso sso
2297 * fragment sso explicit_loc
2299 switch (state
->stage
) {
2300 case MESA_SHADER_VERTEX
:
2301 if (var
->data
.mode
== ir_var_shader_in
) {
2302 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
2308 if (var
->data
.mode
== ir_var_shader_out
) {
2309 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
2318 case MESA_SHADER_TESS_CTRL
:
2319 case MESA_SHADER_TESS_EVAL
:
2320 case MESA_SHADER_GEOMETRY
:
2321 if (var
->data
.mode
== ir_var_shader_in
|| var
->data
.mode
== ir_var_shader_out
) {
2322 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
2331 case MESA_SHADER_FRAGMENT
:
2332 if (var
->data
.mode
== ir_var_shader_in
) {
2333 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
2339 if (var
->data
.mode
== ir_var_shader_out
) {
2340 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
2349 case MESA_SHADER_COMPUTE
:
2350 _mesa_glsl_error(loc
, state
,
2351 "compute shader variables cannot be given "
2352 "explicit locations");
2357 _mesa_glsl_error(loc
, state
,
2358 "%s cannot be given an explicit location in %s shader",
2360 _mesa_shader_stage_to_string(state
->stage
));
2362 var
->data
.explicit_location
= true;
2364 /* This bit of silliness is needed because invalid explicit locations
2365 * are supposed to be flagged during linking. Small negative values
2366 * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
2367 * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
2368 * The linker needs to be able to differentiate these cases. This
2369 * ensures that negative values stay negative.
2371 if (qual
->location
>= 0) {
2372 switch (state
->stage
) {
2373 case MESA_SHADER_VERTEX
:
2374 var
->data
.location
= (var
->data
.mode
== ir_var_shader_in
)
2375 ? (qual
->location
+ VERT_ATTRIB_GENERIC0
)
2376 : (qual
->location
+ VARYING_SLOT_VAR0
);
2379 case MESA_SHADER_TESS_CTRL
:
2380 case MESA_SHADER_TESS_EVAL
:
2381 case MESA_SHADER_GEOMETRY
:
2382 if (var
->data
.patch
)
2383 var
->data
.location
= qual
->location
+ VARYING_SLOT_PATCH0
;
2385 var
->data
.location
= qual
->location
+ VARYING_SLOT_VAR0
;
2388 case MESA_SHADER_FRAGMENT
:
2389 var
->data
.location
= (var
->data
.mode
== ir_var_shader_out
)
2390 ? (qual
->location
+ FRAG_RESULT_DATA0
)
2391 : (qual
->location
+ VARYING_SLOT_VAR0
);
2393 case MESA_SHADER_COMPUTE
:
2394 assert(!"Unexpected shader type");
2398 var
->data
.location
= qual
->location
;
2401 if (qual
->flags
.q
.explicit_index
) {
2402 /* From the GLSL 4.30 specification, section 4.4.2 (Output
2403 * Layout Qualifiers):
2405 * "It is also a compile-time error if a fragment shader
2406 * sets a layout index to less than 0 or greater than 1."
2408 * Older specifications don't mandate a behavior; we take
2409 * this as a clarification and always generate the error.
2411 if (qual
->index
< 0 || qual
->index
> 1) {
2412 _mesa_glsl_error(loc
, state
,
2413 "explicit index may only be 0 or 1");
2415 var
->data
.explicit_index
= true;
2416 var
->data
.index
= qual
->index
;
2423 apply_image_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
2425 struct _mesa_glsl_parse_state
*state
,
2428 const glsl_type
*base_type
= var
->type
->without_array();
2430 if (base_type
->is_image()) {
2431 if (var
->data
.mode
!= ir_var_uniform
&&
2432 var
->data
.mode
!= ir_var_function_in
) {
2433 _mesa_glsl_error(loc
, state
, "image variables may only be declared as "
2434 "function parameters or uniform-qualified "
2435 "global variables");
2438 var
->data
.image_read_only
|= qual
->flags
.q
.read_only
;
2439 var
->data
.image_write_only
|= qual
->flags
.q
.write_only
;
2440 var
->data
.image_coherent
|= qual
->flags
.q
.coherent
;
2441 var
->data
.image_volatile
|= qual
->flags
.q
._volatile
;
2442 var
->data
.image_restrict
|= qual
->flags
.q
.restrict_flag
;
2443 var
->data
.read_only
= true;
2445 if (qual
->flags
.q
.explicit_image_format
) {
2446 if (var
->data
.mode
== ir_var_function_in
) {
2447 _mesa_glsl_error(loc
, state
, "format qualifiers cannot be "
2448 "used on image function parameters");
2451 if (qual
->image_base_type
!= base_type
->sampler_type
) {
2452 _mesa_glsl_error(loc
, state
, "format qualifier doesn't match the "
2453 "base data type of the image");
2456 var
->data
.image_format
= qual
->image_format
;
2458 if (var
->data
.mode
== ir_var_uniform
) {
2459 if (state
->es_shader
) {
2460 _mesa_glsl_error(loc
, state
, "all image uniforms "
2461 "must have a format layout qualifier");
2463 } else if (!qual
->flags
.q
.write_only
) {
2464 _mesa_glsl_error(loc
, state
, "image uniforms not qualified with "
2465 "`writeonly' must have a format layout "
2470 var
->data
.image_format
= GL_NONE
;
2473 /* From page 70 of the GLSL ES 3.1 specification:
2475 * "Except for image variables qualified with the format qualifiers
2476 * r32f, r32i, and r32ui, image variables must specify either memory
2477 * qualifier readonly or the memory qualifier writeonly."
2479 if (state
->es_shader
&&
2480 var
->data
.image_format
!= GL_R32F
&&
2481 var
->data
.image_format
!= GL_R32I
&&
2482 var
->data
.image_format
!= GL_R32UI
&&
2483 !var
->data
.image_read_only
&&
2484 !var
->data
.image_write_only
) {
2485 _mesa_glsl_error(loc
, state
, "image variables of format other than "
2486 "r32f, r32i or r32ui must be qualified `readonly' or "
2490 } else if (qual
->flags
.q
.read_only
||
2491 qual
->flags
.q
.write_only
||
2492 qual
->flags
.q
.coherent
||
2493 qual
->flags
.q
._volatile
||
2494 qual
->flags
.q
.restrict_flag
||
2495 qual
->flags
.q
.explicit_image_format
) {
2496 _mesa_glsl_error(loc
, state
, "memory qualifiers may only be applied to "
2501 static inline const char*
2502 get_layout_qualifier_string(bool origin_upper_left
, bool pixel_center_integer
)
2504 if (origin_upper_left
&& pixel_center_integer
)
2505 return "origin_upper_left, pixel_center_integer";
2506 else if (origin_upper_left
)
2507 return "origin_upper_left";
2508 else if (pixel_center_integer
)
2509 return "pixel_center_integer";
2515 is_conflicting_fragcoord_redeclaration(struct _mesa_glsl_parse_state
*state
,
2516 const struct ast_type_qualifier
*qual
)
2518 /* If gl_FragCoord was previously declared, and the qualifiers were
2519 * different in any way, return true.
2521 if (state
->fs_redeclares_gl_fragcoord
) {
2522 return (state
->fs_pixel_center_integer
!= qual
->flags
.q
.pixel_center_integer
2523 || state
->fs_origin_upper_left
!= qual
->flags
.q
.origin_upper_left
);
2530 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
2532 struct _mesa_glsl_parse_state
*state
,
2536 STATIC_ASSERT(sizeof(qual
->flags
.q
) <= sizeof(qual
->flags
.i
));
2538 if (qual
->flags
.q
.invariant
) {
2539 if (var
->data
.used
) {
2540 _mesa_glsl_error(loc
, state
,
2541 "variable `%s' may not be redeclared "
2542 "`invariant' after being used",
2545 var
->data
.invariant
= 1;
2549 if (qual
->flags
.q
.precise
) {
2550 if (var
->data
.used
) {
2551 _mesa_glsl_error(loc
, state
,
2552 "variable `%s' may not be redeclared "
2553 "`precise' after being used",
2556 var
->data
.precise
= 1;
2560 if (qual
->flags
.q
.subroutine
&& !qual
->flags
.q
.uniform
) {
2561 _mesa_glsl_error(loc
, state
,
2562 "`subroutine' may only be applied to uniforms, "
2563 "subroutine type declarations, or function definitions");
2566 if (qual
->flags
.q
.constant
|| qual
->flags
.q
.attribute
2567 || qual
->flags
.q
.uniform
2568 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
2569 var
->data
.read_only
= 1;
2571 if (qual
->flags
.q
.centroid
)
2572 var
->data
.centroid
= 1;
2574 if (qual
->flags
.q
.sample
)
2575 var
->data
.sample
= 1;
2577 if (state
->stage
== MESA_SHADER_GEOMETRY
&&
2578 qual
->flags
.q
.out
&& qual
->flags
.q
.stream
) {
2579 var
->data
.stream
= qual
->stream
;
2582 if (qual
->flags
.q
.patch
)
2583 var
->data
.patch
= 1;
2585 if (qual
->flags
.q
.attribute
&& state
->stage
!= MESA_SHADER_VERTEX
) {
2586 var
->type
= glsl_type::error_type
;
2587 _mesa_glsl_error(loc
, state
,
2588 "`attribute' variables may not be declared in the "
2590 _mesa_shader_stage_to_string(state
->stage
));
2593 /* Disallow layout qualifiers which may only appear on layout declarations. */
2594 if (qual
->flags
.q
.prim_type
) {
2595 _mesa_glsl_error(loc
, state
,
2596 "Primitive type may only be specified on GS input or output "
2597 "layout declaration, not on variables.");
2600 /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
2602 * "However, the const qualifier cannot be used with out or inout."
2604 * The same section of the GLSL 4.40 spec further clarifies this saying:
2606 * "The const qualifier cannot be used with out or inout, or a
2607 * compile-time error results."
2609 if (is_parameter
&& qual
->flags
.q
.constant
&& qual
->flags
.q
.out
) {
2610 _mesa_glsl_error(loc
, state
,
2611 "`const' may not be applied to `out' or `inout' "
2612 "function parameters");
2615 /* If there is no qualifier that changes the mode of the variable, leave
2616 * the setting alone.
2618 assert(var
->data
.mode
!= ir_var_temporary
);
2619 if (qual
->flags
.q
.in
&& qual
->flags
.q
.out
)
2620 var
->data
.mode
= ir_var_function_inout
;
2621 else if (qual
->flags
.q
.in
)
2622 var
->data
.mode
= is_parameter
? ir_var_function_in
: ir_var_shader_in
;
2623 else if (qual
->flags
.q
.attribute
2624 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
2625 var
->data
.mode
= ir_var_shader_in
;
2626 else if (qual
->flags
.q
.out
)
2627 var
->data
.mode
= is_parameter
? ir_var_function_out
: ir_var_shader_out
;
2628 else if (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_VERTEX
))
2629 var
->data
.mode
= ir_var_shader_out
;
2630 else if (qual
->flags
.q
.uniform
)
2631 var
->data
.mode
= ir_var_uniform
;
2632 else if (qual
->flags
.q
.buffer
)
2633 var
->data
.mode
= ir_var_shader_storage
;
2635 if (!is_parameter
&& is_varying_var(var
, state
->stage
)) {
2636 /* User-defined ins/outs are not permitted in compute shaders. */
2637 if (state
->stage
== MESA_SHADER_COMPUTE
) {
2638 _mesa_glsl_error(loc
, state
,
2639 "user-defined input and output variables are not "
2640 "permitted in compute shaders");
2643 /* This variable is being used to link data between shader stages (in
2644 * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
2645 * that is allowed for such purposes.
2647 * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
2649 * "The varying qualifier can be used only with the data types
2650 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
2653 * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
2654 * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
2656 * "Fragment inputs can only be signed and unsigned integers and
2657 * integer vectors, float, floating-point vectors, matrices, or
2658 * arrays of these. Structures cannot be input.
2660 * Similar text exists in the section on vertex shader outputs.
2662 * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
2663 * 3.00 spec allows structs as well. Varying structs are also allowed
2666 switch (var
->type
->get_scalar_type()->base_type
) {
2667 case GLSL_TYPE_FLOAT
:
2668 /* Ok in all GLSL versions */
2670 case GLSL_TYPE_UINT
:
2672 if (state
->is_version(130, 300))
2674 _mesa_glsl_error(loc
, state
,
2675 "varying variables must be of base type float in %s",
2676 state
->get_version_string());
2678 case GLSL_TYPE_STRUCT
:
2679 if (state
->is_version(150, 300))
2681 _mesa_glsl_error(loc
, state
,
2682 "varying variables may not be of type struct");
2684 case GLSL_TYPE_DOUBLE
:
2687 _mesa_glsl_error(loc
, state
, "illegal type for a varying variable");
2692 if (state
->all_invariant
&& (state
->current_function
== NULL
)) {
2693 switch (state
->stage
) {
2694 case MESA_SHADER_VERTEX
:
2695 if (var
->data
.mode
== ir_var_shader_out
)
2696 var
->data
.invariant
= true;
2698 case MESA_SHADER_TESS_CTRL
:
2699 case MESA_SHADER_TESS_EVAL
:
2700 case MESA_SHADER_GEOMETRY
:
2701 if ((var
->data
.mode
== ir_var_shader_in
)
2702 || (var
->data
.mode
== ir_var_shader_out
))
2703 var
->data
.invariant
= true;
2705 case MESA_SHADER_FRAGMENT
:
2706 if (var
->data
.mode
== ir_var_shader_in
)
2707 var
->data
.invariant
= true;
2709 case MESA_SHADER_COMPUTE
:
2710 /* Invariance isn't meaningful in compute shaders. */
2715 var
->data
.interpolation
=
2716 interpret_interpolation_qualifier(qual
, (ir_variable_mode
) var
->data
.mode
,
2719 var
->data
.pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
2720 var
->data
.origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
2721 if ((qual
->flags
.q
.origin_upper_left
|| qual
->flags
.q
.pixel_center_integer
)
2722 && (strcmp(var
->name
, "gl_FragCoord") != 0)) {
2723 const char *const qual_string
= (qual
->flags
.q
.origin_upper_left
)
2724 ? "origin_upper_left" : "pixel_center_integer";
2726 _mesa_glsl_error(loc
, state
,
2727 "layout qualifier `%s' can only be applied to "
2728 "fragment shader input `gl_FragCoord'",
2732 if (var
->name
!= NULL
&& strcmp(var
->name
, "gl_FragCoord") == 0) {
2734 /* Section 4.3.8.1, page 39 of GLSL 1.50 spec says:
2736 * "Within any shader, the first redeclarations of gl_FragCoord
2737 * must appear before any use of gl_FragCoord."
2739 * Generate a compiler error if above condition is not met by the
2742 ir_variable
*earlier
= state
->symbols
->get_variable("gl_FragCoord");
2743 if (earlier
!= NULL
&&
2744 earlier
->data
.used
&&
2745 !state
->fs_redeclares_gl_fragcoord
) {
2746 _mesa_glsl_error(loc
, state
,
2747 "gl_FragCoord used before its first redeclaration "
2748 "in fragment shader");
2751 /* Make sure all gl_FragCoord redeclarations specify the same layout
2754 if (is_conflicting_fragcoord_redeclaration(state
, qual
)) {
2755 const char *const qual_string
=
2756 get_layout_qualifier_string(qual
->flags
.q
.origin_upper_left
,
2757 qual
->flags
.q
.pixel_center_integer
);
2759 const char *const state_string
=
2760 get_layout_qualifier_string(state
->fs_origin_upper_left
,
2761 state
->fs_pixel_center_integer
);
2763 _mesa_glsl_error(loc
, state
,
2764 "gl_FragCoord redeclared with different layout "
2765 "qualifiers (%s) and (%s) ",
2769 state
->fs_origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
2770 state
->fs_pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
2771 state
->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers
=
2772 !qual
->flags
.q
.origin_upper_left
&& !qual
->flags
.q
.pixel_center_integer
;
2773 state
->fs_redeclares_gl_fragcoord
=
2774 state
->fs_origin_upper_left
||
2775 state
->fs_pixel_center_integer
||
2776 state
->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers
;
2779 if (qual
->flags
.q
.explicit_location
) {
2780 validate_explicit_location(qual
, var
, state
, loc
);
2781 } else if (qual
->flags
.q
.explicit_index
) {
2782 _mesa_glsl_error(loc
, state
, "explicit index requires explicit location");
2785 if (qual
->flags
.q
.explicit_binding
&&
2786 validate_binding_qualifier(state
, loc
, var
->type
, qual
)) {
2787 var
->data
.explicit_binding
= true;
2788 var
->data
.binding
= qual
->binding
;
2791 if (var
->type
->contains_atomic()) {
2792 if (var
->data
.mode
== ir_var_uniform
|| var
->data
.mode
== ir_var_shader_storage
) {
2793 if (var
->data
.explicit_binding
) {
2795 &state
->atomic_counter_offsets
[var
->data
.binding
];
2797 if (*offset
% ATOMIC_COUNTER_SIZE
)
2798 _mesa_glsl_error(loc
, state
,
2799 "misaligned atomic counter offset");
2801 var
->data
.atomic
.offset
= *offset
;
2802 *offset
+= var
->type
->atomic_size();
2805 _mesa_glsl_error(loc
, state
,
2806 "atomic counters require explicit binding point");
2808 } else if (var
->data
.mode
!= ir_var_function_in
) {
2809 _mesa_glsl_error(loc
, state
, "atomic counters may only be declared as "
2810 "function parameters, uniform-qualified or "
2811 "buffer-qualified global variables");
2815 /* Does the declaration use the deprecated 'attribute' or 'varying'
2818 const bool uses_deprecated_qualifier
= qual
->flags
.q
.attribute
2819 || qual
->flags
.q
.varying
;
2822 /* Validate auxiliary storage qualifiers */
2824 /* From section 4.3.4 of the GLSL 1.30 spec:
2825 * "It is an error to use centroid in in a vertex shader."
2827 * From section 4.3.4 of the GLSL ES 3.00 spec:
2828 * "It is an error to use centroid in or interpolation qualifiers in
2829 * a vertex shader input."
2832 /* Section 4.3.6 of the GLSL 1.30 specification states:
2833 * "It is an error to use centroid out in a fragment shader."
2835 * The GL_ARB_shading_language_420pack extension specification states:
2836 * "It is an error to use auxiliary storage qualifiers or interpolation
2837 * qualifiers on an output in a fragment shader."
2839 if (qual
->flags
.q
.sample
&& (!is_varying_var(var
, state
->stage
) || uses_deprecated_qualifier
)) {
2840 _mesa_glsl_error(loc
, state
,
2841 "sample qualifier may only be used on `in` or `out` "
2842 "variables between shader stages");
2844 if (qual
->flags
.q
.centroid
&& !is_varying_var(var
, state
->stage
)) {
2845 _mesa_glsl_error(loc
, state
,
2846 "centroid qualifier may only be used with `in', "
2847 "`out' or `varying' variables between shader stages");
2851 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
2852 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
2853 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
2854 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
2855 * These extensions and all following extensions that add the 'layout'
2856 * keyword have been modified to require the use of 'in' or 'out'.
2858 * The following extension do not allow the deprecated keywords:
2860 * GL_AMD_conservative_depth
2861 * GL_ARB_conservative_depth
2862 * GL_ARB_gpu_shader5
2863 * GL_ARB_separate_shader_objects
2864 * GL_ARB_tessellation_shader
2865 * GL_ARB_transform_feedback3
2866 * GL_ARB_uniform_buffer_object
2868 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
2869 * allow layout with the deprecated keywords.
2871 const bool relaxed_layout_qualifier_checking
=
2872 state
->ARB_fragment_coord_conventions_enable
;
2874 if (qual
->has_layout() && uses_deprecated_qualifier
) {
2875 if (relaxed_layout_qualifier_checking
) {
2876 _mesa_glsl_warning(loc
, state
,
2877 "`layout' qualifier may not be used with "
2878 "`attribute' or `varying'");
2880 _mesa_glsl_error(loc
, state
,
2881 "`layout' qualifier may not be used with "
2882 "`attribute' or `varying'");
2886 /* Layout qualifiers for gl_FragDepth, which are enabled by extension
2887 * AMD_conservative_depth.
2889 int depth_layout_count
= qual
->flags
.q
.depth_any
2890 + qual
->flags
.q
.depth_greater
2891 + qual
->flags
.q
.depth_less
2892 + qual
->flags
.q
.depth_unchanged
;
2893 if (depth_layout_count
> 0
2894 && !state
->AMD_conservative_depth_enable
2895 && !state
->ARB_conservative_depth_enable
) {
2896 _mesa_glsl_error(loc
, state
,
2897 "extension GL_AMD_conservative_depth or "
2898 "GL_ARB_conservative_depth must be enabled "
2899 "to use depth layout qualifiers");
2900 } else if (depth_layout_count
> 0
2901 && strcmp(var
->name
, "gl_FragDepth") != 0) {
2902 _mesa_glsl_error(loc
, state
,
2903 "depth layout qualifiers can be applied only to "
2905 } else if (depth_layout_count
> 1
2906 && strcmp(var
->name
, "gl_FragDepth") == 0) {
2907 _mesa_glsl_error(loc
, state
,
2908 "at most one depth layout qualifier can be applied to "
2911 if (qual
->flags
.q
.depth_any
)
2912 var
->data
.depth_layout
= ir_depth_layout_any
;
2913 else if (qual
->flags
.q
.depth_greater
)
2914 var
->data
.depth_layout
= ir_depth_layout_greater
;
2915 else if (qual
->flags
.q
.depth_less
)
2916 var
->data
.depth_layout
= ir_depth_layout_less
;
2917 else if (qual
->flags
.q
.depth_unchanged
)
2918 var
->data
.depth_layout
= ir_depth_layout_unchanged
;
2920 var
->data
.depth_layout
= ir_depth_layout_none
;
2922 if (qual
->flags
.q
.std140
||
2923 qual
->flags
.q
.std430
||
2924 qual
->flags
.q
.packed
||
2925 qual
->flags
.q
.shared
) {
2926 _mesa_glsl_error(loc
, state
,
2927 "uniform and shader storage block layout qualifiers "
2928 "std140, std430, packed, and shared can only be "
2929 "applied to uniform or shader storage blocks, not "
2933 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
2934 validate_matrix_layout_for_type(state
, loc
, var
->type
, var
);
2937 apply_image_qualifier_to_variable(qual
, var
, state
, loc
);
2939 /* From section 4.4.1.3 of the GLSL 4.50 specification (Fragment Shader
2942 * "Fragment shaders also allow the following layout qualifier on in only
2943 * (not with variable declarations)
2944 * layout-qualifier-id
2945 * early_fragment_tests
2948 if (qual
->flags
.q
.early_fragment_tests
) {
2949 _mesa_glsl_error(loc
, state
, "early_fragment_tests layout qualifier only "
2950 "valid in fragment shader input layout declaration.");
2955 * Get the variable that is being redeclared by this declaration
2957 * Semantic checks to verify the validity of the redeclaration are also
2958 * performed. If semantic checks fail, compilation error will be emitted via
2959 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
2962 * A pointer to an existing variable in the current scope if the declaration
2963 * is a redeclaration, \c NULL otherwise.
2965 static ir_variable
*
2966 get_variable_being_redeclared(ir_variable
*var
, YYLTYPE loc
,
2967 struct _mesa_glsl_parse_state
*state
,
2968 bool allow_all_redeclarations
)
2970 /* Check if this declaration is actually a re-declaration, either to
2971 * resize an array or add qualifiers to an existing variable.
2973 * This is allowed for variables in the current scope, or when at
2974 * global scope (for built-ins in the implicit outer scope).
2976 ir_variable
*earlier
= state
->symbols
->get_variable(var
->name
);
2977 if (earlier
== NULL
||
2978 (state
->current_function
!= NULL
&&
2979 !state
->symbols
->name_declared_this_scope(var
->name
))) {
2984 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
2986 * "It is legal to declare an array without a size and then
2987 * later re-declare the same name as an array of the same
2988 * type and specify a size."
2990 if (earlier
->type
->is_unsized_array() && var
->type
->is_array()
2991 && (var
->type
->fields
.array
== earlier
->type
->fields
.array
)) {
2992 /* FINISHME: This doesn't match the qualifiers on the two
2993 * FINISHME: declarations. It's not 100% clear whether this is
2994 * FINISHME: required or not.
2997 const unsigned size
= unsigned(var
->type
->array_size());
2998 check_builtin_array_max_size(var
->name
, size
, loc
, state
);
2999 if ((size
> 0) && (size
<= earlier
->data
.max_array_access
)) {
3000 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
3002 earlier
->data
.max_array_access
);
3005 earlier
->type
= var
->type
;
3008 } else if ((state
->ARB_fragment_coord_conventions_enable
||
3009 state
->is_version(150, 0))
3010 && strcmp(var
->name
, "gl_FragCoord") == 0
3011 && earlier
->type
== var
->type
3012 && earlier
->data
.mode
== var
->data
.mode
) {
3013 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
3016 earlier
->data
.origin_upper_left
= var
->data
.origin_upper_left
;
3017 earlier
->data
.pixel_center_integer
= var
->data
.pixel_center_integer
;
3019 /* According to section 4.3.7 of the GLSL 1.30 spec,
3020 * the following built-in varaibles can be redeclared with an
3021 * interpolation qualifier:
3024 * * gl_FrontSecondaryColor
3025 * * gl_BackSecondaryColor
3027 * * gl_SecondaryColor
3029 } else if (state
->is_version(130, 0)
3030 && (strcmp(var
->name
, "gl_FrontColor") == 0
3031 || strcmp(var
->name
, "gl_BackColor") == 0
3032 || strcmp(var
->name
, "gl_FrontSecondaryColor") == 0
3033 || strcmp(var
->name
, "gl_BackSecondaryColor") == 0
3034 || strcmp(var
->name
, "gl_Color") == 0
3035 || strcmp(var
->name
, "gl_SecondaryColor") == 0)
3036 && earlier
->type
== var
->type
3037 && earlier
->data
.mode
== var
->data
.mode
) {
3038 earlier
->data
.interpolation
= var
->data
.interpolation
;
3040 /* Layout qualifiers for gl_FragDepth. */
3041 } else if ((state
->AMD_conservative_depth_enable
||
3042 state
->ARB_conservative_depth_enable
)
3043 && strcmp(var
->name
, "gl_FragDepth") == 0
3044 && earlier
->type
== var
->type
3045 && earlier
->data
.mode
== var
->data
.mode
) {
3047 /** From the AMD_conservative_depth spec:
3048 * Within any shader, the first redeclarations of gl_FragDepth
3049 * must appear before any use of gl_FragDepth.
3051 if (earlier
->data
.used
) {
3052 _mesa_glsl_error(&loc
, state
,
3053 "the first redeclaration of gl_FragDepth "
3054 "must appear before any use of gl_FragDepth");
3057 /* Prevent inconsistent redeclaration of depth layout qualifier. */
3058 if (earlier
->data
.depth_layout
!= ir_depth_layout_none
3059 && earlier
->data
.depth_layout
!= var
->data
.depth_layout
) {
3060 _mesa_glsl_error(&loc
, state
,
3061 "gl_FragDepth: depth layout is declared here "
3062 "as '%s, but it was previously declared as "
3064 depth_layout_string(var
->data
.depth_layout
),
3065 depth_layout_string(earlier
->data
.depth_layout
));
3068 earlier
->data
.depth_layout
= var
->data
.depth_layout
;
3070 } else if (allow_all_redeclarations
) {
3071 if (earlier
->data
.mode
!= var
->data
.mode
) {
3072 _mesa_glsl_error(&loc
, state
,
3073 "redeclaration of `%s' with incorrect qualifiers",
3075 } else if (earlier
->type
!= var
->type
) {
3076 _mesa_glsl_error(&loc
, state
,
3077 "redeclaration of `%s' has incorrect type",
3081 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
3088 * Generate the IR for an initializer in a variable declaration
3091 process_initializer(ir_variable
*var
, ast_declaration
*decl
,
3092 ast_fully_specified_type
*type
,
3093 exec_list
*initializer_instructions
,
3094 struct _mesa_glsl_parse_state
*state
)
3096 ir_rvalue
*result
= NULL
;
3098 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
3100 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
3102 * "All uniform variables are read-only and are initialized either
3103 * directly by an application via API commands, or indirectly by
3106 if (var
->data
.mode
== ir_var_uniform
) {
3107 state
->check_version(120, 0, &initializer_loc
,
3108 "cannot initialize uniforms");
3111 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
3113 * "Buffer variables cannot have initializers."
3115 if (var
->data
.mode
== ir_var_shader_storage
) {
3116 _mesa_glsl_error(& initializer_loc
, state
,
3117 "SSBO variables cannot have initializers");
3120 /* From section 4.1.7 of the GLSL 4.40 spec:
3122 * "Opaque variables [...] are initialized only through the
3123 * OpenGL API; they cannot be declared with an initializer in a
3126 if (var
->type
->contains_opaque()) {
3127 _mesa_glsl_error(& initializer_loc
, state
,
3128 "cannot initialize opaque variable");
3131 if ((var
->data
.mode
== ir_var_shader_in
) && (state
->current_function
== NULL
)) {
3132 _mesa_glsl_error(& initializer_loc
, state
,
3133 "cannot initialize %s shader input / %s",
3134 _mesa_shader_stage_to_string(state
->stage
),
3135 (state
->stage
== MESA_SHADER_VERTEX
)
3136 ? "attribute" : "varying");
3139 /* If the initializer is an ast_aggregate_initializer, recursively store
3140 * type information from the LHS into it, so that its hir() function can do
3143 if (decl
->initializer
->oper
== ast_aggregate
)
3144 _mesa_ast_set_aggregate_type(var
->type
, decl
->initializer
);
3146 ir_dereference
*const lhs
= new(state
) ir_dereference_variable(var
);
3147 ir_rvalue
*rhs
= decl
->initializer
->hir(initializer_instructions
, state
);
3149 /* Calculate the constant value if this is a const or uniform
3152 if (type
->qualifier
.flags
.q
.constant
3153 || type
->qualifier
.flags
.q
.uniform
) {
3154 ir_rvalue
*new_rhs
= validate_assignment(state
, initializer_loc
,
3156 if (new_rhs
!= NULL
) {
3159 ir_constant
*constant_value
= rhs
->constant_expression_value();
3160 if (!constant_value
) {
3161 /* If ARB_shading_language_420pack is enabled, initializers of
3162 * const-qualified local variables do not have to be constant
3163 * expressions. Const-qualified global variables must still be
3164 * initialized with constant expressions.
3166 if (!state
->ARB_shading_language_420pack_enable
3167 || state
->current_function
== NULL
) {
3168 _mesa_glsl_error(& initializer_loc
, state
,
3169 "initializer of %s variable `%s' must be a "
3170 "constant expression",
3171 (type
->qualifier
.flags
.q
.constant
)
3172 ? "const" : "uniform",
3174 if (var
->type
->is_numeric()) {
3175 /* Reduce cascading errors. */
3176 var
->constant_value
= ir_constant::zero(state
, var
->type
);
3180 rhs
= constant_value
;
3181 var
->constant_value
= constant_value
;
3184 if (var
->type
->is_numeric()) {
3185 /* Reduce cascading errors. */
3186 var
->constant_value
= ir_constant::zero(state
, var
->type
);
3191 if (rhs
&& !rhs
->type
->is_error()) {
3192 bool temp
= var
->data
.read_only
;
3193 if (type
->qualifier
.flags
.q
.constant
)
3194 var
->data
.read_only
= false;
3196 /* Never emit code to initialize a uniform.
3198 const glsl_type
*initializer_type
;
3199 if (!type
->qualifier
.flags
.q
.uniform
) {
3200 do_assignment(initializer_instructions
, state
,
3205 type
->get_location());
3206 initializer_type
= result
->type
;
3208 initializer_type
= rhs
->type
;
3210 var
->constant_initializer
= rhs
->constant_expression_value();
3211 var
->data
.has_initializer
= true;
3213 /* If the declared variable is an unsized array, it must inherrit
3214 * its full type from the initializer. A declaration such as
3216 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
3220 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
3222 * The assignment generated in the if-statement (below) will also
3223 * automatically handle this case for non-uniforms.
3225 * If the declared variable is not an array, the types must
3226 * already match exactly. As a result, the type assignment
3227 * here can be done unconditionally. For non-uniforms the call
3228 * to do_assignment can change the type of the initializer (via
3229 * the implicit conversion rules). For uniforms the initializer
3230 * must be a constant expression, and the type of that expression
3231 * was validated above.
3233 var
->type
= initializer_type
;
3235 var
->data
.read_only
= temp
;
3242 validate_layout_qualifier_vertex_count(struct _mesa_glsl_parse_state
*state
,
3243 YYLTYPE loc
, ir_variable
*var
,
3244 unsigned num_vertices
,
3246 const char *var_category
)
3248 if (var
->type
->is_unsized_array()) {
3249 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says:
3251 * All geometry shader input unsized array declarations will be
3252 * sized by an earlier input layout qualifier, when present, as per
3253 * the following table.
3255 * Followed by a table mapping each allowed input layout qualifier to
3256 * the corresponding input length.
3258 * Similarly for tessellation control shader outputs.
3260 if (num_vertices
!= 0)
3261 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
3264 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec
3265 * includes the following examples of compile-time errors:
3267 * // code sequence within one shader...
3268 * in vec4 Color1[]; // size unknown
3269 * ...Color1.length()...// illegal, length() unknown
3270 * in vec4 Color2[2]; // size is 2
3271 * ...Color1.length()...// illegal, Color1 still has no size
3272 * in vec4 Color3[3]; // illegal, input sizes are inconsistent
3273 * layout(lines) in; // legal, input size is 2, matching
3274 * in vec4 Color4[3]; // illegal, contradicts layout
3277 * To detect the case illustrated by Color3, we verify that the size of
3278 * an explicitly-sized array matches the size of any previously declared
3279 * explicitly-sized array. To detect the case illustrated by Color4, we
3280 * verify that the size of an explicitly-sized array is consistent with
3281 * any previously declared input layout.
3283 if (num_vertices
!= 0 && var
->type
->length
!= num_vertices
) {
3284 _mesa_glsl_error(&loc
, state
,
3285 "%s size contradicts previously declared layout "
3286 "(size is %u, but layout requires a size of %u)",
3287 var_category
, var
->type
->length
, num_vertices
);
3288 } else if (*size
!= 0 && var
->type
->length
!= *size
) {
3289 _mesa_glsl_error(&loc
, state
,
3290 "%s sizes are inconsistent (size is %u, but a "
3291 "previous declaration has size %u)",
3292 var_category
, var
->type
->length
, *size
);
3294 *size
= var
->type
->length
;
3300 handle_tess_ctrl_shader_output_decl(struct _mesa_glsl_parse_state
*state
,
3301 YYLTYPE loc
, ir_variable
*var
)
3303 unsigned num_vertices
= 0;
3305 if (state
->tcs_output_vertices_specified
) {
3306 num_vertices
= state
->out_qualifier
->vertices
;
3309 if (!var
->type
->is_array() && !var
->data
.patch
) {
3310 _mesa_glsl_error(&loc
, state
,
3311 "tessellation control shader outputs must be arrays");
3313 /* To avoid cascading failures, short circuit the checks below. */
3317 if (var
->data
.patch
)
3320 validate_layout_qualifier_vertex_count(state
, loc
, var
, num_vertices
,
3321 &state
->tcs_output_size
,
3322 "tessellation control shader output");
3326 * Do additional processing necessary for tessellation control/evaluation shader
3327 * input declarations. This covers both interface block arrays and bare input
3331 handle_tess_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
3332 YYLTYPE loc
, ir_variable
*var
)
3334 if (!var
->type
->is_array() && !var
->data
.patch
) {
3335 _mesa_glsl_error(&loc
, state
,
3336 "per-vertex tessellation shader inputs must be arrays");
3337 /* Avoid cascading failures. */
3341 if (var
->data
.patch
)
3344 /* Unsized arrays are implicitly sized to gl_MaxPatchVertices. */
3345 if (var
->type
->is_unsized_array()) {
3346 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
3347 state
->Const
.MaxPatchVertices
);
3353 * Do additional processing necessary for geometry shader input declarations
3354 * (this covers both interface blocks arrays and bare input variables).
3357 handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
3358 YYLTYPE loc
, ir_variable
*var
)
3360 unsigned num_vertices
= 0;
3362 if (state
->gs_input_prim_type_specified
) {
3363 num_vertices
= vertices_per_prim(state
->in_qualifier
->prim_type
);
3366 /* Geometry shader input variables must be arrays. Caller should have
3367 * reported an error for this.
3369 if (!var
->type
->is_array()) {
3370 assert(state
->error
);
3372 /* To avoid cascading failures, short circuit the checks below. */
3376 validate_layout_qualifier_vertex_count(state
, loc
, var
, num_vertices
,
3377 &state
->gs_input_size
,
3378 "geometry shader input");
3382 validate_identifier(const char *identifier
, YYLTYPE loc
,
3383 struct _mesa_glsl_parse_state
*state
)
3385 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
3387 * "Identifiers starting with "gl_" are reserved for use by
3388 * OpenGL, and may not be declared in a shader as either a
3389 * variable or a function."
3391 if (is_gl_identifier(identifier
)) {
3392 _mesa_glsl_error(&loc
, state
,
3393 "identifier `%s' uses reserved `gl_' prefix",
3395 } else if (strstr(identifier
, "__")) {
3396 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
3399 * "In addition, all identifiers containing two
3400 * consecutive underscores (__) are reserved as
3401 * possible future keywords."
3403 * The intention is that names containing __ are reserved for internal
3404 * use by the implementation, and names prefixed with GL_ are reserved
3405 * for use by Khronos. Names simply containing __ are dangerous to use,
3406 * but should be allowed.
3408 * A future version of the GLSL specification will clarify this.
3410 _mesa_glsl_warning(&loc
, state
,
3411 "identifier `%s' uses reserved `__' string",
3417 precision_qualifier_allowed(const glsl_type
*type
)
3419 /* Precision qualifiers apply to floating point, integer and opaque
3422 * Section 4.5.2 (Precision Qualifiers) of the GLSL 1.30 spec says:
3423 * "Any floating point or any integer declaration can have the type
3424 * preceded by one of these precision qualifiers [...] Literal
3425 * constants do not have precision qualifiers. Neither do Boolean
3428 * Section 4.5 (Precision and Precision Qualifiers) of the GLSL 1.30
3431 * "Precision qualifiers are added for code portability with OpenGL
3432 * ES, not for functionality. They have the same syntax as in OpenGL
3435 * Section 8 (Built-In Functions) of the GLSL ES 1.00 spec says:
3437 * "uniform lowp sampler2D sampler;
3440 * lowp vec4 col = texture2D (sampler, coord);
3441 * // texture2D returns lowp"
3443 * From this, we infer that GLSL 1.30 (and later) should allow precision
3444 * qualifiers on sampler types just like float and integer types.
3446 return type
->is_float()
3447 || type
->is_integer()
3448 || type
->is_record()
3449 || type
->contains_opaque();
3453 ast_declarator_list::hir(exec_list
*instructions
,
3454 struct _mesa_glsl_parse_state
*state
)
3457 const struct glsl_type
*decl_type
;
3458 const char *type_name
= NULL
;
3459 ir_rvalue
*result
= NULL
;
3460 YYLTYPE loc
= this->get_location();
3462 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
3464 * "To ensure that a particular output variable is invariant, it is
3465 * necessary to use the invariant qualifier. It can either be used to
3466 * qualify a previously declared variable as being invariant
3468 * invariant gl_Position; // make existing gl_Position be invariant"
3470 * In these cases the parser will set the 'invariant' flag in the declarator
3471 * list, and the type will be NULL.
3473 if (this->invariant
) {
3474 assert(this->type
== NULL
);
3476 if (state
->current_function
!= NULL
) {
3477 _mesa_glsl_error(& loc
, state
,
3478 "all uses of `invariant' keyword must be at global "
3482 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
3483 assert(decl
->array_specifier
== NULL
);
3484 assert(decl
->initializer
== NULL
);
3486 ir_variable
*const earlier
=
3487 state
->symbols
->get_variable(decl
->identifier
);
3488 if (earlier
== NULL
) {
3489 _mesa_glsl_error(& loc
, state
,
3490 "undeclared variable `%s' cannot be marked "
3491 "invariant", decl
->identifier
);
3492 } else if (!is_varying_var(earlier
, state
->stage
)) {
3493 _mesa_glsl_error(&loc
, state
,
3494 "`%s' cannot be marked invariant; interfaces between "
3495 "shader stages only.", decl
->identifier
);
3496 } else if (earlier
->data
.used
) {
3497 _mesa_glsl_error(& loc
, state
,
3498 "variable `%s' may not be redeclared "
3499 "`invariant' after being used",
3502 earlier
->data
.invariant
= true;
3506 /* Invariant redeclarations do not have r-values.
3511 if (this->precise
) {
3512 assert(this->type
== NULL
);
3514 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
3515 assert(decl
->array_specifier
== NULL
);
3516 assert(decl
->initializer
== NULL
);
3518 ir_variable
*const earlier
=
3519 state
->symbols
->get_variable(decl
->identifier
);
3520 if (earlier
== NULL
) {
3521 _mesa_glsl_error(& loc
, state
,
3522 "undeclared variable `%s' cannot be marked "
3523 "precise", decl
->identifier
);
3524 } else if (state
->current_function
!= NULL
&&
3525 !state
->symbols
->name_declared_this_scope(decl
->identifier
)) {
3526 /* Note: we have to check if we're in a function, since
3527 * builtins are treated as having come from another scope.
3529 _mesa_glsl_error(& loc
, state
,
3530 "variable `%s' from an outer scope may not be "
3531 "redeclared `precise' in this scope",
3533 } else if (earlier
->data
.used
) {
3534 _mesa_glsl_error(& loc
, state
,
3535 "variable `%s' may not be redeclared "
3536 "`precise' after being used",
3539 earlier
->data
.precise
= true;
3543 /* Precise redeclarations do not have r-values either. */
3547 assert(this->type
!= NULL
);
3548 assert(!this->invariant
);
3549 assert(!this->precise
);
3551 /* The type specifier may contain a structure definition. Process that
3552 * before any of the variable declarations.
3554 (void) this->type
->specifier
->hir(instructions
, state
);
3556 decl_type
= this->type
->glsl_type(& type_name
, state
);
3558 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
3559 * "Buffer variables may only be declared inside interface blocks
3560 * (section 4.3.9 “Interface Blocks”), which are then referred to as
3561 * shader storage blocks. It is a compile-time error to declare buffer
3562 * variables at global scope (outside a block)."
3564 if (type
->qualifier
.flags
.q
.buffer
&& !decl_type
->is_interface()) {
3565 _mesa_glsl_error(&loc
, state
,
3566 "buffer variables cannot be declared outside "
3567 "interface blocks");
3570 /* An offset-qualified atomic counter declaration sets the default
3571 * offset for the next declaration within the same atomic counter
3574 if (decl_type
&& decl_type
->contains_atomic()) {
3575 if (type
->qualifier
.flags
.q
.explicit_binding
&&
3576 type
->qualifier
.flags
.q
.explicit_offset
)
3577 state
->atomic_counter_offsets
[type
->qualifier
.binding
] =
3578 type
->qualifier
.offset
;
3581 if (this->declarations
.is_empty()) {
3582 /* If there is no structure involved in the program text, there are two
3583 * possible scenarios:
3585 * - The program text contained something like 'vec4;'. This is an
3586 * empty declaration. It is valid but weird. Emit a warning.
3588 * - The program text contained something like 'S;' and 'S' is not the
3589 * name of a known structure type. This is both invalid and weird.
3592 * - The program text contained something like 'mediump float;'
3593 * when the programmer probably meant 'precision mediump
3594 * float;' Emit a warning with a description of what they
3595 * probably meant to do.
3597 * Note that if decl_type is NULL and there is a structure involved,
3598 * there must have been some sort of error with the structure. In this
3599 * case we assume that an error was already generated on this line of
3600 * code for the structure. There is no need to generate an additional,
3603 assert(this->type
->specifier
->structure
== NULL
|| decl_type
!= NULL
3606 if (decl_type
== NULL
) {
3607 _mesa_glsl_error(&loc
, state
,
3608 "invalid type `%s' in empty declaration",
3610 } else if (decl_type
->base_type
== GLSL_TYPE_ATOMIC_UINT
) {
3611 /* Empty atomic counter declarations are allowed and useful
3612 * to set the default offset qualifier.
3615 } else if (this->type
->qualifier
.precision
!= ast_precision_none
) {
3616 if (this->type
->specifier
->structure
!= NULL
) {
3617 _mesa_glsl_error(&loc
, state
,
3618 "precision qualifiers can't be applied "
3621 static const char *const precision_names
[] = {
3628 _mesa_glsl_warning(&loc
, state
,
3629 "empty declaration with precision qualifier, "
3630 "to set the default precision, use "
3631 "`precision %s %s;'",
3632 precision_names
[this->type
->qualifier
.precision
],
3635 } else if (this->type
->specifier
->structure
== NULL
) {
3636 _mesa_glsl_warning(&loc
, state
, "empty declaration");
3640 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
3641 const struct glsl_type
*var_type
;
3643 const char *identifier
= decl
->identifier
;
3644 /* FINISHME: Emit a warning if a variable declaration shadows a
3645 * FINISHME: declaration at a higher scope.
3648 if ((decl_type
== NULL
) || decl_type
->is_void()) {
3649 if (type_name
!= NULL
) {
3650 _mesa_glsl_error(& loc
, state
,
3651 "invalid type `%s' in declaration of `%s'",
3652 type_name
, decl
->identifier
);
3654 _mesa_glsl_error(& loc
, state
,
3655 "invalid type in declaration of `%s'",
3661 if (this->type
->qualifier
.flags
.q
.subroutine
) {
3665 t
= state
->symbols
->get_type(this->type
->specifier
->type_name
);
3667 _mesa_glsl_error(& loc
, state
,
3668 "invalid type in declaration of `%s'",
3670 name
= ralloc_asprintf(ctx
, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state
->stage
), decl
->identifier
);
3675 var_type
= process_array_type(&loc
, decl_type
, decl
->array_specifier
,
3678 var
= new(ctx
) ir_variable(var_type
, identifier
, ir_var_auto
);
3680 /* The 'varying in' and 'varying out' qualifiers can only be used with
3681 * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
3684 if (this->type
->qualifier
.flags
.q
.varying
) {
3685 if (this->type
->qualifier
.flags
.q
.in
) {
3686 _mesa_glsl_error(& loc
, state
,
3687 "`varying in' qualifier in declaration of "
3688 "`%s' only valid for geometry shaders using "
3689 "ARB_geometry_shader4 or EXT_geometry_shader4",
3691 } else if (this->type
->qualifier
.flags
.q
.out
) {
3692 _mesa_glsl_error(& loc
, state
,
3693 "`varying out' qualifier in declaration of "
3694 "`%s' only valid for geometry shaders using "
3695 "ARB_geometry_shader4 or EXT_geometry_shader4",
3700 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
3702 * "Global variables can only use the qualifiers const,
3703 * attribute, uniform, or varying. Only one may be
3706 * Local variables can only use the qualifier const."
3708 * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
3709 * any extension that adds the 'layout' keyword.
3711 if (!state
->is_version(130, 300)
3712 && !state
->has_explicit_attrib_location()
3713 && !state
->has_separate_shader_objects()
3714 && !state
->ARB_fragment_coord_conventions_enable
) {
3715 if (this->type
->qualifier
.flags
.q
.out
) {
3716 _mesa_glsl_error(& loc
, state
,
3717 "`out' qualifier in declaration of `%s' "
3718 "only valid for function parameters in %s",
3719 decl
->identifier
, state
->get_version_string());
3721 if (this->type
->qualifier
.flags
.q
.in
) {
3722 _mesa_glsl_error(& loc
, state
,
3723 "`in' qualifier in declaration of `%s' "
3724 "only valid for function parameters in %s",
3725 decl
->identifier
, state
->get_version_string());
3727 /* FINISHME: Test for other invalid qualifiers. */
3730 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
3733 if (this->type
->qualifier
.flags
.q
.invariant
) {
3734 if (!is_varying_var(var
, state
->stage
)) {
3735 _mesa_glsl_error(&loc
, state
,
3736 "`%s' cannot be marked invariant; interfaces between "
3737 "shader stages only", var
->name
);
3741 if (state
->current_function
!= NULL
) {
3742 const char *mode
= NULL
;
3743 const char *extra
= "";
3745 /* There is no need to check for 'inout' here because the parser will
3746 * only allow that in function parameter lists.
3748 if (this->type
->qualifier
.flags
.q
.attribute
) {
3750 } else if (this->type
->qualifier
.flags
.q
.subroutine
) {
3751 mode
= "subroutine uniform";
3752 } else if (this->type
->qualifier
.flags
.q
.uniform
) {
3754 } else if (this->type
->qualifier
.flags
.q
.varying
) {
3756 } else if (this->type
->qualifier
.flags
.q
.in
) {
3758 extra
= " or in function parameter list";
3759 } else if (this->type
->qualifier
.flags
.q
.out
) {
3761 extra
= " or in function parameter list";
3765 _mesa_glsl_error(& loc
, state
,
3766 "%s variable `%s' must be declared at "
3768 mode
, var
->name
, extra
);
3770 } else if (var
->data
.mode
== ir_var_shader_in
) {
3771 var
->data
.read_only
= true;
3773 if (state
->stage
== MESA_SHADER_VERTEX
) {
3774 bool error_emitted
= false;
3776 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
3778 * "Vertex shader inputs can only be float, floating-point
3779 * vectors, matrices, signed and unsigned integers and integer
3780 * vectors. Vertex shader inputs can also form arrays of these
3781 * types, but not structures."
3783 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
3785 * "Vertex shader inputs can only be float, floating-point
3786 * vectors, matrices, signed and unsigned integers and integer
3787 * vectors. They cannot be arrays or structures."
3789 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
3791 * "The attribute qualifier can be used only with float,
3792 * floating-point vectors, and matrices. Attribute variables
3793 * cannot be declared as arrays or structures."
3795 * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
3797 * "Vertex shader inputs can only be float, floating-point
3798 * vectors, matrices, signed and unsigned integers and integer
3799 * vectors. Vertex shader inputs cannot be arrays or
3802 const glsl_type
*check_type
= var
->type
->without_array();
3804 switch (check_type
->base_type
) {
3805 case GLSL_TYPE_FLOAT
:
3807 case GLSL_TYPE_UINT
:
3809 if (state
->is_version(120, 300))
3811 case GLSL_TYPE_DOUBLE
:
3812 if (check_type
->base_type
== GLSL_TYPE_DOUBLE
&& (state
->is_version(410, 0) || state
->ARB_vertex_attrib_64bit_enable
))
3816 _mesa_glsl_error(& loc
, state
,
3817 "vertex shader input / attribute cannot have "
3819 var
->type
->is_array() ? "array of " : "",
3821 error_emitted
= true;
3824 if (!error_emitted
&& var
->type
->is_array() &&
3825 !state
->check_version(150, 0, &loc
,
3826 "vertex shader input / attribute "
3827 "cannot have array type")) {
3828 error_emitted
= true;
3830 } else if (state
->stage
== MESA_SHADER_GEOMETRY
) {
3831 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
3833 * Geometry shader input variables get the per-vertex values
3834 * written out by vertex shader output variables of the same
3835 * names. Since a geometry shader operates on a set of
3836 * vertices, each input varying variable (or input block, see
3837 * interface blocks below) needs to be declared as an array.
3839 if (!var
->type
->is_array()) {
3840 _mesa_glsl_error(&loc
, state
,
3841 "geometry shader inputs must be arrays");
3844 handle_geometry_shader_input_decl(state
, loc
, var
);
3845 } else if (state
->stage
== MESA_SHADER_FRAGMENT
) {
3846 /* From section 4.3.4 (Input Variables) of the GLSL ES 3.10 spec:
3848 * It is a compile-time error to declare a fragment shader
3849 * input with, or that contains, any of the following types:
3853 * * An array of arrays
3854 * * An array of structures
3855 * * A structure containing an array
3856 * * A structure containing a structure
3858 if (state
->es_shader
) {
3859 const glsl_type
*check_type
= var
->type
->without_array();
3860 if (check_type
->is_boolean() ||
3861 check_type
->contains_opaque()) {
3862 _mesa_glsl_error(&loc
, state
,
3863 "fragment shader input cannot have type %s",
3866 if (var
->type
->is_array() &&
3867 var
->type
->fields
.array
->is_array()) {
3868 _mesa_glsl_error(&loc
, state
,
3870 "cannot have an array of arrays",
3871 _mesa_shader_stage_to_string(state
->stage
));
3873 if (var
->type
->is_array() &&
3874 var
->type
->fields
.array
->is_record()) {
3875 _mesa_glsl_error(&loc
, state
,
3876 "fragment shader input "
3877 "cannot have an array of structs");
3879 if (var
->type
->is_record()) {
3880 for (unsigned i
= 0; i
< var
->type
->length
; i
++) {
3881 if (var
->type
->fields
.structure
[i
].type
->is_array() ||
3882 var
->type
->fields
.structure
[i
].type
->is_record())
3883 _mesa_glsl_error(&loc
, state
,
3884 "fragement shader input cannot have "
3885 "a struct that contains an "
3890 } else if (state
->stage
== MESA_SHADER_TESS_CTRL
||
3891 state
->stage
== MESA_SHADER_TESS_EVAL
) {
3892 handle_tess_shader_input_decl(state
, loc
, var
);
3894 } else if (var
->data
.mode
== ir_var_shader_out
) {
3895 const glsl_type
*check_type
= var
->type
->without_array();
3897 /* From section 4.3.6 (Output variables) of the GLSL 4.40 spec:
3899 * It is a compile-time error to declare a vertex, tessellation
3900 * evaluation, tessellation control, or geometry shader output
3901 * that contains any of the following:
3903 * * A Boolean type (bool, bvec2 ...)
3906 if (check_type
->is_boolean() || check_type
->contains_opaque())
3907 _mesa_glsl_error(&loc
, state
,
3908 "%s shader output cannot have type %s",
3909 _mesa_shader_stage_to_string(state
->stage
),
3912 /* From section 4.3.6 (Output variables) of the GLSL 4.40 spec:
3914 * It is a compile-time error to declare a fragment shader output
3915 * that contains any of the following:
3917 * * A Boolean type (bool, bvec2 ...)
3918 * * A double-precision scalar or vector (double, dvec2 ...)
3923 if (state
->stage
== MESA_SHADER_FRAGMENT
) {
3924 if (check_type
->is_record() || check_type
->is_matrix())
3925 _mesa_glsl_error(&loc
, state
,
3926 "fragment shader output "
3927 "cannot have struct or matrix type");
3928 switch (check_type
->base_type
) {
3929 case GLSL_TYPE_UINT
:
3931 case GLSL_TYPE_FLOAT
:
3934 _mesa_glsl_error(&loc
, state
,
3935 "fragment shader output cannot have "
3936 "type %s", check_type
->name
);
3940 /* From section 4.3.6 (Output Variables) of the GLSL ES 3.10 spec:
3942 * It is a compile-time error to declare a vertex shader output
3943 * with, or that contains, any of the following types:
3947 * * An array of arrays
3948 * * An array of structures
3949 * * A structure containing an array
3950 * * A structure containing a structure
3952 * It is a compile-time error to declare a fragment shader output
3953 * with, or that contains, any of the following types:
3959 * * An array of array
3961 if (state
->es_shader
) {
3962 if (var
->type
->is_array() &&
3963 var
->type
->fields
.array
->is_array()) {
3964 _mesa_glsl_error(&loc
, state
,
3966 "cannot have an array of arrays",
3967 _mesa_shader_stage_to_string(state
->stage
));
3969 if (state
->stage
== MESA_SHADER_VERTEX
) {
3970 if (var
->type
->is_array() &&
3971 var
->type
->fields
.array
->is_record()) {
3972 _mesa_glsl_error(&loc
, state
,
3973 "vertex shader output "
3974 "cannot have an array of structs");
3976 if (var
->type
->is_record()) {
3977 for (unsigned i
= 0; i
< var
->type
->length
; i
++) {
3978 if (var
->type
->fields
.structure
[i
].type
->is_array() ||
3979 var
->type
->fields
.structure
[i
].type
->is_record())
3980 _mesa_glsl_error(&loc
, state
,
3981 "vertex shader output cannot have a "
3982 "struct that contains an "
3989 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
3990 handle_tess_ctrl_shader_output_decl(state
, loc
, var
);
3992 } else if (var
->type
->contains_subroutine()) {
3993 /* declare subroutine uniforms as hidden */
3994 var
->data
.how_declared
= ir_var_hidden
;
3997 /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
3998 * so must integer vertex outputs.
4000 * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
4001 * "Fragment shader inputs that are signed or unsigned integers or
4002 * integer vectors must be qualified with the interpolation qualifier
4005 * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
4006 * "Fragment shader inputs that are, or contain, signed or unsigned
4007 * integers or integer vectors must be qualified with the
4008 * interpolation qualifier flat."
4010 * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
4011 * "Vertex shader outputs that are, or contain, signed or unsigned
4012 * integers or integer vectors must be qualified with the
4013 * interpolation qualifier flat."
4015 * Note that prior to GLSL 1.50, this requirement applied to vertex
4016 * outputs rather than fragment inputs. That creates problems in the
4017 * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
4018 * desktop GL shaders. For GLSL ES shaders, we follow the spec and
4019 * apply the restriction to both vertex outputs and fragment inputs.
4021 * Note also that the desktop GLSL specs are missing the text "or
4022 * contain"; this is presumably an oversight, since there is no
4023 * reasonable way to interpolate a fragment shader input that contains
4026 if (state
->is_version(130, 300) &&
4027 var
->type
->contains_integer() &&
4028 var
->data
.interpolation
!= INTERP_QUALIFIER_FLAT
&&
4029 ((state
->stage
== MESA_SHADER_FRAGMENT
&& var
->data
.mode
== ir_var_shader_in
)
4030 || (state
->stage
== MESA_SHADER_VERTEX
&& var
->data
.mode
== ir_var_shader_out
4031 && state
->es_shader
))) {
4032 const char *var_type
= (state
->stage
== MESA_SHADER_VERTEX
) ?
4033 "vertex output" : "fragment input";
4034 _mesa_glsl_error(&loc
, state
, "if a %s is (or contains) "
4035 "an integer, then it must be qualified with 'flat'",
4039 /* Double fragment inputs must be qualified with 'flat'. */
4040 if (var
->type
->contains_double() &&
4041 var
->data
.interpolation
!= INTERP_QUALIFIER_FLAT
&&
4042 state
->stage
== MESA_SHADER_FRAGMENT
&&
4043 var
->data
.mode
== ir_var_shader_in
) {
4044 _mesa_glsl_error(&loc
, state
, "if a fragment input is (or contains) "
4045 "a double, then it must be qualified with 'flat'",
4049 /* Interpolation qualifiers cannot be applied to 'centroid' and
4050 * 'centroid varying'.
4052 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
4053 * "interpolation qualifiers may only precede the qualifiers in,
4054 * centroid in, out, or centroid out in a declaration. They do not apply
4055 * to the deprecated storage qualifiers varying or centroid varying."
4057 * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
4059 if (state
->is_version(130, 0)
4060 && this->type
->qualifier
.has_interpolation()
4061 && this->type
->qualifier
.flags
.q
.varying
) {
4063 const char *i
= this->type
->qualifier
.interpolation_string();
4066 if (this->type
->qualifier
.flags
.q
.centroid
)
4067 s
= "centroid varying";
4071 _mesa_glsl_error(&loc
, state
,
4072 "qualifier '%s' cannot be applied to the "
4073 "deprecated storage qualifier '%s'", i
, s
);
4077 /* Interpolation qualifiers can only apply to vertex shader outputs and
4078 * fragment shader inputs.
4080 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
4081 * "Outputs from a vertex shader (out) and inputs to a fragment
4082 * shader (in) can be further qualified with one or more of these
4083 * interpolation qualifiers"
4085 * From page 31 (page 37 of the PDF) of the GLSL ES 3.00 spec:
4086 * "These interpolation qualifiers may only precede the qualifiers
4087 * in, centroid in, out, or centroid out in a declaration. They do
4088 * not apply to inputs into a vertex shader or outputs from a
4091 if (state
->is_version(130, 300)
4092 && this->type
->qualifier
.has_interpolation()) {
4094 const char *i
= this->type
->qualifier
.interpolation_string();
4097 switch (state
->stage
) {
4098 case MESA_SHADER_VERTEX
:
4099 if (this->type
->qualifier
.flags
.q
.in
) {
4100 _mesa_glsl_error(&loc
, state
,
4101 "qualifier '%s' cannot be applied to vertex "
4102 "shader inputs", i
);
4105 case MESA_SHADER_FRAGMENT
:
4106 if (this->type
->qualifier
.flags
.q
.out
) {
4107 _mesa_glsl_error(&loc
, state
,
4108 "qualifier '%s' cannot be applied to fragment "
4109 "shader outputs", i
);
4118 /* From section 4.3.4 of the GLSL 4.00 spec:
4119 * "Input variables may not be declared using the patch in qualifier
4120 * in tessellation control or geometry shaders."
4122 * From section 4.3.6 of the GLSL 4.00 spec:
4123 * "It is an error to use patch out in a vertex, tessellation
4124 * evaluation, or geometry shader."
4126 * This doesn't explicitly forbid using them in a fragment shader, but
4127 * that's probably just an oversight.
4129 if (state
->stage
!= MESA_SHADER_TESS_EVAL
4130 && this->type
->qualifier
.flags
.q
.patch
4131 && this->type
->qualifier
.flags
.q
.in
) {
4133 _mesa_glsl_error(&loc
, state
, "'patch in' can only be used in a "
4134 "tessellation evaluation shader");
4137 if (state
->stage
!= MESA_SHADER_TESS_CTRL
4138 && this->type
->qualifier
.flags
.q
.patch
4139 && this->type
->qualifier
.flags
.q
.out
) {
4141 _mesa_glsl_error(&loc
, state
, "'patch out' can only be used in a "
4142 "tessellation control shader");
4145 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
4147 if (this->type
->qualifier
.precision
!= ast_precision_none
) {
4148 state
->check_precision_qualifiers_allowed(&loc
);
4152 /* If a precision qualifier is allowed on a type, it is allowed on
4153 * an array of that type.
4155 if (!(this->type
->qualifier
.precision
== ast_precision_none
4156 || precision_qualifier_allowed(var
->type
->without_array()))) {
4158 _mesa_glsl_error(&loc
, state
,
4159 "precision qualifiers apply only to floating point"
4160 ", integer and opaque types");
4163 /* From section 4.1.7 of the GLSL 4.40 spec:
4165 * "[Opaque types] can only be declared as function
4166 * parameters or uniform-qualified variables."
4168 if (var_type
->contains_opaque() &&
4169 !this->type
->qualifier
.flags
.q
.uniform
) {
4170 _mesa_glsl_error(&loc
, state
,
4171 "opaque variables must be declared uniform");
4174 /* Process the initializer and add its instructions to a temporary
4175 * list. This list will be added to the instruction stream (below) after
4176 * the declaration is added. This is done because in some cases (such as
4177 * redeclarations) the declaration may not actually be added to the
4178 * instruction stream.
4180 exec_list initializer_instructions
;
4182 /* Examine var name here since var may get deleted in the next call */
4183 bool var_is_gl_id
= is_gl_identifier(var
->name
);
4185 ir_variable
*earlier
=
4186 get_variable_being_redeclared(var
, decl
->get_location(), state
,
4187 false /* allow_all_redeclarations */);
4188 if (earlier
!= NULL
) {
4190 earlier
->data
.how_declared
== ir_var_declared_in_block
) {
4191 _mesa_glsl_error(&loc
, state
,
4192 "`%s' has already been redeclared using "
4193 "gl_PerVertex", earlier
->name
);
4195 earlier
->data
.how_declared
= ir_var_declared_normally
;
4198 if (decl
->initializer
!= NULL
) {
4199 result
= process_initializer((earlier
== NULL
) ? var
: earlier
,
4201 &initializer_instructions
, state
);
4204 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
4206 * "It is an error to write to a const variable outside of
4207 * its declaration, so they must be initialized when
4210 if (this->type
->qualifier
.flags
.q
.constant
&& decl
->initializer
== NULL
) {
4211 _mesa_glsl_error(& loc
, state
,
4212 "const declaration of `%s' must be initialized",
4216 if (state
->es_shader
) {
4217 const glsl_type
*const t
= (earlier
== NULL
)
4218 ? var
->type
: earlier
->type
;
4220 if (t
->is_unsized_array())
4221 /* Section 10.17 of the GLSL ES 1.00 specification states that
4222 * unsized array declarations have been removed from the language.
4223 * Arrays that are sized using an initializer are still explicitly
4224 * sized. However, GLSL ES 1.00 does not allow array
4225 * initializers. That is only allowed in GLSL ES 3.00.
4227 * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
4229 * "An array type can also be formed without specifying a size
4230 * if the definition includes an initializer:
4232 * float x[] = float[2] (1.0, 2.0); // declares an array of size 2
4233 * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
4238 _mesa_glsl_error(& loc
, state
,
4239 "unsized array declarations are not allowed in "
4243 /* If the declaration is not a redeclaration, there are a few additional
4244 * semantic checks that must be applied. In addition, variable that was
4245 * created for the declaration should be added to the IR stream.
4247 if (earlier
== NULL
) {
4248 validate_identifier(decl
->identifier
, loc
, state
);
4250 /* Add the variable to the symbol table. Note that the initializer's
4251 * IR was already processed earlier (though it hasn't been emitted
4252 * yet), without the variable in scope.
4254 * This differs from most C-like languages, but it follows the GLSL
4255 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
4258 * "Within a declaration, the scope of a name starts immediately
4259 * after the initializer if present or immediately after the name
4260 * being declared if not."
4262 if (!state
->symbols
->add_variable(var
)) {
4263 YYLTYPE loc
= this->get_location();
4264 _mesa_glsl_error(&loc
, state
, "name `%s' already taken in the "
4265 "current scope", decl
->identifier
);
4269 /* Push the variable declaration to the top. It means that all the
4270 * variable declarations will appear in a funny last-to-first order,
4271 * but otherwise we run into trouble if a function is prototyped, a
4272 * global var is decled, then the function is defined with usage of
4273 * the global var. See glslparsertest's CorrectModule.frag.
4275 instructions
->push_head(var
);
4278 instructions
->append_list(&initializer_instructions
);
4282 /* Generally, variable declarations do not have r-values. However,
4283 * one is used for the declaration in
4285 * while (bool b = some_condition()) {
4289 * so we return the rvalue from the last seen declaration here.
4296 ast_parameter_declarator::hir(exec_list
*instructions
,
4297 struct _mesa_glsl_parse_state
*state
)
4300 const struct glsl_type
*type
;
4301 const char *name
= NULL
;
4302 YYLTYPE loc
= this->get_location();
4304 type
= this->type
->glsl_type(& name
, state
);
4308 _mesa_glsl_error(& loc
, state
,
4309 "invalid type `%s' in declaration of `%s'",
4310 name
, this->identifier
);
4312 _mesa_glsl_error(& loc
, state
,
4313 "invalid type in declaration of `%s'",
4317 type
= glsl_type::error_type
;
4320 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
4322 * "Functions that accept no input arguments need not use void in the
4323 * argument list because prototypes (or definitions) are required and
4324 * therefore there is no ambiguity when an empty argument list "( )" is
4325 * declared. The idiom "(void)" as a parameter list is provided for
4328 * Placing this check here prevents a void parameter being set up
4329 * for a function, which avoids tripping up checks for main taking
4330 * parameters and lookups of an unnamed symbol.
4332 if (type
->is_void()) {
4333 if (this->identifier
!= NULL
)
4334 _mesa_glsl_error(& loc
, state
,
4335 "named parameter cannot have type `void'");
4341 if (formal_parameter
&& (this->identifier
== NULL
)) {
4342 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
4346 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
4347 * call already handled the "vec4[..] foo" case.
4349 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
4351 if (!type
->is_error() && type
->is_unsized_array()) {
4352 _mesa_glsl_error(&loc
, state
, "arrays passed as parameters must have "
4354 type
= glsl_type::error_type
;
4358 ir_variable
*var
= new(ctx
)
4359 ir_variable(type
, this->identifier
, ir_var_function_in
);
4361 /* Apply any specified qualifiers to the parameter declaration. Note that
4362 * for function parameters the default mode is 'in'.
4364 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
,
4367 /* From section 4.1.7 of the GLSL 4.40 spec:
4369 * "Opaque variables cannot be treated as l-values; hence cannot
4370 * be used as out or inout function parameters, nor can they be
4373 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
4374 && type
->contains_opaque()) {
4375 _mesa_glsl_error(&loc
, state
, "out and inout parameters cannot "
4376 "contain opaque variables");
4377 type
= glsl_type::error_type
;
4380 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
4382 * "When calling a function, expressions that do not evaluate to
4383 * l-values cannot be passed to parameters declared as out or inout."
4385 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
4387 * "Other binary or unary expressions, non-dereferenced arrays,
4388 * function names, swizzles with repeated fields, and constants
4389 * cannot be l-values."
4391 * So for GLSL 1.10, passing an array as an out or inout parameter is not
4392 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
4394 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
4396 && !state
->check_version(120, 100, &loc
,
4397 "arrays cannot be out or inout parameters")) {
4398 type
= glsl_type::error_type
;
4401 instructions
->push_tail(var
);
4403 /* Parameter declarations do not have r-values.
4410 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
4412 exec_list
*ir_parameters
,
4413 _mesa_glsl_parse_state
*state
)
4415 ast_parameter_declarator
*void_param
= NULL
;
4418 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
4419 param
->formal_parameter
= formal
;
4420 param
->hir(ir_parameters
, state
);
4428 if ((void_param
!= NULL
) && (count
> 1)) {
4429 YYLTYPE loc
= void_param
->get_location();
4431 _mesa_glsl_error(& loc
, state
,
4432 "`void' parameter must be only parameter");
4438 emit_function(_mesa_glsl_parse_state
*state
, ir_function
*f
)
4440 /* IR invariants disallow function declarations or definitions
4441 * nested within other function definitions. But there is no
4442 * requirement about the relative order of function declarations
4443 * and definitions with respect to one another. So simply insert
4444 * the new ir_function block at the end of the toplevel instruction
4447 state
->toplevel_ir
->push_tail(f
);
4452 ast_function::hir(exec_list
*instructions
,
4453 struct _mesa_glsl_parse_state
*state
)
4456 ir_function
*f
= NULL
;
4457 ir_function_signature
*sig
= NULL
;
4458 exec_list hir_parameters
;
4459 YYLTYPE loc
= this->get_location();
4461 const char *const name
= identifier
;
4463 /* New functions are always added to the top-level IR instruction stream,
4464 * so this instruction list pointer is ignored. See also emit_function
4467 (void) instructions
;
4469 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
4471 * "Function declarations (prototypes) cannot occur inside of functions;
4472 * they must be at global scope, or for the built-in functions, outside
4473 * the global scope."
4475 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
4477 * "User defined functions may only be defined within the global scope."
4479 * Note that this language does not appear in GLSL 1.10.
4481 if ((state
->current_function
!= NULL
) &&
4482 state
->is_version(120, 100)) {
4483 YYLTYPE loc
= this->get_location();
4484 _mesa_glsl_error(&loc
, state
,
4485 "declaration of function `%s' not allowed within "
4486 "function body", name
);
4489 validate_identifier(name
, this->get_location(), state
);
4491 /* Convert the list of function parameters to HIR now so that they can be
4492 * used below to compare this function's signature with previously seen
4493 * signatures for functions with the same name.
4495 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
4497 & hir_parameters
, state
);
4499 const char *return_type_name
;
4500 const glsl_type
*return_type
=
4501 this->return_type
->glsl_type(& return_type_name
, state
);
4504 YYLTYPE loc
= this->get_location();
4505 _mesa_glsl_error(&loc
, state
,
4506 "function `%s' has undeclared return type `%s'",
4507 name
, return_type_name
);
4508 return_type
= glsl_type::error_type
;
4511 /* ARB_shader_subroutine states:
4512 * "Subroutine declarations cannot be prototyped. It is an error to prepend
4513 * subroutine(...) to a function declaration."
4515 if (this->return_type
->qualifier
.flags
.q
.subroutine_def
&& !is_definition
) {
4516 YYLTYPE loc
= this->get_location();
4517 _mesa_glsl_error(&loc
, state
,
4518 "function declaration `%s' cannot have subroutine prepended",
4522 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
4523 * "No qualifier is allowed on the return type of a function."
4525 if (this->return_type
->has_qualifiers()) {
4526 YYLTYPE loc
= this->get_location();
4527 _mesa_glsl_error(& loc
, state
,
4528 "function `%s' return type has qualifiers", name
);
4531 /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
4533 * "Arrays are allowed as arguments and as the return type. In both
4534 * cases, the array must be explicitly sized."
4536 if (return_type
->is_unsized_array()) {
4537 YYLTYPE loc
= this->get_location();
4538 _mesa_glsl_error(& loc
, state
,
4539 "function `%s' return type array must be explicitly "
4543 /* From section 4.1.7 of the GLSL 4.40 spec:
4545 * "[Opaque types] can only be declared as function parameters
4546 * or uniform-qualified variables."
4548 if (return_type
->contains_opaque()) {
4549 YYLTYPE loc
= this->get_location();
4550 _mesa_glsl_error(&loc
, state
,
4551 "function `%s' return type can't contain an opaque type",
4555 /* Create an ir_function if one doesn't already exist. */
4556 f
= state
->symbols
->get_function(name
);
4558 f
= new(ctx
) ir_function(name
);
4559 if (!this->return_type
->qualifier
.flags
.q
.subroutine
) {
4560 if (!state
->symbols
->add_function(f
)) {
4561 /* This function name shadows a non-function use of the same name. */
4562 YYLTYPE loc
= this->get_location();
4563 _mesa_glsl_error(&loc
, state
, "function name `%s' conflicts with "
4564 "non-function", name
);
4568 emit_function(state
, f
);
4571 /* From GLSL ES 3.0 spec, chapter 6.1 "Function Definitions", page 71:
4573 * "A shader cannot redefine or overload built-in functions."
4575 * While in GLSL ES 1.0 specification, chapter 8 "Built-in Functions":
4577 * "User code can overload the built-in functions but cannot redefine
4580 if (state
->es_shader
&& state
->language_version
>= 300) {
4581 /* Local shader has no exact candidates; check the built-ins. */
4582 _mesa_glsl_initialize_builtin_functions();
4583 if (_mesa_glsl_find_builtin_function_by_name(name
)) {
4584 YYLTYPE loc
= this->get_location();
4585 _mesa_glsl_error(& loc
, state
,
4586 "A shader cannot redefine or overload built-in "
4587 "function `%s' in GLSL ES 3.00", name
);
4592 /* Verify that this function's signature either doesn't match a previously
4593 * seen signature for a function with the same name, or, if a match is found,
4594 * that the previously seen signature does not have an associated definition.
4596 if (state
->es_shader
|| f
->has_user_signature()) {
4597 sig
= f
->exact_matching_signature(state
, &hir_parameters
);
4599 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
4600 if (badvar
!= NULL
) {
4601 YYLTYPE loc
= this->get_location();
4603 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
4604 "qualifiers don't match prototype", name
, badvar
);
4607 if (sig
->return_type
!= return_type
) {
4608 YYLTYPE loc
= this->get_location();
4610 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
4611 "match prototype", name
);
4614 if (sig
->is_defined
) {
4615 if (is_definition
) {
4616 YYLTYPE loc
= this->get_location();
4617 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
4619 /* We just encountered a prototype that exactly matches a
4620 * function that's already been defined. This is redundant,
4621 * and we should ignore it.
4629 /* Verify the return type of main() */
4630 if (strcmp(name
, "main") == 0) {
4631 if (! return_type
->is_void()) {
4632 YYLTYPE loc
= this->get_location();
4634 _mesa_glsl_error(& loc
, state
, "main() must return void");
4637 if (!hir_parameters
.is_empty()) {
4638 YYLTYPE loc
= this->get_location();
4640 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
4644 /* Finish storing the information about this new function in its signature.
4647 sig
= new(ctx
) ir_function_signature(return_type
);
4648 f
->add_signature(sig
);
4651 sig
->replace_parameters(&hir_parameters
);
4654 if (this->return_type
->qualifier
.flags
.q
.subroutine_def
) {
4657 f
->num_subroutine_types
= this->return_type
->qualifier
.subroutine_list
->declarations
.length();
4658 f
->subroutine_types
= ralloc_array(state
, const struct glsl_type
*,
4659 f
->num_subroutine_types
);
4661 foreach_list_typed(ast_declaration
, decl
, link
, &this->return_type
->qualifier
.subroutine_list
->declarations
) {
4662 const struct glsl_type
*type
;
4663 /* the subroutine type must be already declared */
4664 type
= state
->symbols
->get_type(decl
->identifier
);
4666 _mesa_glsl_error(& loc
, state
, "unknown type '%s' in subroutine function definition", decl
->identifier
);
4668 f
->subroutine_types
[idx
++] = type
;
4670 state
->subroutines
= (ir_function
**)reralloc(state
, state
->subroutines
,
4672 state
->num_subroutines
+ 1);
4673 state
->subroutines
[state
->num_subroutines
] = f
;
4674 state
->num_subroutines
++;
4678 if (this->return_type
->qualifier
.flags
.q
.subroutine
) {
4679 if (!state
->symbols
->add_type(this->identifier
, glsl_type::get_subroutine_instance(this->identifier
))) {
4680 _mesa_glsl_error(& loc
, state
, "type '%s' previously defined", this->identifier
);
4683 state
->subroutine_types
= (ir_function
**)reralloc(state
, state
->subroutine_types
,
4685 state
->num_subroutine_types
+ 1);
4686 state
->subroutine_types
[state
->num_subroutine_types
] = f
;
4687 state
->num_subroutine_types
++;
4689 f
->is_subroutine
= true;
4692 /* Function declarations (prototypes) do not have r-values.
4699 ast_function_definition::hir(exec_list
*instructions
,
4700 struct _mesa_glsl_parse_state
*state
)
4702 prototype
->is_definition
= true;
4703 prototype
->hir(instructions
, state
);
4705 ir_function_signature
*signature
= prototype
->signature
;
4706 if (signature
== NULL
)
4709 assert(state
->current_function
== NULL
);
4710 state
->current_function
= signature
;
4711 state
->found_return
= false;
4713 /* Duplicate parameters declared in the prototype as concrete variables.
4714 * Add these to the symbol table.
4716 state
->symbols
->push_scope();
4717 foreach_in_list(ir_variable
, var
, &signature
->parameters
) {
4718 assert(var
->as_variable() != NULL
);
4720 /* The only way a parameter would "exist" is if two parameters have
4723 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
4724 YYLTYPE loc
= this->get_location();
4726 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
4728 state
->symbols
->add_variable(var
);
4732 /* Convert the body of the function to HIR. */
4733 this->body
->hir(&signature
->body
, state
);
4734 signature
->is_defined
= true;
4736 state
->symbols
->pop_scope();
4738 assert(state
->current_function
== signature
);
4739 state
->current_function
= NULL
;
4741 if (!signature
->return_type
->is_void() && !state
->found_return
) {
4742 YYLTYPE loc
= this->get_location();
4743 _mesa_glsl_error(& loc
, state
, "function `%s' has non-void return type "
4744 "%s, but no return statement",
4745 signature
->function_name(),
4746 signature
->return_type
->name
);
4749 /* Function definitions do not have r-values.
4756 ast_jump_statement::hir(exec_list
*instructions
,
4757 struct _mesa_glsl_parse_state
*state
)
4764 assert(state
->current_function
);
4766 if (opt_return_value
) {
4767 ir_rvalue
*ret
= opt_return_value
->hir(instructions
, state
);
4769 /* The value of the return type can be NULL if the shader says
4770 * 'return foo();' and foo() is a function that returns void.
4772 * NOTE: The GLSL spec doesn't say that this is an error. The type
4773 * of the return value is void. If the return type of the function is
4774 * also void, then this should compile without error. Seriously.
4776 const glsl_type
*const ret_type
=
4777 (ret
== NULL
) ? glsl_type::void_type
: ret
->type
;
4779 /* Implicit conversions are not allowed for return values prior to
4780 * ARB_shading_language_420pack.
4782 if (state
->current_function
->return_type
!= ret_type
) {
4783 YYLTYPE loc
= this->get_location();
4785 if (state
->ARB_shading_language_420pack_enable
) {
4786 if (!apply_implicit_conversion(state
->current_function
->return_type
,
4788 _mesa_glsl_error(& loc
, state
,
4789 "could not implicitly convert return value "
4790 "to %s, in function `%s'",
4791 state
->current_function
->return_type
->name
,
4792 state
->current_function
->function_name());
4795 _mesa_glsl_error(& loc
, state
,
4796 "`return' with wrong type %s, in function `%s' "
4799 state
->current_function
->function_name(),
4800 state
->current_function
->return_type
->name
);
4802 } else if (state
->current_function
->return_type
->base_type
==
4804 YYLTYPE loc
= this->get_location();
4806 /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
4807 * specs add a clarification:
4809 * "A void function can only use return without a return argument, even if
4810 * the return argument has void type. Return statements only accept values:
4813 * void func2() { return func1(); } // illegal return statement"
4815 _mesa_glsl_error(& loc
, state
,
4816 "void functions can only use `return' without a "
4820 inst
= new(ctx
) ir_return(ret
);
4822 if (state
->current_function
->return_type
->base_type
!=
4824 YYLTYPE loc
= this->get_location();
4826 _mesa_glsl_error(& loc
, state
,
4827 "`return' with no value, in function %s returning "
4829 state
->current_function
->function_name());
4831 inst
= new(ctx
) ir_return
;
4834 state
->found_return
= true;
4835 instructions
->push_tail(inst
);
4840 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
4841 YYLTYPE loc
= this->get_location();
4843 _mesa_glsl_error(& loc
, state
,
4844 "`discard' may only appear in a fragment shader");
4846 instructions
->push_tail(new(ctx
) ir_discard
);
4851 if (mode
== ast_continue
&&
4852 state
->loop_nesting_ast
== NULL
) {
4853 YYLTYPE loc
= this->get_location();
4855 _mesa_glsl_error(& loc
, state
, "continue may only appear in a loop");
4856 } else if (mode
== ast_break
&&
4857 state
->loop_nesting_ast
== NULL
&&
4858 state
->switch_state
.switch_nesting_ast
== NULL
) {
4859 YYLTYPE loc
= this->get_location();
4861 _mesa_glsl_error(& loc
, state
,
4862 "break may only appear in a loop or a switch");
4864 /* For a loop, inline the for loop expression again, since we don't
4865 * know where near the end of the loop body the normal copy of it is
4866 * going to be placed. Same goes for the condition for a do-while
4869 if (state
->loop_nesting_ast
!= NULL
&&
4870 mode
== ast_continue
&& !state
->switch_state
.is_switch_innermost
) {
4871 if (state
->loop_nesting_ast
->rest_expression
) {
4872 state
->loop_nesting_ast
->rest_expression
->hir(instructions
,
4875 if (state
->loop_nesting_ast
->mode
==
4876 ast_iteration_statement::ast_do_while
) {
4877 state
->loop_nesting_ast
->condition_to_hir(instructions
, state
);
4881 if (state
->switch_state
.is_switch_innermost
&&
4882 mode
== ast_continue
) {
4883 /* Set 'continue_inside' to true. */
4884 ir_rvalue
*const true_val
= new (ctx
) ir_constant(true);
4885 ir_dereference_variable
*deref_continue_inside_var
=
4886 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
4887 instructions
->push_tail(new(ctx
) ir_assignment(deref_continue_inside_var
,
4890 /* Break out from the switch, continue for the loop will
4891 * be called right after switch. */
4892 ir_loop_jump
*const jump
=
4893 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
4894 instructions
->push_tail(jump
);
4896 } else if (state
->switch_state
.is_switch_innermost
&&
4897 mode
== ast_break
) {
4898 /* Force break out of switch by inserting a break. */
4899 ir_loop_jump
*const jump
=
4900 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
4901 instructions
->push_tail(jump
);
4903 ir_loop_jump
*const jump
=
4904 new(ctx
) ir_loop_jump((mode
== ast_break
)
4905 ? ir_loop_jump::jump_break
4906 : ir_loop_jump::jump_continue
);
4907 instructions
->push_tail(jump
);
4914 /* Jump instructions do not have r-values.
4921 ast_selection_statement::hir(exec_list
*instructions
,
4922 struct _mesa_glsl_parse_state
*state
)
4926 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
4928 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
4930 * "Any expression whose type evaluates to a Boolean can be used as the
4931 * conditional expression bool-expression. Vector types are not accepted
4932 * as the expression to if."
4934 * The checks are separated so that higher quality diagnostics can be
4935 * generated for cases where both rules are violated.
4937 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
4938 YYLTYPE loc
= this->condition
->get_location();
4940 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
4944 ir_if
*const stmt
= new(ctx
) ir_if(condition
);
4946 if (then_statement
!= NULL
) {
4947 state
->symbols
->push_scope();
4948 then_statement
->hir(& stmt
->then_instructions
, state
);
4949 state
->symbols
->pop_scope();
4952 if (else_statement
!= NULL
) {
4953 state
->symbols
->push_scope();
4954 else_statement
->hir(& stmt
->else_instructions
, state
);
4955 state
->symbols
->pop_scope();
4958 instructions
->push_tail(stmt
);
4960 /* if-statements do not have r-values.
4967 ast_switch_statement::hir(exec_list
*instructions
,
4968 struct _mesa_glsl_parse_state
*state
)
4972 ir_rvalue
*const test_expression
=
4973 this->test_expression
->hir(instructions
, state
);
4975 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
4977 * "The type of init-expression in a switch statement must be a
4980 if (!test_expression
->type
->is_scalar() ||
4981 !test_expression
->type
->is_integer()) {
4982 YYLTYPE loc
= this->test_expression
->get_location();
4984 _mesa_glsl_error(& loc
,
4986 "switch-statement expression must be scalar "
4990 /* Track the switch-statement nesting in a stack-like manner.
4992 struct glsl_switch_state saved
= state
->switch_state
;
4994 state
->switch_state
.is_switch_innermost
= true;
4995 state
->switch_state
.switch_nesting_ast
= this;
4996 state
->switch_state
.labels_ht
= hash_table_ctor(0, hash_table_pointer_hash
,
4997 hash_table_pointer_compare
);
4998 state
->switch_state
.previous_default
= NULL
;
5000 /* Initalize is_fallthru state to false.
5002 ir_rvalue
*const is_fallthru_val
= new (ctx
) ir_constant(false);
5003 state
->switch_state
.is_fallthru_var
=
5004 new(ctx
) ir_variable(glsl_type::bool_type
,
5005 "switch_is_fallthru_tmp",
5007 instructions
->push_tail(state
->switch_state
.is_fallthru_var
);
5009 ir_dereference_variable
*deref_is_fallthru_var
=
5010 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
5011 instructions
->push_tail(new(ctx
) ir_assignment(deref_is_fallthru_var
,
5014 /* Initialize continue_inside state to false.
5016 state
->switch_state
.continue_inside
=
5017 new(ctx
) ir_variable(glsl_type::bool_type
,
5018 "continue_inside_tmp",
5020 instructions
->push_tail(state
->switch_state
.continue_inside
);
5022 ir_rvalue
*const false_val
= new (ctx
) ir_constant(false);
5023 ir_dereference_variable
*deref_continue_inside_var
=
5024 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
5025 instructions
->push_tail(new(ctx
) ir_assignment(deref_continue_inside_var
,
5028 state
->switch_state
.run_default
=
5029 new(ctx
) ir_variable(glsl_type::bool_type
,
5032 instructions
->push_tail(state
->switch_state
.run_default
);
5034 /* Loop around the switch is used for flow control. */
5035 ir_loop
* loop
= new(ctx
) ir_loop();
5036 instructions
->push_tail(loop
);
5038 /* Cache test expression.
5040 test_to_hir(&loop
->body_instructions
, state
);
5042 /* Emit code for body of switch stmt.
5044 body
->hir(&loop
->body_instructions
, state
);
5046 /* Insert a break at the end to exit loop. */
5047 ir_loop_jump
*jump
= new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
5048 loop
->body_instructions
.push_tail(jump
);
5050 /* If we are inside loop, check if continue got called inside switch. */
5051 if (state
->loop_nesting_ast
!= NULL
) {
5052 ir_dereference_variable
*deref_continue_inside
=
5053 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
5054 ir_if
*irif
= new(ctx
) ir_if(deref_continue_inside
);
5055 ir_loop_jump
*jump
= new(ctx
) ir_loop_jump(ir_loop_jump::jump_continue
);
5057 if (state
->loop_nesting_ast
!= NULL
) {
5058 if (state
->loop_nesting_ast
->rest_expression
) {
5059 state
->loop_nesting_ast
->rest_expression
->hir(&irif
->then_instructions
,
5062 if (state
->loop_nesting_ast
->mode
==
5063 ast_iteration_statement::ast_do_while
) {
5064 state
->loop_nesting_ast
->condition_to_hir(&irif
->then_instructions
, state
);
5067 irif
->then_instructions
.push_tail(jump
);
5068 instructions
->push_tail(irif
);
5071 hash_table_dtor(state
->switch_state
.labels_ht
);
5073 state
->switch_state
= saved
;
5075 /* Switch statements do not have r-values. */
5081 ast_switch_statement::test_to_hir(exec_list
*instructions
,
5082 struct _mesa_glsl_parse_state
*state
)
5086 /* Cache value of test expression. */
5087 ir_rvalue
*const test_val
=
5088 test_expression
->hir(instructions
,
5091 state
->switch_state
.test_var
= new(ctx
) ir_variable(test_val
->type
,
5094 ir_dereference_variable
*deref_test_var
=
5095 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
5097 instructions
->push_tail(state
->switch_state
.test_var
);
5098 instructions
->push_tail(new(ctx
) ir_assignment(deref_test_var
, test_val
));
5103 ast_switch_body::hir(exec_list
*instructions
,
5104 struct _mesa_glsl_parse_state
*state
)
5107 stmts
->hir(instructions
, state
);
5109 /* Switch bodies do not have r-values. */
5114 ast_case_statement_list::hir(exec_list
*instructions
,
5115 struct _mesa_glsl_parse_state
*state
)
5117 exec_list default_case
, after_default
, tmp
;
5119 foreach_list_typed (ast_case_statement
, case_stmt
, link
, & this->cases
) {
5120 case_stmt
->hir(&tmp
, state
);
5123 if (state
->switch_state
.previous_default
&& default_case
.is_empty()) {
5124 default_case
.append_list(&tmp
);
5128 /* If default case found, append 'after_default' list. */
5129 if (!default_case
.is_empty())
5130 after_default
.append_list(&tmp
);
5132 instructions
->append_list(&tmp
);
5135 /* Handle the default case. This is done here because default might not be
5136 * the last case. We need to add checks against following cases first to see
5137 * if default should be chosen or not.
5139 if (!default_case
.is_empty()) {
5141 ir_rvalue
*const true_val
= new (state
) ir_constant(true);
5142 ir_dereference_variable
*deref_run_default_var
=
5143 new(state
) ir_dereference_variable(state
->switch_state
.run_default
);
5145 /* Choose to run default case initially, following conditional
5146 * assignments might change this.
5148 ir_assignment
*const init_var
=
5149 new(state
) ir_assignment(deref_run_default_var
, true_val
);
5150 instructions
->push_tail(init_var
);
5152 /* Default case was the last one, no checks required. */
5153 if (after_default
.is_empty()) {
5154 instructions
->append_list(&default_case
);
5158 foreach_in_list(ir_instruction
, ir
, &after_default
) {
5159 ir_assignment
*assign
= ir
->as_assignment();
5164 /* Clone the check between case label and init expression. */
5165 ir_expression
*exp
= (ir_expression
*) assign
->condition
;
5166 ir_expression
*clone
= exp
->clone(state
, NULL
);
5168 ir_dereference_variable
*deref_var
=
5169 new(state
) ir_dereference_variable(state
->switch_state
.run_default
);
5170 ir_rvalue
*const false_val
= new (state
) ir_constant(false);
5172 ir_assignment
*const set_false
=
5173 new(state
) ir_assignment(deref_var
, false_val
, clone
);
5175 instructions
->push_tail(set_false
);
5178 /* Append default case and all cases after it. */
5179 instructions
->append_list(&default_case
);
5180 instructions
->append_list(&after_default
);
5183 /* Case statements do not have r-values. */
5188 ast_case_statement::hir(exec_list
*instructions
,
5189 struct _mesa_glsl_parse_state
*state
)
5191 labels
->hir(instructions
, state
);
5193 /* Guard case statements depending on fallthru state. */
5194 ir_dereference_variable
*const deref_fallthru_guard
=
5195 new(state
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
5196 ir_if
*const test_fallthru
= new(state
) ir_if(deref_fallthru_guard
);
5198 foreach_list_typed (ast_node
, stmt
, link
, & this->stmts
)
5199 stmt
->hir(& test_fallthru
->then_instructions
, state
);
5201 instructions
->push_tail(test_fallthru
);
5203 /* Case statements do not have r-values. */
5209 ast_case_label_list::hir(exec_list
*instructions
,
5210 struct _mesa_glsl_parse_state
*state
)
5212 foreach_list_typed (ast_case_label
, label
, link
, & this->labels
)
5213 label
->hir(instructions
, state
);
5215 /* Case labels do not have r-values. */
5220 ast_case_label::hir(exec_list
*instructions
,
5221 struct _mesa_glsl_parse_state
*state
)
5225 ir_dereference_variable
*deref_fallthru_var
=
5226 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
5228 ir_rvalue
*const true_val
= new(ctx
) ir_constant(true);
5230 /* If not default case, ... */
5231 if (this->test_value
!= NULL
) {
5232 /* Conditionally set fallthru state based on
5233 * comparison of cached test expression value to case label.
5235 ir_rvalue
*const label_rval
= this->test_value
->hir(instructions
, state
);
5236 ir_constant
*label_const
= label_rval
->constant_expression_value();
5239 YYLTYPE loc
= this->test_value
->get_location();
5241 _mesa_glsl_error(& loc
, state
,
5242 "switch statement case label must be a "
5243 "constant expression");
5245 /* Stuff a dummy value in to allow processing to continue. */
5246 label_const
= new(ctx
) ir_constant(0);
5248 ast_expression
*previous_label
= (ast_expression
*)
5249 hash_table_find(state
->switch_state
.labels_ht
,
5250 (void *)(uintptr_t)label_const
->value
.u
[0]);
5252 if (previous_label
) {
5253 YYLTYPE loc
= this->test_value
->get_location();
5254 _mesa_glsl_error(& loc
, state
, "duplicate case value");
5256 loc
= previous_label
->get_location();
5257 _mesa_glsl_error(& loc
, state
, "this is the previous case label");
5259 hash_table_insert(state
->switch_state
.labels_ht
,
5261 (void *)(uintptr_t)label_const
->value
.u
[0]);
5265 ir_dereference_variable
*deref_test_var
=
5266 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
5268 ir_expression
*test_cond
= new(ctx
) ir_expression(ir_binop_all_equal
,
5273 * From GLSL 4.40 specification section 6.2 ("Selection"):
5275 * "The type of the init-expression value in a switch statement must
5276 * be a scalar int or uint. The type of the constant-expression value
5277 * in a case label also must be a scalar int or uint. When any pair
5278 * of these values is tested for "equal value" and the types do not
5279 * match, an implicit conversion will be done to convert the int to a
5280 * uint (see section 4.1.10 “Implicit Conversions”) before the compare
5283 if (label_const
->type
!= state
->switch_state
.test_var
->type
) {
5284 YYLTYPE loc
= this->test_value
->get_location();
5286 const glsl_type
*type_a
= label_const
->type
;
5287 const glsl_type
*type_b
= state
->switch_state
.test_var
->type
;
5289 /* Check if int->uint implicit conversion is supported. */
5290 bool integer_conversion_supported
=
5291 glsl_type::int_type
->can_implicitly_convert_to(glsl_type::uint_type
,
5294 if ((!type_a
->is_integer() || !type_b
->is_integer()) ||
5295 !integer_conversion_supported
) {
5296 _mesa_glsl_error(&loc
, state
, "type mismatch with switch "
5297 "init-expression and case label (%s != %s)",
5298 type_a
->name
, type_b
->name
);
5300 /* Conversion of the case label. */
5301 if (type_a
->base_type
== GLSL_TYPE_INT
) {
5302 if (!apply_implicit_conversion(glsl_type::uint_type
,
5303 test_cond
->operands
[0], state
))
5304 _mesa_glsl_error(&loc
, state
, "implicit type conversion error");
5306 /* Conversion of the init-expression value. */
5307 if (!apply_implicit_conversion(glsl_type::uint_type
,
5308 test_cond
->operands
[1], state
))
5309 _mesa_glsl_error(&loc
, state
, "implicit type conversion error");
5314 ir_assignment
*set_fallthru_on_test
=
5315 new(ctx
) ir_assignment(deref_fallthru_var
, true_val
, test_cond
);
5317 instructions
->push_tail(set_fallthru_on_test
);
5318 } else { /* default case */
5319 if (state
->switch_state
.previous_default
) {
5320 YYLTYPE loc
= this->get_location();
5321 _mesa_glsl_error(& loc
, state
,
5322 "multiple default labels in one switch");
5324 loc
= state
->switch_state
.previous_default
->get_location();
5325 _mesa_glsl_error(& loc
, state
, "this is the first default label");
5327 state
->switch_state
.previous_default
= this;
5329 /* Set fallthru condition on 'run_default' bool. */
5330 ir_dereference_variable
*deref_run_default
=
5331 new(ctx
) ir_dereference_variable(state
->switch_state
.run_default
);
5332 ir_rvalue
*const cond_true
= new(ctx
) ir_constant(true);
5333 ir_expression
*test_cond
= new(ctx
) ir_expression(ir_binop_all_equal
,
5337 /* Set falltrhu state. */
5338 ir_assignment
*set_fallthru
=
5339 new(ctx
) ir_assignment(deref_fallthru_var
, true_val
, test_cond
);
5341 instructions
->push_tail(set_fallthru
);
5344 /* Case statements do not have r-values. */
5349 ast_iteration_statement::condition_to_hir(exec_list
*instructions
,
5350 struct _mesa_glsl_parse_state
*state
)
5354 if (condition
!= NULL
) {
5355 ir_rvalue
*const cond
=
5356 condition
->hir(instructions
, state
);
5359 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
5360 YYLTYPE loc
= condition
->get_location();
5362 _mesa_glsl_error(& loc
, state
,
5363 "loop condition must be scalar boolean");
5365 /* As the first code in the loop body, generate a block that looks
5366 * like 'if (!condition) break;' as the loop termination condition.
5368 ir_rvalue
*const not_cond
=
5369 new(ctx
) ir_expression(ir_unop_logic_not
, cond
);
5371 ir_if
*const if_stmt
= new(ctx
) ir_if(not_cond
);
5373 ir_jump
*const break_stmt
=
5374 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
5376 if_stmt
->then_instructions
.push_tail(break_stmt
);
5377 instructions
->push_tail(if_stmt
);
5384 ast_iteration_statement::hir(exec_list
*instructions
,
5385 struct _mesa_glsl_parse_state
*state
)
5389 /* For-loops and while-loops start a new scope, but do-while loops do not.
5391 if (mode
!= ast_do_while
)
5392 state
->symbols
->push_scope();
5394 if (init_statement
!= NULL
)
5395 init_statement
->hir(instructions
, state
);
5397 ir_loop
*const stmt
= new(ctx
) ir_loop();
5398 instructions
->push_tail(stmt
);
5400 /* Track the current loop nesting. */
5401 ast_iteration_statement
*nesting_ast
= state
->loop_nesting_ast
;
5403 state
->loop_nesting_ast
= this;
5405 /* Likewise, indicate that following code is closest to a loop,
5406 * NOT closest to a switch.
5408 bool saved_is_switch_innermost
= state
->switch_state
.is_switch_innermost
;
5409 state
->switch_state
.is_switch_innermost
= false;
5411 if (mode
!= ast_do_while
)
5412 condition_to_hir(&stmt
->body_instructions
, state
);
5415 body
->hir(& stmt
->body_instructions
, state
);
5417 if (rest_expression
!= NULL
)
5418 rest_expression
->hir(& stmt
->body_instructions
, state
);
5420 if (mode
== ast_do_while
)
5421 condition_to_hir(&stmt
->body_instructions
, state
);
5423 if (mode
!= ast_do_while
)
5424 state
->symbols
->pop_scope();
5426 /* Restore previous nesting before returning. */
5427 state
->loop_nesting_ast
= nesting_ast
;
5428 state
->switch_state
.is_switch_innermost
= saved_is_switch_innermost
;
5430 /* Loops do not have r-values.
5437 * Determine if the given type is valid for establishing a default precision
5440 * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
5442 * "The precision statement
5444 * precision precision-qualifier type;
5446 * can be used to establish a default precision qualifier. The type field
5447 * can be either int or float or any of the sampler types, and the
5448 * precision-qualifier can be lowp, mediump, or highp."
5450 * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
5451 * qualifiers on sampler types, but this seems like an oversight (since the
5452 * intention of including these in GLSL 1.30 is to allow compatibility with ES
5453 * shaders). So we allow int, float, and all sampler types regardless of GLSL
5457 is_valid_default_precision_type(const struct glsl_type
*const type
)
5462 switch (type
->base_type
) {
5464 case GLSL_TYPE_FLOAT
:
5465 /* "int" and "float" are valid, but vectors and matrices are not. */
5466 return type
->vector_elements
== 1 && type
->matrix_columns
== 1;
5467 case GLSL_TYPE_SAMPLER
:
5468 case GLSL_TYPE_IMAGE
:
5469 case GLSL_TYPE_ATOMIC_UINT
:
5478 ast_type_specifier::hir(exec_list
*instructions
,
5479 struct _mesa_glsl_parse_state
*state
)
5481 if (this->default_precision
== ast_precision_none
&& this->structure
== NULL
)
5484 YYLTYPE loc
= this->get_location();
5486 /* If this is a precision statement, check that the type to which it is
5487 * applied is either float or int.
5489 * From section 4.5.3 of the GLSL 1.30 spec:
5490 * "The precision statement
5491 * precision precision-qualifier type;
5492 * can be used to establish a default precision qualifier. The type
5493 * field can be either int or float [...]. Any other types or
5494 * qualifiers will result in an error.
5496 if (this->default_precision
!= ast_precision_none
) {
5497 if (!state
->check_precision_qualifiers_allowed(&loc
))
5500 if (this->structure
!= NULL
) {
5501 _mesa_glsl_error(&loc
, state
,
5502 "precision qualifiers do not apply to structures");
5506 if (this->array_specifier
!= NULL
) {
5507 _mesa_glsl_error(&loc
, state
,
5508 "default precision statements do not apply to "
5513 const struct glsl_type
*const type
=
5514 state
->symbols
->get_type(this->type_name
);
5515 if (!is_valid_default_precision_type(type
)) {
5516 _mesa_glsl_error(&loc
, state
,
5517 "default precision statements apply only to "
5518 "float, int, and opaque types");
5522 if (type
->base_type
== GLSL_TYPE_FLOAT
5524 && state
->stage
== MESA_SHADER_FRAGMENT
) {
5525 /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
5528 * "The fragment language has no default precision qualifier for
5529 * floating point types."
5531 * As a result, we have to track whether or not default precision has
5532 * been specified for float in GLSL ES fragment shaders.
5534 * Earlier in that same section, the spec says:
5536 * "Non-precision qualified declarations will use the precision
5537 * qualifier specified in the most recent precision statement
5538 * that is still in scope. The precision statement has the same
5539 * scoping rules as variable declarations. If it is declared
5540 * inside a compound statement, its effect stops at the end of
5541 * the innermost statement it was declared in. Precision
5542 * statements in nested scopes override precision statements in
5543 * outer scopes. Multiple precision statements for the same basic
5544 * type can appear inside the same scope, with later statements
5545 * overriding earlier statements within that scope."
5547 * Default precision specifications follow the same scope rules as
5548 * variables. So, we can track the state of the default float
5549 * precision in the symbol table, and the rules will just work. This
5550 * is a slight abuse of the symbol table, but it has the semantics
5553 ir_variable
*const junk
=
5554 new(state
) ir_variable(type
, "#default precision",
5557 state
->symbols
->add_variable(junk
);
5560 /* FINISHME: Translate precision statements into IR. */
5564 /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
5565 * process_record_constructor() can do type-checking on C-style initializer
5566 * expressions of structs, but ast_struct_specifier should only be translated
5567 * to HIR if it is declaring the type of a structure.
5569 * The ->is_declaration field is false for initializers of variables
5570 * declared separately from the struct's type definition.
5572 * struct S { ... }; (is_declaration = true)
5573 * struct T { ... } t = { ... }; (is_declaration = true)
5574 * S s = { ... }; (is_declaration = false)
5576 if (this->structure
!= NULL
&& this->structure
->is_declaration
)
5577 return this->structure
->hir(instructions
, state
);
5584 * Process a structure or interface block tree into an array of structure fields
5586 * After parsing, where there are some syntax differnces, structures and
5587 * interface blocks are almost identical. They are similar enough that the
5588 * AST for each can be processed the same way into a set of
5589 * \c glsl_struct_field to describe the members.
5591 * If we're processing an interface block, var_mode should be the type of the
5592 * interface block (ir_var_shader_in, ir_var_shader_out, ir_var_uniform or
5593 * ir_var_shader_storage). If we're processing a structure, var_mode should be
5597 * The number of fields processed. A pointer to the array structure fields is
5598 * stored in \c *fields_ret.
5601 ast_process_structure_or_interface_block(exec_list
*instructions
,
5602 struct _mesa_glsl_parse_state
*state
,
5603 exec_list
*declarations
,
5605 glsl_struct_field
**fields_ret
,
5607 enum glsl_matrix_layout matrix_layout
,
5608 bool allow_reserved_names
,
5609 ir_variable_mode var_mode
)
5611 unsigned decl_count
= 0;
5613 /* Make an initial pass over the list of fields to determine how
5614 * many there are. Each element in this list is an ast_declarator_list.
5615 * This means that we actually need to count the number of elements in the
5616 * 'declarations' list in each of the elements.
5618 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
5619 decl_count
+= decl_list
->declarations
.length();
5622 /* Allocate storage for the fields and process the field
5623 * declarations. As the declarations are processed, try to also convert
5624 * the types to HIR. This ensures that structure definitions embedded in
5625 * other structure definitions or in interface blocks are processed.
5627 glsl_struct_field
*const fields
= ralloc_array(state
, glsl_struct_field
,
5631 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
5632 const char *type_name
;
5634 decl_list
->type
->specifier
->hir(instructions
, state
);
5636 /* Section 10.9 of the GLSL ES 1.00 specification states that
5637 * embedded structure definitions have been removed from the language.
5639 if (state
->es_shader
&& decl_list
->type
->specifier
->structure
!= NULL
) {
5640 _mesa_glsl_error(&loc
, state
, "embedded structure definitions are "
5641 "not allowed in GLSL ES 1.00");
5644 const glsl_type
*decl_type
=
5645 decl_list
->type
->glsl_type(& type_name
, state
);
5647 foreach_list_typed (ast_declaration
, decl
, link
,
5648 &decl_list
->declarations
) {
5649 if (!allow_reserved_names
)
5650 validate_identifier(decl
->identifier
, loc
, state
);
5652 /* From section 4.3.9 of the GLSL 4.40 spec:
5654 * "[In interface blocks] opaque types are not allowed."
5656 * It should be impossible for decl_type to be NULL here. Cases that
5657 * might naturally lead to decl_type being NULL, especially for the
5658 * is_interface case, will have resulted in compilation having
5659 * already halted due to a syntax error.
5661 const struct glsl_type
*field_type
=
5662 decl_type
!= NULL
? decl_type
: glsl_type::error_type
;
5664 if (is_interface
&& field_type
->contains_opaque()) {
5665 YYLTYPE loc
= decl_list
->get_location();
5666 _mesa_glsl_error(&loc
, state
,
5667 "uniform/buffer in non-default interface block contains "
5671 if (field_type
->contains_atomic()) {
5672 /* From section 4.1.7.3 of the GLSL 4.40 spec:
5674 * "Members of structures cannot be declared as atomic counter
5677 YYLTYPE loc
= decl_list
->get_location();
5678 _mesa_glsl_error(&loc
, state
, "atomic counter in structure, "
5679 "shader storage block or uniform block");
5682 if (field_type
->contains_image()) {
5683 /* FINISHME: Same problem as with atomic counters.
5684 * FINISHME: Request clarification from Khronos and add
5685 * FINISHME: spec quotation here.
5687 YYLTYPE loc
= decl_list
->get_location();
5688 _mesa_glsl_error(&loc
, state
,
5689 "image in structure, shader storage block or "
5693 const struct ast_type_qualifier
*const qual
=
5694 & decl_list
->type
->qualifier
;
5695 if (qual
->flags
.q
.std140
||
5696 qual
->flags
.q
.std430
||
5697 qual
->flags
.q
.packed
||
5698 qual
->flags
.q
.shared
) {
5699 _mesa_glsl_error(&loc
, state
,
5700 "uniform/shader storage block layout qualifiers "
5701 "std140, std430, packed, and shared can only be "
5702 "applied to uniform/shader storage blocks, not "
5706 if (qual
->flags
.q
.constant
) {
5707 YYLTYPE loc
= decl_list
->get_location();
5708 _mesa_glsl_error(&loc
, state
,
5709 "const storage qualifier cannot be applied "
5710 "to struct or interface block members");
5713 field_type
= process_array_type(&loc
, decl_type
,
5714 decl
->array_specifier
, state
);
5715 fields
[i
].type
= field_type
;
5716 fields
[i
].name
= decl
->identifier
;
5717 fields
[i
].location
= -1;
5718 fields
[i
].interpolation
=
5719 interpret_interpolation_qualifier(qual
, var_mode
, state
, &loc
);
5720 fields
[i
].centroid
= qual
->flags
.q
.centroid
? 1 : 0;
5721 fields
[i
].sample
= qual
->flags
.q
.sample
? 1 : 0;
5722 fields
[i
].patch
= qual
->flags
.q
.patch
? 1 : 0;
5724 /* Only save explicitly defined streams in block's field */
5725 fields
[i
].stream
= qual
->flags
.q
.explicit_stream
? qual
->stream
: -1;
5727 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
5728 if (!qual
->flags
.q
.uniform
&& !qual
->flags
.q
.buffer
) {
5729 _mesa_glsl_error(&loc
, state
,
5730 "row_major and column_major can only be "
5731 "applied to interface blocks");
5733 validate_matrix_layout_for_type(state
, &loc
, field_type
, NULL
);
5736 if (qual
->flags
.q
.uniform
&& qual
->has_interpolation()) {
5737 _mesa_glsl_error(&loc
, state
,
5738 "interpolation qualifiers cannot be used "
5739 "with uniform interface blocks");
5742 if ((qual
->flags
.q
.uniform
|| !is_interface
) &&
5743 qual
->has_auxiliary_storage()) {
5744 _mesa_glsl_error(&loc
, state
,
5745 "auxiliary storage qualifiers cannot be used "
5746 "in uniform blocks or structures.");
5749 /* Propogate row- / column-major information down the fields of the
5750 * structure or interface block. Structures need this data because
5751 * the structure may contain a structure that contains ... a matrix
5752 * that need the proper layout.
5754 if (field_type
->without_array()->is_matrix()
5755 || field_type
->without_array()->is_record()) {
5756 /* If no layout is specified for the field, inherit the layout
5759 fields
[i
].matrix_layout
= matrix_layout
;
5761 if (qual
->flags
.q
.row_major
)
5762 fields
[i
].matrix_layout
= GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
5763 else if (qual
->flags
.q
.column_major
)
5764 fields
[i
].matrix_layout
= GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
;
5766 /* If we're processing an interface block, the matrix layout must
5767 * be decided by this point.
5769 assert(!is_interface
5770 || fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
5771 || fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
);
5778 assert(i
== decl_count
);
5780 *fields_ret
= fields
;
5786 ast_struct_specifier::hir(exec_list
*instructions
,
5787 struct _mesa_glsl_parse_state
*state
)
5789 YYLTYPE loc
= this->get_location();
5791 /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
5793 * "Anonymous structures are not supported; so embedded structures must
5794 * have a declarator. A name given to an embedded struct is scoped at
5795 * the same level as the struct it is embedded in."
5797 * The same section of the GLSL 1.20 spec says:
5799 * "Anonymous structures are not supported. Embedded structures are not
5802 * struct S { float f; };
5804 * S; // Error: anonymous structures disallowed
5805 * struct { ... }; // Error: embedded structures disallowed
5806 * S s; // Okay: nested structures with name are allowed
5809 * The GLSL ES 1.00 and 3.00 specs have similar langauge and examples. So,
5810 * we allow embedded structures in 1.10 only.
5812 if (state
->language_version
!= 110 && state
->struct_specifier_depth
!= 0)
5813 _mesa_glsl_error(&loc
, state
,
5814 "embedded structure declarations are not allowed");
5816 state
->struct_specifier_depth
++;
5818 glsl_struct_field
*fields
;
5819 unsigned decl_count
=
5820 ast_process_structure_or_interface_block(instructions
,
5822 &this->declarations
,
5826 GLSL_MATRIX_LAYOUT_INHERITED
,
5827 false /* allow_reserved_names */,
5830 validate_identifier(this->name
, loc
, state
);
5832 const glsl_type
*t
=
5833 glsl_type::get_record_instance(fields
, decl_count
, this->name
);
5835 if (!state
->symbols
->add_type(name
, t
)) {
5836 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
5838 const glsl_type
**s
= reralloc(state
, state
->user_structures
,
5840 state
->num_user_structures
+ 1);
5842 s
[state
->num_user_structures
] = t
;
5843 state
->user_structures
= s
;
5844 state
->num_user_structures
++;
5848 state
->struct_specifier_depth
--;
5850 /* Structure type definitions do not have r-values.
5857 * Visitor class which detects whether a given interface block has been used.
5859 class interface_block_usage_visitor
: public ir_hierarchical_visitor
5862 interface_block_usage_visitor(ir_variable_mode mode
, const glsl_type
*block
)
5863 : mode(mode
), block(block
), found(false)
5867 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
5869 if (ir
->var
->data
.mode
== mode
&& ir
->var
->get_interface_type() == block
) {
5873 return visit_continue
;
5876 bool usage_found() const
5882 ir_variable_mode mode
;
5883 const glsl_type
*block
;
5888 is_unsized_array_last_element(ir_variable
*v
)
5890 const glsl_type
*interface_type
= v
->get_interface_type();
5891 int length
= interface_type
->length
;
5893 assert(v
->type
->is_unsized_array());
5895 /* Check if it is the last element of the interface */
5896 if (strcmp(interface_type
->fields
.structure
[length
-1].name
, v
->name
) == 0)
5902 ast_interface_block::hir(exec_list
*instructions
,
5903 struct _mesa_glsl_parse_state
*state
)
5905 YYLTYPE loc
= this->get_location();
5907 /* Interface blocks must be declared at global scope */
5908 if (state
->current_function
!= NULL
) {
5909 _mesa_glsl_error(&loc
, state
,
5910 "Interface block `%s' must be declared "
5915 if (!this->layout
.flags
.q
.buffer
&&
5916 this->layout
.flags
.q
.std430
) {
5917 _mesa_glsl_error(&loc
, state
,
5918 "std430 storage block layout qualifier is supported "
5919 "only for shader storage blocks");
5922 /* The ast_interface_block has a list of ast_declarator_lists. We
5923 * need to turn those into ir_variables with an association
5924 * with this uniform block.
5926 enum glsl_interface_packing packing
;
5927 if (this->layout
.flags
.q
.shared
) {
5928 packing
= GLSL_INTERFACE_PACKING_SHARED
;
5929 } else if (this->layout
.flags
.q
.packed
) {
5930 packing
= GLSL_INTERFACE_PACKING_PACKED
;
5931 } else if (this->layout
.flags
.q
.std430
) {
5932 packing
= GLSL_INTERFACE_PACKING_STD430
;
5934 /* The default layout is std140.
5936 packing
= GLSL_INTERFACE_PACKING_STD140
;
5939 ir_variable_mode var_mode
;
5940 const char *iface_type_name
;
5941 if (this->layout
.flags
.q
.in
) {
5942 var_mode
= ir_var_shader_in
;
5943 iface_type_name
= "in";
5944 } else if (this->layout
.flags
.q
.out
) {
5945 var_mode
= ir_var_shader_out
;
5946 iface_type_name
= "out";
5947 } else if (this->layout
.flags
.q
.uniform
) {
5948 var_mode
= ir_var_uniform
;
5949 iface_type_name
= "uniform";
5950 } else if (this->layout
.flags
.q
.buffer
) {
5951 var_mode
= ir_var_shader_storage
;
5952 iface_type_name
= "buffer";
5954 var_mode
= ir_var_auto
;
5955 iface_type_name
= "UNKNOWN";
5956 assert(!"interface block layout qualifier not found!");
5959 enum glsl_matrix_layout matrix_layout
= GLSL_MATRIX_LAYOUT_INHERITED
;
5960 if (this->layout
.flags
.q
.row_major
)
5961 matrix_layout
= GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
5962 else if (this->layout
.flags
.q
.column_major
)
5963 matrix_layout
= GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
;
5965 bool redeclaring_per_vertex
= strcmp(this->block_name
, "gl_PerVertex") == 0;
5966 exec_list declared_variables
;
5967 glsl_struct_field
*fields
;
5969 /* Treat an interface block as one level of nesting, so that embedded struct
5970 * specifiers will be disallowed.
5972 state
->struct_specifier_depth
++;
5974 unsigned int num_variables
=
5975 ast_process_structure_or_interface_block(&declared_variables
,
5977 &this->declarations
,
5982 redeclaring_per_vertex
,
5985 state
->struct_specifier_depth
--;
5987 if (!redeclaring_per_vertex
) {
5988 validate_identifier(this->block_name
, loc
, state
);
5990 /* From section 4.3.9 ("Interface Blocks") of the GLSL 4.50 spec:
5992 * "Block names have no other use within a shader beyond interface
5993 * matching; it is a compile-time error to use a block name at global
5994 * scope for anything other than as a block name."
5996 ir_variable
*var
= state
->symbols
->get_variable(this->block_name
);
5997 if (var
&& !var
->type
->is_interface()) {
5998 _mesa_glsl_error(&loc
, state
, "Block name `%s' is "
5999 "already used in the scope.",
6004 const glsl_type
*earlier_per_vertex
= NULL
;
6005 if (redeclaring_per_vertex
) {
6006 /* Find the previous declaration of gl_PerVertex. If we're redeclaring
6007 * the named interface block gl_in, we can find it by looking at the
6008 * previous declaration of gl_in. Otherwise we can find it by looking
6009 * at the previous decalartion of any of the built-in outputs,
6012 * Also check that the instance name and array-ness of the redeclaration
6016 case ir_var_shader_in
:
6017 if (ir_variable
*earlier_gl_in
=
6018 state
->symbols
->get_variable("gl_in")) {
6019 earlier_per_vertex
= earlier_gl_in
->get_interface_type();
6021 _mesa_glsl_error(&loc
, state
,
6022 "redeclaration of gl_PerVertex input not allowed "
6024 _mesa_shader_stage_to_string(state
->stage
));
6026 if (this->instance_name
== NULL
||
6027 strcmp(this->instance_name
, "gl_in") != 0 || this->array_specifier
== NULL
) {
6028 _mesa_glsl_error(&loc
, state
,
6029 "gl_PerVertex input must be redeclared as "
6033 case ir_var_shader_out
:
6034 if (ir_variable
*earlier_gl_Position
=
6035 state
->symbols
->get_variable("gl_Position")) {
6036 earlier_per_vertex
= earlier_gl_Position
->get_interface_type();
6037 } else if (ir_variable
*earlier_gl_out
=
6038 state
->symbols
->get_variable("gl_out")) {
6039 earlier_per_vertex
= earlier_gl_out
->get_interface_type();
6041 _mesa_glsl_error(&loc
, state
,
6042 "redeclaration of gl_PerVertex output not "
6043 "allowed in the %s shader",
6044 _mesa_shader_stage_to_string(state
->stage
));
6046 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
6047 if (this->instance_name
== NULL
||
6048 strcmp(this->instance_name
, "gl_out") != 0 || this->array_specifier
== NULL
) {
6049 _mesa_glsl_error(&loc
, state
,
6050 "gl_PerVertex output must be redeclared as "
6054 if (this->instance_name
!= NULL
) {
6055 _mesa_glsl_error(&loc
, state
,
6056 "gl_PerVertex output may not be redeclared with "
6057 "an instance name");
6062 _mesa_glsl_error(&loc
, state
,
6063 "gl_PerVertex must be declared as an input or an "
6068 if (earlier_per_vertex
== NULL
) {
6069 /* An error has already been reported. Bail out to avoid null
6070 * dereferences later in this function.
6075 /* Copy locations from the old gl_PerVertex interface block. */
6076 for (unsigned i
= 0; i
< num_variables
; i
++) {
6077 int j
= earlier_per_vertex
->field_index(fields
[i
].name
);
6079 _mesa_glsl_error(&loc
, state
,
6080 "redeclaration of gl_PerVertex must be a subset "
6081 "of the built-in members of gl_PerVertex");
6083 fields
[i
].location
=
6084 earlier_per_vertex
->fields
.structure
[j
].location
;
6085 fields
[i
].interpolation
=
6086 earlier_per_vertex
->fields
.structure
[j
].interpolation
;
6087 fields
[i
].centroid
=
6088 earlier_per_vertex
->fields
.structure
[j
].centroid
;
6090 earlier_per_vertex
->fields
.structure
[j
].sample
;
6092 earlier_per_vertex
->fields
.structure
[j
].patch
;
6096 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10
6099 * If a built-in interface block is redeclared, it must appear in
6100 * the shader before any use of any member included in the built-in
6101 * declaration, or a compilation error will result.
6103 * This appears to be a clarification to the behaviour established for
6104 * gl_PerVertex by GLSL 1.50, therefore we implement this behaviour
6105 * regardless of GLSL version.
6107 interface_block_usage_visitor
v(var_mode
, earlier_per_vertex
);
6108 v
.run(instructions
);
6109 if (v
.usage_found()) {
6110 _mesa_glsl_error(&loc
, state
,
6111 "redeclaration of a built-in interface block must "
6112 "appear before any use of any member of the "
6117 const glsl_type
*block_type
=
6118 glsl_type::get_interface_instance(fields
,
6122 if (this->layout
.flags
.q
.explicit_binding
)
6123 validate_binding_qualifier(state
, &loc
, block_type
, &this->layout
);
6125 if (!state
->symbols
->add_interface(block_type
->name
, block_type
, var_mode
)) {
6126 YYLTYPE loc
= this->get_location();
6127 _mesa_glsl_error(&loc
, state
, "interface block `%s' with type `%s' "
6128 "already taken in the current scope",
6129 this->block_name
, iface_type_name
);
6132 /* Since interface blocks cannot contain statements, it should be
6133 * impossible for the block to generate any instructions.
6135 assert(declared_variables
.is_empty());
6137 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
6139 * Geometry shader input variables get the per-vertex values written
6140 * out by vertex shader output variables of the same names. Since a
6141 * geometry shader operates on a set of vertices, each input varying
6142 * variable (or input block, see interface blocks below) needs to be
6143 * declared as an array.
6145 if (state
->stage
== MESA_SHADER_GEOMETRY
&& this->array_specifier
== NULL
&&
6146 var_mode
== ir_var_shader_in
) {
6147 _mesa_glsl_error(&loc
, state
, "geometry shader inputs must be arrays");
6148 } else if ((state
->stage
== MESA_SHADER_TESS_CTRL
||
6149 state
->stage
== MESA_SHADER_TESS_EVAL
) &&
6150 this->array_specifier
== NULL
&&
6151 var_mode
== ir_var_shader_in
) {
6152 _mesa_glsl_error(&loc
, state
, "per-vertex tessellation shader inputs must be arrays");
6153 } else if (state
->stage
== MESA_SHADER_TESS_CTRL
&&
6154 this->array_specifier
== NULL
&&
6155 var_mode
== ir_var_shader_out
) {
6156 _mesa_glsl_error(&loc
, state
, "tessellation control shader outputs must be arrays");
6160 /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
6163 * "If an instance name (instance-name) is used, then it puts all the
6164 * members inside a scope within its own name space, accessed with the
6165 * field selector ( . ) operator (analogously to structures)."
6167 if (this->instance_name
) {
6168 if (redeclaring_per_vertex
) {
6169 /* When a built-in in an unnamed interface block is redeclared,
6170 * get_variable_being_redeclared() calls
6171 * check_builtin_array_max_size() to make sure that built-in array
6172 * variables aren't redeclared to illegal sizes. But we're looking
6173 * at a redeclaration of a named built-in interface block. So we
6174 * have to manually call check_builtin_array_max_size() for all parts
6175 * of the interface that are arrays.
6177 for (unsigned i
= 0; i
< num_variables
; i
++) {
6178 if (fields
[i
].type
->is_array()) {
6179 const unsigned size
= fields
[i
].type
->array_size();
6180 check_builtin_array_max_size(fields
[i
].name
, size
, loc
, state
);
6184 validate_identifier(this->instance_name
, loc
, state
);
6189 if (this->array_specifier
!= NULL
) {
6190 /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
6192 * For uniform blocks declared an array, each individual array
6193 * element corresponds to a separate buffer object backing one
6194 * instance of the block. As the array size indicates the number
6195 * of buffer objects needed, uniform block array declarations
6196 * must specify an array size.
6198 * And a few paragraphs later:
6200 * Geometry shader input blocks must be declared as arrays and
6201 * follow the array declaration and linking rules for all
6202 * geometry shader inputs. All other input and output block
6203 * arrays must specify an array size.
6205 * The same applies to tessellation shaders.
6207 * The upshot of this is that the only circumstance where an
6208 * interface array size *doesn't* need to be specified is on a
6209 * geometry shader input, tessellation control shader input,
6210 * tessellation control shader output, and tessellation evaluation
6213 if (this->array_specifier
->is_unsized_array
) {
6214 bool allow_inputs
= state
->stage
== MESA_SHADER_GEOMETRY
||
6215 state
->stage
== MESA_SHADER_TESS_CTRL
||
6216 state
->stage
== MESA_SHADER_TESS_EVAL
;
6217 bool allow_outputs
= state
->stage
== MESA_SHADER_TESS_CTRL
;
6219 if (this->layout
.flags
.q
.in
) {
6221 _mesa_glsl_error(&loc
, state
,
6222 "unsized input block arrays not allowed in "
6224 _mesa_shader_stage_to_string(state
->stage
));
6225 } else if (this->layout
.flags
.q
.out
) {
6227 _mesa_glsl_error(&loc
, state
,
6228 "unsized output block arrays not allowed in "
6230 _mesa_shader_stage_to_string(state
->stage
));
6232 /* by elimination, this is a uniform block array */
6233 _mesa_glsl_error(&loc
, state
,
6234 "unsized uniform block arrays not allowed in "
6236 _mesa_shader_stage_to_string(state
->stage
));
6240 const glsl_type
*block_array_type
=
6241 process_array_type(&loc
, block_type
, this->array_specifier
, state
);
6243 /* From section 4.3.9 (Interface Blocks) of the GLSL ES 3.10 spec:
6245 * * Arrays of arrays of blocks are not allowed
6247 if (state
->es_shader
&& block_array_type
->is_array() &&
6248 block_array_type
->fields
.array
->is_array()) {
6249 _mesa_glsl_error(&loc
, state
,
6250 "arrays of arrays interface blocks are "
6254 if (this->layout
.flags
.q
.explicit_binding
)
6255 validate_binding_qualifier(state
, &loc
, block_array_type
,
6258 var
= new(state
) ir_variable(block_array_type
,
6259 this->instance_name
,
6262 var
= new(state
) ir_variable(block_type
,
6263 this->instance_name
,
6267 var
->data
.matrix_layout
= matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
6268 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
: matrix_layout
;
6270 if (var_mode
== ir_var_shader_in
|| var_mode
== ir_var_uniform
)
6271 var
->data
.read_only
= true;
6273 if (state
->stage
== MESA_SHADER_GEOMETRY
&& var_mode
== ir_var_shader_in
)
6274 handle_geometry_shader_input_decl(state
, loc
, var
);
6275 else if ((state
->stage
== MESA_SHADER_TESS_CTRL
||
6276 state
->stage
== MESA_SHADER_TESS_EVAL
) && var_mode
== ir_var_shader_in
)
6277 handle_tess_shader_input_decl(state
, loc
, var
);
6278 else if (state
->stage
== MESA_SHADER_TESS_CTRL
&& var_mode
== ir_var_shader_out
)
6279 handle_tess_ctrl_shader_output_decl(state
, loc
, var
);
6281 for (unsigned i
= 0; i
< num_variables
; i
++) {
6282 if (fields
[i
].type
->is_unsized_array()) {
6283 if (var_mode
== ir_var_shader_storage
) {
6284 if (i
!= (num_variables
- 1)) {
6285 _mesa_glsl_error(&loc
, state
, "unsized array `%s' definition: "
6286 "only last member of a shader storage block "
6287 "can be defined as unsized array",
6291 /* From GLSL ES 3.10 spec, section 4.1.9 "Arrays":
6293 * "If an array is declared as the last member of a shader storage
6294 * block and the size is not specified at compile-time, it is
6295 * sized at run-time. In all other cases, arrays are sized only
6298 if (state
->es_shader
) {
6299 _mesa_glsl_error(&loc
, state
, "unsized array `%s' definition: "
6300 "only last member of a shader storage block "
6301 "can be defined as unsized array",
6308 if (ir_variable
*earlier
=
6309 state
->symbols
->get_variable(this->instance_name
)) {
6310 if (!redeclaring_per_vertex
) {
6311 _mesa_glsl_error(&loc
, state
, "`%s' redeclared",
6312 this->instance_name
);
6314 earlier
->data
.how_declared
= ir_var_declared_normally
;
6315 earlier
->type
= var
->type
;
6316 earlier
->reinit_interface_type(block_type
);
6319 /* Propagate the "binding" keyword into this UBO's fields;
6320 * the UBO declaration itself doesn't get an ir_variable unless it
6321 * has an instance name. This is ugly.
6323 var
->data
.explicit_binding
= this->layout
.flags
.q
.explicit_binding
;
6324 var
->data
.binding
= this->layout
.binding
;
6326 state
->symbols
->add_variable(var
);
6327 instructions
->push_tail(var
);
6330 /* In order to have an array size, the block must also be declared with
6333 assert(this->array_specifier
== NULL
);
6335 for (unsigned i
= 0; i
< num_variables
; i
++) {
6337 new(state
) ir_variable(fields
[i
].type
,
6338 ralloc_strdup(state
, fields
[i
].name
),
6340 var
->data
.interpolation
= fields
[i
].interpolation
;
6341 var
->data
.centroid
= fields
[i
].centroid
;
6342 var
->data
.sample
= fields
[i
].sample
;
6343 var
->data
.patch
= fields
[i
].patch
;
6344 var
->init_interface_type(block_type
);
6346 if (var_mode
== ir_var_shader_in
|| var_mode
== ir_var_uniform
)
6347 var
->data
.read_only
= true;
6349 if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
) {
6350 var
->data
.matrix_layout
= matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
6351 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
: matrix_layout
;
6353 var
->data
.matrix_layout
= fields
[i
].matrix_layout
;
6356 if (fields
[i
].stream
!= -1 &&
6357 ((unsigned)fields
[i
].stream
) != this->layout
.stream
) {
6358 _mesa_glsl_error(&loc
, state
,
6359 "stream layout qualifier on "
6360 "interface block member `%s' does not match "
6361 "the interface block (%d vs %d)",
6362 var
->name
, fields
[i
].stream
, this->layout
.stream
);
6365 var
->data
.stream
= this->layout
.stream
;
6367 /* Examine var name here since var may get deleted in the next call */
6368 bool var_is_gl_id
= is_gl_identifier(var
->name
);
6370 if (redeclaring_per_vertex
) {
6371 ir_variable
*earlier
=
6372 get_variable_being_redeclared(var
, loc
, state
,
6373 true /* allow_all_redeclarations */);
6374 if (!var_is_gl_id
|| earlier
== NULL
) {
6375 _mesa_glsl_error(&loc
, state
,
6376 "redeclaration of gl_PerVertex can only "
6377 "include built-in variables");
6378 } else if (earlier
->data
.how_declared
== ir_var_declared_normally
) {
6379 _mesa_glsl_error(&loc
, state
,
6380 "`%s' has already been redeclared",
6383 earlier
->data
.how_declared
= ir_var_declared_in_block
;
6384 earlier
->reinit_interface_type(block_type
);
6389 if (state
->symbols
->get_variable(var
->name
) != NULL
)
6390 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
6392 /* Propagate the "binding" keyword into this UBO/SSBO's fields.
6393 * The UBO declaration itself doesn't get an ir_variable unless it
6394 * has an instance name. This is ugly.
6396 var
->data
.explicit_binding
= this->layout
.flags
.q
.explicit_binding
;
6397 var
->data
.binding
= this->layout
.binding
;
6399 if (var
->type
->is_unsized_array()) {
6400 if (var
->is_in_shader_storage_block()) {
6401 if (!is_unsized_array_last_element(var
)) {
6402 _mesa_glsl_error(&loc
, state
, "unsized array `%s' definition: "
6403 "only last member of a shader storage block "
6404 "can be defined as unsized array",
6407 var
->data
.from_ssbo_unsized_array
= true;
6409 /* From GLSL ES 3.10 spec, section 4.1.9 "Arrays":
6411 * "If an array is declared as the last member of a shader storage
6412 * block and the size is not specified at compile-time, it is
6413 * sized at run-time. In all other cases, arrays are sized only
6416 if (state
->es_shader
) {
6417 _mesa_glsl_error(&loc
, state
, "unsized array `%s' definition: "
6418 "only last member of a shader storage block "
6419 "can be defined as unsized array",
6425 state
->symbols
->add_variable(var
);
6426 instructions
->push_tail(var
);
6429 if (redeclaring_per_vertex
&& block_type
!= earlier_per_vertex
) {
6430 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec:
6432 * It is also a compilation error ... to redeclare a built-in
6433 * block and then use a member from that built-in block that was
6434 * not included in the redeclaration.
6436 * This appears to be a clarification to the behaviour established
6437 * for gl_PerVertex by GLSL 1.50, therefore we implement this
6438 * behaviour regardless of GLSL version.
6440 * To prevent the shader from using a member that was not included in
6441 * the redeclaration, we disable any ir_variables that are still
6442 * associated with the old declaration of gl_PerVertex (since we've
6443 * already updated all of the variables contained in the new
6444 * gl_PerVertex to point to it).
6446 * As a side effect this will prevent
6447 * validate_intrastage_interface_blocks() from getting confused and
6448 * thinking there are conflicting definitions of gl_PerVertex in the
6451 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
6452 ir_variable
*const var
= node
->as_variable();
6454 var
->get_interface_type() == earlier_per_vertex
&&
6455 var
->data
.mode
== var_mode
) {
6456 if (var
->data
.how_declared
== ir_var_declared_normally
) {
6457 _mesa_glsl_error(&loc
, state
,
6458 "redeclaration of gl_PerVertex cannot "
6459 "follow a redeclaration of `%s'",
6462 state
->symbols
->disable_variable(var
->name
);
6474 ast_tcs_output_layout::hir(exec_list
*instructions
,
6475 struct _mesa_glsl_parse_state
*state
)
6477 YYLTYPE loc
= this->get_location();
6479 /* If any tessellation control output layout declaration preceded this
6480 * one, make sure it was consistent with this one.
6482 if (state
->tcs_output_vertices_specified
&&
6483 state
->out_qualifier
->vertices
!= this->vertices
) {
6484 _mesa_glsl_error(&loc
, state
,
6485 "tessellation control shader output layout does not "
6486 "match previous declaration");
6490 /* If any shader outputs occurred before this declaration and specified an
6491 * array size, make sure the size they specified is consistent with the
6494 unsigned num_vertices
= this->vertices
;
6495 if (state
->tcs_output_size
!= 0 && state
->tcs_output_size
!= num_vertices
) {
6496 _mesa_glsl_error(&loc
, state
,
6497 "this tessellation control shader output layout "
6498 "specifies %u vertices, but a previous output "
6499 "is declared with size %u",
6500 num_vertices
, state
->tcs_output_size
);
6504 state
->tcs_output_vertices_specified
= true;
6506 /* If any shader outputs occurred before this declaration and did not
6507 * specify an array size, their size is determined now.
6509 foreach_in_list (ir_instruction
, node
, instructions
) {
6510 ir_variable
*var
= node
->as_variable();
6511 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_out
)
6514 /* Note: Not all tessellation control shader output are arrays. */
6515 if (!var
->type
->is_unsized_array() || var
->data
.patch
)
6518 if (var
->data
.max_array_access
>= num_vertices
) {
6519 _mesa_glsl_error(&loc
, state
,
6520 "this tessellation control shader output layout "
6521 "specifies %u vertices, but an access to element "
6522 "%u of output `%s' already exists", num_vertices
,
6523 var
->data
.max_array_access
, var
->name
);
6525 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
6535 ast_gs_input_layout::hir(exec_list
*instructions
,
6536 struct _mesa_glsl_parse_state
*state
)
6538 YYLTYPE loc
= this->get_location();
6540 /* If any geometry input layout declaration preceded this one, make sure it
6541 * was consistent with this one.
6543 if (state
->gs_input_prim_type_specified
&&
6544 state
->in_qualifier
->prim_type
!= this->prim_type
) {
6545 _mesa_glsl_error(&loc
, state
,
6546 "geometry shader input layout does not match"
6547 " previous declaration");
6551 /* If any shader inputs occurred before this declaration and specified an
6552 * array size, make sure the size they specified is consistent with the
6555 unsigned num_vertices
= vertices_per_prim(this->prim_type
);
6556 if (state
->gs_input_size
!= 0 && state
->gs_input_size
!= num_vertices
) {
6557 _mesa_glsl_error(&loc
, state
,
6558 "this geometry shader input layout implies %u vertices"
6559 " per primitive, but a previous input is declared"
6560 " with size %u", num_vertices
, state
->gs_input_size
);
6564 state
->gs_input_prim_type_specified
= true;
6566 /* If any shader inputs occurred before this declaration and did not
6567 * specify an array size, their size is determined now.
6569 foreach_in_list(ir_instruction
, node
, instructions
) {
6570 ir_variable
*var
= node
->as_variable();
6571 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_in
)
6574 /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
6578 if (var
->type
->is_unsized_array()) {
6579 if (var
->data
.max_array_access
>= num_vertices
) {
6580 _mesa_glsl_error(&loc
, state
,
6581 "this geometry shader input layout implies %u"
6582 " vertices, but an access to element %u of input"
6583 " `%s' already exists", num_vertices
,
6584 var
->data
.max_array_access
, var
->name
);
6586 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
6597 ast_cs_input_layout::hir(exec_list
*instructions
,
6598 struct _mesa_glsl_parse_state
*state
)
6600 YYLTYPE loc
= this->get_location();
6602 /* If any compute input layout declaration preceded this one, make sure it
6603 * was consistent with this one.
6605 if (state
->cs_input_local_size_specified
) {
6606 for (int i
= 0; i
< 3; i
++) {
6607 if (state
->cs_input_local_size
[i
] != this->local_size
[i
]) {
6608 _mesa_glsl_error(&loc
, state
,
6609 "compute shader input layout does not match"
6610 " previous declaration");
6616 /* From the ARB_compute_shader specification:
6618 * If the local size of the shader in any dimension is greater
6619 * than the maximum size supported by the implementation for that
6620 * dimension, a compile-time error results.
6622 * It is not clear from the spec how the error should be reported if
6623 * the total size of the work group exceeds
6624 * MAX_COMPUTE_WORK_GROUP_INVOCATIONS, but it seems reasonable to
6625 * report it at compile time as well.
6627 GLuint64 total_invocations
= 1;
6628 for (int i
= 0; i
< 3; i
++) {
6629 if (this->local_size
[i
] > state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]) {
6630 _mesa_glsl_error(&loc
, state
,
6631 "local_size_%c exceeds MAX_COMPUTE_WORK_GROUP_SIZE"
6633 state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]);
6636 total_invocations
*= this->local_size
[i
];
6637 if (total_invocations
>
6638 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
) {
6639 _mesa_glsl_error(&loc
, state
,
6640 "product of local_sizes exceeds "
6641 "MAX_COMPUTE_WORK_GROUP_INVOCATIONS (%d)",
6642 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
);
6647 state
->cs_input_local_size_specified
= true;
6648 for (int i
= 0; i
< 3; i
++)
6649 state
->cs_input_local_size
[i
] = this->local_size
[i
];
6651 /* We may now declare the built-in constant gl_WorkGroupSize (see
6652 * builtin_variable_generator::generate_constants() for why we didn't
6653 * declare it earlier).
6655 ir_variable
*var
= new(state
->symbols
)
6656 ir_variable(glsl_type::uvec3_type
, "gl_WorkGroupSize", ir_var_auto
);
6657 var
->data
.how_declared
= ir_var_declared_implicitly
;
6658 var
->data
.read_only
= true;
6659 instructions
->push_tail(var
);
6660 state
->symbols
->add_variable(var
);
6661 ir_constant_data data
;
6662 memset(&data
, 0, sizeof(data
));
6663 for (int i
= 0; i
< 3; i
++)
6664 data
.u
[i
] = this->local_size
[i
];
6665 var
->constant_value
= new(var
) ir_constant(glsl_type::uvec3_type
, &data
);
6666 var
->constant_initializer
=
6667 new(var
) ir_constant(glsl_type::uvec3_type
, &data
);
6668 var
->data
.has_initializer
= true;
6675 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
6676 exec_list
*instructions
)
6678 bool gl_FragColor_assigned
= false;
6679 bool gl_FragData_assigned
= false;
6680 bool user_defined_fs_output_assigned
= false;
6681 ir_variable
*user_defined_fs_output
= NULL
;
6683 /* It would be nice to have proper location information. */
6685 memset(&loc
, 0, sizeof(loc
));
6687 foreach_in_list(ir_instruction
, node
, instructions
) {
6688 ir_variable
*var
= node
->as_variable();
6690 if (!var
|| !var
->data
.assigned
)
6693 if (strcmp(var
->name
, "gl_FragColor") == 0)
6694 gl_FragColor_assigned
= true;
6695 else if (strcmp(var
->name
, "gl_FragData") == 0)
6696 gl_FragData_assigned
= true;
6697 else if (!is_gl_identifier(var
->name
)) {
6698 if (state
->stage
== MESA_SHADER_FRAGMENT
&&
6699 var
->data
.mode
== ir_var_shader_out
) {
6700 user_defined_fs_output_assigned
= true;
6701 user_defined_fs_output
= var
;
6706 /* From the GLSL 1.30 spec:
6708 * "If a shader statically assigns a value to gl_FragColor, it
6709 * may not assign a value to any element of gl_FragData. If a
6710 * shader statically writes a value to any element of
6711 * gl_FragData, it may not assign a value to
6712 * gl_FragColor. That is, a shader may assign values to either
6713 * gl_FragColor or gl_FragData, but not both. Multiple shaders
6714 * linked together must also consistently write just one of
6715 * these variables. Similarly, if user declared output
6716 * variables are in use (statically assigned to), then the
6717 * built-in variables gl_FragColor and gl_FragData may not be
6718 * assigned to. These incorrect usages all generate compile
6721 if (gl_FragColor_assigned
&& gl_FragData_assigned
) {
6722 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
6723 "`gl_FragColor' and `gl_FragData'");
6724 } else if (gl_FragColor_assigned
&& user_defined_fs_output_assigned
) {
6725 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
6726 "`gl_FragColor' and `%s'",
6727 user_defined_fs_output
->name
);
6728 } else if (gl_FragData_assigned
&& user_defined_fs_output_assigned
) {
6729 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
6730 "`gl_FragData' and `%s'",
6731 user_defined_fs_output
->name
);
6737 remove_per_vertex_blocks(exec_list
*instructions
,
6738 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
)
6740 /* Find the gl_PerVertex interface block of the appropriate (in/out) mode,
6741 * if it exists in this shader type.
6743 const glsl_type
*per_vertex
= NULL
;
6745 case ir_var_shader_in
:
6746 if (ir_variable
*gl_in
= state
->symbols
->get_variable("gl_in"))
6747 per_vertex
= gl_in
->get_interface_type();
6749 case ir_var_shader_out
:
6750 if (ir_variable
*gl_Position
=
6751 state
->symbols
->get_variable("gl_Position")) {
6752 per_vertex
= gl_Position
->get_interface_type();
6756 assert(!"Unexpected mode");
6760 /* If we didn't find a built-in gl_PerVertex interface block, then we don't
6761 * need to do anything.
6763 if (per_vertex
== NULL
)
6766 /* If the interface block is used by the shader, then we don't need to do
6769 interface_block_usage_visitor
v(mode
, per_vertex
);
6770 v
.run(instructions
);
6771 if (v
.usage_found())
6774 /* Remove any ir_variable declarations that refer to the interface block
6777 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
6778 ir_variable
*const var
= node
->as_variable();
6779 if (var
!= NULL
&& var
->get_interface_type() == per_vertex
&&
6780 var
->data
.mode
== mode
) {
6781 state
->symbols
->disable_variable(var
->name
);