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_int_to_uint_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(state
->allow_glsl_120_subset_in_110
? 110 : 120,
967 "whole array assignment forbidden")) {
968 /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
970 * "Other binary or unary expressions, non-dereferenced
971 * arrays, function names, swizzles with repeated fields,
972 * and constants cannot be l-values."
974 * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00.
976 error_emitted
= true;
977 } else if (!lhs
->is_lvalue(state
)) {
978 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
979 error_emitted
= true;
984 validate_assignment(state
, lhs_loc
, lhs
, rhs
, is_initializer
);
985 if (new_rhs
!= NULL
) {
988 /* If the LHS array was not declared with a size, it takes it size from
989 * the RHS. If the LHS is an l-value and a whole array, it must be a
990 * dereference of a variable. Any other case would require that the LHS
991 * is either not an l-value or not a whole array.
993 if (lhs
->type
->is_unsized_array()) {
994 ir_dereference
*const d
= lhs
->as_dereference();
998 ir_variable
*const var
= d
->variable_referenced();
1000 assert(var
!= NULL
);
1002 if (var
->data
.max_array_access
>= rhs
->type
->array_size()) {
1003 /* FINISHME: This should actually log the location of the RHS. */
1004 _mesa_glsl_error(& lhs_loc
, state
, "array size must be > %u due to "
1006 var
->data
.max_array_access
);
1009 var
->type
= glsl_type::get_array_instance(lhs
->type
->fields
.array
,
1010 rhs
->type
->array_size());
1011 d
->type
= var
->type
;
1013 if (lhs
->type
->is_array()) {
1014 mark_whole_array_access(rhs
);
1015 mark_whole_array_access(lhs
);
1018 error_emitted
= true;
1021 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
1022 * but not post_inc) need the converted assigned value as an rvalue
1023 * to handle things like:
1029 if (!error_emitted
) {
1030 ir_variable
*var
= new(ctx
) ir_variable(rhs
->type
, "assignment_tmp",
1032 instructions
->push_tail(var
);
1033 instructions
->push_tail(assign(var
, rhs
));
1035 ir_dereference_variable
*deref_var
=
1036 new(ctx
) ir_dereference_variable(var
);
1037 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, deref_var
));
1038 rvalue
= new(ctx
) ir_dereference_variable(var
);
1040 rvalue
= ir_rvalue::error_value(ctx
);
1042 *out_rvalue
= rvalue
;
1045 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, rhs
));
1049 return error_emitted
;
1053 get_lvalue_copy(exec_list
*instructions
, ir_rvalue
*lvalue
)
1055 void *ctx
= ralloc_parent(lvalue
);
1058 var
= new(ctx
) ir_variable(lvalue
->type
, "_post_incdec_tmp",
1060 instructions
->push_tail(var
);
1062 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
1065 return new(ctx
) ir_dereference_variable(var
);
1070 ast_node::hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
1072 (void) instructions
;
1079 ast_node::has_sequence_subexpression() const
1085 ast_node::set_is_lhs(bool /* new_value */)
1090 ast_function_expression::hir_no_rvalue(exec_list
*instructions
,
1091 struct _mesa_glsl_parse_state
*state
)
1093 (void)hir(instructions
, state
);
1097 ast_aggregate_initializer::hir_no_rvalue(exec_list
*instructions
,
1098 struct _mesa_glsl_parse_state
*state
)
1100 (void)hir(instructions
, state
);
1104 do_comparison(void *mem_ctx
, int operation
, ir_rvalue
*op0
, ir_rvalue
*op1
)
1107 ir_rvalue
*cmp
= NULL
;
1109 if (operation
== ir_binop_all_equal
)
1110 join_op
= ir_binop_logic_and
;
1112 join_op
= ir_binop_logic_or
;
1114 switch (op0
->type
->base_type
) {
1115 case GLSL_TYPE_FLOAT
:
1116 case GLSL_TYPE_FLOAT16
:
1117 case GLSL_TYPE_UINT
:
1119 case GLSL_TYPE_BOOL
:
1120 case GLSL_TYPE_DOUBLE
:
1121 case GLSL_TYPE_UINT64
:
1122 case GLSL_TYPE_INT64
:
1123 case GLSL_TYPE_UINT16
:
1124 case GLSL_TYPE_INT16
:
1125 case GLSL_TYPE_UINT8
:
1126 case GLSL_TYPE_INT8
:
1127 return new(mem_ctx
) ir_expression(operation
, op0
, op1
);
1129 case GLSL_TYPE_ARRAY
: {
1130 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
1131 ir_rvalue
*e0
, *e1
, *result
;
1133 e0
= new(mem_ctx
) ir_dereference_array(op0
->clone(mem_ctx
, NULL
),
1134 new(mem_ctx
) ir_constant(i
));
1135 e1
= new(mem_ctx
) ir_dereference_array(op1
->clone(mem_ctx
, NULL
),
1136 new(mem_ctx
) ir_constant(i
));
1137 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
1140 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
1146 mark_whole_array_access(op0
);
1147 mark_whole_array_access(op1
);
1151 case GLSL_TYPE_STRUCT
: {
1152 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
1153 ir_rvalue
*e0
, *e1
, *result
;
1154 const char *field_name
= op0
->type
->fields
.structure
[i
].name
;
1156 e0
= new(mem_ctx
) ir_dereference_record(op0
->clone(mem_ctx
, NULL
),
1158 e1
= new(mem_ctx
) ir_dereference_record(op1
->clone(mem_ctx
, NULL
),
1160 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
1163 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
1171 case GLSL_TYPE_ERROR
:
1172 case GLSL_TYPE_VOID
:
1173 case GLSL_TYPE_SAMPLER
:
1174 case GLSL_TYPE_IMAGE
:
1175 case GLSL_TYPE_INTERFACE
:
1176 case GLSL_TYPE_ATOMIC_UINT
:
1177 case GLSL_TYPE_SUBROUTINE
:
1178 case GLSL_TYPE_FUNCTION
:
1179 /* I assume a comparison of a struct containing a sampler just
1180 * ignores the sampler present in the type.
1186 cmp
= new(mem_ctx
) ir_constant(true);
1191 /* For logical operations, we want to ensure that the operands are
1192 * scalar booleans. If it isn't, emit an error and return a constant
1193 * boolean to avoid triggering cascading error messages.
1196 get_scalar_boolean_operand(exec_list
*instructions
,
1197 struct _mesa_glsl_parse_state
*state
,
1198 ast_expression
*parent_expr
,
1200 const char *operand_name
,
1201 bool *error_emitted
)
1203 ast_expression
*expr
= parent_expr
->subexpressions
[operand
];
1205 ir_rvalue
*val
= expr
->hir(instructions
, state
);
1207 if (val
->type
->is_boolean() && val
->type
->is_scalar())
1210 if (!*error_emitted
) {
1211 YYLTYPE loc
= expr
->get_location();
1212 _mesa_glsl_error(&loc
, state
, "%s of `%s' must be scalar boolean",
1214 parent_expr
->operator_string(parent_expr
->oper
));
1215 *error_emitted
= true;
1218 return new(ctx
) ir_constant(true);
1222 * If name refers to a builtin array whose maximum allowed size is less than
1223 * size, report an error and return true. Otherwise return false.
1226 check_builtin_array_max_size(const char *name
, unsigned size
,
1227 YYLTYPE loc
, struct _mesa_glsl_parse_state
*state
)
1229 if ((strcmp("gl_TexCoord", name
) == 0)
1230 && (size
> state
->Const
.MaxTextureCoords
)) {
1231 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1233 * "The size [of gl_TexCoord] can be at most
1234 * gl_MaxTextureCoords."
1236 _mesa_glsl_error(&loc
, state
, "`gl_TexCoord' array size cannot "
1237 "be larger than gl_MaxTextureCoords (%u)",
1238 state
->Const
.MaxTextureCoords
);
1239 } else if (strcmp("gl_ClipDistance", name
) == 0) {
1240 state
->clip_dist_size
= size
;
1241 if (size
+ state
->cull_dist_size
> state
->Const
.MaxClipPlanes
) {
1242 /* From section 7.1 (Vertex Shader Special Variables) of the
1245 * "The gl_ClipDistance array is predeclared as unsized and
1246 * must be sized by the shader either redeclaring it with a
1247 * size or indexing it only with integral constant
1248 * expressions. ... The size can be at most
1249 * gl_MaxClipDistances."
1251 _mesa_glsl_error(&loc
, state
, "`gl_ClipDistance' array size cannot "
1252 "be larger than gl_MaxClipDistances (%u)",
1253 state
->Const
.MaxClipPlanes
);
1255 } else if (strcmp("gl_CullDistance", name
) == 0) {
1256 state
->cull_dist_size
= size
;
1257 if (size
+ state
->clip_dist_size
> state
->Const
.MaxClipPlanes
) {
1258 /* From the ARB_cull_distance spec:
1260 * "The gl_CullDistance array is predeclared as unsized and
1261 * must be sized by the shader either redeclaring it with
1262 * a size or indexing it only with integral constant
1263 * expressions. The size determines the number and set of
1264 * enabled cull distances and can be at most
1265 * gl_MaxCullDistances."
1267 _mesa_glsl_error(&loc
, state
, "`gl_CullDistance' array size cannot "
1268 "be larger than gl_MaxCullDistances (%u)",
1269 state
->Const
.MaxClipPlanes
);
1275 * Create the constant 1, of a which is appropriate for incrementing and
1276 * decrementing values of the given GLSL type. For example, if type is vec4,
1277 * this creates a constant value of 1.0 having type float.
1279 * If the given type is invalid for increment and decrement operators, return
1280 * a floating point 1--the error will be detected later.
1283 constant_one_for_inc_dec(void *ctx
, const glsl_type
*type
)
1285 switch (type
->base_type
) {
1286 case GLSL_TYPE_UINT
:
1287 return new(ctx
) ir_constant((unsigned) 1);
1289 return new(ctx
) ir_constant(1);
1290 case GLSL_TYPE_UINT64
:
1291 return new(ctx
) ir_constant((uint64_t) 1);
1292 case GLSL_TYPE_INT64
:
1293 return new(ctx
) ir_constant((int64_t) 1);
1295 case GLSL_TYPE_FLOAT
:
1296 return new(ctx
) ir_constant(1.0f
);
1301 ast_expression::hir(exec_list
*instructions
,
1302 struct _mesa_glsl_parse_state
*state
)
1304 return do_hir(instructions
, state
, true);
1308 ast_expression::hir_no_rvalue(exec_list
*instructions
,
1309 struct _mesa_glsl_parse_state
*state
)
1311 do_hir(instructions
, state
, false);
1315 ast_expression::set_is_lhs(bool new_value
)
1317 /* is_lhs is tracked only to print "variable used uninitialized" warnings,
1318 * if we lack an identifier we can just skip it.
1320 if (this->primary_expression
.identifier
== NULL
)
1323 this->is_lhs
= new_value
;
1325 /* We need to go through the subexpressions tree to cover cases like
1326 * ast_field_selection
1328 if (this->subexpressions
[0] != NULL
)
1329 this->subexpressions
[0]->set_is_lhs(new_value
);
1333 ast_expression::do_hir(exec_list
*instructions
,
1334 struct _mesa_glsl_parse_state
*state
,
1338 static const int operations
[AST_NUM_OPERATORS
] = {
1339 -1, /* ast_assign doesn't convert to ir_expression. */
1340 -1, /* ast_plus doesn't convert to ir_expression. */
1350 ir_binop_less
, /* This is correct. See the ast_greater case below. */
1351 ir_binop_gequal
, /* This is correct. See the ast_lequal case below. */
1354 ir_binop_any_nequal
,
1364 /* Note: The following block of expression types actually convert
1365 * to multiple IR instructions.
1367 ir_binop_mul
, /* ast_mul_assign */
1368 ir_binop_div
, /* ast_div_assign */
1369 ir_binop_mod
, /* ast_mod_assign */
1370 ir_binop_add
, /* ast_add_assign */
1371 ir_binop_sub
, /* ast_sub_assign */
1372 ir_binop_lshift
, /* ast_ls_assign */
1373 ir_binop_rshift
, /* ast_rs_assign */
1374 ir_binop_bit_and
, /* ast_and_assign */
1375 ir_binop_bit_xor
, /* ast_xor_assign */
1376 ir_binop_bit_or
, /* ast_or_assign */
1378 -1, /* ast_conditional doesn't convert to ir_expression. */
1379 ir_binop_add
, /* ast_pre_inc. */
1380 ir_binop_sub
, /* ast_pre_dec. */
1381 ir_binop_add
, /* ast_post_inc. */
1382 ir_binop_sub
, /* ast_post_dec. */
1383 -1, /* ast_field_selection doesn't conv to ir_expression. */
1384 -1, /* ast_array_index doesn't convert to ir_expression. */
1385 -1, /* ast_function_call doesn't conv to ir_expression. */
1386 -1, /* ast_identifier doesn't convert to ir_expression. */
1387 -1, /* ast_int_constant doesn't convert to ir_expression. */
1388 -1, /* ast_uint_constant doesn't conv to ir_expression. */
1389 -1, /* ast_float_constant doesn't conv to ir_expression. */
1390 -1, /* ast_bool_constant doesn't conv to ir_expression. */
1391 -1, /* ast_sequence doesn't convert to ir_expression. */
1392 -1, /* ast_aggregate shouldn't ever even get here. */
1394 ir_rvalue
*result
= NULL
;
1396 const struct glsl_type
*type
, *orig_type
;
1397 bool error_emitted
= false;
1400 loc
= this->get_location();
1402 switch (this->oper
) {
1404 unreachable("ast_aggregate: Should never get here.");
1407 this->subexpressions
[0]->set_is_lhs(true);
1408 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1409 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1412 do_assignment(instructions
, state
,
1413 this->subexpressions
[0]->non_lvalue_description
,
1414 op
[0], op
[1], &result
, needs_rvalue
, false,
1415 this->subexpressions
[0]->get_location());
1420 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1422 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1424 error_emitted
= type
->is_error();
1430 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1432 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1434 error_emitted
= type
->is_error();
1436 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1444 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1445 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1447 type
= arithmetic_result_type(op
[0], op
[1],
1448 (this->oper
== ast_mul
),
1450 error_emitted
= type
->is_error();
1452 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1457 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1458 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1460 type
= modulus_result_type(op
[0], op
[1], state
, &loc
);
1462 assert(operations
[this->oper
] == ir_binop_mod
);
1464 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1466 error_emitted
= type
->is_error();
1471 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1472 error_emitted
= true;
1475 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1476 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1477 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1479 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1481 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1488 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1489 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1491 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
1493 /* The relational operators must either generate an error or result
1494 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1496 assert(type
->is_error()
1497 || (type
->is_boolean() && type
->is_scalar()));
1499 /* Like NIR, GLSL IR does not have opcodes for > or <=. Instead, swap
1500 * the arguments and use < or >=.
1502 if (this->oper
== ast_greater
|| this->oper
== ast_lequal
) {
1503 ir_rvalue
*const tmp
= op
[0];
1508 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1510 error_emitted
= type
->is_error();
1515 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1516 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1518 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1520 * "The equality operators equal (==), and not equal (!=)
1521 * operate on all types. They result in a scalar Boolean. If
1522 * the operand types do not match, then there must be a
1523 * conversion from Section 4.1.10 "Implicit Conversions"
1524 * applied to one operand that can make them match, in which
1525 * case this conversion is done."
1528 if (op
[0]->type
== glsl_type::void_type
|| op
[1]->type
== glsl_type::void_type
) {
1529 _mesa_glsl_error(& loc
, state
, "`%s': wrong operand types: "
1530 "no operation `%1$s' exists that takes a left-hand "
1531 "operand of type 'void' or a right operand of type "
1532 "'void'", (this->oper
== ast_equal
) ? "==" : "!=");
1533 error_emitted
= true;
1534 } else if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
1535 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
1536 || (op
[0]->type
!= op
[1]->type
)) {
1537 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
1538 "type", (this->oper
== ast_equal
) ? "==" : "!=");
1539 error_emitted
= true;
1540 } else if ((op
[0]->type
->is_array() || op
[1]->type
->is_array()) &&
1541 !state
->check_version(120, 300, &loc
,
1542 "array comparisons forbidden")) {
1543 error_emitted
= true;
1544 } else if ((op
[0]->type
->contains_subroutine() ||
1545 op
[1]->type
->contains_subroutine())) {
1546 _mesa_glsl_error(&loc
, state
, "subroutine comparisons forbidden");
1547 error_emitted
= true;
1548 } else if ((op
[0]->type
->contains_opaque() ||
1549 op
[1]->type
->contains_opaque())) {
1550 _mesa_glsl_error(&loc
, state
, "opaque type comparisons forbidden");
1551 error_emitted
= true;
1554 if (error_emitted
) {
1555 result
= new(ctx
) ir_constant(false);
1557 result
= do_comparison(ctx
, operations
[this->oper
], op
[0], op
[1]);
1558 assert(result
->type
== glsl_type::bool_type
);
1565 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1566 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1567 type
= bit_logic_result_type(op
[0], op
[1], this->oper
, state
, &loc
);
1568 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1570 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1574 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1576 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1577 error_emitted
= true;
1580 if (!op
[0]->type
->is_integer_32_64()) {
1581 _mesa_glsl_error(&loc
, state
, "operand of `~' must be an integer");
1582 error_emitted
= true;
1585 type
= error_emitted
? glsl_type::error_type
: op
[0]->type
;
1586 result
= new(ctx
) ir_expression(ir_unop_bit_not
, type
, op
[0], NULL
);
1589 case ast_logic_and
: {
1590 exec_list rhs_instructions
;
1591 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1592 "LHS", &error_emitted
);
1593 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1594 "RHS", &error_emitted
);
1596 if (rhs_instructions
.is_empty()) {
1597 result
= new(ctx
) ir_expression(ir_binop_logic_and
, op
[0], op
[1]);
1599 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1602 instructions
->push_tail(tmp
);
1604 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1605 instructions
->push_tail(stmt
);
1607 stmt
->then_instructions
.append_list(&rhs_instructions
);
1608 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1609 ir_assignment
*const then_assign
=
1610 new(ctx
) ir_assignment(then_deref
, op
[1]);
1611 stmt
->then_instructions
.push_tail(then_assign
);
1613 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1614 ir_assignment
*const else_assign
=
1615 new(ctx
) ir_assignment(else_deref
, new(ctx
) ir_constant(false));
1616 stmt
->else_instructions
.push_tail(else_assign
);
1618 result
= new(ctx
) ir_dereference_variable(tmp
);
1623 case ast_logic_or
: {
1624 exec_list rhs_instructions
;
1625 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1626 "LHS", &error_emitted
);
1627 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1628 "RHS", &error_emitted
);
1630 if (rhs_instructions
.is_empty()) {
1631 result
= new(ctx
) ir_expression(ir_binop_logic_or
, op
[0], op
[1]);
1633 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1636 instructions
->push_tail(tmp
);
1638 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1639 instructions
->push_tail(stmt
);
1641 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1642 ir_assignment
*const then_assign
=
1643 new(ctx
) ir_assignment(then_deref
, new(ctx
) ir_constant(true));
1644 stmt
->then_instructions
.push_tail(then_assign
);
1646 stmt
->else_instructions
.append_list(&rhs_instructions
);
1647 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1648 ir_assignment
*const else_assign
=
1649 new(ctx
) ir_assignment(else_deref
, op
[1]);
1650 stmt
->else_instructions
.push_tail(else_assign
);
1652 result
= new(ctx
) ir_dereference_variable(tmp
);
1658 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1660 * "The logical binary operators and (&&), or ( | | ), and
1661 * exclusive or (^^). They operate only on two Boolean
1662 * expressions and result in a Boolean expression."
1664 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0, "LHS",
1666 op
[1] = get_scalar_boolean_operand(instructions
, state
, this, 1, "RHS",
1669 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1674 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1675 "operand", &error_emitted
);
1677 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1681 case ast_mul_assign
:
1682 case ast_div_assign
:
1683 case ast_add_assign
:
1684 case ast_sub_assign
: {
1685 this->subexpressions
[0]->set_is_lhs(true);
1686 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1687 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1689 orig_type
= op
[0]->type
;
1691 /* Break out if operand types were not parsed successfully. */
1692 if ((op
[0]->type
== glsl_type::error_type
||
1693 op
[1]->type
== glsl_type::error_type
)) {
1694 error_emitted
= true;
1698 type
= arithmetic_result_type(op
[0], op
[1],
1699 (this->oper
== ast_mul_assign
),
1702 if (type
!= orig_type
) {
1703 _mesa_glsl_error(& loc
, state
,
1704 "could not implicitly convert "
1705 "%s to %s", type
->name
, orig_type
->name
);
1706 type
= glsl_type::error_type
;
1709 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1713 do_assignment(instructions
, state
,
1714 this->subexpressions
[0]->non_lvalue_description
,
1715 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1716 &result
, needs_rvalue
, false,
1717 this->subexpressions
[0]->get_location());
1719 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1720 * explicitly test for this because none of the binary expression
1721 * operators allow array operands either.
1727 case ast_mod_assign
: {
1728 this->subexpressions
[0]->set_is_lhs(true);
1729 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1730 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1732 orig_type
= op
[0]->type
;
1733 type
= modulus_result_type(op
[0], op
[1], state
, &loc
);
1735 if (type
!= orig_type
) {
1736 _mesa_glsl_error(& loc
, state
,
1737 "could not implicitly convert "
1738 "%s to %s", type
->name
, orig_type
->name
);
1739 type
= glsl_type::error_type
;
1742 assert(operations
[this->oper
] == ir_binop_mod
);
1744 ir_rvalue
*temp_rhs
;
1745 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1749 do_assignment(instructions
, state
,
1750 this->subexpressions
[0]->non_lvalue_description
,
1751 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1752 &result
, needs_rvalue
, false,
1753 this->subexpressions
[0]->get_location());
1758 case ast_rs_assign
: {
1759 this->subexpressions
[0]->set_is_lhs(true);
1760 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1761 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1762 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1764 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1765 type
, op
[0], op
[1]);
1767 do_assignment(instructions
, state
,
1768 this->subexpressions
[0]->non_lvalue_description
,
1769 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1770 &result
, needs_rvalue
, false,
1771 this->subexpressions
[0]->get_location());
1775 case ast_and_assign
:
1776 case ast_xor_assign
:
1777 case ast_or_assign
: {
1778 this->subexpressions
[0]->set_is_lhs(true);
1779 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1780 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1782 orig_type
= op
[0]->type
;
1783 type
= bit_logic_result_type(op
[0], op
[1], this->oper
, state
, &loc
);
1785 if (type
!= orig_type
) {
1786 _mesa_glsl_error(& loc
, state
,
1787 "could not implicitly convert "
1788 "%s to %s", type
->name
, orig_type
->name
);
1789 type
= glsl_type::error_type
;
1792 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1793 type
, op
[0], op
[1]);
1795 do_assignment(instructions
, state
,
1796 this->subexpressions
[0]->non_lvalue_description
,
1797 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1798 &result
, needs_rvalue
, false,
1799 this->subexpressions
[0]->get_location());
1803 case ast_conditional
: {
1804 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1806 * "The ternary selection operator (?:). It operates on three
1807 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1808 * first expression, which must result in a scalar Boolean."
1810 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1811 "condition", &error_emitted
);
1813 /* The :? operator is implemented by generating an anonymous temporary
1814 * followed by an if-statement. The last instruction in each branch of
1815 * the if-statement assigns a value to the anonymous temporary. This
1816 * temporary is the r-value of the expression.
1818 exec_list then_instructions
;
1819 exec_list else_instructions
;
1821 op
[1] = this->subexpressions
[1]->hir(&then_instructions
, state
);
1822 op
[2] = this->subexpressions
[2]->hir(&else_instructions
, state
);
1824 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1826 * "The second and third expressions can be any type, as
1827 * long their types match, or there is a conversion in
1828 * Section 4.1.10 "Implicit Conversions" that can be applied
1829 * to one of the expressions to make their types match. This
1830 * resulting matching type is the type of the entire
1833 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1834 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1835 || (op
[1]->type
!= op
[2]->type
)) {
1836 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1838 _mesa_glsl_error(& loc
, state
, "second and third operands of ?: "
1839 "operator must have matching types");
1840 error_emitted
= true;
1841 type
= glsl_type::error_type
;
1846 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1848 * "The second and third expressions must be the same type, but can
1849 * be of any type other than an array."
1851 if (type
->is_array() &&
1852 !state
->check_version(120, 300, &loc
,
1853 "second and third operands of ?: operator "
1854 "cannot be arrays")) {
1855 error_emitted
= true;
1858 /* From section 4.1.7 of the GLSL 4.50 spec (Opaque Types):
1860 * "Except for array indexing, structure member selection, and
1861 * parentheses, opaque variables are not allowed to be operands in
1862 * expressions; such use results in a compile-time error."
1864 if (type
->contains_opaque()) {
1865 if (!(state
->has_bindless() && (type
->is_image() || type
->is_sampler()))) {
1866 _mesa_glsl_error(&loc
, state
, "variables of type %s cannot be "
1867 "operands of the ?: operator", type
->name
);
1868 error_emitted
= true;
1872 ir_constant
*cond_val
= op
[0]->constant_expression_value(ctx
);
1874 if (then_instructions
.is_empty()
1875 && else_instructions
.is_empty()
1876 && cond_val
!= NULL
) {
1877 result
= cond_val
->value
.b
[0] ? op
[1] : op
[2];
1879 /* The copy to conditional_tmp reads the whole array. */
1880 if (type
->is_array()) {
1881 mark_whole_array_access(op
[1]);
1882 mark_whole_array_access(op
[2]);
1885 ir_variable
*const tmp
=
1886 new(ctx
) ir_variable(type
, "conditional_tmp", ir_var_temporary
);
1887 instructions
->push_tail(tmp
);
1889 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1890 instructions
->push_tail(stmt
);
1892 then_instructions
.move_nodes_to(& stmt
->then_instructions
);
1893 ir_dereference
*const then_deref
=
1894 new(ctx
) ir_dereference_variable(tmp
);
1895 ir_assignment
*const then_assign
=
1896 new(ctx
) ir_assignment(then_deref
, op
[1]);
1897 stmt
->then_instructions
.push_tail(then_assign
);
1899 else_instructions
.move_nodes_to(& stmt
->else_instructions
);
1900 ir_dereference
*const else_deref
=
1901 new(ctx
) ir_dereference_variable(tmp
);
1902 ir_assignment
*const else_assign
=
1903 new(ctx
) ir_assignment(else_deref
, op
[2]);
1904 stmt
->else_instructions
.push_tail(else_assign
);
1906 result
= new(ctx
) ir_dereference_variable(tmp
);
1913 this->non_lvalue_description
= (this->oper
== ast_pre_inc
)
1914 ? "pre-increment operation" : "pre-decrement operation";
1916 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1917 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1919 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1921 ir_rvalue
*temp_rhs
;
1922 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1926 do_assignment(instructions
, state
,
1927 this->subexpressions
[0]->non_lvalue_description
,
1928 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1929 &result
, needs_rvalue
, false,
1930 this->subexpressions
[0]->get_location());
1935 case ast_post_dec
: {
1936 this->non_lvalue_description
= (this->oper
== ast_post_inc
)
1937 ? "post-increment operation" : "post-decrement operation";
1938 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1939 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1941 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1943 if (error_emitted
) {
1944 result
= ir_rvalue::error_value(ctx
);
1948 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1950 ir_rvalue
*temp_rhs
;
1951 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1954 /* Get a temporary of a copy of the lvalue before it's modified.
1955 * This may get thrown away later.
1957 result
= get_lvalue_copy(instructions
, op
[0]->clone(ctx
, NULL
));
1959 ir_rvalue
*junk_rvalue
;
1961 do_assignment(instructions
, state
,
1962 this->subexpressions
[0]->non_lvalue_description
,
1963 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1964 &junk_rvalue
, false, false,
1965 this->subexpressions
[0]->get_location());
1970 case ast_field_selection
:
1971 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1974 case ast_array_index
: {
1975 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1977 /* Getting if an array is being used uninitialized is beyond what we get
1978 * from ir_value.data.assigned. Setting is_lhs as true would force to
1979 * not raise a uninitialized warning when using an array
1981 subexpressions
[0]->set_is_lhs(true);
1982 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1983 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1985 result
= _mesa_ast_array_index_to_hir(ctx
, state
, op
[0], op
[1],
1988 if (result
->type
->is_error())
1989 error_emitted
= true;
1994 case ast_unsized_array_dim
:
1995 unreachable("ast_unsized_array_dim: Should never get here.");
1997 case ast_function_call
:
1998 /* Should *NEVER* get here. ast_function_call should always be handled
1999 * by ast_function_expression::hir.
2001 unreachable("ast_function_call: handled elsewhere ");
2003 case ast_identifier
: {
2004 /* ast_identifier can appear several places in a full abstract syntax
2005 * tree. This particular use must be at location specified in the grammar
2006 * as 'variable_identifier'.
2009 state
->symbols
->get_variable(this->primary_expression
.identifier
);
2012 /* the identifier might be a subroutine name */
2014 sub_name
= ralloc_asprintf(ctx
, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state
->stage
), this->primary_expression
.identifier
);
2015 var
= state
->symbols
->get_variable(sub_name
);
2016 ralloc_free(sub_name
);
2020 var
->data
.used
= true;
2021 result
= new(ctx
) ir_dereference_variable(var
);
2023 if ((var
->data
.mode
== ir_var_auto
|| var
->data
.mode
== ir_var_shader_out
)
2025 && result
->variable_referenced()->data
.assigned
!= true
2026 && !is_gl_identifier(var
->name
)) {
2027 _mesa_glsl_warning(&loc
, state
, "`%s' used uninitialized",
2028 this->primary_expression
.identifier
);
2031 /* From the EXT_shader_framebuffer_fetch spec:
2033 * "Unless the GL_EXT_shader_framebuffer_fetch extension has been
2034 * enabled in addition, it's an error to use gl_LastFragData if it
2035 * hasn't been explicitly redeclared with layout(noncoherent)."
2037 if (var
->data
.fb_fetch_output
&& var
->data
.memory_coherent
&&
2038 !state
->EXT_shader_framebuffer_fetch_enable
) {
2039 _mesa_glsl_error(&loc
, state
,
2040 "invalid use of framebuffer fetch output not "
2041 "qualified with layout(noncoherent)");
2045 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
2046 this->primary_expression
.identifier
);
2048 result
= ir_rvalue::error_value(ctx
);
2049 error_emitted
= true;
2054 case ast_int_constant
:
2055 result
= new(ctx
) ir_constant(this->primary_expression
.int_constant
);
2058 case ast_uint_constant
:
2059 result
= new(ctx
) ir_constant(this->primary_expression
.uint_constant
);
2062 case ast_float_constant
:
2063 result
= new(ctx
) ir_constant(this->primary_expression
.float_constant
);
2066 case ast_bool_constant
:
2067 result
= new(ctx
) ir_constant(bool(this->primary_expression
.bool_constant
));
2070 case ast_double_constant
:
2071 result
= new(ctx
) ir_constant(this->primary_expression
.double_constant
);
2074 case ast_uint64_constant
:
2075 result
= new(ctx
) ir_constant(this->primary_expression
.uint64_constant
);
2078 case ast_int64_constant
:
2079 result
= new(ctx
) ir_constant(this->primary_expression
.int64_constant
);
2082 case ast_sequence
: {
2083 /* It should not be possible to generate a sequence in the AST without
2084 * any expressions in it.
2086 assert(!this->expressions
.is_empty());
2088 /* The r-value of a sequence is the last expression in the sequence. If
2089 * the other expressions in the sequence do not have side-effects (and
2090 * therefore add instructions to the instruction list), they get dropped
2093 exec_node
*previous_tail
= NULL
;
2094 YYLTYPE previous_operand_loc
= loc
;
2096 foreach_list_typed (ast_node
, ast
, link
, &this->expressions
) {
2097 /* If one of the operands of comma operator does not generate any
2098 * code, we want to emit a warning. At each pass through the loop
2099 * previous_tail will point to the last instruction in the stream
2100 * *before* processing the previous operand. Naturally,
2101 * instructions->get_tail_raw() will point to the last instruction in
2102 * the stream *after* processing the previous operand. If the two
2103 * pointers match, then the previous operand had no effect.
2105 * The warning behavior here differs slightly from GCC. GCC will
2106 * only emit a warning if none of the left-hand operands have an
2107 * effect. However, it will emit a warning for each. I believe that
2108 * there are some cases in C (especially with GCC extensions) where
2109 * it is useful to have an intermediate step in a sequence have no
2110 * effect, but I don't think these cases exist in GLSL. Either way,
2111 * it would be a giant hassle to replicate that behavior.
2113 if (previous_tail
== instructions
->get_tail_raw()) {
2114 _mesa_glsl_warning(&previous_operand_loc
, state
,
2115 "left-hand operand of comma expression has "
2119 /* The tail is directly accessed instead of using the get_tail()
2120 * method for performance reasons. get_tail() has extra code to
2121 * return NULL when the list is empty. We don't care about that
2122 * here, so using get_tail_raw() is fine.
2124 previous_tail
= instructions
->get_tail_raw();
2125 previous_operand_loc
= ast
->get_location();
2127 result
= ast
->hir(instructions
, state
);
2130 /* Any errors should have already been emitted in the loop above.
2132 error_emitted
= true;
2136 type
= NULL
; /* use result->type, not type. */
2137 assert(error_emitted
|| (result
!= NULL
|| !needs_rvalue
));
2139 if (result
&& result
->type
->is_error() && !error_emitted
)
2140 _mesa_glsl_error(& loc
, state
, "type mismatch");
2146 ast_expression::has_sequence_subexpression() const
2148 switch (this->oper
) {
2157 return this->subexpressions
[0]->has_sequence_subexpression();
2179 case ast_array_index
:
2180 case ast_mul_assign
:
2181 case ast_div_assign
:
2182 case ast_add_assign
:
2183 case ast_sub_assign
:
2184 case ast_mod_assign
:
2187 case ast_and_assign
:
2188 case ast_xor_assign
:
2190 return this->subexpressions
[0]->has_sequence_subexpression() ||
2191 this->subexpressions
[1]->has_sequence_subexpression();
2193 case ast_conditional
:
2194 return this->subexpressions
[0]->has_sequence_subexpression() ||
2195 this->subexpressions
[1]->has_sequence_subexpression() ||
2196 this->subexpressions
[2]->has_sequence_subexpression();
2201 case ast_field_selection
:
2202 case ast_identifier
:
2203 case ast_int_constant
:
2204 case ast_uint_constant
:
2205 case ast_float_constant
:
2206 case ast_bool_constant
:
2207 case ast_double_constant
:
2208 case ast_int64_constant
:
2209 case ast_uint64_constant
:
2215 case ast_function_call
:
2216 unreachable("should be handled by ast_function_expression::hir");
2218 case ast_unsized_array_dim
:
2219 unreachable("ast_unsized_array_dim: Should never get here.");
2226 ast_expression_statement::hir(exec_list
*instructions
,
2227 struct _mesa_glsl_parse_state
*state
)
2229 /* It is possible to have expression statements that don't have an
2230 * expression. This is the solitary semicolon:
2232 * for (i = 0; i < 5; i++)
2235 * In this case the expression will be NULL. Test for NULL and don't do
2236 * anything in that case.
2238 if (expression
!= NULL
)
2239 expression
->hir_no_rvalue(instructions
, state
);
2241 /* Statements do not have r-values.
2248 ast_compound_statement::hir(exec_list
*instructions
,
2249 struct _mesa_glsl_parse_state
*state
)
2252 state
->symbols
->push_scope();
2254 foreach_list_typed (ast_node
, ast
, link
, &this->statements
)
2255 ast
->hir(instructions
, state
);
2258 state
->symbols
->pop_scope();
2260 /* Compound statements do not have r-values.
2266 * Evaluate the given exec_node (which should be an ast_node representing
2267 * a single array dimension) and return its integer value.
2270 process_array_size(exec_node
*node
,
2271 struct _mesa_glsl_parse_state
*state
)
2273 void *mem_ctx
= state
;
2275 exec_list dummy_instructions
;
2277 ast_node
*array_size
= exec_node_data(ast_node
, node
, link
);
2280 * Dimensions other than the outermost dimension can by unsized if they
2281 * are immediately sized by a constructor or initializer.
2283 if (((ast_expression
*)array_size
)->oper
== ast_unsized_array_dim
)
2286 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
2287 YYLTYPE loc
= array_size
->get_location();
2290 _mesa_glsl_error(& loc
, state
,
2291 "array size could not be resolved");
2295 if (!ir
->type
->is_integer_32()) {
2296 _mesa_glsl_error(& loc
, state
,
2297 "array size must be integer type");
2301 if (!ir
->type
->is_scalar()) {
2302 _mesa_glsl_error(& loc
, state
,
2303 "array size must be scalar type");
2307 ir_constant
*const size
= ir
->constant_expression_value(mem_ctx
);
2309 (state
->is_version(120, 300) &&
2310 array_size
->has_sequence_subexpression())) {
2311 _mesa_glsl_error(& loc
, state
, "array size must be a "
2312 "constant valued expression");
2316 if (size
->value
.i
[0] <= 0) {
2317 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
2321 assert(size
->type
== ir
->type
);
2323 /* If the array size is const (and we've verified that
2324 * it is) then no instructions should have been emitted
2325 * when we converted it to HIR. If they were emitted,
2326 * then either the array size isn't const after all, or
2327 * we are emitting unnecessary instructions.
2329 assert(dummy_instructions
.is_empty());
2331 return size
->value
.u
[0];
2334 static const glsl_type
*
2335 process_array_type(YYLTYPE
*loc
, const glsl_type
*base
,
2336 ast_array_specifier
*array_specifier
,
2337 struct _mesa_glsl_parse_state
*state
)
2339 const glsl_type
*array_type
= base
;
2341 if (array_specifier
!= NULL
) {
2342 if (base
->is_array()) {
2344 /* From page 19 (page 25) of the GLSL 1.20 spec:
2346 * "Only one-dimensional arrays may be declared."
2348 if (!state
->check_arrays_of_arrays_allowed(loc
)) {
2349 return glsl_type::error_type
;
2353 for (exec_node
*node
= array_specifier
->array_dimensions
.get_tail_raw();
2354 !node
->is_head_sentinel(); node
= node
->prev
) {
2355 unsigned array_size
= process_array_size(node
, state
);
2356 array_type
= glsl_type::get_array_instance(array_type
, array_size
);
2364 precision_qualifier_allowed(const glsl_type
*type
)
2366 /* Precision qualifiers apply to floating point, integer and opaque
2369 * Section 4.5.2 (Precision Qualifiers) of the GLSL 1.30 spec says:
2370 * "Any floating point or any integer declaration can have the type
2371 * preceded by one of these precision qualifiers [...] Literal
2372 * constants do not have precision qualifiers. Neither do Boolean
2375 * Section 4.5 (Precision and Precision Qualifiers) of the GLSL 1.30
2378 * "Precision qualifiers are added for code portability with OpenGL
2379 * ES, not for functionality. They have the same syntax as in OpenGL
2382 * Section 8 (Built-In Functions) of the GLSL ES 1.00 spec says:
2384 * "uniform lowp sampler2D sampler;
2387 * lowp vec4 col = texture2D (sampler, coord);
2388 * // texture2D returns lowp"
2390 * From this, we infer that GLSL 1.30 (and later) should allow precision
2391 * qualifiers on sampler types just like float and integer types.
2393 const glsl_type
*const t
= type
->without_array();
2395 return (t
->is_float() || t
->is_integer_32() || t
->contains_opaque()) &&
2400 ast_type_specifier::glsl_type(const char **name
,
2401 struct _mesa_glsl_parse_state
*state
) const
2403 const struct glsl_type
*type
;
2405 if (this->type
!= NULL
)
2408 type
= structure
->type
;
2410 type
= state
->symbols
->get_type(this->type_name
);
2411 *name
= this->type_name
;
2413 YYLTYPE loc
= this->get_location();
2414 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
2420 * From the OpenGL ES 3.0 spec, 4.5.4 Default Precision Qualifiers:
2422 * "The precision statement
2424 * precision precision-qualifier type;
2426 * can be used to establish a default precision qualifier. The type field can
2427 * be either int or float or any of the sampler types, (...) If type is float,
2428 * the directive applies to non-precision-qualified floating point type
2429 * (scalar, vector, and matrix) declarations. If type is int, the directive
2430 * applies to all non-precision-qualified integer type (scalar, vector, signed,
2431 * and unsigned) declarations."
2433 * We use the symbol table to keep the values of the default precisions for
2434 * each 'type' in each scope and we use the 'type' string from the precision
2435 * statement as key in the symbol table. When we want to retrieve the default
2436 * precision associated with a given glsl_type we need to know the type string
2437 * associated with it. This is what this function returns.
2440 get_type_name_for_precision_qualifier(const glsl_type
*type
)
2442 switch (type
->base_type
) {
2443 case GLSL_TYPE_FLOAT
:
2445 case GLSL_TYPE_UINT
:
2448 case GLSL_TYPE_ATOMIC_UINT
:
2449 return "atomic_uint";
2450 case GLSL_TYPE_IMAGE
:
2452 case GLSL_TYPE_SAMPLER
: {
2453 const unsigned type_idx
=
2454 type
->sampler_array
+ 2 * type
->sampler_shadow
;
2455 const unsigned offset
= type
->is_sampler() ? 0 : 4;
2456 assert(type_idx
< 4);
2457 switch (type
->sampled_type
) {
2458 case GLSL_TYPE_FLOAT
:
2459 switch (type
->sampler_dimensionality
) {
2460 case GLSL_SAMPLER_DIM_1D
: {
2461 assert(type
->is_sampler());
2462 static const char *const names
[4] = {
2463 "sampler1D", "sampler1DArray",
2464 "sampler1DShadow", "sampler1DArrayShadow"
2466 return names
[type_idx
];
2468 case GLSL_SAMPLER_DIM_2D
: {
2469 static const char *const names
[8] = {
2470 "sampler2D", "sampler2DArray",
2471 "sampler2DShadow", "sampler2DArrayShadow",
2472 "image2D", "image2DArray", NULL
, NULL
2474 return names
[offset
+ type_idx
];
2476 case GLSL_SAMPLER_DIM_3D
: {
2477 static const char *const names
[8] = {
2478 "sampler3D", NULL
, NULL
, NULL
,
2479 "image3D", NULL
, NULL
, NULL
2481 return names
[offset
+ type_idx
];
2483 case GLSL_SAMPLER_DIM_CUBE
: {
2484 static const char *const names
[8] = {
2485 "samplerCube", "samplerCubeArray",
2486 "samplerCubeShadow", "samplerCubeArrayShadow",
2487 "imageCube", NULL
, NULL
, NULL
2489 return names
[offset
+ type_idx
];
2491 case GLSL_SAMPLER_DIM_MS
: {
2492 assert(type
->is_sampler());
2493 static const char *const names
[4] = {
2494 "sampler2DMS", "sampler2DMSArray", NULL
, NULL
2496 return names
[type_idx
];
2498 case GLSL_SAMPLER_DIM_RECT
: {
2499 assert(type
->is_sampler());
2500 static const char *const names
[4] = {
2501 "samplerRect", NULL
, "samplerRectShadow", NULL
2503 return names
[type_idx
];
2505 case GLSL_SAMPLER_DIM_BUF
: {
2506 static const char *const names
[8] = {
2507 "samplerBuffer", NULL
, NULL
, NULL
,
2508 "imageBuffer", NULL
, NULL
, NULL
2510 return names
[offset
+ type_idx
];
2512 case GLSL_SAMPLER_DIM_EXTERNAL
: {
2513 assert(type
->is_sampler());
2514 static const char *const names
[4] = {
2515 "samplerExternalOES", NULL
, NULL
, NULL
2517 return names
[type_idx
];
2520 unreachable("Unsupported sampler/image dimensionality");
2521 } /* sampler/image float dimensionality */
2524 switch (type
->sampler_dimensionality
) {
2525 case GLSL_SAMPLER_DIM_1D
: {
2526 assert(type
->is_sampler());
2527 static const char *const names
[4] = {
2528 "isampler1D", "isampler1DArray", NULL
, NULL
2530 return names
[type_idx
];
2532 case GLSL_SAMPLER_DIM_2D
: {
2533 static const char *const names
[8] = {
2534 "isampler2D", "isampler2DArray", NULL
, NULL
,
2535 "iimage2D", "iimage2DArray", NULL
, NULL
2537 return names
[offset
+ type_idx
];
2539 case GLSL_SAMPLER_DIM_3D
: {
2540 static const char *const names
[8] = {
2541 "isampler3D", NULL
, NULL
, NULL
,
2542 "iimage3D", NULL
, NULL
, NULL
2544 return names
[offset
+ type_idx
];
2546 case GLSL_SAMPLER_DIM_CUBE
: {
2547 static const char *const names
[8] = {
2548 "isamplerCube", "isamplerCubeArray", NULL
, NULL
,
2549 "iimageCube", NULL
, NULL
, NULL
2551 return names
[offset
+ type_idx
];
2553 case GLSL_SAMPLER_DIM_MS
: {
2554 assert(type
->is_sampler());
2555 static const char *const names
[4] = {
2556 "isampler2DMS", "isampler2DMSArray", NULL
, NULL
2558 return names
[type_idx
];
2560 case GLSL_SAMPLER_DIM_RECT
: {
2561 assert(type
->is_sampler());
2562 static const char *const names
[4] = {
2563 "isamplerRect", NULL
, "isamplerRectShadow", NULL
2565 return names
[type_idx
];
2567 case GLSL_SAMPLER_DIM_BUF
: {
2568 static const char *const names
[8] = {
2569 "isamplerBuffer", NULL
, NULL
, NULL
,
2570 "iimageBuffer", NULL
, NULL
, NULL
2572 return names
[offset
+ type_idx
];
2575 unreachable("Unsupported isampler/iimage dimensionality");
2576 } /* sampler/image int dimensionality */
2578 case GLSL_TYPE_UINT
:
2579 switch (type
->sampler_dimensionality
) {
2580 case GLSL_SAMPLER_DIM_1D
: {
2581 assert(type
->is_sampler());
2582 static const char *const names
[4] = {
2583 "usampler1D", "usampler1DArray", NULL
, NULL
2585 return names
[type_idx
];
2587 case GLSL_SAMPLER_DIM_2D
: {
2588 static const char *const names
[8] = {
2589 "usampler2D", "usampler2DArray", NULL
, NULL
,
2590 "uimage2D", "uimage2DArray", NULL
, NULL
2592 return names
[offset
+ type_idx
];
2594 case GLSL_SAMPLER_DIM_3D
: {
2595 static const char *const names
[8] = {
2596 "usampler3D", NULL
, NULL
, NULL
,
2597 "uimage3D", NULL
, NULL
, NULL
2599 return names
[offset
+ type_idx
];
2601 case GLSL_SAMPLER_DIM_CUBE
: {
2602 static const char *const names
[8] = {
2603 "usamplerCube", "usamplerCubeArray", NULL
, NULL
,
2604 "uimageCube", NULL
, NULL
, NULL
2606 return names
[offset
+ type_idx
];
2608 case GLSL_SAMPLER_DIM_MS
: {
2609 assert(type
->is_sampler());
2610 static const char *const names
[4] = {
2611 "usampler2DMS", "usampler2DMSArray", NULL
, NULL
2613 return names
[type_idx
];
2615 case GLSL_SAMPLER_DIM_RECT
: {
2616 assert(type
->is_sampler());
2617 static const char *const names
[4] = {
2618 "usamplerRect", NULL
, "usamplerRectShadow", NULL
2620 return names
[type_idx
];
2622 case GLSL_SAMPLER_DIM_BUF
: {
2623 static const char *const names
[8] = {
2624 "usamplerBuffer", NULL
, NULL
, NULL
,
2625 "uimageBuffer", NULL
, NULL
, NULL
2627 return names
[offset
+ type_idx
];
2630 unreachable("Unsupported usampler/uimage dimensionality");
2631 } /* sampler/image uint dimensionality */
2634 unreachable("Unsupported sampler/image type");
2635 } /* sampler/image type */
2637 } /* GLSL_TYPE_SAMPLER/GLSL_TYPE_IMAGE */
2640 unreachable("Unsupported type");
2645 select_gles_precision(unsigned qual_precision
,
2646 const glsl_type
*type
,
2647 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
2649 /* Precision qualifiers do not have any meaning in Desktop GLSL.
2650 * In GLES we take the precision from the type qualifier if present,
2651 * otherwise, if the type of the variable allows precision qualifiers at
2652 * all, we look for the default precision qualifier for that type in the
2655 assert(state
->es_shader
);
2657 unsigned precision
= GLSL_PRECISION_NONE
;
2658 if (qual_precision
) {
2659 precision
= qual_precision
;
2660 } else if (precision_qualifier_allowed(type
)) {
2661 const char *type_name
=
2662 get_type_name_for_precision_qualifier(type
->without_array());
2663 assert(type_name
!= NULL
);
2666 state
->symbols
->get_default_precision_qualifier(type_name
);
2667 if (precision
== ast_precision_none
) {
2668 _mesa_glsl_error(loc
, state
,
2669 "No precision specified in this scope for type `%s'",
2675 /* Section 4.1.7.3 (Atomic Counters) of the GLSL ES 3.10 spec says:
2677 * "The default precision of all atomic types is highp. It is an error to
2678 * declare an atomic type with a different precision or to specify the
2679 * default precision for an atomic type to be lowp or mediump."
2681 if (type
->is_atomic_uint() && precision
!= ast_precision_high
) {
2682 _mesa_glsl_error(loc
, state
,
2683 "atomic_uint can only have highp precision qualifier");
2690 ast_fully_specified_type::glsl_type(const char **name
,
2691 struct _mesa_glsl_parse_state
*state
) const
2693 return this->specifier
->glsl_type(name
, state
);
2697 * Determine whether a toplevel variable declaration declares a varying. This
2698 * function operates by examining the variable's mode and the shader target,
2699 * so it correctly identifies linkage variables regardless of whether they are
2700 * declared using the deprecated "varying" syntax or the new "in/out" syntax.
2702 * Passing a non-toplevel variable declaration (e.g. a function parameter) to
2703 * this function will produce undefined results.
2706 is_varying_var(ir_variable
*var
, gl_shader_stage target
)
2709 case MESA_SHADER_VERTEX
:
2710 return var
->data
.mode
== ir_var_shader_out
;
2711 case MESA_SHADER_FRAGMENT
:
2712 return var
->data
.mode
== ir_var_shader_in
||
2713 (var
->data
.mode
== ir_var_system_value
&&
2714 var
->data
.location
== SYSTEM_VALUE_FRAG_COORD
);
2716 return var
->data
.mode
== ir_var_shader_out
|| var
->data
.mode
== ir_var_shader_in
;
2721 is_allowed_invariant(ir_variable
*var
, struct _mesa_glsl_parse_state
*state
)
2723 if (is_varying_var(var
, state
->stage
))
2726 /* From Section 4.6.1 ("The Invariant Qualifier") GLSL 1.20 spec:
2727 * "Only variables output from a vertex shader can be candidates
2730 if (!state
->is_version(130, 100))
2734 * Later specs remove this language - so allowed invariant
2735 * on fragment shader outputs as well.
2737 if (state
->stage
== MESA_SHADER_FRAGMENT
&&
2738 var
->data
.mode
== ir_var_shader_out
)
2744 * Matrix layout qualifiers are only allowed on certain types
2747 validate_matrix_layout_for_type(struct _mesa_glsl_parse_state
*state
,
2749 const glsl_type
*type
,
2752 if (var
&& !var
->is_in_buffer_block()) {
2753 /* Layout qualifiers may only apply to interface blocks and fields in
2756 _mesa_glsl_error(loc
, state
,
2757 "uniform block layout qualifiers row_major and "
2758 "column_major may not be applied to variables "
2759 "outside of uniform blocks");
2760 } else if (!type
->without_array()->is_matrix()) {
2761 /* The OpenGL ES 3.0 conformance tests did not originally allow
2762 * matrix layout qualifiers on non-matrices. However, the OpenGL
2763 * 4.4 and OpenGL ES 3.0 (revision TBD) specifications were
2764 * amended to specifically allow these layouts on all types. Emit
2765 * a warning so that people know their code may not be portable.
2767 _mesa_glsl_warning(loc
, state
,
2768 "uniform block layout qualifiers row_major and "
2769 "column_major applied to non-matrix types may "
2770 "be rejected by older compilers");
2775 validate_xfb_buffer_qualifier(YYLTYPE
*loc
,
2776 struct _mesa_glsl_parse_state
*state
,
2777 unsigned xfb_buffer
) {
2778 if (xfb_buffer
>= state
->Const
.MaxTransformFeedbackBuffers
) {
2779 _mesa_glsl_error(loc
, state
,
2780 "invalid xfb_buffer specified %d is larger than "
2781 "MAX_TRANSFORM_FEEDBACK_BUFFERS - 1 (%d).",
2783 state
->Const
.MaxTransformFeedbackBuffers
- 1);
2790 /* From the ARB_enhanced_layouts spec:
2792 * "Variables and block members qualified with *xfb_offset* can be
2793 * scalars, vectors, matrices, structures, and (sized) arrays of these.
2794 * The offset must be a multiple of the size of the first component of
2795 * the first qualified variable or block member, or a compile-time error
2796 * results. Further, if applied to an aggregate containing a double,
2797 * the offset must also be a multiple of 8, and the space taken in the
2798 * buffer will be a multiple of 8.
2801 validate_xfb_offset_qualifier(YYLTYPE
*loc
,
2802 struct _mesa_glsl_parse_state
*state
,
2803 int xfb_offset
, const glsl_type
*type
,
2804 unsigned component_size
) {
2805 const glsl_type
*t_without_array
= type
->without_array();
2807 if (xfb_offset
!= -1 && type
->is_unsized_array()) {
2808 _mesa_glsl_error(loc
, state
,
2809 "xfb_offset can't be used with unsized arrays.");
2813 /* Make sure nested structs don't contain unsized arrays, and validate
2814 * any xfb_offsets on interface members.
2816 if (t_without_array
->is_struct() || t_without_array
->is_interface())
2817 for (unsigned int i
= 0; i
< t_without_array
->length
; i
++) {
2818 const glsl_type
*member_t
= t_without_array
->fields
.structure
[i
].type
;
2820 /* When the interface block doesn't have an xfb_offset qualifier then
2821 * we apply the component size rules at the member level.
2823 if (xfb_offset
== -1)
2824 component_size
= member_t
->contains_double() ? 8 : 4;
2826 int xfb_offset
= t_without_array
->fields
.structure
[i
].offset
;
2827 validate_xfb_offset_qualifier(loc
, state
, xfb_offset
, member_t
,
2831 /* Nested structs or interface block without offset may not have had an
2832 * offset applied yet so return.
2834 if (xfb_offset
== -1) {
2838 if (xfb_offset
% component_size
) {
2839 _mesa_glsl_error(loc
, state
,
2840 "invalid qualifier xfb_offset=%d must be a multiple "
2841 "of the first component size of the first qualified "
2842 "variable or block member. Or double if an aggregate "
2843 "that contains a double (%d).",
2844 xfb_offset
, component_size
);
2852 validate_stream_qualifier(YYLTYPE
*loc
, struct _mesa_glsl_parse_state
*state
,
2855 if (stream
>= state
->ctx
->Const
.MaxVertexStreams
) {
2856 _mesa_glsl_error(loc
, state
,
2857 "invalid stream specified %d is larger than "
2858 "MAX_VERTEX_STREAMS - 1 (%d).",
2859 stream
, state
->ctx
->Const
.MaxVertexStreams
- 1);
2867 apply_explicit_binding(struct _mesa_glsl_parse_state
*state
,
2870 const glsl_type
*type
,
2871 const ast_type_qualifier
*qual
)
2873 if (!qual
->flags
.q
.uniform
&& !qual
->flags
.q
.buffer
) {
2874 _mesa_glsl_error(loc
, state
,
2875 "the \"binding\" qualifier only applies to uniforms and "
2876 "shader storage buffer objects");
2880 unsigned qual_binding
;
2881 if (!process_qualifier_constant(state
, loc
, "binding", qual
->binding
,
2886 const struct gl_context
*const ctx
= state
->ctx
;
2887 unsigned elements
= type
->is_array() ? type
->arrays_of_arrays_size() : 1;
2888 unsigned max_index
= qual_binding
+ elements
- 1;
2889 const glsl_type
*base_type
= type
->without_array();
2891 if (base_type
->is_interface()) {
2892 /* UBOs. From page 60 of the GLSL 4.20 specification:
2893 * "If the binding point for any uniform block instance is less than zero,
2894 * or greater than or equal to the implementation-dependent maximum
2895 * number of uniform buffer bindings, a compilation error will occur.
2896 * When the binding identifier is used with a uniform block instanced as
2897 * an array of size N, all elements of the array from binding through
2898 * binding + N – 1 must be within this range."
2900 * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
2902 if (qual
->flags
.q
.uniform
&&
2903 max_index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2904 _mesa_glsl_error(loc
, state
, "layout(binding = %u) for %d UBOs exceeds "
2905 "the maximum number of UBO binding points (%d)",
2906 qual_binding
, elements
,
2907 ctx
->Const
.MaxUniformBufferBindings
);
2911 /* SSBOs. From page 67 of the GLSL 4.30 specification:
2912 * "If the binding point for any uniform or shader storage block instance
2913 * is less than zero, or greater than or equal to the
2914 * implementation-dependent maximum number of uniform buffer bindings, a
2915 * compile-time error will occur. When the binding identifier is used
2916 * with a uniform or shader storage block instanced as an array of size
2917 * N, all elements of the array from binding through binding + N – 1 must
2918 * be within this range."
2920 if (qual
->flags
.q
.buffer
&&
2921 max_index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2922 _mesa_glsl_error(loc
, state
, "layout(binding = %u) for %d SSBOs exceeds "
2923 "the maximum number of SSBO binding points (%d)",
2924 qual_binding
, elements
,
2925 ctx
->Const
.MaxShaderStorageBufferBindings
);
2928 } else if (base_type
->is_sampler()) {
2929 /* Samplers. From page 63 of the GLSL 4.20 specification:
2930 * "If the binding is less than zero, or greater than or equal to the
2931 * implementation-dependent maximum supported number of units, a
2932 * compilation error will occur. When the binding identifier is used
2933 * with an array of size N, all elements of the array from binding
2934 * through binding + N - 1 must be within this range."
2936 unsigned limit
= ctx
->Const
.MaxCombinedTextureImageUnits
;
2938 if (max_index
>= limit
) {
2939 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d samplers "
2940 "exceeds the maximum number of texture image units "
2941 "(%u)", qual_binding
, elements
, limit
);
2945 } else if (base_type
->contains_atomic()) {
2946 assert(ctx
->Const
.MaxAtomicBufferBindings
<= MAX_COMBINED_ATOMIC_BUFFERS
);
2947 if (qual_binding
>= ctx
->Const
.MaxAtomicBufferBindings
) {
2948 _mesa_glsl_error(loc
, state
, "layout(binding = %d) exceeds the "
2949 "maximum number of atomic counter buffer bindings "
2950 "(%u)", qual_binding
,
2951 ctx
->Const
.MaxAtomicBufferBindings
);
2955 } else if ((state
->is_version(420, 310) ||
2956 state
->ARB_shading_language_420pack_enable
) &&
2957 base_type
->is_image()) {
2958 assert(ctx
->Const
.MaxImageUnits
<= MAX_IMAGE_UNITS
);
2959 if (max_index
>= ctx
->Const
.MaxImageUnits
) {
2960 _mesa_glsl_error(loc
, state
, "Image binding %d exceeds the "
2961 "maximum number of image units (%d)", max_index
,
2962 ctx
->Const
.MaxImageUnits
);
2967 _mesa_glsl_error(loc
, state
,
2968 "the \"binding\" qualifier only applies to uniform "
2969 "blocks, storage blocks, opaque variables, or arrays "
2974 var
->data
.explicit_binding
= true;
2975 var
->data
.binding
= qual_binding
;
2981 validate_fragment_flat_interpolation_input(struct _mesa_glsl_parse_state
*state
,
2983 const glsl_interp_mode interpolation
,
2984 const struct glsl_type
*var_type
,
2985 ir_variable_mode mode
)
2987 if (state
->stage
!= MESA_SHADER_FRAGMENT
||
2988 interpolation
== INTERP_MODE_FLAT
||
2989 mode
!= ir_var_shader_in
)
2992 /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
2993 * so must integer vertex outputs.
2995 * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
2996 * "Fragment shader inputs that are signed or unsigned integers or
2997 * integer vectors must be qualified with the interpolation qualifier
3000 * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
3001 * "Fragment shader inputs that are, or contain, signed or unsigned
3002 * integers or integer vectors must be qualified with the
3003 * interpolation qualifier flat."
3005 * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
3006 * "Vertex shader outputs that are, or contain, signed or unsigned
3007 * integers or integer vectors must be qualified with the
3008 * interpolation qualifier flat."
3010 * Note that prior to GLSL 1.50, this requirement applied to vertex
3011 * outputs rather than fragment inputs. That creates problems in the
3012 * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
3013 * desktop GL shaders. For GLSL ES shaders, we follow the spec and
3014 * apply the restriction to both vertex outputs and fragment inputs.
3016 * Note also that the desktop GLSL specs are missing the text "or
3017 * contain"; this is presumably an oversight, since there is no
3018 * reasonable way to interpolate a fragment shader input that contains
3019 * an integer. See Khronos bug #15671.
3021 if ((state
->is_version(130, 300) || state
->EXT_gpu_shader4_enable
)
3022 && var_type
->contains_integer()) {
3023 _mesa_glsl_error(loc
, state
, "if a fragment input is (or contains) "
3024 "an integer, then it must be qualified with 'flat'");
3027 /* Double fragment inputs must be qualified with 'flat'.
3029 * From the "Overview" of the ARB_gpu_shader_fp64 extension spec:
3030 * "This extension does not support interpolation of double-precision
3031 * values; doubles used as fragment shader inputs must be qualified as
3034 * From section 4.3.4 ("Inputs") of the GLSL 4.00 spec:
3035 * "Fragment shader inputs that are signed or unsigned integers, integer
3036 * vectors, or any double-precision floating-point type must be
3037 * qualified with the interpolation qualifier flat."
3039 * Note that the GLSL specs are missing the text "or contain"; this is
3040 * presumably an oversight. See Khronos bug #15671.
3042 * The 'double' type does not exist in GLSL ES so far.
3044 if (state
->has_double()
3045 && var_type
->contains_double()) {
3046 _mesa_glsl_error(loc
, state
, "if a fragment input is (or contains) "
3047 "a double, then it must be qualified with 'flat'");
3050 /* Bindless sampler/image fragment inputs must be qualified with 'flat'.
3052 * From section 4.3.4 of the ARB_bindless_texture spec:
3054 * "(modify last paragraph, p. 35, allowing samplers and images as
3055 * fragment shader inputs) ... Fragment inputs can only be signed and
3056 * unsigned integers and integer vectors, floating point scalars,
3057 * floating-point vectors, matrices, sampler and image types, or arrays
3058 * or structures of these. Fragment shader inputs that are signed or
3059 * unsigned integers, integer vectors, or any double-precision floating-
3060 * point type, or any sampler or image type must be qualified with the
3061 * interpolation qualifier "flat"."
3063 if (state
->has_bindless()
3064 && (var_type
->contains_sampler() || var_type
->contains_image())) {
3065 _mesa_glsl_error(loc
, state
, "if a fragment input is (or contains) "
3066 "a bindless sampler (or image), then it must be "
3067 "qualified with 'flat'");
3072 validate_interpolation_qualifier(struct _mesa_glsl_parse_state
*state
,
3074 const glsl_interp_mode interpolation
,
3075 const struct ast_type_qualifier
*qual
,
3076 const struct glsl_type
*var_type
,
3077 ir_variable_mode mode
)
3079 /* Interpolation qualifiers can only apply to shader inputs or outputs, but
3080 * not to vertex shader inputs nor fragment shader outputs.
3082 * From section 4.3 ("Storage Qualifiers") of the GLSL 1.30 spec:
3083 * "Outputs from a vertex shader (out) and inputs to a fragment
3084 * shader (in) can be further qualified with one or more of these
3085 * interpolation qualifiers"
3087 * "These interpolation qualifiers may only precede the qualifiers in,
3088 * centroid in, out, or centroid out in a declaration. They do not apply
3089 * to the deprecated storage qualifiers varying or centroid
3090 * varying. They also do not apply to inputs into a vertex shader or
3091 * outputs from a fragment shader."
3093 * From section 4.3 ("Storage Qualifiers") of the GLSL ES 3.00 spec:
3094 * "Outputs from a shader (out) and inputs to a shader (in) can be
3095 * further qualified with one of these interpolation qualifiers."
3097 * "These interpolation qualifiers may only precede the qualifiers
3098 * in, centroid in, out, or centroid out in a declaration. They do
3099 * not apply to inputs into a vertex shader or outputs from a
3102 if ((state
->is_version(130, 300) || state
->EXT_gpu_shader4_enable
)
3103 && interpolation
!= INTERP_MODE_NONE
) {
3104 const char *i
= interpolation_string(interpolation
);
3105 if (mode
!= ir_var_shader_in
&& mode
!= ir_var_shader_out
)
3106 _mesa_glsl_error(loc
, state
,
3107 "interpolation qualifier `%s' can only be applied to "
3108 "shader inputs or outputs.", i
);
3110 switch (state
->stage
) {
3111 case MESA_SHADER_VERTEX
:
3112 if (mode
== ir_var_shader_in
) {
3113 _mesa_glsl_error(loc
, state
,
3114 "interpolation qualifier '%s' cannot be applied to "
3115 "vertex shader inputs", i
);
3118 case MESA_SHADER_FRAGMENT
:
3119 if (mode
== ir_var_shader_out
) {
3120 _mesa_glsl_error(loc
, state
,
3121 "interpolation qualifier '%s' cannot be applied to "
3122 "fragment shader outputs", i
);
3130 /* Interpolation qualifiers cannot be applied to 'centroid' and
3131 * 'centroid varying'.
3133 * From section 4.3 ("Storage Qualifiers") of the GLSL 1.30 spec:
3134 * "interpolation qualifiers may only precede the qualifiers in,
3135 * centroid in, out, or centroid out in a declaration. They do not apply
3136 * to the deprecated storage qualifiers varying or centroid varying."
3138 * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
3140 * GL_EXT_gpu_shader4 allows this.
3142 if (state
->is_version(130, 0) && !state
->EXT_gpu_shader4_enable
3143 && interpolation
!= INTERP_MODE_NONE
3144 && qual
->flags
.q
.varying
) {
3146 const char *i
= interpolation_string(interpolation
);
3148 if (qual
->flags
.q
.centroid
)
3149 s
= "centroid varying";
3153 _mesa_glsl_error(loc
, state
,
3154 "qualifier '%s' cannot be applied to the "
3155 "deprecated storage qualifier '%s'", i
, s
);
3158 validate_fragment_flat_interpolation_input(state
, loc
, interpolation
,
3162 static glsl_interp_mode
3163 interpret_interpolation_qualifier(const struct ast_type_qualifier
*qual
,
3164 const struct glsl_type
*var_type
,
3165 ir_variable_mode mode
,
3166 struct _mesa_glsl_parse_state
*state
,
3169 glsl_interp_mode interpolation
;
3170 if (qual
->flags
.q
.flat
)
3171 interpolation
= INTERP_MODE_FLAT
;
3172 else if (qual
->flags
.q
.noperspective
)
3173 interpolation
= INTERP_MODE_NOPERSPECTIVE
;
3174 else if (qual
->flags
.q
.smooth
)
3175 interpolation
= INTERP_MODE_SMOOTH
;
3177 interpolation
= INTERP_MODE_NONE
;
3179 validate_interpolation_qualifier(state
, loc
,
3181 qual
, var_type
, mode
);
3183 return interpolation
;
3188 apply_explicit_location(const struct ast_type_qualifier
*qual
,
3190 struct _mesa_glsl_parse_state
*state
,
3195 unsigned qual_location
;
3196 if (!process_qualifier_constant(state
, loc
, "location", qual
->location
,
3201 /* Checks for GL_ARB_explicit_uniform_location. */
3202 if (qual
->flags
.q
.uniform
) {
3203 if (!state
->check_explicit_uniform_location_allowed(loc
, var
))
3206 const struct gl_context
*const ctx
= state
->ctx
;
3207 unsigned max_loc
= qual_location
+ var
->type
->uniform_locations() - 1;
3209 if (max_loc
>= ctx
->Const
.MaxUserAssignableUniformLocations
) {
3210 _mesa_glsl_error(loc
, state
, "location(s) consumed by uniform %s "
3211 ">= MAX_UNIFORM_LOCATIONS (%u)", var
->name
,
3212 ctx
->Const
.MaxUserAssignableUniformLocations
);
3216 var
->data
.explicit_location
= true;
3217 var
->data
.location
= qual_location
;
3221 /* Between GL_ARB_explicit_attrib_location an
3222 * GL_ARB_separate_shader_objects, the inputs and outputs of any shader
3223 * stage can be assigned explicit locations. The checking here associates
3224 * the correct extension with the correct stage's input / output:
3228 * vertex explicit_loc sso
3229 * tess control sso sso
3232 * fragment sso explicit_loc
3234 switch (state
->stage
) {
3235 case MESA_SHADER_VERTEX
:
3236 if (var
->data
.mode
== ir_var_shader_in
) {
3237 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
3243 if (var
->data
.mode
== ir_var_shader_out
) {
3244 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
3253 case MESA_SHADER_TESS_CTRL
:
3254 case MESA_SHADER_TESS_EVAL
:
3255 case MESA_SHADER_GEOMETRY
:
3256 if (var
->data
.mode
== ir_var_shader_in
|| var
->data
.mode
== ir_var_shader_out
) {
3257 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
3266 case MESA_SHADER_FRAGMENT
:
3267 if (var
->data
.mode
== ir_var_shader_in
) {
3268 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
3274 if (var
->data
.mode
== ir_var_shader_out
) {
3275 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
3284 case MESA_SHADER_COMPUTE
:
3285 _mesa_glsl_error(loc
, state
,
3286 "compute shader variables cannot be given "
3287 "explicit locations");
3295 _mesa_glsl_error(loc
, state
,
3296 "%s cannot be given an explicit location in %s shader",
3298 _mesa_shader_stage_to_string(state
->stage
));
3300 var
->data
.explicit_location
= true;
3302 switch (state
->stage
) {
3303 case MESA_SHADER_VERTEX
:
3304 var
->data
.location
= (var
->data
.mode
== ir_var_shader_in
)
3305 ? (qual_location
+ VERT_ATTRIB_GENERIC0
)
3306 : (qual_location
+ VARYING_SLOT_VAR0
);
3309 case MESA_SHADER_TESS_CTRL
:
3310 case MESA_SHADER_TESS_EVAL
:
3311 case MESA_SHADER_GEOMETRY
:
3312 if (var
->data
.patch
)
3313 var
->data
.location
= qual_location
+ VARYING_SLOT_PATCH0
;
3315 var
->data
.location
= qual_location
+ VARYING_SLOT_VAR0
;
3318 case MESA_SHADER_FRAGMENT
:
3319 var
->data
.location
= (var
->data
.mode
== ir_var_shader_out
)
3320 ? (qual_location
+ FRAG_RESULT_DATA0
)
3321 : (qual_location
+ VARYING_SLOT_VAR0
);
3324 assert(!"Unexpected shader type");
3328 /* Check if index was set for the uniform instead of the function */
3329 if (qual
->flags
.q
.explicit_index
&& qual
->is_subroutine_decl()) {
3330 _mesa_glsl_error(loc
, state
, "an index qualifier can only be "
3331 "used with subroutine functions");
3335 unsigned qual_index
;
3336 if (qual
->flags
.q
.explicit_index
&&
3337 process_qualifier_constant(state
, loc
, "index", qual
->index
,
3339 /* From the GLSL 4.30 specification, section 4.4.2 (Output
3340 * Layout Qualifiers):
3342 * "It is also a compile-time error if a fragment shader
3343 * sets a layout index to less than 0 or greater than 1."
3345 * Older specifications don't mandate a behavior; we take
3346 * this as a clarification and always generate the error.
3348 if (qual_index
> 1) {
3349 _mesa_glsl_error(loc
, state
,
3350 "explicit index may only be 0 or 1");
3352 var
->data
.explicit_index
= true;
3353 var
->data
.index
= qual_index
;
3360 validate_storage_for_sampler_image_types(ir_variable
*var
,
3361 struct _mesa_glsl_parse_state
*state
,
3364 /* From section 4.1.7 of the GLSL 4.40 spec:
3366 * "[Opaque types] can only be declared as function
3367 * parameters or uniform-qualified variables."
3369 * From section 4.1.7 of the ARB_bindless_texture spec:
3371 * "Samplers may be declared as shader inputs and outputs, as uniform
3372 * variables, as temporary variables, and as function parameters."
3374 * From section 4.1.X of the ARB_bindless_texture spec:
3376 * "Images may be declared as shader inputs and outputs, as uniform
3377 * variables, as temporary variables, and as function parameters."
3379 if (state
->has_bindless()) {
3380 if (var
->data
.mode
!= ir_var_auto
&&
3381 var
->data
.mode
!= ir_var_uniform
&&
3382 var
->data
.mode
!= ir_var_shader_in
&&
3383 var
->data
.mode
!= ir_var_shader_out
&&
3384 var
->data
.mode
!= ir_var_function_in
&&
3385 var
->data
.mode
!= ir_var_function_out
&&
3386 var
->data
.mode
!= ir_var_function_inout
) {
3387 _mesa_glsl_error(loc
, state
, "bindless image/sampler variables may "
3388 "only be declared as shader inputs and outputs, as "
3389 "uniform variables, as temporary variables and as "
3390 "function parameters");
3394 if (var
->data
.mode
!= ir_var_uniform
&&
3395 var
->data
.mode
!= ir_var_function_in
) {
3396 _mesa_glsl_error(loc
, state
, "image/sampler variables may only be "
3397 "declared as function parameters or "
3398 "uniform-qualified global variables");
3406 validate_memory_qualifier_for_type(struct _mesa_glsl_parse_state
*state
,
3408 const struct ast_type_qualifier
*qual
,
3409 const glsl_type
*type
)
3411 /* From Section 4.10 (Memory Qualifiers) of the GLSL 4.50 spec:
3413 * "Memory qualifiers are only supported in the declarations of image
3414 * variables, buffer variables, and shader storage blocks; it is an error
3415 * to use such qualifiers in any other declarations.
3417 if (!type
->is_image() && !qual
->flags
.q
.buffer
) {
3418 if (qual
->flags
.q
.read_only
||
3419 qual
->flags
.q
.write_only
||
3420 qual
->flags
.q
.coherent
||
3421 qual
->flags
.q
._volatile
||
3422 qual
->flags
.q
.restrict_flag
) {
3423 _mesa_glsl_error(loc
, state
, "memory qualifiers may only be applied "
3424 "in the declarations of image variables, buffer "
3425 "variables, and shader storage blocks");
3433 validate_image_format_qualifier_for_type(struct _mesa_glsl_parse_state
*state
,
3435 const struct ast_type_qualifier
*qual
,
3436 const glsl_type
*type
)
3438 /* From section 4.4.6.2 (Format Layout Qualifiers) of the GLSL 4.50 spec:
3440 * "Format layout qualifiers can be used on image variable declarations
3441 * (those declared with a basic type having “image ” in its keyword)."
3443 if (!type
->is_image() && qual
->flags
.q
.explicit_image_format
) {
3444 _mesa_glsl_error(loc
, state
, "format layout qualifiers may only be "
3445 "applied to images");
3452 apply_image_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
3454 struct _mesa_glsl_parse_state
*state
,
3457 const glsl_type
*base_type
= var
->type
->without_array();
3459 if (!validate_image_format_qualifier_for_type(state
, loc
, qual
, base_type
) ||
3460 !validate_memory_qualifier_for_type(state
, loc
, qual
, base_type
))
3463 if (!base_type
->is_image())
3466 if (!validate_storage_for_sampler_image_types(var
, state
, loc
))
3469 var
->data
.memory_read_only
|= qual
->flags
.q
.read_only
;
3470 var
->data
.memory_write_only
|= qual
->flags
.q
.write_only
;
3471 var
->data
.memory_coherent
|= qual
->flags
.q
.coherent
;
3472 var
->data
.memory_volatile
|= qual
->flags
.q
._volatile
;
3473 var
->data
.memory_restrict
|= qual
->flags
.q
.restrict_flag
;
3475 if (qual
->flags
.q
.explicit_image_format
) {
3476 if (var
->data
.mode
== ir_var_function_in
) {
3477 _mesa_glsl_error(loc
, state
, "format qualifiers cannot be used on "
3478 "image function parameters");
3481 if (qual
->image_base_type
!= base_type
->sampled_type
) {
3482 _mesa_glsl_error(loc
, state
, "format qualifier doesn't match the base "
3483 "data type of the image");
3486 var
->data
.image_format
= qual
->image_format
;
3487 } else if (state
->has_image_load_formatted()) {
3488 if (var
->data
.mode
== ir_var_uniform
&&
3489 state
->EXT_shader_image_load_formatted_warn
) {
3490 _mesa_glsl_warning(loc
, state
, "GL_EXT_image_load_formatted used");
3493 if (var
->data
.mode
== ir_var_uniform
) {
3494 if (state
->es_shader
||
3495 !(state
->is_version(420, 310) || state
->ARB_shader_image_load_store_enable
)) {
3496 _mesa_glsl_error(loc
, state
, "all image uniforms must have a "
3497 "format layout qualifier");
3498 } else if (!qual
->flags
.q
.write_only
) {
3499 _mesa_glsl_error(loc
, state
, "image uniforms not qualified with "
3500 "`writeonly' must have a format layout qualifier");
3503 var
->data
.image_format
= PIPE_FORMAT_NONE
;
3506 /* From page 70 of the GLSL ES 3.1 specification:
3508 * "Except for image variables qualified with the format qualifiers r32f,
3509 * r32i, and r32ui, image variables must specify either memory qualifier
3510 * readonly or the memory qualifier writeonly."
3512 if (state
->es_shader
&&
3513 var
->data
.image_format
!= PIPE_FORMAT_R32_FLOAT
&&
3514 var
->data
.image_format
!= PIPE_FORMAT_R32_SINT
&&
3515 var
->data
.image_format
!= PIPE_FORMAT_R32_UINT
&&
3516 !var
->data
.memory_read_only
&&
3517 !var
->data
.memory_write_only
) {
3518 _mesa_glsl_error(loc
, state
, "image variables of format other than r32f, "
3519 "r32i or r32ui must be qualified `readonly' or "
3524 static inline const char*
3525 get_layout_qualifier_string(bool origin_upper_left
, bool pixel_center_integer
)
3527 if (origin_upper_left
&& pixel_center_integer
)
3528 return "origin_upper_left, pixel_center_integer";
3529 else if (origin_upper_left
)
3530 return "origin_upper_left";
3531 else if (pixel_center_integer
)
3532 return "pixel_center_integer";
3538 is_conflicting_fragcoord_redeclaration(struct _mesa_glsl_parse_state
*state
,
3539 const struct ast_type_qualifier
*qual
)
3541 /* If gl_FragCoord was previously declared, and the qualifiers were
3542 * different in any way, return true.
3544 if (state
->fs_redeclares_gl_fragcoord
) {
3545 return (state
->fs_pixel_center_integer
!= qual
->flags
.q
.pixel_center_integer
3546 || state
->fs_origin_upper_left
!= qual
->flags
.q
.origin_upper_left
);
3553 is_conflicting_layer_redeclaration(struct _mesa_glsl_parse_state
*state
,
3554 const struct ast_type_qualifier
*qual
)
3556 if (state
->redeclares_gl_layer
) {
3557 return state
->layer_viewport_relative
!= qual
->flags
.q
.viewport_relative
;
3563 validate_array_dimensions(const glsl_type
*t
,
3564 struct _mesa_glsl_parse_state
*state
,
3566 if (t
->is_array()) {
3567 t
= t
->fields
.array
;
3568 while (t
->is_array()) {
3569 if (t
->is_unsized_array()) {
3570 _mesa_glsl_error(loc
, state
,
3571 "only the outermost array dimension can "
3576 t
= t
->fields
.array
;
3582 apply_bindless_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
3584 struct _mesa_glsl_parse_state
*state
,
3587 bool has_local_qualifiers
= qual
->flags
.q
.bindless_sampler
||
3588 qual
->flags
.q
.bindless_image
||
3589 qual
->flags
.q
.bound_sampler
||
3590 qual
->flags
.q
.bound_image
;
3592 /* The ARB_bindless_texture spec says:
3594 * "Modify Section 4.4.6 Opaque-Uniform Layout Qualifiers of the GLSL 4.30
3597 * "If these layout qualifiers are applied to other types of default block
3598 * uniforms, or variables with non-uniform storage, a compile-time error
3599 * will be generated."
3601 if (has_local_qualifiers
&& !qual
->flags
.q
.uniform
) {
3602 _mesa_glsl_error(loc
, state
, "ARB_bindless_texture layout qualifiers "
3603 "can only be applied to default block uniforms or "
3604 "variables with uniform storage");
3608 /* The ARB_bindless_texture spec doesn't state anything in this situation,
3609 * but it makes sense to only allow bindless_sampler/bound_sampler for
3610 * sampler types, and respectively bindless_image/bound_image for image
3613 if ((qual
->flags
.q
.bindless_sampler
|| qual
->flags
.q
.bound_sampler
) &&
3614 !var
->type
->contains_sampler()) {
3615 _mesa_glsl_error(loc
, state
, "bindless_sampler or bound_sampler can only "
3616 "be applied to sampler types");
3620 if ((qual
->flags
.q
.bindless_image
|| qual
->flags
.q
.bound_image
) &&
3621 !var
->type
->contains_image()) {
3622 _mesa_glsl_error(loc
, state
, "bindless_image or bound_image can only be "
3623 "applied to image types");
3627 /* The bindless_sampler/bindless_image (and respectively
3628 * bound_sampler/bound_image) layout qualifiers can be set at global and at
3631 if (var
->type
->contains_sampler() || var
->type
->contains_image()) {
3632 var
->data
.bindless
= qual
->flags
.q
.bindless_sampler
||
3633 qual
->flags
.q
.bindless_image
||
3634 state
->bindless_sampler_specified
||
3635 state
->bindless_image_specified
;
3637 var
->data
.bound
= qual
->flags
.q
.bound_sampler
||
3638 qual
->flags
.q
.bound_image
||
3639 state
->bound_sampler_specified
||
3640 state
->bound_image_specified
;
3645 apply_layout_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
3647 struct _mesa_glsl_parse_state
*state
,
3650 if (var
->name
!= NULL
&& strcmp(var
->name
, "gl_FragCoord") == 0) {
3652 /* Section 4.3.8.1, page 39 of GLSL 1.50 spec says:
3654 * "Within any shader, the first redeclarations of gl_FragCoord
3655 * must appear before any use of gl_FragCoord."
3657 * Generate a compiler error if above condition is not met by the
3660 ir_variable
*earlier
= state
->symbols
->get_variable("gl_FragCoord");
3661 if (earlier
!= NULL
&&
3662 earlier
->data
.used
&&
3663 !state
->fs_redeclares_gl_fragcoord
) {
3664 _mesa_glsl_error(loc
, state
,
3665 "gl_FragCoord used before its first redeclaration "
3666 "in fragment shader");
3669 /* Make sure all gl_FragCoord redeclarations specify the same layout
3672 if (is_conflicting_fragcoord_redeclaration(state
, qual
)) {
3673 const char *const qual_string
=
3674 get_layout_qualifier_string(qual
->flags
.q
.origin_upper_left
,
3675 qual
->flags
.q
.pixel_center_integer
);
3677 const char *const state_string
=
3678 get_layout_qualifier_string(state
->fs_origin_upper_left
,
3679 state
->fs_pixel_center_integer
);
3681 _mesa_glsl_error(loc
, state
,
3682 "gl_FragCoord redeclared with different layout "
3683 "qualifiers (%s) and (%s) ",
3687 state
->fs_origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
3688 state
->fs_pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
3689 state
->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers
=
3690 !qual
->flags
.q
.origin_upper_left
&& !qual
->flags
.q
.pixel_center_integer
;
3691 state
->fs_redeclares_gl_fragcoord
=
3692 state
->fs_origin_upper_left
||
3693 state
->fs_pixel_center_integer
||
3694 state
->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers
;
3697 if ((qual
->flags
.q
.origin_upper_left
|| qual
->flags
.q
.pixel_center_integer
)
3698 && (strcmp(var
->name
, "gl_FragCoord") != 0)) {
3699 const char *const qual_string
= (qual
->flags
.q
.origin_upper_left
)
3700 ? "origin_upper_left" : "pixel_center_integer";
3702 _mesa_glsl_error(loc
, state
,
3703 "layout qualifier `%s' can only be applied to "
3704 "fragment shader input `gl_FragCoord'",
3708 if (qual
->flags
.q
.explicit_location
) {
3709 apply_explicit_location(qual
, var
, state
, loc
);
3711 if (qual
->flags
.q
.explicit_component
) {
3712 unsigned qual_component
;
3713 if (process_qualifier_constant(state
, loc
, "component",
3714 qual
->component
, &qual_component
)) {
3715 const glsl_type
*type
= var
->type
->without_array();
3716 unsigned components
= type
->component_slots();
3718 if (type
->is_matrix() || type
->is_struct()) {
3719 _mesa_glsl_error(loc
, state
, "component layout qualifier "
3720 "cannot be applied to a matrix, a structure, "
3721 "a block, or an array containing any of "
3723 } else if (components
> 4 && type
->is_64bit()) {
3724 _mesa_glsl_error(loc
, state
, "component layout qualifier "
3725 "cannot be applied to dvec%u.",
3727 } else if (qual_component
!= 0 &&
3728 (qual_component
+ components
- 1) > 3) {
3729 _mesa_glsl_error(loc
, state
, "component overflow (%u > 3)",
3730 (qual_component
+ components
- 1));
3731 } else if (qual_component
== 1 && type
->is_64bit()) {
3732 /* We don't bother checking for 3 as it should be caught by the
3733 * overflow check above.
3735 _mesa_glsl_error(loc
, state
, "doubles cannot begin at "
3736 "component 1 or 3");
3738 var
->data
.explicit_component
= true;
3739 var
->data
.location_frac
= qual_component
;
3743 } else if (qual
->flags
.q
.explicit_index
) {
3744 if (!qual
->subroutine_list
)
3745 _mesa_glsl_error(loc
, state
,
3746 "explicit index requires explicit location");
3747 } else if (qual
->flags
.q
.explicit_component
) {
3748 _mesa_glsl_error(loc
, state
,
3749 "explicit component requires explicit location");
3752 if (qual
->flags
.q
.explicit_binding
) {
3753 apply_explicit_binding(state
, loc
, var
, var
->type
, qual
);
3756 if (state
->stage
== MESA_SHADER_GEOMETRY
&&
3757 qual
->flags
.q
.out
&& qual
->flags
.q
.stream
) {
3758 unsigned qual_stream
;
3759 if (process_qualifier_constant(state
, loc
, "stream", qual
->stream
,
3761 validate_stream_qualifier(loc
, state
, qual_stream
)) {
3762 var
->data
.stream
= qual_stream
;
3766 if (qual
->flags
.q
.out
&& qual
->flags
.q
.xfb_buffer
) {
3767 unsigned qual_xfb_buffer
;
3768 if (process_qualifier_constant(state
, loc
, "xfb_buffer",
3769 qual
->xfb_buffer
, &qual_xfb_buffer
) &&
3770 validate_xfb_buffer_qualifier(loc
, state
, qual_xfb_buffer
)) {
3771 var
->data
.xfb_buffer
= qual_xfb_buffer
;
3772 if (qual
->flags
.q
.explicit_xfb_buffer
)
3773 var
->data
.explicit_xfb_buffer
= true;
3777 if (qual
->flags
.q
.explicit_xfb_offset
) {
3778 unsigned qual_xfb_offset
;
3779 unsigned component_size
= var
->type
->contains_double() ? 8 : 4;
3781 if (process_qualifier_constant(state
, loc
, "xfb_offset",
3782 qual
->offset
, &qual_xfb_offset
) &&
3783 validate_xfb_offset_qualifier(loc
, state
, (int) qual_xfb_offset
,
3784 var
->type
, component_size
)) {
3785 var
->data
.offset
= qual_xfb_offset
;
3786 var
->data
.explicit_xfb_offset
= true;
3790 if (qual
->flags
.q
.explicit_xfb_stride
) {
3791 unsigned qual_xfb_stride
;
3792 if (process_qualifier_constant(state
, loc
, "xfb_stride",
3793 qual
->xfb_stride
, &qual_xfb_stride
)) {
3794 var
->data
.xfb_stride
= qual_xfb_stride
;
3795 var
->data
.explicit_xfb_stride
= true;
3799 if (var
->type
->contains_atomic()) {
3800 if (var
->data
.mode
== ir_var_uniform
) {
3801 if (var
->data
.explicit_binding
) {
3803 &state
->atomic_counter_offsets
[var
->data
.binding
];
3805 if (*offset
% ATOMIC_COUNTER_SIZE
)
3806 _mesa_glsl_error(loc
, state
,
3807 "misaligned atomic counter offset");
3809 var
->data
.offset
= *offset
;
3810 *offset
+= var
->type
->atomic_size();
3813 _mesa_glsl_error(loc
, state
,
3814 "atomic counters require explicit binding point");
3816 } else if (var
->data
.mode
!= ir_var_function_in
) {
3817 _mesa_glsl_error(loc
, state
, "atomic counters may only be declared as "
3818 "function parameters or uniform-qualified "
3819 "global variables");
3823 if (var
->type
->contains_sampler() &&
3824 !validate_storage_for_sampler_image_types(var
, state
, loc
))
3827 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
3828 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
3829 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
3830 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
3831 * These extensions and all following extensions that add the 'layout'
3832 * keyword have been modified to require the use of 'in' or 'out'.
3834 * The following extension do not allow the deprecated keywords:
3836 * GL_AMD_conservative_depth
3837 * GL_ARB_conservative_depth
3838 * GL_ARB_gpu_shader5
3839 * GL_ARB_separate_shader_objects
3840 * GL_ARB_tessellation_shader
3841 * GL_ARB_transform_feedback3
3842 * GL_ARB_uniform_buffer_object
3844 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
3845 * allow layout with the deprecated keywords.
3847 const bool relaxed_layout_qualifier_checking
=
3848 state
->ARB_fragment_coord_conventions_enable
;
3850 const bool uses_deprecated_qualifier
= qual
->flags
.q
.attribute
3851 || qual
->flags
.q
.varying
;
3852 if (qual
->has_layout() && uses_deprecated_qualifier
) {
3853 if (relaxed_layout_qualifier_checking
) {
3854 _mesa_glsl_warning(loc
, state
,
3855 "`layout' qualifier may not be used with "
3856 "`attribute' or `varying'");
3858 _mesa_glsl_error(loc
, state
,
3859 "`layout' qualifier may not be used with "
3860 "`attribute' or `varying'");
3864 /* Layout qualifiers for gl_FragDepth, which are enabled by extension
3865 * AMD_conservative_depth.
3867 if (qual
->flags
.q
.depth_type
3868 && !state
->is_version(420, 0)
3869 && !state
->AMD_conservative_depth_enable
3870 && !state
->ARB_conservative_depth_enable
) {
3871 _mesa_glsl_error(loc
, state
,
3872 "extension GL_AMD_conservative_depth or "
3873 "GL_ARB_conservative_depth must be enabled "
3874 "to use depth layout qualifiers");
3875 } else if (qual
->flags
.q
.depth_type
3876 && strcmp(var
->name
, "gl_FragDepth") != 0) {
3877 _mesa_glsl_error(loc
, state
,
3878 "depth layout qualifiers can be applied only to "
3882 switch (qual
->depth_type
) {
3884 var
->data
.depth_layout
= ir_depth_layout_any
;
3886 case ast_depth_greater
:
3887 var
->data
.depth_layout
= ir_depth_layout_greater
;
3889 case ast_depth_less
:
3890 var
->data
.depth_layout
= ir_depth_layout_less
;
3892 case ast_depth_unchanged
:
3893 var
->data
.depth_layout
= ir_depth_layout_unchanged
;
3896 var
->data
.depth_layout
= ir_depth_layout_none
;
3900 if (qual
->flags
.q
.std140
||
3901 qual
->flags
.q
.std430
||
3902 qual
->flags
.q
.packed
||
3903 qual
->flags
.q
.shared
) {
3904 _mesa_glsl_error(loc
, state
,
3905 "uniform and shader storage block layout qualifiers "
3906 "std140, std430, packed, and shared can only be "
3907 "applied to uniform or shader storage blocks, not "
3911 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
3912 validate_matrix_layout_for_type(state
, loc
, var
->type
, var
);
3915 /* From section 4.4.1.3 of the GLSL 4.50 specification (Fragment Shader
3918 * "Fragment shaders also allow the following layout qualifier on in only
3919 * (not with variable declarations)
3920 * layout-qualifier-id
3921 * early_fragment_tests
3924 if (qual
->flags
.q
.early_fragment_tests
) {
3925 _mesa_glsl_error(loc
, state
, "early_fragment_tests layout qualifier only "
3926 "valid in fragment shader input layout declaration.");
3929 if (qual
->flags
.q
.inner_coverage
) {
3930 _mesa_glsl_error(loc
, state
, "inner_coverage layout qualifier only "
3931 "valid in fragment shader input layout declaration.");
3934 if (qual
->flags
.q
.post_depth_coverage
) {
3935 _mesa_glsl_error(loc
, state
, "post_depth_coverage layout qualifier only "
3936 "valid in fragment shader input layout declaration.");
3939 if (state
->has_bindless())
3940 apply_bindless_qualifier_to_variable(qual
, var
, state
, loc
);
3942 if (qual
->flags
.q
.pixel_interlock_ordered
||
3943 qual
->flags
.q
.pixel_interlock_unordered
||
3944 qual
->flags
.q
.sample_interlock_ordered
||
3945 qual
->flags
.q
.sample_interlock_unordered
) {
3946 _mesa_glsl_error(loc
, state
, "interlock layout qualifiers: "
3947 "pixel_interlock_ordered, pixel_interlock_unordered, "
3948 "sample_interlock_ordered and sample_interlock_unordered, "
3949 "only valid in fragment shader input layout declaration.");
3952 if (var
->name
!= NULL
&& strcmp(var
->name
, "gl_Layer") == 0) {
3953 if (is_conflicting_layer_redeclaration(state
, qual
)) {
3954 _mesa_glsl_error(loc
, state
, "gl_Layer redeclaration with "
3955 "different viewport_relative setting than earlier");
3957 state
->redeclares_gl_layer
= 1;
3958 if (qual
->flags
.q
.viewport_relative
) {
3959 state
->layer_viewport_relative
= 1;
3961 } else if (qual
->flags
.q
.viewport_relative
) {
3962 _mesa_glsl_error(loc
, state
,
3963 "viewport_relative qualifier "
3964 "can only be applied to gl_Layer.");
3969 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
3971 struct _mesa_glsl_parse_state
*state
,
3975 STATIC_ASSERT(sizeof(qual
->flags
.q
) <= sizeof(qual
->flags
.i
));
3977 if (qual
->flags
.q
.invariant
) {
3978 if (var
->data
.used
) {
3979 _mesa_glsl_error(loc
, state
,
3980 "variable `%s' may not be redeclared "
3981 "`invariant' after being used",
3984 var
->data
.explicit_invariant
= true;
3985 var
->data
.invariant
= true;
3989 if (qual
->flags
.q
.precise
) {
3990 if (var
->data
.used
) {
3991 _mesa_glsl_error(loc
, state
,
3992 "variable `%s' may not be redeclared "
3993 "`precise' after being used",
3996 var
->data
.precise
= 1;
4000 if (qual
->is_subroutine_decl() && !qual
->flags
.q
.uniform
) {
4001 _mesa_glsl_error(loc
, state
,
4002 "`subroutine' may only be applied to uniforms, "
4003 "subroutine type declarations, or function definitions");
4006 if (qual
->flags
.q
.constant
|| qual
->flags
.q
.attribute
4007 || qual
->flags
.q
.uniform
4008 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
4009 var
->data
.read_only
= 1;
4011 if (qual
->flags
.q
.centroid
)
4012 var
->data
.centroid
= 1;
4014 if (qual
->flags
.q
.sample
)
4015 var
->data
.sample
= 1;
4017 /* Precision qualifiers do not hold any meaning in Desktop GLSL */
4018 if (state
->es_shader
) {
4019 var
->data
.precision
=
4020 select_gles_precision(qual
->precision
, var
->type
, state
, loc
);
4023 if (qual
->flags
.q
.patch
)
4024 var
->data
.patch
= 1;
4026 if (qual
->flags
.q
.attribute
&& state
->stage
!= MESA_SHADER_VERTEX
) {
4027 var
->type
= glsl_type::error_type
;
4028 _mesa_glsl_error(loc
, state
,
4029 "`attribute' variables may not be declared in the "
4031 _mesa_shader_stage_to_string(state
->stage
));
4034 /* Disallow layout qualifiers which may only appear on layout declarations. */
4035 if (qual
->flags
.q
.prim_type
) {
4036 _mesa_glsl_error(loc
, state
,
4037 "Primitive type may only be specified on GS input or output "
4038 "layout declaration, not on variables.");
4041 /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
4043 * "However, the const qualifier cannot be used with out or inout."
4045 * The same section of the GLSL 4.40 spec further clarifies this saying:
4047 * "The const qualifier cannot be used with out or inout, or a
4048 * compile-time error results."
4050 if (is_parameter
&& qual
->flags
.q
.constant
&& qual
->flags
.q
.out
) {
4051 _mesa_glsl_error(loc
, state
,
4052 "`const' may not be applied to `out' or `inout' "
4053 "function parameters");
4056 /* If there is no qualifier that changes the mode of the variable, leave
4057 * the setting alone.
4059 assert(var
->data
.mode
!= ir_var_temporary
);
4060 if (qual
->flags
.q
.in
&& qual
->flags
.q
.out
)
4061 var
->data
.mode
= is_parameter
? ir_var_function_inout
: ir_var_shader_out
;
4062 else if (qual
->flags
.q
.in
)
4063 var
->data
.mode
= is_parameter
? ir_var_function_in
: ir_var_shader_in
;
4064 else if (qual
->flags
.q
.attribute
4065 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
4066 var
->data
.mode
= ir_var_shader_in
;
4067 else if (qual
->flags
.q
.out
)
4068 var
->data
.mode
= is_parameter
? ir_var_function_out
: ir_var_shader_out
;
4069 else if (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_VERTEX
))
4070 var
->data
.mode
= ir_var_shader_out
;
4071 else if (qual
->flags
.q
.uniform
)
4072 var
->data
.mode
= ir_var_uniform
;
4073 else if (qual
->flags
.q
.buffer
)
4074 var
->data
.mode
= ir_var_shader_storage
;
4075 else if (qual
->flags
.q
.shared_storage
)
4076 var
->data
.mode
= ir_var_shader_shared
;
4078 if (!is_parameter
&& state
->has_framebuffer_fetch() &&
4079 state
->stage
== MESA_SHADER_FRAGMENT
) {
4080 if (state
->is_version(130, 300))
4081 var
->data
.fb_fetch_output
= qual
->flags
.q
.in
&& qual
->flags
.q
.out
;
4083 var
->data
.fb_fetch_output
= (strcmp(var
->name
, "gl_LastFragData") == 0);
4086 if (var
->data
.fb_fetch_output
) {
4087 var
->data
.assigned
= true;
4088 var
->data
.memory_coherent
= !qual
->flags
.q
.non_coherent
;
4090 /* From the EXT_shader_framebuffer_fetch spec:
4092 * "It is an error to declare an inout fragment output not qualified
4093 * with layout(noncoherent) if the GL_EXT_shader_framebuffer_fetch
4094 * extension hasn't been enabled."
4096 if (var
->data
.memory_coherent
&&
4097 !state
->EXT_shader_framebuffer_fetch_enable
)
4098 _mesa_glsl_error(loc
, state
,
4099 "invalid declaration of framebuffer fetch output not "
4100 "qualified with layout(noncoherent)");
4103 /* From the EXT_shader_framebuffer_fetch spec:
4105 * "Fragment outputs declared inout may specify the following layout
4106 * qualifier: [...] noncoherent"
4108 if (qual
->flags
.q
.non_coherent
)
4109 _mesa_glsl_error(loc
, state
,
4110 "invalid layout(noncoherent) qualifier not part of "
4111 "framebuffer fetch output declaration");
4114 if (!is_parameter
&& is_varying_var(var
, state
->stage
)) {
4115 /* User-defined ins/outs are not permitted in compute shaders. */
4116 if (state
->stage
== MESA_SHADER_COMPUTE
) {
4117 _mesa_glsl_error(loc
, state
,
4118 "user-defined input and output variables are not "
4119 "permitted in compute shaders");
4122 /* This variable is being used to link data between shader stages (in
4123 * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
4124 * that is allowed for such purposes.
4126 * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
4128 * "The varying qualifier can be used only with the data types
4129 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
4132 * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
4133 * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
4135 * "Fragment inputs can only be signed and unsigned integers and
4136 * integer vectors, float, floating-point vectors, matrices, or
4137 * arrays of these. Structures cannot be input.
4139 * Similar text exists in the section on vertex shader outputs.
4141 * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
4142 * 3.00 spec allows structs as well. Varying structs are also allowed
4145 * From section 4.3.4 of the ARB_bindless_texture spec:
4147 * "(modify third paragraph of the section to allow sampler and image
4148 * types) ... Vertex shader inputs can only be float,
4149 * single-precision floating-point scalars, single-precision
4150 * floating-point vectors, matrices, signed and unsigned integers
4151 * and integer vectors, sampler and image types."
4153 * From section 4.3.6 of the ARB_bindless_texture spec:
4155 * "Output variables can only be floating-point scalars,
4156 * floating-point vectors, matrices, signed or unsigned integers or
4157 * integer vectors, sampler or image types, or arrays or structures
4160 switch (var
->type
->without_array()->base_type
) {
4161 case GLSL_TYPE_FLOAT
:
4162 /* Ok in all GLSL versions */
4164 case GLSL_TYPE_UINT
:
4166 if (state
->is_version(130, 300) || state
->EXT_gpu_shader4_enable
)
4168 _mesa_glsl_error(loc
, state
,
4169 "varying variables must be of base type float in %s",
4170 state
->get_version_string());
4172 case GLSL_TYPE_STRUCT
:
4173 if (state
->is_version(150, 300))
4175 _mesa_glsl_error(loc
, state
,
4176 "varying variables may not be of type struct");
4178 case GLSL_TYPE_DOUBLE
:
4179 case GLSL_TYPE_UINT64
:
4180 case GLSL_TYPE_INT64
:
4182 case GLSL_TYPE_SAMPLER
:
4183 case GLSL_TYPE_IMAGE
:
4184 if (state
->has_bindless())
4188 _mesa_glsl_error(loc
, state
, "illegal type for a varying variable");
4193 if (state
->all_invariant
&& var
->data
.mode
== ir_var_shader_out
) {
4194 var
->data
.explicit_invariant
= true;
4195 var
->data
.invariant
= true;
4198 var
->data
.interpolation
=
4199 interpret_interpolation_qualifier(qual
, var
->type
,
4200 (ir_variable_mode
) var
->data
.mode
,
4203 /* Does the declaration use the deprecated 'attribute' or 'varying'
4206 const bool uses_deprecated_qualifier
= qual
->flags
.q
.attribute
4207 || qual
->flags
.q
.varying
;
4210 /* Validate auxiliary storage qualifiers */
4212 /* From section 4.3.4 of the GLSL 1.30 spec:
4213 * "It is an error to use centroid in in a vertex shader."
4215 * From section 4.3.4 of the GLSL ES 3.00 spec:
4216 * "It is an error to use centroid in or interpolation qualifiers in
4217 * a vertex shader input."
4220 /* Section 4.3.6 of the GLSL 1.30 specification states:
4221 * "It is an error to use centroid out in a fragment shader."
4223 * The GL_ARB_shading_language_420pack extension specification states:
4224 * "It is an error to use auxiliary storage qualifiers or interpolation
4225 * qualifiers on an output in a fragment shader."
4227 if (qual
->flags
.q
.sample
&& (!is_varying_var(var
, state
->stage
) || uses_deprecated_qualifier
)) {
4228 _mesa_glsl_error(loc
, state
,
4229 "sample qualifier may only be used on `in` or `out` "
4230 "variables between shader stages");
4232 if (qual
->flags
.q
.centroid
&& !is_varying_var(var
, state
->stage
)) {
4233 _mesa_glsl_error(loc
, state
,
4234 "centroid qualifier may only be used with `in', "
4235 "`out' or `varying' variables between shader stages");
4238 if (qual
->flags
.q
.shared_storage
&& state
->stage
!= MESA_SHADER_COMPUTE
) {
4239 _mesa_glsl_error(loc
, state
,
4240 "the shared storage qualifiers can only be used with "
4244 apply_image_qualifier_to_variable(qual
, var
, state
, loc
);
4248 * Get the variable that is being redeclared by this declaration or if it
4249 * does not exist, the current declared variable.
4251 * Semantic checks to verify the validity of the redeclaration are also
4252 * performed. If semantic checks fail, compilation error will be emitted via
4253 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
4256 * A pointer to an existing variable in the current scope if the declaration
4257 * is a redeclaration, current variable otherwise. \c is_declared boolean
4258 * will return \c true if the declaration is a redeclaration, \c false
4261 static ir_variable
*
4262 get_variable_being_redeclared(ir_variable
**var_ptr
, YYLTYPE loc
,
4263 struct _mesa_glsl_parse_state
*state
,
4264 bool allow_all_redeclarations
,
4265 bool *is_redeclaration
)
4267 ir_variable
*var
= *var_ptr
;
4269 /* Check if this declaration is actually a re-declaration, either to
4270 * resize an array or add qualifiers to an existing variable.
4272 * This is allowed for variables in the current scope, or when at
4273 * global scope (for built-ins in the implicit outer scope).
4275 ir_variable
*earlier
= state
->symbols
->get_variable(var
->name
);
4276 if (earlier
== NULL
||
4277 (state
->current_function
!= NULL
&&
4278 !state
->symbols
->name_declared_this_scope(var
->name
))) {
4279 *is_redeclaration
= false;
4283 *is_redeclaration
= true;
4285 if (earlier
->data
.how_declared
== ir_var_declared_implicitly
) {
4286 /* Verify that the redeclaration of a built-in does not change the
4287 * storage qualifier. There are a couple special cases.
4289 * 1. Some built-in variables that are defined as 'in' in the
4290 * specification are implemented as system values. Allow
4291 * ir_var_system_value -> ir_var_shader_in.
4293 * 2. gl_LastFragData is implemented as a ir_var_shader_out, but the
4294 * specification requires that redeclarations omit any qualifier.
4295 * Allow ir_var_shader_out -> ir_var_auto for this one variable.
4297 if (earlier
->data
.mode
!= var
->data
.mode
&&
4298 !(earlier
->data
.mode
== ir_var_system_value
&&
4299 var
->data
.mode
== ir_var_shader_in
) &&
4300 !(strcmp(var
->name
, "gl_LastFragData") == 0 &&
4301 var
->data
.mode
== ir_var_auto
)) {
4302 _mesa_glsl_error(&loc
, state
,
4303 "redeclaration cannot change qualification of `%s'",
4308 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
4310 * "It is legal to declare an array without a size and then
4311 * later re-declare the same name as an array of the same
4312 * type and specify a size."
4314 if (earlier
->type
->is_unsized_array() && var
->type
->is_array()
4315 && (var
->type
->fields
.array
== earlier
->type
->fields
.array
)) {
4316 const int size
= var
->type
->array_size();
4317 check_builtin_array_max_size(var
->name
, size
, loc
, state
);
4318 if ((size
> 0) && (size
<= earlier
->data
.max_array_access
)) {
4319 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
4321 earlier
->data
.max_array_access
);
4324 earlier
->type
= var
->type
;
4328 } else if (earlier
->type
!= var
->type
) {
4329 _mesa_glsl_error(&loc
, state
,
4330 "redeclaration of `%s' has incorrect type",
4332 } else if ((state
->ARB_fragment_coord_conventions_enable
||
4333 state
->is_version(150, 0))
4334 && strcmp(var
->name
, "gl_FragCoord") == 0) {
4335 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
4338 * We don't really need to do anything here, just allow the
4339 * redeclaration. Any error on the gl_FragCoord is handled on the ast
4340 * level at apply_layout_qualifier_to_variable using the
4341 * ast_type_qualifier and _mesa_glsl_parse_state, or later at
4344 /* According to section 4.3.7 of the GLSL 1.30 spec,
4345 * the following built-in varaibles can be redeclared with an
4346 * interpolation qualifier:
4349 * * gl_FrontSecondaryColor
4350 * * gl_BackSecondaryColor
4352 * * gl_SecondaryColor
4354 } else if (state
->is_version(130, 0)
4355 && (strcmp(var
->name
, "gl_FrontColor") == 0
4356 || strcmp(var
->name
, "gl_BackColor") == 0
4357 || strcmp(var
->name
, "gl_FrontSecondaryColor") == 0
4358 || strcmp(var
->name
, "gl_BackSecondaryColor") == 0
4359 || strcmp(var
->name
, "gl_Color") == 0
4360 || strcmp(var
->name
, "gl_SecondaryColor") == 0)) {
4361 earlier
->data
.interpolation
= var
->data
.interpolation
;
4363 /* Layout qualifiers for gl_FragDepth. */
4364 } else if ((state
->is_version(420, 0) ||
4365 state
->AMD_conservative_depth_enable
||
4366 state
->ARB_conservative_depth_enable
)
4367 && strcmp(var
->name
, "gl_FragDepth") == 0) {
4369 /** From the AMD_conservative_depth spec:
4370 * Within any shader, the first redeclarations of gl_FragDepth
4371 * must appear before any use of gl_FragDepth.
4373 if (earlier
->data
.used
) {
4374 _mesa_glsl_error(&loc
, state
,
4375 "the first redeclaration of gl_FragDepth "
4376 "must appear before any use of gl_FragDepth");
4379 /* Prevent inconsistent redeclaration of depth layout qualifier. */
4380 if (earlier
->data
.depth_layout
!= ir_depth_layout_none
4381 && earlier
->data
.depth_layout
!= var
->data
.depth_layout
) {
4382 _mesa_glsl_error(&loc
, state
,
4383 "gl_FragDepth: depth layout is declared here "
4384 "as '%s, but it was previously declared as "
4386 depth_layout_string(var
->data
.depth_layout
),
4387 depth_layout_string(earlier
->data
.depth_layout
));
4390 earlier
->data
.depth_layout
= var
->data
.depth_layout
;
4392 } else if (state
->has_framebuffer_fetch() &&
4393 strcmp(var
->name
, "gl_LastFragData") == 0 &&
4394 var
->data
.mode
== ir_var_auto
) {
4395 /* According to the EXT_shader_framebuffer_fetch spec:
4397 * "By default, gl_LastFragData is declared with the mediump precision
4398 * qualifier. This can be changed by redeclaring the corresponding
4399 * variables with the desired precision qualifier."
4401 * "Fragment shaders may specify the following layout qualifier only for
4402 * redeclaring the built-in gl_LastFragData array [...]: noncoherent"
4404 earlier
->data
.precision
= var
->data
.precision
;
4405 earlier
->data
.memory_coherent
= var
->data
.memory_coherent
;
4407 } else if (state
->NV_viewport_array2_enable
&&
4408 strcmp(var
->name
, "gl_Layer") == 0 &&
4409 earlier
->data
.how_declared
== ir_var_declared_implicitly
) {
4410 /* No need to do anything, just allow it. Qualifier is stored in state */
4412 } else if ((earlier
->data
.how_declared
== ir_var_declared_implicitly
&&
4413 state
->allow_builtin_variable_redeclaration
) ||
4414 allow_all_redeclarations
) {
4415 /* Allow verbatim redeclarations of built-in variables. Not explicitly
4416 * valid, but some applications do it.
4419 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
4426 * Generate the IR for an initializer in a variable declaration
4429 process_initializer(ir_variable
*var
, ast_declaration
*decl
,
4430 ast_fully_specified_type
*type
,
4431 exec_list
*initializer_instructions
,
4432 struct _mesa_glsl_parse_state
*state
)
4434 void *mem_ctx
= state
;
4435 ir_rvalue
*result
= NULL
;
4437 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
4439 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
4441 * "All uniform variables are read-only and are initialized either
4442 * directly by an application via API commands, or indirectly by
4445 if (var
->data
.mode
== ir_var_uniform
) {
4446 state
->check_version(120, 0, &initializer_loc
,
4447 "cannot initialize uniform %s",
4451 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
4453 * "Buffer variables cannot have initializers."
4455 if (var
->data
.mode
== ir_var_shader_storage
) {
4456 _mesa_glsl_error(&initializer_loc
, state
,
4457 "cannot initialize buffer variable %s",
4461 /* From section 4.1.7 of the GLSL 4.40 spec:
4463 * "Opaque variables [...] are initialized only through the
4464 * OpenGL API; they cannot be declared with an initializer in a
4467 * From section 4.1.7 of the ARB_bindless_texture spec:
4469 * "Samplers may be declared as shader inputs and outputs, as uniform
4470 * variables, as temporary variables, and as function parameters."
4472 * From section 4.1.X of the ARB_bindless_texture spec:
4474 * "Images may be declared as shader inputs and outputs, as uniform
4475 * variables, as temporary variables, and as function parameters."
4477 if (var
->type
->contains_atomic() ||
4478 (!state
->has_bindless() && var
->type
->contains_opaque())) {
4479 _mesa_glsl_error(&initializer_loc
, state
,
4480 "cannot initialize %s variable %s",
4481 var
->name
, state
->has_bindless() ? "atomic" : "opaque");
4484 if ((var
->data
.mode
== ir_var_shader_in
) && (state
->current_function
== NULL
)) {
4485 _mesa_glsl_error(&initializer_loc
, state
,
4486 "cannot initialize %s shader input / %s %s",
4487 _mesa_shader_stage_to_string(state
->stage
),
4488 (state
->stage
== MESA_SHADER_VERTEX
)
4489 ? "attribute" : "varying",
4493 if (var
->data
.mode
== ir_var_shader_out
&& state
->current_function
== NULL
) {
4494 _mesa_glsl_error(&initializer_loc
, state
,
4495 "cannot initialize %s shader output %s",
4496 _mesa_shader_stage_to_string(state
->stage
),
4500 /* If the initializer is an ast_aggregate_initializer, recursively store
4501 * type information from the LHS into it, so that its hir() function can do
4504 if (decl
->initializer
->oper
== ast_aggregate
)
4505 _mesa_ast_set_aggregate_type(var
->type
, decl
->initializer
);
4507 ir_dereference
*const lhs
= new(state
) ir_dereference_variable(var
);
4508 ir_rvalue
*rhs
= decl
->initializer
->hir(initializer_instructions
, state
);
4510 /* Calculate the constant value if this is a const or uniform
4513 * Section 4.3 (Storage Qualifiers) of the GLSL ES 1.00.17 spec says:
4515 * "Declarations of globals without a storage qualifier, or with
4516 * just the const qualifier, may include initializers, in which case
4517 * they will be initialized before the first line of main() is
4518 * executed. Such initializers must be a constant expression."
4520 * The same section of the GLSL ES 3.00.4 spec has similar language.
4522 if (type
->qualifier
.flags
.q
.constant
4523 || type
->qualifier
.flags
.q
.uniform
4524 || (state
->es_shader
&& state
->current_function
== NULL
)) {
4525 ir_rvalue
*new_rhs
= validate_assignment(state
, initializer_loc
,
4527 if (new_rhs
!= NULL
) {
4530 /* Section 4.3.3 (Constant Expressions) of the GLSL ES 3.00.4 spec
4533 * "A constant expression is one of
4537 * - an expression formed by an operator on operands that are
4538 * all constant expressions, including getting an element of
4539 * a constant array, or a field of a constant structure, or
4540 * components of a constant vector. However, the sequence
4541 * operator ( , ) and the assignment operators ( =, +=, ...)
4542 * are not included in the operators that can create a
4543 * constant expression."
4545 * Section 12.43 (Sequence operator and constant expressions) says:
4547 * "Should the following construct be allowed?
4551 * The expression within the brackets uses the sequence operator
4552 * (',') and returns the integer 3 so the construct is declaring
4553 * a single-dimensional array of size 3. In some languages, the
4554 * construct declares a two-dimensional array. It would be
4555 * preferable to make this construct illegal to avoid confusion.
4557 * One possibility is to change the definition of the sequence
4558 * operator so that it does not return a constant-expression and
4559 * hence cannot be used to declare an array size.
4561 * RESOLUTION: The result of a sequence operator is not a
4562 * constant-expression."
4564 * Section 4.3.3 (Constant Expressions) of the GLSL 4.30.9 spec
4565 * contains language almost identical to the section 4.3.3 in the
4566 * GLSL ES 3.00.4 spec. This is a new limitation for these GLSL
4569 ir_constant
*constant_value
=
4570 rhs
->constant_expression_value(mem_ctx
);
4572 if (!constant_value
||
4573 (state
->is_version(430, 300) &&
4574 decl
->initializer
->has_sequence_subexpression())) {
4575 const char *const variable_mode
=
4576 (type
->qualifier
.flags
.q
.constant
)
4578 : ((type
->qualifier
.flags
.q
.uniform
) ? "uniform" : "global");
4580 /* If ARB_shading_language_420pack is enabled, initializers of
4581 * const-qualified local variables do not have to be constant
4582 * expressions. Const-qualified global variables must still be
4583 * initialized with constant expressions.
4585 if (!state
->has_420pack()
4586 || state
->current_function
== NULL
) {
4587 _mesa_glsl_error(& initializer_loc
, state
,
4588 "initializer of %s variable `%s' must be a "
4589 "constant expression",
4592 if (var
->type
->is_numeric()) {
4593 /* Reduce cascading errors. */
4594 var
->constant_value
= type
->qualifier
.flags
.q
.constant
4595 ? ir_constant::zero(state
, var
->type
) : NULL
;
4599 rhs
= constant_value
;
4600 var
->constant_value
= type
->qualifier
.flags
.q
.constant
4601 ? constant_value
: NULL
;
4604 if (var
->type
->is_numeric()) {
4605 /* Reduce cascading errors. */
4606 rhs
= var
->constant_value
= type
->qualifier
.flags
.q
.constant
4607 ? ir_constant::zero(state
, var
->type
) : NULL
;
4612 if (rhs
&& !rhs
->type
->is_error()) {
4613 bool temp
= var
->data
.read_only
;
4614 if (type
->qualifier
.flags
.q
.constant
)
4615 var
->data
.read_only
= false;
4617 /* Never emit code to initialize a uniform.
4619 const glsl_type
*initializer_type
;
4620 bool error_emitted
= false;
4621 if (!type
->qualifier
.flags
.q
.uniform
) {
4623 do_assignment(initializer_instructions
, state
,
4625 &result
, true, true,
4626 type
->get_location());
4627 initializer_type
= result
->type
;
4629 initializer_type
= rhs
->type
;
4631 if (!error_emitted
) {
4632 var
->constant_initializer
= rhs
->constant_expression_value(mem_ctx
);
4633 var
->data
.has_initializer
= true;
4634 var
->data
.is_implicit_initializer
= false;
4636 /* If the declared variable is an unsized array, it must inherrit
4637 * its full type from the initializer. A declaration such as
4639 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
4643 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
4645 * The assignment generated in the if-statement (below) will also
4646 * automatically handle this case for non-uniforms.
4648 * If the declared variable is not an array, the types must
4649 * already match exactly. As a result, the type assignment
4650 * here can be done unconditionally. For non-uniforms the call
4651 * to do_assignment can change the type of the initializer (via
4652 * the implicit conversion rules). For uniforms the initializer
4653 * must be a constant expression, and the type of that expression
4654 * was validated above.
4656 var
->type
= initializer_type
;
4659 var
->data
.read_only
= temp
;
4666 validate_layout_qualifier_vertex_count(struct _mesa_glsl_parse_state
*state
,
4667 YYLTYPE loc
, ir_variable
*var
,
4668 unsigned num_vertices
,
4670 const char *var_category
)
4672 if (var
->type
->is_unsized_array()) {
4673 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says:
4675 * All geometry shader input unsized array declarations will be
4676 * sized by an earlier input layout qualifier, when present, as per
4677 * the following table.
4679 * Followed by a table mapping each allowed input layout qualifier to
4680 * the corresponding input length.
4682 * Similarly for tessellation control shader outputs.
4684 if (num_vertices
!= 0)
4685 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
4688 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec
4689 * includes the following examples of compile-time errors:
4691 * // code sequence within one shader...
4692 * in vec4 Color1[]; // size unknown
4693 * ...Color1.length()...// illegal, length() unknown
4694 * in vec4 Color2[2]; // size is 2
4695 * ...Color1.length()...// illegal, Color1 still has no size
4696 * in vec4 Color3[3]; // illegal, input sizes are inconsistent
4697 * layout(lines) in; // legal, input size is 2, matching
4698 * in vec4 Color4[3]; // illegal, contradicts layout
4701 * To detect the case illustrated by Color3, we verify that the size of
4702 * an explicitly-sized array matches the size of any previously declared
4703 * explicitly-sized array. To detect the case illustrated by Color4, we
4704 * verify that the size of an explicitly-sized array is consistent with
4705 * any previously declared input layout.
4707 if (num_vertices
!= 0 && var
->type
->length
!= num_vertices
) {
4708 _mesa_glsl_error(&loc
, state
,
4709 "%s size contradicts previously declared layout "
4710 "(size is %u, but layout requires a size of %u)",
4711 var_category
, var
->type
->length
, num_vertices
);
4712 } else if (*size
!= 0 && var
->type
->length
!= *size
) {
4713 _mesa_glsl_error(&loc
, state
,
4714 "%s sizes are inconsistent (size is %u, but a "
4715 "previous declaration has size %u)",
4716 var_category
, var
->type
->length
, *size
);
4718 *size
= var
->type
->length
;
4724 handle_tess_ctrl_shader_output_decl(struct _mesa_glsl_parse_state
*state
,
4725 YYLTYPE loc
, ir_variable
*var
)
4727 unsigned num_vertices
= 0;
4729 if (state
->tcs_output_vertices_specified
) {
4730 if (!state
->out_qualifier
->vertices
->
4731 process_qualifier_constant(state
, "vertices",
4732 &num_vertices
, false)) {
4736 if (num_vertices
> state
->Const
.MaxPatchVertices
) {
4737 _mesa_glsl_error(&loc
, state
, "vertices (%d) exceeds "
4738 "GL_MAX_PATCH_VERTICES", num_vertices
);
4743 if (!var
->type
->is_array() && !var
->data
.patch
) {
4744 _mesa_glsl_error(&loc
, state
,
4745 "tessellation control shader outputs must be arrays");
4747 /* To avoid cascading failures, short circuit the checks below. */
4751 if (var
->data
.patch
)
4754 validate_layout_qualifier_vertex_count(state
, loc
, var
, num_vertices
,
4755 &state
->tcs_output_size
,
4756 "tessellation control shader output");
4760 * Do additional processing necessary for tessellation control/evaluation shader
4761 * input declarations. This covers both interface block arrays and bare input
4765 handle_tess_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
4766 YYLTYPE loc
, ir_variable
*var
)
4768 if (!var
->type
->is_array() && !var
->data
.patch
) {
4769 _mesa_glsl_error(&loc
, state
,
4770 "per-vertex tessellation shader inputs must be arrays");
4771 /* Avoid cascading failures. */
4775 if (var
->data
.patch
)
4778 /* The ARB_tessellation_shader spec says:
4780 * "Declaring an array size is optional. If no size is specified, it
4781 * will be taken from the implementation-dependent maximum patch size
4782 * (gl_MaxPatchVertices). If a size is specified, it must match the
4783 * maximum patch size; otherwise, a compile or link error will occur."
4785 * This text appears twice, once for TCS inputs, and again for TES inputs.
4787 if (var
->type
->is_unsized_array()) {
4788 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
4789 state
->Const
.MaxPatchVertices
);
4790 } else if (var
->type
->length
!= state
->Const
.MaxPatchVertices
) {
4791 _mesa_glsl_error(&loc
, state
,
4792 "per-vertex tessellation shader input arrays must be "
4793 "sized to gl_MaxPatchVertices (%d).",
4794 state
->Const
.MaxPatchVertices
);
4800 * Do additional processing necessary for geometry shader input declarations
4801 * (this covers both interface blocks arrays and bare input variables).
4804 handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
4805 YYLTYPE loc
, ir_variable
*var
)
4807 unsigned num_vertices
= 0;
4809 if (state
->gs_input_prim_type_specified
) {
4810 num_vertices
= vertices_per_prim(state
->in_qualifier
->prim_type
);
4813 /* Geometry shader input variables must be arrays. Caller should have
4814 * reported an error for this.
4816 if (!var
->type
->is_array()) {
4817 assert(state
->error
);
4819 /* To avoid cascading failures, short circuit the checks below. */
4823 validate_layout_qualifier_vertex_count(state
, loc
, var
, num_vertices
,
4824 &state
->gs_input_size
,
4825 "geometry shader input");
4829 validate_identifier(const char *identifier
, YYLTYPE loc
,
4830 struct _mesa_glsl_parse_state
*state
)
4832 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
4834 * "Identifiers starting with "gl_" are reserved for use by
4835 * OpenGL, and may not be declared in a shader as either a
4836 * variable or a function."
4838 if (is_gl_identifier(identifier
)) {
4839 _mesa_glsl_error(&loc
, state
,
4840 "identifier `%s' uses reserved `gl_' prefix",
4842 } else if (strstr(identifier
, "__")) {
4843 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
4846 * "In addition, all identifiers containing two
4847 * consecutive underscores (__) are reserved as
4848 * possible future keywords."
4850 * The intention is that names containing __ are reserved for internal
4851 * use by the implementation, and names prefixed with GL_ are reserved
4852 * for use by Khronos. Names simply containing __ are dangerous to use,
4853 * but should be allowed.
4855 * A future version of the GLSL specification will clarify this.
4857 _mesa_glsl_warning(&loc
, state
,
4858 "identifier `%s' uses reserved `__' string",
4864 ast_declarator_list::hir(exec_list
*instructions
,
4865 struct _mesa_glsl_parse_state
*state
)
4868 const struct glsl_type
*decl_type
;
4869 const char *type_name
= NULL
;
4870 ir_rvalue
*result
= NULL
;
4871 YYLTYPE loc
= this->get_location();
4873 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
4875 * "To ensure that a particular output variable is invariant, it is
4876 * necessary to use the invariant qualifier. It can either be used to
4877 * qualify a previously declared variable as being invariant
4879 * invariant gl_Position; // make existing gl_Position be invariant"
4881 * In these cases the parser will set the 'invariant' flag in the declarator
4882 * list, and the type will be NULL.
4884 if (this->invariant
) {
4885 assert(this->type
== NULL
);
4887 if (state
->current_function
!= NULL
) {
4888 _mesa_glsl_error(& loc
, state
,
4889 "all uses of `invariant' keyword must be at global "
4893 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
4894 assert(decl
->array_specifier
== NULL
);
4895 assert(decl
->initializer
== NULL
);
4897 ir_variable
*const earlier
=
4898 state
->symbols
->get_variable(decl
->identifier
);
4899 if (earlier
== NULL
) {
4900 _mesa_glsl_error(& loc
, state
,
4901 "undeclared variable `%s' cannot be marked "
4902 "invariant", decl
->identifier
);
4903 } else if (!is_allowed_invariant(earlier
, state
)) {
4904 _mesa_glsl_error(&loc
, state
,
4905 "`%s' cannot be marked invariant; interfaces between "
4906 "shader stages only.", decl
->identifier
);
4907 } else if (earlier
->data
.used
) {
4908 _mesa_glsl_error(& loc
, state
,
4909 "variable `%s' may not be redeclared "
4910 "`invariant' after being used",
4913 earlier
->data
.explicit_invariant
= true;
4914 earlier
->data
.invariant
= true;
4918 /* Invariant redeclarations do not have r-values.
4923 if (this->precise
) {
4924 assert(this->type
== NULL
);
4926 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
4927 assert(decl
->array_specifier
== NULL
);
4928 assert(decl
->initializer
== NULL
);
4930 ir_variable
*const earlier
=
4931 state
->symbols
->get_variable(decl
->identifier
);
4932 if (earlier
== NULL
) {
4933 _mesa_glsl_error(& loc
, state
,
4934 "undeclared variable `%s' cannot be marked "
4935 "precise", decl
->identifier
);
4936 } else if (state
->current_function
!= NULL
&&
4937 !state
->symbols
->name_declared_this_scope(decl
->identifier
)) {
4938 /* Note: we have to check if we're in a function, since
4939 * builtins are treated as having come from another scope.
4941 _mesa_glsl_error(& loc
, state
,
4942 "variable `%s' from an outer scope may not be "
4943 "redeclared `precise' in this scope",
4945 } else if (earlier
->data
.used
) {
4946 _mesa_glsl_error(& loc
, state
,
4947 "variable `%s' may not be redeclared "
4948 "`precise' after being used",
4951 earlier
->data
.precise
= true;
4955 /* Precise redeclarations do not have r-values either. */
4959 assert(this->type
!= NULL
);
4960 assert(!this->invariant
);
4961 assert(!this->precise
);
4963 /* GL_EXT_shader_image_load_store base type uses GLSL_TYPE_VOID as a special value to
4964 * indicate that it needs to be updated later (see glsl_parser.yy).
4965 * This is done here, based on the layout qualifier and the type of the image var
4967 if (this->type
->qualifier
.flags
.q
.explicit_image_format
&&
4968 this->type
->specifier
->type
->is_image() &&
4969 this->type
->qualifier
.image_base_type
== GLSL_TYPE_VOID
) {
4970 /* "The ARB_shader_image_load_store says:
4971 * If both extensions are enabled in the shading language, the "size*" layout
4972 * qualifiers are treated as format qualifiers, and are mapped to equivalent
4973 * format qualifiers in the table below, according to the type of image
4975 * image* iimage* uimage*
4976 * -------- -------- --------
4977 * size1x8 n/a r8i r8ui
4978 * size1x16 r16f r16i r16ui
4979 * size1x32 r32f r32i r32ui
4980 * size2x32 rg32f rg32i rg32ui
4981 * size4x32 rgba32f rgba32i rgba32ui"
4983 if (strncmp(this->type
->specifier
->type_name
, "image", strlen("image")) == 0) {
4984 switch (this->type
->qualifier
.image_format
) {
4985 case PIPE_FORMAT_R8_SINT
:
4986 /* No valid qualifier in this case, driver will need to look at
4987 * the underlying image's format (just like no qualifier being
4990 this->type
->qualifier
.image_format
= PIPE_FORMAT_NONE
;
4992 case PIPE_FORMAT_R16_SINT
:
4993 this->type
->qualifier
.image_format
= PIPE_FORMAT_R16_FLOAT
;
4995 case PIPE_FORMAT_R32_SINT
:
4996 this->type
->qualifier
.image_format
= PIPE_FORMAT_R32_FLOAT
;
4998 case PIPE_FORMAT_R32G32_SINT
:
4999 this->type
->qualifier
.image_format
= PIPE_FORMAT_R32G32_FLOAT
;
5001 case PIPE_FORMAT_R32G32B32A32_SINT
:
5002 this->type
->qualifier
.image_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
5005 unreachable("Unknown image format");
5007 this->type
->qualifier
.image_base_type
= GLSL_TYPE_FLOAT
;
5008 } else if (strncmp(this->type
->specifier
->type_name
, "uimage", strlen("uimage")) == 0) {
5009 switch (this->type
->qualifier
.image_format
) {
5010 case PIPE_FORMAT_R8_SINT
:
5011 this->type
->qualifier
.image_format
= PIPE_FORMAT_R8_UINT
;
5013 case PIPE_FORMAT_R16_SINT
:
5014 this->type
->qualifier
.image_format
= PIPE_FORMAT_R16_UINT
;
5016 case PIPE_FORMAT_R32_SINT
:
5017 this->type
->qualifier
.image_format
= PIPE_FORMAT_R32_UINT
;
5019 case PIPE_FORMAT_R32G32_SINT
:
5020 this->type
->qualifier
.image_format
= PIPE_FORMAT_R32G32_UINT
;
5022 case PIPE_FORMAT_R32G32B32A32_SINT
:
5023 this->type
->qualifier
.image_format
= PIPE_FORMAT_R32G32B32A32_UINT
;
5026 unreachable("Unknown image format");
5028 this->type
->qualifier
.image_base_type
= GLSL_TYPE_UINT
;
5029 } else if (strncmp(this->type
->specifier
->type_name
, "iimage", strlen("iimage")) == 0) {
5030 this->type
->qualifier
.image_base_type
= GLSL_TYPE_INT
;
5036 /* The type specifier may contain a structure definition. Process that
5037 * before any of the variable declarations.
5039 (void) this->type
->specifier
->hir(instructions
, state
);
5041 decl_type
= this->type
->glsl_type(& type_name
, state
);
5043 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
5044 * "Buffer variables may only be declared inside interface blocks
5045 * (section 4.3.9 “Interface Blocks”), which are then referred to as
5046 * shader storage blocks. It is a compile-time error to declare buffer
5047 * variables at global scope (outside a block)."
5049 if (type
->qualifier
.flags
.q
.buffer
&& !decl_type
->is_interface()) {
5050 _mesa_glsl_error(&loc
, state
,
5051 "buffer variables cannot be declared outside "
5052 "interface blocks");
5055 /* An offset-qualified atomic counter declaration sets the default
5056 * offset for the next declaration within the same atomic counter
5059 if (decl_type
&& decl_type
->contains_atomic()) {
5060 if (type
->qualifier
.flags
.q
.explicit_binding
&&
5061 type
->qualifier
.flags
.q
.explicit_offset
) {
5062 unsigned qual_binding
;
5063 unsigned qual_offset
;
5064 if (process_qualifier_constant(state
, &loc
, "binding",
5065 type
->qualifier
.binding
,
5067 && process_qualifier_constant(state
, &loc
, "offset",
5068 type
->qualifier
.offset
,
5070 if (qual_binding
< ARRAY_SIZE(state
->atomic_counter_offsets
))
5071 state
->atomic_counter_offsets
[qual_binding
] = qual_offset
;
5075 ast_type_qualifier allowed_atomic_qual_mask
;
5076 allowed_atomic_qual_mask
.flags
.i
= 0;
5077 allowed_atomic_qual_mask
.flags
.q
.explicit_binding
= 1;
5078 allowed_atomic_qual_mask
.flags
.q
.explicit_offset
= 1;
5079 allowed_atomic_qual_mask
.flags
.q
.uniform
= 1;
5081 type
->qualifier
.validate_flags(&loc
, state
, allowed_atomic_qual_mask
,
5082 "invalid layout qualifier for",
5086 if (this->declarations
.is_empty()) {
5087 /* If there is no structure involved in the program text, there are two
5088 * possible scenarios:
5090 * - The program text contained something like 'vec4;'. This is an
5091 * empty declaration. It is valid but weird. Emit a warning.
5093 * - The program text contained something like 'S;' and 'S' is not the
5094 * name of a known structure type. This is both invalid and weird.
5097 * - The program text contained something like 'mediump float;'
5098 * when the programmer probably meant 'precision mediump
5099 * float;' Emit a warning with a description of what they
5100 * probably meant to do.
5102 * Note that if decl_type is NULL and there is a structure involved,
5103 * there must have been some sort of error with the structure. In this
5104 * case we assume that an error was already generated on this line of
5105 * code for the structure. There is no need to generate an additional,
5108 assert(this->type
->specifier
->structure
== NULL
|| decl_type
!= NULL
5111 if (decl_type
== NULL
) {
5112 _mesa_glsl_error(&loc
, state
,
5113 "invalid type `%s' in empty declaration",
5116 if (decl_type
->is_array()) {
5117 /* From Section 13.22 (Array Declarations) of the GLSL ES 3.2
5120 * "... any declaration that leaves the size undefined is
5121 * disallowed as this would add complexity and there are no
5124 if (state
->es_shader
&& decl_type
->is_unsized_array()) {
5125 _mesa_glsl_error(&loc
, state
, "array size must be explicitly "
5126 "or implicitly defined");
5129 /* From Section 4.12 (Empty Declarations) of the GLSL 4.5 spec:
5131 * "The combinations of types and qualifiers that cause
5132 * compile-time or link-time errors are the same whether or not
5133 * the declaration is empty."
5135 validate_array_dimensions(decl_type
, state
, &loc
);
5138 if (decl_type
->is_atomic_uint()) {
5139 /* Empty atomic counter declarations are allowed and useful
5140 * to set the default offset qualifier.
5143 } else if (this->type
->qualifier
.precision
!= ast_precision_none
) {
5144 if (this->type
->specifier
->structure
!= NULL
) {
5145 _mesa_glsl_error(&loc
, state
,
5146 "precision qualifiers can't be applied "
5149 static const char *const precision_names
[] = {
5156 _mesa_glsl_warning(&loc
, state
,
5157 "empty declaration with precision "
5158 "qualifier, to set the default precision, "
5159 "use `precision %s %s;'",
5160 precision_names
[this->type
->
5161 qualifier
.precision
],
5164 } else if (this->type
->specifier
->structure
== NULL
) {
5165 _mesa_glsl_warning(&loc
, state
, "empty declaration");
5170 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
5171 const struct glsl_type
*var_type
;
5173 const char *identifier
= decl
->identifier
;
5174 /* FINISHME: Emit a warning if a variable declaration shadows a
5175 * FINISHME: declaration at a higher scope.
5178 if ((decl_type
== NULL
) || decl_type
->is_void()) {
5179 if (type_name
!= NULL
) {
5180 _mesa_glsl_error(& loc
, state
,
5181 "invalid type `%s' in declaration of `%s'",
5182 type_name
, decl
->identifier
);
5184 _mesa_glsl_error(& loc
, state
,
5185 "invalid type in declaration of `%s'",
5191 if (this->type
->qualifier
.is_subroutine_decl()) {
5195 t
= state
->symbols
->get_type(this->type
->specifier
->type_name
);
5197 _mesa_glsl_error(& loc
, state
,
5198 "invalid type in declaration of `%s'",
5200 name
= ralloc_asprintf(ctx
, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state
->stage
), decl
->identifier
);
5205 var_type
= process_array_type(&loc
, decl_type
, decl
->array_specifier
,
5208 var
= new(ctx
) ir_variable(var_type
, identifier
, ir_var_auto
);
5210 /* The 'varying in' and 'varying out' qualifiers can only be used with
5211 * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
5214 if (this->type
->qualifier
.flags
.q
.varying
) {
5215 if (this->type
->qualifier
.flags
.q
.in
) {
5216 _mesa_glsl_error(& loc
, state
,
5217 "`varying in' qualifier in declaration of "
5218 "`%s' only valid for geometry shaders using "
5219 "ARB_geometry_shader4 or EXT_geometry_shader4",
5221 } else if (this->type
->qualifier
.flags
.q
.out
) {
5222 _mesa_glsl_error(& loc
, state
,
5223 "`varying out' qualifier in declaration of "
5224 "`%s' only valid for geometry shaders using "
5225 "ARB_geometry_shader4 or EXT_geometry_shader4",
5230 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
5232 * "Global variables can only use the qualifiers const,
5233 * attribute, uniform, or varying. Only one may be
5236 * Local variables can only use the qualifier const."
5238 * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
5239 * any extension that adds the 'layout' keyword.
5241 if (!state
->is_version(130, 300)
5242 && !state
->has_explicit_attrib_location()
5243 && !state
->has_separate_shader_objects()
5244 && !state
->ARB_fragment_coord_conventions_enable
) {
5245 /* GL_EXT_gpu_shader4 only allows "varying out" on fragment shader
5246 * outputs. (the varying flag is not set by the parser)
5248 if (this->type
->qualifier
.flags
.q
.out
&&
5249 (!state
->EXT_gpu_shader4_enable
||
5250 state
->stage
!= MESA_SHADER_FRAGMENT
)) {
5251 _mesa_glsl_error(& loc
, state
,
5252 "`out' qualifier in declaration of `%s' "
5253 "only valid for function parameters in %s",
5254 decl
->identifier
, state
->get_version_string());
5256 if (this->type
->qualifier
.flags
.q
.in
) {
5257 _mesa_glsl_error(& loc
, state
,
5258 "`in' qualifier in declaration of `%s' "
5259 "only valid for function parameters in %s",
5260 decl
->identifier
, state
->get_version_string());
5262 /* FINISHME: Test for other invalid qualifiers. */
5265 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
5267 apply_layout_qualifier_to_variable(&this->type
->qualifier
, var
, state
,
5270 if ((state
->zero_init
& (1u << var
->data
.mode
)) &&
5271 (var
->type
->is_numeric() || var
->type
->is_boolean())) {
5272 const ir_constant_data data
= { { 0 } };
5273 var
->data
.has_initializer
= true;
5274 var
->data
.is_implicit_initializer
= true;
5275 var
->constant_initializer
= new(var
) ir_constant(var
->type
, &data
);
5278 if (this->type
->qualifier
.flags
.q
.invariant
) {
5279 if (!is_allowed_invariant(var
, state
)) {
5280 _mesa_glsl_error(&loc
, state
,
5281 "`%s' cannot be marked invariant; interfaces between "
5282 "shader stages only", var
->name
);
5286 if (state
->current_function
!= NULL
) {
5287 const char *mode
= NULL
;
5288 const char *extra
= "";
5290 /* There is no need to check for 'inout' here because the parser will
5291 * only allow that in function parameter lists.
5293 if (this->type
->qualifier
.flags
.q
.attribute
) {
5295 } else if (this->type
->qualifier
.is_subroutine_decl()) {
5296 mode
= "subroutine uniform";
5297 } else if (this->type
->qualifier
.flags
.q
.uniform
) {
5299 } else if (this->type
->qualifier
.flags
.q
.varying
) {
5301 } else if (this->type
->qualifier
.flags
.q
.in
) {
5303 extra
= " or in function parameter list";
5304 } else if (this->type
->qualifier
.flags
.q
.out
) {
5306 extra
= " or in function parameter list";
5310 _mesa_glsl_error(& loc
, state
,
5311 "%s variable `%s' must be declared at "
5313 mode
, var
->name
, extra
);
5315 } else if (var
->data
.mode
== ir_var_shader_in
) {
5316 var
->data
.read_only
= true;
5318 if (state
->stage
== MESA_SHADER_VERTEX
) {
5319 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
5321 * "Vertex shader inputs can only be float, floating-point
5322 * vectors, matrices, signed and unsigned integers and integer
5323 * vectors. Vertex shader inputs can also form arrays of these
5324 * types, but not structures."
5326 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
5328 * "Vertex shader inputs can only be float, floating-point
5329 * vectors, matrices, signed and unsigned integers and integer
5330 * vectors. They cannot be arrays or structures."
5332 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
5334 * "The attribute qualifier can be used only with float,
5335 * floating-point vectors, and matrices. Attribute variables
5336 * cannot be declared as arrays or structures."
5338 * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
5340 * "Vertex shader inputs can only be float, floating-point
5341 * vectors, matrices, signed and unsigned integers and integer
5342 * vectors. Vertex shader inputs cannot be arrays or
5345 * From section 4.3.4 of the ARB_bindless_texture spec:
5347 * "(modify third paragraph of the section to allow sampler and
5348 * image types) ... Vertex shader inputs can only be float,
5349 * single-precision floating-point scalars, single-precision
5350 * floating-point vectors, matrices, signed and unsigned
5351 * integers and integer vectors, sampler and image types."
5353 const glsl_type
*check_type
= var
->type
->without_array();
5356 switch (check_type
->base_type
) {
5357 case GLSL_TYPE_FLOAT
:
5359 case GLSL_TYPE_UINT64
:
5360 case GLSL_TYPE_INT64
:
5362 case GLSL_TYPE_UINT
:
5364 error
= !state
->is_version(120, 300) && !state
->EXT_gpu_shader4_enable
;
5366 case GLSL_TYPE_DOUBLE
:
5367 error
= !state
->is_version(410, 0) && !state
->ARB_vertex_attrib_64bit_enable
;
5369 case GLSL_TYPE_SAMPLER
:
5370 case GLSL_TYPE_IMAGE
:
5371 error
= !state
->has_bindless();
5378 _mesa_glsl_error(& loc
, state
,
5379 "vertex shader input / attribute cannot have "
5381 var
->type
->is_array() ? "array of " : "",
5383 } else if (var
->type
->is_array() &&
5384 !state
->check_version(150, 0, &loc
,
5385 "vertex shader input / attribute "
5386 "cannot have array type")) {
5388 } else if (state
->stage
== MESA_SHADER_GEOMETRY
) {
5389 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
5391 * Geometry shader input variables get the per-vertex values
5392 * written out by vertex shader output variables of the same
5393 * names. Since a geometry shader operates on a set of
5394 * vertices, each input varying variable (or input block, see
5395 * interface blocks below) needs to be declared as an array.
5397 if (!var
->type
->is_array()) {
5398 _mesa_glsl_error(&loc
, state
,
5399 "geometry shader inputs must be arrays");
5402 handle_geometry_shader_input_decl(state
, loc
, var
);
5403 } else if (state
->stage
== MESA_SHADER_FRAGMENT
) {
5404 /* From section 4.3.4 (Input Variables) of the GLSL ES 3.10 spec:
5406 * It is a compile-time error to declare a fragment shader
5407 * input with, or that contains, any of the following types:
5411 * * An array of arrays
5412 * * An array of structures
5413 * * A structure containing an array
5414 * * A structure containing a structure
5416 if (state
->es_shader
) {
5417 const glsl_type
*check_type
= var
->type
->without_array();
5418 if (check_type
->is_boolean() ||
5419 check_type
->contains_opaque()) {
5420 _mesa_glsl_error(&loc
, state
,
5421 "fragment shader input cannot have type %s",
5424 if (var
->type
->is_array() &&
5425 var
->type
->fields
.array
->is_array()) {
5426 _mesa_glsl_error(&loc
, state
,
5428 "cannot have an array of arrays",
5429 _mesa_shader_stage_to_string(state
->stage
));
5431 if (var
->type
->is_array() &&
5432 var
->type
->fields
.array
->is_struct()) {
5433 _mesa_glsl_error(&loc
, state
,
5434 "fragment shader input "
5435 "cannot have an array of structs");
5437 if (var
->type
->is_struct()) {
5438 for (unsigned i
= 0; i
< var
->type
->length
; i
++) {
5439 if (var
->type
->fields
.structure
[i
].type
->is_array() ||
5440 var
->type
->fields
.structure
[i
].type
->is_struct())
5441 _mesa_glsl_error(&loc
, state
,
5442 "fragment shader input cannot have "
5443 "a struct that contains an "
5448 } else if (state
->stage
== MESA_SHADER_TESS_CTRL
||
5449 state
->stage
== MESA_SHADER_TESS_EVAL
) {
5450 handle_tess_shader_input_decl(state
, loc
, var
);
5452 } else if (var
->data
.mode
== ir_var_shader_out
) {
5453 const glsl_type
*check_type
= var
->type
->without_array();
5455 /* From section 4.3.6 (Output variables) of the GLSL 4.40 spec:
5457 * It is a compile-time error to declare a fragment shader output
5458 * that contains any of the following:
5460 * * A Boolean type (bool, bvec2 ...)
5461 * * A double-precision scalar or vector (double, dvec2 ...)
5466 if (state
->stage
== MESA_SHADER_FRAGMENT
) {
5467 if (check_type
->is_struct() || check_type
->is_matrix())
5468 _mesa_glsl_error(&loc
, state
,
5469 "fragment shader output "
5470 "cannot have struct or matrix type");
5471 switch (check_type
->base_type
) {
5472 case GLSL_TYPE_UINT
:
5474 case GLSL_TYPE_FLOAT
:
5477 _mesa_glsl_error(&loc
, state
,
5478 "fragment shader output cannot have "
5479 "type %s", check_type
->name
);
5483 /* From section 4.3.6 (Output Variables) of the GLSL ES 3.10 spec:
5485 * It is a compile-time error to declare a vertex shader output
5486 * with, or that contains, any of the following types:
5490 * * An array of arrays
5491 * * An array of structures
5492 * * A structure containing an array
5493 * * A structure containing a structure
5495 * It is a compile-time error to declare a fragment shader output
5496 * with, or that contains, any of the following types:
5502 * * An array of array
5504 * ES 3.20 updates this to apply to tessellation and geometry shaders
5505 * as well. Because there are per-vertex arrays in the new stages,
5506 * it strikes the "array of..." rules and replaces them with these:
5508 * * For per-vertex-arrayed variables (applies to tessellation
5509 * control, tessellation evaluation and geometry shaders):
5511 * * Per-vertex-arrayed arrays of arrays
5512 * * Per-vertex-arrayed arrays of structures
5514 * * For non-per-vertex-arrayed variables:
5516 * * An array of arrays
5517 * * An array of structures
5519 * which basically says to unwrap the per-vertex aspect and apply
5522 if (state
->es_shader
) {
5523 if (var
->type
->is_array() &&
5524 var
->type
->fields
.array
->is_array()) {
5525 _mesa_glsl_error(&loc
, state
,
5527 "cannot have an array of arrays",
5528 _mesa_shader_stage_to_string(state
->stage
));
5530 if (state
->stage
<= MESA_SHADER_GEOMETRY
) {
5531 const glsl_type
*type
= var
->type
;
5533 if (state
->stage
== MESA_SHADER_TESS_CTRL
&&
5534 !var
->data
.patch
&& var
->type
->is_array()) {
5535 type
= var
->type
->fields
.array
;
5538 if (type
->is_array() && type
->fields
.array
->is_struct()) {
5539 _mesa_glsl_error(&loc
, state
,
5540 "%s shader output cannot have "
5541 "an array of structs",
5542 _mesa_shader_stage_to_string(state
->stage
));
5544 if (type
->is_struct()) {
5545 for (unsigned i
= 0; i
< type
->length
; i
++) {
5546 if (type
->fields
.structure
[i
].type
->is_array() ||
5547 type
->fields
.structure
[i
].type
->is_struct())
5548 _mesa_glsl_error(&loc
, state
,
5549 "%s shader output cannot have a "
5550 "struct that contains an "
5552 _mesa_shader_stage_to_string(state
->stage
));
5558 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
5559 handle_tess_ctrl_shader_output_decl(state
, loc
, var
);
5561 } else if (var
->type
->contains_subroutine()) {
5562 /* declare subroutine uniforms as hidden */
5563 var
->data
.how_declared
= ir_var_hidden
;
5566 /* From section 4.3.4 of the GLSL 4.00 spec:
5567 * "Input variables may not be declared using the patch in qualifier
5568 * in tessellation control or geometry shaders."
5570 * From section 4.3.6 of the GLSL 4.00 spec:
5571 * "It is an error to use patch out in a vertex, tessellation
5572 * evaluation, or geometry shader."
5574 * This doesn't explicitly forbid using them in a fragment shader, but
5575 * that's probably just an oversight.
5577 if (state
->stage
!= MESA_SHADER_TESS_EVAL
5578 && this->type
->qualifier
.flags
.q
.patch
5579 && this->type
->qualifier
.flags
.q
.in
) {
5581 _mesa_glsl_error(&loc
, state
, "'patch in' can only be used in a "
5582 "tessellation evaluation shader");
5585 if (state
->stage
!= MESA_SHADER_TESS_CTRL
5586 && this->type
->qualifier
.flags
.q
.patch
5587 && this->type
->qualifier
.flags
.q
.out
) {
5589 _mesa_glsl_error(&loc
, state
, "'patch out' can only be used in a "
5590 "tessellation control shader");
5593 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
5595 if (this->type
->qualifier
.precision
!= ast_precision_none
) {
5596 state
->check_precision_qualifiers_allowed(&loc
);
5599 if (this->type
->qualifier
.precision
!= ast_precision_none
&&
5600 !precision_qualifier_allowed(var
->type
)) {
5601 _mesa_glsl_error(&loc
, state
,
5602 "precision qualifiers apply only to floating point"
5603 ", integer and opaque types");
5606 /* From section 4.1.7 of the GLSL 4.40 spec:
5608 * "[Opaque types] can only be declared as function
5609 * parameters or uniform-qualified variables."
5611 * From section 4.1.7 of the ARB_bindless_texture spec:
5613 * "Samplers may be declared as shader inputs and outputs, as uniform
5614 * variables, as temporary variables, and as function parameters."
5616 * From section 4.1.X of the ARB_bindless_texture spec:
5618 * "Images may be declared as shader inputs and outputs, as uniform
5619 * variables, as temporary variables, and as function parameters."
5621 if (!this->type
->qualifier
.flags
.q
.uniform
&&
5622 (var_type
->contains_atomic() ||
5623 (!state
->has_bindless() && var_type
->contains_opaque()))) {
5624 _mesa_glsl_error(&loc
, state
,
5625 "%s variables must be declared uniform",
5626 state
->has_bindless() ? "atomic" : "opaque");
5629 /* Process the initializer and add its instructions to a temporary
5630 * list. This list will be added to the instruction stream (below) after
5631 * the declaration is added. This is done because in some cases (such as
5632 * redeclarations) the declaration may not actually be added to the
5633 * instruction stream.
5635 exec_list initializer_instructions
;
5637 /* Examine var name here since var may get deleted in the next call */
5638 bool var_is_gl_id
= is_gl_identifier(var
->name
);
5640 bool is_redeclaration
;
5641 var
= get_variable_being_redeclared(&var
, decl
->get_location(), state
,
5642 false /* allow_all_redeclarations */,
5644 if (is_redeclaration
) {
5646 var
->data
.how_declared
== ir_var_declared_in_block
) {
5647 _mesa_glsl_error(&loc
, state
,
5648 "`%s' has already been redeclared using "
5649 "gl_PerVertex", var
->name
);
5651 var
->data
.how_declared
= ir_var_declared_normally
;
5654 if (decl
->initializer
!= NULL
) {
5655 result
= process_initializer(var
,
5657 &initializer_instructions
, state
);
5659 validate_array_dimensions(var_type
, state
, &loc
);
5662 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
5664 * "It is an error to write to a const variable outside of
5665 * its declaration, so they must be initialized when
5668 if (this->type
->qualifier
.flags
.q
.constant
&& decl
->initializer
== NULL
) {
5669 _mesa_glsl_error(& loc
, state
,
5670 "const declaration of `%s' must be initialized",
5674 if (state
->es_shader
) {
5675 const glsl_type
*const t
= var
->type
;
5677 /* Skip the unsized array check for TCS/TES/GS inputs & TCS outputs.
5679 * The GL_OES_tessellation_shader spec says about inputs:
5681 * "Declaring an array size is optional. If no size is specified,
5682 * it will be taken from the implementation-dependent maximum
5683 * patch size (gl_MaxPatchVertices)."
5685 * and about TCS outputs:
5687 * "If no size is specified, it will be taken from output patch
5688 * size declared in the shader."
5690 * The GL_OES_geometry_shader spec says:
5692 * "All geometry shader input unsized array declarations will be
5693 * sized by an earlier input primitive layout qualifier, when
5694 * present, as per the following table."
5696 const bool implicitly_sized
=
5697 (var
->data
.mode
== ir_var_shader_in
&&
5698 state
->stage
>= MESA_SHADER_TESS_CTRL
&&
5699 state
->stage
<= MESA_SHADER_GEOMETRY
) ||
5700 (var
->data
.mode
== ir_var_shader_out
&&
5701 state
->stage
== MESA_SHADER_TESS_CTRL
);
5703 if (t
->is_unsized_array() && !implicitly_sized
)
5704 /* Section 10.17 of the GLSL ES 1.00 specification states that
5705 * unsized array declarations have been removed from the language.
5706 * Arrays that are sized using an initializer are still explicitly
5707 * sized. However, GLSL ES 1.00 does not allow array
5708 * initializers. That is only allowed in GLSL ES 3.00.
5710 * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
5712 * "An array type can also be formed without specifying a size
5713 * if the definition includes an initializer:
5715 * float x[] = float[2] (1.0, 2.0); // declares an array of size 2
5716 * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
5721 _mesa_glsl_error(& loc
, state
,
5722 "unsized array declarations are not allowed in "
5726 /* Section 4.4.6.1 Atomic Counter Layout Qualifiers of the GLSL 4.60 spec:
5728 * "It is a compile-time error to declare an unsized array of
5731 if (var
->type
->is_unsized_array() &&
5732 var
->type
->without_array()->base_type
== GLSL_TYPE_ATOMIC_UINT
) {
5733 _mesa_glsl_error(& loc
, state
,
5734 "Unsized array of atomic_uint is not allowed");
5737 /* If the declaration is not a redeclaration, there are a few additional
5738 * semantic checks that must be applied. In addition, variable that was
5739 * created for the declaration should be added to the IR stream.
5741 if (!is_redeclaration
) {
5742 validate_identifier(decl
->identifier
, loc
, state
);
5744 /* Add the variable to the symbol table. Note that the initializer's
5745 * IR was already processed earlier (though it hasn't been emitted
5746 * yet), without the variable in scope.
5748 * This differs from most C-like languages, but it follows the GLSL
5749 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
5752 * "Within a declaration, the scope of a name starts immediately
5753 * after the initializer if present or immediately after the name
5754 * being declared if not."
5756 if (!state
->symbols
->add_variable(var
)) {
5757 YYLTYPE loc
= this->get_location();
5758 _mesa_glsl_error(&loc
, state
, "name `%s' already taken in the "
5759 "current scope", decl
->identifier
);
5763 /* Push the variable declaration to the top. It means that all the
5764 * variable declarations will appear in a funny last-to-first order,
5765 * but otherwise we run into trouble if a function is prototyped, a
5766 * global var is decled, then the function is defined with usage of
5767 * the global var. See glslparsertest's CorrectModule.frag.
5769 instructions
->push_head(var
);
5772 instructions
->append_list(&initializer_instructions
);
5776 /* Generally, variable declarations do not have r-values. However,
5777 * one is used for the declaration in
5779 * while (bool b = some_condition()) {
5783 * so we return the rvalue from the last seen declaration here.
5790 ast_parameter_declarator::hir(exec_list
*instructions
,
5791 struct _mesa_glsl_parse_state
*state
)
5794 const struct glsl_type
*type
;
5795 const char *name
= NULL
;
5796 YYLTYPE loc
= this->get_location();
5798 type
= this->type
->glsl_type(& name
, state
);
5802 _mesa_glsl_error(& loc
, state
,
5803 "invalid type `%s' in declaration of `%s'",
5804 name
, this->identifier
);
5806 _mesa_glsl_error(& loc
, state
,
5807 "invalid type in declaration of `%s'",
5811 type
= glsl_type::error_type
;
5814 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
5816 * "Functions that accept no input arguments need not use void in the
5817 * argument list because prototypes (or definitions) are required and
5818 * therefore there is no ambiguity when an empty argument list "( )" is
5819 * declared. The idiom "(void)" as a parameter list is provided for
5822 * Placing this check here prevents a void parameter being set up
5823 * for a function, which avoids tripping up checks for main taking
5824 * parameters and lookups of an unnamed symbol.
5826 if (type
->is_void()) {
5827 if (this->identifier
!= NULL
)
5828 _mesa_glsl_error(& loc
, state
,
5829 "named parameter cannot have type `void'");
5835 if (formal_parameter
&& (this->identifier
== NULL
)) {
5836 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
5840 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
5841 * call already handled the "vec4[..] foo" case.
5843 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
5845 if (!type
->is_error() && type
->is_unsized_array()) {
5846 _mesa_glsl_error(&loc
, state
, "arrays passed as parameters must have "
5848 type
= glsl_type::error_type
;
5852 ir_variable
*var
= new(ctx
)
5853 ir_variable(type
, this->identifier
, ir_var_function_in
);
5855 /* Apply any specified qualifiers to the parameter declaration. Note that
5856 * for function parameters the default mode is 'in'.
5858 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
,
5861 if (((1u << var
->data
.mode
) & state
->zero_init
) &&
5862 (var
->type
->is_numeric() || var
->type
->is_boolean())) {
5863 const ir_constant_data data
= { { 0 } };
5864 var
->data
.has_initializer
= true;
5865 var
->data
.is_implicit_initializer
= true;
5866 var
->constant_initializer
= new(var
) ir_constant(var
->type
, &data
);
5869 /* From section 4.1.7 of the GLSL 4.40 spec:
5871 * "Opaque variables cannot be treated as l-values; hence cannot
5872 * be used as out or inout function parameters, nor can they be
5875 * From section 4.1.7 of the ARB_bindless_texture spec:
5877 * "Samplers can be used as l-values, so can be assigned into and used
5878 * as "out" and "inout" function parameters."
5880 * From section 4.1.X of the ARB_bindless_texture spec:
5882 * "Images can be used as l-values, so can be assigned into and used as
5883 * "out" and "inout" function parameters."
5885 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
5886 && (type
->contains_atomic() ||
5887 (!state
->has_bindless() && type
->contains_opaque()))) {
5888 _mesa_glsl_error(&loc
, state
, "out and inout parameters cannot "
5889 "contain %s variables",
5890 state
->has_bindless() ? "atomic" : "opaque");
5891 type
= glsl_type::error_type
;
5894 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
5896 * "When calling a function, expressions that do not evaluate to
5897 * l-values cannot be passed to parameters declared as out or inout."
5899 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
5901 * "Other binary or unary expressions, non-dereferenced arrays,
5902 * function names, swizzles with repeated fields, and constants
5903 * cannot be l-values."
5905 * So for GLSL 1.10, passing an array as an out or inout parameter is not
5906 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
5908 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
5910 && !state
->check_version(120, 100, &loc
,
5911 "arrays cannot be out or inout parameters")) {
5912 type
= glsl_type::error_type
;
5915 instructions
->push_tail(var
);
5917 /* Parameter declarations do not have r-values.
5924 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
5926 exec_list
*ir_parameters
,
5927 _mesa_glsl_parse_state
*state
)
5929 ast_parameter_declarator
*void_param
= NULL
;
5932 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
5933 param
->formal_parameter
= formal
;
5934 param
->hir(ir_parameters
, state
);
5942 if ((void_param
!= NULL
) && (count
> 1)) {
5943 YYLTYPE loc
= void_param
->get_location();
5945 _mesa_glsl_error(& loc
, state
,
5946 "`void' parameter must be only parameter");
5952 emit_function(_mesa_glsl_parse_state
*state
, ir_function
*f
)
5954 /* IR invariants disallow function declarations or definitions
5955 * nested within other function definitions. But there is no
5956 * requirement about the relative order of function declarations
5957 * and definitions with respect to one another. So simply insert
5958 * the new ir_function block at the end of the toplevel instruction
5961 state
->toplevel_ir
->push_tail(f
);
5966 ast_function::hir(exec_list
*instructions
,
5967 struct _mesa_glsl_parse_state
*state
)
5970 ir_function
*f
= NULL
;
5971 ir_function_signature
*sig
= NULL
;
5972 exec_list hir_parameters
;
5973 YYLTYPE loc
= this->get_location();
5975 const char *const name
= identifier
;
5977 /* New functions are always added to the top-level IR instruction stream,
5978 * so this instruction list pointer is ignored. See also emit_function
5981 (void) instructions
;
5983 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
5985 * "Function declarations (prototypes) cannot occur inside of functions;
5986 * they must be at global scope, or for the built-in functions, outside
5987 * the global scope."
5989 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
5991 * "User defined functions may only be defined within the global scope."
5993 * Note that this language does not appear in GLSL 1.10.
5995 if ((state
->current_function
!= NULL
) &&
5996 state
->is_version(120, 100)) {
5997 YYLTYPE loc
= this->get_location();
5998 _mesa_glsl_error(&loc
, state
,
5999 "declaration of function `%s' not allowed within "
6000 "function body", name
);
6003 validate_identifier(name
, this->get_location(), state
);
6005 /* Convert the list of function parameters to HIR now so that they can be
6006 * used below to compare this function's signature with previously seen
6007 * signatures for functions with the same name.
6009 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
6011 & hir_parameters
, state
);
6013 const char *return_type_name
;
6014 const glsl_type
*return_type
=
6015 this->return_type
->glsl_type(& return_type_name
, state
);
6018 YYLTYPE loc
= this->get_location();
6019 _mesa_glsl_error(&loc
, state
,
6020 "function `%s' has undeclared return type `%s'",
6021 name
, return_type_name
);
6022 return_type
= glsl_type::error_type
;
6025 /* ARB_shader_subroutine states:
6026 * "Subroutine declarations cannot be prototyped. It is an error to prepend
6027 * subroutine(...) to a function declaration."
6029 if (this->return_type
->qualifier
.subroutine_list
&& !is_definition
) {
6030 YYLTYPE loc
= this->get_location();
6031 _mesa_glsl_error(&loc
, state
,
6032 "function declaration `%s' cannot have subroutine prepended",
6036 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
6037 * "No qualifier is allowed on the return type of a function."
6039 if (this->return_type
->has_qualifiers(state
)) {
6040 YYLTYPE loc
= this->get_location();
6041 _mesa_glsl_error(& loc
, state
,
6042 "function `%s' return type has qualifiers", name
);
6045 /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
6047 * "Arrays are allowed as arguments and as the return type. In both
6048 * cases, the array must be explicitly sized."
6050 if (return_type
->is_unsized_array()) {
6051 YYLTYPE loc
= this->get_location();
6052 _mesa_glsl_error(& loc
, state
,
6053 "function `%s' return type array must be explicitly "
6057 /* From Section 6.1 (Function Definitions) of the GLSL 1.00 spec:
6059 * "Arrays are allowed as arguments, but not as the return type. [...]
6060 * The return type can also be a structure if the structure does not
6061 * contain an array."
6063 if (state
->language_version
== 100 && return_type
->contains_array()) {
6064 YYLTYPE loc
= this->get_location();
6065 _mesa_glsl_error(& loc
, state
,
6066 "function `%s' return type contains an array", name
);
6069 /* From section 4.1.7 of the GLSL 4.40 spec:
6071 * "[Opaque types] can only be declared as function parameters
6072 * or uniform-qualified variables."
6074 * The ARB_bindless_texture spec doesn't clearly state this, but as it says
6075 * "Replace Section 4.1.7 (Samplers), p. 25" and, "Replace Section 4.1.X,
6076 * (Images)", this should be allowed.
6078 if (return_type
->contains_atomic() ||
6079 (!state
->has_bindless() && return_type
->contains_opaque())) {
6080 YYLTYPE loc
= this->get_location();
6081 _mesa_glsl_error(&loc
, state
,
6082 "function `%s' return type can't contain an %s type",
6083 name
, state
->has_bindless() ? "atomic" : "opaque");
6087 if (return_type
->is_subroutine()) {
6088 YYLTYPE loc
= this->get_location();
6089 _mesa_glsl_error(&loc
, state
,
6090 "function `%s' return type can't be a subroutine type",
6094 /* Get the precision for the return type */
6095 unsigned return_precision
;
6097 if (state
->es_shader
) {
6098 YYLTYPE loc
= this->get_location();
6100 select_gles_precision(this->return_type
->qualifier
.precision
,
6105 return_precision
= GLSL_PRECISION_NONE
;
6108 /* Create an ir_function if one doesn't already exist. */
6109 f
= state
->symbols
->get_function(name
);
6111 f
= new(ctx
) ir_function(name
);
6112 if (!this->return_type
->qualifier
.is_subroutine_decl()) {
6113 if (!state
->symbols
->add_function(f
)) {
6114 /* This function name shadows a non-function use of the same name. */
6115 YYLTYPE loc
= this->get_location();
6116 _mesa_glsl_error(&loc
, state
, "function name `%s' conflicts with "
6117 "non-function", name
);
6121 emit_function(state
, f
);
6124 /* From GLSL ES 3.0 spec, chapter 6.1 "Function Definitions", page 71:
6126 * "A shader cannot redefine or overload built-in functions."
6128 * While in GLSL ES 1.0 specification, chapter 8 "Built-in Functions":
6130 * "User code can overload the built-in functions but cannot redefine
6133 if (state
->es_shader
) {
6134 /* Local shader has no exact candidates; check the built-ins. */
6135 if (state
->language_version
>= 300 &&
6136 _mesa_glsl_has_builtin_function(state
, name
)) {
6137 YYLTYPE loc
= this->get_location();
6138 _mesa_glsl_error(& loc
, state
,
6139 "A shader cannot redefine or overload built-in "
6140 "function `%s' in GLSL ES 3.00", name
);
6144 if (state
->language_version
== 100) {
6145 ir_function_signature
*sig
=
6146 _mesa_glsl_find_builtin_function(state
, name
, &hir_parameters
);
6147 if (sig
&& sig
->is_builtin()) {
6148 _mesa_glsl_error(& loc
, state
,
6149 "A shader cannot redefine built-in "
6150 "function `%s' in GLSL ES 1.00", name
);
6155 /* Verify that this function's signature either doesn't match a previously
6156 * seen signature for a function with the same name, or, if a match is found,
6157 * that the previously seen signature does not have an associated definition.
6159 if (state
->es_shader
|| f
->has_user_signature()) {
6160 sig
= f
->exact_matching_signature(state
, &hir_parameters
);
6162 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
6163 if (badvar
!= NULL
) {
6164 YYLTYPE loc
= this->get_location();
6166 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
6167 "qualifiers don't match prototype", name
, badvar
);
6170 if (sig
->return_type
!= return_type
) {
6171 YYLTYPE loc
= this->get_location();
6173 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
6174 "match prototype", name
);
6177 if (sig
->return_precision
!= return_precision
) {
6178 YYLTYPE loc
= this->get_location();
6180 _mesa_glsl_error(&loc
, state
, "function `%s' return type precision "
6181 "doesn't match prototype", name
);
6184 if (sig
->is_defined
) {
6185 if (is_definition
) {
6186 YYLTYPE loc
= this->get_location();
6187 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
6189 /* We just encountered a prototype that exactly matches a
6190 * function that's already been defined. This is redundant,
6191 * and we should ignore it.
6195 } else if (state
->language_version
== 100 && !is_definition
) {
6196 /* From the GLSL 1.00 spec, section 4.2.7:
6198 * "A particular variable, structure or function declaration
6199 * may occur at most once within a scope with the exception
6200 * that a single function prototype plus the corresponding
6201 * function definition are allowed."
6203 YYLTYPE loc
= this->get_location();
6204 _mesa_glsl_error(&loc
, state
, "function `%s' redeclared", name
);
6209 /* Verify the return type of main() */
6210 if (strcmp(name
, "main") == 0) {
6211 if (! return_type
->is_void()) {
6212 YYLTYPE loc
= this->get_location();
6214 _mesa_glsl_error(& loc
, state
, "main() must return void");
6217 if (!hir_parameters
.is_empty()) {
6218 YYLTYPE loc
= this->get_location();
6220 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
6224 /* Finish storing the information about this new function in its signature.
6227 sig
= new(ctx
) ir_function_signature(return_type
);
6228 sig
->return_precision
= return_precision
;
6229 f
->add_signature(sig
);
6232 sig
->replace_parameters(&hir_parameters
);
6235 if (this->return_type
->qualifier
.subroutine_list
) {
6238 if (this->return_type
->qualifier
.flags
.q
.explicit_index
) {
6239 unsigned qual_index
;
6240 if (process_qualifier_constant(state
, &loc
, "index",
6241 this->return_type
->qualifier
.index
,
6243 if (!state
->has_explicit_uniform_location()) {
6244 _mesa_glsl_error(&loc
, state
, "subroutine index requires "
6245 "GL_ARB_explicit_uniform_location or "
6247 } else if (qual_index
>= MAX_SUBROUTINES
) {
6248 _mesa_glsl_error(&loc
, state
,
6249 "invalid subroutine index (%d) index must "
6250 "be a number between 0 and "
6251 "GL_MAX_SUBROUTINES - 1 (%d)", qual_index
,
6252 MAX_SUBROUTINES
- 1);
6254 f
->subroutine_index
= qual_index
;
6259 f
->num_subroutine_types
= this->return_type
->qualifier
.subroutine_list
->declarations
.length();
6260 f
->subroutine_types
= ralloc_array(state
, const struct glsl_type
*,
6261 f
->num_subroutine_types
);
6263 foreach_list_typed(ast_declaration
, decl
, link
, &this->return_type
->qualifier
.subroutine_list
->declarations
) {
6264 const struct glsl_type
*type
;
6265 /* the subroutine type must be already declared */
6266 type
= state
->symbols
->get_type(decl
->identifier
);
6268 _mesa_glsl_error(& loc
, state
, "unknown type '%s' in subroutine function definition", decl
->identifier
);
6271 for (int i
= 0; i
< state
->num_subroutine_types
; i
++) {
6272 ir_function
*fn
= state
->subroutine_types
[i
];
6273 ir_function_signature
*tsig
= NULL
;
6275 if (strcmp(fn
->name
, decl
->identifier
))
6278 tsig
= fn
->matching_signature(state
, &sig
->parameters
,
6281 _mesa_glsl_error(& loc
, state
, "subroutine type mismatch '%s' - signatures do not match\n", decl
->identifier
);
6283 if (tsig
->return_type
!= sig
->return_type
) {
6284 _mesa_glsl_error(& loc
, state
, "subroutine type mismatch '%s' - return types do not match\n", decl
->identifier
);
6288 f
->subroutine_types
[idx
++] = type
;
6290 state
->subroutines
= (ir_function
**)reralloc(state
, state
->subroutines
,
6292 state
->num_subroutines
+ 1);
6293 state
->subroutines
[state
->num_subroutines
] = f
;
6294 state
->num_subroutines
++;
6298 if (this->return_type
->qualifier
.is_subroutine_decl()) {
6299 if (!state
->symbols
->add_type(this->identifier
, glsl_type::get_subroutine_instance(this->identifier
))) {
6300 _mesa_glsl_error(& loc
, state
, "type '%s' previously defined", this->identifier
);
6303 state
->subroutine_types
= (ir_function
**)reralloc(state
, state
->subroutine_types
,
6305 state
->num_subroutine_types
+ 1);
6306 state
->subroutine_types
[state
->num_subroutine_types
] = f
;
6307 state
->num_subroutine_types
++;
6309 f
->is_subroutine
= true;
6312 /* Function declarations (prototypes) do not have r-values.
6319 ast_function_definition::hir(exec_list
*instructions
,
6320 struct _mesa_glsl_parse_state
*state
)
6322 prototype
->is_definition
= true;
6323 prototype
->hir(instructions
, state
);
6325 ir_function_signature
*signature
= prototype
->signature
;
6326 if (signature
== NULL
)
6329 assert(state
->current_function
== NULL
);
6330 state
->current_function
= signature
;
6331 state
->found_return
= false;
6332 state
->found_begin_interlock
= false;
6333 state
->found_end_interlock
= false;
6335 /* Duplicate parameters declared in the prototype as concrete variables.
6336 * Add these to the symbol table.
6338 state
->symbols
->push_scope();
6339 foreach_in_list(ir_variable
, var
, &signature
->parameters
) {
6340 assert(var
->as_variable() != NULL
);
6342 /* The only way a parameter would "exist" is if two parameters have
6345 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
6346 YYLTYPE loc
= this->get_location();
6348 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
6350 state
->symbols
->add_variable(var
);
6354 /* Convert the body of the function to HIR. */
6355 this->body
->hir(&signature
->body
, state
);
6356 signature
->is_defined
= true;
6358 state
->symbols
->pop_scope();
6360 assert(state
->current_function
== signature
);
6361 state
->current_function
= NULL
;
6363 if (!signature
->return_type
->is_void() && !state
->found_return
) {
6364 YYLTYPE loc
= this->get_location();
6365 _mesa_glsl_error(& loc
, state
, "function `%s' has non-void return type "
6366 "%s, but no return statement",
6367 signature
->function_name(),
6368 signature
->return_type
->name
);
6371 /* Function definitions do not have r-values.
6378 ast_jump_statement::hir(exec_list
*instructions
,
6379 struct _mesa_glsl_parse_state
*state
)
6386 assert(state
->current_function
);
6388 if (opt_return_value
) {
6389 ir_rvalue
*ret
= opt_return_value
->hir(instructions
, state
);
6391 /* The value of the return type can be NULL if the shader says
6392 * 'return foo();' and foo() is a function that returns void.
6394 * NOTE: The GLSL spec doesn't say that this is an error. The type
6395 * of the return value is void. If the return type of the function is
6396 * also void, then this should compile without error. Seriously.
6398 const glsl_type
*const ret_type
=
6399 (ret
== NULL
) ? glsl_type::void_type
: ret
->type
;
6401 /* Implicit conversions are not allowed for return values prior to
6402 * ARB_shading_language_420pack.
6404 if (state
->current_function
->return_type
!= ret_type
) {
6405 YYLTYPE loc
= this->get_location();
6407 if (state
->has_420pack()) {
6408 if (!apply_implicit_conversion(state
->current_function
->return_type
,
6410 || (ret
->type
!= state
->current_function
->return_type
)) {
6411 _mesa_glsl_error(& loc
, state
,
6412 "could not implicitly convert return value "
6413 "to %s, in function `%s'",
6414 state
->current_function
->return_type
->name
,
6415 state
->current_function
->function_name());
6418 _mesa_glsl_error(& loc
, state
,
6419 "`return' with wrong type %s, in function `%s' "
6422 state
->current_function
->function_name(),
6423 state
->current_function
->return_type
->name
);
6425 } else if (state
->current_function
->return_type
->base_type
==
6427 YYLTYPE loc
= this->get_location();
6429 /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
6430 * specs add a clarification:
6432 * "A void function can only use return without a return argument, even if
6433 * the return argument has void type. Return statements only accept values:
6436 * void func2() { return func1(); } // illegal return statement"
6438 _mesa_glsl_error(& loc
, state
,
6439 "void functions can only use `return' without a "
6443 inst
= new(ctx
) ir_return(ret
);
6445 if (state
->current_function
->return_type
->base_type
!=
6447 YYLTYPE loc
= this->get_location();
6449 _mesa_glsl_error(& loc
, state
,
6450 "`return' with no value, in function %s returning "
6452 state
->current_function
->function_name());
6454 inst
= new(ctx
) ir_return
;
6457 state
->found_return
= true;
6458 instructions
->push_tail(inst
);
6463 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
6464 YYLTYPE loc
= this->get_location();
6466 _mesa_glsl_error(& loc
, state
,
6467 "`discard' may only appear in a fragment shader");
6469 instructions
->push_tail(new(ctx
) ir_discard
);
6474 if (mode
== ast_continue
&&
6475 state
->loop_nesting_ast
== NULL
) {
6476 YYLTYPE loc
= this->get_location();
6478 _mesa_glsl_error(& loc
, state
, "continue may only appear in a loop");
6479 } else if (mode
== ast_break
&&
6480 state
->loop_nesting_ast
== NULL
&&
6481 state
->switch_state
.switch_nesting_ast
== NULL
) {
6482 YYLTYPE loc
= this->get_location();
6484 _mesa_glsl_error(& loc
, state
,
6485 "break may only appear in a loop or a switch");
6487 /* For a loop, inline the for loop expression again, since we don't
6488 * know where near the end of the loop body the normal copy of it is
6489 * going to be placed. Same goes for the condition for a do-while
6492 if (state
->loop_nesting_ast
!= NULL
&&
6493 mode
== ast_continue
&& !state
->switch_state
.is_switch_innermost
) {
6494 if (state
->loop_nesting_ast
->rest_expression
) {
6495 state
->loop_nesting_ast
->rest_expression
->hir(instructions
,
6498 if (state
->loop_nesting_ast
->mode
==
6499 ast_iteration_statement::ast_do_while
) {
6500 state
->loop_nesting_ast
->condition_to_hir(instructions
, state
);
6504 if (state
->switch_state
.is_switch_innermost
&&
6505 mode
== ast_continue
) {
6506 /* Set 'continue_inside' to true. */
6507 ir_rvalue
*const true_val
= new (ctx
) ir_constant(true);
6508 ir_dereference_variable
*deref_continue_inside_var
=
6509 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
6510 instructions
->push_tail(new(ctx
) ir_assignment(deref_continue_inside_var
,
6513 /* Break out from the switch, continue for the loop will
6514 * be called right after switch. */
6515 ir_loop_jump
*const jump
=
6516 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
6517 instructions
->push_tail(jump
);
6519 } else if (state
->switch_state
.is_switch_innermost
&&
6520 mode
== ast_break
) {
6521 /* Force break out of switch by inserting a break. */
6522 ir_loop_jump
*const jump
=
6523 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
6524 instructions
->push_tail(jump
);
6526 ir_loop_jump
*const jump
=
6527 new(ctx
) ir_loop_jump((mode
== ast_break
)
6528 ? ir_loop_jump::jump_break
6529 : ir_loop_jump::jump_continue
);
6530 instructions
->push_tail(jump
);
6537 /* Jump instructions do not have r-values.
6544 ast_demote_statement::hir(exec_list
*instructions
,
6545 struct _mesa_glsl_parse_state
*state
)
6549 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
6550 YYLTYPE loc
= this->get_location();
6552 _mesa_glsl_error(& loc
, state
,
6553 "`demote' may only appear in a fragment shader");
6556 instructions
->push_tail(new(ctx
) ir_demote
);
6563 ast_selection_statement::hir(exec_list
*instructions
,
6564 struct _mesa_glsl_parse_state
*state
)
6568 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
6570 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
6572 * "Any expression whose type evaluates to a Boolean can be used as the
6573 * conditional expression bool-expression. Vector types are not accepted
6574 * as the expression to if."
6576 * The checks are separated so that higher quality diagnostics can be
6577 * generated for cases where both rules are violated.
6579 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
6580 YYLTYPE loc
= this->condition
->get_location();
6582 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
6586 ir_if
*const stmt
= new(ctx
) ir_if(condition
);
6588 if (then_statement
!= NULL
) {
6589 state
->symbols
->push_scope();
6590 then_statement
->hir(& stmt
->then_instructions
, state
);
6591 state
->symbols
->pop_scope();
6594 if (else_statement
!= NULL
) {
6595 state
->symbols
->push_scope();
6596 else_statement
->hir(& stmt
->else_instructions
, state
);
6597 state
->symbols
->pop_scope();
6600 instructions
->push_tail(stmt
);
6602 /* if-statements do not have r-values.
6609 /** Value of the case label. */
6612 /** Does this label occur after the default? */
6616 * AST for the case label.
6618 * This is only used to generate error messages for duplicate labels.
6620 ast_expression
*ast
;
6623 /* Used for detection of duplicate case values, compare
6624 * given contents directly.
6627 compare_case_value(const void *a
, const void *b
)
6629 return ((struct case_label
*) a
)->value
== ((struct case_label
*) b
)->value
;
6633 /* Used for detection of duplicate case values, just
6634 * returns key contents as is.
6637 key_contents(const void *key
)
6639 return ((struct case_label
*) key
)->value
;
6644 ast_switch_statement::hir(exec_list
*instructions
,
6645 struct _mesa_glsl_parse_state
*state
)
6649 ir_rvalue
*const test_expression
=
6650 this->test_expression
->hir(instructions
, state
);
6652 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
6654 * "The type of init-expression in a switch statement must be a
6657 if (!test_expression
->type
->is_scalar() ||
6658 !test_expression
->type
->is_integer_32()) {
6659 YYLTYPE loc
= this->test_expression
->get_location();
6661 _mesa_glsl_error(& loc
,
6663 "switch-statement expression must be scalar "
6668 /* Track the switch-statement nesting in a stack-like manner.
6670 struct glsl_switch_state saved
= state
->switch_state
;
6672 state
->switch_state
.is_switch_innermost
= true;
6673 state
->switch_state
.switch_nesting_ast
= this;
6674 state
->switch_state
.labels_ht
=
6675 _mesa_hash_table_create(NULL
, key_contents
,
6676 compare_case_value
);
6677 state
->switch_state
.previous_default
= NULL
;
6679 /* Initalize is_fallthru state to false.
6681 ir_rvalue
*const is_fallthru_val
= new (ctx
) ir_constant(false);
6682 state
->switch_state
.is_fallthru_var
=
6683 new(ctx
) ir_variable(glsl_type::bool_type
,
6684 "switch_is_fallthru_tmp",
6686 instructions
->push_tail(state
->switch_state
.is_fallthru_var
);
6688 ir_dereference_variable
*deref_is_fallthru_var
=
6689 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
6690 instructions
->push_tail(new(ctx
) ir_assignment(deref_is_fallthru_var
,
6693 /* Initialize continue_inside state to false.
6695 state
->switch_state
.continue_inside
=
6696 new(ctx
) ir_variable(glsl_type::bool_type
,
6697 "continue_inside_tmp",
6699 instructions
->push_tail(state
->switch_state
.continue_inside
);
6701 ir_rvalue
*const false_val
= new (ctx
) ir_constant(false);
6702 ir_dereference_variable
*deref_continue_inside_var
=
6703 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
6704 instructions
->push_tail(new(ctx
) ir_assignment(deref_continue_inside_var
,
6707 state
->switch_state
.run_default
=
6708 new(ctx
) ir_variable(glsl_type::bool_type
,
6711 instructions
->push_tail(state
->switch_state
.run_default
);
6713 /* Loop around the switch is used for flow control. */
6714 ir_loop
* loop
= new(ctx
) ir_loop();
6715 instructions
->push_tail(loop
);
6717 /* Cache test expression.
6719 test_to_hir(&loop
->body_instructions
, state
);
6721 /* Emit code for body of switch stmt.
6723 body
->hir(&loop
->body_instructions
, state
);
6725 /* Insert a break at the end to exit loop. */
6726 ir_loop_jump
*jump
= new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
6727 loop
->body_instructions
.push_tail(jump
);
6729 /* If we are inside loop, check if continue got called inside switch. */
6730 if (state
->loop_nesting_ast
!= NULL
) {
6731 ir_dereference_variable
*deref_continue_inside
=
6732 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
6733 ir_if
*irif
= new(ctx
) ir_if(deref_continue_inside
);
6734 ir_loop_jump
*jump
= new(ctx
) ir_loop_jump(ir_loop_jump::jump_continue
);
6736 if (state
->loop_nesting_ast
!= NULL
) {
6737 if (state
->loop_nesting_ast
->rest_expression
) {
6738 state
->loop_nesting_ast
->rest_expression
->hir(&irif
->then_instructions
,
6741 if (state
->loop_nesting_ast
->mode
==
6742 ast_iteration_statement::ast_do_while
) {
6743 state
->loop_nesting_ast
->condition_to_hir(&irif
->then_instructions
, state
);
6746 irif
->then_instructions
.push_tail(jump
);
6747 instructions
->push_tail(irif
);
6750 _mesa_hash_table_destroy(state
->switch_state
.labels_ht
, NULL
);
6752 state
->switch_state
= saved
;
6754 /* Switch statements do not have r-values. */
6760 ast_switch_statement::test_to_hir(exec_list
*instructions
,
6761 struct _mesa_glsl_parse_state
*state
)
6765 /* set to true to avoid a duplicate "use of uninitialized variable" warning
6766 * on the switch test case. The first one would be already raised when
6767 * getting the test_expression at ast_switch_statement::hir
6769 test_expression
->set_is_lhs(true);
6770 /* Cache value of test expression. */
6771 ir_rvalue
*const test_val
= test_expression
->hir(instructions
, state
);
6773 state
->switch_state
.test_var
= new(ctx
) ir_variable(test_val
->type
,
6776 ir_dereference_variable
*deref_test_var
=
6777 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
6779 instructions
->push_tail(state
->switch_state
.test_var
);
6780 instructions
->push_tail(new(ctx
) ir_assignment(deref_test_var
, test_val
));
6785 ast_switch_body::hir(exec_list
*instructions
,
6786 struct _mesa_glsl_parse_state
*state
)
6789 stmts
->hir(instructions
, state
);
6791 /* Switch bodies do not have r-values. */
6796 ast_case_statement_list::hir(exec_list
*instructions
,
6797 struct _mesa_glsl_parse_state
*state
)
6799 exec_list default_case
, after_default
, tmp
;
6801 foreach_list_typed (ast_case_statement
, case_stmt
, link
, & this->cases
) {
6802 case_stmt
->hir(&tmp
, state
);
6805 if (state
->switch_state
.previous_default
&& default_case
.is_empty()) {
6806 default_case
.append_list(&tmp
);
6810 /* If default case found, append 'after_default' list. */
6811 if (!default_case
.is_empty())
6812 after_default
.append_list(&tmp
);
6814 instructions
->append_list(&tmp
);
6817 /* Handle the default case. This is done here because default might not be
6818 * the last case. We need to add checks against following cases first to see
6819 * if default should be chosen or not.
6821 if (!default_case
.is_empty()) {
6822 ir_factory
body(instructions
, state
);
6824 ir_expression
*cmp
= NULL
;
6826 hash_table_foreach(state
->switch_state
.labels_ht
, entry
) {
6827 const struct case_label
*const l
= (struct case_label
*) entry
->data
;
6829 /* If the switch init-value is the value of one of the labels that
6830 * occurs after the default case, disable execution of the default
6833 if (l
->after_default
) {
6834 ir_constant
*const cnst
=
6835 state
->switch_state
.test_var
->type
->base_type
== GLSL_TYPE_UINT
6836 ? body
.constant(unsigned(l
->value
))
6837 : body
.constant(int(l
->value
));
6840 ? equal(cnst
, state
->switch_state
.test_var
)
6841 : logic_or(cmp
, equal(cnst
, state
->switch_state
.test_var
));
6846 body
.emit(assign(state
->switch_state
.run_default
, logic_not(cmp
)));
6848 body
.emit(assign(state
->switch_state
.run_default
, body
.constant(true)));
6850 /* Append default case and all cases after it. */
6851 instructions
->append_list(&default_case
);
6852 instructions
->append_list(&after_default
);
6855 /* Case statements do not have r-values. */
6860 ast_case_statement::hir(exec_list
*instructions
,
6861 struct _mesa_glsl_parse_state
*state
)
6863 labels
->hir(instructions
, state
);
6865 /* Guard case statements depending on fallthru state. */
6866 ir_dereference_variable
*const deref_fallthru_guard
=
6867 new(state
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
6868 ir_if
*const test_fallthru
= new(state
) ir_if(deref_fallthru_guard
);
6870 foreach_list_typed (ast_node
, stmt
, link
, & this->stmts
)
6871 stmt
->hir(& test_fallthru
->then_instructions
, state
);
6873 instructions
->push_tail(test_fallthru
);
6875 /* Case statements do not have r-values. */
6881 ast_case_label_list::hir(exec_list
*instructions
,
6882 struct _mesa_glsl_parse_state
*state
)
6884 foreach_list_typed (ast_case_label
, label
, link
, & this->labels
)
6885 label
->hir(instructions
, state
);
6887 /* Case labels do not have r-values. */
6892 ast_case_label::hir(exec_list
*instructions
,
6893 struct _mesa_glsl_parse_state
*state
)
6895 ir_factory
body(instructions
, state
);
6897 ir_variable
*const fallthru_var
= state
->switch_state
.is_fallthru_var
;
6899 /* If not default case, ... */
6900 if (this->test_value
!= NULL
) {
6901 /* Conditionally set fallthru state based on
6902 * comparison of cached test expression value to case label.
6904 ir_rvalue
*const label_rval
= this->test_value
->hir(instructions
, state
);
6905 ir_constant
*label_const
=
6906 label_rval
->constant_expression_value(body
.mem_ctx
);
6909 YYLTYPE loc
= this->test_value
->get_location();
6911 _mesa_glsl_error(& loc
, state
,
6912 "switch statement case label must be a "
6913 "constant expression");
6915 /* Stuff a dummy value in to allow processing to continue. */
6916 label_const
= body
.constant(0);
6919 _mesa_hash_table_search(state
->switch_state
.labels_ht
,
6920 &label_const
->value
.u
[0]);
6923 const struct case_label
*const l
=
6924 (struct case_label
*) entry
->data
;
6925 const ast_expression
*const previous_label
= l
->ast
;
6926 YYLTYPE loc
= this->test_value
->get_location();
6928 _mesa_glsl_error(& loc
, state
, "duplicate case value");
6930 loc
= previous_label
->get_location();
6931 _mesa_glsl_error(& loc
, state
, "this is the previous case label");
6933 struct case_label
*l
= ralloc(state
->switch_state
.labels_ht
,
6936 l
->value
= label_const
->value
.u
[0];
6937 l
->after_default
= state
->switch_state
.previous_default
!= NULL
;
6938 l
->ast
= this->test_value
;
6940 _mesa_hash_table_insert(state
->switch_state
.labels_ht
,
6941 &label_const
->value
.u
[0],
6946 /* Create an r-value version of the ir_constant label here (after we may
6947 * have created a fake one in error cases) that can be passed to
6948 * apply_implicit_conversion below.
6950 ir_rvalue
*label
= label_const
;
6952 ir_rvalue
*deref_test_var
=
6953 new(body
.mem_ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
6956 * From GLSL 4.40 specification section 6.2 ("Selection"):
6958 * "The type of the init-expression value in a switch statement must
6959 * be a scalar int or uint. The type of the constant-expression value
6960 * in a case label also must be a scalar int or uint. When any pair
6961 * of these values is tested for "equal value" and the types do not
6962 * match, an implicit conversion will be done to convert the int to a
6963 * uint (see section 4.1.10 “Implicit Conversions”) before the compare
6966 if (label
->type
!= state
->switch_state
.test_var
->type
) {
6967 YYLTYPE loc
= this->test_value
->get_location();
6969 const glsl_type
*type_a
= label
->type
;
6970 const glsl_type
*type_b
= state
->switch_state
.test_var
->type
;
6972 /* Check if int->uint implicit conversion is supported. */
6973 bool integer_conversion_supported
=
6974 glsl_type::int_type
->can_implicitly_convert_to(glsl_type::uint_type
,
6977 if ((!type_a
->is_integer_32() || !type_b
->is_integer_32()) ||
6978 !integer_conversion_supported
) {
6979 _mesa_glsl_error(&loc
, state
, "type mismatch with switch "
6980 "init-expression and case label (%s != %s)",
6981 type_a
->name
, type_b
->name
);
6983 /* Conversion of the case label. */
6984 if (type_a
->base_type
== GLSL_TYPE_INT
) {
6985 if (!apply_implicit_conversion(glsl_type::uint_type
,
6987 _mesa_glsl_error(&loc
, state
, "implicit type conversion error");
6989 /* Conversion of the init-expression value. */
6990 if (!apply_implicit_conversion(glsl_type::uint_type
,
6991 deref_test_var
, state
))
6992 _mesa_glsl_error(&loc
, state
, "implicit type conversion error");
6996 /* If the implicit conversion was allowed, the types will already be
6997 * the same. If the implicit conversion wasn't allowed, smash the
6998 * type of the label anyway. This will prevent the expression
6999 * constructor (below) from failing an assertion.
7001 label
->type
= deref_test_var
->type
;
7004 body
.emit(assign(fallthru_var
,
7005 logic_or(fallthru_var
, equal(label
, deref_test_var
))));
7006 } else { /* default case */
7007 if (state
->switch_state
.previous_default
) {
7008 YYLTYPE loc
= this->get_location();
7009 _mesa_glsl_error(& loc
, state
,
7010 "multiple default labels in one switch");
7012 loc
= state
->switch_state
.previous_default
->get_location();
7013 _mesa_glsl_error(& loc
, state
, "this is the first default label");
7015 state
->switch_state
.previous_default
= this;
7017 /* Set fallthru condition on 'run_default' bool. */
7018 body
.emit(assign(fallthru_var
,
7019 logic_or(fallthru_var
,
7020 state
->switch_state
.run_default
)));
7023 /* Case statements do not have r-values. */
7028 ast_iteration_statement::condition_to_hir(exec_list
*instructions
,
7029 struct _mesa_glsl_parse_state
*state
)
7033 if (condition
!= NULL
) {
7034 ir_rvalue
*const cond
=
7035 condition
->hir(instructions
, state
);
7038 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
7039 YYLTYPE loc
= condition
->get_location();
7041 _mesa_glsl_error(& loc
, state
,
7042 "loop condition must be scalar boolean");
7044 /* As the first code in the loop body, generate a block that looks
7045 * like 'if (!condition) break;' as the loop termination condition.
7047 ir_rvalue
*const not_cond
=
7048 new(ctx
) ir_expression(ir_unop_logic_not
, cond
);
7050 ir_if
*const if_stmt
= new(ctx
) ir_if(not_cond
);
7052 ir_jump
*const break_stmt
=
7053 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
7055 if_stmt
->then_instructions
.push_tail(break_stmt
);
7056 instructions
->push_tail(if_stmt
);
7063 ast_iteration_statement::hir(exec_list
*instructions
,
7064 struct _mesa_glsl_parse_state
*state
)
7068 /* For-loops and while-loops start a new scope, but do-while loops do not.
7070 if (mode
!= ast_do_while
)
7071 state
->symbols
->push_scope();
7073 if (init_statement
!= NULL
)
7074 init_statement
->hir(instructions
, state
);
7076 ir_loop
*const stmt
= new(ctx
) ir_loop();
7077 instructions
->push_tail(stmt
);
7079 /* Track the current loop nesting. */
7080 ast_iteration_statement
*nesting_ast
= state
->loop_nesting_ast
;
7082 state
->loop_nesting_ast
= this;
7084 /* Likewise, indicate that following code is closest to a loop,
7085 * NOT closest to a switch.
7087 bool saved_is_switch_innermost
= state
->switch_state
.is_switch_innermost
;
7088 state
->switch_state
.is_switch_innermost
= false;
7090 if (mode
!= ast_do_while
)
7091 condition_to_hir(&stmt
->body_instructions
, state
);
7094 body
->hir(& stmt
->body_instructions
, state
);
7096 if (rest_expression
!= NULL
)
7097 rest_expression
->hir(& stmt
->body_instructions
, state
);
7099 if (mode
== ast_do_while
)
7100 condition_to_hir(&stmt
->body_instructions
, state
);
7102 if (mode
!= ast_do_while
)
7103 state
->symbols
->pop_scope();
7105 /* Restore previous nesting before returning. */
7106 state
->loop_nesting_ast
= nesting_ast
;
7107 state
->switch_state
.is_switch_innermost
= saved_is_switch_innermost
;
7109 /* Loops do not have r-values.
7116 * Determine if the given type is valid for establishing a default precision
7119 * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
7121 * "The precision statement
7123 * precision precision-qualifier type;
7125 * can be used to establish a default precision qualifier. The type field
7126 * can be either int or float or any of the sampler types, and the
7127 * precision-qualifier can be lowp, mediump, or highp."
7129 * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
7130 * qualifiers on sampler types, but this seems like an oversight (since the
7131 * intention of including these in GLSL 1.30 is to allow compatibility with ES
7132 * shaders). So we allow int, float, and all sampler types regardless of GLSL
7136 is_valid_default_precision_type(const struct glsl_type
*const type
)
7141 switch (type
->base_type
) {
7143 case GLSL_TYPE_FLOAT
:
7144 /* "int" and "float" are valid, but vectors and matrices are not. */
7145 return type
->vector_elements
== 1 && type
->matrix_columns
== 1;
7146 case GLSL_TYPE_SAMPLER
:
7147 case GLSL_TYPE_IMAGE
:
7148 case GLSL_TYPE_ATOMIC_UINT
:
7157 ast_type_specifier::hir(exec_list
*instructions
,
7158 struct _mesa_glsl_parse_state
*state
)
7160 if (this->default_precision
== ast_precision_none
&& this->structure
== NULL
)
7163 YYLTYPE loc
= this->get_location();
7165 /* If this is a precision statement, check that the type to which it is
7166 * applied is either float or int.
7168 * From section 4.5.3 of the GLSL 1.30 spec:
7169 * "The precision statement
7170 * precision precision-qualifier type;
7171 * can be used to establish a default precision qualifier. The type
7172 * field can be either int or float [...]. Any other types or
7173 * qualifiers will result in an error.
7175 if (this->default_precision
!= ast_precision_none
) {
7176 if (!state
->check_precision_qualifiers_allowed(&loc
))
7179 if (this->structure
!= NULL
) {
7180 _mesa_glsl_error(&loc
, state
,
7181 "precision qualifiers do not apply to structures");
7185 if (this->array_specifier
!= NULL
) {
7186 _mesa_glsl_error(&loc
, state
,
7187 "default precision statements do not apply to "
7192 const struct glsl_type
*const type
=
7193 state
->symbols
->get_type(this->type_name
);
7194 if (!is_valid_default_precision_type(type
)) {
7195 _mesa_glsl_error(&loc
, state
,
7196 "default precision statements apply only to "
7197 "float, int, and opaque types");
7201 if (state
->es_shader
) {
7202 /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
7205 * "Non-precision qualified declarations will use the precision
7206 * qualifier specified in the most recent precision statement
7207 * that is still in scope. The precision statement has the same
7208 * scoping rules as variable declarations. If it is declared
7209 * inside a compound statement, its effect stops at the end of
7210 * the innermost statement it was declared in. Precision
7211 * statements in nested scopes override precision statements in
7212 * outer scopes. Multiple precision statements for the same basic
7213 * type can appear inside the same scope, with later statements
7214 * overriding earlier statements within that scope."
7216 * Default precision specifications follow the same scope rules as
7217 * variables. So, we can track the state of the default precision
7218 * qualifiers in the symbol table, and the rules will just work. This
7219 * is a slight abuse of the symbol table, but it has the semantics
7222 state
->symbols
->add_default_precision_qualifier(this->type_name
,
7223 this->default_precision
);
7226 /* FINISHME: Translate precision statements into IR. */
7230 /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
7231 * process_record_constructor() can do type-checking on C-style initializer
7232 * expressions of structs, but ast_struct_specifier should only be translated
7233 * to HIR if it is declaring the type of a structure.
7235 * The ->is_declaration field is false for initializers of variables
7236 * declared separately from the struct's type definition.
7238 * struct S { ... }; (is_declaration = true)
7239 * struct T { ... } t = { ... }; (is_declaration = true)
7240 * S s = { ... }; (is_declaration = false)
7242 if (this->structure
!= NULL
&& this->structure
->is_declaration
)
7243 return this->structure
->hir(instructions
, state
);
7250 * Process a structure or interface block tree into an array of structure fields
7252 * After parsing, where there are some syntax differnces, structures and
7253 * interface blocks are almost identical. They are similar enough that the
7254 * AST for each can be processed the same way into a set of
7255 * \c glsl_struct_field to describe the members.
7257 * If we're processing an interface block, var_mode should be the type of the
7258 * interface block (ir_var_shader_in, ir_var_shader_out, ir_var_uniform or
7259 * ir_var_shader_storage). If we're processing a structure, var_mode should be
7263 * The number of fields processed. A pointer to the array structure fields is
7264 * stored in \c *fields_ret.
7267 ast_process_struct_or_iface_block_members(exec_list
*instructions
,
7268 struct _mesa_glsl_parse_state
*state
,
7269 exec_list
*declarations
,
7270 glsl_struct_field
**fields_ret
,
7272 enum glsl_matrix_layout matrix_layout
,
7273 bool allow_reserved_names
,
7274 ir_variable_mode var_mode
,
7275 ast_type_qualifier
*layout
,
7276 unsigned block_stream
,
7277 unsigned block_xfb_buffer
,
7278 unsigned block_xfb_offset
,
7279 unsigned expl_location
,
7280 unsigned expl_align
)
7282 unsigned decl_count
= 0;
7283 unsigned next_offset
= 0;
7285 /* Make an initial pass over the list of fields to determine how
7286 * many there are. Each element in this list is an ast_declarator_list.
7287 * This means that we actually need to count the number of elements in the
7288 * 'declarations' list in each of the elements.
7290 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
7291 decl_count
+= decl_list
->declarations
.length();
7294 /* Allocate storage for the fields and process the field
7295 * declarations. As the declarations are processed, try to also convert
7296 * the types to HIR. This ensures that structure definitions embedded in
7297 * other structure definitions or in interface blocks are processed.
7299 glsl_struct_field
*const fields
= rzalloc_array(state
, glsl_struct_field
,
7302 bool first_member
= true;
7303 bool first_member_has_explicit_location
= false;
7306 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
7307 const char *type_name
;
7308 YYLTYPE loc
= decl_list
->get_location();
7310 decl_list
->type
->specifier
->hir(instructions
, state
);
7312 /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
7314 * "Anonymous structures are not supported; so embedded structures
7315 * must have a declarator. A name given to an embedded struct is
7316 * scoped at the same level as the struct it is embedded in."
7318 * The same section of the GLSL 1.20 spec says:
7320 * "Anonymous structures are not supported. Embedded structures are
7323 * The GLSL ES 1.00 and 3.00 specs have similar langauge. So, we allow
7324 * embedded structures in 1.10 only.
7326 if (state
->language_version
!= 110 &&
7327 decl_list
->type
->specifier
->structure
!= NULL
)
7328 _mesa_glsl_error(&loc
, state
,
7329 "embedded structure declarations are not allowed");
7331 const glsl_type
*decl_type
=
7332 decl_list
->type
->glsl_type(& type_name
, state
);
7334 const struct ast_type_qualifier
*const qual
=
7335 &decl_list
->type
->qualifier
;
7337 /* From section 4.3.9 of the GLSL 4.40 spec:
7339 * "[In interface blocks] opaque types are not allowed."
7341 * It should be impossible for decl_type to be NULL here. Cases that
7342 * might naturally lead to decl_type being NULL, especially for the
7343 * is_interface case, will have resulted in compilation having
7344 * already halted due to a syntax error.
7349 /* From section 4.3.7 of the ARB_bindless_texture spec:
7351 * "(remove the following bullet from the last list on p. 39,
7352 * thereby permitting sampler types in interface blocks; image
7353 * types are also permitted in blocks by this extension)"
7355 * * sampler types are not allowed
7357 if (decl_type
->contains_atomic() ||
7358 (!state
->has_bindless() && decl_type
->contains_opaque())) {
7359 _mesa_glsl_error(&loc
, state
, "uniform/buffer in non-default "
7360 "interface block contains %s variable",
7361 state
->has_bindless() ? "atomic" : "opaque");
7364 if (decl_type
->contains_atomic()) {
7365 /* From section 4.1.7.3 of the GLSL 4.40 spec:
7367 * "Members of structures cannot be declared as atomic counter
7370 _mesa_glsl_error(&loc
, state
, "atomic counter in structure");
7373 if (!state
->has_bindless() && decl_type
->contains_image()) {
7374 /* FINISHME: Same problem as with atomic counters.
7375 * FINISHME: Request clarification from Khronos and add
7376 * FINISHME: spec quotation here.
7378 _mesa_glsl_error(&loc
, state
, "image in structure");
7382 if (qual
->flags
.q
.explicit_binding
) {
7383 _mesa_glsl_error(&loc
, state
,
7384 "binding layout qualifier cannot be applied "
7385 "to struct or interface block members");
7389 if (!first_member
) {
7390 if (!layout
->flags
.q
.explicit_location
&&
7391 ((first_member_has_explicit_location
&&
7392 !qual
->flags
.q
.explicit_location
) ||
7393 (!first_member_has_explicit_location
&&
7394 qual
->flags
.q
.explicit_location
))) {
7395 _mesa_glsl_error(&loc
, state
,
7396 "when block-level location layout qualifier "
7397 "is not supplied either all members must "
7398 "have a location layout qualifier or all "
7399 "members must not have a location layout "
7403 first_member
= false;
7404 first_member_has_explicit_location
=
7405 qual
->flags
.q
.explicit_location
;
7409 if (qual
->flags
.q
.std140
||
7410 qual
->flags
.q
.std430
||
7411 qual
->flags
.q
.packed
||
7412 qual
->flags
.q
.shared
) {
7413 _mesa_glsl_error(&loc
, state
,
7414 "uniform/shader storage block layout qualifiers "
7415 "std140, std430, packed, and shared can only be "
7416 "applied to uniform/shader storage blocks, not "
7420 if (qual
->flags
.q
.constant
) {
7421 _mesa_glsl_error(&loc
, state
,
7422 "const storage qualifier cannot be applied "
7423 "to struct or interface block members");
7426 validate_memory_qualifier_for_type(state
, &loc
, qual
, decl_type
);
7427 validate_image_format_qualifier_for_type(state
, &loc
, qual
, decl_type
);
7429 /* From Section 4.4.2.3 (Geometry Outputs) of the GLSL 4.50 spec:
7431 * "A block member may be declared with a stream identifier, but
7432 * the specified stream must match the stream associated with the
7433 * containing block."
7435 if (qual
->flags
.q
.explicit_stream
) {
7436 unsigned qual_stream
;
7437 if (process_qualifier_constant(state
, &loc
, "stream",
7438 qual
->stream
, &qual_stream
) &&
7439 qual_stream
!= block_stream
) {
7440 _mesa_glsl_error(&loc
, state
, "stream layout qualifier on "
7441 "interface block member does not match "
7442 "the interface block (%u vs %u)", qual_stream
,
7448 unsigned explicit_xfb_buffer
= 0;
7449 if (qual
->flags
.q
.explicit_xfb_buffer
) {
7450 unsigned qual_xfb_buffer
;
7451 if (process_qualifier_constant(state
, &loc
, "xfb_buffer",
7452 qual
->xfb_buffer
, &qual_xfb_buffer
)) {
7453 explicit_xfb_buffer
= 1;
7454 if (qual_xfb_buffer
!= block_xfb_buffer
)
7455 _mesa_glsl_error(&loc
, state
, "xfb_buffer layout qualifier on "
7456 "interface block member does not match "
7457 "the interface block (%u vs %u)",
7458 qual_xfb_buffer
, block_xfb_buffer
);
7460 xfb_buffer
= (int) qual_xfb_buffer
;
7463 explicit_xfb_buffer
= layout
->flags
.q
.explicit_xfb_buffer
;
7464 xfb_buffer
= (int) block_xfb_buffer
;
7467 int xfb_stride
= -1;
7468 if (qual
->flags
.q
.explicit_xfb_stride
) {
7469 unsigned qual_xfb_stride
;
7470 if (process_qualifier_constant(state
, &loc
, "xfb_stride",
7471 qual
->xfb_stride
, &qual_xfb_stride
)) {
7472 xfb_stride
= (int) qual_xfb_stride
;
7476 if (qual
->flags
.q
.uniform
&& qual
->has_interpolation()) {
7477 _mesa_glsl_error(&loc
, state
,
7478 "interpolation qualifiers cannot be used "
7479 "with uniform interface blocks");
7482 if ((qual
->flags
.q
.uniform
|| !is_interface
) &&
7483 qual
->has_auxiliary_storage()) {
7484 _mesa_glsl_error(&loc
, state
,
7485 "auxiliary storage qualifiers cannot be used "
7486 "in uniform blocks or structures.");
7489 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
7490 if (!qual
->flags
.q
.uniform
&& !qual
->flags
.q
.buffer
) {
7491 _mesa_glsl_error(&loc
, state
,
7492 "row_major and column_major can only be "
7493 "applied to interface blocks");
7495 validate_matrix_layout_for_type(state
, &loc
, decl_type
, NULL
);
7498 foreach_list_typed (ast_declaration
, decl
, link
,
7499 &decl_list
->declarations
) {
7500 YYLTYPE loc
= decl
->get_location();
7502 if (!allow_reserved_names
)
7503 validate_identifier(decl
->identifier
, loc
, state
);
7505 const struct glsl_type
*field_type
=
7506 process_array_type(&loc
, decl_type
, decl
->array_specifier
, state
);
7507 validate_array_dimensions(field_type
, state
, &loc
);
7508 fields
[i
].type
= field_type
;
7509 fields
[i
].name
= decl
->identifier
;
7510 fields
[i
].interpolation
=
7511 interpret_interpolation_qualifier(qual
, field_type
,
7512 var_mode
, state
, &loc
);
7513 fields
[i
].centroid
= qual
->flags
.q
.centroid
? 1 : 0;
7514 fields
[i
].sample
= qual
->flags
.q
.sample
? 1 : 0;
7515 fields
[i
].patch
= qual
->flags
.q
.patch
? 1 : 0;
7516 fields
[i
].offset
= -1;
7517 fields
[i
].explicit_xfb_buffer
= explicit_xfb_buffer
;
7518 fields
[i
].xfb_buffer
= xfb_buffer
;
7519 fields
[i
].xfb_stride
= xfb_stride
;
7521 if (qual
->flags
.q
.explicit_location
) {
7522 unsigned qual_location
;
7523 if (process_qualifier_constant(state
, &loc
, "location",
7524 qual
->location
, &qual_location
)) {
7525 fields
[i
].location
= qual_location
+
7526 (fields
[i
].patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
);
7527 expl_location
= fields
[i
].location
+
7528 fields
[i
].type
->count_attribute_slots(false);
7531 if (layout
&& layout
->flags
.q
.explicit_location
) {
7532 fields
[i
].location
= expl_location
;
7533 expl_location
+= fields
[i
].type
->count_attribute_slots(false);
7535 fields
[i
].location
= -1;
7539 /* Offset can only be used with std430 and std140 layouts an initial
7540 * value of 0 is used for error detection.
7546 if (qual
->flags
.q
.row_major
||
7547 matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
7553 if(layout
->flags
.q
.std140
) {
7554 align
= field_type
->std140_base_alignment(row_major
);
7555 size
= field_type
->std140_size(row_major
);
7556 } else if (layout
->flags
.q
.std430
) {
7557 align
= field_type
->std430_base_alignment(row_major
);
7558 size
= field_type
->std430_size(row_major
);
7562 if (qual
->flags
.q
.explicit_offset
) {
7563 unsigned qual_offset
;
7564 if (process_qualifier_constant(state
, &loc
, "offset",
7565 qual
->offset
, &qual_offset
)) {
7566 if (align
!= 0 && size
!= 0) {
7567 if (next_offset
> qual_offset
)
7568 _mesa_glsl_error(&loc
, state
, "layout qualifier "
7569 "offset overlaps previous member");
7571 if (qual_offset
% align
) {
7572 _mesa_glsl_error(&loc
, state
, "layout qualifier offset "
7573 "must be a multiple of the base "
7574 "alignment of %s", field_type
->name
);
7576 fields
[i
].offset
= qual_offset
;
7577 next_offset
= qual_offset
+ size
;
7579 _mesa_glsl_error(&loc
, state
, "offset can only be used "
7580 "with std430 and std140 layouts");
7585 if (qual
->flags
.q
.explicit_align
|| expl_align
!= 0) {
7586 unsigned offset
= fields
[i
].offset
!= -1 ? fields
[i
].offset
:
7588 if (align
== 0 || size
== 0) {
7589 _mesa_glsl_error(&loc
, state
, "align can only be used with "
7590 "std430 and std140 layouts");
7591 } else if (qual
->flags
.q
.explicit_align
) {
7592 unsigned member_align
;
7593 if (process_qualifier_constant(state
, &loc
, "align",
7594 qual
->align
, &member_align
)) {
7595 if (member_align
== 0 ||
7596 member_align
& (member_align
- 1)) {
7597 _mesa_glsl_error(&loc
, state
, "align layout qualifier "
7598 "is not a power of 2");
7600 fields
[i
].offset
= glsl_align(offset
, member_align
);
7601 next_offset
= fields
[i
].offset
+ size
;
7605 fields
[i
].offset
= glsl_align(offset
, expl_align
);
7606 next_offset
= fields
[i
].offset
+ size
;
7608 } else if (!qual
->flags
.q
.explicit_offset
) {
7609 if (align
!= 0 && size
!= 0)
7610 next_offset
= glsl_align(next_offset
, align
) + size
;
7613 /* From the ARB_enhanced_layouts spec:
7615 * "The given offset applies to the first component of the first
7616 * member of the qualified entity. Then, within the qualified
7617 * entity, subsequent components are each assigned, in order, to
7618 * the next available offset aligned to a multiple of that
7619 * component's size. Aggregate types are flattened down to the
7620 * component level to get this sequence of components."
7622 if (qual
->flags
.q
.explicit_xfb_offset
) {
7623 unsigned xfb_offset
;
7624 if (process_qualifier_constant(state
, &loc
, "xfb_offset",
7625 qual
->offset
, &xfb_offset
)) {
7626 fields
[i
].offset
= xfb_offset
;
7627 block_xfb_offset
= fields
[i
].offset
+
7628 4 * field_type
->component_slots();
7631 if (layout
&& layout
->flags
.q
.explicit_xfb_offset
) {
7632 unsigned align
= field_type
->is_64bit() ? 8 : 4;
7633 fields
[i
].offset
= glsl_align(block_xfb_offset
, align
);
7634 block_xfb_offset
+= 4 * field_type
->component_slots();
7638 /* Propogate row- / column-major information down the fields of the
7639 * structure or interface block. Structures need this data because
7640 * the structure may contain a structure that contains ... a matrix
7641 * that need the proper layout.
7643 if (is_interface
&& layout
&&
7644 (layout
->flags
.q
.uniform
|| layout
->flags
.q
.buffer
) &&
7645 (field_type
->without_array()->is_matrix()
7646 || field_type
->without_array()->is_struct())) {
7647 /* If no layout is specified for the field, inherit the layout
7650 fields
[i
].matrix_layout
= matrix_layout
;
7652 if (qual
->flags
.q
.row_major
)
7653 fields
[i
].matrix_layout
= GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
7654 else if (qual
->flags
.q
.column_major
)
7655 fields
[i
].matrix_layout
= GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
;
7657 /* If we're processing an uniform or buffer block, the matrix
7658 * layout must be decided by this point.
7660 assert(fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
7661 || fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
);
7664 /* Memory qualifiers are allowed on buffer and image variables, while
7665 * the format qualifier is only accepted for images.
7667 if (var_mode
== ir_var_shader_storage
||
7668 field_type
->without_array()->is_image()) {
7669 /* For readonly and writeonly qualifiers the field definition,
7670 * if set, overwrites the layout qualifier.
7672 if (qual
->flags
.q
.read_only
|| qual
->flags
.q
.write_only
) {
7673 fields
[i
].memory_read_only
= qual
->flags
.q
.read_only
;
7674 fields
[i
].memory_write_only
= qual
->flags
.q
.write_only
;
7676 fields
[i
].memory_read_only
=
7677 layout
? layout
->flags
.q
.read_only
: 0;
7678 fields
[i
].memory_write_only
=
7679 layout
? layout
->flags
.q
.write_only
: 0;
7682 /* For other qualifiers, we set the flag if either the layout
7683 * qualifier or the field qualifier are set
7685 fields
[i
].memory_coherent
= qual
->flags
.q
.coherent
||
7686 (layout
&& layout
->flags
.q
.coherent
);
7687 fields
[i
].memory_volatile
= qual
->flags
.q
._volatile
||
7688 (layout
&& layout
->flags
.q
._volatile
);
7689 fields
[i
].memory_restrict
= qual
->flags
.q
.restrict_flag
||
7690 (layout
&& layout
->flags
.q
.restrict_flag
);
7692 if (field_type
->without_array()->is_image()) {
7693 if (qual
->flags
.q
.explicit_image_format
) {
7694 if (qual
->image_base_type
!=
7695 field_type
->without_array()->sampled_type
) {
7696 _mesa_glsl_error(&loc
, state
, "format qualifier doesn't "
7697 "match the base data type of the image");
7700 fields
[i
].image_format
= qual
->image_format
;
7702 if (!qual
->flags
.q
.write_only
) {
7703 _mesa_glsl_error(&loc
, state
, "image not qualified with "
7704 "`writeonly' must have a format layout "
7708 fields
[i
].image_format
= PIPE_FORMAT_NONE
;
7713 /* Precision qualifiers do not hold any meaning in Desktop GLSL */
7714 if (state
->es_shader
) {
7715 fields
[i
].precision
= select_gles_precision(qual
->precision
,
7720 fields
[i
].precision
= qual
->precision
;
7727 assert(i
== decl_count
);
7729 *fields_ret
= fields
;
7735 ast_struct_specifier::hir(exec_list
*instructions
,
7736 struct _mesa_glsl_parse_state
*state
)
7738 YYLTYPE loc
= this->get_location();
7740 unsigned expl_location
= 0;
7741 if (layout
&& layout
->flags
.q
.explicit_location
) {
7742 if (!process_qualifier_constant(state
, &loc
, "location",
7743 layout
->location
, &expl_location
)) {
7746 expl_location
= VARYING_SLOT_VAR0
+ expl_location
;
7750 glsl_struct_field
*fields
;
7751 unsigned decl_count
=
7752 ast_process_struct_or_iface_block_members(instructions
,
7754 &this->declarations
,
7757 GLSL_MATRIX_LAYOUT_INHERITED
,
7758 false /* allow_reserved_names */,
7761 0, /* for interface only */
7762 0, /* for interface only */
7763 0, /* for interface only */
7765 0 /* for interface only */);
7767 validate_identifier(this->name
, loc
, state
);
7769 type
= glsl_type::get_struct_instance(fields
, decl_count
, this->name
);
7771 if (!type
->is_anonymous() && !state
->symbols
->add_type(name
, type
)) {
7772 const glsl_type
*match
= state
->symbols
->get_type(name
);
7773 /* allow struct matching for desktop GL - older UE4 does this */
7774 if (match
!= NULL
&& state
->is_version(130, 0) && match
->record_compare(type
, true, false))
7775 _mesa_glsl_warning(& loc
, state
, "struct `%s' previously defined", name
);
7777 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
7779 const glsl_type
**s
= reralloc(state
, state
->user_structures
,
7781 state
->num_user_structures
+ 1);
7783 s
[state
->num_user_structures
] = type
;
7784 state
->user_structures
= s
;
7785 state
->num_user_structures
++;
7789 /* Structure type definitions do not have r-values.
7796 * Visitor class which detects whether a given interface block has been used.
7798 class interface_block_usage_visitor
: public ir_hierarchical_visitor
7801 interface_block_usage_visitor(ir_variable_mode mode
, const glsl_type
*block
)
7802 : mode(mode
), block(block
), found(false)
7806 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
7808 if (ir
->var
->data
.mode
== mode
&& ir
->var
->get_interface_type() == block
) {
7812 return visit_continue
;
7815 bool usage_found() const
7821 ir_variable_mode mode
;
7822 const glsl_type
*block
;
7827 is_unsized_array_last_element(ir_variable
*v
)
7829 const glsl_type
*interface_type
= v
->get_interface_type();
7830 int length
= interface_type
->length
;
7832 assert(v
->type
->is_unsized_array());
7834 /* Check if it is the last element of the interface */
7835 if (strcmp(interface_type
->fields
.structure
[length
-1].name
, v
->name
) == 0)
7841 apply_memory_qualifiers(ir_variable
*var
, glsl_struct_field field
)
7843 var
->data
.memory_read_only
= field
.memory_read_only
;
7844 var
->data
.memory_write_only
= field
.memory_write_only
;
7845 var
->data
.memory_coherent
= field
.memory_coherent
;
7846 var
->data
.memory_volatile
= field
.memory_volatile
;
7847 var
->data
.memory_restrict
= field
.memory_restrict
;
7851 ast_interface_block::hir(exec_list
*instructions
,
7852 struct _mesa_glsl_parse_state
*state
)
7854 YYLTYPE loc
= this->get_location();
7856 /* Interface blocks must be declared at global scope */
7857 if (state
->current_function
!= NULL
) {
7858 _mesa_glsl_error(&loc
, state
,
7859 "Interface block `%s' must be declared "
7864 /* Validate qualifiers:
7866 * - Layout Qualifiers as per the table in Section 4.4
7867 * ("Layout Qualifiers") of the GLSL 4.50 spec.
7869 * - Memory Qualifiers as per Section 4.10 ("Memory Qualifiers") of the
7872 * "Additionally, memory qualifiers may also be used in the declaration
7873 * of shader storage blocks"
7875 * Note the table in Section 4.4 says std430 is allowed on both uniform and
7876 * buffer blocks however Section 4.4.5 (Uniform and Shader Storage Block
7877 * Layout Qualifiers) of the GLSL 4.50 spec says:
7879 * "The std430 qualifier is supported only for shader storage blocks;
7880 * using std430 on a uniform block will result in a compile-time error."
7882 ast_type_qualifier allowed_blk_qualifiers
;
7883 allowed_blk_qualifiers
.flags
.i
= 0;
7884 if (this->layout
.flags
.q
.buffer
|| this->layout
.flags
.q
.uniform
) {
7885 allowed_blk_qualifiers
.flags
.q
.shared
= 1;
7886 allowed_blk_qualifiers
.flags
.q
.packed
= 1;
7887 allowed_blk_qualifiers
.flags
.q
.std140
= 1;
7888 allowed_blk_qualifiers
.flags
.q
.row_major
= 1;
7889 allowed_blk_qualifiers
.flags
.q
.column_major
= 1;
7890 allowed_blk_qualifiers
.flags
.q
.explicit_align
= 1;
7891 allowed_blk_qualifiers
.flags
.q
.explicit_binding
= 1;
7892 if (this->layout
.flags
.q
.buffer
) {
7893 allowed_blk_qualifiers
.flags
.q
.buffer
= 1;
7894 allowed_blk_qualifiers
.flags
.q
.std430
= 1;
7895 allowed_blk_qualifiers
.flags
.q
.coherent
= 1;
7896 allowed_blk_qualifiers
.flags
.q
._volatile
= 1;
7897 allowed_blk_qualifiers
.flags
.q
.restrict_flag
= 1;
7898 allowed_blk_qualifiers
.flags
.q
.read_only
= 1;
7899 allowed_blk_qualifiers
.flags
.q
.write_only
= 1;
7901 allowed_blk_qualifiers
.flags
.q
.uniform
= 1;
7904 /* Interface block */
7905 assert(this->layout
.flags
.q
.in
|| this->layout
.flags
.q
.out
);
7907 allowed_blk_qualifiers
.flags
.q
.explicit_location
= 1;
7908 if (this->layout
.flags
.q
.out
) {
7909 allowed_blk_qualifiers
.flags
.q
.out
= 1;
7910 if (state
->stage
== MESA_SHADER_GEOMETRY
||
7911 state
->stage
== MESA_SHADER_TESS_CTRL
||
7912 state
->stage
== MESA_SHADER_TESS_EVAL
||
7913 state
->stage
== MESA_SHADER_VERTEX
) {
7914 allowed_blk_qualifiers
.flags
.q
.explicit_xfb_offset
= 1;
7915 allowed_blk_qualifiers
.flags
.q
.explicit_xfb_buffer
= 1;
7916 allowed_blk_qualifiers
.flags
.q
.xfb_buffer
= 1;
7917 allowed_blk_qualifiers
.flags
.q
.explicit_xfb_stride
= 1;
7918 allowed_blk_qualifiers
.flags
.q
.xfb_stride
= 1;
7919 if (state
->stage
== MESA_SHADER_GEOMETRY
) {
7920 allowed_blk_qualifiers
.flags
.q
.stream
= 1;
7921 allowed_blk_qualifiers
.flags
.q
.explicit_stream
= 1;
7923 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
7924 allowed_blk_qualifiers
.flags
.q
.patch
= 1;
7928 allowed_blk_qualifiers
.flags
.q
.in
= 1;
7929 if (state
->stage
== MESA_SHADER_TESS_EVAL
) {
7930 allowed_blk_qualifiers
.flags
.q
.patch
= 1;
7935 this->layout
.validate_flags(&loc
, state
, allowed_blk_qualifiers
,
7936 "invalid qualifier for block",
7939 enum glsl_interface_packing packing
;
7940 if (this->layout
.flags
.q
.std140
) {
7941 packing
= GLSL_INTERFACE_PACKING_STD140
;
7942 } else if (this->layout
.flags
.q
.packed
) {
7943 packing
= GLSL_INTERFACE_PACKING_PACKED
;
7944 } else if (this->layout
.flags
.q
.std430
) {
7945 packing
= GLSL_INTERFACE_PACKING_STD430
;
7947 /* The default layout is shared.
7949 packing
= GLSL_INTERFACE_PACKING_SHARED
;
7952 ir_variable_mode var_mode
;
7953 const char *iface_type_name
;
7954 if (this->layout
.flags
.q
.in
) {
7955 var_mode
= ir_var_shader_in
;
7956 iface_type_name
= "in";
7957 } else if (this->layout
.flags
.q
.out
) {
7958 var_mode
= ir_var_shader_out
;
7959 iface_type_name
= "out";
7960 } else if (this->layout
.flags
.q
.uniform
) {
7961 var_mode
= ir_var_uniform
;
7962 iface_type_name
= "uniform";
7963 } else if (this->layout
.flags
.q
.buffer
) {
7964 var_mode
= ir_var_shader_storage
;
7965 iface_type_name
= "buffer";
7967 var_mode
= ir_var_auto
;
7968 iface_type_name
= "UNKNOWN";
7969 assert(!"interface block layout qualifier not found!");
7972 enum glsl_matrix_layout matrix_layout
= GLSL_MATRIX_LAYOUT_INHERITED
;
7973 if (this->layout
.flags
.q
.row_major
)
7974 matrix_layout
= GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
7975 else if (this->layout
.flags
.q
.column_major
)
7976 matrix_layout
= GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
;
7978 bool redeclaring_per_vertex
= strcmp(this->block_name
, "gl_PerVertex") == 0;
7979 exec_list declared_variables
;
7980 glsl_struct_field
*fields
;
7982 /* For blocks that accept memory qualifiers (i.e. shader storage), verify
7983 * that we don't have incompatible qualifiers
7985 if (this->layout
.flags
.q
.read_only
&& this->layout
.flags
.q
.write_only
) {
7986 _mesa_glsl_error(&loc
, state
,
7987 "Interface block sets both readonly and writeonly");
7990 unsigned qual_stream
;
7991 if (!process_qualifier_constant(state
, &loc
, "stream", this->layout
.stream
,
7993 !validate_stream_qualifier(&loc
, state
, qual_stream
)) {
7994 /* If the stream qualifier is invalid it doesn't make sense to continue
7995 * on and try to compare stream layouts on member variables against it
7996 * so just return early.
8001 unsigned qual_xfb_buffer
;
8002 if (!process_qualifier_constant(state
, &loc
, "xfb_buffer",
8003 layout
.xfb_buffer
, &qual_xfb_buffer
) ||
8004 !validate_xfb_buffer_qualifier(&loc
, state
, qual_xfb_buffer
)) {
8008 unsigned qual_xfb_offset
;
8009 if (layout
.flags
.q
.explicit_xfb_offset
) {
8010 if (!process_qualifier_constant(state
, &loc
, "xfb_offset",
8011 layout
.offset
, &qual_xfb_offset
)) {
8016 unsigned qual_xfb_stride
;
8017 if (layout
.flags
.q
.explicit_xfb_stride
) {
8018 if (!process_qualifier_constant(state
, &loc
, "xfb_stride",
8019 layout
.xfb_stride
, &qual_xfb_stride
)) {
8024 unsigned expl_location
= 0;
8025 if (layout
.flags
.q
.explicit_location
) {
8026 if (!process_qualifier_constant(state
, &loc
, "location",
8027 layout
.location
, &expl_location
)) {
8030 expl_location
+= this->layout
.flags
.q
.patch
? VARYING_SLOT_PATCH0
8031 : VARYING_SLOT_VAR0
;
8035 unsigned expl_align
= 0;
8036 if (layout
.flags
.q
.explicit_align
) {
8037 if (!process_qualifier_constant(state
, &loc
, "align",
8038 layout
.align
, &expl_align
)) {
8041 if (expl_align
== 0 || expl_align
& (expl_align
- 1)) {
8042 _mesa_glsl_error(&loc
, state
, "align layout qualifier is not a "
8049 unsigned int num_variables
=
8050 ast_process_struct_or_iface_block_members(&declared_variables
,
8052 &this->declarations
,
8056 redeclaring_per_vertex
,
8065 if (!redeclaring_per_vertex
) {
8066 validate_identifier(this->block_name
, loc
, state
);
8068 /* From section 4.3.9 ("Interface Blocks") of the GLSL 4.50 spec:
8070 * "Block names have no other use within a shader beyond interface
8071 * matching; it is a compile-time error to use a block name at global
8072 * scope for anything other than as a block name."
8074 ir_variable
*var
= state
->symbols
->get_variable(this->block_name
);
8075 if (var
&& !var
->type
->is_interface()) {
8076 _mesa_glsl_error(&loc
, state
, "Block name `%s' is "
8077 "already used in the scope.",
8082 const glsl_type
*earlier_per_vertex
= NULL
;
8083 if (redeclaring_per_vertex
) {
8084 /* Find the previous declaration of gl_PerVertex. If we're redeclaring
8085 * the named interface block gl_in, we can find it by looking at the
8086 * previous declaration of gl_in. Otherwise we can find it by looking
8087 * at the previous decalartion of any of the built-in outputs,
8090 * Also check that the instance name and array-ness of the redeclaration
8094 case ir_var_shader_in
:
8095 if (ir_variable
*earlier_gl_in
=
8096 state
->symbols
->get_variable("gl_in")) {
8097 earlier_per_vertex
= earlier_gl_in
->get_interface_type();
8099 _mesa_glsl_error(&loc
, state
,
8100 "redeclaration of gl_PerVertex input not allowed "
8102 _mesa_shader_stage_to_string(state
->stage
));
8104 if (this->instance_name
== NULL
||
8105 strcmp(this->instance_name
, "gl_in") != 0 || this->array_specifier
== NULL
||
8106 !this->array_specifier
->is_single_dimension()) {
8107 _mesa_glsl_error(&loc
, state
,
8108 "gl_PerVertex input must be redeclared as "
8112 case ir_var_shader_out
:
8113 if (ir_variable
*earlier_gl_Position
=
8114 state
->symbols
->get_variable("gl_Position")) {
8115 earlier_per_vertex
= earlier_gl_Position
->get_interface_type();
8116 } else if (ir_variable
*earlier_gl_out
=
8117 state
->symbols
->get_variable("gl_out")) {
8118 earlier_per_vertex
= earlier_gl_out
->get_interface_type();
8120 _mesa_glsl_error(&loc
, state
,
8121 "redeclaration of gl_PerVertex output not "
8122 "allowed in the %s shader",
8123 _mesa_shader_stage_to_string(state
->stage
));
8125 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
8126 if (this->instance_name
== NULL
||
8127 strcmp(this->instance_name
, "gl_out") != 0 || this->array_specifier
== NULL
) {
8128 _mesa_glsl_error(&loc
, state
,
8129 "gl_PerVertex output must be redeclared as "
8133 if (this->instance_name
!= NULL
) {
8134 _mesa_glsl_error(&loc
, state
,
8135 "gl_PerVertex output may not be redeclared with "
8136 "an instance name");
8141 _mesa_glsl_error(&loc
, state
,
8142 "gl_PerVertex must be declared as an input or an "
8147 if (earlier_per_vertex
== NULL
) {
8148 /* An error has already been reported. Bail out to avoid null
8149 * dereferences later in this function.
8154 /* Copy locations from the old gl_PerVertex interface block. */
8155 for (unsigned i
= 0; i
< num_variables
; i
++) {
8156 int j
= earlier_per_vertex
->field_index(fields
[i
].name
);
8158 _mesa_glsl_error(&loc
, state
,
8159 "redeclaration of gl_PerVertex must be a subset "
8160 "of the built-in members of gl_PerVertex");
8162 fields
[i
].location
=
8163 earlier_per_vertex
->fields
.structure
[j
].location
;
8165 earlier_per_vertex
->fields
.structure
[j
].offset
;
8166 fields
[i
].interpolation
=
8167 earlier_per_vertex
->fields
.structure
[j
].interpolation
;
8168 fields
[i
].centroid
=
8169 earlier_per_vertex
->fields
.structure
[j
].centroid
;
8171 earlier_per_vertex
->fields
.structure
[j
].sample
;
8173 earlier_per_vertex
->fields
.structure
[j
].patch
;
8174 fields
[i
].precision
=
8175 earlier_per_vertex
->fields
.structure
[j
].precision
;
8176 fields
[i
].explicit_xfb_buffer
=
8177 earlier_per_vertex
->fields
.structure
[j
].explicit_xfb_buffer
;
8178 fields
[i
].xfb_buffer
=
8179 earlier_per_vertex
->fields
.structure
[j
].xfb_buffer
;
8180 fields
[i
].xfb_stride
=
8181 earlier_per_vertex
->fields
.structure
[j
].xfb_stride
;
8185 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10
8188 * If a built-in interface block is redeclared, it must appear in
8189 * the shader before any use of any member included in the built-in
8190 * declaration, or a compilation error will result.
8192 * This appears to be a clarification to the behaviour established for
8193 * gl_PerVertex by GLSL 1.50, therefore we implement this behaviour
8194 * regardless of GLSL version.
8196 interface_block_usage_visitor
v(var_mode
, earlier_per_vertex
);
8197 v
.run(instructions
);
8198 if (v
.usage_found()) {
8199 _mesa_glsl_error(&loc
, state
,
8200 "redeclaration of a built-in interface block must "
8201 "appear before any use of any member of the "
8206 const glsl_type
*block_type
=
8207 glsl_type::get_interface_instance(fields
,
8211 GLSL_MATRIX_LAYOUT_ROW_MAJOR
,
8214 unsigned component_size
= block_type
->contains_double() ? 8 : 4;
8216 layout
.flags
.q
.explicit_xfb_offset
? (int) qual_xfb_offset
: -1;
8217 validate_xfb_offset_qualifier(&loc
, state
, xfb_offset
, block_type
,
8220 if (!state
->symbols
->add_interface(block_type
->name
, block_type
, var_mode
)) {
8221 YYLTYPE loc
= this->get_location();
8222 _mesa_glsl_error(&loc
, state
, "interface block `%s' with type `%s' "
8223 "already taken in the current scope",
8224 this->block_name
, iface_type_name
);
8227 /* Since interface blocks cannot contain statements, it should be
8228 * impossible for the block to generate any instructions.
8230 assert(declared_variables
.is_empty());
8232 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
8234 * Geometry shader input variables get the per-vertex values written
8235 * out by vertex shader output variables of the same names. Since a
8236 * geometry shader operates on a set of vertices, each input varying
8237 * variable (or input block, see interface blocks below) needs to be
8238 * declared as an array.
8240 if (state
->stage
== MESA_SHADER_GEOMETRY
&& this->array_specifier
== NULL
&&
8241 var_mode
== ir_var_shader_in
) {
8242 _mesa_glsl_error(&loc
, state
, "geometry shader inputs must be arrays");
8243 } else if ((state
->stage
== MESA_SHADER_TESS_CTRL
||
8244 state
->stage
== MESA_SHADER_TESS_EVAL
) &&
8245 !this->layout
.flags
.q
.patch
&&
8246 this->array_specifier
== NULL
&&
8247 var_mode
== ir_var_shader_in
) {
8248 _mesa_glsl_error(&loc
, state
, "per-vertex tessellation shader inputs must be arrays");
8249 } else if (state
->stage
== MESA_SHADER_TESS_CTRL
&&
8250 !this->layout
.flags
.q
.patch
&&
8251 this->array_specifier
== NULL
&&
8252 var_mode
== ir_var_shader_out
) {
8253 _mesa_glsl_error(&loc
, state
, "tessellation control shader outputs must be arrays");
8257 /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
8260 * "If an instance name (instance-name) is used, then it puts all the
8261 * members inside a scope within its own name space, accessed with the
8262 * field selector ( . ) operator (analogously to structures)."
8264 if (this->instance_name
) {
8265 if (redeclaring_per_vertex
) {
8266 /* When a built-in in an unnamed interface block is redeclared,
8267 * get_variable_being_redeclared() calls
8268 * check_builtin_array_max_size() to make sure that built-in array
8269 * variables aren't redeclared to illegal sizes. But we're looking
8270 * at a redeclaration of a named built-in interface block. So we
8271 * have to manually call check_builtin_array_max_size() for all parts
8272 * of the interface that are arrays.
8274 for (unsigned i
= 0; i
< num_variables
; i
++) {
8275 if (fields
[i
].type
->is_array()) {
8276 const unsigned size
= fields
[i
].type
->array_size();
8277 check_builtin_array_max_size(fields
[i
].name
, size
, loc
, state
);
8281 validate_identifier(this->instance_name
, loc
, state
);
8286 if (this->array_specifier
!= NULL
) {
8287 const glsl_type
*block_array_type
=
8288 process_array_type(&loc
, block_type
, this->array_specifier
, state
);
8290 /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
8292 * For uniform blocks declared an array, each individual array
8293 * element corresponds to a separate buffer object backing one
8294 * instance of the block. As the array size indicates the number
8295 * of buffer objects needed, uniform block array declarations
8296 * must specify an array size.
8298 * And a few paragraphs later:
8300 * Geometry shader input blocks must be declared as arrays and
8301 * follow the array declaration and linking rules for all
8302 * geometry shader inputs. All other input and output block
8303 * arrays must specify an array size.
8305 * The same applies to tessellation shaders.
8307 * The upshot of this is that the only circumstance where an
8308 * interface array size *doesn't* need to be specified is on a
8309 * geometry shader input, tessellation control shader input,
8310 * tessellation control shader output, and tessellation evaluation
8313 if (block_array_type
->is_unsized_array()) {
8314 bool allow_inputs
= state
->stage
== MESA_SHADER_GEOMETRY
||
8315 state
->stage
== MESA_SHADER_TESS_CTRL
||
8316 state
->stage
== MESA_SHADER_TESS_EVAL
;
8317 bool allow_outputs
= state
->stage
== MESA_SHADER_TESS_CTRL
;
8319 if (this->layout
.flags
.q
.in
) {
8321 _mesa_glsl_error(&loc
, state
,
8322 "unsized input block arrays not allowed in "
8324 _mesa_shader_stage_to_string(state
->stage
));
8325 } else if (this->layout
.flags
.q
.out
) {
8327 _mesa_glsl_error(&loc
, state
,
8328 "unsized output block arrays not allowed in "
8330 _mesa_shader_stage_to_string(state
->stage
));
8332 /* by elimination, this is a uniform block array */
8333 _mesa_glsl_error(&loc
, state
,
8334 "unsized uniform block arrays not allowed in "
8336 _mesa_shader_stage_to_string(state
->stage
));
8340 /* From section 4.3.9 (Interface Blocks) of the GLSL ES 3.10 spec:
8342 * * Arrays of arrays of blocks are not allowed
8344 if (state
->es_shader
&& block_array_type
->is_array() &&
8345 block_array_type
->fields
.array
->is_array()) {
8346 _mesa_glsl_error(&loc
, state
,
8347 "arrays of arrays interface blocks are "
8351 var
= new(state
) ir_variable(block_array_type
,
8352 this->instance_name
,
8355 var
= new(state
) ir_variable(block_type
,
8356 this->instance_name
,
8360 var
->data
.matrix_layout
= matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
8361 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
: matrix_layout
;
8363 if (var_mode
== ir_var_shader_in
|| var_mode
== ir_var_uniform
)
8364 var
->data
.read_only
= true;
8366 var
->data
.patch
= this->layout
.flags
.q
.patch
;
8368 if (state
->stage
== MESA_SHADER_GEOMETRY
&& var_mode
== ir_var_shader_in
)
8369 handle_geometry_shader_input_decl(state
, loc
, var
);
8370 else if ((state
->stage
== MESA_SHADER_TESS_CTRL
||
8371 state
->stage
== MESA_SHADER_TESS_EVAL
) && var_mode
== ir_var_shader_in
)
8372 handle_tess_shader_input_decl(state
, loc
, var
);
8373 else if (state
->stage
== MESA_SHADER_TESS_CTRL
&& var_mode
== ir_var_shader_out
)
8374 handle_tess_ctrl_shader_output_decl(state
, loc
, var
);
8376 for (unsigned i
= 0; i
< num_variables
; i
++) {
8377 if (var
->data
.mode
== ir_var_shader_storage
)
8378 apply_memory_qualifiers(var
, fields
[i
]);
8381 if (ir_variable
*earlier
=
8382 state
->symbols
->get_variable(this->instance_name
)) {
8383 if (!redeclaring_per_vertex
) {
8384 _mesa_glsl_error(&loc
, state
, "`%s' redeclared",
8385 this->instance_name
);
8387 earlier
->data
.how_declared
= ir_var_declared_normally
;
8388 earlier
->type
= var
->type
;
8389 earlier
->reinit_interface_type(block_type
);
8392 if (this->layout
.flags
.q
.explicit_binding
) {
8393 apply_explicit_binding(state
, &loc
, var
, var
->type
,
8397 var
->data
.stream
= qual_stream
;
8398 if (layout
.flags
.q
.explicit_location
) {
8399 var
->data
.location
= expl_location
;
8400 var
->data
.explicit_location
= true;
8403 state
->symbols
->add_variable(var
);
8404 instructions
->push_tail(var
);
8407 /* In order to have an array size, the block must also be declared with
8410 assert(this->array_specifier
== NULL
);
8412 for (unsigned i
= 0; i
< num_variables
; i
++) {
8414 new(state
) ir_variable(fields
[i
].type
,
8415 ralloc_strdup(state
, fields
[i
].name
),
8417 var
->data
.interpolation
= fields
[i
].interpolation
;
8418 var
->data
.centroid
= fields
[i
].centroid
;
8419 var
->data
.sample
= fields
[i
].sample
;
8420 var
->data
.patch
= fields
[i
].patch
;
8421 var
->data
.stream
= qual_stream
;
8422 var
->data
.location
= fields
[i
].location
;
8424 if (fields
[i
].location
!= -1)
8425 var
->data
.explicit_location
= true;
8427 var
->data
.explicit_xfb_buffer
= fields
[i
].explicit_xfb_buffer
;
8428 var
->data
.xfb_buffer
= fields
[i
].xfb_buffer
;
8430 if (fields
[i
].offset
!= -1)
8431 var
->data
.explicit_xfb_offset
= true;
8432 var
->data
.offset
= fields
[i
].offset
;
8434 var
->init_interface_type(block_type
);
8436 if (var_mode
== ir_var_shader_in
|| var_mode
== ir_var_uniform
)
8437 var
->data
.read_only
= true;
8439 /* Precision qualifiers do not have any meaning in Desktop GLSL */
8440 if (state
->es_shader
) {
8441 var
->data
.precision
=
8442 select_gles_precision(fields
[i
].precision
, fields
[i
].type
,
8446 if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
) {
8447 var
->data
.matrix_layout
= matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
8448 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
: matrix_layout
;
8450 var
->data
.matrix_layout
= fields
[i
].matrix_layout
;
8453 if (var
->data
.mode
== ir_var_shader_storage
)
8454 apply_memory_qualifiers(var
, fields
[i
]);
8456 /* Examine var name here since var may get deleted in the next call */
8457 bool var_is_gl_id
= is_gl_identifier(var
->name
);
8459 if (redeclaring_per_vertex
) {
8460 bool is_redeclaration
;
8462 get_variable_being_redeclared(&var
, loc
, state
,
8463 true /* allow_all_redeclarations */,
8465 if (!var_is_gl_id
|| !is_redeclaration
) {
8466 _mesa_glsl_error(&loc
, state
,
8467 "redeclaration of gl_PerVertex can only "
8468 "include built-in variables");
8469 } else if (var
->data
.how_declared
== ir_var_declared_normally
) {
8470 _mesa_glsl_error(&loc
, state
,
8471 "`%s' has already been redeclared",
8474 var
->data
.how_declared
= ir_var_declared_in_block
;
8475 var
->reinit_interface_type(block_type
);
8480 if (state
->symbols
->get_variable(var
->name
) != NULL
)
8481 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
8483 /* Propagate the "binding" keyword into this UBO/SSBO's fields.
8484 * The UBO declaration itself doesn't get an ir_variable unless it
8485 * has an instance name. This is ugly.
8487 if (this->layout
.flags
.q
.explicit_binding
) {
8488 apply_explicit_binding(state
, &loc
, var
,
8489 var
->get_interface_type(), &this->layout
);
8492 if (var
->type
->is_unsized_array()) {
8493 if (var
->is_in_shader_storage_block() &&
8494 is_unsized_array_last_element(var
)) {
8495 var
->data
.from_ssbo_unsized_array
= true;
8497 /* From GLSL ES 3.10 spec, section 4.1.9 "Arrays":
8499 * "If an array is declared as the last member of a shader storage
8500 * block and the size is not specified at compile-time, it is
8501 * sized at run-time. In all other cases, arrays are sized only
8504 * In desktop GLSL it is allowed to have unsized-arrays that are
8505 * not last, as long as we can determine that they are implicitly
8508 if (state
->es_shader
) {
8509 _mesa_glsl_error(&loc
, state
, "unsized array `%s' "
8510 "definition: only last member of a shader "
8511 "storage block can be defined as unsized "
8512 "array", fields
[i
].name
);
8517 state
->symbols
->add_variable(var
);
8518 instructions
->push_tail(var
);
8521 if (redeclaring_per_vertex
&& block_type
!= earlier_per_vertex
) {
8522 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec:
8524 * It is also a compilation error ... to redeclare a built-in
8525 * block and then use a member from that built-in block that was
8526 * not included in the redeclaration.
8528 * This appears to be a clarification to the behaviour established
8529 * for gl_PerVertex by GLSL 1.50, therefore we implement this
8530 * behaviour regardless of GLSL version.
8532 * To prevent the shader from using a member that was not included in
8533 * the redeclaration, we disable any ir_variables that are still
8534 * associated with the old declaration of gl_PerVertex (since we've
8535 * already updated all of the variables contained in the new
8536 * gl_PerVertex to point to it).
8538 * As a side effect this will prevent
8539 * validate_intrastage_interface_blocks() from getting confused and
8540 * thinking there are conflicting definitions of gl_PerVertex in the
8543 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
8544 ir_variable
*const var
= node
->as_variable();
8546 var
->get_interface_type() == earlier_per_vertex
&&
8547 var
->data
.mode
== var_mode
) {
8548 if (var
->data
.how_declared
== ir_var_declared_normally
) {
8549 _mesa_glsl_error(&loc
, state
,
8550 "redeclaration of gl_PerVertex cannot "
8551 "follow a redeclaration of `%s'",
8554 state
->symbols
->disable_variable(var
->name
);
8566 ast_tcs_output_layout::hir(exec_list
*instructions
,
8567 struct _mesa_glsl_parse_state
*state
)
8569 YYLTYPE loc
= this->get_location();
8571 unsigned num_vertices
;
8572 if (!state
->out_qualifier
->vertices
->
8573 process_qualifier_constant(state
, "vertices", &num_vertices
,
8575 /* return here to stop cascading incorrect error messages */
8579 /* If any shader outputs occurred before this declaration and specified an
8580 * array size, make sure the size they specified is consistent with the
8583 if (state
->tcs_output_size
!= 0 && state
->tcs_output_size
!= num_vertices
) {
8584 _mesa_glsl_error(&loc
, state
,
8585 "this tessellation control shader output layout "
8586 "specifies %u vertices, but a previous output "
8587 "is declared with size %u",
8588 num_vertices
, state
->tcs_output_size
);
8592 state
->tcs_output_vertices_specified
= true;
8594 /* If any shader outputs occurred before this declaration and did not
8595 * specify an array size, their size is determined now.
8597 foreach_in_list (ir_instruction
, node
, instructions
) {
8598 ir_variable
*var
= node
->as_variable();
8599 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_out
)
8602 /* Note: Not all tessellation control shader output are arrays. */
8603 if (!var
->type
->is_unsized_array() || var
->data
.patch
)
8606 if (var
->data
.max_array_access
>= (int)num_vertices
) {
8607 _mesa_glsl_error(&loc
, state
,
8608 "this tessellation control shader output layout "
8609 "specifies %u vertices, but an access to element "
8610 "%u of output `%s' already exists", num_vertices
,
8611 var
->data
.max_array_access
, var
->name
);
8613 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
8623 ast_gs_input_layout::hir(exec_list
*instructions
,
8624 struct _mesa_glsl_parse_state
*state
)
8626 YYLTYPE loc
= this->get_location();
8628 /* Should have been prevented by the parser. */
8629 assert(!state
->gs_input_prim_type_specified
8630 || state
->in_qualifier
->prim_type
== this->prim_type
);
8632 /* If any shader inputs occurred before this declaration and specified an
8633 * array size, make sure the size they specified is consistent with the
8636 unsigned num_vertices
= vertices_per_prim(this->prim_type
);
8637 if (state
->gs_input_size
!= 0 && state
->gs_input_size
!= num_vertices
) {
8638 _mesa_glsl_error(&loc
, state
,
8639 "this geometry shader input layout implies %u vertices"
8640 " per primitive, but a previous input is declared"
8641 " with size %u", num_vertices
, state
->gs_input_size
);
8645 state
->gs_input_prim_type_specified
= true;
8647 /* If any shader inputs occurred before this declaration and did not
8648 * specify an array size, their size is determined now.
8650 foreach_in_list(ir_instruction
, node
, instructions
) {
8651 ir_variable
*var
= node
->as_variable();
8652 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_in
)
8655 /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
8659 if (var
->type
->is_unsized_array()) {
8660 if (var
->data
.max_array_access
>= (int)num_vertices
) {
8661 _mesa_glsl_error(&loc
, state
,
8662 "this geometry shader input layout implies %u"
8663 " vertices, but an access to element %u of input"
8664 " `%s' already exists", num_vertices
,
8665 var
->data
.max_array_access
, var
->name
);
8667 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
8678 ast_cs_input_layout::hir(exec_list
*instructions
,
8679 struct _mesa_glsl_parse_state
*state
)
8681 YYLTYPE loc
= this->get_location();
8683 /* From the ARB_compute_shader specification:
8685 * If the local size of the shader in any dimension is greater
8686 * than the maximum size supported by the implementation for that
8687 * dimension, a compile-time error results.
8689 * It is not clear from the spec how the error should be reported if
8690 * the total size of the work group exceeds
8691 * MAX_COMPUTE_WORK_GROUP_INVOCATIONS, but it seems reasonable to
8692 * report it at compile time as well.
8694 GLuint64 total_invocations
= 1;
8695 unsigned qual_local_size
[3];
8696 for (int i
= 0; i
< 3; i
++) {
8698 char *local_size_str
= ralloc_asprintf(NULL
, "invalid local_size_%c",
8700 /* Infer a local_size of 1 for unspecified dimensions */
8701 if (this->local_size
[i
] == NULL
) {
8702 qual_local_size
[i
] = 1;
8703 } else if (!this->local_size
[i
]->
8704 process_qualifier_constant(state
, local_size_str
,
8705 &qual_local_size
[i
], false)) {
8706 ralloc_free(local_size_str
);
8709 ralloc_free(local_size_str
);
8711 if (qual_local_size
[i
] > state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]) {
8712 _mesa_glsl_error(&loc
, state
,
8713 "local_size_%c exceeds MAX_COMPUTE_WORK_GROUP_SIZE"
8715 state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]);
8718 total_invocations
*= qual_local_size
[i
];
8719 if (total_invocations
>
8720 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
) {
8721 _mesa_glsl_error(&loc
, state
,
8722 "product of local_sizes exceeds "
8723 "MAX_COMPUTE_WORK_GROUP_INVOCATIONS (%d)",
8724 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
);
8729 /* If any compute input layout declaration preceded this one, make sure it
8730 * was consistent with this one.
8732 if (state
->cs_input_local_size_specified
) {
8733 for (int i
= 0; i
< 3; i
++) {
8734 if (state
->cs_input_local_size
[i
] != qual_local_size
[i
]) {
8735 _mesa_glsl_error(&loc
, state
,
8736 "compute shader input layout does not match"
8737 " previous declaration");
8743 /* The ARB_compute_variable_group_size spec says:
8745 * If a compute shader including a *local_size_variable* qualifier also
8746 * declares a fixed local group size using the *local_size_x*,
8747 * *local_size_y*, or *local_size_z* qualifiers, a compile-time error
8750 if (state
->cs_input_local_size_variable_specified
) {
8751 _mesa_glsl_error(&loc
, state
,
8752 "compute shader can't include both a variable and a "
8753 "fixed local group size");
8757 state
->cs_input_local_size_specified
= true;
8758 for (int i
= 0; i
< 3; i
++)
8759 state
->cs_input_local_size
[i
] = qual_local_size
[i
];
8761 /* We may now declare the built-in constant gl_WorkGroupSize (see
8762 * builtin_variable_generator::generate_constants() for why we didn't
8763 * declare it earlier).
8765 ir_variable
*var
= new(state
->symbols
)
8766 ir_variable(glsl_type::uvec3_type
, "gl_WorkGroupSize", ir_var_auto
);
8767 var
->data
.how_declared
= ir_var_declared_implicitly
;
8768 var
->data
.read_only
= true;
8769 instructions
->push_tail(var
);
8770 state
->symbols
->add_variable(var
);
8771 ir_constant_data data
;
8772 memset(&data
, 0, sizeof(data
));
8773 for (int i
= 0; i
< 3; i
++)
8774 data
.u
[i
] = qual_local_size
[i
];
8775 var
->constant_value
= new(var
) ir_constant(glsl_type::uvec3_type
, &data
);
8776 var
->constant_initializer
=
8777 new(var
) ir_constant(glsl_type::uvec3_type
, &data
);
8778 var
->data
.has_initializer
= true;
8779 var
->data
.is_implicit_initializer
= false;
8786 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
8787 exec_list
*instructions
)
8789 bool gl_FragColor_assigned
= false;
8790 bool gl_FragData_assigned
= false;
8791 bool gl_FragSecondaryColor_assigned
= false;
8792 bool gl_FragSecondaryData_assigned
= false;
8793 bool user_defined_fs_output_assigned
= false;
8794 ir_variable
*user_defined_fs_output
= NULL
;
8796 /* It would be nice to have proper location information. */
8798 memset(&loc
, 0, sizeof(loc
));
8800 foreach_in_list(ir_instruction
, node
, instructions
) {
8801 ir_variable
*var
= node
->as_variable();
8803 if (!var
|| !var
->data
.assigned
)
8806 if (strcmp(var
->name
, "gl_FragColor") == 0) {
8807 gl_FragColor_assigned
= true;
8808 if (!var
->constant_initializer
&& state
->zero_init
) {
8809 const ir_constant_data data
= { { 0 } };
8810 var
->data
.has_initializer
= true;
8811 var
->data
.is_implicit_initializer
= true;
8812 var
->constant_initializer
= new(var
) ir_constant(var
->type
, &data
);
8815 else if (strcmp(var
->name
, "gl_FragData") == 0)
8816 gl_FragData_assigned
= true;
8817 else if (strcmp(var
->name
, "gl_SecondaryFragColorEXT") == 0)
8818 gl_FragSecondaryColor_assigned
= true;
8819 else if (strcmp(var
->name
, "gl_SecondaryFragDataEXT") == 0)
8820 gl_FragSecondaryData_assigned
= true;
8821 else if (!is_gl_identifier(var
->name
)) {
8822 if (state
->stage
== MESA_SHADER_FRAGMENT
&&
8823 var
->data
.mode
== ir_var_shader_out
) {
8824 user_defined_fs_output_assigned
= true;
8825 user_defined_fs_output
= var
;
8830 /* From the GLSL 1.30 spec:
8832 * "If a shader statically assigns a value to gl_FragColor, it
8833 * may not assign a value to any element of gl_FragData. If a
8834 * shader statically writes a value to any element of
8835 * gl_FragData, it may not assign a value to
8836 * gl_FragColor. That is, a shader may assign values to either
8837 * gl_FragColor or gl_FragData, but not both. Multiple shaders
8838 * linked together must also consistently write just one of
8839 * these variables. Similarly, if user declared output
8840 * variables are in use (statically assigned to), then the
8841 * built-in variables gl_FragColor and gl_FragData may not be
8842 * assigned to. These incorrect usages all generate compile
8845 if (gl_FragColor_assigned
&& gl_FragData_assigned
) {
8846 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8847 "`gl_FragColor' and `gl_FragData'");
8848 } else if (gl_FragColor_assigned
&& user_defined_fs_output_assigned
) {
8849 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8850 "`gl_FragColor' and `%s'",
8851 user_defined_fs_output
->name
);
8852 } else if (gl_FragSecondaryColor_assigned
&& gl_FragSecondaryData_assigned
) {
8853 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8854 "`gl_FragSecondaryColorEXT' and"
8855 " `gl_FragSecondaryDataEXT'");
8856 } else if (gl_FragColor_assigned
&& gl_FragSecondaryData_assigned
) {
8857 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8858 "`gl_FragColor' and"
8859 " `gl_FragSecondaryDataEXT'");
8860 } else if (gl_FragData_assigned
&& gl_FragSecondaryColor_assigned
) {
8861 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8863 " `gl_FragSecondaryColorEXT'");
8864 } else if (gl_FragData_assigned
&& user_defined_fs_output_assigned
) {
8865 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8866 "`gl_FragData' and `%s'",
8867 user_defined_fs_output
->name
);
8870 if ((gl_FragSecondaryColor_assigned
|| gl_FragSecondaryData_assigned
) &&
8871 !state
->EXT_blend_func_extended_enable
) {
8872 _mesa_glsl_error(&loc
, state
,
8873 "Dual source blending requires EXT_blend_func_extended");
8878 verify_subroutine_associated_funcs(struct _mesa_glsl_parse_state
*state
)
8881 memset(&loc
, 0, sizeof(loc
));
8883 /* Section 6.1.2 (Subroutines) of the GLSL 4.00 spec says:
8885 * "A program will fail to compile or link if any shader
8886 * or stage contains two or more functions with the same
8887 * name if the name is associated with a subroutine type."
8890 for (int i
= 0; i
< state
->num_subroutines
; i
++) {
8891 unsigned definitions
= 0;
8892 ir_function
*fn
= state
->subroutines
[i
];
8893 /* Calculate number of function definitions with the same name */
8894 foreach_in_list(ir_function_signature
, sig
, &fn
->signatures
) {
8895 if (sig
->is_defined
) {
8896 if (++definitions
> 1) {
8897 _mesa_glsl_error(&loc
, state
,
8898 "%s shader contains two or more function "
8899 "definitions with name `%s', which is "
8900 "associated with a subroutine type.\n",
8901 _mesa_shader_stage_to_string(state
->stage
),
8911 remove_per_vertex_blocks(exec_list
*instructions
,
8912 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
)
8914 /* Find the gl_PerVertex interface block of the appropriate (in/out) mode,
8915 * if it exists in this shader type.
8917 const glsl_type
*per_vertex
= NULL
;
8919 case ir_var_shader_in
:
8920 if (ir_variable
*gl_in
= state
->symbols
->get_variable("gl_in"))
8921 per_vertex
= gl_in
->get_interface_type();
8923 case ir_var_shader_out
:
8924 if (ir_variable
*gl_Position
=
8925 state
->symbols
->get_variable("gl_Position")) {
8926 per_vertex
= gl_Position
->get_interface_type();
8930 assert(!"Unexpected mode");
8934 /* If we didn't find a built-in gl_PerVertex interface block, then we don't
8935 * need to do anything.
8937 if (per_vertex
== NULL
)
8940 /* If the interface block is used by the shader, then we don't need to do
8943 interface_block_usage_visitor
v(mode
, per_vertex
);
8944 v
.run(instructions
);
8945 if (v
.usage_found())
8948 /* Remove any ir_variable declarations that refer to the interface block
8951 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
8952 ir_variable
*const var
= node
->as_variable();
8953 if (var
!= NULL
&& var
->get_interface_type() == per_vertex
&&
8954 var
->data
.mode
== mode
) {
8955 state
->symbols
->disable_variable(var
->name
);
8962 ast_warnings_toggle::hir(exec_list
*,
8963 struct _mesa_glsl_parse_state
*state
)
8965 state
->warnings_enabled
= enable
;