2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
26 * Convert abstract syntax to to high-level intermediate reprensentation (HIR).
28 * During the conversion to HIR, the majority of the symantic checking is
29 * preformed on the program. This includes:
31 * * Symbol table management
35 * The majority of this work could be done during parsing, and the parser could
36 * probably generate HIR directly. However, this results in frequent changes
37 * to the parser code. Since we do not assume that every system this complier
38 * is built on will have Flex and Bison installed, we have to store the code
39 * generated by these tools in our version control system. In other parts of
40 * the system we've seen problems where a parser was changed but the generated
41 * code was not committed, merge conflicts where created because two developers
42 * had slightly different versions of Bison installed, etc.
44 * I have also noticed that running Bison generated parsers in GDB is very
45 * irritating. When you get a segfault on '$$ = $1->foo', you can't very
46 * well 'print $1' in GDB.
48 * As a result, my preference is to put as little C code as possible in the
49 * parser (and lexer) sources.
52 #include "glsl_symbol_table.h"
53 #include "glsl_parser_extras.h"
55 #include "compiler/glsl_types.h"
56 #include "util/hash_table.h"
57 #include "main/mtypes.h"
58 #include "main/macros.h"
59 #include "main/shaderobj.h"
61 #include "ir_builder.h"
62 #include "builtin_functions.h"
64 using namespace ir_builder
;
67 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
68 exec_list
*instructions
);
70 verify_subroutine_associated_funcs(struct _mesa_glsl_parse_state
*state
);
73 remove_per_vertex_blocks(exec_list
*instructions
,
74 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
);
77 * Visitor class that finds the first instance of any write-only variable that
78 * is ever read, if any
80 class read_from_write_only_variable_visitor
: public ir_hierarchical_visitor
83 read_from_write_only_variable_visitor() : found(NULL
)
87 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
89 if (this->in_assignee
)
90 return visit_continue
;
92 ir_variable
*var
= ir
->variable_referenced();
93 /* We can have memory_write_only set on both images and buffer variables,
94 * but in the former there is a distinction between reads from
95 * the variable itself (write_only) and from the memory they point to
96 * (memory_write_only), while in the case of buffer variables there is
97 * no such distinction, that is why this check here is limited to
98 * buffer variables alone.
100 if (!var
|| var
->data
.mode
!= ir_var_shader_storage
)
101 return visit_continue
;
103 if (var
->data
.memory_write_only
) {
108 return visit_continue
;
111 ir_variable
*get_variable() {
115 virtual ir_visitor_status
visit_enter(ir_expression
*ir
)
117 /* .length() doesn't actually read anything */
118 if (ir
->operation
== ir_unop_ssbo_unsized_array_length
)
119 return visit_continue_with_parent
;
121 return visit_continue
;
129 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
131 _mesa_glsl_initialize_variables(instructions
, state
);
133 state
->symbols
->separate_function_namespace
= state
->language_version
== 110;
135 state
->current_function
= NULL
;
137 state
->toplevel_ir
= instructions
;
139 state
->gs_input_prim_type_specified
= false;
140 state
->tcs_output_vertices_specified
= false;
141 state
->cs_input_local_size_specified
= false;
143 /* Section 4.2 of the GLSL 1.20 specification states:
144 * "The built-in functions are scoped in a scope outside the global scope
145 * users declare global variables in. That is, a shader's global scope,
146 * available for user-defined functions and global variables, is nested
147 * inside the scope containing the built-in functions."
149 * Since built-in functions like ftransform() access built-in variables,
150 * it follows that those must be in the outer scope as well.
152 * We push scope here to create this nesting effect...but don't pop.
153 * This way, a shader's globals are still in the symbol table for use
156 state
->symbols
->push_scope();
158 foreach_list_typed (ast_node
, ast
, link
, & state
->translation_unit
)
159 ast
->hir(instructions
, state
);
161 verify_subroutine_associated_funcs(state
);
162 detect_recursion_unlinked(state
, instructions
);
163 detect_conflicting_assignments(state
, instructions
);
165 state
->toplevel_ir
= NULL
;
167 /* Move all of the variable declarations to the front of the IR list, and
168 * reverse the order. This has the (intended!) side effect that vertex
169 * shader inputs and fragment shader outputs will appear in the IR in the
170 * same order that they appeared in the shader code. This results in the
171 * locations being assigned in the declared order. Many (arguably buggy)
172 * applications depend on this behavior, and it matches what nearly all
175 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
176 ir_variable
*const var
= node
->as_variable();
182 instructions
->push_head(var
);
185 /* Figure out if gl_FragCoord is actually used in fragment shader */
186 ir_variable
*const var
= state
->symbols
->get_variable("gl_FragCoord");
188 state
->fs_uses_gl_fragcoord
= var
->data
.used
;
190 /* From section 7.1 (Built-In Language Variables) of the GLSL 4.10 spec:
192 * If multiple shaders using members of a built-in block belonging to
193 * the same interface are linked together in the same program, they
194 * must all redeclare the built-in block in the same way, as described
195 * in section 4.3.7 "Interface Blocks" for interface block matching, or
196 * a link error will result.
198 * The phrase "using members of a built-in block" implies that if two
199 * shaders are linked together and one of them *does not use* any members
200 * of the built-in block, then that shader does not need to have a matching
201 * redeclaration of the built-in block.
203 * This appears to be a clarification to the behaviour established for
204 * gl_PerVertex by GLSL 1.50, therefore implement it regardless of GLSL
207 * The definition of "interface" in section 4.3.7 that applies here is as
210 * The boundary between adjacent programmable pipeline stages: This
211 * spans all the outputs in all compilation units of the first stage
212 * and all the inputs in all compilation units of the second stage.
214 * Therefore this rule applies to both inter- and intra-stage linking.
216 * The easiest way to implement this is to check whether the shader uses
217 * gl_PerVertex right after ast-to-ir conversion, and if it doesn't, simply
218 * remove all the relevant variable declaration from the IR, so that the
219 * linker won't see them and complain about mismatches.
221 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_in
);
222 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_out
);
224 /* Check that we don't have reads from write-only variables */
225 read_from_write_only_variable_visitor v
;
227 ir_variable
*error_var
= v
.get_variable();
229 /* It would be nice to have proper location information, but for that
230 * we would need to check this as we process each kind of AST node
233 memset(&loc
, 0, sizeof(loc
));
234 _mesa_glsl_error(&loc
, state
, "Read from write-only variable `%s'",
240 static ir_expression_operation
241 get_implicit_conversion_operation(const glsl_type
*to
, const glsl_type
*from
,
242 struct _mesa_glsl_parse_state
*state
)
244 switch (to
->base_type
) {
245 case GLSL_TYPE_FLOAT
:
246 switch (from
->base_type
) {
247 case GLSL_TYPE_INT
: return ir_unop_i2f
;
248 case GLSL_TYPE_UINT
: return ir_unop_u2f
;
249 default: return (ir_expression_operation
)0;
253 if (!state
->has_implicit_uint_to_int_conversion())
254 return (ir_expression_operation
)0;
255 switch (from
->base_type
) {
256 case GLSL_TYPE_INT
: return ir_unop_i2u
;
257 default: return (ir_expression_operation
)0;
260 case GLSL_TYPE_DOUBLE
:
261 if (!state
->has_double())
262 return (ir_expression_operation
)0;
263 switch (from
->base_type
) {
264 case GLSL_TYPE_INT
: return ir_unop_i2d
;
265 case GLSL_TYPE_UINT
: return ir_unop_u2d
;
266 case GLSL_TYPE_FLOAT
: return ir_unop_f2d
;
267 case GLSL_TYPE_INT64
: return ir_unop_i642d
;
268 case GLSL_TYPE_UINT64
: return ir_unop_u642d
;
269 default: return (ir_expression_operation
)0;
272 case GLSL_TYPE_UINT64
:
273 if (!state
->has_int64())
274 return (ir_expression_operation
)0;
275 switch (from
->base_type
) {
276 case GLSL_TYPE_INT
: return ir_unop_i2u64
;
277 case GLSL_TYPE_UINT
: return ir_unop_u2u64
;
278 case GLSL_TYPE_INT64
: return ir_unop_i642u64
;
279 default: return (ir_expression_operation
)0;
282 case GLSL_TYPE_INT64
:
283 if (!state
->has_int64())
284 return (ir_expression_operation
)0;
285 switch (from
->base_type
) {
286 case GLSL_TYPE_INT
: return ir_unop_i2i64
;
287 default: return (ir_expression_operation
)0;
290 default: return (ir_expression_operation
)0;
296 * If a conversion is available, convert one operand to a different type
298 * The \c from \c ir_rvalue is converted "in place".
300 * \param to Type that the operand it to be converted to
301 * \param from Operand that is being converted
302 * \param state GLSL compiler state
305 * If a conversion is possible (or unnecessary), \c true is returned.
306 * Otherwise \c false is returned.
309 apply_implicit_conversion(const glsl_type
*to
, ir_rvalue
* &from
,
310 struct _mesa_glsl_parse_state
*state
)
313 if (to
->base_type
== from
->type
->base_type
)
316 /* Prior to GLSL 1.20, there are no implicit conversions */
317 if (!state
->has_implicit_conversions())
320 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
322 * "There are no implicit array or structure conversions. For
323 * example, an array of int cannot be implicitly converted to an
326 if (!to
->is_numeric() || !from
->type
->is_numeric())
329 /* We don't actually want the specific type `to`, we want a type
330 * with the same base type as `to`, but the same vector width as
333 to
= glsl_type::get_instance(to
->base_type
, from
->type
->vector_elements
,
334 from
->type
->matrix_columns
);
336 ir_expression_operation op
= get_implicit_conversion_operation(to
, from
->type
, state
);
338 from
= new(ctx
) ir_expression(op
, to
, from
, NULL
);
346 static const struct glsl_type
*
347 arithmetic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
349 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
351 const glsl_type
*type_a
= value_a
->type
;
352 const glsl_type
*type_b
= value_b
->type
;
354 /* From GLSL 1.50 spec, page 56:
356 * "The arithmetic binary operators add (+), subtract (-),
357 * multiply (*), and divide (/) operate on integer and
358 * floating-point scalars, vectors, and matrices."
360 if (!type_a
->is_numeric() || !type_b
->is_numeric()) {
361 _mesa_glsl_error(loc
, state
,
362 "operands to arithmetic operators must be numeric");
363 return glsl_type::error_type
;
367 /* "If one operand is floating-point based and the other is
368 * not, then the conversions from Section 4.1.10 "Implicit
369 * Conversions" are applied to the non-floating-point-based operand."
371 if (!apply_implicit_conversion(type_a
, value_b
, state
)
372 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
373 _mesa_glsl_error(loc
, state
,
374 "could not implicitly convert operands to "
375 "arithmetic operator");
376 return glsl_type::error_type
;
378 type_a
= value_a
->type
;
379 type_b
= value_b
->type
;
381 /* "If the operands are integer types, they must both be signed or
384 * From this rule and the preceeding conversion it can be inferred that
385 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
386 * The is_numeric check above already filtered out the case where either
387 * type is not one of these, so now the base types need only be tested for
390 if (type_a
->base_type
!= type_b
->base_type
) {
391 _mesa_glsl_error(loc
, state
,
392 "base type mismatch for arithmetic operator");
393 return glsl_type::error_type
;
396 /* "All arithmetic binary operators result in the same fundamental type
397 * (signed integer, unsigned integer, or floating-point) as the
398 * operands they operate on, after operand type conversion. After
399 * conversion, the following cases are valid
401 * * The two operands are scalars. In this case the operation is
402 * applied, resulting in a scalar."
404 if (type_a
->is_scalar() && type_b
->is_scalar())
407 /* "* One operand is a scalar, and the other is a vector or matrix.
408 * In this case, the scalar operation is applied independently to each
409 * component of the vector or matrix, resulting in the same size
412 if (type_a
->is_scalar()) {
413 if (!type_b
->is_scalar())
415 } else if (type_b
->is_scalar()) {
419 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
420 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
423 assert(!type_a
->is_scalar());
424 assert(!type_b
->is_scalar());
426 /* "* The two operands are vectors of the same size. In this case, the
427 * operation is done component-wise resulting in the same size
430 if (type_a
->is_vector() && type_b
->is_vector()) {
431 if (type_a
== type_b
) {
434 _mesa_glsl_error(loc
, state
,
435 "vector size mismatch for arithmetic operator");
436 return glsl_type::error_type
;
440 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
441 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
442 * <vector, vector> have been handled. At least one of the operands must
443 * be matrix. Further, since there are no integer matrix types, the base
444 * type of both operands must be float.
446 assert(type_a
->is_matrix() || type_b
->is_matrix());
447 assert(type_a
->is_float() || type_a
->is_double());
448 assert(type_b
->is_float() || type_b
->is_double());
450 /* "* The operator is add (+), subtract (-), or divide (/), and the
451 * operands are matrices with the same number of rows and the same
452 * number of columns. In this case, the operation is done component-
453 * wise resulting in the same size matrix."
454 * * The operator is multiply (*), where both operands are matrices or
455 * one operand is a vector and the other a matrix. A right vector
456 * operand is treated as a column vector and a left vector operand as a
457 * row vector. In all these cases, it is required that the number of
458 * columns of the left operand is equal to the number of rows of the
459 * right operand. Then, the multiply (*) operation does a linear
460 * algebraic multiply, yielding an object that has the same number of
461 * rows as the left operand and the same number of columns as the right
462 * operand. Section 5.10 "Vector and Matrix Operations" explains in
463 * more detail how vectors and matrices are operated on."
466 if (type_a
== type_b
)
469 const glsl_type
*type
= glsl_type::get_mul_type(type_a
, type_b
);
471 if (type
== glsl_type::error_type
) {
472 _mesa_glsl_error(loc
, state
,
473 "size mismatch for matrix multiplication");
480 /* "All other cases are illegal."
482 _mesa_glsl_error(loc
, state
, "type mismatch");
483 return glsl_type::error_type
;
487 static const struct glsl_type
*
488 unary_arithmetic_result_type(const struct glsl_type
*type
,
489 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
491 /* From GLSL 1.50 spec, page 57:
493 * "The arithmetic unary operators negate (-), post- and pre-increment
494 * and decrement (-- and ++) operate on integer or floating-point
495 * values (including vectors and matrices). All unary operators work
496 * component-wise on their operands. These result with the same type
499 if (!type
->is_numeric()) {
500 _mesa_glsl_error(loc
, state
,
501 "operands to arithmetic operators must be numeric");
502 return glsl_type::error_type
;
509 * \brief Return the result type of a bit-logic operation.
511 * If the given types to the bit-logic operator are invalid, return
512 * glsl_type::error_type.
514 * \param value_a LHS of bit-logic op
515 * \param value_b RHS of bit-logic op
517 static const struct glsl_type
*
518 bit_logic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
520 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
522 const glsl_type
*type_a
= value_a
->type
;
523 const glsl_type
*type_b
= value_b
->type
;
525 if (!state
->check_bitwise_operations_allowed(loc
)) {
526 return glsl_type::error_type
;
529 /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
531 * "The bitwise operators and (&), exclusive-or (^), and inclusive-or
532 * (|). The operands must be of type signed or unsigned integers or
535 if (!type_a
->is_integer_32_64()) {
536 _mesa_glsl_error(loc
, state
, "LHS of `%s' must be an integer",
537 ast_expression::operator_string(op
));
538 return glsl_type::error_type
;
540 if (!type_b
->is_integer_32_64()) {
541 _mesa_glsl_error(loc
, state
, "RHS of `%s' must be an integer",
542 ast_expression::operator_string(op
));
543 return glsl_type::error_type
;
546 /* Prior to GLSL 4.0 / GL_ARB_gpu_shader5, implicit conversions didn't
547 * make sense for bitwise operations, as they don't operate on floats.
549 * GLSL 4.0 added implicit int -> uint conversions, which are relevant
550 * here. It wasn't clear whether or not we should apply them to bitwise
551 * operations. However, Khronos has decided that they should in future
552 * language revisions. Applications also rely on this behavior. We opt
553 * to apply them in general, but issue a portability warning.
555 * See https://www.khronos.org/bugzilla/show_bug.cgi?id=1405
557 if (type_a
->base_type
!= type_b
->base_type
) {
558 if (!apply_implicit_conversion(type_a
, value_b
, state
)
559 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
560 _mesa_glsl_error(loc
, state
,
561 "could not implicitly convert operands to "
563 ast_expression::operator_string(op
));
564 return glsl_type::error_type
;
566 _mesa_glsl_warning(loc
, state
,
567 "some implementations may not support implicit "
568 "int -> uint conversions for `%s' operators; "
569 "consider casting explicitly for portability",
570 ast_expression::operator_string(op
));
572 type_a
= value_a
->type
;
573 type_b
= value_b
->type
;
576 /* "The fundamental types of the operands (signed or unsigned) must
579 if (type_a
->base_type
!= type_b
->base_type
) {
580 _mesa_glsl_error(loc
, state
, "operands of `%s' must have the same "
581 "base type", ast_expression::operator_string(op
));
582 return glsl_type::error_type
;
585 /* "The operands cannot be vectors of differing size." */
586 if (type_a
->is_vector() &&
587 type_b
->is_vector() &&
588 type_a
->vector_elements
!= type_b
->vector_elements
) {
589 _mesa_glsl_error(loc
, state
, "operands of `%s' cannot be vectors of "
590 "different sizes", ast_expression::operator_string(op
));
591 return glsl_type::error_type
;
594 /* "If one operand is a scalar and the other a vector, the scalar is
595 * applied component-wise to the vector, resulting in the same type as
596 * the vector. The fundamental types of the operands [...] will be the
597 * resulting fundamental type."
599 if (type_a
->is_scalar())
605 static const struct glsl_type
*
606 modulus_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
607 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
609 const glsl_type
*type_a
= value_a
->type
;
610 const glsl_type
*type_b
= value_b
->type
;
612 if (!state
->EXT_gpu_shader4_enable
&&
613 !state
->check_version(130, 300, loc
, "operator '%%' is reserved")) {
614 return glsl_type::error_type
;
617 /* Section 5.9 (Expressions) of the GLSL 4.00 specification says:
619 * "The operator modulus (%) operates on signed or unsigned integers or
622 if (!type_a
->is_integer_32_64()) {
623 _mesa_glsl_error(loc
, state
, "LHS of operator %% must be an integer");
624 return glsl_type::error_type
;
626 if (!type_b
->is_integer_32_64()) {
627 _mesa_glsl_error(loc
, state
, "RHS of operator %% must be an integer");
628 return glsl_type::error_type
;
631 /* "If the fundamental types in the operands do not match, then the
632 * conversions from section 4.1.10 "Implicit Conversions" are applied
633 * to create matching types."
635 * Note that GLSL 4.00 (and GL_ARB_gpu_shader5) introduced implicit
636 * int -> uint conversion rules. Prior to that, there were no implicit
637 * conversions. So it's harmless to apply them universally - no implicit
638 * conversions will exist. If the types don't match, we'll receive false,
639 * and raise an error, satisfying the GLSL 1.50 spec, page 56:
641 * "The operand types must both be signed or unsigned."
643 if (!apply_implicit_conversion(type_a
, value_b
, state
) &&
644 !apply_implicit_conversion(type_b
, value_a
, state
)) {
645 _mesa_glsl_error(loc
, state
,
646 "could not implicitly convert operands to "
647 "modulus (%%) operator");
648 return glsl_type::error_type
;
650 type_a
= value_a
->type
;
651 type_b
= value_b
->type
;
653 /* "The operands cannot be vectors of differing size. If one operand is
654 * a scalar and the other vector, then the scalar is applied component-
655 * wise to the vector, resulting in the same type as the vector. If both
656 * are vectors of the same size, the result is computed component-wise."
658 if (type_a
->is_vector()) {
659 if (!type_b
->is_vector()
660 || (type_a
->vector_elements
== type_b
->vector_elements
))
665 /* "The operator modulus (%) is not defined for any other data types
666 * (non-integer types)."
668 _mesa_glsl_error(loc
, state
, "type mismatch");
669 return glsl_type::error_type
;
673 static const struct glsl_type
*
674 relational_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
675 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
677 const glsl_type
*type_a
= value_a
->type
;
678 const glsl_type
*type_b
= value_b
->type
;
680 /* From GLSL 1.50 spec, page 56:
681 * "The relational operators greater than (>), less than (<), greater
682 * than or equal (>=), and less than or equal (<=) operate only on
683 * scalar integer and scalar floating-point expressions."
685 if (!type_a
->is_numeric()
686 || !type_b
->is_numeric()
687 || !type_a
->is_scalar()
688 || !type_b
->is_scalar()) {
689 _mesa_glsl_error(loc
, state
,
690 "operands to relational operators must be scalar and "
692 return glsl_type::error_type
;
695 /* "Either the operands' types must match, or the conversions from
696 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
697 * operand, after which the types must match."
699 if (!apply_implicit_conversion(type_a
, value_b
, state
)
700 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
701 _mesa_glsl_error(loc
, state
,
702 "could not implicitly convert operands to "
703 "relational operator");
704 return glsl_type::error_type
;
706 type_a
= value_a
->type
;
707 type_b
= value_b
->type
;
709 if (type_a
->base_type
!= type_b
->base_type
) {
710 _mesa_glsl_error(loc
, state
, "base type mismatch");
711 return glsl_type::error_type
;
714 /* "The result is scalar Boolean."
716 return glsl_type::bool_type
;
720 * \brief Return the result type of a bit-shift operation.
722 * If the given types to the bit-shift operator are invalid, return
723 * glsl_type::error_type.
725 * \param type_a Type of LHS of bit-shift op
726 * \param type_b Type of RHS of bit-shift op
728 static const struct glsl_type
*
729 shift_result_type(const struct glsl_type
*type_a
,
730 const struct glsl_type
*type_b
,
732 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
734 if (!state
->check_bitwise_operations_allowed(loc
)) {
735 return glsl_type::error_type
;
738 /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
740 * "The shift operators (<<) and (>>). For both operators, the operands
741 * must be signed or unsigned integers or integer vectors. One operand
742 * can be signed while the other is unsigned."
744 if (!type_a
->is_integer_32_64()) {
745 _mesa_glsl_error(loc
, state
, "LHS of operator %s must be an integer or "
746 "integer vector", ast_expression::operator_string(op
));
747 return glsl_type::error_type
;
750 if (!type_b
->is_integer_32()) {
751 _mesa_glsl_error(loc
, state
, "RHS of operator %s must be an integer or "
752 "integer vector", ast_expression::operator_string(op
));
753 return glsl_type::error_type
;
756 /* "If the first operand is a scalar, the second operand has to be
759 if (type_a
->is_scalar() && !type_b
->is_scalar()) {
760 _mesa_glsl_error(loc
, state
, "if the first operand of %s is scalar, the "
761 "second must be scalar as well",
762 ast_expression::operator_string(op
));
763 return glsl_type::error_type
;
766 /* If both operands are vectors, check that they have same number of
769 if (type_a
->is_vector() &&
770 type_b
->is_vector() &&
771 type_a
->vector_elements
!= type_b
->vector_elements
) {
772 _mesa_glsl_error(loc
, state
, "vector operands to operator %s must "
773 "have same number of elements",
774 ast_expression::operator_string(op
));
775 return glsl_type::error_type
;
778 /* "In all cases, the resulting type will be the same type as the left
785 * Returns the innermost array index expression in an rvalue tree.
786 * This is the largest indexing level -- if an array of blocks, then
787 * it is the block index rather than an indexing expression for an
788 * array-typed member of an array of blocks.
791 find_innermost_array_index(ir_rvalue
*rv
)
793 ir_dereference_array
*last
= NULL
;
795 if (rv
->as_dereference_array()) {
796 last
= rv
->as_dereference_array();
798 } else if (rv
->as_dereference_record())
799 rv
= rv
->as_dereference_record()->record
;
800 else if (rv
->as_swizzle())
801 rv
= rv
->as_swizzle()->val
;
807 return last
->array_index
;
813 * Validates that a value can be assigned to a location with a specified type
815 * Validates that \c rhs can be assigned to some location. If the types are
816 * not an exact match but an automatic conversion is possible, \c rhs will be
820 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
821 * Otherwise the actual RHS to be assigned will be returned. This may be
822 * \c rhs, or it may be \c rhs after some type conversion.
825 * In addition to being used for assignments, this function is used to
826 * type-check return values.
829 validate_assignment(struct _mesa_glsl_parse_state
*state
,
830 YYLTYPE loc
, ir_rvalue
*lhs
,
831 ir_rvalue
*rhs
, bool is_initializer
)
833 /* If there is already some error in the RHS, just return it. Anything
834 * else will lead to an avalanche of error message back to the user.
836 if (rhs
->type
->is_error())
839 /* In the Tessellation Control Shader:
840 * If a per-vertex output variable is used as an l-value, it is an error
841 * if the expression indicating the vertex number is not the identifier
844 if (state
->stage
== MESA_SHADER_TESS_CTRL
&& !lhs
->type
->is_error()) {
845 ir_variable
*var
= lhs
->variable_referenced();
846 if (var
&& var
->data
.mode
== ir_var_shader_out
&& !var
->data
.patch
) {
847 ir_rvalue
*index
= find_innermost_array_index(lhs
);
848 ir_variable
*index_var
= index
? index
->variable_referenced() : NULL
;
849 if (!index_var
|| strcmp(index_var
->name
, "gl_InvocationID") != 0) {
850 _mesa_glsl_error(&loc
, state
,
851 "Tessellation control shader outputs can only "
852 "be indexed by gl_InvocationID");
858 /* If the types are identical, the assignment can trivially proceed.
860 if (rhs
->type
== lhs
->type
)
863 /* If the array element types are the same and the LHS is unsized,
864 * the assignment is okay for initializers embedded in variable
867 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
868 * is handled by ir_dereference::is_lvalue.
870 const glsl_type
*lhs_t
= lhs
->type
;
871 const glsl_type
*rhs_t
= rhs
->type
;
872 bool unsized_array
= false;
873 while(lhs_t
->is_array()) {
875 break; /* the rest of the inner arrays match so break out early */
876 if (!rhs_t
->is_array()) {
877 unsized_array
= false;
878 break; /* number of dimensions mismatch */
880 if (lhs_t
->length
== rhs_t
->length
) {
881 lhs_t
= lhs_t
->fields
.array
;
882 rhs_t
= rhs_t
->fields
.array
;
884 } else if (lhs_t
->is_unsized_array()) {
885 unsized_array
= true;
887 unsized_array
= false;
888 break; /* sized array mismatch */
890 lhs_t
= lhs_t
->fields
.array
;
891 rhs_t
= rhs_t
->fields
.array
;
894 if (is_initializer
) {
895 if (rhs
->type
->get_scalar_type() == lhs
->type
->get_scalar_type())
898 _mesa_glsl_error(&loc
, state
,
899 "implicitly sized arrays cannot be assigned");
904 /* Check for implicit conversion in GLSL 1.20 */
905 if (apply_implicit_conversion(lhs
->type
, rhs
, state
)) {
906 if (rhs
->type
== lhs
->type
)
910 _mesa_glsl_error(&loc
, state
,
911 "%s of type %s cannot be assigned to "
912 "variable of type %s",
913 is_initializer
? "initializer" : "value",
914 rhs
->type
->name
, lhs
->type
->name
);
920 mark_whole_array_access(ir_rvalue
*access
)
922 ir_dereference_variable
*deref
= access
->as_dereference_variable();
924 if (deref
&& deref
->var
) {
925 deref
->var
->data
.max_array_access
= deref
->type
->length
- 1;
930 do_assignment(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
931 const char *non_lvalue_description
,
932 ir_rvalue
*lhs
, ir_rvalue
*rhs
,
933 ir_rvalue
**out_rvalue
, bool needs_rvalue
,
938 bool error_emitted
= (lhs
->type
->is_error() || rhs
->type
->is_error());
940 ir_variable
*lhs_var
= lhs
->variable_referenced();
942 lhs_var
->data
.assigned
= true;
944 if (!error_emitted
) {
945 if (non_lvalue_description
!= NULL
) {
946 _mesa_glsl_error(&lhs_loc
, state
,
948 non_lvalue_description
);
949 error_emitted
= true;
950 } else if (lhs_var
!= NULL
&& (lhs_var
->data
.read_only
||
951 (lhs_var
->data
.mode
== ir_var_shader_storage
&&
952 lhs_var
->data
.memory_read_only
))) {
953 /* We can have memory_read_only set on both images and buffer variables,
954 * but in the former there is a distinction between assignments to
955 * the variable itself (read_only) and to the memory they point to
956 * (memory_read_only), while in the case of buffer variables there is
957 * no such distinction, that is why this check here is limited to
958 * buffer variables alone.
960 _mesa_glsl_error(&lhs_loc
, state
,
961 "assignment to read-only variable '%s'",
963 error_emitted
= true;
964 } else if (lhs
->type
->is_array() &&
965 !state
->check_version(120, 300, &lhs_loc
,
966 "whole array assignment forbidden")) {
967 /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
969 * "Other binary or unary expressions, non-dereferenced
970 * arrays, function names, swizzles with repeated fields,
971 * and constants cannot be l-values."
973 * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00.
975 error_emitted
= true;
976 } else if (!lhs
->is_lvalue(state
)) {
977 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
978 error_emitted
= true;
983 validate_assignment(state
, lhs_loc
, lhs
, rhs
, is_initializer
);
984 if (new_rhs
!= NULL
) {
987 /* If the LHS array was not declared with a size, it takes it size from
988 * the RHS. If the LHS is an l-value and a whole array, it must be a
989 * dereference of a variable. Any other case would require that the LHS
990 * is either not an l-value or not a whole array.
992 if (lhs
->type
->is_unsized_array()) {
993 ir_dereference
*const d
= lhs
->as_dereference();
997 ir_variable
*const var
= d
->variable_referenced();
1001 if (var
->data
.max_array_access
>= rhs
->type
->array_size()) {
1002 /* FINISHME: This should actually log the location of the RHS. */
1003 _mesa_glsl_error(& lhs_loc
, state
, "array size must be > %u due to "
1005 var
->data
.max_array_access
);
1008 var
->type
= glsl_type::get_array_instance(lhs
->type
->fields
.array
,
1009 rhs
->type
->array_size());
1010 d
->type
= var
->type
;
1012 if (lhs
->type
->is_array()) {
1013 mark_whole_array_access(rhs
);
1014 mark_whole_array_access(lhs
);
1017 error_emitted
= true;
1020 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
1021 * but not post_inc) need the converted assigned value as an rvalue
1022 * to handle things like:
1028 if (!error_emitted
) {
1029 ir_variable
*var
= new(ctx
) ir_variable(rhs
->type
, "assignment_tmp",
1031 instructions
->push_tail(var
);
1032 instructions
->push_tail(assign(var
, rhs
));
1034 ir_dereference_variable
*deref_var
=
1035 new(ctx
) ir_dereference_variable(var
);
1036 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, deref_var
));
1037 rvalue
= new(ctx
) ir_dereference_variable(var
);
1039 rvalue
= ir_rvalue::error_value(ctx
);
1041 *out_rvalue
= rvalue
;
1044 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, rhs
));
1048 return error_emitted
;
1052 get_lvalue_copy(exec_list
*instructions
, ir_rvalue
*lvalue
)
1054 void *ctx
= ralloc_parent(lvalue
);
1057 var
= new(ctx
) ir_variable(lvalue
->type
, "_post_incdec_tmp",
1059 instructions
->push_tail(var
);
1061 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
1064 return new(ctx
) ir_dereference_variable(var
);
1069 ast_node::hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
1071 (void) instructions
;
1078 ast_node::has_sequence_subexpression() const
1084 ast_node::set_is_lhs(bool /* new_value */)
1089 ast_function_expression::hir_no_rvalue(exec_list
*instructions
,
1090 struct _mesa_glsl_parse_state
*state
)
1092 (void)hir(instructions
, state
);
1096 ast_aggregate_initializer::hir_no_rvalue(exec_list
*instructions
,
1097 struct _mesa_glsl_parse_state
*state
)
1099 (void)hir(instructions
, state
);
1103 do_comparison(void *mem_ctx
, int operation
, ir_rvalue
*op0
, ir_rvalue
*op1
)
1106 ir_rvalue
*cmp
= NULL
;
1108 if (operation
== ir_binop_all_equal
)
1109 join_op
= ir_binop_logic_and
;
1111 join_op
= ir_binop_logic_or
;
1113 switch (op0
->type
->base_type
) {
1114 case GLSL_TYPE_FLOAT
:
1115 case GLSL_TYPE_FLOAT16
:
1116 case GLSL_TYPE_UINT
:
1118 case GLSL_TYPE_BOOL
:
1119 case GLSL_TYPE_DOUBLE
:
1120 case GLSL_TYPE_UINT64
:
1121 case GLSL_TYPE_INT64
:
1122 case GLSL_TYPE_UINT16
:
1123 case GLSL_TYPE_INT16
:
1124 case GLSL_TYPE_UINT8
:
1125 case GLSL_TYPE_INT8
:
1126 return new(mem_ctx
) ir_expression(operation
, op0
, op1
);
1128 case GLSL_TYPE_ARRAY
: {
1129 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
1130 ir_rvalue
*e0
, *e1
, *result
;
1132 e0
= new(mem_ctx
) ir_dereference_array(op0
->clone(mem_ctx
, NULL
),
1133 new(mem_ctx
) ir_constant(i
));
1134 e1
= new(mem_ctx
) ir_dereference_array(op1
->clone(mem_ctx
, NULL
),
1135 new(mem_ctx
) ir_constant(i
));
1136 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
1139 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
1145 mark_whole_array_access(op0
);
1146 mark_whole_array_access(op1
);
1150 case GLSL_TYPE_STRUCT
: {
1151 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
1152 ir_rvalue
*e0
, *e1
, *result
;
1153 const char *field_name
= op0
->type
->fields
.structure
[i
].name
;
1155 e0
= new(mem_ctx
) ir_dereference_record(op0
->clone(mem_ctx
, NULL
),
1157 e1
= new(mem_ctx
) ir_dereference_record(op1
->clone(mem_ctx
, NULL
),
1159 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
1162 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
1170 case GLSL_TYPE_ERROR
:
1171 case GLSL_TYPE_VOID
:
1172 case GLSL_TYPE_SAMPLER
:
1173 case GLSL_TYPE_IMAGE
:
1174 case GLSL_TYPE_INTERFACE
:
1175 case GLSL_TYPE_ATOMIC_UINT
:
1176 case GLSL_TYPE_SUBROUTINE
:
1177 case GLSL_TYPE_FUNCTION
:
1178 /* I assume a comparison of a struct containing a sampler just
1179 * ignores the sampler present in the type.
1185 cmp
= new(mem_ctx
) ir_constant(true);
1190 /* For logical operations, we want to ensure that the operands are
1191 * scalar booleans. If it isn't, emit an error and return a constant
1192 * boolean to avoid triggering cascading error messages.
1195 get_scalar_boolean_operand(exec_list
*instructions
,
1196 struct _mesa_glsl_parse_state
*state
,
1197 ast_expression
*parent_expr
,
1199 const char *operand_name
,
1200 bool *error_emitted
)
1202 ast_expression
*expr
= parent_expr
->subexpressions
[operand
];
1204 ir_rvalue
*val
= expr
->hir(instructions
, state
);
1206 if (val
->type
->is_boolean() && val
->type
->is_scalar())
1209 if (!*error_emitted
) {
1210 YYLTYPE loc
= expr
->get_location();
1211 _mesa_glsl_error(&loc
, state
, "%s of `%s' must be scalar boolean",
1213 parent_expr
->operator_string(parent_expr
->oper
));
1214 *error_emitted
= true;
1217 return new(ctx
) ir_constant(true);
1221 * If name refers to a builtin array whose maximum allowed size is less than
1222 * size, report an error and return true. Otherwise return false.
1225 check_builtin_array_max_size(const char *name
, unsigned size
,
1226 YYLTYPE loc
, struct _mesa_glsl_parse_state
*state
)
1228 if ((strcmp("gl_TexCoord", name
) == 0)
1229 && (size
> state
->Const
.MaxTextureCoords
)) {
1230 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1232 * "The size [of gl_TexCoord] can be at most
1233 * gl_MaxTextureCoords."
1235 _mesa_glsl_error(&loc
, state
, "`gl_TexCoord' array size cannot "
1236 "be larger than gl_MaxTextureCoords (%u)",
1237 state
->Const
.MaxTextureCoords
);
1238 } else if (strcmp("gl_ClipDistance", name
) == 0) {
1239 state
->clip_dist_size
= size
;
1240 if (size
+ state
->cull_dist_size
> state
->Const
.MaxClipPlanes
) {
1241 /* From section 7.1 (Vertex Shader Special Variables) of the
1244 * "The gl_ClipDistance array is predeclared as unsized and
1245 * must be sized by the shader either redeclaring it with a
1246 * size or indexing it only with integral constant
1247 * expressions. ... The size can be at most
1248 * gl_MaxClipDistances."
1250 _mesa_glsl_error(&loc
, state
, "`gl_ClipDistance' array size cannot "
1251 "be larger than gl_MaxClipDistances (%u)",
1252 state
->Const
.MaxClipPlanes
);
1254 } else if (strcmp("gl_CullDistance", name
) == 0) {
1255 state
->cull_dist_size
= size
;
1256 if (size
+ state
->clip_dist_size
> state
->Const
.MaxClipPlanes
) {
1257 /* From the ARB_cull_distance spec:
1259 * "The gl_CullDistance array is predeclared as unsized and
1260 * must be sized by the shader either redeclaring it with
1261 * a size or indexing it only with integral constant
1262 * expressions. The size determines the number and set of
1263 * enabled cull distances and can be at most
1264 * gl_MaxCullDistances."
1266 _mesa_glsl_error(&loc
, state
, "`gl_CullDistance' array size cannot "
1267 "be larger than gl_MaxCullDistances (%u)",
1268 state
->Const
.MaxClipPlanes
);
1274 * Create the constant 1, of a which is appropriate for incrementing and
1275 * decrementing values of the given GLSL type. For example, if type is vec4,
1276 * this creates a constant value of 1.0 having type float.
1278 * If the given type is invalid for increment and decrement operators, return
1279 * a floating point 1--the error will be detected later.
1282 constant_one_for_inc_dec(void *ctx
, const glsl_type
*type
)
1284 switch (type
->base_type
) {
1285 case GLSL_TYPE_UINT
:
1286 return new(ctx
) ir_constant((unsigned) 1);
1288 return new(ctx
) ir_constant(1);
1289 case GLSL_TYPE_UINT64
:
1290 return new(ctx
) ir_constant((uint64_t) 1);
1291 case GLSL_TYPE_INT64
:
1292 return new(ctx
) ir_constant((int64_t) 1);
1294 case GLSL_TYPE_FLOAT
:
1295 return new(ctx
) ir_constant(1.0f
);
1300 ast_expression::hir(exec_list
*instructions
,
1301 struct _mesa_glsl_parse_state
*state
)
1303 return do_hir(instructions
, state
, true);
1307 ast_expression::hir_no_rvalue(exec_list
*instructions
,
1308 struct _mesa_glsl_parse_state
*state
)
1310 do_hir(instructions
, state
, false);
1314 ast_expression::set_is_lhs(bool new_value
)
1316 /* is_lhs is tracked only to print "variable used uninitialized" warnings,
1317 * if we lack an identifier we can just skip it.
1319 if (this->primary_expression
.identifier
== NULL
)
1322 this->is_lhs
= new_value
;
1324 /* We need to go through the subexpressions tree to cover cases like
1325 * ast_field_selection
1327 if (this->subexpressions
[0] != NULL
)
1328 this->subexpressions
[0]->set_is_lhs(new_value
);
1332 ast_expression::do_hir(exec_list
*instructions
,
1333 struct _mesa_glsl_parse_state
*state
,
1337 static const int operations
[AST_NUM_OPERATORS
] = {
1338 -1, /* ast_assign doesn't convert to ir_expression. */
1339 -1, /* ast_plus doesn't convert to ir_expression. */
1349 ir_binop_less
, /* This is correct. See the ast_greater case below. */
1350 ir_binop_gequal
, /* This is correct. See the ast_lequal case below. */
1353 ir_binop_any_nequal
,
1363 /* Note: The following block of expression types actually convert
1364 * to multiple IR instructions.
1366 ir_binop_mul
, /* ast_mul_assign */
1367 ir_binop_div
, /* ast_div_assign */
1368 ir_binop_mod
, /* ast_mod_assign */
1369 ir_binop_add
, /* ast_add_assign */
1370 ir_binop_sub
, /* ast_sub_assign */
1371 ir_binop_lshift
, /* ast_ls_assign */
1372 ir_binop_rshift
, /* ast_rs_assign */
1373 ir_binop_bit_and
, /* ast_and_assign */
1374 ir_binop_bit_xor
, /* ast_xor_assign */
1375 ir_binop_bit_or
, /* ast_or_assign */
1377 -1, /* ast_conditional doesn't convert to ir_expression. */
1378 ir_binop_add
, /* ast_pre_inc. */
1379 ir_binop_sub
, /* ast_pre_dec. */
1380 ir_binop_add
, /* ast_post_inc. */
1381 ir_binop_sub
, /* ast_post_dec. */
1382 -1, /* ast_field_selection doesn't conv to ir_expression. */
1383 -1, /* ast_array_index doesn't convert to ir_expression. */
1384 -1, /* ast_function_call doesn't conv to ir_expression. */
1385 -1, /* ast_identifier doesn't convert to ir_expression. */
1386 -1, /* ast_int_constant doesn't convert to ir_expression. */
1387 -1, /* ast_uint_constant doesn't conv to ir_expression. */
1388 -1, /* ast_float_constant doesn't conv to ir_expression. */
1389 -1, /* ast_bool_constant doesn't conv to ir_expression. */
1390 -1, /* ast_sequence doesn't convert to ir_expression. */
1391 -1, /* ast_aggregate shouldn't ever even get here. */
1393 ir_rvalue
*result
= NULL
;
1395 const struct glsl_type
*type
, *orig_type
;
1396 bool error_emitted
= false;
1399 loc
= this->get_location();
1401 switch (this->oper
) {
1403 unreachable("ast_aggregate: Should never get here.");
1406 this->subexpressions
[0]->set_is_lhs(true);
1407 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1408 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1411 do_assignment(instructions
, state
,
1412 this->subexpressions
[0]->non_lvalue_description
,
1413 op
[0], op
[1], &result
, needs_rvalue
, false,
1414 this->subexpressions
[0]->get_location());
1419 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1421 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1423 error_emitted
= type
->is_error();
1429 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1431 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1433 error_emitted
= type
->is_error();
1435 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1443 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1444 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1446 type
= arithmetic_result_type(op
[0], op
[1],
1447 (this->oper
== ast_mul
),
1449 error_emitted
= type
->is_error();
1451 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1456 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1457 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1459 type
= modulus_result_type(op
[0], op
[1], state
, &loc
);
1461 assert(operations
[this->oper
] == ir_binop_mod
);
1463 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1465 error_emitted
= type
->is_error();
1470 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1471 error_emitted
= true;
1474 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1475 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1476 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1478 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1480 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1487 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1488 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1490 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
1492 /* The relational operators must either generate an error or result
1493 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1495 assert(type
->is_error()
1496 || (type
->is_boolean() && type
->is_scalar()));
1498 /* Like NIR, GLSL IR does not have opcodes for > or <=. Instead, swap
1499 * the arguments and use < or >=.
1501 if (this->oper
== ast_greater
|| this->oper
== ast_lequal
) {
1502 ir_rvalue
*const tmp
= op
[0];
1507 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1509 error_emitted
= type
->is_error();
1514 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1515 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1517 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1519 * "The equality operators equal (==), and not equal (!=)
1520 * operate on all types. They result in a scalar Boolean. If
1521 * the operand types do not match, then there must be a
1522 * conversion from Section 4.1.10 "Implicit Conversions"
1523 * applied to one operand that can make them match, in which
1524 * case this conversion is done."
1527 if (op
[0]->type
== glsl_type::void_type
|| op
[1]->type
== glsl_type::void_type
) {
1528 _mesa_glsl_error(& loc
, state
, "`%s': wrong operand types: "
1529 "no operation `%1$s' exists that takes a left-hand "
1530 "operand of type 'void' or a right operand of type "
1531 "'void'", (this->oper
== ast_equal
) ? "==" : "!=");
1532 error_emitted
= true;
1533 } else if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
1534 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
1535 || (op
[0]->type
!= op
[1]->type
)) {
1536 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
1537 "type", (this->oper
== ast_equal
) ? "==" : "!=");
1538 error_emitted
= true;
1539 } else if ((op
[0]->type
->is_array() || op
[1]->type
->is_array()) &&
1540 !state
->check_version(120, 300, &loc
,
1541 "array comparisons forbidden")) {
1542 error_emitted
= true;
1543 } else if ((op
[0]->type
->contains_subroutine() ||
1544 op
[1]->type
->contains_subroutine())) {
1545 _mesa_glsl_error(&loc
, state
, "subroutine comparisons forbidden");
1546 error_emitted
= true;
1547 } else if ((op
[0]->type
->contains_opaque() ||
1548 op
[1]->type
->contains_opaque())) {
1549 _mesa_glsl_error(&loc
, state
, "opaque type comparisons forbidden");
1550 error_emitted
= true;
1553 if (error_emitted
) {
1554 result
= new(ctx
) ir_constant(false);
1556 result
= do_comparison(ctx
, operations
[this->oper
], op
[0], op
[1]);
1557 assert(result
->type
== glsl_type::bool_type
);
1564 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1565 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1566 type
= bit_logic_result_type(op
[0], op
[1], this->oper
, state
, &loc
);
1567 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1569 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1573 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1575 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1576 error_emitted
= true;
1579 if (!op
[0]->type
->is_integer_32_64()) {
1580 _mesa_glsl_error(&loc
, state
, "operand of `~' must be an integer");
1581 error_emitted
= true;
1584 type
= error_emitted
? glsl_type::error_type
: op
[0]->type
;
1585 result
= new(ctx
) ir_expression(ir_unop_bit_not
, type
, op
[0], NULL
);
1588 case ast_logic_and
: {
1589 exec_list rhs_instructions
;
1590 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1591 "LHS", &error_emitted
);
1592 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1593 "RHS", &error_emitted
);
1595 if (rhs_instructions
.is_empty()) {
1596 result
= new(ctx
) ir_expression(ir_binop_logic_and
, op
[0], op
[1]);
1598 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1601 instructions
->push_tail(tmp
);
1603 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1604 instructions
->push_tail(stmt
);
1606 stmt
->then_instructions
.append_list(&rhs_instructions
);
1607 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1608 ir_assignment
*const then_assign
=
1609 new(ctx
) ir_assignment(then_deref
, op
[1]);
1610 stmt
->then_instructions
.push_tail(then_assign
);
1612 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1613 ir_assignment
*const else_assign
=
1614 new(ctx
) ir_assignment(else_deref
, new(ctx
) ir_constant(false));
1615 stmt
->else_instructions
.push_tail(else_assign
);
1617 result
= new(ctx
) ir_dereference_variable(tmp
);
1622 case ast_logic_or
: {
1623 exec_list rhs_instructions
;
1624 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1625 "LHS", &error_emitted
);
1626 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1627 "RHS", &error_emitted
);
1629 if (rhs_instructions
.is_empty()) {
1630 result
= new(ctx
) ir_expression(ir_binop_logic_or
, op
[0], op
[1]);
1632 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1635 instructions
->push_tail(tmp
);
1637 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1638 instructions
->push_tail(stmt
);
1640 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1641 ir_assignment
*const then_assign
=
1642 new(ctx
) ir_assignment(then_deref
, new(ctx
) ir_constant(true));
1643 stmt
->then_instructions
.push_tail(then_assign
);
1645 stmt
->else_instructions
.append_list(&rhs_instructions
);
1646 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1647 ir_assignment
*const else_assign
=
1648 new(ctx
) ir_assignment(else_deref
, op
[1]);
1649 stmt
->else_instructions
.push_tail(else_assign
);
1651 result
= new(ctx
) ir_dereference_variable(tmp
);
1657 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1659 * "The logical binary operators and (&&), or ( | | ), and
1660 * exclusive or (^^). They operate only on two Boolean
1661 * expressions and result in a Boolean expression."
1663 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0, "LHS",
1665 op
[1] = get_scalar_boolean_operand(instructions
, state
, this, 1, "RHS",
1668 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1673 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1674 "operand", &error_emitted
);
1676 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1680 case ast_mul_assign
:
1681 case ast_div_assign
:
1682 case ast_add_assign
:
1683 case ast_sub_assign
: {
1684 this->subexpressions
[0]->set_is_lhs(true);
1685 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1686 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1688 orig_type
= op
[0]->type
;
1690 /* Break out if operand types were not parsed successfully. */
1691 if ((op
[0]->type
== glsl_type::error_type
||
1692 op
[1]->type
== glsl_type::error_type
))
1695 type
= arithmetic_result_type(op
[0], op
[1],
1696 (this->oper
== ast_mul_assign
),
1699 if (type
!= orig_type
) {
1700 _mesa_glsl_error(& loc
, state
,
1701 "could not implicitly convert "
1702 "%s to %s", type
->name
, orig_type
->name
);
1703 type
= glsl_type::error_type
;
1706 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1710 do_assignment(instructions
, state
,
1711 this->subexpressions
[0]->non_lvalue_description
,
1712 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1713 &result
, needs_rvalue
, false,
1714 this->subexpressions
[0]->get_location());
1716 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1717 * explicitly test for this because none of the binary expression
1718 * operators allow array operands either.
1724 case ast_mod_assign
: {
1725 this->subexpressions
[0]->set_is_lhs(true);
1726 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1727 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1729 orig_type
= op
[0]->type
;
1730 type
= modulus_result_type(op
[0], op
[1], state
, &loc
);
1732 if (type
!= orig_type
) {
1733 _mesa_glsl_error(& loc
, state
,
1734 "could not implicitly convert "
1735 "%s to %s", type
->name
, orig_type
->name
);
1736 type
= glsl_type::error_type
;
1739 assert(operations
[this->oper
] == ir_binop_mod
);
1741 ir_rvalue
*temp_rhs
;
1742 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1746 do_assignment(instructions
, state
,
1747 this->subexpressions
[0]->non_lvalue_description
,
1748 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1749 &result
, needs_rvalue
, false,
1750 this->subexpressions
[0]->get_location());
1755 case ast_rs_assign
: {
1756 this->subexpressions
[0]->set_is_lhs(true);
1757 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1758 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1759 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1761 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1762 type
, op
[0], op
[1]);
1764 do_assignment(instructions
, state
,
1765 this->subexpressions
[0]->non_lvalue_description
,
1766 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1767 &result
, needs_rvalue
, false,
1768 this->subexpressions
[0]->get_location());
1772 case ast_and_assign
:
1773 case ast_xor_assign
:
1774 case ast_or_assign
: {
1775 this->subexpressions
[0]->set_is_lhs(true);
1776 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1777 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1779 orig_type
= op
[0]->type
;
1780 type
= bit_logic_result_type(op
[0], op
[1], this->oper
, state
, &loc
);
1782 if (type
!= orig_type
) {
1783 _mesa_glsl_error(& loc
, state
,
1784 "could not implicitly convert "
1785 "%s to %s", type
->name
, orig_type
->name
);
1786 type
= glsl_type::error_type
;
1789 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1790 type
, op
[0], op
[1]);
1792 do_assignment(instructions
, state
,
1793 this->subexpressions
[0]->non_lvalue_description
,
1794 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1795 &result
, needs_rvalue
, false,
1796 this->subexpressions
[0]->get_location());
1800 case ast_conditional
: {
1801 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1803 * "The ternary selection operator (?:). It operates on three
1804 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1805 * first expression, which must result in a scalar Boolean."
1807 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1808 "condition", &error_emitted
);
1810 /* The :? operator is implemented by generating an anonymous temporary
1811 * followed by an if-statement. The last instruction in each branch of
1812 * the if-statement assigns a value to the anonymous temporary. This
1813 * temporary is the r-value of the expression.
1815 exec_list then_instructions
;
1816 exec_list else_instructions
;
1818 op
[1] = this->subexpressions
[1]->hir(&then_instructions
, state
);
1819 op
[2] = this->subexpressions
[2]->hir(&else_instructions
, state
);
1821 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1823 * "The second and third expressions can be any type, as
1824 * long their types match, or there is a conversion in
1825 * Section 4.1.10 "Implicit Conversions" that can be applied
1826 * to one of the expressions to make their types match. This
1827 * resulting matching type is the type of the entire
1830 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1831 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1832 || (op
[1]->type
!= op
[2]->type
)) {
1833 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1835 _mesa_glsl_error(& loc
, state
, "second and third operands of ?: "
1836 "operator must have matching types");
1837 error_emitted
= true;
1838 type
= glsl_type::error_type
;
1843 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1845 * "The second and third expressions must be the same type, but can
1846 * be of any type other than an array."
1848 if (type
->is_array() &&
1849 !state
->check_version(120, 300, &loc
,
1850 "second and third operands of ?: operator "
1851 "cannot be arrays")) {
1852 error_emitted
= true;
1855 /* From section 4.1.7 of the GLSL 4.50 spec (Opaque Types):
1857 * "Except for array indexing, structure member selection, and
1858 * parentheses, opaque variables are not allowed to be operands in
1859 * expressions; such use results in a compile-time error."
1861 if (type
->contains_opaque()) {
1862 if (!(state
->has_bindless() && (type
->is_image() || type
->is_sampler()))) {
1863 _mesa_glsl_error(&loc
, state
, "variables of type %s cannot be "
1864 "operands of the ?: operator", type
->name
);
1865 error_emitted
= true;
1869 ir_constant
*cond_val
= op
[0]->constant_expression_value(ctx
);
1871 if (then_instructions
.is_empty()
1872 && else_instructions
.is_empty()
1873 && cond_val
!= NULL
) {
1874 result
= cond_val
->value
.b
[0] ? op
[1] : op
[2];
1876 /* The copy to conditional_tmp reads the whole array. */
1877 if (type
->is_array()) {
1878 mark_whole_array_access(op
[1]);
1879 mark_whole_array_access(op
[2]);
1882 ir_variable
*const tmp
=
1883 new(ctx
) ir_variable(type
, "conditional_tmp", ir_var_temporary
);
1884 instructions
->push_tail(tmp
);
1886 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1887 instructions
->push_tail(stmt
);
1889 then_instructions
.move_nodes_to(& stmt
->then_instructions
);
1890 ir_dereference
*const then_deref
=
1891 new(ctx
) ir_dereference_variable(tmp
);
1892 ir_assignment
*const then_assign
=
1893 new(ctx
) ir_assignment(then_deref
, op
[1]);
1894 stmt
->then_instructions
.push_tail(then_assign
);
1896 else_instructions
.move_nodes_to(& stmt
->else_instructions
);
1897 ir_dereference
*const else_deref
=
1898 new(ctx
) ir_dereference_variable(tmp
);
1899 ir_assignment
*const else_assign
=
1900 new(ctx
) ir_assignment(else_deref
, op
[2]);
1901 stmt
->else_instructions
.push_tail(else_assign
);
1903 result
= new(ctx
) ir_dereference_variable(tmp
);
1910 this->non_lvalue_description
= (this->oper
== ast_pre_inc
)
1911 ? "pre-increment operation" : "pre-decrement operation";
1913 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1914 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1916 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1918 ir_rvalue
*temp_rhs
;
1919 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1923 do_assignment(instructions
, state
,
1924 this->subexpressions
[0]->non_lvalue_description
,
1925 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1926 &result
, needs_rvalue
, false,
1927 this->subexpressions
[0]->get_location());
1932 case ast_post_dec
: {
1933 this->non_lvalue_description
= (this->oper
== ast_post_inc
)
1934 ? "post-increment operation" : "post-decrement operation";
1935 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1936 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1938 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1940 if (error_emitted
) {
1941 result
= ir_rvalue::error_value(ctx
);
1945 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1947 ir_rvalue
*temp_rhs
;
1948 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1951 /* Get a temporary of a copy of the lvalue before it's modified.
1952 * This may get thrown away later.
1954 result
= get_lvalue_copy(instructions
, op
[0]->clone(ctx
, NULL
));
1956 ir_rvalue
*junk_rvalue
;
1958 do_assignment(instructions
, state
,
1959 this->subexpressions
[0]->non_lvalue_description
,
1960 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1961 &junk_rvalue
, false, false,
1962 this->subexpressions
[0]->get_location());
1967 case ast_field_selection
:
1968 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1971 case ast_array_index
: {
1972 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1974 /* Getting if an array is being used uninitialized is beyond what we get
1975 * from ir_value.data.assigned. Setting is_lhs as true would force to
1976 * not raise a uninitialized warning when using an array
1978 subexpressions
[0]->set_is_lhs(true);
1979 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1980 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1982 result
= _mesa_ast_array_index_to_hir(ctx
, state
, op
[0], op
[1],
1985 if (result
->type
->is_error())
1986 error_emitted
= true;
1991 case ast_unsized_array_dim
:
1992 unreachable("ast_unsized_array_dim: Should never get here.");
1994 case ast_function_call
:
1995 /* Should *NEVER* get here. ast_function_call should always be handled
1996 * by ast_function_expression::hir.
1998 unreachable("ast_function_call: handled elsewhere ");
2000 case ast_identifier
: {
2001 /* ast_identifier can appear several places in a full abstract syntax
2002 * tree. This particular use must be at location specified in the grammar
2003 * as 'variable_identifier'.
2006 state
->symbols
->get_variable(this->primary_expression
.identifier
);
2009 /* the identifier might be a subroutine name */
2011 sub_name
= ralloc_asprintf(ctx
, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state
->stage
), this->primary_expression
.identifier
);
2012 var
= state
->symbols
->get_variable(sub_name
);
2013 ralloc_free(sub_name
);
2017 var
->data
.used
= true;
2018 result
= new(ctx
) ir_dereference_variable(var
);
2020 if ((var
->data
.mode
== ir_var_auto
|| var
->data
.mode
== ir_var_shader_out
)
2022 && result
->variable_referenced()->data
.assigned
!= true
2023 && !is_gl_identifier(var
->name
)) {
2024 _mesa_glsl_warning(&loc
, state
, "`%s' used uninitialized",
2025 this->primary_expression
.identifier
);
2028 /* From the EXT_shader_framebuffer_fetch spec:
2030 * "Unless the GL_EXT_shader_framebuffer_fetch extension has been
2031 * enabled in addition, it's an error to use gl_LastFragData if it
2032 * hasn't been explicitly redeclared with layout(noncoherent)."
2034 if (var
->data
.fb_fetch_output
&& var
->data
.memory_coherent
&&
2035 !state
->EXT_shader_framebuffer_fetch_enable
) {
2036 _mesa_glsl_error(&loc
, state
,
2037 "invalid use of framebuffer fetch output not "
2038 "qualified with layout(noncoherent)");
2042 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
2043 this->primary_expression
.identifier
);
2045 result
= ir_rvalue::error_value(ctx
);
2046 error_emitted
= true;
2051 case ast_int_constant
:
2052 result
= new(ctx
) ir_constant(this->primary_expression
.int_constant
);
2055 case ast_uint_constant
:
2056 result
= new(ctx
) ir_constant(this->primary_expression
.uint_constant
);
2059 case ast_float_constant
:
2060 result
= new(ctx
) ir_constant(this->primary_expression
.float_constant
);
2063 case ast_bool_constant
:
2064 result
= new(ctx
) ir_constant(bool(this->primary_expression
.bool_constant
));
2067 case ast_double_constant
:
2068 result
= new(ctx
) ir_constant(this->primary_expression
.double_constant
);
2071 case ast_uint64_constant
:
2072 result
= new(ctx
) ir_constant(this->primary_expression
.uint64_constant
);
2075 case ast_int64_constant
:
2076 result
= new(ctx
) ir_constant(this->primary_expression
.int64_constant
);
2079 case ast_sequence
: {
2080 /* It should not be possible to generate a sequence in the AST without
2081 * any expressions in it.
2083 assert(!this->expressions
.is_empty());
2085 /* The r-value of a sequence is the last expression in the sequence. If
2086 * the other expressions in the sequence do not have side-effects (and
2087 * therefore add instructions to the instruction list), they get dropped
2090 exec_node
*previous_tail
= NULL
;
2091 YYLTYPE previous_operand_loc
= loc
;
2093 foreach_list_typed (ast_node
, ast
, link
, &this->expressions
) {
2094 /* If one of the operands of comma operator does not generate any
2095 * code, we want to emit a warning. At each pass through the loop
2096 * previous_tail will point to the last instruction in the stream
2097 * *before* processing the previous operand. Naturally,
2098 * instructions->get_tail_raw() will point to the last instruction in
2099 * the stream *after* processing the previous operand. If the two
2100 * pointers match, then the previous operand had no effect.
2102 * The warning behavior here differs slightly from GCC. GCC will
2103 * only emit a warning if none of the left-hand operands have an
2104 * effect. However, it will emit a warning for each. I believe that
2105 * there are some cases in C (especially with GCC extensions) where
2106 * it is useful to have an intermediate step in a sequence have no
2107 * effect, but I don't think these cases exist in GLSL. Either way,
2108 * it would be a giant hassle to replicate that behavior.
2110 if (previous_tail
== instructions
->get_tail_raw()) {
2111 _mesa_glsl_warning(&previous_operand_loc
, state
,
2112 "left-hand operand of comma expression has "
2116 /* The tail is directly accessed instead of using the get_tail()
2117 * method for performance reasons. get_tail() has extra code to
2118 * return NULL when the list is empty. We don't care about that
2119 * here, so using get_tail_raw() is fine.
2121 previous_tail
= instructions
->get_tail_raw();
2122 previous_operand_loc
= ast
->get_location();
2124 result
= ast
->hir(instructions
, state
);
2127 /* Any errors should have already been emitted in the loop above.
2129 error_emitted
= true;
2133 type
= NULL
; /* use result->type, not type. */
2134 assert(result
!= NULL
|| !needs_rvalue
);
2136 if (result
&& result
->type
->is_error() && !error_emitted
)
2137 _mesa_glsl_error(& loc
, state
, "type mismatch");
2143 ast_expression::has_sequence_subexpression() const
2145 switch (this->oper
) {
2154 return this->subexpressions
[0]->has_sequence_subexpression();
2176 case ast_array_index
:
2177 case ast_mul_assign
:
2178 case ast_div_assign
:
2179 case ast_add_assign
:
2180 case ast_sub_assign
:
2181 case ast_mod_assign
:
2184 case ast_and_assign
:
2185 case ast_xor_assign
:
2187 return this->subexpressions
[0]->has_sequence_subexpression() ||
2188 this->subexpressions
[1]->has_sequence_subexpression();
2190 case ast_conditional
:
2191 return this->subexpressions
[0]->has_sequence_subexpression() ||
2192 this->subexpressions
[1]->has_sequence_subexpression() ||
2193 this->subexpressions
[2]->has_sequence_subexpression();
2198 case ast_field_selection
:
2199 case ast_identifier
:
2200 case ast_int_constant
:
2201 case ast_uint_constant
:
2202 case ast_float_constant
:
2203 case ast_bool_constant
:
2204 case ast_double_constant
:
2205 case ast_int64_constant
:
2206 case ast_uint64_constant
:
2212 case ast_function_call
:
2213 unreachable("should be handled by ast_function_expression::hir");
2215 case ast_unsized_array_dim
:
2216 unreachable("ast_unsized_array_dim: Should never get here.");
2223 ast_expression_statement::hir(exec_list
*instructions
,
2224 struct _mesa_glsl_parse_state
*state
)
2226 /* It is possible to have expression statements that don't have an
2227 * expression. This is the solitary semicolon:
2229 * for (i = 0; i < 5; i++)
2232 * In this case the expression will be NULL. Test for NULL and don't do
2233 * anything in that case.
2235 if (expression
!= NULL
)
2236 expression
->hir_no_rvalue(instructions
, state
);
2238 /* Statements do not have r-values.
2245 ast_compound_statement::hir(exec_list
*instructions
,
2246 struct _mesa_glsl_parse_state
*state
)
2249 state
->symbols
->push_scope();
2251 foreach_list_typed (ast_node
, ast
, link
, &this->statements
)
2252 ast
->hir(instructions
, state
);
2255 state
->symbols
->pop_scope();
2257 /* Compound statements do not have r-values.
2263 * Evaluate the given exec_node (which should be an ast_node representing
2264 * a single array dimension) and return its integer value.
2267 process_array_size(exec_node
*node
,
2268 struct _mesa_glsl_parse_state
*state
)
2270 void *mem_ctx
= state
;
2272 exec_list dummy_instructions
;
2274 ast_node
*array_size
= exec_node_data(ast_node
, node
, link
);
2277 * Dimensions other than the outermost dimension can by unsized if they
2278 * are immediately sized by a constructor or initializer.
2280 if (((ast_expression
*)array_size
)->oper
== ast_unsized_array_dim
)
2283 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
2284 YYLTYPE loc
= array_size
->get_location();
2287 _mesa_glsl_error(& loc
, state
,
2288 "array size could not be resolved");
2292 if (!ir
->type
->is_integer_32()) {
2293 _mesa_glsl_error(& loc
, state
,
2294 "array size must be integer type");
2298 if (!ir
->type
->is_scalar()) {
2299 _mesa_glsl_error(& loc
, state
,
2300 "array size must be scalar type");
2304 ir_constant
*const size
= ir
->constant_expression_value(mem_ctx
);
2306 (state
->is_version(120, 300) &&
2307 array_size
->has_sequence_subexpression())) {
2308 _mesa_glsl_error(& loc
, state
, "array size must be a "
2309 "constant valued expression");
2313 if (size
->value
.i
[0] <= 0) {
2314 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
2318 assert(size
->type
== ir
->type
);
2320 /* If the array size is const (and we've verified that
2321 * it is) then no instructions should have been emitted
2322 * when we converted it to HIR. If they were emitted,
2323 * then either the array size isn't const after all, or
2324 * we are emitting unnecessary instructions.
2326 assert(dummy_instructions
.is_empty());
2328 return size
->value
.u
[0];
2331 static const glsl_type
*
2332 process_array_type(YYLTYPE
*loc
, const glsl_type
*base
,
2333 ast_array_specifier
*array_specifier
,
2334 struct _mesa_glsl_parse_state
*state
)
2336 const glsl_type
*array_type
= base
;
2338 if (array_specifier
!= NULL
) {
2339 if (base
->is_array()) {
2341 /* From page 19 (page 25) of the GLSL 1.20 spec:
2343 * "Only one-dimensional arrays may be declared."
2345 if (!state
->check_arrays_of_arrays_allowed(loc
)) {
2346 return glsl_type::error_type
;
2350 for (exec_node
*node
= array_specifier
->array_dimensions
.get_tail_raw();
2351 !node
->is_head_sentinel(); node
= node
->prev
) {
2352 unsigned array_size
= process_array_size(node
, state
);
2353 array_type
= glsl_type::get_array_instance(array_type
, array_size
);
2361 precision_qualifier_allowed(const glsl_type
*type
)
2363 /* Precision qualifiers apply to floating point, integer and opaque
2366 * Section 4.5.2 (Precision Qualifiers) of the GLSL 1.30 spec says:
2367 * "Any floating point or any integer declaration can have the type
2368 * preceded by one of these precision qualifiers [...] Literal
2369 * constants do not have precision qualifiers. Neither do Boolean
2372 * Section 4.5 (Precision and Precision Qualifiers) of the GLSL 1.30
2375 * "Precision qualifiers are added for code portability with OpenGL
2376 * ES, not for functionality. They have the same syntax as in OpenGL
2379 * Section 8 (Built-In Functions) of the GLSL ES 1.00 spec says:
2381 * "uniform lowp sampler2D sampler;
2384 * lowp vec4 col = texture2D (sampler, coord);
2385 * // texture2D returns lowp"
2387 * From this, we infer that GLSL 1.30 (and later) should allow precision
2388 * qualifiers on sampler types just like float and integer types.
2390 const glsl_type
*const t
= type
->without_array();
2392 return (t
->is_float() || t
->is_integer_32() || t
->contains_opaque()) &&
2397 ast_type_specifier::glsl_type(const char **name
,
2398 struct _mesa_glsl_parse_state
*state
) const
2400 const struct glsl_type
*type
;
2402 if (this->type
!= NULL
)
2405 type
= structure
->type
;
2407 type
= state
->symbols
->get_type(this->type_name
);
2408 *name
= this->type_name
;
2410 YYLTYPE loc
= this->get_location();
2411 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
2417 * From the OpenGL ES 3.0 spec, 4.5.4 Default Precision Qualifiers:
2419 * "The precision statement
2421 * precision precision-qualifier type;
2423 * can be used to establish a default precision qualifier. The type field can
2424 * be either int or float or any of the sampler types, (...) If type is float,
2425 * the directive applies to non-precision-qualified floating point type
2426 * (scalar, vector, and matrix) declarations. If type is int, the directive
2427 * applies to all non-precision-qualified integer type (scalar, vector, signed,
2428 * and unsigned) declarations."
2430 * We use the symbol table to keep the values of the default precisions for
2431 * each 'type' in each scope and we use the 'type' string from the precision
2432 * statement as key in the symbol table. When we want to retrieve the default
2433 * precision associated with a given glsl_type we need to know the type string
2434 * associated with it. This is what this function returns.
2437 get_type_name_for_precision_qualifier(const glsl_type
*type
)
2439 switch (type
->base_type
) {
2440 case GLSL_TYPE_FLOAT
:
2442 case GLSL_TYPE_UINT
:
2445 case GLSL_TYPE_ATOMIC_UINT
:
2446 return "atomic_uint";
2447 case GLSL_TYPE_IMAGE
:
2449 case GLSL_TYPE_SAMPLER
: {
2450 const unsigned type_idx
=
2451 type
->sampler_array
+ 2 * type
->sampler_shadow
;
2452 const unsigned offset
= type
->is_sampler() ? 0 : 4;
2453 assert(type_idx
< 4);
2454 switch (type
->sampled_type
) {
2455 case GLSL_TYPE_FLOAT
:
2456 switch (type
->sampler_dimensionality
) {
2457 case GLSL_SAMPLER_DIM_1D
: {
2458 assert(type
->is_sampler());
2459 static const char *const names
[4] = {
2460 "sampler1D", "sampler1DArray",
2461 "sampler1DShadow", "sampler1DArrayShadow"
2463 return names
[type_idx
];
2465 case GLSL_SAMPLER_DIM_2D
: {
2466 static const char *const names
[8] = {
2467 "sampler2D", "sampler2DArray",
2468 "sampler2DShadow", "sampler2DArrayShadow",
2469 "image2D", "image2DArray", NULL
, NULL
2471 return names
[offset
+ type_idx
];
2473 case GLSL_SAMPLER_DIM_3D
: {
2474 static const char *const names
[8] = {
2475 "sampler3D", NULL
, NULL
, NULL
,
2476 "image3D", NULL
, NULL
, NULL
2478 return names
[offset
+ type_idx
];
2480 case GLSL_SAMPLER_DIM_CUBE
: {
2481 static const char *const names
[8] = {
2482 "samplerCube", "samplerCubeArray",
2483 "samplerCubeShadow", "samplerCubeArrayShadow",
2484 "imageCube", NULL
, NULL
, NULL
2486 return names
[offset
+ type_idx
];
2488 case GLSL_SAMPLER_DIM_MS
: {
2489 assert(type
->is_sampler());
2490 static const char *const names
[4] = {
2491 "sampler2DMS", "sampler2DMSArray", NULL
, NULL
2493 return names
[type_idx
];
2495 case GLSL_SAMPLER_DIM_RECT
: {
2496 assert(type
->is_sampler());
2497 static const char *const names
[4] = {
2498 "samplerRect", NULL
, "samplerRectShadow", NULL
2500 return names
[type_idx
];
2502 case GLSL_SAMPLER_DIM_BUF
: {
2503 static const char *const names
[8] = {
2504 "samplerBuffer", NULL
, NULL
, NULL
,
2505 "imageBuffer", NULL
, NULL
, NULL
2507 return names
[offset
+ type_idx
];
2509 case GLSL_SAMPLER_DIM_EXTERNAL
: {
2510 assert(type
->is_sampler());
2511 static const char *const names
[4] = {
2512 "samplerExternalOES", NULL
, NULL
, NULL
2514 return names
[type_idx
];
2517 unreachable("Unsupported sampler/image dimensionality");
2518 } /* sampler/image float dimensionality */
2521 switch (type
->sampler_dimensionality
) {
2522 case GLSL_SAMPLER_DIM_1D
: {
2523 assert(type
->is_sampler());
2524 static const char *const names
[4] = {
2525 "isampler1D", "isampler1DArray", NULL
, NULL
2527 return names
[type_idx
];
2529 case GLSL_SAMPLER_DIM_2D
: {
2530 static const char *const names
[8] = {
2531 "isampler2D", "isampler2DArray", NULL
, NULL
,
2532 "iimage2D", "iimage2DArray", NULL
, NULL
2534 return names
[offset
+ type_idx
];
2536 case GLSL_SAMPLER_DIM_3D
: {
2537 static const char *const names
[8] = {
2538 "isampler3D", NULL
, NULL
, NULL
,
2539 "iimage3D", NULL
, NULL
, NULL
2541 return names
[offset
+ type_idx
];
2543 case GLSL_SAMPLER_DIM_CUBE
: {
2544 static const char *const names
[8] = {
2545 "isamplerCube", "isamplerCubeArray", NULL
, NULL
,
2546 "iimageCube", NULL
, NULL
, NULL
2548 return names
[offset
+ type_idx
];
2550 case GLSL_SAMPLER_DIM_MS
: {
2551 assert(type
->is_sampler());
2552 static const char *const names
[4] = {
2553 "isampler2DMS", "isampler2DMSArray", NULL
, NULL
2555 return names
[type_idx
];
2557 case GLSL_SAMPLER_DIM_RECT
: {
2558 assert(type
->is_sampler());
2559 static const char *const names
[4] = {
2560 "isamplerRect", NULL
, "isamplerRectShadow", NULL
2562 return names
[type_idx
];
2564 case GLSL_SAMPLER_DIM_BUF
: {
2565 static const char *const names
[8] = {
2566 "isamplerBuffer", NULL
, NULL
, NULL
,
2567 "iimageBuffer", NULL
, NULL
, NULL
2569 return names
[offset
+ type_idx
];
2572 unreachable("Unsupported isampler/iimage dimensionality");
2573 } /* sampler/image int dimensionality */
2575 case GLSL_TYPE_UINT
:
2576 switch (type
->sampler_dimensionality
) {
2577 case GLSL_SAMPLER_DIM_1D
: {
2578 assert(type
->is_sampler());
2579 static const char *const names
[4] = {
2580 "usampler1D", "usampler1DArray", NULL
, NULL
2582 return names
[type_idx
];
2584 case GLSL_SAMPLER_DIM_2D
: {
2585 static const char *const names
[8] = {
2586 "usampler2D", "usampler2DArray", NULL
, NULL
,
2587 "uimage2D", "uimage2DArray", NULL
, NULL
2589 return names
[offset
+ type_idx
];
2591 case GLSL_SAMPLER_DIM_3D
: {
2592 static const char *const names
[8] = {
2593 "usampler3D", NULL
, NULL
, NULL
,
2594 "uimage3D", NULL
, NULL
, NULL
2596 return names
[offset
+ type_idx
];
2598 case GLSL_SAMPLER_DIM_CUBE
: {
2599 static const char *const names
[8] = {
2600 "usamplerCube", "usamplerCubeArray", NULL
, NULL
,
2601 "uimageCube", NULL
, NULL
, NULL
2603 return names
[offset
+ type_idx
];
2605 case GLSL_SAMPLER_DIM_MS
: {
2606 assert(type
->is_sampler());
2607 static const char *const names
[4] = {
2608 "usampler2DMS", "usampler2DMSArray", NULL
, NULL
2610 return names
[type_idx
];
2612 case GLSL_SAMPLER_DIM_RECT
: {
2613 assert(type
->is_sampler());
2614 static const char *const names
[4] = {
2615 "usamplerRect", NULL
, "usamplerRectShadow", NULL
2617 return names
[type_idx
];
2619 case GLSL_SAMPLER_DIM_BUF
: {
2620 static const char *const names
[8] = {
2621 "usamplerBuffer", NULL
, NULL
, NULL
,
2622 "uimageBuffer", NULL
, NULL
, NULL
2624 return names
[offset
+ type_idx
];
2627 unreachable("Unsupported usampler/uimage dimensionality");
2628 } /* sampler/image uint dimensionality */
2631 unreachable("Unsupported sampler/image type");
2632 } /* sampler/image type */
2634 } /* GLSL_TYPE_SAMPLER/GLSL_TYPE_IMAGE */
2637 unreachable("Unsupported type");
2642 select_gles_precision(unsigned qual_precision
,
2643 const glsl_type
*type
,
2644 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
2646 /* Precision qualifiers do not have any meaning in Desktop GLSL.
2647 * In GLES we take the precision from the type qualifier if present,
2648 * otherwise, if the type of the variable allows precision qualifiers at
2649 * all, we look for the default precision qualifier for that type in the
2652 assert(state
->es_shader
);
2654 unsigned precision
= GLSL_PRECISION_NONE
;
2655 if (qual_precision
) {
2656 precision
= qual_precision
;
2657 } else if (precision_qualifier_allowed(type
)) {
2658 const char *type_name
=
2659 get_type_name_for_precision_qualifier(type
->without_array());
2660 assert(type_name
!= NULL
);
2663 state
->symbols
->get_default_precision_qualifier(type_name
);
2664 if (precision
== ast_precision_none
) {
2665 _mesa_glsl_error(loc
, state
,
2666 "No precision specified in this scope for type `%s'",
2672 /* Section 4.1.7.3 (Atomic Counters) of the GLSL ES 3.10 spec says:
2674 * "The default precision of all atomic types is highp. It is an error to
2675 * declare an atomic type with a different precision or to specify the
2676 * default precision for an atomic type to be lowp or mediump."
2678 if (type
->is_atomic_uint() && precision
!= ast_precision_high
) {
2679 _mesa_glsl_error(loc
, state
,
2680 "atomic_uint can only have highp precision qualifier");
2687 ast_fully_specified_type::glsl_type(const char **name
,
2688 struct _mesa_glsl_parse_state
*state
) const
2690 return this->specifier
->glsl_type(name
, state
);
2694 * Determine whether a toplevel variable declaration declares a varying. This
2695 * function operates by examining the variable's mode and the shader target,
2696 * so it correctly identifies linkage variables regardless of whether they are
2697 * declared using the deprecated "varying" syntax or the new "in/out" syntax.
2699 * Passing a non-toplevel variable declaration (e.g. a function parameter) to
2700 * this function will produce undefined results.
2703 is_varying_var(ir_variable
*var
, gl_shader_stage target
)
2706 case MESA_SHADER_VERTEX
:
2707 return var
->data
.mode
== ir_var_shader_out
;
2708 case MESA_SHADER_FRAGMENT
:
2709 return var
->data
.mode
== ir_var_shader_in
||
2710 (var
->data
.mode
== ir_var_system_value
&&
2711 var
->data
.location
== SYSTEM_VALUE_FRAG_COORD
);
2713 return var
->data
.mode
== ir_var_shader_out
|| var
->data
.mode
== ir_var_shader_in
;
2718 is_allowed_invariant(ir_variable
*var
, struct _mesa_glsl_parse_state
*state
)
2720 if (is_varying_var(var
, state
->stage
))
2723 /* From Section 4.6.1 ("The Invariant Qualifier") GLSL 1.20 spec:
2724 * "Only variables output from a vertex shader can be candidates
2727 if (!state
->is_version(130, 100))
2731 * Later specs remove this language - so allowed invariant
2732 * on fragment shader outputs as well.
2734 if (state
->stage
== MESA_SHADER_FRAGMENT
&&
2735 var
->data
.mode
== ir_var_shader_out
)
2741 * Matrix layout qualifiers are only allowed on certain types
2744 validate_matrix_layout_for_type(struct _mesa_glsl_parse_state
*state
,
2746 const glsl_type
*type
,
2749 if (var
&& !var
->is_in_buffer_block()) {
2750 /* Layout qualifiers may only apply to interface blocks and fields in
2753 _mesa_glsl_error(loc
, state
,
2754 "uniform block layout qualifiers row_major and "
2755 "column_major may not be applied to variables "
2756 "outside of uniform blocks");
2757 } else if (!type
->without_array()->is_matrix()) {
2758 /* The OpenGL ES 3.0 conformance tests did not originally allow
2759 * matrix layout qualifiers on non-matrices. However, the OpenGL
2760 * 4.4 and OpenGL ES 3.0 (revision TBD) specifications were
2761 * amended to specifically allow these layouts on all types. Emit
2762 * a warning so that people know their code may not be portable.
2764 _mesa_glsl_warning(loc
, state
,
2765 "uniform block layout qualifiers row_major and "
2766 "column_major applied to non-matrix types may "
2767 "be rejected by older compilers");
2772 validate_xfb_buffer_qualifier(YYLTYPE
*loc
,
2773 struct _mesa_glsl_parse_state
*state
,
2774 unsigned xfb_buffer
) {
2775 if (xfb_buffer
>= state
->Const
.MaxTransformFeedbackBuffers
) {
2776 _mesa_glsl_error(loc
, state
,
2777 "invalid xfb_buffer specified %d is larger than "
2778 "MAX_TRANSFORM_FEEDBACK_BUFFERS - 1 (%d).",
2780 state
->Const
.MaxTransformFeedbackBuffers
- 1);
2787 /* From the ARB_enhanced_layouts spec:
2789 * "Variables and block members qualified with *xfb_offset* can be
2790 * scalars, vectors, matrices, structures, and (sized) arrays of these.
2791 * The offset must be a multiple of the size of the first component of
2792 * the first qualified variable or block member, or a compile-time error
2793 * results. Further, if applied to an aggregate containing a double,
2794 * the offset must also be a multiple of 8, and the space taken in the
2795 * buffer will be a multiple of 8.
2798 validate_xfb_offset_qualifier(YYLTYPE
*loc
,
2799 struct _mesa_glsl_parse_state
*state
,
2800 int xfb_offset
, const glsl_type
*type
,
2801 unsigned component_size
) {
2802 const glsl_type
*t_without_array
= type
->without_array();
2804 if (xfb_offset
!= -1 && type
->is_unsized_array()) {
2805 _mesa_glsl_error(loc
, state
,
2806 "xfb_offset can't be used with unsized arrays.");
2810 /* Make sure nested structs don't contain unsized arrays, and validate
2811 * any xfb_offsets on interface members.
2813 if (t_without_array
->is_struct() || t_without_array
->is_interface())
2814 for (unsigned int i
= 0; i
< t_without_array
->length
; i
++) {
2815 const glsl_type
*member_t
= t_without_array
->fields
.structure
[i
].type
;
2817 /* When the interface block doesn't have an xfb_offset qualifier then
2818 * we apply the component size rules at the member level.
2820 if (xfb_offset
== -1)
2821 component_size
= member_t
->contains_double() ? 8 : 4;
2823 int xfb_offset
= t_without_array
->fields
.structure
[i
].offset
;
2824 validate_xfb_offset_qualifier(loc
, state
, xfb_offset
, member_t
,
2828 /* Nested structs or interface block without offset may not have had an
2829 * offset applied yet so return.
2831 if (xfb_offset
== -1) {
2835 if (xfb_offset
% component_size
) {
2836 _mesa_glsl_error(loc
, state
,
2837 "invalid qualifier xfb_offset=%d must be a multiple "
2838 "of the first component size of the first qualified "
2839 "variable or block member. Or double if an aggregate "
2840 "that contains a double (%d).",
2841 xfb_offset
, component_size
);
2849 validate_stream_qualifier(YYLTYPE
*loc
, struct _mesa_glsl_parse_state
*state
,
2852 if (stream
>= state
->ctx
->Const
.MaxVertexStreams
) {
2853 _mesa_glsl_error(loc
, state
,
2854 "invalid stream specified %d is larger than "
2855 "MAX_VERTEX_STREAMS - 1 (%d).",
2856 stream
, state
->ctx
->Const
.MaxVertexStreams
- 1);
2864 apply_explicit_binding(struct _mesa_glsl_parse_state
*state
,
2867 const glsl_type
*type
,
2868 const ast_type_qualifier
*qual
)
2870 if (!qual
->flags
.q
.uniform
&& !qual
->flags
.q
.buffer
) {
2871 _mesa_glsl_error(loc
, state
,
2872 "the \"binding\" qualifier only applies to uniforms and "
2873 "shader storage buffer objects");
2877 unsigned qual_binding
;
2878 if (!process_qualifier_constant(state
, loc
, "binding", qual
->binding
,
2883 const struct gl_context
*const ctx
= state
->ctx
;
2884 unsigned elements
= type
->is_array() ? type
->arrays_of_arrays_size() : 1;
2885 unsigned max_index
= qual_binding
+ elements
- 1;
2886 const glsl_type
*base_type
= type
->without_array();
2888 if (base_type
->is_interface()) {
2889 /* UBOs. From page 60 of the GLSL 4.20 specification:
2890 * "If the binding point for any uniform block instance is less than zero,
2891 * or greater than or equal to the implementation-dependent maximum
2892 * number of uniform buffer bindings, a compilation error will occur.
2893 * When the binding identifier is used with a uniform block instanced as
2894 * an array of size N, all elements of the array from binding through
2895 * binding + N – 1 must be within this range."
2897 * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
2899 if (qual
->flags
.q
.uniform
&&
2900 max_index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2901 _mesa_glsl_error(loc
, state
, "layout(binding = %u) for %d UBOs exceeds "
2902 "the maximum number of UBO binding points (%d)",
2903 qual_binding
, elements
,
2904 ctx
->Const
.MaxUniformBufferBindings
);
2908 /* SSBOs. From page 67 of the GLSL 4.30 specification:
2909 * "If the binding point for any uniform or shader storage block instance
2910 * is less than zero, or greater than or equal to the
2911 * implementation-dependent maximum number of uniform buffer bindings, a
2912 * compile-time error will occur. When the binding identifier is used
2913 * with a uniform or shader storage block instanced as an array of size
2914 * N, all elements of the array from binding through binding + N – 1 must
2915 * be within this range."
2917 if (qual
->flags
.q
.buffer
&&
2918 max_index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2919 _mesa_glsl_error(loc
, state
, "layout(binding = %u) for %d SSBOs exceeds "
2920 "the maximum number of SSBO binding points (%d)",
2921 qual_binding
, elements
,
2922 ctx
->Const
.MaxShaderStorageBufferBindings
);
2925 } else if (base_type
->is_sampler()) {
2926 /* Samplers. From page 63 of the GLSL 4.20 specification:
2927 * "If the binding is less than zero, or greater than or equal to the
2928 * implementation-dependent maximum supported number of units, a
2929 * compilation error will occur. When the binding identifier is used
2930 * with an array of size N, all elements of the array from binding
2931 * through binding + N - 1 must be within this range."
2933 unsigned limit
= ctx
->Const
.MaxCombinedTextureImageUnits
;
2935 if (max_index
>= limit
) {
2936 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d samplers "
2937 "exceeds the maximum number of texture image units "
2938 "(%u)", qual_binding
, elements
, limit
);
2942 } else if (base_type
->contains_atomic()) {
2943 assert(ctx
->Const
.MaxAtomicBufferBindings
<= MAX_COMBINED_ATOMIC_BUFFERS
);
2944 if (qual_binding
>= ctx
->Const
.MaxAtomicBufferBindings
) {
2945 _mesa_glsl_error(loc
, state
, "layout(binding = %d) exceeds the "
2946 "maximum number of atomic counter buffer bindings "
2947 "(%u)", qual_binding
,
2948 ctx
->Const
.MaxAtomicBufferBindings
);
2952 } else if ((state
->is_version(420, 310) ||
2953 state
->ARB_shading_language_420pack_enable
) &&
2954 base_type
->is_image()) {
2955 assert(ctx
->Const
.MaxImageUnits
<= MAX_IMAGE_UNITS
);
2956 if (max_index
>= ctx
->Const
.MaxImageUnits
) {
2957 _mesa_glsl_error(loc
, state
, "Image binding %d exceeds the "
2958 "maximum number of image units (%d)", max_index
,
2959 ctx
->Const
.MaxImageUnits
);
2964 _mesa_glsl_error(loc
, state
,
2965 "the \"binding\" qualifier only applies to uniform "
2966 "blocks, storage blocks, opaque variables, or arrays "
2971 var
->data
.explicit_binding
= true;
2972 var
->data
.binding
= qual_binding
;
2978 validate_fragment_flat_interpolation_input(struct _mesa_glsl_parse_state
*state
,
2980 const glsl_interp_mode interpolation
,
2981 const struct glsl_type
*var_type
,
2982 ir_variable_mode mode
)
2984 if (state
->stage
!= MESA_SHADER_FRAGMENT
||
2985 interpolation
== INTERP_MODE_FLAT
||
2986 mode
!= ir_var_shader_in
)
2989 /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
2990 * so must integer vertex outputs.
2992 * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
2993 * "Fragment shader inputs that are signed or unsigned integers or
2994 * integer vectors must be qualified with the interpolation qualifier
2997 * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
2998 * "Fragment shader inputs that are, or contain, signed or unsigned
2999 * integers or integer vectors must be qualified with the
3000 * interpolation qualifier flat."
3002 * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
3003 * "Vertex shader outputs that are, or contain, signed or unsigned
3004 * integers or integer vectors must be qualified with the
3005 * interpolation qualifier flat."
3007 * Note that prior to GLSL 1.50, this requirement applied to vertex
3008 * outputs rather than fragment inputs. That creates problems in the
3009 * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
3010 * desktop GL shaders. For GLSL ES shaders, we follow the spec and
3011 * apply the restriction to both vertex outputs and fragment inputs.
3013 * Note also that the desktop GLSL specs are missing the text "or
3014 * contain"; this is presumably an oversight, since there is no
3015 * reasonable way to interpolate a fragment shader input that contains
3016 * an integer. See Khronos bug #15671.
3018 if ((state
->is_version(130, 300) || state
->EXT_gpu_shader4_enable
)
3019 && var_type
->contains_integer()) {
3020 _mesa_glsl_error(loc
, state
, "if a fragment input is (or contains) "
3021 "an integer, then it must be qualified with 'flat'");
3024 /* Double fragment inputs must be qualified with 'flat'.
3026 * From the "Overview" of the ARB_gpu_shader_fp64 extension spec:
3027 * "This extension does not support interpolation of double-precision
3028 * values; doubles used as fragment shader inputs must be qualified as
3031 * From section 4.3.4 ("Inputs") of the GLSL 4.00 spec:
3032 * "Fragment shader inputs that are signed or unsigned integers, integer
3033 * vectors, or any double-precision floating-point type must be
3034 * qualified with the interpolation qualifier flat."
3036 * Note that the GLSL specs are missing the text "or contain"; this is
3037 * presumably an oversight. See Khronos bug #15671.
3039 * The 'double' type does not exist in GLSL ES so far.
3041 if (state
->has_double()
3042 && var_type
->contains_double()) {
3043 _mesa_glsl_error(loc
, state
, "if a fragment input is (or contains) "
3044 "a double, then it must be qualified with 'flat'");
3047 /* Bindless sampler/image fragment inputs must be qualified with 'flat'.
3049 * From section 4.3.4 of the ARB_bindless_texture spec:
3051 * "(modify last paragraph, p. 35, allowing samplers and images as
3052 * fragment shader inputs) ... Fragment inputs can only be signed and
3053 * unsigned integers and integer vectors, floating point scalars,
3054 * floating-point vectors, matrices, sampler and image types, or arrays
3055 * or structures of these. Fragment shader inputs that are signed or
3056 * unsigned integers, integer vectors, or any double-precision floating-
3057 * point type, or any sampler or image type must be qualified with the
3058 * interpolation qualifier "flat"."
3060 if (state
->has_bindless()
3061 && (var_type
->contains_sampler() || var_type
->contains_image())) {
3062 _mesa_glsl_error(loc
, state
, "if a fragment input is (or contains) "
3063 "a bindless sampler (or image), then it must be "
3064 "qualified with 'flat'");
3069 validate_interpolation_qualifier(struct _mesa_glsl_parse_state
*state
,
3071 const glsl_interp_mode interpolation
,
3072 const struct ast_type_qualifier
*qual
,
3073 const struct glsl_type
*var_type
,
3074 ir_variable_mode mode
)
3076 /* Interpolation qualifiers can only apply to shader inputs or outputs, but
3077 * not to vertex shader inputs nor fragment shader outputs.
3079 * From section 4.3 ("Storage Qualifiers") of the GLSL 1.30 spec:
3080 * "Outputs from a vertex shader (out) and inputs to a fragment
3081 * shader (in) can be further qualified with one or more of these
3082 * interpolation qualifiers"
3084 * "These interpolation qualifiers may only precede the qualifiers in,
3085 * centroid in, out, or centroid out in a declaration. They do not apply
3086 * to the deprecated storage qualifiers varying or centroid
3087 * varying. They also do not apply to inputs into a vertex shader or
3088 * outputs from a fragment shader."
3090 * From section 4.3 ("Storage Qualifiers") of the GLSL ES 3.00 spec:
3091 * "Outputs from a shader (out) and inputs to a shader (in) can be
3092 * further qualified with one of these interpolation qualifiers."
3094 * "These interpolation qualifiers may only precede the qualifiers
3095 * in, centroid in, out, or centroid out in a declaration. They do
3096 * not apply to inputs into a vertex shader or outputs from a
3099 if ((state
->is_version(130, 300) || state
->EXT_gpu_shader4_enable
)
3100 && interpolation
!= INTERP_MODE_NONE
) {
3101 const char *i
= interpolation_string(interpolation
);
3102 if (mode
!= ir_var_shader_in
&& mode
!= ir_var_shader_out
)
3103 _mesa_glsl_error(loc
, state
,
3104 "interpolation qualifier `%s' can only be applied to "
3105 "shader inputs or outputs.", i
);
3107 switch (state
->stage
) {
3108 case MESA_SHADER_VERTEX
:
3109 if (mode
== ir_var_shader_in
) {
3110 _mesa_glsl_error(loc
, state
,
3111 "interpolation qualifier '%s' cannot be applied to "
3112 "vertex shader inputs", i
);
3115 case MESA_SHADER_FRAGMENT
:
3116 if (mode
== ir_var_shader_out
) {
3117 _mesa_glsl_error(loc
, state
,
3118 "interpolation qualifier '%s' cannot be applied to "
3119 "fragment shader outputs", i
);
3127 /* Interpolation qualifiers cannot be applied to 'centroid' and
3128 * 'centroid varying'.
3130 * From section 4.3 ("Storage Qualifiers") of the GLSL 1.30 spec:
3131 * "interpolation qualifiers may only precede the qualifiers in,
3132 * centroid in, out, or centroid out in a declaration. They do not apply
3133 * to the deprecated storage qualifiers varying or centroid varying."
3135 * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
3137 * GL_EXT_gpu_shader4 allows this.
3139 if (state
->is_version(130, 0) && !state
->EXT_gpu_shader4_enable
3140 && interpolation
!= INTERP_MODE_NONE
3141 && qual
->flags
.q
.varying
) {
3143 const char *i
= interpolation_string(interpolation
);
3145 if (qual
->flags
.q
.centroid
)
3146 s
= "centroid varying";
3150 _mesa_glsl_error(loc
, state
,
3151 "qualifier '%s' cannot be applied to the "
3152 "deprecated storage qualifier '%s'", i
, s
);
3155 validate_fragment_flat_interpolation_input(state
, loc
, interpolation
,
3159 static glsl_interp_mode
3160 interpret_interpolation_qualifier(const struct ast_type_qualifier
*qual
,
3161 const struct glsl_type
*var_type
,
3162 ir_variable_mode mode
,
3163 struct _mesa_glsl_parse_state
*state
,
3166 glsl_interp_mode interpolation
;
3167 if (qual
->flags
.q
.flat
)
3168 interpolation
= INTERP_MODE_FLAT
;
3169 else if (qual
->flags
.q
.noperspective
)
3170 interpolation
= INTERP_MODE_NOPERSPECTIVE
;
3171 else if (qual
->flags
.q
.smooth
)
3172 interpolation
= INTERP_MODE_SMOOTH
;
3174 interpolation
= INTERP_MODE_NONE
;
3176 validate_interpolation_qualifier(state
, loc
,
3178 qual
, var_type
, mode
);
3180 return interpolation
;
3185 apply_explicit_location(const struct ast_type_qualifier
*qual
,
3187 struct _mesa_glsl_parse_state
*state
,
3192 unsigned qual_location
;
3193 if (!process_qualifier_constant(state
, loc
, "location", qual
->location
,
3198 /* Checks for GL_ARB_explicit_uniform_location. */
3199 if (qual
->flags
.q
.uniform
) {
3200 if (!state
->check_explicit_uniform_location_allowed(loc
, var
))
3203 const struct gl_context
*const ctx
= state
->ctx
;
3204 unsigned max_loc
= qual_location
+ var
->type
->uniform_locations() - 1;
3206 if (max_loc
>= ctx
->Const
.MaxUserAssignableUniformLocations
) {
3207 _mesa_glsl_error(loc
, state
, "location(s) consumed by uniform %s "
3208 ">= MAX_UNIFORM_LOCATIONS (%u)", var
->name
,
3209 ctx
->Const
.MaxUserAssignableUniformLocations
);
3213 var
->data
.explicit_location
= true;
3214 var
->data
.location
= qual_location
;
3218 /* Between GL_ARB_explicit_attrib_location an
3219 * GL_ARB_separate_shader_objects, the inputs and outputs of any shader
3220 * stage can be assigned explicit locations. The checking here associates
3221 * the correct extension with the correct stage's input / output:
3225 * vertex explicit_loc sso
3226 * tess control sso sso
3229 * fragment sso explicit_loc
3231 switch (state
->stage
) {
3232 case MESA_SHADER_VERTEX
:
3233 if (var
->data
.mode
== ir_var_shader_in
) {
3234 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
3240 if (var
->data
.mode
== ir_var_shader_out
) {
3241 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
3250 case MESA_SHADER_TESS_CTRL
:
3251 case MESA_SHADER_TESS_EVAL
:
3252 case MESA_SHADER_GEOMETRY
:
3253 if (var
->data
.mode
== ir_var_shader_in
|| var
->data
.mode
== ir_var_shader_out
) {
3254 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
3263 case MESA_SHADER_FRAGMENT
:
3264 if (var
->data
.mode
== ir_var_shader_in
) {
3265 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
3271 if (var
->data
.mode
== ir_var_shader_out
) {
3272 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
3281 case MESA_SHADER_COMPUTE
:
3282 _mesa_glsl_error(loc
, state
,
3283 "compute shader variables cannot be given "
3284 "explicit locations");
3292 _mesa_glsl_error(loc
, state
,
3293 "%s cannot be given an explicit location in %s shader",
3295 _mesa_shader_stage_to_string(state
->stage
));
3297 var
->data
.explicit_location
= true;
3299 switch (state
->stage
) {
3300 case MESA_SHADER_VERTEX
:
3301 var
->data
.location
= (var
->data
.mode
== ir_var_shader_in
)
3302 ? (qual_location
+ VERT_ATTRIB_GENERIC0
)
3303 : (qual_location
+ VARYING_SLOT_VAR0
);
3306 case MESA_SHADER_TESS_CTRL
:
3307 case MESA_SHADER_TESS_EVAL
:
3308 case MESA_SHADER_GEOMETRY
:
3309 if (var
->data
.patch
)
3310 var
->data
.location
= qual_location
+ VARYING_SLOT_PATCH0
;
3312 var
->data
.location
= qual_location
+ VARYING_SLOT_VAR0
;
3315 case MESA_SHADER_FRAGMENT
:
3316 var
->data
.location
= (var
->data
.mode
== ir_var_shader_out
)
3317 ? (qual_location
+ FRAG_RESULT_DATA0
)
3318 : (qual_location
+ VARYING_SLOT_VAR0
);
3321 assert(!"Unexpected shader type");
3325 /* Check if index was set for the uniform instead of the function */
3326 if (qual
->flags
.q
.explicit_index
&& qual
->is_subroutine_decl()) {
3327 _mesa_glsl_error(loc
, state
, "an index qualifier can only be "
3328 "used with subroutine functions");
3332 unsigned qual_index
;
3333 if (qual
->flags
.q
.explicit_index
&&
3334 process_qualifier_constant(state
, loc
, "index", qual
->index
,
3336 /* From the GLSL 4.30 specification, section 4.4.2 (Output
3337 * Layout Qualifiers):
3339 * "It is also a compile-time error if a fragment shader
3340 * sets a layout index to less than 0 or greater than 1."
3342 * Older specifications don't mandate a behavior; we take
3343 * this as a clarification and always generate the error.
3345 if (qual_index
> 1) {
3346 _mesa_glsl_error(loc
, state
,
3347 "explicit index may only be 0 or 1");
3349 var
->data
.explicit_index
= true;
3350 var
->data
.index
= qual_index
;
3357 validate_storage_for_sampler_image_types(ir_variable
*var
,
3358 struct _mesa_glsl_parse_state
*state
,
3361 /* From section 4.1.7 of the GLSL 4.40 spec:
3363 * "[Opaque types] can only be declared as function
3364 * parameters or uniform-qualified variables."
3366 * From section 4.1.7 of the ARB_bindless_texture spec:
3368 * "Samplers may be declared as shader inputs and outputs, as uniform
3369 * variables, as temporary variables, and as function parameters."
3371 * From section 4.1.X of the ARB_bindless_texture spec:
3373 * "Images may be declared as shader inputs and outputs, as uniform
3374 * variables, as temporary variables, and as function parameters."
3376 if (state
->has_bindless()) {
3377 if (var
->data
.mode
!= ir_var_auto
&&
3378 var
->data
.mode
!= ir_var_uniform
&&
3379 var
->data
.mode
!= ir_var_shader_in
&&
3380 var
->data
.mode
!= ir_var_shader_out
&&
3381 var
->data
.mode
!= ir_var_function_in
&&
3382 var
->data
.mode
!= ir_var_function_out
&&
3383 var
->data
.mode
!= ir_var_function_inout
) {
3384 _mesa_glsl_error(loc
, state
, "bindless image/sampler variables may "
3385 "only be declared as shader inputs and outputs, as "
3386 "uniform variables, as temporary variables and as "
3387 "function parameters");
3391 if (var
->data
.mode
!= ir_var_uniform
&&
3392 var
->data
.mode
!= ir_var_function_in
) {
3393 _mesa_glsl_error(loc
, state
, "image/sampler variables may only be "
3394 "declared as function parameters or "
3395 "uniform-qualified global variables");
3403 validate_memory_qualifier_for_type(struct _mesa_glsl_parse_state
*state
,
3405 const struct ast_type_qualifier
*qual
,
3406 const glsl_type
*type
)
3408 /* From Section 4.10 (Memory Qualifiers) of the GLSL 4.50 spec:
3410 * "Memory qualifiers are only supported in the declarations of image
3411 * variables, buffer variables, and shader storage blocks; it is an error
3412 * to use such qualifiers in any other declarations.
3414 if (!type
->is_image() && !qual
->flags
.q
.buffer
) {
3415 if (qual
->flags
.q
.read_only
||
3416 qual
->flags
.q
.write_only
||
3417 qual
->flags
.q
.coherent
||
3418 qual
->flags
.q
._volatile
||
3419 qual
->flags
.q
.restrict_flag
) {
3420 _mesa_glsl_error(loc
, state
, "memory qualifiers may only be applied "
3421 "in the declarations of image variables, buffer "
3422 "variables, and shader storage blocks");
3430 validate_image_format_qualifier_for_type(struct _mesa_glsl_parse_state
*state
,
3432 const struct ast_type_qualifier
*qual
,
3433 const glsl_type
*type
)
3435 /* From section 4.4.6.2 (Format Layout Qualifiers) of the GLSL 4.50 spec:
3437 * "Format layout qualifiers can be used on image variable declarations
3438 * (those declared with a basic type having “image ” in its keyword)."
3440 if (!type
->is_image() && qual
->flags
.q
.explicit_image_format
) {
3441 _mesa_glsl_error(loc
, state
, "format layout qualifiers may only be "
3442 "applied to images");
3449 apply_image_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
3451 struct _mesa_glsl_parse_state
*state
,
3454 const glsl_type
*base_type
= var
->type
->without_array();
3456 if (!validate_image_format_qualifier_for_type(state
, loc
, qual
, base_type
) ||
3457 !validate_memory_qualifier_for_type(state
, loc
, qual
, base_type
))
3460 if (!base_type
->is_image())
3463 if (!validate_storage_for_sampler_image_types(var
, state
, loc
))
3466 var
->data
.memory_read_only
|= qual
->flags
.q
.read_only
;
3467 var
->data
.memory_write_only
|= qual
->flags
.q
.write_only
;
3468 var
->data
.memory_coherent
|= qual
->flags
.q
.coherent
;
3469 var
->data
.memory_volatile
|= qual
->flags
.q
._volatile
;
3470 var
->data
.memory_restrict
|= qual
->flags
.q
.restrict_flag
;
3472 if (qual
->flags
.q
.explicit_image_format
) {
3473 if (var
->data
.mode
== ir_var_function_in
) {
3474 _mesa_glsl_error(loc
, state
, "format qualifiers cannot be used on "
3475 "image function parameters");
3478 if (qual
->image_base_type
!= base_type
->sampled_type
) {
3479 _mesa_glsl_error(loc
, state
, "format qualifier doesn't match the base "
3480 "data type of the image");
3483 var
->data
.image_format
= qual
->image_format
;
3484 } else if (state
->has_image_load_formatted()) {
3485 if (var
->data
.mode
== ir_var_uniform
&&
3486 state
->EXT_shader_image_load_formatted_warn
) {
3487 _mesa_glsl_warning(loc
, state
, "GL_EXT_image_load_formatted used");
3490 if (var
->data
.mode
== ir_var_uniform
) {
3491 if (state
->es_shader
||
3492 !(state
->is_version(420, 310) || state
->ARB_shader_image_load_store_enable
)) {
3493 _mesa_glsl_error(loc
, state
, "all image uniforms must have a "
3494 "format layout qualifier");
3495 } else if (!qual
->flags
.q
.write_only
) {
3496 _mesa_glsl_error(loc
, state
, "image uniforms not qualified with "
3497 "`writeonly' must have a format layout qualifier");
3500 var
->data
.image_format
= GL_NONE
;
3503 /* From page 70 of the GLSL ES 3.1 specification:
3505 * "Except for image variables qualified with the format qualifiers r32f,
3506 * r32i, and r32ui, image variables must specify either memory qualifier
3507 * readonly or the memory qualifier writeonly."
3509 if (state
->es_shader
&&
3510 var
->data
.image_format
!= GL_R32F
&&
3511 var
->data
.image_format
!= GL_R32I
&&
3512 var
->data
.image_format
!= GL_R32UI
&&
3513 !var
->data
.memory_read_only
&&
3514 !var
->data
.memory_write_only
) {
3515 _mesa_glsl_error(loc
, state
, "image variables of format other than r32f, "
3516 "r32i or r32ui must be qualified `readonly' or "
3521 static inline const char*
3522 get_layout_qualifier_string(bool origin_upper_left
, bool pixel_center_integer
)
3524 if (origin_upper_left
&& pixel_center_integer
)
3525 return "origin_upper_left, pixel_center_integer";
3526 else if (origin_upper_left
)
3527 return "origin_upper_left";
3528 else if (pixel_center_integer
)
3529 return "pixel_center_integer";
3535 is_conflicting_fragcoord_redeclaration(struct _mesa_glsl_parse_state
*state
,
3536 const struct ast_type_qualifier
*qual
)
3538 /* If gl_FragCoord was previously declared, and the qualifiers were
3539 * different in any way, return true.
3541 if (state
->fs_redeclares_gl_fragcoord
) {
3542 return (state
->fs_pixel_center_integer
!= qual
->flags
.q
.pixel_center_integer
3543 || state
->fs_origin_upper_left
!= qual
->flags
.q
.origin_upper_left
);
3550 validate_array_dimensions(const glsl_type
*t
,
3551 struct _mesa_glsl_parse_state
*state
,
3553 if (t
->is_array()) {
3554 t
= t
->fields
.array
;
3555 while (t
->is_array()) {
3556 if (t
->is_unsized_array()) {
3557 _mesa_glsl_error(loc
, state
,
3558 "only the outermost array dimension can "
3563 t
= t
->fields
.array
;
3569 apply_bindless_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
3571 struct _mesa_glsl_parse_state
*state
,
3574 bool has_local_qualifiers
= qual
->flags
.q
.bindless_sampler
||
3575 qual
->flags
.q
.bindless_image
||
3576 qual
->flags
.q
.bound_sampler
||
3577 qual
->flags
.q
.bound_image
;
3579 /* The ARB_bindless_texture spec says:
3581 * "Modify Section 4.4.6 Opaque-Uniform Layout Qualifiers of the GLSL 4.30
3584 * "If these layout qualifiers are applied to other types of default block
3585 * uniforms, or variables with non-uniform storage, a compile-time error
3586 * will be generated."
3588 if (has_local_qualifiers
&& !qual
->flags
.q
.uniform
) {
3589 _mesa_glsl_error(loc
, state
, "ARB_bindless_texture layout qualifiers "
3590 "can only be applied to default block uniforms or "
3591 "variables with uniform storage");
3595 /* The ARB_bindless_texture spec doesn't state anything in this situation,
3596 * but it makes sense to only allow bindless_sampler/bound_sampler for
3597 * sampler types, and respectively bindless_image/bound_image for image
3600 if ((qual
->flags
.q
.bindless_sampler
|| qual
->flags
.q
.bound_sampler
) &&
3601 !var
->type
->contains_sampler()) {
3602 _mesa_glsl_error(loc
, state
, "bindless_sampler or bound_sampler can only "
3603 "be applied to sampler types");
3607 if ((qual
->flags
.q
.bindless_image
|| qual
->flags
.q
.bound_image
) &&
3608 !var
->type
->contains_image()) {
3609 _mesa_glsl_error(loc
, state
, "bindless_image or bound_image can only be "
3610 "applied to image types");
3614 /* The bindless_sampler/bindless_image (and respectively
3615 * bound_sampler/bound_image) layout qualifiers can be set at global and at
3618 if (var
->type
->contains_sampler() || var
->type
->contains_image()) {
3619 var
->data
.bindless
= qual
->flags
.q
.bindless_sampler
||
3620 qual
->flags
.q
.bindless_image
||
3621 state
->bindless_sampler_specified
||
3622 state
->bindless_image_specified
;
3624 var
->data
.bound
= qual
->flags
.q
.bound_sampler
||
3625 qual
->flags
.q
.bound_image
||
3626 state
->bound_sampler_specified
||
3627 state
->bound_image_specified
;
3632 apply_layout_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
3634 struct _mesa_glsl_parse_state
*state
,
3637 if (var
->name
!= NULL
&& strcmp(var
->name
, "gl_FragCoord") == 0) {
3639 /* Section 4.3.8.1, page 39 of GLSL 1.50 spec says:
3641 * "Within any shader, the first redeclarations of gl_FragCoord
3642 * must appear before any use of gl_FragCoord."
3644 * Generate a compiler error if above condition is not met by the
3647 ir_variable
*earlier
= state
->symbols
->get_variable("gl_FragCoord");
3648 if (earlier
!= NULL
&&
3649 earlier
->data
.used
&&
3650 !state
->fs_redeclares_gl_fragcoord
) {
3651 _mesa_glsl_error(loc
, state
,
3652 "gl_FragCoord used before its first redeclaration "
3653 "in fragment shader");
3656 /* Make sure all gl_FragCoord redeclarations specify the same layout
3659 if (is_conflicting_fragcoord_redeclaration(state
, qual
)) {
3660 const char *const qual_string
=
3661 get_layout_qualifier_string(qual
->flags
.q
.origin_upper_left
,
3662 qual
->flags
.q
.pixel_center_integer
);
3664 const char *const state_string
=
3665 get_layout_qualifier_string(state
->fs_origin_upper_left
,
3666 state
->fs_pixel_center_integer
);
3668 _mesa_glsl_error(loc
, state
,
3669 "gl_FragCoord redeclared with different layout "
3670 "qualifiers (%s) and (%s) ",
3674 state
->fs_origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
3675 state
->fs_pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
3676 state
->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers
=
3677 !qual
->flags
.q
.origin_upper_left
&& !qual
->flags
.q
.pixel_center_integer
;
3678 state
->fs_redeclares_gl_fragcoord
=
3679 state
->fs_origin_upper_left
||
3680 state
->fs_pixel_center_integer
||
3681 state
->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers
;
3684 if ((qual
->flags
.q
.origin_upper_left
|| qual
->flags
.q
.pixel_center_integer
)
3685 && (strcmp(var
->name
, "gl_FragCoord") != 0)) {
3686 const char *const qual_string
= (qual
->flags
.q
.origin_upper_left
)
3687 ? "origin_upper_left" : "pixel_center_integer";
3689 _mesa_glsl_error(loc
, state
,
3690 "layout qualifier `%s' can only be applied to "
3691 "fragment shader input `gl_FragCoord'",
3695 if (qual
->flags
.q
.explicit_location
) {
3696 apply_explicit_location(qual
, var
, state
, loc
);
3698 if (qual
->flags
.q
.explicit_component
) {
3699 unsigned qual_component
;
3700 if (process_qualifier_constant(state
, loc
, "component",
3701 qual
->component
, &qual_component
)) {
3702 const glsl_type
*type
= var
->type
->without_array();
3703 unsigned components
= type
->component_slots();
3705 if (type
->is_matrix() || type
->is_struct()) {
3706 _mesa_glsl_error(loc
, state
, "component layout qualifier "
3707 "cannot be applied to a matrix, a structure, "
3708 "a block, or an array containing any of "
3710 } else if (components
> 4 && type
->is_64bit()) {
3711 _mesa_glsl_error(loc
, state
, "component layout qualifier "
3712 "cannot be applied to dvec%u.",
3714 } else if (qual_component
!= 0 &&
3715 (qual_component
+ components
- 1) > 3) {
3716 _mesa_glsl_error(loc
, state
, "component overflow (%u > 3)",
3717 (qual_component
+ components
- 1));
3718 } else if (qual_component
== 1 && type
->is_64bit()) {
3719 /* We don't bother checking for 3 as it should be caught by the
3720 * overflow check above.
3722 _mesa_glsl_error(loc
, state
, "doubles cannot begin at "
3723 "component 1 or 3");
3725 var
->data
.explicit_component
= true;
3726 var
->data
.location_frac
= qual_component
;
3730 } else if (qual
->flags
.q
.explicit_index
) {
3731 if (!qual
->subroutine_list
)
3732 _mesa_glsl_error(loc
, state
,
3733 "explicit index requires explicit location");
3734 } else if (qual
->flags
.q
.explicit_component
) {
3735 _mesa_glsl_error(loc
, state
,
3736 "explicit component requires explicit location");
3739 if (qual
->flags
.q
.explicit_binding
) {
3740 apply_explicit_binding(state
, loc
, var
, var
->type
, qual
);
3743 if (state
->stage
== MESA_SHADER_GEOMETRY
&&
3744 qual
->flags
.q
.out
&& qual
->flags
.q
.stream
) {
3745 unsigned qual_stream
;
3746 if (process_qualifier_constant(state
, loc
, "stream", qual
->stream
,
3748 validate_stream_qualifier(loc
, state
, qual_stream
)) {
3749 var
->data
.stream
= qual_stream
;
3753 if (qual
->flags
.q
.out
&& qual
->flags
.q
.xfb_buffer
) {
3754 unsigned qual_xfb_buffer
;
3755 if (process_qualifier_constant(state
, loc
, "xfb_buffer",
3756 qual
->xfb_buffer
, &qual_xfb_buffer
) &&
3757 validate_xfb_buffer_qualifier(loc
, state
, qual_xfb_buffer
)) {
3758 var
->data
.xfb_buffer
= qual_xfb_buffer
;
3759 if (qual
->flags
.q
.explicit_xfb_buffer
)
3760 var
->data
.explicit_xfb_buffer
= true;
3764 if (qual
->flags
.q
.explicit_xfb_offset
) {
3765 unsigned qual_xfb_offset
;
3766 unsigned component_size
= var
->type
->contains_double() ? 8 : 4;
3768 if (process_qualifier_constant(state
, loc
, "xfb_offset",
3769 qual
->offset
, &qual_xfb_offset
) &&
3770 validate_xfb_offset_qualifier(loc
, state
, (int) qual_xfb_offset
,
3771 var
->type
, component_size
)) {
3772 var
->data
.offset
= qual_xfb_offset
;
3773 var
->data
.explicit_xfb_offset
= true;
3777 if (qual
->flags
.q
.explicit_xfb_stride
) {
3778 unsigned qual_xfb_stride
;
3779 if (process_qualifier_constant(state
, loc
, "xfb_stride",
3780 qual
->xfb_stride
, &qual_xfb_stride
)) {
3781 var
->data
.xfb_stride
= qual_xfb_stride
;
3782 var
->data
.explicit_xfb_stride
= true;
3786 if (var
->type
->contains_atomic()) {
3787 if (var
->data
.mode
== ir_var_uniform
) {
3788 if (var
->data
.explicit_binding
) {
3790 &state
->atomic_counter_offsets
[var
->data
.binding
];
3792 if (*offset
% ATOMIC_COUNTER_SIZE
)
3793 _mesa_glsl_error(loc
, state
,
3794 "misaligned atomic counter offset");
3796 var
->data
.offset
= *offset
;
3797 *offset
+= var
->type
->atomic_size();
3800 _mesa_glsl_error(loc
, state
,
3801 "atomic counters require explicit binding point");
3803 } else if (var
->data
.mode
!= ir_var_function_in
) {
3804 _mesa_glsl_error(loc
, state
, "atomic counters may only be declared as "
3805 "function parameters or uniform-qualified "
3806 "global variables");
3810 if (var
->type
->contains_sampler() &&
3811 !validate_storage_for_sampler_image_types(var
, state
, loc
))
3814 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
3815 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
3816 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
3817 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
3818 * These extensions and all following extensions that add the 'layout'
3819 * keyword have been modified to require the use of 'in' or 'out'.
3821 * The following extension do not allow the deprecated keywords:
3823 * GL_AMD_conservative_depth
3824 * GL_ARB_conservative_depth
3825 * GL_ARB_gpu_shader5
3826 * GL_ARB_separate_shader_objects
3827 * GL_ARB_tessellation_shader
3828 * GL_ARB_transform_feedback3
3829 * GL_ARB_uniform_buffer_object
3831 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
3832 * allow layout with the deprecated keywords.
3834 const bool relaxed_layout_qualifier_checking
=
3835 state
->ARB_fragment_coord_conventions_enable
;
3837 const bool uses_deprecated_qualifier
= qual
->flags
.q
.attribute
3838 || qual
->flags
.q
.varying
;
3839 if (qual
->has_layout() && uses_deprecated_qualifier
) {
3840 if (relaxed_layout_qualifier_checking
) {
3841 _mesa_glsl_warning(loc
, state
,
3842 "`layout' qualifier may not be used with "
3843 "`attribute' or `varying'");
3845 _mesa_glsl_error(loc
, state
,
3846 "`layout' qualifier may not be used with "
3847 "`attribute' or `varying'");
3851 /* Layout qualifiers for gl_FragDepth, which are enabled by extension
3852 * AMD_conservative_depth.
3854 if (qual
->flags
.q
.depth_type
3855 && !state
->is_version(420, 0)
3856 && !state
->AMD_conservative_depth_enable
3857 && !state
->ARB_conservative_depth_enable
) {
3858 _mesa_glsl_error(loc
, state
,
3859 "extension GL_AMD_conservative_depth or "
3860 "GL_ARB_conservative_depth must be enabled "
3861 "to use depth layout qualifiers");
3862 } else if (qual
->flags
.q
.depth_type
3863 && strcmp(var
->name
, "gl_FragDepth") != 0) {
3864 _mesa_glsl_error(loc
, state
,
3865 "depth layout qualifiers can be applied only to "
3869 switch (qual
->depth_type
) {
3871 var
->data
.depth_layout
= ir_depth_layout_any
;
3873 case ast_depth_greater
:
3874 var
->data
.depth_layout
= ir_depth_layout_greater
;
3876 case ast_depth_less
:
3877 var
->data
.depth_layout
= ir_depth_layout_less
;
3879 case ast_depth_unchanged
:
3880 var
->data
.depth_layout
= ir_depth_layout_unchanged
;
3883 var
->data
.depth_layout
= ir_depth_layout_none
;
3887 if (qual
->flags
.q
.std140
||
3888 qual
->flags
.q
.std430
||
3889 qual
->flags
.q
.packed
||
3890 qual
->flags
.q
.shared
) {
3891 _mesa_glsl_error(loc
, state
,
3892 "uniform and shader storage block layout qualifiers "
3893 "std140, std430, packed, and shared can only be "
3894 "applied to uniform or shader storage blocks, not "
3898 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
3899 validate_matrix_layout_for_type(state
, loc
, var
->type
, var
);
3902 /* From section 4.4.1.3 of the GLSL 4.50 specification (Fragment Shader
3905 * "Fragment shaders also allow the following layout qualifier on in only
3906 * (not with variable declarations)
3907 * layout-qualifier-id
3908 * early_fragment_tests
3911 if (qual
->flags
.q
.early_fragment_tests
) {
3912 _mesa_glsl_error(loc
, state
, "early_fragment_tests layout qualifier only "
3913 "valid in fragment shader input layout declaration.");
3916 if (qual
->flags
.q
.inner_coverage
) {
3917 _mesa_glsl_error(loc
, state
, "inner_coverage layout qualifier only "
3918 "valid in fragment shader input layout declaration.");
3921 if (qual
->flags
.q
.post_depth_coverage
) {
3922 _mesa_glsl_error(loc
, state
, "post_depth_coverage layout qualifier only "
3923 "valid in fragment shader input layout declaration.");
3926 if (state
->has_bindless())
3927 apply_bindless_qualifier_to_variable(qual
, var
, state
, loc
);
3929 if (qual
->flags
.q
.pixel_interlock_ordered
||
3930 qual
->flags
.q
.pixel_interlock_unordered
||
3931 qual
->flags
.q
.sample_interlock_ordered
||
3932 qual
->flags
.q
.sample_interlock_unordered
) {
3933 _mesa_glsl_error(loc
, state
, "interlock layout qualifiers: "
3934 "pixel_interlock_ordered, pixel_interlock_unordered, "
3935 "sample_interlock_ordered and sample_interlock_unordered, "
3936 "only valid in fragment shader input layout declaration.");
3941 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
3943 struct _mesa_glsl_parse_state
*state
,
3947 STATIC_ASSERT(sizeof(qual
->flags
.q
) <= sizeof(qual
->flags
.i
));
3949 if (qual
->flags
.q
.invariant
) {
3950 if (var
->data
.used
) {
3951 _mesa_glsl_error(loc
, state
,
3952 "variable `%s' may not be redeclared "
3953 "`invariant' after being used",
3956 var
->data
.explicit_invariant
= true;
3957 var
->data
.invariant
= true;
3961 if (qual
->flags
.q
.precise
) {
3962 if (var
->data
.used
) {
3963 _mesa_glsl_error(loc
, state
,
3964 "variable `%s' may not be redeclared "
3965 "`precise' after being used",
3968 var
->data
.precise
= 1;
3972 if (qual
->is_subroutine_decl() && !qual
->flags
.q
.uniform
) {
3973 _mesa_glsl_error(loc
, state
,
3974 "`subroutine' may only be applied to uniforms, "
3975 "subroutine type declarations, or function definitions");
3978 if (qual
->flags
.q
.constant
|| qual
->flags
.q
.attribute
3979 || qual
->flags
.q
.uniform
3980 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
3981 var
->data
.read_only
= 1;
3983 if (qual
->flags
.q
.centroid
)
3984 var
->data
.centroid
= 1;
3986 if (qual
->flags
.q
.sample
)
3987 var
->data
.sample
= 1;
3989 /* Precision qualifiers do not hold any meaning in Desktop GLSL */
3990 if (state
->es_shader
) {
3991 var
->data
.precision
=
3992 select_gles_precision(qual
->precision
, var
->type
, state
, loc
);
3995 if (qual
->flags
.q
.patch
)
3996 var
->data
.patch
= 1;
3998 if (qual
->flags
.q
.attribute
&& state
->stage
!= MESA_SHADER_VERTEX
) {
3999 var
->type
= glsl_type::error_type
;
4000 _mesa_glsl_error(loc
, state
,
4001 "`attribute' variables may not be declared in the "
4003 _mesa_shader_stage_to_string(state
->stage
));
4006 /* Disallow layout qualifiers which may only appear on layout declarations. */
4007 if (qual
->flags
.q
.prim_type
) {
4008 _mesa_glsl_error(loc
, state
,
4009 "Primitive type may only be specified on GS input or output "
4010 "layout declaration, not on variables.");
4013 /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
4015 * "However, the const qualifier cannot be used with out or inout."
4017 * The same section of the GLSL 4.40 spec further clarifies this saying:
4019 * "The const qualifier cannot be used with out or inout, or a
4020 * compile-time error results."
4022 if (is_parameter
&& qual
->flags
.q
.constant
&& qual
->flags
.q
.out
) {
4023 _mesa_glsl_error(loc
, state
,
4024 "`const' may not be applied to `out' or `inout' "
4025 "function parameters");
4028 /* If there is no qualifier that changes the mode of the variable, leave
4029 * the setting alone.
4031 assert(var
->data
.mode
!= ir_var_temporary
);
4032 if (qual
->flags
.q
.in
&& qual
->flags
.q
.out
)
4033 var
->data
.mode
= is_parameter
? ir_var_function_inout
: ir_var_shader_out
;
4034 else if (qual
->flags
.q
.in
)
4035 var
->data
.mode
= is_parameter
? ir_var_function_in
: ir_var_shader_in
;
4036 else if (qual
->flags
.q
.attribute
4037 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
4038 var
->data
.mode
= ir_var_shader_in
;
4039 else if (qual
->flags
.q
.out
)
4040 var
->data
.mode
= is_parameter
? ir_var_function_out
: ir_var_shader_out
;
4041 else if (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_VERTEX
))
4042 var
->data
.mode
= ir_var_shader_out
;
4043 else if (qual
->flags
.q
.uniform
)
4044 var
->data
.mode
= ir_var_uniform
;
4045 else if (qual
->flags
.q
.buffer
)
4046 var
->data
.mode
= ir_var_shader_storage
;
4047 else if (qual
->flags
.q
.shared_storage
)
4048 var
->data
.mode
= ir_var_shader_shared
;
4050 if (!is_parameter
&& state
->has_framebuffer_fetch() &&
4051 state
->stage
== MESA_SHADER_FRAGMENT
) {
4052 if (state
->is_version(130, 300))
4053 var
->data
.fb_fetch_output
= qual
->flags
.q
.in
&& qual
->flags
.q
.out
;
4055 var
->data
.fb_fetch_output
= (strcmp(var
->name
, "gl_LastFragData") == 0);
4058 if (var
->data
.fb_fetch_output
) {
4059 var
->data
.assigned
= true;
4060 var
->data
.memory_coherent
= !qual
->flags
.q
.non_coherent
;
4062 /* From the EXT_shader_framebuffer_fetch spec:
4064 * "It is an error to declare an inout fragment output not qualified
4065 * with layout(noncoherent) if the GL_EXT_shader_framebuffer_fetch
4066 * extension hasn't been enabled."
4068 if (var
->data
.memory_coherent
&&
4069 !state
->EXT_shader_framebuffer_fetch_enable
)
4070 _mesa_glsl_error(loc
, state
,
4071 "invalid declaration of framebuffer fetch output not "
4072 "qualified with layout(noncoherent)");
4075 /* From the EXT_shader_framebuffer_fetch spec:
4077 * "Fragment outputs declared inout may specify the following layout
4078 * qualifier: [...] noncoherent"
4080 if (qual
->flags
.q
.non_coherent
)
4081 _mesa_glsl_error(loc
, state
,
4082 "invalid layout(noncoherent) qualifier not part of "
4083 "framebuffer fetch output declaration");
4086 if (!is_parameter
&& is_varying_var(var
, state
->stage
)) {
4087 /* User-defined ins/outs are not permitted in compute shaders. */
4088 if (state
->stage
== MESA_SHADER_COMPUTE
) {
4089 _mesa_glsl_error(loc
, state
,
4090 "user-defined input and output variables are not "
4091 "permitted in compute shaders");
4094 /* This variable is being used to link data between shader stages (in
4095 * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
4096 * that is allowed for such purposes.
4098 * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
4100 * "The varying qualifier can be used only with the data types
4101 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
4104 * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
4105 * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
4107 * "Fragment inputs can only be signed and unsigned integers and
4108 * integer vectors, float, floating-point vectors, matrices, or
4109 * arrays of these. Structures cannot be input.
4111 * Similar text exists in the section on vertex shader outputs.
4113 * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
4114 * 3.00 spec allows structs as well. Varying structs are also allowed
4117 * From section 4.3.4 of the ARB_bindless_texture spec:
4119 * "(modify third paragraph of the section to allow sampler and image
4120 * types) ... Vertex shader inputs can only be float,
4121 * single-precision floating-point scalars, single-precision
4122 * floating-point vectors, matrices, signed and unsigned integers
4123 * and integer vectors, sampler and image types."
4125 * From section 4.3.6 of the ARB_bindless_texture spec:
4127 * "Output variables can only be floating-point scalars,
4128 * floating-point vectors, matrices, signed or unsigned integers or
4129 * integer vectors, sampler or image types, or arrays or structures
4132 switch (var
->type
->without_array()->base_type
) {
4133 case GLSL_TYPE_FLOAT
:
4134 /* Ok in all GLSL versions */
4136 case GLSL_TYPE_UINT
:
4138 if (state
->is_version(130, 300) || state
->EXT_gpu_shader4_enable
)
4140 _mesa_glsl_error(loc
, state
,
4141 "varying variables must be of base type float in %s",
4142 state
->get_version_string());
4144 case GLSL_TYPE_STRUCT
:
4145 if (state
->is_version(150, 300))
4147 _mesa_glsl_error(loc
, state
,
4148 "varying variables may not be of type struct");
4150 case GLSL_TYPE_DOUBLE
:
4151 case GLSL_TYPE_UINT64
:
4152 case GLSL_TYPE_INT64
:
4154 case GLSL_TYPE_SAMPLER
:
4155 case GLSL_TYPE_IMAGE
:
4156 if (state
->has_bindless())
4160 _mesa_glsl_error(loc
, state
, "illegal type for a varying variable");
4165 if (state
->all_invariant
&& var
->data
.mode
== ir_var_shader_out
) {
4166 var
->data
.explicit_invariant
= true;
4167 var
->data
.invariant
= true;
4170 var
->data
.interpolation
=
4171 interpret_interpolation_qualifier(qual
, var
->type
,
4172 (ir_variable_mode
) var
->data
.mode
,
4175 /* Does the declaration use the deprecated 'attribute' or 'varying'
4178 const bool uses_deprecated_qualifier
= qual
->flags
.q
.attribute
4179 || qual
->flags
.q
.varying
;
4182 /* Validate auxiliary storage qualifiers */
4184 /* From section 4.3.4 of the GLSL 1.30 spec:
4185 * "It is an error to use centroid in in a vertex shader."
4187 * From section 4.3.4 of the GLSL ES 3.00 spec:
4188 * "It is an error to use centroid in or interpolation qualifiers in
4189 * a vertex shader input."
4192 /* Section 4.3.6 of the GLSL 1.30 specification states:
4193 * "It is an error to use centroid out in a fragment shader."
4195 * The GL_ARB_shading_language_420pack extension specification states:
4196 * "It is an error to use auxiliary storage qualifiers or interpolation
4197 * qualifiers on an output in a fragment shader."
4199 if (qual
->flags
.q
.sample
&& (!is_varying_var(var
, state
->stage
) || uses_deprecated_qualifier
)) {
4200 _mesa_glsl_error(loc
, state
,
4201 "sample qualifier may only be used on `in` or `out` "
4202 "variables between shader stages");
4204 if (qual
->flags
.q
.centroid
&& !is_varying_var(var
, state
->stage
)) {
4205 _mesa_glsl_error(loc
, state
,
4206 "centroid qualifier may only be used with `in', "
4207 "`out' or `varying' variables between shader stages");
4210 if (qual
->flags
.q
.shared_storage
&& state
->stage
!= MESA_SHADER_COMPUTE
) {
4211 _mesa_glsl_error(loc
, state
,
4212 "the shared storage qualifiers can only be used with "
4216 apply_image_qualifier_to_variable(qual
, var
, state
, loc
);
4220 * Get the variable that is being redeclared by this declaration or if it
4221 * does not exist, the current declared variable.
4223 * Semantic checks to verify the validity of the redeclaration are also
4224 * performed. If semantic checks fail, compilation error will be emitted via
4225 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
4228 * A pointer to an existing variable in the current scope if the declaration
4229 * is a redeclaration, current variable otherwise. \c is_declared boolean
4230 * will return \c true if the declaration is a redeclaration, \c false
4233 static ir_variable
*
4234 get_variable_being_redeclared(ir_variable
**var_ptr
, YYLTYPE loc
,
4235 struct _mesa_glsl_parse_state
*state
,
4236 bool allow_all_redeclarations
,
4237 bool *is_redeclaration
)
4239 ir_variable
*var
= *var_ptr
;
4241 /* Check if this declaration is actually a re-declaration, either to
4242 * resize an array or add qualifiers to an existing variable.
4244 * This is allowed for variables in the current scope, or when at
4245 * global scope (for built-ins in the implicit outer scope).
4247 ir_variable
*earlier
= state
->symbols
->get_variable(var
->name
);
4248 if (earlier
== NULL
||
4249 (state
->current_function
!= NULL
&&
4250 !state
->symbols
->name_declared_this_scope(var
->name
))) {
4251 *is_redeclaration
= false;
4255 *is_redeclaration
= true;
4257 if (earlier
->data
.how_declared
== ir_var_declared_implicitly
) {
4258 /* Verify that the redeclaration of a built-in does not change the
4259 * storage qualifier. There are a couple special cases.
4261 * 1. Some built-in variables that are defined as 'in' in the
4262 * specification are implemented as system values. Allow
4263 * ir_var_system_value -> ir_var_shader_in.
4265 * 2. gl_LastFragData is implemented as a ir_var_shader_out, but the
4266 * specification requires that redeclarations omit any qualifier.
4267 * Allow ir_var_shader_out -> ir_var_auto for this one variable.
4269 if (earlier
->data
.mode
!= var
->data
.mode
&&
4270 !(earlier
->data
.mode
== ir_var_system_value
&&
4271 var
->data
.mode
== ir_var_shader_in
) &&
4272 !(strcmp(var
->name
, "gl_LastFragData") == 0 &&
4273 var
->data
.mode
== ir_var_auto
)) {
4274 _mesa_glsl_error(&loc
, state
,
4275 "redeclaration cannot change qualification of `%s'",
4280 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
4282 * "It is legal to declare an array without a size and then
4283 * later re-declare the same name as an array of the same
4284 * type and specify a size."
4286 if (earlier
->type
->is_unsized_array() && var
->type
->is_array()
4287 && (var
->type
->fields
.array
== earlier
->type
->fields
.array
)) {
4288 const int size
= var
->type
->array_size();
4289 check_builtin_array_max_size(var
->name
, size
, loc
, state
);
4290 if ((size
> 0) && (size
<= earlier
->data
.max_array_access
)) {
4291 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
4293 earlier
->data
.max_array_access
);
4296 earlier
->type
= var
->type
;
4300 } else if (earlier
->type
!= var
->type
) {
4301 _mesa_glsl_error(&loc
, state
,
4302 "redeclaration of `%s' has incorrect type",
4304 } else if ((state
->ARB_fragment_coord_conventions_enable
||
4305 state
->is_version(150, 0))
4306 && strcmp(var
->name
, "gl_FragCoord") == 0) {
4307 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
4310 * We don't really need to do anything here, just allow the
4311 * redeclaration. Any error on the gl_FragCoord is handled on the ast
4312 * level at apply_layout_qualifier_to_variable using the
4313 * ast_type_qualifier and _mesa_glsl_parse_state, or later at
4316 /* According to section 4.3.7 of the GLSL 1.30 spec,
4317 * the following built-in varaibles can be redeclared with an
4318 * interpolation qualifier:
4321 * * gl_FrontSecondaryColor
4322 * * gl_BackSecondaryColor
4324 * * gl_SecondaryColor
4326 } else if (state
->is_version(130, 0)
4327 && (strcmp(var
->name
, "gl_FrontColor") == 0
4328 || strcmp(var
->name
, "gl_BackColor") == 0
4329 || strcmp(var
->name
, "gl_FrontSecondaryColor") == 0
4330 || strcmp(var
->name
, "gl_BackSecondaryColor") == 0
4331 || strcmp(var
->name
, "gl_Color") == 0
4332 || strcmp(var
->name
, "gl_SecondaryColor") == 0)) {
4333 earlier
->data
.interpolation
= var
->data
.interpolation
;
4335 /* Layout qualifiers for gl_FragDepth. */
4336 } else if ((state
->is_version(420, 0) ||
4337 state
->AMD_conservative_depth_enable
||
4338 state
->ARB_conservative_depth_enable
)
4339 && strcmp(var
->name
, "gl_FragDepth") == 0) {
4341 /** From the AMD_conservative_depth spec:
4342 * Within any shader, the first redeclarations of gl_FragDepth
4343 * must appear before any use of gl_FragDepth.
4345 if (earlier
->data
.used
) {
4346 _mesa_glsl_error(&loc
, state
,
4347 "the first redeclaration of gl_FragDepth "
4348 "must appear before any use of gl_FragDepth");
4351 /* Prevent inconsistent redeclaration of depth layout qualifier. */
4352 if (earlier
->data
.depth_layout
!= ir_depth_layout_none
4353 && earlier
->data
.depth_layout
!= var
->data
.depth_layout
) {
4354 _mesa_glsl_error(&loc
, state
,
4355 "gl_FragDepth: depth layout is declared here "
4356 "as '%s, but it was previously declared as "
4358 depth_layout_string(var
->data
.depth_layout
),
4359 depth_layout_string(earlier
->data
.depth_layout
));
4362 earlier
->data
.depth_layout
= var
->data
.depth_layout
;
4364 } else if (state
->has_framebuffer_fetch() &&
4365 strcmp(var
->name
, "gl_LastFragData") == 0 &&
4366 var
->data
.mode
== ir_var_auto
) {
4367 /* According to the EXT_shader_framebuffer_fetch spec:
4369 * "By default, gl_LastFragData is declared with the mediump precision
4370 * qualifier. This can be changed by redeclaring the corresponding
4371 * variables with the desired precision qualifier."
4373 * "Fragment shaders may specify the following layout qualifier only for
4374 * redeclaring the built-in gl_LastFragData array [...]: noncoherent"
4376 earlier
->data
.precision
= var
->data
.precision
;
4377 earlier
->data
.memory_coherent
= var
->data
.memory_coherent
;
4379 } else if ((earlier
->data
.how_declared
== ir_var_declared_implicitly
&&
4380 state
->allow_builtin_variable_redeclaration
) ||
4381 allow_all_redeclarations
) {
4382 /* Allow verbatim redeclarations of built-in variables. Not explicitly
4383 * valid, but some applications do it.
4386 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
4393 * Generate the IR for an initializer in a variable declaration
4396 process_initializer(ir_variable
*var
, ast_declaration
*decl
,
4397 ast_fully_specified_type
*type
,
4398 exec_list
*initializer_instructions
,
4399 struct _mesa_glsl_parse_state
*state
)
4401 void *mem_ctx
= state
;
4402 ir_rvalue
*result
= NULL
;
4404 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
4406 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
4408 * "All uniform variables are read-only and are initialized either
4409 * directly by an application via API commands, or indirectly by
4412 if (var
->data
.mode
== ir_var_uniform
) {
4413 state
->check_version(120, 0, &initializer_loc
,
4414 "cannot initialize uniform %s",
4418 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
4420 * "Buffer variables cannot have initializers."
4422 if (var
->data
.mode
== ir_var_shader_storage
) {
4423 _mesa_glsl_error(&initializer_loc
, state
,
4424 "cannot initialize buffer variable %s",
4428 /* From section 4.1.7 of the GLSL 4.40 spec:
4430 * "Opaque variables [...] are initialized only through the
4431 * OpenGL API; they cannot be declared with an initializer in a
4434 * From section 4.1.7 of the ARB_bindless_texture spec:
4436 * "Samplers may be declared as shader inputs and outputs, as uniform
4437 * variables, as temporary variables, and as function parameters."
4439 * From section 4.1.X of the ARB_bindless_texture spec:
4441 * "Images may be declared as shader inputs and outputs, as uniform
4442 * variables, as temporary variables, and as function parameters."
4444 if (var
->type
->contains_atomic() ||
4445 (!state
->has_bindless() && var
->type
->contains_opaque())) {
4446 _mesa_glsl_error(&initializer_loc
, state
,
4447 "cannot initialize %s variable %s",
4448 var
->name
, state
->has_bindless() ? "atomic" : "opaque");
4451 if ((var
->data
.mode
== ir_var_shader_in
) && (state
->current_function
== NULL
)) {
4452 _mesa_glsl_error(&initializer_loc
, state
,
4453 "cannot initialize %s shader input / %s %s",
4454 _mesa_shader_stage_to_string(state
->stage
),
4455 (state
->stage
== MESA_SHADER_VERTEX
)
4456 ? "attribute" : "varying",
4460 if (var
->data
.mode
== ir_var_shader_out
&& state
->current_function
== NULL
) {
4461 _mesa_glsl_error(&initializer_loc
, state
,
4462 "cannot initialize %s shader output %s",
4463 _mesa_shader_stage_to_string(state
->stage
),
4467 /* If the initializer is an ast_aggregate_initializer, recursively store
4468 * type information from the LHS into it, so that its hir() function can do
4471 if (decl
->initializer
->oper
== ast_aggregate
)
4472 _mesa_ast_set_aggregate_type(var
->type
, decl
->initializer
);
4474 ir_dereference
*const lhs
= new(state
) ir_dereference_variable(var
);
4475 ir_rvalue
*rhs
= decl
->initializer
->hir(initializer_instructions
, state
);
4477 /* Calculate the constant value if this is a const or uniform
4480 * Section 4.3 (Storage Qualifiers) of the GLSL ES 1.00.17 spec says:
4482 * "Declarations of globals without a storage qualifier, or with
4483 * just the const qualifier, may include initializers, in which case
4484 * they will be initialized before the first line of main() is
4485 * executed. Such initializers must be a constant expression."
4487 * The same section of the GLSL ES 3.00.4 spec has similar language.
4489 if (type
->qualifier
.flags
.q
.constant
4490 || type
->qualifier
.flags
.q
.uniform
4491 || (state
->es_shader
&& state
->current_function
== NULL
)) {
4492 ir_rvalue
*new_rhs
= validate_assignment(state
, initializer_loc
,
4494 if (new_rhs
!= NULL
) {
4497 /* Section 4.3.3 (Constant Expressions) of the GLSL ES 3.00.4 spec
4500 * "A constant expression is one of
4504 * - an expression formed by an operator on operands that are
4505 * all constant expressions, including getting an element of
4506 * a constant array, or a field of a constant structure, or
4507 * components of a constant vector. However, the sequence
4508 * operator ( , ) and the assignment operators ( =, +=, ...)
4509 * are not included in the operators that can create a
4510 * constant expression."
4512 * Section 12.43 (Sequence operator and constant expressions) says:
4514 * "Should the following construct be allowed?
4518 * The expression within the brackets uses the sequence operator
4519 * (',') and returns the integer 3 so the construct is declaring
4520 * a single-dimensional array of size 3. In some languages, the
4521 * construct declares a two-dimensional array. It would be
4522 * preferable to make this construct illegal to avoid confusion.
4524 * One possibility is to change the definition of the sequence
4525 * operator so that it does not return a constant-expression and
4526 * hence cannot be used to declare an array size.
4528 * RESOLUTION: The result of a sequence operator is not a
4529 * constant-expression."
4531 * Section 4.3.3 (Constant Expressions) of the GLSL 4.30.9 spec
4532 * contains language almost identical to the section 4.3.3 in the
4533 * GLSL ES 3.00.4 spec. This is a new limitation for these GLSL
4536 ir_constant
*constant_value
=
4537 rhs
->constant_expression_value(mem_ctx
);
4539 if (!constant_value
||
4540 (state
->is_version(430, 300) &&
4541 decl
->initializer
->has_sequence_subexpression())) {
4542 const char *const variable_mode
=
4543 (type
->qualifier
.flags
.q
.constant
)
4545 : ((type
->qualifier
.flags
.q
.uniform
) ? "uniform" : "global");
4547 /* If ARB_shading_language_420pack is enabled, initializers of
4548 * const-qualified local variables do not have to be constant
4549 * expressions. Const-qualified global variables must still be
4550 * initialized with constant expressions.
4552 if (!state
->has_420pack()
4553 || state
->current_function
== NULL
) {
4554 _mesa_glsl_error(& initializer_loc
, state
,
4555 "initializer of %s variable `%s' must be a "
4556 "constant expression",
4559 if (var
->type
->is_numeric()) {
4560 /* Reduce cascading errors. */
4561 var
->constant_value
= type
->qualifier
.flags
.q
.constant
4562 ? ir_constant::zero(state
, var
->type
) : NULL
;
4566 rhs
= constant_value
;
4567 var
->constant_value
= type
->qualifier
.flags
.q
.constant
4568 ? constant_value
: NULL
;
4571 if (var
->type
->is_numeric()) {
4572 /* Reduce cascading errors. */
4573 rhs
= var
->constant_value
= type
->qualifier
.flags
.q
.constant
4574 ? ir_constant::zero(state
, var
->type
) : NULL
;
4579 if (rhs
&& !rhs
->type
->is_error()) {
4580 bool temp
= var
->data
.read_only
;
4581 if (type
->qualifier
.flags
.q
.constant
)
4582 var
->data
.read_only
= false;
4584 /* Never emit code to initialize a uniform.
4586 const glsl_type
*initializer_type
;
4587 bool error_emitted
= false;
4588 if (!type
->qualifier
.flags
.q
.uniform
) {
4590 do_assignment(initializer_instructions
, state
,
4592 &result
, true, true,
4593 type
->get_location());
4594 initializer_type
= result
->type
;
4596 initializer_type
= rhs
->type
;
4598 if (!error_emitted
) {
4599 var
->constant_initializer
= rhs
->constant_expression_value(mem_ctx
);
4600 var
->data
.has_initializer
= true;
4602 /* If the declared variable is an unsized array, it must inherrit
4603 * its full type from the initializer. A declaration such as
4605 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
4609 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
4611 * The assignment generated in the if-statement (below) will also
4612 * automatically handle this case for non-uniforms.
4614 * If the declared variable is not an array, the types must
4615 * already match exactly. As a result, the type assignment
4616 * here can be done unconditionally. For non-uniforms the call
4617 * to do_assignment can change the type of the initializer (via
4618 * the implicit conversion rules). For uniforms the initializer
4619 * must be a constant expression, and the type of that expression
4620 * was validated above.
4622 var
->type
= initializer_type
;
4625 var
->data
.read_only
= temp
;
4632 validate_layout_qualifier_vertex_count(struct _mesa_glsl_parse_state
*state
,
4633 YYLTYPE loc
, ir_variable
*var
,
4634 unsigned num_vertices
,
4636 const char *var_category
)
4638 if (var
->type
->is_unsized_array()) {
4639 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says:
4641 * All geometry shader input unsized array declarations will be
4642 * sized by an earlier input layout qualifier, when present, as per
4643 * the following table.
4645 * Followed by a table mapping each allowed input layout qualifier to
4646 * the corresponding input length.
4648 * Similarly for tessellation control shader outputs.
4650 if (num_vertices
!= 0)
4651 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
4654 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec
4655 * includes the following examples of compile-time errors:
4657 * // code sequence within one shader...
4658 * in vec4 Color1[]; // size unknown
4659 * ...Color1.length()...// illegal, length() unknown
4660 * in vec4 Color2[2]; // size is 2
4661 * ...Color1.length()...// illegal, Color1 still has no size
4662 * in vec4 Color3[3]; // illegal, input sizes are inconsistent
4663 * layout(lines) in; // legal, input size is 2, matching
4664 * in vec4 Color4[3]; // illegal, contradicts layout
4667 * To detect the case illustrated by Color3, we verify that the size of
4668 * an explicitly-sized array matches the size of any previously declared
4669 * explicitly-sized array. To detect the case illustrated by Color4, we
4670 * verify that the size of an explicitly-sized array is consistent with
4671 * any previously declared input layout.
4673 if (num_vertices
!= 0 && var
->type
->length
!= num_vertices
) {
4674 _mesa_glsl_error(&loc
, state
,
4675 "%s size contradicts previously declared layout "
4676 "(size is %u, but layout requires a size of %u)",
4677 var_category
, var
->type
->length
, num_vertices
);
4678 } else if (*size
!= 0 && var
->type
->length
!= *size
) {
4679 _mesa_glsl_error(&loc
, state
,
4680 "%s sizes are inconsistent (size is %u, but a "
4681 "previous declaration has size %u)",
4682 var_category
, var
->type
->length
, *size
);
4684 *size
= var
->type
->length
;
4690 handle_tess_ctrl_shader_output_decl(struct _mesa_glsl_parse_state
*state
,
4691 YYLTYPE loc
, ir_variable
*var
)
4693 unsigned num_vertices
= 0;
4695 if (state
->tcs_output_vertices_specified
) {
4696 if (!state
->out_qualifier
->vertices
->
4697 process_qualifier_constant(state
, "vertices",
4698 &num_vertices
, false)) {
4702 if (num_vertices
> state
->Const
.MaxPatchVertices
) {
4703 _mesa_glsl_error(&loc
, state
, "vertices (%d) exceeds "
4704 "GL_MAX_PATCH_VERTICES", num_vertices
);
4709 if (!var
->type
->is_array() && !var
->data
.patch
) {
4710 _mesa_glsl_error(&loc
, state
,
4711 "tessellation control shader outputs must be arrays");
4713 /* To avoid cascading failures, short circuit the checks below. */
4717 if (var
->data
.patch
)
4720 validate_layout_qualifier_vertex_count(state
, loc
, var
, num_vertices
,
4721 &state
->tcs_output_size
,
4722 "tessellation control shader output");
4726 * Do additional processing necessary for tessellation control/evaluation shader
4727 * input declarations. This covers both interface block arrays and bare input
4731 handle_tess_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
4732 YYLTYPE loc
, ir_variable
*var
)
4734 if (!var
->type
->is_array() && !var
->data
.patch
) {
4735 _mesa_glsl_error(&loc
, state
,
4736 "per-vertex tessellation shader inputs must be arrays");
4737 /* Avoid cascading failures. */
4741 if (var
->data
.patch
)
4744 /* The ARB_tessellation_shader spec says:
4746 * "Declaring an array size is optional. If no size is specified, it
4747 * will be taken from the implementation-dependent maximum patch size
4748 * (gl_MaxPatchVertices). If a size is specified, it must match the
4749 * maximum patch size; otherwise, a compile or link error will occur."
4751 * This text appears twice, once for TCS inputs, and again for TES inputs.
4753 if (var
->type
->is_unsized_array()) {
4754 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
4755 state
->Const
.MaxPatchVertices
);
4756 } else if (var
->type
->length
!= state
->Const
.MaxPatchVertices
) {
4757 _mesa_glsl_error(&loc
, state
,
4758 "per-vertex tessellation shader input arrays must be "
4759 "sized to gl_MaxPatchVertices (%d).",
4760 state
->Const
.MaxPatchVertices
);
4766 * Do additional processing necessary for geometry shader input declarations
4767 * (this covers both interface blocks arrays and bare input variables).
4770 handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
4771 YYLTYPE loc
, ir_variable
*var
)
4773 unsigned num_vertices
= 0;
4775 if (state
->gs_input_prim_type_specified
) {
4776 num_vertices
= vertices_per_prim(state
->in_qualifier
->prim_type
);
4779 /* Geometry shader input variables must be arrays. Caller should have
4780 * reported an error for this.
4782 if (!var
->type
->is_array()) {
4783 assert(state
->error
);
4785 /* To avoid cascading failures, short circuit the checks below. */
4789 validate_layout_qualifier_vertex_count(state
, loc
, var
, num_vertices
,
4790 &state
->gs_input_size
,
4791 "geometry shader input");
4795 validate_identifier(const char *identifier
, YYLTYPE loc
,
4796 struct _mesa_glsl_parse_state
*state
)
4798 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
4800 * "Identifiers starting with "gl_" are reserved for use by
4801 * OpenGL, and may not be declared in a shader as either a
4802 * variable or a function."
4804 if (is_gl_identifier(identifier
)) {
4805 _mesa_glsl_error(&loc
, state
,
4806 "identifier `%s' uses reserved `gl_' prefix",
4808 } else if (strstr(identifier
, "__")) {
4809 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
4812 * "In addition, all identifiers containing two
4813 * consecutive underscores (__) are reserved as
4814 * possible future keywords."
4816 * The intention is that names containing __ are reserved for internal
4817 * use by the implementation, and names prefixed with GL_ are reserved
4818 * for use by Khronos. Names simply containing __ are dangerous to use,
4819 * but should be allowed.
4821 * A future version of the GLSL specification will clarify this.
4823 _mesa_glsl_warning(&loc
, state
,
4824 "identifier `%s' uses reserved `__' string",
4830 ast_declarator_list::hir(exec_list
*instructions
,
4831 struct _mesa_glsl_parse_state
*state
)
4834 const struct glsl_type
*decl_type
;
4835 const char *type_name
= NULL
;
4836 ir_rvalue
*result
= NULL
;
4837 YYLTYPE loc
= this->get_location();
4839 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
4841 * "To ensure that a particular output variable is invariant, it is
4842 * necessary to use the invariant qualifier. It can either be used to
4843 * qualify a previously declared variable as being invariant
4845 * invariant gl_Position; // make existing gl_Position be invariant"
4847 * In these cases the parser will set the 'invariant' flag in the declarator
4848 * list, and the type will be NULL.
4850 if (this->invariant
) {
4851 assert(this->type
== NULL
);
4853 if (state
->current_function
!= NULL
) {
4854 _mesa_glsl_error(& loc
, state
,
4855 "all uses of `invariant' keyword must be at global "
4859 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
4860 assert(decl
->array_specifier
== NULL
);
4861 assert(decl
->initializer
== NULL
);
4863 ir_variable
*const earlier
=
4864 state
->symbols
->get_variable(decl
->identifier
);
4865 if (earlier
== NULL
) {
4866 _mesa_glsl_error(& loc
, state
,
4867 "undeclared variable `%s' cannot be marked "
4868 "invariant", decl
->identifier
);
4869 } else if (!is_allowed_invariant(earlier
, state
)) {
4870 _mesa_glsl_error(&loc
, state
,
4871 "`%s' cannot be marked invariant; interfaces between "
4872 "shader stages only.", decl
->identifier
);
4873 } else if (earlier
->data
.used
) {
4874 _mesa_glsl_error(& loc
, state
,
4875 "variable `%s' may not be redeclared "
4876 "`invariant' after being used",
4879 earlier
->data
.explicit_invariant
= true;
4880 earlier
->data
.invariant
= true;
4884 /* Invariant redeclarations do not have r-values.
4889 if (this->precise
) {
4890 assert(this->type
== NULL
);
4892 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
4893 assert(decl
->array_specifier
== NULL
);
4894 assert(decl
->initializer
== NULL
);
4896 ir_variable
*const earlier
=
4897 state
->symbols
->get_variable(decl
->identifier
);
4898 if (earlier
== NULL
) {
4899 _mesa_glsl_error(& loc
, state
,
4900 "undeclared variable `%s' cannot be marked "
4901 "precise", decl
->identifier
);
4902 } else if (state
->current_function
!= NULL
&&
4903 !state
->symbols
->name_declared_this_scope(decl
->identifier
)) {
4904 /* Note: we have to check if we're in a function, since
4905 * builtins are treated as having come from another scope.
4907 _mesa_glsl_error(& loc
, state
,
4908 "variable `%s' from an outer scope may not be "
4909 "redeclared `precise' in this scope",
4911 } else if (earlier
->data
.used
) {
4912 _mesa_glsl_error(& loc
, state
,
4913 "variable `%s' may not be redeclared "
4914 "`precise' after being used",
4917 earlier
->data
.precise
= true;
4921 /* Precise redeclarations do not have r-values either. */
4925 assert(this->type
!= NULL
);
4926 assert(!this->invariant
);
4927 assert(!this->precise
);
4929 /* GL_EXT_shader_image_load_store base type uses GLSL_TYPE_VOID as a special value to
4930 * indicate that it needs to be updated later (see glsl_parser.yy).
4931 * This is done here, based on the layout qualifier and the type of the image var
4933 if (this->type
->qualifier
.flags
.q
.explicit_image_format
&&
4934 this->type
->specifier
->type
->is_image() &&
4935 this->type
->qualifier
.image_base_type
== GLSL_TYPE_VOID
) {
4936 /* "The ARB_shader_image_load_store says:
4937 * If both extensions are enabled in the shading language, the "size*" layout
4938 * qualifiers are treated as format qualifiers, and are mapped to equivalent
4939 * format qualifiers in the table below, according to the type of image
4941 * image* iimage* uimage*
4942 * -------- -------- --------
4943 * size1x8 n/a r8i r8ui
4944 * size1x16 r16f r16i r16ui
4945 * size1x32 r32f r32i r32ui
4946 * size2x32 rg32f rg32i rg32ui
4947 * size4x32 rgba32f rgba32i rgba32ui"
4949 if (strncmp(this->type
->specifier
->type_name
, "image", strlen("image")) == 0) {
4950 this->type
->qualifier
.image_format
= GL_R8
+
4951 this->type
->qualifier
.image_format
- GL_R8I
;
4952 this->type
->qualifier
.image_base_type
= GLSL_TYPE_FLOAT
;
4953 } else if (strncmp(this->type
->specifier
->type_name
, "uimage", strlen("uimage")) == 0) {
4954 this->type
->qualifier
.image_format
= GL_R8UI
+
4955 this->type
->qualifier
.image_format
- GL_R8I
;
4956 this->type
->qualifier
.image_base_type
= GLSL_TYPE_UINT
;
4957 } else if (strncmp(this->type
->specifier
->type_name
, "iimage", strlen("iimage")) == 0) {
4958 this->type
->qualifier
.image_base_type
= GLSL_TYPE_INT
;
4964 /* The type specifier may contain a structure definition. Process that
4965 * before any of the variable declarations.
4967 (void) this->type
->specifier
->hir(instructions
, state
);
4969 decl_type
= this->type
->glsl_type(& type_name
, state
);
4971 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
4972 * "Buffer variables may only be declared inside interface blocks
4973 * (section 4.3.9 “Interface Blocks”), which are then referred to as
4974 * shader storage blocks. It is a compile-time error to declare buffer
4975 * variables at global scope (outside a block)."
4977 if (type
->qualifier
.flags
.q
.buffer
&& !decl_type
->is_interface()) {
4978 _mesa_glsl_error(&loc
, state
,
4979 "buffer variables cannot be declared outside "
4980 "interface blocks");
4983 /* An offset-qualified atomic counter declaration sets the default
4984 * offset for the next declaration within the same atomic counter
4987 if (decl_type
&& decl_type
->contains_atomic()) {
4988 if (type
->qualifier
.flags
.q
.explicit_binding
&&
4989 type
->qualifier
.flags
.q
.explicit_offset
) {
4990 unsigned qual_binding
;
4991 unsigned qual_offset
;
4992 if (process_qualifier_constant(state
, &loc
, "binding",
4993 type
->qualifier
.binding
,
4995 && process_qualifier_constant(state
, &loc
, "offset",
4996 type
->qualifier
.offset
,
4998 if (qual_binding
< ARRAY_SIZE(state
->atomic_counter_offsets
))
4999 state
->atomic_counter_offsets
[qual_binding
] = qual_offset
;
5003 ast_type_qualifier allowed_atomic_qual_mask
;
5004 allowed_atomic_qual_mask
.flags
.i
= 0;
5005 allowed_atomic_qual_mask
.flags
.q
.explicit_binding
= 1;
5006 allowed_atomic_qual_mask
.flags
.q
.explicit_offset
= 1;
5007 allowed_atomic_qual_mask
.flags
.q
.uniform
= 1;
5009 type
->qualifier
.validate_flags(&loc
, state
, allowed_atomic_qual_mask
,
5010 "invalid layout qualifier for",
5014 if (this->declarations
.is_empty()) {
5015 /* If there is no structure involved in the program text, there are two
5016 * possible scenarios:
5018 * - The program text contained something like 'vec4;'. This is an
5019 * empty declaration. It is valid but weird. Emit a warning.
5021 * - The program text contained something like 'S;' and 'S' is not the
5022 * name of a known structure type. This is both invalid and weird.
5025 * - The program text contained something like 'mediump float;'
5026 * when the programmer probably meant 'precision mediump
5027 * float;' Emit a warning with a description of what they
5028 * probably meant to do.
5030 * Note that if decl_type is NULL and there is a structure involved,
5031 * there must have been some sort of error with the structure. In this
5032 * case we assume that an error was already generated on this line of
5033 * code for the structure. There is no need to generate an additional,
5036 assert(this->type
->specifier
->structure
== NULL
|| decl_type
!= NULL
5039 if (decl_type
== NULL
) {
5040 _mesa_glsl_error(&loc
, state
,
5041 "invalid type `%s' in empty declaration",
5044 if (decl_type
->is_array()) {
5045 /* From Section 13.22 (Array Declarations) of the GLSL ES 3.2
5048 * "... any declaration that leaves the size undefined is
5049 * disallowed as this would add complexity and there are no
5052 if (state
->es_shader
&& decl_type
->is_unsized_array()) {
5053 _mesa_glsl_error(&loc
, state
, "array size must be explicitly "
5054 "or implicitly defined");
5057 /* From Section 4.12 (Empty Declarations) of the GLSL 4.5 spec:
5059 * "The combinations of types and qualifiers that cause
5060 * compile-time or link-time errors are the same whether or not
5061 * the declaration is empty."
5063 validate_array_dimensions(decl_type
, state
, &loc
);
5066 if (decl_type
->is_atomic_uint()) {
5067 /* Empty atomic counter declarations are allowed and useful
5068 * to set the default offset qualifier.
5071 } else if (this->type
->qualifier
.precision
!= ast_precision_none
) {
5072 if (this->type
->specifier
->structure
!= NULL
) {
5073 _mesa_glsl_error(&loc
, state
,
5074 "precision qualifiers can't be applied "
5077 static const char *const precision_names
[] = {
5084 _mesa_glsl_warning(&loc
, state
,
5085 "empty declaration with precision "
5086 "qualifier, to set the default precision, "
5087 "use `precision %s %s;'",
5088 precision_names
[this->type
->
5089 qualifier
.precision
],
5092 } else if (this->type
->specifier
->structure
== NULL
) {
5093 _mesa_glsl_warning(&loc
, state
, "empty declaration");
5098 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
5099 const struct glsl_type
*var_type
;
5101 const char *identifier
= decl
->identifier
;
5102 /* FINISHME: Emit a warning if a variable declaration shadows a
5103 * FINISHME: declaration at a higher scope.
5106 if ((decl_type
== NULL
) || decl_type
->is_void()) {
5107 if (type_name
!= NULL
) {
5108 _mesa_glsl_error(& loc
, state
,
5109 "invalid type `%s' in declaration of `%s'",
5110 type_name
, decl
->identifier
);
5112 _mesa_glsl_error(& loc
, state
,
5113 "invalid type in declaration of `%s'",
5119 if (this->type
->qualifier
.is_subroutine_decl()) {
5123 t
= state
->symbols
->get_type(this->type
->specifier
->type_name
);
5125 _mesa_glsl_error(& loc
, state
,
5126 "invalid type in declaration of `%s'",
5128 name
= ralloc_asprintf(ctx
, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state
->stage
), decl
->identifier
);
5133 var_type
= process_array_type(&loc
, decl_type
, decl
->array_specifier
,
5136 var
= new(ctx
) ir_variable(var_type
, identifier
, ir_var_auto
);
5138 /* The 'varying in' and 'varying out' qualifiers can only be used with
5139 * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
5142 if (this->type
->qualifier
.flags
.q
.varying
) {
5143 if (this->type
->qualifier
.flags
.q
.in
) {
5144 _mesa_glsl_error(& loc
, state
,
5145 "`varying in' qualifier in declaration of "
5146 "`%s' only valid for geometry shaders using "
5147 "ARB_geometry_shader4 or EXT_geometry_shader4",
5149 } else if (this->type
->qualifier
.flags
.q
.out
) {
5150 _mesa_glsl_error(& loc
, state
,
5151 "`varying out' qualifier in declaration of "
5152 "`%s' only valid for geometry shaders using "
5153 "ARB_geometry_shader4 or EXT_geometry_shader4",
5158 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
5160 * "Global variables can only use the qualifiers const,
5161 * attribute, uniform, or varying. Only one may be
5164 * Local variables can only use the qualifier const."
5166 * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
5167 * any extension that adds the 'layout' keyword.
5169 if (!state
->is_version(130, 300)
5170 && !state
->has_explicit_attrib_location()
5171 && !state
->has_separate_shader_objects()
5172 && !state
->ARB_fragment_coord_conventions_enable
) {
5173 /* GL_EXT_gpu_shader4 only allows "varying out" on fragment shader
5174 * outputs. (the varying flag is not set by the parser)
5176 if (this->type
->qualifier
.flags
.q
.out
&&
5177 (!state
->EXT_gpu_shader4_enable
||
5178 state
->stage
!= MESA_SHADER_FRAGMENT
)) {
5179 _mesa_glsl_error(& loc
, state
,
5180 "`out' qualifier in declaration of `%s' "
5181 "only valid for function parameters in %s",
5182 decl
->identifier
, state
->get_version_string());
5184 if (this->type
->qualifier
.flags
.q
.in
) {
5185 _mesa_glsl_error(& loc
, state
,
5186 "`in' qualifier in declaration of `%s' "
5187 "only valid for function parameters in %s",
5188 decl
->identifier
, state
->get_version_string());
5190 /* FINISHME: Test for other invalid qualifiers. */
5193 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
5195 apply_layout_qualifier_to_variable(&this->type
->qualifier
, var
, state
,
5198 if ((var
->data
.mode
== ir_var_auto
|| var
->data
.mode
== ir_var_temporary
)
5199 && (var
->type
->is_numeric() || var
->type
->is_boolean())
5200 && state
->zero_init
) {
5201 const ir_constant_data data
= { { 0 } };
5202 var
->data
.has_initializer
= true;
5203 var
->constant_initializer
= new(var
) ir_constant(var
->type
, &data
);
5206 if (this->type
->qualifier
.flags
.q
.invariant
) {
5207 if (!is_allowed_invariant(var
, state
)) {
5208 _mesa_glsl_error(&loc
, state
,
5209 "`%s' cannot be marked invariant; interfaces between "
5210 "shader stages only", var
->name
);
5214 if (state
->current_function
!= NULL
) {
5215 const char *mode
= NULL
;
5216 const char *extra
= "";
5218 /* There is no need to check for 'inout' here because the parser will
5219 * only allow that in function parameter lists.
5221 if (this->type
->qualifier
.flags
.q
.attribute
) {
5223 } else if (this->type
->qualifier
.is_subroutine_decl()) {
5224 mode
= "subroutine uniform";
5225 } else if (this->type
->qualifier
.flags
.q
.uniform
) {
5227 } else if (this->type
->qualifier
.flags
.q
.varying
) {
5229 } else if (this->type
->qualifier
.flags
.q
.in
) {
5231 extra
= " or in function parameter list";
5232 } else if (this->type
->qualifier
.flags
.q
.out
) {
5234 extra
= " or in function parameter list";
5238 _mesa_glsl_error(& loc
, state
,
5239 "%s variable `%s' must be declared at "
5241 mode
, var
->name
, extra
);
5243 } else if (var
->data
.mode
== ir_var_shader_in
) {
5244 var
->data
.read_only
= true;
5246 if (state
->stage
== MESA_SHADER_VERTEX
) {
5247 bool error_emitted
= false;
5249 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
5251 * "Vertex shader inputs can only be float, floating-point
5252 * vectors, matrices, signed and unsigned integers and integer
5253 * vectors. Vertex shader inputs can also form arrays of these
5254 * types, but not structures."
5256 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
5258 * "Vertex shader inputs can only be float, floating-point
5259 * vectors, matrices, signed and unsigned integers and integer
5260 * vectors. They cannot be arrays or structures."
5262 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
5264 * "The attribute qualifier can be used only with float,
5265 * floating-point vectors, and matrices. Attribute variables
5266 * cannot be declared as arrays or structures."
5268 * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
5270 * "Vertex shader inputs can only be float, floating-point
5271 * vectors, matrices, signed and unsigned integers and integer
5272 * vectors. Vertex shader inputs cannot be arrays or
5275 * From section 4.3.4 of the ARB_bindless_texture spec:
5277 * "(modify third paragraph of the section to allow sampler and
5278 * image types) ... Vertex shader inputs can only be float,
5279 * single-precision floating-point scalars, single-precision
5280 * floating-point vectors, matrices, signed and unsigned
5281 * integers and integer vectors, sampler and image types."
5283 const glsl_type
*check_type
= var
->type
->without_array();
5285 switch (check_type
->base_type
) {
5286 case GLSL_TYPE_FLOAT
:
5288 case GLSL_TYPE_UINT64
:
5289 case GLSL_TYPE_INT64
:
5291 case GLSL_TYPE_UINT
:
5293 if (state
->is_version(120, 300) || state
->EXT_gpu_shader4_enable
)
5295 case GLSL_TYPE_DOUBLE
:
5296 if (check_type
->is_double() && (state
->is_version(410, 0) || state
->ARB_vertex_attrib_64bit_enable
))
5298 case GLSL_TYPE_SAMPLER
:
5299 if (check_type
->is_sampler() && state
->has_bindless())
5301 case GLSL_TYPE_IMAGE
:
5302 if (check_type
->is_image() && state
->has_bindless())
5306 _mesa_glsl_error(& loc
, state
,
5307 "vertex shader input / attribute cannot have "
5309 var
->type
->is_array() ? "array of " : "",
5311 error_emitted
= true;
5314 if (!error_emitted
&& var
->type
->is_array() &&
5315 !state
->check_version(150, 0, &loc
,
5316 "vertex shader input / attribute "
5317 "cannot have array type")) {
5318 error_emitted
= true;
5320 } else if (state
->stage
== MESA_SHADER_GEOMETRY
) {
5321 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
5323 * Geometry shader input variables get the per-vertex values
5324 * written out by vertex shader output variables of the same
5325 * names. Since a geometry shader operates on a set of
5326 * vertices, each input varying variable (or input block, see
5327 * interface blocks below) needs to be declared as an array.
5329 if (!var
->type
->is_array()) {
5330 _mesa_glsl_error(&loc
, state
,
5331 "geometry shader inputs must be arrays");
5334 handle_geometry_shader_input_decl(state
, loc
, var
);
5335 } else if (state
->stage
== MESA_SHADER_FRAGMENT
) {
5336 /* From section 4.3.4 (Input Variables) of the GLSL ES 3.10 spec:
5338 * It is a compile-time error to declare a fragment shader
5339 * input with, or that contains, any of the following types:
5343 * * An array of arrays
5344 * * An array of structures
5345 * * A structure containing an array
5346 * * A structure containing a structure
5348 if (state
->es_shader
) {
5349 const glsl_type
*check_type
= var
->type
->without_array();
5350 if (check_type
->is_boolean() ||
5351 check_type
->contains_opaque()) {
5352 _mesa_glsl_error(&loc
, state
,
5353 "fragment shader input cannot have type %s",
5356 if (var
->type
->is_array() &&
5357 var
->type
->fields
.array
->is_array()) {
5358 _mesa_glsl_error(&loc
, state
,
5360 "cannot have an array of arrays",
5361 _mesa_shader_stage_to_string(state
->stage
));
5363 if (var
->type
->is_array() &&
5364 var
->type
->fields
.array
->is_struct()) {
5365 _mesa_glsl_error(&loc
, state
,
5366 "fragment shader input "
5367 "cannot have an array of structs");
5369 if (var
->type
->is_struct()) {
5370 for (unsigned i
= 0; i
< var
->type
->length
; i
++) {
5371 if (var
->type
->fields
.structure
[i
].type
->is_array() ||
5372 var
->type
->fields
.structure
[i
].type
->is_struct())
5373 _mesa_glsl_error(&loc
, state
,
5374 "fragment shader input cannot have "
5375 "a struct that contains an "
5380 } else if (state
->stage
== MESA_SHADER_TESS_CTRL
||
5381 state
->stage
== MESA_SHADER_TESS_EVAL
) {
5382 handle_tess_shader_input_decl(state
, loc
, var
);
5384 } else if (var
->data
.mode
== ir_var_shader_out
) {
5385 const glsl_type
*check_type
= var
->type
->without_array();
5387 /* From section 4.3.6 (Output variables) of the GLSL 4.40 spec:
5389 * It is a compile-time error to declare a fragment shader output
5390 * that contains any of the following:
5392 * * A Boolean type (bool, bvec2 ...)
5393 * * A double-precision scalar or vector (double, dvec2 ...)
5398 if (state
->stage
== MESA_SHADER_FRAGMENT
) {
5399 if (check_type
->is_struct() || check_type
->is_matrix())
5400 _mesa_glsl_error(&loc
, state
,
5401 "fragment shader output "
5402 "cannot have struct or matrix type");
5403 switch (check_type
->base_type
) {
5404 case GLSL_TYPE_UINT
:
5406 case GLSL_TYPE_FLOAT
:
5409 _mesa_glsl_error(&loc
, state
,
5410 "fragment shader output cannot have "
5411 "type %s", check_type
->name
);
5415 /* From section 4.3.6 (Output Variables) of the GLSL ES 3.10 spec:
5417 * It is a compile-time error to declare a vertex shader output
5418 * with, or that contains, any of the following types:
5422 * * An array of arrays
5423 * * An array of structures
5424 * * A structure containing an array
5425 * * A structure containing a structure
5427 * It is a compile-time error to declare a fragment shader output
5428 * with, or that contains, any of the following types:
5434 * * An array of array
5436 * ES 3.20 updates this to apply to tessellation and geometry shaders
5437 * as well. Because there are per-vertex arrays in the new stages,
5438 * it strikes the "array of..." rules and replaces them with these:
5440 * * For per-vertex-arrayed variables (applies to tessellation
5441 * control, tessellation evaluation and geometry shaders):
5443 * * Per-vertex-arrayed arrays of arrays
5444 * * Per-vertex-arrayed arrays of structures
5446 * * For non-per-vertex-arrayed variables:
5448 * * An array of arrays
5449 * * An array of structures
5451 * which basically says to unwrap the per-vertex aspect and apply
5454 if (state
->es_shader
) {
5455 if (var
->type
->is_array() &&
5456 var
->type
->fields
.array
->is_array()) {
5457 _mesa_glsl_error(&loc
, state
,
5459 "cannot have an array of arrays",
5460 _mesa_shader_stage_to_string(state
->stage
));
5462 if (state
->stage
<= MESA_SHADER_GEOMETRY
) {
5463 const glsl_type
*type
= var
->type
;
5465 if (state
->stage
== MESA_SHADER_TESS_CTRL
&&
5466 !var
->data
.patch
&& var
->type
->is_array()) {
5467 type
= var
->type
->fields
.array
;
5470 if (type
->is_array() && type
->fields
.array
->is_struct()) {
5471 _mesa_glsl_error(&loc
, state
,
5472 "%s shader output cannot have "
5473 "an array of structs",
5474 _mesa_shader_stage_to_string(state
->stage
));
5476 if (type
->is_struct()) {
5477 for (unsigned i
= 0; i
< type
->length
; i
++) {
5478 if (type
->fields
.structure
[i
].type
->is_array() ||
5479 type
->fields
.structure
[i
].type
->is_struct())
5480 _mesa_glsl_error(&loc
, state
,
5481 "%s shader output cannot have a "
5482 "struct that contains an "
5484 _mesa_shader_stage_to_string(state
->stage
));
5490 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
5491 handle_tess_ctrl_shader_output_decl(state
, loc
, var
);
5493 } else if (var
->type
->contains_subroutine()) {
5494 /* declare subroutine uniforms as hidden */
5495 var
->data
.how_declared
= ir_var_hidden
;
5498 /* From section 4.3.4 of the GLSL 4.00 spec:
5499 * "Input variables may not be declared using the patch in qualifier
5500 * in tessellation control or geometry shaders."
5502 * From section 4.3.6 of the GLSL 4.00 spec:
5503 * "It is an error to use patch out in a vertex, tessellation
5504 * evaluation, or geometry shader."
5506 * This doesn't explicitly forbid using them in a fragment shader, but
5507 * that's probably just an oversight.
5509 if (state
->stage
!= MESA_SHADER_TESS_EVAL
5510 && this->type
->qualifier
.flags
.q
.patch
5511 && this->type
->qualifier
.flags
.q
.in
) {
5513 _mesa_glsl_error(&loc
, state
, "'patch in' can only be used in a "
5514 "tessellation evaluation shader");
5517 if (state
->stage
!= MESA_SHADER_TESS_CTRL
5518 && this->type
->qualifier
.flags
.q
.patch
5519 && this->type
->qualifier
.flags
.q
.out
) {
5521 _mesa_glsl_error(&loc
, state
, "'patch out' can only be used in a "
5522 "tessellation control shader");
5525 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
5527 if (this->type
->qualifier
.precision
!= ast_precision_none
) {
5528 state
->check_precision_qualifiers_allowed(&loc
);
5531 if (this->type
->qualifier
.precision
!= ast_precision_none
&&
5532 !precision_qualifier_allowed(var
->type
)) {
5533 _mesa_glsl_error(&loc
, state
,
5534 "precision qualifiers apply only to floating point"
5535 ", integer and opaque types");
5538 /* From section 4.1.7 of the GLSL 4.40 spec:
5540 * "[Opaque types] can only be declared as function
5541 * parameters or uniform-qualified variables."
5543 * From section 4.1.7 of the ARB_bindless_texture spec:
5545 * "Samplers may be declared as shader inputs and outputs, as uniform
5546 * variables, as temporary variables, and as function parameters."
5548 * From section 4.1.X of the ARB_bindless_texture spec:
5550 * "Images may be declared as shader inputs and outputs, as uniform
5551 * variables, as temporary variables, and as function parameters."
5553 if (!this->type
->qualifier
.flags
.q
.uniform
&&
5554 (var_type
->contains_atomic() ||
5555 (!state
->has_bindless() && var_type
->contains_opaque()))) {
5556 _mesa_glsl_error(&loc
, state
,
5557 "%s variables must be declared uniform",
5558 state
->has_bindless() ? "atomic" : "opaque");
5561 /* Process the initializer and add its instructions to a temporary
5562 * list. This list will be added to the instruction stream (below) after
5563 * the declaration is added. This is done because in some cases (such as
5564 * redeclarations) the declaration may not actually be added to the
5565 * instruction stream.
5567 exec_list initializer_instructions
;
5569 /* Examine var name here since var may get deleted in the next call */
5570 bool var_is_gl_id
= is_gl_identifier(var
->name
);
5572 bool is_redeclaration
;
5573 var
= get_variable_being_redeclared(&var
, decl
->get_location(), state
,
5574 false /* allow_all_redeclarations */,
5576 if (is_redeclaration
) {
5578 var
->data
.how_declared
== ir_var_declared_in_block
) {
5579 _mesa_glsl_error(&loc
, state
,
5580 "`%s' has already been redeclared using "
5581 "gl_PerVertex", var
->name
);
5583 var
->data
.how_declared
= ir_var_declared_normally
;
5586 if (decl
->initializer
!= NULL
) {
5587 result
= process_initializer(var
,
5589 &initializer_instructions
, state
);
5591 validate_array_dimensions(var_type
, state
, &loc
);
5594 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
5596 * "It is an error to write to a const variable outside of
5597 * its declaration, so they must be initialized when
5600 if (this->type
->qualifier
.flags
.q
.constant
&& decl
->initializer
== NULL
) {
5601 _mesa_glsl_error(& loc
, state
,
5602 "const declaration of `%s' must be initialized",
5606 if (state
->es_shader
) {
5607 const glsl_type
*const t
= var
->type
;
5609 /* Skip the unsized array check for TCS/TES/GS inputs & TCS outputs.
5611 * The GL_OES_tessellation_shader spec says about inputs:
5613 * "Declaring an array size is optional. If no size is specified,
5614 * it will be taken from the implementation-dependent maximum
5615 * patch size (gl_MaxPatchVertices)."
5617 * and about TCS outputs:
5619 * "If no size is specified, it will be taken from output patch
5620 * size declared in the shader."
5622 * The GL_OES_geometry_shader spec says:
5624 * "All geometry shader input unsized array declarations will be
5625 * sized by an earlier input primitive layout qualifier, when
5626 * present, as per the following table."
5628 const bool implicitly_sized
=
5629 (var
->data
.mode
== ir_var_shader_in
&&
5630 state
->stage
>= MESA_SHADER_TESS_CTRL
&&
5631 state
->stage
<= MESA_SHADER_GEOMETRY
) ||
5632 (var
->data
.mode
== ir_var_shader_out
&&
5633 state
->stage
== MESA_SHADER_TESS_CTRL
);
5635 if (t
->is_unsized_array() && !implicitly_sized
)
5636 /* Section 10.17 of the GLSL ES 1.00 specification states that
5637 * unsized array declarations have been removed from the language.
5638 * Arrays that are sized using an initializer are still explicitly
5639 * sized. However, GLSL ES 1.00 does not allow array
5640 * initializers. That is only allowed in GLSL ES 3.00.
5642 * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
5644 * "An array type can also be formed without specifying a size
5645 * if the definition includes an initializer:
5647 * float x[] = float[2] (1.0, 2.0); // declares an array of size 2
5648 * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
5653 _mesa_glsl_error(& loc
, state
,
5654 "unsized array declarations are not allowed in "
5658 /* Section 4.4.6.1 Atomic Counter Layout Qualifiers of the GLSL 4.60 spec:
5660 * "It is a compile-time error to declare an unsized array of
5663 if (var
->type
->is_unsized_array() &&
5664 var
->type
->without_array()->base_type
== GLSL_TYPE_ATOMIC_UINT
) {
5665 _mesa_glsl_error(& loc
, state
,
5666 "Unsized array of atomic_uint is not allowed");
5669 /* If the declaration is not a redeclaration, there are a few additional
5670 * semantic checks that must be applied. In addition, variable that was
5671 * created for the declaration should be added to the IR stream.
5673 if (!is_redeclaration
) {
5674 validate_identifier(decl
->identifier
, loc
, state
);
5676 /* Add the variable to the symbol table. Note that the initializer's
5677 * IR was already processed earlier (though it hasn't been emitted
5678 * yet), without the variable in scope.
5680 * This differs from most C-like languages, but it follows the GLSL
5681 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
5684 * "Within a declaration, the scope of a name starts immediately
5685 * after the initializer if present or immediately after the name
5686 * being declared if not."
5688 if (!state
->symbols
->add_variable(var
)) {
5689 YYLTYPE loc
= this->get_location();
5690 _mesa_glsl_error(&loc
, state
, "name `%s' already taken in the "
5691 "current scope", decl
->identifier
);
5695 /* Push the variable declaration to the top. It means that all the
5696 * variable declarations will appear in a funny last-to-first order,
5697 * but otherwise we run into trouble if a function is prototyped, a
5698 * global var is decled, then the function is defined with usage of
5699 * the global var. See glslparsertest's CorrectModule.frag.
5701 instructions
->push_head(var
);
5704 instructions
->append_list(&initializer_instructions
);
5708 /* Generally, variable declarations do not have r-values. However,
5709 * one is used for the declaration in
5711 * while (bool b = some_condition()) {
5715 * so we return the rvalue from the last seen declaration here.
5722 ast_parameter_declarator::hir(exec_list
*instructions
,
5723 struct _mesa_glsl_parse_state
*state
)
5726 const struct glsl_type
*type
;
5727 const char *name
= NULL
;
5728 YYLTYPE loc
= this->get_location();
5730 type
= this->type
->glsl_type(& name
, state
);
5734 _mesa_glsl_error(& loc
, state
,
5735 "invalid type `%s' in declaration of `%s'",
5736 name
, this->identifier
);
5738 _mesa_glsl_error(& loc
, state
,
5739 "invalid type in declaration of `%s'",
5743 type
= glsl_type::error_type
;
5746 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
5748 * "Functions that accept no input arguments need not use void in the
5749 * argument list because prototypes (or definitions) are required and
5750 * therefore there is no ambiguity when an empty argument list "( )" is
5751 * declared. The idiom "(void)" as a parameter list is provided for
5754 * Placing this check here prevents a void parameter being set up
5755 * for a function, which avoids tripping up checks for main taking
5756 * parameters and lookups of an unnamed symbol.
5758 if (type
->is_void()) {
5759 if (this->identifier
!= NULL
)
5760 _mesa_glsl_error(& loc
, state
,
5761 "named parameter cannot have type `void'");
5767 if (formal_parameter
&& (this->identifier
== NULL
)) {
5768 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
5772 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
5773 * call already handled the "vec4[..] foo" case.
5775 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
5777 if (!type
->is_error() && type
->is_unsized_array()) {
5778 _mesa_glsl_error(&loc
, state
, "arrays passed as parameters must have "
5780 type
= glsl_type::error_type
;
5784 ir_variable
*var
= new(ctx
)
5785 ir_variable(type
, this->identifier
, ir_var_function_in
);
5787 /* Apply any specified qualifiers to the parameter declaration. Note that
5788 * for function parameters the default mode is 'in'.
5790 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
,
5793 /* From section 4.1.7 of the GLSL 4.40 spec:
5795 * "Opaque variables cannot be treated as l-values; hence cannot
5796 * be used as out or inout function parameters, nor can they be
5799 * From section 4.1.7 of the ARB_bindless_texture spec:
5801 * "Samplers can be used as l-values, so can be assigned into and used
5802 * as "out" and "inout" function parameters."
5804 * From section 4.1.X of the ARB_bindless_texture spec:
5806 * "Images can be used as l-values, so can be assigned into and used as
5807 * "out" and "inout" function parameters."
5809 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
5810 && (type
->contains_atomic() ||
5811 (!state
->has_bindless() && type
->contains_opaque()))) {
5812 _mesa_glsl_error(&loc
, state
, "out and inout parameters cannot "
5813 "contain %s variables",
5814 state
->has_bindless() ? "atomic" : "opaque");
5815 type
= glsl_type::error_type
;
5818 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
5820 * "When calling a function, expressions that do not evaluate to
5821 * l-values cannot be passed to parameters declared as out or inout."
5823 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
5825 * "Other binary or unary expressions, non-dereferenced arrays,
5826 * function names, swizzles with repeated fields, and constants
5827 * cannot be l-values."
5829 * So for GLSL 1.10, passing an array as an out or inout parameter is not
5830 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
5832 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
5834 && !state
->check_version(120, 100, &loc
,
5835 "arrays cannot be out or inout parameters")) {
5836 type
= glsl_type::error_type
;
5839 instructions
->push_tail(var
);
5841 /* Parameter declarations do not have r-values.
5848 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
5850 exec_list
*ir_parameters
,
5851 _mesa_glsl_parse_state
*state
)
5853 ast_parameter_declarator
*void_param
= NULL
;
5856 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
5857 param
->formal_parameter
= formal
;
5858 param
->hir(ir_parameters
, state
);
5866 if ((void_param
!= NULL
) && (count
> 1)) {
5867 YYLTYPE loc
= void_param
->get_location();
5869 _mesa_glsl_error(& loc
, state
,
5870 "`void' parameter must be only parameter");
5876 emit_function(_mesa_glsl_parse_state
*state
, ir_function
*f
)
5878 /* IR invariants disallow function declarations or definitions
5879 * nested within other function definitions. But there is no
5880 * requirement about the relative order of function declarations
5881 * and definitions with respect to one another. So simply insert
5882 * the new ir_function block at the end of the toplevel instruction
5885 state
->toplevel_ir
->push_tail(f
);
5890 ast_function::hir(exec_list
*instructions
,
5891 struct _mesa_glsl_parse_state
*state
)
5894 ir_function
*f
= NULL
;
5895 ir_function_signature
*sig
= NULL
;
5896 exec_list hir_parameters
;
5897 YYLTYPE loc
= this->get_location();
5899 const char *const name
= identifier
;
5901 /* New functions are always added to the top-level IR instruction stream,
5902 * so this instruction list pointer is ignored. See also emit_function
5905 (void) instructions
;
5907 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
5909 * "Function declarations (prototypes) cannot occur inside of functions;
5910 * they must be at global scope, or for the built-in functions, outside
5911 * the global scope."
5913 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
5915 * "User defined functions may only be defined within the global scope."
5917 * Note that this language does not appear in GLSL 1.10.
5919 if ((state
->current_function
!= NULL
) &&
5920 state
->is_version(120, 100)) {
5921 YYLTYPE loc
= this->get_location();
5922 _mesa_glsl_error(&loc
, state
,
5923 "declaration of function `%s' not allowed within "
5924 "function body", name
);
5927 validate_identifier(name
, this->get_location(), state
);
5929 /* Convert the list of function parameters to HIR now so that they can be
5930 * used below to compare this function's signature with previously seen
5931 * signatures for functions with the same name.
5933 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
5935 & hir_parameters
, state
);
5937 const char *return_type_name
;
5938 const glsl_type
*return_type
=
5939 this->return_type
->glsl_type(& return_type_name
, state
);
5942 YYLTYPE loc
= this->get_location();
5943 _mesa_glsl_error(&loc
, state
,
5944 "function `%s' has undeclared return type `%s'",
5945 name
, return_type_name
);
5946 return_type
= glsl_type::error_type
;
5949 /* ARB_shader_subroutine states:
5950 * "Subroutine declarations cannot be prototyped. It is an error to prepend
5951 * subroutine(...) to a function declaration."
5953 if (this->return_type
->qualifier
.subroutine_list
&& !is_definition
) {
5954 YYLTYPE loc
= this->get_location();
5955 _mesa_glsl_error(&loc
, state
,
5956 "function declaration `%s' cannot have subroutine prepended",
5960 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
5961 * "No qualifier is allowed on the return type of a function."
5963 if (this->return_type
->has_qualifiers(state
)) {
5964 YYLTYPE loc
= this->get_location();
5965 _mesa_glsl_error(& loc
, state
,
5966 "function `%s' return type has qualifiers", name
);
5969 /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
5971 * "Arrays are allowed as arguments and as the return type. In both
5972 * cases, the array must be explicitly sized."
5974 if (return_type
->is_unsized_array()) {
5975 YYLTYPE loc
= this->get_location();
5976 _mesa_glsl_error(& loc
, state
,
5977 "function `%s' return type array must be explicitly "
5981 /* From Section 6.1 (Function Definitions) of the GLSL 1.00 spec:
5983 * "Arrays are allowed as arguments, but not as the return type. [...]
5984 * The return type can also be a structure if the structure does not
5985 * contain an array."
5987 if (state
->language_version
== 100 && return_type
->contains_array()) {
5988 YYLTYPE loc
= this->get_location();
5989 _mesa_glsl_error(& loc
, state
,
5990 "function `%s' return type contains an array", name
);
5993 /* From section 4.1.7 of the GLSL 4.40 spec:
5995 * "[Opaque types] can only be declared as function parameters
5996 * or uniform-qualified variables."
5998 * The ARB_bindless_texture spec doesn't clearly state this, but as it says
5999 * "Replace Section 4.1.7 (Samplers), p. 25" and, "Replace Section 4.1.X,
6000 * (Images)", this should be allowed.
6002 if (return_type
->contains_atomic() ||
6003 (!state
->has_bindless() && return_type
->contains_opaque())) {
6004 YYLTYPE loc
= this->get_location();
6005 _mesa_glsl_error(&loc
, state
,
6006 "function `%s' return type can't contain an %s type",
6007 name
, state
->has_bindless() ? "atomic" : "opaque");
6011 if (return_type
->is_subroutine()) {
6012 YYLTYPE loc
= this->get_location();
6013 _mesa_glsl_error(&loc
, state
,
6014 "function `%s' return type can't be a subroutine type",
6018 /* Get the precision for the return type */
6019 unsigned return_precision
;
6021 if (state
->es_shader
) {
6022 YYLTYPE loc
= this->get_location();
6024 select_gles_precision(this->return_type
->qualifier
.precision
,
6029 return_precision
= GLSL_PRECISION_NONE
;
6032 /* Create an ir_function if one doesn't already exist. */
6033 f
= state
->symbols
->get_function(name
);
6035 f
= new(ctx
) ir_function(name
);
6036 if (!this->return_type
->qualifier
.is_subroutine_decl()) {
6037 if (!state
->symbols
->add_function(f
)) {
6038 /* This function name shadows a non-function use of the same name. */
6039 YYLTYPE loc
= this->get_location();
6040 _mesa_glsl_error(&loc
, state
, "function name `%s' conflicts with "
6041 "non-function", name
);
6045 emit_function(state
, f
);
6048 /* From GLSL ES 3.0 spec, chapter 6.1 "Function Definitions", page 71:
6050 * "A shader cannot redefine or overload built-in functions."
6052 * While in GLSL ES 1.0 specification, chapter 8 "Built-in Functions":
6054 * "User code can overload the built-in functions but cannot redefine
6057 if (state
->es_shader
) {
6058 /* Local shader has no exact candidates; check the built-ins. */
6059 if (state
->language_version
>= 300 &&
6060 _mesa_glsl_has_builtin_function(state
, name
)) {
6061 YYLTYPE loc
= this->get_location();
6062 _mesa_glsl_error(& loc
, state
,
6063 "A shader cannot redefine or overload built-in "
6064 "function `%s' in GLSL ES 3.00", name
);
6068 if (state
->language_version
== 100) {
6069 ir_function_signature
*sig
=
6070 _mesa_glsl_find_builtin_function(state
, name
, &hir_parameters
);
6071 if (sig
&& sig
->is_builtin()) {
6072 _mesa_glsl_error(& loc
, state
,
6073 "A shader cannot redefine built-in "
6074 "function `%s' in GLSL ES 1.00", name
);
6079 /* Verify that this function's signature either doesn't match a previously
6080 * seen signature for a function with the same name, or, if a match is found,
6081 * that the previously seen signature does not have an associated definition.
6083 if (state
->es_shader
|| f
->has_user_signature()) {
6084 sig
= f
->exact_matching_signature(state
, &hir_parameters
);
6086 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
6087 if (badvar
!= NULL
) {
6088 YYLTYPE loc
= this->get_location();
6090 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
6091 "qualifiers don't match prototype", name
, badvar
);
6094 if (sig
->return_type
!= return_type
) {
6095 YYLTYPE loc
= this->get_location();
6097 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
6098 "match prototype", name
);
6101 if (sig
->return_precision
!= return_precision
) {
6102 YYLTYPE loc
= this->get_location();
6104 _mesa_glsl_error(&loc
, state
, "function `%s' return type precision "
6105 "doesn't match prototype", name
);
6108 if (sig
->is_defined
) {
6109 if (is_definition
) {
6110 YYLTYPE loc
= this->get_location();
6111 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
6113 /* We just encountered a prototype that exactly matches a
6114 * function that's already been defined. This is redundant,
6115 * and we should ignore it.
6119 } else if (state
->language_version
== 100 && !is_definition
) {
6120 /* From the GLSL 1.00 spec, section 4.2.7:
6122 * "A particular variable, structure or function declaration
6123 * may occur at most once within a scope with the exception
6124 * that a single function prototype plus the corresponding
6125 * function definition are allowed."
6127 YYLTYPE loc
= this->get_location();
6128 _mesa_glsl_error(&loc
, state
, "function `%s' redeclared", name
);
6133 /* Verify the return type of main() */
6134 if (strcmp(name
, "main") == 0) {
6135 if (! return_type
->is_void()) {
6136 YYLTYPE loc
= this->get_location();
6138 _mesa_glsl_error(& loc
, state
, "main() must return void");
6141 if (!hir_parameters
.is_empty()) {
6142 YYLTYPE loc
= this->get_location();
6144 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
6148 /* Finish storing the information about this new function in its signature.
6151 sig
= new(ctx
) ir_function_signature(return_type
);
6152 sig
->return_precision
= return_precision
;
6153 f
->add_signature(sig
);
6156 sig
->replace_parameters(&hir_parameters
);
6159 if (this->return_type
->qualifier
.subroutine_list
) {
6162 if (this->return_type
->qualifier
.flags
.q
.explicit_index
) {
6163 unsigned qual_index
;
6164 if (process_qualifier_constant(state
, &loc
, "index",
6165 this->return_type
->qualifier
.index
,
6167 if (!state
->has_explicit_uniform_location()) {
6168 _mesa_glsl_error(&loc
, state
, "subroutine index requires "
6169 "GL_ARB_explicit_uniform_location or "
6171 } else if (qual_index
>= MAX_SUBROUTINES
) {
6172 _mesa_glsl_error(&loc
, state
,
6173 "invalid subroutine index (%d) index must "
6174 "be a number between 0 and "
6175 "GL_MAX_SUBROUTINES - 1 (%d)", qual_index
,
6176 MAX_SUBROUTINES
- 1);
6178 f
->subroutine_index
= qual_index
;
6183 f
->num_subroutine_types
= this->return_type
->qualifier
.subroutine_list
->declarations
.length();
6184 f
->subroutine_types
= ralloc_array(state
, const struct glsl_type
*,
6185 f
->num_subroutine_types
);
6187 foreach_list_typed(ast_declaration
, decl
, link
, &this->return_type
->qualifier
.subroutine_list
->declarations
) {
6188 const struct glsl_type
*type
;
6189 /* the subroutine type must be already declared */
6190 type
= state
->symbols
->get_type(decl
->identifier
);
6192 _mesa_glsl_error(& loc
, state
, "unknown type '%s' in subroutine function definition", decl
->identifier
);
6195 for (int i
= 0; i
< state
->num_subroutine_types
; i
++) {
6196 ir_function
*fn
= state
->subroutine_types
[i
];
6197 ir_function_signature
*tsig
= NULL
;
6199 if (strcmp(fn
->name
, decl
->identifier
))
6202 tsig
= fn
->matching_signature(state
, &sig
->parameters
,
6205 _mesa_glsl_error(& loc
, state
, "subroutine type mismatch '%s' - signatures do not match\n", decl
->identifier
);
6207 if (tsig
->return_type
!= sig
->return_type
) {
6208 _mesa_glsl_error(& loc
, state
, "subroutine type mismatch '%s' - return types do not match\n", decl
->identifier
);
6212 f
->subroutine_types
[idx
++] = type
;
6214 state
->subroutines
= (ir_function
**)reralloc(state
, state
->subroutines
,
6216 state
->num_subroutines
+ 1);
6217 state
->subroutines
[state
->num_subroutines
] = f
;
6218 state
->num_subroutines
++;
6222 if (this->return_type
->qualifier
.is_subroutine_decl()) {
6223 if (!state
->symbols
->add_type(this->identifier
, glsl_type::get_subroutine_instance(this->identifier
))) {
6224 _mesa_glsl_error(& loc
, state
, "type '%s' previously defined", this->identifier
);
6227 state
->subroutine_types
= (ir_function
**)reralloc(state
, state
->subroutine_types
,
6229 state
->num_subroutine_types
+ 1);
6230 state
->subroutine_types
[state
->num_subroutine_types
] = f
;
6231 state
->num_subroutine_types
++;
6233 f
->is_subroutine
= true;
6236 /* Function declarations (prototypes) do not have r-values.
6243 ast_function_definition::hir(exec_list
*instructions
,
6244 struct _mesa_glsl_parse_state
*state
)
6246 prototype
->is_definition
= true;
6247 prototype
->hir(instructions
, state
);
6249 ir_function_signature
*signature
= prototype
->signature
;
6250 if (signature
== NULL
)
6253 assert(state
->current_function
== NULL
);
6254 state
->current_function
= signature
;
6255 state
->found_return
= false;
6256 state
->found_begin_interlock
= false;
6257 state
->found_end_interlock
= false;
6259 /* Duplicate parameters declared in the prototype as concrete variables.
6260 * Add these to the symbol table.
6262 state
->symbols
->push_scope();
6263 foreach_in_list(ir_variable
, var
, &signature
->parameters
) {
6264 assert(var
->as_variable() != NULL
);
6266 /* The only way a parameter would "exist" is if two parameters have
6269 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
6270 YYLTYPE loc
= this->get_location();
6272 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
6274 state
->symbols
->add_variable(var
);
6278 /* Convert the body of the function to HIR. */
6279 this->body
->hir(&signature
->body
, state
);
6280 signature
->is_defined
= true;
6282 state
->symbols
->pop_scope();
6284 assert(state
->current_function
== signature
);
6285 state
->current_function
= NULL
;
6287 if (!signature
->return_type
->is_void() && !state
->found_return
) {
6288 YYLTYPE loc
= this->get_location();
6289 _mesa_glsl_error(& loc
, state
, "function `%s' has non-void return type "
6290 "%s, but no return statement",
6291 signature
->function_name(),
6292 signature
->return_type
->name
);
6295 /* Function definitions do not have r-values.
6302 ast_jump_statement::hir(exec_list
*instructions
,
6303 struct _mesa_glsl_parse_state
*state
)
6310 assert(state
->current_function
);
6312 if (opt_return_value
) {
6313 ir_rvalue
*ret
= opt_return_value
->hir(instructions
, state
);
6315 /* The value of the return type can be NULL if the shader says
6316 * 'return foo();' and foo() is a function that returns void.
6318 * NOTE: The GLSL spec doesn't say that this is an error. The type
6319 * of the return value is void. If the return type of the function is
6320 * also void, then this should compile without error. Seriously.
6322 const glsl_type
*const ret_type
=
6323 (ret
== NULL
) ? glsl_type::void_type
: ret
->type
;
6325 /* Implicit conversions are not allowed for return values prior to
6326 * ARB_shading_language_420pack.
6328 if (state
->current_function
->return_type
!= ret_type
) {
6329 YYLTYPE loc
= this->get_location();
6331 if (state
->has_420pack()) {
6332 if (!apply_implicit_conversion(state
->current_function
->return_type
,
6334 || (ret
->type
!= state
->current_function
->return_type
)) {
6335 _mesa_glsl_error(& loc
, state
,
6336 "could not implicitly convert return value "
6337 "to %s, in function `%s'",
6338 state
->current_function
->return_type
->name
,
6339 state
->current_function
->function_name());
6342 _mesa_glsl_error(& loc
, state
,
6343 "`return' with wrong type %s, in function `%s' "
6346 state
->current_function
->function_name(),
6347 state
->current_function
->return_type
->name
);
6349 } else if (state
->current_function
->return_type
->base_type
==
6351 YYLTYPE loc
= this->get_location();
6353 /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
6354 * specs add a clarification:
6356 * "A void function can only use return without a return argument, even if
6357 * the return argument has void type. Return statements only accept values:
6360 * void func2() { return func1(); } // illegal return statement"
6362 _mesa_glsl_error(& loc
, state
,
6363 "void functions can only use `return' without a "
6367 inst
= new(ctx
) ir_return(ret
);
6369 if (state
->current_function
->return_type
->base_type
!=
6371 YYLTYPE loc
= this->get_location();
6373 _mesa_glsl_error(& loc
, state
,
6374 "`return' with no value, in function %s returning "
6376 state
->current_function
->function_name());
6378 inst
= new(ctx
) ir_return
;
6381 state
->found_return
= true;
6382 instructions
->push_tail(inst
);
6387 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
6388 YYLTYPE loc
= this->get_location();
6390 _mesa_glsl_error(& loc
, state
,
6391 "`discard' may only appear in a fragment shader");
6393 instructions
->push_tail(new(ctx
) ir_discard
);
6398 if (mode
== ast_continue
&&
6399 state
->loop_nesting_ast
== NULL
) {
6400 YYLTYPE loc
= this->get_location();
6402 _mesa_glsl_error(& loc
, state
, "continue may only appear in a loop");
6403 } else if (mode
== ast_break
&&
6404 state
->loop_nesting_ast
== NULL
&&
6405 state
->switch_state
.switch_nesting_ast
== NULL
) {
6406 YYLTYPE loc
= this->get_location();
6408 _mesa_glsl_error(& loc
, state
,
6409 "break may only appear in a loop or a switch");
6411 /* For a loop, inline the for loop expression again, since we don't
6412 * know where near the end of the loop body the normal copy of it is
6413 * going to be placed. Same goes for the condition for a do-while
6416 if (state
->loop_nesting_ast
!= NULL
&&
6417 mode
== ast_continue
&& !state
->switch_state
.is_switch_innermost
) {
6418 if (state
->loop_nesting_ast
->rest_expression
) {
6419 state
->loop_nesting_ast
->rest_expression
->hir(instructions
,
6422 if (state
->loop_nesting_ast
->mode
==
6423 ast_iteration_statement::ast_do_while
) {
6424 state
->loop_nesting_ast
->condition_to_hir(instructions
, state
);
6428 if (state
->switch_state
.is_switch_innermost
&&
6429 mode
== ast_continue
) {
6430 /* Set 'continue_inside' to true. */
6431 ir_rvalue
*const true_val
= new (ctx
) ir_constant(true);
6432 ir_dereference_variable
*deref_continue_inside_var
=
6433 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
6434 instructions
->push_tail(new(ctx
) ir_assignment(deref_continue_inside_var
,
6437 /* Break out from the switch, continue for the loop will
6438 * be called right after switch. */
6439 ir_loop_jump
*const jump
=
6440 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
6441 instructions
->push_tail(jump
);
6443 } else if (state
->switch_state
.is_switch_innermost
&&
6444 mode
== ast_break
) {
6445 /* Force break out of switch by inserting a break. */
6446 ir_loop_jump
*const jump
=
6447 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
6448 instructions
->push_tail(jump
);
6450 ir_loop_jump
*const jump
=
6451 new(ctx
) ir_loop_jump((mode
== ast_break
)
6452 ? ir_loop_jump::jump_break
6453 : ir_loop_jump::jump_continue
);
6454 instructions
->push_tail(jump
);
6461 /* Jump instructions do not have r-values.
6468 ast_selection_statement::hir(exec_list
*instructions
,
6469 struct _mesa_glsl_parse_state
*state
)
6473 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
6475 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
6477 * "Any expression whose type evaluates to a Boolean can be used as the
6478 * conditional expression bool-expression. Vector types are not accepted
6479 * as the expression to if."
6481 * The checks are separated so that higher quality diagnostics can be
6482 * generated for cases where both rules are violated.
6484 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
6485 YYLTYPE loc
= this->condition
->get_location();
6487 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
6491 ir_if
*const stmt
= new(ctx
) ir_if(condition
);
6493 if (then_statement
!= NULL
) {
6494 state
->symbols
->push_scope();
6495 then_statement
->hir(& stmt
->then_instructions
, state
);
6496 state
->symbols
->pop_scope();
6499 if (else_statement
!= NULL
) {
6500 state
->symbols
->push_scope();
6501 else_statement
->hir(& stmt
->else_instructions
, state
);
6502 state
->symbols
->pop_scope();
6505 instructions
->push_tail(stmt
);
6507 /* if-statements do not have r-values.
6514 /** Value of the case label. */
6517 /** Does this label occur after the default? */
6521 * AST for the case label.
6523 * This is only used to generate error messages for duplicate labels.
6525 ast_expression
*ast
;
6528 /* Used for detection of duplicate case values, compare
6529 * given contents directly.
6532 compare_case_value(const void *a
, const void *b
)
6534 return ((struct case_label
*) a
)->value
== ((struct case_label
*) b
)->value
;
6538 /* Used for detection of duplicate case values, just
6539 * returns key contents as is.
6542 key_contents(const void *key
)
6544 return ((struct case_label
*) key
)->value
;
6549 ast_switch_statement::hir(exec_list
*instructions
,
6550 struct _mesa_glsl_parse_state
*state
)
6554 ir_rvalue
*const test_expression
=
6555 this->test_expression
->hir(instructions
, state
);
6557 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
6559 * "The type of init-expression in a switch statement must be a
6562 if (!test_expression
->type
->is_scalar() ||
6563 !test_expression
->type
->is_integer_32()) {
6564 YYLTYPE loc
= this->test_expression
->get_location();
6566 _mesa_glsl_error(& loc
,
6568 "switch-statement expression must be scalar "
6573 /* Track the switch-statement nesting in a stack-like manner.
6575 struct glsl_switch_state saved
= state
->switch_state
;
6577 state
->switch_state
.is_switch_innermost
= true;
6578 state
->switch_state
.switch_nesting_ast
= this;
6579 state
->switch_state
.labels_ht
=
6580 _mesa_hash_table_create(NULL
, key_contents
,
6581 compare_case_value
);
6582 state
->switch_state
.previous_default
= NULL
;
6584 /* Initalize is_fallthru state to false.
6586 ir_rvalue
*const is_fallthru_val
= new (ctx
) ir_constant(false);
6587 state
->switch_state
.is_fallthru_var
=
6588 new(ctx
) ir_variable(glsl_type::bool_type
,
6589 "switch_is_fallthru_tmp",
6591 instructions
->push_tail(state
->switch_state
.is_fallthru_var
);
6593 ir_dereference_variable
*deref_is_fallthru_var
=
6594 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
6595 instructions
->push_tail(new(ctx
) ir_assignment(deref_is_fallthru_var
,
6598 /* Initialize continue_inside state to false.
6600 state
->switch_state
.continue_inside
=
6601 new(ctx
) ir_variable(glsl_type::bool_type
,
6602 "continue_inside_tmp",
6604 instructions
->push_tail(state
->switch_state
.continue_inside
);
6606 ir_rvalue
*const false_val
= new (ctx
) ir_constant(false);
6607 ir_dereference_variable
*deref_continue_inside_var
=
6608 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
6609 instructions
->push_tail(new(ctx
) ir_assignment(deref_continue_inside_var
,
6612 state
->switch_state
.run_default
=
6613 new(ctx
) ir_variable(glsl_type::bool_type
,
6616 instructions
->push_tail(state
->switch_state
.run_default
);
6618 /* Loop around the switch is used for flow control. */
6619 ir_loop
* loop
= new(ctx
) ir_loop();
6620 instructions
->push_tail(loop
);
6622 /* Cache test expression.
6624 test_to_hir(&loop
->body_instructions
, state
);
6626 /* Emit code for body of switch stmt.
6628 body
->hir(&loop
->body_instructions
, state
);
6630 /* Insert a break at the end to exit loop. */
6631 ir_loop_jump
*jump
= new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
6632 loop
->body_instructions
.push_tail(jump
);
6634 /* If we are inside loop, check if continue got called inside switch. */
6635 if (state
->loop_nesting_ast
!= NULL
) {
6636 ir_dereference_variable
*deref_continue_inside
=
6637 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
6638 ir_if
*irif
= new(ctx
) ir_if(deref_continue_inside
);
6639 ir_loop_jump
*jump
= new(ctx
) ir_loop_jump(ir_loop_jump::jump_continue
);
6641 if (state
->loop_nesting_ast
!= NULL
) {
6642 if (state
->loop_nesting_ast
->rest_expression
) {
6643 state
->loop_nesting_ast
->rest_expression
->hir(&irif
->then_instructions
,
6646 if (state
->loop_nesting_ast
->mode
==
6647 ast_iteration_statement::ast_do_while
) {
6648 state
->loop_nesting_ast
->condition_to_hir(&irif
->then_instructions
, state
);
6651 irif
->then_instructions
.push_tail(jump
);
6652 instructions
->push_tail(irif
);
6655 _mesa_hash_table_destroy(state
->switch_state
.labels_ht
, NULL
);
6657 state
->switch_state
= saved
;
6659 /* Switch statements do not have r-values. */
6665 ast_switch_statement::test_to_hir(exec_list
*instructions
,
6666 struct _mesa_glsl_parse_state
*state
)
6670 /* set to true to avoid a duplicate "use of uninitialized variable" warning
6671 * on the switch test case. The first one would be already raised when
6672 * getting the test_expression at ast_switch_statement::hir
6674 test_expression
->set_is_lhs(true);
6675 /* Cache value of test expression. */
6676 ir_rvalue
*const test_val
= test_expression
->hir(instructions
, state
);
6678 state
->switch_state
.test_var
= new(ctx
) ir_variable(test_val
->type
,
6681 ir_dereference_variable
*deref_test_var
=
6682 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
6684 instructions
->push_tail(state
->switch_state
.test_var
);
6685 instructions
->push_tail(new(ctx
) ir_assignment(deref_test_var
, test_val
));
6690 ast_switch_body::hir(exec_list
*instructions
,
6691 struct _mesa_glsl_parse_state
*state
)
6694 stmts
->hir(instructions
, state
);
6696 /* Switch bodies do not have r-values. */
6701 ast_case_statement_list::hir(exec_list
*instructions
,
6702 struct _mesa_glsl_parse_state
*state
)
6704 exec_list default_case
, after_default
, tmp
;
6706 foreach_list_typed (ast_case_statement
, case_stmt
, link
, & this->cases
) {
6707 case_stmt
->hir(&tmp
, state
);
6710 if (state
->switch_state
.previous_default
&& default_case
.is_empty()) {
6711 default_case
.append_list(&tmp
);
6715 /* If default case found, append 'after_default' list. */
6716 if (!default_case
.is_empty())
6717 after_default
.append_list(&tmp
);
6719 instructions
->append_list(&tmp
);
6722 /* Handle the default case. This is done here because default might not be
6723 * the last case. We need to add checks against following cases first to see
6724 * if default should be chosen or not.
6726 if (!default_case
.is_empty()) {
6727 ir_factory
body(instructions
, state
);
6729 ir_expression
*cmp
= NULL
;
6731 hash_table_foreach(state
->switch_state
.labels_ht
, entry
) {
6732 const struct case_label
*const l
= (struct case_label
*) entry
->data
;
6734 /* If the switch init-value is the value of one of the labels that
6735 * occurs after the default case, disable execution of the default
6738 if (l
->after_default
) {
6739 ir_constant
*const cnst
=
6740 state
->switch_state
.test_var
->type
->base_type
== GLSL_TYPE_UINT
6741 ? body
.constant(unsigned(l
->value
))
6742 : body
.constant(int(l
->value
));
6745 ? equal(cnst
, state
->switch_state
.test_var
)
6746 : logic_or(cmp
, equal(cnst
, state
->switch_state
.test_var
));
6751 body
.emit(assign(state
->switch_state
.run_default
, logic_not(cmp
)));
6753 body
.emit(assign(state
->switch_state
.run_default
, body
.constant(true)));
6755 /* Append default case and all cases after it. */
6756 instructions
->append_list(&default_case
);
6757 instructions
->append_list(&after_default
);
6760 /* Case statements do not have r-values. */
6765 ast_case_statement::hir(exec_list
*instructions
,
6766 struct _mesa_glsl_parse_state
*state
)
6768 labels
->hir(instructions
, state
);
6770 /* Guard case statements depending on fallthru state. */
6771 ir_dereference_variable
*const deref_fallthru_guard
=
6772 new(state
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
6773 ir_if
*const test_fallthru
= new(state
) ir_if(deref_fallthru_guard
);
6775 foreach_list_typed (ast_node
, stmt
, link
, & this->stmts
)
6776 stmt
->hir(& test_fallthru
->then_instructions
, state
);
6778 instructions
->push_tail(test_fallthru
);
6780 /* Case statements do not have r-values. */
6786 ast_case_label_list::hir(exec_list
*instructions
,
6787 struct _mesa_glsl_parse_state
*state
)
6789 foreach_list_typed (ast_case_label
, label
, link
, & this->labels
)
6790 label
->hir(instructions
, state
);
6792 /* Case labels do not have r-values. */
6797 ast_case_label::hir(exec_list
*instructions
,
6798 struct _mesa_glsl_parse_state
*state
)
6800 ir_factory
body(instructions
, state
);
6802 ir_variable
*const fallthru_var
= state
->switch_state
.is_fallthru_var
;
6804 /* If not default case, ... */
6805 if (this->test_value
!= NULL
) {
6806 /* Conditionally set fallthru state based on
6807 * comparison of cached test expression value to case label.
6809 ir_rvalue
*const label_rval
= this->test_value
->hir(instructions
, state
);
6810 ir_constant
*label_const
=
6811 label_rval
->constant_expression_value(body
.mem_ctx
);
6814 YYLTYPE loc
= this->test_value
->get_location();
6816 _mesa_glsl_error(& loc
, state
,
6817 "switch statement case label must be a "
6818 "constant expression");
6820 /* Stuff a dummy value in to allow processing to continue. */
6821 label_const
= body
.constant(0);
6824 _mesa_hash_table_search(state
->switch_state
.labels_ht
,
6825 &label_const
->value
.u
[0]);
6828 const struct case_label
*const l
=
6829 (struct case_label
*) entry
->data
;
6830 const ast_expression
*const previous_label
= l
->ast
;
6831 YYLTYPE loc
= this->test_value
->get_location();
6833 _mesa_glsl_error(& loc
, state
, "duplicate case value");
6835 loc
= previous_label
->get_location();
6836 _mesa_glsl_error(& loc
, state
, "this is the previous case label");
6838 struct case_label
*l
= ralloc(state
->switch_state
.labels_ht
,
6841 l
->value
= label_const
->value
.u
[0];
6842 l
->after_default
= state
->switch_state
.previous_default
!= NULL
;
6843 l
->ast
= this->test_value
;
6845 _mesa_hash_table_insert(state
->switch_state
.labels_ht
,
6846 &label_const
->value
.u
[0],
6851 /* Create an r-value version of the ir_constant label here (after we may
6852 * have created a fake one in error cases) that can be passed to
6853 * apply_implicit_conversion below.
6855 ir_rvalue
*label
= label_const
;
6857 ir_rvalue
*deref_test_var
=
6858 new(body
.mem_ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
6861 * From GLSL 4.40 specification section 6.2 ("Selection"):
6863 * "The type of the init-expression value in a switch statement must
6864 * be a scalar int or uint. The type of the constant-expression value
6865 * in a case label also must be a scalar int or uint. When any pair
6866 * of these values is tested for "equal value" and the types do not
6867 * match, an implicit conversion will be done to convert the int to a
6868 * uint (see section 4.1.10 “Implicit Conversions”) before the compare
6871 if (label
->type
!= state
->switch_state
.test_var
->type
) {
6872 YYLTYPE loc
= this->test_value
->get_location();
6874 const glsl_type
*type_a
= label
->type
;
6875 const glsl_type
*type_b
= state
->switch_state
.test_var
->type
;
6877 /* Check if int->uint implicit conversion is supported. */
6878 bool integer_conversion_supported
=
6879 glsl_type::int_type
->can_implicitly_convert_to(glsl_type::uint_type
,
6882 if ((!type_a
->is_integer_32() || !type_b
->is_integer_32()) ||
6883 !integer_conversion_supported
) {
6884 _mesa_glsl_error(&loc
, state
, "type mismatch with switch "
6885 "init-expression and case label (%s != %s)",
6886 type_a
->name
, type_b
->name
);
6888 /* Conversion of the case label. */
6889 if (type_a
->base_type
== GLSL_TYPE_INT
) {
6890 if (!apply_implicit_conversion(glsl_type::uint_type
,
6892 _mesa_glsl_error(&loc
, state
, "implicit type conversion error");
6894 /* Conversion of the init-expression value. */
6895 if (!apply_implicit_conversion(glsl_type::uint_type
,
6896 deref_test_var
, state
))
6897 _mesa_glsl_error(&loc
, state
, "implicit type conversion error");
6901 /* If the implicit conversion was allowed, the types will already be
6902 * the same. If the implicit conversion wasn't allowed, smash the
6903 * type of the label anyway. This will prevent the expression
6904 * constructor (below) from failing an assertion.
6906 label
->type
= deref_test_var
->type
;
6909 body
.emit(assign(fallthru_var
,
6910 logic_or(fallthru_var
, equal(label
, deref_test_var
))));
6911 } else { /* default case */
6912 if (state
->switch_state
.previous_default
) {
6913 YYLTYPE loc
= this->get_location();
6914 _mesa_glsl_error(& loc
, state
,
6915 "multiple default labels in one switch");
6917 loc
= state
->switch_state
.previous_default
->get_location();
6918 _mesa_glsl_error(& loc
, state
, "this is the first default label");
6920 state
->switch_state
.previous_default
= this;
6922 /* Set fallthru condition on 'run_default' bool. */
6923 body
.emit(assign(fallthru_var
,
6924 logic_or(fallthru_var
,
6925 state
->switch_state
.run_default
)));
6928 /* Case statements do not have r-values. */
6933 ast_iteration_statement::condition_to_hir(exec_list
*instructions
,
6934 struct _mesa_glsl_parse_state
*state
)
6938 if (condition
!= NULL
) {
6939 ir_rvalue
*const cond
=
6940 condition
->hir(instructions
, state
);
6943 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
6944 YYLTYPE loc
= condition
->get_location();
6946 _mesa_glsl_error(& loc
, state
,
6947 "loop condition must be scalar boolean");
6949 /* As the first code in the loop body, generate a block that looks
6950 * like 'if (!condition) break;' as the loop termination condition.
6952 ir_rvalue
*const not_cond
=
6953 new(ctx
) ir_expression(ir_unop_logic_not
, cond
);
6955 ir_if
*const if_stmt
= new(ctx
) ir_if(not_cond
);
6957 ir_jump
*const break_stmt
=
6958 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
6960 if_stmt
->then_instructions
.push_tail(break_stmt
);
6961 instructions
->push_tail(if_stmt
);
6968 ast_iteration_statement::hir(exec_list
*instructions
,
6969 struct _mesa_glsl_parse_state
*state
)
6973 /* For-loops and while-loops start a new scope, but do-while loops do not.
6975 if (mode
!= ast_do_while
)
6976 state
->symbols
->push_scope();
6978 if (init_statement
!= NULL
)
6979 init_statement
->hir(instructions
, state
);
6981 ir_loop
*const stmt
= new(ctx
) ir_loop();
6982 instructions
->push_tail(stmt
);
6984 /* Track the current loop nesting. */
6985 ast_iteration_statement
*nesting_ast
= state
->loop_nesting_ast
;
6987 state
->loop_nesting_ast
= this;
6989 /* Likewise, indicate that following code is closest to a loop,
6990 * NOT closest to a switch.
6992 bool saved_is_switch_innermost
= state
->switch_state
.is_switch_innermost
;
6993 state
->switch_state
.is_switch_innermost
= false;
6995 if (mode
!= ast_do_while
)
6996 condition_to_hir(&stmt
->body_instructions
, state
);
6999 body
->hir(& stmt
->body_instructions
, state
);
7001 if (rest_expression
!= NULL
)
7002 rest_expression
->hir(& stmt
->body_instructions
, state
);
7004 if (mode
== ast_do_while
)
7005 condition_to_hir(&stmt
->body_instructions
, state
);
7007 if (mode
!= ast_do_while
)
7008 state
->symbols
->pop_scope();
7010 /* Restore previous nesting before returning. */
7011 state
->loop_nesting_ast
= nesting_ast
;
7012 state
->switch_state
.is_switch_innermost
= saved_is_switch_innermost
;
7014 /* Loops do not have r-values.
7021 * Determine if the given type is valid for establishing a default precision
7024 * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
7026 * "The precision statement
7028 * precision precision-qualifier type;
7030 * can be used to establish a default precision qualifier. The type field
7031 * can be either int or float or any of the sampler types, and the
7032 * precision-qualifier can be lowp, mediump, or highp."
7034 * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
7035 * qualifiers on sampler types, but this seems like an oversight (since the
7036 * intention of including these in GLSL 1.30 is to allow compatibility with ES
7037 * shaders). So we allow int, float, and all sampler types regardless of GLSL
7041 is_valid_default_precision_type(const struct glsl_type
*const type
)
7046 switch (type
->base_type
) {
7048 case GLSL_TYPE_FLOAT
:
7049 /* "int" and "float" are valid, but vectors and matrices are not. */
7050 return type
->vector_elements
== 1 && type
->matrix_columns
== 1;
7051 case GLSL_TYPE_SAMPLER
:
7052 case GLSL_TYPE_IMAGE
:
7053 case GLSL_TYPE_ATOMIC_UINT
:
7062 ast_type_specifier::hir(exec_list
*instructions
,
7063 struct _mesa_glsl_parse_state
*state
)
7065 if (this->default_precision
== ast_precision_none
&& this->structure
== NULL
)
7068 YYLTYPE loc
= this->get_location();
7070 /* If this is a precision statement, check that the type to which it is
7071 * applied is either float or int.
7073 * From section 4.5.3 of the GLSL 1.30 spec:
7074 * "The precision statement
7075 * precision precision-qualifier type;
7076 * can be used to establish a default precision qualifier. The type
7077 * field can be either int or float [...]. Any other types or
7078 * qualifiers will result in an error.
7080 if (this->default_precision
!= ast_precision_none
) {
7081 if (!state
->check_precision_qualifiers_allowed(&loc
))
7084 if (this->structure
!= NULL
) {
7085 _mesa_glsl_error(&loc
, state
,
7086 "precision qualifiers do not apply to structures");
7090 if (this->array_specifier
!= NULL
) {
7091 _mesa_glsl_error(&loc
, state
,
7092 "default precision statements do not apply to "
7097 const struct glsl_type
*const type
=
7098 state
->symbols
->get_type(this->type_name
);
7099 if (!is_valid_default_precision_type(type
)) {
7100 _mesa_glsl_error(&loc
, state
,
7101 "default precision statements apply only to "
7102 "float, int, and opaque types");
7106 if (state
->es_shader
) {
7107 /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
7110 * "Non-precision qualified declarations will use the precision
7111 * qualifier specified in the most recent precision statement
7112 * that is still in scope. The precision statement has the same
7113 * scoping rules as variable declarations. If it is declared
7114 * inside a compound statement, its effect stops at the end of
7115 * the innermost statement it was declared in. Precision
7116 * statements in nested scopes override precision statements in
7117 * outer scopes. Multiple precision statements for the same basic
7118 * type can appear inside the same scope, with later statements
7119 * overriding earlier statements within that scope."
7121 * Default precision specifications follow the same scope rules as
7122 * variables. So, we can track the state of the default precision
7123 * qualifiers in the symbol table, and the rules will just work. This
7124 * is a slight abuse of the symbol table, but it has the semantics
7127 state
->symbols
->add_default_precision_qualifier(this->type_name
,
7128 this->default_precision
);
7131 /* FINISHME: Translate precision statements into IR. */
7135 /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
7136 * process_record_constructor() can do type-checking on C-style initializer
7137 * expressions of structs, but ast_struct_specifier should only be translated
7138 * to HIR if it is declaring the type of a structure.
7140 * The ->is_declaration field is false for initializers of variables
7141 * declared separately from the struct's type definition.
7143 * struct S { ... }; (is_declaration = true)
7144 * struct T { ... } t = { ... }; (is_declaration = true)
7145 * S s = { ... }; (is_declaration = false)
7147 if (this->structure
!= NULL
&& this->structure
->is_declaration
)
7148 return this->structure
->hir(instructions
, state
);
7155 * Process a structure or interface block tree into an array of structure fields
7157 * After parsing, where there are some syntax differnces, structures and
7158 * interface blocks are almost identical. They are similar enough that the
7159 * AST for each can be processed the same way into a set of
7160 * \c glsl_struct_field to describe the members.
7162 * If we're processing an interface block, var_mode should be the type of the
7163 * interface block (ir_var_shader_in, ir_var_shader_out, ir_var_uniform or
7164 * ir_var_shader_storage). If we're processing a structure, var_mode should be
7168 * The number of fields processed. A pointer to the array structure fields is
7169 * stored in \c *fields_ret.
7172 ast_process_struct_or_iface_block_members(exec_list
*instructions
,
7173 struct _mesa_glsl_parse_state
*state
,
7174 exec_list
*declarations
,
7175 glsl_struct_field
**fields_ret
,
7177 enum glsl_matrix_layout matrix_layout
,
7178 bool allow_reserved_names
,
7179 ir_variable_mode var_mode
,
7180 ast_type_qualifier
*layout
,
7181 unsigned block_stream
,
7182 unsigned block_xfb_buffer
,
7183 unsigned block_xfb_offset
,
7184 unsigned expl_location
,
7185 unsigned expl_align
)
7187 unsigned decl_count
= 0;
7188 unsigned next_offset
= 0;
7190 /* Make an initial pass over the list of fields to determine how
7191 * many there are. Each element in this list is an ast_declarator_list.
7192 * This means that we actually need to count the number of elements in the
7193 * 'declarations' list in each of the elements.
7195 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
7196 decl_count
+= decl_list
->declarations
.length();
7199 /* Allocate storage for the fields and process the field
7200 * declarations. As the declarations are processed, try to also convert
7201 * the types to HIR. This ensures that structure definitions embedded in
7202 * other structure definitions or in interface blocks are processed.
7204 glsl_struct_field
*const fields
= rzalloc_array(state
, glsl_struct_field
,
7207 bool first_member
= true;
7208 bool first_member_has_explicit_location
= false;
7211 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
7212 const char *type_name
;
7213 YYLTYPE loc
= decl_list
->get_location();
7215 decl_list
->type
->specifier
->hir(instructions
, state
);
7217 /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
7219 * "Anonymous structures are not supported; so embedded structures
7220 * must have a declarator. A name given to an embedded struct is
7221 * scoped at the same level as the struct it is embedded in."
7223 * The same section of the GLSL 1.20 spec says:
7225 * "Anonymous structures are not supported. Embedded structures are
7228 * The GLSL ES 1.00 and 3.00 specs have similar langauge. So, we allow
7229 * embedded structures in 1.10 only.
7231 if (state
->language_version
!= 110 &&
7232 decl_list
->type
->specifier
->structure
!= NULL
)
7233 _mesa_glsl_error(&loc
, state
,
7234 "embedded structure declarations are not allowed");
7236 const glsl_type
*decl_type
=
7237 decl_list
->type
->glsl_type(& type_name
, state
);
7239 const struct ast_type_qualifier
*const qual
=
7240 &decl_list
->type
->qualifier
;
7242 /* From section 4.3.9 of the GLSL 4.40 spec:
7244 * "[In interface blocks] opaque types are not allowed."
7246 * It should be impossible for decl_type to be NULL here. Cases that
7247 * might naturally lead to decl_type being NULL, especially for the
7248 * is_interface case, will have resulted in compilation having
7249 * already halted due to a syntax error.
7254 /* From section 4.3.7 of the ARB_bindless_texture spec:
7256 * "(remove the following bullet from the last list on p. 39,
7257 * thereby permitting sampler types in interface blocks; image
7258 * types are also permitted in blocks by this extension)"
7260 * * sampler types are not allowed
7262 if (decl_type
->contains_atomic() ||
7263 (!state
->has_bindless() && decl_type
->contains_opaque())) {
7264 _mesa_glsl_error(&loc
, state
, "uniform/buffer in non-default "
7265 "interface block contains %s variable",
7266 state
->has_bindless() ? "atomic" : "opaque");
7269 if (decl_type
->contains_atomic()) {
7270 /* From section 4.1.7.3 of the GLSL 4.40 spec:
7272 * "Members of structures cannot be declared as atomic counter
7275 _mesa_glsl_error(&loc
, state
, "atomic counter in structure");
7278 if (!state
->has_bindless() && decl_type
->contains_image()) {
7279 /* FINISHME: Same problem as with atomic counters.
7280 * FINISHME: Request clarification from Khronos and add
7281 * FINISHME: spec quotation here.
7283 _mesa_glsl_error(&loc
, state
, "image in structure");
7287 if (qual
->flags
.q
.explicit_binding
) {
7288 _mesa_glsl_error(&loc
, state
,
7289 "binding layout qualifier cannot be applied "
7290 "to struct or interface block members");
7294 if (!first_member
) {
7295 if (!layout
->flags
.q
.explicit_location
&&
7296 ((first_member_has_explicit_location
&&
7297 !qual
->flags
.q
.explicit_location
) ||
7298 (!first_member_has_explicit_location
&&
7299 qual
->flags
.q
.explicit_location
))) {
7300 _mesa_glsl_error(&loc
, state
,
7301 "when block-level location layout qualifier "
7302 "is not supplied either all members must "
7303 "have a location layout qualifier or all "
7304 "members must not have a location layout "
7308 first_member
= false;
7309 first_member_has_explicit_location
=
7310 qual
->flags
.q
.explicit_location
;
7314 if (qual
->flags
.q
.std140
||
7315 qual
->flags
.q
.std430
||
7316 qual
->flags
.q
.packed
||
7317 qual
->flags
.q
.shared
) {
7318 _mesa_glsl_error(&loc
, state
,
7319 "uniform/shader storage block layout qualifiers "
7320 "std140, std430, packed, and shared can only be "
7321 "applied to uniform/shader storage blocks, not "
7325 if (qual
->flags
.q
.constant
) {
7326 _mesa_glsl_error(&loc
, state
,
7327 "const storage qualifier cannot be applied "
7328 "to struct or interface block members");
7331 validate_memory_qualifier_for_type(state
, &loc
, qual
, decl_type
);
7332 validate_image_format_qualifier_for_type(state
, &loc
, qual
, decl_type
);
7334 /* From Section 4.4.2.3 (Geometry Outputs) of the GLSL 4.50 spec:
7336 * "A block member may be declared with a stream identifier, but
7337 * the specified stream must match the stream associated with the
7338 * containing block."
7340 if (qual
->flags
.q
.explicit_stream
) {
7341 unsigned qual_stream
;
7342 if (process_qualifier_constant(state
, &loc
, "stream",
7343 qual
->stream
, &qual_stream
) &&
7344 qual_stream
!= block_stream
) {
7345 _mesa_glsl_error(&loc
, state
, "stream layout qualifier on "
7346 "interface block member does not match "
7347 "the interface block (%u vs %u)", qual_stream
,
7353 unsigned explicit_xfb_buffer
= 0;
7354 if (qual
->flags
.q
.explicit_xfb_buffer
) {
7355 unsigned qual_xfb_buffer
;
7356 if (process_qualifier_constant(state
, &loc
, "xfb_buffer",
7357 qual
->xfb_buffer
, &qual_xfb_buffer
)) {
7358 explicit_xfb_buffer
= 1;
7359 if (qual_xfb_buffer
!= block_xfb_buffer
)
7360 _mesa_glsl_error(&loc
, state
, "xfb_buffer layout qualifier on "
7361 "interface block member does not match "
7362 "the interface block (%u vs %u)",
7363 qual_xfb_buffer
, block_xfb_buffer
);
7365 xfb_buffer
= (int) qual_xfb_buffer
;
7368 explicit_xfb_buffer
= layout
->flags
.q
.explicit_xfb_buffer
;
7369 xfb_buffer
= (int) block_xfb_buffer
;
7372 int xfb_stride
= -1;
7373 if (qual
->flags
.q
.explicit_xfb_stride
) {
7374 unsigned qual_xfb_stride
;
7375 if (process_qualifier_constant(state
, &loc
, "xfb_stride",
7376 qual
->xfb_stride
, &qual_xfb_stride
)) {
7377 xfb_stride
= (int) qual_xfb_stride
;
7381 if (qual
->flags
.q
.uniform
&& qual
->has_interpolation()) {
7382 _mesa_glsl_error(&loc
, state
,
7383 "interpolation qualifiers cannot be used "
7384 "with uniform interface blocks");
7387 if ((qual
->flags
.q
.uniform
|| !is_interface
) &&
7388 qual
->has_auxiliary_storage()) {
7389 _mesa_glsl_error(&loc
, state
,
7390 "auxiliary storage qualifiers cannot be used "
7391 "in uniform blocks or structures.");
7394 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
7395 if (!qual
->flags
.q
.uniform
&& !qual
->flags
.q
.buffer
) {
7396 _mesa_glsl_error(&loc
, state
,
7397 "row_major and column_major can only be "
7398 "applied to interface blocks");
7400 validate_matrix_layout_for_type(state
, &loc
, decl_type
, NULL
);
7403 foreach_list_typed (ast_declaration
, decl
, link
,
7404 &decl_list
->declarations
) {
7405 YYLTYPE loc
= decl
->get_location();
7407 if (!allow_reserved_names
)
7408 validate_identifier(decl
->identifier
, loc
, state
);
7410 const struct glsl_type
*field_type
=
7411 process_array_type(&loc
, decl_type
, decl
->array_specifier
, state
);
7412 validate_array_dimensions(field_type
, state
, &loc
);
7413 fields
[i
].type
= field_type
;
7414 fields
[i
].name
= decl
->identifier
;
7415 fields
[i
].interpolation
=
7416 interpret_interpolation_qualifier(qual
, field_type
,
7417 var_mode
, state
, &loc
);
7418 fields
[i
].centroid
= qual
->flags
.q
.centroid
? 1 : 0;
7419 fields
[i
].sample
= qual
->flags
.q
.sample
? 1 : 0;
7420 fields
[i
].patch
= qual
->flags
.q
.patch
? 1 : 0;
7421 fields
[i
].offset
= -1;
7422 fields
[i
].explicit_xfb_buffer
= explicit_xfb_buffer
;
7423 fields
[i
].xfb_buffer
= xfb_buffer
;
7424 fields
[i
].xfb_stride
= xfb_stride
;
7426 if (qual
->flags
.q
.explicit_location
) {
7427 unsigned qual_location
;
7428 if (process_qualifier_constant(state
, &loc
, "location",
7429 qual
->location
, &qual_location
)) {
7430 fields
[i
].location
= qual_location
+
7431 (fields
[i
].patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
);
7432 expl_location
= fields
[i
].location
+
7433 fields
[i
].type
->count_attribute_slots(false);
7436 if (layout
&& layout
->flags
.q
.explicit_location
) {
7437 fields
[i
].location
= expl_location
;
7438 expl_location
+= fields
[i
].type
->count_attribute_slots(false);
7440 fields
[i
].location
= -1;
7444 /* Offset can only be used with std430 and std140 layouts an initial
7445 * value of 0 is used for error detection.
7451 if (qual
->flags
.q
.row_major
||
7452 matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
7458 if(layout
->flags
.q
.std140
) {
7459 align
= field_type
->std140_base_alignment(row_major
);
7460 size
= field_type
->std140_size(row_major
);
7461 } else if (layout
->flags
.q
.std430
) {
7462 align
= field_type
->std430_base_alignment(row_major
);
7463 size
= field_type
->std430_size(row_major
);
7467 if (qual
->flags
.q
.explicit_offset
) {
7468 unsigned qual_offset
;
7469 if (process_qualifier_constant(state
, &loc
, "offset",
7470 qual
->offset
, &qual_offset
)) {
7471 if (align
!= 0 && size
!= 0) {
7472 if (next_offset
> qual_offset
)
7473 _mesa_glsl_error(&loc
, state
, "layout qualifier "
7474 "offset overlaps previous member");
7476 if (qual_offset
% align
) {
7477 _mesa_glsl_error(&loc
, state
, "layout qualifier offset "
7478 "must be a multiple of the base "
7479 "alignment of %s", field_type
->name
);
7481 fields
[i
].offset
= qual_offset
;
7482 next_offset
= qual_offset
+ size
;
7484 _mesa_glsl_error(&loc
, state
, "offset can only be used "
7485 "with std430 and std140 layouts");
7490 if (qual
->flags
.q
.explicit_align
|| expl_align
!= 0) {
7491 unsigned offset
= fields
[i
].offset
!= -1 ? fields
[i
].offset
:
7493 if (align
== 0 || size
== 0) {
7494 _mesa_glsl_error(&loc
, state
, "align can only be used with "
7495 "std430 and std140 layouts");
7496 } else if (qual
->flags
.q
.explicit_align
) {
7497 unsigned member_align
;
7498 if (process_qualifier_constant(state
, &loc
, "align",
7499 qual
->align
, &member_align
)) {
7500 if (member_align
== 0 ||
7501 member_align
& (member_align
- 1)) {
7502 _mesa_glsl_error(&loc
, state
, "align layout qualifier "
7503 "is not a power of 2");
7505 fields
[i
].offset
= glsl_align(offset
, member_align
);
7506 next_offset
= fields
[i
].offset
+ size
;
7510 fields
[i
].offset
= glsl_align(offset
, expl_align
);
7511 next_offset
= fields
[i
].offset
+ size
;
7513 } else if (!qual
->flags
.q
.explicit_offset
) {
7514 if (align
!= 0 && size
!= 0)
7515 next_offset
= glsl_align(next_offset
, align
) + size
;
7518 /* From the ARB_enhanced_layouts spec:
7520 * "The given offset applies to the first component of the first
7521 * member of the qualified entity. Then, within the qualified
7522 * entity, subsequent components are each assigned, in order, to
7523 * the next available offset aligned to a multiple of that
7524 * component's size. Aggregate types are flattened down to the
7525 * component level to get this sequence of components."
7527 if (qual
->flags
.q
.explicit_xfb_offset
) {
7528 unsigned xfb_offset
;
7529 if (process_qualifier_constant(state
, &loc
, "xfb_offset",
7530 qual
->offset
, &xfb_offset
)) {
7531 fields
[i
].offset
= xfb_offset
;
7532 block_xfb_offset
= fields
[i
].offset
+
7533 4 * field_type
->component_slots();
7536 if (layout
&& layout
->flags
.q
.explicit_xfb_offset
) {
7537 unsigned align
= field_type
->is_64bit() ? 8 : 4;
7538 fields
[i
].offset
= glsl_align(block_xfb_offset
, align
);
7539 block_xfb_offset
+= 4 * field_type
->component_slots();
7543 /* Propogate row- / column-major information down the fields of the
7544 * structure or interface block. Structures need this data because
7545 * the structure may contain a structure that contains ... a matrix
7546 * that need the proper layout.
7548 if (is_interface
&& layout
&&
7549 (layout
->flags
.q
.uniform
|| layout
->flags
.q
.buffer
) &&
7550 (field_type
->without_array()->is_matrix()
7551 || field_type
->without_array()->is_struct())) {
7552 /* If no layout is specified for the field, inherit the layout
7555 fields
[i
].matrix_layout
= matrix_layout
;
7557 if (qual
->flags
.q
.row_major
)
7558 fields
[i
].matrix_layout
= GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
7559 else if (qual
->flags
.q
.column_major
)
7560 fields
[i
].matrix_layout
= GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
;
7562 /* If we're processing an uniform or buffer block, the matrix
7563 * layout must be decided by this point.
7565 assert(fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
7566 || fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
);
7569 /* Memory qualifiers are allowed on buffer and image variables, while
7570 * the format qualifier is only accepted for images.
7572 if (var_mode
== ir_var_shader_storage
||
7573 field_type
->without_array()->is_image()) {
7574 /* For readonly and writeonly qualifiers the field definition,
7575 * if set, overwrites the layout qualifier.
7577 if (qual
->flags
.q
.read_only
|| qual
->flags
.q
.write_only
) {
7578 fields
[i
].memory_read_only
= qual
->flags
.q
.read_only
;
7579 fields
[i
].memory_write_only
= qual
->flags
.q
.write_only
;
7581 fields
[i
].memory_read_only
=
7582 layout
? layout
->flags
.q
.read_only
: 0;
7583 fields
[i
].memory_write_only
=
7584 layout
? layout
->flags
.q
.write_only
: 0;
7587 /* For other qualifiers, we set the flag if either the layout
7588 * qualifier or the field qualifier are set
7590 fields
[i
].memory_coherent
= qual
->flags
.q
.coherent
||
7591 (layout
&& layout
->flags
.q
.coherent
);
7592 fields
[i
].memory_volatile
= qual
->flags
.q
._volatile
||
7593 (layout
&& layout
->flags
.q
._volatile
);
7594 fields
[i
].memory_restrict
= qual
->flags
.q
.restrict_flag
||
7595 (layout
&& layout
->flags
.q
.restrict_flag
);
7597 if (field_type
->without_array()->is_image()) {
7598 if (qual
->flags
.q
.explicit_image_format
) {
7599 if (qual
->image_base_type
!=
7600 field_type
->without_array()->sampled_type
) {
7601 _mesa_glsl_error(&loc
, state
, "format qualifier doesn't "
7602 "match the base data type of the image");
7605 fields
[i
].image_format
= qual
->image_format
;
7607 if (!qual
->flags
.q
.write_only
) {
7608 _mesa_glsl_error(&loc
, state
, "image not qualified with "
7609 "`writeonly' must have a format layout "
7613 fields
[i
].image_format
= GL_NONE
;
7618 /* Precision qualifiers do not hold any meaning in Desktop GLSL */
7619 if (state
->es_shader
) {
7620 fields
[i
].precision
= select_gles_precision(qual
->precision
,
7625 fields
[i
].precision
= qual
->precision
;
7632 assert(i
== decl_count
);
7634 *fields_ret
= fields
;
7640 ast_struct_specifier::hir(exec_list
*instructions
,
7641 struct _mesa_glsl_parse_state
*state
)
7643 YYLTYPE loc
= this->get_location();
7645 unsigned expl_location
= 0;
7646 if (layout
&& layout
->flags
.q
.explicit_location
) {
7647 if (!process_qualifier_constant(state
, &loc
, "location",
7648 layout
->location
, &expl_location
)) {
7651 expl_location
= VARYING_SLOT_VAR0
+ expl_location
;
7655 glsl_struct_field
*fields
;
7656 unsigned decl_count
=
7657 ast_process_struct_or_iface_block_members(instructions
,
7659 &this->declarations
,
7662 GLSL_MATRIX_LAYOUT_INHERITED
,
7663 false /* allow_reserved_names */,
7666 0, /* for interface only */
7667 0, /* for interface only */
7668 0, /* for interface only */
7670 0 /* for interface only */);
7672 validate_identifier(this->name
, loc
, state
);
7674 type
= glsl_type::get_struct_instance(fields
, decl_count
, this->name
);
7676 if (!type
->is_anonymous() && !state
->symbols
->add_type(name
, type
)) {
7677 const glsl_type
*match
= state
->symbols
->get_type(name
);
7678 /* allow struct matching for desktop GL - older UE4 does this */
7679 if (match
!= NULL
&& state
->is_version(130, 0) && match
->record_compare(type
, true, false))
7680 _mesa_glsl_warning(& loc
, state
, "struct `%s' previously defined", name
);
7682 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
7684 const glsl_type
**s
= reralloc(state
, state
->user_structures
,
7686 state
->num_user_structures
+ 1);
7688 s
[state
->num_user_structures
] = type
;
7689 state
->user_structures
= s
;
7690 state
->num_user_structures
++;
7694 /* Structure type definitions do not have r-values.
7701 * Visitor class which detects whether a given interface block has been used.
7703 class interface_block_usage_visitor
: public ir_hierarchical_visitor
7706 interface_block_usage_visitor(ir_variable_mode mode
, const glsl_type
*block
)
7707 : mode(mode
), block(block
), found(false)
7711 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
7713 if (ir
->var
->data
.mode
== mode
&& ir
->var
->get_interface_type() == block
) {
7717 return visit_continue
;
7720 bool usage_found() const
7726 ir_variable_mode mode
;
7727 const glsl_type
*block
;
7732 is_unsized_array_last_element(ir_variable
*v
)
7734 const glsl_type
*interface_type
= v
->get_interface_type();
7735 int length
= interface_type
->length
;
7737 assert(v
->type
->is_unsized_array());
7739 /* Check if it is the last element of the interface */
7740 if (strcmp(interface_type
->fields
.structure
[length
-1].name
, v
->name
) == 0)
7746 apply_memory_qualifiers(ir_variable
*var
, glsl_struct_field field
)
7748 var
->data
.memory_read_only
= field
.memory_read_only
;
7749 var
->data
.memory_write_only
= field
.memory_write_only
;
7750 var
->data
.memory_coherent
= field
.memory_coherent
;
7751 var
->data
.memory_volatile
= field
.memory_volatile
;
7752 var
->data
.memory_restrict
= field
.memory_restrict
;
7756 ast_interface_block::hir(exec_list
*instructions
,
7757 struct _mesa_glsl_parse_state
*state
)
7759 YYLTYPE loc
= this->get_location();
7761 /* Interface blocks must be declared at global scope */
7762 if (state
->current_function
!= NULL
) {
7763 _mesa_glsl_error(&loc
, state
,
7764 "Interface block `%s' must be declared "
7769 /* Validate qualifiers:
7771 * - Layout Qualifiers as per the table in Section 4.4
7772 * ("Layout Qualifiers") of the GLSL 4.50 spec.
7774 * - Memory Qualifiers as per Section 4.10 ("Memory Qualifiers") of the
7777 * "Additionally, memory qualifiers may also be used in the declaration
7778 * of shader storage blocks"
7780 * Note the table in Section 4.4 says std430 is allowed on both uniform and
7781 * buffer blocks however Section 4.4.5 (Uniform and Shader Storage Block
7782 * Layout Qualifiers) of the GLSL 4.50 spec says:
7784 * "The std430 qualifier is supported only for shader storage blocks;
7785 * using std430 on a uniform block will result in a compile-time error."
7787 ast_type_qualifier allowed_blk_qualifiers
;
7788 allowed_blk_qualifiers
.flags
.i
= 0;
7789 if (this->layout
.flags
.q
.buffer
|| this->layout
.flags
.q
.uniform
) {
7790 allowed_blk_qualifiers
.flags
.q
.shared
= 1;
7791 allowed_blk_qualifiers
.flags
.q
.packed
= 1;
7792 allowed_blk_qualifiers
.flags
.q
.std140
= 1;
7793 allowed_blk_qualifiers
.flags
.q
.row_major
= 1;
7794 allowed_blk_qualifiers
.flags
.q
.column_major
= 1;
7795 allowed_blk_qualifiers
.flags
.q
.explicit_align
= 1;
7796 allowed_blk_qualifiers
.flags
.q
.explicit_binding
= 1;
7797 if (this->layout
.flags
.q
.buffer
) {
7798 allowed_blk_qualifiers
.flags
.q
.buffer
= 1;
7799 allowed_blk_qualifiers
.flags
.q
.std430
= 1;
7800 allowed_blk_qualifiers
.flags
.q
.coherent
= 1;
7801 allowed_blk_qualifiers
.flags
.q
._volatile
= 1;
7802 allowed_blk_qualifiers
.flags
.q
.restrict_flag
= 1;
7803 allowed_blk_qualifiers
.flags
.q
.read_only
= 1;
7804 allowed_blk_qualifiers
.flags
.q
.write_only
= 1;
7806 allowed_blk_qualifiers
.flags
.q
.uniform
= 1;
7809 /* Interface block */
7810 assert(this->layout
.flags
.q
.in
|| this->layout
.flags
.q
.out
);
7812 allowed_blk_qualifiers
.flags
.q
.explicit_location
= 1;
7813 if (this->layout
.flags
.q
.out
) {
7814 allowed_blk_qualifiers
.flags
.q
.out
= 1;
7815 if (state
->stage
== MESA_SHADER_GEOMETRY
||
7816 state
->stage
== MESA_SHADER_TESS_CTRL
||
7817 state
->stage
== MESA_SHADER_TESS_EVAL
||
7818 state
->stage
== MESA_SHADER_VERTEX
) {
7819 allowed_blk_qualifiers
.flags
.q
.explicit_xfb_offset
= 1;
7820 allowed_blk_qualifiers
.flags
.q
.explicit_xfb_buffer
= 1;
7821 allowed_blk_qualifiers
.flags
.q
.xfb_buffer
= 1;
7822 allowed_blk_qualifiers
.flags
.q
.explicit_xfb_stride
= 1;
7823 allowed_blk_qualifiers
.flags
.q
.xfb_stride
= 1;
7824 if (state
->stage
== MESA_SHADER_GEOMETRY
) {
7825 allowed_blk_qualifiers
.flags
.q
.stream
= 1;
7826 allowed_blk_qualifiers
.flags
.q
.explicit_stream
= 1;
7828 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
7829 allowed_blk_qualifiers
.flags
.q
.patch
= 1;
7833 allowed_blk_qualifiers
.flags
.q
.in
= 1;
7834 if (state
->stage
== MESA_SHADER_TESS_EVAL
) {
7835 allowed_blk_qualifiers
.flags
.q
.patch
= 1;
7840 this->layout
.validate_flags(&loc
, state
, allowed_blk_qualifiers
,
7841 "invalid qualifier for block",
7844 enum glsl_interface_packing packing
;
7845 if (this->layout
.flags
.q
.std140
) {
7846 packing
= GLSL_INTERFACE_PACKING_STD140
;
7847 } else if (this->layout
.flags
.q
.packed
) {
7848 packing
= GLSL_INTERFACE_PACKING_PACKED
;
7849 } else if (this->layout
.flags
.q
.std430
) {
7850 packing
= GLSL_INTERFACE_PACKING_STD430
;
7852 /* The default layout is shared.
7854 packing
= GLSL_INTERFACE_PACKING_SHARED
;
7857 ir_variable_mode var_mode
;
7858 const char *iface_type_name
;
7859 if (this->layout
.flags
.q
.in
) {
7860 var_mode
= ir_var_shader_in
;
7861 iface_type_name
= "in";
7862 } else if (this->layout
.flags
.q
.out
) {
7863 var_mode
= ir_var_shader_out
;
7864 iface_type_name
= "out";
7865 } else if (this->layout
.flags
.q
.uniform
) {
7866 var_mode
= ir_var_uniform
;
7867 iface_type_name
= "uniform";
7868 } else if (this->layout
.flags
.q
.buffer
) {
7869 var_mode
= ir_var_shader_storage
;
7870 iface_type_name
= "buffer";
7872 var_mode
= ir_var_auto
;
7873 iface_type_name
= "UNKNOWN";
7874 assert(!"interface block layout qualifier not found!");
7877 enum glsl_matrix_layout matrix_layout
= GLSL_MATRIX_LAYOUT_INHERITED
;
7878 if (this->layout
.flags
.q
.row_major
)
7879 matrix_layout
= GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
7880 else if (this->layout
.flags
.q
.column_major
)
7881 matrix_layout
= GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
;
7883 bool redeclaring_per_vertex
= strcmp(this->block_name
, "gl_PerVertex") == 0;
7884 exec_list declared_variables
;
7885 glsl_struct_field
*fields
;
7887 /* For blocks that accept memory qualifiers (i.e. shader storage), verify
7888 * that we don't have incompatible qualifiers
7890 if (this->layout
.flags
.q
.read_only
&& this->layout
.flags
.q
.write_only
) {
7891 _mesa_glsl_error(&loc
, state
,
7892 "Interface block sets both readonly and writeonly");
7895 unsigned qual_stream
;
7896 if (!process_qualifier_constant(state
, &loc
, "stream", this->layout
.stream
,
7898 !validate_stream_qualifier(&loc
, state
, qual_stream
)) {
7899 /* If the stream qualifier is invalid it doesn't make sense to continue
7900 * on and try to compare stream layouts on member variables against it
7901 * so just return early.
7906 unsigned qual_xfb_buffer
;
7907 if (!process_qualifier_constant(state
, &loc
, "xfb_buffer",
7908 layout
.xfb_buffer
, &qual_xfb_buffer
) ||
7909 !validate_xfb_buffer_qualifier(&loc
, state
, qual_xfb_buffer
)) {
7913 unsigned qual_xfb_offset
;
7914 if (layout
.flags
.q
.explicit_xfb_offset
) {
7915 if (!process_qualifier_constant(state
, &loc
, "xfb_offset",
7916 layout
.offset
, &qual_xfb_offset
)) {
7921 unsigned qual_xfb_stride
;
7922 if (layout
.flags
.q
.explicit_xfb_stride
) {
7923 if (!process_qualifier_constant(state
, &loc
, "xfb_stride",
7924 layout
.xfb_stride
, &qual_xfb_stride
)) {
7929 unsigned expl_location
= 0;
7930 if (layout
.flags
.q
.explicit_location
) {
7931 if (!process_qualifier_constant(state
, &loc
, "location",
7932 layout
.location
, &expl_location
)) {
7935 expl_location
+= this->layout
.flags
.q
.patch
? VARYING_SLOT_PATCH0
7936 : VARYING_SLOT_VAR0
;
7940 unsigned expl_align
= 0;
7941 if (layout
.flags
.q
.explicit_align
) {
7942 if (!process_qualifier_constant(state
, &loc
, "align",
7943 layout
.align
, &expl_align
)) {
7946 if (expl_align
== 0 || expl_align
& (expl_align
- 1)) {
7947 _mesa_glsl_error(&loc
, state
, "align layout qualifier is not a "
7954 unsigned int num_variables
=
7955 ast_process_struct_or_iface_block_members(&declared_variables
,
7957 &this->declarations
,
7961 redeclaring_per_vertex
,
7970 if (!redeclaring_per_vertex
) {
7971 validate_identifier(this->block_name
, loc
, state
);
7973 /* From section 4.3.9 ("Interface Blocks") of the GLSL 4.50 spec:
7975 * "Block names have no other use within a shader beyond interface
7976 * matching; it is a compile-time error to use a block name at global
7977 * scope for anything other than as a block name."
7979 ir_variable
*var
= state
->symbols
->get_variable(this->block_name
);
7980 if (var
&& !var
->type
->is_interface()) {
7981 _mesa_glsl_error(&loc
, state
, "Block name `%s' is "
7982 "already used in the scope.",
7987 const glsl_type
*earlier_per_vertex
= NULL
;
7988 if (redeclaring_per_vertex
) {
7989 /* Find the previous declaration of gl_PerVertex. If we're redeclaring
7990 * the named interface block gl_in, we can find it by looking at the
7991 * previous declaration of gl_in. Otherwise we can find it by looking
7992 * at the previous decalartion of any of the built-in outputs,
7995 * Also check that the instance name and array-ness of the redeclaration
7999 case ir_var_shader_in
:
8000 if (ir_variable
*earlier_gl_in
=
8001 state
->symbols
->get_variable("gl_in")) {
8002 earlier_per_vertex
= earlier_gl_in
->get_interface_type();
8004 _mesa_glsl_error(&loc
, state
,
8005 "redeclaration of gl_PerVertex input not allowed "
8007 _mesa_shader_stage_to_string(state
->stage
));
8009 if (this->instance_name
== NULL
||
8010 strcmp(this->instance_name
, "gl_in") != 0 || this->array_specifier
== NULL
||
8011 !this->array_specifier
->is_single_dimension()) {
8012 _mesa_glsl_error(&loc
, state
,
8013 "gl_PerVertex input must be redeclared as "
8017 case ir_var_shader_out
:
8018 if (ir_variable
*earlier_gl_Position
=
8019 state
->symbols
->get_variable("gl_Position")) {
8020 earlier_per_vertex
= earlier_gl_Position
->get_interface_type();
8021 } else if (ir_variable
*earlier_gl_out
=
8022 state
->symbols
->get_variable("gl_out")) {
8023 earlier_per_vertex
= earlier_gl_out
->get_interface_type();
8025 _mesa_glsl_error(&loc
, state
,
8026 "redeclaration of gl_PerVertex output not "
8027 "allowed in the %s shader",
8028 _mesa_shader_stage_to_string(state
->stage
));
8030 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
8031 if (this->instance_name
== NULL
||
8032 strcmp(this->instance_name
, "gl_out") != 0 || this->array_specifier
== NULL
) {
8033 _mesa_glsl_error(&loc
, state
,
8034 "gl_PerVertex output must be redeclared as "
8038 if (this->instance_name
!= NULL
) {
8039 _mesa_glsl_error(&loc
, state
,
8040 "gl_PerVertex output may not be redeclared with "
8041 "an instance name");
8046 _mesa_glsl_error(&loc
, state
,
8047 "gl_PerVertex must be declared as an input or an "
8052 if (earlier_per_vertex
== NULL
) {
8053 /* An error has already been reported. Bail out to avoid null
8054 * dereferences later in this function.
8059 /* Copy locations from the old gl_PerVertex interface block. */
8060 for (unsigned i
= 0; i
< num_variables
; i
++) {
8061 int j
= earlier_per_vertex
->field_index(fields
[i
].name
);
8063 _mesa_glsl_error(&loc
, state
,
8064 "redeclaration of gl_PerVertex must be a subset "
8065 "of the built-in members of gl_PerVertex");
8067 fields
[i
].location
=
8068 earlier_per_vertex
->fields
.structure
[j
].location
;
8070 earlier_per_vertex
->fields
.structure
[j
].offset
;
8071 fields
[i
].interpolation
=
8072 earlier_per_vertex
->fields
.structure
[j
].interpolation
;
8073 fields
[i
].centroid
=
8074 earlier_per_vertex
->fields
.structure
[j
].centroid
;
8076 earlier_per_vertex
->fields
.structure
[j
].sample
;
8078 earlier_per_vertex
->fields
.structure
[j
].patch
;
8079 fields
[i
].precision
=
8080 earlier_per_vertex
->fields
.structure
[j
].precision
;
8081 fields
[i
].explicit_xfb_buffer
=
8082 earlier_per_vertex
->fields
.structure
[j
].explicit_xfb_buffer
;
8083 fields
[i
].xfb_buffer
=
8084 earlier_per_vertex
->fields
.structure
[j
].xfb_buffer
;
8085 fields
[i
].xfb_stride
=
8086 earlier_per_vertex
->fields
.structure
[j
].xfb_stride
;
8090 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10
8093 * If a built-in interface block is redeclared, it must appear in
8094 * the shader before any use of any member included in the built-in
8095 * declaration, or a compilation error will result.
8097 * This appears to be a clarification to the behaviour established for
8098 * gl_PerVertex by GLSL 1.50, therefore we implement this behaviour
8099 * regardless of GLSL version.
8101 interface_block_usage_visitor
v(var_mode
, earlier_per_vertex
);
8102 v
.run(instructions
);
8103 if (v
.usage_found()) {
8104 _mesa_glsl_error(&loc
, state
,
8105 "redeclaration of a built-in interface block must "
8106 "appear before any use of any member of the "
8111 const glsl_type
*block_type
=
8112 glsl_type::get_interface_instance(fields
,
8116 GLSL_MATRIX_LAYOUT_ROW_MAJOR
,
8119 unsigned component_size
= block_type
->contains_double() ? 8 : 4;
8121 layout
.flags
.q
.explicit_xfb_offset
? (int) qual_xfb_offset
: -1;
8122 validate_xfb_offset_qualifier(&loc
, state
, xfb_offset
, block_type
,
8125 if (!state
->symbols
->add_interface(block_type
->name
, block_type
, var_mode
)) {
8126 YYLTYPE loc
= this->get_location();
8127 _mesa_glsl_error(&loc
, state
, "interface block `%s' with type `%s' "
8128 "already taken in the current scope",
8129 this->block_name
, iface_type_name
);
8132 /* Since interface blocks cannot contain statements, it should be
8133 * impossible for the block to generate any instructions.
8135 assert(declared_variables
.is_empty());
8137 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
8139 * Geometry shader input variables get the per-vertex values written
8140 * out by vertex shader output variables of the same names. Since a
8141 * geometry shader operates on a set of vertices, each input varying
8142 * variable (or input block, see interface blocks below) needs to be
8143 * declared as an array.
8145 if (state
->stage
== MESA_SHADER_GEOMETRY
&& this->array_specifier
== NULL
&&
8146 var_mode
== ir_var_shader_in
) {
8147 _mesa_glsl_error(&loc
, state
, "geometry shader inputs must be arrays");
8148 } else if ((state
->stage
== MESA_SHADER_TESS_CTRL
||
8149 state
->stage
== MESA_SHADER_TESS_EVAL
) &&
8150 !this->layout
.flags
.q
.patch
&&
8151 this->array_specifier
== NULL
&&
8152 var_mode
== ir_var_shader_in
) {
8153 _mesa_glsl_error(&loc
, state
, "per-vertex tessellation shader inputs must be arrays");
8154 } else if (state
->stage
== MESA_SHADER_TESS_CTRL
&&
8155 !this->layout
.flags
.q
.patch
&&
8156 this->array_specifier
== NULL
&&
8157 var_mode
== ir_var_shader_out
) {
8158 _mesa_glsl_error(&loc
, state
, "tessellation control shader outputs must be arrays");
8162 /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
8165 * "If an instance name (instance-name) is used, then it puts all the
8166 * members inside a scope within its own name space, accessed with the
8167 * field selector ( . ) operator (analogously to structures)."
8169 if (this->instance_name
) {
8170 if (redeclaring_per_vertex
) {
8171 /* When a built-in in an unnamed interface block is redeclared,
8172 * get_variable_being_redeclared() calls
8173 * check_builtin_array_max_size() to make sure that built-in array
8174 * variables aren't redeclared to illegal sizes. But we're looking
8175 * at a redeclaration of a named built-in interface block. So we
8176 * have to manually call check_builtin_array_max_size() for all parts
8177 * of the interface that are arrays.
8179 for (unsigned i
= 0; i
< num_variables
; i
++) {
8180 if (fields
[i
].type
->is_array()) {
8181 const unsigned size
= fields
[i
].type
->array_size();
8182 check_builtin_array_max_size(fields
[i
].name
, size
, loc
, state
);
8186 validate_identifier(this->instance_name
, loc
, state
);
8191 if (this->array_specifier
!= NULL
) {
8192 const glsl_type
*block_array_type
=
8193 process_array_type(&loc
, block_type
, this->array_specifier
, state
);
8195 /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
8197 * For uniform blocks declared an array, each individual array
8198 * element corresponds to a separate buffer object backing one
8199 * instance of the block. As the array size indicates the number
8200 * of buffer objects needed, uniform block array declarations
8201 * must specify an array size.
8203 * And a few paragraphs later:
8205 * Geometry shader input blocks must be declared as arrays and
8206 * follow the array declaration and linking rules for all
8207 * geometry shader inputs. All other input and output block
8208 * arrays must specify an array size.
8210 * The same applies to tessellation shaders.
8212 * The upshot of this is that the only circumstance where an
8213 * interface array size *doesn't* need to be specified is on a
8214 * geometry shader input, tessellation control shader input,
8215 * tessellation control shader output, and tessellation evaluation
8218 if (block_array_type
->is_unsized_array()) {
8219 bool allow_inputs
= state
->stage
== MESA_SHADER_GEOMETRY
||
8220 state
->stage
== MESA_SHADER_TESS_CTRL
||
8221 state
->stage
== MESA_SHADER_TESS_EVAL
;
8222 bool allow_outputs
= state
->stage
== MESA_SHADER_TESS_CTRL
;
8224 if (this->layout
.flags
.q
.in
) {
8226 _mesa_glsl_error(&loc
, state
,
8227 "unsized input block arrays not allowed in "
8229 _mesa_shader_stage_to_string(state
->stage
));
8230 } else if (this->layout
.flags
.q
.out
) {
8232 _mesa_glsl_error(&loc
, state
,
8233 "unsized output block arrays not allowed in "
8235 _mesa_shader_stage_to_string(state
->stage
));
8237 /* by elimination, this is a uniform block array */
8238 _mesa_glsl_error(&loc
, state
,
8239 "unsized uniform block arrays not allowed in "
8241 _mesa_shader_stage_to_string(state
->stage
));
8245 /* From section 4.3.9 (Interface Blocks) of the GLSL ES 3.10 spec:
8247 * * Arrays of arrays of blocks are not allowed
8249 if (state
->es_shader
&& block_array_type
->is_array() &&
8250 block_array_type
->fields
.array
->is_array()) {
8251 _mesa_glsl_error(&loc
, state
,
8252 "arrays of arrays interface blocks are "
8256 var
= new(state
) ir_variable(block_array_type
,
8257 this->instance_name
,
8260 var
= new(state
) ir_variable(block_type
,
8261 this->instance_name
,
8265 var
->data
.matrix_layout
= matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
8266 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
: matrix_layout
;
8268 if (var_mode
== ir_var_shader_in
|| var_mode
== ir_var_uniform
)
8269 var
->data
.read_only
= true;
8271 var
->data
.patch
= this->layout
.flags
.q
.patch
;
8273 if (state
->stage
== MESA_SHADER_GEOMETRY
&& var_mode
== ir_var_shader_in
)
8274 handle_geometry_shader_input_decl(state
, loc
, var
);
8275 else if ((state
->stage
== MESA_SHADER_TESS_CTRL
||
8276 state
->stage
== MESA_SHADER_TESS_EVAL
) && var_mode
== ir_var_shader_in
)
8277 handle_tess_shader_input_decl(state
, loc
, var
);
8278 else if (state
->stage
== MESA_SHADER_TESS_CTRL
&& var_mode
== ir_var_shader_out
)
8279 handle_tess_ctrl_shader_output_decl(state
, loc
, var
);
8281 for (unsigned i
= 0; i
< num_variables
; i
++) {
8282 if (var
->data
.mode
== ir_var_shader_storage
)
8283 apply_memory_qualifiers(var
, fields
[i
]);
8286 if (ir_variable
*earlier
=
8287 state
->symbols
->get_variable(this->instance_name
)) {
8288 if (!redeclaring_per_vertex
) {
8289 _mesa_glsl_error(&loc
, state
, "`%s' redeclared",
8290 this->instance_name
);
8292 earlier
->data
.how_declared
= ir_var_declared_normally
;
8293 earlier
->type
= var
->type
;
8294 earlier
->reinit_interface_type(block_type
);
8297 if (this->layout
.flags
.q
.explicit_binding
) {
8298 apply_explicit_binding(state
, &loc
, var
, var
->type
,
8302 var
->data
.stream
= qual_stream
;
8303 if (layout
.flags
.q
.explicit_location
) {
8304 var
->data
.location
= expl_location
;
8305 var
->data
.explicit_location
= true;
8308 state
->symbols
->add_variable(var
);
8309 instructions
->push_tail(var
);
8312 /* In order to have an array size, the block must also be declared with
8315 assert(this->array_specifier
== NULL
);
8317 for (unsigned i
= 0; i
< num_variables
; i
++) {
8319 new(state
) ir_variable(fields
[i
].type
,
8320 ralloc_strdup(state
, fields
[i
].name
),
8322 var
->data
.interpolation
= fields
[i
].interpolation
;
8323 var
->data
.centroid
= fields
[i
].centroid
;
8324 var
->data
.sample
= fields
[i
].sample
;
8325 var
->data
.patch
= fields
[i
].patch
;
8326 var
->data
.stream
= qual_stream
;
8327 var
->data
.location
= fields
[i
].location
;
8329 if (fields
[i
].location
!= -1)
8330 var
->data
.explicit_location
= true;
8332 var
->data
.explicit_xfb_buffer
= fields
[i
].explicit_xfb_buffer
;
8333 var
->data
.xfb_buffer
= fields
[i
].xfb_buffer
;
8335 if (fields
[i
].offset
!= -1)
8336 var
->data
.explicit_xfb_offset
= true;
8337 var
->data
.offset
= fields
[i
].offset
;
8339 var
->init_interface_type(block_type
);
8341 if (var_mode
== ir_var_shader_in
|| var_mode
== ir_var_uniform
)
8342 var
->data
.read_only
= true;
8344 /* Precision qualifiers do not have any meaning in Desktop GLSL */
8345 if (state
->es_shader
) {
8346 var
->data
.precision
=
8347 select_gles_precision(fields
[i
].precision
, fields
[i
].type
,
8351 if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
) {
8352 var
->data
.matrix_layout
= matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
8353 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
: matrix_layout
;
8355 var
->data
.matrix_layout
= fields
[i
].matrix_layout
;
8358 if (var
->data
.mode
== ir_var_shader_storage
)
8359 apply_memory_qualifiers(var
, fields
[i
]);
8361 /* Examine var name here since var may get deleted in the next call */
8362 bool var_is_gl_id
= is_gl_identifier(var
->name
);
8364 if (redeclaring_per_vertex
) {
8365 bool is_redeclaration
;
8367 get_variable_being_redeclared(&var
, loc
, state
,
8368 true /* allow_all_redeclarations */,
8370 if (!var_is_gl_id
|| !is_redeclaration
) {
8371 _mesa_glsl_error(&loc
, state
,
8372 "redeclaration of gl_PerVertex can only "
8373 "include built-in variables");
8374 } else if (var
->data
.how_declared
== ir_var_declared_normally
) {
8375 _mesa_glsl_error(&loc
, state
,
8376 "`%s' has already been redeclared",
8379 var
->data
.how_declared
= ir_var_declared_in_block
;
8380 var
->reinit_interface_type(block_type
);
8385 if (state
->symbols
->get_variable(var
->name
) != NULL
)
8386 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
8388 /* Propagate the "binding" keyword into this UBO/SSBO's fields.
8389 * The UBO declaration itself doesn't get an ir_variable unless it
8390 * has an instance name. This is ugly.
8392 if (this->layout
.flags
.q
.explicit_binding
) {
8393 apply_explicit_binding(state
, &loc
, var
,
8394 var
->get_interface_type(), &this->layout
);
8397 if (var
->type
->is_unsized_array()) {
8398 if (var
->is_in_shader_storage_block() &&
8399 is_unsized_array_last_element(var
)) {
8400 var
->data
.from_ssbo_unsized_array
= true;
8402 /* From GLSL ES 3.10 spec, section 4.1.9 "Arrays":
8404 * "If an array is declared as the last member of a shader storage
8405 * block and the size is not specified at compile-time, it is
8406 * sized at run-time. In all other cases, arrays are sized only
8409 * In desktop GLSL it is allowed to have unsized-arrays that are
8410 * not last, as long as we can determine that they are implicitly
8413 if (state
->es_shader
) {
8414 _mesa_glsl_error(&loc
, state
, "unsized array `%s' "
8415 "definition: only last member of a shader "
8416 "storage block can be defined as unsized "
8417 "array", fields
[i
].name
);
8422 state
->symbols
->add_variable(var
);
8423 instructions
->push_tail(var
);
8426 if (redeclaring_per_vertex
&& block_type
!= earlier_per_vertex
) {
8427 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec:
8429 * It is also a compilation error ... to redeclare a built-in
8430 * block and then use a member from that built-in block that was
8431 * not included in the redeclaration.
8433 * This appears to be a clarification to the behaviour established
8434 * for gl_PerVertex by GLSL 1.50, therefore we implement this
8435 * behaviour regardless of GLSL version.
8437 * To prevent the shader from using a member that was not included in
8438 * the redeclaration, we disable any ir_variables that are still
8439 * associated with the old declaration of gl_PerVertex (since we've
8440 * already updated all of the variables contained in the new
8441 * gl_PerVertex to point to it).
8443 * As a side effect this will prevent
8444 * validate_intrastage_interface_blocks() from getting confused and
8445 * thinking there are conflicting definitions of gl_PerVertex in the
8448 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
8449 ir_variable
*const var
= node
->as_variable();
8451 var
->get_interface_type() == earlier_per_vertex
&&
8452 var
->data
.mode
== var_mode
) {
8453 if (var
->data
.how_declared
== ir_var_declared_normally
) {
8454 _mesa_glsl_error(&loc
, state
,
8455 "redeclaration of gl_PerVertex cannot "
8456 "follow a redeclaration of `%s'",
8459 state
->symbols
->disable_variable(var
->name
);
8471 ast_tcs_output_layout::hir(exec_list
*instructions
,
8472 struct _mesa_glsl_parse_state
*state
)
8474 YYLTYPE loc
= this->get_location();
8476 unsigned num_vertices
;
8477 if (!state
->out_qualifier
->vertices
->
8478 process_qualifier_constant(state
, "vertices", &num_vertices
,
8480 /* return here to stop cascading incorrect error messages */
8484 /* If any shader outputs occurred before this declaration and specified an
8485 * array size, make sure the size they specified is consistent with the
8488 if (state
->tcs_output_size
!= 0 && state
->tcs_output_size
!= num_vertices
) {
8489 _mesa_glsl_error(&loc
, state
,
8490 "this tessellation control shader output layout "
8491 "specifies %u vertices, but a previous output "
8492 "is declared with size %u",
8493 num_vertices
, state
->tcs_output_size
);
8497 state
->tcs_output_vertices_specified
= true;
8499 /* If any shader outputs occurred before this declaration and did not
8500 * specify an array size, their size is determined now.
8502 foreach_in_list (ir_instruction
, node
, instructions
) {
8503 ir_variable
*var
= node
->as_variable();
8504 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_out
)
8507 /* Note: Not all tessellation control shader output are arrays. */
8508 if (!var
->type
->is_unsized_array() || var
->data
.patch
)
8511 if (var
->data
.max_array_access
>= (int)num_vertices
) {
8512 _mesa_glsl_error(&loc
, state
,
8513 "this tessellation control shader output layout "
8514 "specifies %u vertices, but an access to element "
8515 "%u of output `%s' already exists", num_vertices
,
8516 var
->data
.max_array_access
, var
->name
);
8518 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
8528 ast_gs_input_layout::hir(exec_list
*instructions
,
8529 struct _mesa_glsl_parse_state
*state
)
8531 YYLTYPE loc
= this->get_location();
8533 /* Should have been prevented by the parser. */
8534 assert(!state
->gs_input_prim_type_specified
8535 || state
->in_qualifier
->prim_type
== this->prim_type
);
8537 /* If any shader inputs occurred before this declaration and specified an
8538 * array size, make sure the size they specified is consistent with the
8541 unsigned num_vertices
= vertices_per_prim(this->prim_type
);
8542 if (state
->gs_input_size
!= 0 && state
->gs_input_size
!= num_vertices
) {
8543 _mesa_glsl_error(&loc
, state
,
8544 "this geometry shader input layout implies %u vertices"
8545 " per primitive, but a previous input is declared"
8546 " with size %u", num_vertices
, state
->gs_input_size
);
8550 state
->gs_input_prim_type_specified
= true;
8552 /* If any shader inputs occurred before this declaration and did not
8553 * specify an array size, their size is determined now.
8555 foreach_in_list(ir_instruction
, node
, instructions
) {
8556 ir_variable
*var
= node
->as_variable();
8557 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_in
)
8560 /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
8564 if (var
->type
->is_unsized_array()) {
8565 if (var
->data
.max_array_access
>= (int)num_vertices
) {
8566 _mesa_glsl_error(&loc
, state
,
8567 "this geometry shader input layout implies %u"
8568 " vertices, but an access to element %u of input"
8569 " `%s' already exists", num_vertices
,
8570 var
->data
.max_array_access
, var
->name
);
8572 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
8583 ast_cs_input_layout::hir(exec_list
*instructions
,
8584 struct _mesa_glsl_parse_state
*state
)
8586 YYLTYPE loc
= this->get_location();
8588 /* From the ARB_compute_shader specification:
8590 * If the local size of the shader in any dimension is greater
8591 * than the maximum size supported by the implementation for that
8592 * dimension, a compile-time error results.
8594 * It is not clear from the spec how the error should be reported if
8595 * the total size of the work group exceeds
8596 * MAX_COMPUTE_WORK_GROUP_INVOCATIONS, but it seems reasonable to
8597 * report it at compile time as well.
8599 GLuint64 total_invocations
= 1;
8600 unsigned qual_local_size
[3];
8601 for (int i
= 0; i
< 3; i
++) {
8603 char *local_size_str
= ralloc_asprintf(NULL
, "invalid local_size_%c",
8605 /* Infer a local_size of 1 for unspecified dimensions */
8606 if (this->local_size
[i
] == NULL
) {
8607 qual_local_size
[i
] = 1;
8608 } else if (!this->local_size
[i
]->
8609 process_qualifier_constant(state
, local_size_str
,
8610 &qual_local_size
[i
], false)) {
8611 ralloc_free(local_size_str
);
8614 ralloc_free(local_size_str
);
8616 if (qual_local_size
[i
] > state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]) {
8617 _mesa_glsl_error(&loc
, state
,
8618 "local_size_%c exceeds MAX_COMPUTE_WORK_GROUP_SIZE"
8620 state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]);
8623 total_invocations
*= qual_local_size
[i
];
8624 if (total_invocations
>
8625 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
) {
8626 _mesa_glsl_error(&loc
, state
,
8627 "product of local_sizes exceeds "
8628 "MAX_COMPUTE_WORK_GROUP_INVOCATIONS (%d)",
8629 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
);
8634 /* If any compute input layout declaration preceded this one, make sure it
8635 * was consistent with this one.
8637 if (state
->cs_input_local_size_specified
) {
8638 for (int i
= 0; i
< 3; i
++) {
8639 if (state
->cs_input_local_size
[i
] != qual_local_size
[i
]) {
8640 _mesa_glsl_error(&loc
, state
,
8641 "compute shader input layout does not match"
8642 " previous declaration");
8648 /* The ARB_compute_variable_group_size spec says:
8650 * If a compute shader including a *local_size_variable* qualifier also
8651 * declares a fixed local group size using the *local_size_x*,
8652 * *local_size_y*, or *local_size_z* qualifiers, a compile-time error
8655 if (state
->cs_input_local_size_variable_specified
) {
8656 _mesa_glsl_error(&loc
, state
,
8657 "compute shader can't include both a variable and a "
8658 "fixed local group size");
8662 state
->cs_input_local_size_specified
= true;
8663 for (int i
= 0; i
< 3; i
++)
8664 state
->cs_input_local_size
[i
] = qual_local_size
[i
];
8666 /* We may now declare the built-in constant gl_WorkGroupSize (see
8667 * builtin_variable_generator::generate_constants() for why we didn't
8668 * declare it earlier).
8670 ir_variable
*var
= new(state
->symbols
)
8671 ir_variable(glsl_type::uvec3_type
, "gl_WorkGroupSize", ir_var_auto
);
8672 var
->data
.how_declared
= ir_var_declared_implicitly
;
8673 var
->data
.read_only
= true;
8674 instructions
->push_tail(var
);
8675 state
->symbols
->add_variable(var
);
8676 ir_constant_data data
;
8677 memset(&data
, 0, sizeof(data
));
8678 for (int i
= 0; i
< 3; i
++)
8679 data
.u
[i
] = qual_local_size
[i
];
8680 var
->constant_value
= new(var
) ir_constant(glsl_type::uvec3_type
, &data
);
8681 var
->constant_initializer
=
8682 new(var
) ir_constant(glsl_type::uvec3_type
, &data
);
8683 var
->data
.has_initializer
= true;
8690 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
8691 exec_list
*instructions
)
8693 bool gl_FragColor_assigned
= false;
8694 bool gl_FragData_assigned
= false;
8695 bool gl_FragSecondaryColor_assigned
= false;
8696 bool gl_FragSecondaryData_assigned
= false;
8697 bool user_defined_fs_output_assigned
= false;
8698 ir_variable
*user_defined_fs_output
= NULL
;
8700 /* It would be nice to have proper location information. */
8702 memset(&loc
, 0, sizeof(loc
));
8704 foreach_in_list(ir_instruction
, node
, instructions
) {
8705 ir_variable
*var
= node
->as_variable();
8707 if (!var
|| !var
->data
.assigned
)
8710 if (strcmp(var
->name
, "gl_FragColor") == 0)
8711 gl_FragColor_assigned
= true;
8712 else if (strcmp(var
->name
, "gl_FragData") == 0)
8713 gl_FragData_assigned
= true;
8714 else if (strcmp(var
->name
, "gl_SecondaryFragColorEXT") == 0)
8715 gl_FragSecondaryColor_assigned
= true;
8716 else if (strcmp(var
->name
, "gl_SecondaryFragDataEXT") == 0)
8717 gl_FragSecondaryData_assigned
= true;
8718 else if (!is_gl_identifier(var
->name
)) {
8719 if (state
->stage
== MESA_SHADER_FRAGMENT
&&
8720 var
->data
.mode
== ir_var_shader_out
) {
8721 user_defined_fs_output_assigned
= true;
8722 user_defined_fs_output
= var
;
8727 /* From the GLSL 1.30 spec:
8729 * "If a shader statically assigns a value to gl_FragColor, it
8730 * may not assign a value to any element of gl_FragData. If a
8731 * shader statically writes a value to any element of
8732 * gl_FragData, it may not assign a value to
8733 * gl_FragColor. That is, a shader may assign values to either
8734 * gl_FragColor or gl_FragData, but not both. Multiple shaders
8735 * linked together must also consistently write just one of
8736 * these variables. Similarly, if user declared output
8737 * variables are in use (statically assigned to), then the
8738 * built-in variables gl_FragColor and gl_FragData may not be
8739 * assigned to. These incorrect usages all generate compile
8742 if (gl_FragColor_assigned
&& gl_FragData_assigned
) {
8743 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8744 "`gl_FragColor' and `gl_FragData'");
8745 } else if (gl_FragColor_assigned
&& user_defined_fs_output_assigned
) {
8746 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8747 "`gl_FragColor' and `%s'",
8748 user_defined_fs_output
->name
);
8749 } else if (gl_FragSecondaryColor_assigned
&& gl_FragSecondaryData_assigned
) {
8750 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8751 "`gl_FragSecondaryColorEXT' and"
8752 " `gl_FragSecondaryDataEXT'");
8753 } else if (gl_FragColor_assigned
&& gl_FragSecondaryData_assigned
) {
8754 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8755 "`gl_FragColor' and"
8756 " `gl_FragSecondaryDataEXT'");
8757 } else if (gl_FragData_assigned
&& gl_FragSecondaryColor_assigned
) {
8758 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8760 " `gl_FragSecondaryColorEXT'");
8761 } else if (gl_FragData_assigned
&& user_defined_fs_output_assigned
) {
8762 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8763 "`gl_FragData' and `%s'",
8764 user_defined_fs_output
->name
);
8767 if ((gl_FragSecondaryColor_assigned
|| gl_FragSecondaryData_assigned
) &&
8768 !state
->EXT_blend_func_extended_enable
) {
8769 _mesa_glsl_error(&loc
, state
,
8770 "Dual source blending requires EXT_blend_func_extended");
8775 verify_subroutine_associated_funcs(struct _mesa_glsl_parse_state
*state
)
8778 memset(&loc
, 0, sizeof(loc
));
8780 /* Section 6.1.2 (Subroutines) of the GLSL 4.00 spec says:
8782 * "A program will fail to compile or link if any shader
8783 * or stage contains two or more functions with the same
8784 * name if the name is associated with a subroutine type."
8787 for (int i
= 0; i
< state
->num_subroutines
; i
++) {
8788 unsigned definitions
= 0;
8789 ir_function
*fn
= state
->subroutines
[i
];
8790 /* Calculate number of function definitions with the same name */
8791 foreach_in_list(ir_function_signature
, sig
, &fn
->signatures
) {
8792 if (sig
->is_defined
) {
8793 if (++definitions
> 1) {
8794 _mesa_glsl_error(&loc
, state
,
8795 "%s shader contains two or more function "
8796 "definitions with name `%s', which is "
8797 "associated with a subroutine type.\n",
8798 _mesa_shader_stage_to_string(state
->stage
),
8808 remove_per_vertex_blocks(exec_list
*instructions
,
8809 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
)
8811 /* Find the gl_PerVertex interface block of the appropriate (in/out) mode,
8812 * if it exists in this shader type.
8814 const glsl_type
*per_vertex
= NULL
;
8816 case ir_var_shader_in
:
8817 if (ir_variable
*gl_in
= state
->symbols
->get_variable("gl_in"))
8818 per_vertex
= gl_in
->get_interface_type();
8820 case ir_var_shader_out
:
8821 if (ir_variable
*gl_Position
=
8822 state
->symbols
->get_variable("gl_Position")) {
8823 per_vertex
= gl_Position
->get_interface_type();
8827 assert(!"Unexpected mode");
8831 /* If we didn't find a built-in gl_PerVertex interface block, then we don't
8832 * need to do anything.
8834 if (per_vertex
== NULL
)
8837 /* If the interface block is used by the shader, then we don't need to do
8840 interface_block_usage_visitor
v(mode
, per_vertex
);
8841 v
.run(instructions
);
8842 if (v
.usage_found())
8845 /* Remove any ir_variable declarations that refer to the interface block
8848 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
8849 ir_variable
*const var
= node
->as_variable();
8850 if (var
!= NULL
&& var
->get_interface_type() == per_vertex
&&
8851 var
->data
.mode
== mode
) {
8852 state
->symbols
->disable_variable(var
->name
);
8859 ast_warnings_toggle::hir(exec_list
*,
8860 struct _mesa_glsl_parse_state
*state
)
8862 state
->warnings_enabled
= enable
;