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/macros.h"
58 #include "main/shaderobj.h"
60 #include "ir_builder.h"
61 #include "builtin_functions.h"
63 using namespace ir_builder
;
66 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
67 exec_list
*instructions
);
69 remove_per_vertex_blocks(exec_list
*instructions
,
70 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
);
73 * Visitor class that finds the first instance of any write-only variable that
74 * is ever read, if any
76 class read_from_write_only_variable_visitor
: public ir_hierarchical_visitor
79 read_from_write_only_variable_visitor() : found(NULL
)
83 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
85 if (this->in_assignee
)
86 return visit_continue
;
88 ir_variable
*var
= ir
->variable_referenced();
89 /* We can have image_write_only set on both images and buffer variables,
90 * but in the former there is a distinction between reads from
91 * the variable itself (write_only) and from the memory they point to
92 * (image_write_only), while in the case of buffer variables there is
93 * no such distinction, that is why this check here is limited to
94 * buffer variables alone.
96 if (!var
|| var
->data
.mode
!= ir_var_shader_storage
)
97 return visit_continue
;
99 if (var
->data
.image_write_only
) {
104 return visit_continue
;
107 ir_variable
*get_variable() {
111 virtual ir_visitor_status
visit_enter(ir_expression
*ir
)
113 /* .length() doesn't actually read anything */
114 if (ir
->operation
== ir_unop_ssbo_unsized_array_length
)
115 return visit_continue_with_parent
;
117 return visit_continue
;
125 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
127 _mesa_glsl_initialize_variables(instructions
, state
);
129 state
->symbols
->separate_function_namespace
= state
->language_version
== 110;
131 state
->current_function
= NULL
;
133 state
->toplevel_ir
= instructions
;
135 state
->gs_input_prim_type_specified
= false;
136 state
->tcs_output_vertices_specified
= false;
137 state
->cs_input_local_size_specified
= false;
139 /* Section 4.2 of the GLSL 1.20 specification states:
140 * "The built-in functions are scoped in a scope outside the global scope
141 * users declare global variables in. That is, a shader's global scope,
142 * available for user-defined functions and global variables, is nested
143 * inside the scope containing the built-in functions."
145 * Since built-in functions like ftransform() access built-in variables,
146 * it follows that those must be in the outer scope as well.
148 * We push scope here to create this nesting effect...but don't pop.
149 * This way, a shader's globals are still in the symbol table for use
152 state
->symbols
->push_scope();
154 foreach_list_typed (ast_node
, ast
, link
, & state
->translation_unit
)
155 ast
->hir(instructions
, state
);
157 detect_recursion_unlinked(state
, instructions
);
158 detect_conflicting_assignments(state
, instructions
);
160 state
->toplevel_ir
= NULL
;
162 /* Move all of the variable declarations to the front of the IR list, and
163 * reverse the order. This has the (intended!) side effect that vertex
164 * shader inputs and fragment shader outputs will appear in the IR in the
165 * same order that they appeared in the shader code. This results in the
166 * locations being assigned in the declared order. Many (arguably buggy)
167 * applications depend on this behavior, and it matches what nearly all
170 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
171 ir_variable
*const var
= node
->as_variable();
177 instructions
->push_head(var
);
180 /* Figure out if gl_FragCoord is actually used in fragment shader */
181 ir_variable
*const var
= state
->symbols
->get_variable("gl_FragCoord");
183 state
->fs_uses_gl_fragcoord
= var
->data
.used
;
185 /* From section 7.1 (Built-In Language Variables) of the GLSL 4.10 spec:
187 * If multiple shaders using members of a built-in block belonging to
188 * the same interface are linked together in the same program, they
189 * must all redeclare the built-in block in the same way, as described
190 * in section 4.3.7 "Interface Blocks" for interface block matching, or
191 * a link error will result.
193 * The phrase "using members of a built-in block" implies that if two
194 * shaders are linked together and one of them *does not use* any members
195 * of the built-in block, then that shader does not need to have a matching
196 * redeclaration of the built-in block.
198 * This appears to be a clarification to the behaviour established for
199 * gl_PerVertex by GLSL 1.50, therefore implement it regardless of GLSL
202 * The definition of "interface" in section 4.3.7 that applies here is as
205 * The boundary between adjacent programmable pipeline stages: This
206 * spans all the outputs in all compilation units of the first stage
207 * and all the inputs in all compilation units of the second stage.
209 * Therefore this rule applies to both inter- and intra-stage linking.
211 * The easiest way to implement this is to check whether the shader uses
212 * gl_PerVertex right after ast-to-ir conversion, and if it doesn't, simply
213 * remove all the relevant variable declaration from the IR, so that the
214 * linker won't see them and complain about mismatches.
216 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_in
);
217 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_out
);
219 /* Check that we don't have reads from write-only variables */
220 read_from_write_only_variable_visitor v
;
222 ir_variable
*error_var
= v
.get_variable();
224 /* It would be nice to have proper location information, but for that
225 * we would need to check this as we process each kind of AST node
228 memset(&loc
, 0, sizeof(loc
));
229 _mesa_glsl_error(&loc
, state
, "Read from write-only variable `%s'",
235 static ir_expression_operation
236 get_implicit_conversion_operation(const glsl_type
*to
, const glsl_type
*from
,
237 struct _mesa_glsl_parse_state
*state
)
239 switch (to
->base_type
) {
240 case GLSL_TYPE_FLOAT
:
241 switch (from
->base_type
) {
242 case GLSL_TYPE_INT
: return ir_unop_i2f
;
243 case GLSL_TYPE_UINT
: return ir_unop_u2f
;
244 default: return (ir_expression_operation
)0;
248 if (!state
->is_version(400, 0) && !state
->ARB_gpu_shader5_enable
249 && !state
->MESA_shader_integer_functions_enable
)
250 return (ir_expression_operation
)0;
251 switch (from
->base_type
) {
252 case GLSL_TYPE_INT
: return ir_unop_i2u
;
253 default: return (ir_expression_operation
)0;
256 case GLSL_TYPE_DOUBLE
:
257 if (!state
->has_double())
258 return (ir_expression_operation
)0;
259 switch (from
->base_type
) {
260 case GLSL_TYPE_INT
: return ir_unop_i2d
;
261 case GLSL_TYPE_UINT
: return ir_unop_u2d
;
262 case GLSL_TYPE_FLOAT
: return ir_unop_f2d
;
263 case GLSL_TYPE_INT64
: return ir_unop_i642d
;
264 case GLSL_TYPE_UINT64
: return ir_unop_u642d
;
265 default: return (ir_expression_operation
)0;
268 case GLSL_TYPE_UINT64
:
269 if (!state
->has_int64())
270 return (ir_expression_operation
)0;
271 switch (from
->base_type
) {
272 case GLSL_TYPE_INT
: return ir_unop_i2u64
;
273 case GLSL_TYPE_UINT
: return ir_unop_u2u64
;
274 case GLSL_TYPE_INT64
: return ir_unop_i642u64
;
275 default: return (ir_expression_operation
)0;
278 case GLSL_TYPE_INT64
:
279 if (!state
->has_int64())
280 return (ir_expression_operation
)0;
281 switch (from
->base_type
) {
282 case GLSL_TYPE_INT
: return ir_unop_i2i64
;
283 default: return (ir_expression_operation
)0;
286 default: return (ir_expression_operation
)0;
292 * If a conversion is available, convert one operand to a different type
294 * The \c from \c ir_rvalue is converted "in place".
296 * \param to Type that the operand it to be converted to
297 * \param from Operand that is being converted
298 * \param state GLSL compiler state
301 * If a conversion is possible (or unnecessary), \c true is returned.
302 * Otherwise \c false is returned.
305 apply_implicit_conversion(const glsl_type
*to
, ir_rvalue
* &from
,
306 struct _mesa_glsl_parse_state
*state
)
309 if (to
->base_type
== from
->type
->base_type
)
312 /* Prior to GLSL 1.20, there are no implicit conversions */
313 if (!state
->is_version(120, 0))
316 /* ESSL does not allow implicit conversions */
317 if (state
->es_shader
)
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
->base_type
== GLSL_TYPE_FLOAT
||
448 type_a
->base_type
== GLSL_TYPE_DOUBLE
);
449 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
||
450 type_b
->base_type
== GLSL_TYPE_DOUBLE
);
452 /* "* The operator is add (+), subtract (-), or divide (/), and the
453 * operands are matrices with the same number of rows and the same
454 * number of columns. In this case, the operation is done component-
455 * wise resulting in the same size matrix."
456 * * The operator is multiply (*), where both operands are matrices or
457 * one operand is a vector and the other a matrix. A right vector
458 * operand is treated as a column vector and a left vector operand as a
459 * row vector. In all these cases, it is required that the number of
460 * columns of the left operand is equal to the number of rows of the
461 * right operand. Then, the multiply (*) operation does a linear
462 * algebraic multiply, yielding an object that has the same number of
463 * rows as the left operand and the same number of columns as the right
464 * operand. Section 5.10 "Vector and Matrix Operations" explains in
465 * more detail how vectors and matrices are operated on."
468 if (type_a
== type_b
)
471 const glsl_type
*type
= glsl_type::get_mul_type(type_a
, type_b
);
473 if (type
== glsl_type::error_type
) {
474 _mesa_glsl_error(loc
, state
,
475 "size mismatch for matrix multiplication");
482 /* "All other cases are illegal."
484 _mesa_glsl_error(loc
, state
, "type mismatch");
485 return glsl_type::error_type
;
489 static const struct glsl_type
*
490 unary_arithmetic_result_type(const struct glsl_type
*type
,
491 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
493 /* From GLSL 1.50 spec, page 57:
495 * "The arithmetic unary operators negate (-), post- and pre-increment
496 * and decrement (-- and ++) operate on integer or floating-point
497 * values (including vectors and matrices). All unary operators work
498 * component-wise on their operands. These result with the same type
501 if (!type
->is_numeric()) {
502 _mesa_glsl_error(loc
, state
,
503 "operands to arithmetic operators must be numeric");
504 return glsl_type::error_type
;
511 * \brief Return the result type of a bit-logic operation.
513 * If the given types to the bit-logic operator are invalid, return
514 * glsl_type::error_type.
516 * \param value_a LHS of bit-logic op
517 * \param value_b RHS of bit-logic op
519 static const struct glsl_type
*
520 bit_logic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
522 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
524 const glsl_type
*type_a
= value_a
->type
;
525 const glsl_type
*type_b
= value_b
->type
;
527 if (!state
->check_bitwise_operations_allowed(loc
)) {
528 return glsl_type::error_type
;
531 /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
533 * "The bitwise operators and (&), exclusive-or (^), and inclusive-or
534 * (|). The operands must be of type signed or unsigned integers or
537 if (!type_a
->is_integer_32_64()) {
538 _mesa_glsl_error(loc
, state
, "LHS of `%s' must be an integer",
539 ast_expression::operator_string(op
));
540 return glsl_type::error_type
;
542 if (!type_b
->is_integer_32_64()) {
543 _mesa_glsl_error(loc
, state
, "RHS of `%s' must be an integer",
544 ast_expression::operator_string(op
));
545 return glsl_type::error_type
;
548 /* Prior to GLSL 4.0 / GL_ARB_gpu_shader5, implicit conversions didn't
549 * make sense for bitwise operations, as they don't operate on floats.
551 * GLSL 4.0 added implicit int -> uint conversions, which are relevant
552 * here. It wasn't clear whether or not we should apply them to bitwise
553 * operations. However, Khronos has decided that they should in future
554 * language revisions. Applications also rely on this behavior. We opt
555 * to apply them in general, but issue a portability warning.
557 * See https://www.khronos.org/bugzilla/show_bug.cgi?id=1405
559 if (type_a
->base_type
!= type_b
->base_type
) {
560 if (!apply_implicit_conversion(type_a
, value_b
, state
)
561 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
562 _mesa_glsl_error(loc
, state
,
563 "could not implicitly convert operands to "
565 ast_expression::operator_string(op
));
566 return glsl_type::error_type
;
568 _mesa_glsl_warning(loc
, state
,
569 "some implementations may not support implicit "
570 "int -> uint conversions for `%s' operators; "
571 "consider casting explicitly for portability",
572 ast_expression::operator_string(op
));
574 type_a
= value_a
->type
;
575 type_b
= value_b
->type
;
578 /* "The fundamental types of the operands (signed or unsigned) must
581 if (type_a
->base_type
!= type_b
->base_type
) {
582 _mesa_glsl_error(loc
, state
, "operands of `%s' must have the same "
583 "base type", ast_expression::operator_string(op
));
584 return glsl_type::error_type
;
587 /* "The operands cannot be vectors of differing size." */
588 if (type_a
->is_vector() &&
589 type_b
->is_vector() &&
590 type_a
->vector_elements
!= type_b
->vector_elements
) {
591 _mesa_glsl_error(loc
, state
, "operands of `%s' cannot be vectors of "
592 "different sizes", ast_expression::operator_string(op
));
593 return glsl_type::error_type
;
596 /* "If one operand is a scalar and the other a vector, the scalar is
597 * applied component-wise to the vector, resulting in the same type as
598 * the vector. The fundamental types of the operands [...] will be the
599 * resulting fundamental type."
601 if (type_a
->is_scalar())
607 static const struct glsl_type
*
608 modulus_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
609 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
611 const glsl_type
*type_a
= value_a
->type
;
612 const glsl_type
*type_b
= value_b
->type
;
614 if (!state
->check_version(130, 300, loc
, "operator '%%' is reserved")) {
615 return glsl_type::error_type
;
618 /* Section 5.9 (Expressions) of the GLSL 4.00 specification says:
620 * "The operator modulus (%) operates on signed or unsigned integers or
623 if (!type_a
->is_integer_32_64()) {
624 _mesa_glsl_error(loc
, state
, "LHS of operator %% must be an integer");
625 return glsl_type::error_type
;
627 if (!type_b
->is_integer_32_64()) {
628 _mesa_glsl_error(loc
, state
, "RHS of operator %% must be an integer");
629 return glsl_type::error_type
;
632 /* "If the fundamental types in the operands do not match, then the
633 * conversions from section 4.1.10 "Implicit Conversions" are applied
634 * to create matching types."
636 * Note that GLSL 4.00 (and GL_ARB_gpu_shader5) introduced implicit
637 * int -> uint conversion rules. Prior to that, there were no implicit
638 * conversions. So it's harmless to apply them universally - no implicit
639 * conversions will exist. If the types don't match, we'll receive false,
640 * and raise an error, satisfying the GLSL 1.50 spec, page 56:
642 * "The operand types must both be signed or unsigned."
644 if (!apply_implicit_conversion(type_a
, value_b
, state
) &&
645 !apply_implicit_conversion(type_b
, value_a
, state
)) {
646 _mesa_glsl_error(loc
, state
,
647 "could not implicitly convert operands to "
648 "modulus (%%) operator");
649 return glsl_type::error_type
;
651 type_a
= value_a
->type
;
652 type_b
= value_b
->type
;
654 /* "The operands cannot be vectors of differing size. If one operand is
655 * a scalar and the other vector, then the scalar is applied component-
656 * wise to the vector, resulting in the same type as the vector. If both
657 * are vectors of the same size, the result is computed component-wise."
659 if (type_a
->is_vector()) {
660 if (!type_b
->is_vector()
661 || (type_a
->vector_elements
== type_b
->vector_elements
))
666 /* "The operator modulus (%) is not defined for any other data types
667 * (non-integer types)."
669 _mesa_glsl_error(loc
, state
, "type mismatch");
670 return glsl_type::error_type
;
674 static const struct glsl_type
*
675 relational_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
676 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
678 const glsl_type
*type_a
= value_a
->type
;
679 const glsl_type
*type_b
= value_b
->type
;
681 /* From GLSL 1.50 spec, page 56:
682 * "The relational operators greater than (>), less than (<), greater
683 * than or equal (>=), and less than or equal (<=) operate only on
684 * scalar integer and scalar floating-point expressions."
686 if (!type_a
->is_numeric()
687 || !type_b
->is_numeric()
688 || !type_a
->is_scalar()
689 || !type_b
->is_scalar()) {
690 _mesa_glsl_error(loc
, state
,
691 "operands to relational operators must be scalar and "
693 return glsl_type::error_type
;
696 /* "Either the operands' types must match, or the conversions from
697 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
698 * operand, after which the types must match."
700 if (!apply_implicit_conversion(type_a
, value_b
, state
)
701 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
702 _mesa_glsl_error(loc
, state
,
703 "could not implicitly convert operands to "
704 "relational operator");
705 return glsl_type::error_type
;
707 type_a
= value_a
->type
;
708 type_b
= value_b
->type
;
710 if (type_a
->base_type
!= type_b
->base_type
) {
711 _mesa_glsl_error(loc
, state
, "base type mismatch");
712 return glsl_type::error_type
;
715 /* "The result is scalar Boolean."
717 return glsl_type::bool_type
;
721 * \brief Return the result type of a bit-shift operation.
723 * If the given types to the bit-shift operator are invalid, return
724 * glsl_type::error_type.
726 * \param type_a Type of LHS of bit-shift op
727 * \param type_b Type of RHS of bit-shift op
729 static const struct glsl_type
*
730 shift_result_type(const struct glsl_type
*type_a
,
731 const struct glsl_type
*type_b
,
733 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
735 if (!state
->check_bitwise_operations_allowed(loc
)) {
736 return glsl_type::error_type
;
739 /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
741 * "The shift operators (<<) and (>>). For both operators, the operands
742 * must be signed or unsigned integers or integer vectors. One operand
743 * can be signed while the other is unsigned."
745 if (!type_a
->is_integer_32_64()) {
746 _mesa_glsl_error(loc
, state
, "LHS of operator %s must be an integer or "
747 "integer vector", ast_expression::operator_string(op
));
748 return glsl_type::error_type
;
751 if (!type_b
->is_integer()) {
752 _mesa_glsl_error(loc
, state
, "RHS of operator %s must be an integer or "
753 "integer vector", ast_expression::operator_string(op
));
754 return glsl_type::error_type
;
757 /* "If the first operand is a scalar, the second operand has to be
760 if (type_a
->is_scalar() && !type_b
->is_scalar()) {
761 _mesa_glsl_error(loc
, state
, "if the first operand of %s is scalar, the "
762 "second must be scalar as well",
763 ast_expression::operator_string(op
));
764 return glsl_type::error_type
;
767 /* If both operands are vectors, check that they have same number of
770 if (type_a
->is_vector() &&
771 type_b
->is_vector() &&
772 type_a
->vector_elements
!= type_b
->vector_elements
) {
773 _mesa_glsl_error(loc
, state
, "vector operands to operator %s must "
774 "have same number of elements",
775 ast_expression::operator_string(op
));
776 return glsl_type::error_type
;
779 /* "In all cases, the resulting type will be the same type as the left
786 * Returns the innermost array index expression in an rvalue tree.
787 * This is the largest indexing level -- if an array of blocks, then
788 * it is the block index rather than an indexing expression for an
789 * array-typed member of an array of blocks.
792 find_innermost_array_index(ir_rvalue
*rv
)
794 ir_dereference_array
*last
= NULL
;
796 if (rv
->as_dereference_array()) {
797 last
= rv
->as_dereference_array();
799 } else if (rv
->as_dereference_record())
800 rv
= rv
->as_dereference_record()->record
;
801 else if (rv
->as_swizzle())
802 rv
= rv
->as_swizzle()->val
;
808 return last
->array_index
;
814 * Validates that a value can be assigned to a location with a specified type
816 * Validates that \c rhs can be assigned to some location. If the types are
817 * not an exact match but an automatic conversion is possible, \c rhs will be
821 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
822 * Otherwise the actual RHS to be assigned will be returned. This may be
823 * \c rhs, or it may be \c rhs after some type conversion.
826 * In addition to being used for assignments, this function is used to
827 * type-check return values.
830 validate_assignment(struct _mesa_glsl_parse_state
*state
,
831 YYLTYPE loc
, ir_rvalue
*lhs
,
832 ir_rvalue
*rhs
, bool is_initializer
)
834 /* If there is already some error in the RHS, just return it. Anything
835 * else will lead to an avalanche of error message back to the user.
837 if (rhs
->type
->is_error())
840 /* In the Tessellation Control Shader:
841 * If a per-vertex output variable is used as an l-value, it is an error
842 * if the expression indicating the vertex number is not the identifier
845 if (state
->stage
== MESA_SHADER_TESS_CTRL
&& !lhs
->type
->is_error()) {
846 ir_variable
*var
= lhs
->variable_referenced();
847 if (var
&& var
->data
.mode
== ir_var_shader_out
&& !var
->data
.patch
) {
848 ir_rvalue
*index
= find_innermost_array_index(lhs
);
849 ir_variable
*index_var
= index
? index
->variable_referenced() : NULL
;
850 if (!index_var
|| strcmp(index_var
->name
, "gl_InvocationID") != 0) {
851 _mesa_glsl_error(&loc
, state
,
852 "Tessellation control shader outputs can only "
853 "be indexed by gl_InvocationID");
859 /* If the types are identical, the assignment can trivially proceed.
861 if (rhs
->type
== lhs
->type
)
864 /* If the array element types are the same and the LHS is unsized,
865 * the assignment is okay for initializers embedded in variable
868 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
869 * is handled by ir_dereference::is_lvalue.
871 const glsl_type
*lhs_t
= lhs
->type
;
872 const glsl_type
*rhs_t
= rhs
->type
;
873 bool unsized_array
= false;
874 while(lhs_t
->is_array()) {
876 break; /* the rest of the inner arrays match so break out early */
877 if (!rhs_t
->is_array()) {
878 unsized_array
= false;
879 break; /* number of dimensions mismatch */
881 if (lhs_t
->length
== rhs_t
->length
) {
882 lhs_t
= lhs_t
->fields
.array
;
883 rhs_t
= rhs_t
->fields
.array
;
885 } else if (lhs_t
->is_unsized_array()) {
886 unsized_array
= true;
888 unsized_array
= false;
889 break; /* sized array mismatch */
891 lhs_t
= lhs_t
->fields
.array
;
892 rhs_t
= rhs_t
->fields
.array
;
895 if (is_initializer
) {
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
.image_read_only
))) {
953 /* We can have image_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 * (image_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()) {
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
);
1018 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
1019 * but not post_inc) need the converted assigned value as an rvalue
1020 * to handle things like:
1026 if (!error_emitted
) {
1027 ir_variable
*var
= new(ctx
) ir_variable(rhs
->type
, "assignment_tmp",
1029 instructions
->push_tail(var
);
1030 instructions
->push_tail(assign(var
, rhs
));
1032 ir_dereference_variable
*deref_var
=
1033 new(ctx
) ir_dereference_variable(var
);
1034 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, deref_var
));
1035 rvalue
= new(ctx
) ir_dereference_variable(var
);
1037 rvalue
= ir_rvalue::error_value(ctx
);
1039 *out_rvalue
= rvalue
;
1042 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, rhs
));
1046 return error_emitted
;
1050 get_lvalue_copy(exec_list
*instructions
, ir_rvalue
*lvalue
)
1052 void *ctx
= ralloc_parent(lvalue
);
1055 var
= new(ctx
) ir_variable(lvalue
->type
, "_post_incdec_tmp",
1057 instructions
->push_tail(var
);
1059 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
1062 return new(ctx
) ir_dereference_variable(var
);
1067 ast_node::hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
1069 (void) instructions
;
1076 ast_node::has_sequence_subexpression() const
1082 ast_node::set_is_lhs(bool /* new_value */)
1087 ast_function_expression::hir_no_rvalue(exec_list
*instructions
,
1088 struct _mesa_glsl_parse_state
*state
)
1090 (void)hir(instructions
, state
);
1094 ast_aggregate_initializer::hir_no_rvalue(exec_list
*instructions
,
1095 struct _mesa_glsl_parse_state
*state
)
1097 (void)hir(instructions
, state
);
1101 do_comparison(void *mem_ctx
, int operation
, ir_rvalue
*op0
, ir_rvalue
*op1
)
1104 ir_rvalue
*cmp
= NULL
;
1106 if (operation
== ir_binop_all_equal
)
1107 join_op
= ir_binop_logic_and
;
1109 join_op
= ir_binop_logic_or
;
1111 switch (op0
->type
->base_type
) {
1112 case GLSL_TYPE_FLOAT
:
1113 case GLSL_TYPE_UINT
:
1115 case GLSL_TYPE_BOOL
:
1116 case GLSL_TYPE_DOUBLE
:
1117 case GLSL_TYPE_UINT64
:
1118 case GLSL_TYPE_INT64
:
1119 return new(mem_ctx
) ir_expression(operation
, op0
, op1
);
1121 case GLSL_TYPE_ARRAY
: {
1122 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
1123 ir_rvalue
*e0
, *e1
, *result
;
1125 e0
= new(mem_ctx
) ir_dereference_array(op0
->clone(mem_ctx
, NULL
),
1126 new(mem_ctx
) ir_constant(i
));
1127 e1
= new(mem_ctx
) ir_dereference_array(op1
->clone(mem_ctx
, NULL
),
1128 new(mem_ctx
) ir_constant(i
));
1129 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
1132 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
1138 mark_whole_array_access(op0
);
1139 mark_whole_array_access(op1
);
1143 case GLSL_TYPE_STRUCT
: {
1144 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
1145 ir_rvalue
*e0
, *e1
, *result
;
1146 const char *field_name
= op0
->type
->fields
.structure
[i
].name
;
1148 e0
= new(mem_ctx
) ir_dereference_record(op0
->clone(mem_ctx
, NULL
),
1150 e1
= new(mem_ctx
) ir_dereference_record(op1
->clone(mem_ctx
, NULL
),
1152 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
1155 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
1163 case GLSL_TYPE_ERROR
:
1164 case GLSL_TYPE_VOID
:
1165 case GLSL_TYPE_SAMPLER
:
1166 case GLSL_TYPE_IMAGE
:
1167 case GLSL_TYPE_INTERFACE
:
1168 case GLSL_TYPE_ATOMIC_UINT
:
1169 case GLSL_TYPE_SUBROUTINE
:
1170 case GLSL_TYPE_FUNCTION
:
1171 /* I assume a comparison of a struct containing a sampler just
1172 * ignores the sampler present in the type.
1178 cmp
= new(mem_ctx
) ir_constant(true);
1183 /* For logical operations, we want to ensure that the operands are
1184 * scalar booleans. If it isn't, emit an error and return a constant
1185 * boolean to avoid triggering cascading error messages.
1188 get_scalar_boolean_operand(exec_list
*instructions
,
1189 struct _mesa_glsl_parse_state
*state
,
1190 ast_expression
*parent_expr
,
1192 const char *operand_name
,
1193 bool *error_emitted
)
1195 ast_expression
*expr
= parent_expr
->subexpressions
[operand
];
1197 ir_rvalue
*val
= expr
->hir(instructions
, state
);
1199 if (val
->type
->is_boolean() && val
->type
->is_scalar())
1202 if (!*error_emitted
) {
1203 YYLTYPE loc
= expr
->get_location();
1204 _mesa_glsl_error(&loc
, state
, "%s of `%s' must be scalar boolean",
1206 parent_expr
->operator_string(parent_expr
->oper
));
1207 *error_emitted
= true;
1210 return new(ctx
) ir_constant(true);
1214 * If name refers to a builtin array whose maximum allowed size is less than
1215 * size, report an error and return true. Otherwise return false.
1218 check_builtin_array_max_size(const char *name
, unsigned size
,
1219 YYLTYPE loc
, struct _mesa_glsl_parse_state
*state
)
1221 if ((strcmp("gl_TexCoord", name
) == 0)
1222 && (size
> state
->Const
.MaxTextureCoords
)) {
1223 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1225 * "The size [of gl_TexCoord] can be at most
1226 * gl_MaxTextureCoords."
1228 _mesa_glsl_error(&loc
, state
, "`gl_TexCoord' array size cannot "
1229 "be larger than gl_MaxTextureCoords (%u)",
1230 state
->Const
.MaxTextureCoords
);
1231 } else if (strcmp("gl_ClipDistance", name
) == 0) {
1232 state
->clip_dist_size
= size
;
1233 if (size
+ state
->cull_dist_size
> state
->Const
.MaxClipPlanes
) {
1234 /* From section 7.1 (Vertex Shader Special Variables) of the
1237 * "The gl_ClipDistance array is predeclared as unsized and
1238 * must be sized by the shader either redeclaring it with a
1239 * size or indexing it only with integral constant
1240 * expressions. ... The size can be at most
1241 * gl_MaxClipDistances."
1243 _mesa_glsl_error(&loc
, state
, "`gl_ClipDistance' array size cannot "
1244 "be larger than gl_MaxClipDistances (%u)",
1245 state
->Const
.MaxClipPlanes
);
1247 } else if (strcmp("gl_CullDistance", name
) == 0) {
1248 state
->cull_dist_size
= size
;
1249 if (size
+ state
->clip_dist_size
> state
->Const
.MaxClipPlanes
) {
1250 /* From the ARB_cull_distance spec:
1252 * "The gl_CullDistance array is predeclared as unsized and
1253 * must be sized by the shader either redeclaring it with
1254 * a size or indexing it only with integral constant
1255 * expressions. The size determines the number and set of
1256 * enabled cull distances and can be at most
1257 * gl_MaxCullDistances."
1259 _mesa_glsl_error(&loc
, state
, "`gl_CullDistance' array size cannot "
1260 "be larger than gl_MaxCullDistances (%u)",
1261 state
->Const
.MaxClipPlanes
);
1267 * Create the constant 1, of a which is appropriate for incrementing and
1268 * decrementing values of the given GLSL type. For example, if type is vec4,
1269 * this creates a constant value of 1.0 having type float.
1271 * If the given type is invalid for increment and decrement operators, return
1272 * a floating point 1--the error will be detected later.
1275 constant_one_for_inc_dec(void *ctx
, const glsl_type
*type
)
1277 switch (type
->base_type
) {
1278 case GLSL_TYPE_UINT
:
1279 return new(ctx
) ir_constant((unsigned) 1);
1281 return new(ctx
) ir_constant(1);
1282 case GLSL_TYPE_UINT64
:
1283 return new(ctx
) ir_constant((uint64_t) 1);
1284 case GLSL_TYPE_INT64
:
1285 return new(ctx
) ir_constant((int64_t) 1);
1287 case GLSL_TYPE_FLOAT
:
1288 return new(ctx
) ir_constant(1.0f
);
1293 ast_expression::hir(exec_list
*instructions
,
1294 struct _mesa_glsl_parse_state
*state
)
1296 return do_hir(instructions
, state
, true);
1300 ast_expression::hir_no_rvalue(exec_list
*instructions
,
1301 struct _mesa_glsl_parse_state
*state
)
1303 do_hir(instructions
, state
, false);
1307 ast_expression::set_is_lhs(bool new_value
)
1309 /* is_lhs is tracked only to print "variable used uninitialized" warnings,
1310 * if we lack an identifier we can just skip it.
1312 if (this->primary_expression
.identifier
== NULL
)
1315 this->is_lhs
= new_value
;
1317 /* We need to go through the subexpressions tree to cover cases like
1318 * ast_field_selection
1320 if (this->subexpressions
[0] != NULL
)
1321 this->subexpressions
[0]->set_is_lhs(new_value
);
1325 ast_expression::do_hir(exec_list
*instructions
,
1326 struct _mesa_glsl_parse_state
*state
,
1330 static const int operations
[AST_NUM_OPERATORS
] = {
1331 -1, /* ast_assign doesn't convert to ir_expression. */
1332 -1, /* ast_plus doesn't convert to ir_expression. */
1346 ir_binop_any_nequal
,
1356 /* Note: The following block of expression types actually convert
1357 * to multiple IR instructions.
1359 ir_binop_mul
, /* ast_mul_assign */
1360 ir_binop_div
, /* ast_div_assign */
1361 ir_binop_mod
, /* ast_mod_assign */
1362 ir_binop_add
, /* ast_add_assign */
1363 ir_binop_sub
, /* ast_sub_assign */
1364 ir_binop_lshift
, /* ast_ls_assign */
1365 ir_binop_rshift
, /* ast_rs_assign */
1366 ir_binop_bit_and
, /* ast_and_assign */
1367 ir_binop_bit_xor
, /* ast_xor_assign */
1368 ir_binop_bit_or
, /* ast_or_assign */
1370 -1, /* ast_conditional doesn't convert to ir_expression. */
1371 ir_binop_add
, /* ast_pre_inc. */
1372 ir_binop_sub
, /* ast_pre_dec. */
1373 ir_binop_add
, /* ast_post_inc. */
1374 ir_binop_sub
, /* ast_post_dec. */
1375 -1, /* ast_field_selection doesn't conv to ir_expression. */
1376 -1, /* ast_array_index doesn't convert to ir_expression. */
1377 -1, /* ast_function_call doesn't conv to ir_expression. */
1378 -1, /* ast_identifier doesn't convert to ir_expression. */
1379 -1, /* ast_int_constant doesn't convert to ir_expression. */
1380 -1, /* ast_uint_constant doesn't conv to ir_expression. */
1381 -1, /* ast_float_constant doesn't conv to ir_expression. */
1382 -1, /* ast_bool_constant doesn't conv to ir_expression. */
1383 -1, /* ast_sequence doesn't convert to ir_expression. */
1384 -1, /* ast_aggregate shouldn't ever even get here. */
1386 ir_rvalue
*result
= NULL
;
1388 const struct glsl_type
*type
, *orig_type
;
1389 bool error_emitted
= false;
1392 loc
= this->get_location();
1394 switch (this->oper
) {
1396 assert(!"ast_aggregate: Should never get here.");
1400 this->subexpressions
[0]->set_is_lhs(true);
1401 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1402 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1405 do_assignment(instructions
, state
,
1406 this->subexpressions
[0]->non_lvalue_description
,
1407 op
[0], op
[1], &result
, needs_rvalue
, false,
1408 this->subexpressions
[0]->get_location());
1413 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1415 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1417 error_emitted
= type
->is_error();
1423 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1425 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1427 error_emitted
= type
->is_error();
1429 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1437 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1438 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1440 type
= arithmetic_result_type(op
[0], op
[1],
1441 (this->oper
== ast_mul
),
1443 error_emitted
= type
->is_error();
1445 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1450 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1451 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1453 type
= modulus_result_type(op
[0], op
[1], state
, &loc
);
1455 assert(operations
[this->oper
] == ir_binop_mod
);
1457 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1459 error_emitted
= type
->is_error();
1464 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1465 error_emitted
= true;
1468 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1469 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1470 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1472 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1474 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1481 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1482 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1484 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
1486 /* The relational operators must either generate an error or result
1487 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1489 assert(type
->is_error()
1490 || ((type
->base_type
== GLSL_TYPE_BOOL
)
1491 && type
->is_scalar()));
1493 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1495 error_emitted
= type
->is_error();
1500 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1501 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1503 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1505 * "The equality operators equal (==), and not equal (!=)
1506 * operate on all types. They result in a scalar Boolean. If
1507 * the operand types do not match, then there must be a
1508 * conversion from Section 4.1.10 "Implicit Conversions"
1509 * applied to one operand that can make them match, in which
1510 * case this conversion is done."
1513 if (op
[0]->type
== glsl_type::void_type
|| op
[1]->type
== glsl_type::void_type
) {
1514 _mesa_glsl_error(& loc
, state
, "`%s': wrong operand types: "
1515 "no operation `%1$s' exists that takes a left-hand "
1516 "operand of type 'void' or a right operand of type "
1517 "'void'", (this->oper
== ast_equal
) ? "==" : "!=");
1518 error_emitted
= true;
1519 } else if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
1520 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
1521 || (op
[0]->type
!= op
[1]->type
)) {
1522 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
1523 "type", (this->oper
== ast_equal
) ? "==" : "!=");
1524 error_emitted
= true;
1525 } else if ((op
[0]->type
->is_array() || op
[1]->type
->is_array()) &&
1526 !state
->check_version(120, 300, &loc
,
1527 "array comparisons forbidden")) {
1528 error_emitted
= true;
1529 } else if ((op
[0]->type
->contains_subroutine() ||
1530 op
[1]->type
->contains_subroutine())) {
1531 _mesa_glsl_error(&loc
, state
, "subroutine comparisons forbidden");
1532 error_emitted
= true;
1533 } else if ((op
[0]->type
->contains_opaque() ||
1534 op
[1]->type
->contains_opaque())) {
1535 _mesa_glsl_error(&loc
, state
, "opaque type comparisons forbidden");
1536 error_emitted
= true;
1539 if (error_emitted
) {
1540 result
= new(ctx
) ir_constant(false);
1542 result
= do_comparison(ctx
, operations
[this->oper
], op
[0], op
[1]);
1543 assert(result
->type
== glsl_type::bool_type
);
1550 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1551 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1552 type
= bit_logic_result_type(op
[0], op
[1], this->oper
, state
, &loc
);
1553 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1555 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1559 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1561 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1562 error_emitted
= true;
1565 if (!op
[0]->type
->is_integer_32_64()) {
1566 _mesa_glsl_error(&loc
, state
, "operand of `~' must be an integer");
1567 error_emitted
= true;
1570 type
= error_emitted
? glsl_type::error_type
: op
[0]->type
;
1571 result
= new(ctx
) ir_expression(ir_unop_bit_not
, type
, op
[0], NULL
);
1574 case ast_logic_and
: {
1575 exec_list rhs_instructions
;
1576 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1577 "LHS", &error_emitted
);
1578 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1579 "RHS", &error_emitted
);
1581 if (rhs_instructions
.is_empty()) {
1582 result
= new(ctx
) ir_expression(ir_binop_logic_and
, op
[0], op
[1]);
1583 type
= result
->type
;
1585 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1588 instructions
->push_tail(tmp
);
1590 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1591 instructions
->push_tail(stmt
);
1593 stmt
->then_instructions
.append_list(&rhs_instructions
);
1594 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1595 ir_assignment
*const then_assign
=
1596 new(ctx
) ir_assignment(then_deref
, op
[1]);
1597 stmt
->then_instructions
.push_tail(then_assign
);
1599 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1600 ir_assignment
*const else_assign
=
1601 new(ctx
) ir_assignment(else_deref
, new(ctx
) ir_constant(false));
1602 stmt
->else_instructions
.push_tail(else_assign
);
1604 result
= new(ctx
) ir_dereference_variable(tmp
);
1610 case ast_logic_or
: {
1611 exec_list rhs_instructions
;
1612 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1613 "LHS", &error_emitted
);
1614 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1615 "RHS", &error_emitted
);
1617 if (rhs_instructions
.is_empty()) {
1618 result
= new(ctx
) ir_expression(ir_binop_logic_or
, op
[0], op
[1]);
1619 type
= result
->type
;
1621 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1624 instructions
->push_tail(tmp
);
1626 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1627 instructions
->push_tail(stmt
);
1629 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1630 ir_assignment
*const then_assign
=
1631 new(ctx
) ir_assignment(then_deref
, new(ctx
) ir_constant(true));
1632 stmt
->then_instructions
.push_tail(then_assign
);
1634 stmt
->else_instructions
.append_list(&rhs_instructions
);
1635 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1636 ir_assignment
*const else_assign
=
1637 new(ctx
) ir_assignment(else_deref
, op
[1]);
1638 stmt
->else_instructions
.push_tail(else_assign
);
1640 result
= new(ctx
) ir_dereference_variable(tmp
);
1647 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1649 * "The logical binary operators and (&&), or ( | | ), and
1650 * exclusive or (^^). They operate only on two Boolean
1651 * expressions and result in a Boolean expression."
1653 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0, "LHS",
1655 op
[1] = get_scalar_boolean_operand(instructions
, state
, this, 1, "RHS",
1658 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1663 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1664 "operand", &error_emitted
);
1666 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1670 case ast_mul_assign
:
1671 case ast_div_assign
:
1672 case ast_add_assign
:
1673 case ast_sub_assign
: {
1674 this->subexpressions
[0]->set_is_lhs(true);
1675 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1676 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1678 orig_type
= op
[0]->type
;
1679 type
= arithmetic_result_type(op
[0], op
[1],
1680 (this->oper
== ast_mul_assign
),
1683 if (type
!= orig_type
) {
1684 _mesa_glsl_error(& loc
, state
,
1685 "could not implicitly convert "
1686 "%s to %s", type
->name
, orig_type
->name
);
1687 type
= glsl_type::error_type
;
1690 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1694 do_assignment(instructions
, state
,
1695 this->subexpressions
[0]->non_lvalue_description
,
1696 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1697 &result
, needs_rvalue
, false,
1698 this->subexpressions
[0]->get_location());
1700 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1701 * explicitly test for this because none of the binary expression
1702 * operators allow array operands either.
1708 case ast_mod_assign
: {
1709 this->subexpressions
[0]->set_is_lhs(true);
1710 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1711 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1713 orig_type
= op
[0]->type
;
1714 type
= modulus_result_type(op
[0], op
[1], state
, &loc
);
1716 if (type
!= orig_type
) {
1717 _mesa_glsl_error(& loc
, state
,
1718 "could not implicitly convert "
1719 "%s to %s", type
->name
, orig_type
->name
);
1720 type
= glsl_type::error_type
;
1723 assert(operations
[this->oper
] == ir_binop_mod
);
1725 ir_rvalue
*temp_rhs
;
1726 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1730 do_assignment(instructions
, state
,
1731 this->subexpressions
[0]->non_lvalue_description
,
1732 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1733 &result
, needs_rvalue
, false,
1734 this->subexpressions
[0]->get_location());
1739 case ast_rs_assign
: {
1740 this->subexpressions
[0]->set_is_lhs(true);
1741 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1742 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1743 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1745 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1746 type
, op
[0], op
[1]);
1748 do_assignment(instructions
, state
,
1749 this->subexpressions
[0]->non_lvalue_description
,
1750 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1751 &result
, needs_rvalue
, false,
1752 this->subexpressions
[0]->get_location());
1756 case ast_and_assign
:
1757 case ast_xor_assign
:
1758 case ast_or_assign
: {
1759 this->subexpressions
[0]->set_is_lhs(true);
1760 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1761 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1763 orig_type
= op
[0]->type
;
1764 type
= bit_logic_result_type(op
[0], op
[1], this->oper
, state
, &loc
);
1766 if (type
!= orig_type
) {
1767 _mesa_glsl_error(& loc
, state
,
1768 "could not implicitly convert "
1769 "%s to %s", type
->name
, orig_type
->name
);
1770 type
= glsl_type::error_type
;
1773 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1774 type
, op
[0], op
[1]);
1776 do_assignment(instructions
, state
,
1777 this->subexpressions
[0]->non_lvalue_description
,
1778 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1779 &result
, needs_rvalue
, false,
1780 this->subexpressions
[0]->get_location());
1784 case ast_conditional
: {
1785 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1787 * "The ternary selection operator (?:). It operates on three
1788 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1789 * first expression, which must result in a scalar Boolean."
1791 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1792 "condition", &error_emitted
);
1794 /* The :? operator is implemented by generating an anonymous temporary
1795 * followed by an if-statement. The last instruction in each branch of
1796 * the if-statement assigns a value to the anonymous temporary. This
1797 * temporary is the r-value of the expression.
1799 exec_list then_instructions
;
1800 exec_list else_instructions
;
1802 op
[1] = this->subexpressions
[1]->hir(&then_instructions
, state
);
1803 op
[2] = this->subexpressions
[2]->hir(&else_instructions
, state
);
1805 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1807 * "The second and third expressions can be any type, as
1808 * long their types match, or there is a conversion in
1809 * Section 4.1.10 "Implicit Conversions" that can be applied
1810 * to one of the expressions to make their types match. This
1811 * resulting matching type is the type of the entire
1814 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1815 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1816 || (op
[1]->type
!= op
[2]->type
)) {
1817 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1819 _mesa_glsl_error(& loc
, state
, "second and third operands of ?: "
1820 "operator must have matching types");
1821 error_emitted
= true;
1822 type
= glsl_type::error_type
;
1827 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1829 * "The second and third expressions must be the same type, but can
1830 * be of any type other than an array."
1832 if (type
->is_array() &&
1833 !state
->check_version(120, 300, &loc
,
1834 "second and third operands of ?: operator "
1835 "cannot be arrays")) {
1836 error_emitted
= true;
1839 /* From section 4.1.7 of the GLSL 4.50 spec (Opaque Types):
1841 * "Except for array indexing, structure member selection, and
1842 * parentheses, opaque variables are not allowed to be operands in
1843 * expressions; such use results in a compile-time error."
1845 if (type
->contains_opaque()) {
1846 _mesa_glsl_error(&loc
, state
, "opaque variables cannot be operands "
1847 "of the ?: operator");
1848 error_emitted
= true;
1851 ir_constant
*cond_val
= op
[0]->constant_expression_value();
1853 if (then_instructions
.is_empty()
1854 && else_instructions
.is_empty()
1855 && cond_val
!= NULL
) {
1856 result
= cond_val
->value
.b
[0] ? op
[1] : op
[2];
1858 /* The copy to conditional_tmp reads the whole array. */
1859 if (type
->is_array()) {
1860 mark_whole_array_access(op
[1]);
1861 mark_whole_array_access(op
[2]);
1864 ir_variable
*const tmp
=
1865 new(ctx
) ir_variable(type
, "conditional_tmp", ir_var_temporary
);
1866 instructions
->push_tail(tmp
);
1868 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1869 instructions
->push_tail(stmt
);
1871 then_instructions
.move_nodes_to(& stmt
->then_instructions
);
1872 ir_dereference
*const then_deref
=
1873 new(ctx
) ir_dereference_variable(tmp
);
1874 ir_assignment
*const then_assign
=
1875 new(ctx
) ir_assignment(then_deref
, op
[1]);
1876 stmt
->then_instructions
.push_tail(then_assign
);
1878 else_instructions
.move_nodes_to(& stmt
->else_instructions
);
1879 ir_dereference
*const else_deref
=
1880 new(ctx
) ir_dereference_variable(tmp
);
1881 ir_assignment
*const else_assign
=
1882 new(ctx
) ir_assignment(else_deref
, op
[2]);
1883 stmt
->else_instructions
.push_tail(else_assign
);
1885 result
= new(ctx
) ir_dereference_variable(tmp
);
1892 this->non_lvalue_description
= (this->oper
== ast_pre_inc
)
1893 ? "pre-increment operation" : "pre-decrement operation";
1895 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1896 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1898 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1900 ir_rvalue
*temp_rhs
;
1901 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1905 do_assignment(instructions
, state
,
1906 this->subexpressions
[0]->non_lvalue_description
,
1907 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1908 &result
, needs_rvalue
, false,
1909 this->subexpressions
[0]->get_location());
1914 case ast_post_dec
: {
1915 this->non_lvalue_description
= (this->oper
== ast_post_inc
)
1916 ? "post-increment operation" : "post-decrement operation";
1917 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1918 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1920 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1922 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1924 ir_rvalue
*temp_rhs
;
1925 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1928 /* Get a temporary of a copy of the lvalue before it's modified.
1929 * This may get thrown away later.
1931 result
= get_lvalue_copy(instructions
, op
[0]->clone(ctx
, NULL
));
1933 ir_rvalue
*junk_rvalue
;
1935 do_assignment(instructions
, state
,
1936 this->subexpressions
[0]->non_lvalue_description
,
1937 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1938 &junk_rvalue
, false, false,
1939 this->subexpressions
[0]->get_location());
1944 case ast_field_selection
:
1945 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1948 case ast_array_index
: {
1949 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1951 /* Getting if an array is being used uninitialized is beyond what we get
1952 * from ir_value.data.assigned. Setting is_lhs as true would force to
1953 * not raise a uninitialized warning when using an array
1955 subexpressions
[0]->set_is_lhs(true);
1956 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1957 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1959 result
= _mesa_ast_array_index_to_hir(ctx
, state
, op
[0], op
[1],
1962 if (result
->type
->is_error())
1963 error_emitted
= true;
1968 case ast_unsized_array_dim
:
1969 assert(!"ast_unsized_array_dim: Should never get here.");
1972 case ast_function_call
:
1973 /* Should *NEVER* get here. ast_function_call should always be handled
1974 * by ast_function_expression::hir.
1979 case ast_identifier
: {
1980 /* ast_identifier can appear several places in a full abstract syntax
1981 * tree. This particular use must be at location specified in the grammar
1982 * as 'variable_identifier'.
1985 state
->symbols
->get_variable(this->primary_expression
.identifier
);
1988 /* the identifier might be a subroutine name */
1990 sub_name
= ralloc_asprintf(ctx
, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state
->stage
), this->primary_expression
.identifier
);
1991 var
= state
->symbols
->get_variable(sub_name
);
1992 ralloc_free(sub_name
);
1996 var
->data
.used
= true;
1997 result
= new(ctx
) ir_dereference_variable(var
);
1999 if ((var
->data
.mode
== ir_var_auto
|| var
->data
.mode
== ir_var_shader_out
)
2001 && result
->variable_referenced()->data
.assigned
!= true
2002 && !is_gl_identifier(var
->name
)) {
2003 _mesa_glsl_warning(&loc
, state
, "`%s' used uninitialized",
2004 this->primary_expression
.identifier
);
2007 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
2008 this->primary_expression
.identifier
);
2010 result
= ir_rvalue::error_value(ctx
);
2011 error_emitted
= true;
2016 case ast_int_constant
:
2017 result
= new(ctx
) ir_constant(this->primary_expression
.int_constant
);
2020 case ast_uint_constant
:
2021 result
= new(ctx
) ir_constant(this->primary_expression
.uint_constant
);
2024 case ast_float_constant
:
2025 result
= new(ctx
) ir_constant(this->primary_expression
.float_constant
);
2028 case ast_bool_constant
:
2029 result
= new(ctx
) ir_constant(bool(this->primary_expression
.bool_constant
));
2032 case ast_double_constant
:
2033 result
= new(ctx
) ir_constant(this->primary_expression
.double_constant
);
2036 case ast_uint64_constant
:
2037 result
= new(ctx
) ir_constant(this->primary_expression
.uint64_constant
);
2040 case ast_int64_constant
:
2041 result
= new(ctx
) ir_constant(this->primary_expression
.int64_constant
);
2044 case ast_sequence
: {
2045 /* It should not be possible to generate a sequence in the AST without
2046 * any expressions in it.
2048 assert(!this->expressions
.is_empty());
2050 /* The r-value of a sequence is the last expression in the sequence. If
2051 * the other expressions in the sequence do not have side-effects (and
2052 * therefore add instructions to the instruction list), they get dropped
2055 exec_node
*previous_tail
= NULL
;
2056 YYLTYPE previous_operand_loc
= loc
;
2058 foreach_list_typed (ast_node
, ast
, link
, &this->expressions
) {
2059 /* If one of the operands of comma operator does not generate any
2060 * code, we want to emit a warning. At each pass through the loop
2061 * previous_tail will point to the last instruction in the stream
2062 * *before* processing the previous operand. Naturally,
2063 * instructions->get_tail_raw() will point to the last instruction in
2064 * the stream *after* processing the previous operand. If the two
2065 * pointers match, then the previous operand had no effect.
2067 * The warning behavior here differs slightly from GCC. GCC will
2068 * only emit a warning if none of the left-hand operands have an
2069 * effect. However, it will emit a warning for each. I believe that
2070 * there are some cases in C (especially with GCC extensions) where
2071 * it is useful to have an intermediate step in a sequence have no
2072 * effect, but I don't think these cases exist in GLSL. Either way,
2073 * it would be a giant hassle to replicate that behavior.
2075 if (previous_tail
== instructions
->get_tail_raw()) {
2076 _mesa_glsl_warning(&previous_operand_loc
, state
,
2077 "left-hand operand of comma expression has "
2081 /* The tail is directly accessed instead of using the get_tail()
2082 * method for performance reasons. get_tail() has extra code to
2083 * return NULL when the list is empty. We don't care about that
2084 * here, so using get_tail_raw() is fine.
2086 previous_tail
= instructions
->get_tail_raw();
2087 previous_operand_loc
= ast
->get_location();
2089 result
= ast
->hir(instructions
, state
);
2092 /* Any errors should have already been emitted in the loop above.
2094 error_emitted
= true;
2098 type
= NULL
; /* use result->type, not type. */
2099 assert(result
!= NULL
|| !needs_rvalue
);
2101 if (result
&& result
->type
->is_error() && !error_emitted
)
2102 _mesa_glsl_error(& loc
, state
, "type mismatch");
2108 ast_expression::has_sequence_subexpression() const
2110 switch (this->oper
) {
2119 return this->subexpressions
[0]->has_sequence_subexpression();
2141 case ast_array_index
:
2142 case ast_mul_assign
:
2143 case ast_div_assign
:
2144 case ast_add_assign
:
2145 case ast_sub_assign
:
2146 case ast_mod_assign
:
2149 case ast_and_assign
:
2150 case ast_xor_assign
:
2152 return this->subexpressions
[0]->has_sequence_subexpression() ||
2153 this->subexpressions
[1]->has_sequence_subexpression();
2155 case ast_conditional
:
2156 return this->subexpressions
[0]->has_sequence_subexpression() ||
2157 this->subexpressions
[1]->has_sequence_subexpression() ||
2158 this->subexpressions
[2]->has_sequence_subexpression();
2163 case ast_field_selection
:
2164 case ast_identifier
:
2165 case ast_int_constant
:
2166 case ast_uint_constant
:
2167 case ast_float_constant
:
2168 case ast_bool_constant
:
2169 case ast_double_constant
:
2170 case ast_int64_constant
:
2171 case ast_uint64_constant
:
2177 case ast_function_call
:
2178 unreachable("should be handled by ast_function_expression::hir");
2180 case ast_unsized_array_dim
:
2181 unreachable("ast_unsized_array_dim: Should never get here.");
2188 ast_expression_statement::hir(exec_list
*instructions
,
2189 struct _mesa_glsl_parse_state
*state
)
2191 /* It is possible to have expression statements that don't have an
2192 * expression. This is the solitary semicolon:
2194 * for (i = 0; i < 5; i++)
2197 * In this case the expression will be NULL. Test for NULL and don't do
2198 * anything in that case.
2200 if (expression
!= NULL
)
2201 expression
->hir_no_rvalue(instructions
, state
);
2203 /* Statements do not have r-values.
2210 ast_compound_statement::hir(exec_list
*instructions
,
2211 struct _mesa_glsl_parse_state
*state
)
2214 state
->symbols
->push_scope();
2216 foreach_list_typed (ast_node
, ast
, link
, &this->statements
)
2217 ast
->hir(instructions
, state
);
2220 state
->symbols
->pop_scope();
2222 /* Compound statements do not have r-values.
2228 * Evaluate the given exec_node (which should be an ast_node representing
2229 * a single array dimension) and return its integer value.
2232 process_array_size(exec_node
*node
,
2233 struct _mesa_glsl_parse_state
*state
)
2235 exec_list dummy_instructions
;
2237 ast_node
*array_size
= exec_node_data(ast_node
, node
, link
);
2240 * Dimensions other than the outermost dimension can by unsized if they
2241 * are immediately sized by a constructor or initializer.
2243 if (((ast_expression
*)array_size
)->oper
== ast_unsized_array_dim
)
2246 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
2247 YYLTYPE loc
= array_size
->get_location();
2250 _mesa_glsl_error(& loc
, state
,
2251 "array size could not be resolved");
2255 if (!ir
->type
->is_integer()) {
2256 _mesa_glsl_error(& loc
, state
,
2257 "array size must be integer type");
2261 if (!ir
->type
->is_scalar()) {
2262 _mesa_glsl_error(& loc
, state
,
2263 "array size must be scalar type");
2267 ir_constant
*const size
= ir
->constant_expression_value();
2269 (state
->is_version(120, 300) &&
2270 array_size
->has_sequence_subexpression())) {
2271 _mesa_glsl_error(& loc
, state
, "array size must be a "
2272 "constant valued expression");
2276 if (size
->value
.i
[0] <= 0) {
2277 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
2281 assert(size
->type
== ir
->type
);
2283 /* If the array size is const (and we've verified that
2284 * it is) then no instructions should have been emitted
2285 * when we converted it to HIR. If they were emitted,
2286 * then either the array size isn't const after all, or
2287 * we are emitting unnecessary instructions.
2289 assert(dummy_instructions
.is_empty());
2291 return size
->value
.u
[0];
2294 static const glsl_type
*
2295 process_array_type(YYLTYPE
*loc
, const glsl_type
*base
,
2296 ast_array_specifier
*array_specifier
,
2297 struct _mesa_glsl_parse_state
*state
)
2299 const glsl_type
*array_type
= base
;
2301 if (array_specifier
!= NULL
) {
2302 if (base
->is_array()) {
2304 /* From page 19 (page 25) of the GLSL 1.20 spec:
2306 * "Only one-dimensional arrays may be declared."
2308 if (!state
->check_arrays_of_arrays_allowed(loc
)) {
2309 return glsl_type::error_type
;
2313 for (exec_node
*node
= array_specifier
->array_dimensions
.get_tail_raw();
2314 !node
->is_head_sentinel(); node
= node
->prev
) {
2315 unsigned array_size
= process_array_size(node
, state
);
2316 array_type
= glsl_type::get_array_instance(array_type
, array_size
);
2324 precision_qualifier_allowed(const glsl_type
*type
)
2326 /* Precision qualifiers apply to floating point, integer and opaque
2329 * Section 4.5.2 (Precision Qualifiers) of the GLSL 1.30 spec says:
2330 * "Any floating point or any integer declaration can have the type
2331 * preceded by one of these precision qualifiers [...] Literal
2332 * constants do not have precision qualifiers. Neither do Boolean
2335 * Section 4.5 (Precision and Precision Qualifiers) of the GLSL 1.30
2338 * "Precision qualifiers are added for code portability with OpenGL
2339 * ES, not for functionality. They have the same syntax as in OpenGL
2342 * Section 8 (Built-In Functions) of the GLSL ES 1.00 spec says:
2344 * "uniform lowp sampler2D sampler;
2347 * lowp vec4 col = texture2D (sampler, coord);
2348 * // texture2D returns lowp"
2350 * From this, we infer that GLSL 1.30 (and later) should allow precision
2351 * qualifiers on sampler types just like float and integer types.
2353 const glsl_type
*const t
= type
->without_array();
2355 return (t
->is_float() || t
->is_integer() || t
->contains_opaque()) &&
2360 ast_type_specifier::glsl_type(const char **name
,
2361 struct _mesa_glsl_parse_state
*state
) const
2363 const struct glsl_type
*type
;
2365 type
= state
->symbols
->get_type(this->type_name
);
2366 *name
= this->type_name
;
2368 YYLTYPE loc
= this->get_location();
2369 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
2375 * From the OpenGL ES 3.0 spec, 4.5.4 Default Precision Qualifiers:
2377 * "The precision statement
2379 * precision precision-qualifier type;
2381 * can be used to establish a default precision qualifier. The type field can
2382 * be either int or float or any of the sampler types, (...) If type is float,
2383 * the directive applies to non-precision-qualified floating point type
2384 * (scalar, vector, and matrix) declarations. If type is int, the directive
2385 * applies to all non-precision-qualified integer type (scalar, vector, signed,
2386 * and unsigned) declarations."
2388 * We use the symbol table to keep the values of the default precisions for
2389 * each 'type' in each scope and we use the 'type' string from the precision
2390 * statement as key in the symbol table. When we want to retrieve the default
2391 * precision associated with a given glsl_type we need to know the type string
2392 * associated with it. This is what this function returns.
2395 get_type_name_for_precision_qualifier(const glsl_type
*type
)
2397 switch (type
->base_type
) {
2398 case GLSL_TYPE_FLOAT
:
2400 case GLSL_TYPE_UINT
:
2403 case GLSL_TYPE_ATOMIC_UINT
:
2404 return "atomic_uint";
2405 case GLSL_TYPE_IMAGE
:
2407 case GLSL_TYPE_SAMPLER
: {
2408 const unsigned type_idx
=
2409 type
->sampler_array
+ 2 * type
->sampler_shadow
;
2410 const unsigned offset
= type
->base_type
== GLSL_TYPE_SAMPLER
? 0 : 4;
2411 assert(type_idx
< 4);
2412 switch (type
->sampled_type
) {
2413 case GLSL_TYPE_FLOAT
:
2414 switch (type
->sampler_dimensionality
) {
2415 case GLSL_SAMPLER_DIM_1D
: {
2416 assert(type
->base_type
== GLSL_TYPE_SAMPLER
);
2417 static const char *const names
[4] = {
2418 "sampler1D", "sampler1DArray",
2419 "sampler1DShadow", "sampler1DArrayShadow"
2421 return names
[type_idx
];
2423 case GLSL_SAMPLER_DIM_2D
: {
2424 static const char *const names
[8] = {
2425 "sampler2D", "sampler2DArray",
2426 "sampler2DShadow", "sampler2DArrayShadow",
2427 "image2D", "image2DArray", NULL
, NULL
2429 return names
[offset
+ type_idx
];
2431 case GLSL_SAMPLER_DIM_3D
: {
2432 static const char *const names
[8] = {
2433 "sampler3D", NULL
, NULL
, NULL
,
2434 "image3D", NULL
, NULL
, NULL
2436 return names
[offset
+ type_idx
];
2438 case GLSL_SAMPLER_DIM_CUBE
: {
2439 static const char *const names
[8] = {
2440 "samplerCube", "samplerCubeArray",
2441 "samplerCubeShadow", "samplerCubeArrayShadow",
2442 "imageCube", NULL
, NULL
, NULL
2444 return names
[offset
+ type_idx
];
2446 case GLSL_SAMPLER_DIM_MS
: {
2447 assert(type
->base_type
== GLSL_TYPE_SAMPLER
);
2448 static const char *const names
[4] = {
2449 "sampler2DMS", "sampler2DMSArray", NULL
, NULL
2451 return names
[type_idx
];
2453 case GLSL_SAMPLER_DIM_RECT
: {
2454 assert(type
->base_type
== GLSL_TYPE_SAMPLER
);
2455 static const char *const names
[4] = {
2456 "samplerRect", NULL
, "samplerRectShadow", NULL
2458 return names
[type_idx
];
2460 case GLSL_SAMPLER_DIM_BUF
: {
2461 static const char *const names
[8] = {
2462 "samplerBuffer", NULL
, NULL
, NULL
,
2463 "imageBuffer", NULL
, NULL
, NULL
2465 return names
[offset
+ type_idx
];
2467 case GLSL_SAMPLER_DIM_EXTERNAL
: {
2468 assert(type
->base_type
== GLSL_TYPE_SAMPLER
);
2469 static const char *const names
[4] = {
2470 "samplerExternalOES", NULL
, NULL
, NULL
2472 return names
[type_idx
];
2475 unreachable("Unsupported sampler/image dimensionality");
2476 } /* sampler/image float dimensionality */
2479 switch (type
->sampler_dimensionality
) {
2480 case GLSL_SAMPLER_DIM_1D
: {
2481 assert(type
->base_type
== GLSL_TYPE_SAMPLER
);
2482 static const char *const names
[4] = {
2483 "isampler1D", "isampler1DArray", NULL
, NULL
2485 return names
[type_idx
];
2487 case GLSL_SAMPLER_DIM_2D
: {
2488 static const char *const names
[8] = {
2489 "isampler2D", "isampler2DArray", NULL
, NULL
,
2490 "iimage2D", "iimage2DArray", NULL
, NULL
2492 return names
[offset
+ type_idx
];
2494 case GLSL_SAMPLER_DIM_3D
: {
2495 static const char *const names
[8] = {
2496 "isampler3D", NULL
, NULL
, NULL
,
2497 "iimage3D", NULL
, NULL
, NULL
2499 return names
[offset
+ type_idx
];
2501 case GLSL_SAMPLER_DIM_CUBE
: {
2502 static const char *const names
[8] = {
2503 "isamplerCube", "isamplerCubeArray", NULL
, NULL
,
2504 "iimageCube", NULL
, NULL
, NULL
2506 return names
[offset
+ type_idx
];
2508 case GLSL_SAMPLER_DIM_MS
: {
2509 assert(type
->base_type
== GLSL_TYPE_SAMPLER
);
2510 static const char *const names
[4] = {
2511 "isampler2DMS", "isampler2DMSArray", NULL
, NULL
2513 return names
[type_idx
];
2515 case GLSL_SAMPLER_DIM_RECT
: {
2516 assert(type
->base_type
== GLSL_TYPE_SAMPLER
);
2517 static const char *const names
[4] = {
2518 "isamplerRect", NULL
, "isamplerRectShadow", NULL
2520 return names
[type_idx
];
2522 case GLSL_SAMPLER_DIM_BUF
: {
2523 static const char *const names
[8] = {
2524 "isamplerBuffer", NULL
, NULL
, NULL
,
2525 "iimageBuffer", NULL
, NULL
, NULL
2527 return names
[offset
+ type_idx
];
2530 unreachable("Unsupported isampler/iimage dimensionality");
2531 } /* sampler/image int dimensionality */
2533 case GLSL_TYPE_UINT
:
2534 switch (type
->sampler_dimensionality
) {
2535 case GLSL_SAMPLER_DIM_1D
: {
2536 assert(type
->base_type
== GLSL_TYPE_SAMPLER
);
2537 static const char *const names
[4] = {
2538 "usampler1D", "usampler1DArray", NULL
, NULL
2540 return names
[type_idx
];
2542 case GLSL_SAMPLER_DIM_2D
: {
2543 static const char *const names
[8] = {
2544 "usampler2D", "usampler2DArray", NULL
, NULL
,
2545 "uimage2D", "uimage2DArray", NULL
, NULL
2547 return names
[offset
+ type_idx
];
2549 case GLSL_SAMPLER_DIM_3D
: {
2550 static const char *const names
[8] = {
2551 "usampler3D", NULL
, NULL
, NULL
,
2552 "uimage3D", NULL
, NULL
, NULL
2554 return names
[offset
+ type_idx
];
2556 case GLSL_SAMPLER_DIM_CUBE
: {
2557 static const char *const names
[8] = {
2558 "usamplerCube", "usamplerCubeArray", NULL
, NULL
,
2559 "uimageCube", NULL
, NULL
, NULL
2561 return names
[offset
+ type_idx
];
2563 case GLSL_SAMPLER_DIM_MS
: {
2564 assert(type
->base_type
== GLSL_TYPE_SAMPLER
);
2565 static const char *const names
[4] = {
2566 "usampler2DMS", "usampler2DMSArray", NULL
, NULL
2568 return names
[type_idx
];
2570 case GLSL_SAMPLER_DIM_RECT
: {
2571 assert(type
->base_type
== GLSL_TYPE_SAMPLER
);
2572 static const char *const names
[4] = {
2573 "usamplerRect", NULL
, "usamplerRectShadow", NULL
2575 return names
[type_idx
];
2577 case GLSL_SAMPLER_DIM_BUF
: {
2578 static const char *const names
[8] = {
2579 "usamplerBuffer", NULL
, NULL
, NULL
,
2580 "uimageBuffer", NULL
, NULL
, NULL
2582 return names
[offset
+ type_idx
];
2585 unreachable("Unsupported usampler/uimage dimensionality");
2586 } /* sampler/image uint dimensionality */
2589 unreachable("Unsupported sampler/image type");
2590 } /* sampler/image type */
2592 } /* GLSL_TYPE_SAMPLER/GLSL_TYPE_IMAGE */
2595 unreachable("Unsupported type");
2600 select_gles_precision(unsigned qual_precision
,
2601 const glsl_type
*type
,
2602 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
2604 /* Precision qualifiers do not have any meaning in Desktop GLSL.
2605 * In GLES we take the precision from the type qualifier if present,
2606 * otherwise, if the type of the variable allows precision qualifiers at
2607 * all, we look for the default precision qualifier for that type in the
2610 assert(state
->es_shader
);
2612 unsigned precision
= GLSL_PRECISION_NONE
;
2613 if (qual_precision
) {
2614 precision
= qual_precision
;
2615 } else if (precision_qualifier_allowed(type
)) {
2616 const char *type_name
=
2617 get_type_name_for_precision_qualifier(type
->without_array());
2618 assert(type_name
!= NULL
);
2621 state
->symbols
->get_default_precision_qualifier(type_name
);
2622 if (precision
== ast_precision_none
) {
2623 _mesa_glsl_error(loc
, state
,
2624 "No precision specified in this scope for type `%s'",
2630 /* Section 4.1.7.3 (Atomic Counters) of the GLSL ES 3.10 spec says:
2632 * "The default precision of all atomic types is highp. It is an error to
2633 * declare an atomic type with a different precision or to specify the
2634 * default precision for an atomic type to be lowp or mediump."
2636 if (type
->base_type
== GLSL_TYPE_ATOMIC_UINT
&&
2637 precision
!= ast_precision_high
) {
2638 _mesa_glsl_error(loc
, state
,
2639 "atomic_uint can only have highp precision qualifier");
2646 ast_fully_specified_type::glsl_type(const char **name
,
2647 struct _mesa_glsl_parse_state
*state
) const
2649 return this->specifier
->glsl_type(name
, state
);
2653 * Determine whether a toplevel variable declaration declares a varying. This
2654 * function operates by examining the variable's mode and the shader target,
2655 * so it correctly identifies linkage variables regardless of whether they are
2656 * declared using the deprecated "varying" syntax or the new "in/out" syntax.
2658 * Passing a non-toplevel variable declaration (e.g. a function parameter) to
2659 * this function will produce undefined results.
2662 is_varying_var(ir_variable
*var
, gl_shader_stage target
)
2665 case MESA_SHADER_VERTEX
:
2666 return var
->data
.mode
== ir_var_shader_out
;
2667 case MESA_SHADER_FRAGMENT
:
2668 return var
->data
.mode
== ir_var_shader_in
;
2670 return var
->data
.mode
== ir_var_shader_out
|| var
->data
.mode
== ir_var_shader_in
;
2675 is_allowed_invariant(ir_variable
*var
, struct _mesa_glsl_parse_state
*state
)
2677 if (is_varying_var(var
, state
->stage
))
2680 /* From Section 4.6.1 ("The Invariant Qualifier") GLSL 1.20 spec:
2681 * "Only variables output from a vertex shader can be candidates
2684 if (!state
->is_version(130, 0))
2688 * Later specs remove this language - so allowed invariant
2689 * on fragment shader outputs as well.
2691 if (state
->stage
== MESA_SHADER_FRAGMENT
&&
2692 var
->data
.mode
== ir_var_shader_out
)
2698 * Matrix layout qualifiers are only allowed on certain types
2701 validate_matrix_layout_for_type(struct _mesa_glsl_parse_state
*state
,
2703 const glsl_type
*type
,
2706 if (var
&& !var
->is_in_buffer_block()) {
2707 /* Layout qualifiers may only apply to interface blocks and fields in
2710 _mesa_glsl_error(loc
, state
,
2711 "uniform block layout qualifiers row_major and "
2712 "column_major may not be applied to variables "
2713 "outside of uniform blocks");
2714 } else if (!type
->without_array()->is_matrix()) {
2715 /* The OpenGL ES 3.0 conformance tests did not originally allow
2716 * matrix layout qualifiers on non-matrices. However, the OpenGL
2717 * 4.4 and OpenGL ES 3.0 (revision TBD) specifications were
2718 * amended to specifically allow these layouts on all types. Emit
2719 * a warning so that people know their code may not be portable.
2721 _mesa_glsl_warning(loc
, state
,
2722 "uniform block layout qualifiers row_major and "
2723 "column_major applied to non-matrix types may "
2724 "be rejected by older compilers");
2729 validate_xfb_buffer_qualifier(YYLTYPE
*loc
,
2730 struct _mesa_glsl_parse_state
*state
,
2731 unsigned xfb_buffer
) {
2732 if (xfb_buffer
>= state
->Const
.MaxTransformFeedbackBuffers
) {
2733 _mesa_glsl_error(loc
, state
,
2734 "invalid xfb_buffer specified %d is larger than "
2735 "MAX_TRANSFORM_FEEDBACK_BUFFERS - 1 (%d).",
2737 state
->Const
.MaxTransformFeedbackBuffers
- 1);
2744 /* From the ARB_enhanced_layouts spec:
2746 * "Variables and block members qualified with *xfb_offset* can be
2747 * scalars, vectors, matrices, structures, and (sized) arrays of these.
2748 * The offset must be a multiple of the size of the first component of
2749 * the first qualified variable or block member, or a compile-time error
2750 * results. Further, if applied to an aggregate containing a double,
2751 * the offset must also be a multiple of 8, and the space taken in the
2752 * buffer will be a multiple of 8.
2755 validate_xfb_offset_qualifier(YYLTYPE
*loc
,
2756 struct _mesa_glsl_parse_state
*state
,
2757 int xfb_offset
, const glsl_type
*type
,
2758 unsigned component_size
) {
2759 const glsl_type
*t_without_array
= type
->without_array();
2761 if (xfb_offset
!= -1 && type
->is_unsized_array()) {
2762 _mesa_glsl_error(loc
, state
,
2763 "xfb_offset can't be used with unsized arrays.");
2767 /* Make sure nested structs don't contain unsized arrays, and validate
2768 * any xfb_offsets on interface members.
2770 if (t_without_array
->is_record() || t_without_array
->is_interface())
2771 for (unsigned int i
= 0; i
< t_without_array
->length
; i
++) {
2772 const glsl_type
*member_t
= t_without_array
->fields
.structure
[i
].type
;
2774 /* When the interface block doesn't have an xfb_offset qualifier then
2775 * we apply the component size rules at the member level.
2777 if (xfb_offset
== -1)
2778 component_size
= member_t
->contains_double() ? 8 : 4;
2780 int xfb_offset
= t_without_array
->fields
.structure
[i
].offset
;
2781 validate_xfb_offset_qualifier(loc
, state
, xfb_offset
, member_t
,
2785 /* Nested structs or interface block without offset may not have had an
2786 * offset applied yet so return.
2788 if (xfb_offset
== -1) {
2792 if (xfb_offset
% component_size
) {
2793 _mesa_glsl_error(loc
, state
,
2794 "invalid qualifier xfb_offset=%d must be a multiple "
2795 "of the first component size of the first qualified "
2796 "variable or block member. Or double if an aggregate "
2797 "that contains a double (%d).",
2798 xfb_offset
, component_size
);
2806 validate_stream_qualifier(YYLTYPE
*loc
, struct _mesa_glsl_parse_state
*state
,
2809 if (stream
>= state
->ctx
->Const
.MaxVertexStreams
) {
2810 _mesa_glsl_error(loc
, state
,
2811 "invalid stream specified %d is larger than "
2812 "MAX_VERTEX_STREAMS - 1 (%d).",
2813 stream
, state
->ctx
->Const
.MaxVertexStreams
- 1);
2821 apply_explicit_binding(struct _mesa_glsl_parse_state
*state
,
2824 const glsl_type
*type
,
2825 const ast_type_qualifier
*qual
)
2827 if (!qual
->flags
.q
.uniform
&& !qual
->flags
.q
.buffer
) {
2828 _mesa_glsl_error(loc
, state
,
2829 "the \"binding\" qualifier only applies to uniforms and "
2830 "shader storage buffer objects");
2834 unsigned qual_binding
;
2835 if (!process_qualifier_constant(state
, loc
, "binding", qual
->binding
,
2840 const struct gl_context
*const ctx
= state
->ctx
;
2841 unsigned elements
= type
->is_array() ? type
->arrays_of_arrays_size() : 1;
2842 unsigned max_index
= qual_binding
+ elements
- 1;
2843 const glsl_type
*base_type
= type
->without_array();
2845 if (base_type
->is_interface()) {
2846 /* UBOs. From page 60 of the GLSL 4.20 specification:
2847 * "If the binding point for any uniform block instance is less than zero,
2848 * or greater than or equal to the implementation-dependent maximum
2849 * number of uniform buffer bindings, a compilation error will occur.
2850 * When the binding identifier is used with a uniform block instanced as
2851 * an array of size N, all elements of the array from binding through
2852 * binding + N – 1 must be within this range."
2854 * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
2856 if (qual
->flags
.q
.uniform
&&
2857 max_index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2858 _mesa_glsl_error(loc
, state
, "layout(binding = %u) for %d UBOs exceeds "
2859 "the maximum number of UBO binding points (%d)",
2860 qual_binding
, elements
,
2861 ctx
->Const
.MaxUniformBufferBindings
);
2865 /* SSBOs. From page 67 of the GLSL 4.30 specification:
2866 * "If the binding point for any uniform or shader storage block instance
2867 * is less than zero, or greater than or equal to the
2868 * implementation-dependent maximum number of uniform buffer bindings, a
2869 * compile-time error will occur. When the binding identifier is used
2870 * with a uniform or shader storage block instanced as an array of size
2871 * N, all elements of the array from binding through binding + N – 1 must
2872 * be within this range."
2874 if (qual
->flags
.q
.buffer
&&
2875 max_index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2876 _mesa_glsl_error(loc
, state
, "layout(binding = %u) for %d SSBOs exceeds "
2877 "the maximum number of SSBO binding points (%d)",
2878 qual_binding
, elements
,
2879 ctx
->Const
.MaxShaderStorageBufferBindings
);
2882 } else if (base_type
->is_sampler()) {
2883 /* Samplers. From page 63 of the GLSL 4.20 specification:
2884 * "If the binding is less than zero, or greater than or equal to the
2885 * implementation-dependent maximum supported number of units, a
2886 * compilation error will occur. When the binding identifier is used
2887 * with an array of size N, all elements of the array from binding
2888 * through binding + N - 1 must be within this range."
2890 unsigned limit
= ctx
->Const
.MaxCombinedTextureImageUnits
;
2892 if (max_index
>= limit
) {
2893 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d samplers "
2894 "exceeds the maximum number of texture image units "
2895 "(%u)", qual_binding
, elements
, limit
);
2899 } else if (base_type
->contains_atomic()) {
2900 assert(ctx
->Const
.MaxAtomicBufferBindings
<= MAX_COMBINED_ATOMIC_BUFFERS
);
2901 if (qual_binding
>= ctx
->Const
.MaxAtomicBufferBindings
) {
2902 _mesa_glsl_error(loc
, state
, "layout(binding = %d) exceeds the "
2903 " maximum number of atomic counter buffer bindings"
2904 "(%u)", qual_binding
,
2905 ctx
->Const
.MaxAtomicBufferBindings
);
2909 } else if ((state
->is_version(420, 310) ||
2910 state
->ARB_shading_language_420pack_enable
) &&
2911 base_type
->is_image()) {
2912 assert(ctx
->Const
.MaxImageUnits
<= MAX_IMAGE_UNITS
);
2913 if (max_index
>= ctx
->Const
.MaxImageUnits
) {
2914 _mesa_glsl_error(loc
, state
, "Image binding %d exceeds the "
2915 " maximum number of image units (%d)", max_index
,
2916 ctx
->Const
.MaxImageUnits
);
2921 _mesa_glsl_error(loc
, state
,
2922 "the \"binding\" qualifier only applies to uniform "
2923 "blocks, opaque variables, or arrays thereof");
2927 var
->data
.explicit_binding
= true;
2928 var
->data
.binding
= qual_binding
;
2935 validate_interpolation_qualifier(struct _mesa_glsl_parse_state
*state
,
2937 const glsl_interp_mode interpolation
,
2938 const struct ast_type_qualifier
*qual
,
2939 const struct glsl_type
*var_type
,
2940 ir_variable_mode mode
)
2942 /* Interpolation qualifiers can only apply to shader inputs or outputs, but
2943 * not to vertex shader inputs nor fragment shader outputs.
2945 * From section 4.3 ("Storage Qualifiers") of the GLSL 1.30 spec:
2946 * "Outputs from a vertex shader (out) and inputs to a fragment
2947 * shader (in) can be further qualified with one or more of these
2948 * interpolation qualifiers"
2950 * "These interpolation qualifiers may only precede the qualifiers in,
2951 * centroid in, out, or centroid out in a declaration. They do not apply
2952 * to the deprecated storage qualifiers varying or centroid
2953 * varying. They also do not apply to inputs into a vertex shader or
2954 * outputs from a fragment shader."
2956 * From section 4.3 ("Storage Qualifiers") of the GLSL ES 3.00 spec:
2957 * "Outputs from a shader (out) and inputs to a shader (in) can be
2958 * further qualified with one of these interpolation qualifiers."
2960 * "These interpolation qualifiers may only precede the qualifiers
2961 * in, centroid in, out, or centroid out in a declaration. They do
2962 * not apply to inputs into a vertex shader or outputs from a
2965 if (state
->is_version(130, 300)
2966 && interpolation
!= INTERP_MODE_NONE
) {
2967 const char *i
= interpolation_string(interpolation
);
2968 if (mode
!= ir_var_shader_in
&& mode
!= ir_var_shader_out
)
2969 _mesa_glsl_error(loc
, state
,
2970 "interpolation qualifier `%s' can only be applied to "
2971 "shader inputs or outputs.", i
);
2973 switch (state
->stage
) {
2974 case MESA_SHADER_VERTEX
:
2975 if (mode
== ir_var_shader_in
) {
2976 _mesa_glsl_error(loc
, state
,
2977 "interpolation qualifier '%s' cannot be applied to "
2978 "vertex shader inputs", i
);
2981 case MESA_SHADER_FRAGMENT
:
2982 if (mode
== ir_var_shader_out
) {
2983 _mesa_glsl_error(loc
, state
,
2984 "interpolation qualifier '%s' cannot be applied to "
2985 "fragment shader outputs", i
);
2993 /* Interpolation qualifiers cannot be applied to 'centroid' and
2994 * 'centroid varying'.
2996 * From section 4.3 ("Storage Qualifiers") of the GLSL 1.30 spec:
2997 * "interpolation qualifiers may only precede the qualifiers in,
2998 * centroid in, out, or centroid out in a declaration. They do not apply
2999 * to the deprecated storage qualifiers varying or centroid varying."
3001 * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
3003 if (state
->is_version(130, 0)
3004 && interpolation
!= INTERP_MODE_NONE
3005 && qual
->flags
.q
.varying
) {
3007 const char *i
= interpolation_string(interpolation
);
3009 if (qual
->flags
.q
.centroid
)
3010 s
= "centroid varying";
3014 _mesa_glsl_error(loc
, state
,
3015 "qualifier '%s' cannot be applied to the "
3016 "deprecated storage qualifier '%s'", i
, s
);
3019 /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
3020 * so must integer vertex outputs.
3022 * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
3023 * "Fragment shader inputs that are signed or unsigned integers or
3024 * integer vectors must be qualified with the interpolation qualifier
3027 * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
3028 * "Fragment shader inputs that are, or contain, signed or unsigned
3029 * integers or integer vectors must be qualified with the
3030 * interpolation qualifier flat."
3032 * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
3033 * "Vertex shader outputs that are, or contain, signed or unsigned
3034 * integers or integer vectors must be qualified with the
3035 * interpolation qualifier flat."
3037 * Note that prior to GLSL 1.50, this requirement applied to vertex
3038 * outputs rather than fragment inputs. That creates problems in the
3039 * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
3040 * desktop GL shaders. For GLSL ES shaders, we follow the spec and
3041 * apply the restriction to both vertex outputs and fragment inputs.
3043 * Note also that the desktop GLSL specs are missing the text "or
3044 * contain"; this is presumably an oversight, since there is no
3045 * reasonable way to interpolate a fragment shader input that contains
3046 * an integer. See Khronos bug #15671.
3048 if (state
->is_version(130, 300)
3049 && var_type
->contains_integer()
3050 && interpolation
!= INTERP_MODE_FLAT
3051 && state
->stage
== MESA_SHADER_FRAGMENT
3052 && mode
== ir_var_shader_in
) {
3053 _mesa_glsl_error(loc
, state
, "if a fragment input is (or contains) "
3054 "an integer, then it must be qualified with 'flat'");
3057 /* Double fragment inputs must be qualified with 'flat'.
3059 * From the "Overview" of the ARB_gpu_shader_fp64 extension spec:
3060 * "This extension does not support interpolation of double-precision
3061 * values; doubles used as fragment shader inputs must be qualified as
3064 * From section 4.3.4 ("Inputs") of the GLSL 4.00 spec:
3065 * "Fragment shader inputs that are signed or unsigned integers, integer
3066 * vectors, or any double-precision floating-point type must be
3067 * qualified with the interpolation qualifier flat."
3069 * Note that the GLSL specs are missing the text "or contain"; this is
3070 * presumably an oversight. See Khronos bug #15671.
3072 * The 'double' type does not exist in GLSL ES so far.
3074 if (state
->has_double()
3075 && var_type
->contains_double()
3076 && interpolation
!= INTERP_MODE_FLAT
3077 && state
->stage
== MESA_SHADER_FRAGMENT
3078 && mode
== ir_var_shader_in
) {
3079 _mesa_glsl_error(loc
, state
, "if a fragment input is (or contains) "
3080 "a double, then it must be qualified with 'flat'");
3084 static glsl_interp_mode
3085 interpret_interpolation_qualifier(const struct ast_type_qualifier
*qual
,
3086 const struct glsl_type
*var_type
,
3087 ir_variable_mode mode
,
3088 struct _mesa_glsl_parse_state
*state
,
3091 glsl_interp_mode interpolation
;
3092 if (qual
->flags
.q
.flat
)
3093 interpolation
= INTERP_MODE_FLAT
;
3094 else if (qual
->flags
.q
.noperspective
)
3095 interpolation
= INTERP_MODE_NOPERSPECTIVE
;
3096 else if (qual
->flags
.q
.smooth
)
3097 interpolation
= INTERP_MODE_SMOOTH
;
3098 else if (state
->es_shader
&&
3099 ((mode
== ir_var_shader_in
&&
3100 state
->stage
!= MESA_SHADER_VERTEX
) ||
3101 (mode
== ir_var_shader_out
&&
3102 state
->stage
!= MESA_SHADER_FRAGMENT
)))
3103 /* Section 4.3.9 (Interpolation) of the GLSL ES 3.00 spec says:
3105 * "When no interpolation qualifier is present, smooth interpolation
3108 interpolation
= INTERP_MODE_SMOOTH
;
3110 interpolation
= INTERP_MODE_NONE
;
3112 validate_interpolation_qualifier(state
, loc
,
3114 qual
, var_type
, mode
);
3116 return interpolation
;
3121 apply_explicit_location(const struct ast_type_qualifier
*qual
,
3123 struct _mesa_glsl_parse_state
*state
,
3128 unsigned qual_location
;
3129 if (!process_qualifier_constant(state
, loc
, "location", qual
->location
,
3134 /* Checks for GL_ARB_explicit_uniform_location. */
3135 if (qual
->flags
.q
.uniform
) {
3136 if (!state
->check_explicit_uniform_location_allowed(loc
, var
))
3139 const struct gl_context
*const ctx
= state
->ctx
;
3140 unsigned max_loc
= qual_location
+ var
->type
->uniform_locations() - 1;
3142 if (max_loc
>= ctx
->Const
.MaxUserAssignableUniformLocations
) {
3143 _mesa_glsl_error(loc
, state
, "location(s) consumed by uniform %s "
3144 ">= MAX_UNIFORM_LOCATIONS (%u)", var
->name
,
3145 ctx
->Const
.MaxUserAssignableUniformLocations
);
3149 var
->data
.explicit_location
= true;
3150 var
->data
.location
= qual_location
;
3154 /* Between GL_ARB_explicit_attrib_location an
3155 * GL_ARB_separate_shader_objects, the inputs and outputs of any shader
3156 * stage can be assigned explicit locations. The checking here associates
3157 * the correct extension with the correct stage's input / output:
3161 * vertex explicit_loc sso
3162 * tess control sso sso
3165 * fragment sso explicit_loc
3167 switch (state
->stage
) {
3168 case MESA_SHADER_VERTEX
:
3169 if (var
->data
.mode
== ir_var_shader_in
) {
3170 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
3176 if (var
->data
.mode
== ir_var_shader_out
) {
3177 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
3186 case MESA_SHADER_TESS_CTRL
:
3187 case MESA_SHADER_TESS_EVAL
:
3188 case MESA_SHADER_GEOMETRY
:
3189 if (var
->data
.mode
== ir_var_shader_in
|| var
->data
.mode
== ir_var_shader_out
) {
3190 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
3199 case MESA_SHADER_FRAGMENT
:
3200 if (var
->data
.mode
== ir_var_shader_in
) {
3201 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
3207 if (var
->data
.mode
== ir_var_shader_out
) {
3208 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
3217 case MESA_SHADER_COMPUTE
:
3218 _mesa_glsl_error(loc
, state
,
3219 "compute shader variables cannot be given "
3220 "explicit locations");
3225 _mesa_glsl_error(loc
, state
,
3226 "%s cannot be given an explicit location in %s shader",
3228 _mesa_shader_stage_to_string(state
->stage
));
3230 var
->data
.explicit_location
= true;
3232 switch (state
->stage
) {
3233 case MESA_SHADER_VERTEX
:
3234 var
->data
.location
= (var
->data
.mode
== ir_var_shader_in
)
3235 ? (qual_location
+ VERT_ATTRIB_GENERIC0
)
3236 : (qual_location
+ VARYING_SLOT_VAR0
);
3239 case MESA_SHADER_TESS_CTRL
:
3240 case MESA_SHADER_TESS_EVAL
:
3241 case MESA_SHADER_GEOMETRY
:
3242 if (var
->data
.patch
)
3243 var
->data
.location
= qual_location
+ VARYING_SLOT_PATCH0
;
3245 var
->data
.location
= qual_location
+ VARYING_SLOT_VAR0
;
3248 case MESA_SHADER_FRAGMENT
:
3249 var
->data
.location
= (var
->data
.mode
== ir_var_shader_out
)
3250 ? (qual_location
+ FRAG_RESULT_DATA0
)
3251 : (qual_location
+ VARYING_SLOT_VAR0
);
3253 case MESA_SHADER_COMPUTE
:
3254 assert(!"Unexpected shader type");
3258 /* Check if index was set for the uniform instead of the function */
3259 if (qual
->flags
.q
.explicit_index
&& qual
->flags
.q
.subroutine
) {
3260 _mesa_glsl_error(loc
, state
, "an index qualifier can only be "
3261 "used with subroutine functions");
3265 unsigned qual_index
;
3266 if (qual
->flags
.q
.explicit_index
&&
3267 process_qualifier_constant(state
, loc
, "index", qual
->index
,
3269 /* From the GLSL 4.30 specification, section 4.4.2 (Output
3270 * Layout Qualifiers):
3272 * "It is also a compile-time error if a fragment shader
3273 * sets a layout index to less than 0 or greater than 1."
3275 * Older specifications don't mandate a behavior; we take
3276 * this as a clarification and always generate the error.
3278 if (qual_index
> 1) {
3279 _mesa_glsl_error(loc
, state
,
3280 "explicit index may only be 0 or 1");
3282 var
->data
.explicit_index
= true;
3283 var
->data
.index
= qual_index
;
3290 apply_image_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
3292 struct _mesa_glsl_parse_state
*state
,
3295 const glsl_type
*base_type
= var
->type
->without_array();
3297 if (base_type
->is_image()) {
3298 if (var
->data
.mode
!= ir_var_uniform
&&
3299 var
->data
.mode
!= ir_var_function_in
) {
3300 _mesa_glsl_error(loc
, state
, "image variables may only be declared as "
3301 "function parameters or uniform-qualified "
3302 "global variables");
3305 var
->data
.image_read_only
|= qual
->flags
.q
.read_only
;
3306 var
->data
.image_write_only
|= qual
->flags
.q
.write_only
;
3307 var
->data
.image_coherent
|= qual
->flags
.q
.coherent
;
3308 var
->data
.image_volatile
|= qual
->flags
.q
._volatile
;
3309 var
->data
.image_restrict
|= qual
->flags
.q
.restrict_flag
;
3310 var
->data
.read_only
= true;
3312 if (qual
->flags
.q
.explicit_image_format
) {
3313 if (var
->data
.mode
== ir_var_function_in
) {
3314 _mesa_glsl_error(loc
, state
, "format qualifiers cannot be "
3315 "used on image function parameters");
3318 if (qual
->image_base_type
!= base_type
->sampled_type
) {
3319 _mesa_glsl_error(loc
, state
, "format qualifier doesn't match the "
3320 "base data type of the image");
3323 var
->data
.image_format
= qual
->image_format
;
3325 if (var
->data
.mode
== ir_var_uniform
) {
3326 if (state
->es_shader
) {
3327 _mesa_glsl_error(loc
, state
, "all image uniforms "
3328 "must have a format layout qualifier");
3330 } else if (!qual
->flags
.q
.write_only
) {
3331 _mesa_glsl_error(loc
, state
, "image uniforms not qualified with "
3332 "`writeonly' must have a format layout "
3337 var
->data
.image_format
= GL_NONE
;
3340 /* From page 70 of the GLSL ES 3.1 specification:
3342 * "Except for image variables qualified with the format qualifiers
3343 * r32f, r32i, and r32ui, image variables must specify either memory
3344 * qualifier readonly or the memory qualifier writeonly."
3346 if (state
->es_shader
&&
3347 var
->data
.image_format
!= GL_R32F
&&
3348 var
->data
.image_format
!= GL_R32I
&&
3349 var
->data
.image_format
!= GL_R32UI
&&
3350 !var
->data
.image_read_only
&&
3351 !var
->data
.image_write_only
) {
3352 _mesa_glsl_error(loc
, state
, "image variables of format other than "
3353 "r32f, r32i or r32ui must be qualified `readonly' or "
3357 } else if (qual
->flags
.q
.read_only
||
3358 qual
->flags
.q
.write_only
||
3359 qual
->flags
.q
.coherent
||
3360 qual
->flags
.q
._volatile
||
3361 qual
->flags
.q
.restrict_flag
||
3362 qual
->flags
.q
.explicit_image_format
) {
3363 _mesa_glsl_error(loc
, state
, "memory qualifiers may only be applied to "
3368 static inline const char*
3369 get_layout_qualifier_string(bool origin_upper_left
, bool pixel_center_integer
)
3371 if (origin_upper_left
&& pixel_center_integer
)
3372 return "origin_upper_left, pixel_center_integer";
3373 else if (origin_upper_left
)
3374 return "origin_upper_left";
3375 else if (pixel_center_integer
)
3376 return "pixel_center_integer";
3382 is_conflicting_fragcoord_redeclaration(struct _mesa_glsl_parse_state
*state
,
3383 const struct ast_type_qualifier
*qual
)
3385 /* If gl_FragCoord was previously declared, and the qualifiers were
3386 * different in any way, return true.
3388 if (state
->fs_redeclares_gl_fragcoord
) {
3389 return (state
->fs_pixel_center_integer
!= qual
->flags
.q
.pixel_center_integer
3390 || state
->fs_origin_upper_left
!= qual
->flags
.q
.origin_upper_left
);
3397 validate_array_dimensions(const glsl_type
*t
,
3398 struct _mesa_glsl_parse_state
*state
,
3400 if (t
->is_array()) {
3401 t
= t
->fields
.array
;
3402 while (t
->is_array()) {
3403 if (t
->is_unsized_array()) {
3404 _mesa_glsl_error(loc
, state
,
3405 "only the outermost array dimension can "
3410 t
= t
->fields
.array
;
3416 apply_layout_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
3418 struct _mesa_glsl_parse_state
*state
,
3421 if (var
->name
!= NULL
&& strcmp(var
->name
, "gl_FragCoord") == 0) {
3423 /* Section 4.3.8.1, page 39 of GLSL 1.50 spec says:
3425 * "Within any shader, the first redeclarations of gl_FragCoord
3426 * must appear before any use of gl_FragCoord."
3428 * Generate a compiler error if above condition is not met by the
3431 ir_variable
*earlier
= state
->symbols
->get_variable("gl_FragCoord");
3432 if (earlier
!= NULL
&&
3433 earlier
->data
.used
&&
3434 !state
->fs_redeclares_gl_fragcoord
) {
3435 _mesa_glsl_error(loc
, state
,
3436 "gl_FragCoord used before its first redeclaration "
3437 "in fragment shader");
3440 /* Make sure all gl_FragCoord redeclarations specify the same layout
3443 if (is_conflicting_fragcoord_redeclaration(state
, qual
)) {
3444 const char *const qual_string
=
3445 get_layout_qualifier_string(qual
->flags
.q
.origin_upper_left
,
3446 qual
->flags
.q
.pixel_center_integer
);
3448 const char *const state_string
=
3449 get_layout_qualifier_string(state
->fs_origin_upper_left
,
3450 state
->fs_pixel_center_integer
);
3452 _mesa_glsl_error(loc
, state
,
3453 "gl_FragCoord redeclared with different layout "
3454 "qualifiers (%s) and (%s) ",
3458 state
->fs_origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
3459 state
->fs_pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
3460 state
->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers
=
3461 !qual
->flags
.q
.origin_upper_left
&& !qual
->flags
.q
.pixel_center_integer
;
3462 state
->fs_redeclares_gl_fragcoord
=
3463 state
->fs_origin_upper_left
||
3464 state
->fs_pixel_center_integer
||
3465 state
->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers
;
3468 var
->data
.pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
3469 var
->data
.origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
3470 if ((qual
->flags
.q
.origin_upper_left
|| qual
->flags
.q
.pixel_center_integer
)
3471 && (strcmp(var
->name
, "gl_FragCoord") != 0)) {
3472 const char *const qual_string
= (qual
->flags
.q
.origin_upper_left
)
3473 ? "origin_upper_left" : "pixel_center_integer";
3475 _mesa_glsl_error(loc
, state
,
3476 "layout qualifier `%s' can only be applied to "
3477 "fragment shader input `gl_FragCoord'",
3481 if (qual
->flags
.q
.explicit_location
) {
3482 apply_explicit_location(qual
, var
, state
, loc
);
3484 if (qual
->flags
.q
.explicit_component
) {
3485 unsigned qual_component
;
3486 if (process_qualifier_constant(state
, loc
, "component",
3487 qual
->component
, &qual_component
)) {
3488 const glsl_type
*type
= var
->type
->without_array();
3489 unsigned components
= type
->component_slots();
3491 if (type
->is_matrix() || type
->is_record()) {
3492 _mesa_glsl_error(loc
, state
, "component layout qualifier "
3493 "cannot be applied to a matrix, a structure, "
3494 "a block, or an array containing any of "
3496 } else if (qual_component
!= 0 &&
3497 (qual_component
+ components
- 1) > 3) {
3498 _mesa_glsl_error(loc
, state
, "component overflow (%u > 3)",
3499 (qual_component
+ components
- 1));
3500 } else if (qual_component
== 1 && type
->is_64bit()) {
3501 /* We don't bother checking for 3 as it should be caught by the
3502 * overflow check above.
3504 _mesa_glsl_error(loc
, state
, "doubles cannot begin at "
3505 "component 1 or 3");
3507 var
->data
.explicit_component
= true;
3508 var
->data
.location_frac
= qual_component
;
3512 } else if (qual
->flags
.q
.explicit_index
) {
3513 if (!qual
->flags
.q
.subroutine_def
)
3514 _mesa_glsl_error(loc
, state
,
3515 "explicit index requires explicit location");
3516 } else if (qual
->flags
.q
.explicit_component
) {
3517 _mesa_glsl_error(loc
, state
,
3518 "explicit component requires explicit location");
3521 if (qual
->flags
.q
.explicit_binding
) {
3522 apply_explicit_binding(state
, loc
, var
, var
->type
, qual
);
3525 if (state
->stage
== MESA_SHADER_GEOMETRY
&&
3526 qual
->flags
.q
.out
&& qual
->flags
.q
.stream
) {
3527 unsigned qual_stream
;
3528 if (process_qualifier_constant(state
, loc
, "stream", qual
->stream
,
3530 validate_stream_qualifier(loc
, state
, qual_stream
)) {
3531 var
->data
.stream
= qual_stream
;
3535 if (qual
->flags
.q
.out
&& qual
->flags
.q
.xfb_buffer
) {
3536 unsigned qual_xfb_buffer
;
3537 if (process_qualifier_constant(state
, loc
, "xfb_buffer",
3538 qual
->xfb_buffer
, &qual_xfb_buffer
) &&
3539 validate_xfb_buffer_qualifier(loc
, state
, qual_xfb_buffer
)) {
3540 var
->data
.xfb_buffer
= qual_xfb_buffer
;
3541 if (qual
->flags
.q
.explicit_xfb_buffer
)
3542 var
->data
.explicit_xfb_buffer
= true;
3546 if (qual
->flags
.q
.explicit_xfb_offset
) {
3547 unsigned qual_xfb_offset
;
3548 unsigned component_size
= var
->type
->contains_double() ? 8 : 4;
3550 if (process_qualifier_constant(state
, loc
, "xfb_offset",
3551 qual
->offset
, &qual_xfb_offset
) &&
3552 validate_xfb_offset_qualifier(loc
, state
, (int) qual_xfb_offset
,
3553 var
->type
, component_size
)) {
3554 var
->data
.offset
= qual_xfb_offset
;
3555 var
->data
.explicit_xfb_offset
= true;
3559 if (qual
->flags
.q
.explicit_xfb_stride
) {
3560 unsigned qual_xfb_stride
;
3561 if (process_qualifier_constant(state
, loc
, "xfb_stride",
3562 qual
->xfb_stride
, &qual_xfb_stride
)) {
3563 var
->data
.xfb_stride
= qual_xfb_stride
;
3564 var
->data
.explicit_xfb_stride
= true;
3568 if (var
->type
->contains_atomic()) {
3569 if (var
->data
.mode
== ir_var_uniform
) {
3570 if (var
->data
.explicit_binding
) {
3572 &state
->atomic_counter_offsets
[var
->data
.binding
];
3574 if (*offset
% ATOMIC_COUNTER_SIZE
)
3575 _mesa_glsl_error(loc
, state
,
3576 "misaligned atomic counter offset");
3578 var
->data
.offset
= *offset
;
3579 *offset
+= var
->type
->atomic_size();
3582 _mesa_glsl_error(loc
, state
,
3583 "atomic counters require explicit binding point");
3585 } else if (var
->data
.mode
!= ir_var_function_in
) {
3586 _mesa_glsl_error(loc
, state
, "atomic counters may only be declared as "
3587 "function parameters or uniform-qualified "
3588 "global variables");
3592 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
3593 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
3594 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
3595 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
3596 * These extensions and all following extensions that add the 'layout'
3597 * keyword have been modified to require the use of 'in' or 'out'.
3599 * The following extension do not allow the deprecated keywords:
3601 * GL_AMD_conservative_depth
3602 * GL_ARB_conservative_depth
3603 * GL_ARB_gpu_shader5
3604 * GL_ARB_separate_shader_objects
3605 * GL_ARB_tessellation_shader
3606 * GL_ARB_transform_feedback3
3607 * GL_ARB_uniform_buffer_object
3609 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
3610 * allow layout with the deprecated keywords.
3612 const bool relaxed_layout_qualifier_checking
=
3613 state
->ARB_fragment_coord_conventions_enable
;
3615 const bool uses_deprecated_qualifier
= qual
->flags
.q
.attribute
3616 || qual
->flags
.q
.varying
;
3617 if (qual
->has_layout() && uses_deprecated_qualifier
) {
3618 if (relaxed_layout_qualifier_checking
) {
3619 _mesa_glsl_warning(loc
, state
,
3620 "`layout' qualifier may not be used with "
3621 "`attribute' or `varying'");
3623 _mesa_glsl_error(loc
, state
,
3624 "`layout' qualifier may not be used with "
3625 "`attribute' or `varying'");
3629 /* Layout qualifiers for gl_FragDepth, which are enabled by extension
3630 * AMD_conservative_depth.
3632 int depth_layout_count
= qual
->flags
.q
.depth_any
3633 + qual
->flags
.q
.depth_greater
3634 + qual
->flags
.q
.depth_less
3635 + qual
->flags
.q
.depth_unchanged
;
3636 if (depth_layout_count
> 0
3637 && !state
->is_version(420, 0)
3638 && !state
->AMD_conservative_depth_enable
3639 && !state
->ARB_conservative_depth_enable
) {
3640 _mesa_glsl_error(loc
, state
,
3641 "extension GL_AMD_conservative_depth or "
3642 "GL_ARB_conservative_depth must be enabled "
3643 "to use depth layout qualifiers");
3644 } else if (depth_layout_count
> 0
3645 && strcmp(var
->name
, "gl_FragDepth") != 0) {
3646 _mesa_glsl_error(loc
, state
,
3647 "depth layout qualifiers can be applied only to "
3649 } else if (depth_layout_count
> 1
3650 && strcmp(var
->name
, "gl_FragDepth") == 0) {
3651 _mesa_glsl_error(loc
, state
,
3652 "at most one depth layout qualifier can be applied to "
3655 if (qual
->flags
.q
.depth_any
)
3656 var
->data
.depth_layout
= ir_depth_layout_any
;
3657 else if (qual
->flags
.q
.depth_greater
)
3658 var
->data
.depth_layout
= ir_depth_layout_greater
;
3659 else if (qual
->flags
.q
.depth_less
)
3660 var
->data
.depth_layout
= ir_depth_layout_less
;
3661 else if (qual
->flags
.q
.depth_unchanged
)
3662 var
->data
.depth_layout
= ir_depth_layout_unchanged
;
3664 var
->data
.depth_layout
= ir_depth_layout_none
;
3666 if (qual
->flags
.q
.std140
||
3667 qual
->flags
.q
.std430
||
3668 qual
->flags
.q
.packed
||
3669 qual
->flags
.q
.shared
) {
3670 _mesa_glsl_error(loc
, state
,
3671 "uniform and shader storage block layout qualifiers "
3672 "std140, std430, packed, and shared can only be "
3673 "applied to uniform or shader storage blocks, not "
3677 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
3678 validate_matrix_layout_for_type(state
, loc
, var
->type
, var
);
3681 /* From section 4.4.1.3 of the GLSL 4.50 specification (Fragment Shader
3684 * "Fragment shaders also allow the following layout qualifier on in only
3685 * (not with variable declarations)
3686 * layout-qualifier-id
3687 * early_fragment_tests
3690 if (qual
->flags
.q
.early_fragment_tests
) {
3691 _mesa_glsl_error(loc
, state
, "early_fragment_tests layout qualifier only "
3692 "valid in fragment shader input layout declaration.");
3695 if (qual
->flags
.q
.inner_coverage
) {
3696 _mesa_glsl_error(loc
, state
, "inner_coverage layout qualifier only "
3697 "valid in fragment shader input layout declaration.");
3700 if (qual
->flags
.q
.post_depth_coverage
) {
3701 _mesa_glsl_error(loc
, state
, "post_depth_coverage layout qualifier only "
3702 "valid in fragment shader input layout declaration.");
3707 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
3709 struct _mesa_glsl_parse_state
*state
,
3713 STATIC_ASSERT(sizeof(qual
->flags
.q
) <= sizeof(qual
->flags
.i
));
3715 if (qual
->flags
.q
.invariant
) {
3716 if (var
->data
.used
) {
3717 _mesa_glsl_error(loc
, state
,
3718 "variable `%s' may not be redeclared "
3719 "`invariant' after being used",
3722 var
->data
.invariant
= 1;
3726 if (qual
->flags
.q
.precise
) {
3727 if (var
->data
.used
) {
3728 _mesa_glsl_error(loc
, state
,
3729 "variable `%s' may not be redeclared "
3730 "`precise' after being used",
3733 var
->data
.precise
= 1;
3737 if (qual
->flags
.q
.subroutine
&& !qual
->flags
.q
.uniform
) {
3738 _mesa_glsl_error(loc
, state
,
3739 "`subroutine' may only be applied to uniforms, "
3740 "subroutine type declarations, or function definitions");
3743 if (qual
->flags
.q
.constant
|| qual
->flags
.q
.attribute
3744 || qual
->flags
.q
.uniform
3745 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
3746 var
->data
.read_only
= 1;
3748 if (qual
->flags
.q
.centroid
)
3749 var
->data
.centroid
= 1;
3751 if (qual
->flags
.q
.sample
)
3752 var
->data
.sample
= 1;
3754 /* Precision qualifiers do not hold any meaning in Desktop GLSL */
3755 if (state
->es_shader
) {
3756 var
->data
.precision
=
3757 select_gles_precision(qual
->precision
, var
->type
, state
, loc
);
3760 if (qual
->flags
.q
.patch
)
3761 var
->data
.patch
= 1;
3763 if (qual
->flags
.q
.attribute
&& state
->stage
!= MESA_SHADER_VERTEX
) {
3764 var
->type
= glsl_type::error_type
;
3765 _mesa_glsl_error(loc
, state
,
3766 "`attribute' variables may not be declared in the "
3768 _mesa_shader_stage_to_string(state
->stage
));
3771 /* Disallow layout qualifiers which may only appear on layout declarations. */
3772 if (qual
->flags
.q
.prim_type
) {
3773 _mesa_glsl_error(loc
, state
,
3774 "Primitive type may only be specified on GS input or output "
3775 "layout declaration, not on variables.");
3778 /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
3780 * "However, the const qualifier cannot be used with out or inout."
3782 * The same section of the GLSL 4.40 spec further clarifies this saying:
3784 * "The const qualifier cannot be used with out or inout, or a
3785 * compile-time error results."
3787 if (is_parameter
&& qual
->flags
.q
.constant
&& qual
->flags
.q
.out
) {
3788 _mesa_glsl_error(loc
, state
,
3789 "`const' may not be applied to `out' or `inout' "
3790 "function parameters");
3793 /* If there is no qualifier that changes the mode of the variable, leave
3794 * the setting alone.
3796 assert(var
->data
.mode
!= ir_var_temporary
);
3797 if (qual
->flags
.q
.in
&& qual
->flags
.q
.out
)
3798 var
->data
.mode
= is_parameter
? ir_var_function_inout
: ir_var_shader_out
;
3799 else if (qual
->flags
.q
.in
)
3800 var
->data
.mode
= is_parameter
? ir_var_function_in
: ir_var_shader_in
;
3801 else if (qual
->flags
.q
.attribute
3802 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
3803 var
->data
.mode
= ir_var_shader_in
;
3804 else if (qual
->flags
.q
.out
)
3805 var
->data
.mode
= is_parameter
? ir_var_function_out
: ir_var_shader_out
;
3806 else if (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_VERTEX
))
3807 var
->data
.mode
= ir_var_shader_out
;
3808 else if (qual
->flags
.q
.uniform
)
3809 var
->data
.mode
= ir_var_uniform
;
3810 else if (qual
->flags
.q
.buffer
)
3811 var
->data
.mode
= ir_var_shader_storage
;
3812 else if (qual
->flags
.q
.shared_storage
)
3813 var
->data
.mode
= ir_var_shader_shared
;
3815 var
->data
.fb_fetch_output
= state
->stage
== MESA_SHADER_FRAGMENT
&&
3816 qual
->flags
.q
.in
&& qual
->flags
.q
.out
;
3818 if (!is_parameter
&& is_varying_var(var
, state
->stage
)) {
3819 /* User-defined ins/outs are not permitted in compute shaders. */
3820 if (state
->stage
== MESA_SHADER_COMPUTE
) {
3821 _mesa_glsl_error(loc
, state
,
3822 "user-defined input and output variables are not "
3823 "permitted in compute shaders");
3826 /* This variable is being used to link data between shader stages (in
3827 * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
3828 * that is allowed for such purposes.
3830 * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
3832 * "The varying qualifier can be used only with the data types
3833 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
3836 * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
3837 * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
3839 * "Fragment inputs can only be signed and unsigned integers and
3840 * integer vectors, float, floating-point vectors, matrices, or
3841 * arrays of these. Structures cannot be input.
3843 * Similar text exists in the section on vertex shader outputs.
3845 * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
3846 * 3.00 spec allows structs as well. Varying structs are also allowed
3849 switch (var
->type
->get_scalar_type()->base_type
) {
3850 case GLSL_TYPE_FLOAT
:
3851 /* Ok in all GLSL versions */
3853 case GLSL_TYPE_UINT
:
3855 if (state
->is_version(130, 300))
3857 _mesa_glsl_error(loc
, state
,
3858 "varying variables must be of base type float in %s",
3859 state
->get_version_string());
3861 case GLSL_TYPE_STRUCT
:
3862 if (state
->is_version(150, 300))
3864 _mesa_glsl_error(loc
, state
,
3865 "varying variables may not be of type struct");
3867 case GLSL_TYPE_DOUBLE
:
3868 case GLSL_TYPE_UINT64
:
3869 case GLSL_TYPE_INT64
:
3872 _mesa_glsl_error(loc
, state
, "illegal type for a varying variable");
3877 if (state
->all_invariant
&& (state
->current_function
== NULL
)) {
3878 switch (state
->stage
) {
3879 case MESA_SHADER_VERTEX
:
3880 if (var
->data
.mode
== ir_var_shader_out
)
3881 var
->data
.invariant
= true;
3883 case MESA_SHADER_TESS_CTRL
:
3884 case MESA_SHADER_TESS_EVAL
:
3885 case MESA_SHADER_GEOMETRY
:
3886 if ((var
->data
.mode
== ir_var_shader_in
)
3887 || (var
->data
.mode
== ir_var_shader_out
))
3888 var
->data
.invariant
= true;
3890 case MESA_SHADER_FRAGMENT
:
3891 if (var
->data
.mode
== ir_var_shader_in
)
3892 var
->data
.invariant
= true;
3894 case MESA_SHADER_COMPUTE
:
3895 /* Invariance isn't meaningful in compute shaders. */
3900 var
->data
.interpolation
=
3901 interpret_interpolation_qualifier(qual
, var
->type
,
3902 (ir_variable_mode
) var
->data
.mode
,
3905 /* Does the declaration use the deprecated 'attribute' or 'varying'
3908 const bool uses_deprecated_qualifier
= qual
->flags
.q
.attribute
3909 || qual
->flags
.q
.varying
;
3912 /* Validate auxiliary storage qualifiers */
3914 /* From section 4.3.4 of the GLSL 1.30 spec:
3915 * "It is an error to use centroid in in a vertex shader."
3917 * From section 4.3.4 of the GLSL ES 3.00 spec:
3918 * "It is an error to use centroid in or interpolation qualifiers in
3919 * a vertex shader input."
3922 /* Section 4.3.6 of the GLSL 1.30 specification states:
3923 * "It is an error to use centroid out in a fragment shader."
3925 * The GL_ARB_shading_language_420pack extension specification states:
3926 * "It is an error to use auxiliary storage qualifiers or interpolation
3927 * qualifiers on an output in a fragment shader."
3929 if (qual
->flags
.q
.sample
&& (!is_varying_var(var
, state
->stage
) || uses_deprecated_qualifier
)) {
3930 _mesa_glsl_error(loc
, state
,
3931 "sample qualifier may only be used on `in` or `out` "
3932 "variables between shader stages");
3934 if (qual
->flags
.q
.centroid
&& !is_varying_var(var
, state
->stage
)) {
3935 _mesa_glsl_error(loc
, state
,
3936 "centroid qualifier may only be used with `in', "
3937 "`out' or `varying' variables between shader stages");
3940 if (qual
->flags
.q
.shared_storage
&& state
->stage
!= MESA_SHADER_COMPUTE
) {
3941 _mesa_glsl_error(loc
, state
,
3942 "the shared storage qualifiers can only be used with "
3946 apply_image_qualifier_to_variable(qual
, var
, state
, loc
);
3950 * Get the variable that is being redeclared by this declaration
3952 * Semantic checks to verify the validity of the redeclaration are also
3953 * performed. If semantic checks fail, compilation error will be emitted via
3954 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
3957 * A pointer to an existing variable in the current scope if the declaration
3958 * is a redeclaration, \c NULL otherwise.
3960 static ir_variable
*
3961 get_variable_being_redeclared(ir_variable
*var
, YYLTYPE loc
,
3962 struct _mesa_glsl_parse_state
*state
,
3963 bool allow_all_redeclarations
)
3965 /* Check if this declaration is actually a re-declaration, either to
3966 * resize an array or add qualifiers to an existing variable.
3968 * This is allowed for variables in the current scope, or when at
3969 * global scope (for built-ins in the implicit outer scope).
3971 ir_variable
*earlier
= state
->symbols
->get_variable(var
->name
);
3972 if (earlier
== NULL
||
3973 (state
->current_function
!= NULL
&&
3974 !state
->symbols
->name_declared_this_scope(var
->name
))) {
3979 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
3981 * "It is legal to declare an array without a size and then
3982 * later re-declare the same name as an array of the same
3983 * type and specify a size."
3985 if (earlier
->type
->is_unsized_array() && var
->type
->is_array()
3986 && (var
->type
->fields
.array
== earlier
->type
->fields
.array
)) {
3987 /* FINISHME: This doesn't match the qualifiers on the two
3988 * FINISHME: declarations. It's not 100% clear whether this is
3989 * FINISHME: required or not.
3992 const int size
= var
->type
->array_size();
3993 check_builtin_array_max_size(var
->name
, size
, loc
, state
);
3994 if ((size
> 0) && (size
<= earlier
->data
.max_array_access
)) {
3995 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
3997 earlier
->data
.max_array_access
);
4000 earlier
->type
= var
->type
;
4003 } else if ((state
->ARB_fragment_coord_conventions_enable
||
4004 state
->is_version(150, 0))
4005 && strcmp(var
->name
, "gl_FragCoord") == 0
4006 && earlier
->type
== var
->type
4007 && var
->data
.mode
== ir_var_shader_in
) {
4008 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
4011 earlier
->data
.origin_upper_left
= var
->data
.origin_upper_left
;
4012 earlier
->data
.pixel_center_integer
= var
->data
.pixel_center_integer
;
4014 /* According to section 4.3.7 of the GLSL 1.30 spec,
4015 * the following built-in varaibles can be redeclared with an
4016 * interpolation qualifier:
4019 * * gl_FrontSecondaryColor
4020 * * gl_BackSecondaryColor
4022 * * gl_SecondaryColor
4024 } else if (state
->is_version(130, 0)
4025 && (strcmp(var
->name
, "gl_FrontColor") == 0
4026 || strcmp(var
->name
, "gl_BackColor") == 0
4027 || strcmp(var
->name
, "gl_FrontSecondaryColor") == 0
4028 || strcmp(var
->name
, "gl_BackSecondaryColor") == 0
4029 || strcmp(var
->name
, "gl_Color") == 0
4030 || strcmp(var
->name
, "gl_SecondaryColor") == 0)
4031 && earlier
->type
== var
->type
4032 && earlier
->data
.mode
== var
->data
.mode
) {
4033 earlier
->data
.interpolation
= var
->data
.interpolation
;
4035 /* Layout qualifiers for gl_FragDepth. */
4036 } else if ((state
->is_version(420, 0) ||
4037 state
->AMD_conservative_depth_enable
||
4038 state
->ARB_conservative_depth_enable
)
4039 && strcmp(var
->name
, "gl_FragDepth") == 0
4040 && earlier
->type
== var
->type
4041 && earlier
->data
.mode
== var
->data
.mode
) {
4043 /** From the AMD_conservative_depth spec:
4044 * Within any shader, the first redeclarations of gl_FragDepth
4045 * must appear before any use of gl_FragDepth.
4047 if (earlier
->data
.used
) {
4048 _mesa_glsl_error(&loc
, state
,
4049 "the first redeclaration of gl_FragDepth "
4050 "must appear before any use of gl_FragDepth");
4053 /* Prevent inconsistent redeclaration of depth layout qualifier. */
4054 if (earlier
->data
.depth_layout
!= ir_depth_layout_none
4055 && earlier
->data
.depth_layout
!= var
->data
.depth_layout
) {
4056 _mesa_glsl_error(&loc
, state
,
4057 "gl_FragDepth: depth layout is declared here "
4058 "as '%s, but it was previously declared as "
4060 depth_layout_string(var
->data
.depth_layout
),
4061 depth_layout_string(earlier
->data
.depth_layout
));
4064 earlier
->data
.depth_layout
= var
->data
.depth_layout
;
4066 } else if (state
->has_framebuffer_fetch() &&
4067 strcmp(var
->name
, "gl_LastFragData") == 0 &&
4068 var
->type
== earlier
->type
&&
4069 var
->data
.mode
== ir_var_auto
) {
4070 /* According to the EXT_shader_framebuffer_fetch spec:
4072 * "By default, gl_LastFragData is declared with the mediump precision
4073 * qualifier. This can be changed by redeclaring the corresponding
4074 * variables with the desired precision qualifier."
4076 earlier
->data
.precision
= var
->data
.precision
;
4078 } else if (allow_all_redeclarations
) {
4079 if (earlier
->data
.mode
!= var
->data
.mode
) {
4080 _mesa_glsl_error(&loc
, state
,
4081 "redeclaration of `%s' with incorrect qualifiers",
4083 } else if (earlier
->type
!= var
->type
) {
4084 _mesa_glsl_error(&loc
, state
,
4085 "redeclaration of `%s' has incorrect type",
4089 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
4096 * Generate the IR for an initializer in a variable declaration
4099 process_initializer(ir_variable
*var
, ast_declaration
*decl
,
4100 ast_fully_specified_type
*type
,
4101 exec_list
*initializer_instructions
,
4102 struct _mesa_glsl_parse_state
*state
)
4104 ir_rvalue
*result
= NULL
;
4106 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
4108 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
4110 * "All uniform variables are read-only and are initialized either
4111 * directly by an application via API commands, or indirectly by
4114 if (var
->data
.mode
== ir_var_uniform
) {
4115 state
->check_version(120, 0, &initializer_loc
,
4116 "cannot initialize uniform %s",
4120 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
4122 * "Buffer variables cannot have initializers."
4124 if (var
->data
.mode
== ir_var_shader_storage
) {
4125 _mesa_glsl_error(&initializer_loc
, state
,
4126 "cannot initialize buffer variable %s",
4130 /* From section 4.1.7 of the GLSL 4.40 spec:
4132 * "Opaque variables [...] are initialized only through the
4133 * OpenGL API; they cannot be declared with an initializer in a
4136 if (var
->type
->contains_opaque()) {
4137 _mesa_glsl_error(&initializer_loc
, state
,
4138 "cannot initialize opaque variable %s",
4142 if ((var
->data
.mode
== ir_var_shader_in
) && (state
->current_function
== NULL
)) {
4143 _mesa_glsl_error(&initializer_loc
, state
,
4144 "cannot initialize %s shader input / %s %s",
4145 _mesa_shader_stage_to_string(state
->stage
),
4146 (state
->stage
== MESA_SHADER_VERTEX
)
4147 ? "attribute" : "varying",
4151 if (var
->data
.mode
== ir_var_shader_out
&& state
->current_function
== NULL
) {
4152 _mesa_glsl_error(&initializer_loc
, state
,
4153 "cannot initialize %s shader output %s",
4154 _mesa_shader_stage_to_string(state
->stage
),
4158 /* If the initializer is an ast_aggregate_initializer, recursively store
4159 * type information from the LHS into it, so that its hir() function can do
4162 if (decl
->initializer
->oper
== ast_aggregate
)
4163 _mesa_ast_set_aggregate_type(var
->type
, decl
->initializer
);
4165 ir_dereference
*const lhs
= new(state
) ir_dereference_variable(var
);
4166 ir_rvalue
*rhs
= decl
->initializer
->hir(initializer_instructions
, state
);
4168 /* Calculate the constant value if this is a const or uniform
4171 * Section 4.3 (Storage Qualifiers) of the GLSL ES 1.00.17 spec says:
4173 * "Declarations of globals without a storage qualifier, or with
4174 * just the const qualifier, may include initializers, in which case
4175 * they will be initialized before the first line of main() is
4176 * executed. Such initializers must be a constant expression."
4178 * The same section of the GLSL ES 3.00.4 spec has similar language.
4180 if (type
->qualifier
.flags
.q
.constant
4181 || type
->qualifier
.flags
.q
.uniform
4182 || (state
->es_shader
&& state
->current_function
== NULL
)) {
4183 ir_rvalue
*new_rhs
= validate_assignment(state
, initializer_loc
,
4185 if (new_rhs
!= NULL
) {
4188 /* Section 4.3.3 (Constant Expressions) of the GLSL ES 3.00.4 spec
4191 * "A constant expression is one of
4195 * - an expression formed by an operator on operands that are
4196 * all constant expressions, including getting an element of
4197 * a constant array, or a field of a constant structure, or
4198 * components of a constant vector. However, the sequence
4199 * operator ( , ) and the assignment operators ( =, +=, ...)
4200 * are not included in the operators that can create a
4201 * constant expression."
4203 * Section 12.43 (Sequence operator and constant expressions) says:
4205 * "Should the following construct be allowed?
4209 * The expression within the brackets uses the sequence operator
4210 * (',') and returns the integer 3 so the construct is declaring
4211 * a single-dimensional array of size 3. In some languages, the
4212 * construct declares a two-dimensional array. It would be
4213 * preferable to make this construct illegal to avoid confusion.
4215 * One possibility is to change the definition of the sequence
4216 * operator so that it does not return a constant-expression and
4217 * hence cannot be used to declare an array size.
4219 * RESOLUTION: The result of a sequence operator is not a
4220 * constant-expression."
4222 * Section 4.3.3 (Constant Expressions) of the GLSL 4.30.9 spec
4223 * contains language almost identical to the section 4.3.3 in the
4224 * GLSL ES 3.00.4 spec. This is a new limitation for these GLSL
4227 ir_constant
*constant_value
= rhs
->constant_expression_value();
4228 if (!constant_value
||
4229 (state
->is_version(430, 300) &&
4230 decl
->initializer
->has_sequence_subexpression())) {
4231 const char *const variable_mode
=
4232 (type
->qualifier
.flags
.q
.constant
)
4234 : ((type
->qualifier
.flags
.q
.uniform
) ? "uniform" : "global");
4236 /* If ARB_shading_language_420pack is enabled, initializers of
4237 * const-qualified local variables do not have to be constant
4238 * expressions. Const-qualified global variables must still be
4239 * initialized with constant expressions.
4241 if (!state
->has_420pack()
4242 || state
->current_function
== NULL
) {
4243 _mesa_glsl_error(& initializer_loc
, state
,
4244 "initializer of %s variable `%s' must be a "
4245 "constant expression",
4248 if (var
->type
->is_numeric()) {
4249 /* Reduce cascading errors. */
4250 var
->constant_value
= type
->qualifier
.flags
.q
.constant
4251 ? ir_constant::zero(state
, var
->type
) : NULL
;
4255 rhs
= constant_value
;
4256 var
->constant_value
= type
->qualifier
.flags
.q
.constant
4257 ? constant_value
: NULL
;
4260 if (var
->type
->is_numeric()) {
4261 /* Reduce cascading errors. */
4262 var
->constant_value
= type
->qualifier
.flags
.q
.constant
4263 ? ir_constant::zero(state
, var
->type
) : NULL
;
4268 if (rhs
&& !rhs
->type
->is_error()) {
4269 bool temp
= var
->data
.read_only
;
4270 if (type
->qualifier
.flags
.q
.constant
)
4271 var
->data
.read_only
= false;
4273 /* Never emit code to initialize a uniform.
4275 const glsl_type
*initializer_type
;
4276 if (!type
->qualifier
.flags
.q
.uniform
) {
4277 do_assignment(initializer_instructions
, state
,
4282 type
->get_location());
4283 initializer_type
= result
->type
;
4285 initializer_type
= rhs
->type
;
4287 var
->constant_initializer
= rhs
->constant_expression_value();
4288 var
->data
.has_initializer
= true;
4290 /* If the declared variable is an unsized array, it must inherrit
4291 * its full type from the initializer. A declaration such as
4293 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
4297 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
4299 * The assignment generated in the if-statement (below) will also
4300 * automatically handle this case for non-uniforms.
4302 * If the declared variable is not an array, the types must
4303 * already match exactly. As a result, the type assignment
4304 * here can be done unconditionally. For non-uniforms the call
4305 * to do_assignment can change the type of the initializer (via
4306 * the implicit conversion rules). For uniforms the initializer
4307 * must be a constant expression, and the type of that expression
4308 * was validated above.
4310 var
->type
= initializer_type
;
4312 var
->data
.read_only
= temp
;
4319 validate_layout_qualifier_vertex_count(struct _mesa_glsl_parse_state
*state
,
4320 YYLTYPE loc
, ir_variable
*var
,
4321 unsigned num_vertices
,
4323 const char *var_category
)
4325 if (var
->type
->is_unsized_array()) {
4326 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says:
4328 * All geometry shader input unsized array declarations will be
4329 * sized by an earlier input layout qualifier, when present, as per
4330 * the following table.
4332 * Followed by a table mapping each allowed input layout qualifier to
4333 * the corresponding input length.
4335 * Similarly for tessellation control shader outputs.
4337 if (num_vertices
!= 0)
4338 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
4341 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec
4342 * includes the following examples of compile-time errors:
4344 * // code sequence within one shader...
4345 * in vec4 Color1[]; // size unknown
4346 * ...Color1.length()...// illegal, length() unknown
4347 * in vec4 Color2[2]; // size is 2
4348 * ...Color1.length()...// illegal, Color1 still has no size
4349 * in vec4 Color3[3]; // illegal, input sizes are inconsistent
4350 * layout(lines) in; // legal, input size is 2, matching
4351 * in vec4 Color4[3]; // illegal, contradicts layout
4354 * To detect the case illustrated by Color3, we verify that the size of
4355 * an explicitly-sized array matches the size of any previously declared
4356 * explicitly-sized array. To detect the case illustrated by Color4, we
4357 * verify that the size of an explicitly-sized array is consistent with
4358 * any previously declared input layout.
4360 if (num_vertices
!= 0 && var
->type
->length
!= num_vertices
) {
4361 _mesa_glsl_error(&loc
, state
,
4362 "%s size contradicts previously declared layout "
4363 "(size is %u, but layout requires a size of %u)",
4364 var_category
, var
->type
->length
, num_vertices
);
4365 } else if (*size
!= 0 && var
->type
->length
!= *size
) {
4366 _mesa_glsl_error(&loc
, state
,
4367 "%s sizes are inconsistent (size is %u, but a "
4368 "previous declaration has size %u)",
4369 var_category
, var
->type
->length
, *size
);
4371 *size
= var
->type
->length
;
4377 handle_tess_ctrl_shader_output_decl(struct _mesa_glsl_parse_state
*state
,
4378 YYLTYPE loc
, ir_variable
*var
)
4380 unsigned num_vertices
= 0;
4382 if (state
->tcs_output_vertices_specified
) {
4383 if (!state
->out_qualifier
->vertices
->
4384 process_qualifier_constant(state
, "vertices",
4385 &num_vertices
, false)) {
4389 if (num_vertices
> state
->Const
.MaxPatchVertices
) {
4390 _mesa_glsl_error(&loc
, state
, "vertices (%d) exceeds "
4391 "GL_MAX_PATCH_VERTICES", num_vertices
);
4396 if (!var
->type
->is_array() && !var
->data
.patch
) {
4397 _mesa_glsl_error(&loc
, state
,
4398 "tessellation control shader outputs must be arrays");
4400 /* To avoid cascading failures, short circuit the checks below. */
4404 if (var
->data
.patch
)
4407 var
->data
.tess_varying_implicit_sized_array
= var
->type
->is_unsized_array();
4409 validate_layout_qualifier_vertex_count(state
, loc
, var
, num_vertices
,
4410 &state
->tcs_output_size
,
4411 "tessellation control shader output");
4415 * Do additional processing necessary for tessellation control/evaluation shader
4416 * input declarations. This covers both interface block arrays and bare input
4420 handle_tess_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
4421 YYLTYPE loc
, ir_variable
*var
)
4423 if (!var
->type
->is_array() && !var
->data
.patch
) {
4424 _mesa_glsl_error(&loc
, state
,
4425 "per-vertex tessellation shader inputs must be arrays");
4426 /* Avoid cascading failures. */
4430 if (var
->data
.patch
)
4433 /* The ARB_tessellation_shader spec says:
4435 * "Declaring an array size is optional. If no size is specified, it
4436 * will be taken from the implementation-dependent maximum patch size
4437 * (gl_MaxPatchVertices). If a size is specified, it must match the
4438 * maximum patch size; otherwise, a compile or link error will occur."
4440 * This text appears twice, once for TCS inputs, and again for TES inputs.
4442 if (var
->type
->is_unsized_array()) {
4443 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
4444 state
->Const
.MaxPatchVertices
);
4445 var
->data
.tess_varying_implicit_sized_array
= true;
4446 } else if (var
->type
->length
!= state
->Const
.MaxPatchVertices
) {
4447 _mesa_glsl_error(&loc
, state
,
4448 "per-vertex tessellation shader input arrays must be "
4449 "sized to gl_MaxPatchVertices (%d).",
4450 state
->Const
.MaxPatchVertices
);
4456 * Do additional processing necessary for geometry shader input declarations
4457 * (this covers both interface blocks arrays and bare input variables).
4460 handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
4461 YYLTYPE loc
, ir_variable
*var
)
4463 unsigned num_vertices
= 0;
4465 if (state
->gs_input_prim_type_specified
) {
4466 num_vertices
= vertices_per_prim(state
->in_qualifier
->prim_type
);
4469 /* Geometry shader input variables must be arrays. Caller should have
4470 * reported an error for this.
4472 if (!var
->type
->is_array()) {
4473 assert(state
->error
);
4475 /* To avoid cascading failures, short circuit the checks below. */
4479 validate_layout_qualifier_vertex_count(state
, loc
, var
, num_vertices
,
4480 &state
->gs_input_size
,
4481 "geometry shader input");
4485 validate_identifier(const char *identifier
, YYLTYPE loc
,
4486 struct _mesa_glsl_parse_state
*state
)
4488 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
4490 * "Identifiers starting with "gl_" are reserved for use by
4491 * OpenGL, and may not be declared in a shader as either a
4492 * variable or a function."
4494 if (is_gl_identifier(identifier
)) {
4495 _mesa_glsl_error(&loc
, state
,
4496 "identifier `%s' uses reserved `gl_' prefix",
4498 } else if (strstr(identifier
, "__")) {
4499 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
4502 * "In addition, all identifiers containing two
4503 * consecutive underscores (__) are reserved as
4504 * possible future keywords."
4506 * The intention is that names containing __ are reserved for internal
4507 * use by the implementation, and names prefixed with GL_ are reserved
4508 * for use by Khronos. Names simply containing __ are dangerous to use,
4509 * but should be allowed.
4511 * A future version of the GLSL specification will clarify this.
4513 _mesa_glsl_warning(&loc
, state
,
4514 "identifier `%s' uses reserved `__' string",
4520 ast_declarator_list::hir(exec_list
*instructions
,
4521 struct _mesa_glsl_parse_state
*state
)
4524 const struct glsl_type
*decl_type
;
4525 const char *type_name
= NULL
;
4526 ir_rvalue
*result
= NULL
;
4527 YYLTYPE loc
= this->get_location();
4529 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
4531 * "To ensure that a particular output variable is invariant, it is
4532 * necessary to use the invariant qualifier. It can either be used to
4533 * qualify a previously declared variable as being invariant
4535 * invariant gl_Position; // make existing gl_Position be invariant"
4537 * In these cases the parser will set the 'invariant' flag in the declarator
4538 * list, and the type will be NULL.
4540 if (this->invariant
) {
4541 assert(this->type
== NULL
);
4543 if (state
->current_function
!= NULL
) {
4544 _mesa_glsl_error(& loc
, state
,
4545 "all uses of `invariant' keyword must be at global "
4549 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
4550 assert(decl
->array_specifier
== NULL
);
4551 assert(decl
->initializer
== NULL
);
4553 ir_variable
*const earlier
=
4554 state
->symbols
->get_variable(decl
->identifier
);
4555 if (earlier
== NULL
) {
4556 _mesa_glsl_error(& loc
, state
,
4557 "undeclared variable `%s' cannot be marked "
4558 "invariant", decl
->identifier
);
4559 } else if (!is_allowed_invariant(earlier
, state
)) {
4560 _mesa_glsl_error(&loc
, state
,
4561 "`%s' cannot be marked invariant; interfaces between "
4562 "shader stages only.", decl
->identifier
);
4563 } else if (earlier
->data
.used
) {
4564 _mesa_glsl_error(& loc
, state
,
4565 "variable `%s' may not be redeclared "
4566 "`invariant' after being used",
4569 earlier
->data
.invariant
= true;
4573 /* Invariant redeclarations do not have r-values.
4578 if (this->precise
) {
4579 assert(this->type
== NULL
);
4581 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
4582 assert(decl
->array_specifier
== NULL
);
4583 assert(decl
->initializer
== NULL
);
4585 ir_variable
*const earlier
=
4586 state
->symbols
->get_variable(decl
->identifier
);
4587 if (earlier
== NULL
) {
4588 _mesa_glsl_error(& loc
, state
,
4589 "undeclared variable `%s' cannot be marked "
4590 "precise", decl
->identifier
);
4591 } else if (state
->current_function
!= NULL
&&
4592 !state
->symbols
->name_declared_this_scope(decl
->identifier
)) {
4593 /* Note: we have to check if we're in a function, since
4594 * builtins are treated as having come from another scope.
4596 _mesa_glsl_error(& loc
, state
,
4597 "variable `%s' from an outer scope may not be "
4598 "redeclared `precise' in this scope",
4600 } else if (earlier
->data
.used
) {
4601 _mesa_glsl_error(& loc
, state
,
4602 "variable `%s' may not be redeclared "
4603 "`precise' after being used",
4606 earlier
->data
.precise
= true;
4610 /* Precise redeclarations do not have r-values either. */
4614 assert(this->type
!= NULL
);
4615 assert(!this->invariant
);
4616 assert(!this->precise
);
4618 /* The type specifier may contain a structure definition. Process that
4619 * before any of the variable declarations.
4621 (void) this->type
->specifier
->hir(instructions
, state
);
4623 decl_type
= this->type
->glsl_type(& type_name
, state
);
4625 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
4626 * "Buffer variables may only be declared inside interface blocks
4627 * (section 4.3.9 “Interface Blocks”), which are then referred to as
4628 * shader storage blocks. It is a compile-time error to declare buffer
4629 * variables at global scope (outside a block)."
4631 if (type
->qualifier
.flags
.q
.buffer
&& !decl_type
->is_interface()) {
4632 _mesa_glsl_error(&loc
, state
,
4633 "buffer variables cannot be declared outside "
4634 "interface blocks");
4637 /* An offset-qualified atomic counter declaration sets the default
4638 * offset for the next declaration within the same atomic counter
4641 if (decl_type
&& decl_type
->contains_atomic()) {
4642 if (type
->qualifier
.flags
.q
.explicit_binding
&&
4643 type
->qualifier
.flags
.q
.explicit_offset
) {
4644 unsigned qual_binding
;
4645 unsigned qual_offset
;
4646 if (process_qualifier_constant(state
, &loc
, "binding",
4647 type
->qualifier
.binding
,
4649 && process_qualifier_constant(state
, &loc
, "offset",
4650 type
->qualifier
.offset
,
4652 state
->atomic_counter_offsets
[qual_binding
] = qual_offset
;
4656 ast_type_qualifier allowed_atomic_qual_mask
;
4657 allowed_atomic_qual_mask
.flags
.i
= 0;
4658 allowed_atomic_qual_mask
.flags
.q
.explicit_binding
= 1;
4659 allowed_atomic_qual_mask
.flags
.q
.explicit_offset
= 1;
4660 allowed_atomic_qual_mask
.flags
.q
.uniform
= 1;
4662 type
->qualifier
.validate_flags(&loc
, state
, allowed_atomic_qual_mask
,
4663 "invalid layout qualifier for",
4667 if (this->declarations
.is_empty()) {
4668 /* If there is no structure involved in the program text, there are two
4669 * possible scenarios:
4671 * - The program text contained something like 'vec4;'. This is an
4672 * empty declaration. It is valid but weird. Emit a warning.
4674 * - The program text contained something like 'S;' and 'S' is not the
4675 * name of a known structure type. This is both invalid and weird.
4678 * - The program text contained something like 'mediump float;'
4679 * when the programmer probably meant 'precision mediump
4680 * float;' Emit a warning with a description of what they
4681 * probably meant to do.
4683 * Note that if decl_type is NULL and there is a structure involved,
4684 * there must have been some sort of error with the structure. In this
4685 * case we assume that an error was already generated on this line of
4686 * code for the structure. There is no need to generate an additional,
4689 assert(this->type
->specifier
->structure
== NULL
|| decl_type
!= NULL
4692 if (decl_type
== NULL
) {
4693 _mesa_glsl_error(&loc
, state
,
4694 "invalid type `%s' in empty declaration",
4697 if (decl_type
->base_type
== GLSL_TYPE_ARRAY
) {
4698 /* From Section 13.22 (Array Declarations) of the GLSL ES 3.2
4701 * "... any declaration that leaves the size undefined is
4702 * disallowed as this would add complexity and there are no
4705 if (state
->es_shader
&& decl_type
->is_unsized_array()) {
4706 _mesa_glsl_error(&loc
, state
, "array size must be explicitly "
4707 "or implicitly defined");
4710 /* From Section 4.12 (Empty Declarations) of the GLSL 4.5 spec:
4712 * "The combinations of types and qualifiers that cause
4713 * compile-time or link-time errors are the same whether or not
4714 * the declaration is empty."
4716 validate_array_dimensions(decl_type
, state
, &loc
);
4719 if (decl_type
->base_type
== GLSL_TYPE_ATOMIC_UINT
) {
4720 /* Empty atomic counter declarations are allowed and useful
4721 * to set the default offset qualifier.
4724 } else if (this->type
->qualifier
.precision
!= ast_precision_none
) {
4725 if (this->type
->specifier
->structure
!= NULL
) {
4726 _mesa_glsl_error(&loc
, state
,
4727 "precision qualifiers can't be applied "
4730 static const char *const precision_names
[] = {
4737 _mesa_glsl_warning(&loc
, state
,
4738 "empty declaration with precision "
4739 "qualifier, to set the default precision, "
4740 "use `precision %s %s;'",
4741 precision_names
[this->type
->
4742 qualifier
.precision
],
4745 } else if (this->type
->specifier
->structure
== NULL
) {
4746 _mesa_glsl_warning(&loc
, state
, "empty declaration");
4751 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
4752 const struct glsl_type
*var_type
;
4754 const char *identifier
= decl
->identifier
;
4755 /* FINISHME: Emit a warning if a variable declaration shadows a
4756 * FINISHME: declaration at a higher scope.
4759 if ((decl_type
== NULL
) || decl_type
->is_void()) {
4760 if (type_name
!= NULL
) {
4761 _mesa_glsl_error(& loc
, state
,
4762 "invalid type `%s' in declaration of `%s'",
4763 type_name
, decl
->identifier
);
4765 _mesa_glsl_error(& loc
, state
,
4766 "invalid type in declaration of `%s'",
4772 if (this->type
->qualifier
.flags
.q
.subroutine
) {
4776 t
= state
->symbols
->get_type(this->type
->specifier
->type_name
);
4778 _mesa_glsl_error(& loc
, state
,
4779 "invalid type in declaration of `%s'",
4781 name
= ralloc_asprintf(ctx
, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state
->stage
), decl
->identifier
);
4786 var_type
= process_array_type(&loc
, decl_type
, decl
->array_specifier
,
4789 var
= new(ctx
) ir_variable(var_type
, identifier
, ir_var_auto
);
4791 /* The 'varying in' and 'varying out' qualifiers can only be used with
4792 * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
4795 if (this->type
->qualifier
.flags
.q
.varying
) {
4796 if (this->type
->qualifier
.flags
.q
.in
) {
4797 _mesa_glsl_error(& loc
, state
,
4798 "`varying in' qualifier in declaration of "
4799 "`%s' only valid for geometry shaders using "
4800 "ARB_geometry_shader4 or EXT_geometry_shader4",
4802 } else if (this->type
->qualifier
.flags
.q
.out
) {
4803 _mesa_glsl_error(& loc
, state
,
4804 "`varying out' qualifier in declaration of "
4805 "`%s' only valid for geometry shaders using "
4806 "ARB_geometry_shader4 or EXT_geometry_shader4",
4811 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
4813 * "Global variables can only use the qualifiers const,
4814 * attribute, uniform, or varying. Only one may be
4817 * Local variables can only use the qualifier const."
4819 * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
4820 * any extension that adds the 'layout' keyword.
4822 if (!state
->is_version(130, 300)
4823 && !state
->has_explicit_attrib_location()
4824 && !state
->has_separate_shader_objects()
4825 && !state
->ARB_fragment_coord_conventions_enable
) {
4826 if (this->type
->qualifier
.flags
.q
.out
) {
4827 _mesa_glsl_error(& loc
, state
,
4828 "`out' qualifier in declaration of `%s' "
4829 "only valid for function parameters in %s",
4830 decl
->identifier
, state
->get_version_string());
4832 if (this->type
->qualifier
.flags
.q
.in
) {
4833 _mesa_glsl_error(& loc
, state
,
4834 "`in' qualifier in declaration of `%s' "
4835 "only valid for function parameters in %s",
4836 decl
->identifier
, state
->get_version_string());
4838 /* FINISHME: Test for other invalid qualifiers. */
4841 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
4843 apply_layout_qualifier_to_variable(&this->type
->qualifier
, var
, state
,
4846 if ((var
->data
.mode
== ir_var_auto
|| var
->data
.mode
== ir_var_temporary
)
4847 && (var
->type
->is_numeric() || var
->type
->is_boolean())
4848 && state
->zero_init
) {
4849 const ir_constant_data data
= {0};
4850 var
->data
.has_initializer
= true;
4851 var
->constant_initializer
= new(var
) ir_constant(var
->type
, &data
);
4854 if (this->type
->qualifier
.flags
.q
.invariant
) {
4855 if (!is_allowed_invariant(var
, state
)) {
4856 _mesa_glsl_error(&loc
, state
,
4857 "`%s' cannot be marked invariant; interfaces between "
4858 "shader stages only", var
->name
);
4862 if (state
->current_function
!= NULL
) {
4863 const char *mode
= NULL
;
4864 const char *extra
= "";
4866 /* There is no need to check for 'inout' here because the parser will
4867 * only allow that in function parameter lists.
4869 if (this->type
->qualifier
.flags
.q
.attribute
) {
4871 } else if (this->type
->qualifier
.flags
.q
.subroutine
) {
4872 mode
= "subroutine uniform";
4873 } else if (this->type
->qualifier
.flags
.q
.uniform
) {
4875 } else if (this->type
->qualifier
.flags
.q
.varying
) {
4877 } else if (this->type
->qualifier
.flags
.q
.in
) {
4879 extra
= " or in function parameter list";
4880 } else if (this->type
->qualifier
.flags
.q
.out
) {
4882 extra
= " or in function parameter list";
4886 _mesa_glsl_error(& loc
, state
,
4887 "%s variable `%s' must be declared at "
4889 mode
, var
->name
, extra
);
4891 } else if (var
->data
.mode
== ir_var_shader_in
) {
4892 var
->data
.read_only
= true;
4894 if (state
->stage
== MESA_SHADER_VERTEX
) {
4895 bool error_emitted
= false;
4897 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
4899 * "Vertex shader inputs can only be float, floating-point
4900 * vectors, matrices, signed and unsigned integers and integer
4901 * vectors. Vertex shader inputs can also form arrays of these
4902 * types, but not structures."
4904 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
4906 * "Vertex shader inputs can only be float, floating-point
4907 * vectors, matrices, signed and unsigned integers and integer
4908 * vectors. They cannot be arrays or structures."
4910 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
4912 * "The attribute qualifier can be used only with float,
4913 * floating-point vectors, and matrices. Attribute variables
4914 * cannot be declared as arrays or structures."
4916 * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
4918 * "Vertex shader inputs can only be float, floating-point
4919 * vectors, matrices, signed and unsigned integers and integer
4920 * vectors. Vertex shader inputs cannot be arrays or
4923 const glsl_type
*check_type
= var
->type
->without_array();
4925 switch (check_type
->base_type
) {
4926 case GLSL_TYPE_FLOAT
:
4928 case GLSL_TYPE_UINT64
:
4929 case GLSL_TYPE_INT64
:
4931 case GLSL_TYPE_UINT
:
4933 if (state
->is_version(120, 300))
4935 case GLSL_TYPE_DOUBLE
:
4936 if (check_type
->base_type
== GLSL_TYPE_DOUBLE
&& (state
->is_version(410, 0) || state
->ARB_vertex_attrib_64bit_enable
))
4940 _mesa_glsl_error(& loc
, state
,
4941 "vertex shader input / attribute cannot have "
4943 var
->type
->is_array() ? "array of " : "",
4945 error_emitted
= true;
4948 if (!error_emitted
&& var
->type
->is_array() &&
4949 !state
->check_version(150, 0, &loc
,
4950 "vertex shader input / attribute "
4951 "cannot have array type")) {
4952 error_emitted
= true;
4954 } else if (state
->stage
== MESA_SHADER_GEOMETRY
) {
4955 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
4957 * Geometry shader input variables get the per-vertex values
4958 * written out by vertex shader output variables of the same
4959 * names. Since a geometry shader operates on a set of
4960 * vertices, each input varying variable (or input block, see
4961 * interface blocks below) needs to be declared as an array.
4963 if (!var
->type
->is_array()) {
4964 _mesa_glsl_error(&loc
, state
,
4965 "geometry shader inputs must be arrays");
4968 handle_geometry_shader_input_decl(state
, loc
, var
);
4969 } else if (state
->stage
== MESA_SHADER_FRAGMENT
) {
4970 /* From section 4.3.4 (Input Variables) of the GLSL ES 3.10 spec:
4972 * It is a compile-time error to declare a fragment shader
4973 * input with, or that contains, any of the following types:
4977 * * An array of arrays
4978 * * An array of structures
4979 * * A structure containing an array
4980 * * A structure containing a structure
4982 if (state
->es_shader
) {
4983 const glsl_type
*check_type
= var
->type
->without_array();
4984 if (check_type
->is_boolean() ||
4985 check_type
->contains_opaque()) {
4986 _mesa_glsl_error(&loc
, state
,
4987 "fragment shader input cannot have type %s",
4990 if (var
->type
->is_array() &&
4991 var
->type
->fields
.array
->is_array()) {
4992 _mesa_glsl_error(&loc
, state
,
4994 "cannot have an array of arrays",
4995 _mesa_shader_stage_to_string(state
->stage
));
4997 if (var
->type
->is_array() &&
4998 var
->type
->fields
.array
->is_record()) {
4999 _mesa_glsl_error(&loc
, state
,
5000 "fragment shader input "
5001 "cannot have an array of structs");
5003 if (var
->type
->is_record()) {
5004 for (unsigned i
= 0; i
< var
->type
->length
; i
++) {
5005 if (var
->type
->fields
.structure
[i
].type
->is_array() ||
5006 var
->type
->fields
.structure
[i
].type
->is_record())
5007 _mesa_glsl_error(&loc
, state
,
5008 "fragement shader input cannot have "
5009 "a struct that contains an "
5014 } else if (state
->stage
== MESA_SHADER_TESS_CTRL
||
5015 state
->stage
== MESA_SHADER_TESS_EVAL
) {
5016 handle_tess_shader_input_decl(state
, loc
, var
);
5018 } else if (var
->data
.mode
== ir_var_shader_out
) {
5019 const glsl_type
*check_type
= var
->type
->without_array();
5021 /* From section 4.3.6 (Output variables) of the GLSL 4.40 spec:
5023 * It is a compile-time error to declare a vertex, tessellation
5024 * evaluation, tessellation control, or geometry shader output
5025 * that contains any of the following:
5027 * * A Boolean type (bool, bvec2 ...)
5030 if (check_type
->is_boolean() || check_type
->contains_opaque())
5031 _mesa_glsl_error(&loc
, state
,
5032 "%s shader output cannot have type %s",
5033 _mesa_shader_stage_to_string(state
->stage
),
5036 /* From section 4.3.6 (Output variables) of the GLSL 4.40 spec:
5038 * It is a compile-time error to declare a fragment shader output
5039 * that contains any of the following:
5041 * * A Boolean type (bool, bvec2 ...)
5042 * * A double-precision scalar or vector (double, dvec2 ...)
5047 if (state
->stage
== MESA_SHADER_FRAGMENT
) {
5048 if (check_type
->is_record() || check_type
->is_matrix())
5049 _mesa_glsl_error(&loc
, state
,
5050 "fragment shader output "
5051 "cannot have struct or matrix type");
5052 switch (check_type
->base_type
) {
5053 case GLSL_TYPE_UINT
:
5055 case GLSL_TYPE_FLOAT
:
5058 _mesa_glsl_error(&loc
, state
,
5059 "fragment shader output cannot have "
5060 "type %s", check_type
->name
);
5064 /* From section 4.3.6 (Output Variables) of the GLSL ES 3.10 spec:
5066 * It is a compile-time error to declare a vertex shader output
5067 * with, or that contains, any of the following types:
5071 * * An array of arrays
5072 * * An array of structures
5073 * * A structure containing an array
5074 * * A structure containing a structure
5076 * It is a compile-time error to declare a fragment shader output
5077 * with, or that contains, any of the following types:
5083 * * An array of array
5085 * ES 3.20 updates this to apply to tessellation and geometry shaders
5086 * as well. Because there are per-vertex arrays in the new stages,
5087 * it strikes the "array of..." rules and replaces them with these:
5089 * * For per-vertex-arrayed variables (applies to tessellation
5090 * control, tessellation evaluation and geometry shaders):
5092 * * Per-vertex-arrayed arrays of arrays
5093 * * Per-vertex-arrayed arrays of structures
5095 * * For non-per-vertex-arrayed variables:
5097 * * An array of arrays
5098 * * An array of structures
5100 * which basically says to unwrap the per-vertex aspect and apply
5103 if (state
->es_shader
) {
5104 if (var
->type
->is_array() &&
5105 var
->type
->fields
.array
->is_array()) {
5106 _mesa_glsl_error(&loc
, state
,
5108 "cannot have an array of arrays",
5109 _mesa_shader_stage_to_string(state
->stage
));
5111 if (state
->stage
<= MESA_SHADER_GEOMETRY
) {
5112 const glsl_type
*type
= var
->type
;
5114 if (state
->stage
== MESA_SHADER_TESS_CTRL
&&
5115 !var
->data
.patch
&& var
->type
->is_array()) {
5116 type
= var
->type
->fields
.array
;
5119 if (type
->is_array() && type
->fields
.array
->is_record()) {
5120 _mesa_glsl_error(&loc
, state
,
5121 "%s shader output cannot have "
5122 "an array of structs",
5123 _mesa_shader_stage_to_string(state
->stage
));
5125 if (type
->is_record()) {
5126 for (unsigned i
= 0; i
< type
->length
; i
++) {
5127 if (type
->fields
.structure
[i
].type
->is_array() ||
5128 type
->fields
.structure
[i
].type
->is_record())
5129 _mesa_glsl_error(&loc
, state
,
5130 "%s shader output cannot have a "
5131 "struct that contains an "
5133 _mesa_shader_stage_to_string(state
->stage
));
5139 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
5140 handle_tess_ctrl_shader_output_decl(state
, loc
, var
);
5142 } else if (var
->type
->contains_subroutine()) {
5143 /* declare subroutine uniforms as hidden */
5144 var
->data
.how_declared
= ir_var_hidden
;
5147 /* From section 4.3.4 of the GLSL 4.00 spec:
5148 * "Input variables may not be declared using the patch in qualifier
5149 * in tessellation control or geometry shaders."
5151 * From section 4.3.6 of the GLSL 4.00 spec:
5152 * "It is an error to use patch out in a vertex, tessellation
5153 * evaluation, or geometry shader."
5155 * This doesn't explicitly forbid using them in a fragment shader, but
5156 * that's probably just an oversight.
5158 if (state
->stage
!= MESA_SHADER_TESS_EVAL
5159 && this->type
->qualifier
.flags
.q
.patch
5160 && this->type
->qualifier
.flags
.q
.in
) {
5162 _mesa_glsl_error(&loc
, state
, "'patch in' can only be used in a "
5163 "tessellation evaluation shader");
5166 if (state
->stage
!= MESA_SHADER_TESS_CTRL
5167 && this->type
->qualifier
.flags
.q
.patch
5168 && this->type
->qualifier
.flags
.q
.out
) {
5170 _mesa_glsl_error(&loc
, state
, "'patch out' can only be used in a "
5171 "tessellation control shader");
5174 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
5176 if (this->type
->qualifier
.precision
!= ast_precision_none
) {
5177 state
->check_precision_qualifiers_allowed(&loc
);
5180 if (this->type
->qualifier
.precision
!= ast_precision_none
&&
5181 !precision_qualifier_allowed(var
->type
)) {
5182 _mesa_glsl_error(&loc
, state
,
5183 "precision qualifiers apply only to floating point"
5184 ", integer and opaque types");
5187 /* From section 4.1.7 of the GLSL 4.40 spec:
5189 * "[Opaque types] can only be declared as function
5190 * parameters or uniform-qualified variables."
5192 if (var_type
->contains_opaque() &&
5193 !this->type
->qualifier
.flags
.q
.uniform
) {
5194 _mesa_glsl_error(&loc
, state
,
5195 "opaque variables must be declared uniform");
5198 /* Process the initializer and add its instructions to a temporary
5199 * list. This list will be added to the instruction stream (below) after
5200 * the declaration is added. This is done because in some cases (such as
5201 * redeclarations) the declaration may not actually be added to the
5202 * instruction stream.
5204 exec_list initializer_instructions
;
5206 /* Examine var name here since var may get deleted in the next call */
5207 bool var_is_gl_id
= is_gl_identifier(var
->name
);
5209 ir_variable
*earlier
=
5210 get_variable_being_redeclared(var
, decl
->get_location(), state
,
5211 false /* allow_all_redeclarations */);
5212 if (earlier
!= NULL
) {
5214 earlier
->data
.how_declared
== ir_var_declared_in_block
) {
5215 _mesa_glsl_error(&loc
, state
,
5216 "`%s' has already been redeclared using "
5217 "gl_PerVertex", earlier
->name
);
5219 earlier
->data
.how_declared
= ir_var_declared_normally
;
5222 if (decl
->initializer
!= NULL
) {
5223 result
= process_initializer((earlier
== NULL
) ? var
: earlier
,
5225 &initializer_instructions
, state
);
5227 validate_array_dimensions(var_type
, state
, &loc
);
5230 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
5232 * "It is an error to write to a const variable outside of
5233 * its declaration, so they must be initialized when
5236 if (this->type
->qualifier
.flags
.q
.constant
&& decl
->initializer
== NULL
) {
5237 _mesa_glsl_error(& loc
, state
,
5238 "const declaration of `%s' must be initialized",
5242 if (state
->es_shader
) {
5243 const glsl_type
*const t
= (earlier
== NULL
)
5244 ? var
->type
: earlier
->type
;
5246 /* Skip the unsized array check for TCS/TES/GS inputs & TCS outputs.
5248 * The GL_OES_tessellation_shader spec says about inputs:
5250 * "Declaring an array size is optional. If no size is specified,
5251 * it will be taken from the implementation-dependent maximum
5252 * patch size (gl_MaxPatchVertices)."
5254 * and about TCS outputs:
5256 * "If no size is specified, it will be taken from output patch
5257 * size declared in the shader."
5259 * The GL_OES_geometry_shader spec says:
5261 * "All geometry shader input unsized array declarations will be
5262 * sized by an earlier input primitive layout qualifier, when
5263 * present, as per the following table."
5265 const bool implicitly_sized
=
5266 (var
->data
.mode
== ir_var_shader_in
&&
5267 state
->stage
>= MESA_SHADER_TESS_CTRL
&&
5268 state
->stage
<= MESA_SHADER_GEOMETRY
) ||
5269 (var
->data
.mode
== ir_var_shader_out
&&
5270 state
->stage
== MESA_SHADER_TESS_CTRL
);
5272 if (t
->is_unsized_array() && !implicitly_sized
)
5273 /* Section 10.17 of the GLSL ES 1.00 specification states that
5274 * unsized array declarations have been removed from the language.
5275 * Arrays that are sized using an initializer are still explicitly
5276 * sized. However, GLSL ES 1.00 does not allow array
5277 * initializers. That is only allowed in GLSL ES 3.00.
5279 * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
5281 * "An array type can also be formed without specifying a size
5282 * if the definition includes an initializer:
5284 * float x[] = float[2] (1.0, 2.0); // declares an array of size 2
5285 * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
5290 _mesa_glsl_error(& loc
, state
,
5291 "unsized array declarations are not allowed in "
5295 /* If the declaration is not a redeclaration, there are a few additional
5296 * semantic checks that must be applied. In addition, variable that was
5297 * created for the declaration should be added to the IR stream.
5299 if (earlier
== NULL
) {
5300 validate_identifier(decl
->identifier
, loc
, state
);
5302 /* Add the variable to the symbol table. Note that the initializer's
5303 * IR was already processed earlier (though it hasn't been emitted
5304 * yet), without the variable in scope.
5306 * This differs from most C-like languages, but it follows the GLSL
5307 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
5310 * "Within a declaration, the scope of a name starts immediately
5311 * after the initializer if present or immediately after the name
5312 * being declared if not."
5314 if (!state
->symbols
->add_variable(var
)) {
5315 YYLTYPE loc
= this->get_location();
5316 _mesa_glsl_error(&loc
, state
, "name `%s' already taken in the "
5317 "current scope", decl
->identifier
);
5321 /* Push the variable declaration to the top. It means that all the
5322 * variable declarations will appear in a funny last-to-first order,
5323 * but otherwise we run into trouble if a function is prototyped, a
5324 * global var is decled, then the function is defined with usage of
5325 * the global var. See glslparsertest's CorrectModule.frag.
5327 instructions
->push_head(var
);
5330 instructions
->append_list(&initializer_instructions
);
5334 /* Generally, variable declarations do not have r-values. However,
5335 * one is used for the declaration in
5337 * while (bool b = some_condition()) {
5341 * so we return the rvalue from the last seen declaration here.
5348 ast_parameter_declarator::hir(exec_list
*instructions
,
5349 struct _mesa_glsl_parse_state
*state
)
5352 const struct glsl_type
*type
;
5353 const char *name
= NULL
;
5354 YYLTYPE loc
= this->get_location();
5356 type
= this->type
->glsl_type(& name
, state
);
5360 _mesa_glsl_error(& loc
, state
,
5361 "invalid type `%s' in declaration of `%s'",
5362 name
, this->identifier
);
5364 _mesa_glsl_error(& loc
, state
,
5365 "invalid type in declaration of `%s'",
5369 type
= glsl_type::error_type
;
5372 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
5374 * "Functions that accept no input arguments need not use void in the
5375 * argument list because prototypes (or definitions) are required and
5376 * therefore there is no ambiguity when an empty argument list "( )" is
5377 * declared. The idiom "(void)" as a parameter list is provided for
5380 * Placing this check here prevents a void parameter being set up
5381 * for a function, which avoids tripping up checks for main taking
5382 * parameters and lookups of an unnamed symbol.
5384 if (type
->is_void()) {
5385 if (this->identifier
!= NULL
)
5386 _mesa_glsl_error(& loc
, state
,
5387 "named parameter cannot have type `void'");
5393 if (formal_parameter
&& (this->identifier
== NULL
)) {
5394 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
5398 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
5399 * call already handled the "vec4[..] foo" case.
5401 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
5403 if (!type
->is_error() && type
->is_unsized_array()) {
5404 _mesa_glsl_error(&loc
, state
, "arrays passed as parameters must have "
5406 type
= glsl_type::error_type
;
5410 ir_variable
*var
= new(ctx
)
5411 ir_variable(type
, this->identifier
, ir_var_function_in
);
5413 /* Apply any specified qualifiers to the parameter declaration. Note that
5414 * for function parameters the default mode is 'in'.
5416 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
,
5419 /* From section 4.1.7 of the GLSL 4.40 spec:
5421 * "Opaque variables cannot be treated as l-values; hence cannot
5422 * be used as out or inout function parameters, nor can they be
5425 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
5426 && type
->contains_opaque()) {
5427 _mesa_glsl_error(&loc
, state
, "out and inout parameters cannot "
5428 "contain opaque variables");
5429 type
= glsl_type::error_type
;
5432 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
5434 * "When calling a function, expressions that do not evaluate to
5435 * l-values cannot be passed to parameters declared as out or inout."
5437 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
5439 * "Other binary or unary expressions, non-dereferenced arrays,
5440 * function names, swizzles with repeated fields, and constants
5441 * cannot be l-values."
5443 * So for GLSL 1.10, passing an array as an out or inout parameter is not
5444 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
5446 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
5448 && !state
->check_version(120, 100, &loc
,
5449 "arrays cannot be out or inout parameters")) {
5450 type
= glsl_type::error_type
;
5453 instructions
->push_tail(var
);
5455 /* Parameter declarations do not have r-values.
5462 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
5464 exec_list
*ir_parameters
,
5465 _mesa_glsl_parse_state
*state
)
5467 ast_parameter_declarator
*void_param
= NULL
;
5470 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
5471 param
->formal_parameter
= formal
;
5472 param
->hir(ir_parameters
, state
);
5480 if ((void_param
!= NULL
) && (count
> 1)) {
5481 YYLTYPE loc
= void_param
->get_location();
5483 _mesa_glsl_error(& loc
, state
,
5484 "`void' parameter must be only parameter");
5490 emit_function(_mesa_glsl_parse_state
*state
, ir_function
*f
)
5492 /* IR invariants disallow function declarations or definitions
5493 * nested within other function definitions. But there is no
5494 * requirement about the relative order of function declarations
5495 * and definitions with respect to one another. So simply insert
5496 * the new ir_function block at the end of the toplevel instruction
5499 state
->toplevel_ir
->push_tail(f
);
5504 ast_function::hir(exec_list
*instructions
,
5505 struct _mesa_glsl_parse_state
*state
)
5508 ir_function
*f
= NULL
;
5509 ir_function_signature
*sig
= NULL
;
5510 exec_list hir_parameters
;
5511 YYLTYPE loc
= this->get_location();
5513 const char *const name
= identifier
;
5515 /* New functions are always added to the top-level IR instruction stream,
5516 * so this instruction list pointer is ignored. See also emit_function
5519 (void) instructions
;
5521 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
5523 * "Function declarations (prototypes) cannot occur inside of functions;
5524 * they must be at global scope, or for the built-in functions, outside
5525 * the global scope."
5527 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
5529 * "User defined functions may only be defined within the global scope."
5531 * Note that this language does not appear in GLSL 1.10.
5533 if ((state
->current_function
!= NULL
) &&
5534 state
->is_version(120, 100)) {
5535 YYLTYPE loc
= this->get_location();
5536 _mesa_glsl_error(&loc
, state
,
5537 "declaration of function `%s' not allowed within "
5538 "function body", name
);
5541 validate_identifier(name
, this->get_location(), state
);
5543 /* Convert the list of function parameters to HIR now so that they can be
5544 * used below to compare this function's signature with previously seen
5545 * signatures for functions with the same name.
5547 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
5549 & hir_parameters
, state
);
5551 const char *return_type_name
;
5552 const glsl_type
*return_type
=
5553 this->return_type
->glsl_type(& return_type_name
, state
);
5556 YYLTYPE loc
= this->get_location();
5557 _mesa_glsl_error(&loc
, state
,
5558 "function `%s' has undeclared return type `%s'",
5559 name
, return_type_name
);
5560 return_type
= glsl_type::error_type
;
5563 /* ARB_shader_subroutine states:
5564 * "Subroutine declarations cannot be prototyped. It is an error to prepend
5565 * subroutine(...) to a function declaration."
5567 if (this->return_type
->qualifier
.flags
.q
.subroutine_def
&& !is_definition
) {
5568 YYLTYPE loc
= this->get_location();
5569 _mesa_glsl_error(&loc
, state
,
5570 "function declaration `%s' cannot have subroutine prepended",
5574 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
5575 * "No qualifier is allowed on the return type of a function."
5577 if (this->return_type
->has_qualifiers(state
)) {
5578 YYLTYPE loc
= this->get_location();
5579 _mesa_glsl_error(& loc
, state
,
5580 "function `%s' return type has qualifiers", name
);
5583 /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
5585 * "Arrays are allowed as arguments and as the return type. In both
5586 * cases, the array must be explicitly sized."
5588 if (return_type
->is_unsized_array()) {
5589 YYLTYPE loc
= this->get_location();
5590 _mesa_glsl_error(& loc
, state
,
5591 "function `%s' return type array must be explicitly "
5595 /* From section 4.1.7 of the GLSL 4.40 spec:
5597 * "[Opaque types] can only be declared as function parameters
5598 * or uniform-qualified variables."
5600 if (return_type
->contains_opaque()) {
5601 YYLTYPE loc
= this->get_location();
5602 _mesa_glsl_error(&loc
, state
,
5603 "function `%s' return type can't contain an opaque type",
5608 if (return_type
->is_subroutine()) {
5609 YYLTYPE loc
= this->get_location();
5610 _mesa_glsl_error(&loc
, state
,
5611 "function `%s' return type can't be a subroutine type",
5616 /* Create an ir_function if one doesn't already exist. */
5617 f
= state
->symbols
->get_function(name
);
5619 f
= new(ctx
) ir_function(name
);
5620 if (!this->return_type
->qualifier
.flags
.q
.subroutine
) {
5621 if (!state
->symbols
->add_function(f
)) {
5622 /* This function name shadows a non-function use of the same name. */
5623 YYLTYPE loc
= this->get_location();
5624 _mesa_glsl_error(&loc
, state
, "function name `%s' conflicts with "
5625 "non-function", name
);
5629 emit_function(state
, f
);
5632 /* From GLSL ES 3.0 spec, chapter 6.1 "Function Definitions", page 71:
5634 * "A shader cannot redefine or overload built-in functions."
5636 * While in GLSL ES 1.0 specification, chapter 8 "Built-in Functions":
5638 * "User code can overload the built-in functions but cannot redefine
5641 if (state
->es_shader
&& state
->language_version
>= 300) {
5642 /* Local shader has no exact candidates; check the built-ins. */
5643 _mesa_glsl_initialize_builtin_functions();
5644 if (_mesa_glsl_find_builtin_function_by_name(name
)) {
5645 YYLTYPE loc
= this->get_location();
5646 _mesa_glsl_error(& loc
, state
,
5647 "A shader cannot redefine or overload built-in "
5648 "function `%s' in GLSL ES 3.00", name
);
5653 /* Verify that this function's signature either doesn't match a previously
5654 * seen signature for a function with the same name, or, if a match is found,
5655 * that the previously seen signature does not have an associated definition.
5657 if (state
->es_shader
|| f
->has_user_signature()) {
5658 sig
= f
->exact_matching_signature(state
, &hir_parameters
);
5660 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
5661 if (badvar
!= NULL
) {
5662 YYLTYPE loc
= this->get_location();
5664 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
5665 "qualifiers don't match prototype", name
, badvar
);
5668 if (sig
->return_type
!= return_type
) {
5669 YYLTYPE loc
= this->get_location();
5671 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
5672 "match prototype", name
);
5675 if (sig
->is_defined
) {
5676 if (is_definition
) {
5677 YYLTYPE loc
= this->get_location();
5678 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
5680 /* We just encountered a prototype that exactly matches a
5681 * function that's already been defined. This is redundant,
5682 * and we should ignore it.
5690 /* Verify the return type of main() */
5691 if (strcmp(name
, "main") == 0) {
5692 if (! return_type
->is_void()) {
5693 YYLTYPE loc
= this->get_location();
5695 _mesa_glsl_error(& loc
, state
, "main() must return void");
5698 if (!hir_parameters
.is_empty()) {
5699 YYLTYPE loc
= this->get_location();
5701 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
5705 /* Finish storing the information about this new function in its signature.
5708 sig
= new(ctx
) ir_function_signature(return_type
);
5709 f
->add_signature(sig
);
5712 sig
->replace_parameters(&hir_parameters
);
5715 if (this->return_type
->qualifier
.flags
.q
.subroutine_def
) {
5718 if (this->return_type
->qualifier
.flags
.q
.explicit_index
) {
5719 unsigned qual_index
;
5720 if (process_qualifier_constant(state
, &loc
, "index",
5721 this->return_type
->qualifier
.index
,
5723 if (!state
->has_explicit_uniform_location()) {
5724 _mesa_glsl_error(&loc
, state
, "subroutine index requires "
5725 "GL_ARB_explicit_uniform_location or "
5727 } else if (qual_index
>= MAX_SUBROUTINES
) {
5728 _mesa_glsl_error(&loc
, state
,
5729 "invalid subroutine index (%d) index must "
5730 "be a number between 0 and "
5731 "GL_MAX_SUBROUTINES - 1 (%d)", qual_index
,
5732 MAX_SUBROUTINES
- 1);
5734 f
->subroutine_index
= qual_index
;
5739 f
->num_subroutine_types
= this->return_type
->qualifier
.subroutine_list
->declarations
.length();
5740 f
->subroutine_types
= ralloc_array(state
, const struct glsl_type
*,
5741 f
->num_subroutine_types
);
5743 foreach_list_typed(ast_declaration
, decl
, link
, &this->return_type
->qualifier
.subroutine_list
->declarations
) {
5744 const struct glsl_type
*type
;
5745 /* the subroutine type must be already declared */
5746 type
= state
->symbols
->get_type(decl
->identifier
);
5748 _mesa_glsl_error(& loc
, state
, "unknown type '%s' in subroutine function definition", decl
->identifier
);
5751 for (int i
= 0; i
< state
->num_subroutine_types
; i
++) {
5752 ir_function
*fn
= state
->subroutine_types
[i
];
5753 ir_function_signature
*tsig
= NULL
;
5755 if (strcmp(fn
->name
, decl
->identifier
))
5758 tsig
= fn
->matching_signature(state
, &sig
->parameters
,
5761 _mesa_glsl_error(& loc
, state
, "subroutine type mismatch '%s' - signatures do not match\n", decl
->identifier
);
5763 if (tsig
->return_type
!= sig
->return_type
) {
5764 _mesa_glsl_error(& loc
, state
, "subroutine type mismatch '%s' - return types do not match\n", decl
->identifier
);
5768 f
->subroutine_types
[idx
++] = type
;
5770 state
->subroutines
= (ir_function
**)reralloc(state
, state
->subroutines
,
5772 state
->num_subroutines
+ 1);
5773 state
->subroutines
[state
->num_subroutines
] = f
;
5774 state
->num_subroutines
++;
5778 if (this->return_type
->qualifier
.flags
.q
.subroutine
) {
5779 if (!state
->symbols
->add_type(this->identifier
, glsl_type::get_subroutine_instance(this->identifier
))) {
5780 _mesa_glsl_error(& loc
, state
, "type '%s' previously defined", this->identifier
);
5783 state
->subroutine_types
= (ir_function
**)reralloc(state
, state
->subroutine_types
,
5785 state
->num_subroutine_types
+ 1);
5786 state
->subroutine_types
[state
->num_subroutine_types
] = f
;
5787 state
->num_subroutine_types
++;
5789 f
->is_subroutine
= true;
5792 /* Function declarations (prototypes) do not have r-values.
5799 ast_function_definition::hir(exec_list
*instructions
,
5800 struct _mesa_glsl_parse_state
*state
)
5802 prototype
->is_definition
= true;
5803 prototype
->hir(instructions
, state
);
5805 ir_function_signature
*signature
= prototype
->signature
;
5806 if (signature
== NULL
)
5809 assert(state
->current_function
== NULL
);
5810 state
->current_function
= signature
;
5811 state
->found_return
= false;
5813 /* Duplicate parameters declared in the prototype as concrete variables.
5814 * Add these to the symbol table.
5816 state
->symbols
->push_scope();
5817 foreach_in_list(ir_variable
, var
, &signature
->parameters
) {
5818 assert(var
->as_variable() != NULL
);
5820 /* The only way a parameter would "exist" is if two parameters have
5823 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
5824 YYLTYPE loc
= this->get_location();
5826 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
5828 state
->symbols
->add_variable(var
);
5832 /* Convert the body of the function to HIR. */
5833 this->body
->hir(&signature
->body
, state
);
5834 signature
->is_defined
= true;
5836 state
->symbols
->pop_scope();
5838 assert(state
->current_function
== signature
);
5839 state
->current_function
= NULL
;
5841 if (!signature
->return_type
->is_void() && !state
->found_return
) {
5842 YYLTYPE loc
= this->get_location();
5843 _mesa_glsl_error(& loc
, state
, "function `%s' has non-void return type "
5844 "%s, but no return statement",
5845 signature
->function_name(),
5846 signature
->return_type
->name
);
5849 /* Function definitions do not have r-values.
5856 ast_jump_statement::hir(exec_list
*instructions
,
5857 struct _mesa_glsl_parse_state
*state
)
5864 assert(state
->current_function
);
5866 if (opt_return_value
) {
5867 ir_rvalue
*ret
= opt_return_value
->hir(instructions
, state
);
5869 /* The value of the return type can be NULL if the shader says
5870 * 'return foo();' and foo() is a function that returns void.
5872 * NOTE: The GLSL spec doesn't say that this is an error. The type
5873 * of the return value is void. If the return type of the function is
5874 * also void, then this should compile without error. Seriously.
5876 const glsl_type
*const ret_type
=
5877 (ret
== NULL
) ? glsl_type::void_type
: ret
->type
;
5879 /* Implicit conversions are not allowed for return values prior to
5880 * ARB_shading_language_420pack.
5882 if (state
->current_function
->return_type
!= ret_type
) {
5883 YYLTYPE loc
= this->get_location();
5885 if (state
->has_420pack()) {
5886 if (!apply_implicit_conversion(state
->current_function
->return_type
,
5888 _mesa_glsl_error(& loc
, state
,
5889 "could not implicitly convert return value "
5890 "to %s, in function `%s'",
5891 state
->current_function
->return_type
->name
,
5892 state
->current_function
->function_name());
5895 _mesa_glsl_error(& loc
, state
,
5896 "`return' with wrong type %s, in function `%s' "
5899 state
->current_function
->function_name(),
5900 state
->current_function
->return_type
->name
);
5902 } else if (state
->current_function
->return_type
->base_type
==
5904 YYLTYPE loc
= this->get_location();
5906 /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
5907 * specs add a clarification:
5909 * "A void function can only use return without a return argument, even if
5910 * the return argument has void type. Return statements only accept values:
5913 * void func2() { return func1(); } // illegal return statement"
5915 _mesa_glsl_error(& loc
, state
,
5916 "void functions can only use `return' without a "
5920 inst
= new(ctx
) ir_return(ret
);
5922 if (state
->current_function
->return_type
->base_type
!=
5924 YYLTYPE loc
= this->get_location();
5926 _mesa_glsl_error(& loc
, state
,
5927 "`return' with no value, in function %s returning "
5929 state
->current_function
->function_name());
5931 inst
= new(ctx
) ir_return
;
5934 state
->found_return
= true;
5935 instructions
->push_tail(inst
);
5940 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
5941 YYLTYPE loc
= this->get_location();
5943 _mesa_glsl_error(& loc
, state
,
5944 "`discard' may only appear in a fragment shader");
5946 instructions
->push_tail(new(ctx
) ir_discard
);
5951 if (mode
== ast_continue
&&
5952 state
->loop_nesting_ast
== NULL
) {
5953 YYLTYPE loc
= this->get_location();
5955 _mesa_glsl_error(& loc
, state
, "continue may only appear in a loop");
5956 } else if (mode
== ast_break
&&
5957 state
->loop_nesting_ast
== NULL
&&
5958 state
->switch_state
.switch_nesting_ast
== NULL
) {
5959 YYLTYPE loc
= this->get_location();
5961 _mesa_glsl_error(& loc
, state
,
5962 "break may only appear in a loop or a switch");
5964 /* For a loop, inline the for loop expression again, since we don't
5965 * know where near the end of the loop body the normal copy of it is
5966 * going to be placed. Same goes for the condition for a do-while
5969 if (state
->loop_nesting_ast
!= NULL
&&
5970 mode
== ast_continue
&& !state
->switch_state
.is_switch_innermost
) {
5971 if (state
->loop_nesting_ast
->rest_expression
) {
5972 state
->loop_nesting_ast
->rest_expression
->hir(instructions
,
5975 if (state
->loop_nesting_ast
->mode
==
5976 ast_iteration_statement::ast_do_while
) {
5977 state
->loop_nesting_ast
->condition_to_hir(instructions
, state
);
5981 if (state
->switch_state
.is_switch_innermost
&&
5982 mode
== ast_continue
) {
5983 /* Set 'continue_inside' to true. */
5984 ir_rvalue
*const true_val
= new (ctx
) ir_constant(true);
5985 ir_dereference_variable
*deref_continue_inside_var
=
5986 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
5987 instructions
->push_tail(new(ctx
) ir_assignment(deref_continue_inside_var
,
5990 /* Break out from the switch, continue for the loop will
5991 * be called right after switch. */
5992 ir_loop_jump
*const jump
=
5993 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
5994 instructions
->push_tail(jump
);
5996 } else if (state
->switch_state
.is_switch_innermost
&&
5997 mode
== ast_break
) {
5998 /* Force break out of switch by inserting a break. */
5999 ir_loop_jump
*const jump
=
6000 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
6001 instructions
->push_tail(jump
);
6003 ir_loop_jump
*const jump
=
6004 new(ctx
) ir_loop_jump((mode
== ast_break
)
6005 ? ir_loop_jump::jump_break
6006 : ir_loop_jump::jump_continue
);
6007 instructions
->push_tail(jump
);
6014 /* Jump instructions do not have r-values.
6021 ast_selection_statement::hir(exec_list
*instructions
,
6022 struct _mesa_glsl_parse_state
*state
)
6026 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
6028 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
6030 * "Any expression whose type evaluates to a Boolean can be used as the
6031 * conditional expression bool-expression. Vector types are not accepted
6032 * as the expression to if."
6034 * The checks are separated so that higher quality diagnostics can be
6035 * generated for cases where both rules are violated.
6037 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
6038 YYLTYPE loc
= this->condition
->get_location();
6040 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
6044 ir_if
*const stmt
= new(ctx
) ir_if(condition
);
6046 if (then_statement
!= NULL
) {
6047 state
->symbols
->push_scope();
6048 then_statement
->hir(& stmt
->then_instructions
, state
);
6049 state
->symbols
->pop_scope();
6052 if (else_statement
!= NULL
) {
6053 state
->symbols
->push_scope();
6054 else_statement
->hir(& stmt
->else_instructions
, state
);
6055 state
->symbols
->pop_scope();
6058 instructions
->push_tail(stmt
);
6060 /* if-statements do not have r-values.
6066 /* Used for detection of duplicate case values, compare
6067 * given contents directly.
6070 compare_case_value(const void *a
, const void *b
)
6072 return *(unsigned *) a
== *(unsigned *) b
;
6076 /* Used for detection of duplicate case values, just
6077 * returns key contents as is.
6080 key_contents(const void *key
)
6082 return *(unsigned *) key
;
6087 ast_switch_statement::hir(exec_list
*instructions
,
6088 struct _mesa_glsl_parse_state
*state
)
6092 ir_rvalue
*const test_expression
=
6093 this->test_expression
->hir(instructions
, state
);
6095 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
6097 * "The type of init-expression in a switch statement must be a
6100 if (!test_expression
->type
->is_scalar() ||
6101 !test_expression
->type
->is_integer()) {
6102 YYLTYPE loc
= this->test_expression
->get_location();
6104 _mesa_glsl_error(& loc
,
6106 "switch-statement expression must be scalar "
6110 /* Track the switch-statement nesting in a stack-like manner.
6112 struct glsl_switch_state saved
= state
->switch_state
;
6114 state
->switch_state
.is_switch_innermost
= true;
6115 state
->switch_state
.switch_nesting_ast
= this;
6116 state
->switch_state
.labels_ht
=
6117 _mesa_hash_table_create(NULL
, key_contents
,
6118 compare_case_value
);
6119 state
->switch_state
.previous_default
= NULL
;
6121 /* Initalize is_fallthru state to false.
6123 ir_rvalue
*const is_fallthru_val
= new (ctx
) ir_constant(false);
6124 state
->switch_state
.is_fallthru_var
=
6125 new(ctx
) ir_variable(glsl_type::bool_type
,
6126 "switch_is_fallthru_tmp",
6128 instructions
->push_tail(state
->switch_state
.is_fallthru_var
);
6130 ir_dereference_variable
*deref_is_fallthru_var
=
6131 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
6132 instructions
->push_tail(new(ctx
) ir_assignment(deref_is_fallthru_var
,
6135 /* Initialize continue_inside state to false.
6137 state
->switch_state
.continue_inside
=
6138 new(ctx
) ir_variable(glsl_type::bool_type
,
6139 "continue_inside_tmp",
6141 instructions
->push_tail(state
->switch_state
.continue_inside
);
6143 ir_rvalue
*const false_val
= new (ctx
) ir_constant(false);
6144 ir_dereference_variable
*deref_continue_inside_var
=
6145 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
6146 instructions
->push_tail(new(ctx
) ir_assignment(deref_continue_inside_var
,
6149 state
->switch_state
.run_default
=
6150 new(ctx
) ir_variable(glsl_type::bool_type
,
6153 instructions
->push_tail(state
->switch_state
.run_default
);
6155 /* Loop around the switch is used for flow control. */
6156 ir_loop
* loop
= new(ctx
) ir_loop();
6157 instructions
->push_tail(loop
);
6159 /* Cache test expression.
6161 test_to_hir(&loop
->body_instructions
, state
);
6163 /* Emit code for body of switch stmt.
6165 body
->hir(&loop
->body_instructions
, state
);
6167 /* Insert a break at the end to exit loop. */
6168 ir_loop_jump
*jump
= new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
6169 loop
->body_instructions
.push_tail(jump
);
6171 /* If we are inside loop, check if continue got called inside switch. */
6172 if (state
->loop_nesting_ast
!= NULL
) {
6173 ir_dereference_variable
*deref_continue_inside
=
6174 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
6175 ir_if
*irif
= new(ctx
) ir_if(deref_continue_inside
);
6176 ir_loop_jump
*jump
= new(ctx
) ir_loop_jump(ir_loop_jump::jump_continue
);
6178 if (state
->loop_nesting_ast
!= NULL
) {
6179 if (state
->loop_nesting_ast
->rest_expression
) {
6180 state
->loop_nesting_ast
->rest_expression
->hir(&irif
->then_instructions
,
6183 if (state
->loop_nesting_ast
->mode
==
6184 ast_iteration_statement::ast_do_while
) {
6185 state
->loop_nesting_ast
->condition_to_hir(&irif
->then_instructions
, state
);
6188 irif
->then_instructions
.push_tail(jump
);
6189 instructions
->push_tail(irif
);
6192 _mesa_hash_table_destroy(state
->switch_state
.labels_ht
, NULL
);
6194 state
->switch_state
= saved
;
6196 /* Switch statements do not have r-values. */
6202 ast_switch_statement::test_to_hir(exec_list
*instructions
,
6203 struct _mesa_glsl_parse_state
*state
)
6207 /* set to true to avoid a duplicate "use of uninitialized variable" warning
6208 * on the switch test case. The first one would be already raised when
6209 * getting the test_expression at ast_switch_statement::hir
6211 test_expression
->set_is_lhs(true);
6212 /* Cache value of test expression. */
6213 ir_rvalue
*const test_val
= test_expression
->hir(instructions
, state
);
6215 state
->switch_state
.test_var
= new(ctx
) ir_variable(test_val
->type
,
6218 ir_dereference_variable
*deref_test_var
=
6219 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
6221 instructions
->push_tail(state
->switch_state
.test_var
);
6222 instructions
->push_tail(new(ctx
) ir_assignment(deref_test_var
, test_val
));
6227 ast_switch_body::hir(exec_list
*instructions
,
6228 struct _mesa_glsl_parse_state
*state
)
6231 stmts
->hir(instructions
, state
);
6233 /* Switch bodies do not have r-values. */
6238 ast_case_statement_list::hir(exec_list
*instructions
,
6239 struct _mesa_glsl_parse_state
*state
)
6241 exec_list default_case
, after_default
, tmp
;
6243 foreach_list_typed (ast_case_statement
, case_stmt
, link
, & this->cases
) {
6244 case_stmt
->hir(&tmp
, state
);
6247 if (state
->switch_state
.previous_default
&& default_case
.is_empty()) {
6248 default_case
.append_list(&tmp
);
6252 /* If default case found, append 'after_default' list. */
6253 if (!default_case
.is_empty())
6254 after_default
.append_list(&tmp
);
6256 instructions
->append_list(&tmp
);
6259 /* Handle the default case. This is done here because default might not be
6260 * the last case. We need to add checks against following cases first to see
6261 * if default should be chosen or not.
6263 if (!default_case
.is_empty()) {
6265 ir_rvalue
*const true_val
= new (state
) ir_constant(true);
6266 ir_dereference_variable
*deref_run_default_var
=
6267 new(state
) ir_dereference_variable(state
->switch_state
.run_default
);
6269 /* Choose to run default case initially, following conditional
6270 * assignments might change this.
6272 ir_assignment
*const init_var
=
6273 new(state
) ir_assignment(deref_run_default_var
, true_val
);
6274 instructions
->push_tail(init_var
);
6276 /* Default case was the last one, no checks required. */
6277 if (after_default
.is_empty()) {
6278 instructions
->append_list(&default_case
);
6282 foreach_in_list(ir_instruction
, ir
, &after_default
) {
6283 ir_assignment
*assign
= ir
->as_assignment();
6288 /* Clone the check between case label and init expression. */
6289 ir_expression
*exp
= (ir_expression
*) assign
->condition
;
6290 ir_expression
*clone
= exp
->clone(state
, NULL
);
6292 ir_dereference_variable
*deref_var
=
6293 new(state
) ir_dereference_variable(state
->switch_state
.run_default
);
6294 ir_rvalue
*const false_val
= new (state
) ir_constant(false);
6296 ir_assignment
*const set_false
=
6297 new(state
) ir_assignment(deref_var
, false_val
, clone
);
6299 instructions
->push_tail(set_false
);
6302 /* Append default case and all cases after it. */
6303 instructions
->append_list(&default_case
);
6304 instructions
->append_list(&after_default
);
6307 /* Case statements do not have r-values. */
6312 ast_case_statement::hir(exec_list
*instructions
,
6313 struct _mesa_glsl_parse_state
*state
)
6315 labels
->hir(instructions
, state
);
6317 /* Guard case statements depending on fallthru state. */
6318 ir_dereference_variable
*const deref_fallthru_guard
=
6319 new(state
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
6320 ir_if
*const test_fallthru
= new(state
) ir_if(deref_fallthru_guard
);
6322 foreach_list_typed (ast_node
, stmt
, link
, & this->stmts
)
6323 stmt
->hir(& test_fallthru
->then_instructions
, state
);
6325 instructions
->push_tail(test_fallthru
);
6327 /* Case statements do not have r-values. */
6333 ast_case_label_list::hir(exec_list
*instructions
,
6334 struct _mesa_glsl_parse_state
*state
)
6336 foreach_list_typed (ast_case_label
, label
, link
, & this->labels
)
6337 label
->hir(instructions
, state
);
6339 /* Case labels do not have r-values. */
6344 ast_case_label::hir(exec_list
*instructions
,
6345 struct _mesa_glsl_parse_state
*state
)
6349 ir_dereference_variable
*deref_fallthru_var
=
6350 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
6352 ir_rvalue
*const true_val
= new(ctx
) ir_constant(true);
6354 /* If not default case, ... */
6355 if (this->test_value
!= NULL
) {
6356 /* Conditionally set fallthru state based on
6357 * comparison of cached test expression value to case label.
6359 ir_rvalue
*const label_rval
= this->test_value
->hir(instructions
, state
);
6360 ir_constant
*label_const
= label_rval
->constant_expression_value();
6363 YYLTYPE loc
= this->test_value
->get_location();
6365 _mesa_glsl_error(& loc
, state
,
6366 "switch statement case label must be a "
6367 "constant expression");
6369 /* Stuff a dummy value in to allow processing to continue. */
6370 label_const
= new(ctx
) ir_constant(0);
6373 _mesa_hash_table_search(state
->switch_state
.labels_ht
,
6374 (void *)(uintptr_t)&label_const
->value
.u
[0]);
6377 ast_expression
*previous_label
= (ast_expression
*) entry
->data
;
6378 YYLTYPE loc
= this->test_value
->get_location();
6379 _mesa_glsl_error(& loc
, state
, "duplicate case value");
6381 loc
= previous_label
->get_location();
6382 _mesa_glsl_error(& loc
, state
, "this is the previous case label");
6384 _mesa_hash_table_insert(state
->switch_state
.labels_ht
,
6385 (void *)(uintptr_t)&label_const
->value
.u
[0],
6390 ir_dereference_variable
*deref_test_var
=
6391 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
6393 ir_expression
*test_cond
= new(ctx
) ir_expression(ir_binop_all_equal
,
6398 * From GLSL 4.40 specification section 6.2 ("Selection"):
6400 * "The type of the init-expression value in a switch statement must
6401 * be a scalar int or uint. The type of the constant-expression value
6402 * in a case label also must be a scalar int or uint. When any pair
6403 * of these values is tested for "equal value" and the types do not
6404 * match, an implicit conversion will be done to convert the int to a
6405 * uint (see section 4.1.10 “Implicit Conversions”) before the compare
6408 if (label_const
->type
!= state
->switch_state
.test_var
->type
) {
6409 YYLTYPE loc
= this->test_value
->get_location();
6411 const glsl_type
*type_a
= label_const
->type
;
6412 const glsl_type
*type_b
= state
->switch_state
.test_var
->type
;
6414 /* Check if int->uint implicit conversion is supported. */
6415 bool integer_conversion_supported
=
6416 glsl_type::int_type
->can_implicitly_convert_to(glsl_type::uint_type
,
6419 if ((!type_a
->is_integer() || !type_b
->is_integer()) ||
6420 !integer_conversion_supported
) {
6421 _mesa_glsl_error(&loc
, state
, "type mismatch with switch "
6422 "init-expression and case label (%s != %s)",
6423 type_a
->name
, type_b
->name
);
6425 /* Conversion of the case label. */
6426 if (type_a
->base_type
== GLSL_TYPE_INT
) {
6427 if (!apply_implicit_conversion(glsl_type::uint_type
,
6428 test_cond
->operands
[0], state
))
6429 _mesa_glsl_error(&loc
, state
, "implicit type conversion error");
6431 /* Conversion of the init-expression value. */
6432 if (!apply_implicit_conversion(glsl_type::uint_type
,
6433 test_cond
->operands
[1], state
))
6434 _mesa_glsl_error(&loc
, state
, "implicit type conversion error");
6439 ir_assignment
*set_fallthru_on_test
=
6440 new(ctx
) ir_assignment(deref_fallthru_var
, true_val
, test_cond
);
6442 instructions
->push_tail(set_fallthru_on_test
);
6443 } else { /* default case */
6444 if (state
->switch_state
.previous_default
) {
6445 YYLTYPE loc
= this->get_location();
6446 _mesa_glsl_error(& loc
, state
,
6447 "multiple default labels in one switch");
6449 loc
= state
->switch_state
.previous_default
->get_location();
6450 _mesa_glsl_error(& loc
, state
, "this is the first default label");
6452 state
->switch_state
.previous_default
= this;
6454 /* Set fallthru condition on 'run_default' bool. */
6455 ir_dereference_variable
*deref_run_default
=
6456 new(ctx
) ir_dereference_variable(state
->switch_state
.run_default
);
6457 ir_rvalue
*const cond_true
= new(ctx
) ir_constant(true);
6458 ir_expression
*test_cond
= new(ctx
) ir_expression(ir_binop_all_equal
,
6462 /* Set falltrhu state. */
6463 ir_assignment
*set_fallthru
=
6464 new(ctx
) ir_assignment(deref_fallthru_var
, true_val
, test_cond
);
6466 instructions
->push_tail(set_fallthru
);
6469 /* Case statements do not have r-values. */
6474 ast_iteration_statement::condition_to_hir(exec_list
*instructions
,
6475 struct _mesa_glsl_parse_state
*state
)
6479 if (condition
!= NULL
) {
6480 ir_rvalue
*const cond
=
6481 condition
->hir(instructions
, state
);
6484 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
6485 YYLTYPE loc
= condition
->get_location();
6487 _mesa_glsl_error(& loc
, state
,
6488 "loop condition must be scalar boolean");
6490 /* As the first code in the loop body, generate a block that looks
6491 * like 'if (!condition) break;' as the loop termination condition.
6493 ir_rvalue
*const not_cond
=
6494 new(ctx
) ir_expression(ir_unop_logic_not
, cond
);
6496 ir_if
*const if_stmt
= new(ctx
) ir_if(not_cond
);
6498 ir_jump
*const break_stmt
=
6499 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
6501 if_stmt
->then_instructions
.push_tail(break_stmt
);
6502 instructions
->push_tail(if_stmt
);
6509 ast_iteration_statement::hir(exec_list
*instructions
,
6510 struct _mesa_glsl_parse_state
*state
)
6514 /* For-loops and while-loops start a new scope, but do-while loops do not.
6516 if (mode
!= ast_do_while
)
6517 state
->symbols
->push_scope();
6519 if (init_statement
!= NULL
)
6520 init_statement
->hir(instructions
, state
);
6522 ir_loop
*const stmt
= new(ctx
) ir_loop();
6523 instructions
->push_tail(stmt
);
6525 /* Track the current loop nesting. */
6526 ast_iteration_statement
*nesting_ast
= state
->loop_nesting_ast
;
6528 state
->loop_nesting_ast
= this;
6530 /* Likewise, indicate that following code is closest to a loop,
6531 * NOT closest to a switch.
6533 bool saved_is_switch_innermost
= state
->switch_state
.is_switch_innermost
;
6534 state
->switch_state
.is_switch_innermost
= false;
6536 if (mode
!= ast_do_while
)
6537 condition_to_hir(&stmt
->body_instructions
, state
);
6540 body
->hir(& stmt
->body_instructions
, state
);
6542 if (rest_expression
!= NULL
)
6543 rest_expression
->hir(& stmt
->body_instructions
, state
);
6545 if (mode
== ast_do_while
)
6546 condition_to_hir(&stmt
->body_instructions
, state
);
6548 if (mode
!= ast_do_while
)
6549 state
->symbols
->pop_scope();
6551 /* Restore previous nesting before returning. */
6552 state
->loop_nesting_ast
= nesting_ast
;
6553 state
->switch_state
.is_switch_innermost
= saved_is_switch_innermost
;
6555 /* Loops do not have r-values.
6562 * Determine if the given type is valid for establishing a default precision
6565 * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
6567 * "The precision statement
6569 * precision precision-qualifier type;
6571 * can be used to establish a default precision qualifier. The type field
6572 * can be either int or float or any of the sampler types, and the
6573 * precision-qualifier can be lowp, mediump, or highp."
6575 * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
6576 * qualifiers on sampler types, but this seems like an oversight (since the
6577 * intention of including these in GLSL 1.30 is to allow compatibility with ES
6578 * shaders). So we allow int, float, and all sampler types regardless of GLSL
6582 is_valid_default_precision_type(const struct glsl_type
*const type
)
6587 switch (type
->base_type
) {
6589 case GLSL_TYPE_FLOAT
:
6590 /* "int" and "float" are valid, but vectors and matrices are not. */
6591 return type
->vector_elements
== 1 && type
->matrix_columns
== 1;
6592 case GLSL_TYPE_SAMPLER
:
6593 case GLSL_TYPE_IMAGE
:
6594 case GLSL_TYPE_ATOMIC_UINT
:
6603 ast_type_specifier::hir(exec_list
*instructions
,
6604 struct _mesa_glsl_parse_state
*state
)
6606 if (this->default_precision
== ast_precision_none
&& this->structure
== NULL
)
6609 YYLTYPE loc
= this->get_location();
6611 /* If this is a precision statement, check that the type to which it is
6612 * applied is either float or int.
6614 * From section 4.5.3 of the GLSL 1.30 spec:
6615 * "The precision statement
6616 * precision precision-qualifier type;
6617 * can be used to establish a default precision qualifier. The type
6618 * field can be either int or float [...]. Any other types or
6619 * qualifiers will result in an error.
6621 if (this->default_precision
!= ast_precision_none
) {
6622 if (!state
->check_precision_qualifiers_allowed(&loc
))
6625 if (this->structure
!= NULL
) {
6626 _mesa_glsl_error(&loc
, state
,
6627 "precision qualifiers do not apply to structures");
6631 if (this->array_specifier
!= NULL
) {
6632 _mesa_glsl_error(&loc
, state
,
6633 "default precision statements do not apply to "
6638 const struct glsl_type
*const type
=
6639 state
->symbols
->get_type(this->type_name
);
6640 if (!is_valid_default_precision_type(type
)) {
6641 _mesa_glsl_error(&loc
, state
,
6642 "default precision statements apply only to "
6643 "float, int, and opaque types");
6647 if (state
->es_shader
) {
6648 /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
6651 * "Non-precision qualified declarations will use the precision
6652 * qualifier specified in the most recent precision statement
6653 * that is still in scope. The precision statement has the same
6654 * scoping rules as variable declarations. If it is declared
6655 * inside a compound statement, its effect stops at the end of
6656 * the innermost statement it was declared in. Precision
6657 * statements in nested scopes override precision statements in
6658 * outer scopes. Multiple precision statements for the same basic
6659 * type can appear inside the same scope, with later statements
6660 * overriding earlier statements within that scope."
6662 * Default precision specifications follow the same scope rules as
6663 * variables. So, we can track the state of the default precision
6664 * qualifiers in the symbol table, and the rules will just work. This
6665 * is a slight abuse of the symbol table, but it has the semantics
6668 state
->symbols
->add_default_precision_qualifier(this->type_name
,
6669 this->default_precision
);
6672 /* FINISHME: Translate precision statements into IR. */
6676 /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
6677 * process_record_constructor() can do type-checking on C-style initializer
6678 * expressions of structs, but ast_struct_specifier should only be translated
6679 * to HIR if it is declaring the type of a structure.
6681 * The ->is_declaration field is false for initializers of variables
6682 * declared separately from the struct's type definition.
6684 * struct S { ... }; (is_declaration = true)
6685 * struct T { ... } t = { ... }; (is_declaration = true)
6686 * S s = { ... }; (is_declaration = false)
6688 if (this->structure
!= NULL
&& this->structure
->is_declaration
)
6689 return this->structure
->hir(instructions
, state
);
6696 * Process a structure or interface block tree into an array of structure fields
6698 * After parsing, where there are some syntax differnces, structures and
6699 * interface blocks are almost identical. They are similar enough that the
6700 * AST for each can be processed the same way into a set of
6701 * \c glsl_struct_field to describe the members.
6703 * If we're processing an interface block, var_mode should be the type of the
6704 * interface block (ir_var_shader_in, ir_var_shader_out, ir_var_uniform or
6705 * ir_var_shader_storage). If we're processing a structure, var_mode should be
6709 * The number of fields processed. A pointer to the array structure fields is
6710 * stored in \c *fields_ret.
6713 ast_process_struct_or_iface_block_members(exec_list
*instructions
,
6714 struct _mesa_glsl_parse_state
*state
,
6715 exec_list
*declarations
,
6716 glsl_struct_field
**fields_ret
,
6718 enum glsl_matrix_layout matrix_layout
,
6719 bool allow_reserved_names
,
6720 ir_variable_mode var_mode
,
6721 ast_type_qualifier
*layout
,
6722 unsigned block_stream
,
6723 unsigned block_xfb_buffer
,
6724 unsigned block_xfb_offset
,
6725 unsigned expl_location
,
6726 unsigned expl_align
)
6728 unsigned decl_count
= 0;
6729 unsigned next_offset
= 0;
6731 /* Make an initial pass over the list of fields to determine how
6732 * many there are. Each element in this list is an ast_declarator_list.
6733 * This means that we actually need to count the number of elements in the
6734 * 'declarations' list in each of the elements.
6736 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
6737 decl_count
+= decl_list
->declarations
.length();
6740 /* Allocate storage for the fields and process the field
6741 * declarations. As the declarations are processed, try to also convert
6742 * the types to HIR. This ensures that structure definitions embedded in
6743 * other structure definitions or in interface blocks are processed.
6745 glsl_struct_field
*const fields
= rzalloc_array(state
, glsl_struct_field
,
6748 bool first_member
= true;
6749 bool first_member_has_explicit_location
= false;
6752 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
6753 const char *type_name
;
6754 YYLTYPE loc
= decl_list
->get_location();
6756 decl_list
->type
->specifier
->hir(instructions
, state
);
6758 /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
6760 * "Anonymous structures are not supported; so embedded structures
6761 * must have a declarator. A name given to an embedded struct is
6762 * scoped at the same level as the struct it is embedded in."
6764 * The same section of the GLSL 1.20 spec says:
6766 * "Anonymous structures are not supported. Embedded structures are
6769 * The GLSL ES 1.00 and 3.00 specs have similar langauge. So, we allow
6770 * embedded structures in 1.10 only.
6772 if (state
->language_version
!= 110 &&
6773 decl_list
->type
->specifier
->structure
!= NULL
)
6774 _mesa_glsl_error(&loc
, state
,
6775 "embedded structure declarations are not allowed");
6777 const glsl_type
*decl_type
=
6778 decl_list
->type
->glsl_type(& type_name
, state
);
6780 const struct ast_type_qualifier
*const qual
=
6781 &decl_list
->type
->qualifier
;
6783 /* From section 4.3.9 of the GLSL 4.40 spec:
6785 * "[In interface blocks] opaque types are not allowed."
6787 * It should be impossible for decl_type to be NULL here. Cases that
6788 * might naturally lead to decl_type being NULL, especially for the
6789 * is_interface case, will have resulted in compilation having
6790 * already halted due to a syntax error.
6795 if (decl_type
->contains_opaque()) {
6796 _mesa_glsl_error(&loc
, state
, "uniform/buffer in non-default "
6797 "interface block contains opaque variable");
6800 if (decl_type
->contains_atomic()) {
6801 /* From section 4.1.7.3 of the GLSL 4.40 spec:
6803 * "Members of structures cannot be declared as atomic counter
6806 _mesa_glsl_error(&loc
, state
, "atomic counter in structure");
6809 if (decl_type
->contains_image()) {
6810 /* FINISHME: Same problem as with atomic counters.
6811 * FINISHME: Request clarification from Khronos and add
6812 * FINISHME: spec quotation here.
6814 _mesa_glsl_error(&loc
, state
, "image in structure");
6818 if (qual
->flags
.q
.explicit_binding
) {
6819 _mesa_glsl_error(&loc
, state
,
6820 "binding layout qualifier cannot be applied "
6821 "to struct or interface block members");
6825 if (!first_member
) {
6826 if (!layout
->flags
.q
.explicit_location
&&
6827 ((first_member_has_explicit_location
&&
6828 !qual
->flags
.q
.explicit_location
) ||
6829 (!first_member_has_explicit_location
&&
6830 qual
->flags
.q
.explicit_location
))) {
6831 _mesa_glsl_error(&loc
, state
,
6832 "when block-level location layout qualifier "
6833 "is not supplied either all members must "
6834 "have a location layout qualifier or all "
6835 "members must not have a location layout "
6839 first_member
= false;
6840 first_member_has_explicit_location
=
6841 qual
->flags
.q
.explicit_location
;
6845 if (qual
->flags
.q
.std140
||
6846 qual
->flags
.q
.std430
||
6847 qual
->flags
.q
.packed
||
6848 qual
->flags
.q
.shared
) {
6849 _mesa_glsl_error(&loc
, state
,
6850 "uniform/shader storage block layout qualifiers "
6851 "std140, std430, packed, and shared can only be "
6852 "applied to uniform/shader storage blocks, not "
6856 if (qual
->flags
.q
.constant
) {
6857 _mesa_glsl_error(&loc
, state
,
6858 "const storage qualifier cannot be applied "
6859 "to struct or interface block members");
6862 /* From Section 4.4.2.3 (Geometry Outputs) of the GLSL 4.50 spec:
6864 * "A block member may be declared with a stream identifier, but
6865 * the specified stream must match the stream associated with the
6866 * containing block."
6868 if (qual
->flags
.q
.explicit_stream
) {
6869 unsigned qual_stream
;
6870 if (process_qualifier_constant(state
, &loc
, "stream",
6871 qual
->stream
, &qual_stream
) &&
6872 qual_stream
!= block_stream
) {
6873 _mesa_glsl_error(&loc
, state
, "stream layout qualifier on "
6874 "interface block member does not match "
6875 "the interface block (%u vs %u)", qual_stream
,
6881 unsigned explicit_xfb_buffer
= 0;
6882 if (qual
->flags
.q
.explicit_xfb_buffer
) {
6883 unsigned qual_xfb_buffer
;
6884 if (process_qualifier_constant(state
, &loc
, "xfb_buffer",
6885 qual
->xfb_buffer
, &qual_xfb_buffer
)) {
6886 explicit_xfb_buffer
= 1;
6887 if (qual_xfb_buffer
!= block_xfb_buffer
)
6888 _mesa_glsl_error(&loc
, state
, "xfb_buffer layout qualifier on "
6889 "interface block member does not match "
6890 "the interface block (%u vs %u)",
6891 qual_xfb_buffer
, block_xfb_buffer
);
6893 xfb_buffer
= (int) qual_xfb_buffer
;
6896 explicit_xfb_buffer
= layout
->flags
.q
.explicit_xfb_buffer
;
6897 xfb_buffer
= (int) block_xfb_buffer
;
6900 int xfb_stride
= -1;
6901 if (qual
->flags
.q
.explicit_xfb_stride
) {
6902 unsigned qual_xfb_stride
;
6903 if (process_qualifier_constant(state
, &loc
, "xfb_stride",
6904 qual
->xfb_stride
, &qual_xfb_stride
)) {
6905 xfb_stride
= (int) qual_xfb_stride
;
6909 if (qual
->flags
.q
.uniform
&& qual
->has_interpolation()) {
6910 _mesa_glsl_error(&loc
, state
,
6911 "interpolation qualifiers cannot be used "
6912 "with uniform interface blocks");
6915 if ((qual
->flags
.q
.uniform
|| !is_interface
) &&
6916 qual
->has_auxiliary_storage()) {
6917 _mesa_glsl_error(&loc
, state
,
6918 "auxiliary storage qualifiers cannot be used "
6919 "in uniform blocks or structures.");
6922 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
6923 if (!qual
->flags
.q
.uniform
&& !qual
->flags
.q
.buffer
) {
6924 _mesa_glsl_error(&loc
, state
,
6925 "row_major and column_major can only be "
6926 "applied to interface blocks");
6928 validate_matrix_layout_for_type(state
, &loc
, decl_type
, NULL
);
6931 if (qual
->flags
.q
.read_only
&& qual
->flags
.q
.write_only
) {
6932 _mesa_glsl_error(&loc
, state
, "buffer variable can't be both "
6933 "readonly and writeonly.");
6936 foreach_list_typed (ast_declaration
, decl
, link
,
6937 &decl_list
->declarations
) {
6938 YYLTYPE loc
= decl
->get_location();
6940 if (!allow_reserved_names
)
6941 validate_identifier(decl
->identifier
, loc
, state
);
6943 const struct glsl_type
*field_type
=
6944 process_array_type(&loc
, decl_type
, decl
->array_specifier
, state
);
6945 validate_array_dimensions(field_type
, state
, &loc
);
6946 fields
[i
].type
= field_type
;
6947 fields
[i
].name
= decl
->identifier
;
6948 fields
[i
].interpolation
=
6949 interpret_interpolation_qualifier(qual
, field_type
,
6950 var_mode
, state
, &loc
);
6951 fields
[i
].centroid
= qual
->flags
.q
.centroid
? 1 : 0;
6952 fields
[i
].sample
= qual
->flags
.q
.sample
? 1 : 0;
6953 fields
[i
].patch
= qual
->flags
.q
.patch
? 1 : 0;
6954 fields
[i
].precision
= qual
->precision
;
6955 fields
[i
].offset
= -1;
6956 fields
[i
].explicit_xfb_buffer
= explicit_xfb_buffer
;
6957 fields
[i
].xfb_buffer
= xfb_buffer
;
6958 fields
[i
].xfb_stride
= xfb_stride
;
6960 if (qual
->flags
.q
.explicit_location
) {
6961 unsigned qual_location
;
6962 if (process_qualifier_constant(state
, &loc
, "location",
6963 qual
->location
, &qual_location
)) {
6964 fields
[i
].location
= qual_location
+
6965 (fields
[i
].patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
);
6966 expl_location
= fields
[i
].location
+
6967 fields
[i
].type
->count_attribute_slots(false);
6970 if (layout
&& layout
->flags
.q
.explicit_location
) {
6971 fields
[i
].location
= expl_location
;
6972 expl_location
+= fields
[i
].type
->count_attribute_slots(false);
6974 fields
[i
].location
= -1;
6978 /* Offset can only be used with std430 and std140 layouts an initial
6979 * value of 0 is used for error detection.
6985 if (qual
->flags
.q
.row_major
||
6986 matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
6992 if(layout
->flags
.q
.std140
) {
6993 align
= field_type
->std140_base_alignment(row_major
);
6994 size
= field_type
->std140_size(row_major
);
6995 } else if (layout
->flags
.q
.std430
) {
6996 align
= field_type
->std430_base_alignment(row_major
);
6997 size
= field_type
->std430_size(row_major
);
7001 if (qual
->flags
.q
.explicit_offset
) {
7002 unsigned qual_offset
;
7003 if (process_qualifier_constant(state
, &loc
, "offset",
7004 qual
->offset
, &qual_offset
)) {
7005 if (align
!= 0 && size
!= 0) {
7006 if (next_offset
> qual_offset
)
7007 _mesa_glsl_error(&loc
, state
, "layout qualifier "
7008 "offset overlaps previous member");
7010 if (qual_offset
% align
) {
7011 _mesa_glsl_error(&loc
, state
, "layout qualifier offset "
7012 "must be a multiple of the base "
7013 "alignment of %s", field_type
->name
);
7015 fields
[i
].offset
= qual_offset
;
7016 next_offset
= glsl_align(qual_offset
+ size
, align
);
7018 _mesa_glsl_error(&loc
, state
, "offset can only be used "
7019 "with std430 and std140 layouts");
7024 if (qual
->flags
.q
.explicit_align
|| expl_align
!= 0) {
7025 unsigned offset
= fields
[i
].offset
!= -1 ? fields
[i
].offset
:
7027 if (align
== 0 || size
== 0) {
7028 _mesa_glsl_error(&loc
, state
, "align can only be used with "
7029 "std430 and std140 layouts");
7030 } else if (qual
->flags
.q
.explicit_align
) {
7031 unsigned member_align
;
7032 if (process_qualifier_constant(state
, &loc
, "align",
7033 qual
->align
, &member_align
)) {
7034 if (member_align
== 0 ||
7035 member_align
& (member_align
- 1)) {
7036 _mesa_glsl_error(&loc
, state
, "align layout qualifier "
7037 "in not a power of 2");
7039 fields
[i
].offset
= glsl_align(offset
, member_align
);
7040 next_offset
= glsl_align(fields
[i
].offset
+ size
, align
);
7044 fields
[i
].offset
= glsl_align(offset
, expl_align
);
7045 next_offset
= glsl_align(fields
[i
].offset
+ size
, align
);
7047 } else if (!qual
->flags
.q
.explicit_offset
) {
7048 if (align
!= 0 && size
!= 0)
7049 next_offset
= glsl_align(next_offset
+ size
, align
);
7052 /* From the ARB_enhanced_layouts spec:
7054 * "The given offset applies to the first component of the first
7055 * member of the qualified entity. Then, within the qualified
7056 * entity, subsequent components are each assigned, in order, to
7057 * the next available offset aligned to a multiple of that
7058 * component's size. Aggregate types are flattened down to the
7059 * component level to get this sequence of components."
7061 if (qual
->flags
.q
.explicit_xfb_offset
) {
7062 unsigned xfb_offset
;
7063 if (process_qualifier_constant(state
, &loc
, "xfb_offset",
7064 qual
->offset
, &xfb_offset
)) {
7065 fields
[i
].offset
= xfb_offset
;
7066 block_xfb_offset
= fields
[i
].offset
+
7067 MAX2(xfb_stride
, (int) (4 * field_type
->component_slots()));
7070 if (layout
&& layout
->flags
.q
.explicit_xfb_offset
) {
7071 unsigned align
= field_type
->is_64bit() ? 8 : 4;
7072 fields
[i
].offset
= glsl_align(block_xfb_offset
, align
);
7074 MAX2(xfb_stride
, (int) (4 * field_type
->component_slots()));
7078 /* Propogate row- / column-major information down the fields of the
7079 * structure or interface block. Structures need this data because
7080 * the structure may contain a structure that contains ... a matrix
7081 * that need the proper layout.
7083 if (is_interface
&& layout
&&
7084 (layout
->flags
.q
.uniform
|| layout
->flags
.q
.buffer
) &&
7085 (field_type
->without_array()->is_matrix()
7086 || field_type
->without_array()->is_record())) {
7087 /* If no layout is specified for the field, inherit the layout
7090 fields
[i
].matrix_layout
= matrix_layout
;
7092 if (qual
->flags
.q
.row_major
)
7093 fields
[i
].matrix_layout
= GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
7094 else if (qual
->flags
.q
.column_major
)
7095 fields
[i
].matrix_layout
= GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
;
7097 /* If we're processing an uniform or buffer block, the matrix
7098 * layout must be decided by this point.
7100 assert(fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
7101 || fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
);
7104 /* Image qualifiers are allowed on buffer variables, which can only
7105 * be defined inside shader storage buffer objects
7107 if (layout
&& var_mode
== ir_var_shader_storage
) {
7108 /* For readonly and writeonly qualifiers the field definition,
7109 * if set, overwrites the layout qualifier.
7111 if (qual
->flags
.q
.read_only
) {
7112 fields
[i
].image_read_only
= true;
7113 fields
[i
].image_write_only
= false;
7114 } else if (qual
->flags
.q
.write_only
) {
7115 fields
[i
].image_read_only
= false;
7116 fields
[i
].image_write_only
= true;
7118 fields
[i
].image_read_only
= layout
->flags
.q
.read_only
;
7119 fields
[i
].image_write_only
= layout
->flags
.q
.write_only
;
7122 /* For other qualifiers, we set the flag if either the layout
7123 * qualifier or the field qualifier are set
7125 fields
[i
].image_coherent
= qual
->flags
.q
.coherent
||
7126 layout
->flags
.q
.coherent
;
7127 fields
[i
].image_volatile
= qual
->flags
.q
._volatile
||
7128 layout
->flags
.q
._volatile
;
7129 fields
[i
].image_restrict
= qual
->flags
.q
.restrict_flag
||
7130 layout
->flags
.q
.restrict_flag
;
7137 assert(i
== decl_count
);
7139 *fields_ret
= fields
;
7145 ast_struct_specifier::hir(exec_list
*instructions
,
7146 struct _mesa_glsl_parse_state
*state
)
7148 YYLTYPE loc
= this->get_location();
7150 unsigned expl_location
= 0;
7151 if (layout
&& layout
->flags
.q
.explicit_location
) {
7152 if (!process_qualifier_constant(state
, &loc
, "location",
7153 layout
->location
, &expl_location
)) {
7156 expl_location
= VARYING_SLOT_VAR0
+ expl_location
;
7160 glsl_struct_field
*fields
;
7161 unsigned decl_count
=
7162 ast_process_struct_or_iface_block_members(instructions
,
7164 &this->declarations
,
7167 GLSL_MATRIX_LAYOUT_INHERITED
,
7168 false /* allow_reserved_names */,
7171 0, /* for interface only */
7172 0, /* for interface only */
7173 0, /* for interface only */
7175 0 /* for interface only */);
7177 validate_identifier(this->name
, loc
, state
);
7179 const glsl_type
*t
=
7180 glsl_type::get_record_instance(fields
, decl_count
, this->name
);
7182 if (!state
->symbols
->add_type(name
, t
)) {
7183 const glsl_type
*match
= state
->symbols
->get_type(name
);
7184 /* allow struct matching for desktop GL - older UE4 does this */
7185 if (match
!= NULL
&& state
->is_version(130, 0) && match
->record_compare(t
, false))
7186 _mesa_glsl_warning(& loc
, state
, "struct `%s' previously defined", name
);
7188 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
7190 const glsl_type
**s
= reralloc(state
, state
->user_structures
,
7192 state
->num_user_structures
+ 1);
7194 s
[state
->num_user_structures
] = t
;
7195 state
->user_structures
= s
;
7196 state
->num_user_structures
++;
7200 /* Structure type definitions do not have r-values.
7207 * Visitor class which detects whether a given interface block has been used.
7209 class interface_block_usage_visitor
: public ir_hierarchical_visitor
7212 interface_block_usage_visitor(ir_variable_mode mode
, const glsl_type
*block
)
7213 : mode(mode
), block(block
), found(false)
7217 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
7219 if (ir
->var
->data
.mode
== mode
&& ir
->var
->get_interface_type() == block
) {
7223 return visit_continue
;
7226 bool usage_found() const
7232 ir_variable_mode mode
;
7233 const glsl_type
*block
;
7238 is_unsized_array_last_element(ir_variable
*v
)
7240 const glsl_type
*interface_type
= v
->get_interface_type();
7241 int length
= interface_type
->length
;
7243 assert(v
->type
->is_unsized_array());
7245 /* Check if it is the last element of the interface */
7246 if (strcmp(interface_type
->fields
.structure
[length
-1].name
, v
->name
) == 0)
7252 apply_memory_qualifiers(ir_variable
*var
, glsl_struct_field field
)
7254 var
->data
.image_read_only
= field
.image_read_only
;
7255 var
->data
.image_write_only
= field
.image_write_only
;
7256 var
->data
.image_coherent
= field
.image_coherent
;
7257 var
->data
.image_volatile
= field
.image_volatile
;
7258 var
->data
.image_restrict
= field
.image_restrict
;
7262 ast_interface_block::hir(exec_list
*instructions
,
7263 struct _mesa_glsl_parse_state
*state
)
7265 YYLTYPE loc
= this->get_location();
7267 /* Interface blocks must be declared at global scope */
7268 if (state
->current_function
!= NULL
) {
7269 _mesa_glsl_error(&loc
, state
,
7270 "Interface block `%s' must be declared "
7275 /* Validate qualifiers:
7277 * - Layout Qualifiers as per the table in Section 4.4
7278 * ("Layout Qualifiers") of the GLSL 4.50 spec.
7280 * - Memory Qualifiers as per Section 4.10 ("Memory Qualifiers") of the
7283 * "Additionally, memory qualifiers may also be used in the declaration
7284 * of shader storage blocks"
7286 * Note the table in Section 4.4 says std430 is allowed on both uniform and
7287 * buffer blocks however Section 4.4.5 (Uniform and Shader Storage Block
7288 * Layout Qualifiers) of the GLSL 4.50 spec says:
7290 * "The std430 qualifier is supported only for shader storage blocks;
7291 * using std430 on a uniform block will result in a compile-time error."
7293 ast_type_qualifier allowed_blk_qualifiers
;
7294 allowed_blk_qualifiers
.flags
.i
= 0;
7295 if (this->layout
.flags
.q
.buffer
|| this->layout
.flags
.q
.uniform
) {
7296 allowed_blk_qualifiers
.flags
.q
.shared
= 1;
7297 allowed_blk_qualifiers
.flags
.q
.packed
= 1;
7298 allowed_blk_qualifiers
.flags
.q
.std140
= 1;
7299 allowed_blk_qualifiers
.flags
.q
.row_major
= 1;
7300 allowed_blk_qualifiers
.flags
.q
.column_major
= 1;
7301 allowed_blk_qualifiers
.flags
.q
.explicit_align
= 1;
7302 allowed_blk_qualifiers
.flags
.q
.explicit_binding
= 1;
7303 if (this->layout
.flags
.q
.buffer
) {
7304 allowed_blk_qualifiers
.flags
.q
.buffer
= 1;
7305 allowed_blk_qualifiers
.flags
.q
.std430
= 1;
7306 allowed_blk_qualifiers
.flags
.q
.coherent
= 1;
7307 allowed_blk_qualifiers
.flags
.q
._volatile
= 1;
7308 allowed_blk_qualifiers
.flags
.q
.restrict_flag
= 1;
7309 allowed_blk_qualifiers
.flags
.q
.read_only
= 1;
7310 allowed_blk_qualifiers
.flags
.q
.write_only
= 1;
7312 allowed_blk_qualifiers
.flags
.q
.uniform
= 1;
7315 /* Interface block */
7316 assert(this->layout
.flags
.q
.in
|| this->layout
.flags
.q
.out
);
7318 allowed_blk_qualifiers
.flags
.q
.explicit_location
= 1;
7319 if (this->layout
.flags
.q
.out
) {
7320 allowed_blk_qualifiers
.flags
.q
.out
= 1;
7321 if (state
->stage
== MESA_SHADER_GEOMETRY
||
7322 state
->stage
== MESA_SHADER_TESS_CTRL
||
7323 state
->stage
== MESA_SHADER_TESS_EVAL
||
7324 state
->stage
== MESA_SHADER_VERTEX
) {
7325 allowed_blk_qualifiers
.flags
.q
.explicit_xfb_offset
= 1;
7326 allowed_blk_qualifiers
.flags
.q
.explicit_xfb_buffer
= 1;
7327 allowed_blk_qualifiers
.flags
.q
.xfb_buffer
= 1;
7328 allowed_blk_qualifiers
.flags
.q
.explicit_xfb_stride
= 1;
7329 allowed_blk_qualifiers
.flags
.q
.xfb_stride
= 1;
7330 if (state
->stage
== MESA_SHADER_GEOMETRY
) {
7331 allowed_blk_qualifiers
.flags
.q
.stream
= 1;
7332 allowed_blk_qualifiers
.flags
.q
.explicit_stream
= 1;
7334 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
7335 allowed_blk_qualifiers
.flags
.q
.patch
= 1;
7339 allowed_blk_qualifiers
.flags
.q
.in
= 1;
7340 if (state
->stage
== MESA_SHADER_TESS_EVAL
) {
7341 allowed_blk_qualifiers
.flags
.q
.patch
= 1;
7346 this->layout
.validate_flags(&loc
, state
, allowed_blk_qualifiers
,
7347 "invalid qualifier for block",
7350 /* The ast_interface_block has a list of ast_declarator_lists. We
7351 * need to turn those into ir_variables with an association
7352 * with this uniform block.
7354 enum glsl_interface_packing packing
;
7355 if (this->layout
.flags
.q
.shared
) {
7356 packing
= GLSL_INTERFACE_PACKING_SHARED
;
7357 } else if (this->layout
.flags
.q
.packed
) {
7358 packing
= GLSL_INTERFACE_PACKING_PACKED
;
7359 } else if (this->layout
.flags
.q
.std430
) {
7360 packing
= GLSL_INTERFACE_PACKING_STD430
;
7362 /* The default layout is std140.
7364 packing
= GLSL_INTERFACE_PACKING_STD140
;
7367 ir_variable_mode var_mode
;
7368 const char *iface_type_name
;
7369 if (this->layout
.flags
.q
.in
) {
7370 var_mode
= ir_var_shader_in
;
7371 iface_type_name
= "in";
7372 } else if (this->layout
.flags
.q
.out
) {
7373 var_mode
= ir_var_shader_out
;
7374 iface_type_name
= "out";
7375 } else if (this->layout
.flags
.q
.uniform
) {
7376 var_mode
= ir_var_uniform
;
7377 iface_type_name
= "uniform";
7378 } else if (this->layout
.flags
.q
.buffer
) {
7379 var_mode
= ir_var_shader_storage
;
7380 iface_type_name
= "buffer";
7382 var_mode
= ir_var_auto
;
7383 iface_type_name
= "UNKNOWN";
7384 assert(!"interface block layout qualifier not found!");
7387 enum glsl_matrix_layout matrix_layout
= GLSL_MATRIX_LAYOUT_INHERITED
;
7388 if (this->layout
.flags
.q
.row_major
)
7389 matrix_layout
= GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
7390 else if (this->layout
.flags
.q
.column_major
)
7391 matrix_layout
= GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
;
7393 bool redeclaring_per_vertex
= strcmp(this->block_name
, "gl_PerVertex") == 0;
7394 exec_list declared_variables
;
7395 glsl_struct_field
*fields
;
7397 /* For blocks that accept memory qualifiers (i.e. shader storage), verify
7398 * that we don't have incompatible qualifiers
7400 if (this->layout
.flags
.q
.read_only
&& this->layout
.flags
.q
.write_only
) {
7401 _mesa_glsl_error(&loc
, state
,
7402 "Interface block sets both readonly and writeonly");
7405 unsigned qual_stream
;
7406 if (!process_qualifier_constant(state
, &loc
, "stream", this->layout
.stream
,
7408 !validate_stream_qualifier(&loc
, state
, qual_stream
)) {
7409 /* If the stream qualifier is invalid it doesn't make sense to continue
7410 * on and try to compare stream layouts on member variables against it
7411 * so just return early.
7416 unsigned qual_xfb_buffer
;
7417 if (!process_qualifier_constant(state
, &loc
, "xfb_buffer",
7418 layout
.xfb_buffer
, &qual_xfb_buffer
) ||
7419 !validate_xfb_buffer_qualifier(&loc
, state
, qual_xfb_buffer
)) {
7423 unsigned qual_xfb_offset
;
7424 if (layout
.flags
.q
.explicit_xfb_offset
) {
7425 if (!process_qualifier_constant(state
, &loc
, "xfb_offset",
7426 layout
.offset
, &qual_xfb_offset
)) {
7431 unsigned qual_xfb_stride
;
7432 if (layout
.flags
.q
.explicit_xfb_stride
) {
7433 if (!process_qualifier_constant(state
, &loc
, "xfb_stride",
7434 layout
.xfb_stride
, &qual_xfb_stride
)) {
7439 unsigned expl_location
= 0;
7440 if (layout
.flags
.q
.explicit_location
) {
7441 if (!process_qualifier_constant(state
, &loc
, "location",
7442 layout
.location
, &expl_location
)) {
7445 expl_location
+= this->layout
.flags
.q
.patch
? VARYING_SLOT_PATCH0
7446 : VARYING_SLOT_VAR0
;
7450 unsigned expl_align
= 0;
7451 if (layout
.flags
.q
.explicit_align
) {
7452 if (!process_qualifier_constant(state
, &loc
, "align",
7453 layout
.align
, &expl_align
)) {
7456 if (expl_align
== 0 || expl_align
& (expl_align
- 1)) {
7457 _mesa_glsl_error(&loc
, state
, "align layout qualifier in not a "
7464 unsigned int num_variables
=
7465 ast_process_struct_or_iface_block_members(&declared_variables
,
7467 &this->declarations
,
7471 redeclaring_per_vertex
,
7480 if (!redeclaring_per_vertex
) {
7481 validate_identifier(this->block_name
, loc
, state
);
7483 /* From section 4.3.9 ("Interface Blocks") of the GLSL 4.50 spec:
7485 * "Block names have no other use within a shader beyond interface
7486 * matching; it is a compile-time error to use a block name at global
7487 * scope for anything other than as a block name."
7489 ir_variable
*var
= state
->symbols
->get_variable(this->block_name
);
7490 if (var
&& !var
->type
->is_interface()) {
7491 _mesa_glsl_error(&loc
, state
, "Block name `%s' is "
7492 "already used in the scope.",
7497 const glsl_type
*earlier_per_vertex
= NULL
;
7498 if (redeclaring_per_vertex
) {
7499 /* Find the previous declaration of gl_PerVertex. If we're redeclaring
7500 * the named interface block gl_in, we can find it by looking at the
7501 * previous declaration of gl_in. Otherwise we can find it by looking
7502 * at the previous decalartion of any of the built-in outputs,
7505 * Also check that the instance name and array-ness of the redeclaration
7509 case ir_var_shader_in
:
7510 if (ir_variable
*earlier_gl_in
=
7511 state
->symbols
->get_variable("gl_in")) {
7512 earlier_per_vertex
= earlier_gl_in
->get_interface_type();
7514 _mesa_glsl_error(&loc
, state
,
7515 "redeclaration of gl_PerVertex input not allowed "
7517 _mesa_shader_stage_to_string(state
->stage
));
7519 if (this->instance_name
== NULL
||
7520 strcmp(this->instance_name
, "gl_in") != 0 || this->array_specifier
== NULL
||
7521 !this->array_specifier
->is_single_dimension()) {
7522 _mesa_glsl_error(&loc
, state
,
7523 "gl_PerVertex input must be redeclared as "
7527 case ir_var_shader_out
:
7528 if (ir_variable
*earlier_gl_Position
=
7529 state
->symbols
->get_variable("gl_Position")) {
7530 earlier_per_vertex
= earlier_gl_Position
->get_interface_type();
7531 } else if (ir_variable
*earlier_gl_out
=
7532 state
->symbols
->get_variable("gl_out")) {
7533 earlier_per_vertex
= earlier_gl_out
->get_interface_type();
7535 _mesa_glsl_error(&loc
, state
,
7536 "redeclaration of gl_PerVertex output not "
7537 "allowed in the %s shader",
7538 _mesa_shader_stage_to_string(state
->stage
));
7540 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
7541 if (this->instance_name
== NULL
||
7542 strcmp(this->instance_name
, "gl_out") != 0 || this->array_specifier
== NULL
) {
7543 _mesa_glsl_error(&loc
, state
,
7544 "gl_PerVertex output must be redeclared as "
7548 if (this->instance_name
!= NULL
) {
7549 _mesa_glsl_error(&loc
, state
,
7550 "gl_PerVertex output may not be redeclared with "
7551 "an instance name");
7556 _mesa_glsl_error(&loc
, state
,
7557 "gl_PerVertex must be declared as an input or an "
7562 if (earlier_per_vertex
== NULL
) {
7563 /* An error has already been reported. Bail out to avoid null
7564 * dereferences later in this function.
7569 /* Copy locations from the old gl_PerVertex interface block. */
7570 for (unsigned i
= 0; i
< num_variables
; i
++) {
7571 int j
= earlier_per_vertex
->field_index(fields
[i
].name
);
7573 _mesa_glsl_error(&loc
, state
,
7574 "redeclaration of gl_PerVertex must be a subset "
7575 "of the built-in members of gl_PerVertex");
7577 fields
[i
].location
=
7578 earlier_per_vertex
->fields
.structure
[j
].location
;
7580 earlier_per_vertex
->fields
.structure
[j
].offset
;
7581 fields
[i
].interpolation
=
7582 earlier_per_vertex
->fields
.structure
[j
].interpolation
;
7583 fields
[i
].centroid
=
7584 earlier_per_vertex
->fields
.structure
[j
].centroid
;
7586 earlier_per_vertex
->fields
.structure
[j
].sample
;
7588 earlier_per_vertex
->fields
.structure
[j
].patch
;
7589 fields
[i
].precision
=
7590 earlier_per_vertex
->fields
.structure
[j
].precision
;
7591 fields
[i
].explicit_xfb_buffer
=
7592 earlier_per_vertex
->fields
.structure
[j
].explicit_xfb_buffer
;
7593 fields
[i
].xfb_buffer
=
7594 earlier_per_vertex
->fields
.structure
[j
].xfb_buffer
;
7595 fields
[i
].xfb_stride
=
7596 earlier_per_vertex
->fields
.structure
[j
].xfb_stride
;
7600 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10
7603 * If a built-in interface block is redeclared, it must appear in
7604 * the shader before any use of any member included in the built-in
7605 * declaration, or a compilation error will result.
7607 * This appears to be a clarification to the behaviour established for
7608 * gl_PerVertex by GLSL 1.50, therefore we implement this behaviour
7609 * regardless of GLSL version.
7611 interface_block_usage_visitor
v(var_mode
, earlier_per_vertex
);
7612 v
.run(instructions
);
7613 if (v
.usage_found()) {
7614 _mesa_glsl_error(&loc
, state
,
7615 "redeclaration of a built-in interface block must "
7616 "appear before any use of any member of the "
7621 const glsl_type
*block_type
=
7622 glsl_type::get_interface_instance(fields
,
7626 GLSL_MATRIX_LAYOUT_ROW_MAJOR
,
7629 unsigned component_size
= block_type
->contains_double() ? 8 : 4;
7631 layout
.flags
.q
.explicit_xfb_offset
? (int) qual_xfb_offset
: -1;
7632 validate_xfb_offset_qualifier(&loc
, state
, xfb_offset
, block_type
,
7635 if (!state
->symbols
->add_interface(block_type
->name
, block_type
, var_mode
)) {
7636 YYLTYPE loc
= this->get_location();
7637 _mesa_glsl_error(&loc
, state
, "interface block `%s' with type `%s' "
7638 "already taken in the current scope",
7639 this->block_name
, iface_type_name
);
7642 /* Since interface blocks cannot contain statements, it should be
7643 * impossible for the block to generate any instructions.
7645 assert(declared_variables
.is_empty());
7647 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
7649 * Geometry shader input variables get the per-vertex values written
7650 * out by vertex shader output variables of the same names. Since a
7651 * geometry shader operates on a set of vertices, each input varying
7652 * variable (or input block, see interface blocks below) needs to be
7653 * declared as an array.
7655 if (state
->stage
== MESA_SHADER_GEOMETRY
&& this->array_specifier
== NULL
&&
7656 var_mode
== ir_var_shader_in
) {
7657 _mesa_glsl_error(&loc
, state
, "geometry shader inputs must be arrays");
7658 } else if ((state
->stage
== MESA_SHADER_TESS_CTRL
||
7659 state
->stage
== MESA_SHADER_TESS_EVAL
) &&
7660 !this->layout
.flags
.q
.patch
&&
7661 this->array_specifier
== NULL
&&
7662 var_mode
== ir_var_shader_in
) {
7663 _mesa_glsl_error(&loc
, state
, "per-vertex tessellation shader inputs must be arrays");
7664 } else if (state
->stage
== MESA_SHADER_TESS_CTRL
&&
7665 !this->layout
.flags
.q
.patch
&&
7666 this->array_specifier
== NULL
&&
7667 var_mode
== ir_var_shader_out
) {
7668 _mesa_glsl_error(&loc
, state
, "tessellation control shader outputs must be arrays");
7672 /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
7675 * "If an instance name (instance-name) is used, then it puts all the
7676 * members inside a scope within its own name space, accessed with the
7677 * field selector ( . ) operator (analogously to structures)."
7679 if (this->instance_name
) {
7680 if (redeclaring_per_vertex
) {
7681 /* When a built-in in an unnamed interface block is redeclared,
7682 * get_variable_being_redeclared() calls
7683 * check_builtin_array_max_size() to make sure that built-in array
7684 * variables aren't redeclared to illegal sizes. But we're looking
7685 * at a redeclaration of a named built-in interface block. So we
7686 * have to manually call check_builtin_array_max_size() for all parts
7687 * of the interface that are arrays.
7689 for (unsigned i
= 0; i
< num_variables
; i
++) {
7690 if (fields
[i
].type
->is_array()) {
7691 const unsigned size
= fields
[i
].type
->array_size();
7692 check_builtin_array_max_size(fields
[i
].name
, size
, loc
, state
);
7696 validate_identifier(this->instance_name
, loc
, state
);
7701 if (this->array_specifier
!= NULL
) {
7702 const glsl_type
*block_array_type
=
7703 process_array_type(&loc
, block_type
, this->array_specifier
, state
);
7705 /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
7707 * For uniform blocks declared an array, each individual array
7708 * element corresponds to a separate buffer object backing one
7709 * instance of the block. As the array size indicates the number
7710 * of buffer objects needed, uniform block array declarations
7711 * must specify an array size.
7713 * And a few paragraphs later:
7715 * Geometry shader input blocks must be declared as arrays and
7716 * follow the array declaration and linking rules for all
7717 * geometry shader inputs. All other input and output block
7718 * arrays must specify an array size.
7720 * The same applies to tessellation shaders.
7722 * The upshot of this is that the only circumstance where an
7723 * interface array size *doesn't* need to be specified is on a
7724 * geometry shader input, tessellation control shader input,
7725 * tessellation control shader output, and tessellation evaluation
7728 if (block_array_type
->is_unsized_array()) {
7729 bool allow_inputs
= state
->stage
== MESA_SHADER_GEOMETRY
||
7730 state
->stage
== MESA_SHADER_TESS_CTRL
||
7731 state
->stage
== MESA_SHADER_TESS_EVAL
;
7732 bool allow_outputs
= state
->stage
== MESA_SHADER_TESS_CTRL
;
7734 if (this->layout
.flags
.q
.in
) {
7736 _mesa_glsl_error(&loc
, state
,
7737 "unsized input block arrays not allowed in "
7739 _mesa_shader_stage_to_string(state
->stage
));
7740 } else if (this->layout
.flags
.q
.out
) {
7742 _mesa_glsl_error(&loc
, state
,
7743 "unsized output block arrays not allowed in "
7745 _mesa_shader_stage_to_string(state
->stage
));
7747 /* by elimination, this is a uniform block array */
7748 _mesa_glsl_error(&loc
, state
,
7749 "unsized uniform block arrays not allowed in "
7751 _mesa_shader_stage_to_string(state
->stage
));
7755 /* From section 4.3.9 (Interface Blocks) of the GLSL ES 3.10 spec:
7757 * * Arrays of arrays of blocks are not allowed
7759 if (state
->es_shader
&& block_array_type
->is_array() &&
7760 block_array_type
->fields
.array
->is_array()) {
7761 _mesa_glsl_error(&loc
, state
,
7762 "arrays of arrays interface blocks are "
7766 var
= new(state
) ir_variable(block_array_type
,
7767 this->instance_name
,
7770 var
= new(state
) ir_variable(block_type
,
7771 this->instance_name
,
7775 var
->data
.matrix_layout
= matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
7776 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
: matrix_layout
;
7778 if (var_mode
== ir_var_shader_in
|| var_mode
== ir_var_uniform
)
7779 var
->data
.read_only
= true;
7781 var
->data
.patch
= this->layout
.flags
.q
.patch
;
7783 if (state
->stage
== MESA_SHADER_GEOMETRY
&& var_mode
== ir_var_shader_in
)
7784 handle_geometry_shader_input_decl(state
, loc
, var
);
7785 else if ((state
->stage
== MESA_SHADER_TESS_CTRL
||
7786 state
->stage
== MESA_SHADER_TESS_EVAL
) && var_mode
== ir_var_shader_in
)
7787 handle_tess_shader_input_decl(state
, loc
, var
);
7788 else if (state
->stage
== MESA_SHADER_TESS_CTRL
&& var_mode
== ir_var_shader_out
)
7789 handle_tess_ctrl_shader_output_decl(state
, loc
, var
);
7791 for (unsigned i
= 0; i
< num_variables
; i
++) {
7792 if (var
->data
.mode
== ir_var_shader_storage
)
7793 apply_memory_qualifiers(var
, fields
[i
]);
7796 if (ir_variable
*earlier
=
7797 state
->symbols
->get_variable(this->instance_name
)) {
7798 if (!redeclaring_per_vertex
) {
7799 _mesa_glsl_error(&loc
, state
, "`%s' redeclared",
7800 this->instance_name
);
7802 earlier
->data
.how_declared
= ir_var_declared_normally
;
7803 earlier
->type
= var
->type
;
7804 earlier
->reinit_interface_type(block_type
);
7807 if (this->layout
.flags
.q
.explicit_binding
) {
7808 apply_explicit_binding(state
, &loc
, var
, var
->type
,
7812 var
->data
.stream
= qual_stream
;
7813 if (layout
.flags
.q
.explicit_location
) {
7814 var
->data
.location
= expl_location
;
7815 var
->data
.explicit_location
= true;
7818 state
->symbols
->add_variable(var
);
7819 instructions
->push_tail(var
);
7822 /* In order to have an array size, the block must also be declared with
7825 assert(this->array_specifier
== NULL
);
7827 for (unsigned i
= 0; i
< num_variables
; i
++) {
7829 new(state
) ir_variable(fields
[i
].type
,
7830 ralloc_strdup(state
, fields
[i
].name
),
7832 var
->data
.interpolation
= fields
[i
].interpolation
;
7833 var
->data
.centroid
= fields
[i
].centroid
;
7834 var
->data
.sample
= fields
[i
].sample
;
7835 var
->data
.patch
= fields
[i
].patch
;
7836 var
->data
.stream
= qual_stream
;
7837 var
->data
.location
= fields
[i
].location
;
7839 if (fields
[i
].location
!= -1)
7840 var
->data
.explicit_location
= true;
7842 var
->data
.explicit_xfb_buffer
= fields
[i
].explicit_xfb_buffer
;
7843 var
->data
.xfb_buffer
= fields
[i
].xfb_buffer
;
7845 if (fields
[i
].offset
!= -1)
7846 var
->data
.explicit_xfb_offset
= true;
7847 var
->data
.offset
= fields
[i
].offset
;
7849 var
->init_interface_type(block_type
);
7851 if (var_mode
== ir_var_shader_in
|| var_mode
== ir_var_uniform
)
7852 var
->data
.read_only
= true;
7854 /* Precision qualifiers do not have any meaning in Desktop GLSL */
7855 if (state
->es_shader
) {
7856 var
->data
.precision
=
7857 select_gles_precision(fields
[i
].precision
, fields
[i
].type
,
7861 if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
) {
7862 var
->data
.matrix_layout
= matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
7863 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
: matrix_layout
;
7865 var
->data
.matrix_layout
= fields
[i
].matrix_layout
;
7868 if (var
->data
.mode
== ir_var_shader_storage
)
7869 apply_memory_qualifiers(var
, fields
[i
]);
7871 /* Examine var name here since var may get deleted in the next call */
7872 bool var_is_gl_id
= is_gl_identifier(var
->name
);
7874 if (redeclaring_per_vertex
) {
7875 ir_variable
*earlier
=
7876 get_variable_being_redeclared(var
, loc
, state
,
7877 true /* allow_all_redeclarations */);
7878 if (!var_is_gl_id
|| earlier
== NULL
) {
7879 _mesa_glsl_error(&loc
, state
,
7880 "redeclaration of gl_PerVertex can only "
7881 "include built-in variables");
7882 } else if (earlier
->data
.how_declared
== ir_var_declared_normally
) {
7883 _mesa_glsl_error(&loc
, state
,
7884 "`%s' has already been redeclared",
7887 earlier
->data
.how_declared
= ir_var_declared_in_block
;
7888 earlier
->reinit_interface_type(block_type
);
7893 if (state
->symbols
->get_variable(var
->name
) != NULL
)
7894 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
7896 /* Propagate the "binding" keyword into this UBO/SSBO's fields.
7897 * The UBO declaration itself doesn't get an ir_variable unless it
7898 * has an instance name. This is ugly.
7900 if (this->layout
.flags
.q
.explicit_binding
) {
7901 apply_explicit_binding(state
, &loc
, var
,
7902 var
->get_interface_type(), &this->layout
);
7905 if (var
->type
->is_unsized_array()) {
7906 if (var
->is_in_shader_storage_block()) {
7907 if (is_unsized_array_last_element(var
)) {
7908 var
->data
.from_ssbo_unsized_array
= true;
7911 /* From GLSL ES 3.10 spec, section 4.1.9 "Arrays":
7913 * "If an array is declared as the last member of a shader storage
7914 * block and the size is not specified at compile-time, it is
7915 * sized at run-time. In all other cases, arrays are sized only
7918 if (state
->es_shader
) {
7919 _mesa_glsl_error(&loc
, state
, "unsized array `%s' "
7920 "definition: only last member of a shader "
7921 "storage block can be defined as unsized "
7922 "array", fields
[i
].name
);
7927 state
->symbols
->add_variable(var
);
7928 instructions
->push_tail(var
);
7931 if (redeclaring_per_vertex
&& block_type
!= earlier_per_vertex
) {
7932 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec:
7934 * It is also a compilation error ... to redeclare a built-in
7935 * block and then use a member from that built-in block that was
7936 * not included in the redeclaration.
7938 * This appears to be a clarification to the behaviour established
7939 * for gl_PerVertex by GLSL 1.50, therefore we implement this
7940 * behaviour regardless of GLSL version.
7942 * To prevent the shader from using a member that was not included in
7943 * the redeclaration, we disable any ir_variables that are still
7944 * associated with the old declaration of gl_PerVertex (since we've
7945 * already updated all of the variables contained in the new
7946 * gl_PerVertex to point to it).
7948 * As a side effect this will prevent
7949 * validate_intrastage_interface_blocks() from getting confused and
7950 * thinking there are conflicting definitions of gl_PerVertex in the
7953 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
7954 ir_variable
*const var
= node
->as_variable();
7956 var
->get_interface_type() == earlier_per_vertex
&&
7957 var
->data
.mode
== var_mode
) {
7958 if (var
->data
.how_declared
== ir_var_declared_normally
) {
7959 _mesa_glsl_error(&loc
, state
,
7960 "redeclaration of gl_PerVertex cannot "
7961 "follow a redeclaration of `%s'",
7964 state
->symbols
->disable_variable(var
->name
);
7976 ast_tcs_output_layout::hir(exec_list
*instructions
,
7977 struct _mesa_glsl_parse_state
*state
)
7979 YYLTYPE loc
= this->get_location();
7981 unsigned num_vertices
;
7982 if (!state
->out_qualifier
->vertices
->
7983 process_qualifier_constant(state
, "vertices", &num_vertices
,
7985 /* return here to stop cascading incorrect error messages */
7989 /* If any shader outputs occurred before this declaration and specified an
7990 * array size, make sure the size they specified is consistent with the
7993 if (state
->tcs_output_size
!= 0 && state
->tcs_output_size
!= num_vertices
) {
7994 _mesa_glsl_error(&loc
, state
,
7995 "this tessellation control shader output layout "
7996 "specifies %u vertices, but a previous output "
7997 "is declared with size %u",
7998 num_vertices
, state
->tcs_output_size
);
8002 state
->tcs_output_vertices_specified
= true;
8004 /* If any shader outputs occurred before this declaration and did not
8005 * specify an array size, their size is determined now.
8007 foreach_in_list (ir_instruction
, node
, instructions
) {
8008 ir_variable
*var
= node
->as_variable();
8009 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_out
)
8012 /* Note: Not all tessellation control shader output are arrays. */
8013 if (!var
->type
->is_unsized_array() || var
->data
.patch
)
8016 if (var
->data
.max_array_access
>= (int)num_vertices
) {
8017 _mesa_glsl_error(&loc
, state
,
8018 "this tessellation control shader output layout "
8019 "specifies %u vertices, but an access to element "
8020 "%u of output `%s' already exists", num_vertices
,
8021 var
->data
.max_array_access
, var
->name
);
8023 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
8033 ast_gs_input_layout::hir(exec_list
*instructions
,
8034 struct _mesa_glsl_parse_state
*state
)
8036 YYLTYPE loc
= this->get_location();
8038 /* Should have been prevented by the parser. */
8039 assert(!state
->gs_input_prim_type_specified
8040 || state
->in_qualifier
->prim_type
== this->prim_type
);
8042 /* If any shader inputs occurred before this declaration and specified an
8043 * array size, make sure the size they specified is consistent with the
8046 unsigned num_vertices
= vertices_per_prim(this->prim_type
);
8047 if (state
->gs_input_size
!= 0 && state
->gs_input_size
!= num_vertices
) {
8048 _mesa_glsl_error(&loc
, state
,
8049 "this geometry shader input layout implies %u vertices"
8050 " per primitive, but a previous input is declared"
8051 " with size %u", num_vertices
, state
->gs_input_size
);
8055 state
->gs_input_prim_type_specified
= true;
8057 /* If any shader inputs occurred before this declaration and did not
8058 * specify an array size, their size is determined now.
8060 foreach_in_list(ir_instruction
, node
, instructions
) {
8061 ir_variable
*var
= node
->as_variable();
8062 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_in
)
8065 /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
8069 if (var
->type
->is_unsized_array()) {
8070 if (var
->data
.max_array_access
>= (int)num_vertices
) {
8071 _mesa_glsl_error(&loc
, state
,
8072 "this geometry shader input layout implies %u"
8073 " vertices, but an access to element %u of input"
8074 " `%s' already exists", num_vertices
,
8075 var
->data
.max_array_access
, var
->name
);
8077 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
8088 ast_cs_input_layout::hir(exec_list
*instructions
,
8089 struct _mesa_glsl_parse_state
*state
)
8091 YYLTYPE loc
= this->get_location();
8093 /* From the ARB_compute_shader specification:
8095 * If the local size of the shader in any dimension is greater
8096 * than the maximum size supported by the implementation for that
8097 * dimension, a compile-time error results.
8099 * It is not clear from the spec how the error should be reported if
8100 * the total size of the work group exceeds
8101 * MAX_COMPUTE_WORK_GROUP_INVOCATIONS, but it seems reasonable to
8102 * report it at compile time as well.
8104 GLuint64 total_invocations
= 1;
8105 unsigned qual_local_size
[3];
8106 for (int i
= 0; i
< 3; i
++) {
8108 char *local_size_str
= ralloc_asprintf(NULL
, "invalid local_size_%c",
8110 /* Infer a local_size of 1 for unspecified dimensions */
8111 if (this->local_size
[i
] == NULL
) {
8112 qual_local_size
[i
] = 1;
8113 } else if (!this->local_size
[i
]->
8114 process_qualifier_constant(state
, local_size_str
,
8115 &qual_local_size
[i
], false)) {
8116 ralloc_free(local_size_str
);
8119 ralloc_free(local_size_str
);
8121 if (qual_local_size
[i
] > state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]) {
8122 _mesa_glsl_error(&loc
, state
,
8123 "local_size_%c exceeds MAX_COMPUTE_WORK_GROUP_SIZE"
8125 state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]);
8128 total_invocations
*= qual_local_size
[i
];
8129 if (total_invocations
>
8130 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
) {
8131 _mesa_glsl_error(&loc
, state
,
8132 "product of local_sizes exceeds "
8133 "MAX_COMPUTE_WORK_GROUP_INVOCATIONS (%d)",
8134 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
);
8139 /* If any compute input layout declaration preceded this one, make sure it
8140 * was consistent with this one.
8142 if (state
->cs_input_local_size_specified
) {
8143 for (int i
= 0; i
< 3; i
++) {
8144 if (state
->cs_input_local_size
[i
] != qual_local_size
[i
]) {
8145 _mesa_glsl_error(&loc
, state
,
8146 "compute shader input layout does not match"
8147 " previous declaration");
8153 /* The ARB_compute_variable_group_size spec says:
8155 * If a compute shader including a *local_size_variable* qualifier also
8156 * declares a fixed local group size using the *local_size_x*,
8157 * *local_size_y*, or *local_size_z* qualifiers, a compile-time error
8160 if (state
->cs_input_local_size_variable_specified
) {
8161 _mesa_glsl_error(&loc
, state
,
8162 "compute shader can't include both a variable and a "
8163 "fixed local group size");
8167 state
->cs_input_local_size_specified
= true;
8168 for (int i
= 0; i
< 3; i
++)
8169 state
->cs_input_local_size
[i
] = qual_local_size
[i
];
8171 /* We may now declare the built-in constant gl_WorkGroupSize (see
8172 * builtin_variable_generator::generate_constants() for why we didn't
8173 * declare it earlier).
8175 ir_variable
*var
= new(state
->symbols
)
8176 ir_variable(glsl_type::uvec3_type
, "gl_WorkGroupSize", ir_var_auto
);
8177 var
->data
.how_declared
= ir_var_declared_implicitly
;
8178 var
->data
.read_only
= true;
8179 instructions
->push_tail(var
);
8180 state
->symbols
->add_variable(var
);
8181 ir_constant_data data
;
8182 memset(&data
, 0, sizeof(data
));
8183 for (int i
= 0; i
< 3; i
++)
8184 data
.u
[i
] = qual_local_size
[i
];
8185 var
->constant_value
= new(var
) ir_constant(glsl_type::uvec3_type
, &data
);
8186 var
->constant_initializer
=
8187 new(var
) ir_constant(glsl_type::uvec3_type
, &data
);
8188 var
->data
.has_initializer
= true;
8195 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
8196 exec_list
*instructions
)
8198 bool gl_FragColor_assigned
= false;
8199 bool gl_FragData_assigned
= false;
8200 bool gl_FragSecondaryColor_assigned
= false;
8201 bool gl_FragSecondaryData_assigned
= false;
8202 bool user_defined_fs_output_assigned
= false;
8203 ir_variable
*user_defined_fs_output
= NULL
;
8205 /* It would be nice to have proper location information. */
8207 memset(&loc
, 0, sizeof(loc
));
8209 foreach_in_list(ir_instruction
, node
, instructions
) {
8210 ir_variable
*var
= node
->as_variable();
8212 if (!var
|| !var
->data
.assigned
)
8215 if (strcmp(var
->name
, "gl_FragColor") == 0)
8216 gl_FragColor_assigned
= true;
8217 else if (strcmp(var
->name
, "gl_FragData") == 0)
8218 gl_FragData_assigned
= true;
8219 else if (strcmp(var
->name
, "gl_SecondaryFragColorEXT") == 0)
8220 gl_FragSecondaryColor_assigned
= true;
8221 else if (strcmp(var
->name
, "gl_SecondaryFragDataEXT") == 0)
8222 gl_FragSecondaryData_assigned
= true;
8223 else if (!is_gl_identifier(var
->name
)) {
8224 if (state
->stage
== MESA_SHADER_FRAGMENT
&&
8225 var
->data
.mode
== ir_var_shader_out
) {
8226 user_defined_fs_output_assigned
= true;
8227 user_defined_fs_output
= var
;
8232 /* From the GLSL 1.30 spec:
8234 * "If a shader statically assigns a value to gl_FragColor, it
8235 * may not assign a value to any element of gl_FragData. If a
8236 * shader statically writes a value to any element of
8237 * gl_FragData, it may not assign a value to
8238 * gl_FragColor. That is, a shader may assign values to either
8239 * gl_FragColor or gl_FragData, but not both. Multiple shaders
8240 * linked together must also consistently write just one of
8241 * these variables. Similarly, if user declared output
8242 * variables are in use (statically assigned to), then the
8243 * built-in variables gl_FragColor and gl_FragData may not be
8244 * assigned to. These incorrect usages all generate compile
8247 if (gl_FragColor_assigned
&& gl_FragData_assigned
) {
8248 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8249 "`gl_FragColor' and `gl_FragData'");
8250 } else if (gl_FragColor_assigned
&& user_defined_fs_output_assigned
) {
8251 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8252 "`gl_FragColor' and `%s'",
8253 user_defined_fs_output
->name
);
8254 } else if (gl_FragSecondaryColor_assigned
&& gl_FragSecondaryData_assigned
) {
8255 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8256 "`gl_FragSecondaryColorEXT' and"
8257 " `gl_FragSecondaryDataEXT'");
8258 } else if (gl_FragColor_assigned
&& gl_FragSecondaryData_assigned
) {
8259 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8260 "`gl_FragColor' and"
8261 " `gl_FragSecondaryDataEXT'");
8262 } else if (gl_FragData_assigned
&& gl_FragSecondaryColor_assigned
) {
8263 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8265 " `gl_FragSecondaryColorEXT'");
8266 } else if (gl_FragData_assigned
&& user_defined_fs_output_assigned
) {
8267 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
8268 "`gl_FragData' and `%s'",
8269 user_defined_fs_output
->name
);
8272 if ((gl_FragSecondaryColor_assigned
|| gl_FragSecondaryData_assigned
) &&
8273 !state
->EXT_blend_func_extended_enable
) {
8274 _mesa_glsl_error(&loc
, state
,
8275 "Dual source blending requires EXT_blend_func_extended");
8281 remove_per_vertex_blocks(exec_list
*instructions
,
8282 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
)
8284 /* Find the gl_PerVertex interface block of the appropriate (in/out) mode,
8285 * if it exists in this shader type.
8287 const glsl_type
*per_vertex
= NULL
;
8289 case ir_var_shader_in
:
8290 if (ir_variable
*gl_in
= state
->symbols
->get_variable("gl_in"))
8291 per_vertex
= gl_in
->get_interface_type();
8293 case ir_var_shader_out
:
8294 if (ir_variable
*gl_Position
=
8295 state
->symbols
->get_variable("gl_Position")) {
8296 per_vertex
= gl_Position
->get_interface_type();
8300 assert(!"Unexpected mode");
8304 /* If we didn't find a built-in gl_PerVertex interface block, then we don't
8305 * need to do anything.
8307 if (per_vertex
== NULL
)
8310 /* If the interface block is used by the shader, then we don't need to do
8313 interface_block_usage_visitor
v(mode
, per_vertex
);
8314 v
.run(instructions
);
8315 if (v
.usage_found())
8318 /* Remove any ir_variable declarations that refer to the interface block
8321 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
8322 ir_variable
*const var
= node
->as_variable();
8323 if (var
!= NULL
&& var
->get_interface_type() == per_vertex
&&
8324 var
->data
.mode
== mode
) {
8325 state
->symbols
->disable_variable(var
->name
);