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 "compiler/glsl_types.h"
56 #include "util/hash_table.h"
57 #include "main/mtypes.h"
58 #include "main/macros.h"
59 #include "main/shaderobj.h"
61 #include "ir_builder.h"
62 #include "builtin_functions.h"
64 using namespace ir_builder
;
67 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
68 exec_list
*instructions
);
70 verify_subroutine_associated_funcs(struct _mesa_glsl_parse_state
*state
);
73 remove_per_vertex_blocks(exec_list
*instructions
,
74 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
);
77 * Visitor class that finds the first instance of any write-only variable that
78 * is ever read, if any
80 class read_from_write_only_variable_visitor
: public ir_hierarchical_visitor
83 read_from_write_only_variable_visitor() : found(NULL
)
87 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
89 if (this->in_assignee
)
90 return visit_continue
;
92 ir_variable
*var
= ir
->variable_referenced();
93 /* We can have memory_write_only set on both images and buffer variables,
94 * but in the former there is a distinction between reads from
95 * the variable itself (write_only) and from the memory they point to
96 * (memory_write_only), while in the case of buffer variables there is
97 * no such distinction, that is why this check here is limited to
98 * buffer variables alone.
100 if (!var
|| var
->data
.mode
!= ir_var_shader_storage
)
101 return visit_continue
;
103 if (var
->data
.memory_write_only
) {
108 return visit_continue
;
111 ir_variable
*get_variable() {
115 virtual ir_visitor_status
visit_enter(ir_expression
*ir
)
117 /* .length() doesn't actually read anything */
118 if (ir
->operation
== ir_unop_ssbo_unsized_array_length
)
119 return visit_continue_with_parent
;
121 return visit_continue
;
129 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
131 _mesa_glsl_initialize_variables(instructions
, state
);
133 state
->symbols
->separate_function_namespace
= state
->language_version
== 110;
135 state
->current_function
= NULL
;
137 state
->toplevel_ir
= instructions
;
139 state
->gs_input_prim_type_specified
= false;
140 state
->tcs_output_vertices_specified
= false;
141 state
->cs_input_local_size_specified
= false;
143 /* Section 4.2 of the GLSL 1.20 specification states:
144 * "The built-in functions are scoped in a scope outside the global scope
145 * users declare global variables in. That is, a shader's global scope,
146 * available for user-defined functions and global variables, is nested
147 * inside the scope containing the built-in functions."
149 * Since built-in functions like ftransform() access built-in variables,
150 * it follows that those must be in the outer scope as well.
152 * We push scope here to create this nesting effect...but don't pop.
153 * This way, a shader's globals are still in the symbol table for use
156 state
->symbols
->push_scope();
158 foreach_list_typed (ast_node
, ast
, link
, & state
->translation_unit
)
159 ast
->hir(instructions
, state
);
161 verify_subroutine_associated_funcs(state
);
162 detect_recursion_unlinked(state
, instructions
);
163 detect_conflicting_assignments(state
, instructions
);
165 state
->toplevel_ir
= NULL
;
167 /* Move all of the variable declarations to the front of the IR list, and
168 * reverse the order. This has the (intended!) side effect that vertex
169 * shader inputs and fragment shader outputs will appear in the IR in the
170 * same order that they appeared in the shader code. This results in the
171 * locations being assigned in the declared order. Many (arguably buggy)
172 * applications depend on this behavior, and it matches what nearly all
175 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
176 ir_variable
*const var
= node
->as_variable();
182 instructions
->push_head(var
);
185 /* Figure out if gl_FragCoord is actually used in fragment shader */
186 ir_variable
*const var
= state
->symbols
->get_variable("gl_FragCoord");
188 state
->fs_uses_gl_fragcoord
= var
->data
.used
;
190 /* From section 7.1 (Built-In Language Variables) of the GLSL 4.10 spec:
192 * If multiple shaders using members of a built-in block belonging to
193 * the same interface are linked together in the same program, they
194 * must all redeclare the built-in block in the same way, as described
195 * in section 4.3.7 "Interface Blocks" for interface block matching, or
196 * a link error will result.
198 * The phrase "using members of a built-in block" implies that if two
199 * shaders are linked together and one of them *does not use* any members
200 * of the built-in block, then that shader does not need to have a matching
201 * redeclaration of the built-in block.
203 * This appears to be a clarification to the behaviour established for
204 * gl_PerVertex by GLSL 1.50, therefore implement it regardless of GLSL
207 * The definition of "interface" in section 4.3.7 that applies here is as
210 * The boundary between adjacent programmable pipeline stages: This
211 * spans all the outputs in all compilation units of the first stage
212 * and all the inputs in all compilation units of the second stage.
214 * Therefore this rule applies to both inter- and intra-stage linking.
216 * The easiest way to implement this is to check whether the shader uses
217 * gl_PerVertex right after ast-to-ir conversion, and if it doesn't, simply
218 * remove all the relevant variable declaration from the IR, so that the
219 * linker won't see them and complain about mismatches.
221 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_in
);
222 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_out
);
224 /* Check that we don't have reads from write-only variables */
225 read_from_write_only_variable_visitor v
;
227 ir_variable
*error_var
= v
.get_variable();
229 /* It would be nice to have proper location information, but for that
230 * we would need to check this as we process each kind of AST node
233 memset(&loc
, 0, sizeof(loc
));
234 _mesa_glsl_error(&loc
, state
, "Read from write-only variable `%s'",
240 static ir_expression_operation
241 get_implicit_conversion_operation(const glsl_type
*to
, const glsl_type
*from
,
242 struct _mesa_glsl_parse_state
*state
)
244 switch (to
->base_type
) {
245 case GLSL_TYPE_FLOAT
:
246 switch (from
->base_type
) {
247 case GLSL_TYPE_INT
: return ir_unop_i2f
;
248 case GLSL_TYPE_UINT
: return ir_unop_u2f
;
249 default: return (ir_expression_operation
)0;
253 if (!state
->has_implicit_uint_to_int_conversion())
254 return (ir_expression_operation
)0;
255 switch (from
->base_type
) {
256 case GLSL_TYPE_INT
: return ir_unop_i2u
;
257 default: return (ir_expression_operation
)0;
260 case GLSL_TYPE_DOUBLE
:
261 if (!state
->has_double())
262 return (ir_expression_operation
)0;
263 switch (from
->base_type
) {
264 case GLSL_TYPE_INT
: return ir_unop_i2d
;
265 case GLSL_TYPE_UINT
: return ir_unop_u2d
;
266 case GLSL_TYPE_FLOAT
: return ir_unop_f2d
;
267 case GLSL_TYPE_INT64
: return ir_unop_i642d
;
268 case GLSL_TYPE_UINT64
: return ir_unop_u642d
;
269 default: return (ir_expression_operation
)0;
272 case GLSL_TYPE_UINT64
:
273 if (!state
->has_int64())
274 return (ir_expression_operation
)0;
275 switch (from
->base_type
) {
276 case GLSL_TYPE_INT
: return ir_unop_i2u64
;
277 case GLSL_TYPE_UINT
: return ir_unop_u2u64
;
278 case GLSL_TYPE_INT64
: return ir_unop_i642u64
;
279 default: return (ir_expression_operation
)0;
282 case GLSL_TYPE_INT64
:
283 if (!state
->has_int64())
284 return (ir_expression_operation
)0;
285 switch (from
->base_type
) {
286 case GLSL_TYPE_INT
: return ir_unop_i2i64
;
287 default: return (ir_expression_operation
)0;
290 default: return (ir_expression_operation
)0;
296 * If a conversion is available, convert one operand to a different type
298 * The \c from \c ir_rvalue is converted "in place".
300 * \param to Type that the operand it to be converted to
301 * \param from Operand that is being converted
302 * \param state GLSL compiler state
305 * If a conversion is possible (or unnecessary), \c true is returned.
306 * Otherwise \c false is returned.
309 apply_implicit_conversion(const glsl_type
*to
, ir_rvalue
* &from
,
310 struct _mesa_glsl_parse_state
*state
)
313 if (to
->base_type
== from
->type
->base_type
)
316 /* Prior to GLSL 1.20, there are no implicit conversions */
317 if (!state
->has_implicit_conversions())
320 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
322 * "There are no implicit array or structure conversions. For
323 * example, an array of int cannot be implicitly converted to an
326 if (!to
->is_numeric() || !from
->type
->is_numeric())
329 /* We don't actually want the specific type `to`, we want a type
330 * with the same base type as `to`, but the same vector width as
333 to
= glsl_type::get_instance(to
->base_type
, from
->type
->vector_elements
,
334 from
->type
->matrix_columns
);
336 ir_expression_operation op
= get_implicit_conversion_operation(to
, from
->type
, state
);
338 from
= new(ctx
) ir_expression(op
, to
, from
, NULL
);
346 static const struct glsl_type
*
347 arithmetic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
349 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
351 const glsl_type
*type_a
= value_a
->type
;
352 const glsl_type
*type_b
= value_b
->type
;
354 /* From GLSL 1.50 spec, page 56:
356 * "The arithmetic binary operators add (+), subtract (-),
357 * multiply (*), and divide (/) operate on integer and
358 * floating-point scalars, vectors, and matrices."
360 if (!type_a
->is_numeric() || !type_b
->is_numeric()) {
361 _mesa_glsl_error(loc
, state
,
362 "operands to arithmetic operators must be numeric");
363 return glsl_type::error_type
;
367 /* "If one operand is floating-point based and the other is
368 * not, then the conversions from Section 4.1.10 "Implicit
369 * Conversions" are applied to the non-floating-point-based operand."
371 if (!apply_implicit_conversion(type_a
, value_b
, state
)
372 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
373 _mesa_glsl_error(loc
, state
,
374 "could not implicitly convert operands to "
375 "arithmetic operator");
376 return glsl_type::error_type
;
378 type_a
= value_a
->type
;
379 type_b
= value_b
->type
;
381 /* "If the operands are integer types, they must both be signed or
384 * From this rule and the preceeding conversion it can be inferred that
385 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
386 * The is_numeric check above already filtered out the case where either
387 * type is not one of these, so now the base types need only be tested for
390 if (type_a
->base_type
!= type_b
->base_type
) {
391 _mesa_glsl_error(loc
, state
,
392 "base type mismatch for arithmetic operator");
393 return glsl_type::error_type
;
396 /* "All arithmetic binary operators result in the same fundamental type
397 * (signed integer, unsigned integer, or floating-point) as the
398 * operands they operate on, after operand type conversion. After
399 * conversion, the following cases are valid
401 * * The two operands are scalars. In this case the operation is
402 * applied, resulting in a scalar."
404 if (type_a
->is_scalar() && type_b
->is_scalar())
407 /* "* One operand is a scalar, and the other is a vector or matrix.
408 * In this case, the scalar operation is applied independently to each
409 * component of the vector or matrix, resulting in the same size
412 if (type_a
->is_scalar()) {
413 if (!type_b
->is_scalar())
415 } else if (type_b
->is_scalar()) {
419 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
420 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
423 assert(!type_a
->is_scalar());
424 assert(!type_b
->is_scalar());
426 /* "* The two operands are vectors of the same size. In this case, the
427 * operation is done component-wise resulting in the same size
430 if (type_a
->is_vector() && type_b
->is_vector()) {
431 if (type_a
== type_b
) {
434 _mesa_glsl_error(loc
, state
,
435 "vector size mismatch for arithmetic operator");
436 return glsl_type::error_type
;
440 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
441 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
442 * <vector, vector> have been handled. At least one of the operands must
443 * be matrix. Further, since there are no integer matrix types, the base
444 * type of both operands must be float.
446 assert(type_a
->is_matrix() || type_b
->is_matrix());
447 assert(type_a
->is_float() || type_a
->is_double());
448 assert(type_b
->is_float() || type_b
->is_double());
450 /* "* The operator is add (+), subtract (-), or divide (/), and the
451 * operands are matrices with the same number of rows and the same
452 * number of columns. In this case, the operation is done component-
453 * wise resulting in the same size matrix."
454 * * The operator is multiply (*), where both operands are matrices or
455 * one operand is a vector and the other a matrix. A right vector
456 * operand is treated as a column vector and a left vector operand as a
457 * row vector. In all these cases, it is required that the number of
458 * columns of the left operand is equal to the number of rows of the
459 * right operand. Then, the multiply (*) operation does a linear
460 * algebraic multiply, yielding an object that has the same number of
461 * rows as the left operand and the same number of columns as the right
462 * operand. Section 5.10 "Vector and Matrix Operations" explains in
463 * more detail how vectors and matrices are operated on."
466 if (type_a
== type_b
)
469 const glsl_type
*type
= glsl_type::get_mul_type(type_a
, type_b
);
471 if (type
== glsl_type::error_type
) {
472 _mesa_glsl_error(loc
, state
,
473 "size mismatch for matrix multiplication");
480 /* "All other cases are illegal."
482 _mesa_glsl_error(loc
, state
, "type mismatch");
483 return glsl_type::error_type
;
487 static const struct glsl_type
*
488 unary_arithmetic_result_type(const struct glsl_type
*type
,
489 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
491 /* From GLSL 1.50 spec, page 57:
493 * "The arithmetic unary operators negate (-), post- and pre-increment
494 * and decrement (-- and ++) operate on integer or floating-point
495 * values (including vectors and matrices). All unary operators work
496 * component-wise on their operands. These result with the same type
499 if (!type
->is_numeric()) {
500 _mesa_glsl_error(loc
, state
,
501 "operands to arithmetic operators must be numeric");
502 return glsl_type::error_type
;
509 * \brief Return the result type of a bit-logic operation.
511 * If the given types to the bit-logic operator are invalid, return
512 * glsl_type::error_type.
514 * \param value_a LHS of bit-logic op
515 * \param value_b RHS of bit-logic op
517 static const struct glsl_type
*
518 bit_logic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
520 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
522 const glsl_type
*type_a
= value_a
->type
;
523 const glsl_type
*type_b
= value_b
->type
;
525 if (!state
->check_bitwise_operations_allowed(loc
)) {
526 return glsl_type::error_type
;
529 /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
531 * "The bitwise operators and (&), exclusive-or (^), and inclusive-or
532 * (|). The operands must be of type signed or unsigned integers or
535 if (!type_a
->is_integer_32_64()) {
536 _mesa_glsl_error(loc
, state
, "LHS of `%s' must be an integer",
537 ast_expression::operator_string(op
));
538 return glsl_type::error_type
;
540 if (!type_b
->is_integer_32_64()) {
541 _mesa_glsl_error(loc
, state
, "RHS of `%s' must be an integer",
542 ast_expression::operator_string(op
));
543 return glsl_type::error_type
;
546 /* Prior to GLSL 4.0 / GL_ARB_gpu_shader5, implicit conversions didn't
547 * make sense for bitwise operations, as they don't operate on floats.
549 * GLSL 4.0 added implicit int -> uint conversions, which are relevant
550 * here. It wasn't clear whether or not we should apply them to bitwise
551 * operations. However, Khronos has decided that they should in future
552 * language revisions. Applications also rely on this behavior. We opt
553 * to apply them in general, but issue a portability warning.
555 * See https://www.khronos.org/bugzilla/show_bug.cgi?id=1405
557 if (type_a
->base_type
!= type_b
->base_type
) {
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 "
563 ast_expression::operator_string(op
));
564 return glsl_type::error_type
;
566 _mesa_glsl_warning(loc
, state
,
567 "some implementations may not support implicit "
568 "int -> uint conversions for `%s' operators; "
569 "consider casting explicitly for portability",
570 ast_expression::operator_string(op
));
572 type_a
= value_a
->type
;
573 type_b
= value_b
->type
;
576 /* "The fundamental types of the operands (signed or unsigned) must
579 if (type_a
->base_type
!= type_b
->base_type
) {
580 _mesa_glsl_error(loc
, state
, "operands of `%s' must have the same "
581 "base type", ast_expression::operator_string(op
));
582 return glsl_type::error_type
;
585 /* "The operands cannot be vectors of differing size." */
586 if (type_a
->is_vector() &&
587 type_b
->is_vector() &&
588 type_a
->vector_elements
!= type_b
->vector_elements
) {
589 _mesa_glsl_error(loc
, state
, "operands of `%s' cannot be vectors of "
590 "different sizes", ast_expression::operator_string(op
));
591 return glsl_type::error_type
;
594 /* "If one operand is a scalar and the other a vector, the scalar is
595 * applied component-wise to the vector, resulting in the same type as
596 * the vector. The fundamental types of the operands [...] will be the
597 * resulting fundamental type."
599 if (type_a
->is_scalar())
605 static const struct glsl_type
*
606 modulus_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
607 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
609 const glsl_type
*type_a
= value_a
->type
;
610 const glsl_type
*type_b
= value_b
->type
;
612 if (!state
->EXT_gpu_shader4_enable
&&
613 !state
->check_version(130, 300, loc
, "operator '%%' is reserved")) {
614 return glsl_type::error_type
;
617 /* Section 5.9 (Expressions) of the GLSL 4.00 specification says:
619 * "The operator modulus (%) operates on signed or unsigned integers or
622 if (!type_a
->is_integer_32_64()) {
623 _mesa_glsl_error(loc
, state
, "LHS of operator %% must be an integer");
624 return glsl_type::error_type
;
626 if (!type_b
->is_integer_32_64()) {
627 _mesa_glsl_error(loc
, state
, "RHS of operator %% must be an integer");
628 return glsl_type::error_type
;
631 /* "If the fundamental types in the operands do not match, then the
632 * conversions from section 4.1.10 "Implicit Conversions" are applied
633 * to create matching types."
635 * Note that GLSL 4.00 (and GL_ARB_gpu_shader5) introduced implicit
636 * int -> uint conversion rules. Prior to that, there were no implicit
637 * conversions. So it's harmless to apply them universally - no implicit
638 * conversions will exist. If the types don't match, we'll receive false,
639 * and raise an error, satisfying the GLSL 1.50 spec, page 56:
641 * "The operand types must both be signed or unsigned."
643 if (!apply_implicit_conversion(type_a
, value_b
, state
) &&
644 !apply_implicit_conversion(type_b
, value_a
, state
)) {
645 _mesa_glsl_error(loc
, state
,
646 "could not implicitly convert operands to "
647 "modulus (%%) operator");
648 return glsl_type::error_type
;
650 type_a
= value_a
->type
;
651 type_b
= value_b
->type
;
653 /* "The operands cannot be vectors of differing size. If one operand is
654 * a scalar and the other vector, then the scalar is applied component-
655 * wise to the vector, resulting in the same type as the vector. If both
656 * are vectors of the same size, the result is computed component-wise."
658 if (type_a
->is_vector()) {
659 if (!type_b
->is_vector()
660 || (type_a
->vector_elements
== type_b
->vector_elements
))
665 /* "The operator modulus (%) is not defined for any other data types
666 * (non-integer types)."
668 _mesa_glsl_error(loc
, state
, "type mismatch");
669 return glsl_type::error_type
;
673 static const struct glsl_type
*
674 relational_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
675 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
677 const glsl_type
*type_a
= value_a
->type
;
678 const glsl_type
*type_b
= value_b
->type
;
680 /* From GLSL 1.50 spec, page 56:
681 * "The relational operators greater than (>), less than (<), greater
682 * than or equal (>=), and less than or equal (<=) operate only on
683 * scalar integer and scalar floating-point expressions."
685 if (!type_a
->is_numeric()
686 || !type_b
->is_numeric()
687 || !type_a
->is_scalar()
688 || !type_b
->is_scalar()) {
689 _mesa_glsl_error(loc
, state
,
690 "operands to relational operators must be scalar and "
692 return glsl_type::error_type
;
695 /* "Either the operands' types must match, or the conversions from
696 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
697 * operand, after which the types must match."
699 if (!apply_implicit_conversion(type_a
, value_b
, state
)
700 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
701 _mesa_glsl_error(loc
, state
,
702 "could not implicitly convert operands to "
703 "relational operator");
704 return glsl_type::error_type
;
706 type_a
= value_a
->type
;
707 type_b
= value_b
->type
;
709 if (type_a
->base_type
!= type_b
->base_type
) {
710 _mesa_glsl_error(loc
, state
, "base type mismatch");
711 return glsl_type::error_type
;
714 /* "The result is scalar Boolean."
716 return glsl_type::bool_type
;
720 * \brief Return the result type of a bit-shift operation.
722 * If the given types to the bit-shift operator are invalid, return
723 * glsl_type::error_type.
725 * \param type_a Type of LHS of bit-shift op
726 * \param type_b Type of RHS of bit-shift op
728 static const struct glsl_type
*
729 shift_result_type(const struct glsl_type
*type_a
,
730 const struct glsl_type
*type_b
,
732 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
734 if (!state
->check_bitwise_operations_allowed(loc
)) {
735 return glsl_type::error_type
;
738 /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
740 * "The shift operators (<<) and (>>). For both operators, the operands
741 * must be signed or unsigned integers or integer vectors. One operand
742 * can be signed while the other is unsigned."
744 if (!type_a
->is_integer_32_64()) {
745 _mesa_glsl_error(loc
, state
, "LHS of operator %s must be an integer or "
746 "integer vector", ast_expression::operator_string(op
));
747 return glsl_type::error_type
;
750 if (!type_b
->is_integer_32()) {
751 _mesa_glsl_error(loc
, state
, "RHS of operator %s must be an integer or "
752 "integer vector", ast_expression::operator_string(op
));
753 return glsl_type::error_type
;
756 /* "If the first operand is a scalar, the second operand has to be
759 if (type_a
->is_scalar() && !type_b
->is_scalar()) {
760 _mesa_glsl_error(loc
, state
, "if the first operand of %s is scalar, the "
761 "second must be scalar as well",
762 ast_expression::operator_string(op
));
763 return glsl_type::error_type
;
766 /* If both operands are vectors, check that they have same number of
769 if (type_a
->is_vector() &&
770 type_b
->is_vector() &&
771 type_a
->vector_elements
!= type_b
->vector_elements
) {
772 _mesa_glsl_error(loc
, state
, "vector operands to operator %s must "
773 "have same number of elements",
774 ast_expression::operator_string(op
));
775 return glsl_type::error_type
;
778 /* "In all cases, the resulting type will be the same type as the left
785 * Returns the innermost array index expression in an rvalue tree.
786 * This is the largest indexing level -- if an array of blocks, then
787 * it is the block index rather than an indexing expression for an
788 * array-typed member of an array of blocks.
791 find_innermost_array_index(ir_rvalue
*rv
)
793 ir_dereference_array
*last
= NULL
;
795 if (rv
->as_dereference_array()) {
796 last
= rv
->as_dereference_array();
798 } else if (rv
->as_dereference_record())
799 rv
= rv
->as_dereference_record()->record
;
800 else if (rv
->as_swizzle())
801 rv
= rv
->as_swizzle()->val
;
807 return last
->array_index
;
813 * Validates that a value can be assigned to a location with a specified type
815 * Validates that \c rhs can be assigned to some location. If the types are
816 * not an exact match but an automatic conversion is possible, \c rhs will be
820 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
821 * Otherwise the actual RHS to be assigned will be returned. This may be
822 * \c rhs, or it may be \c rhs after some type conversion.
825 * In addition to being used for assignments, this function is used to
826 * type-check return values.
829 validate_assignment(struct _mesa_glsl_parse_state
*state
,
830 YYLTYPE loc
, ir_rvalue
*lhs
,
831 ir_rvalue
*rhs
, bool is_initializer
)
833 /* If there is already some error in the RHS, just return it. Anything
834 * else will lead to an avalanche of error message back to the user.
836 if (rhs
->type
->is_error())
839 /* In the Tessellation Control Shader:
840 * If a per-vertex output variable is used as an l-value, it is an error
841 * if the expression indicating the vertex number is not the identifier
844 if (state
->stage
== MESA_SHADER_TESS_CTRL
&& !lhs
->type
->is_error()) {
845 ir_variable
*var
= lhs
->variable_referenced();
846 if (var
&& var
->data
.mode
== ir_var_shader_out
&& !var
->data
.patch
) {
847 ir_rvalue
*index
= find_innermost_array_index(lhs
);
848 ir_variable
*index_var
= index
? index
->variable_referenced() : NULL
;
849 if (!index_var
|| strcmp(index_var
->name
, "gl_InvocationID") != 0) {
850 _mesa_glsl_error(&loc
, state
,
851 "Tessellation control shader outputs can only "
852 "be indexed by gl_InvocationID");
858 /* If the types are identical, the assignment can trivially proceed.
860 if (rhs
->type
== lhs
->type
)
863 /* If the array element types are the same and the LHS is unsized,
864 * the assignment is okay for initializers embedded in variable
867 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
868 * is handled by ir_dereference::is_lvalue.
870 const glsl_type
*lhs_t
= lhs
->type
;
871 const glsl_type
*rhs_t
= rhs
->type
;
872 bool unsized_array
= false;
873 while(lhs_t
->is_array()) {
875 break; /* the rest of the inner arrays match so break out early */
876 if (!rhs_t
->is_array()) {
877 unsized_array
= false;
878 break; /* number of dimensions mismatch */
880 if (lhs_t
->length
== rhs_t
->length
) {
881 lhs_t
= lhs_t
->fields
.array
;
882 rhs_t
= rhs_t
->fields
.array
;
884 } else if (lhs_t
->is_unsized_array()) {
885 unsized_array
= true;
887 unsized_array
= false;
888 break; /* sized array mismatch */
890 lhs_t
= lhs_t
->fields
.array
;
891 rhs_t
= rhs_t
->fields
.array
;
894 if (is_initializer
) {
895 if (rhs
->type
->get_scalar_type() == lhs
->type
->get_scalar_type())
898 _mesa_glsl_error(&loc
, state
,
899 "implicitly sized arrays cannot be assigned");
904 /* Check for implicit conversion in GLSL 1.20 */
905 if (apply_implicit_conversion(lhs
->type
, rhs
, state
)) {
906 if (rhs
->type
== lhs
->type
)
910 _mesa_glsl_error(&loc
, state
,
911 "%s of type %s cannot be assigned to "
912 "variable of type %s",
913 is_initializer
? "initializer" : "value",
914 rhs
->type
->name
, lhs
->type
->name
);
920 mark_whole_array_access(ir_rvalue
*access
)
922 ir_dereference_variable
*deref
= access
->as_dereference_variable();
924 if (deref
&& deref
->var
) {
925 deref
->var
->data
.max_array_access
= deref
->type
->length
- 1;
930 do_assignment(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
931 const char *non_lvalue_description
,
932 ir_rvalue
*lhs
, ir_rvalue
*rhs
,
933 ir_rvalue
**out_rvalue
, bool needs_rvalue
,
938 bool error_emitted
= (lhs
->type
->is_error() || rhs
->type
->is_error());
940 ir_variable
*lhs_var
= lhs
->variable_referenced();
942 lhs_var
->data
.assigned
= true;
944 if (!error_emitted
) {
945 if (non_lvalue_description
!= NULL
) {
946 _mesa_glsl_error(&lhs_loc
, state
,
948 non_lvalue_description
);
949 error_emitted
= true;
950 } else if (lhs_var
!= NULL
&& (lhs_var
->data
.read_only
||
951 (lhs_var
->data
.mode
== ir_var_shader_storage
&&
952 lhs_var
->data
.memory_read_only
))) {
953 /* We can have memory_read_only set on both images and buffer variables,
954 * but in the former there is a distinction between assignments to
955 * the variable itself (read_only) and to the memory they point to
956 * (memory_read_only), while in the case of buffer variables there is
957 * no such distinction, that is why this check here is limited to
958 * buffer variables alone.
960 _mesa_glsl_error(&lhs_loc
, state
,
961 "assignment to read-only variable '%s'",
963 error_emitted
= true;
964 } else if (lhs
->type
->is_array() &&
965 !state
->check_version(120, 300, &lhs_loc
,
966 "whole array assignment forbidden")) {
967 /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
969 * "Other binary or unary expressions, non-dereferenced
970 * arrays, function names, swizzles with repeated fields,
971 * and constants cannot be l-values."
973 * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00.
975 error_emitted
= true;
976 } else if (!lhs
->is_lvalue(state
)) {
977 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
978 error_emitted
= true;
983 validate_assignment(state
, lhs_loc
, lhs
, rhs
, is_initializer
);
984 if (new_rhs
!= NULL
) {
987 /* If the LHS array was not declared with a size, it takes it size from
988 * the RHS. If the LHS is an l-value and a whole array, it must be a
989 * dereference of a variable. Any other case would require that the LHS
990 * is either not an l-value or not a whole array.
992 if (lhs
->type
->is_unsized_array()) {
993 ir_dereference
*const d
= lhs
->as_dereference();
997 ir_variable
*const var
= d
->variable_referenced();
1001 if (var
->data
.max_array_access
>= rhs
->type
->array_size()) {
1002 /* FINISHME: This should actually log the location of the RHS. */
1003 _mesa_glsl_error(& lhs_loc
, state
, "array size must be > %u due to "
1005 var
->data
.max_array_access
);
1008 var
->type
= glsl_type::get_array_instance(lhs
->type
->fields
.array
,
1009 rhs
->type
->array_size());
1010 d
->type
= var
->type
;
1012 if (lhs
->type
->is_array()) {
1013 mark_whole_array_access(rhs
);
1014 mark_whole_array_access(lhs
);
1017 error_emitted
= true;
1020 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
1021 * but not post_inc) need the converted assigned value as an rvalue
1022 * to handle things like:
1028 if (!error_emitted
) {
1029 ir_variable
*var
= new(ctx
) ir_variable(rhs
->type
, "assignment_tmp",
1031 instructions
->push_tail(var
);
1032 instructions
->push_tail(assign(var
, rhs
));
1034 ir_dereference_variable
*deref_var
=
1035 new(ctx
) ir_dereference_variable(var
);
1036 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, deref_var
));
1037 rvalue
= new(ctx
) ir_dereference_variable(var
);
1039 rvalue
= ir_rvalue::error_value(ctx
);
1041 *out_rvalue
= rvalue
;
1044 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, rhs
));
1048 return error_emitted
;
1052 get_lvalue_copy(exec_list
*instructions
, ir_rvalue
*lvalue
)
1054 void *ctx
= ralloc_parent(lvalue
);
1057 var
= new(ctx
) ir_variable(lvalue
->type
, "_post_incdec_tmp",
1059 instructions
->push_tail(var
);
1061 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
1064 return new(ctx
) ir_dereference_variable(var
);
1069 ast_node::hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
1071 (void) instructions
;
1078 ast_node::has_sequence_subexpression() const
1084 ast_node::set_is_lhs(bool /* new_value */)
1089 ast_function_expression::hir_no_rvalue(exec_list
*instructions
,
1090 struct _mesa_glsl_parse_state
*state
)
1092 (void)hir(instructions
, state
);
1096 ast_aggregate_initializer::hir_no_rvalue(exec_list
*instructions
,
1097 struct _mesa_glsl_parse_state
*state
)
1099 (void)hir(instructions
, state
);
1103 do_comparison(void *mem_ctx
, int operation
, ir_rvalue
*op0
, ir_rvalue
*op1
)
1106 ir_rvalue
*cmp
= NULL
;
1108 if (operation
== ir_binop_all_equal
)
1109 join_op
= ir_binop_logic_and
;
1111 join_op
= ir_binop_logic_or
;
1113 switch (op0
->type
->base_type
) {
1114 case GLSL_TYPE_FLOAT
:
1115 case GLSL_TYPE_FLOAT16
:
1116 case GLSL_TYPE_UINT
:
1118 case GLSL_TYPE_BOOL
:
1119 case GLSL_TYPE_DOUBLE
:
1120 case GLSL_TYPE_UINT64
:
1121 case GLSL_TYPE_INT64
:
1122 case GLSL_TYPE_UINT16
:
1123 case GLSL_TYPE_INT16
:
1124 case GLSL_TYPE_UINT8
:
1125 case GLSL_TYPE_INT8
:
1126 return new(mem_ctx
) ir_expression(operation
, op0
, op1
);
1128 case GLSL_TYPE_ARRAY
: {
1129 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
1130 ir_rvalue
*e0
, *e1
, *result
;
1132 e0
= new(mem_ctx
) ir_dereference_array(op0
->clone(mem_ctx
, NULL
),
1133 new(mem_ctx
) ir_constant(i
));
1134 e1
= new(mem_ctx
) ir_dereference_array(op1
->clone(mem_ctx
, NULL
),
1135 new(mem_ctx
) ir_constant(i
));
1136 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
1139 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
1145 mark_whole_array_access(op0
);
1146 mark_whole_array_access(op1
);
1150 case GLSL_TYPE_STRUCT
: {
1151 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
1152 ir_rvalue
*e0
, *e1
, *result
;
1153 const char *field_name
= op0
->type
->fields
.structure
[i
].name
;
1155 e0
= new(mem_ctx
) ir_dereference_record(op0
->clone(mem_ctx
, NULL
),
1157 e1
= new(mem_ctx
) ir_dereference_record(op1
->clone(mem_ctx
, NULL
),
1159 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
1162 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
1170 case GLSL_TYPE_ERROR
:
1171 case GLSL_TYPE_VOID
:
1172 case GLSL_TYPE_SAMPLER
:
1173 case GLSL_TYPE_IMAGE
:
1174 case GLSL_TYPE_INTERFACE
:
1175 case GLSL_TYPE_ATOMIC_UINT
:
1176 case GLSL_TYPE_SUBROUTINE
:
1177 case GLSL_TYPE_FUNCTION
:
1178 /* I assume a comparison of a struct containing a sampler just
1179 * ignores the sampler present in the type.
1185 cmp
= new(mem_ctx
) ir_constant(true);
1190 /* For logical operations, we want to ensure that the operands are
1191 * scalar booleans. If it isn't, emit an error and return a constant
1192 * boolean to avoid triggering cascading error messages.
1195 get_scalar_boolean_operand(exec_list
*instructions
,
1196 struct _mesa_glsl_parse_state
*state
,
1197 ast_expression
*parent_expr
,
1199 const char *operand_name
,
1200 bool *error_emitted
)
1202 ast_expression
*expr
= parent_expr
->subexpressions
[operand
];
1204 ir_rvalue
*val
= expr
->hir(instructions
, state
);
1206 if (val
->type
->is_boolean() && val
->type
->is_scalar())
1209 if (!*error_emitted
) {
1210 YYLTYPE loc
= expr
->get_location();
1211 _mesa_glsl_error(&loc
, state
, "%s of `%s' must be scalar boolean",
1213 parent_expr
->operator_string(parent_expr
->oper
));
1214 *error_emitted
= true;
1217 return new(ctx
) ir_constant(true);
1221 * If name refers to a builtin array whose maximum allowed size is less than
1222 * size, report an error and return true. Otherwise return false.
1225 check_builtin_array_max_size(const char *name
, unsigned size
,
1226 YYLTYPE loc
, struct _mesa_glsl_parse_state
*state
)
1228 if ((strcmp("gl_TexCoord", name
) == 0)
1229 && (size
> state
->Const
.MaxTextureCoords
)) {
1230 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1232 * "The size [of gl_TexCoord] can be at most
1233 * gl_MaxTextureCoords."
1235 _mesa_glsl_error(&loc
, state
, "`gl_TexCoord' array size cannot "
1236 "be larger than gl_MaxTextureCoords (%u)",
1237 state
->Const
.MaxTextureCoords
);
1238 } else if (strcmp("gl_ClipDistance", name
) == 0) {
1239 state
->clip_dist_size
= size
;
1240 if (size
+ state
->cull_dist_size
> state
->Const
.MaxClipPlanes
) {
1241 /* From section 7.1 (Vertex Shader Special Variables) of the
1244 * "The gl_ClipDistance array is predeclared as unsized and
1245 * must be sized by the shader either redeclaring it with a
1246 * size or indexing it only with integral constant
1247 * expressions. ... The size can be at most
1248 * gl_MaxClipDistances."
1250 _mesa_glsl_error(&loc
, state
, "`gl_ClipDistance' array size cannot "
1251 "be larger than gl_MaxClipDistances (%u)",
1252 state
->Const
.MaxClipPlanes
);
1254 } else if (strcmp("gl_CullDistance", name
) == 0) {
1255 state
->cull_dist_size
= size
;
1256 if (size
+ state
->clip_dist_size
> state
->Const
.MaxClipPlanes
) {
1257 /* From the ARB_cull_distance spec:
1259 * "The gl_CullDistance array is predeclared as unsized and
1260 * must be sized by the shader either redeclaring it with
1261 * a size or indexing it only with integral constant
1262 * expressions. The size determines the number and set of
1263 * enabled cull distances and can be at most
1264 * gl_MaxCullDistances."
1266 _mesa_glsl_error(&loc
, state
, "`gl_CullDistance' array size cannot "
1267 "be larger than gl_MaxCullDistances (%u)",
1268 state
->Const
.MaxClipPlanes
);
1274 * Create the constant 1, of a which is appropriate for incrementing and
1275 * decrementing values of the given GLSL type. For example, if type is vec4,
1276 * this creates a constant value of 1.0 having type float.
1278 * If the given type is invalid for increment and decrement operators, return
1279 * a floating point 1--the error will be detected later.
1282 constant_one_for_inc_dec(void *ctx
, const glsl_type
*type
)
1284 switch (type
->base_type
) {
1285 case GLSL_TYPE_UINT
:
1286 return new(ctx
) ir_constant((unsigned) 1);
1288 return new(ctx
) ir_constant(1);
1289 case GLSL_TYPE_UINT64
:
1290 return new(ctx
) ir_constant((uint64_t) 1);
1291 case GLSL_TYPE_INT64
:
1292 return new(ctx
) ir_constant((int64_t) 1);
1294 case GLSL_TYPE_FLOAT
:
1295 return new(ctx
) ir_constant(1.0f
);
1300 ast_expression::hir(exec_list
*instructions
,
1301 struct _mesa_glsl_parse_state
*state
)
1303 return do_hir(instructions
, state
, true);
1307 ast_expression::hir_no_rvalue(exec_list
*instructions
,
1308 struct _mesa_glsl_parse_state
*state
)
1310 do_hir(instructions
, state
, false);
1314 ast_expression::set_is_lhs(bool new_value
)
1316 /* is_lhs is tracked only to print "variable used uninitialized" warnings,
1317 * if we lack an identifier we can just skip it.
1319 if (this->primary_expression
.identifier
== NULL
)
1322 this->is_lhs
= new_value
;
1324 /* We need to go through the subexpressions tree to cover cases like
1325 * ast_field_selection
1327 if (this->subexpressions
[0] != NULL
)
1328 this->subexpressions
[0]->set_is_lhs(new_value
);
1332 ast_expression::do_hir(exec_list
*instructions
,
1333 struct _mesa_glsl_parse_state
*state
,
1337 static const int operations
[AST_NUM_OPERATORS
] = {
1338 -1, /* ast_assign doesn't convert to ir_expression. */
1339 -1, /* ast_plus doesn't convert to ir_expression. */
1349 ir_binop_less
, /* This is correct. See the ast_greater case below. */
1350 ir_binop_gequal
, /* This is correct. See the ast_lequal case below. */
1353 ir_binop_any_nequal
,
1363 /* Note: The following block of expression types actually convert
1364 * to multiple IR instructions.
1366 ir_binop_mul
, /* ast_mul_assign */
1367 ir_binop_div
, /* ast_div_assign */
1368 ir_binop_mod
, /* ast_mod_assign */
1369 ir_binop_add
, /* ast_add_assign */
1370 ir_binop_sub
, /* ast_sub_assign */
1371 ir_binop_lshift
, /* ast_ls_assign */
1372 ir_binop_rshift
, /* ast_rs_assign */
1373 ir_binop_bit_and
, /* ast_and_assign */
1374 ir_binop_bit_xor
, /* ast_xor_assign */
1375 ir_binop_bit_or
, /* ast_or_assign */
1377 -1, /* ast_conditional doesn't convert to ir_expression. */
1378 ir_binop_add
, /* ast_pre_inc. */
1379 ir_binop_sub
, /* ast_pre_dec. */
1380 ir_binop_add
, /* ast_post_inc. */
1381 ir_binop_sub
, /* ast_post_dec. */
1382 -1, /* ast_field_selection doesn't conv to ir_expression. */
1383 -1, /* ast_array_index doesn't convert to ir_expression. */
1384 -1, /* ast_function_call doesn't conv to ir_expression. */
1385 -1, /* ast_identifier doesn't convert to ir_expression. */
1386 -1, /* ast_int_constant doesn't convert to ir_expression. */
1387 -1, /* ast_uint_constant doesn't conv to ir_expression. */
1388 -1, /* ast_float_constant doesn't conv to ir_expression. */
1389 -1, /* ast_bool_constant doesn't conv to ir_expression. */
1390 -1, /* ast_sequence doesn't convert to ir_expression. */
1391 -1, /* ast_aggregate shouldn't ever even get here. */
1393 ir_rvalue
*result
= NULL
;
1395 const struct glsl_type
*type
, *orig_type
;
1396 bool error_emitted
= false;
1399 loc
= this->get_location();
1401 switch (this->oper
) {
1403 unreachable("ast_aggregate: Should never get here.");
1406 this->subexpressions
[0]->set_is_lhs(true);
1407 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1408 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1411 do_assignment(instructions
, state
,
1412 this->subexpressions
[0]->non_lvalue_description
,
1413 op
[0], op
[1], &result
, needs_rvalue
, false,
1414 this->subexpressions
[0]->get_location());
1419 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1421 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1423 error_emitted
= type
->is_error();
1429 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1431 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1433 error_emitted
= type
->is_error();
1435 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1443 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1444 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1446 type
= arithmetic_result_type(op
[0], op
[1],
1447 (this->oper
== ast_mul
),
1449 error_emitted
= type
->is_error();
1451 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1456 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1457 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1459 type
= modulus_result_type(op
[0], op
[1], state
, &loc
);
1461 assert(operations
[this->oper
] == ir_binop_mod
);
1463 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1465 error_emitted
= type
->is_error();
1470 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1471 error_emitted
= true;
1474 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1475 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1476 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1478 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1480 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1487 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1488 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1490 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
1492 /* The relational operators must either generate an error or result
1493 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1495 assert(type
->is_error()
1496 || (type
->is_boolean() && type
->is_scalar()));
1498 /* Like NIR, GLSL IR does not have opcodes for > or <=. Instead, swap
1499 * the arguments and use < or >=.
1501 if (this->oper
== ast_greater
|| this->oper
== ast_lequal
) {
1502 ir_rvalue
*const tmp
= op
[0];
1507 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1509 error_emitted
= type
->is_error();
1514 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1515 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1517 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1519 * "The equality operators equal (==), and not equal (!=)
1520 * operate on all types. They result in a scalar Boolean. If
1521 * the operand types do not match, then there must be a
1522 * conversion from Section 4.1.10 "Implicit Conversions"
1523 * applied to one operand that can make them match, in which
1524 * case this conversion is done."
1527 if (op
[0]->type
== glsl_type::void_type
|| op
[1]->type
== glsl_type::void_type
) {
1528 _mesa_glsl_error(& loc
, state
, "`%s': wrong operand types: "
1529 "no operation `%1$s' exists that takes a left-hand "
1530 "operand of type 'void' or a right operand of type "
1531 "'void'", (this->oper
== ast_equal
) ? "==" : "!=");
1532 error_emitted
= true;
1533 } else if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
1534 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
1535 || (op
[0]->type
!= op
[1]->type
)) {
1536 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
1537 "type", (this->oper
== ast_equal
) ? "==" : "!=");
1538 error_emitted
= true;
1539 } else if ((op
[0]->type
->is_array() || op
[1]->type
->is_array()) &&
1540 !state
->check_version(120, 300, &loc
,
1541 "array comparisons forbidden")) {
1542 error_emitted
= true;
1543 } else if ((op
[0]->type
->contains_subroutine() ||
1544 op
[1]->type
->contains_subroutine())) {
1545 _mesa_glsl_error(&loc
, state
, "subroutine comparisons forbidden");
1546 error_emitted
= true;
1547 } else if ((op
[0]->type
->contains_opaque() ||
1548 op
[1]->type
->contains_opaque())) {
1549 _mesa_glsl_error(&loc
, state
, "opaque type comparisons forbidden");
1550 error_emitted
= true;
1553 if (error_emitted
) {
1554 result
= new(ctx
) ir_constant(false);
1556 result
= do_comparison(ctx
, operations
[this->oper
], op
[0], op
[1]);
1557 assert(result
->type
== glsl_type::bool_type
);
1564 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1565 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1566 type
= bit_logic_result_type(op
[0], op
[1], this->oper
, state
, &loc
);
1567 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1569 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1573 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1575 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1576 error_emitted
= true;
1579 if (!op
[0]->type
->is_integer_32_64()) {
1580 _mesa_glsl_error(&loc
, state
, "operand of `~' must be an integer");
1581 error_emitted
= true;
1584 type
= error_emitted
? glsl_type::error_type
: op
[0]->type
;
1585 result
= new(ctx
) ir_expression(ir_unop_bit_not
, type
, op
[0], NULL
);
1588 case ast_logic_and
: {
1589 exec_list rhs_instructions
;
1590 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1591 "LHS", &error_emitted
);
1592 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1593 "RHS", &error_emitted
);
1595 if (rhs_instructions
.is_empty()) {
1596 result
= new(ctx
) ir_expression(ir_binop_logic_and
, op
[0], op
[1]);
1598 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1601 instructions
->push_tail(tmp
);
1603 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1604 instructions
->push_tail(stmt
);
1606 stmt
->then_instructions
.append_list(&rhs_instructions
);
1607 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1608 ir_assignment
*const then_assign
=
1609 new(ctx
) ir_assignment(then_deref
, op
[1]);
1610 stmt
->then_instructions
.push_tail(then_assign
);
1612 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1613 ir_assignment
*const else_assign
=
1614 new(ctx
) ir_assignment(else_deref
, new(ctx
) ir_constant(false));
1615 stmt
->else_instructions
.push_tail(else_assign
);
1617 result
= new(ctx
) ir_dereference_variable(tmp
);
1622 case ast_logic_or
: {
1623 exec_list rhs_instructions
;
1624 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1625 "LHS", &error_emitted
);
1626 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1627 "RHS", &error_emitted
);
1629 if (rhs_instructions
.is_empty()) {
1630 result
= new(ctx
) ir_expression(ir_binop_logic_or
, op
[0], op
[1]);
1632 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1635 instructions
->push_tail(tmp
);
1637 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1638 instructions
->push_tail(stmt
);
1640 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1641 ir_assignment
*const then_assign
=
1642 new(ctx
) ir_assignment(then_deref
, new(ctx
) ir_constant(true));
1643 stmt
->then_instructions
.push_tail(then_assign
);
1645 stmt
->else_instructions
.append_list(&rhs_instructions
);
1646 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1647 ir_assignment
*const else_assign
=
1648 new(ctx
) ir_assignment(else_deref
, op
[1]);
1649 stmt
->else_instructions
.push_tail(else_assign
);
1651 result
= new(ctx
) ir_dereference_variable(tmp
);
1657 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1659 * "The logical binary operators and (&&), or ( | | ), and
1660 * exclusive or (^^). They operate only on two Boolean
1661 * expressions and result in a Boolean expression."
1663 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0, "LHS",
1665 op
[1] = get_scalar_boolean_operand(instructions
, state
, this, 1, "RHS",
1668 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1673 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1674 "operand", &error_emitted
);
1676 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1680 case ast_mul_assign
:
1681 case ast_div_assign
:
1682 case ast_add_assign
:
1683 case ast_sub_assign
: {
1684 this->subexpressions
[0]->set_is_lhs(true);
1685 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1686 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1688 orig_type
= op
[0]->type
;
1690 /* Break out if operand types were not parsed successfully. */
1691 if ((op
[0]->type
== glsl_type::error_type
||
1692 op
[1]->type
== glsl_type::error_type
)) {
1693 error_emitted
= true;
1697 type
= arithmetic_result_type(op
[0], op
[1],
1698 (this->oper
== ast_mul_assign
),
1701 if (type
!= orig_type
) {
1702 _mesa_glsl_error(& loc
, state
,
1703 "could not implicitly convert "
1704 "%s to %s", type
->name
, orig_type
->name
);
1705 type
= glsl_type::error_type
;
1708 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1712 do_assignment(instructions
, state
,
1713 this->subexpressions
[0]->non_lvalue_description
,
1714 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1715 &result
, needs_rvalue
, false,
1716 this->subexpressions
[0]->get_location());
1718 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1719 * explicitly test for this because none of the binary expression
1720 * operators allow array operands either.
1726 case ast_mod_assign
: {
1727 this->subexpressions
[0]->set_is_lhs(true);
1728 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1729 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1731 orig_type
= op
[0]->type
;
1732 type
= modulus_result_type(op
[0], op
[1], state
, &loc
);
1734 if (type
!= orig_type
) {
1735 _mesa_glsl_error(& loc
, state
,
1736 "could not implicitly convert "
1737 "%s to %s", type
->name
, orig_type
->name
);
1738 type
= glsl_type::error_type
;
1741 assert(operations
[this->oper
] == ir_binop_mod
);
1743 ir_rvalue
*temp_rhs
;
1744 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1748 do_assignment(instructions
, state
,
1749 this->subexpressions
[0]->non_lvalue_description
,
1750 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1751 &result
, needs_rvalue
, false,
1752 this->subexpressions
[0]->get_location());
1757 case ast_rs_assign
: {
1758 this->subexpressions
[0]->set_is_lhs(true);
1759 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1760 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1761 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1763 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1764 type
, op
[0], op
[1]);
1766 do_assignment(instructions
, state
,
1767 this->subexpressions
[0]->non_lvalue_description
,
1768 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1769 &result
, needs_rvalue
, false,
1770 this->subexpressions
[0]->get_location());
1774 case ast_and_assign
:
1775 case ast_xor_assign
:
1776 case ast_or_assign
: {
1777 this->subexpressions
[0]->set_is_lhs(true);
1778 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1779 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1781 orig_type
= op
[0]->type
;
1782 type
= bit_logic_result_type(op
[0], op
[1], this->oper
, state
, &loc
);
1784 if (type
!= orig_type
) {
1785 _mesa_glsl_error(& loc
, state
,
1786 "could not implicitly convert "
1787 "%s to %s", type
->name
, orig_type
->name
);
1788 type
= glsl_type::error_type
;
1791 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1792 type
, op
[0], op
[1]);
1794 do_assignment(instructions
, state
,
1795 this->subexpressions
[0]->non_lvalue_description
,
1796 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1797 &result
, needs_rvalue
, false,
1798 this->subexpressions
[0]->get_location());
1802 case ast_conditional
: {
1803 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1805 * "The ternary selection operator (?:). It operates on three
1806 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1807 * first expression, which must result in a scalar Boolean."
1809 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1810 "condition", &error_emitted
);
1812 /* The :? operator is implemented by generating an anonymous temporary
1813 * followed by an if-statement. The last instruction in each branch of
1814 * the if-statement assigns a value to the anonymous temporary. This
1815 * temporary is the r-value of the expression.
1817 exec_list then_instructions
;
1818 exec_list else_instructions
;
1820 op
[1] = this->subexpressions
[1]->hir(&then_instructions
, state
);
1821 op
[2] = this->subexpressions
[2]->hir(&else_instructions
, state
);
1823 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1825 * "The second and third expressions can be any type, as
1826 * long their types match, or there is a conversion in
1827 * Section 4.1.10 "Implicit Conversions" that can be applied
1828 * to one of the expressions to make their types match. This
1829 * resulting matching type is the type of the entire
1832 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1833 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1834 || (op
[1]->type
!= op
[2]->type
)) {
1835 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1837 _mesa_glsl_error(& loc
, state
, "second and third operands of ?: "
1838 "operator must have matching types");
1839 error_emitted
= true;
1840 type
= glsl_type::error_type
;
1845 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1847 * "The second and third expressions must be the same type, but can
1848 * be of any type other than an array."
1850 if (type
->is_array() &&
1851 !state
->check_version(120, 300, &loc
,
1852 "second and third operands of ?: operator "
1853 "cannot be arrays")) {
1854 error_emitted
= true;
1857 /* From section 4.1.7 of the GLSL 4.50 spec (Opaque Types):
1859 * "Except for array indexing, structure member selection, and
1860 * parentheses, opaque variables are not allowed to be operands in
1861 * expressions; such use results in a compile-time error."
1863 if (type
->contains_opaque()) {
1864 if (!(state
->has_bindless() && (type
->is_image() || type
->is_sampler()))) {
1865 _mesa_glsl_error(&loc
, state
, "variables of type %s cannot be "
1866 "operands of the ?: operator", type
->name
);
1867 error_emitted
= true;
1871 ir_constant
*cond_val
= op
[0]->constant_expression_value(ctx
);
1873 if (then_instructions
.is_empty()
1874 && else_instructions
.is_empty()
1875 && cond_val
!= NULL
) {
1876 result
= cond_val
->value
.b
[0] ? op
[1] : op
[2];
1878 /* The copy to conditional_tmp reads the whole array. */
1879 if (type
->is_array()) {
1880 mark_whole_array_access(op
[1]);
1881 mark_whole_array_access(op
[2]);
1884 ir_variable
*const tmp
=
1885 new(ctx
) ir_variable(type
, "conditional_tmp", ir_var_temporary
);
1886 instructions
->push_tail(tmp
);
1888 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1889 instructions
->push_tail(stmt
);
1891 then_instructions
.move_nodes_to(& stmt
->then_instructions
);
1892 ir_dereference
*const then_deref
=
1893 new(ctx
) ir_dereference_variable(tmp
);
1894 ir_assignment
*const then_assign
=
1895 new(ctx
) ir_assignment(then_deref
, op
[1]);
1896 stmt
->then_instructions
.push_tail(then_assign
);
1898 else_instructions
.move_nodes_to(& stmt
->else_instructions
);
1899 ir_dereference
*const else_deref
=
1900 new(ctx
) ir_dereference_variable(tmp
);
1901 ir_assignment
*const else_assign
=
1902 new(ctx
) ir_assignment(else_deref
, op
[2]);
1903 stmt
->else_instructions
.push_tail(else_assign
);
1905 result
= new(ctx
) ir_dereference_variable(tmp
);
1912 this->non_lvalue_description
= (this->oper
== ast_pre_inc
)
1913 ? "pre-increment operation" : "pre-decrement operation";
1915 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1916 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1918 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1920 ir_rvalue
*temp_rhs
;
1921 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1925 do_assignment(instructions
, state
,
1926 this->subexpressions
[0]->non_lvalue_description
,
1927 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1928 &result
, needs_rvalue
, false,
1929 this->subexpressions
[0]->get_location());
1934 case ast_post_dec
: {
1935 this->non_lvalue_description
= (this->oper
== ast_post_inc
)
1936 ? "post-increment operation" : "post-decrement operation";
1937 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1938 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1940 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1942 if (error_emitted
) {
1943 result
= ir_rvalue::error_value(ctx
);
1947 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1949 ir_rvalue
*temp_rhs
;
1950 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1953 /* Get a temporary of a copy of the lvalue before it's modified.
1954 * This may get thrown away later.
1956 result
= get_lvalue_copy(instructions
, op
[0]->clone(ctx
, NULL
));
1958 ir_rvalue
*junk_rvalue
;
1960 do_assignment(instructions
, state
,
1961 this->subexpressions
[0]->non_lvalue_description
,
1962 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1963 &junk_rvalue
, false, false,
1964 this->subexpressions
[0]->get_location());
1969 case ast_field_selection
:
1970 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1973 case ast_array_index
: {
1974 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1976 /* Getting if an array is being used uninitialized is beyond what we get
1977 * from ir_value.data.assigned. Setting is_lhs as true would force to
1978 * not raise a uninitialized warning when using an array
1980 subexpressions
[0]->set_is_lhs(true);
1981 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1982 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1984 result
= _mesa_ast_array_index_to_hir(ctx
, state
, op
[0], op
[1],
1987 if (result
->type
->is_error())
1988 error_emitted
= true;
1993 case ast_unsized_array_dim
:
1994 unreachable("ast_unsized_array_dim: Should never get here.");
1996 case ast_function_call
:
1997 /* Should *NEVER* get here. ast_function_call should always be handled
1998 * by ast_function_expression::hir.
2000 unreachable("ast_function_call: handled elsewhere ");
2002 case ast_identifier
: {
2003 /* ast_identifier can appear several places in a full abstract syntax
2004 * tree. This particular use must be at location specified in the grammar
2005 * as 'variable_identifier'.
2008 state
->symbols
->get_variable(this->primary_expression
.identifier
);
2011 /* the identifier might be a subroutine name */
2013 sub_name
= ralloc_asprintf(ctx
, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state
->stage
), this->primary_expression
.identifier
);
2014 var
= state
->symbols
->get_variable(sub_name
);
2015 ralloc_free(sub_name
);
2019 var
->data
.used
= true;
2020 result
= new(ctx
) ir_dereference_variable(var
);
2022 if ((var
->data
.mode
== ir_var_auto
|| var
->data
.mode
== ir_var_shader_out
)
2024 && result
->variable_referenced()->data
.assigned
!= true
2025 && !is_gl_identifier(var
->name
)) {
2026 _mesa_glsl_warning(&loc
, state
, "`%s' used uninitialized",
2027 this->primary_expression
.identifier
);
2030 /* From the EXT_shader_framebuffer_fetch spec:
2032 * "Unless the GL_EXT_shader_framebuffer_fetch extension has been
2033 * enabled in addition, it's an error to use gl_LastFragData if it
2034 * hasn't been explicitly redeclared with layout(noncoherent)."
2036 if (var
->data
.fb_fetch_output
&& var
->data
.memory_coherent
&&
2037 !state
->EXT_shader_framebuffer_fetch_enable
) {
2038 _mesa_glsl_error(&loc
, state
,
2039 "invalid use of framebuffer fetch output not "
2040 "qualified with layout(noncoherent)");
2044 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
2045 this->primary_expression
.identifier
);
2047 result
= ir_rvalue::error_value(ctx
);
2048 error_emitted
= true;
2053 case ast_int_constant
:
2054 result
= new(ctx
) ir_constant(this->primary_expression
.int_constant
);
2057 case ast_uint_constant
:
2058 result
= new(ctx
) ir_constant(this->primary_expression
.uint_constant
);
2061 case ast_float_constant
:
2062 result
= new(ctx
) ir_constant(this->primary_expression
.float_constant
);
2065 case ast_bool_constant
:
2066 result
= new(ctx
) ir_constant(bool(this->primary_expression
.bool_constant
));
2069 case ast_double_constant
:
2070 result
= new(ctx
) ir_constant(this->primary_expression
.double_constant
);
2073 case ast_uint64_constant
:
2074 result
= new(ctx
) ir_constant(this->primary_expression
.uint64_constant
);
2077 case ast_int64_constant
:
2078 result
= new(ctx
) ir_constant(this->primary_expression
.int64_constant
);
2081 case ast_sequence
: {
2082 /* It should not be possible to generate a sequence in the AST without
2083 * any expressions in it.
2085 assert(!this->expressions
.is_empty());
2087 /* The r-value of a sequence is the last expression in the sequence. If
2088 * the other expressions in the sequence do not have side-effects (and
2089 * therefore add instructions to the instruction list), they get dropped
2092 exec_node
*previous_tail
= NULL
;
2093 YYLTYPE previous_operand_loc
= loc
;
2095 foreach_list_typed (ast_node
, ast
, link
, &this->expressions
) {
2096 /* If one of the operands of comma operator does not generate any
2097 * code, we want to emit a warning. At each pass through the loop
2098 * previous_tail will point to the last instruction in the stream
2099 * *before* processing the previous operand. Naturally,
2100 * instructions->get_tail_raw() will point to the last instruction in
2101 * the stream *after* processing the previous operand. If the two
2102 * pointers match, then the previous operand had no effect.
2104 * The warning behavior here differs slightly from GCC. GCC will
2105 * only emit a warning if none of the left-hand operands have an
2106 * effect. However, it will emit a warning for each. I believe that
2107 * there are some cases in C (especially with GCC extensions) where
2108 * it is useful to have an intermediate step in a sequence have no
2109 * effect, but I don't think these cases exist in GLSL. Either way,
2110 * it would be a giant hassle to replicate that behavior.
2112 if (previous_tail
== instructions
->get_tail_raw()) {
2113 _mesa_glsl_warning(&previous_operand_loc
, state
,
2114 "left-hand operand of comma expression has "
2118 /* The tail is directly accessed instead of using the get_tail()
2119 * method for performance reasons. get_tail() has extra code to
2120 * return NULL when the list is empty. We don't care about that
2121 * here, so using get_tail_raw() is fine.
2123 previous_tail
= instructions
->get_tail_raw();
2124 previous_operand_loc
= ast
->get_location();
2126 result
= ast
->hir(instructions
, state
);
2129 /* Any errors should have already been emitted in the loop above.
2131 error_emitted
= true;
2135 type
= NULL
; /* use result->type, not type. */
2136 assert(error_emitted
|| (result
!= NULL
|| !needs_rvalue
));
2138 if (result
&& result
->type
->is_error() && !error_emitted
)
2139 _mesa_glsl_error(& loc
, state
, "type mismatch");
2145 ast_expression::has_sequence_subexpression() const
2147 switch (this->oper
) {
2156 return this->subexpressions
[0]->has_sequence_subexpression();
2178 case ast_array_index
:
2179 case ast_mul_assign
:
2180 case ast_div_assign
:
2181 case ast_add_assign
:
2182 case ast_sub_assign
:
2183 case ast_mod_assign
:
2186 case ast_and_assign
:
2187 case ast_xor_assign
:
2189 return this->subexpressions
[0]->has_sequence_subexpression() ||
2190 this->subexpressions
[1]->has_sequence_subexpression();
2192 case ast_conditional
:
2193 return this->subexpressions
[0]->has_sequence_subexpression() ||
2194 this->subexpressions
[1]->has_sequence_subexpression() ||
2195 this->subexpressions
[2]->has_sequence_subexpression();
2200 case ast_field_selection
:
2201 case ast_identifier
:
2202 case ast_int_constant
:
2203 case ast_uint_constant
:
2204 case ast_float_constant
:
2205 case ast_bool_constant
:
2206 case ast_double_constant
:
2207 case ast_int64_constant
:
2208 case ast_uint64_constant
:
2214 case ast_function_call
:
2215 unreachable("should be handled by ast_function_expression::hir");
2217 case ast_unsized_array_dim
:
2218 unreachable("ast_unsized_array_dim: Should never get here.");
2225 ast_expression_statement::hir(exec_list
*instructions
,
2226 struct _mesa_glsl_parse_state
*state
)
2228 /* It is possible to have expression statements that don't have an
2229 * expression. This is the solitary semicolon:
2231 * for (i = 0; i < 5; i++)
2234 * In this case the expression will be NULL. Test for NULL and don't do
2235 * anything in that case.
2237 if (expression
!= NULL
)
2238 expression
->hir_no_rvalue(instructions
, state
);
2240 /* Statements do not have r-values.
2247 ast_compound_statement::hir(exec_list
*instructions
,
2248 struct _mesa_glsl_parse_state
*state
)
2251 state
->symbols
->push_scope();
2253 foreach_list_typed (ast_node
, ast
, link
, &this->statements
)
2254 ast
->hir(instructions
, state
);
2257 state
->symbols
->pop_scope();
2259 /* Compound statements do not have r-values.
2265 * Evaluate the given exec_node (which should be an ast_node representing
2266 * a single array dimension) and return its integer value.
2269 process_array_size(exec_node
*node
,
2270 struct _mesa_glsl_parse_state
*state
)
2272 void *mem_ctx
= state
;
2274 exec_list dummy_instructions
;
2276 ast_node
*array_size
= exec_node_data(ast_node
, node
, link
);
2279 * Dimensions other than the outermost dimension can by unsized if they
2280 * are immediately sized by a constructor or initializer.
2282 if (((ast_expression
*)array_size
)->oper
== ast_unsized_array_dim
)
2285 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
2286 YYLTYPE loc
= array_size
->get_location();
2289 _mesa_glsl_error(& loc
, state
,
2290 "array size could not be resolved");
2294 if (!ir
->type
->is_integer_32()) {
2295 _mesa_glsl_error(& loc
, state
,
2296 "array size must be integer type");
2300 if (!ir
->type
->is_scalar()) {
2301 _mesa_glsl_error(& loc
, state
,
2302 "array size must be scalar type");
2306 ir_constant
*const size
= ir
->constant_expression_value(mem_ctx
);
2308 (state
->is_version(120, 300) &&
2309 array_size
->has_sequence_subexpression())) {
2310 _mesa_glsl_error(& loc
, state
, "array size must be a "
2311 "constant valued expression");
2315 if (size
->value
.i
[0] <= 0) {
2316 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
2320 assert(size
->type
== ir
->type
);
2322 /* If the array size is const (and we've verified that
2323 * it is) then no instructions should have been emitted
2324 * when we converted it to HIR. If they were emitted,
2325 * then either the array size isn't const after all, or
2326 * we are emitting unnecessary instructions.
2328 assert(dummy_instructions
.is_empty());
2330 return size
->value
.u
[0];
2333 static const glsl_type
*
2334 process_array_type(YYLTYPE
*loc
, const glsl_type
*base
,
2335 ast_array_specifier
*array_specifier
,
2336 struct _mesa_glsl_parse_state
*state
)
2338 const glsl_type
*array_type
= base
;
2340 if (array_specifier
!= NULL
) {
2341 if (base
->is_array()) {
2343 /* From page 19 (page 25) of the GLSL 1.20 spec:
2345 * "Only one-dimensional arrays may be declared."
2347 if (!state
->check_arrays_of_arrays_allowed(loc
)) {
2348 return glsl_type::error_type
;
2352 for (exec_node
*node
= array_specifier
->array_dimensions
.get_tail_raw();
2353 !node
->is_head_sentinel(); node
= node
->prev
) {
2354 unsigned array_size
= process_array_size(node
, state
);
2355 array_type
= glsl_type::get_array_instance(array_type
, array_size
);
2363 precision_qualifier_allowed(const glsl_type
*type
)
2365 /* Precision qualifiers apply to floating point, integer and opaque
2368 * Section 4.5.2 (Precision Qualifiers) of the GLSL 1.30 spec says:
2369 * "Any floating point or any integer declaration can have the type
2370 * preceded by one of these precision qualifiers [...] Literal
2371 * constants do not have precision qualifiers. Neither do Boolean
2374 * Section 4.5 (Precision and Precision Qualifiers) of the GLSL 1.30
2377 * "Precision qualifiers are added for code portability with OpenGL
2378 * ES, not for functionality. They have the same syntax as in OpenGL
2381 * Section 8 (Built-In Functions) of the GLSL ES 1.00 spec says:
2383 * "uniform lowp sampler2D sampler;
2386 * lowp vec4 col = texture2D (sampler, coord);
2387 * // texture2D returns lowp"
2389 * From this, we infer that GLSL 1.30 (and later) should allow precision
2390 * qualifiers on sampler types just like float and integer types.
2392 const glsl_type
*const t
= type
->without_array();
2394 return (t
->is_float() || t
->is_integer_32() || t
->contains_opaque()) &&
2399 ast_type_specifier::glsl_type(const char **name
,
2400 struct _mesa_glsl_parse_state
*state
) const
2402 const struct glsl_type
*type
;
2404 if (this->type
!= NULL
)
2407 type
= structure
->type
;
2409 type
= state
->symbols
->get_type(this->type_name
);
2410 *name
= this->type_name
;
2412 YYLTYPE loc
= this->get_location();
2413 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
2419 * From the OpenGL ES 3.0 spec, 4.5.4 Default Precision Qualifiers:
2421 * "The precision statement
2423 * precision precision-qualifier type;
2425 * can be used to establish a default precision qualifier. The type field can
2426 * be either int or float or any of the sampler types, (...) If type is float,
2427 * the directive applies to non-precision-qualified floating point type
2428 * (scalar, vector, and matrix) declarations. If type is int, the directive
2429 * applies to all non-precision-qualified integer type (scalar, vector, signed,
2430 * and unsigned) declarations."
2432 * We use the symbol table to keep the values of the default precisions for
2433 * each 'type' in each scope and we use the 'type' string from the precision
2434 * statement as key in the symbol table. When we want to retrieve the default
2435 * precision associated with a given glsl_type we need to know the type string
2436 * associated with it. This is what this function returns.
2439 get_type_name_for_precision_qualifier(const glsl_type
*type
)
2441 switch (type
->base_type
) {
2442 case GLSL_TYPE_FLOAT
:
2444 case GLSL_TYPE_UINT
:
2447 case GLSL_TYPE_ATOMIC_UINT
:
2448 return "atomic_uint";
2449 case GLSL_TYPE_IMAGE
:
2451 case GLSL_TYPE_SAMPLER
: {
2452 const unsigned type_idx
=
2453 type
->sampler_array
+ 2 * type
->sampler_shadow
;
2454 const unsigned offset
= type
->is_sampler() ? 0 : 4;
2455 assert(type_idx
< 4);
2456 switch (type
->sampled_type
) {
2457 case GLSL_TYPE_FLOAT
:
2458 switch (type
->sampler_dimensionality
) {
2459 case GLSL_SAMPLER_DIM_1D
: {
2460 assert(type
->is_sampler());
2461 static const char *const names
[4] = {
2462 "sampler1D", "sampler1DArray",
2463 "sampler1DShadow", "sampler1DArrayShadow"
2465 return names
[type_idx
];
2467 case GLSL_SAMPLER_DIM_2D
: {
2468 static const char *const names
[8] = {
2469 "sampler2D", "sampler2DArray",
2470 "sampler2DShadow", "sampler2DArrayShadow",
2471 "image2D", "image2DArray", NULL
, NULL
2473 return names
[offset
+ type_idx
];
2475 case GLSL_SAMPLER_DIM_3D
: {
2476 static const char *const names
[8] = {
2477 "sampler3D", NULL
, NULL
, NULL
,
2478 "image3D", NULL
, NULL
, NULL
2480 return names
[offset
+ type_idx
];
2482 case GLSL_SAMPLER_DIM_CUBE
: {
2483 static const char *const names
[8] = {
2484 "samplerCube", "samplerCubeArray",
2485 "samplerCubeShadow", "samplerCubeArrayShadow",
2486 "imageCube", NULL
, NULL
, NULL
2488 return names
[offset
+ type_idx
];
2490 case GLSL_SAMPLER_DIM_MS
: {
2491 assert(type
->is_sampler());
2492 static const char *const names
[4] = {
2493 "sampler2DMS", "sampler2DMSArray", NULL
, NULL
2495 return names
[type_idx
];
2497 case GLSL_SAMPLER_DIM_RECT
: {
2498 assert(type
->is_sampler());
2499 static const char *const names
[4] = {
2500 "samplerRect", NULL
, "samplerRectShadow", NULL
2502 return names
[type_idx
];
2504 case GLSL_SAMPLER_DIM_BUF
: {
2505 static const char *const names
[8] = {
2506 "samplerBuffer", NULL
, NULL
, NULL
,
2507 "imageBuffer", NULL
, NULL
, NULL
2509 return names
[offset
+ type_idx
];
2511 case GLSL_SAMPLER_DIM_EXTERNAL
: {
2512 assert(type
->is_sampler());
2513 static const char *const names
[4] = {
2514 "samplerExternalOES", NULL
, NULL
, NULL
2516 return names
[type_idx
];
2519 unreachable("Unsupported sampler/image dimensionality");
2520 } /* sampler/image float dimensionality */
2523 switch (type
->sampler_dimensionality
) {
2524 case GLSL_SAMPLER_DIM_1D
: {
2525 assert(type
->is_sampler());
2526 static const char *const names
[4] = {
2527 "isampler1D", "isampler1DArray", NULL
, NULL
2529 return names
[type_idx
];
2531 case GLSL_SAMPLER_DIM_2D
: {
2532 static const char *const names
[8] = {
2533 "isampler2D", "isampler2DArray", NULL
, NULL
,
2534 "iimage2D", "iimage2DArray", NULL
, NULL
2536 return names
[offset
+ type_idx
];
2538 case GLSL_SAMPLER_DIM_3D
: {
2539 static const char *const names
[8] = {
2540 "isampler3D", NULL
, NULL
, NULL
,
2541 "iimage3D", NULL
, NULL
, NULL
2543 return names
[offset
+ type_idx
];
2545 case GLSL_SAMPLER_DIM_CUBE
: {
2546 static const char *const names
[8] = {
2547 "isamplerCube", "isamplerCubeArray", NULL
, NULL
,
2548 "iimageCube", NULL
, NULL
, NULL
2550 return names
[offset
+ type_idx
];
2552 case GLSL_SAMPLER_DIM_MS
: {
2553 assert(type
->is_sampler());
2554 static const char *const names
[4] = {
2555 "isampler2DMS", "isampler2DMSArray", NULL
, NULL
2557 return names
[type_idx
];
2559 case GLSL_SAMPLER_DIM_RECT
: {
2560 assert(type
->is_sampler());
2561 static const char *const names
[4] = {
2562 "isamplerRect", NULL
, "isamplerRectShadow", NULL
2564 return names
[type_idx
];
2566 case GLSL_SAMPLER_DIM_BUF
: {
2567 static const char *const names
[8] = {
2568 "isamplerBuffer", NULL
, NULL
, NULL
,
2569 "iimageBuffer", NULL
, NULL
, NULL
2571 return names
[offset
+ type_idx
];
2574 unreachable("Unsupported isampler/iimage dimensionality");
2575 } /* sampler/image int dimensionality */
2577 case GLSL_TYPE_UINT
:
2578 switch (type
->sampler_dimensionality
) {
2579 case GLSL_SAMPLER_DIM_1D
: {
2580 assert(type
->is_sampler());
2581 static const char *const names
[4] = {
2582 "usampler1D", "usampler1DArray", NULL
, NULL
2584 return names
[type_idx
];
2586 case GLSL_SAMPLER_DIM_2D
: {
2587 static const char *const names
[8] = {
2588 "usampler2D", "usampler2DArray", NULL
, NULL
,
2589 "uimage2D", "uimage2DArray", NULL
, NULL
2591 return names
[offset
+ type_idx
];
2593 case GLSL_SAMPLER_DIM_3D
: {
2594 static const char *const names
[8] = {
2595 "usampler3D", NULL
, NULL
, NULL
,
2596 "uimage3D", NULL
, NULL
, NULL
2598 return names
[offset
+ type_idx
];
2600 case GLSL_SAMPLER_DIM_CUBE
: {
2601 static const char *const names
[8] = {
2602 "usamplerCube", "usamplerCubeArray", NULL
, NULL
,
2603 "uimageCube", NULL
, NULL
, NULL
2605 return names
[offset
+ type_idx
];
2607 case GLSL_SAMPLER_DIM_MS
: {
2608 assert(type
->is_sampler());
2609 static const char *const names
[4] = {
2610 "usampler2DMS", "usampler2DMSArray", NULL
, NULL
2612 return names
[type_idx
];
2614 case GLSL_SAMPLER_DIM_RECT
: {
2615 assert(type
->is_sampler());
2616 static const char *const names
[4] = {
2617 "usamplerRect", NULL
, "usamplerRectShadow", NULL
2619 return names
[type_idx
];
2621 case GLSL_SAMPLER_DIM_BUF
: {
2622 static const char *const names
[8] = {
2623 "usamplerBuffer", NULL
, NULL
, NULL
,
2624 "uimageBuffer", NULL
, NULL
, NULL
2626 return names
[offset
+ type_idx
];
2629 unreachable("Unsupported usampler/uimage dimensionality");
2630 } /* sampler/image uint dimensionality */
2633 unreachable("Unsupported sampler/image type");
2634 } /* sampler/image type */
2636 } /* GLSL_TYPE_SAMPLER/GLSL_TYPE_IMAGE */
2639 unreachable("Unsupported type");
2644 select_gles_precision(unsigned qual_precision
,
2645 const glsl_type
*type
,
2646 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
2648 /* Precision qualifiers do not have any meaning in Desktop GLSL.
2649 * In GLES we take the precision from the type qualifier if present,
2650 * otherwise, if the type of the variable allows precision qualifiers at
2651 * all, we look for the default precision qualifier for that type in the
2654 assert(state
->es_shader
);
2656 unsigned precision
= GLSL_PRECISION_NONE
;
2657 if (qual_precision
) {
2658 precision
= qual_precision
;
2659 } else if (precision_qualifier_allowed(type
)) {
2660 const char *type_name
=
2661 get_type_name_for_precision_qualifier(type
->without_array());
2662 assert(type_name
!= NULL
);
2665 state
->symbols
->get_default_precision_qualifier(type_name
);
2666 if (precision
== ast_precision_none
) {
2667 _mesa_glsl_error(loc
, state
,
2668 "No precision specified in this scope for type `%s'",
2674 /* Section 4.1.7.3 (Atomic Counters) of the GLSL ES 3.10 spec says:
2676 * "The default precision of all atomic types is highp. It is an error to
2677 * declare an atomic type with a different precision or to specify the
2678 * default precision for an atomic type to be lowp or mediump."
2680 if (type
->is_atomic_uint() && precision
!= ast_precision_high
) {
2681 _mesa_glsl_error(loc
, state
,
2682 "atomic_uint can only have highp precision qualifier");
2689 ast_fully_specified_type::glsl_type(const char **name
,
2690 struct _mesa_glsl_parse_state
*state
) const
2692 return this->specifier
->glsl_type(name
, state
);
2696 * Determine whether a toplevel variable declaration declares a varying. This
2697 * function operates by examining the variable's mode and the shader target,
2698 * so it correctly identifies linkage variables regardless of whether they are
2699 * declared using the deprecated "varying" syntax or the new "in/out" syntax.
2701 * Passing a non-toplevel variable declaration (e.g. a function parameter) to
2702 * this function will produce undefined results.
2705 is_varying_var(ir_variable
*var
, gl_shader_stage target
)
2708 case MESA_SHADER_VERTEX
:
2709 return var
->data
.mode
== ir_var_shader_out
;
2710 case MESA_SHADER_FRAGMENT
:
2711 return var
->data
.mode
== ir_var_shader_in
||
2712 (var
->data
.mode
== ir_var_system_value
&&
2713 var
->data
.location
== SYSTEM_VALUE_FRAG_COORD
);
2715 return var
->data
.mode
== ir_var_shader_out
|| var
->data
.mode
== ir_var_shader_in
;
2720 is_allowed_invariant(ir_variable
*var
, struct _mesa_glsl_parse_state
*state
)
2722 if (is_varying_var(var
, state
->stage
))
2725 /* From Section 4.6.1 ("The Invariant Qualifier") GLSL 1.20 spec:
2726 * "Only variables output from a vertex shader can be candidates
2729 if (!state
->is_version(130, 100))
2733 * Later specs remove this language - so allowed invariant
2734 * on fragment shader outputs as well.
2736 if (state
->stage
== MESA_SHADER_FRAGMENT
&&
2737 var
->data
.mode
== ir_var_shader_out
)
2743 * Matrix layout qualifiers are only allowed on certain types
2746 validate_matrix_layout_for_type(struct _mesa_glsl_parse_state
*state
,
2748 const glsl_type
*type
,
2751 if (var
&& !var
->is_in_buffer_block()) {
2752 /* Layout qualifiers may only apply to interface blocks and fields in
2755 _mesa_glsl_error(loc
, state
,
2756 "uniform block layout qualifiers row_major and "
2757 "column_major may not be applied to variables "
2758 "outside of uniform blocks");
2759 } else if (!type
->without_array()->is_matrix()) {
2760 /* The OpenGL ES 3.0 conformance tests did not originally allow
2761 * matrix layout qualifiers on non-matrices. However, the OpenGL
2762 * 4.4 and OpenGL ES 3.0 (revision TBD) specifications were
2763 * amended to specifically allow these layouts on all types. Emit
2764 * a warning so that people know their code may not be portable.
2766 _mesa_glsl_warning(loc
, state
,
2767 "uniform block layout qualifiers row_major and "
2768 "column_major applied to non-matrix types may "
2769 "be rejected by older compilers");
2774 validate_xfb_buffer_qualifier(YYLTYPE
*loc
,
2775 struct _mesa_glsl_parse_state
*state
,
2776 unsigned xfb_buffer
) {
2777 if (xfb_buffer
>= state
->Const
.MaxTransformFeedbackBuffers
) {
2778 _mesa_glsl_error(loc
, state
,
2779 "invalid xfb_buffer specified %d is larger than "
2780 "MAX_TRANSFORM_FEEDBACK_BUFFERS - 1 (%d).",
2782 state
->Const
.MaxTransformFeedbackBuffers
- 1);
2789 /* From the ARB_enhanced_layouts spec:
2791 * "Variables and block members qualified with *xfb_offset* can be
2792 * scalars, vectors, matrices, structures, and (sized) arrays of these.
2793 * The offset must be a multiple of the size of the first component of
2794 * the first qualified variable or block member, or a compile-time error
2795 * results. Further, if applied to an aggregate containing a double,
2796 * the offset must also be a multiple of 8, and the space taken in the
2797 * buffer will be a multiple of 8.
2800 validate_xfb_offset_qualifier(YYLTYPE
*loc
,
2801 struct _mesa_glsl_parse_state
*state
,
2802 int xfb_offset
, const glsl_type
*type
,
2803 unsigned component_size
) {
2804 const glsl_type
*t_without_array
= type
->without_array();
2806 if (xfb_offset
!= -1 && type
->is_unsized_array()) {
2807 _mesa_glsl_error(loc
, state
,
2808 "xfb_offset can't be used with unsized arrays.");
2812 /* Make sure nested structs don't contain unsized arrays, and validate
2813 * any xfb_offsets on interface members.
2815 if (t_without_array
->is_struct() || t_without_array
->is_interface())
2816 for (unsigned int i
= 0; i
< t_without_array
->length
; i
++) {
2817 const glsl_type
*member_t
= t_without_array
->fields
.structure
[i
].type
;
2819 /* When the interface block doesn't have an xfb_offset qualifier then
2820 * we apply the component size rules at the member level.
2822 if (xfb_offset
== -1)
2823 component_size
= member_t
->contains_double() ? 8 : 4;
2825 int xfb_offset
= t_without_array
->fields
.structure
[i
].offset
;
2826 validate_xfb_offset_qualifier(loc
, state
, xfb_offset
, member_t
,
2830 /* Nested structs or interface block without offset may not have had an
2831 * offset applied yet so return.
2833 if (xfb_offset
== -1) {
2837 if (xfb_offset
% component_size
) {
2838 _mesa_glsl_error(loc
, state
,
2839 "invalid qualifier xfb_offset=%d must be a multiple "
2840 "of the first component size of the first qualified "
2841 "variable or block member. Or double if an aggregate "
2842 "that contains a double (%d).",
2843 xfb_offset
, component_size
);
2851 validate_stream_qualifier(YYLTYPE
*loc
, struct _mesa_glsl_parse_state
*state
,
2854 if (stream
>= state
->ctx
->Const
.MaxVertexStreams
) {
2855 _mesa_glsl_error(loc
, state
,
2856 "invalid stream specified %d is larger than "
2857 "MAX_VERTEX_STREAMS - 1 (%d).",
2858 stream
, state
->ctx
->Const
.MaxVertexStreams
- 1);
2866 apply_explicit_binding(struct _mesa_glsl_parse_state
*state
,
2869 const glsl_type
*type
,
2870 const ast_type_qualifier
*qual
)
2872 if (!qual
->flags
.q
.uniform
&& !qual
->flags
.q
.buffer
) {
2873 _mesa_glsl_error(loc
, state
,
2874 "the \"binding\" qualifier only applies to uniforms and "
2875 "shader storage buffer objects");
2879 unsigned qual_binding
;
2880 if (!process_qualifier_constant(state
, loc
, "binding", qual
->binding
,
2885 const struct gl_context
*const ctx
= state
->ctx
;
2886 unsigned elements
= type
->is_array() ? type
->arrays_of_arrays_size() : 1;
2887 unsigned max_index
= qual_binding
+ elements
- 1;
2888 const glsl_type
*base_type
= type
->without_array();
2890 if (base_type
->is_interface()) {
2891 /* UBOs. From page 60 of the GLSL 4.20 specification:
2892 * "If the binding point for any uniform block instance is less than zero,
2893 * or greater than or equal to the implementation-dependent maximum
2894 * number of uniform buffer bindings, a compilation error will occur.
2895 * When the binding identifier is used with a uniform block instanced as
2896 * an array of size N, all elements of the array from binding through
2897 * binding + N – 1 must be within this range."
2899 * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
2901 if (qual
->flags
.q
.uniform
&&
2902 max_index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2903 _mesa_glsl_error(loc
, state
, "layout(binding = %u) for %d UBOs exceeds "
2904 "the maximum number of UBO binding points (%d)",
2905 qual_binding
, elements
,
2906 ctx
->Const
.MaxUniformBufferBindings
);
2910 /* SSBOs. From page 67 of the GLSL 4.30 specification:
2911 * "If the binding point for any uniform or shader storage block instance
2912 * is less than zero, or greater than or equal to the
2913 * implementation-dependent maximum number of uniform buffer bindings, a
2914 * compile-time error will occur. When the binding identifier is used
2915 * with a uniform or shader storage block instanced as an array of size
2916 * N, all elements of the array from binding through binding + N – 1 must
2917 * be within this range."
2919 if (qual
->flags
.q
.buffer
&&
2920 max_index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2921 _mesa_glsl_error(loc
, state
, "layout(binding = %u) for %d SSBOs exceeds "
2922 "the maximum number of SSBO binding points (%d)",
2923 qual_binding
, elements
,
2924 ctx
->Const
.MaxShaderStorageBufferBindings
);
2927 } else if (base_type
->is_sampler()) {
2928 /* Samplers. From page 63 of the GLSL 4.20 specification:
2929 * "If the binding is less than zero, or greater than or equal to the
2930 * implementation-dependent maximum supported number of units, a
2931 * compilation error will occur. When the binding identifier is used
2932 * with an array of size N, all elements of the array from binding
2933 * through binding + N - 1 must be within this range."
2935 unsigned limit
= ctx
->Const
.MaxCombinedTextureImageUnits
;
2937 if (max_index
>= limit
) {
2938 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d samplers "
2939 "exceeds the maximum number of texture image units "
2940 "(%u)", qual_binding
, elements
, limit
);
2944 } else if (base_type
->contains_atomic()) {
2945 assert(ctx
->Const
.MaxAtomicBufferBindings
<= MAX_COMBINED_ATOMIC_BUFFERS
);
2946 if (qual_binding
>= ctx
->Const
.MaxAtomicBufferBindings
) {
2947 _mesa_glsl_error(loc
, state
, "layout(binding = %d) exceeds the "
2948 "maximum number of atomic counter buffer bindings "
2949 "(%u)", qual_binding
,
2950 ctx
->Const
.MaxAtomicBufferBindings
);
2954 } else if ((state
->is_version(420, 310) ||
2955 state
->ARB_shading_language_420pack_enable
) &&
2956 base_type
->is_image()) {
2957 assert(ctx
->Const
.MaxImageUnits
<= MAX_IMAGE_UNITS
);
2958 if (max_index
>= ctx
->Const
.MaxImageUnits
) {
2959 _mesa_glsl_error(loc
, state
, "Image binding %d exceeds the "
2960 "maximum number of image units (%d)", max_index
,
2961 ctx
->Const
.MaxImageUnits
);
2966 _mesa_glsl_error(loc
, state
,
2967 "the \"binding\" qualifier only applies to uniform "
2968 "blocks, storage blocks, opaque variables, or arrays "
2973 var
->data
.explicit_binding
= true;
2974 var
->data
.binding
= qual_binding
;
2980 validate_fragment_flat_interpolation_input(struct _mesa_glsl_parse_state
*state
,
2982 const glsl_interp_mode interpolation
,
2983 const struct glsl_type
*var_type
,
2984 ir_variable_mode mode
)
2986 if (state
->stage
!= MESA_SHADER_FRAGMENT
||
2987 interpolation
== INTERP_MODE_FLAT
||
2988 mode
!= ir_var_shader_in
)
2991 /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
2992 * so must integer vertex outputs.
2994 * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
2995 * "Fragment shader inputs that are signed or unsigned integers or
2996 * integer vectors must be qualified with the interpolation qualifier
2999 * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
3000 * "Fragment shader inputs that are, or contain, signed or unsigned
3001 * integers or integer vectors must be qualified with the
3002 * interpolation qualifier flat."
3004 * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
3005 * "Vertex shader outputs that are, or contain, signed or unsigned
3006 * integers or integer vectors must be qualified with the
3007 * interpolation qualifier flat."
3009 * Note that prior to GLSL 1.50, this requirement applied to vertex
3010 * outputs rather than fragment inputs. That creates problems in the
3011 * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
3012 * desktop GL shaders. For GLSL ES shaders, we follow the spec and
3013 * apply the restriction to both vertex outputs and fragment inputs.
3015 * Note also that the desktop GLSL specs are missing the text "or
3016 * contain"; this is presumably an oversight, since there is no
3017 * reasonable way to interpolate a fragment shader input that contains
3018 * an integer. See Khronos bug #15671.
3020 if ((state
->is_version(130, 300) || state
->EXT_gpu_shader4_enable
)
3021 && var_type
->contains_integer()) {
3022 _mesa_glsl_error(loc
, state
, "if a fragment input is (or contains) "
3023 "an integer, then it must be qualified with 'flat'");
3026 /* Double fragment inputs must be qualified with 'flat'.
3028 * From the "Overview" of the ARB_gpu_shader_fp64 extension spec:
3029 * "This extension does not support interpolation of double-precision
3030 * values; doubles used as fragment shader inputs must be qualified as
3033 * From section 4.3.4 ("Inputs") of the GLSL 4.00 spec:
3034 * "Fragment shader inputs that are signed or unsigned integers, integer
3035 * vectors, or any double-precision floating-point type must be
3036 * qualified with the interpolation qualifier flat."
3038 * Note that the GLSL specs are missing the text "or contain"; this is
3039 * presumably an oversight. See Khronos bug #15671.
3041 * The 'double' type does not exist in GLSL ES so far.
3043 if (state
->has_double()
3044 && var_type
->contains_double()) {
3045 _mesa_glsl_error(loc
, state
, "if a fragment input is (or contains) "
3046 "a double, then it must be qualified with 'flat'");
3049 /* Bindless sampler/image fragment inputs must be qualified with 'flat'.
3051 * From section 4.3.4 of the ARB_bindless_texture spec:
3053 * "(modify last paragraph, p. 35, allowing samplers and images as
3054 * fragment shader inputs) ... Fragment inputs can only be signed and
3055 * unsigned integers and integer vectors, floating point scalars,
3056 * floating-point vectors, matrices, sampler and image types, or arrays
3057 * or structures of these. Fragment shader inputs that are signed or
3058 * unsigned integers, integer vectors, or any double-precision floating-
3059 * point type, or any sampler or image type must be qualified with the
3060 * interpolation qualifier "flat"."
3062 if (state
->has_bindless()
3063 && (var_type
->contains_sampler() || var_type
->contains_image())) {
3064 _mesa_glsl_error(loc
, state
, "if a fragment input is (or contains) "
3065 "a bindless sampler (or image), then it must be "
3066 "qualified with 'flat'");
3071 validate_interpolation_qualifier(struct _mesa_glsl_parse_state
*state
,
3073 const glsl_interp_mode interpolation
,
3074 const struct ast_type_qualifier
*qual
,
3075 const struct glsl_type
*var_type
,
3076 ir_variable_mode mode
)
3078 /* Interpolation qualifiers can only apply to shader inputs or outputs, but
3079 * not to vertex shader inputs nor fragment shader outputs.
3081 * From section 4.3 ("Storage Qualifiers") of the GLSL 1.30 spec:
3082 * "Outputs from a vertex shader (out) and inputs to a fragment
3083 * shader (in) can be further qualified with one or more of these
3084 * interpolation qualifiers"
3086 * "These interpolation qualifiers may only precede the qualifiers in,
3087 * centroid in, out, or centroid out in a declaration. They do not apply
3088 * to the deprecated storage qualifiers varying or centroid
3089 * varying. They also do not apply to inputs into a vertex shader or
3090 * outputs from a fragment shader."
3092 * From section 4.3 ("Storage Qualifiers") of the GLSL ES 3.00 spec:
3093 * "Outputs from a shader (out) and inputs to a shader (in) can be
3094 * further qualified with one of these interpolation qualifiers."
3096 * "These interpolation qualifiers may only precede the qualifiers
3097 * in, centroid in, out, or centroid out in a declaration. They do
3098 * not apply to inputs into a vertex shader or outputs from a
3101 if ((state
->is_version(130, 300) || state
->EXT_gpu_shader4_enable
)
3102 && interpolation
!= INTERP_MODE_NONE
) {
3103 const char *i
= interpolation_string(interpolation
);
3104 if (mode
!= ir_var_shader_in
&& mode
!= ir_var_shader_out
)
3105 _mesa_glsl_error(loc
, state
,
3106 "interpolation qualifier `%s' can only be applied to "
3107 "shader inputs or outputs.", i
);
3109 switch (state
->stage
) {
3110 case MESA_SHADER_VERTEX
:
3111 if (mode
== ir_var_shader_in
) {
3112 _mesa_glsl_error(loc
, state
,
3113 "interpolation qualifier '%s' cannot be applied to "
3114 "vertex shader inputs", i
);
3117 case MESA_SHADER_FRAGMENT
:
3118 if (mode
== ir_var_shader_out
) {
3119 _mesa_glsl_error(loc
, state
,
3120 "interpolation qualifier '%s' cannot be applied to "
3121 "fragment shader outputs", i
);
3129 /* Interpolation qualifiers cannot be applied to 'centroid' and
3130 * 'centroid varying'.
3132 * From section 4.3 ("Storage Qualifiers") of the GLSL 1.30 spec:
3133 * "interpolation qualifiers may only precede the qualifiers in,
3134 * centroid in, out, or centroid out in a declaration. They do not apply
3135 * to the deprecated storage qualifiers varying or centroid varying."
3137 * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
3139 * GL_EXT_gpu_shader4 allows this.
3141 if (state
->is_version(130, 0) && !state
->EXT_gpu_shader4_enable
3142 && interpolation
!= INTERP_MODE_NONE
3143 && qual
->flags
.q
.varying
) {
3145 const char *i
= interpolation_string(interpolation
);
3147 if (qual
->flags
.q
.centroid
)
3148 s
= "centroid varying";
3152 _mesa_glsl_error(loc
, state
,
3153 "qualifier '%s' cannot be applied to the "
3154 "deprecated storage qualifier '%s'", i
, s
);
3157 validate_fragment_flat_interpolation_input(state
, loc
, interpolation
,
3161 static glsl_interp_mode
3162 interpret_interpolation_qualifier(const struct ast_type_qualifier
*qual
,
3163 const struct glsl_type
*var_type
,
3164 ir_variable_mode mode
,
3165 struct _mesa_glsl_parse_state
*state
,
3168 glsl_interp_mode interpolation
;
3169 if (qual
->flags
.q
.flat
)
3170 interpolation
= INTERP_MODE_FLAT
;
3171 else if (qual
->flags
.q
.noperspective
)
3172 interpolation
= INTERP_MODE_NOPERSPECTIVE
;
3173 else if (qual
->flags
.q
.smooth
)
3174 interpolation
= INTERP_MODE_SMOOTH
;
3176 interpolation
= INTERP_MODE_NONE
;
3178 validate_interpolation_qualifier(state
, loc
,
3180 qual
, var_type
, mode
);
3182 return interpolation
;
3187 apply_explicit_location(const struct ast_type_qualifier
*qual
,
3189 struct _mesa_glsl_parse_state
*state
,
3194 unsigned qual_location
;
3195 if (!process_qualifier_constant(state
, loc
, "location", qual
->location
,
3200 /* Checks for GL_ARB_explicit_uniform_location. */
3201 if (qual
->flags
.q
.uniform
) {
3202 if (!state
->check_explicit_uniform_location_allowed(loc
, var
))
3205 const struct gl_context
*const ctx
= state
->ctx
;
3206 unsigned max_loc
= qual_location
+ var
->type
->uniform_locations() - 1;
3208 if (max_loc
>= ctx
->Const
.MaxUserAssignableUniformLocations
) {
3209 _mesa_glsl_error(loc
, state
, "location(s) consumed by uniform %s "
3210 ">= MAX_UNIFORM_LOCATIONS (%u)", var
->name
,
3211 ctx
->Const
.MaxUserAssignableUniformLocations
);
3215 var
->data
.explicit_location
= true;
3216 var
->data
.location
= qual_location
;
3220 /* Between GL_ARB_explicit_attrib_location an
3221 * GL_ARB_separate_shader_objects, the inputs and outputs of any shader
3222 * stage can be assigned explicit locations. The checking here associates
3223 * the correct extension with the correct stage's input / output:
3227 * vertex explicit_loc sso
3228 * tess control sso sso
3231 * fragment sso explicit_loc
3233 switch (state
->stage
) {
3234 case MESA_SHADER_VERTEX
:
3235 if (var
->data
.mode
== ir_var_shader_in
) {
3236 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
3242 if (var
->data
.mode
== ir_var_shader_out
) {
3243 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
3252 case MESA_SHADER_TESS_CTRL
:
3253 case MESA_SHADER_TESS_EVAL
:
3254 case MESA_SHADER_GEOMETRY
:
3255 if (var
->data
.mode
== ir_var_shader_in
|| var
->data
.mode
== ir_var_shader_out
) {
3256 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
3265 case MESA_SHADER_FRAGMENT
:
3266 if (var
->data
.mode
== ir_var_shader_in
) {
3267 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
3273 if (var
->data
.mode
== ir_var_shader_out
) {
3274 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
3283 case MESA_SHADER_COMPUTE
:
3284 _mesa_glsl_error(loc
, state
,
3285 "compute shader variables cannot be given "
3286 "explicit locations");
3294 _mesa_glsl_error(loc
, state
,
3295 "%s cannot be given an explicit location in %s shader",
3297 _mesa_shader_stage_to_string(state
->stage
));
3299 var
->data
.explicit_location
= true;
3301 switch (state
->stage
) {
3302 case MESA_SHADER_VERTEX
:
3303 var
->data
.location
= (var
->data
.mode
== ir_var_shader_in
)
3304 ? (qual_location
+ VERT_ATTRIB_GENERIC0
)
3305 : (qual_location
+ VARYING_SLOT_VAR0
);
3308 case MESA_SHADER_TESS_CTRL
:
3309 case MESA_SHADER_TESS_EVAL
:
3310 case MESA_SHADER_GEOMETRY
:
3311 if (var
->data
.patch
)
3312 var
->data
.location
= qual_location
+ VARYING_SLOT_PATCH0
;
3314 var
->data
.location
= qual_location
+ VARYING_SLOT_VAR0
;
3317 case MESA_SHADER_FRAGMENT
:
3318 var
->data
.location
= (var
->data
.mode
== ir_var_shader_out
)
3319 ? (qual_location
+ FRAG_RESULT_DATA0
)
3320 : (qual_location
+ VARYING_SLOT_VAR0
);
3323 assert(!"Unexpected shader type");
3327 /* Check if index was set for the uniform instead of the function */
3328 if (qual
->flags
.q
.explicit_index
&& qual
->is_subroutine_decl()) {
3329 _mesa_glsl_error(loc
, state
, "an index qualifier can only be "
3330 "used with subroutine functions");
3334 unsigned qual_index
;
3335 if (qual
->flags
.q
.explicit_index
&&
3336 process_qualifier_constant(state
, loc
, "index", qual
->index
,
3338 /* From the GLSL 4.30 specification, section 4.4.2 (Output
3339 * Layout Qualifiers):
3341 * "It is also a compile-time error if a fragment shader
3342 * sets a layout index to less than 0 or greater than 1."
3344 * Older specifications don't mandate a behavior; we take
3345 * this as a clarification and always generate the error.
3347 if (qual_index
> 1) {
3348 _mesa_glsl_error(loc
, state
,
3349 "explicit index may only be 0 or 1");
3351 var
->data
.explicit_index
= true;
3352 var
->data
.index
= qual_index
;
3359 validate_storage_for_sampler_image_types(ir_variable
*var
,
3360 struct _mesa_glsl_parse_state
*state
,
3363 /* From section 4.1.7 of the GLSL 4.40 spec:
3365 * "[Opaque types] can only be declared as function
3366 * parameters or uniform-qualified variables."
3368 * From section 4.1.7 of the ARB_bindless_texture spec:
3370 * "Samplers may be declared as shader inputs and outputs, as uniform
3371 * variables, as temporary variables, and as function parameters."
3373 * From section 4.1.X of the ARB_bindless_texture spec:
3375 * "Images may be declared as shader inputs and outputs, as uniform
3376 * variables, as temporary variables, and as function parameters."
3378 if (state
->has_bindless()) {
3379 if (var
->data
.mode
!= ir_var_auto
&&
3380 var
->data
.mode
!= ir_var_uniform
&&
3381 var
->data
.mode
!= ir_var_shader_in
&&
3382 var
->data
.mode
!= ir_var_shader_out
&&
3383 var
->data
.mode
!= ir_var_function_in
&&
3384 var
->data
.mode
!= ir_var_function_out
&&
3385 var
->data
.mode
!= ir_var_function_inout
) {
3386 _mesa_glsl_error(loc
, state
, "bindless image/sampler variables may "
3387 "only be declared as shader inputs and outputs, as "
3388 "uniform variables, as temporary variables and as "
3389 "function parameters");
3393 if (var
->data
.mode
!= ir_var_uniform
&&
3394 var
->data
.mode
!= ir_var_function_in
) {
3395 _mesa_glsl_error(loc
, state
, "image/sampler variables may only be "
3396 "declared as function parameters or "
3397 "uniform-qualified global variables");
3405 validate_memory_qualifier_for_type(struct _mesa_glsl_parse_state
*state
,
3407 const struct ast_type_qualifier
*qual
,
3408 const glsl_type
*type
)
3410 /* From Section 4.10 (Memory Qualifiers) of the GLSL 4.50 spec:
3412 * "Memory qualifiers are only supported in the declarations of image
3413 * variables, buffer variables, and shader storage blocks; it is an error
3414 * to use such qualifiers in any other declarations.
3416 if (!type
->is_image() && !qual
->flags
.q
.buffer
) {
3417 if (qual
->flags
.q
.read_only
||
3418 qual
->flags
.q
.write_only
||
3419 qual
->flags
.q
.coherent
||
3420 qual
->flags
.q
._volatile
||
3421 qual
->flags
.q
.restrict_flag
) {
3422 _mesa_glsl_error(loc
, state
, "memory qualifiers may only be applied "
3423 "in the declarations of image variables, buffer "
3424 "variables, and shader storage blocks");
3432 validate_image_format_qualifier_for_type(struct _mesa_glsl_parse_state
*state
,
3434 const struct ast_type_qualifier
*qual
,
3435 const glsl_type
*type
)
3437 /* From section 4.4.6.2 (Format Layout Qualifiers) of the GLSL 4.50 spec:
3439 * "Format layout qualifiers can be used on image variable declarations
3440 * (those declared with a basic type having “image ” in its keyword)."
3442 if (!type
->is_image() && qual
->flags
.q
.explicit_image_format
) {
3443 _mesa_glsl_error(loc
, state
, "format layout qualifiers may only be "
3444 "applied to images");
3451 apply_image_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
3453 struct _mesa_glsl_parse_state
*state
,
3456 const glsl_type
*base_type
= var
->type
->without_array();
3458 if (!validate_image_format_qualifier_for_type(state
, loc
, qual
, base_type
) ||
3459 !validate_memory_qualifier_for_type(state
, loc
, qual
, base_type
))
3462 if (!base_type
->is_image())
3465 if (!validate_storage_for_sampler_image_types(var
, state
, loc
))
3468 var
->data
.memory_read_only
|= qual
->flags
.q
.read_only
;
3469 var
->data
.memory_write_only
|= qual
->flags
.q
.write_only
;
3470 var
->data
.memory_coherent
|= qual
->flags
.q
.coherent
;
3471 var
->data
.memory_volatile
|= qual
->flags
.q
._volatile
;
3472 var
->data
.memory_restrict
|= qual
->flags
.q
.restrict_flag
;
3474 if (qual
->flags
.q
.explicit_image_format
) {
3475 if (var
->data
.mode
== ir_var_function_in
) {
3476 _mesa_glsl_error(loc
, state
, "format qualifiers cannot be used on "
3477 "image function parameters");
3480 if (qual
->image_base_type
!= base_type
->sampled_type
) {
3481 _mesa_glsl_error(loc
, state
, "format qualifier doesn't match the base "
3482 "data type of the image");
3485 var
->data
.image_format
= qual
->image_format
;
3486 } else if (state
->has_image_load_formatted()) {
3487 if (var
->data
.mode
== ir_var_uniform
&&
3488 state
->EXT_shader_image_load_formatted_warn
) {
3489 _mesa_glsl_warning(loc
, state
, "GL_EXT_image_load_formatted used");
3492 if (var
->data
.mode
== ir_var_uniform
) {
3493 if (state
->es_shader
||
3494 !(state
->is_version(420, 310) || state
->ARB_shader_image_load_store_enable
)) {
3495 _mesa_glsl_error(loc
, state
, "all image uniforms must have a "
3496 "format layout qualifier");
3497 } else if (!qual
->flags
.q
.write_only
) {
3498 _mesa_glsl_error(loc
, state
, "image uniforms not qualified with "
3499 "`writeonly' must have a format layout qualifier");
3502 var
->data
.image_format
= PIPE_FORMAT_NONE
;
3505 /* From page 70 of the GLSL ES 3.1 specification:
3507 * "Except for image variables qualified with the format qualifiers r32f,
3508 * r32i, and r32ui, image variables must specify either memory qualifier
3509 * readonly or the memory qualifier writeonly."
3511 if (state
->es_shader
&&
3512 var
->data
.image_format
!= PIPE_FORMAT_R32_FLOAT
&&
3513 var
->data
.image_format
!= PIPE_FORMAT_R32_SINT
&&
3514 var
->data
.image_format
!= PIPE_FORMAT_R32_UINT
&&
3515 !var
->data
.memory_read_only
&&
3516 !var
->data
.memory_write_only
) {
3517 _mesa_glsl_error(loc
, state
, "image variables of format other than r32f, "
3518 "r32i or r32ui must be qualified `readonly' or "
3523 static inline const char*
3524 get_layout_qualifier_string(bool origin_upper_left
, bool pixel_center_integer
)
3526 if (origin_upper_left
&& pixel_center_integer
)
3527 return "origin_upper_left, pixel_center_integer";
3528 else if (origin_upper_left
)
3529 return "origin_upper_left";
3530 else if (pixel_center_integer
)
3531 return "pixel_center_integer";
3537 is_conflicting_fragcoord_redeclaration(struct _mesa_glsl_parse_state
*state
,
3538 const struct ast_type_qualifier
*qual
)
3540 /* If gl_FragCoord was previously declared, and the qualifiers were
3541 * different in any way, return true.
3543 if (state
->fs_redeclares_gl_fragcoord
) {
3544 return (state
->fs_pixel_center_integer
!= qual
->flags
.q
.pixel_center_integer
3545 || state
->fs_origin_upper_left
!= qual
->flags
.q
.origin_upper_left
);
3552 validate_array_dimensions(const glsl_type
*t
,
3553 struct _mesa_glsl_parse_state
*state
,
3555 if (t
->is_array()) {
3556 t
= t
->fields
.array
;
3557 while (t
->is_array()) {
3558 if (t
->is_unsized_array()) {
3559 _mesa_glsl_error(loc
, state
,
3560 "only the outermost array dimension can "
3565 t
= t
->fields
.array
;
3571 apply_bindless_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
3573 struct _mesa_glsl_parse_state
*state
,
3576 bool has_local_qualifiers
= qual
->flags
.q
.bindless_sampler
||
3577 qual
->flags
.q
.bindless_image
||
3578 qual
->flags
.q
.bound_sampler
||
3579 qual
->flags
.q
.bound_image
;
3581 /* The ARB_bindless_texture spec says:
3583 * "Modify Section 4.4.6 Opaque-Uniform Layout Qualifiers of the GLSL 4.30
3586 * "If these layout qualifiers are applied to other types of default block
3587 * uniforms, or variables with non-uniform storage, a compile-time error
3588 * will be generated."
3590 if (has_local_qualifiers
&& !qual
->flags
.q
.uniform
) {
3591 _mesa_glsl_error(loc
, state
, "ARB_bindless_texture layout qualifiers "
3592 "can only be applied to default block uniforms or "
3593 "variables with uniform storage");
3597 /* The ARB_bindless_texture spec doesn't state anything in this situation,
3598 * but it makes sense to only allow bindless_sampler/bound_sampler for
3599 * sampler types, and respectively bindless_image/bound_image for image
3602 if ((qual
->flags
.q
.bindless_sampler
|| qual
->flags
.q
.bound_sampler
) &&
3603 !var
->type
->contains_sampler()) {
3604 _mesa_glsl_error(loc
, state
, "bindless_sampler or bound_sampler can only "
3605 "be applied to sampler types");
3609 if ((qual
->flags
.q
.bindless_image
|| qual
->flags
.q
.bound_image
) &&
3610 !var
->type
->contains_image()) {
3611 _mesa_glsl_error(loc
, state
, "bindless_image or bound_image can only be "
3612 "applied to image types");
3616 /* The bindless_sampler/bindless_image (and respectively
3617 * bound_sampler/bound_image) layout qualifiers can be set at global and at
3620 if (var
->type
->contains_sampler() || var
->type
->contains_image()) {
3621 var
->data
.bindless
= qual
->flags
.q
.bindless_sampler
||
3622 qual
->flags
.q
.bindless_image
||
3623 state
->bindless_sampler_specified
||
3624 state
->bindless_image_specified
;
3626 var
->data
.bound
= qual
->flags
.q
.bound_sampler
||
3627 qual
->flags
.q
.bound_image
||
3628 state
->bound_sampler_specified
||
3629 state
->bound_image_specified
;
3634 apply_layout_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
3636 struct _mesa_glsl_parse_state
*state
,
3639 if (var
->name
!= NULL
&& strcmp(var
->name
, "gl_FragCoord") == 0) {
3641 /* Section 4.3.8.1, page 39 of GLSL 1.50 spec says:
3643 * "Within any shader, the first redeclarations of gl_FragCoord
3644 * must appear before any use of gl_FragCoord."
3646 * Generate a compiler error if above condition is not met by the
3649 ir_variable
*earlier
= state
->symbols
->get_variable("gl_FragCoord");
3650 if (earlier
!= NULL
&&
3651 earlier
->data
.used
&&
3652 !state
->fs_redeclares_gl_fragcoord
) {
3653 _mesa_glsl_error(loc
, state
,
3654 "gl_FragCoord used before its first redeclaration "
3655 "in fragment shader");
3658 /* Make sure all gl_FragCoord redeclarations specify the same layout
3661 if (is_conflicting_fragcoord_redeclaration(state
, qual
)) {
3662 const char *const qual_string
=
3663 get_layout_qualifier_string(qual
->flags
.q
.origin_upper_left
,
3664 qual
->flags
.q
.pixel_center_integer
);
3666 const char *const state_string
=
3667 get_layout_qualifier_string(state
->fs_origin_upper_left
,
3668 state
->fs_pixel_center_integer
);
3670 _mesa_glsl_error(loc
, state
,
3671 "gl_FragCoord redeclared with different layout "
3672 "qualifiers (%s) and (%s) ",
3676 state
->fs_origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
3677 state
->fs_pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
3678 state
->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers
=
3679 !qual
->flags
.q
.origin_upper_left
&& !qual
->flags
.q
.pixel_center_integer
;
3680 state
->fs_redeclares_gl_fragcoord
=
3681 state
->fs_origin_upper_left
||
3682 state
->fs_pixel_center_integer
||
3683 state
->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers
;
3686 if ((qual
->flags
.q
.origin_upper_left
|| qual
->flags
.q
.pixel_center_integer
)
3687 && (strcmp(var
->name
, "gl_FragCoord") != 0)) {
3688 const char *const qual_string
= (qual
->flags
.q
.origin_upper_left
)
3689 ? "origin_upper_left" : "pixel_center_integer";
3691 _mesa_glsl_error(loc
, state
,
3692 "layout qualifier `%s' can only be applied to "
3693 "fragment shader input `gl_FragCoord'",
3697 if (qual
->flags
.q
.explicit_location
) {
3698 apply_explicit_location(qual
, var
, state
, loc
);
3700 if (qual
->flags
.q
.explicit_component
) {
3701 unsigned qual_component
;
3702 if (process_qualifier_constant(state
, loc
, "component",
3703 qual
->component
, &qual_component
)) {
3704 const glsl_type
*type
= var
->type
->without_array();
3705 unsigned components
= type
->component_slots();
3707 if (type
->is_matrix() || type
->is_struct()) {
3708 _mesa_glsl_error(loc
, state
, "component layout qualifier "
3709 "cannot be applied to a matrix, a structure, "
3710 "a block, or an array containing any of "
3712 } else if (components
> 4 && type
->is_64bit()) {
3713 _mesa_glsl_error(loc
, state
, "component layout qualifier "
3714 "cannot be applied to dvec%u.",
3716 } else if (qual_component
!= 0 &&
3717 (qual_component
+ components
- 1) > 3) {
3718 _mesa_glsl_error(loc
, state
, "component overflow (%u > 3)",
3719 (qual_component
+ components
- 1));
3720 } else if (qual_component
== 1 && type
->is_64bit()) {
3721 /* We don't bother checking for 3 as it should be caught by the
3722 * overflow check above.
3724 _mesa_glsl_error(loc
, state
, "doubles cannot begin at "
3725 "component 1 or 3");
3727 var
->data
.explicit_component
= true;
3728 var
->data
.location_frac
= qual_component
;
3732 } else if (qual
->flags
.q
.explicit_index
) {
3733 if (!qual
->subroutine_list
)
3734 _mesa_glsl_error(loc
, state
,
3735 "explicit index requires explicit location");
3736 } else if (qual
->flags
.q
.explicit_component
) {
3737 _mesa_glsl_error(loc
, state
,
3738 "explicit component requires explicit location");
3741 if (qual
->flags
.q
.explicit_binding
) {
3742 apply_explicit_binding(state
, loc
, var
, var
->type
, qual
);
3745 if (state
->stage
== MESA_SHADER_GEOMETRY
&&
3746 qual
->flags
.q
.out
&& qual
->flags
.q
.stream
) {
3747 unsigned qual_stream
;
3748 if (process_qualifier_constant(state
, loc
, "stream", qual
->stream
,
3750 validate_stream_qualifier(loc
, state
, qual_stream
)) {
3751 var
->data
.stream
= qual_stream
;
3755 if (qual
->flags
.q
.out
&& qual
->flags
.q
.xfb_buffer
) {
3756 unsigned qual_xfb_buffer
;
3757 if (process_qualifier_constant(state
, loc
, "xfb_buffer",
3758 qual
->xfb_buffer
, &qual_xfb_buffer
) &&
3759 validate_xfb_buffer_qualifier(loc
, state
, qual_xfb_buffer
)) {
3760 var
->data
.xfb_buffer
= qual_xfb_buffer
;
3761 if (qual
->flags
.q
.explicit_xfb_buffer
)
3762 var
->data
.explicit_xfb_buffer
= true;
3766 if (qual
->flags
.q
.explicit_xfb_offset
) {
3767 unsigned qual_xfb_offset
;
3768 unsigned component_size
= var
->type
->contains_double() ? 8 : 4;
3770 if (process_qualifier_constant(state
, loc
, "xfb_offset",
3771 qual
->offset
, &qual_xfb_offset
) &&
3772 validate_xfb_offset_qualifier(loc
, state
, (int) qual_xfb_offset
,
3773 var
->type
, component_size
)) {
3774 var
->data
.offset
= qual_xfb_offset
;
3775 var
->data
.explicit_xfb_offset
= true;
3779 if (qual
->flags
.q
.explicit_xfb_stride
) {
3780 unsigned qual_xfb_stride
;
3781 if (process_qualifier_constant(state
, loc
, "xfb_stride",
3782 qual
->xfb_stride
, &qual_xfb_stride
)) {
3783 var
->data
.xfb_stride
= qual_xfb_stride
;
3784 var
->data
.explicit_xfb_stride
= true;
3788 if (var
->type
->contains_atomic()) {
3789 if (var
->data
.mode
== ir_var_uniform
) {
3790 if (var
->data
.explicit_binding
) {
3792 &state
->atomic_counter_offsets
[var
->data
.binding
];
3794 if (*offset
% ATOMIC_COUNTER_SIZE
)
3795 _mesa_glsl_error(loc
, state
,
3796 "misaligned atomic counter offset");
3798 var
->data
.offset
= *offset
;
3799 *offset
+= var
->type
->atomic_size();
3802 _mesa_glsl_error(loc
, state
,
3803 "atomic counters require explicit binding point");
3805 } else if (var
->data
.mode
!= ir_var_function_in
) {
3806 _mesa_glsl_error(loc
, state
, "atomic counters may only be declared as "
3807 "function parameters or uniform-qualified "
3808 "global variables");
3812 if (var
->type
->contains_sampler() &&
3813 !validate_storage_for_sampler_image_types(var
, state
, loc
))
3816 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
3817 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
3818 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
3819 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
3820 * These extensions and all following extensions that add the 'layout'
3821 * keyword have been modified to require the use of 'in' or 'out'.
3823 * The following extension do not allow the deprecated keywords:
3825 * GL_AMD_conservative_depth
3826 * GL_ARB_conservative_depth
3827 * GL_ARB_gpu_shader5
3828 * GL_ARB_separate_shader_objects
3829 * GL_ARB_tessellation_shader
3830 * GL_ARB_transform_feedback3
3831 * GL_ARB_uniform_buffer_object
3833 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
3834 * allow layout with the deprecated keywords.
3836 const bool relaxed_layout_qualifier_checking
=
3837 state
->ARB_fragment_coord_conventions_enable
;
3839 const bool uses_deprecated_qualifier
= qual
->flags
.q
.attribute
3840 || qual
->flags
.q
.varying
;
3841 if (qual
->has_layout() && uses_deprecated_qualifier
) {
3842 if (relaxed_layout_qualifier_checking
) {
3843 _mesa_glsl_warning(loc
, state
,
3844 "`layout' qualifier may not be used with "
3845 "`attribute' or `varying'");
3847 _mesa_glsl_error(loc
, state
,
3848 "`layout' qualifier may not be used with "
3849 "`attribute' or `varying'");
3853 /* Layout qualifiers for gl_FragDepth, which are enabled by extension
3854 * AMD_conservative_depth.
3856 if (qual
->flags
.q
.depth_type
3857 && !state
->is_version(420, 0)
3858 && !state
->AMD_conservative_depth_enable
3859 && !state
->ARB_conservative_depth_enable
) {
3860 _mesa_glsl_error(loc
, state
,
3861 "extension GL_AMD_conservative_depth or "
3862 "GL_ARB_conservative_depth must be enabled "
3863 "to use depth layout qualifiers");
3864 } else if (qual
->flags
.q
.depth_type
3865 && strcmp(var
->name
, "gl_FragDepth") != 0) {
3866 _mesa_glsl_error(loc
, state
,
3867 "depth layout qualifiers can be applied only to "
3871 switch (qual
->depth_type
) {
3873 var
->data
.depth_layout
= ir_depth_layout_any
;
3875 case ast_depth_greater
:
3876 var
->data
.depth_layout
= ir_depth_layout_greater
;
3878 case ast_depth_less
:
3879 var
->data
.depth_layout
= ir_depth_layout_less
;
3881 case ast_depth_unchanged
:
3882 var
->data
.depth_layout
= ir_depth_layout_unchanged
;
3885 var
->data
.depth_layout
= ir_depth_layout_none
;
3889 if (qual
->flags
.q
.std140
||
3890 qual
->flags
.q
.std430
||
3891 qual
->flags
.q
.packed
||
3892 qual
->flags
.q
.shared
) {
3893 _mesa_glsl_error(loc
, state
,
3894 "uniform and shader storage block layout qualifiers "
3895 "std140, std430, packed, and shared can only be "
3896 "applied to uniform or shader storage blocks, not "
3900 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
3901 validate_matrix_layout_for_type(state
, loc
, var
->type
, var
);
3904 /* From section 4.4.1.3 of the GLSL 4.50 specification (Fragment Shader
3907 * "Fragment shaders also allow the following layout qualifier on in only
3908 * (not with variable declarations)
3909 * layout-qualifier-id
3910 * early_fragment_tests
3913 if (qual
->flags
.q
.early_fragment_tests
) {
3914 _mesa_glsl_error(loc
, state
, "early_fragment_tests layout qualifier only "
3915 "valid in fragment shader input layout declaration.");
3918 if (qual
->flags
.q
.inner_coverage
) {
3919 _mesa_glsl_error(loc
, state
, "inner_coverage layout qualifier only "
3920 "valid in fragment shader input layout declaration.");
3923 if (qual
->flags
.q
.post_depth_coverage
) {
3924 _mesa_glsl_error(loc
, state
, "post_depth_coverage layout qualifier only "
3925 "valid in fragment shader input layout declaration.");
3928 if (state
->has_bindless())
3929 apply_bindless_qualifier_to_variable(qual
, var
, state
, loc
);
3931 if (qual
->flags
.q
.pixel_interlock_ordered
||
3932 qual
->flags
.q
.pixel_interlock_unordered
||
3933 qual
->flags
.q
.sample_interlock_ordered
||
3934 qual
->flags
.q
.sample_interlock_unordered
) {
3935 _mesa_glsl_error(loc
, state
, "interlock layout qualifiers: "
3936 "pixel_interlock_ordered, pixel_interlock_unordered, "
3937 "sample_interlock_ordered and sample_interlock_unordered, "
3938 "only valid in fragment shader input layout declaration.");
3943 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
3945 struct _mesa_glsl_parse_state
*state
,
3949 STATIC_ASSERT(sizeof(qual
->flags
.q
) <= sizeof(qual
->flags
.i
));
3951 if (qual
->flags
.q
.invariant
) {
3952 if (var
->data
.used
) {
3953 _mesa_glsl_error(loc
, state
,
3954 "variable `%s' may not be redeclared "
3955 "`invariant' after being used",
3958 var
->data
.explicit_invariant
= true;
3959 var
->data
.invariant
= true;
3963 if (qual
->flags
.q
.precise
) {
3964 if (var
->data
.used
) {
3965 _mesa_glsl_error(loc
, state
,
3966 "variable `%s' may not be redeclared "
3967 "`precise' after being used",
3970 var
->data
.precise
= 1;
3974 if (qual
->is_subroutine_decl() && !qual
->flags
.q
.uniform
) {
3975 _mesa_glsl_error(loc
, state
,
3976 "`subroutine' may only be applied to uniforms, "
3977 "subroutine type declarations, or function definitions");
3980 if (qual
->flags
.q
.constant
|| qual
->flags
.q
.attribute
3981 || qual
->flags
.q
.uniform
3982 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
3983 var
->data
.read_only
= 1;
3985 if (qual
->flags
.q
.centroid
)
3986 var
->data
.centroid
= 1;
3988 if (qual
->flags
.q
.sample
)
3989 var
->data
.sample
= 1;
3991 /* Precision qualifiers do not hold any meaning in Desktop GLSL */
3992 if (state
->es_shader
) {
3993 var
->data
.precision
=
3994 select_gles_precision(qual
->precision
, var
->type
, state
, loc
);
3997 if (qual
->flags
.q
.patch
)
3998 var
->data
.patch
= 1;
4000 if (qual
->flags
.q
.attribute
&& state
->stage
!= MESA_SHADER_VERTEX
) {
4001 var
->type
= glsl_type::error_type
;
4002 _mesa_glsl_error(loc
, state
,
4003 "`attribute' variables may not be declared in the "
4005 _mesa_shader_stage_to_string(state
->stage
));
4008 /* Disallow layout qualifiers which may only appear on layout declarations. */
4009 if (qual
->flags
.q
.prim_type
) {
4010 _mesa_glsl_error(loc
, state
,
4011 "Primitive type may only be specified on GS input or output "
4012 "layout declaration, not on variables.");
4015 /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
4017 * "However, the const qualifier cannot be used with out or inout."
4019 * The same section of the GLSL 4.40 spec further clarifies this saying:
4021 * "The const qualifier cannot be used with out or inout, or a
4022 * compile-time error results."
4024 if (is_parameter
&& qual
->flags
.q
.constant
&& qual
->flags
.q
.out
) {
4025 _mesa_glsl_error(loc
, state
,
4026 "`const' may not be applied to `out' or `inout' "
4027 "function parameters");
4030 /* If there is no qualifier that changes the mode of the variable, leave
4031 * the setting alone.
4033 assert(var
->data
.mode
!= ir_var_temporary
);
4034 if (qual
->flags
.q
.in
&& qual
->flags
.q
.out
)
4035 var
->data
.mode
= is_parameter
? ir_var_function_inout
: ir_var_shader_out
;
4036 else if (qual
->flags
.q
.in
)
4037 var
->data
.mode
= is_parameter
? ir_var_function_in
: ir_var_shader_in
;
4038 else if (qual
->flags
.q
.attribute
4039 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
4040 var
->data
.mode
= ir_var_shader_in
;
4041 else if (qual
->flags
.q
.out
)
4042 var
->data
.mode
= is_parameter
? ir_var_function_out
: ir_var_shader_out
;
4043 else if (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_VERTEX
))
4044 var
->data
.mode
= ir_var_shader_out
;
4045 else if (qual
->flags
.q
.uniform
)
4046 var
->data
.mode
= ir_var_uniform
;
4047 else if (qual
->flags
.q
.buffer
)
4048 var
->data
.mode
= ir_var_shader_storage
;
4049 else if (qual
->flags
.q
.shared_storage
)
4050 var
->data
.mode
= ir_var_shader_shared
;
4052 if (!is_parameter
&& state
->has_framebuffer_fetch() &&
4053 state
->stage
== MESA_SHADER_FRAGMENT
) {
4054 if (state
->is_version(130, 300))
4055 var
->data
.fb_fetch_output
= qual
->flags
.q
.in
&& qual
->flags
.q
.out
;
4057 var
->data
.fb_fetch_output
= (strcmp(var
->name
, "gl_LastFragData") == 0);
4060 if (var
->data
.fb_fetch_output
) {
4061 var
->data
.assigned
= true;
4062 var
->data
.memory_coherent
= !qual
->flags
.q
.non_coherent
;
4064 /* From the EXT_shader_framebuffer_fetch spec:
4066 * "It is an error to declare an inout fragment output not qualified
4067 * with layout(noncoherent) if the GL_EXT_shader_framebuffer_fetch
4068 * extension hasn't been enabled."
4070 if (var
->data
.memory_coherent
&&
4071 !state
->EXT_shader_framebuffer_fetch_enable
)
4072 _mesa_glsl_error(loc
, state
,
4073 "invalid declaration of framebuffer fetch output not "
4074 "qualified with layout(noncoherent)");
4077 /* From the EXT_shader_framebuffer_fetch spec:
4079 * "Fragment outputs declared inout may specify the following layout
4080 * qualifier: [...] noncoherent"
4082 if (qual
->flags
.q
.non_coherent
)
4083 _mesa_glsl_error(loc
, state
,
4084 "invalid layout(noncoherent) qualifier not part of "
4085 "framebuffer fetch output declaration");
4088 if (!is_parameter
&& is_varying_var(var
, state
->stage
)) {
4089 /* User-defined ins/outs are not permitted in compute shaders. */
4090 if (state
->stage
== MESA_SHADER_COMPUTE
) {
4091 _mesa_glsl_error(loc
, state
,
4092 "user-defined input and output variables are not "
4093 "permitted in compute shaders");
4096 /* This variable is being used to link data between shader stages (in
4097 * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
4098 * that is allowed for such purposes.
4100 * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
4102 * "The varying qualifier can be used only with the data types
4103 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
4106 * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
4107 * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
4109 * "Fragment inputs can only be signed and unsigned integers and
4110 * integer vectors, float, floating-point vectors, matrices, or
4111 * arrays of these. Structures cannot be input.
4113 * Similar text exists in the section on vertex shader outputs.
4115 * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
4116 * 3.00 spec allows structs as well. Varying structs are also allowed
4119 * From section 4.3.4 of the ARB_bindless_texture spec:
4121 * "(modify third paragraph of the section to allow sampler and image
4122 * types) ... Vertex shader inputs can only be float,
4123 * single-precision floating-point scalars, single-precision
4124 * floating-point vectors, matrices, signed and unsigned integers
4125 * and integer vectors, sampler and image types."
4127 * From section 4.3.6 of the ARB_bindless_texture spec:
4129 * "Output variables can only be floating-point scalars,
4130 * floating-point vectors, matrices, signed or unsigned integers or
4131 * integer vectors, sampler or image types, or arrays or structures
4134 switch (var
->type
->without_array()->base_type
) {
4135 case GLSL_TYPE_FLOAT
:
4136 /* Ok in all GLSL versions */
4138 case GLSL_TYPE_UINT
:
4140 if (state
->is_version(130, 300) || state
->EXT_gpu_shader4_enable
)
4142 _mesa_glsl_error(loc
, state
,
4143 "varying variables must be of base type float in %s",
4144 state
->get_version_string());
4146 case GLSL_TYPE_STRUCT
:
4147 if (state
->is_version(150, 300))
4149 _mesa_glsl_error(loc
, state
,
4150 "varying variables may not be of type struct");
4152 case GLSL_TYPE_DOUBLE
:
4153 case GLSL_TYPE_UINT64
:
4154 case GLSL_TYPE_INT64
:
4156 case GLSL_TYPE_SAMPLER
:
4157 case GLSL_TYPE_IMAGE
:
4158 if (state
->has_bindless())
4162 _mesa_glsl_error(loc
, state
, "illegal type for a varying variable");
4167 if (state
->all_invariant
&& var
->data
.mode
== ir_var_shader_out
) {
4168 var
->data
.explicit_invariant
= true;
4169 var
->data
.invariant
= true;
4172 var
->data
.interpolation
=
4173 interpret_interpolation_qualifier(qual
, var
->type
,
4174 (ir_variable_mode
) var
->data
.mode
,
4177 /* Does the declaration use the deprecated 'attribute' or 'varying'
4180 const bool uses_deprecated_qualifier
= qual
->flags
.q
.attribute
4181 || qual
->flags
.q
.varying
;
4184 /* Validate auxiliary storage qualifiers */
4186 /* From section 4.3.4 of the GLSL 1.30 spec:
4187 * "It is an error to use centroid in in a vertex shader."
4189 * From section 4.3.4 of the GLSL ES 3.00 spec:
4190 * "It is an error to use centroid in or interpolation qualifiers in
4191 * a vertex shader input."
4194 /* Section 4.3.6 of the GLSL 1.30 specification states:
4195 * "It is an error to use centroid out in a fragment shader."
4197 * The GL_ARB_shading_language_420pack extension specification states:
4198 * "It is an error to use auxiliary storage qualifiers or interpolation
4199 * qualifiers on an output in a fragment shader."
4201 if (qual
->flags
.q
.sample
&& (!is_varying_var(var
, state
->stage
) || uses_deprecated_qualifier
)) {
4202 _mesa_glsl_error(loc
, state
,
4203 "sample qualifier may only be used on `in` or `out` "
4204 "variables between shader stages");
4206 if (qual
->flags
.q
.centroid
&& !is_varying_var(var
, state
->stage
)) {
4207 _mesa_glsl_error(loc
, state
,
4208 "centroid qualifier may only be used with `in', "
4209 "`out' or `varying' variables between shader stages");
4212 if (qual
->flags
.q
.shared_storage
&& state
->stage
!= MESA_SHADER_COMPUTE
) {
4213 _mesa_glsl_error(loc
, state
,
4214 "the shared storage qualifiers can only be used with "
4218 apply_image_qualifier_to_variable(qual
, var
, state
, loc
);
4222 * Get the variable that is being redeclared by this declaration or if it
4223 * does not exist, the current declared variable.
4225 * Semantic checks to verify the validity of the redeclaration are also
4226 * performed. If semantic checks fail, compilation error will be emitted via
4227 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
4230 * A pointer to an existing variable in the current scope if the declaration
4231 * is a redeclaration, current variable otherwise. \c is_declared boolean
4232 * will return \c true if the declaration is a redeclaration, \c false
4235 static ir_variable
*
4236 get_variable_being_redeclared(ir_variable
**var_ptr
, YYLTYPE loc
,
4237 struct _mesa_glsl_parse_state
*state
,
4238 bool allow_all_redeclarations
,
4239 bool *is_redeclaration
)
4241 ir_variable
*var
= *var_ptr
;
4243 /* Check if this declaration is actually a re-declaration, either to
4244 * resize an array or add qualifiers to an existing variable.
4246 * This is allowed for variables in the current scope, or when at
4247 * global scope (for built-ins in the implicit outer scope).
4249 ir_variable
*earlier
= state
->symbols
->get_variable(var
->name
);
4250 if (earlier
== NULL
||
4251 (state
->current_function
!= NULL
&&
4252 !state
->symbols
->name_declared_this_scope(var
->name
))) {
4253 *is_redeclaration
= false;
4257 *is_redeclaration
= true;
4259 if (earlier
->data
.how_declared
== ir_var_declared_implicitly
) {
4260 /* Verify that the redeclaration of a built-in does not change the
4261 * storage qualifier. There are a couple special cases.
4263 * 1. Some built-in variables that are defined as 'in' in the
4264 * specification are implemented as system values. Allow
4265 * ir_var_system_value -> ir_var_shader_in.
4267 * 2. gl_LastFragData is implemented as a ir_var_shader_out, but the
4268 * specification requires that redeclarations omit any qualifier.
4269 * Allow ir_var_shader_out -> ir_var_auto for this one variable.
4271 if (earlier
->data
.mode
!= var
->data
.mode
&&
4272 !(earlier
->data
.mode
== ir_var_system_value
&&
4273 var
->data
.mode
== ir_var_shader_in
) &&
4274 !(strcmp(var
->name
, "gl_LastFragData") == 0 &&
4275 var
->data
.mode
== ir_var_auto
)) {
4276 _mesa_glsl_error(&loc
, state
,
4277 "redeclaration cannot change qualification of `%s'",
4282 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
4284 * "It is legal to declare an array without a size and then
4285 * later re-declare the same name as an array of the same
4286 * type and specify a size."
4288 if (earlier
->type
->is_unsized_array() && var
->type
->is_array()
4289 && (var
->type
->fields
.array
== earlier
->type
->fields
.array
)) {
4290 const int size
= var
->type
->array_size();
4291 check_builtin_array_max_size(var
->name
, size
, loc
, state
);
4292 if ((size
> 0) && (size
<= earlier
->data
.max_array_access
)) {
4293 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
4295 earlier
->data
.max_array_access
);
4298 earlier
->type
= var
->type
;
4302 } else if (earlier
->type
!= var
->type
) {
4303 _mesa_glsl_error(&loc
, state
,
4304 "redeclaration of `%s' has incorrect type",
4306 } else if ((state
->ARB_fragment_coord_conventions_enable
||
4307 state
->is_version(150, 0))
4308 && strcmp(var
->name
, "gl_FragCoord") == 0) {
4309 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
4312 * We don't really need to do anything here, just allow the
4313 * redeclaration. Any error on the gl_FragCoord is handled on the ast
4314 * level at apply_layout_qualifier_to_variable using the
4315 * ast_type_qualifier and _mesa_glsl_parse_state, or later at
4318 /* According to section 4.3.7 of the GLSL 1.30 spec,
4319 * the following built-in varaibles can be redeclared with an
4320 * interpolation qualifier:
4323 * * gl_FrontSecondaryColor
4324 * * gl_BackSecondaryColor
4326 * * gl_SecondaryColor
4328 } else if (state
->is_version(130, 0)
4329 && (strcmp(var
->name
, "gl_FrontColor") == 0
4330 || strcmp(var
->name
, "gl_BackColor") == 0
4331 || strcmp(var
->name
, "gl_FrontSecondaryColor") == 0
4332 || strcmp(var
->name
, "gl_BackSecondaryColor") == 0
4333 || strcmp(var
->name
, "gl_Color") == 0
4334 || strcmp(var
->name
, "gl_SecondaryColor") == 0)) {
4335 earlier
->data
.interpolation
= var
->data
.interpolation
;
4337 /* Layout qualifiers for gl_FragDepth. */
4338 } else if ((state
->is_version(420, 0) ||
4339 state
->AMD_conservative_depth_enable
||
4340 state
->ARB_conservative_depth_enable
)
4341 && strcmp(var
->name
, "gl_FragDepth") == 0) {
4343 /** From the AMD_conservative_depth spec:
4344 * Within any shader, the first redeclarations of gl_FragDepth
4345 * must appear before any use of gl_FragDepth.
4347 if (earlier
->data
.used
) {
4348 _mesa_glsl_error(&loc
, state
,
4349 "the first redeclaration of gl_FragDepth "
4350 "must appear before any use of gl_FragDepth");
4353 /* Prevent inconsistent redeclaration of depth layout qualifier. */
4354 if (earlier
->data
.depth_layout
!= ir_depth_layout_none
4355 && earlier
->data
.depth_layout
!= var
->data
.depth_layout
) {
4356 _mesa_glsl_error(&loc
, state
,
4357 "gl_FragDepth: depth layout is declared here "
4358 "as '%s, but it was previously declared as "
4360 depth_layout_string(var
->data
.depth_layout
),
4361 depth_layout_string(earlier
->data
.depth_layout
));
4364 earlier
->data
.depth_layout
= var
->data
.depth_layout
;
4366 } else if (state
->has_framebuffer_fetch() &&
4367 strcmp(var
->name
, "gl_LastFragData") == 0 &&
4368 var
->data
.mode
== ir_var_auto
) {
4369 /* According to the EXT_shader_framebuffer_fetch spec:
4371 * "By default, gl_LastFragData is declared with the mediump precision
4372 * qualifier. This can be changed by redeclaring the corresponding
4373 * variables with the desired precision qualifier."
4375 * "Fragment shaders may specify the following layout qualifier only for
4376 * redeclaring the built-in gl_LastFragData array [...]: noncoherent"
4378 earlier
->data
.precision
= var
->data
.precision
;
4379 earlier
->data
.memory_coherent
= var
->data
.memory_coherent
;
4381 } else if ((earlier
->data
.how_declared
== ir_var_declared_implicitly
&&
4382 state
->allow_builtin_variable_redeclaration
) ||
4383 allow_all_redeclarations
) {
4384 /* Allow verbatim redeclarations of built-in variables. Not explicitly
4385 * valid, but some applications do it.
4388 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
4395 * Generate the IR for an initializer in a variable declaration
4398 process_initializer(ir_variable
*var
, ast_declaration
*decl
,
4399 ast_fully_specified_type
*type
,
4400 exec_list
*initializer_instructions
,
4401 struct _mesa_glsl_parse_state
*state
)
4403 void *mem_ctx
= state
;
4404 ir_rvalue
*result
= NULL
;
4406 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
4408 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
4410 * "All uniform variables are read-only and are initialized either
4411 * directly by an application via API commands, or indirectly by
4414 if (var
->data
.mode
== ir_var_uniform
) {
4415 state
->check_version(120, 0, &initializer_loc
,
4416 "cannot initialize uniform %s",
4420 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
4422 * "Buffer variables cannot have initializers."
4424 if (var
->data
.mode
== ir_var_shader_storage
) {
4425 _mesa_glsl_error(&initializer_loc
, state
,
4426 "cannot initialize buffer variable %s",
4430 /* From section 4.1.7 of the GLSL 4.40 spec:
4432 * "Opaque variables [...] are initialized only through the
4433 * OpenGL API; they cannot be declared with an initializer in a
4436 * From section 4.1.7 of the ARB_bindless_texture spec:
4438 * "Samplers may be declared as shader inputs and outputs, as uniform
4439 * variables, as temporary variables, and as function parameters."
4441 * From section 4.1.X of the ARB_bindless_texture spec:
4443 * "Images may be declared as shader inputs and outputs, as uniform
4444 * variables, as temporary variables, and as function parameters."
4446 if (var
->type
->contains_atomic() ||
4447 (!state
->has_bindless() && var
->type
->contains_opaque())) {
4448 _mesa_glsl_error(&initializer_loc
, state
,
4449 "cannot initialize %s variable %s",
4450 var
->name
, state
->has_bindless() ? "atomic" : "opaque");
4453 if ((var
->data
.mode
== ir_var_shader_in
) && (state
->current_function
== NULL
)) {
4454 _mesa_glsl_error(&initializer_loc
, state
,
4455 "cannot initialize %s shader input / %s %s",
4456 _mesa_shader_stage_to_string(state
->stage
),
4457 (state
->stage
== MESA_SHADER_VERTEX
)
4458 ? "attribute" : "varying",
4462 if (var
->data
.mode
== ir_var_shader_out
&& state
->current_function
== NULL
) {
4463 _mesa_glsl_error(&initializer_loc
, state
,
4464 "cannot initialize %s shader output %s",
4465 _mesa_shader_stage_to_string(state
->stage
),
4469 /* If the initializer is an ast_aggregate_initializer, recursively store
4470 * type information from the LHS into it, so that its hir() function can do
4473 if (decl
->initializer
->oper
== ast_aggregate
)
4474 _mesa_ast_set_aggregate_type(var
->type
, decl
->initializer
);
4476 ir_dereference
*const lhs
= new(state
) ir_dereference_variable(var
);
4477 ir_rvalue
*rhs
= decl
->initializer
->hir(initializer_instructions
, state
);
4479 /* Calculate the constant value if this is a const or uniform
4482 * Section 4.3 (Storage Qualifiers) of the GLSL ES 1.00.17 spec says:
4484 * "Declarations of globals without a storage qualifier, or with
4485 * just the const qualifier, may include initializers, in which case
4486 * they will be initialized before the first line of main() is
4487 * executed. Such initializers must be a constant expression."
4489 * The same section of the GLSL ES 3.00.4 spec has similar language.
4491 if (type
->qualifier
.flags
.q
.constant
4492 || type
->qualifier
.flags
.q
.uniform
4493 || (state
->es_shader
&& state
->current_function
== NULL
)) {
4494 ir_rvalue
*new_rhs
= validate_assignment(state
, initializer_loc
,
4496 if (new_rhs
!= NULL
) {
4499 /* Section 4.3.3 (Constant Expressions) of the GLSL ES 3.00.4 spec
4502 * "A constant expression is one of
4506 * - an expression formed by an operator on operands that are
4507 * all constant expressions, including getting an element of
4508 * a constant array, or a field of a constant structure, or
4509 * components of a constant vector. However, the sequence
4510 * operator ( , ) and the assignment operators ( =, +=, ...)
4511 * are not included in the operators that can create a
4512 * constant expression."
4514 * Section 12.43 (Sequence operator and constant expressions) says:
4516 * "Should the following construct be allowed?
4520 * The expression within the brackets uses the sequence operator
4521 * (',') and returns the integer 3 so the construct is declaring
4522 * a single-dimensional array of size 3. In some languages, the
4523 * construct declares a two-dimensional array. It would be
4524 * preferable to make this construct illegal to avoid confusion.
4526 * One possibility is to change the definition of the sequence
4527 * operator so that it does not return a constant-expression and
4528 * hence cannot be used to declare an array size.
4530 * RESOLUTION: The result of a sequence operator is not a
4531 * constant-expression."
4533 * Section 4.3.3 (Constant Expressions) of the GLSL 4.30.9 spec
4534 * contains language almost identical to the section 4.3.3 in the
4535 * GLSL ES 3.00.4 spec. This is a new limitation for these GLSL
4538 ir_constant
*constant_value
=
4539 rhs
->constant_expression_value(mem_ctx
);
4541 if (!constant_value
||
4542 (state
->is_version(430, 300) &&
4543 decl
->initializer
->has_sequence_subexpression())) {
4544 const char *const variable_mode
=
4545 (type
->qualifier
.flags
.q
.constant
)
4547 : ((type
->qualifier
.flags
.q
.uniform
) ? "uniform" : "global");
4549 /* If ARB_shading_language_420pack is enabled, initializers of
4550 * const-qualified local variables do not have to be constant
4551 * expressions. Const-qualified global variables must still be
4552 * initialized with constant expressions.
4554 if (!state
->has_420pack()
4555 || state
->current_function
== NULL
) {
4556 _mesa_glsl_error(& initializer_loc
, state
,
4557 "initializer of %s variable `%s' must be a "
4558 "constant expression",
4561 if (var
->type
->is_numeric()) {
4562 /* Reduce cascading errors. */
4563 var
->constant_value
= type
->qualifier
.flags
.q
.constant
4564 ? ir_constant::zero(state
, var
->type
) : NULL
;
4568 rhs
= constant_value
;
4569 var
->constant_value
= type
->qualifier
.flags
.q
.constant
4570 ? constant_value
: NULL
;
4573 if (var
->type
->is_numeric()) {
4574 /* Reduce cascading errors. */
4575 rhs
= var
->constant_value
= type
->qualifier
.flags
.q
.constant
4576 ? ir_constant::zero(state
, var
->type
) : NULL
;
4581 if (rhs
&& !rhs
->type
->is_error()) {
4582 bool temp
= var
->data
.read_only
;
4583 if (type
->qualifier
.flags
.q
.constant
)
4584 var
->data
.read_only
= false;
4586 /* Never emit code to initialize a uniform.
4588 const glsl_type
*initializer_type
;
4589 bool error_emitted
= false;
4590 if (!type
->qualifier
.flags
.q
.uniform
) {
4592 do_assignment(initializer_instructions
, state
,
4594 &result
, true, true,
4595 type
->get_location());
4596 initializer_type
= result
->type
;
4598 initializer_type
= rhs
->type
;
4600 if (!error_emitted
) {
4601 var
->constant_initializer
= rhs
->constant_expression_value(mem_ctx
);
4602 var
->data
.has_initializer
= true;
4604 /* If the declared variable is an unsized array, it must inherrit
4605 * its full type from the initializer. A declaration such as
4607 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
4611 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
4613 * The assignment generated in the if-statement (below) will also
4614 * automatically handle this case for non-uniforms.
4616 * If the declared variable is not an array, the types must
4617 * already match exactly. As a result, the type assignment
4618 * here can be done unconditionally. For non-uniforms the call
4619 * to do_assignment can change the type of the initializer (via
4620 * the implicit conversion rules). For uniforms the initializer
4621 * must be a constant expression, and the type of that expression
4622 * was validated above.
4624 var
->type
= initializer_type
;
4627 var
->data
.read_only
= temp
;
4634 validate_layout_qualifier_vertex_count(struct _mesa_glsl_parse_state
*state
,
4635 YYLTYPE loc
, ir_variable
*var
,
4636 unsigned num_vertices
,
4638 const char *var_category
)
4640 if (var
->type
->is_unsized_array()) {
4641 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says:
4643 * All geometry shader input unsized array declarations will be
4644 * sized by an earlier input layout qualifier, when present, as per
4645 * the following table.
4647 * Followed by a table mapping each allowed input layout qualifier to
4648 * the corresponding input length.
4650 * Similarly for tessellation control shader outputs.
4652 if (num_vertices
!= 0)
4653 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
4656 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec
4657 * includes the following examples of compile-time errors:
4659 * // code sequence within one shader...
4660 * in vec4 Color1[]; // size unknown
4661 * ...Color1.length()...// illegal, length() unknown
4662 * in vec4 Color2[2]; // size is 2
4663 * ...Color1.length()...// illegal, Color1 still has no size
4664 * in vec4 Color3[3]; // illegal, input sizes are inconsistent
4665 * layout(lines) in; // legal, input size is 2, matching
4666 * in vec4 Color4[3]; // illegal, contradicts layout
4669 * To detect the case illustrated by Color3, we verify that the size of
4670 * an explicitly-sized array matches the size of any previously declared
4671 * explicitly-sized array. To detect the case illustrated by Color4, we
4672 * verify that the size of an explicitly-sized array is consistent with
4673 * any previously declared input layout.
4675 if (num_vertices
!= 0 && var
->type
->length
!= num_vertices
) {
4676 _mesa_glsl_error(&loc
, state
,
4677 "%s size contradicts previously declared layout "
4678 "(size is %u, but layout requires a size of %u)",
4679 var_category
, var
->type
->length
, num_vertices
);
4680 } else if (*size
!= 0 && var
->type
->length
!= *size
) {
4681 _mesa_glsl_error(&loc
, state
,
4682 "%s sizes are inconsistent (size is %u, but a "
4683 "previous declaration has size %u)",
4684 var_category
, var
->type
->length
, *size
);
4686 *size
= var
->type
->length
;
4692 handle_tess_ctrl_shader_output_decl(struct _mesa_glsl_parse_state
*state
,
4693 YYLTYPE loc
, ir_variable
*var
)
4695 unsigned num_vertices
= 0;
4697 if (state
->tcs_output_vertices_specified
) {
4698 if (!state
->out_qualifier
->vertices
->
4699 process_qualifier_constant(state
, "vertices",
4700 &num_vertices
, false)) {
4704 if (num_vertices
> state
->Const
.MaxPatchVertices
) {
4705 _mesa_glsl_error(&loc
, state
, "vertices (%d) exceeds "
4706 "GL_MAX_PATCH_VERTICES", num_vertices
);
4711 if (!var
->type
->is_array() && !var
->data
.patch
) {
4712 _mesa_glsl_error(&loc
, state
,
4713 "tessellation control shader outputs must be arrays");
4715 /* To avoid cascading failures, short circuit the checks below. */
4719 if (var
->data
.patch
)
4722 validate_layout_qualifier_vertex_count(state
, loc
, var
, num_vertices
,
4723 &state
->tcs_output_size
,
4724 "tessellation control shader output");
4728 * Do additional processing necessary for tessellation control/evaluation shader
4729 * input declarations. This covers both interface block arrays and bare input
4733 handle_tess_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
4734 YYLTYPE loc
, ir_variable
*var
)
4736 if (!var
->type
->is_array() && !var
->data
.patch
) {
4737 _mesa_glsl_error(&loc
, state
,
4738 "per-vertex tessellation shader inputs must be arrays");
4739 /* Avoid cascading failures. */
4743 if (var
->data
.patch
)
4746 /* The ARB_tessellation_shader spec says:
4748 * "Declaring an array size is optional. If no size is specified, it
4749 * will be taken from the implementation-dependent maximum patch size
4750 * (gl_MaxPatchVertices). If a size is specified, it must match the
4751 * maximum patch size; otherwise, a compile or link error will occur."
4753 * This text appears twice, once for TCS inputs, and again for TES inputs.
4755 if (var
->type
->is_unsized_array()) {
4756 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
4757 state
->Const
.MaxPatchVertices
);
4758 } else if (var
->type
->length
!= state
->Const
.MaxPatchVertices
) {
4759 _mesa_glsl_error(&loc
, state
,
4760 "per-vertex tessellation shader input arrays must be "
4761 "sized to gl_MaxPatchVertices (%d).",
4762 state
->Const
.MaxPatchVertices
);
4768 * Do additional processing necessary for geometry shader input declarations
4769 * (this covers both interface blocks arrays and bare input variables).
4772 handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
4773 YYLTYPE loc
, ir_variable
*var
)
4775 unsigned num_vertices
= 0;
4777 if (state
->gs_input_prim_type_specified
) {
4778 num_vertices
= vertices_per_prim(state
->in_qualifier
->prim_type
);
4781 /* Geometry shader input variables must be arrays. Caller should have
4782 * reported an error for this.
4784 if (!var
->type
->is_array()) {
4785 assert(state
->error
);
4787 /* To avoid cascading failures, short circuit the checks below. */
4791 validate_layout_qualifier_vertex_count(state
, loc
, var
, num_vertices
,
4792 &state
->gs_input_size
,
4793 "geometry shader input");
4797 validate_identifier(const char *identifier
, YYLTYPE loc
,
4798 struct _mesa_glsl_parse_state
*state
)
4800 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
4802 * "Identifiers starting with "gl_" are reserved for use by
4803 * OpenGL, and may not be declared in a shader as either a
4804 * variable or a function."
4806 if (is_gl_identifier(identifier
)) {
4807 _mesa_glsl_error(&loc
, state
,
4808 "identifier `%s' uses reserved `gl_' prefix",
4810 } else if (strstr(identifier
, "__")) {
4811 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
4814 * "In addition, all identifiers containing two
4815 * consecutive underscores (__) are reserved as
4816 * possible future keywords."
4818 * The intention is that names containing __ are reserved for internal
4819 * use by the implementation, and names prefixed with GL_ are reserved
4820 * for use by Khronos. Names simply containing __ are dangerous to use,
4821 * but should be allowed.
4823 * A future version of the GLSL specification will clarify this.
4825 _mesa_glsl_warning(&loc
, state
,
4826 "identifier `%s' uses reserved `__' string",
4832 ast_declarator_list::hir(exec_list
*instructions
,
4833 struct _mesa_glsl_parse_state
*state
)
4836 const struct glsl_type
*decl_type
;
4837 const char *type_name
= NULL
;
4838 ir_rvalue
*result
= NULL
;
4839 YYLTYPE loc
= this->get_location();
4841 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
4843 * "To ensure that a particular output variable is invariant, it is
4844 * necessary to use the invariant qualifier. It can either be used to
4845 * qualify a previously declared variable as being invariant
4847 * invariant gl_Position; // make existing gl_Position be invariant"
4849 * In these cases the parser will set the 'invariant' flag in the declarator
4850 * list, and the type will be NULL.
4852 if (this->invariant
) {
4853 assert(this->type
== NULL
);
4855 if (state
->current_function
!= NULL
) {
4856 _mesa_glsl_error(& loc
, state
,
4857 "all uses of `invariant' keyword must be at global "
4861 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
4862 assert(decl
->array_specifier
== NULL
);
4863 assert(decl
->initializer
== NULL
);
4865 ir_variable
*const earlier
=
4866 state
->symbols
->get_variable(decl
->identifier
);
4867 if (earlier
== NULL
) {
4868 _mesa_glsl_error(& loc
, state
,
4869 "undeclared variable `%s' cannot be marked "
4870 "invariant", decl
->identifier
);
4871 } else if (!is_allowed_invariant(earlier
, state
)) {
4872 _mesa_glsl_error(&loc
, state
,
4873 "`%s' cannot be marked invariant; interfaces between "
4874 "shader stages only.", decl
->identifier
);
4875 } else if (earlier
->data
.used
) {
4876 _mesa_glsl_error(& loc
, state
,
4877 "variable `%s' may not be redeclared "
4878 "`invariant' after being used",
4881 earlier
->data
.explicit_invariant
= true;
4882 earlier
->data
.invariant
= true;
4886 /* Invariant redeclarations do not have r-values.
4891 if (this->precise
) {
4892 assert(this->type
== NULL
);
4894 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
4895 assert(decl
->array_specifier
== NULL
);
4896 assert(decl
->initializer
== NULL
);
4898 ir_variable
*const earlier
=
4899 state
->symbols
->get_variable(decl
->identifier
);
4900 if (earlier
== NULL
) {
4901 _mesa_glsl_error(& loc
, state
,
4902 "undeclared variable `%s' cannot be marked "
4903 "precise", decl
->identifier
);
4904 } else if (state
->current_function
!= NULL
&&
4905 !state
->symbols
->name_declared_this_scope(decl
->identifier
)) {
4906 /* Note: we have to check if we're in a function, since
4907 * builtins are treated as having come from another scope.
4909 _mesa_glsl_error(& loc
, state
,
4910 "variable `%s' from an outer scope may not be "
4911 "redeclared `precise' in this scope",
4913 } else if (earlier
->data
.used
) {
4914 _mesa_glsl_error(& loc
, state
,
4915 "variable `%s' may not be redeclared "
4916 "`precise' after being used",
4919 earlier
->data
.precise
= true;
4923 /* Precise redeclarations do not have r-values either. */
4927 assert(this->type
!= NULL
);
4928 assert(!this->invariant
);
4929 assert(!this->precise
);
4931 /* GL_EXT_shader_image_load_store base type uses GLSL_TYPE_VOID as a special value to
4932 * indicate that it needs to be updated later (see glsl_parser.yy).
4933 * This is done here, based on the layout qualifier and the type of the image var
4935 if (this->type
->qualifier
.flags
.q
.explicit_image_format
&&
4936 this->type
->specifier
->type
->is_image() &&
4937 this->type
->qualifier
.image_base_type
== GLSL_TYPE_VOID
) {
4938 /* "The ARB_shader_image_load_store says:
4939 * If both extensions are enabled in the shading language, the "size*" layout
4940 * qualifiers are treated as format qualifiers, and are mapped to equivalent
4941 * format qualifiers in the table below, according to the type of image
4943 * image* iimage* uimage*
4944 * -------- -------- --------
4945 * size1x8 n/a r8i r8ui
4946 * size1x16 r16f r16i r16ui
4947 * size1x32 r32f r32i r32ui
4948 * size2x32 rg32f rg32i rg32ui
4949 * size4x32 rgba32f rgba32i rgba32ui"
4951 if (strncmp(this->type
->specifier
->type_name
, "image", strlen("image")) == 0) {
4952 switch (this->type
->qualifier
.image_format
) {
4953 case PIPE_FORMAT_R8_SINT
:
4954 /* No valid qualifier in this case, driver will need to look at
4955 * the underlying image's format (just like no qualifier being
4958 this->type
->qualifier
.image_format
= PIPE_FORMAT_NONE
;
4960 case PIPE_FORMAT_R16_SINT
:
4961 this->type
->qualifier
.image_format
= PIPE_FORMAT_R16_FLOAT
;
4963 case PIPE_FORMAT_R32_SINT
:
4964 this->type
->qualifier
.image_format
= PIPE_FORMAT_R32_FLOAT
;
4966 case PIPE_FORMAT_R32G32_SINT
:
4967 this->type
->qualifier
.image_format
= PIPE_FORMAT_R32G32_FLOAT
;
4969 case PIPE_FORMAT_R32G32B32A32_SINT
:
4970 this->type
->qualifier
.image_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
4973 unreachable("Unknown image format");
4975 this->type
->qualifier
.image_base_type
= GLSL_TYPE_FLOAT
;
4976 } else if (strncmp(this->type
->specifier
->type_name
, "uimage", strlen("uimage")) == 0) {
4977 switch (this->type
->qualifier
.image_format
) {
4978 case PIPE_FORMAT_R8_SINT
:
4979 this->type
->qualifier
.image_format
= PIPE_FORMAT_R8_UINT
;
4981 case PIPE_FORMAT_R16_SINT
:
4982 this->type
->qualifier
.image_format
= PIPE_FORMAT_R16_UINT
;
4984 case PIPE_FORMAT_R32_SINT
:
4985 this->type
->qualifier
.image_format
= PIPE_FORMAT_R32_UINT
;
4987 case PIPE_FORMAT_R32G32_SINT
:
4988 this->type
->qualifier
.image_format
= PIPE_FORMAT_R32G32_UINT
;
4990 case PIPE_FORMAT_R32G32B32A32_SINT
:
4991 this->type
->qualifier
.image_format
= PIPE_FORMAT_R32G32B32A32_UINT
;
4994 unreachable("Unknown image format");
4996 this->type
->qualifier
.image_base_type
= GLSL_TYPE_UINT
;
4997 } else if (strncmp(this->type
->specifier
->type_name
, "iimage", strlen("iimage")) == 0) {
4998 this->type
->qualifier
.image_base_type
= GLSL_TYPE_INT
;
5004 /* The type specifier may contain a structure definition. Process that
5005 * before any of the variable declarations.
5007 (void) this->type
->specifier
->hir(instructions
, state
);
5009 decl_type
= this->type
->glsl_type(& type_name
, state
);
5011 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
5012 * "Buffer variables may only be declared inside interface blocks
5013 * (section 4.3.9 “Interface Blocks”), which are then referred to as
5014 * shader storage blocks. It is a compile-time error to declare buffer
5015 * variables at global scope (outside a block)."
5017 if (type
->qualifier
.flags
.q
.buffer
&& !decl_type
->is_interface()) {
5018 _mesa_glsl_error(&loc
, state
,
5019 "buffer variables cannot be declared outside "
5020 "interface blocks");
5023 /* An offset-qualified atomic counter declaration sets the default
5024 * offset for the next declaration within the same atomic counter
5027 if (decl_type
&& decl_type
->contains_atomic()) {
5028 if (type
->qualifier
.flags
.q
.explicit_binding
&&
5029 type
->qualifier
.flags
.q
.explicit_offset
) {
5030 unsigned qual_binding
;
5031 unsigned qual_offset
;
5032 if (process_qualifier_constant(state
, &loc
, "binding",
5033 type
->qualifier
.binding
,
5035 && process_qualifier_constant(state
, &loc
, "offset",
5036 type
->qualifier
.offset
,
5038 if (qual_binding
< ARRAY_SIZE(state
->atomic_counter_offsets
))
5039 state
->atomic_counter_offsets
[qual_binding
] = qual_offset
;
5043 ast_type_qualifier allowed_atomic_qual_mask
;
5044 allowed_atomic_qual_mask
.flags
.i
= 0;
5045 allowed_atomic_qual_mask
.flags
.q
.explicit_binding
= 1;
5046 allowed_atomic_qual_mask
.flags
.q
.explicit_offset
= 1;
5047 allowed_atomic_qual_mask
.flags
.q
.uniform
= 1;
5049 type
->qualifier
.validate_flags(&loc
, state
, allowed_atomic_qual_mask
,
5050 "invalid layout qualifier for",
5054 if (this->declarations
.is_empty()) {
5055 /* If there is no structure involved in the program text, there are two
5056 * possible scenarios:
5058 * - The program text contained something like 'vec4;'. This is an
5059 * empty declaration. It is valid but weird. Emit a warning.
5061 * - The program text contained something like 'S;' and 'S' is not the
5062 * name of a known structure type. This is both invalid and weird.
5065 * - The program text contained something like 'mediump float;'
5066 * when the programmer probably meant 'precision mediump
5067 * float;' Emit a warning with a description of what they
5068 * probably meant to do.
5070 * Note that if decl_type is NULL and there is a structure involved,
5071 * there must have been some sort of error with the structure. In this
5072 * case we assume that an error was already generated on this line of
5073 * code for the structure. There is no need to generate an additional,
5076 assert(this->type
->specifier
->structure
== NULL
|| decl_type
!= NULL
5079 if (decl_type
== NULL
) {
5080 _mesa_glsl_error(&loc
, state
,
5081 "invalid type `%s' in empty declaration",
5084 if (decl_type
->is_array()) {
5085 /* From Section 13.22 (Array Declarations) of the GLSL ES 3.2
5088 * "... any declaration that leaves the size undefined is
5089 * disallowed as this would add complexity and there are no
5092 if (state
->es_shader
&& decl_type
->is_unsized_array()) {
5093 _mesa_glsl_error(&loc
, state
, "array size must be explicitly "
5094 "or implicitly defined");
5097 /* From Section 4.12 (Empty Declarations) of the GLSL 4.5 spec:
5099 * "The combinations of types and qualifiers that cause
5100 * compile-time or link-time errors are the same whether or not
5101 * the declaration is empty."
5103 validate_array_dimensions(decl_type
, state
, &loc
);
5106 if (decl_type
->is_atomic_uint()) {
5107 /* Empty atomic counter declarations are allowed and useful
5108 * to set the default offset qualifier.
5111 } else if (this->type
->qualifier
.precision
!= ast_precision_none
) {
5112 if (this->type
->specifier
->structure
!= NULL
) {
5113 _mesa_glsl_error(&loc
, state
,
5114 "precision qualifiers can't be applied "
5117 static const char *const precision_names
[] = {
5124 _mesa_glsl_warning(&loc
, state
,
5125 "empty declaration with precision "
5126 "qualifier, to set the default precision, "
5127 "use `precision %s %s;'",
5128 precision_names
[this->type
->
5129 qualifier
.precision
],
5132 } else if (this->type
->specifier
->structure
== NULL
) {
5133 _mesa_glsl_warning(&loc
, state
, "empty declaration");
5138 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
5139 const struct glsl_type
*var_type
;
5141 const char *identifier
= decl
->identifier
;
5142 /* FINISHME: Emit a warning if a variable declaration shadows a
5143 * FINISHME: declaration at a higher scope.
5146 if ((decl_type
== NULL
) || decl_type
->is_void()) {
5147 if (type_name
!= NULL
) {
5148 _mesa_glsl_error(& loc
, state
,
5149 "invalid type `%s' in declaration of `%s'",
5150 type_name
, decl
->identifier
);
5152 _mesa_glsl_error(& loc
, state
,
5153 "invalid type in declaration of `%s'",
5159 if (this->type
->qualifier
.is_subroutine_decl()) {
5163 t
= state
->symbols
->get_type(this->type
->specifier
->type_name
);
5165 _mesa_glsl_error(& loc
, state
,
5166 "invalid type in declaration of `%s'",
5168 name
= ralloc_asprintf(ctx
, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state
->stage
), decl
->identifier
);
5173 var_type
= process_array_type(&loc
, decl_type
, decl
->array_specifier
,
5176 var
= new(ctx
) ir_variable(var_type
, identifier
, ir_var_auto
);
5178 /* The 'varying in' and 'varying out' qualifiers can only be used with
5179 * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
5182 if (this->type
->qualifier
.flags
.q
.varying
) {
5183 if (this->type
->qualifier
.flags
.q
.in
) {
5184 _mesa_glsl_error(& loc
, state
,
5185 "`varying in' qualifier in declaration of "
5186 "`%s' only valid for geometry shaders using "
5187 "ARB_geometry_shader4 or EXT_geometry_shader4",
5189 } else if (this->type
->qualifier
.flags
.q
.out
) {
5190 _mesa_glsl_error(& loc
, state
,
5191 "`varying out' qualifier in declaration of "
5192 "`%s' only valid for geometry shaders using "
5193 "ARB_geometry_shader4 or EXT_geometry_shader4",
5198 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
5200 * "Global variables can only use the qualifiers const,
5201 * attribute, uniform, or varying. Only one may be
5204 * Local variables can only use the qualifier const."
5206 * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
5207 * any extension that adds the 'layout' keyword.
5209 if (!state
->is_version(130, 300)
5210 && !state
->has_explicit_attrib_location()
5211 && !state
->has_separate_shader_objects()
5212 && !state
->ARB_fragment_coord_conventions_enable
) {
5213 /* GL_EXT_gpu_shader4 only allows "varying out" on fragment shader
5214 * outputs. (the varying flag is not set by the parser)
5216 if (this->type
->qualifier
.flags
.q
.out
&&
5217 (!state
->EXT_gpu_shader4_enable
||
5218 state
->stage
!= MESA_SHADER_FRAGMENT
)) {
5219 _mesa_glsl_error(& loc
, state
,
5220 "`out' qualifier in declaration of `%s' "
5221 "only valid for function parameters in %s",
5222 decl
->identifier
, state
->get_version_string());
5224 if (this->type
->qualifier
.flags
.q
.in
) {
5225 _mesa_glsl_error(& loc
, state
,
5226 "`in' qualifier in declaration of `%s' "
5227 "only valid for function parameters in %s",
5228 decl
->identifier
, state
->get_version_string());
5230 /* FINISHME: Test for other invalid qualifiers. */
5233 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
5235 apply_layout_qualifier_to_variable(&this->type
->qualifier
, var
, state
,
5238 if ((var
->data
.mode
== ir_var_auto
|| var
->data
.mode
== ir_var_temporary
5239 || var
->data
.mode
== ir_var_shader_out
)
5240 && (var
->type
->is_numeric() || var
->type
->is_boolean())
5241 && state
->zero_init
) {
5242 const ir_constant_data data
= { { 0 } };
5243 var
->data
.has_initializer
= true;
5244 var
->constant_initializer
= new(var
) ir_constant(var
->type
, &data
);
5247 if (this->type
->qualifier
.flags
.q
.invariant
) {
5248 if (!is_allowed_invariant(var
, state
)) {
5249 _mesa_glsl_error(&loc
, state
,
5250 "`%s' cannot be marked invariant; interfaces between "
5251 "shader stages only", var
->name
);
5255 if (state
->current_function
!= NULL
) {
5256 const char *mode
= NULL
;
5257 const char *extra
= "";
5259 /* There is no need to check for 'inout' here because the parser will
5260 * only allow that in function parameter lists.
5262 if (this->type
->qualifier
.flags
.q
.attribute
) {
5264 } else if (this->type
->qualifier
.is_subroutine_decl()) {
5265 mode
= "subroutine uniform";
5266 } else if (this->type
->qualifier
.flags
.q
.uniform
) {
5268 } else if (this->type
->qualifier
.flags
.q
.varying
) {
5270 } else if (this->type
->qualifier
.flags
.q
.in
) {
5272 extra
= " or in function parameter list";
5273 } else if (this->type
->qualifier
.flags
.q
.out
) {
5275 extra
= " or in function parameter list";
5279 _mesa_glsl_error(& loc
, state
,
5280 "%s variable `%s' must be declared at "
5282 mode
, var
->name
, extra
);
5284 } else if (var
->data
.mode
== ir_var_shader_in
) {
5285 var
->data
.read_only
= true;
5287 if (state
->stage
== MESA_SHADER_VERTEX
) {
5288 bool error_emitted
= false;
5290 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
5292 * "Vertex shader inputs can only be float, floating-point
5293 * vectors, matrices, signed and unsigned integers and integer
5294 * vectors. Vertex shader inputs can also form arrays of these
5295 * types, but not structures."
5297 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
5299 * "Vertex shader inputs can only be float, floating-point
5300 * vectors, matrices, signed and unsigned integers and integer
5301 * vectors. They cannot be arrays or structures."
5303 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
5305 * "The attribute qualifier can be used only with float,
5306 * floating-point vectors, and matrices. Attribute variables
5307 * cannot be declared as arrays or structures."
5309 * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
5311 * "Vertex shader inputs can only be float, floating-point
5312 * vectors, matrices, signed and unsigned integers and integer
5313 * vectors. Vertex shader inputs cannot be arrays or
5316 * From section 4.3.4 of the ARB_bindless_texture spec:
5318 * "(modify third paragraph of the section to allow sampler and
5319 * image types) ... Vertex shader inputs can only be float,
5320 * single-precision floating-point scalars, single-precision
5321 * floating-point vectors, matrices, signed and unsigned
5322 * integers and integer vectors, sampler and image types."
5324 const glsl_type
*check_type
= var
->type
->without_array();
5326 switch (check_type
->base_type
) {
5327 case GLSL_TYPE_FLOAT
:
5329 case GLSL_TYPE_UINT64
:
5330 case GLSL_TYPE_INT64
:
5332 case GLSL_TYPE_UINT
:
5334 if (state
->is_version(120, 300) || state
->EXT_gpu_shader4_enable
)
5336 case GLSL_TYPE_DOUBLE
:
5337 if (check_type
->is_double() && (state
->is_version(410, 0) || state
->ARB_vertex_attrib_64bit_enable
))
5339 case GLSL_TYPE_SAMPLER
:
5340 if (check_type
->is_sampler() && state
->has_bindless())
5342 case GLSL_TYPE_IMAGE
:
5343 if (check_type
->is_image() && state
->has_bindless())
5347 _mesa_glsl_error(& loc
, state
,
5348 "vertex shader input / attribute cannot have "
5350 var
->type
->is_array() ? "array of " : "",
5352 error_emitted
= true;
5355 if (!error_emitted
&& var
->type
->is_array() &&
5356 !state
->check_version(150, 0, &loc
,
5357 "vertex shader input / attribute "
5358 "cannot have array type")) {
5359 error_emitted
= true;
5361 } else if (state
->stage
== MESA_SHADER_GEOMETRY
) {
5362 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
5364 * Geometry shader input variables get the per-vertex values
5365 * written out by vertex shader output variables of the same
5366 * names. Since a geometry shader operates on a set of
5367 * vertices, each input varying variable (or input block, see
5368 * interface blocks below) needs to be declared as an array.
5370 if (!var
->type
->is_array()) {
5371 _mesa_glsl_error(&loc
, state
,
5372 "geometry shader inputs must be arrays");
5375 handle_geometry_shader_input_decl(state
, loc
, var
);
5376 } else if (state
->stage
== MESA_SHADER_FRAGMENT
) {
5377 /* From section 4.3.4 (Input Variables) of the GLSL ES 3.10 spec:
5379 * It is a compile-time error to declare a fragment shader
5380 * input with, or that contains, any of the following types:
5384 * * An array of arrays
5385 * * An array of structures
5386 * * A structure containing an array
5387 * * A structure containing a structure
5389 if (state
->es_shader
) {
5390 const glsl_type
*check_type
= var
->type
->without_array();
5391 if (check_type
->is_boolean() ||
5392 check_type
->contains_opaque()) {
5393 _mesa_glsl_error(&loc
, state
,
5394 "fragment shader input cannot have type %s",
5397 if (var
->type
->is_array() &&
5398 var
->type
->fields
.array
->is_array()) {
5399 _mesa_glsl_error(&loc
, state
,
5401 "cannot have an array of arrays",
5402 _mesa_shader_stage_to_string(state
->stage
));
5404 if (var
->type
->is_array() &&
5405 var
->type
->fields
.array
->is_struct()) {
5406 _mesa_glsl_error(&loc
, state
,
5407 "fragment shader input "
5408 "cannot have an array of structs");
5410 if (var
->type
->is_struct()) {
5411 for (unsigned i
= 0; i
< var
->type
->length
; i
++) {
5412 if (var
->type
->fields
.structure
[i
].type
->is_array() ||
5413 var
->type
->fields
.structure
[i
].type
->is_struct())
5414 _mesa_glsl_error(&loc
, state
,
5415 "fragment shader input cannot have "
5416 "a struct that contains an "
5421 } else if (state
->stage
== MESA_SHADER_TESS_CTRL
||
5422 state
->stage
== MESA_SHADER_TESS_EVAL
) {
5423 handle_tess_shader_input_decl(state
, loc
, var
);
5425 } else if (var
->data
.mode
== ir_var_shader_out
) {
5426 const glsl_type
*check_type
= var
->type
->without_array();
5428 /* From section 4.3.6 (Output variables) of the GLSL 4.40 spec:
5430 * It is a compile-time error to declare a fragment shader output
5431 * that contains any of the following:
5433 * * A Boolean type (bool, bvec2 ...)
5434 * * A double-precision scalar or vector (double, dvec2 ...)
5439 if (state
->stage
== MESA_SHADER_FRAGMENT
) {
5440 if (check_type
->is_struct() || check_type
->is_matrix())
5441 _mesa_glsl_error(&loc
, state
,
5442 "fragment shader output "
5443 "cannot have struct or matrix type");
5444 switch (check_type
->base_type
) {
5445 case GLSL_TYPE_UINT
:
5447 case GLSL_TYPE_FLOAT
:
5450 _mesa_glsl_error(&loc
, state
,
5451 "fragment shader output cannot have "
5452 "type %s", check_type
->name
);
5456 /* From section 4.3.6 (Output Variables) of the GLSL ES 3.10 spec:
5458 * It is a compile-time error to declare a vertex shader output
5459 * with, or that contains, any of the following types:
5463 * * An array of arrays
5464 * * An array of structures
5465 * * A structure containing an array
5466 * * A structure containing a structure
5468 * It is a compile-time error to declare a fragment shader output
5469 * with, or that contains, any of the following types:
5475 * * An array of array
5477 * ES 3.20 updates this to apply to tessellation and geometry shaders
5478 * as well. Because there are per-vertex arrays in the new stages,
5479 * it strikes the "array of..." rules and replaces them with these:
5481 * * For per-vertex-arrayed variables (applies to tessellation
5482 * control, tessellation evaluation and geometry shaders):
5484 * * Per-vertex-arrayed arrays of arrays
5485 * * Per-vertex-arrayed arrays of structures
5487 * * For non-per-vertex-arrayed variables:
5489 * * An array of arrays
5490 * * An array of structures
5492 * which basically says to unwrap the per-vertex aspect and apply
5495 if (state
->es_shader
) {
5496 if (var
->type
->is_array() &&
5497 var
->type
->fields
.array
->is_array()) {
5498 _mesa_glsl_error(&loc
, state
,
5500 "cannot have an array of arrays",
5501 _mesa_shader_stage_to_string(state
->stage
));
5503 if (state
->stage
<= MESA_SHADER_GEOMETRY
) {
5504 const glsl_type
*type
= var
->type
;
5506 if (state
->stage
== MESA_SHADER_TESS_CTRL
&&
5507 !var
->data
.patch
&& var
->type
->is_array()) {
5508 type
= var
->type
->fields
.array
;
5511 if (type
->is_array() && type
->fields
.array
->is_struct()) {
5512 _mesa_glsl_error(&loc
, state
,
5513 "%s shader output cannot have "
5514 "an array of structs",
5515 _mesa_shader_stage_to_string(state
->stage
));
5517 if (type
->is_struct()) {
5518 for (unsigned i
= 0; i
< type
->length
; i
++) {
5519 if (type
->fields
.structure
[i
].type
->is_array() ||
5520 type
->fields
.structure
[i
].type
->is_struct())
5521 _mesa_glsl_error(&loc
, state
,
5522 "%s shader output cannot have a "
5523 "struct that contains an "
5525 _mesa_shader_stage_to_string(state
->stage
));
5531 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
5532 handle_tess_ctrl_shader_output_decl(state
, loc
, var
);
5534 } else if (var
->type
->contains_subroutine()) {
5535 /* declare subroutine uniforms as hidden */
5536 var
->data
.how_declared
= ir_var_hidden
;
5539 /* From section 4.3.4 of the GLSL 4.00 spec:
5540 * "Input variables may not be declared using the patch in qualifier
5541 * in tessellation control or geometry shaders."
5543 * From section 4.3.6 of the GLSL 4.00 spec:
5544 * "It is an error to use patch out in a vertex, tessellation
5545 * evaluation, or geometry shader."
5547 * This doesn't explicitly forbid using them in a fragment shader, but
5548 * that's probably just an oversight.
5550 if (state
->stage
!= MESA_SHADER_TESS_EVAL
5551 && this->type
->qualifier
.flags
.q
.patch
5552 && this->type
->qualifier
.flags
.q
.in
) {
5554 _mesa_glsl_error(&loc
, state
, "'patch in' can only be used in a "
5555 "tessellation evaluation shader");
5558 if (state
->stage
!= MESA_SHADER_TESS_CTRL
5559 && this->type
->qualifier
.flags
.q
.patch
5560 && this->type
->qualifier
.flags
.q
.out
) {
5562 _mesa_glsl_error(&loc
, state
, "'patch out' can only be used in a "
5563 "tessellation control shader");
5566 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
5568 if (this->type
->qualifier
.precision
!= ast_precision_none
) {
5569 state
->check_precision_qualifiers_allowed(&loc
);
5572 if (this->type
->qualifier
.precision
!= ast_precision_none
&&
5573 !precision_qualifier_allowed(var
->type
)) {
5574 _mesa_glsl_error(&loc
, state
,
5575 "precision qualifiers apply only to floating point"
5576 ", integer and opaque types");
5579 /* From section 4.1.7 of the GLSL 4.40 spec:
5581 * "[Opaque types] can only be declared as function
5582 * parameters or uniform-qualified variables."
5584 * From section 4.1.7 of the ARB_bindless_texture spec:
5586 * "Samplers may be declared as shader inputs and outputs, as uniform
5587 * variables, as temporary variables, and as function parameters."
5589 * From section 4.1.X of the ARB_bindless_texture spec:
5591 * "Images may be declared as shader inputs and outputs, as uniform
5592 * variables, as temporary variables, and as function parameters."
5594 if (!this->type
->qualifier
.flags
.q
.uniform
&&
5595 (var_type
->contains_atomic() ||
5596 (!state
->has_bindless() && var_type
->contains_opaque()))) {
5597 _mesa_glsl_error(&loc
, state
,
5598 "%s variables must be declared uniform",
5599 state
->has_bindless() ? "atomic" : "opaque");
5602 /* Process the initializer and add its instructions to a temporary
5603 * list. This list will be added to the instruction stream (below) after
5604 * the declaration is added. This is done because in some cases (such as
5605 * redeclarations) the declaration may not actually be added to the
5606 * instruction stream.
5608 exec_list initializer_instructions
;
5610 /* Examine var name here since var may get deleted in the next call */
5611 bool var_is_gl_id
= is_gl_identifier(var
->name
);
5613 bool is_redeclaration
;
5614 var
= get_variable_being_redeclared(&var
, decl
->get_location(), state
,
5615 false /* allow_all_redeclarations */,
5617 if (is_redeclaration
) {
5619 var
->data
.how_declared
== ir_var_declared_in_block
) {
5620 _mesa_glsl_error(&loc
, state
,
5621 "`%s' has already been redeclared using "
5622 "gl_PerVertex", var
->name
);
5624 var
->data
.how_declared
= ir_var_declared_normally
;
5627 if (decl
->initializer
!= NULL
) {
5628 result
= process_initializer(var
,
5630 &initializer_instructions
, state
);
5632 validate_array_dimensions(var_type
, state
, &loc
);
5635 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
5637 * "It is an error to write to a const variable outside of
5638 * its declaration, so they must be initialized when
5641 if (this->type
->qualifier
.flags
.q
.constant
&& decl
->initializer
== NULL
) {
5642 _mesa_glsl_error(& loc
, state
,
5643 "const declaration of `%s' must be initialized",
5647 if (state
->es_shader
) {
5648 const glsl_type
*const t
= var
->type
;
5650 /* Skip the unsized array check for TCS/TES/GS inputs & TCS outputs.
5652 * The GL_OES_tessellation_shader spec says about inputs:
5654 * "Declaring an array size is optional. If no size is specified,
5655 * it will be taken from the implementation-dependent maximum
5656 * patch size (gl_MaxPatchVertices)."
5658 * and about TCS outputs:
5660 * "If no size is specified, it will be taken from output patch
5661 * size declared in the shader."
5663 * The GL_OES_geometry_shader spec says:
5665 * "All geometry shader input unsized array declarations will be
5666 * sized by an earlier input primitive layout qualifier, when
5667 * present, as per the following table."
5669 const bool implicitly_sized
=
5670 (var
->data
.mode
== ir_var_shader_in
&&
5671 state
->stage
>= MESA_SHADER_TESS_CTRL
&&
5672 state
->stage
<= MESA_SHADER_GEOMETRY
) ||
5673 (var
->data
.mode
== ir_var_shader_out
&&
5674 state
->stage
== MESA_SHADER_TESS_CTRL
);
5676 if (t
->is_unsized_array() && !implicitly_sized
)
5677 /* Section 10.17 of the GLSL ES 1.00 specification states that
5678 * unsized array declarations have been removed from the language.
5679 * Arrays that are sized using an initializer are still explicitly
5680 * sized. However, GLSL ES 1.00 does not allow array
5681 * initializers. That is only allowed in GLSL ES 3.00.
5683 * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
5685 * "An array type can also be formed without specifying a size
5686 * if the definition includes an initializer:
5688 * float x[] = float[2] (1.0, 2.0); // declares an array of size 2
5689 * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
5694 _mesa_glsl_error(& loc
, state
,
5695 "unsized array declarations are not allowed in "
5699 /* Section 4.4.6.1 Atomic Counter Layout Qualifiers of the GLSL 4.60 spec:
5701 * "It is a compile-time error to declare an unsized array of
5704 if (var
->type
->is_unsized_array() &&
5705 var
->type
->without_array()->base_type
== GLSL_TYPE_ATOMIC_UINT
) {
5706 _mesa_glsl_error(& loc
, state
,
5707 "Unsized array of atomic_uint is not allowed");
5710 /* If the declaration is not a redeclaration, there are a few additional
5711 * semantic checks that must be applied. In addition, variable that was
5712 * created for the declaration should be added to the IR stream.
5714 if (!is_redeclaration
) {
5715 validate_identifier(decl
->identifier
, loc
, state
);
5717 /* Add the variable to the symbol table. Note that the initializer's
5718 * IR was already processed earlier (though it hasn't been emitted
5719 * yet), without the variable in scope.
5721 * This differs from most C-like languages, but it follows the GLSL
5722 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
5725 * "Within a declaration, the scope of a name starts immediately
5726 * after the initializer if present or immediately after the name
5727 * being declared if not."
5729 if (!state
->symbols
->add_variable(var
)) {
5730 YYLTYPE loc
= this->get_location();
5731 _mesa_glsl_error(&loc
, state
, "name `%s' already taken in the "
5732 "current scope", decl
->identifier
);
5736 /* Push the variable declaration to the top. It means that all the
5737 * variable declarations will appear in a funny last-to-first order,
5738 * but otherwise we run into trouble if a function is prototyped, a
5739 * global var is decled, then the function is defined with usage of
5740 * the global var. See glslparsertest's CorrectModule.frag.
5742 instructions
->push_head(var
);
5745 instructions
->append_list(&initializer_instructions
);
5749 /* Generally, variable declarations do not have r-values. However,
5750 * one is used for the declaration in
5752 * while (bool b = some_condition()) {
5756 * so we return the rvalue from the last seen declaration here.
5763 ast_parameter_declarator::hir(exec_list
*instructions
,
5764 struct _mesa_glsl_parse_state
*state
)
5767 const struct glsl_type
*type
;
5768 const char *name
= NULL
;
5769 YYLTYPE loc
= this->get_location();
5771 type
= this->type
->glsl_type(& name
, state
);
5775 _mesa_glsl_error(& loc
, state
,
5776 "invalid type `%s' in declaration of `%s'",
5777 name
, this->identifier
);
5779 _mesa_glsl_error(& loc
, state
,
5780 "invalid type in declaration of `%s'",
5784 type
= glsl_type::error_type
;
5787 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
5789 * "Functions that accept no input arguments need not use void in the
5790 * argument list because prototypes (or definitions) are required and
5791 * therefore there is no ambiguity when an empty argument list "( )" is
5792 * declared. The idiom "(void)" as a parameter list is provided for
5795 * Placing this check here prevents a void parameter being set up
5796 * for a function, which avoids tripping up checks for main taking
5797 * parameters and lookups of an unnamed symbol.
5799 if (type
->is_void()) {
5800 if (this->identifier
!= NULL
)
5801 _mesa_glsl_error(& loc
, state
,
5802 "named parameter cannot have type `void'");
5808 if (formal_parameter
&& (this->identifier
== NULL
)) {
5809 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
5813 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
5814 * call already handled the "vec4[..] foo" case.
5816 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
5818 if (!type
->is_error() && type
->is_unsized_array()) {
5819 _mesa_glsl_error(&loc
, state
, "arrays passed as parameters must have "
5821 type
= glsl_type::error_type
;
5825 ir_variable
*var
= new(ctx
)
5826 ir_variable(type
, this->identifier
, ir_var_function_in
);
5828 /* Apply any specified qualifiers to the parameter declaration. Note that
5829 * for function parameters the default mode is 'in'.
5831 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
,
5834 /* From section 4.1.7 of the GLSL 4.40 spec:
5836 * "Opaque variables cannot be treated as l-values; hence cannot
5837 * be used as out or inout function parameters, nor can they be
5840 * From section 4.1.7 of the ARB_bindless_texture spec:
5842 * "Samplers can be used as l-values, so can be assigned into and used
5843 * as "out" and "inout" function parameters."
5845 * From section 4.1.X of the ARB_bindless_texture spec:
5847 * "Images can be used as l-values, so can be assigned into and used as
5848 * "out" and "inout" function parameters."
5850 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
5851 && (type
->contains_atomic() ||
5852 (!state
->has_bindless() && type
->contains_opaque()))) {
5853 _mesa_glsl_error(&loc
, state
, "out and inout parameters cannot "
5854 "contain %s variables",
5855 state
->has_bindless() ? "atomic" : "opaque");
5856 type
= glsl_type::error_type
;
5859 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
5861 * "When calling a function, expressions that do not evaluate to
5862 * l-values cannot be passed to parameters declared as out or inout."
5864 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
5866 * "Other binary or unary expressions, non-dereferenced arrays,
5867 * function names, swizzles with repeated fields, and constants
5868 * cannot be l-values."
5870 * So for GLSL 1.10, passing an array as an out or inout parameter is not
5871 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
5873 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
5875 && !state
->check_version(120, 100, &loc
,
5876 "arrays cannot be out or inout parameters")) {
5877 type
= glsl_type::error_type
;
5880 instructions
->push_tail(var
);
5882 /* Parameter declarations do not have r-values.
5889 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
5891 exec_list
*ir_parameters
,
5892 _mesa_glsl_parse_state
*state
)
5894 ast_parameter_declarator
*void_param
= NULL
;
5897 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
5898 param
->formal_parameter
= formal
;
5899 param
->hir(ir_parameters
, state
);
5907 if ((void_param
!= NULL
) && (count
> 1)) {
5908 YYLTYPE loc
= void_param
->get_location();
5910 _mesa_glsl_error(& loc
, state
,
5911 "`void' parameter must be only parameter");
5917 emit_function(_mesa_glsl_parse_state
*state
, ir_function
*f
)
5919 /* IR invariants disallow function declarations or definitions
5920 * nested within other function definitions. But there is no
5921 * requirement about the relative order of function declarations
5922 * and definitions with respect to one another. So simply insert
5923 * the new ir_function block at the end of the toplevel instruction
5926 state
->toplevel_ir
->push_tail(f
);
5931 ast_function::hir(exec_list
*instructions
,
5932 struct _mesa_glsl_parse_state
*state
)
5935 ir_function
*f
= NULL
;
5936 ir_function_signature
*sig
= NULL
;
5937 exec_list hir_parameters
;
5938 YYLTYPE loc
= this->get_location();
5940 const char *const name
= identifier
;
5942 /* New functions are always added to the top-level IR instruction stream,
5943 * so this instruction list pointer is ignored. See also emit_function
5946 (void) instructions
;
5948 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
5950 * "Function declarations (prototypes) cannot occur inside of functions;
5951 * they must be at global scope, or for the built-in functions, outside
5952 * the global scope."
5954 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
5956 * "User defined functions may only be defined within the global scope."
5958 * Note that this language does not appear in GLSL 1.10.
5960 if ((state
->current_function
!= NULL
) &&
5961 state
->is_version(120, 100)) {
5962 YYLTYPE loc
= this->get_location();
5963 _mesa_glsl_error(&loc
, state
,
5964 "declaration of function `%s' not allowed within "
5965 "function body", name
);
5968 validate_identifier(name
, this->get_location(), state
);
5970 /* Convert the list of function parameters to HIR now so that they can be
5971 * used below to compare this function's signature with previously seen
5972 * signatures for functions with the same name.
5974 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
5976 & hir_parameters
, state
);
5978 const char *return_type_name
;
5979 const glsl_type
*return_type
=
5980 this->return_type
->glsl_type(& return_type_name
, state
);
5983 YYLTYPE loc
= this->get_location();
5984 _mesa_glsl_error(&loc
, state
,
5985 "function `%s' has undeclared return type `%s'",
5986 name
, return_type_name
);
5987 return_type
= glsl_type::error_type
;
5990 /* ARB_shader_subroutine states:
5991 * "Subroutine declarations cannot be prototyped. It is an error to prepend
5992 * subroutine(...) to a function declaration."
5994 if (this->return_type
->qualifier
.subroutine_list
&& !is_definition
) {
5995 YYLTYPE loc
= this->get_location();
5996 _mesa_glsl_error(&loc
, state
,
5997 "function declaration `%s' cannot have subroutine prepended",
6001 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
6002 * "No qualifier is allowed on the return type of a function."
6004 if (this->return_type
->has_qualifiers(state
)) {
6005 YYLTYPE loc
= this->get_location();
6006 _mesa_glsl_error(& loc
, state
,
6007 "function `%s' return type has qualifiers", name
);
6010 /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
6012 * "Arrays are allowed as arguments and as the return type. In both
6013 * cases, the array must be explicitly sized."
6015 if (return_type
->is_unsized_array()) {
6016 YYLTYPE loc
= this->get_location();
6017 _mesa_glsl_error(& loc
, state
,
6018 "function `%s' return type array must be explicitly "
6022 /* From Section 6.1 (Function Definitions) of the GLSL 1.00 spec:
6024 * "Arrays are allowed as arguments, but not as the return type. [...]
6025 * The return type can also be a structure if the structure does not
6026 * contain an array."
6028 if (state
->language_version
== 100 && return_type
->contains_array()) {
6029 YYLTYPE loc
= this->get_location();
6030 _mesa_glsl_error(& loc
, state
,
6031 "function `%s' return type contains an array", name
);
6034 /* From section 4.1.7 of the GLSL 4.40 spec:
6036 * "[Opaque types] can only be declared as function parameters
6037 * or uniform-qualified variables."
6039 * The ARB_bindless_texture spec doesn't clearly state this, but as it says
6040 * "Replace Section 4.1.7 (Samplers), p. 25" and, "Replace Section 4.1.X,
6041 * (Images)", this should be allowed.
6043 if (return_type
->contains_atomic() ||
6044 (!state
->has_bindless() && return_type
->contains_opaque())) {
6045 YYLTYPE loc
= this->get_location();
6046 _mesa_glsl_error(&loc
, state
,
6047 "function `%s' return type can't contain an %s type",
6048 name
, state
->has_bindless() ? "atomic" : "opaque");
6052 if (return_type
->is_subroutine()) {
6053 YYLTYPE loc
= this->get_location();
6054 _mesa_glsl_error(&loc
, state
,
6055 "function `%s' return type can't be a subroutine type",
6059 /* Get the precision for the return type */
6060 unsigned return_precision
;
6062 if (state
->es_shader
) {
6063 YYLTYPE loc
= this->get_location();
6065 select_gles_precision(this->return_type
->qualifier
.precision
,
6070 return_precision
= GLSL_PRECISION_NONE
;
6073 /* Create an ir_function if one doesn't already exist. */
6074 f
= state
->symbols
->get_function(name
);
6076 f
= new(ctx
) ir_function(name
);
6077 if (!this->return_type
->qualifier
.is_subroutine_decl()) {
6078 if (!state
->symbols
->add_function(f
)) {
6079 /* This function name shadows a non-function use of the same name. */
6080 YYLTYPE loc
= this->get_location();
6081 _mesa_glsl_error(&loc
, state
, "function name `%s' conflicts with "
6082 "non-function", name
);
6086 emit_function(state
, f
);
6089 /* From GLSL ES 3.0 spec, chapter 6.1 "Function Definitions", page 71:
6091 * "A shader cannot redefine or overload built-in functions."
6093 * While in GLSL ES 1.0 specification, chapter 8 "Built-in Functions":
6095 * "User code can overload the built-in functions but cannot redefine
6098 if (state
->es_shader
) {
6099 /* Local shader has no exact candidates; check the built-ins. */
6100 if (state
->language_version
>= 300 &&
6101 _mesa_glsl_has_builtin_function(state
, name
)) {
6102 YYLTYPE loc
= this->get_location();
6103 _mesa_glsl_error(& loc
, state
,
6104 "A shader cannot redefine or overload built-in "
6105 "function `%s' in GLSL ES 3.00", name
);
6109 if (state
->language_version
== 100) {
6110 ir_function_signature
*sig
=
6111 _mesa_glsl_find_builtin_function(state
, name
, &hir_parameters
);
6112 if (sig
&& sig
->is_builtin()) {
6113 _mesa_glsl_error(& loc
, state
,
6114 "A shader cannot redefine built-in "
6115 "function `%s' in GLSL ES 1.00", name
);
6120 /* Verify that this function's signature either doesn't match a previously
6121 * seen signature for a function with the same name, or, if a match is found,
6122 * that the previously seen signature does not have an associated definition.
6124 if (state
->es_shader
|| f
->has_user_signature()) {
6125 sig
= f
->exact_matching_signature(state
, &hir_parameters
);
6127 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
6128 if (badvar
!= NULL
) {
6129 YYLTYPE loc
= this->get_location();
6131 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
6132 "qualifiers don't match prototype", name
, badvar
);
6135 if (sig
->return_type
!= return_type
) {
6136 YYLTYPE loc
= this->get_location();
6138 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
6139 "match prototype", name
);
6142 if (sig
->return_precision
!= return_precision
) {
6143 YYLTYPE loc
= this->get_location();
6145 _mesa_glsl_error(&loc
, state
, "function `%s' return type precision "
6146 "doesn't match prototype", name
);
6149 if (sig
->is_defined
) {
6150 if (is_definition
) {
6151 YYLTYPE loc
= this->get_location();
6152 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
6154 /* We just encountered a prototype that exactly matches a
6155 * function that's already been defined. This is redundant,
6156 * and we should ignore it.
6160 } else if (state
->language_version
== 100 && !is_definition
) {
6161 /* From the GLSL 1.00 spec, section 4.2.7:
6163 * "A particular variable, structure or function declaration
6164 * may occur at most once within a scope with the exception
6165 * that a single function prototype plus the corresponding
6166 * function definition are allowed."
6168 YYLTYPE loc
= this->get_location();
6169 _mesa_glsl_error(&loc
, state
, "function `%s' redeclared", name
);
6174 /* Verify the return type of main() */
6175 if (strcmp(name
, "main") == 0) {
6176 if (! return_type
->is_void()) {
6177 YYLTYPE loc
= this->get_location();
6179 _mesa_glsl_error(& loc
, state
, "main() must return void");
6182 if (!hir_parameters
.is_empty()) {
6183 YYLTYPE loc
= this->get_location();
6185 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
6189 /* Finish storing the information about this new function in its signature.
6192 sig
= new(ctx
) ir_function_signature(return_type
);
6193 sig
->return_precision
= return_precision
;
6194 f
->add_signature(sig
);
6197 sig
->replace_parameters(&hir_parameters
);
6200 if (this->return_type
->qualifier
.subroutine_list
) {
6203 if (this->return_type
->qualifier
.flags
.q
.explicit_index
) {
6204 unsigned qual_index
;
6205 if (process_qualifier_constant(state
, &loc
, "index",
6206 this->return_type
->qualifier
.index
,
6208 if (!state
->has_explicit_uniform_location()) {
6209 _mesa_glsl_error(&loc
, state
, "subroutine index requires "
6210 "GL_ARB_explicit_uniform_location or "
6212 } else if (qual_index
>= MAX_SUBROUTINES
) {
6213 _mesa_glsl_error(&loc
, state
,
6214 "invalid subroutine index (%d) index must "
6215 "be a number between 0 and "
6216 "GL_MAX_SUBROUTINES - 1 (%d)", qual_index
,
6217 MAX_SUBROUTINES
- 1);
6219 f
->subroutine_index
= qual_index
;
6224 f
->num_subroutine_types
= this->return_type
->qualifier
.subroutine_list
->declarations
.length();
6225 f
->subroutine_types
= ralloc_array(state
, const struct glsl_type
*,
6226 f
->num_subroutine_types
);
6228 foreach_list_typed(ast_declaration
, decl
, link
, &this->return_type
->qualifier
.subroutine_list
->declarations
) {
6229 const struct glsl_type
*type
;
6230 /* the subroutine type must be already declared */
6231 type
= state
->symbols
->get_type(decl
->identifier
);
6233 _mesa_glsl_error(& loc
, state
, "unknown type '%s' in subroutine function definition", decl
->identifier
);
6236 for (int i
= 0; i
< state
->num_subroutine_types
; i
++) {
6237 ir_function
*fn
= state
->subroutine_types
[i
];
6238 ir_function_signature
*tsig
= NULL
;
6240 if (strcmp(fn
->name
, decl
->identifier
))
6243 tsig
= fn
->matching_signature(state
, &sig
->parameters
,
6246 _mesa_glsl_error(& loc
, state
, "subroutine type mismatch '%s' - signatures do not match\n", decl
->identifier
);
6248 if (tsig
->return_type
!= sig
->return_type
) {
6249 _mesa_glsl_error(& loc
, state
, "subroutine type mismatch '%s' - return types do not match\n", decl
->identifier
);
6253 f
->subroutine_types
[idx
++] = type
;
6255 state
->subroutines
= (ir_function
**)reralloc(state
, state
->subroutines
,
6257 state
->num_subroutines
+ 1);
6258 state
->subroutines
[state
->num_subroutines
] = f
;
6259 state
->num_subroutines
++;
6263 if (this->return_type
->qualifier
.is_subroutine_decl()) {
6264 if (!state
->symbols
->add_type(this->identifier
, glsl_type::get_subroutine_instance(this->identifier
))) {
6265 _mesa_glsl_error(& loc
, state
, "type '%s' previously defined", this->identifier
);
6268 state
->subroutine_types
= (ir_function
**)reralloc(state
, state
->subroutine_types
,
6270 state
->num_subroutine_types
+ 1);
6271 state
->subroutine_types
[state
->num_subroutine_types
] = f
;
6272 state
->num_subroutine_types
++;
6274 f
->is_subroutine
= true;
6277 /* Function declarations (prototypes) do not have r-values.
6284 ast_function_definition::hir(exec_list
*instructions
,
6285 struct _mesa_glsl_parse_state
*state
)
6287 prototype
->is_definition
= true;
6288 prototype
->hir(instructions
, state
);
6290 ir_function_signature
*signature
= prototype
->signature
;
6291 if (signature
== NULL
)
6294 assert(state
->current_function
== NULL
);
6295 state
->current_function
= signature
;
6296 state
->found_return
= false;
6297 state
->found_begin_interlock
= false;
6298 state
->found_end_interlock
= false;
6300 /* Duplicate parameters declared in the prototype as concrete variables.
6301 * Add these to the symbol table.
6303 state
->symbols
->push_scope();
6304 foreach_in_list(ir_variable
, var
, &signature
->parameters
) {
6305 assert(var
->as_variable() != NULL
);
6307 /* The only way a parameter would "exist" is if two parameters have
6310 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
6311 YYLTYPE loc
= this->get_location();
6313 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
6315 state
->symbols
->add_variable(var
);
6319 /* Convert the body of the function to HIR. */
6320 this->body
->hir(&signature
->body
, state
);
6321 signature
->is_defined
= true;
6323 state
->symbols
->pop_scope();
6325 assert(state
->current_function
== signature
);
6326 state
->current_function
= NULL
;
6328 if (!signature
->return_type
->is_void() && !state
->found_return
) {
6329 YYLTYPE loc
= this->get_location();
6330 _mesa_glsl_error(& loc
, state
, "function `%s' has non-void return type "
6331 "%s, but no return statement",
6332 signature
->function_name(),
6333 signature
->return_type
->name
);
6336 /* Function definitions do not have r-values.
6343 ast_jump_statement::hir(exec_list
*instructions
,
6344 struct _mesa_glsl_parse_state
*state
)
6351 assert(state
->current_function
);
6353 if (opt_return_value
) {
6354 ir_rvalue
*ret
= opt_return_value
->hir(instructions
, state
);
6356 /* The value of the return type can be NULL if the shader says
6357 * 'return foo();' and foo() is a function that returns void.
6359 * NOTE: The GLSL spec doesn't say that this is an error. The type
6360 * of the return value is void. If the return type of the function is
6361 * also void, then this should compile without error. Seriously.
6363 const glsl_type
*const ret_type
=
6364 (ret
== NULL
) ? glsl_type::void_type
: ret
->type
;
6366 /* Implicit conversions are not allowed for return values prior to
6367 * ARB_shading_language_420pack.
6369 if (state
->current_function
->return_type
!= ret_type
) {
6370 YYLTYPE loc
= this->get_location();
6372 if (state
->has_420pack()) {
6373 if (!apply_implicit_conversion(state
->current_function
->return_type
,
6375 || (ret
->type
!= state
->current_function
->return_type
)) {
6376 _mesa_glsl_error(& loc
, state
,
6377 "could not implicitly convert return value "
6378 "to %s, in function `%s'",
6379 state
->current_function
->return_type
->name
,
6380 state
->current_function
->function_name());
6383 _mesa_glsl_error(& loc
, state
,
6384 "`return' with wrong type %s, in function `%s' "
6387 state
->current_function
->function_name(),
6388 state
->current_function
->return_type
->name
);
6390 } else if (state
->current_function
->return_type
->base_type
==
6392 YYLTYPE loc
= this->get_location();
6394 /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
6395 * specs add a clarification:
6397 * "A void function can only use return without a return argument, even if
6398 * the return argument has void type. Return statements only accept values:
6401 * void func2() { return func1(); } // illegal return statement"
6403 _mesa_glsl_error(& loc
, state
,
6404 "void functions can only use `return' without a "
6408 inst
= new(ctx
) ir_return(ret
);
6410 if (state
->current_function
->return_type
->base_type
!=
6412 YYLTYPE loc
= this->get_location();
6414 _mesa_glsl_error(& loc
, state
,
6415 "`return' with no value, in function %s returning "
6417 state
->current_function
->function_name());
6419 inst
= new(ctx
) ir_return
;
6422 state
->found_return
= true;
6423 instructions
->push_tail(inst
);
6428 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
6429 YYLTYPE loc
= this->get_location();
6431 _mesa_glsl_error(& loc
, state
,
6432 "`discard' may only appear in a fragment shader");
6434 instructions
->push_tail(new(ctx
) ir_discard
);
6439 if (mode
== ast_continue
&&
6440 state
->loop_nesting_ast
== NULL
) {
6441 YYLTYPE loc
= this->get_location();
6443 _mesa_glsl_error(& loc
, state
, "continue may only appear in a loop");
6444 } else if (mode
== ast_break
&&
6445 state
->loop_nesting_ast
== NULL
&&
6446 state
->switch_state
.switch_nesting_ast
== NULL
) {
6447 YYLTYPE loc
= this->get_location();
6449 _mesa_glsl_error(& loc
, state
,
6450 "break may only appear in a loop or a switch");
6452 /* For a loop, inline the for loop expression again, since we don't
6453 * know where near the end of the loop body the normal copy of it is
6454 * going to be placed. Same goes for the condition for a do-while
6457 if (state
->loop_nesting_ast
!= NULL
&&
6458 mode
== ast_continue
&& !state
->switch_state
.is_switch_innermost
) {
6459 if (state
->loop_nesting_ast
->rest_expression
) {
6460 state
->loop_nesting_ast
->rest_expression
->hir(instructions
,
6463 if (state
->loop_nesting_ast
->mode
==
6464 ast_iteration_statement::ast_do_while
) {
6465 state
->loop_nesting_ast
->condition_to_hir(instructions
, state
);
6469 if (state
->switch_state
.is_switch_innermost
&&
6470 mode
== ast_continue
) {
6471 /* Set 'continue_inside' to true. */
6472 ir_rvalue
*const true_val
= new (ctx
) ir_constant(true);
6473 ir_dereference_variable
*deref_continue_inside_var
=
6474 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
6475 instructions
->push_tail(new(ctx
) ir_assignment(deref_continue_inside_var
,
6478 /* Break out from the switch, continue for the loop will
6479 * be called right after switch. */
6480 ir_loop_jump
*const jump
=
6481 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
6482 instructions
->push_tail(jump
);
6484 } else if (state
->switch_state
.is_switch_innermost
&&
6485 mode
== ast_break
) {
6486 /* Force break out of switch by inserting a break. */
6487 ir_loop_jump
*const jump
=
6488 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
6489 instructions
->push_tail(jump
);
6491 ir_loop_jump
*const jump
=
6492 new(ctx
) ir_loop_jump((mode
== ast_break
)
6493 ? ir_loop_jump::jump_break
6494 : ir_loop_jump::jump_continue
);
6495 instructions
->push_tail(jump
);
6502 /* Jump instructions do not have r-values.
6509 ast_demote_statement::hir(exec_list
*instructions
,
6510 struct _mesa_glsl_parse_state
*state
)
6514 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
6515 YYLTYPE loc
= this->get_location();
6517 _mesa_glsl_error(& loc
, state
,
6518 "`demote' may only appear in a fragment shader");
6521 instructions
->push_tail(new(ctx
) ir_demote
);
6528 ast_selection_statement::hir(exec_list
*instructions
,
6529 struct _mesa_glsl_parse_state
*state
)
6533 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
6535 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
6537 * "Any expression whose type evaluates to a Boolean can be used as the
6538 * conditional expression bool-expression. Vector types are not accepted
6539 * as the expression to if."
6541 * The checks are separated so that higher quality diagnostics can be
6542 * generated for cases where both rules are violated.
6544 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
6545 YYLTYPE loc
= this->condition
->get_location();
6547 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
6551 ir_if
*const stmt
= new(ctx
) ir_if(condition
);
6553 if (then_statement
!= NULL
) {
6554 state
->symbols
->push_scope();
6555 then_statement
->hir(& stmt
->then_instructions
, state
);
6556 state
->symbols
->pop_scope();
6559 if (else_statement
!= NULL
) {
6560 state
->symbols
->push_scope();
6561 else_statement
->hir(& stmt
->else_instructions
, state
);
6562 state
->symbols
->pop_scope();
6565 instructions
->push_tail(stmt
);
6567 /* if-statements do not have r-values.
6574 /** Value of the case label. */
6577 /** Does this label occur after the default? */
6581 * AST for the case label.
6583 * This is only used to generate error messages for duplicate labels.
6585 ast_expression
*ast
;
6588 /* Used for detection of duplicate case values, compare
6589 * given contents directly.
6592 compare_case_value(const void *a
, const void *b
)
6594 return ((struct case_label
*) a
)->value
== ((struct case_label
*) b
)->value
;
6598 /* Used for detection of duplicate case values, just
6599 * returns key contents as is.
6602 key_contents(const void *key
)
6604 return ((struct case_label
*) key
)->value
;
6609 ast_switch_statement::hir(exec_list
*instructions
,
6610 struct _mesa_glsl_parse_state
*state
)
6614 ir_rvalue
*const test_expression
=
6615 this->test_expression
->hir(instructions
, state
);
6617 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
6619 * "The type of init-expression in a switch statement must be a
6622 if (!test_expression
->type
->is_scalar() ||
6623 !test_expression
->type
->is_integer_32()) {
6624 YYLTYPE loc
= this->test_expression
->get_location();
6626 _mesa_glsl_error(& loc
,
6628 "switch-statement expression must be scalar "
6633 /* Track the switch-statement nesting in a stack-like manner.
6635 struct glsl_switch_state saved
= state
->switch_state
;
6637 state
->switch_state
.is_switch_innermost
= true;
6638 state
->switch_state
.switch_nesting_ast
= this;
6639 state
->switch_state
.labels_ht
=
6640 _mesa_hash_table_create(NULL
, key_contents
,
6641 compare_case_value
);
6642 state
->switch_state
.previous_default
= NULL
;
6644 /* Initalize is_fallthru state to false.
6646 ir_rvalue
*const is_fallthru_val
= new (ctx
) ir_constant(false);
6647 state
->switch_state
.is_fallthru_var
=
6648 new(ctx
) ir_variable(glsl_type::bool_type
,
6649 "switch_is_fallthru_tmp",
6651 instructions
->push_tail(state
->switch_state
.is_fallthru_var
);
6653 ir_dereference_variable
*deref_is_fallthru_var
=
6654 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
6655 instructions
->push_tail(new(ctx
) ir_assignment(deref_is_fallthru_var
,
6658 /* Initialize continue_inside state to false.
6660 state
->switch_state
.continue_inside
=
6661 new(ctx
) ir_variable(glsl_type::bool_type
,
6662 "continue_inside_tmp",
6664 instructions
->push_tail(state
->switch_state
.continue_inside
);
6666 ir_rvalue
*const false_val
= new (ctx
) ir_constant(false);
6667 ir_dereference_variable
*deref_continue_inside_var
=
6668 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
6669 instructions
->push_tail(new(ctx
) ir_assignment(deref_continue_inside_var
,
6672 state
->switch_state
.run_default
=
6673 new(ctx
) ir_variable(glsl_type::bool_type
,
6676 instructions
->push_tail(state
->switch_state
.run_default
);
6678 /* Loop around the switch is used for flow control. */
6679 ir_loop
* loop
= new(ctx
) ir_loop();
6680 instructions
->push_tail(loop
);
6682 /* Cache test expression.
6684 test_to_hir(&loop
->body_instructions
, state
);
6686 /* Emit code for body of switch stmt.
6688 body
->hir(&loop
->body_instructions
, state
);
6690 /* Insert a break at the end to exit loop. */
6691 ir_loop_jump
*jump
= new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
6692 loop
->body_instructions
.push_tail(jump
);
6694 /* If we are inside loop, check if continue got called inside switch. */
6695 if (state
->loop_nesting_ast
!= NULL
) {
6696 ir_dereference_variable
*deref_continue_inside
=
6697 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
6698 ir_if
*irif
= new(ctx
) ir_if(deref_continue_inside
);
6699 ir_loop_jump
*jump
= new(ctx
) ir_loop_jump(ir_loop_jump::jump_continue
);
6701 if (state
->loop_nesting_ast
!= NULL
) {
6702 if (state
->loop_nesting_ast
->rest_expression
) {
6703 state
->loop_nesting_ast
->rest_expression
->hir(&irif
->then_instructions
,
6706 if (state
->loop_nesting_ast
->mode
==
6707 ast_iteration_statement::ast_do_while
) {
6708 state
->loop_nesting_ast
->condition_to_hir(&irif
->then_instructions
, state
);
6711 irif
->then_instructions
.push_tail(jump
);
6712 instructions
->push_tail(irif
);
6715 _mesa_hash_table_destroy(state
->switch_state
.labels_ht
, NULL
);
6717 state
->switch_state
= saved
;
6719 /* Switch statements do not have r-values. */
6725 ast_switch_statement::test_to_hir(exec_list
*instructions
,
6726 struct _mesa_glsl_parse_state
*state
)
6730 /* set to true to avoid a duplicate "use of uninitialized variable" warning
6731 * on the switch test case. The first one would be already raised when
6732 * getting the test_expression at ast_switch_statement::hir
6734 test_expression
->set_is_lhs(true);
6735 /* Cache value of test expression. */
6736 ir_rvalue
*const test_val
= test_expression
->hir(instructions
, state
);
6738 state
->switch_state
.test_var
= new(ctx
) ir_variable(test_val
->type
,
6741 ir_dereference_variable
*deref_test_var
=
6742 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
6744 instructions
->push_tail(state
->switch_state
.test_var
);
6745 instructions
->push_tail(new(ctx
) ir_assignment(deref_test_var
, test_val
));
6750 ast_switch_body::hir(exec_list
*instructions
,
6751 struct _mesa_glsl_parse_state
*state
)
6754 stmts
->hir(instructions
, state
);
6756 /* Switch bodies do not have r-values. */
6761 ast_case_statement_list::hir(exec_list
*instructions
,
6762 struct _mesa_glsl_parse_state
*state
)
6764 exec_list default_case
, after_default
, tmp
;
6766 foreach_list_typed (ast_case_statement
, case_stmt
, link
, & this->cases
) {
6767 case_stmt
->hir(&tmp
, state
);
6770 if (state
->switch_state
.previous_default
&& default_case
.is_empty()) {
6771 default_case
.append_list(&tmp
);
6775 /* If default case found, append 'after_default' list. */
6776 if (!default_case
.is_empty())
6777 after_default
.append_list(&tmp
);
6779 instructions
->append_list(&tmp
);
6782 /* Handle the default case. This is done here because default might not be
6783 * the last case. We need to add checks against following cases first to see
6784 * if default should be chosen or not.
6786 if (!default_case
.is_empty()) {
6787 ir_factory
body(instructions
, state
);
6789 ir_expression
*cmp
= NULL
;
6791 hash_table_foreach(state
->switch_state
.labels_ht
, entry
) {
6792 const struct case_label
*const l
= (struct case_label
*) entry
->data
;
6794 /* If the switch init-value is the value of one of the labels that
6795 * occurs after the default case, disable execution of the default
6798 if (l
->after_default
) {
6799 ir_constant
*const cnst
=
6800 state
->switch_state
.test_var
->type
->base_type
== GLSL_TYPE_UINT
6801 ? body
.constant(unsigned(l
->value
))
6802 : body
.constant(int(l
->value
));
6805 ? equal(cnst
, state
->switch_state
.test_var
)
6806 : logic_or(cmp
, equal(cnst
, state
->switch_state
.test_var
));
6811 body
.emit(assign(state
->switch_state
.run_default
, logic_not(cmp
)));
6813 body
.emit(assign(state
->switch_state
.run_default
, body
.constant(true)));
6815 /* Append default case and all cases after it. */
6816 instructions
->append_list(&default_case
);
6817 instructions
->append_list(&after_default
);
6820 /* Case statements do not have r-values. */
6825 ast_case_statement::hir(exec_list
*instructions
,
6826 struct _mesa_glsl_parse_state
*state
)
6828 labels
->hir(instructions
, state
);
6830 /* Guard case statements depending on fallthru state. */
6831 ir_dereference_variable
*const deref_fallthru_guard
=
6832 new(state
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
6833 ir_if
*const test_fallthru
= new(state
) ir_if(deref_fallthru_guard
);
6835 foreach_list_typed (ast_node
, stmt
, link
, & this->stmts
)
6836 stmt
->hir(& test_fallthru
->then_instructions
, state
);
6838 instructions
->push_tail(test_fallthru
);
6840 /* Case statements do not have r-values. */
6846 ast_case_label_list::hir(exec_list
*instructions
,
6847 struct _mesa_glsl_parse_state
*state
)
6849 foreach_list_typed (ast_case_label
, label
, link
, & this->labels
)
6850 label
->hir(instructions
, state
);
6852 /* Case labels do not have r-values. */
6857 ast_case_label::hir(exec_list
*instructions
,
6858 struct _mesa_glsl_parse_state
*state
)
6860 ir_factory
body(instructions
, state
);
6862 ir_variable
*const fallthru_var
= state
->switch_state
.is_fallthru_var
;
6864 /* If not default case, ... */
6865 if (this->test_value
!= NULL
) {
6866 /* Conditionally set fallthru state based on
6867 * comparison of cached test expression value to case label.
6869 ir_rvalue
*const label_rval
= this->test_value
->hir(instructions
, state
);
6870 ir_constant
*label_const
=
6871 label_rval
->constant_expression_value(body
.mem_ctx
);
6874 YYLTYPE loc
= this->test_value
->get_location();
6876 _mesa_glsl_error(& loc
, state
,
6877 "switch statement case label must be a "
6878 "constant expression");
6880 /* Stuff a dummy value in to allow processing to continue. */
6881 label_const
= body
.constant(0);
6884 _mesa_hash_table_search(state
->switch_state
.labels_ht
,
6885 &label_const
->value
.u
[0]);
6888 const struct case_label
*const l
=
6889 (struct case_label
*) entry
->data
;
6890 const ast_expression
*const previous_label
= l
->ast
;
6891 YYLTYPE loc
= this->test_value
->get_location();
6893 _mesa_glsl_error(& loc
, state
, "duplicate case value");
6895 loc
= previous_label
->get_location();
6896 _mesa_glsl_error(& loc
, state
, "this is the previous case label");
6898 struct case_label
*l
= ralloc(state
->switch_state
.labels_ht
,
6901 l
->value
= label_const
->value
.u
[0];
6902 l
->after_default
= state
->switch_state
.previous_default
!= NULL
;
6903 l
->ast
= this->test_value
;
6905 _mesa_hash_table_insert(state
->switch_state
.labels_ht
,
6906 &label_const
->value
.u
[0],
6911 /* Create an r-value version of the ir_constant label here (after we may
6912 * have created a fake one in error cases) that can be passed to
6913 * apply_implicit_conversion below.
6915 ir_rvalue
*label
= label_const
;
6917 ir_rvalue
*deref_test_var
=
6918 new(body
.mem_ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
6921 * From GLSL 4.40 specification section 6.2 ("Selection"):
6923 * "The type of the init-expression value in a switch statement must
6924 * be a scalar int or uint. The type of the constant-expression value
6925 * in a case label also must be a scalar int or uint. When any pair
6926 * of these values is tested for "equal value" and the types do not
6927 * match, an implicit conversion will be done to convert the int to a
6928 * uint (see section 4.1.10 “Implicit Conversions”) before the compare
6931 if (label
->type
!= state
->switch_state
.test_var
->type
) {
6932 YYLTYPE loc
= this->test_value
->get_location();
6934 const glsl_type
*type_a
= label
->type
;
6935 const glsl_type
*type_b
= state
->switch_state
.test_var
->type
;
6937 /* Check if int->uint implicit conversion is supported. */
6938 bool integer_conversion_supported
=
6939 glsl_type::int_type
->can_implicitly_convert_to(glsl_type::uint_type
,
6942 if ((!type_a
->is_integer_32() || !type_b
->is_integer_32()) ||
6943 !integer_conversion_supported
) {
6944 _mesa_glsl_error(&loc
, state
, "type mismatch with switch "
6945 "init-expression and case label (%s != %s)",
6946 type_a
->name
, type_b
->name
);
6948 /* Conversion of the case label. */
6949 if (type_a
->base_type
== GLSL_TYPE_INT
) {
6950 if (!apply_implicit_conversion(glsl_type::uint_type
,
6952 _mesa_glsl_error(&loc
, state
, "implicit type conversion error");
6954 /* Conversion of the init-expression value. */
6955 if (!apply_implicit_conversion(glsl_type::uint_type
,
6956 deref_test_var
, state
))
6957 _mesa_glsl_error(&loc
, state
, "implicit type conversion error");
6961 /* If the implicit conversion was allowed, the types will already be
6962 * the same. If the implicit conversion wasn't allowed, smash the
6963 * type of the label anyway. This will prevent the expression
6964 * constructor (below) from failing an assertion.
6966 label
->type
= deref_test_var
->type
;
6969 body
.emit(assign(fallthru_var
,
6970 logic_or(fallthru_var
, equal(label
, deref_test_var
))));
6971 } else { /* default case */
6972 if (state
->switch_state
.previous_default
) {
6973 YYLTYPE loc
= this->get_location();
6974 _mesa_glsl_error(& loc
, state
,
6975 "multiple default labels in one switch");
6977 loc
= state
->switch_state
.previous_default
->get_location();
6978 _mesa_glsl_error(& loc
, state
, "this is the first default label");
6980 state
->switch_state
.previous_default
= this;
6982 /* Set fallthru condition on 'run_default' bool. */
6983 body
.emit(assign(fallthru_var
,
6984 logic_or(fallthru_var
,
6985 state
->switch_state
.run_default
)));
6988 /* Case statements do not have r-values. */
6993 ast_iteration_statement::condition_to_hir(exec_list
*instructions
,
6994 struct _mesa_glsl_parse_state
*state
)
6998 if (condition
!= NULL
) {
6999 ir_rvalue
*const cond
=
7000 condition
->hir(instructions
, state
);
7003 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
7004 YYLTYPE loc
= condition
->get_location();
7006 _mesa_glsl_error(& loc
, state
,
7007 "loop condition must be scalar boolean");
7009 /* As the first code in the loop body, generate a block that looks
7010 * like 'if (!condition) break;' as the loop termination condition.
7012 ir_rvalue
*const not_cond
=
7013 new(ctx
) ir_expression(ir_unop_logic_not
, cond
);
7015 ir_if
*const if_stmt
= new(ctx
) ir_if(not_cond
);
7017 ir_jump
*const break_stmt
=
7018 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
7020 if_stmt
->then_instructions
.push_tail(break_stmt
);
7021 instructions
->push_tail(if_stmt
);
7028 ast_iteration_statement::hir(exec_list
*instructions
,
7029 struct _mesa_glsl_parse_state
*state
)
7033 /* For-loops and while-loops start a new scope, but do-while loops do not.
7035 if (mode
!= ast_do_while
)
7036 state
->symbols
->push_scope();
7038 if (init_statement
!= NULL
)
7039 init_statement
->hir(instructions
, state
);
7041 ir_loop
*const stmt
= new(ctx
) ir_loop();
7042 instructions
->push_tail(stmt
);
7044 /* Track the current loop nesting. */
7045 ast_iteration_statement
*nesting_ast
= state
->loop_nesting_ast
;
7047 state
->loop_nesting_ast
= this;
7049 /* Likewise, indicate that following code is closest to a loop,
7050 * NOT closest to a switch.
7052 bool saved_is_switch_innermost
= state
->switch_state
.is_switch_innermost
;
7053 state
->switch_state
.is_switch_innermost
= false;
7055 if (mode
!= ast_do_while
)
7056 condition_to_hir(&stmt
->body_instructions
, state
);
7059 body
->hir(& stmt
->body_instructions
, state
);
7061 if (rest_expression
!= NULL
)
7062 rest_expression
->hir(& stmt
->body_instructions
, state
);
7064 if (mode
== ast_do_while
)
7065 condition_to_hir(&stmt
->body_instructions
, state
);
7067 if (mode
!= ast_do_while
)
7068 state
->symbols
->pop_scope();
7070 /* Restore previous nesting before returning. */
7071 state
->loop_nesting_ast
= nesting_ast
;
7072 state
->switch_state
.is_switch_innermost
= saved_is_switch_innermost
;
7074 /* Loops do not have r-values.
7081 * Determine if the given type is valid for establishing a default precision
7084 * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
7086 * "The precision statement
7088 * precision precision-qualifier type;
7090 * can be used to establish a default precision qualifier. The type field
7091 * can be either int or float or any of the sampler types, and the
7092 * precision-qualifier can be lowp, mediump, or highp."
7094 * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
7095 * qualifiers on sampler types, but this seems like an oversight (since the
7096 * intention of including these in GLSL 1.30 is to allow compatibility with ES
7097 * shaders). So we allow int, float, and all sampler types regardless of GLSL
7101 is_valid_default_precision_type(const struct glsl_type
*const type
)
7106 switch (type
->base_type
) {
7108 case GLSL_TYPE_FLOAT
:
7109 /* "int" and "float" are valid, but vectors and matrices are not. */
7110 return type
->vector_elements
== 1 && type
->matrix_columns
== 1;
7111 case GLSL_TYPE_SAMPLER
:
7112 case GLSL_TYPE_IMAGE
:
7113 case GLSL_TYPE_ATOMIC_UINT
:
7122 ast_type_specifier::hir(exec_list
*instructions
,
7123 struct _mesa_glsl_parse_state
*state
)
7125 if (this->default_precision
== ast_precision_none
&& this->structure
== NULL
)
7128 YYLTYPE loc
= this->get_location();
7130 /* If this is a precision statement, check that the type to which it is
7131 * applied is either float or int.
7133 * From section 4.5.3 of the GLSL 1.30 spec:
7134 * "The precision statement
7135 * precision precision-qualifier type;
7136 * can be used to establish a default precision qualifier. The type
7137 * field can be either int or float [...]. Any other types or
7138 * qualifiers will result in an error.
7140 if (this->default_precision
!= ast_precision_none
) {
7141 if (!state
->check_precision_qualifiers_allowed(&loc
))
7144 if (this->structure
!= NULL
) {
7145 _mesa_glsl_error(&loc
, state
,
7146 "precision qualifiers do not apply to structures");
7150 if (this->array_specifier
!= NULL
) {
7151 _mesa_glsl_error(&loc
, state
,
7152 "default precision statements do not apply to "
7157 const struct glsl_type
*const type
=
7158 state
->symbols
->get_type(this->type_name
);
7159 if (!is_valid_default_precision_type(type
)) {
7160 _mesa_glsl_error(&loc
, state
,
7161 "default precision statements apply only to "
7162 "float, int, and opaque types");
7166 if (state
->es_shader
) {
7167 /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
7170 * "Non-precision qualified declarations will use the precision
7171 * qualifier specified in the most recent precision statement
7172 * that is still in scope. The precision statement has the same
7173 * scoping rules as variable declarations. If it is declared
7174 * inside a compound statement, its effect stops at the end of
7175 * the innermost statement it was declared in. Precision
7176 * statements in nested scopes override precision statements in
7177 * outer scopes. Multiple precision statements for the same basic
7178 * type can appear inside the same scope, with later statements
7179 * overriding earlier statements within that scope."
7181 * Default precision specifications follow the same scope rules as
7182 * variables. So, we can track the state of the default precision
7183 * qualifiers in the symbol table, and the rules will just work. This
7184 * is a slight abuse of the symbol table, but it has the semantics
7187 state
->symbols
->add_default_precision_qualifier(this->type_name
,
7188 this->default_precision
);
7191 /* FINISHME: Translate precision statements into IR. */
7195 /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
7196 * process_record_constructor() can do type-checking on C-style initializer
7197 * expressions of structs, but ast_struct_specifier should only be translated
7198 * to HIR if it is declaring the type of a structure.
7200 * The ->is_declaration field is false for initializers of variables
7201 * declared separately from the struct's type definition.
7203 * struct S { ... }; (is_declaration = true)
7204 * struct T { ... } t = { ... }; (is_declaration = true)
7205 * S s = { ... }; (is_declaration = false)
7207 if (this->structure
!= NULL
&& this->structure
->is_declaration
)
7208 return this->structure
->hir(instructions
, state
);
7215 * Process a structure or interface block tree into an array of structure fields
7217 * After parsing, where there are some syntax differnces, structures and
7218 * interface blocks are almost identical. They are similar enough that the
7219 * AST for each can be processed the same way into a set of
7220 * \c glsl_struct_field to describe the members.
7222 * If we're processing an interface block, var_mode should be the type of the
7223 * interface block (ir_var_shader_in, ir_var_shader_out, ir_var_uniform or
7224 * ir_var_shader_storage). If we're processing a structure, var_mode should be
7228 * The number of fields processed. A pointer to the array structure fields is
7229 * stored in \c *fields_ret.
7232 ast_process_struct_or_iface_block_members(exec_list
*instructions
,
7233 struct _mesa_glsl_parse_state
*state
,
7234 exec_list
*declarations
,
7235 glsl_struct_field
**fields_ret
,
7237 enum glsl_matrix_layout matrix_layout
,
7238 bool allow_reserved_names
,
7239 ir_variable_mode var_mode
,
7240 ast_type_qualifier
*layout
,
7241 unsigned block_stream
,
7242 unsigned block_xfb_buffer
,
7243 unsigned block_xfb_offset
,
7244 unsigned expl_location
,
7245 unsigned expl_align
)
7247 unsigned decl_count
= 0;
7248 unsigned next_offset
= 0;
7250 /* Make an initial pass over the list of fields to determine how
7251 * many there are. Each element in this list is an ast_declarator_list.
7252 * This means that we actually need to count the number of elements in the
7253 * 'declarations' list in each of the elements.
7255 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
7256 decl_count
+= decl_list
->declarations
.length();
7259 /* Allocate storage for the fields and process the field
7260 * declarations. As the declarations are processed, try to also convert
7261 * the types to HIR. This ensures that structure definitions embedded in
7262 * other structure definitions or in interface blocks are processed.
7264 glsl_struct_field
*const fields
= rzalloc_array(state
, glsl_struct_field
,
7267 bool first_member
= true;
7268 bool first_member_has_explicit_location
= false;
7271 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
7272 const char *type_name
;
7273 YYLTYPE loc
= decl_list
->get_location();
7275 decl_list
->type
->specifier
->hir(instructions
, state
);
7277 /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
7279 * "Anonymous structures are not supported; so embedded structures
7280 * must have a declarator. A name given to an embedded struct is
7281 * scoped at the same level as the struct it is embedded in."
7283 * The same section of the GLSL 1.20 spec says:
7285 * "Anonymous structures are not supported. Embedded structures are
7288 * The GLSL ES 1.00 and 3.00 specs have similar langauge. So, we allow
7289 * embedded structures in 1.10 only.
7291 if (state
->language_version
!= 110 &&
7292 decl_list
->type
->specifier
->structure
!= NULL
)
7293 _mesa_glsl_error(&loc
, state
,
7294 "embedded structure declarations are not allowed");
7296 const glsl_type
*decl_type
=
7297 decl_list
->type
->glsl_type(& type_name
, state
);
7299 const struct ast_type_qualifier
*const qual
=
7300 &decl_list
->type
->qualifier
;
7302 /* From section 4.3.9 of the GLSL 4.40 spec:
7304 * "[In interface blocks] opaque types are not allowed."
7306 * It should be impossible for decl_type to be NULL here. Cases that
7307 * might naturally lead to decl_type being NULL, especially for the
7308 * is_interface case, will have resulted in compilation having
7309 * already halted due to a syntax error.
7314 /* From section 4.3.7 of the ARB_bindless_texture spec:
7316 * "(remove the following bullet from the last list on p. 39,
7317 * thereby permitting sampler types in interface blocks; image
7318 * types are also permitted in blocks by this extension)"
7320 * * sampler types are not allowed
7322 if (decl_type
->contains_atomic() ||
7323 (!state
->has_bindless() && decl_type
->contains_opaque())) {
7324 _mesa_glsl_error(&loc
, state
, "uniform/buffer in non-default "
7325 "interface block contains %s variable",
7326 state
->has_bindless() ? "atomic" : "opaque");
7329 if (decl_type
->contains_atomic()) {
7330 /* From section 4.1.7.3 of the GLSL 4.40 spec:
7332 * "Members of structures cannot be declared as atomic counter
7335 _mesa_glsl_error(&loc
, state
, "atomic counter in structure");
7338 if (!state
->has_bindless() && decl_type
->contains_image()) {
7339 /* FINISHME: Same problem as with atomic counters.
7340 * FINISHME: Request clarification from Khronos and add
7341 * FINISHME: spec quotation here.
7343 _mesa_glsl_error(&loc
, state
, "image in structure");
7347 if (qual
->flags
.q
.explicit_binding
) {
7348 _mesa_glsl_error(&loc
, state
,
7349 "binding layout qualifier cannot be applied "
7350 "to struct or interface block members");
7354 if (!first_member
) {
7355 if (!layout
->flags
.q
.explicit_location
&&
7356 ((first_member_has_explicit_location
&&
7357 !qual
->flags
.q
.explicit_location
) ||
7358 (!first_member_has_explicit_location
&&
7359 qual
->flags
.q
.explicit_location
))) {
7360 _mesa_glsl_error(&loc
, state
,
7361 "when block-level location layout qualifier "
7362 "is not supplied either all members must "
7363 "have a location layout qualifier or all "
7364 "members must not have a location layout "
7368 first_member
= false;
7369 first_member_has_explicit_location
=
7370 qual
->flags
.q
.explicit_location
;
7374 if (qual
->flags
.q
.std140
||
7375 qual
->flags
.q
.std430
||
7376 qual
->flags
.q
.packed
||
7377 qual
->flags
.q
.shared
) {
7378 _mesa_glsl_error(&loc
, state
,
7379 "uniform/shader storage block layout qualifiers "
7380 "std140, std430, packed, and shared can only be "
7381 "applied to uniform/shader storage blocks, not "
7385 if (qual
->flags
.q
.constant
) {
7386 _mesa_glsl_error(&loc
, state
,
7387 "const storage qualifier cannot be applied "
7388 "to struct or interface block members");
7391 validate_memory_qualifier_for_type(state
, &loc
, qual
, decl_type
);
7392 validate_image_format_qualifier_for_type(state
, &loc
, qual
, decl_type
);
7394 /* From Section 4.4.2.3 (Geometry Outputs) of the GLSL 4.50 spec:
7396 * "A block member may be declared with a stream identifier, but
7397 * the specified stream must match the stream associated with the
7398 * containing block."
7400 if (qual
->flags
.q
.explicit_stream
) {
7401 unsigned qual_stream
;
7402 if (process_qualifier_constant(state
, &loc
, "stream",
7403 qual
->stream
, &qual_stream
) &&
7404 qual_stream
!= block_stream
) {
7405 _mesa_glsl_error(&loc
, state
, "stream layout qualifier on "
7406 "interface block member does not match "
7407 "the interface block (%u vs %u)", qual_stream
,
7413 unsigned explicit_xfb_buffer
= 0;
7414 if (qual
->flags
.q
.explicit_xfb_buffer
) {
7415 unsigned qual_xfb_buffer
;
7416 if (process_qualifier_constant(state
, &loc
, "xfb_buffer",
7417 qual
->xfb_buffer
, &qual_xfb_buffer
)) {
7418 explicit_xfb_buffer
= 1;
7419 if (qual_xfb_buffer
!= block_xfb_buffer
)
7420 _mesa_glsl_error(&loc
, state
, "xfb_buffer layout qualifier on "
7421 "interface block member does not match "
7422 "the interface block (%u vs %u)",
7423 qual_xfb_buffer
, block_xfb_buffer
);
7425 xfb_buffer
= (int) qual_xfb_buffer
;
7428 explicit_xfb_buffer
= layout
->flags
.q
.explicit_xfb_buffer
;
7429 xfb_buffer
= (int) block_xfb_buffer
;
7432 int xfb_stride
= -1;
7433 if (qual
->flags
.q
.explicit_xfb_stride
) {
7434 unsigned qual_xfb_stride
;
7435 if (process_qualifier_constant(state
, &loc
, "xfb_stride",
7436 qual
->xfb_stride
, &qual_xfb_stride
)) {
7437 xfb_stride
= (int) qual_xfb_stride
;
7441 if (qual
->flags
.q
.uniform
&& qual
->has_interpolation()) {
7442 _mesa_glsl_error(&loc
, state
,
7443 "interpolation qualifiers cannot be used "
7444 "with uniform interface blocks");
7447 if ((qual
->flags
.q
.uniform
|| !is_interface
) &&
7448 qual
->has_auxiliary_storage()) {
7449 _mesa_glsl_error(&loc
, state
,
7450 "auxiliary storage qualifiers cannot be used "
7451 "in uniform blocks or structures.");
7454 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
7455 if (!qual
->flags
.q
.uniform
&& !qual
->flags
.q
.buffer
) {
7456 _mesa_glsl_error(&loc
, state
,
7457 "row_major and column_major can only be "
7458 "applied to interface blocks");
7460 validate_matrix_layout_for_type(state
, &loc
, decl_type
, NULL
);
7463 foreach_list_typed (ast_declaration
, decl
, link
,
7464 &decl_list
->declarations
) {
7465 YYLTYPE loc
= decl
->get_location();
7467 if (!allow_reserved_names
)
7468 validate_identifier(decl
->identifier
, loc
, state
);
7470 const struct glsl_type
*field_type
=
7471 process_array_type(&loc
, decl_type
, decl
->array_specifier
, state
);
7472 validate_array_dimensions(field_type
, state
, &loc
);
7473 fields
[i
].type
= field_type
;
7474 fields
[i
].name
= decl
->identifier
;
7475 fields
[i
].interpolation
=
7476 interpret_interpolation_qualifier(qual
, field_type
,
7477 var_mode
, state
, &loc
);
7478 fields
[i
].centroid
= qual
->flags
.q
.centroid
? 1 : 0;
7479 fields
[i
].sample
= qual
->flags
.q
.sample
? 1 : 0;
7480 fields
[i
].patch
= qual
->flags
.q
.patch
? 1 : 0;
7481 fields
[i
].offset
= -1;
7482 fields
[i
].explicit_xfb_buffer
= explicit_xfb_buffer
;
7483 fields
[i
].xfb_buffer
= xfb_buffer
;
7484 fields
[i
].xfb_stride
= xfb_stride
;
7486 if (qual
->flags
.q
.explicit_location
) {
7487 unsigned qual_location
;
7488 if (process_qualifier_constant(state
, &loc
, "location",
7489 qual
->location
, &qual_location
)) {
7490 fields
[i
].location
= qual_location
+
7491 (fields
[i
].patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
);
7492 expl_location
= fields
[i
].location
+
7493 fields
[i
].type
->count_attribute_slots(false);
7496 if (layout
&& layout
->flags
.q
.explicit_location
) {
7497 fields
[i
].location
= expl_location
;
7498 expl_location
+= fields
[i
].type
->count_attribute_slots(false);
7500 fields
[i
].location
= -1;
7504 /* Offset can only be used with std430 and std140 layouts an initial
7505 * value of 0 is used for error detection.
7511 if (qual
->flags
.q
.row_major
||
7512 matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
7518 if(layout
->flags
.q
.std140
) {
7519 align
= field_type
->std140_base_alignment(row_major
);
7520 size
= field_type
->std140_size(row_major
);
7521 } else if (layout
->flags
.q
.std430
) {
7522 align
= field_type
->std430_base_alignment(row_major
);
7523 size
= field_type
->std430_size(row_major
);
7527 if (qual
->flags
.q
.explicit_offset
) {
7528 unsigned qual_offset
;
7529 if (process_qualifier_constant(state
, &loc
, "offset",
7530 qual
->offset
, &qual_offset
)) {
7531 if (align
!= 0 && size
!= 0) {
7532 if (next_offset
> qual_offset
)
7533 _mesa_glsl_error(&loc
, state
, "layout qualifier "
7534 "offset overlaps previous member");
7536 if (qual_offset
% align
) {
7537 _mesa_glsl_error(&loc
, state
, "layout qualifier offset "
7538 "must be a multiple of the base "
7539 "alignment of %s", field_type
->name
);
7541 fields
[i
].offset
= qual_offset
;
7542 next_offset
= qual_offset
+ size
;
7544 _mesa_glsl_error(&loc
, state
, "offset can only be used "
7545 "with std430 and std140 layouts");
7550 if (qual
->flags
.q
.explicit_align
|| expl_align
!= 0) {
7551 unsigned offset
= fields
[i
].offset
!= -1 ? fields
[i
].offset
:
7553 if (align
== 0 || size
== 0) {
7554 _mesa_glsl_error(&loc
, state
, "align can only be used with "
7555 "std430 and std140 layouts");
7556 } else if (qual
->flags
.q
.explicit_align
) {
7557 unsigned member_align
;
7558 if (process_qualifier_constant(state
, &loc
, "align",
7559 qual
->align
, &member_align
)) {
7560 if (member_align
== 0 ||
7561 member_align
& (member_align
- 1)) {
7562 _mesa_glsl_error(&loc
, state
, "align layout qualifier "
7563 "is not a power of 2");
7565 fields
[i
].offset
= glsl_align(offset
, member_align
);
7566 next_offset
= fields
[i
].offset
+ size
;
7570 fields
[i
].offset
= glsl_align(offset
, expl_align
);
7571 next_offset
= fields
[i
].offset
+ size
;
7573 } else if (!qual
->flags
.q
.explicit_offset
) {
7574 if (align
!= 0 && size
!= 0)
7575 next_offset
= glsl_align(next_offset
, align
) + size
;
7578 /* From the ARB_enhanced_layouts spec:
7580 * "The given offset applies to the first component of the first
7581 * member of the qualified entity. Then, within the qualified
7582 * entity, subsequent components are each assigned, in order, to
7583 * the next available offset aligned to a multiple of that
7584 * component's size. Aggregate types are flattened down to the
7585 * component level to get this sequence of components."
7587 if (qual
->flags
.q
.explicit_xfb_offset
) {
7588 unsigned xfb_offset
;
7589 if (process_qualifier_constant(state
, &loc
, "xfb_offset",
7590 qual
->offset
, &xfb_offset
)) {
7591 fields
[i
].offset
= xfb_offset
;
7592 block_xfb_offset
= fields
[i
].offset
+
7593 4 * field_type
->component_slots();
7596 if (layout
&& layout
->flags
.q
.explicit_xfb_offset
) {
7597 unsigned align
= field_type
->is_64bit() ? 8 : 4;
7598 fields
[i
].offset
= glsl_align(block_xfb_offset
, align
);
7599 block_xfb_offset
+= 4 * field_type
->component_slots();
7603 /* Propogate row- / column-major information down the fields of the
7604 * structure or interface block. Structures need this data because
7605 * the structure may contain a structure that contains ... a matrix
7606 * that need the proper layout.
7608 if (is_interface
&& layout
&&
7609 (layout
->flags
.q
.uniform
|| layout
->flags
.q
.buffer
) &&
7610 (field_type
->without_array()->is_matrix()
7611 || field_type
->without_array()->is_struct())) {
7612 /* If no layout is specified for the field, inherit the layout
7615 fields
[i
].matrix_layout
= matrix_layout
;
7617 if (qual
->flags
.q
.row_major
)
7618 fields
[i
].matrix_layout
= GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
7619 else if (qual
->flags
.q
.column_major
)
7620 fields
[i
].matrix_layout
= GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
;
7622 /* If we're processing an uniform or buffer block, the matrix
7623 * layout must be decided by this point.
7625 assert(fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
7626 || fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
);
7629 /* Memory qualifiers are allowed on buffer and image variables, while
7630 * the format qualifier is only accepted for images.
7632 if (var_mode
== ir_var_shader_storage
||
7633 field_type
->without_array()->is_image()) {
7634 /* For readonly and writeonly qualifiers the field definition,
7635 * if set, overwrites the layout qualifier.
7637 if (qual
->flags
.q
.read_only
|| qual
->flags
.q
.write_only
) {
7638 fields
[i
].memory_read_only
= qual
->flags
.q
.read_only
;
7639 fields
[i
].memory_write_only
= qual
->flags
.q
.write_only
;
7641 fields
[i
].memory_read_only
=
7642 layout
? layout
->flags
.q
.read_only
: 0;
7643 fields
[i
].memory_write_only
=
7644 layout
? layout
->flags
.q
.write_only
: 0;
7647 /* For other qualifiers, we set the flag if either the layout
7648 * qualifier or the field qualifier are set
7650 fields
[i
].memory_coherent
= qual
->flags
.q
.coherent
||
7651 (layout
&& layout
->flags
.q
.coherent
);
7652 fields
[i
].memory_volatile
= qual
->flags
.q
._volatile
||
7653 (layout
&& layout
->flags
.q
._volatile
);
7654 fields
[i
].memory_restrict
= qual
->flags
.q
.restrict_flag
||
7655 (layout
&& layout
->flags
.q
.restrict_flag
);
7657 if (field_type
->without_array()->is_image()) {
7658 if (qual
->flags
.q
.explicit_image_format
) {
7659 if (qual
->image_base_type
!=
7660 field_type
->without_array()->sampled_type
) {
7661 _mesa_glsl_error(&loc
, state
, "format qualifier doesn't "
7662 "match the base data type of the image");
7665 fields
[i
].image_format
= qual
->image_format
;
7667 if (!qual
->flags
.q
.write_only
) {
7668 _mesa_glsl_error(&loc
, state
, "image not qualified with "
7669 "`writeonly' must have a format layout "
7673 fields
[i
].image_format
= PIPE_FORMAT_NONE
;
7678 /* Precision qualifiers do not hold any meaning in Desktop GLSL */
7679 if (state
->es_shader
) {
7680 fields
[i
].precision
= select_gles_precision(qual
->precision
,
7685 fields
[i
].precision
= qual
->precision
;
7692 assert(i
== decl_count
);
7694 *fields_ret
= fields
;
7700 ast_struct_specifier::hir(exec_list
*instructions
,
7701 struct _mesa_glsl_parse_state
*state
)
7703 YYLTYPE loc
= this->get_location();
7705 unsigned expl_location
= 0;
7706 if (layout
&& layout
->flags
.q
.explicit_location
) {
7707 if (!process_qualifier_constant(state
, &loc
, "location",
7708 layout
->location
, &expl_location
)) {
7711 expl_location
= VARYING_SLOT_VAR0
+ expl_location
;
7715 glsl_struct_field
*fields
;
7716 unsigned decl_count
=
7717 ast_process_struct_or_iface_block_members(instructions
,
7719 &this->declarations
,
7722 GLSL_MATRIX_LAYOUT_INHERITED
,
7723 false /* allow_reserved_names */,
7726 0, /* for interface only */
7727 0, /* for interface only */
7728 0, /* for interface only */
7730 0 /* for interface only */);
7732 validate_identifier(this->name
, loc
, state
);
7734 type
= glsl_type::get_struct_instance(fields
, decl_count
, this->name
);
7736 if (!type
->is_anonymous() && !state
->symbols
->add_type(name
, type
)) {
7737 const glsl_type
*match
= state
->symbols
->get_type(name
);
7738 /* allow struct matching for desktop GL - older UE4 does this */
7739 if (match
!= NULL
&& state
->is_version(130, 0) && match
->record_compare(type
, true, false))
7740 _mesa_glsl_warning(& loc
, state
, "struct `%s' previously defined", name
);
7742 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
7744 const glsl_type
**s
= reralloc(state
, state
->user_structures
,
7746 state
->num_user_structures
+ 1);
7748 s
[state
->num_user_structures
] = type
;
7749 state
->user_structures
= s
;
7750 state
->num_user_structures
++;
7754 /* Structure type definitions do not have r-values.
7761 * Visitor class which detects whether a given interface block has been used.
7763 class interface_block_usage_visitor
: public ir_hierarchical_visitor
7766 interface_block_usage_visitor(ir_variable_mode mode
, const glsl_type
*block
)
7767 : mode(mode
), block(block
), found(false)
7771 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
7773 if (ir
->var
->data
.mode
== mode
&& ir
->var
->get_interface_type() == block
) {
7777 return visit_continue
;
7780 bool usage_found() const
7786 ir_variable_mode mode
;
7787 const glsl_type
*block
;
7792 is_unsized_array_last_element(ir_variable
*v
)
7794 const glsl_type
*interface_type
= v
->get_interface_type();
7795 int length
= interface_type
->length
;
7797 assert(v
->type
->is_unsized_array());
7799 /* Check if it is the last element of the interface */
7800 if (strcmp(interface_type
->fields
.structure
[length
-1].name
, v
->name
) == 0)
7806 apply_memory_qualifiers(ir_variable
*var
, glsl_struct_field field
)
7808 var
->data
.memory_read_only
= field
.memory_read_only
;
7809 var
->data
.memory_write_only
= field
.memory_write_only
;
7810 var
->data
.memory_coherent
= field
.memory_coherent
;
7811 var
->data
.memory_volatile
= field
.memory_volatile
;
7812 var
->data
.memory_restrict
= field
.memory_restrict
;
7816 ast_interface_block::hir(exec_list
*instructions
,
7817 struct _mesa_glsl_parse_state
*state
)
7819 YYLTYPE loc
= this->get_location();
7821 /* Interface blocks must be declared at global scope */
7822 if (state
->current_function
!= NULL
) {
7823 _mesa_glsl_error(&loc
, state
,
7824 "Interface block `%s' must be declared "
7829 /* Validate qualifiers:
7831 * - Layout Qualifiers as per the table in Section 4.4
7832 * ("Layout Qualifiers") of the GLSL 4.50 spec.
7834 * - Memory Qualifiers as per Section 4.10 ("Memory Qualifiers") of the
7837 * "Additionally, memory qualifiers may also be used in the declaration
7838 * of shader storage blocks"
7840 * Note the table in Section 4.4 says std430 is allowed on both uniform and
7841 * buffer blocks however Section 4.4.5 (Uniform and Shader Storage Block
7842 * Layout Qualifiers) of the GLSL 4.50 spec says:
7844 * "The std430 qualifier is supported only for shader storage blocks;
7845 * using std430 on a uniform block will result in a compile-time error."
7847 ast_type_qualifier allowed_blk_qualifiers
;
7848 allowed_blk_qualifiers
.flags
.i
= 0;
7849 if (this->layout
.flags
.q
.buffer
|| this->layout
.flags
.q
.uniform
) {
7850 allowed_blk_qualifiers
.flags
.q
.shared
= 1;
7851 allowed_blk_qualifiers
.flags
.q
.packed
= 1;
7852 allowed_blk_qualifiers
.flags
.q
.std140
= 1;
7853 allowed_blk_qualifiers
.flags
.q
.row_major
= 1;
7854 allowed_blk_qualifiers
.flags
.q
.column_major
= 1;
7855 allowed_blk_qualifiers
.flags
.q
.explicit_align
= 1;
7856 allowed_blk_qualifiers
.flags
.q
.explicit_binding
= 1;
7857 if (this->layout
.flags
.q
.buffer
) {
7858 allowed_blk_qualifiers
.flags
.q
.buffer
= 1;
7859 allowed_blk_qualifiers
.flags
.q
.std430
= 1;
7860 allowed_blk_qualifiers
.flags
.q
.coherent
= 1;
7861 allowed_blk_qualifiers
.flags
.q
._volatile
= 1;
7862 allowed_blk_qualifiers
.flags
.q
.restrict_flag
= 1;
7863 allowed_blk_qualifiers
.flags
.q
.read_only
= 1;
7864 allowed_blk_qualifiers
.flags
.q
.write_only
= 1;
7866 allowed_blk_qualifiers
.flags
.q
.uniform
= 1;
7869 /* Interface block */
7870 assert(this->layout
.flags
.q
.in
|| this->layout
.flags
.q
.out
);
7872 allowed_blk_qualifiers
.flags
.q
.explicit_location
= 1;
7873 if (this->layout
.flags
.q
.out
) {
7874 allowed_blk_qualifiers
.flags
.q
.out
= 1;
7875 if (state
->stage
== MESA_SHADER_GEOMETRY
||
7876 state
->stage
== MESA_SHADER_TESS_CTRL
||
7877 state
->stage
== MESA_SHADER_TESS_EVAL
||
7878 state
->stage
== MESA_SHADER_VERTEX
) {
7879 allowed_blk_qualifiers
.flags
.q
.explicit_xfb_offset
= 1;
7880 allowed_blk_qualifiers
.flags
.q
.explicit_xfb_buffer
= 1;
7881 allowed_blk_qualifiers
.flags
.q
.xfb_buffer
= 1;
7882 allowed_blk_qualifiers
.flags
.q
.explicit_xfb_stride
= 1;
7883 allowed_blk_qualifiers
.flags
.q
.xfb_stride
= 1;
7884 if (state
->stage
== MESA_SHADER_GEOMETRY
) {
7885 allowed_blk_qualifiers
.flags
.q
.stream
= 1;
7886 allowed_blk_qualifiers
.flags
.q
.explicit_stream
= 1;
7888 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
7889 allowed_blk_qualifiers
.flags
.q
.patch
= 1;
7893 allowed_blk_qualifiers
.flags
.q
.in
= 1;
7894 if (state
->stage
== MESA_SHADER_TESS_EVAL
) {
7895 allowed_blk_qualifiers
.flags
.q
.patch
= 1;
7900 this->layout
.validate_flags(&loc
, state
, allowed_blk_qualifiers
,
7901 "invalid qualifier for block",
7904 enum glsl_interface_packing packing
;
7905 if (this->layout
.flags
.q
.std140
) {
7906 packing
= GLSL_INTERFACE_PACKING_STD140
;
7907 } else if (this->layout
.flags
.q
.packed
) {
7908 packing
= GLSL_INTERFACE_PACKING_PACKED
;
7909 } else if (this->layout
.flags
.q
.std430
) {
7910 packing
= GLSL_INTERFACE_PACKING_STD430
;
7912 /* The default layout is shared.
7914 packing
= GLSL_INTERFACE_PACKING_SHARED
;
7917 ir_variable_mode var_mode
;
7918 const char *iface_type_name
;
7919 if (this->layout
.flags
.q
.in
) {
7920 var_mode
= ir_var_shader_in
;
7921 iface_type_name
= "in";
7922 } else if (this->layout
.flags
.q
.out
) {
7923 var_mode
= ir_var_shader_out
;
7924 iface_type_name
= "out";
7925 } else if (this->layout
.flags
.q
.uniform
) {
7926 var_mode
= ir_var_uniform
;
7927 iface_type_name
= "uniform";
7928 } else if (this->layout
.flags
.q
.buffer
) {
7929 var_mode
= ir_var_shader_storage
;
7930 iface_type_name
= "buffer";
7932 var_mode
= ir_var_auto
;
7933 iface_type_name
= "UNKNOWN";
7934 assert(!"interface block layout qualifier not found!");
7937 enum glsl_matrix_layout matrix_layout
= GLSL_MATRIX_LAYOUT_INHERITED
;
7938 if (this->layout
.flags
.q
.row_major
)
7939 matrix_layout
= GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
7940 else if (this->layout
.flags
.q
.column_major
)
7941 matrix_layout
= GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
;
7943 bool redeclaring_per_vertex
= strcmp(this->block_name
, "gl_PerVertex") == 0;
7944 exec_list declared_variables
;
7945 glsl_struct_field
*fields
;
7947 /* For blocks that accept memory qualifiers (i.e. shader storage), verify
7948 * that we don't have incompatible qualifiers
7950 if (this->layout
.flags
.q
.read_only
&& this->layout
.flags
.q
.write_only
) {
7951 _mesa_glsl_error(&loc
, state
,
7952 "Interface block sets both readonly and writeonly");
7955 unsigned qual_stream
;
7956 if (!process_qualifier_constant(state
, &loc
, "stream", this->layout
.stream
,
7958 !validate_stream_qualifier(&loc
, state
, qual_stream
)) {
7959 /* If the stream qualifier is invalid it doesn't make sense to continue
7960 * on and try to compare stream layouts on member variables against it
7961 * so just return early.
7966 unsigned qual_xfb_buffer
;
7967 if (!process_qualifier_constant(state
, &loc
, "xfb_buffer",
7968 layout
.xfb_buffer
, &qual_xfb_buffer
) ||
7969 !validate_xfb_buffer_qualifier(&loc
, state
, qual_xfb_buffer
)) {
7973 unsigned qual_xfb_offset
;
7974 if (layout
.flags
.q
.explicit_xfb_offset
) {
7975 if (!process_qualifier_constant(state
, &loc
, "xfb_offset",
7976 layout
.offset
, &qual_xfb_offset
)) {
7981 unsigned qual_xfb_stride
;
7982 if (layout
.flags
.q
.explicit_xfb_stride
) {
7983 if (!process_qualifier_constant(state
, &loc
, "xfb_stride",
7984 layout
.xfb_stride
, &qual_xfb_stride
)) {
7989 unsigned expl_location
= 0;
7990 if (layout
.flags
.q
.explicit_location
) {
7991 if (!process_qualifier_constant(state
, &loc
, "location",
7992 layout
.location
, &expl_location
)) {
7995 expl_location
+= this->layout
.flags
.q
.patch
? VARYING_SLOT_PATCH0
7996 : VARYING_SLOT_VAR0
;
8000 unsigned expl_align
= 0;
8001 if (layout
.flags
.q
.explicit_align
) {
8002 if (!process_qualifier_constant(state
, &loc
, "align",
8003 layout
.align
, &expl_align
)) {
8006 if (expl_align
== 0 || expl_align
& (expl_align
- 1)) {
8007 _mesa_glsl_error(&loc
, state
, "align layout qualifier is not a "
8014 unsigned int num_variables
=
8015 ast_process_struct_or_iface_block_members(&declared_variables
,
8017 &this->declarations
,
8021 redeclaring_per_vertex
,
8030 if (!redeclaring_per_vertex
) {
8031 validate_identifier(this->block_name
, loc
, state
);
8033 /* From section 4.3.9 ("Interface Blocks") of the GLSL 4.50 spec:
8035 * "Block names have no other use within a shader beyond interface
8036 * matching; it is a compile-time error to use a block name at global
8037 * scope for anything other than as a block name."
8039 ir_variable
*var
= state
->symbols
->get_variable(this->block_name
);
8040 if (var
&& !var
->type
->is_interface()) {
8041 _mesa_glsl_error(&loc
, state
, "Block name `%s' is "
8042 "already used in the scope.",
8047 const glsl_type
*earlier_per_vertex
= NULL
;
8048 if (redeclaring_per_vertex
) {
8049 /* Find the previous declaration of gl_PerVertex. If we're redeclaring
8050 * the named interface block gl_in, we can find it by looking at the
8051 * previous declaration of gl_in. Otherwise we can find it by looking
8052 * at the previous decalartion of any of the built-in outputs,
8055 * Also check that the instance name and array-ness of the redeclaration
8059 case ir_var_shader_in
:
8060 if (ir_variable
*earlier_gl_in
=
8061 state
->symbols
->get_variable("gl_in")) {
8062 earlier_per_vertex
= earlier_gl_in
->get_interface_type();
8064 _mesa_glsl_error(&loc
, state
,
8065 "redeclaration of gl_PerVertex input not allowed "
8067 _mesa_shader_stage_to_string(state
->stage
));
8069 if (this->instance_name
== NULL
||
8070 strcmp(this->instance_name
, "gl_in") != 0 || this->array_specifier
== NULL
||
8071 !this->array_specifier
->is_single_dimension()) {
8072 _mesa_glsl_error(&loc
, state
,
8073 "gl_PerVertex input must be redeclared as "
8077 case ir_var_shader_out
:
8078 if (ir_variable
*earlier_gl_Position
=
8079 state
->symbols
->get_variable("gl_Position")) {
8080 earlier_per_vertex
= earlier_gl_Position
->get_interface_type();
8081 } else if (ir_variable
*earlier_gl_out
=
8082 state
->symbols
->get_variable("gl_out")) {
8083 earlier_per_vertex
= earlier_gl_out
->get_interface_type();
8085 _mesa_glsl_error(&loc
, state
,
8086 "redeclaration of gl_PerVertex output not "
8087 "allowed in the %s shader",
8088 _mesa_shader_stage_to_string(state
->stage
));
8090 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
8091 if (this->instance_name
== NULL
||
8092 strcmp(this->instance_name
, "gl_out") != 0 || this->array_specifier
== NULL
) {
8093 _mesa_glsl_error(&loc
, state
,
8094 "gl_PerVertex output must be redeclared as "
8098 if (this->instance_name
!= NULL
) {
8099 _mesa_glsl_error(&loc
, state
,
8100 "gl_PerVertex output may not be redeclared with "
8101 "an instance name");
8106 _mesa_glsl_error(&loc
, state
,
8107 "gl_PerVertex must be declared as an input or an "
8112 if (earlier_per_vertex
== NULL
) {
8113 /* An error has already been reported. Bail out to avoid null
8114 * dereferences later in this function.
8119 /* Copy locations from the old gl_PerVertex interface block. */
8120 for (unsigned i
= 0; i
< num_variables
; i
++) {
8121 int j
= earlier_per_vertex
->field_index(fields
[i
].name
);
8123 _mesa_glsl_error(&loc
, state
,
8124 "redeclaration of gl_PerVertex must be a subset "
8125 "of the built-in members of gl_PerVertex");
8127 fields
[i
].location
=
8128 earlier_per_vertex
->fields
.structure
[j
].location
;
8130 earlier_per_vertex
->fields
.structure
[j
].offset
;
8131 fields
[i
].interpolation
=
8132 earlier_per_vertex
->fields
.structure
[j
].interpolation
;
8133 fields
[i
].centroid
=
8134 earlier_per_vertex
->fields
.structure
[j
].centroid
;
8136 earlier_per_vertex
->fields
.structure
[j
].sample
;
8138 earlier_per_vertex
->fields
.structure
[j
].patch
;
8139 fields
[i
].precision
=
8140 earlier_per_vertex
->fields
.structure
[j
].precision
;
8141 fields
[i
].explicit_xfb_buffer
=
8142 earlier_per_vertex
->fields
.structure
[j
].explicit_xfb_buffer
;
8143 fields
[i
].xfb_buffer
=
8144 earlier_per_vertex
->fields
.structure
[j
].xfb_buffer
;
8145 fields
[i
].xfb_stride
=
8146 earlier_per_vertex
->fields
.structure
[j
].xfb_stride
;
8150 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10
8153 * If a built-in interface block is redeclared, it must appear in
8154 * the shader before any use of any member included in the built-in
8155 * declaration, or a compilation error will result.
8157 * This appears to be a clarification to the behaviour established for
8158 * gl_PerVertex by GLSL 1.50, therefore we implement this behaviour
8159 * regardless of GLSL version.
8161 interface_block_usage_visitor
v(var_mode
, earlier_per_vertex
);
8162 v
.run(instructions
);
8163 if (v
.usage_found()) {
8164 _mesa_glsl_error(&loc
, state
,
8165 "redeclaration of a built-in interface block must "
8166 "appear before any use of any member of the "
8171 const glsl_type
*block_type
=
8172 glsl_type::get_interface_instance(fields
,
8176 GLSL_MATRIX_LAYOUT_ROW_MAJOR
,
8179 unsigned component_size
= block_type
->contains_double() ? 8 : 4;
8181 layout
.flags
.q
.explicit_xfb_offset
? (int) qual_xfb_offset
: -1;
8182 validate_xfb_offset_qualifier(&loc
, state
, xfb_offset
, block_type
,
8185 if (!state
->symbols
->add_interface(block_type
->name
, block_type
, var_mode
)) {
8186 YYLTYPE loc
= this->get_location();
8187 _mesa_glsl_error(&loc
, state
, "interface block `%s' with type `%s' "
8188 "already taken in the current scope",
8189 this->block_name
, iface_type_name
);
8192 /* Since interface blocks cannot contain statements, it should be
8193 * impossible for the block to generate any instructions.
8195 assert(declared_variables
.is_empty());
8197 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
8199 * Geometry shader input variables get the per-vertex values written
8200 * out by vertex shader output variables of the same names. Since a
8201 * geometry shader operates on a set of vertices, each input varying
8202 * variable (or input block, see interface blocks below) needs to be
8203 * declared as an array.
8205 if (state
->stage
== MESA_SHADER_GEOMETRY
&& this->array_specifier
== NULL
&&
8206 var_mode
== ir_var_shader_in
) {
8207 _mesa_glsl_error(&loc
, state
, "geometry shader inputs must be arrays");
8208 } else if ((state
->stage
== MESA_SHADER_TESS_CTRL
||
8209 state
->stage
== MESA_SHADER_TESS_EVAL
) &&
8210 !this->layout
.flags
.q
.patch
&&
8211 this->array_specifier
== NULL
&&
8212 var_mode
== ir_var_shader_in
) {
8213 _mesa_glsl_error(&loc
, state
, "per-vertex tessellation shader inputs must be arrays");
8214 } else if (state
->stage
== MESA_SHADER_TESS_CTRL
&&
8215 !this->layout
.flags
.q
.patch
&&
8216 this->array_specifier
== NULL
&&
8217 var_mode
== ir_var_shader_out
) {
8218 _mesa_glsl_error(&loc
, state
, "tessellation control shader outputs must be arrays");
8222 /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
8225 * "If an instance name (instance-name) is used, then it puts all the
8226 * members inside a scope within its own name space, accessed with the
8227 * field selector ( . ) operator (analogously to structures)."
8229 if (this->instance_name
) {
8230 if (redeclaring_per_vertex
) {
8231 /* When a built-in in an unnamed interface block is redeclared,
8232 * get_variable_being_redeclared() calls
8233 * check_builtin_array_max_size() to make sure that built-in array
8234 * variables aren't redeclared to illegal sizes. But we're looking
8235 * at a redeclaration of a named built-in interface block. So we
8236 * have to manually call check_builtin_array_max_size() for all parts
8237 * of the interface that are arrays.
8239 for (unsigned i
= 0; i
< num_variables
; i
++) {
8240 if (fields
[i
].type
->is_array()) {
8241 const unsigned size
= fields
[i
].type
->array_size();
8242 check_builtin_array_max_size(fields
[i
].name
, size
, loc
, state
);
8246 validate_identifier(this->instance_name
, loc
, state
);
8251 if (this->array_specifier
!= NULL
) {
8252 const glsl_type
*block_array_type
=
8253 process_array_type(&loc
, block_type
, this->array_specifier
, state
);
8255 /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
8257 * For uniform blocks declared an array, each individual array
8258 * element corresponds to a separate buffer object backing one
8259 * instance of the block. As the array size indicates the number
8260 * of buffer objects needed, uniform block array declarations
8261 * must specify an array size.
8263 * And a few paragraphs later:
8265 * Geometry shader input blocks must be declared as arrays and
8266 * follow the array declaration and linking rules for all
8267 * geometry shader inputs. All other input and output block
8268 * arrays must specify an array size.
8270 * The same applies to tessellation shaders.
8272 * The upshot of this is that the only circumstance where an
8273 * interface array size *doesn't* need to be specified is on a
8274 * geometry shader input, tessellation control shader input,
8275 * tessellation control shader output, and tessellation evaluation
8278 if (block_array_type
->is_unsized_array()) {
8279 bool allow_inputs
= state
->stage
== MESA_SHADER_GEOMETRY
||
8280 state
->stage
== MESA_SHADER_TESS_CTRL
||
8281 state
->stage
== MESA_SHADER_TESS_EVAL
;
8282 bool allow_outputs
= state
->stage
== MESA_SHADER_TESS_CTRL
;
8284 if (this->layout
.flags
.q
.in
) {
8286 _mesa_glsl_error(&loc
, state
,
8287 "unsized input block arrays not allowed in "
8289 _mesa_shader_stage_to_string(state
->stage
));
8290 } else if (this->layout
.flags
.q
.out
) {
8292 _mesa_glsl_error(&loc
, state
,
8293 "unsized output block arrays not allowed in "
8295 _mesa_shader_stage_to_string(state
->stage
));
8297 /* by elimination, this is a uniform block array */
8298 _mesa_glsl_error(&loc
, state
,
8299 "unsized uniform block arrays not allowed in "
8301 _mesa_shader_stage_to_string(state
->stage
));
8305 /* From section 4.3.9 (Interface Blocks) of the GLSL ES 3.10 spec:
8307 * * Arrays of arrays of blocks are not allowed
8309 if (state
->es_shader
&& block_array_type
->is_array() &&
8310 block_array_type
->fields
.array
->is_array()) {
8311 _mesa_glsl_error(&loc
, state
,
8312 "arrays of arrays interface blocks are "
8316 var
= new(state
) ir_variable(block_array_type
,
8317 this->instance_name
,
8320 var
= new(state
) ir_variable(block_type
,
8321 this->instance_name
,
8325 var
->data
.matrix_layout
= matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
8326 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
: matrix_layout
;
8328 if (var_mode
== ir_var_shader_in
|| var_mode
== ir_var_uniform
)
8329 var
->data
.read_only
= true;
8331 var
->data
.patch
= this->layout
.flags
.q
.patch
;
8333 if (state
->stage
== MESA_SHADER_GEOMETRY
&& var_mode
== ir_var_shader_in
)
8334 handle_geometry_shader_input_decl(state
, loc
, var
);
8335 else if ((state
->stage
== MESA_SHADER_TESS_CTRL
||
8336 state
->stage
== MESA_SHADER_TESS_EVAL
) && var_mode
== ir_var_shader_in
)
8337 handle_tess_shader_input_decl(state
, loc
, var
);
8338 else if (state
->stage
== MESA_SHADER_TESS_CTRL
&& var_mode
== ir_var_shader_out
)
8339 handle_tess_ctrl_shader_output_decl(state
, loc
, var
);
8341 for (unsigned i
= 0; i
< num_variables
; i
++) {
8342 if (var
->data
.mode
== ir_var_shader_storage
)
8343 apply_memory_qualifiers(var
, fields
[i
]);
8346 if (ir_variable
*earlier
=
8347 state
->symbols
->get_variable(this->instance_name
)) {
8348 if (!redeclaring_per_vertex
) {
8349 _mesa_glsl_error(&loc
, state
, "`%s' redeclared",
8350 this->instance_name
);
8352 earlier
->data
.how_declared
= ir_var_declared_normally
;
8353 earlier
->type
= var
->type
;
8354 earlier
->reinit_interface_type(block_type
);
8357 if (this->layout
.flags
.q
.explicit_binding
) {
8358 apply_explicit_binding(state
, &loc
, var
, var
->type
,
8362 var
->data
.stream
= qual_stream
;
8363 if (layout
.flags
.q
.explicit_location
) {
8364 var
->data
.location
= expl_location
;
8365 var
->data
.explicit_location
= true;
8368 state
->symbols
->add_variable(var
);
8369 instructions
->push_tail(var
);
8372 /* In order to have an array size, the block must also be declared with
8375 assert(this->array_specifier
== NULL
);
8377 for (unsigned i
= 0; i
< num_variables
; i
++) {
8379 new(state
) ir_variable(fields
[i
].type
,
8380 ralloc_strdup(state
, fields
[i
].name
),
8382 var
->data
.interpolation
= fields
[i
].interpolation
;
8383 var
->data
.centroid
= fields
[i
].centroid
;
8384 var
->data
.sample
= fields
[i
].sample
;
8385 var
->data
.patch
= fields
[i
].patch
;
8386 var
->data
.stream
= qual_stream
;
8387 var
->data
.location
= fields
[i
].location
;
8389 if (fields
[i
].location
!= -1)
8390 var
->data
.explicit_location
= true;
8392 var
->data
.explicit_xfb_buffer
= fields
[i
].explicit_xfb_buffer
;
8393 var
->data
.xfb_buffer
= fields
[i
].xfb_buffer
;
8395 if (fields
[i
].offset
!= -1)
8396 var
->data
.explicit_xfb_offset
= true;
8397 var
->data
.offset
= fields
[i
].offset
;
8399 var
->init_interface_type(block_type
);
8401 if (var_mode
== ir_var_shader_in
|| var_mode
== ir_var_uniform
)
8402 var
->data
.read_only
= true;
8404 /* Precision qualifiers do not have any meaning in Desktop GLSL */
8405 if (state
->es_shader
) {
8406 var
->data
.precision
=
8407 select_gles_precision(fields
[i
].precision
, fields
[i
].type
,
8411 if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
) {
8412 var
->data
.matrix_layout
= matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
8413 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
: matrix_layout
;
8415 var
->data
.matrix_layout
= fields
[i
].matrix_layout
;
8418 if (var
->data
.mode
== ir_var_shader_storage
)
8419 apply_memory_qualifiers(var
, fields
[i
]);
8421 /* Examine var name here since var may get deleted in the next call */
8422 bool var_is_gl_id
= is_gl_identifier(var
->name
);
8424 if (redeclaring_per_vertex
) {
8425 bool is_redeclaration
;
8427 get_variable_being_redeclared(&var
, loc
, state
,
8428 true /* allow_all_redeclarations */,
8430 if (!var_is_gl_id
|| !is_redeclaration
) {
8431 _mesa_glsl_error(&loc
, state
,
8432 "redeclaration of gl_PerVertex can only "
8433 "include built-in variables");
8434 } else if (var
->data
.how_declared
== ir_var_declared_normally
) {
8435 _mesa_glsl_error(&loc
, state
,
8436 "`%s' has already been redeclared",
8439 var
->data
.how_declared
= ir_var_declared_in_block
;
8440 var
->reinit_interface_type(block_type
);
8445 if (state
->symbols
->get_variable(var
->name
) != NULL
)
8446 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
8448 /* Propagate the "binding" keyword into this UBO/SSBO's fields.
8449 * The UBO declaration itself doesn't get an ir_variable unless it
8450 * has an instance name. This is ugly.
8452 if (this->layout
.flags
.q
.explicit_binding
) {
8453 apply_explicit_binding(state
, &loc
, var
,
8454 var
->get_interface_type(), &this->layout
);
8457 if (var
->type
->is_unsized_array()) {
8458 if (var
->is_in_shader_storage_block() &&
8459 is_unsized_array_last_element(var
)) {
8460 var
->data
.from_ssbo_unsized_array
= true;
8462 /* From GLSL ES 3.10 spec, section 4.1.9 "Arrays":
8464 * "If an array is declared as the last member of a shader storage
8465 * block and the size is not specified at compile-time, it is
8466 * sized at run-time. In all other cases, arrays are sized only
8469 * In desktop GLSL it is allowed to have unsized-arrays that are
8470 * not last, as long as we can determine that they are implicitly
8473 if (state
->es_shader
) {
8474 _mesa_glsl_error(&loc
, state
, "unsized array `%s' "
8475 "definition: only last member of a shader "
8476 "storage block can be defined as unsized "
8477 "array", fields
[i
].name
);
8482 state
->symbols
->add_variable(var
);
8483 instructions
->push_tail(var
);
8486 if (redeclaring_per_vertex
&& block_type
!= earlier_per_vertex
) {
8487 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec:
8489 * It is also a compilation error ... to redeclare a built-in
8490 * block and then use a member from that built-in block that was
8491 * not included in the redeclaration.
8493 * This appears to be a clarification to the behaviour established
8494 * for gl_PerVertex by GLSL 1.50, therefore we implement this
8495 * behaviour regardless of GLSL version.
8497 * To prevent the shader from using a member that was not included in
8498 * the redeclaration, we disable any ir_variables that are still
8499 * associated with the old declaration of gl_PerVertex (since we've
8500 * already updated all of the variables contained in the new
8501 * gl_PerVertex to point to it).
8503 * As a side effect this will prevent
8504 * validate_intrastage_interface_blocks() from getting confused and
8505 * thinking there are conflicting definitions of gl_PerVertex in the
8508 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
8509 ir_variable
*const var
= node
->as_variable();
8511 var
->get_interface_type() == earlier_per_vertex
&&
8512 var
->data
.mode
== var_mode
) {
8513 if (var
->data
.how_declared
== ir_var_declared_normally
) {
8514 _mesa_glsl_error(&loc
, state
,
8515 "redeclaration of gl_PerVertex cannot "
8516 "follow a redeclaration of `%s'",
8519 state
->symbols
->disable_variable(var
->name
);
8531 ast_tcs_output_layout::hir(exec_list
*instructions
,
8532 struct _mesa_glsl_parse_state
*state
)
8534 YYLTYPE loc
= this->get_location();
8536 unsigned num_vertices
;
8537 if (!state
->out_qualifier
->vertices
->
8538 process_qualifier_constant(state
, "vertices", &num_vertices
,
8540 /* return here to stop cascading incorrect error messages */
8544 /* If any shader outputs occurred before this declaration and specified an
8545 * array size, make sure the size they specified is consistent with the
8548 if (state
->tcs_output_size
!= 0 && state
->tcs_output_size
!= num_vertices
) {
8549 _mesa_glsl_error(&loc
, state
,
8550 "this tessellation control shader output layout "
8551 "specifies %u vertices, but a previous output "
8552 "is declared with size %u",
8553 num_vertices
, state
->tcs_output_size
);
8557 state
->tcs_output_vertices_specified
= true;
8559 /* If any shader outputs occurred before this declaration and did not
8560 * specify an array size, their size is determined now.
8562 foreach_in_list (ir_instruction
, node
, instructions
) {
8563 ir_variable
*var
= node
->as_variable();
8564 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_out
)
8567 /* Note: Not all tessellation control shader output are arrays. */
8568 if (!var
->type
->is_unsized_array() || var
->data
.patch
)
8571 if (var
->data
.max_array_access
>= (int)num_vertices
) {
8572 _mesa_glsl_error(&loc
, state
,
8573 "this tessellation control shader output layout "
8574 "specifies %u vertices, but an access to element "
8575 "%u of output `%s' already exists", num_vertices
,
8576 var
->data
.max_array_access
, var
->name
);
8578 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
8588 ast_gs_input_layout::hir(exec_list
*instructions
,
8589 struct _mesa_glsl_parse_state
*state
)
8591 YYLTYPE loc
= this->get_location();
8593 /* Should have been prevented by the parser. */
8594 assert(!state
->gs_input_prim_type_specified
8595 || state
->in_qualifier
->prim_type
== this->prim_type
);
8597 /* If any shader inputs occurred before this declaration and specified an
8598 * array size, make sure the size they specified is consistent with the
8601 unsigned num_vertices
= vertices_per_prim(this->prim_type
);
8602 if (state
->gs_input_size
!= 0 && state
->gs_input_size
!= num_vertices
) {
8603 _mesa_glsl_error(&loc
, state
,
8604 "this geometry shader input layout implies %u vertices"
8605 " per primitive, but a previous input is declared"
8606 " with size %u", num_vertices
, state
->gs_input_size
);
8610 state
->gs_input_prim_type_specified
= true;
8612 /* If any shader inputs occurred before this declaration and did not
8613 * specify an array size, their size is determined now.
8615 foreach_in_list(ir_instruction
, node
, instructions
) {
8616 ir_variable
*var
= node
->as_variable();
8617 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_in
)
8620 /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
8624 if (var
->type
->is_unsized_array()) {
8625 if (var
->data
.max_array_access
>= (int)num_vertices
) {
8626 _mesa_glsl_error(&loc
, state
,
8627 "this geometry shader input layout implies %u"
8628 " vertices, but an access to element %u of input"
8629 " `%s' already exists", num_vertices
,
8630 var
->data
.max_array_access
, var
->name
);
8632 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
8643 ast_cs_input_layout::hir(exec_list
*instructions
,
8644 struct _mesa_glsl_parse_state
*state
)
8646 YYLTYPE loc
= this->get_location();
8648 /* From the ARB_compute_shader specification:
8650 * If the local size of the shader in any dimension is greater
8651 * than the maximum size supported by the implementation for that
8652 * dimension, a compile-time error results.
8654 * It is not clear from the spec how the error should be reported if
8655 * the total size of the work group exceeds
8656 * MAX_COMPUTE_WORK_GROUP_INVOCATIONS, but it seems reasonable to
8657 * report it at compile time as well.
8659 GLuint64 total_invocations
= 1;
8660 unsigned qual_local_size
[3];
8661 for (int i
= 0; i
< 3; i
++) {
8663 char *local_size_str
= ralloc_asprintf(NULL
, "invalid local_size_%c",
8665 /* Infer a local_size of 1 for unspecified dimensions */
8666 if (this->local_size
[i
] == NULL
) {
8667 qual_local_size
[i
] = 1;
8668 } else if (!this->local_size
[i
]->
8669 process_qualifier_constant(state
, local_size_str
,
8670 &qual_local_size
[i
], false)) {
8671 ralloc_free(local_size_str
);
8674 ralloc_free(local_size_str
);
8676 if (qual_local_size
[i
] > state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]) {
8677 _mesa_glsl_error(&loc
, state
,
8678 "local_size_%c exceeds MAX_COMPUTE_WORK_GROUP_SIZE"
8680 state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]);
8683 total_invocations
*= qual_local_size
[i
];
8684 if (total_invocations
>
8685 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
) {
8686 _mesa_glsl_error(&loc
, state
,
8687 "product of local_sizes exceeds "
8688 "MAX_COMPUTE_WORK_GROUP_INVOCATIONS (%d)",
8689 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
);
8694 /* If any compute input layout declaration preceded this one, make sure it
8695 * was consistent with this one.
8697 if (state
->cs_input_local_size_specified
) {
8698 for (int i
= 0; i
< 3; i
++) {
8699 if (state
->cs_input_local_size
[i
] != qual_local_size
[i
]) {
8700 _mesa_glsl_error(&loc
, state
,
8701 "compute shader input layout does not match"
8702 " previous declaration");
8708 /* The ARB_compute_variable_group_size spec says:
8710 * If a compute shader including a *local_size_variable* qualifier also
8711 * declares a fixed local group size using the *local_size_x*,
8712 * *local_size_y*, or *local_size_z* qualifiers, a compile-time error
8715 if (state
->cs_input_local_size_variable_specified
) {
8716 _mesa_glsl_error(&loc
, state
,
8717 "compute shader can't include both a variable and a "
8718 "fixed local group size");
8722 state
->cs_input_local_size_specified
= true;
8723 for (int i
= 0; i
< 3; i
++)
8724 state
->cs_input_local_size
[i
] = qual_local_size
[i
];
8726 /* We may now declare the built-in constant gl_WorkGroupSize (see
8727 * builtin_variable_generator::generate_constants() for why we didn't
8728 * declare it earlier).
8730 ir_variable
*var
= new(state
->symbols
)
8731 ir_variable(glsl_type::uvec3_type
, "gl_WorkGroupSize", ir_var_auto
);
8732 var
->data
.how_declared
= ir_var_declared_implicitly
;
8733 var
->data
.read_only
= true;
8734 instructions
->push_tail(var
);
8735 state
->symbols
->add_variable(var
);
8736 ir_constant_data data
;
8737 memset(&data
, 0, sizeof(data
));
8738 for (int i
= 0; i
< 3; i
++)
8739 data
.u
[i
] = qual_local_size
[i
];
8740 var
->constant_value
= new(var
) ir_constant(glsl_type::uvec3_type
, &data
);
8741 var
->constant_initializer
=
8742 new(var
) ir_constant(glsl_type::uvec3_type
, &data
);
8743 var
->data
.has_initializer
= true;
8750 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
8751 exec_list
*instructions
)
8753 bool gl_FragColor_assigned
= false;
8754 bool gl_FragData_assigned
= false;
8755 bool gl_FragSecondaryColor_assigned
= false;
8756 bool gl_FragSecondaryData_assigned
= false;
8757 bool user_defined_fs_output_assigned
= false;
8758 ir_variable
*user_defined_fs_output
= NULL
;
8760 /* It would be nice to have proper location information. */
8762 memset(&loc
, 0, sizeof(loc
));
8764 foreach_in_list(ir_instruction
, node
, instructions
) {
8765 ir_variable
*var
= node
->as_variable();
8767 if (!var
|| !var
->data
.assigned
)
8770 if (strcmp(var
->name
, "gl_FragColor") == 0)
8771 gl_FragColor_assigned
= true;
8772 else if (strcmp(var
->name
, "gl_FragData") == 0)
8773 gl_FragData_assigned
= true;
8774 else if (strcmp(var
->name
, "gl_SecondaryFragColorEXT") == 0)
8775 gl_FragSecondaryColor_assigned
= true;
8776 else if (strcmp(var
->name
, "gl_SecondaryFragDataEXT") == 0)
8777 gl_FragSecondaryData_assigned
= true;
8778 else if (!is_gl_identifier(var
->name
)) {
8779 if (state
->stage
== MESA_SHADER_FRAGMENT
&&
8780 var
->data
.mode
== ir_var_shader_out
) {
8781 user_defined_fs_output_assigned
= true;
8782 user_defined_fs_output
= var
;
8787 /* From the GLSL 1.30 spec:
8789 * "If a shader statically assigns a value to gl_FragColor, it
8790 * may not assign a value to any element of gl_FragData. If a
8791 * shader statically writes a value to any element of
8792 * gl_FragData, it may not assign a value to
8793 * gl_FragColor. That is, a shader may assign values to either
8794 * gl_FragColor or gl_FragData, but not both. Multiple shaders
8795 * linked together must also consistently write just one of
8796 * these variables. Similarly, if user declared output
8797 * variables are in use (statically assigned to), then the
8798 * built-in variables gl_FragColor and gl_FragData may not be
8799 * assigned to. These incorrect usages all generate compile
8802 if (gl_FragColor_assigned
&& gl_FragData_assigned
) {
8803 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8804 "`gl_FragColor' and `gl_FragData'");
8805 } else if (gl_FragColor_assigned
&& user_defined_fs_output_assigned
) {
8806 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8807 "`gl_FragColor' and `%s'",
8808 user_defined_fs_output
->name
);
8809 } else if (gl_FragSecondaryColor_assigned
&& gl_FragSecondaryData_assigned
) {
8810 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8811 "`gl_FragSecondaryColorEXT' and"
8812 " `gl_FragSecondaryDataEXT'");
8813 } else if (gl_FragColor_assigned
&& gl_FragSecondaryData_assigned
) {
8814 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8815 "`gl_FragColor' and"
8816 " `gl_FragSecondaryDataEXT'");
8817 } else if (gl_FragData_assigned
&& gl_FragSecondaryColor_assigned
) {
8818 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8820 " `gl_FragSecondaryColorEXT'");
8821 } else if (gl_FragData_assigned
&& user_defined_fs_output_assigned
) {
8822 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8823 "`gl_FragData' and `%s'",
8824 user_defined_fs_output
->name
);
8827 if ((gl_FragSecondaryColor_assigned
|| gl_FragSecondaryData_assigned
) &&
8828 !state
->EXT_blend_func_extended_enable
) {
8829 _mesa_glsl_error(&loc
, state
,
8830 "Dual source blending requires EXT_blend_func_extended");
8835 verify_subroutine_associated_funcs(struct _mesa_glsl_parse_state
*state
)
8838 memset(&loc
, 0, sizeof(loc
));
8840 /* Section 6.1.2 (Subroutines) of the GLSL 4.00 spec says:
8842 * "A program will fail to compile or link if any shader
8843 * or stage contains two or more functions with the same
8844 * name if the name is associated with a subroutine type."
8847 for (int i
= 0; i
< state
->num_subroutines
; i
++) {
8848 unsigned definitions
= 0;
8849 ir_function
*fn
= state
->subroutines
[i
];
8850 /* Calculate number of function definitions with the same name */
8851 foreach_in_list(ir_function_signature
, sig
, &fn
->signatures
) {
8852 if (sig
->is_defined
) {
8853 if (++definitions
> 1) {
8854 _mesa_glsl_error(&loc
, state
,
8855 "%s shader contains two or more function "
8856 "definitions with name `%s', which is "
8857 "associated with a subroutine type.\n",
8858 _mesa_shader_stage_to_string(state
->stage
),
8868 remove_per_vertex_blocks(exec_list
*instructions
,
8869 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
)
8871 /* Find the gl_PerVertex interface block of the appropriate (in/out) mode,
8872 * if it exists in this shader type.
8874 const glsl_type
*per_vertex
= NULL
;
8876 case ir_var_shader_in
:
8877 if (ir_variable
*gl_in
= state
->symbols
->get_variable("gl_in"))
8878 per_vertex
= gl_in
->get_interface_type();
8880 case ir_var_shader_out
:
8881 if (ir_variable
*gl_Position
=
8882 state
->symbols
->get_variable("gl_Position")) {
8883 per_vertex
= gl_Position
->get_interface_type();
8887 assert(!"Unexpected mode");
8891 /* If we didn't find a built-in gl_PerVertex interface block, then we don't
8892 * need to do anything.
8894 if (per_vertex
== NULL
)
8897 /* If the interface block is used by the shader, then we don't need to do
8900 interface_block_usage_visitor
v(mode
, per_vertex
);
8901 v
.run(instructions
);
8902 if (v
.usage_found())
8905 /* Remove any ir_variable declarations that refer to the interface block
8908 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
8909 ir_variable
*const var
= node
->as_variable();
8910 if (var
!= NULL
&& var
->get_interface_type() == per_vertex
&&
8911 var
->data
.mode
== mode
) {
8912 state
->symbols
->disable_variable(var
->name
);
8919 ast_warnings_toggle::hir(exec_list
*,
8920 struct _mesa_glsl_parse_state
*state
)
8922 state
->warnings_enabled
= enable
;