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 /* The GL_EXT_shader_image_load_store spec says:
4987 * A layout of "size1x8" is illegal for image variables associated
4988 * with floating-point data types.
4990 _mesa_glsl_error(& loc
, state
,
4991 "size1x8 is illegal for image variables "
4992 "with floating-point data types.");
4994 case PIPE_FORMAT_R16_SINT
:
4995 this->type
->qualifier
.image_format
= PIPE_FORMAT_R16_FLOAT
;
4997 case PIPE_FORMAT_R32_SINT
:
4998 this->type
->qualifier
.image_format
= PIPE_FORMAT_R32_FLOAT
;
5000 case PIPE_FORMAT_R32G32_SINT
:
5001 this->type
->qualifier
.image_format
= PIPE_FORMAT_R32G32_FLOAT
;
5003 case PIPE_FORMAT_R32G32B32A32_SINT
:
5004 this->type
->qualifier
.image_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
5007 unreachable("Unknown image format");
5009 this->type
->qualifier
.image_base_type
= GLSL_TYPE_FLOAT
;
5010 } else if (strncmp(this->type
->specifier
->type_name
, "uimage", strlen("uimage")) == 0) {
5011 switch (this->type
->qualifier
.image_format
) {
5012 case PIPE_FORMAT_R8_SINT
:
5013 this->type
->qualifier
.image_format
= PIPE_FORMAT_R8_UINT
;
5015 case PIPE_FORMAT_R16_SINT
:
5016 this->type
->qualifier
.image_format
= PIPE_FORMAT_R16_UINT
;
5018 case PIPE_FORMAT_R32_SINT
:
5019 this->type
->qualifier
.image_format
= PIPE_FORMAT_R32_UINT
;
5021 case PIPE_FORMAT_R32G32_SINT
:
5022 this->type
->qualifier
.image_format
= PIPE_FORMAT_R32G32_UINT
;
5024 case PIPE_FORMAT_R32G32B32A32_SINT
:
5025 this->type
->qualifier
.image_format
= PIPE_FORMAT_R32G32B32A32_UINT
;
5028 unreachable("Unknown image format");
5030 this->type
->qualifier
.image_base_type
= GLSL_TYPE_UINT
;
5031 } else if (strncmp(this->type
->specifier
->type_name
, "iimage", strlen("iimage")) == 0) {
5032 this->type
->qualifier
.image_base_type
= GLSL_TYPE_INT
;
5038 /* The type specifier may contain a structure definition. Process that
5039 * before any of the variable declarations.
5041 (void) this->type
->specifier
->hir(instructions
, state
);
5043 decl_type
= this->type
->glsl_type(& type_name
, state
);
5045 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
5046 * "Buffer variables may only be declared inside interface blocks
5047 * (section 4.3.9 “Interface Blocks”), which are then referred to as
5048 * shader storage blocks. It is a compile-time error to declare buffer
5049 * variables at global scope (outside a block)."
5051 if (type
->qualifier
.flags
.q
.buffer
&& !decl_type
->is_interface()) {
5052 _mesa_glsl_error(&loc
, state
,
5053 "buffer variables cannot be declared outside "
5054 "interface blocks");
5057 /* An offset-qualified atomic counter declaration sets the default
5058 * offset for the next declaration within the same atomic counter
5061 if (decl_type
&& decl_type
->contains_atomic()) {
5062 if (type
->qualifier
.flags
.q
.explicit_binding
&&
5063 type
->qualifier
.flags
.q
.explicit_offset
) {
5064 unsigned qual_binding
;
5065 unsigned qual_offset
;
5066 if (process_qualifier_constant(state
, &loc
, "binding",
5067 type
->qualifier
.binding
,
5069 && process_qualifier_constant(state
, &loc
, "offset",
5070 type
->qualifier
.offset
,
5072 if (qual_binding
< ARRAY_SIZE(state
->atomic_counter_offsets
))
5073 state
->atomic_counter_offsets
[qual_binding
] = qual_offset
;
5077 ast_type_qualifier allowed_atomic_qual_mask
;
5078 allowed_atomic_qual_mask
.flags
.i
= 0;
5079 allowed_atomic_qual_mask
.flags
.q
.explicit_binding
= 1;
5080 allowed_atomic_qual_mask
.flags
.q
.explicit_offset
= 1;
5081 allowed_atomic_qual_mask
.flags
.q
.uniform
= 1;
5083 type
->qualifier
.validate_flags(&loc
, state
, allowed_atomic_qual_mask
,
5084 "invalid layout qualifier for",
5088 if (this->declarations
.is_empty()) {
5089 /* If there is no structure involved in the program text, there are two
5090 * possible scenarios:
5092 * - The program text contained something like 'vec4;'. This is an
5093 * empty declaration. It is valid but weird. Emit a warning.
5095 * - The program text contained something like 'S;' and 'S' is not the
5096 * name of a known structure type. This is both invalid and weird.
5099 * - The program text contained something like 'mediump float;'
5100 * when the programmer probably meant 'precision mediump
5101 * float;' Emit a warning with a description of what they
5102 * probably meant to do.
5104 * Note that if decl_type is NULL and there is a structure involved,
5105 * there must have been some sort of error with the structure. In this
5106 * case we assume that an error was already generated on this line of
5107 * code for the structure. There is no need to generate an additional,
5110 assert(this->type
->specifier
->structure
== NULL
|| decl_type
!= NULL
5113 if (decl_type
== NULL
) {
5114 _mesa_glsl_error(&loc
, state
,
5115 "invalid type `%s' in empty declaration",
5118 if (decl_type
->is_array()) {
5119 /* From Section 13.22 (Array Declarations) of the GLSL ES 3.2
5122 * "... any declaration that leaves the size undefined is
5123 * disallowed as this would add complexity and there are no
5126 if (state
->es_shader
&& decl_type
->is_unsized_array()) {
5127 _mesa_glsl_error(&loc
, state
, "array size must be explicitly "
5128 "or implicitly defined");
5131 /* From Section 4.12 (Empty Declarations) of the GLSL 4.5 spec:
5133 * "The combinations of types and qualifiers that cause
5134 * compile-time or link-time errors are the same whether or not
5135 * the declaration is empty."
5137 validate_array_dimensions(decl_type
, state
, &loc
);
5140 if (decl_type
->is_atomic_uint()) {
5141 /* Empty atomic counter declarations are allowed and useful
5142 * to set the default offset qualifier.
5145 } else if (this->type
->qualifier
.precision
!= ast_precision_none
) {
5146 if (this->type
->specifier
->structure
!= NULL
) {
5147 _mesa_glsl_error(&loc
, state
,
5148 "precision qualifiers can't be applied "
5151 static const char *const precision_names
[] = {
5158 _mesa_glsl_warning(&loc
, state
,
5159 "empty declaration with precision "
5160 "qualifier, to set the default precision, "
5161 "use `precision %s %s;'",
5162 precision_names
[this->type
->
5163 qualifier
.precision
],
5166 } else if (this->type
->specifier
->structure
== NULL
) {
5167 _mesa_glsl_warning(&loc
, state
, "empty declaration");
5172 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
5173 const struct glsl_type
*var_type
;
5175 const char *identifier
= decl
->identifier
;
5176 /* FINISHME: Emit a warning if a variable declaration shadows a
5177 * FINISHME: declaration at a higher scope.
5180 if ((decl_type
== NULL
) || decl_type
->is_void()) {
5181 if (type_name
!= NULL
) {
5182 _mesa_glsl_error(& loc
, state
,
5183 "invalid type `%s' in declaration of `%s'",
5184 type_name
, decl
->identifier
);
5186 _mesa_glsl_error(& loc
, state
,
5187 "invalid type in declaration of `%s'",
5193 if (this->type
->qualifier
.is_subroutine_decl()) {
5197 t
= state
->symbols
->get_type(this->type
->specifier
->type_name
);
5199 _mesa_glsl_error(& loc
, state
,
5200 "invalid type in declaration of `%s'",
5202 name
= ralloc_asprintf(ctx
, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state
->stage
), decl
->identifier
);
5207 var_type
= process_array_type(&loc
, decl_type
, decl
->array_specifier
,
5210 var
= new(ctx
) ir_variable(var_type
, identifier
, ir_var_auto
);
5212 /* The 'varying in' and 'varying out' qualifiers can only be used with
5213 * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
5216 if (this->type
->qualifier
.flags
.q
.varying
) {
5217 if (this->type
->qualifier
.flags
.q
.in
) {
5218 _mesa_glsl_error(& loc
, state
,
5219 "`varying in' qualifier in declaration of "
5220 "`%s' only valid for geometry shaders using "
5221 "ARB_geometry_shader4 or EXT_geometry_shader4",
5223 } else if (this->type
->qualifier
.flags
.q
.out
) {
5224 _mesa_glsl_error(& loc
, state
,
5225 "`varying out' qualifier in declaration of "
5226 "`%s' only valid for geometry shaders using "
5227 "ARB_geometry_shader4 or EXT_geometry_shader4",
5232 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
5234 * "Global variables can only use the qualifiers const,
5235 * attribute, uniform, or varying. Only one may be
5238 * Local variables can only use the qualifier const."
5240 * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
5241 * any extension that adds the 'layout' keyword.
5243 if (!state
->is_version(130, 300)
5244 && !state
->has_explicit_attrib_location()
5245 && !state
->has_separate_shader_objects()
5246 && !state
->ARB_fragment_coord_conventions_enable
) {
5247 /* GL_EXT_gpu_shader4 only allows "varying out" on fragment shader
5248 * outputs. (the varying flag is not set by the parser)
5250 if (this->type
->qualifier
.flags
.q
.out
&&
5251 (!state
->EXT_gpu_shader4_enable
||
5252 state
->stage
!= MESA_SHADER_FRAGMENT
)) {
5253 _mesa_glsl_error(& loc
, state
,
5254 "`out' qualifier in declaration of `%s' "
5255 "only valid for function parameters in %s",
5256 decl
->identifier
, state
->get_version_string());
5258 if (this->type
->qualifier
.flags
.q
.in
) {
5259 _mesa_glsl_error(& loc
, state
,
5260 "`in' qualifier in declaration of `%s' "
5261 "only valid for function parameters in %s",
5262 decl
->identifier
, state
->get_version_string());
5264 /* FINISHME: Test for other invalid qualifiers. */
5267 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
5269 apply_layout_qualifier_to_variable(&this->type
->qualifier
, var
, state
,
5272 if ((state
->zero_init
& (1u << var
->data
.mode
)) &&
5273 (var
->type
->is_numeric() || var
->type
->is_boolean())) {
5274 const ir_constant_data data
= { { 0 } };
5275 var
->data
.has_initializer
= true;
5276 var
->data
.is_implicit_initializer
= true;
5277 var
->constant_initializer
= new(var
) ir_constant(var
->type
, &data
);
5280 if (this->type
->qualifier
.flags
.q
.invariant
) {
5281 if (!is_allowed_invariant(var
, state
)) {
5282 _mesa_glsl_error(&loc
, state
,
5283 "`%s' cannot be marked invariant; interfaces between "
5284 "shader stages only", var
->name
);
5288 if (state
->current_function
!= NULL
) {
5289 const char *mode
= NULL
;
5290 const char *extra
= "";
5292 /* There is no need to check for 'inout' here because the parser will
5293 * only allow that in function parameter lists.
5295 if (this->type
->qualifier
.flags
.q
.attribute
) {
5297 } else if (this->type
->qualifier
.is_subroutine_decl()) {
5298 mode
= "subroutine uniform";
5299 } else if (this->type
->qualifier
.flags
.q
.uniform
) {
5301 } else if (this->type
->qualifier
.flags
.q
.varying
) {
5303 } else if (this->type
->qualifier
.flags
.q
.in
) {
5305 extra
= " or in function parameter list";
5306 } else if (this->type
->qualifier
.flags
.q
.out
) {
5308 extra
= " or in function parameter list";
5312 _mesa_glsl_error(& loc
, state
,
5313 "%s variable `%s' must be declared at "
5315 mode
, var
->name
, extra
);
5317 } else if (var
->data
.mode
== ir_var_shader_in
) {
5318 var
->data
.read_only
= true;
5320 if (state
->stage
== MESA_SHADER_VERTEX
) {
5321 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
5323 * "Vertex shader inputs can only be float, floating-point
5324 * vectors, matrices, signed and unsigned integers and integer
5325 * vectors. Vertex shader inputs can also form arrays of these
5326 * types, but not structures."
5328 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
5330 * "Vertex shader inputs can only be float, floating-point
5331 * vectors, matrices, signed and unsigned integers and integer
5332 * vectors. They cannot be arrays or structures."
5334 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
5336 * "The attribute qualifier can be used only with float,
5337 * floating-point vectors, and matrices. Attribute variables
5338 * cannot be declared as arrays or structures."
5340 * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
5342 * "Vertex shader inputs can only be float, floating-point
5343 * vectors, matrices, signed and unsigned integers and integer
5344 * vectors. Vertex shader inputs cannot be arrays or
5347 * From section 4.3.4 of the ARB_bindless_texture spec:
5349 * "(modify third paragraph of the section to allow sampler and
5350 * image types) ... Vertex shader inputs can only be float,
5351 * single-precision floating-point scalars, single-precision
5352 * floating-point vectors, matrices, signed and unsigned
5353 * integers and integer vectors, sampler and image types."
5355 const glsl_type
*check_type
= var
->type
->without_array();
5358 switch (check_type
->base_type
) {
5359 case GLSL_TYPE_FLOAT
:
5361 case GLSL_TYPE_UINT64
:
5362 case GLSL_TYPE_INT64
:
5364 case GLSL_TYPE_UINT
:
5366 error
= !state
->is_version(120, 300) && !state
->EXT_gpu_shader4_enable
;
5368 case GLSL_TYPE_DOUBLE
:
5369 error
= !state
->is_version(410, 0) && !state
->ARB_vertex_attrib_64bit_enable
;
5371 case GLSL_TYPE_SAMPLER
:
5372 case GLSL_TYPE_IMAGE
:
5373 error
= !state
->has_bindless();
5380 _mesa_glsl_error(& loc
, state
,
5381 "vertex shader input / attribute cannot have "
5383 var
->type
->is_array() ? "array of " : "",
5385 } else if (var
->type
->is_array() &&
5386 !state
->check_version(150, 0, &loc
,
5387 "vertex shader input / attribute "
5388 "cannot have array type")) {
5390 } else if (state
->stage
== MESA_SHADER_GEOMETRY
) {
5391 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
5393 * Geometry shader input variables get the per-vertex values
5394 * written out by vertex shader output variables of the same
5395 * names. Since a geometry shader operates on a set of
5396 * vertices, each input varying variable (or input block, see
5397 * interface blocks below) needs to be declared as an array.
5399 if (!var
->type
->is_array()) {
5400 _mesa_glsl_error(&loc
, state
,
5401 "geometry shader inputs must be arrays");
5404 handle_geometry_shader_input_decl(state
, loc
, var
);
5405 } else if (state
->stage
== MESA_SHADER_FRAGMENT
) {
5406 /* From section 4.3.4 (Input Variables) of the GLSL ES 3.10 spec:
5408 * It is a compile-time error to declare a fragment shader
5409 * input with, or that contains, any of the following types:
5413 * * An array of arrays
5414 * * An array of structures
5415 * * A structure containing an array
5416 * * A structure containing a structure
5418 if (state
->es_shader
) {
5419 const glsl_type
*check_type
= var
->type
->without_array();
5420 if (check_type
->is_boolean() ||
5421 check_type
->contains_opaque()) {
5422 _mesa_glsl_error(&loc
, state
,
5423 "fragment shader input cannot have type %s",
5426 if (var
->type
->is_array() &&
5427 var
->type
->fields
.array
->is_array()) {
5428 _mesa_glsl_error(&loc
, state
,
5430 "cannot have an array of arrays",
5431 _mesa_shader_stage_to_string(state
->stage
));
5433 if (var
->type
->is_array() &&
5434 var
->type
->fields
.array
->is_struct()) {
5435 _mesa_glsl_error(&loc
, state
,
5436 "fragment shader input "
5437 "cannot have an array of structs");
5439 if (var
->type
->is_struct()) {
5440 for (unsigned i
= 0; i
< var
->type
->length
; i
++) {
5441 if (var
->type
->fields
.structure
[i
].type
->is_array() ||
5442 var
->type
->fields
.structure
[i
].type
->is_struct())
5443 _mesa_glsl_error(&loc
, state
,
5444 "fragment shader input cannot have "
5445 "a struct that contains an "
5450 } else if (state
->stage
== MESA_SHADER_TESS_CTRL
||
5451 state
->stage
== MESA_SHADER_TESS_EVAL
) {
5452 handle_tess_shader_input_decl(state
, loc
, var
);
5454 } else if (var
->data
.mode
== ir_var_shader_out
) {
5455 const glsl_type
*check_type
= var
->type
->without_array();
5457 /* From section 4.3.6 (Output variables) of the GLSL 4.40 spec:
5459 * It is a compile-time error to declare a fragment shader output
5460 * that contains any of the following:
5462 * * A Boolean type (bool, bvec2 ...)
5463 * * A double-precision scalar or vector (double, dvec2 ...)
5468 if (state
->stage
== MESA_SHADER_FRAGMENT
) {
5469 if (check_type
->is_struct() || check_type
->is_matrix())
5470 _mesa_glsl_error(&loc
, state
,
5471 "fragment shader output "
5472 "cannot have struct or matrix type");
5473 switch (check_type
->base_type
) {
5474 case GLSL_TYPE_UINT
:
5476 case GLSL_TYPE_FLOAT
:
5479 _mesa_glsl_error(&loc
, state
,
5480 "fragment shader output cannot have "
5481 "type %s", check_type
->name
);
5485 /* From section 4.3.6 (Output Variables) of the GLSL ES 3.10 spec:
5487 * It is a compile-time error to declare a vertex shader output
5488 * with, or that contains, any of the following types:
5492 * * An array of arrays
5493 * * An array of structures
5494 * * A structure containing an array
5495 * * A structure containing a structure
5497 * It is a compile-time error to declare a fragment shader output
5498 * with, or that contains, any of the following types:
5504 * * An array of array
5506 * ES 3.20 updates this to apply to tessellation and geometry shaders
5507 * as well. Because there are per-vertex arrays in the new stages,
5508 * it strikes the "array of..." rules and replaces them with these:
5510 * * For per-vertex-arrayed variables (applies to tessellation
5511 * control, tessellation evaluation and geometry shaders):
5513 * * Per-vertex-arrayed arrays of arrays
5514 * * Per-vertex-arrayed arrays of structures
5516 * * For non-per-vertex-arrayed variables:
5518 * * An array of arrays
5519 * * An array of structures
5521 * which basically says to unwrap the per-vertex aspect and apply
5524 if (state
->es_shader
) {
5525 if (var
->type
->is_array() &&
5526 var
->type
->fields
.array
->is_array()) {
5527 _mesa_glsl_error(&loc
, state
,
5529 "cannot have an array of arrays",
5530 _mesa_shader_stage_to_string(state
->stage
));
5532 if (state
->stage
<= MESA_SHADER_GEOMETRY
) {
5533 const glsl_type
*type
= var
->type
;
5535 if (state
->stage
== MESA_SHADER_TESS_CTRL
&&
5536 !var
->data
.patch
&& var
->type
->is_array()) {
5537 type
= var
->type
->fields
.array
;
5540 if (type
->is_array() && type
->fields
.array
->is_struct()) {
5541 _mesa_glsl_error(&loc
, state
,
5542 "%s shader output cannot have "
5543 "an array of structs",
5544 _mesa_shader_stage_to_string(state
->stage
));
5546 if (type
->is_struct()) {
5547 for (unsigned i
= 0; i
< type
->length
; i
++) {
5548 if (type
->fields
.structure
[i
].type
->is_array() ||
5549 type
->fields
.structure
[i
].type
->is_struct())
5550 _mesa_glsl_error(&loc
, state
,
5551 "%s shader output cannot have a "
5552 "struct that contains an "
5554 _mesa_shader_stage_to_string(state
->stage
));
5560 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
5561 handle_tess_ctrl_shader_output_decl(state
, loc
, var
);
5563 } else if (var
->type
->contains_subroutine()) {
5564 /* declare subroutine uniforms as hidden */
5565 var
->data
.how_declared
= ir_var_hidden
;
5568 /* From section 4.3.4 of the GLSL 4.00 spec:
5569 * "Input variables may not be declared using the patch in qualifier
5570 * in tessellation control or geometry shaders."
5572 * From section 4.3.6 of the GLSL 4.00 spec:
5573 * "It is an error to use patch out in a vertex, tessellation
5574 * evaluation, or geometry shader."
5576 * This doesn't explicitly forbid using them in a fragment shader, but
5577 * that's probably just an oversight.
5579 if (state
->stage
!= MESA_SHADER_TESS_EVAL
5580 && this->type
->qualifier
.flags
.q
.patch
5581 && this->type
->qualifier
.flags
.q
.in
) {
5583 _mesa_glsl_error(&loc
, state
, "'patch in' can only be used in a "
5584 "tessellation evaluation shader");
5587 if (state
->stage
!= MESA_SHADER_TESS_CTRL
5588 && this->type
->qualifier
.flags
.q
.patch
5589 && this->type
->qualifier
.flags
.q
.out
) {
5591 _mesa_glsl_error(&loc
, state
, "'patch out' can only be used in a "
5592 "tessellation control shader");
5595 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
5597 if (this->type
->qualifier
.precision
!= ast_precision_none
) {
5598 state
->check_precision_qualifiers_allowed(&loc
);
5601 if (this->type
->qualifier
.precision
!= ast_precision_none
&&
5602 !precision_qualifier_allowed(var
->type
)) {
5603 _mesa_glsl_error(&loc
, state
,
5604 "precision qualifiers apply only to floating point"
5605 ", integer and opaque types");
5608 /* From section 4.1.7 of the GLSL 4.40 spec:
5610 * "[Opaque types] can only be declared as function
5611 * parameters or uniform-qualified variables."
5613 * From section 4.1.7 of the ARB_bindless_texture spec:
5615 * "Samplers may be declared as shader inputs and outputs, as uniform
5616 * variables, as temporary variables, and as function parameters."
5618 * From section 4.1.X of the ARB_bindless_texture spec:
5620 * "Images may be declared as shader inputs and outputs, as uniform
5621 * variables, as temporary variables, and as function parameters."
5623 if (!this->type
->qualifier
.flags
.q
.uniform
&&
5624 (var_type
->contains_atomic() ||
5625 (!state
->has_bindless() && var_type
->contains_opaque()))) {
5626 _mesa_glsl_error(&loc
, state
,
5627 "%s variables must be declared uniform",
5628 state
->has_bindless() ? "atomic" : "opaque");
5631 /* Process the initializer and add its instructions to a temporary
5632 * list. This list will be added to the instruction stream (below) after
5633 * the declaration is added. This is done because in some cases (such as
5634 * redeclarations) the declaration may not actually be added to the
5635 * instruction stream.
5637 exec_list initializer_instructions
;
5639 /* Examine var name here since var may get deleted in the next call */
5640 bool var_is_gl_id
= is_gl_identifier(var
->name
);
5642 bool is_redeclaration
;
5643 var
= get_variable_being_redeclared(&var
, decl
->get_location(), state
,
5644 false /* allow_all_redeclarations */,
5646 if (is_redeclaration
) {
5648 var
->data
.how_declared
== ir_var_declared_in_block
) {
5649 _mesa_glsl_error(&loc
, state
,
5650 "`%s' has already been redeclared using "
5651 "gl_PerVertex", var
->name
);
5653 var
->data
.how_declared
= ir_var_declared_normally
;
5656 if (decl
->initializer
!= NULL
) {
5657 result
= process_initializer(var
,
5659 &initializer_instructions
, state
);
5661 validate_array_dimensions(var_type
, state
, &loc
);
5664 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
5666 * "It is an error to write to a const variable outside of
5667 * its declaration, so they must be initialized when
5670 if (this->type
->qualifier
.flags
.q
.constant
&& decl
->initializer
== NULL
) {
5671 _mesa_glsl_error(& loc
, state
,
5672 "const declaration of `%s' must be initialized",
5676 if (state
->es_shader
) {
5677 const glsl_type
*const t
= var
->type
;
5679 /* Skip the unsized array check for TCS/TES/GS inputs & TCS outputs.
5681 * The GL_OES_tessellation_shader spec says about inputs:
5683 * "Declaring an array size is optional. If no size is specified,
5684 * it will be taken from the implementation-dependent maximum
5685 * patch size (gl_MaxPatchVertices)."
5687 * and about TCS outputs:
5689 * "If no size is specified, it will be taken from output patch
5690 * size declared in the shader."
5692 * The GL_OES_geometry_shader spec says:
5694 * "All geometry shader input unsized array declarations will be
5695 * sized by an earlier input primitive layout qualifier, when
5696 * present, as per the following table."
5698 const bool implicitly_sized
=
5699 (var
->data
.mode
== ir_var_shader_in
&&
5700 state
->stage
>= MESA_SHADER_TESS_CTRL
&&
5701 state
->stage
<= MESA_SHADER_GEOMETRY
) ||
5702 (var
->data
.mode
== ir_var_shader_out
&&
5703 state
->stage
== MESA_SHADER_TESS_CTRL
);
5705 if (t
->is_unsized_array() && !implicitly_sized
)
5706 /* Section 10.17 of the GLSL ES 1.00 specification states that
5707 * unsized array declarations have been removed from the language.
5708 * Arrays that are sized using an initializer are still explicitly
5709 * sized. However, GLSL ES 1.00 does not allow array
5710 * initializers. That is only allowed in GLSL ES 3.00.
5712 * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
5714 * "An array type can also be formed without specifying a size
5715 * if the definition includes an initializer:
5717 * float x[] = float[2] (1.0, 2.0); // declares an array of size 2
5718 * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
5723 _mesa_glsl_error(& loc
, state
,
5724 "unsized array declarations are not allowed in "
5728 /* Section 4.4.6.1 Atomic Counter Layout Qualifiers of the GLSL 4.60 spec:
5730 * "It is a compile-time error to declare an unsized array of
5733 if (var
->type
->is_unsized_array() &&
5734 var
->type
->without_array()->base_type
== GLSL_TYPE_ATOMIC_UINT
) {
5735 _mesa_glsl_error(& loc
, state
,
5736 "Unsized array of atomic_uint is not allowed");
5739 /* If the declaration is not a redeclaration, there are a few additional
5740 * semantic checks that must be applied. In addition, variable that was
5741 * created for the declaration should be added to the IR stream.
5743 if (!is_redeclaration
) {
5744 validate_identifier(decl
->identifier
, loc
, state
);
5746 /* Add the variable to the symbol table. Note that the initializer's
5747 * IR was already processed earlier (though it hasn't been emitted
5748 * yet), without the variable in scope.
5750 * This differs from most C-like languages, but it follows the GLSL
5751 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
5754 * "Within a declaration, the scope of a name starts immediately
5755 * after the initializer if present or immediately after the name
5756 * being declared if not."
5758 if (!state
->symbols
->add_variable(var
)) {
5759 YYLTYPE loc
= this->get_location();
5760 _mesa_glsl_error(&loc
, state
, "name `%s' already taken in the "
5761 "current scope", decl
->identifier
);
5765 /* Push the variable declaration to the top. It means that all the
5766 * variable declarations will appear in a funny last-to-first order,
5767 * but otherwise we run into trouble if a function is prototyped, a
5768 * global var is decled, then the function is defined with usage of
5769 * the global var. See glslparsertest's CorrectModule.frag.
5771 instructions
->push_head(var
);
5774 instructions
->append_list(&initializer_instructions
);
5778 /* Generally, variable declarations do not have r-values. However,
5779 * one is used for the declaration in
5781 * while (bool b = some_condition()) {
5785 * so we return the rvalue from the last seen declaration here.
5792 ast_parameter_declarator::hir(exec_list
*instructions
,
5793 struct _mesa_glsl_parse_state
*state
)
5796 const struct glsl_type
*type
;
5797 const char *name
= NULL
;
5798 YYLTYPE loc
= this->get_location();
5800 type
= this->type
->glsl_type(& name
, state
);
5804 _mesa_glsl_error(& loc
, state
,
5805 "invalid type `%s' in declaration of `%s'",
5806 name
, this->identifier
);
5808 _mesa_glsl_error(& loc
, state
,
5809 "invalid type in declaration of `%s'",
5813 type
= glsl_type::error_type
;
5816 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
5818 * "Functions that accept no input arguments need not use void in the
5819 * argument list because prototypes (or definitions) are required and
5820 * therefore there is no ambiguity when an empty argument list "( )" is
5821 * declared. The idiom "(void)" as a parameter list is provided for
5824 * Placing this check here prevents a void parameter being set up
5825 * for a function, which avoids tripping up checks for main taking
5826 * parameters and lookups of an unnamed symbol.
5828 if (type
->is_void()) {
5829 if (this->identifier
!= NULL
)
5830 _mesa_glsl_error(& loc
, state
,
5831 "named parameter cannot have type `void'");
5837 if (formal_parameter
&& (this->identifier
== NULL
)) {
5838 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
5842 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
5843 * call already handled the "vec4[..] foo" case.
5845 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
5847 if (!type
->is_error() && type
->is_unsized_array()) {
5848 _mesa_glsl_error(&loc
, state
, "arrays passed as parameters must have "
5850 type
= glsl_type::error_type
;
5854 ir_variable
*var
= new(ctx
)
5855 ir_variable(type
, this->identifier
, ir_var_function_in
);
5857 /* Apply any specified qualifiers to the parameter declaration. Note that
5858 * for function parameters the default mode is 'in'.
5860 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
,
5863 if (((1u << var
->data
.mode
) & state
->zero_init
) &&
5864 (var
->type
->is_numeric() || var
->type
->is_boolean())) {
5865 const ir_constant_data data
= { { 0 } };
5866 var
->data
.has_initializer
= true;
5867 var
->data
.is_implicit_initializer
= true;
5868 var
->constant_initializer
= new(var
) ir_constant(var
->type
, &data
);
5871 /* From section 4.1.7 of the GLSL 4.40 spec:
5873 * "Opaque variables cannot be treated as l-values; hence cannot
5874 * be used as out or inout function parameters, nor can they be
5877 * From section 4.1.7 of the ARB_bindless_texture spec:
5879 * "Samplers can be used as l-values, so can be assigned into and used
5880 * as "out" and "inout" function parameters."
5882 * From section 4.1.X of the ARB_bindless_texture spec:
5884 * "Images can be used as l-values, so can be assigned into and used as
5885 * "out" and "inout" function parameters."
5887 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
5888 && (type
->contains_atomic() ||
5889 (!state
->has_bindless() && type
->contains_opaque()))) {
5890 _mesa_glsl_error(&loc
, state
, "out and inout parameters cannot "
5891 "contain %s variables",
5892 state
->has_bindless() ? "atomic" : "opaque");
5893 type
= glsl_type::error_type
;
5896 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
5898 * "When calling a function, expressions that do not evaluate to
5899 * l-values cannot be passed to parameters declared as out or inout."
5901 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
5903 * "Other binary or unary expressions, non-dereferenced arrays,
5904 * function names, swizzles with repeated fields, and constants
5905 * cannot be l-values."
5907 * So for GLSL 1.10, passing an array as an out or inout parameter is not
5908 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
5910 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
5912 && !state
->check_version(120, 100, &loc
,
5913 "arrays cannot be out or inout parameters")) {
5914 type
= glsl_type::error_type
;
5917 instructions
->push_tail(var
);
5919 /* Parameter declarations do not have r-values.
5926 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
5928 exec_list
*ir_parameters
,
5929 _mesa_glsl_parse_state
*state
)
5931 ast_parameter_declarator
*void_param
= NULL
;
5934 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
5935 param
->formal_parameter
= formal
;
5936 param
->hir(ir_parameters
, state
);
5944 if ((void_param
!= NULL
) && (count
> 1)) {
5945 YYLTYPE loc
= void_param
->get_location();
5947 _mesa_glsl_error(& loc
, state
,
5948 "`void' parameter must be only parameter");
5954 emit_function(_mesa_glsl_parse_state
*state
, ir_function
*f
)
5956 /* IR invariants disallow function declarations or definitions
5957 * nested within other function definitions. But there is no
5958 * requirement about the relative order of function declarations
5959 * and definitions with respect to one another. So simply insert
5960 * the new ir_function block at the end of the toplevel instruction
5963 state
->toplevel_ir
->push_tail(f
);
5968 ast_function::hir(exec_list
*instructions
,
5969 struct _mesa_glsl_parse_state
*state
)
5972 ir_function
*f
= NULL
;
5973 ir_function_signature
*sig
= NULL
;
5974 exec_list hir_parameters
;
5975 YYLTYPE loc
= this->get_location();
5977 const char *const name
= identifier
;
5979 /* New functions are always added to the top-level IR instruction stream,
5980 * so this instruction list pointer is ignored. See also emit_function
5983 (void) instructions
;
5985 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
5987 * "Function declarations (prototypes) cannot occur inside of functions;
5988 * they must be at global scope, or for the built-in functions, outside
5989 * the global scope."
5991 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
5993 * "User defined functions may only be defined within the global scope."
5995 * Note that this language does not appear in GLSL 1.10.
5997 if ((state
->current_function
!= NULL
) &&
5998 state
->is_version(120, 100)) {
5999 YYLTYPE loc
= this->get_location();
6000 _mesa_glsl_error(&loc
, state
,
6001 "declaration of function `%s' not allowed within "
6002 "function body", name
);
6005 validate_identifier(name
, this->get_location(), state
);
6007 /* Convert the list of function parameters to HIR now so that they can be
6008 * used below to compare this function's signature with previously seen
6009 * signatures for functions with the same name.
6011 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
6013 & hir_parameters
, state
);
6015 const char *return_type_name
;
6016 const glsl_type
*return_type
=
6017 this->return_type
->glsl_type(& return_type_name
, state
);
6020 YYLTYPE loc
= this->get_location();
6021 _mesa_glsl_error(&loc
, state
,
6022 "function `%s' has undeclared return type `%s'",
6023 name
, return_type_name
);
6024 return_type
= glsl_type::error_type
;
6027 /* ARB_shader_subroutine states:
6028 * "Subroutine declarations cannot be prototyped. It is an error to prepend
6029 * subroutine(...) to a function declaration."
6031 if (this->return_type
->qualifier
.subroutine_list
&& !is_definition
) {
6032 YYLTYPE loc
= this->get_location();
6033 _mesa_glsl_error(&loc
, state
,
6034 "function declaration `%s' cannot have subroutine prepended",
6038 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
6039 * "No qualifier is allowed on the return type of a function."
6041 if (this->return_type
->has_qualifiers(state
)) {
6042 YYLTYPE loc
= this->get_location();
6043 _mesa_glsl_error(& loc
, state
,
6044 "function `%s' return type has qualifiers", name
);
6047 /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
6049 * "Arrays are allowed as arguments and as the return type. In both
6050 * cases, the array must be explicitly sized."
6052 if (return_type
->is_unsized_array()) {
6053 YYLTYPE loc
= this->get_location();
6054 _mesa_glsl_error(& loc
, state
,
6055 "function `%s' return type array must be explicitly "
6059 /* From Section 6.1 (Function Definitions) of the GLSL 1.00 spec:
6061 * "Arrays are allowed as arguments, but not as the return type. [...]
6062 * The return type can also be a structure if the structure does not
6063 * contain an array."
6065 if (state
->language_version
== 100 && return_type
->contains_array()) {
6066 YYLTYPE loc
= this->get_location();
6067 _mesa_glsl_error(& loc
, state
,
6068 "function `%s' return type contains an array", name
);
6071 /* From section 4.1.7 of the GLSL 4.40 spec:
6073 * "[Opaque types] can only be declared as function parameters
6074 * or uniform-qualified variables."
6076 * The ARB_bindless_texture spec doesn't clearly state this, but as it says
6077 * "Replace Section 4.1.7 (Samplers), p. 25" and, "Replace Section 4.1.X,
6078 * (Images)", this should be allowed.
6080 if (return_type
->contains_atomic() ||
6081 (!state
->has_bindless() && return_type
->contains_opaque())) {
6082 YYLTYPE loc
= this->get_location();
6083 _mesa_glsl_error(&loc
, state
,
6084 "function `%s' return type can't contain an %s type",
6085 name
, state
->has_bindless() ? "atomic" : "opaque");
6089 if (return_type
->is_subroutine()) {
6090 YYLTYPE loc
= this->get_location();
6091 _mesa_glsl_error(&loc
, state
,
6092 "function `%s' return type can't be a subroutine type",
6096 /* Get the precision for the return type */
6097 unsigned return_precision
;
6099 if (state
->es_shader
) {
6100 YYLTYPE loc
= this->get_location();
6102 select_gles_precision(this->return_type
->qualifier
.precision
,
6107 return_precision
= GLSL_PRECISION_NONE
;
6110 /* Create an ir_function if one doesn't already exist. */
6111 f
= state
->symbols
->get_function(name
);
6113 f
= new(ctx
) ir_function(name
);
6114 if (!this->return_type
->qualifier
.is_subroutine_decl()) {
6115 if (!state
->symbols
->add_function(f
)) {
6116 /* This function name shadows a non-function use of the same name. */
6117 YYLTYPE loc
= this->get_location();
6118 _mesa_glsl_error(&loc
, state
, "function name `%s' conflicts with "
6119 "non-function", name
);
6123 emit_function(state
, f
);
6126 /* From GLSL ES 3.0 spec, chapter 6.1 "Function Definitions", page 71:
6128 * "A shader cannot redefine or overload built-in functions."
6130 * While in GLSL ES 1.0 specification, chapter 8 "Built-in Functions":
6132 * "User code can overload the built-in functions but cannot redefine
6135 if (state
->es_shader
) {
6136 /* Local shader has no exact candidates; check the built-ins. */
6137 if (state
->language_version
>= 300 &&
6138 _mesa_glsl_has_builtin_function(state
, name
)) {
6139 YYLTYPE loc
= this->get_location();
6140 _mesa_glsl_error(& loc
, state
,
6141 "A shader cannot redefine or overload built-in "
6142 "function `%s' in GLSL ES 3.00", name
);
6146 if (state
->language_version
== 100) {
6147 ir_function_signature
*sig
=
6148 _mesa_glsl_find_builtin_function(state
, name
, &hir_parameters
);
6149 if (sig
&& sig
->is_builtin()) {
6150 _mesa_glsl_error(& loc
, state
,
6151 "A shader cannot redefine built-in "
6152 "function `%s' in GLSL ES 1.00", name
);
6157 /* Verify that this function's signature either doesn't match a previously
6158 * seen signature for a function with the same name, or, if a match is found,
6159 * that the previously seen signature does not have an associated definition.
6161 if (state
->es_shader
|| f
->has_user_signature()) {
6162 sig
= f
->exact_matching_signature(state
, &hir_parameters
);
6164 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
6165 if (badvar
!= NULL
) {
6166 YYLTYPE loc
= this->get_location();
6168 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
6169 "qualifiers don't match prototype", name
, badvar
);
6172 if (sig
->return_type
!= return_type
) {
6173 YYLTYPE loc
= this->get_location();
6175 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
6176 "match prototype", name
);
6179 if (sig
->return_precision
!= return_precision
) {
6180 YYLTYPE loc
= this->get_location();
6182 _mesa_glsl_error(&loc
, state
, "function `%s' return type precision "
6183 "doesn't match prototype", name
);
6186 if (sig
->is_defined
) {
6187 if (is_definition
) {
6188 YYLTYPE loc
= this->get_location();
6189 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
6191 /* We just encountered a prototype that exactly matches a
6192 * function that's already been defined. This is redundant,
6193 * and we should ignore it.
6197 } else if (state
->language_version
== 100 && !is_definition
) {
6198 /* From the GLSL 1.00 spec, section 4.2.7:
6200 * "A particular variable, structure or function declaration
6201 * may occur at most once within a scope with the exception
6202 * that a single function prototype plus the corresponding
6203 * function definition are allowed."
6205 YYLTYPE loc
= this->get_location();
6206 _mesa_glsl_error(&loc
, state
, "function `%s' redeclared", name
);
6211 /* Verify the return type of main() */
6212 if (strcmp(name
, "main") == 0) {
6213 if (! return_type
->is_void()) {
6214 YYLTYPE loc
= this->get_location();
6216 _mesa_glsl_error(& loc
, state
, "main() must return void");
6219 if (!hir_parameters
.is_empty()) {
6220 YYLTYPE loc
= this->get_location();
6222 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
6226 /* Finish storing the information about this new function in its signature.
6229 sig
= new(ctx
) ir_function_signature(return_type
);
6230 sig
->return_precision
= return_precision
;
6231 f
->add_signature(sig
);
6234 sig
->replace_parameters(&hir_parameters
);
6237 if (this->return_type
->qualifier
.subroutine_list
) {
6240 if (this->return_type
->qualifier
.flags
.q
.explicit_index
) {
6241 unsigned qual_index
;
6242 if (process_qualifier_constant(state
, &loc
, "index",
6243 this->return_type
->qualifier
.index
,
6245 if (!state
->has_explicit_uniform_location()) {
6246 _mesa_glsl_error(&loc
, state
, "subroutine index requires "
6247 "GL_ARB_explicit_uniform_location or "
6249 } else if (qual_index
>= MAX_SUBROUTINES
) {
6250 _mesa_glsl_error(&loc
, state
,
6251 "invalid subroutine index (%d) index must "
6252 "be a number between 0 and "
6253 "GL_MAX_SUBROUTINES - 1 (%d)", qual_index
,
6254 MAX_SUBROUTINES
- 1);
6256 f
->subroutine_index
= qual_index
;
6261 f
->num_subroutine_types
= this->return_type
->qualifier
.subroutine_list
->declarations
.length();
6262 f
->subroutine_types
= ralloc_array(state
, const struct glsl_type
*,
6263 f
->num_subroutine_types
);
6265 foreach_list_typed(ast_declaration
, decl
, link
, &this->return_type
->qualifier
.subroutine_list
->declarations
) {
6266 const struct glsl_type
*type
;
6267 /* the subroutine type must be already declared */
6268 type
= state
->symbols
->get_type(decl
->identifier
);
6270 _mesa_glsl_error(& loc
, state
, "unknown type '%s' in subroutine function definition", decl
->identifier
);
6273 for (int i
= 0; i
< state
->num_subroutine_types
; i
++) {
6274 ir_function
*fn
= state
->subroutine_types
[i
];
6275 ir_function_signature
*tsig
= NULL
;
6277 if (strcmp(fn
->name
, decl
->identifier
))
6280 tsig
= fn
->matching_signature(state
, &sig
->parameters
,
6283 _mesa_glsl_error(& loc
, state
, "subroutine type mismatch '%s' - signatures do not match\n", decl
->identifier
);
6285 if (tsig
->return_type
!= sig
->return_type
) {
6286 _mesa_glsl_error(& loc
, state
, "subroutine type mismatch '%s' - return types do not match\n", decl
->identifier
);
6290 f
->subroutine_types
[idx
++] = type
;
6292 state
->subroutines
= (ir_function
**)reralloc(state
, state
->subroutines
,
6294 state
->num_subroutines
+ 1);
6295 state
->subroutines
[state
->num_subroutines
] = f
;
6296 state
->num_subroutines
++;
6300 if (this->return_type
->qualifier
.is_subroutine_decl()) {
6301 if (!state
->symbols
->add_type(this->identifier
, glsl_type::get_subroutine_instance(this->identifier
))) {
6302 _mesa_glsl_error(& loc
, state
, "type '%s' previously defined", this->identifier
);
6305 state
->subroutine_types
= (ir_function
**)reralloc(state
, state
->subroutine_types
,
6307 state
->num_subroutine_types
+ 1);
6308 state
->subroutine_types
[state
->num_subroutine_types
] = f
;
6309 state
->num_subroutine_types
++;
6311 f
->is_subroutine
= true;
6314 /* Function declarations (prototypes) do not have r-values.
6321 ast_function_definition::hir(exec_list
*instructions
,
6322 struct _mesa_glsl_parse_state
*state
)
6324 prototype
->is_definition
= true;
6325 prototype
->hir(instructions
, state
);
6327 ir_function_signature
*signature
= prototype
->signature
;
6328 if (signature
== NULL
)
6331 assert(state
->current_function
== NULL
);
6332 state
->current_function
= signature
;
6333 state
->found_return
= false;
6334 state
->found_begin_interlock
= false;
6335 state
->found_end_interlock
= false;
6337 /* Duplicate parameters declared in the prototype as concrete variables.
6338 * Add these to the symbol table.
6340 state
->symbols
->push_scope();
6341 foreach_in_list(ir_variable
, var
, &signature
->parameters
) {
6342 assert(var
->as_variable() != NULL
);
6344 /* The only way a parameter would "exist" is if two parameters have
6347 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
6348 YYLTYPE loc
= this->get_location();
6350 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
6352 state
->symbols
->add_variable(var
);
6356 /* Convert the body of the function to HIR. */
6357 this->body
->hir(&signature
->body
, state
);
6358 signature
->is_defined
= true;
6360 state
->symbols
->pop_scope();
6362 assert(state
->current_function
== signature
);
6363 state
->current_function
= NULL
;
6365 if (!signature
->return_type
->is_void() && !state
->found_return
) {
6366 YYLTYPE loc
= this->get_location();
6367 _mesa_glsl_error(& loc
, state
, "function `%s' has non-void return type "
6368 "%s, but no return statement",
6369 signature
->function_name(),
6370 signature
->return_type
->name
);
6373 /* Function definitions do not have r-values.
6380 ast_jump_statement::hir(exec_list
*instructions
,
6381 struct _mesa_glsl_parse_state
*state
)
6388 assert(state
->current_function
);
6390 if (opt_return_value
) {
6391 ir_rvalue
*ret
= opt_return_value
->hir(instructions
, state
);
6393 /* The value of the return type can be NULL if the shader says
6394 * 'return foo();' and foo() is a function that returns void.
6396 * NOTE: The GLSL spec doesn't say that this is an error. The type
6397 * of the return value is void. If the return type of the function is
6398 * also void, then this should compile without error. Seriously.
6400 const glsl_type
*const ret_type
=
6401 (ret
== NULL
) ? glsl_type::void_type
: ret
->type
;
6403 /* Implicit conversions are not allowed for return values prior to
6404 * ARB_shading_language_420pack.
6406 if (state
->current_function
->return_type
!= ret_type
) {
6407 YYLTYPE loc
= this->get_location();
6409 if (state
->has_420pack()) {
6410 if (!apply_implicit_conversion(state
->current_function
->return_type
,
6412 || (ret
->type
!= state
->current_function
->return_type
)) {
6413 _mesa_glsl_error(& loc
, state
,
6414 "could not implicitly convert return value "
6415 "to %s, in function `%s'",
6416 state
->current_function
->return_type
->name
,
6417 state
->current_function
->function_name());
6420 _mesa_glsl_error(& loc
, state
,
6421 "`return' with wrong type %s, in function `%s' "
6424 state
->current_function
->function_name(),
6425 state
->current_function
->return_type
->name
);
6427 } else if (state
->current_function
->return_type
->base_type
==
6429 YYLTYPE loc
= this->get_location();
6431 /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
6432 * specs add a clarification:
6434 * "A void function can only use return without a return argument, even if
6435 * the return argument has void type. Return statements only accept values:
6438 * void func2() { return func1(); } // illegal return statement"
6440 _mesa_glsl_error(& loc
, state
,
6441 "void functions can only use `return' without a "
6445 inst
= new(ctx
) ir_return(ret
);
6447 if (state
->current_function
->return_type
->base_type
!=
6449 YYLTYPE loc
= this->get_location();
6451 _mesa_glsl_error(& loc
, state
,
6452 "`return' with no value, in function %s returning "
6454 state
->current_function
->function_name());
6456 inst
= new(ctx
) ir_return
;
6459 state
->found_return
= true;
6460 instructions
->push_tail(inst
);
6465 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
6466 YYLTYPE loc
= this->get_location();
6468 _mesa_glsl_error(& loc
, state
,
6469 "`discard' may only appear in a fragment shader");
6471 instructions
->push_tail(new(ctx
) ir_discard
);
6476 if (mode
== ast_continue
&&
6477 state
->loop_nesting_ast
== NULL
) {
6478 YYLTYPE loc
= this->get_location();
6480 _mesa_glsl_error(& loc
, state
, "continue may only appear in a loop");
6481 } else if (mode
== ast_break
&&
6482 state
->loop_nesting_ast
== NULL
&&
6483 state
->switch_state
.switch_nesting_ast
== NULL
) {
6484 YYLTYPE loc
= this->get_location();
6486 _mesa_glsl_error(& loc
, state
,
6487 "break may only appear in a loop or a switch");
6489 /* For a loop, inline the for loop expression again, since we don't
6490 * know where near the end of the loop body the normal copy of it is
6491 * going to be placed. Same goes for the condition for a do-while
6494 if (state
->loop_nesting_ast
!= NULL
&&
6495 mode
== ast_continue
&& !state
->switch_state
.is_switch_innermost
) {
6496 if (state
->loop_nesting_ast
->rest_expression
) {
6497 state
->loop_nesting_ast
->rest_expression
->hir(instructions
,
6500 if (state
->loop_nesting_ast
->mode
==
6501 ast_iteration_statement::ast_do_while
) {
6502 state
->loop_nesting_ast
->condition_to_hir(instructions
, state
);
6506 if (state
->switch_state
.is_switch_innermost
&&
6507 mode
== ast_continue
) {
6508 /* Set 'continue_inside' to true. */
6509 ir_rvalue
*const true_val
= new (ctx
) ir_constant(true);
6510 ir_dereference_variable
*deref_continue_inside_var
=
6511 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
6512 instructions
->push_tail(new(ctx
) ir_assignment(deref_continue_inside_var
,
6515 /* Break out from the switch, continue for the loop will
6516 * be called right after switch. */
6517 ir_loop_jump
*const jump
=
6518 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
6519 instructions
->push_tail(jump
);
6521 } else if (state
->switch_state
.is_switch_innermost
&&
6522 mode
== ast_break
) {
6523 /* Force break out of switch by inserting a break. */
6524 ir_loop_jump
*const jump
=
6525 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
6526 instructions
->push_tail(jump
);
6528 ir_loop_jump
*const jump
=
6529 new(ctx
) ir_loop_jump((mode
== ast_break
)
6530 ? ir_loop_jump::jump_break
6531 : ir_loop_jump::jump_continue
);
6532 instructions
->push_tail(jump
);
6539 /* Jump instructions do not have r-values.
6546 ast_demote_statement::hir(exec_list
*instructions
,
6547 struct _mesa_glsl_parse_state
*state
)
6551 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
6552 YYLTYPE loc
= this->get_location();
6554 _mesa_glsl_error(& loc
, state
,
6555 "`demote' may only appear in a fragment shader");
6558 instructions
->push_tail(new(ctx
) ir_demote
);
6565 ast_selection_statement::hir(exec_list
*instructions
,
6566 struct _mesa_glsl_parse_state
*state
)
6570 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
6572 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
6574 * "Any expression whose type evaluates to a Boolean can be used as the
6575 * conditional expression bool-expression. Vector types are not accepted
6576 * as the expression to if."
6578 * The checks are separated so that higher quality diagnostics can be
6579 * generated for cases where both rules are violated.
6581 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
6582 YYLTYPE loc
= this->condition
->get_location();
6584 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
6588 ir_if
*const stmt
= new(ctx
) ir_if(condition
);
6590 if (then_statement
!= NULL
) {
6591 state
->symbols
->push_scope();
6592 then_statement
->hir(& stmt
->then_instructions
, state
);
6593 state
->symbols
->pop_scope();
6596 if (else_statement
!= NULL
) {
6597 state
->symbols
->push_scope();
6598 else_statement
->hir(& stmt
->else_instructions
, state
);
6599 state
->symbols
->pop_scope();
6602 instructions
->push_tail(stmt
);
6604 /* if-statements do not have r-values.
6611 /** Value of the case label. */
6614 /** Does this label occur after the default? */
6618 * AST for the case label.
6620 * This is only used to generate error messages for duplicate labels.
6622 ast_expression
*ast
;
6625 /* Used for detection of duplicate case values, compare
6626 * given contents directly.
6629 compare_case_value(const void *a
, const void *b
)
6631 return ((struct case_label
*) a
)->value
== ((struct case_label
*) b
)->value
;
6635 /* Used for detection of duplicate case values, just
6636 * returns key contents as is.
6639 key_contents(const void *key
)
6641 return ((struct case_label
*) key
)->value
;
6646 ast_switch_statement::hir(exec_list
*instructions
,
6647 struct _mesa_glsl_parse_state
*state
)
6651 ir_rvalue
*const test_expression
=
6652 this->test_expression
->hir(instructions
, state
);
6654 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
6656 * "The type of init-expression in a switch statement must be a
6659 if (!test_expression
->type
->is_scalar() ||
6660 !test_expression
->type
->is_integer_32()) {
6661 YYLTYPE loc
= this->test_expression
->get_location();
6663 _mesa_glsl_error(& loc
,
6665 "switch-statement expression must be scalar "
6670 /* Track the switch-statement nesting in a stack-like manner.
6672 struct glsl_switch_state saved
= state
->switch_state
;
6674 state
->switch_state
.is_switch_innermost
= true;
6675 state
->switch_state
.switch_nesting_ast
= this;
6676 state
->switch_state
.labels_ht
=
6677 _mesa_hash_table_create(NULL
, key_contents
,
6678 compare_case_value
);
6679 state
->switch_state
.previous_default
= NULL
;
6681 /* Initalize is_fallthru state to false.
6683 ir_rvalue
*const is_fallthru_val
= new (ctx
) ir_constant(false);
6684 state
->switch_state
.is_fallthru_var
=
6685 new(ctx
) ir_variable(glsl_type::bool_type
,
6686 "switch_is_fallthru_tmp",
6688 instructions
->push_tail(state
->switch_state
.is_fallthru_var
);
6690 ir_dereference_variable
*deref_is_fallthru_var
=
6691 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
6692 instructions
->push_tail(new(ctx
) ir_assignment(deref_is_fallthru_var
,
6695 /* Initialize continue_inside state to false.
6697 state
->switch_state
.continue_inside
=
6698 new(ctx
) ir_variable(glsl_type::bool_type
,
6699 "continue_inside_tmp",
6701 instructions
->push_tail(state
->switch_state
.continue_inside
);
6703 ir_rvalue
*const false_val
= new (ctx
) ir_constant(false);
6704 ir_dereference_variable
*deref_continue_inside_var
=
6705 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
6706 instructions
->push_tail(new(ctx
) ir_assignment(deref_continue_inside_var
,
6709 state
->switch_state
.run_default
=
6710 new(ctx
) ir_variable(glsl_type::bool_type
,
6713 instructions
->push_tail(state
->switch_state
.run_default
);
6715 /* Loop around the switch is used for flow control. */
6716 ir_loop
* loop
= new(ctx
) ir_loop();
6717 instructions
->push_tail(loop
);
6719 /* Cache test expression.
6721 test_to_hir(&loop
->body_instructions
, state
);
6723 /* Emit code for body of switch stmt.
6725 body
->hir(&loop
->body_instructions
, state
);
6727 /* Insert a break at the end to exit loop. */
6728 ir_loop_jump
*jump
= new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
6729 loop
->body_instructions
.push_tail(jump
);
6731 /* If we are inside loop, check if continue got called inside switch. */
6732 if (state
->loop_nesting_ast
!= NULL
) {
6733 ir_dereference_variable
*deref_continue_inside
=
6734 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
6735 ir_if
*irif
= new(ctx
) ir_if(deref_continue_inside
);
6736 ir_loop_jump
*jump
= new(ctx
) ir_loop_jump(ir_loop_jump::jump_continue
);
6738 if (state
->loop_nesting_ast
!= NULL
) {
6739 if (state
->loop_nesting_ast
->rest_expression
) {
6740 state
->loop_nesting_ast
->rest_expression
->hir(&irif
->then_instructions
,
6743 if (state
->loop_nesting_ast
->mode
==
6744 ast_iteration_statement::ast_do_while
) {
6745 state
->loop_nesting_ast
->condition_to_hir(&irif
->then_instructions
, state
);
6748 irif
->then_instructions
.push_tail(jump
);
6749 instructions
->push_tail(irif
);
6752 _mesa_hash_table_destroy(state
->switch_state
.labels_ht
, NULL
);
6754 state
->switch_state
= saved
;
6756 /* Switch statements do not have r-values. */
6762 ast_switch_statement::test_to_hir(exec_list
*instructions
,
6763 struct _mesa_glsl_parse_state
*state
)
6767 /* set to true to avoid a duplicate "use of uninitialized variable" warning
6768 * on the switch test case. The first one would be already raised when
6769 * getting the test_expression at ast_switch_statement::hir
6771 test_expression
->set_is_lhs(true);
6772 /* Cache value of test expression. */
6773 ir_rvalue
*const test_val
= test_expression
->hir(instructions
, state
);
6775 state
->switch_state
.test_var
= new(ctx
) ir_variable(test_val
->type
,
6778 ir_dereference_variable
*deref_test_var
=
6779 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
6781 instructions
->push_tail(state
->switch_state
.test_var
);
6782 instructions
->push_tail(new(ctx
) ir_assignment(deref_test_var
, test_val
));
6787 ast_switch_body::hir(exec_list
*instructions
,
6788 struct _mesa_glsl_parse_state
*state
)
6791 stmts
->hir(instructions
, state
);
6793 /* Switch bodies do not have r-values. */
6798 ast_case_statement_list::hir(exec_list
*instructions
,
6799 struct _mesa_glsl_parse_state
*state
)
6801 exec_list default_case
, after_default
, tmp
;
6803 foreach_list_typed (ast_case_statement
, case_stmt
, link
, & this->cases
) {
6804 case_stmt
->hir(&tmp
, state
);
6807 if (state
->switch_state
.previous_default
&& default_case
.is_empty()) {
6808 default_case
.append_list(&tmp
);
6812 /* If default case found, append 'after_default' list. */
6813 if (!default_case
.is_empty())
6814 after_default
.append_list(&tmp
);
6816 instructions
->append_list(&tmp
);
6819 /* Handle the default case. This is done here because default might not be
6820 * the last case. We need to add checks against following cases first to see
6821 * if default should be chosen or not.
6823 if (!default_case
.is_empty()) {
6824 ir_factory
body(instructions
, state
);
6826 ir_expression
*cmp
= NULL
;
6828 hash_table_foreach(state
->switch_state
.labels_ht
, entry
) {
6829 const struct case_label
*const l
= (struct case_label
*) entry
->data
;
6831 /* If the switch init-value is the value of one of the labels that
6832 * occurs after the default case, disable execution of the default
6835 if (l
->after_default
) {
6836 ir_constant
*const cnst
=
6837 state
->switch_state
.test_var
->type
->base_type
== GLSL_TYPE_UINT
6838 ? body
.constant(unsigned(l
->value
))
6839 : body
.constant(int(l
->value
));
6842 ? equal(cnst
, state
->switch_state
.test_var
)
6843 : logic_or(cmp
, equal(cnst
, state
->switch_state
.test_var
));
6848 body
.emit(assign(state
->switch_state
.run_default
, logic_not(cmp
)));
6850 body
.emit(assign(state
->switch_state
.run_default
, body
.constant(true)));
6852 /* Append default case and all cases after it. */
6853 instructions
->append_list(&default_case
);
6854 instructions
->append_list(&after_default
);
6857 /* Case statements do not have r-values. */
6862 ast_case_statement::hir(exec_list
*instructions
,
6863 struct _mesa_glsl_parse_state
*state
)
6865 labels
->hir(instructions
, state
);
6867 /* Guard case statements depending on fallthru state. */
6868 ir_dereference_variable
*const deref_fallthru_guard
=
6869 new(state
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
6870 ir_if
*const test_fallthru
= new(state
) ir_if(deref_fallthru_guard
);
6872 foreach_list_typed (ast_node
, stmt
, link
, & this->stmts
)
6873 stmt
->hir(& test_fallthru
->then_instructions
, state
);
6875 instructions
->push_tail(test_fallthru
);
6877 /* Case statements do not have r-values. */
6883 ast_case_label_list::hir(exec_list
*instructions
,
6884 struct _mesa_glsl_parse_state
*state
)
6886 foreach_list_typed (ast_case_label
, label
, link
, & this->labels
)
6887 label
->hir(instructions
, state
);
6889 /* Case labels do not have r-values. */
6894 ast_case_label::hir(exec_list
*instructions
,
6895 struct _mesa_glsl_parse_state
*state
)
6897 ir_factory
body(instructions
, state
);
6899 ir_variable
*const fallthru_var
= state
->switch_state
.is_fallthru_var
;
6901 /* If not default case, ... */
6902 if (this->test_value
!= NULL
) {
6903 /* Conditionally set fallthru state based on
6904 * comparison of cached test expression value to case label.
6906 ir_rvalue
*const label_rval
= this->test_value
->hir(instructions
, state
);
6907 ir_constant
*label_const
=
6908 label_rval
->constant_expression_value(body
.mem_ctx
);
6911 YYLTYPE loc
= this->test_value
->get_location();
6913 _mesa_glsl_error(& loc
, state
,
6914 "switch statement case label must be a "
6915 "constant expression");
6917 /* Stuff a dummy value in to allow processing to continue. */
6918 label_const
= body
.constant(0);
6921 _mesa_hash_table_search(state
->switch_state
.labels_ht
,
6922 &label_const
->value
.u
[0]);
6925 const struct case_label
*const l
=
6926 (struct case_label
*) entry
->data
;
6927 const ast_expression
*const previous_label
= l
->ast
;
6928 YYLTYPE loc
= this->test_value
->get_location();
6930 _mesa_glsl_error(& loc
, state
, "duplicate case value");
6932 loc
= previous_label
->get_location();
6933 _mesa_glsl_error(& loc
, state
, "this is the previous case label");
6935 struct case_label
*l
= ralloc(state
->switch_state
.labels_ht
,
6938 l
->value
= label_const
->value
.u
[0];
6939 l
->after_default
= state
->switch_state
.previous_default
!= NULL
;
6940 l
->ast
= this->test_value
;
6942 _mesa_hash_table_insert(state
->switch_state
.labels_ht
,
6943 &label_const
->value
.u
[0],
6948 /* Create an r-value version of the ir_constant label here (after we may
6949 * have created a fake one in error cases) that can be passed to
6950 * apply_implicit_conversion below.
6952 ir_rvalue
*label
= label_const
;
6954 ir_rvalue
*deref_test_var
=
6955 new(body
.mem_ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
6958 * From GLSL 4.40 specification section 6.2 ("Selection"):
6960 * "The type of the init-expression value in a switch statement must
6961 * be a scalar int or uint. The type of the constant-expression value
6962 * in a case label also must be a scalar int or uint. When any pair
6963 * of these values is tested for "equal value" and the types do not
6964 * match, an implicit conversion will be done to convert the int to a
6965 * uint (see section 4.1.10 “Implicit Conversions”) before the compare
6968 if (label
->type
!= state
->switch_state
.test_var
->type
) {
6969 YYLTYPE loc
= this->test_value
->get_location();
6971 const glsl_type
*type_a
= label
->type
;
6972 const glsl_type
*type_b
= state
->switch_state
.test_var
->type
;
6974 /* Check if int->uint implicit conversion is supported. */
6975 bool integer_conversion_supported
=
6976 glsl_type::int_type
->can_implicitly_convert_to(glsl_type::uint_type
,
6979 if ((!type_a
->is_integer_32() || !type_b
->is_integer_32()) ||
6980 !integer_conversion_supported
) {
6981 _mesa_glsl_error(&loc
, state
, "type mismatch with switch "
6982 "init-expression and case label (%s != %s)",
6983 type_a
->name
, type_b
->name
);
6985 /* Conversion of the case label. */
6986 if (type_a
->base_type
== GLSL_TYPE_INT
) {
6987 if (!apply_implicit_conversion(glsl_type::uint_type
,
6989 _mesa_glsl_error(&loc
, state
, "implicit type conversion error");
6991 /* Conversion of the init-expression value. */
6992 if (!apply_implicit_conversion(glsl_type::uint_type
,
6993 deref_test_var
, state
))
6994 _mesa_glsl_error(&loc
, state
, "implicit type conversion error");
6998 /* If the implicit conversion was allowed, the types will already be
6999 * the same. If the implicit conversion wasn't allowed, smash the
7000 * type of the label anyway. This will prevent the expression
7001 * constructor (below) from failing an assertion.
7003 label
->type
= deref_test_var
->type
;
7006 body
.emit(assign(fallthru_var
,
7007 logic_or(fallthru_var
, equal(label
, deref_test_var
))));
7008 } else { /* default case */
7009 if (state
->switch_state
.previous_default
) {
7010 YYLTYPE loc
= this->get_location();
7011 _mesa_glsl_error(& loc
, state
,
7012 "multiple default labels in one switch");
7014 loc
= state
->switch_state
.previous_default
->get_location();
7015 _mesa_glsl_error(& loc
, state
, "this is the first default label");
7017 state
->switch_state
.previous_default
= this;
7019 /* Set fallthru condition on 'run_default' bool. */
7020 body
.emit(assign(fallthru_var
,
7021 logic_or(fallthru_var
,
7022 state
->switch_state
.run_default
)));
7025 /* Case statements do not have r-values. */
7030 ast_iteration_statement::condition_to_hir(exec_list
*instructions
,
7031 struct _mesa_glsl_parse_state
*state
)
7035 if (condition
!= NULL
) {
7036 ir_rvalue
*const cond
=
7037 condition
->hir(instructions
, state
);
7040 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
7041 YYLTYPE loc
= condition
->get_location();
7043 _mesa_glsl_error(& loc
, state
,
7044 "loop condition must be scalar boolean");
7046 /* As the first code in the loop body, generate a block that looks
7047 * like 'if (!condition) break;' as the loop termination condition.
7049 ir_rvalue
*const not_cond
=
7050 new(ctx
) ir_expression(ir_unop_logic_not
, cond
);
7052 ir_if
*const if_stmt
= new(ctx
) ir_if(not_cond
);
7054 ir_jump
*const break_stmt
=
7055 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
7057 if_stmt
->then_instructions
.push_tail(break_stmt
);
7058 instructions
->push_tail(if_stmt
);
7065 ast_iteration_statement::hir(exec_list
*instructions
,
7066 struct _mesa_glsl_parse_state
*state
)
7070 /* For-loops and while-loops start a new scope, but do-while loops do not.
7072 if (mode
!= ast_do_while
)
7073 state
->symbols
->push_scope();
7075 if (init_statement
!= NULL
)
7076 init_statement
->hir(instructions
, state
);
7078 ir_loop
*const stmt
= new(ctx
) ir_loop();
7079 instructions
->push_tail(stmt
);
7081 /* Track the current loop nesting. */
7082 ast_iteration_statement
*nesting_ast
= state
->loop_nesting_ast
;
7084 state
->loop_nesting_ast
= this;
7086 /* Likewise, indicate that following code is closest to a loop,
7087 * NOT closest to a switch.
7089 bool saved_is_switch_innermost
= state
->switch_state
.is_switch_innermost
;
7090 state
->switch_state
.is_switch_innermost
= false;
7092 if (mode
!= ast_do_while
)
7093 condition_to_hir(&stmt
->body_instructions
, state
);
7096 body
->hir(& stmt
->body_instructions
, state
);
7098 if (rest_expression
!= NULL
)
7099 rest_expression
->hir(& stmt
->body_instructions
, state
);
7101 if (mode
== ast_do_while
)
7102 condition_to_hir(&stmt
->body_instructions
, state
);
7104 if (mode
!= ast_do_while
)
7105 state
->symbols
->pop_scope();
7107 /* Restore previous nesting before returning. */
7108 state
->loop_nesting_ast
= nesting_ast
;
7109 state
->switch_state
.is_switch_innermost
= saved_is_switch_innermost
;
7111 /* Loops do not have r-values.
7118 * Determine if the given type is valid for establishing a default precision
7121 * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
7123 * "The precision statement
7125 * precision precision-qualifier type;
7127 * can be used to establish a default precision qualifier. The type field
7128 * can be either int or float or any of the sampler types, and the
7129 * precision-qualifier can be lowp, mediump, or highp."
7131 * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
7132 * qualifiers on sampler types, but this seems like an oversight (since the
7133 * intention of including these in GLSL 1.30 is to allow compatibility with ES
7134 * shaders). So we allow int, float, and all sampler types regardless of GLSL
7138 is_valid_default_precision_type(const struct glsl_type
*const type
)
7143 switch (type
->base_type
) {
7145 case GLSL_TYPE_FLOAT
:
7146 /* "int" and "float" are valid, but vectors and matrices are not. */
7147 return type
->vector_elements
== 1 && type
->matrix_columns
== 1;
7148 case GLSL_TYPE_SAMPLER
:
7149 case GLSL_TYPE_IMAGE
:
7150 case GLSL_TYPE_ATOMIC_UINT
:
7159 ast_type_specifier::hir(exec_list
*instructions
,
7160 struct _mesa_glsl_parse_state
*state
)
7162 if (this->default_precision
== ast_precision_none
&& this->structure
== NULL
)
7165 YYLTYPE loc
= this->get_location();
7167 /* If this is a precision statement, check that the type to which it is
7168 * applied is either float or int.
7170 * From section 4.5.3 of the GLSL 1.30 spec:
7171 * "The precision statement
7172 * precision precision-qualifier type;
7173 * can be used to establish a default precision qualifier. The type
7174 * field can be either int or float [...]. Any other types or
7175 * qualifiers will result in an error.
7177 if (this->default_precision
!= ast_precision_none
) {
7178 if (!state
->check_precision_qualifiers_allowed(&loc
))
7181 if (this->structure
!= NULL
) {
7182 _mesa_glsl_error(&loc
, state
,
7183 "precision qualifiers do not apply to structures");
7187 if (this->array_specifier
!= NULL
) {
7188 _mesa_glsl_error(&loc
, state
,
7189 "default precision statements do not apply to "
7194 const struct glsl_type
*const type
=
7195 state
->symbols
->get_type(this->type_name
);
7196 if (!is_valid_default_precision_type(type
)) {
7197 _mesa_glsl_error(&loc
, state
,
7198 "default precision statements apply only to "
7199 "float, int, and opaque types");
7203 if (state
->es_shader
) {
7204 /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
7207 * "Non-precision qualified declarations will use the precision
7208 * qualifier specified in the most recent precision statement
7209 * that is still in scope. The precision statement has the same
7210 * scoping rules as variable declarations. If it is declared
7211 * inside a compound statement, its effect stops at the end of
7212 * the innermost statement it was declared in. Precision
7213 * statements in nested scopes override precision statements in
7214 * outer scopes. Multiple precision statements for the same basic
7215 * type can appear inside the same scope, with later statements
7216 * overriding earlier statements within that scope."
7218 * Default precision specifications follow the same scope rules as
7219 * variables. So, we can track the state of the default precision
7220 * qualifiers in the symbol table, and the rules will just work. This
7221 * is a slight abuse of the symbol table, but it has the semantics
7224 state
->symbols
->add_default_precision_qualifier(this->type_name
,
7225 this->default_precision
);
7228 /* FINISHME: Translate precision statements into IR. */
7232 /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
7233 * process_record_constructor() can do type-checking on C-style initializer
7234 * expressions of structs, but ast_struct_specifier should only be translated
7235 * to HIR if it is declaring the type of a structure.
7237 * The ->is_declaration field is false for initializers of variables
7238 * declared separately from the struct's type definition.
7240 * struct S { ... }; (is_declaration = true)
7241 * struct T { ... } t = { ... }; (is_declaration = true)
7242 * S s = { ... }; (is_declaration = false)
7244 if (this->structure
!= NULL
&& this->structure
->is_declaration
)
7245 return this->structure
->hir(instructions
, state
);
7252 * Process a structure or interface block tree into an array of structure fields
7254 * After parsing, where there are some syntax differnces, structures and
7255 * interface blocks are almost identical. They are similar enough that the
7256 * AST for each can be processed the same way into a set of
7257 * \c glsl_struct_field to describe the members.
7259 * If we're processing an interface block, var_mode should be the type of the
7260 * interface block (ir_var_shader_in, ir_var_shader_out, ir_var_uniform or
7261 * ir_var_shader_storage). If we're processing a structure, var_mode should be
7265 * The number of fields processed. A pointer to the array structure fields is
7266 * stored in \c *fields_ret.
7269 ast_process_struct_or_iface_block_members(exec_list
*instructions
,
7270 struct _mesa_glsl_parse_state
*state
,
7271 exec_list
*declarations
,
7272 glsl_struct_field
**fields_ret
,
7274 enum glsl_matrix_layout matrix_layout
,
7275 bool allow_reserved_names
,
7276 ir_variable_mode var_mode
,
7277 ast_type_qualifier
*layout
,
7278 unsigned block_stream
,
7279 unsigned block_xfb_buffer
,
7280 unsigned block_xfb_offset
,
7281 unsigned expl_location
,
7282 unsigned expl_align
)
7284 unsigned decl_count
= 0;
7285 unsigned next_offset
= 0;
7287 /* Make an initial pass over the list of fields to determine how
7288 * many there are. Each element in this list is an ast_declarator_list.
7289 * This means that we actually need to count the number of elements in the
7290 * 'declarations' list in each of the elements.
7292 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
7293 decl_count
+= decl_list
->declarations
.length();
7296 /* Allocate storage for the fields and process the field
7297 * declarations. As the declarations are processed, try to also convert
7298 * the types to HIR. This ensures that structure definitions embedded in
7299 * other structure definitions or in interface blocks are processed.
7301 glsl_struct_field
*const fields
= rzalloc_array(state
, glsl_struct_field
,
7304 bool first_member
= true;
7305 bool first_member_has_explicit_location
= false;
7308 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
7309 const char *type_name
;
7310 YYLTYPE loc
= decl_list
->get_location();
7312 decl_list
->type
->specifier
->hir(instructions
, state
);
7314 /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
7316 * "Anonymous structures are not supported; so embedded structures
7317 * must have a declarator. A name given to an embedded struct is
7318 * scoped at the same level as the struct it is embedded in."
7320 * The same section of the GLSL 1.20 spec says:
7322 * "Anonymous structures are not supported. Embedded structures are
7325 * The GLSL ES 1.00 and 3.00 specs have similar langauge. So, we allow
7326 * embedded structures in 1.10 only.
7328 if (state
->language_version
!= 110 &&
7329 decl_list
->type
->specifier
->structure
!= NULL
)
7330 _mesa_glsl_error(&loc
, state
,
7331 "embedded structure declarations are not allowed");
7333 const glsl_type
*decl_type
=
7334 decl_list
->type
->glsl_type(& type_name
, state
);
7336 const struct ast_type_qualifier
*const qual
=
7337 &decl_list
->type
->qualifier
;
7339 /* From section 4.3.9 of the GLSL 4.40 spec:
7341 * "[In interface blocks] opaque types are not allowed."
7343 * It should be impossible for decl_type to be NULL here. Cases that
7344 * might naturally lead to decl_type being NULL, especially for the
7345 * is_interface case, will have resulted in compilation having
7346 * already halted due to a syntax error.
7351 /* From section 4.3.7 of the ARB_bindless_texture spec:
7353 * "(remove the following bullet from the last list on p. 39,
7354 * thereby permitting sampler types in interface blocks; image
7355 * types are also permitted in blocks by this extension)"
7357 * * sampler types are not allowed
7359 if (decl_type
->contains_atomic() ||
7360 (!state
->has_bindless() && decl_type
->contains_opaque())) {
7361 _mesa_glsl_error(&loc
, state
, "uniform/buffer in non-default "
7362 "interface block contains %s variable",
7363 state
->has_bindless() ? "atomic" : "opaque");
7366 if (decl_type
->contains_atomic()) {
7367 /* From section 4.1.7.3 of the GLSL 4.40 spec:
7369 * "Members of structures cannot be declared as atomic counter
7372 _mesa_glsl_error(&loc
, state
, "atomic counter in structure");
7375 if (!state
->has_bindless() && decl_type
->contains_image()) {
7376 /* FINISHME: Same problem as with atomic counters.
7377 * FINISHME: Request clarification from Khronos and add
7378 * FINISHME: spec quotation here.
7380 _mesa_glsl_error(&loc
, state
, "image in structure");
7384 if (qual
->flags
.q
.explicit_binding
) {
7385 _mesa_glsl_error(&loc
, state
,
7386 "binding layout qualifier cannot be applied "
7387 "to struct or interface block members");
7391 if (!first_member
) {
7392 if (!layout
->flags
.q
.explicit_location
&&
7393 ((first_member_has_explicit_location
&&
7394 !qual
->flags
.q
.explicit_location
) ||
7395 (!first_member_has_explicit_location
&&
7396 qual
->flags
.q
.explicit_location
))) {
7397 _mesa_glsl_error(&loc
, state
,
7398 "when block-level location layout qualifier "
7399 "is not supplied either all members must "
7400 "have a location layout qualifier or all "
7401 "members must not have a location layout "
7405 first_member
= false;
7406 first_member_has_explicit_location
=
7407 qual
->flags
.q
.explicit_location
;
7411 if (qual
->flags
.q
.std140
||
7412 qual
->flags
.q
.std430
||
7413 qual
->flags
.q
.packed
||
7414 qual
->flags
.q
.shared
) {
7415 _mesa_glsl_error(&loc
, state
,
7416 "uniform/shader storage block layout qualifiers "
7417 "std140, std430, packed, and shared can only be "
7418 "applied to uniform/shader storage blocks, not "
7422 if (qual
->flags
.q
.constant
) {
7423 _mesa_glsl_error(&loc
, state
,
7424 "const storage qualifier cannot be applied "
7425 "to struct or interface block members");
7428 validate_memory_qualifier_for_type(state
, &loc
, qual
, decl_type
);
7429 validate_image_format_qualifier_for_type(state
, &loc
, qual
, decl_type
);
7431 /* From Section 4.4.2.3 (Geometry Outputs) of the GLSL 4.50 spec:
7433 * "A block member may be declared with a stream identifier, but
7434 * the specified stream must match the stream associated with the
7435 * containing block."
7437 if (qual
->flags
.q
.explicit_stream
) {
7438 unsigned qual_stream
;
7439 if (process_qualifier_constant(state
, &loc
, "stream",
7440 qual
->stream
, &qual_stream
) &&
7441 qual_stream
!= block_stream
) {
7442 _mesa_glsl_error(&loc
, state
, "stream layout qualifier on "
7443 "interface block member does not match "
7444 "the interface block (%u vs %u)", qual_stream
,
7450 unsigned explicit_xfb_buffer
= 0;
7451 if (qual
->flags
.q
.explicit_xfb_buffer
) {
7452 unsigned qual_xfb_buffer
;
7453 if (process_qualifier_constant(state
, &loc
, "xfb_buffer",
7454 qual
->xfb_buffer
, &qual_xfb_buffer
)) {
7455 explicit_xfb_buffer
= 1;
7456 if (qual_xfb_buffer
!= block_xfb_buffer
)
7457 _mesa_glsl_error(&loc
, state
, "xfb_buffer layout qualifier on "
7458 "interface block member does not match "
7459 "the interface block (%u vs %u)",
7460 qual_xfb_buffer
, block_xfb_buffer
);
7462 xfb_buffer
= (int) qual_xfb_buffer
;
7465 explicit_xfb_buffer
= layout
->flags
.q
.explicit_xfb_buffer
;
7466 xfb_buffer
= (int) block_xfb_buffer
;
7469 int xfb_stride
= -1;
7470 if (qual
->flags
.q
.explicit_xfb_stride
) {
7471 unsigned qual_xfb_stride
;
7472 if (process_qualifier_constant(state
, &loc
, "xfb_stride",
7473 qual
->xfb_stride
, &qual_xfb_stride
)) {
7474 xfb_stride
= (int) qual_xfb_stride
;
7478 if (qual
->flags
.q
.uniform
&& qual
->has_interpolation()) {
7479 _mesa_glsl_error(&loc
, state
,
7480 "interpolation qualifiers cannot be used "
7481 "with uniform interface blocks");
7484 if ((qual
->flags
.q
.uniform
|| !is_interface
) &&
7485 qual
->has_auxiliary_storage()) {
7486 _mesa_glsl_error(&loc
, state
,
7487 "auxiliary storage qualifiers cannot be used "
7488 "in uniform blocks or structures.");
7491 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
7492 if (!qual
->flags
.q
.uniform
&& !qual
->flags
.q
.buffer
) {
7493 _mesa_glsl_error(&loc
, state
,
7494 "row_major and column_major can only be "
7495 "applied to interface blocks");
7497 validate_matrix_layout_for_type(state
, &loc
, decl_type
, NULL
);
7500 foreach_list_typed (ast_declaration
, decl
, link
,
7501 &decl_list
->declarations
) {
7502 YYLTYPE loc
= decl
->get_location();
7504 if (!allow_reserved_names
)
7505 validate_identifier(decl
->identifier
, loc
, state
);
7507 const struct glsl_type
*field_type
=
7508 process_array_type(&loc
, decl_type
, decl
->array_specifier
, state
);
7509 validate_array_dimensions(field_type
, state
, &loc
);
7510 fields
[i
].type
= field_type
;
7511 fields
[i
].name
= decl
->identifier
;
7512 fields
[i
].interpolation
=
7513 interpret_interpolation_qualifier(qual
, field_type
,
7514 var_mode
, state
, &loc
);
7515 fields
[i
].centroid
= qual
->flags
.q
.centroid
? 1 : 0;
7516 fields
[i
].sample
= qual
->flags
.q
.sample
? 1 : 0;
7517 fields
[i
].patch
= qual
->flags
.q
.patch
? 1 : 0;
7518 fields
[i
].offset
= -1;
7519 fields
[i
].explicit_xfb_buffer
= explicit_xfb_buffer
;
7520 fields
[i
].xfb_buffer
= xfb_buffer
;
7521 fields
[i
].xfb_stride
= xfb_stride
;
7523 if (qual
->flags
.q
.explicit_location
) {
7524 unsigned qual_location
;
7525 if (process_qualifier_constant(state
, &loc
, "location",
7526 qual
->location
, &qual_location
)) {
7527 fields
[i
].location
= qual_location
+
7528 (fields
[i
].patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
);
7529 expl_location
= fields
[i
].location
+
7530 fields
[i
].type
->count_attribute_slots(false);
7533 if (layout
&& layout
->flags
.q
.explicit_location
) {
7534 fields
[i
].location
= expl_location
;
7535 expl_location
+= fields
[i
].type
->count_attribute_slots(false);
7537 fields
[i
].location
= -1;
7541 /* Offset can only be used with std430 and std140 layouts an initial
7542 * value of 0 is used for error detection.
7548 if (qual
->flags
.q
.row_major
||
7549 matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
7555 if(layout
->flags
.q
.std140
) {
7556 align
= field_type
->std140_base_alignment(row_major
);
7557 size
= field_type
->std140_size(row_major
);
7558 } else if (layout
->flags
.q
.std430
) {
7559 align
= field_type
->std430_base_alignment(row_major
);
7560 size
= field_type
->std430_size(row_major
);
7564 if (qual
->flags
.q
.explicit_offset
) {
7565 unsigned qual_offset
;
7566 if (process_qualifier_constant(state
, &loc
, "offset",
7567 qual
->offset
, &qual_offset
)) {
7568 if (align
!= 0 && size
!= 0) {
7569 if (next_offset
> qual_offset
)
7570 _mesa_glsl_error(&loc
, state
, "layout qualifier "
7571 "offset overlaps previous member");
7573 if (qual_offset
% align
) {
7574 _mesa_glsl_error(&loc
, state
, "layout qualifier offset "
7575 "must be a multiple of the base "
7576 "alignment of %s", field_type
->name
);
7578 fields
[i
].offset
= qual_offset
;
7579 next_offset
= qual_offset
+ size
;
7581 _mesa_glsl_error(&loc
, state
, "offset can only be used "
7582 "with std430 and std140 layouts");
7587 if (qual
->flags
.q
.explicit_align
|| expl_align
!= 0) {
7588 unsigned offset
= fields
[i
].offset
!= -1 ? fields
[i
].offset
:
7590 if (align
== 0 || size
== 0) {
7591 _mesa_glsl_error(&loc
, state
, "align can only be used with "
7592 "std430 and std140 layouts");
7593 } else if (qual
->flags
.q
.explicit_align
) {
7594 unsigned member_align
;
7595 if (process_qualifier_constant(state
, &loc
, "align",
7596 qual
->align
, &member_align
)) {
7597 if (member_align
== 0 ||
7598 member_align
& (member_align
- 1)) {
7599 _mesa_glsl_error(&loc
, state
, "align layout qualifier "
7600 "is not a power of 2");
7602 fields
[i
].offset
= glsl_align(offset
, member_align
);
7603 next_offset
= fields
[i
].offset
+ size
;
7607 fields
[i
].offset
= glsl_align(offset
, expl_align
);
7608 next_offset
= fields
[i
].offset
+ size
;
7610 } else if (!qual
->flags
.q
.explicit_offset
) {
7611 if (align
!= 0 && size
!= 0)
7612 next_offset
= glsl_align(next_offset
, align
) + size
;
7615 /* From the ARB_enhanced_layouts spec:
7617 * "The given offset applies to the first component of the first
7618 * member of the qualified entity. Then, within the qualified
7619 * entity, subsequent components are each assigned, in order, to
7620 * the next available offset aligned to a multiple of that
7621 * component's size. Aggregate types are flattened down to the
7622 * component level to get this sequence of components."
7624 if (qual
->flags
.q
.explicit_xfb_offset
) {
7625 unsigned xfb_offset
;
7626 if (process_qualifier_constant(state
, &loc
, "xfb_offset",
7627 qual
->offset
, &xfb_offset
)) {
7628 fields
[i
].offset
= xfb_offset
;
7629 block_xfb_offset
= fields
[i
].offset
+
7630 4 * field_type
->component_slots();
7633 if (layout
&& layout
->flags
.q
.explicit_xfb_offset
) {
7634 unsigned align
= field_type
->is_64bit() ? 8 : 4;
7635 fields
[i
].offset
= glsl_align(block_xfb_offset
, align
);
7636 block_xfb_offset
+= 4 * field_type
->component_slots();
7640 /* Propogate row- / column-major information down the fields of the
7641 * structure or interface block. Structures need this data because
7642 * the structure may contain a structure that contains ... a matrix
7643 * that need the proper layout.
7645 if (is_interface
&& layout
&&
7646 (layout
->flags
.q
.uniform
|| layout
->flags
.q
.buffer
) &&
7647 (field_type
->without_array()->is_matrix()
7648 || field_type
->without_array()->is_struct())) {
7649 /* If no layout is specified for the field, inherit the layout
7652 fields
[i
].matrix_layout
= matrix_layout
;
7654 if (qual
->flags
.q
.row_major
)
7655 fields
[i
].matrix_layout
= GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
7656 else if (qual
->flags
.q
.column_major
)
7657 fields
[i
].matrix_layout
= GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
;
7659 /* If we're processing an uniform or buffer block, the matrix
7660 * layout must be decided by this point.
7662 assert(fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
7663 || fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
);
7666 /* Memory qualifiers are allowed on buffer and image variables, while
7667 * the format qualifier is only accepted for images.
7669 if (var_mode
== ir_var_shader_storage
||
7670 field_type
->without_array()->is_image()) {
7671 /* For readonly and writeonly qualifiers the field definition,
7672 * if set, overwrites the layout qualifier.
7674 if (qual
->flags
.q
.read_only
|| qual
->flags
.q
.write_only
) {
7675 fields
[i
].memory_read_only
= qual
->flags
.q
.read_only
;
7676 fields
[i
].memory_write_only
= qual
->flags
.q
.write_only
;
7678 fields
[i
].memory_read_only
=
7679 layout
? layout
->flags
.q
.read_only
: 0;
7680 fields
[i
].memory_write_only
=
7681 layout
? layout
->flags
.q
.write_only
: 0;
7684 /* For other qualifiers, we set the flag if either the layout
7685 * qualifier or the field qualifier are set
7687 fields
[i
].memory_coherent
= qual
->flags
.q
.coherent
||
7688 (layout
&& layout
->flags
.q
.coherent
);
7689 fields
[i
].memory_volatile
= qual
->flags
.q
._volatile
||
7690 (layout
&& layout
->flags
.q
._volatile
);
7691 fields
[i
].memory_restrict
= qual
->flags
.q
.restrict_flag
||
7692 (layout
&& layout
->flags
.q
.restrict_flag
);
7694 if (field_type
->without_array()->is_image()) {
7695 if (qual
->flags
.q
.explicit_image_format
) {
7696 if (qual
->image_base_type
!=
7697 field_type
->without_array()->sampled_type
) {
7698 _mesa_glsl_error(&loc
, state
, "format qualifier doesn't "
7699 "match the base data type of the image");
7702 fields
[i
].image_format
= qual
->image_format
;
7704 if (!qual
->flags
.q
.write_only
) {
7705 _mesa_glsl_error(&loc
, state
, "image not qualified with "
7706 "`writeonly' must have a format layout "
7710 fields
[i
].image_format
= PIPE_FORMAT_NONE
;
7715 /* Precision qualifiers do not hold any meaning in Desktop GLSL */
7716 if (state
->es_shader
) {
7717 fields
[i
].precision
= select_gles_precision(qual
->precision
,
7722 fields
[i
].precision
= qual
->precision
;
7729 assert(i
== decl_count
);
7731 *fields_ret
= fields
;
7737 ast_struct_specifier::hir(exec_list
*instructions
,
7738 struct _mesa_glsl_parse_state
*state
)
7740 YYLTYPE loc
= this->get_location();
7742 unsigned expl_location
= 0;
7743 if (layout
&& layout
->flags
.q
.explicit_location
) {
7744 if (!process_qualifier_constant(state
, &loc
, "location",
7745 layout
->location
, &expl_location
)) {
7748 expl_location
= VARYING_SLOT_VAR0
+ expl_location
;
7752 glsl_struct_field
*fields
;
7753 unsigned decl_count
=
7754 ast_process_struct_or_iface_block_members(instructions
,
7756 &this->declarations
,
7759 GLSL_MATRIX_LAYOUT_INHERITED
,
7760 false /* allow_reserved_names */,
7763 0, /* for interface only */
7764 0, /* for interface only */
7765 0, /* for interface only */
7767 0 /* for interface only */);
7769 validate_identifier(this->name
, loc
, state
);
7771 type
= glsl_type::get_struct_instance(fields
, decl_count
, this->name
);
7773 if (!type
->is_anonymous() && !state
->symbols
->add_type(name
, type
)) {
7774 const glsl_type
*match
= state
->symbols
->get_type(name
);
7775 /* allow struct matching for desktop GL - older UE4 does this */
7776 if (match
!= NULL
&& state
->is_version(130, 0) && match
->record_compare(type
, true, false))
7777 _mesa_glsl_warning(& loc
, state
, "struct `%s' previously defined", name
);
7779 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
7781 const glsl_type
**s
= reralloc(state
, state
->user_structures
,
7783 state
->num_user_structures
+ 1);
7785 s
[state
->num_user_structures
] = type
;
7786 state
->user_structures
= s
;
7787 state
->num_user_structures
++;
7791 /* Structure type definitions do not have r-values.
7798 * Visitor class which detects whether a given interface block has been used.
7800 class interface_block_usage_visitor
: public ir_hierarchical_visitor
7803 interface_block_usage_visitor(ir_variable_mode mode
, const glsl_type
*block
)
7804 : mode(mode
), block(block
), found(false)
7808 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
7810 if (ir
->var
->data
.mode
== mode
&& ir
->var
->get_interface_type() == block
) {
7814 return visit_continue
;
7817 bool usage_found() const
7823 ir_variable_mode mode
;
7824 const glsl_type
*block
;
7829 is_unsized_array_last_element(ir_variable
*v
)
7831 const glsl_type
*interface_type
= v
->get_interface_type();
7832 int length
= interface_type
->length
;
7834 assert(v
->type
->is_unsized_array());
7836 /* Check if it is the last element of the interface */
7837 if (strcmp(interface_type
->fields
.structure
[length
-1].name
, v
->name
) == 0)
7843 apply_memory_qualifiers(ir_variable
*var
, glsl_struct_field field
)
7845 var
->data
.memory_read_only
= field
.memory_read_only
;
7846 var
->data
.memory_write_only
= field
.memory_write_only
;
7847 var
->data
.memory_coherent
= field
.memory_coherent
;
7848 var
->data
.memory_volatile
= field
.memory_volatile
;
7849 var
->data
.memory_restrict
= field
.memory_restrict
;
7853 ast_interface_block::hir(exec_list
*instructions
,
7854 struct _mesa_glsl_parse_state
*state
)
7856 YYLTYPE loc
= this->get_location();
7858 /* Interface blocks must be declared at global scope */
7859 if (state
->current_function
!= NULL
) {
7860 _mesa_glsl_error(&loc
, state
,
7861 "Interface block `%s' must be declared "
7866 /* Validate qualifiers:
7868 * - Layout Qualifiers as per the table in Section 4.4
7869 * ("Layout Qualifiers") of the GLSL 4.50 spec.
7871 * - Memory Qualifiers as per Section 4.10 ("Memory Qualifiers") of the
7874 * "Additionally, memory qualifiers may also be used in the declaration
7875 * of shader storage blocks"
7877 * Note the table in Section 4.4 says std430 is allowed on both uniform and
7878 * buffer blocks however Section 4.4.5 (Uniform and Shader Storage Block
7879 * Layout Qualifiers) of the GLSL 4.50 spec says:
7881 * "The std430 qualifier is supported only for shader storage blocks;
7882 * using std430 on a uniform block will result in a compile-time error."
7884 ast_type_qualifier allowed_blk_qualifiers
;
7885 allowed_blk_qualifiers
.flags
.i
= 0;
7886 if (this->layout
.flags
.q
.buffer
|| this->layout
.flags
.q
.uniform
) {
7887 allowed_blk_qualifiers
.flags
.q
.shared
= 1;
7888 allowed_blk_qualifiers
.flags
.q
.packed
= 1;
7889 allowed_blk_qualifiers
.flags
.q
.std140
= 1;
7890 allowed_blk_qualifiers
.flags
.q
.row_major
= 1;
7891 allowed_blk_qualifiers
.flags
.q
.column_major
= 1;
7892 allowed_blk_qualifiers
.flags
.q
.explicit_align
= 1;
7893 allowed_blk_qualifiers
.flags
.q
.explicit_binding
= 1;
7894 if (this->layout
.flags
.q
.buffer
) {
7895 allowed_blk_qualifiers
.flags
.q
.buffer
= 1;
7896 allowed_blk_qualifiers
.flags
.q
.std430
= 1;
7897 allowed_blk_qualifiers
.flags
.q
.coherent
= 1;
7898 allowed_blk_qualifiers
.flags
.q
._volatile
= 1;
7899 allowed_blk_qualifiers
.flags
.q
.restrict_flag
= 1;
7900 allowed_blk_qualifiers
.flags
.q
.read_only
= 1;
7901 allowed_blk_qualifiers
.flags
.q
.write_only
= 1;
7903 allowed_blk_qualifiers
.flags
.q
.uniform
= 1;
7906 /* Interface block */
7907 assert(this->layout
.flags
.q
.in
|| this->layout
.flags
.q
.out
);
7909 allowed_blk_qualifiers
.flags
.q
.explicit_location
= 1;
7910 if (this->layout
.flags
.q
.out
) {
7911 allowed_blk_qualifiers
.flags
.q
.out
= 1;
7912 if (state
->stage
== MESA_SHADER_GEOMETRY
||
7913 state
->stage
== MESA_SHADER_TESS_CTRL
||
7914 state
->stage
== MESA_SHADER_TESS_EVAL
||
7915 state
->stage
== MESA_SHADER_VERTEX
) {
7916 allowed_blk_qualifiers
.flags
.q
.explicit_xfb_offset
= 1;
7917 allowed_blk_qualifiers
.flags
.q
.explicit_xfb_buffer
= 1;
7918 allowed_blk_qualifiers
.flags
.q
.xfb_buffer
= 1;
7919 allowed_blk_qualifiers
.flags
.q
.explicit_xfb_stride
= 1;
7920 allowed_blk_qualifiers
.flags
.q
.xfb_stride
= 1;
7921 if (state
->stage
== MESA_SHADER_GEOMETRY
) {
7922 allowed_blk_qualifiers
.flags
.q
.stream
= 1;
7923 allowed_blk_qualifiers
.flags
.q
.explicit_stream
= 1;
7925 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
7926 allowed_blk_qualifiers
.flags
.q
.patch
= 1;
7930 allowed_blk_qualifiers
.flags
.q
.in
= 1;
7931 if (state
->stage
== MESA_SHADER_TESS_EVAL
) {
7932 allowed_blk_qualifiers
.flags
.q
.patch
= 1;
7937 this->layout
.validate_flags(&loc
, state
, allowed_blk_qualifiers
,
7938 "invalid qualifier for block",
7941 enum glsl_interface_packing packing
;
7942 if (this->layout
.flags
.q
.std140
) {
7943 packing
= GLSL_INTERFACE_PACKING_STD140
;
7944 } else if (this->layout
.flags
.q
.packed
) {
7945 packing
= GLSL_INTERFACE_PACKING_PACKED
;
7946 } else if (this->layout
.flags
.q
.std430
) {
7947 packing
= GLSL_INTERFACE_PACKING_STD430
;
7949 /* The default layout is shared.
7951 packing
= GLSL_INTERFACE_PACKING_SHARED
;
7954 ir_variable_mode var_mode
;
7955 const char *iface_type_name
;
7956 if (this->layout
.flags
.q
.in
) {
7957 var_mode
= ir_var_shader_in
;
7958 iface_type_name
= "in";
7959 } else if (this->layout
.flags
.q
.out
) {
7960 var_mode
= ir_var_shader_out
;
7961 iface_type_name
= "out";
7962 } else if (this->layout
.flags
.q
.uniform
) {
7963 var_mode
= ir_var_uniform
;
7964 iface_type_name
= "uniform";
7965 } else if (this->layout
.flags
.q
.buffer
) {
7966 var_mode
= ir_var_shader_storage
;
7967 iface_type_name
= "buffer";
7969 var_mode
= ir_var_auto
;
7970 iface_type_name
= "UNKNOWN";
7971 assert(!"interface block layout qualifier not found!");
7974 enum glsl_matrix_layout matrix_layout
= GLSL_MATRIX_LAYOUT_INHERITED
;
7975 if (this->layout
.flags
.q
.row_major
)
7976 matrix_layout
= GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
7977 else if (this->layout
.flags
.q
.column_major
)
7978 matrix_layout
= GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
;
7980 bool redeclaring_per_vertex
= strcmp(this->block_name
, "gl_PerVertex") == 0;
7981 exec_list declared_variables
;
7982 glsl_struct_field
*fields
;
7984 /* For blocks that accept memory qualifiers (i.e. shader storage), verify
7985 * that we don't have incompatible qualifiers
7987 if (this->layout
.flags
.q
.read_only
&& this->layout
.flags
.q
.write_only
) {
7988 _mesa_glsl_error(&loc
, state
,
7989 "Interface block sets both readonly and writeonly");
7992 unsigned qual_stream
;
7993 if (!process_qualifier_constant(state
, &loc
, "stream", this->layout
.stream
,
7995 !validate_stream_qualifier(&loc
, state
, qual_stream
)) {
7996 /* If the stream qualifier is invalid it doesn't make sense to continue
7997 * on and try to compare stream layouts on member variables against it
7998 * so just return early.
8003 unsigned qual_xfb_buffer
;
8004 if (!process_qualifier_constant(state
, &loc
, "xfb_buffer",
8005 layout
.xfb_buffer
, &qual_xfb_buffer
) ||
8006 !validate_xfb_buffer_qualifier(&loc
, state
, qual_xfb_buffer
)) {
8010 unsigned qual_xfb_offset
;
8011 if (layout
.flags
.q
.explicit_xfb_offset
) {
8012 if (!process_qualifier_constant(state
, &loc
, "xfb_offset",
8013 layout
.offset
, &qual_xfb_offset
)) {
8018 unsigned qual_xfb_stride
;
8019 if (layout
.flags
.q
.explicit_xfb_stride
) {
8020 if (!process_qualifier_constant(state
, &loc
, "xfb_stride",
8021 layout
.xfb_stride
, &qual_xfb_stride
)) {
8026 unsigned expl_location
= 0;
8027 if (layout
.flags
.q
.explicit_location
) {
8028 if (!process_qualifier_constant(state
, &loc
, "location",
8029 layout
.location
, &expl_location
)) {
8032 expl_location
+= this->layout
.flags
.q
.patch
? VARYING_SLOT_PATCH0
8033 : VARYING_SLOT_VAR0
;
8037 unsigned expl_align
= 0;
8038 if (layout
.flags
.q
.explicit_align
) {
8039 if (!process_qualifier_constant(state
, &loc
, "align",
8040 layout
.align
, &expl_align
)) {
8043 if (expl_align
== 0 || expl_align
& (expl_align
- 1)) {
8044 _mesa_glsl_error(&loc
, state
, "align layout qualifier is not a "
8051 unsigned int num_variables
=
8052 ast_process_struct_or_iface_block_members(&declared_variables
,
8054 &this->declarations
,
8058 redeclaring_per_vertex
,
8067 if (!redeclaring_per_vertex
) {
8068 validate_identifier(this->block_name
, loc
, state
);
8070 /* From section 4.3.9 ("Interface Blocks") of the GLSL 4.50 spec:
8072 * "Block names have no other use within a shader beyond interface
8073 * matching; it is a compile-time error to use a block name at global
8074 * scope for anything other than as a block name."
8076 ir_variable
*var
= state
->symbols
->get_variable(this->block_name
);
8077 if (var
&& !var
->type
->is_interface()) {
8078 _mesa_glsl_error(&loc
, state
, "Block name `%s' is "
8079 "already used in the scope.",
8084 const glsl_type
*earlier_per_vertex
= NULL
;
8085 if (redeclaring_per_vertex
) {
8086 /* Find the previous declaration of gl_PerVertex. If we're redeclaring
8087 * the named interface block gl_in, we can find it by looking at the
8088 * previous declaration of gl_in. Otherwise we can find it by looking
8089 * at the previous decalartion of any of the built-in outputs,
8092 * Also check that the instance name and array-ness of the redeclaration
8096 case ir_var_shader_in
:
8097 if (ir_variable
*earlier_gl_in
=
8098 state
->symbols
->get_variable("gl_in")) {
8099 earlier_per_vertex
= earlier_gl_in
->get_interface_type();
8101 _mesa_glsl_error(&loc
, state
,
8102 "redeclaration of gl_PerVertex input not allowed "
8104 _mesa_shader_stage_to_string(state
->stage
));
8106 if (this->instance_name
== NULL
||
8107 strcmp(this->instance_name
, "gl_in") != 0 || this->array_specifier
== NULL
||
8108 !this->array_specifier
->is_single_dimension()) {
8109 _mesa_glsl_error(&loc
, state
,
8110 "gl_PerVertex input must be redeclared as "
8114 case ir_var_shader_out
:
8115 if (ir_variable
*earlier_gl_Position
=
8116 state
->symbols
->get_variable("gl_Position")) {
8117 earlier_per_vertex
= earlier_gl_Position
->get_interface_type();
8118 } else if (ir_variable
*earlier_gl_out
=
8119 state
->symbols
->get_variable("gl_out")) {
8120 earlier_per_vertex
= earlier_gl_out
->get_interface_type();
8122 _mesa_glsl_error(&loc
, state
,
8123 "redeclaration of gl_PerVertex output not "
8124 "allowed in the %s shader",
8125 _mesa_shader_stage_to_string(state
->stage
));
8127 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
8128 if (this->instance_name
== NULL
||
8129 strcmp(this->instance_name
, "gl_out") != 0 || this->array_specifier
== NULL
) {
8130 _mesa_glsl_error(&loc
, state
,
8131 "gl_PerVertex output must be redeclared as "
8135 if (this->instance_name
!= NULL
) {
8136 _mesa_glsl_error(&loc
, state
,
8137 "gl_PerVertex output may not be redeclared with "
8138 "an instance name");
8143 _mesa_glsl_error(&loc
, state
,
8144 "gl_PerVertex must be declared as an input or an "
8149 if (earlier_per_vertex
== NULL
) {
8150 /* An error has already been reported. Bail out to avoid null
8151 * dereferences later in this function.
8156 /* Copy locations from the old gl_PerVertex interface block. */
8157 for (unsigned i
= 0; i
< num_variables
; i
++) {
8158 int j
= earlier_per_vertex
->field_index(fields
[i
].name
);
8160 _mesa_glsl_error(&loc
, state
,
8161 "redeclaration of gl_PerVertex must be a subset "
8162 "of the built-in members of gl_PerVertex");
8164 fields
[i
].location
=
8165 earlier_per_vertex
->fields
.structure
[j
].location
;
8167 earlier_per_vertex
->fields
.structure
[j
].offset
;
8168 fields
[i
].interpolation
=
8169 earlier_per_vertex
->fields
.structure
[j
].interpolation
;
8170 fields
[i
].centroid
=
8171 earlier_per_vertex
->fields
.structure
[j
].centroid
;
8173 earlier_per_vertex
->fields
.structure
[j
].sample
;
8175 earlier_per_vertex
->fields
.structure
[j
].patch
;
8176 fields
[i
].precision
=
8177 earlier_per_vertex
->fields
.structure
[j
].precision
;
8178 fields
[i
].explicit_xfb_buffer
=
8179 earlier_per_vertex
->fields
.structure
[j
].explicit_xfb_buffer
;
8180 fields
[i
].xfb_buffer
=
8181 earlier_per_vertex
->fields
.structure
[j
].xfb_buffer
;
8182 fields
[i
].xfb_stride
=
8183 earlier_per_vertex
->fields
.structure
[j
].xfb_stride
;
8187 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10
8190 * If a built-in interface block is redeclared, it must appear in
8191 * the shader before any use of any member included in the built-in
8192 * declaration, or a compilation error will result.
8194 * This appears to be a clarification to the behaviour established for
8195 * gl_PerVertex by GLSL 1.50, therefore we implement this behaviour
8196 * regardless of GLSL version.
8198 interface_block_usage_visitor
v(var_mode
, earlier_per_vertex
);
8199 v
.run(instructions
);
8200 if (v
.usage_found()) {
8201 _mesa_glsl_error(&loc
, state
,
8202 "redeclaration of a built-in interface block must "
8203 "appear before any use of any member of the "
8208 const glsl_type
*block_type
=
8209 glsl_type::get_interface_instance(fields
,
8213 GLSL_MATRIX_LAYOUT_ROW_MAJOR
,
8216 unsigned component_size
= block_type
->contains_double() ? 8 : 4;
8218 layout
.flags
.q
.explicit_xfb_offset
? (int) qual_xfb_offset
: -1;
8219 validate_xfb_offset_qualifier(&loc
, state
, xfb_offset
, block_type
,
8222 if (!state
->symbols
->add_interface(block_type
->name
, block_type
, var_mode
)) {
8223 YYLTYPE loc
= this->get_location();
8224 _mesa_glsl_error(&loc
, state
, "interface block `%s' with type `%s' "
8225 "already taken in the current scope",
8226 this->block_name
, iface_type_name
);
8229 /* Since interface blocks cannot contain statements, it should be
8230 * impossible for the block to generate any instructions.
8232 assert(declared_variables
.is_empty());
8234 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
8236 * Geometry shader input variables get the per-vertex values written
8237 * out by vertex shader output variables of the same names. Since a
8238 * geometry shader operates on a set of vertices, each input varying
8239 * variable (or input block, see interface blocks below) needs to be
8240 * declared as an array.
8242 if (state
->stage
== MESA_SHADER_GEOMETRY
&& this->array_specifier
== NULL
&&
8243 var_mode
== ir_var_shader_in
) {
8244 _mesa_glsl_error(&loc
, state
, "geometry shader inputs must be arrays");
8245 } else if ((state
->stage
== MESA_SHADER_TESS_CTRL
||
8246 state
->stage
== MESA_SHADER_TESS_EVAL
) &&
8247 !this->layout
.flags
.q
.patch
&&
8248 this->array_specifier
== NULL
&&
8249 var_mode
== ir_var_shader_in
) {
8250 _mesa_glsl_error(&loc
, state
, "per-vertex tessellation shader inputs must be arrays");
8251 } else if (state
->stage
== MESA_SHADER_TESS_CTRL
&&
8252 !this->layout
.flags
.q
.patch
&&
8253 this->array_specifier
== NULL
&&
8254 var_mode
== ir_var_shader_out
) {
8255 _mesa_glsl_error(&loc
, state
, "tessellation control shader outputs must be arrays");
8259 /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
8262 * "If an instance name (instance-name) is used, then it puts all the
8263 * members inside a scope within its own name space, accessed with the
8264 * field selector ( . ) operator (analogously to structures)."
8266 if (this->instance_name
) {
8267 if (redeclaring_per_vertex
) {
8268 /* When a built-in in an unnamed interface block is redeclared,
8269 * get_variable_being_redeclared() calls
8270 * check_builtin_array_max_size() to make sure that built-in array
8271 * variables aren't redeclared to illegal sizes. But we're looking
8272 * at a redeclaration of a named built-in interface block. So we
8273 * have to manually call check_builtin_array_max_size() for all parts
8274 * of the interface that are arrays.
8276 for (unsigned i
= 0; i
< num_variables
; i
++) {
8277 if (fields
[i
].type
->is_array()) {
8278 const unsigned size
= fields
[i
].type
->array_size();
8279 check_builtin_array_max_size(fields
[i
].name
, size
, loc
, state
);
8283 validate_identifier(this->instance_name
, loc
, state
);
8288 if (this->array_specifier
!= NULL
) {
8289 const glsl_type
*block_array_type
=
8290 process_array_type(&loc
, block_type
, this->array_specifier
, state
);
8292 /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
8294 * For uniform blocks declared an array, each individual array
8295 * element corresponds to a separate buffer object backing one
8296 * instance of the block. As the array size indicates the number
8297 * of buffer objects needed, uniform block array declarations
8298 * must specify an array size.
8300 * And a few paragraphs later:
8302 * Geometry shader input blocks must be declared as arrays and
8303 * follow the array declaration and linking rules for all
8304 * geometry shader inputs. All other input and output block
8305 * arrays must specify an array size.
8307 * The same applies to tessellation shaders.
8309 * The upshot of this is that the only circumstance where an
8310 * interface array size *doesn't* need to be specified is on a
8311 * geometry shader input, tessellation control shader input,
8312 * tessellation control shader output, and tessellation evaluation
8315 if (block_array_type
->is_unsized_array()) {
8316 bool allow_inputs
= state
->stage
== MESA_SHADER_GEOMETRY
||
8317 state
->stage
== MESA_SHADER_TESS_CTRL
||
8318 state
->stage
== MESA_SHADER_TESS_EVAL
;
8319 bool allow_outputs
= state
->stage
== MESA_SHADER_TESS_CTRL
;
8321 if (this->layout
.flags
.q
.in
) {
8323 _mesa_glsl_error(&loc
, state
,
8324 "unsized input block arrays not allowed in "
8326 _mesa_shader_stage_to_string(state
->stage
));
8327 } else if (this->layout
.flags
.q
.out
) {
8329 _mesa_glsl_error(&loc
, state
,
8330 "unsized output block arrays not allowed in "
8332 _mesa_shader_stage_to_string(state
->stage
));
8334 /* by elimination, this is a uniform block array */
8335 _mesa_glsl_error(&loc
, state
,
8336 "unsized uniform block arrays not allowed in "
8338 _mesa_shader_stage_to_string(state
->stage
));
8342 /* From section 4.3.9 (Interface Blocks) of the GLSL ES 3.10 spec:
8344 * * Arrays of arrays of blocks are not allowed
8346 if (state
->es_shader
&& block_array_type
->is_array() &&
8347 block_array_type
->fields
.array
->is_array()) {
8348 _mesa_glsl_error(&loc
, state
,
8349 "arrays of arrays interface blocks are "
8353 var
= new(state
) ir_variable(block_array_type
,
8354 this->instance_name
,
8357 var
= new(state
) ir_variable(block_type
,
8358 this->instance_name
,
8362 var
->data
.matrix_layout
= matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
8363 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
: matrix_layout
;
8365 if (var_mode
== ir_var_shader_in
|| var_mode
== ir_var_uniform
)
8366 var
->data
.read_only
= true;
8368 var
->data
.patch
= this->layout
.flags
.q
.patch
;
8370 if (state
->stage
== MESA_SHADER_GEOMETRY
&& var_mode
== ir_var_shader_in
)
8371 handle_geometry_shader_input_decl(state
, loc
, var
);
8372 else if ((state
->stage
== MESA_SHADER_TESS_CTRL
||
8373 state
->stage
== MESA_SHADER_TESS_EVAL
) && var_mode
== ir_var_shader_in
)
8374 handle_tess_shader_input_decl(state
, loc
, var
);
8375 else if (state
->stage
== MESA_SHADER_TESS_CTRL
&& var_mode
== ir_var_shader_out
)
8376 handle_tess_ctrl_shader_output_decl(state
, loc
, var
);
8378 for (unsigned i
= 0; i
< num_variables
; i
++) {
8379 if (var
->data
.mode
== ir_var_shader_storage
)
8380 apply_memory_qualifiers(var
, fields
[i
]);
8383 if (ir_variable
*earlier
=
8384 state
->symbols
->get_variable(this->instance_name
)) {
8385 if (!redeclaring_per_vertex
) {
8386 _mesa_glsl_error(&loc
, state
, "`%s' redeclared",
8387 this->instance_name
);
8389 earlier
->data
.how_declared
= ir_var_declared_normally
;
8390 earlier
->type
= var
->type
;
8391 earlier
->reinit_interface_type(block_type
);
8394 if (this->layout
.flags
.q
.explicit_binding
) {
8395 apply_explicit_binding(state
, &loc
, var
, var
->type
,
8399 var
->data
.stream
= qual_stream
;
8400 if (layout
.flags
.q
.explicit_location
) {
8401 var
->data
.location
= expl_location
;
8402 var
->data
.explicit_location
= true;
8405 state
->symbols
->add_variable(var
);
8406 instructions
->push_tail(var
);
8409 /* In order to have an array size, the block must also be declared with
8412 assert(this->array_specifier
== NULL
);
8414 for (unsigned i
= 0; i
< num_variables
; i
++) {
8416 new(state
) ir_variable(fields
[i
].type
,
8417 ralloc_strdup(state
, fields
[i
].name
),
8419 var
->data
.interpolation
= fields
[i
].interpolation
;
8420 var
->data
.centroid
= fields
[i
].centroid
;
8421 var
->data
.sample
= fields
[i
].sample
;
8422 var
->data
.patch
= fields
[i
].patch
;
8423 var
->data
.stream
= qual_stream
;
8424 var
->data
.location
= fields
[i
].location
;
8426 if (fields
[i
].location
!= -1)
8427 var
->data
.explicit_location
= true;
8429 var
->data
.explicit_xfb_buffer
= fields
[i
].explicit_xfb_buffer
;
8430 var
->data
.xfb_buffer
= fields
[i
].xfb_buffer
;
8432 if (fields
[i
].offset
!= -1)
8433 var
->data
.explicit_xfb_offset
= true;
8434 var
->data
.offset
= fields
[i
].offset
;
8436 var
->init_interface_type(block_type
);
8438 if (var_mode
== ir_var_shader_in
|| var_mode
== ir_var_uniform
)
8439 var
->data
.read_only
= true;
8441 /* Precision qualifiers do not have any meaning in Desktop GLSL */
8442 if (state
->es_shader
) {
8443 var
->data
.precision
=
8444 select_gles_precision(fields
[i
].precision
, fields
[i
].type
,
8448 if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
) {
8449 var
->data
.matrix_layout
= matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
8450 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
: matrix_layout
;
8452 var
->data
.matrix_layout
= fields
[i
].matrix_layout
;
8455 if (var
->data
.mode
== ir_var_shader_storage
)
8456 apply_memory_qualifiers(var
, fields
[i
]);
8458 /* Examine var name here since var may get deleted in the next call */
8459 bool var_is_gl_id
= is_gl_identifier(var
->name
);
8461 if (redeclaring_per_vertex
) {
8462 bool is_redeclaration
;
8464 get_variable_being_redeclared(&var
, loc
, state
,
8465 true /* allow_all_redeclarations */,
8467 if (!var_is_gl_id
|| !is_redeclaration
) {
8468 _mesa_glsl_error(&loc
, state
,
8469 "redeclaration of gl_PerVertex can only "
8470 "include built-in variables");
8471 } else if (var
->data
.how_declared
== ir_var_declared_normally
) {
8472 _mesa_glsl_error(&loc
, state
,
8473 "`%s' has already been redeclared",
8476 var
->data
.how_declared
= ir_var_declared_in_block
;
8477 var
->reinit_interface_type(block_type
);
8482 if (state
->symbols
->get_variable(var
->name
) != NULL
)
8483 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
8485 /* Propagate the "binding" keyword into this UBO/SSBO's fields.
8486 * The UBO declaration itself doesn't get an ir_variable unless it
8487 * has an instance name. This is ugly.
8489 if (this->layout
.flags
.q
.explicit_binding
) {
8490 apply_explicit_binding(state
, &loc
, var
,
8491 var
->get_interface_type(), &this->layout
);
8494 if (var
->type
->is_unsized_array()) {
8495 if (var
->is_in_shader_storage_block() &&
8496 is_unsized_array_last_element(var
)) {
8497 var
->data
.from_ssbo_unsized_array
= true;
8499 /* From GLSL ES 3.10 spec, section 4.1.9 "Arrays":
8501 * "If an array is declared as the last member of a shader storage
8502 * block and the size is not specified at compile-time, it is
8503 * sized at run-time. In all other cases, arrays are sized only
8506 * In desktop GLSL it is allowed to have unsized-arrays that are
8507 * not last, as long as we can determine that they are implicitly
8510 if (state
->es_shader
) {
8511 _mesa_glsl_error(&loc
, state
, "unsized array `%s' "
8512 "definition: only last member of a shader "
8513 "storage block can be defined as unsized "
8514 "array", fields
[i
].name
);
8519 state
->symbols
->add_variable(var
);
8520 instructions
->push_tail(var
);
8523 if (redeclaring_per_vertex
&& block_type
!= earlier_per_vertex
) {
8524 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec:
8526 * It is also a compilation error ... to redeclare a built-in
8527 * block and then use a member from that built-in block that was
8528 * not included in the redeclaration.
8530 * This appears to be a clarification to the behaviour established
8531 * for gl_PerVertex by GLSL 1.50, therefore we implement this
8532 * behaviour regardless of GLSL version.
8534 * To prevent the shader from using a member that was not included in
8535 * the redeclaration, we disable any ir_variables that are still
8536 * associated with the old declaration of gl_PerVertex (since we've
8537 * already updated all of the variables contained in the new
8538 * gl_PerVertex to point to it).
8540 * As a side effect this will prevent
8541 * validate_intrastage_interface_blocks() from getting confused and
8542 * thinking there are conflicting definitions of gl_PerVertex in the
8545 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
8546 ir_variable
*const var
= node
->as_variable();
8548 var
->get_interface_type() == earlier_per_vertex
&&
8549 var
->data
.mode
== var_mode
) {
8550 if (var
->data
.how_declared
== ir_var_declared_normally
) {
8551 _mesa_glsl_error(&loc
, state
,
8552 "redeclaration of gl_PerVertex cannot "
8553 "follow a redeclaration of `%s'",
8556 state
->symbols
->disable_variable(var
->name
);
8568 ast_tcs_output_layout::hir(exec_list
*instructions
,
8569 struct _mesa_glsl_parse_state
*state
)
8571 YYLTYPE loc
= this->get_location();
8573 unsigned num_vertices
;
8574 if (!state
->out_qualifier
->vertices
->
8575 process_qualifier_constant(state
, "vertices", &num_vertices
,
8577 /* return here to stop cascading incorrect error messages */
8581 /* If any shader outputs occurred before this declaration and specified an
8582 * array size, make sure the size they specified is consistent with the
8585 if (state
->tcs_output_size
!= 0 && state
->tcs_output_size
!= num_vertices
) {
8586 _mesa_glsl_error(&loc
, state
,
8587 "this tessellation control shader output layout "
8588 "specifies %u vertices, but a previous output "
8589 "is declared with size %u",
8590 num_vertices
, state
->tcs_output_size
);
8594 state
->tcs_output_vertices_specified
= true;
8596 /* If any shader outputs occurred before this declaration and did not
8597 * specify an array size, their size is determined now.
8599 foreach_in_list (ir_instruction
, node
, instructions
) {
8600 ir_variable
*var
= node
->as_variable();
8601 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_out
)
8604 /* Note: Not all tessellation control shader output are arrays. */
8605 if (!var
->type
->is_unsized_array() || var
->data
.patch
)
8608 if (var
->data
.max_array_access
>= (int)num_vertices
) {
8609 _mesa_glsl_error(&loc
, state
,
8610 "this tessellation control shader output layout "
8611 "specifies %u vertices, but an access to element "
8612 "%u of output `%s' already exists", num_vertices
,
8613 var
->data
.max_array_access
, var
->name
);
8615 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
8625 ast_gs_input_layout::hir(exec_list
*instructions
,
8626 struct _mesa_glsl_parse_state
*state
)
8628 YYLTYPE loc
= this->get_location();
8630 /* Should have been prevented by the parser. */
8631 assert(!state
->gs_input_prim_type_specified
8632 || state
->in_qualifier
->prim_type
== this->prim_type
);
8634 /* If any shader inputs occurred before this declaration and specified an
8635 * array size, make sure the size they specified is consistent with the
8638 unsigned num_vertices
= vertices_per_prim(this->prim_type
);
8639 if (state
->gs_input_size
!= 0 && state
->gs_input_size
!= num_vertices
) {
8640 _mesa_glsl_error(&loc
, state
,
8641 "this geometry shader input layout implies %u vertices"
8642 " per primitive, but a previous input is declared"
8643 " with size %u", num_vertices
, state
->gs_input_size
);
8647 state
->gs_input_prim_type_specified
= true;
8649 /* If any shader inputs occurred before this declaration and did not
8650 * specify an array size, their size is determined now.
8652 foreach_in_list(ir_instruction
, node
, instructions
) {
8653 ir_variable
*var
= node
->as_variable();
8654 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_in
)
8657 /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
8661 if (var
->type
->is_unsized_array()) {
8662 if (var
->data
.max_array_access
>= (int)num_vertices
) {
8663 _mesa_glsl_error(&loc
, state
,
8664 "this geometry shader input layout implies %u"
8665 " vertices, but an access to element %u of input"
8666 " `%s' already exists", num_vertices
,
8667 var
->data
.max_array_access
, var
->name
);
8669 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
8680 ast_cs_input_layout::hir(exec_list
*instructions
,
8681 struct _mesa_glsl_parse_state
*state
)
8683 YYLTYPE loc
= this->get_location();
8685 /* From the ARB_compute_shader specification:
8687 * If the local size of the shader in any dimension is greater
8688 * than the maximum size supported by the implementation for that
8689 * dimension, a compile-time error results.
8691 * It is not clear from the spec how the error should be reported if
8692 * the total size of the work group exceeds
8693 * MAX_COMPUTE_WORK_GROUP_INVOCATIONS, but it seems reasonable to
8694 * report it at compile time as well.
8696 GLuint64 total_invocations
= 1;
8697 unsigned qual_local_size
[3];
8698 for (int i
= 0; i
< 3; i
++) {
8700 char *local_size_str
= ralloc_asprintf(NULL
, "invalid local_size_%c",
8702 /* Infer a local_size of 1 for unspecified dimensions */
8703 if (this->local_size
[i
] == NULL
) {
8704 qual_local_size
[i
] = 1;
8705 } else if (!this->local_size
[i
]->
8706 process_qualifier_constant(state
, local_size_str
,
8707 &qual_local_size
[i
], false)) {
8708 ralloc_free(local_size_str
);
8711 ralloc_free(local_size_str
);
8713 if (qual_local_size
[i
] > state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]) {
8714 _mesa_glsl_error(&loc
, state
,
8715 "local_size_%c exceeds MAX_COMPUTE_WORK_GROUP_SIZE"
8717 state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]);
8720 total_invocations
*= qual_local_size
[i
];
8721 if (total_invocations
>
8722 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
) {
8723 _mesa_glsl_error(&loc
, state
,
8724 "product of local_sizes exceeds "
8725 "MAX_COMPUTE_WORK_GROUP_INVOCATIONS (%d)",
8726 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
);
8731 /* If any compute input layout declaration preceded this one, make sure it
8732 * was consistent with this one.
8734 if (state
->cs_input_local_size_specified
) {
8735 for (int i
= 0; i
< 3; i
++) {
8736 if (state
->cs_input_local_size
[i
] != qual_local_size
[i
]) {
8737 _mesa_glsl_error(&loc
, state
,
8738 "compute shader input layout does not match"
8739 " previous declaration");
8745 /* The ARB_compute_variable_group_size spec says:
8747 * If a compute shader including a *local_size_variable* qualifier also
8748 * declares a fixed local group size using the *local_size_x*,
8749 * *local_size_y*, or *local_size_z* qualifiers, a compile-time error
8752 if (state
->cs_input_local_size_variable_specified
) {
8753 _mesa_glsl_error(&loc
, state
,
8754 "compute shader can't include both a variable and a "
8755 "fixed local group size");
8759 state
->cs_input_local_size_specified
= true;
8760 for (int i
= 0; i
< 3; i
++)
8761 state
->cs_input_local_size
[i
] = qual_local_size
[i
];
8763 /* We may now declare the built-in constant gl_WorkGroupSize (see
8764 * builtin_variable_generator::generate_constants() for why we didn't
8765 * declare it earlier).
8767 ir_variable
*var
= new(state
->symbols
)
8768 ir_variable(glsl_type::uvec3_type
, "gl_WorkGroupSize", ir_var_auto
);
8769 var
->data
.how_declared
= ir_var_declared_implicitly
;
8770 var
->data
.read_only
= true;
8771 instructions
->push_tail(var
);
8772 state
->symbols
->add_variable(var
);
8773 ir_constant_data data
;
8774 memset(&data
, 0, sizeof(data
));
8775 for (int i
= 0; i
< 3; i
++)
8776 data
.u
[i
] = qual_local_size
[i
];
8777 var
->constant_value
= new(var
) ir_constant(glsl_type::uvec3_type
, &data
);
8778 var
->constant_initializer
=
8779 new(var
) ir_constant(glsl_type::uvec3_type
, &data
);
8780 var
->data
.has_initializer
= true;
8781 var
->data
.is_implicit_initializer
= false;
8788 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
8789 exec_list
*instructions
)
8791 bool gl_FragColor_assigned
= false;
8792 bool gl_FragData_assigned
= false;
8793 bool gl_FragSecondaryColor_assigned
= false;
8794 bool gl_FragSecondaryData_assigned
= false;
8795 bool user_defined_fs_output_assigned
= false;
8796 ir_variable
*user_defined_fs_output
= NULL
;
8798 /* It would be nice to have proper location information. */
8800 memset(&loc
, 0, sizeof(loc
));
8802 foreach_in_list(ir_instruction
, node
, instructions
) {
8803 ir_variable
*var
= node
->as_variable();
8805 if (!var
|| !var
->data
.assigned
)
8808 if (strcmp(var
->name
, "gl_FragColor") == 0) {
8809 gl_FragColor_assigned
= true;
8810 if (!var
->constant_initializer
&& state
->zero_init
) {
8811 const ir_constant_data data
= { { 0 } };
8812 var
->data
.has_initializer
= true;
8813 var
->data
.is_implicit_initializer
= true;
8814 var
->constant_initializer
= new(var
) ir_constant(var
->type
, &data
);
8817 else if (strcmp(var
->name
, "gl_FragData") == 0)
8818 gl_FragData_assigned
= true;
8819 else if (strcmp(var
->name
, "gl_SecondaryFragColorEXT") == 0)
8820 gl_FragSecondaryColor_assigned
= true;
8821 else if (strcmp(var
->name
, "gl_SecondaryFragDataEXT") == 0)
8822 gl_FragSecondaryData_assigned
= true;
8823 else if (!is_gl_identifier(var
->name
)) {
8824 if (state
->stage
== MESA_SHADER_FRAGMENT
&&
8825 var
->data
.mode
== ir_var_shader_out
) {
8826 user_defined_fs_output_assigned
= true;
8827 user_defined_fs_output
= var
;
8832 /* From the GLSL 1.30 spec:
8834 * "If a shader statically assigns a value to gl_FragColor, it
8835 * may not assign a value to any element of gl_FragData. If a
8836 * shader statically writes a value to any element of
8837 * gl_FragData, it may not assign a value to
8838 * gl_FragColor. That is, a shader may assign values to either
8839 * gl_FragColor or gl_FragData, but not both. Multiple shaders
8840 * linked together must also consistently write just one of
8841 * these variables. Similarly, if user declared output
8842 * variables are in use (statically assigned to), then the
8843 * built-in variables gl_FragColor and gl_FragData may not be
8844 * assigned to. These incorrect usages all generate compile
8847 if (gl_FragColor_assigned
&& gl_FragData_assigned
) {
8848 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8849 "`gl_FragColor' and `gl_FragData'");
8850 } else if (gl_FragColor_assigned
&& user_defined_fs_output_assigned
) {
8851 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8852 "`gl_FragColor' and `%s'",
8853 user_defined_fs_output
->name
);
8854 } else if (gl_FragSecondaryColor_assigned
&& gl_FragSecondaryData_assigned
) {
8855 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8856 "`gl_FragSecondaryColorEXT' and"
8857 " `gl_FragSecondaryDataEXT'");
8858 } else if (gl_FragColor_assigned
&& gl_FragSecondaryData_assigned
) {
8859 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8860 "`gl_FragColor' and"
8861 " `gl_FragSecondaryDataEXT'");
8862 } else if (gl_FragData_assigned
&& gl_FragSecondaryColor_assigned
) {
8863 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8865 " `gl_FragSecondaryColorEXT'");
8866 } else if (gl_FragData_assigned
&& user_defined_fs_output_assigned
) {
8867 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8868 "`gl_FragData' and `%s'",
8869 user_defined_fs_output
->name
);
8872 if ((gl_FragSecondaryColor_assigned
|| gl_FragSecondaryData_assigned
) &&
8873 !state
->EXT_blend_func_extended_enable
) {
8874 _mesa_glsl_error(&loc
, state
,
8875 "Dual source blending requires EXT_blend_func_extended");
8880 verify_subroutine_associated_funcs(struct _mesa_glsl_parse_state
*state
)
8883 memset(&loc
, 0, sizeof(loc
));
8885 /* Section 6.1.2 (Subroutines) of the GLSL 4.00 spec says:
8887 * "A program will fail to compile or link if any shader
8888 * or stage contains two or more functions with the same
8889 * name if the name is associated with a subroutine type."
8892 for (int i
= 0; i
< state
->num_subroutines
; i
++) {
8893 unsigned definitions
= 0;
8894 ir_function
*fn
= state
->subroutines
[i
];
8895 /* Calculate number of function definitions with the same name */
8896 foreach_in_list(ir_function_signature
, sig
, &fn
->signatures
) {
8897 if (sig
->is_defined
) {
8898 if (++definitions
> 1) {
8899 _mesa_glsl_error(&loc
, state
,
8900 "%s shader contains two or more function "
8901 "definitions with name `%s', which is "
8902 "associated with a subroutine type.\n",
8903 _mesa_shader_stage_to_string(state
->stage
),
8913 remove_per_vertex_blocks(exec_list
*instructions
,
8914 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
)
8916 /* Find the gl_PerVertex interface block of the appropriate (in/out) mode,
8917 * if it exists in this shader type.
8919 const glsl_type
*per_vertex
= NULL
;
8921 case ir_var_shader_in
:
8922 if (ir_variable
*gl_in
= state
->symbols
->get_variable("gl_in"))
8923 per_vertex
= gl_in
->get_interface_type();
8925 case ir_var_shader_out
:
8926 if (ir_variable
*gl_Position
=
8927 state
->symbols
->get_variable("gl_Position")) {
8928 per_vertex
= gl_Position
->get_interface_type();
8932 assert(!"Unexpected mode");
8936 /* If we didn't find a built-in gl_PerVertex interface block, then we don't
8937 * need to do anything.
8939 if (per_vertex
== NULL
)
8942 /* If the interface block is used by the shader, then we don't need to do
8945 interface_block_usage_visitor
v(mode
, per_vertex
);
8946 v
.run(instructions
);
8947 if (v
.usage_found())
8950 /* Remove any ir_variable declarations that refer to the interface block
8953 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
8954 ir_variable
*const var
= node
->as_variable();
8955 if (var
!= NULL
&& var
->get_interface_type() == per_vertex
&&
8956 var
->data
.mode
== mode
) {
8957 state
->symbols
->disable_variable(var
->name
);
8964 ast_warnings_toggle::hir(exec_list
*,
8965 struct _mesa_glsl_parse_state
*state
)
8967 state
->warnings_enabled
= enable
;