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 "glsl_types.h"
56 #include "program/hash_table.h"
57 #include "main/shaderobj.h"
59 #include "ir_builder.h"
61 using namespace ir_builder
;
64 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
65 exec_list
*instructions
);
67 remove_per_vertex_blocks(exec_list
*instructions
,
68 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
);
72 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
74 _mesa_glsl_initialize_variables(instructions
, state
);
76 state
->symbols
->separate_function_namespace
= state
->language_version
== 110;
78 state
->current_function
= NULL
;
80 state
->toplevel_ir
= instructions
;
82 state
->gs_input_prim_type_specified
= false;
83 state
->tcs_output_vertices_specified
= false;
84 state
->cs_input_local_size_specified
= false;
86 /* Section 4.2 of the GLSL 1.20 specification states:
87 * "The built-in functions are scoped in a scope outside the global scope
88 * users declare global variables in. That is, a shader's global scope,
89 * available for user-defined functions and global variables, is nested
90 * inside the scope containing the built-in functions."
92 * Since built-in functions like ftransform() access built-in variables,
93 * it follows that those must be in the outer scope as well.
95 * We push scope here to create this nesting effect...but don't pop.
96 * This way, a shader's globals are still in the symbol table for use
99 state
->symbols
->push_scope();
101 foreach_list_typed (ast_node
, ast
, link
, & state
->translation_unit
)
102 ast
->hir(instructions
, state
);
104 detect_recursion_unlinked(state
, instructions
);
105 detect_conflicting_assignments(state
, instructions
);
107 state
->toplevel_ir
= NULL
;
109 /* Move all of the variable declarations to the front of the IR list, and
110 * reverse the order. This has the (intended!) side effect that vertex
111 * shader inputs and fragment shader outputs will appear in the IR in the
112 * same order that they appeared in the shader code. This results in the
113 * locations being assigned in the declared order. Many (arguably buggy)
114 * applications depend on this behavior, and it matches what nearly all
117 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
118 ir_variable
*const var
= node
->as_variable();
124 instructions
->push_head(var
);
127 /* Figure out if gl_FragCoord is actually used in fragment shader */
128 ir_variable
*const var
= state
->symbols
->get_variable("gl_FragCoord");
130 state
->fs_uses_gl_fragcoord
= var
->data
.used
;
132 /* From section 7.1 (Built-In Language Variables) of the GLSL 4.10 spec:
134 * If multiple shaders using members of a built-in block belonging to
135 * the same interface are linked together in the same program, they
136 * must all redeclare the built-in block in the same way, as described
137 * in section 4.3.7 "Interface Blocks" for interface block matching, or
138 * a link error will result.
140 * The phrase "using members of a built-in block" implies that if two
141 * shaders are linked together and one of them *does not use* any members
142 * of the built-in block, then that shader does not need to have a matching
143 * redeclaration of the built-in block.
145 * This appears to be a clarification to the behaviour established for
146 * gl_PerVertex by GLSL 1.50, therefore implement it regardless of GLSL
149 * The definition of "interface" in section 4.3.7 that applies here is as
152 * The boundary between adjacent programmable pipeline stages: This
153 * spans all the outputs in all compilation units of the first stage
154 * and all the inputs in all compilation units of the second stage.
156 * Therefore this rule applies to both inter- and intra-stage linking.
158 * The easiest way to implement this is to check whether the shader uses
159 * gl_PerVertex right after ast-to-ir conversion, and if it doesn't, simply
160 * remove all the relevant variable declaration from the IR, so that the
161 * linker won't see them and complain about mismatches.
163 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_in
);
164 remove_per_vertex_blocks(instructions
, state
, ir_var_shader_out
);
168 static ir_expression_operation
169 get_conversion_operation(const glsl_type
*to
, const glsl_type
*from
,
170 struct _mesa_glsl_parse_state
*state
)
172 switch (to
->base_type
) {
173 case GLSL_TYPE_FLOAT
:
174 switch (from
->base_type
) {
175 case GLSL_TYPE_INT
: return ir_unop_i2f
;
176 case GLSL_TYPE_UINT
: return ir_unop_u2f
;
177 case GLSL_TYPE_DOUBLE
: return ir_unop_d2f
;
178 default: return (ir_expression_operation
)0;
182 if (!state
->is_version(400, 0) && !state
->ARB_gpu_shader5_enable
)
183 return (ir_expression_operation
)0;
184 switch (from
->base_type
) {
185 case GLSL_TYPE_INT
: return ir_unop_i2u
;
186 default: return (ir_expression_operation
)0;
189 case GLSL_TYPE_DOUBLE
:
190 if (!state
->has_double())
191 return (ir_expression_operation
)0;
192 switch (from
->base_type
) {
193 case GLSL_TYPE_INT
: return ir_unop_i2d
;
194 case GLSL_TYPE_UINT
: return ir_unop_u2d
;
195 case GLSL_TYPE_FLOAT
: return ir_unop_f2d
;
196 default: return (ir_expression_operation
)0;
199 default: return (ir_expression_operation
)0;
205 * If a conversion is available, convert one operand to a different type
207 * The \c from \c ir_rvalue is converted "in place".
209 * \param to Type that the operand it to be converted to
210 * \param from Operand that is being converted
211 * \param state GLSL compiler state
214 * If a conversion is possible (or unnecessary), \c true is returned.
215 * Otherwise \c false is returned.
218 apply_implicit_conversion(const glsl_type
*to
, ir_rvalue
* &from
,
219 struct _mesa_glsl_parse_state
*state
)
222 if (to
->base_type
== from
->type
->base_type
)
225 /* Prior to GLSL 1.20, there are no implicit conversions */
226 if (!state
->is_version(120, 0))
229 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
231 * "There are no implicit array or structure conversions. For
232 * example, an array of int cannot be implicitly converted to an
235 if (!to
->is_numeric() || !from
->type
->is_numeric())
238 /* We don't actually want the specific type `to`, we want a type
239 * with the same base type as `to`, but the same vector width as
242 to
= glsl_type::get_instance(to
->base_type
, from
->type
->vector_elements
,
243 from
->type
->matrix_columns
);
245 ir_expression_operation op
= get_conversion_operation(to
, from
->type
, state
);
247 from
= new(ctx
) ir_expression(op
, to
, from
, NULL
);
255 static const struct glsl_type
*
256 arithmetic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
258 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
260 const glsl_type
*type_a
= value_a
->type
;
261 const glsl_type
*type_b
= value_b
->type
;
263 /* From GLSL 1.50 spec, page 56:
265 * "The arithmetic binary operators add (+), subtract (-),
266 * multiply (*), and divide (/) operate on integer and
267 * floating-point scalars, vectors, and matrices."
269 if (!type_a
->is_numeric() || !type_b
->is_numeric()) {
270 _mesa_glsl_error(loc
, state
,
271 "operands to arithmetic operators must be numeric");
272 return glsl_type::error_type
;
276 /* "If one operand is floating-point based and the other is
277 * not, then the conversions from Section 4.1.10 "Implicit
278 * Conversions" are applied to the non-floating-point-based operand."
280 if (!apply_implicit_conversion(type_a
, value_b
, state
)
281 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
282 _mesa_glsl_error(loc
, state
,
283 "could not implicitly convert operands to "
284 "arithmetic operator");
285 return glsl_type::error_type
;
287 type_a
= value_a
->type
;
288 type_b
= value_b
->type
;
290 /* "If the operands are integer types, they must both be signed or
293 * From this rule and the preceeding conversion it can be inferred that
294 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
295 * The is_numeric check above already filtered out the case where either
296 * type is not one of these, so now the base types need only be tested for
299 if (type_a
->base_type
!= type_b
->base_type
) {
300 _mesa_glsl_error(loc
, state
,
301 "base type mismatch for arithmetic operator");
302 return glsl_type::error_type
;
305 /* "All arithmetic binary operators result in the same fundamental type
306 * (signed integer, unsigned integer, or floating-point) as the
307 * operands they operate on, after operand type conversion. After
308 * conversion, the following cases are valid
310 * * The two operands are scalars. In this case the operation is
311 * applied, resulting in a scalar."
313 if (type_a
->is_scalar() && type_b
->is_scalar())
316 /* "* One operand is a scalar, and the other is a vector or matrix.
317 * In this case, the scalar operation is applied independently to each
318 * component of the vector or matrix, resulting in the same size
321 if (type_a
->is_scalar()) {
322 if (!type_b
->is_scalar())
324 } else if (type_b
->is_scalar()) {
328 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
329 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
332 assert(!type_a
->is_scalar());
333 assert(!type_b
->is_scalar());
335 /* "* The two operands are vectors of the same size. In this case, the
336 * operation is done component-wise resulting in the same size
339 if (type_a
->is_vector() && type_b
->is_vector()) {
340 if (type_a
== type_b
) {
343 _mesa_glsl_error(loc
, state
,
344 "vector size mismatch for arithmetic operator");
345 return glsl_type::error_type
;
349 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
350 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
351 * <vector, vector> have been handled. At least one of the operands must
352 * be matrix. Further, since there are no integer matrix types, the base
353 * type of both operands must be float.
355 assert(type_a
->is_matrix() || type_b
->is_matrix());
356 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
||
357 type_a
->base_type
== GLSL_TYPE_DOUBLE
);
358 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
||
359 type_b
->base_type
== GLSL_TYPE_DOUBLE
);
361 /* "* The operator is add (+), subtract (-), or divide (/), and the
362 * operands are matrices with the same number of rows and the same
363 * number of columns. In this case, the operation is done component-
364 * wise resulting in the same size matrix."
365 * * The operator is multiply (*), where both operands are matrices or
366 * one operand is a vector and the other a matrix. A right vector
367 * operand is treated as a column vector and a left vector operand as a
368 * row vector. In all these cases, it is required that the number of
369 * columns of the left operand is equal to the number of rows of the
370 * right operand. Then, the multiply (*) operation does a linear
371 * algebraic multiply, yielding an object that has the same number of
372 * rows as the left operand and the same number of columns as the right
373 * operand. Section 5.10 "Vector and Matrix Operations" explains in
374 * more detail how vectors and matrices are operated on."
377 if (type_a
== type_b
)
380 const glsl_type
*type
= glsl_type::get_mul_type(type_a
, type_b
);
382 if (type
== glsl_type::error_type
) {
383 _mesa_glsl_error(loc
, state
,
384 "size mismatch for matrix multiplication");
391 /* "All other cases are illegal."
393 _mesa_glsl_error(loc
, state
, "type mismatch");
394 return glsl_type::error_type
;
398 static const struct glsl_type
*
399 unary_arithmetic_result_type(const struct glsl_type
*type
,
400 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
402 /* From GLSL 1.50 spec, page 57:
404 * "The arithmetic unary operators negate (-), post- and pre-increment
405 * and decrement (-- and ++) operate on integer or floating-point
406 * values (including vectors and matrices). All unary operators work
407 * component-wise on their operands. These result with the same type
410 if (!type
->is_numeric()) {
411 _mesa_glsl_error(loc
, state
,
412 "operands to arithmetic operators must be numeric");
413 return glsl_type::error_type
;
420 * \brief Return the result type of a bit-logic operation.
422 * If the given types to the bit-logic operator are invalid, return
423 * glsl_type::error_type.
425 * \param type_a Type of LHS of bit-logic op
426 * \param type_b Type of RHS of bit-logic op
428 static const struct glsl_type
*
429 bit_logic_result_type(const struct glsl_type
*type_a
,
430 const struct glsl_type
*type_b
,
432 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
434 if (!state
->check_bitwise_operations_allowed(loc
)) {
435 return glsl_type::error_type
;
438 /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
440 * "The bitwise operators and (&), exclusive-or (^), and inclusive-or
441 * (|). The operands must be of type signed or unsigned integers or
444 if (!type_a
->is_integer()) {
445 _mesa_glsl_error(loc
, state
, "LHS of `%s' must be an integer",
446 ast_expression::operator_string(op
));
447 return glsl_type::error_type
;
449 if (!type_b
->is_integer()) {
450 _mesa_glsl_error(loc
, state
, "RHS of `%s' must be an integer",
451 ast_expression::operator_string(op
));
452 return glsl_type::error_type
;
455 /* "The fundamental types of the operands (signed or unsigned) must
458 if (type_a
->base_type
!= type_b
->base_type
) {
459 _mesa_glsl_error(loc
, state
, "operands of `%s' must have the same "
460 "base type", ast_expression::operator_string(op
));
461 return glsl_type::error_type
;
464 /* "The operands cannot be vectors of differing size." */
465 if (type_a
->is_vector() &&
466 type_b
->is_vector() &&
467 type_a
->vector_elements
!= type_b
->vector_elements
) {
468 _mesa_glsl_error(loc
, state
, "operands of `%s' cannot be vectors of "
469 "different sizes", ast_expression::operator_string(op
));
470 return glsl_type::error_type
;
473 /* "If one operand is a scalar and the other a vector, the scalar is
474 * applied component-wise to the vector, resulting in the same type as
475 * the vector. The fundamental types of the operands [...] will be the
476 * resulting fundamental type."
478 if (type_a
->is_scalar())
484 static const struct glsl_type
*
485 modulus_result_type(const struct glsl_type
*type_a
,
486 const struct glsl_type
*type_b
,
487 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
489 if (!state
->check_version(130, 300, loc
, "operator '%%' is reserved")) {
490 return glsl_type::error_type
;
493 /* From GLSL 1.50 spec, page 56:
494 * "The operator modulus (%) operates on signed or unsigned integers or
495 * integer vectors. The operand types must both be signed or both be
498 if (!type_a
->is_integer()) {
499 _mesa_glsl_error(loc
, state
, "LHS of operator %% must be an integer");
500 return glsl_type::error_type
;
502 if (!type_b
->is_integer()) {
503 _mesa_glsl_error(loc
, state
, "RHS of operator %% must be an integer");
504 return glsl_type::error_type
;
506 if (type_a
->base_type
!= type_b
->base_type
) {
507 _mesa_glsl_error(loc
, state
,
508 "operands of %% must have the same base type");
509 return glsl_type::error_type
;
512 /* "The operands cannot be vectors of differing size. If one operand is
513 * a scalar and the other vector, then the scalar is applied component-
514 * wise to the vector, resulting in the same type as the vector. If both
515 * are vectors of the same size, the result is computed component-wise."
517 if (type_a
->is_vector()) {
518 if (!type_b
->is_vector()
519 || (type_a
->vector_elements
== type_b
->vector_elements
))
524 /* "The operator modulus (%) is not defined for any other data types
525 * (non-integer types)."
527 _mesa_glsl_error(loc
, state
, "type mismatch");
528 return glsl_type::error_type
;
532 static const struct glsl_type
*
533 relational_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
534 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
536 const glsl_type
*type_a
= value_a
->type
;
537 const glsl_type
*type_b
= value_b
->type
;
539 /* From GLSL 1.50 spec, page 56:
540 * "The relational operators greater than (>), less than (<), greater
541 * than or equal (>=), and less than or equal (<=) operate only on
542 * scalar integer and scalar floating-point expressions."
544 if (!type_a
->is_numeric()
545 || !type_b
->is_numeric()
546 || !type_a
->is_scalar()
547 || !type_b
->is_scalar()) {
548 _mesa_glsl_error(loc
, state
,
549 "operands to relational operators must be scalar and "
551 return glsl_type::error_type
;
554 /* "Either the operands' types must match, or the conversions from
555 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
556 * operand, after which the types must match."
558 if (!apply_implicit_conversion(type_a
, value_b
, state
)
559 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
560 _mesa_glsl_error(loc
, state
,
561 "could not implicitly convert operands to "
562 "relational operator");
563 return glsl_type::error_type
;
565 type_a
= value_a
->type
;
566 type_b
= value_b
->type
;
568 if (type_a
->base_type
!= type_b
->base_type
) {
569 _mesa_glsl_error(loc
, state
, "base type mismatch");
570 return glsl_type::error_type
;
573 /* "The result is scalar Boolean."
575 return glsl_type::bool_type
;
579 * \brief Return the result type of a bit-shift operation.
581 * If the given types to the bit-shift operator are invalid, return
582 * glsl_type::error_type.
584 * \param type_a Type of LHS of bit-shift op
585 * \param type_b Type of RHS of bit-shift op
587 static const struct glsl_type
*
588 shift_result_type(const struct glsl_type
*type_a
,
589 const struct glsl_type
*type_b
,
591 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
593 if (!state
->check_bitwise_operations_allowed(loc
)) {
594 return glsl_type::error_type
;
597 /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
599 * "The shift operators (<<) and (>>). For both operators, the operands
600 * must be signed or unsigned integers or integer vectors. One operand
601 * can be signed while the other is unsigned."
603 if (!type_a
->is_integer()) {
604 _mesa_glsl_error(loc
, state
, "LHS of operator %s must be an integer or "
605 "integer vector", ast_expression::operator_string(op
));
606 return glsl_type::error_type
;
609 if (!type_b
->is_integer()) {
610 _mesa_glsl_error(loc
, state
, "RHS of operator %s must be an integer or "
611 "integer vector", ast_expression::operator_string(op
));
612 return glsl_type::error_type
;
615 /* "If the first operand is a scalar, the second operand has to be
618 if (type_a
->is_scalar() && !type_b
->is_scalar()) {
619 _mesa_glsl_error(loc
, state
, "if the first operand of %s is scalar, the "
620 "second must be scalar as well",
621 ast_expression::operator_string(op
));
622 return glsl_type::error_type
;
625 /* If both operands are vectors, check that they have same number of
628 if (type_a
->is_vector() &&
629 type_b
->is_vector() &&
630 type_a
->vector_elements
!= type_b
->vector_elements
) {
631 _mesa_glsl_error(loc
, state
, "vector operands to operator %s must "
632 "have same number of elements",
633 ast_expression::operator_string(op
));
634 return glsl_type::error_type
;
637 /* "In all cases, the resulting type will be the same type as the left
644 * Returns the innermost array index expression in an rvalue tree.
645 * This is the largest indexing level -- if an array of blocks, then
646 * it is the block index rather than an indexing expression for an
647 * array-typed member of an array of blocks.
650 find_innermost_array_index(ir_rvalue
*rv
)
652 ir_dereference_array
*last
= NULL
;
654 if (rv
->as_dereference_array()) {
655 last
= rv
->as_dereference_array();
657 } else if (rv
->as_dereference_record())
658 rv
= rv
->as_dereference_record()->record
;
659 else if (rv
->as_swizzle())
660 rv
= rv
->as_swizzle()->val
;
666 return last
->array_index
;
672 * Validates that a value can be assigned to a location with a specified type
674 * Validates that \c rhs can be assigned to some location. If the types are
675 * not an exact match but an automatic conversion is possible, \c rhs will be
679 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
680 * Otherwise the actual RHS to be assigned will be returned. This may be
681 * \c rhs, or it may be \c rhs after some type conversion.
684 * In addition to being used for assignments, this function is used to
685 * type-check return values.
688 validate_assignment(struct _mesa_glsl_parse_state
*state
,
689 YYLTYPE loc
, ir_rvalue
*lhs
,
690 ir_rvalue
*rhs
, bool is_initializer
)
692 /* If there is already some error in the RHS, just return it. Anything
693 * else will lead to an avalanche of error message back to the user.
695 if (rhs
->type
->is_error())
698 /* In the Tessellation Control Shader:
699 * If a per-vertex output variable is used as an l-value, it is an error
700 * if the expression indicating the vertex number is not the identifier
703 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
704 ir_variable
*var
= lhs
->variable_referenced();
705 if (var
->data
.mode
== ir_var_shader_out
&& !var
->data
.patch
) {
706 ir_rvalue
*index
= find_innermost_array_index(lhs
);
707 ir_variable
*index_var
= index
? index
->variable_referenced() : NULL
;
708 if (!index_var
|| strcmp(index_var
->name
, "gl_InvocationID") != 0) {
709 _mesa_glsl_error(&loc
, state
,
710 "Tessellation control shader outputs can only "
711 "be indexed by gl_InvocationID");
717 /* If the types are identical, the assignment can trivially proceed.
719 if (rhs
->type
== lhs
->type
)
722 /* If the array element types are the same and the LHS is unsized,
723 * the assignment is okay for initializers embedded in variable
726 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
727 * is handled by ir_dereference::is_lvalue.
729 if (lhs
->type
->is_unsized_array() && rhs
->type
->is_array()
730 && (lhs
->type
->fields
.array
== rhs
->type
->fields
.array
)) {
731 if (is_initializer
) {
734 _mesa_glsl_error(&loc
, state
,
735 "implicitly sized arrays cannot be assigned");
740 /* Check for implicit conversion in GLSL 1.20 */
741 if (apply_implicit_conversion(lhs
->type
, rhs
, state
)) {
742 if (rhs
->type
== lhs
->type
)
746 _mesa_glsl_error(&loc
, state
,
747 "%s of type %s cannot be assigned to "
748 "variable of type %s",
749 is_initializer
? "initializer" : "value",
750 rhs
->type
->name
, lhs
->type
->name
);
756 mark_whole_array_access(ir_rvalue
*access
)
758 ir_dereference_variable
*deref
= access
->as_dereference_variable();
760 if (deref
&& deref
->var
) {
761 deref
->var
->data
.max_array_access
= deref
->type
->length
- 1;
766 do_assignment(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
767 const char *non_lvalue_description
,
768 ir_rvalue
*lhs
, ir_rvalue
*rhs
,
769 ir_rvalue
**out_rvalue
, bool needs_rvalue
,
774 bool error_emitted
= (lhs
->type
->is_error() || rhs
->type
->is_error());
775 ir_rvalue
*extract_channel
= NULL
;
777 /* If the assignment LHS comes back as an ir_binop_vector_extract
778 * expression, move it to the RHS as an ir_triop_vector_insert.
780 if (lhs
->ir_type
== ir_type_expression
) {
781 ir_expression
*const lhs_expr
= lhs
->as_expression();
783 if (unlikely(lhs_expr
->operation
== ir_binop_vector_extract
)) {
785 validate_assignment(state
, lhs_loc
, lhs
,
786 rhs
, is_initializer
);
788 if (new_rhs
== NULL
) {
792 * - LHS: (expression float vector_extract <vec> <channel>)
796 * - RHS: (expression vec2 vector_insert <vec> <channel> <scalar>)
798 * The LHS type is now a vector instead of a scalar. Since GLSL
799 * allows assignments to be used as rvalues, we need to re-extract
800 * the channel from assignment_temp when returning the rvalue.
802 extract_channel
= lhs_expr
->operands
[1];
803 rhs
= new(ctx
) ir_expression(ir_triop_vector_insert
,
804 lhs_expr
->operands
[0]->type
,
805 lhs_expr
->operands
[0],
808 lhs
= lhs_expr
->operands
[0]->clone(ctx
, NULL
);
813 ir_variable
*lhs_var
= lhs
->variable_referenced();
815 lhs_var
->data
.assigned
= true;
817 if (!error_emitted
) {
818 if (non_lvalue_description
!= NULL
) {
819 _mesa_glsl_error(&lhs_loc
, state
,
821 non_lvalue_description
);
822 error_emitted
= true;
823 } else if (lhs_var
!= NULL
&& lhs_var
->data
.read_only
) {
824 _mesa_glsl_error(&lhs_loc
, state
,
825 "assignment to read-only variable '%s'",
827 error_emitted
= true;
828 } else if (lhs
->type
->is_array() &&
829 !state
->check_version(120, 300, &lhs_loc
,
830 "whole array assignment forbidden")) {
831 /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
833 * "Other binary or unary expressions, non-dereferenced
834 * arrays, function names, swizzles with repeated fields,
835 * and constants cannot be l-values."
837 * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00.
839 error_emitted
= true;
840 } else if (!lhs
->is_lvalue()) {
841 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
842 error_emitted
= true;
847 validate_assignment(state
, lhs_loc
, lhs
, rhs
, is_initializer
);
848 if (new_rhs
!= NULL
) {
851 /* If the LHS array was not declared with a size, it takes it size from
852 * the RHS. If the LHS is an l-value and a whole array, it must be a
853 * dereference of a variable. Any other case would require that the LHS
854 * is either not an l-value or not a whole array.
856 if (lhs
->type
->is_unsized_array()) {
857 ir_dereference
*const d
= lhs
->as_dereference();
861 ir_variable
*const var
= d
->variable_referenced();
865 if (var
->data
.max_array_access
>= unsigned(rhs
->type
->array_size())) {
866 /* FINISHME: This should actually log the location of the RHS. */
867 _mesa_glsl_error(& lhs_loc
, state
, "array size must be > %u due to "
869 var
->data
.max_array_access
);
872 var
->type
= glsl_type::get_array_instance(lhs
->type
->fields
.array
,
873 rhs
->type
->array_size());
876 if (lhs
->type
->is_array()) {
877 mark_whole_array_access(rhs
);
878 mark_whole_array_access(lhs
);
882 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
883 * but not post_inc) need the converted assigned value as an rvalue
884 * to handle things like:
889 ir_variable
*var
= new(ctx
) ir_variable(rhs
->type
, "assignment_tmp",
891 instructions
->push_tail(var
);
892 instructions
->push_tail(assign(var
, rhs
));
894 if (!error_emitted
) {
895 ir_dereference_variable
*deref_var
= new(ctx
) ir_dereference_variable(var
);
896 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, deref_var
));
898 ir_rvalue
*rvalue
= new(ctx
) ir_dereference_variable(var
);
900 if (extract_channel
) {
901 rvalue
= new(ctx
) ir_expression(ir_binop_vector_extract
,
903 extract_channel
->clone(ctx
, NULL
));
906 *out_rvalue
= rvalue
;
909 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, rhs
));
913 return error_emitted
;
917 get_lvalue_copy(exec_list
*instructions
, ir_rvalue
*lvalue
)
919 void *ctx
= ralloc_parent(lvalue
);
922 var
= new(ctx
) ir_variable(lvalue
->type
, "_post_incdec_tmp",
924 instructions
->push_tail(var
);
926 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
929 return new(ctx
) ir_dereference_variable(var
);
934 ast_node::hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
943 ast_function_expression::hir_no_rvalue(exec_list
*instructions
,
944 struct _mesa_glsl_parse_state
*state
)
946 (void)hir(instructions
, state
);
950 ast_aggregate_initializer::hir_no_rvalue(exec_list
*instructions
,
951 struct _mesa_glsl_parse_state
*state
)
953 (void)hir(instructions
, state
);
957 do_comparison(void *mem_ctx
, int operation
, ir_rvalue
*op0
, ir_rvalue
*op1
)
960 ir_rvalue
*cmp
= NULL
;
962 if (operation
== ir_binop_all_equal
)
963 join_op
= ir_binop_logic_and
;
965 join_op
= ir_binop_logic_or
;
967 switch (op0
->type
->base_type
) {
968 case GLSL_TYPE_FLOAT
:
972 case GLSL_TYPE_DOUBLE
:
973 return new(mem_ctx
) ir_expression(operation
, op0
, op1
);
975 case GLSL_TYPE_ARRAY
: {
976 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
977 ir_rvalue
*e0
, *e1
, *result
;
979 e0
= new(mem_ctx
) ir_dereference_array(op0
->clone(mem_ctx
, NULL
),
980 new(mem_ctx
) ir_constant(i
));
981 e1
= new(mem_ctx
) ir_dereference_array(op1
->clone(mem_ctx
, NULL
),
982 new(mem_ctx
) ir_constant(i
));
983 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
986 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
992 mark_whole_array_access(op0
);
993 mark_whole_array_access(op1
);
997 case GLSL_TYPE_STRUCT
: {
998 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
999 ir_rvalue
*e0
, *e1
, *result
;
1000 const char *field_name
= op0
->type
->fields
.structure
[i
].name
;
1002 e0
= new(mem_ctx
) ir_dereference_record(op0
->clone(mem_ctx
, NULL
),
1004 e1
= new(mem_ctx
) ir_dereference_record(op1
->clone(mem_ctx
, NULL
),
1006 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
1009 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
1017 case GLSL_TYPE_ERROR
:
1018 case GLSL_TYPE_VOID
:
1019 case GLSL_TYPE_SAMPLER
:
1020 case GLSL_TYPE_IMAGE
:
1021 case GLSL_TYPE_INTERFACE
:
1022 case GLSL_TYPE_ATOMIC_UINT
:
1023 case GLSL_TYPE_SUBROUTINE
:
1024 /* I assume a comparison of a struct containing a sampler just
1025 * ignores the sampler present in the type.
1031 cmp
= new(mem_ctx
) ir_constant(true);
1036 /* For logical operations, we want to ensure that the operands are
1037 * scalar booleans. If it isn't, emit an error and return a constant
1038 * boolean to avoid triggering cascading error messages.
1041 get_scalar_boolean_operand(exec_list
*instructions
,
1042 struct _mesa_glsl_parse_state
*state
,
1043 ast_expression
*parent_expr
,
1045 const char *operand_name
,
1046 bool *error_emitted
)
1048 ast_expression
*expr
= parent_expr
->subexpressions
[operand
];
1050 ir_rvalue
*val
= expr
->hir(instructions
, state
);
1052 if (val
->type
->is_boolean() && val
->type
->is_scalar())
1055 if (!*error_emitted
) {
1056 YYLTYPE loc
= expr
->get_location();
1057 _mesa_glsl_error(&loc
, state
, "%s of `%s' must be scalar boolean",
1059 parent_expr
->operator_string(parent_expr
->oper
));
1060 *error_emitted
= true;
1063 return new(ctx
) ir_constant(true);
1067 * If name refers to a builtin array whose maximum allowed size is less than
1068 * size, report an error and return true. Otherwise return false.
1071 check_builtin_array_max_size(const char *name
, unsigned size
,
1072 YYLTYPE loc
, struct _mesa_glsl_parse_state
*state
)
1074 if ((strcmp("gl_TexCoord", name
) == 0)
1075 && (size
> state
->Const
.MaxTextureCoords
)) {
1076 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1078 * "The size [of gl_TexCoord] can be at most
1079 * gl_MaxTextureCoords."
1081 _mesa_glsl_error(&loc
, state
, "`gl_TexCoord' array size cannot "
1082 "be larger than gl_MaxTextureCoords (%u)",
1083 state
->Const
.MaxTextureCoords
);
1084 } else if (strcmp("gl_ClipDistance", name
) == 0
1085 && size
> state
->Const
.MaxClipPlanes
) {
1086 /* From section 7.1 (Vertex Shader Special Variables) of the
1089 * "The gl_ClipDistance array is predeclared as unsized and
1090 * must be sized by the shader either redeclaring it with a
1091 * size or indexing it only with integral constant
1092 * expressions. ... The size can be at most
1093 * gl_MaxClipDistances."
1095 _mesa_glsl_error(&loc
, state
, "`gl_ClipDistance' array size cannot "
1096 "be larger than gl_MaxClipDistances (%u)",
1097 state
->Const
.MaxClipPlanes
);
1102 * Create the constant 1, of a which is appropriate for incrementing and
1103 * decrementing values of the given GLSL type. For example, if type is vec4,
1104 * this creates a constant value of 1.0 having type float.
1106 * If the given type is invalid for increment and decrement operators, return
1107 * a floating point 1--the error will be detected later.
1110 constant_one_for_inc_dec(void *ctx
, const glsl_type
*type
)
1112 switch (type
->base_type
) {
1113 case GLSL_TYPE_UINT
:
1114 return new(ctx
) ir_constant((unsigned) 1);
1116 return new(ctx
) ir_constant(1);
1118 case GLSL_TYPE_FLOAT
:
1119 return new(ctx
) ir_constant(1.0f
);
1124 ast_expression::hir(exec_list
*instructions
,
1125 struct _mesa_glsl_parse_state
*state
)
1127 return do_hir(instructions
, state
, true);
1131 ast_expression::hir_no_rvalue(exec_list
*instructions
,
1132 struct _mesa_glsl_parse_state
*state
)
1134 do_hir(instructions
, state
, false);
1138 ast_expression::do_hir(exec_list
*instructions
,
1139 struct _mesa_glsl_parse_state
*state
,
1143 static const int operations
[AST_NUM_OPERATORS
] = {
1144 -1, /* ast_assign doesn't convert to ir_expression. */
1145 -1, /* ast_plus doesn't convert to ir_expression. */
1159 ir_binop_any_nequal
,
1169 /* Note: The following block of expression types actually convert
1170 * to multiple IR instructions.
1172 ir_binop_mul
, /* ast_mul_assign */
1173 ir_binop_div
, /* ast_div_assign */
1174 ir_binop_mod
, /* ast_mod_assign */
1175 ir_binop_add
, /* ast_add_assign */
1176 ir_binop_sub
, /* ast_sub_assign */
1177 ir_binop_lshift
, /* ast_ls_assign */
1178 ir_binop_rshift
, /* ast_rs_assign */
1179 ir_binop_bit_and
, /* ast_and_assign */
1180 ir_binop_bit_xor
, /* ast_xor_assign */
1181 ir_binop_bit_or
, /* ast_or_assign */
1183 -1, /* ast_conditional doesn't convert to ir_expression. */
1184 ir_binop_add
, /* ast_pre_inc. */
1185 ir_binop_sub
, /* ast_pre_dec. */
1186 ir_binop_add
, /* ast_post_inc. */
1187 ir_binop_sub
, /* ast_post_dec. */
1188 -1, /* ast_field_selection doesn't conv to ir_expression. */
1189 -1, /* ast_array_index doesn't convert to ir_expression. */
1190 -1, /* ast_function_call doesn't conv to ir_expression. */
1191 -1, /* ast_identifier doesn't convert to ir_expression. */
1192 -1, /* ast_int_constant doesn't convert to ir_expression. */
1193 -1, /* ast_uint_constant doesn't conv to ir_expression. */
1194 -1, /* ast_float_constant doesn't conv to ir_expression. */
1195 -1, /* ast_bool_constant doesn't conv to ir_expression. */
1196 -1, /* ast_sequence doesn't convert to ir_expression. */
1198 ir_rvalue
*result
= NULL
;
1200 const struct glsl_type
*type
; /* a temporary variable for switch cases */
1201 bool error_emitted
= false;
1204 loc
= this->get_location();
1206 switch (this->oper
) {
1208 assert(!"ast_aggregate: Should never get here.");
1212 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1213 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1216 do_assignment(instructions
, state
,
1217 this->subexpressions
[0]->non_lvalue_description
,
1218 op
[0], op
[1], &result
, needs_rvalue
, false,
1219 this->subexpressions
[0]->get_location());
1224 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1226 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1228 error_emitted
= type
->is_error();
1234 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1236 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1238 error_emitted
= type
->is_error();
1240 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1248 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1249 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1251 type
= arithmetic_result_type(op
[0], op
[1],
1252 (this->oper
== ast_mul
),
1254 error_emitted
= type
->is_error();
1256 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1261 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1262 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1264 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1266 assert(operations
[this->oper
] == ir_binop_mod
);
1268 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1270 error_emitted
= type
->is_error();
1275 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1276 error_emitted
= true;
1279 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1280 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1281 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1283 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1285 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1292 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1293 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1295 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
1297 /* The relational operators must either generate an error or result
1298 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1300 assert(type
->is_error()
1301 || ((type
->base_type
== GLSL_TYPE_BOOL
)
1302 && type
->is_scalar()));
1304 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1306 error_emitted
= type
->is_error();
1311 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1312 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1314 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1316 * "The equality operators equal (==), and not equal (!=)
1317 * operate on all types. They result in a scalar Boolean. If
1318 * the operand types do not match, then there must be a
1319 * conversion from Section 4.1.10 "Implicit Conversions"
1320 * applied to one operand that can make them match, in which
1321 * case this conversion is done."
1324 if (op
[0]->type
== glsl_type::void_type
|| op
[1]->type
== glsl_type::void_type
) {
1325 _mesa_glsl_error(& loc
, state
, "`%s': wrong operand types: "
1326 "no operation `%1$s' exists that takes a left-hand "
1327 "operand of type 'void' or a right operand of type "
1328 "'void'", (this->oper
== ast_equal
) ? "==" : "!=");
1329 error_emitted
= true;
1330 } else if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
1331 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
1332 || (op
[0]->type
!= op
[1]->type
)) {
1333 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
1334 "type", (this->oper
== ast_equal
) ? "==" : "!=");
1335 error_emitted
= true;
1336 } else if ((op
[0]->type
->is_array() || op
[1]->type
->is_array()) &&
1337 !state
->check_version(120, 300, &loc
,
1338 "array comparisons forbidden")) {
1339 error_emitted
= true;
1340 } else if ((op
[0]->type
->contains_opaque() ||
1341 op
[1]->type
->contains_opaque())) {
1342 _mesa_glsl_error(&loc
, state
, "opaque type comparisons forbidden");
1343 error_emitted
= true;
1346 if (error_emitted
) {
1347 result
= new(ctx
) ir_constant(false);
1349 result
= do_comparison(ctx
, operations
[this->oper
], op
[0], op
[1]);
1350 assert(result
->type
== glsl_type::bool_type
);
1357 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1358 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1359 type
= bit_logic_result_type(op
[0]->type
, op
[1]->type
, this->oper
,
1361 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1363 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1367 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1369 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1370 error_emitted
= true;
1373 if (!op
[0]->type
->is_integer()) {
1374 _mesa_glsl_error(&loc
, state
, "operand of `~' must be an integer");
1375 error_emitted
= true;
1378 type
= error_emitted
? glsl_type::error_type
: op
[0]->type
;
1379 result
= new(ctx
) ir_expression(ir_unop_bit_not
, type
, op
[0], NULL
);
1382 case ast_logic_and
: {
1383 exec_list rhs_instructions
;
1384 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1385 "LHS", &error_emitted
);
1386 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1387 "RHS", &error_emitted
);
1389 if (rhs_instructions
.is_empty()) {
1390 result
= new(ctx
) ir_expression(ir_binop_logic_and
, op
[0], op
[1]);
1391 type
= result
->type
;
1393 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1396 instructions
->push_tail(tmp
);
1398 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1399 instructions
->push_tail(stmt
);
1401 stmt
->then_instructions
.append_list(&rhs_instructions
);
1402 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1403 ir_assignment
*const then_assign
=
1404 new(ctx
) ir_assignment(then_deref
, op
[1]);
1405 stmt
->then_instructions
.push_tail(then_assign
);
1407 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1408 ir_assignment
*const else_assign
=
1409 new(ctx
) ir_assignment(else_deref
, new(ctx
) ir_constant(false));
1410 stmt
->else_instructions
.push_tail(else_assign
);
1412 result
= new(ctx
) ir_dereference_variable(tmp
);
1418 case ast_logic_or
: {
1419 exec_list rhs_instructions
;
1420 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1421 "LHS", &error_emitted
);
1422 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1423 "RHS", &error_emitted
);
1425 if (rhs_instructions
.is_empty()) {
1426 result
= new(ctx
) ir_expression(ir_binop_logic_or
, op
[0], op
[1]);
1427 type
= result
->type
;
1429 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1432 instructions
->push_tail(tmp
);
1434 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1435 instructions
->push_tail(stmt
);
1437 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1438 ir_assignment
*const then_assign
=
1439 new(ctx
) ir_assignment(then_deref
, new(ctx
) ir_constant(true));
1440 stmt
->then_instructions
.push_tail(then_assign
);
1442 stmt
->else_instructions
.append_list(&rhs_instructions
);
1443 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1444 ir_assignment
*const else_assign
=
1445 new(ctx
) ir_assignment(else_deref
, op
[1]);
1446 stmt
->else_instructions
.push_tail(else_assign
);
1448 result
= new(ctx
) ir_dereference_variable(tmp
);
1455 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1457 * "The logical binary operators and (&&), or ( | | ), and
1458 * exclusive or (^^). They operate only on two Boolean
1459 * expressions and result in a Boolean expression."
1461 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0, "LHS",
1463 op
[1] = get_scalar_boolean_operand(instructions
, state
, this, 1, "RHS",
1466 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1471 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1472 "operand", &error_emitted
);
1474 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1478 case ast_mul_assign
:
1479 case ast_div_assign
:
1480 case ast_add_assign
:
1481 case ast_sub_assign
: {
1482 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1483 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1485 type
= arithmetic_result_type(op
[0], op
[1],
1486 (this->oper
== ast_mul_assign
),
1489 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1493 do_assignment(instructions
, state
,
1494 this->subexpressions
[0]->non_lvalue_description
,
1495 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1496 &result
, needs_rvalue
, false,
1497 this->subexpressions
[0]->get_location());
1499 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1500 * explicitly test for this because none of the binary expression
1501 * operators allow array operands either.
1507 case ast_mod_assign
: {
1508 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1509 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1511 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1513 assert(operations
[this->oper
] == ir_binop_mod
);
1515 ir_rvalue
*temp_rhs
;
1516 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1520 do_assignment(instructions
, state
,
1521 this->subexpressions
[0]->non_lvalue_description
,
1522 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1523 &result
, needs_rvalue
, false,
1524 this->subexpressions
[0]->get_location());
1529 case ast_rs_assign
: {
1530 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1531 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1532 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1534 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1535 type
, op
[0], op
[1]);
1537 do_assignment(instructions
, state
,
1538 this->subexpressions
[0]->non_lvalue_description
,
1539 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1540 &result
, needs_rvalue
, false,
1541 this->subexpressions
[0]->get_location());
1545 case ast_and_assign
:
1546 case ast_xor_assign
:
1547 case ast_or_assign
: {
1548 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1549 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1550 type
= bit_logic_result_type(op
[0]->type
, op
[1]->type
, this->oper
,
1552 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1553 type
, op
[0], op
[1]);
1555 do_assignment(instructions
, state
,
1556 this->subexpressions
[0]->non_lvalue_description
,
1557 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1558 &result
, needs_rvalue
, false,
1559 this->subexpressions
[0]->get_location());
1563 case ast_conditional
: {
1564 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1566 * "The ternary selection operator (?:). It operates on three
1567 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1568 * first expression, which must result in a scalar Boolean."
1570 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1571 "condition", &error_emitted
);
1573 /* The :? operator is implemented by generating an anonymous temporary
1574 * followed by an if-statement. The last instruction in each branch of
1575 * the if-statement assigns a value to the anonymous temporary. This
1576 * temporary is the r-value of the expression.
1578 exec_list then_instructions
;
1579 exec_list else_instructions
;
1581 op
[1] = this->subexpressions
[1]->hir(&then_instructions
, state
);
1582 op
[2] = this->subexpressions
[2]->hir(&else_instructions
, state
);
1584 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1586 * "The second and third expressions can be any type, as
1587 * long their types match, or there is a conversion in
1588 * Section 4.1.10 "Implicit Conversions" that can be applied
1589 * to one of the expressions to make their types match. This
1590 * resulting matching type is the type of the entire
1593 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1594 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1595 || (op
[1]->type
!= op
[2]->type
)) {
1596 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1598 _mesa_glsl_error(& loc
, state
, "second and third operands of ?: "
1599 "operator must have matching types");
1600 error_emitted
= true;
1601 type
= glsl_type::error_type
;
1606 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1608 * "The second and third expressions must be the same type, but can
1609 * be of any type other than an array."
1611 if (type
->is_array() &&
1612 !state
->check_version(120, 300, &loc
,
1613 "second and third operands of ?: operator "
1614 "cannot be arrays")) {
1615 error_emitted
= true;
1618 /* From section 4.1.7 of the GLSL 4.50 spec (Opaque Types):
1620 * "Except for array indexing, structure member selection, and
1621 * parentheses, opaque variables are not allowed to be operands in
1622 * expressions; such use results in a compile-time error."
1624 if (type
->contains_opaque()) {
1625 _mesa_glsl_error(&loc
, state
, "opaque variables cannot be operands "
1626 "of the ?: operator");
1627 error_emitted
= true;
1630 ir_constant
*cond_val
= op
[0]->constant_expression_value();
1632 if (then_instructions
.is_empty()
1633 && else_instructions
.is_empty()
1634 && cond_val
!= NULL
) {
1635 result
= cond_val
->value
.b
[0] ? op
[1] : op
[2];
1637 /* The copy to conditional_tmp reads the whole array. */
1638 if (type
->is_array()) {
1639 mark_whole_array_access(op
[1]);
1640 mark_whole_array_access(op
[2]);
1643 ir_variable
*const tmp
=
1644 new(ctx
) ir_variable(type
, "conditional_tmp", ir_var_temporary
);
1645 instructions
->push_tail(tmp
);
1647 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1648 instructions
->push_tail(stmt
);
1650 then_instructions
.move_nodes_to(& stmt
->then_instructions
);
1651 ir_dereference
*const then_deref
=
1652 new(ctx
) ir_dereference_variable(tmp
);
1653 ir_assignment
*const then_assign
=
1654 new(ctx
) ir_assignment(then_deref
, op
[1]);
1655 stmt
->then_instructions
.push_tail(then_assign
);
1657 else_instructions
.move_nodes_to(& stmt
->else_instructions
);
1658 ir_dereference
*const else_deref
=
1659 new(ctx
) ir_dereference_variable(tmp
);
1660 ir_assignment
*const else_assign
=
1661 new(ctx
) ir_assignment(else_deref
, op
[2]);
1662 stmt
->else_instructions
.push_tail(else_assign
);
1664 result
= new(ctx
) ir_dereference_variable(tmp
);
1671 this->non_lvalue_description
= (this->oper
== ast_pre_inc
)
1672 ? "pre-increment operation" : "pre-decrement operation";
1674 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1675 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1677 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1679 ir_rvalue
*temp_rhs
;
1680 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1684 do_assignment(instructions
, state
,
1685 this->subexpressions
[0]->non_lvalue_description
,
1686 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1687 &result
, needs_rvalue
, false,
1688 this->subexpressions
[0]->get_location());
1693 case ast_post_dec
: {
1694 this->non_lvalue_description
= (this->oper
== ast_post_inc
)
1695 ? "post-increment operation" : "post-decrement operation";
1696 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1697 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1699 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1701 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1703 ir_rvalue
*temp_rhs
;
1704 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1707 /* Get a temporary of a copy of the lvalue before it's modified.
1708 * This may get thrown away later.
1710 result
= get_lvalue_copy(instructions
, op
[0]->clone(ctx
, NULL
));
1712 ir_rvalue
*junk_rvalue
;
1714 do_assignment(instructions
, state
,
1715 this->subexpressions
[0]->non_lvalue_description
,
1716 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1717 &junk_rvalue
, false, false,
1718 this->subexpressions
[0]->get_location());
1723 case ast_field_selection
:
1724 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1727 case ast_array_index
: {
1728 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1730 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1731 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1733 result
= _mesa_ast_array_index_to_hir(ctx
, state
, op
[0], op
[1],
1736 if (result
->type
->is_error())
1737 error_emitted
= true;
1742 case ast_function_call
:
1743 /* Should *NEVER* get here. ast_function_call should always be handled
1744 * by ast_function_expression::hir.
1749 case ast_identifier
: {
1750 /* ast_identifier can appear several places in a full abstract syntax
1751 * tree. This particular use must be at location specified in the grammar
1752 * as 'variable_identifier'.
1755 state
->symbols
->get_variable(this->primary_expression
.identifier
);
1758 var
->data
.used
= true;
1759 result
= new(ctx
) ir_dereference_variable(var
);
1761 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
1762 this->primary_expression
.identifier
);
1764 result
= ir_rvalue::error_value(ctx
);
1765 error_emitted
= true;
1770 case ast_int_constant
:
1771 result
= new(ctx
) ir_constant(this->primary_expression
.int_constant
);
1774 case ast_uint_constant
:
1775 result
= new(ctx
) ir_constant(this->primary_expression
.uint_constant
);
1778 case ast_float_constant
:
1779 result
= new(ctx
) ir_constant(this->primary_expression
.float_constant
);
1782 case ast_bool_constant
:
1783 result
= new(ctx
) ir_constant(bool(this->primary_expression
.bool_constant
));
1786 case ast_double_constant
:
1787 result
= new(ctx
) ir_constant(this->primary_expression
.double_constant
);
1790 case ast_sequence
: {
1791 /* It should not be possible to generate a sequence in the AST without
1792 * any expressions in it.
1794 assert(!this->expressions
.is_empty());
1796 /* The r-value of a sequence is the last expression in the sequence. If
1797 * the other expressions in the sequence do not have side-effects (and
1798 * therefore add instructions to the instruction list), they get dropped
1801 exec_node
*previous_tail_pred
= NULL
;
1802 YYLTYPE previous_operand_loc
= loc
;
1804 foreach_list_typed (ast_node
, ast
, link
, &this->expressions
) {
1805 /* If one of the operands of comma operator does not generate any
1806 * code, we want to emit a warning. At each pass through the loop
1807 * previous_tail_pred will point to the last instruction in the
1808 * stream *before* processing the previous operand. Naturally,
1809 * instructions->tail_pred will point to the last instruction in the
1810 * stream *after* processing the previous operand. If the two
1811 * pointers match, then the previous operand had no effect.
1813 * The warning behavior here differs slightly from GCC. GCC will
1814 * only emit a warning if none of the left-hand operands have an
1815 * effect. However, it will emit a warning for each. I believe that
1816 * there are some cases in C (especially with GCC extensions) where
1817 * it is useful to have an intermediate step in a sequence have no
1818 * effect, but I don't think these cases exist in GLSL. Either way,
1819 * it would be a giant hassle to replicate that behavior.
1821 if (previous_tail_pred
== instructions
->tail_pred
) {
1822 _mesa_glsl_warning(&previous_operand_loc
, state
,
1823 "left-hand operand of comma expression has "
1827 /* tail_pred is directly accessed instead of using the get_tail()
1828 * method for performance reasons. get_tail() has extra code to
1829 * return NULL when the list is empty. We don't care about that
1830 * here, so using tail_pred directly is fine.
1832 previous_tail_pred
= instructions
->tail_pred
;
1833 previous_operand_loc
= ast
->get_location();
1835 result
= ast
->hir(instructions
, state
);
1838 /* Any errors should have already been emitted in the loop above.
1840 error_emitted
= true;
1844 type
= NULL
; /* use result->type, not type. */
1845 assert(result
!= NULL
|| !needs_rvalue
);
1847 if (result
&& result
->type
->is_error() && !error_emitted
)
1848 _mesa_glsl_error(& loc
, state
, "type mismatch");
1855 ast_expression_statement::hir(exec_list
*instructions
,
1856 struct _mesa_glsl_parse_state
*state
)
1858 /* It is possible to have expression statements that don't have an
1859 * expression. This is the solitary semicolon:
1861 * for (i = 0; i < 5; i++)
1864 * In this case the expression will be NULL. Test for NULL and don't do
1865 * anything in that case.
1867 if (expression
!= NULL
)
1868 expression
->hir_no_rvalue(instructions
, state
);
1870 /* Statements do not have r-values.
1877 ast_compound_statement::hir(exec_list
*instructions
,
1878 struct _mesa_glsl_parse_state
*state
)
1881 state
->symbols
->push_scope();
1883 foreach_list_typed (ast_node
, ast
, link
, &this->statements
)
1884 ast
->hir(instructions
, state
);
1887 state
->symbols
->pop_scope();
1889 /* Compound statements do not have r-values.
1895 * Evaluate the given exec_node (which should be an ast_node representing
1896 * a single array dimension) and return its integer value.
1899 process_array_size(exec_node
*node
,
1900 struct _mesa_glsl_parse_state
*state
)
1902 exec_list dummy_instructions
;
1904 ast_node
*array_size
= exec_node_data(ast_node
, node
, link
);
1905 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
1906 YYLTYPE loc
= array_size
->get_location();
1909 _mesa_glsl_error(& loc
, state
,
1910 "array size could not be resolved");
1914 if (!ir
->type
->is_integer()) {
1915 _mesa_glsl_error(& loc
, state
,
1916 "array size must be integer type");
1920 if (!ir
->type
->is_scalar()) {
1921 _mesa_glsl_error(& loc
, state
,
1922 "array size must be scalar type");
1926 ir_constant
*const size
= ir
->constant_expression_value();
1928 _mesa_glsl_error(& loc
, state
, "array size must be a "
1929 "constant valued expression");
1933 if (size
->value
.i
[0] <= 0) {
1934 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
1938 assert(size
->type
== ir
->type
);
1940 /* If the array size is const (and we've verified that
1941 * it is) then no instructions should have been emitted
1942 * when we converted it to HIR. If they were emitted,
1943 * then either the array size isn't const after all, or
1944 * we are emitting unnecessary instructions.
1946 assert(dummy_instructions
.is_empty());
1948 return size
->value
.u
[0];
1951 static const glsl_type
*
1952 process_array_type(YYLTYPE
*loc
, const glsl_type
*base
,
1953 ast_array_specifier
*array_specifier
,
1954 struct _mesa_glsl_parse_state
*state
)
1956 const glsl_type
*array_type
= base
;
1958 if (array_specifier
!= NULL
) {
1959 if (base
->is_array()) {
1961 /* From page 19 (page 25) of the GLSL 1.20 spec:
1963 * "Only one-dimensional arrays may be declared."
1965 if (!state
->ARB_arrays_of_arrays_enable
) {
1966 _mesa_glsl_error(loc
, state
,
1967 "invalid array of `%s'"
1968 "GL_ARB_arrays_of_arrays "
1969 "required for defining arrays of arrays",
1971 return glsl_type::error_type
;
1974 if (base
->length
== 0) {
1975 _mesa_glsl_error(loc
, state
,
1976 "only the outermost array dimension can "
1979 return glsl_type::error_type
;
1983 for (exec_node
*node
= array_specifier
->array_dimensions
.tail_pred
;
1984 !node
->is_head_sentinel(); node
= node
->prev
) {
1985 unsigned array_size
= process_array_size(node
, state
);
1986 array_type
= glsl_type::get_array_instance(array_type
, array_size
);
1989 if (array_specifier
->is_unsized_array
)
1990 array_type
= glsl_type::get_array_instance(array_type
, 0);
1998 ast_type_specifier::glsl_type(const char **name
,
1999 struct _mesa_glsl_parse_state
*state
) const
2001 const struct glsl_type
*type
;
2003 type
= state
->symbols
->get_type(this->type_name
);
2004 *name
= this->type_name
;
2006 YYLTYPE loc
= this->get_location();
2007 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
2013 ast_fully_specified_type::glsl_type(const char **name
,
2014 struct _mesa_glsl_parse_state
*state
) const
2016 const struct glsl_type
*type
= this->specifier
->glsl_type(name
, state
);
2021 if (type
->base_type
== GLSL_TYPE_FLOAT
2023 && state
->stage
== MESA_SHADER_FRAGMENT
2024 && this->qualifier
.precision
== ast_precision_none
2025 && state
->symbols
->get_variable("#default precision") == NULL
) {
2026 YYLTYPE loc
= this->get_location();
2027 _mesa_glsl_error(&loc
, state
,
2028 "no precision specified this scope for type `%s'",
2036 * Determine whether a toplevel variable declaration declares a varying. This
2037 * function operates by examining the variable's mode and the shader target,
2038 * so it correctly identifies linkage variables regardless of whether they are
2039 * declared using the deprecated "varying" syntax or the new "in/out" syntax.
2041 * Passing a non-toplevel variable declaration (e.g. a function parameter) to
2042 * this function will produce undefined results.
2045 is_varying_var(ir_variable
*var
, gl_shader_stage target
)
2048 case MESA_SHADER_VERTEX
:
2049 return var
->data
.mode
== ir_var_shader_out
;
2050 case MESA_SHADER_FRAGMENT
:
2051 return var
->data
.mode
== ir_var_shader_in
;
2053 return var
->data
.mode
== ir_var_shader_out
|| var
->data
.mode
== ir_var_shader_in
;
2059 * Matrix layout qualifiers are only allowed on certain types
2062 validate_matrix_layout_for_type(struct _mesa_glsl_parse_state
*state
,
2064 const glsl_type
*type
,
2067 if (var
&& !var
->is_in_buffer_block()) {
2068 /* Layout qualifiers may only apply to interface blocks and fields in
2071 _mesa_glsl_error(loc
, state
,
2072 "uniform block layout qualifiers row_major and "
2073 "column_major may not be applied to variables "
2074 "outside of uniform blocks");
2075 } else if (!type
->is_matrix()) {
2076 /* The OpenGL ES 3.0 conformance tests did not originally allow
2077 * matrix layout qualifiers on non-matrices. However, the OpenGL
2078 * 4.4 and OpenGL ES 3.0 (revision TBD) specifications were
2079 * amended to specifically allow these layouts on all types. Emit
2080 * a warning so that people know their code may not be portable.
2082 _mesa_glsl_warning(loc
, state
,
2083 "uniform block layout qualifiers row_major and "
2084 "column_major applied to non-matrix types may "
2085 "be rejected by older compilers");
2086 } else if (type
->is_record()) {
2087 /* We allow 'layout(row_major)' on structure types because it's the only
2088 * way to get row-major layouts on matrices contained in structures.
2090 _mesa_glsl_warning(loc
, state
,
2091 "uniform block layout qualifiers row_major and "
2092 "column_major applied to structure types is not "
2093 "strictly conformant and may be rejected by other "
2099 validate_binding_qualifier(struct _mesa_glsl_parse_state
*state
,
2102 const ast_type_qualifier
*qual
)
2104 if (var
->data
.mode
!= ir_var_uniform
&& var
->data
.mode
!= ir_var_shader_storage
) {
2105 _mesa_glsl_error(loc
, state
,
2106 "the \"binding\" qualifier only applies to uniforms and "
2107 "shader storage buffer objects");
2111 if (qual
->binding
< 0) {
2112 _mesa_glsl_error(loc
, state
, "binding values must be >= 0");
2116 const struct gl_context
*const ctx
= state
->ctx
;
2117 unsigned elements
= var
->type
->is_array() ? var
->type
->length
: 1;
2118 unsigned max_index
= qual
->binding
+ elements
- 1;
2120 if (var
->type
->is_interface()) {
2121 /* UBOs. From page 60 of the GLSL 4.20 specification:
2122 * "If the binding point for any uniform block instance is less than zero,
2123 * or greater than or equal to the implementation-dependent maximum
2124 * number of uniform buffer bindings, a compilation error will occur.
2125 * When the binding identifier is used with a uniform block instanced as
2126 * an array of size N, all elements of the array from binding through
2127 * binding + N – 1 must be within this range."
2129 * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
2131 if (var
->data
.mode
== ir_var_uniform
&&
2132 max_index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2133 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d UBOs exceeds "
2134 "the maximum number of UBO binding points (%d)",
2135 qual
->binding
, elements
,
2136 ctx
->Const
.MaxUniformBufferBindings
);
2139 /* SSBOs. From page 67 of the GLSL 4.30 specification:
2140 * "If the binding point for any uniform or shader storage block instance
2141 * is less than zero, or greater than or equal to the
2142 * implementation-dependent maximum number of uniform buffer bindings, a
2143 * compile-time error will occur. When the binding identifier is used
2144 * with a uniform or shader storage block instanced as an array of size
2145 * N, all elements of the array from binding through binding + N – 1 must
2146 * be within this range."
2148 if (var
->data
.mode
== ir_var_shader_storage
&&
2149 max_index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2150 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d SSBOs exceeds "
2151 "the maximum number of SSBO binding points (%d)",
2152 qual
->binding
, elements
,
2153 ctx
->Const
.MaxShaderStorageBufferBindings
);
2156 } else if (var
->type
->is_sampler() ||
2157 (var
->type
->is_array() && var
->type
->fields
.array
->is_sampler())) {
2158 /* Samplers. From page 63 of the GLSL 4.20 specification:
2159 * "If the binding is less than zero, or greater than or equal to the
2160 * implementation-dependent maximum supported number of units, a
2161 * compilation error will occur. When the binding identifier is used
2162 * with an array of size N, all elements of the array from binding
2163 * through binding + N - 1 must be within this range."
2165 unsigned limit
= ctx
->Const
.MaxCombinedTextureImageUnits
;
2167 if (max_index
>= limit
) {
2168 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d samplers "
2169 "exceeds the maximum number of texture image units "
2170 "(%d)", qual
->binding
, elements
, limit
);
2174 } else if (var
->type
->contains_atomic()) {
2175 assert(ctx
->Const
.MaxAtomicBufferBindings
<= MAX_COMBINED_ATOMIC_BUFFERS
);
2176 if (unsigned(qual
->binding
) >= ctx
->Const
.MaxAtomicBufferBindings
) {
2177 _mesa_glsl_error(loc
, state
, "layout(binding = %d) exceeds the "
2178 " maximum number of atomic counter buffer bindings"
2179 "(%d)", qual
->binding
,
2180 ctx
->Const
.MaxAtomicBufferBindings
);
2185 _mesa_glsl_error(loc
, state
,
2186 "the \"binding\" qualifier only applies to uniform "
2187 "blocks, samplers, atomic counters, or arrays thereof");
2195 static glsl_interp_qualifier
2196 interpret_interpolation_qualifier(const struct ast_type_qualifier
*qual
,
2197 ir_variable_mode mode
,
2198 struct _mesa_glsl_parse_state
*state
,
2201 glsl_interp_qualifier interpolation
;
2202 if (qual
->flags
.q
.flat
)
2203 interpolation
= INTERP_QUALIFIER_FLAT
;
2204 else if (qual
->flags
.q
.noperspective
)
2205 interpolation
= INTERP_QUALIFIER_NOPERSPECTIVE
;
2206 else if (qual
->flags
.q
.smooth
)
2207 interpolation
= INTERP_QUALIFIER_SMOOTH
;
2209 interpolation
= INTERP_QUALIFIER_NONE
;
2211 if (interpolation
!= INTERP_QUALIFIER_NONE
) {
2212 if (mode
!= ir_var_shader_in
&& mode
!= ir_var_shader_out
) {
2213 _mesa_glsl_error(loc
, state
,
2214 "interpolation qualifier `%s' can only be applied to "
2215 "shader inputs or outputs.",
2216 interpolation_string(interpolation
));
2220 if ((state
->stage
== MESA_SHADER_VERTEX
&& mode
== ir_var_shader_in
) ||
2221 (state
->stage
== MESA_SHADER_FRAGMENT
&& mode
== ir_var_shader_out
)) {
2222 _mesa_glsl_error(loc
, state
,
2223 "interpolation qualifier `%s' cannot be applied to "
2224 "vertex shader inputs or fragment shader outputs",
2225 interpolation_string(interpolation
));
2229 return interpolation
;
2234 validate_explicit_location(const struct ast_type_qualifier
*qual
,
2236 struct _mesa_glsl_parse_state
*state
,
2241 /* Checks for GL_ARB_explicit_uniform_location. */
2242 if (qual
->flags
.q
.uniform
) {
2243 if (!state
->check_explicit_uniform_location_allowed(loc
, var
))
2246 const struct gl_context
*const ctx
= state
->ctx
;
2247 unsigned max_loc
= qual
->location
+ var
->type
->uniform_locations() - 1;
2249 /* ARB_explicit_uniform_location specification states:
2251 * "The explicitly defined locations and the generated locations
2252 * must be in the range of 0 to MAX_UNIFORM_LOCATIONS minus one."
2254 * "Valid locations for default-block uniform variable locations
2255 * are in the range of 0 to the implementation-defined maximum
2256 * number of uniform locations."
2258 if (qual
->location
< 0) {
2259 _mesa_glsl_error(loc
, state
,
2260 "explicit location < 0 for uniform %s", var
->name
);
2264 if (max_loc
>= ctx
->Const
.MaxUserAssignableUniformLocations
) {
2265 _mesa_glsl_error(loc
, state
, "location(s) consumed by uniform %s "
2266 ">= MAX_UNIFORM_LOCATIONS (%u)", var
->name
,
2267 ctx
->Const
.MaxUserAssignableUniformLocations
);
2271 var
->data
.explicit_location
= true;
2272 var
->data
.location
= qual
->location
;
2276 /* Between GL_ARB_explicit_attrib_location an
2277 * GL_ARB_separate_shader_objects, the inputs and outputs of any shader
2278 * stage can be assigned explicit locations. The checking here associates
2279 * the correct extension with the correct stage's input / output:
2283 * vertex explicit_loc sso
2284 * tess control sso sso
2287 * fragment sso explicit_loc
2289 switch (state
->stage
) {
2290 case MESA_SHADER_VERTEX
:
2291 if (var
->data
.mode
== ir_var_shader_in
) {
2292 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
2298 if (var
->data
.mode
== ir_var_shader_out
) {
2299 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
2308 case MESA_SHADER_TESS_CTRL
:
2309 case MESA_SHADER_TESS_EVAL
:
2310 case MESA_SHADER_GEOMETRY
:
2311 if (var
->data
.mode
== ir_var_shader_in
|| var
->data
.mode
== ir_var_shader_out
) {
2312 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
2321 case MESA_SHADER_FRAGMENT
:
2322 if (var
->data
.mode
== ir_var_shader_in
) {
2323 if (!state
->check_separate_shader_objects_allowed(loc
, var
))
2329 if (var
->data
.mode
== ir_var_shader_out
) {
2330 if (!state
->check_explicit_attrib_location_allowed(loc
, var
))
2339 case MESA_SHADER_COMPUTE
:
2340 _mesa_glsl_error(loc
, state
,
2341 "compute shader variables cannot be given "
2342 "explicit locations");
2347 _mesa_glsl_error(loc
, state
,
2348 "%s cannot be given an explicit location in %s shader",
2350 _mesa_shader_stage_to_string(state
->stage
));
2352 var
->data
.explicit_location
= true;
2354 /* This bit of silliness is needed because invalid explicit locations
2355 * are supposed to be flagged during linking. Small negative values
2356 * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
2357 * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
2358 * The linker needs to be able to differentiate these cases. This
2359 * ensures that negative values stay negative.
2361 if (qual
->location
>= 0) {
2362 switch (state
->stage
) {
2363 case MESA_SHADER_VERTEX
:
2364 var
->data
.location
= (var
->data
.mode
== ir_var_shader_in
)
2365 ? (qual
->location
+ VERT_ATTRIB_GENERIC0
)
2366 : (qual
->location
+ VARYING_SLOT_VAR0
);
2369 case MESA_SHADER_TESS_CTRL
:
2370 case MESA_SHADER_TESS_EVAL
:
2371 case MESA_SHADER_GEOMETRY
:
2372 if (var
->data
.patch
)
2373 var
->data
.location
= qual
->location
+ VARYING_SLOT_PATCH0
;
2375 var
->data
.location
= qual
->location
+ VARYING_SLOT_VAR0
;
2378 case MESA_SHADER_FRAGMENT
:
2379 var
->data
.location
= (var
->data
.mode
== ir_var_shader_out
)
2380 ? (qual
->location
+ FRAG_RESULT_DATA0
)
2381 : (qual
->location
+ VARYING_SLOT_VAR0
);
2383 case MESA_SHADER_COMPUTE
:
2384 assert(!"Unexpected shader type");
2388 var
->data
.location
= qual
->location
;
2391 if (qual
->flags
.q
.explicit_index
) {
2392 /* From the GLSL 4.30 specification, section 4.4.2 (Output
2393 * Layout Qualifiers):
2395 * "It is also a compile-time error if a fragment shader
2396 * sets a layout index to less than 0 or greater than 1."
2398 * Older specifications don't mandate a behavior; we take
2399 * this as a clarification and always generate the error.
2401 if (qual
->index
< 0 || qual
->index
> 1) {
2402 _mesa_glsl_error(loc
, state
,
2403 "explicit index may only be 0 or 1");
2405 var
->data
.explicit_index
= true;
2406 var
->data
.index
= qual
->index
;
2413 apply_image_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
2415 struct _mesa_glsl_parse_state
*state
,
2418 const glsl_type
*base_type
= var
->type
->without_array();
2420 if (base_type
->is_image()) {
2421 if (var
->data
.mode
!= ir_var_uniform
&&
2422 var
->data
.mode
!= ir_var_function_in
) {
2423 _mesa_glsl_error(loc
, state
, "image variables may only be declared as "
2424 "function parameters or uniform-qualified "
2425 "global variables");
2428 var
->data
.image_read_only
|= qual
->flags
.q
.read_only
;
2429 var
->data
.image_write_only
|= qual
->flags
.q
.write_only
;
2430 var
->data
.image_coherent
|= qual
->flags
.q
.coherent
;
2431 var
->data
.image_volatile
|= qual
->flags
.q
._volatile
;
2432 var
->data
.image_restrict
|= qual
->flags
.q
.restrict_flag
;
2433 var
->data
.read_only
= true;
2435 if (qual
->flags
.q
.explicit_image_format
) {
2436 if (var
->data
.mode
== ir_var_function_in
) {
2437 _mesa_glsl_error(loc
, state
, "format qualifiers cannot be "
2438 "used on image function parameters");
2441 if (qual
->image_base_type
!= base_type
->sampler_type
) {
2442 _mesa_glsl_error(loc
, state
, "format qualifier doesn't match the "
2443 "base data type of the image");
2446 var
->data
.image_format
= qual
->image_format
;
2448 if (var
->data
.mode
== ir_var_uniform
&& !qual
->flags
.q
.write_only
) {
2449 _mesa_glsl_error(loc
, state
, "uniforms not qualified with "
2450 "`writeonly' must have a format layout "
2454 var
->data
.image_format
= GL_NONE
;
2456 } else if (qual
->flags
.q
.read_only
||
2457 qual
->flags
.q
.write_only
||
2458 qual
->flags
.q
.coherent
||
2459 qual
->flags
.q
._volatile
||
2460 qual
->flags
.q
.restrict_flag
||
2461 qual
->flags
.q
.explicit_image_format
) {
2462 _mesa_glsl_error(loc
, state
, "memory qualifiers may only be applied to "
2467 static inline const char*
2468 get_layout_qualifier_string(bool origin_upper_left
, bool pixel_center_integer
)
2470 if (origin_upper_left
&& pixel_center_integer
)
2471 return "origin_upper_left, pixel_center_integer";
2472 else if (origin_upper_left
)
2473 return "origin_upper_left";
2474 else if (pixel_center_integer
)
2475 return "pixel_center_integer";
2481 is_conflicting_fragcoord_redeclaration(struct _mesa_glsl_parse_state
*state
,
2482 const struct ast_type_qualifier
*qual
)
2484 /* If gl_FragCoord was previously declared, and the qualifiers were
2485 * different in any way, return true.
2487 if (state
->fs_redeclares_gl_fragcoord
) {
2488 return (state
->fs_pixel_center_integer
!= qual
->flags
.q
.pixel_center_integer
2489 || state
->fs_origin_upper_left
!= qual
->flags
.q
.origin_upper_left
);
2496 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
2498 struct _mesa_glsl_parse_state
*state
,
2502 STATIC_ASSERT(sizeof(qual
->flags
.q
) <= sizeof(qual
->flags
.i
));
2504 if (qual
->flags
.q
.invariant
) {
2505 if (var
->data
.used
) {
2506 _mesa_glsl_error(loc
, state
,
2507 "variable `%s' may not be redeclared "
2508 "`invariant' after being used",
2511 var
->data
.invariant
= 1;
2515 if (qual
->flags
.q
.precise
) {
2516 if (var
->data
.used
) {
2517 _mesa_glsl_error(loc
, state
,
2518 "variable `%s' may not be redeclared "
2519 "`precise' after being used",
2522 var
->data
.precise
= 1;
2526 if (qual
->flags
.q
.subroutine
&& !qual
->flags
.q
.uniform
) {
2527 _mesa_glsl_error(loc
, state
,
2528 "`subroutine' may only be applied to uniforms, "
2529 "subroutine type declarations, or function definitions");
2532 if (qual
->flags
.q
.constant
|| qual
->flags
.q
.attribute
2533 || qual
->flags
.q
.uniform
2534 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
2535 var
->data
.read_only
= 1;
2537 if (qual
->flags
.q
.centroid
)
2538 var
->data
.centroid
= 1;
2540 if (qual
->flags
.q
.sample
)
2541 var
->data
.sample
= 1;
2543 if (state
->stage
== MESA_SHADER_GEOMETRY
&&
2544 qual
->flags
.q
.out
&& qual
->flags
.q
.stream
) {
2545 var
->data
.stream
= qual
->stream
;
2548 if (qual
->flags
.q
.patch
)
2549 var
->data
.patch
= 1;
2551 if (qual
->flags
.q
.attribute
&& state
->stage
!= MESA_SHADER_VERTEX
) {
2552 var
->type
= glsl_type::error_type
;
2553 _mesa_glsl_error(loc
, state
,
2554 "`attribute' variables may not be declared in the "
2556 _mesa_shader_stage_to_string(state
->stage
));
2559 /* Disallow layout qualifiers which may only appear on layout declarations. */
2560 if (qual
->flags
.q
.prim_type
) {
2561 _mesa_glsl_error(loc
, state
,
2562 "Primitive type may only be specified on GS input or output "
2563 "layout declaration, not on variables.");
2566 /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
2568 * "However, the const qualifier cannot be used with out or inout."
2570 * The same section of the GLSL 4.40 spec further clarifies this saying:
2572 * "The const qualifier cannot be used with out or inout, or a
2573 * compile-time error results."
2575 if (is_parameter
&& qual
->flags
.q
.constant
&& qual
->flags
.q
.out
) {
2576 _mesa_glsl_error(loc
, state
,
2577 "`const' may not be applied to `out' or `inout' "
2578 "function parameters");
2581 /* If there is no qualifier that changes the mode of the variable, leave
2582 * the setting alone.
2584 assert(var
->data
.mode
!= ir_var_temporary
);
2585 if (qual
->flags
.q
.in
&& qual
->flags
.q
.out
)
2586 var
->data
.mode
= ir_var_function_inout
;
2587 else if (qual
->flags
.q
.in
)
2588 var
->data
.mode
= is_parameter
? ir_var_function_in
: ir_var_shader_in
;
2589 else if (qual
->flags
.q
.attribute
2590 || (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_FRAGMENT
)))
2591 var
->data
.mode
= ir_var_shader_in
;
2592 else if (qual
->flags
.q
.out
)
2593 var
->data
.mode
= is_parameter
? ir_var_function_out
: ir_var_shader_out
;
2594 else if (qual
->flags
.q
.varying
&& (state
->stage
== MESA_SHADER_VERTEX
))
2595 var
->data
.mode
= ir_var_shader_out
;
2596 else if (qual
->flags
.q
.uniform
)
2597 var
->data
.mode
= ir_var_uniform
;
2598 else if (qual
->flags
.q
.buffer
)
2599 var
->data
.mode
= ir_var_shader_storage
;
2601 if (!is_parameter
&& is_varying_var(var
, state
->stage
)) {
2602 /* User-defined ins/outs are not permitted in compute shaders. */
2603 if (state
->stage
== MESA_SHADER_COMPUTE
) {
2604 _mesa_glsl_error(loc
, state
,
2605 "user-defined input and output variables are not "
2606 "permitted in compute shaders");
2609 /* This variable is being used to link data between shader stages (in
2610 * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
2611 * that is allowed for such purposes.
2613 * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
2615 * "The varying qualifier can be used only with the data types
2616 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
2619 * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
2620 * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
2622 * "Fragment inputs can only be signed and unsigned integers and
2623 * integer vectors, float, floating-point vectors, matrices, or
2624 * arrays of these. Structures cannot be input.
2626 * Similar text exists in the section on vertex shader outputs.
2628 * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
2629 * 3.00 spec allows structs as well. Varying structs are also allowed
2632 switch (var
->type
->get_scalar_type()->base_type
) {
2633 case GLSL_TYPE_FLOAT
:
2634 /* Ok in all GLSL versions */
2636 case GLSL_TYPE_UINT
:
2638 if (state
->is_version(130, 300))
2640 _mesa_glsl_error(loc
, state
,
2641 "varying variables must be of base type float in %s",
2642 state
->get_version_string());
2644 case GLSL_TYPE_STRUCT
:
2645 if (state
->is_version(150, 300))
2647 _mesa_glsl_error(loc
, state
,
2648 "varying variables may not be of type struct");
2650 case GLSL_TYPE_DOUBLE
:
2653 _mesa_glsl_error(loc
, state
, "illegal type for a varying variable");
2658 if (state
->all_invariant
&& (state
->current_function
== NULL
)) {
2659 switch (state
->stage
) {
2660 case MESA_SHADER_VERTEX
:
2661 if (var
->data
.mode
== ir_var_shader_out
)
2662 var
->data
.invariant
= true;
2664 case MESA_SHADER_TESS_CTRL
:
2665 case MESA_SHADER_TESS_EVAL
:
2666 case MESA_SHADER_GEOMETRY
:
2667 if ((var
->data
.mode
== ir_var_shader_in
)
2668 || (var
->data
.mode
== ir_var_shader_out
))
2669 var
->data
.invariant
= true;
2671 case MESA_SHADER_FRAGMENT
:
2672 if (var
->data
.mode
== ir_var_shader_in
)
2673 var
->data
.invariant
= true;
2675 case MESA_SHADER_COMPUTE
:
2676 /* Invariance isn't meaningful in compute shaders. */
2681 var
->data
.interpolation
=
2682 interpret_interpolation_qualifier(qual
, (ir_variable_mode
) var
->data
.mode
,
2685 var
->data
.pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
2686 var
->data
.origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
2687 if ((qual
->flags
.q
.origin_upper_left
|| qual
->flags
.q
.pixel_center_integer
)
2688 && (strcmp(var
->name
, "gl_FragCoord") != 0)) {
2689 const char *const qual_string
= (qual
->flags
.q
.origin_upper_left
)
2690 ? "origin_upper_left" : "pixel_center_integer";
2692 _mesa_glsl_error(loc
, state
,
2693 "layout qualifier `%s' can only be applied to "
2694 "fragment shader input `gl_FragCoord'",
2698 if (var
->name
!= NULL
&& strcmp(var
->name
, "gl_FragCoord") == 0) {
2700 /* Section 4.3.8.1, page 39 of GLSL 1.50 spec says:
2702 * "Within any shader, the first redeclarations of gl_FragCoord
2703 * must appear before any use of gl_FragCoord."
2705 * Generate a compiler error if above condition is not met by the
2708 ir_variable
*earlier
= state
->symbols
->get_variable("gl_FragCoord");
2709 if (earlier
!= NULL
&&
2710 earlier
->data
.used
&&
2711 !state
->fs_redeclares_gl_fragcoord
) {
2712 _mesa_glsl_error(loc
, state
,
2713 "gl_FragCoord used before its first redeclaration "
2714 "in fragment shader");
2717 /* Make sure all gl_FragCoord redeclarations specify the same layout
2720 if (is_conflicting_fragcoord_redeclaration(state
, qual
)) {
2721 const char *const qual_string
=
2722 get_layout_qualifier_string(qual
->flags
.q
.origin_upper_left
,
2723 qual
->flags
.q
.pixel_center_integer
);
2725 const char *const state_string
=
2726 get_layout_qualifier_string(state
->fs_origin_upper_left
,
2727 state
->fs_pixel_center_integer
);
2729 _mesa_glsl_error(loc
, state
,
2730 "gl_FragCoord redeclared with different layout "
2731 "qualifiers (%s) and (%s) ",
2735 state
->fs_origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
2736 state
->fs_pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
2737 state
->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers
=
2738 !qual
->flags
.q
.origin_upper_left
&& !qual
->flags
.q
.pixel_center_integer
;
2739 state
->fs_redeclares_gl_fragcoord
=
2740 state
->fs_origin_upper_left
||
2741 state
->fs_pixel_center_integer
||
2742 state
->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers
;
2745 if (qual
->flags
.q
.explicit_location
) {
2746 validate_explicit_location(qual
, var
, state
, loc
);
2747 } else if (qual
->flags
.q
.explicit_index
) {
2748 _mesa_glsl_error(loc
, state
, "explicit index requires explicit location");
2751 if (qual
->flags
.q
.explicit_binding
&&
2752 validate_binding_qualifier(state
, loc
, var
, qual
)) {
2753 var
->data
.explicit_binding
= true;
2754 var
->data
.binding
= qual
->binding
;
2757 if (var
->type
->contains_atomic()) {
2758 if (var
->data
.mode
== ir_var_uniform
) {
2759 if (var
->data
.explicit_binding
) {
2761 &state
->atomic_counter_offsets
[var
->data
.binding
];
2763 if (*offset
% ATOMIC_COUNTER_SIZE
)
2764 _mesa_glsl_error(loc
, state
,
2765 "misaligned atomic counter offset");
2767 var
->data
.atomic
.offset
= *offset
;
2768 *offset
+= var
->type
->atomic_size();
2771 _mesa_glsl_error(loc
, state
,
2772 "atomic counters require explicit binding point");
2774 } else if (var
->data
.mode
!= ir_var_function_in
) {
2775 _mesa_glsl_error(loc
, state
, "atomic counters may only be declared as "
2776 "function parameters or uniform-qualified "
2777 "global variables");
2781 /* Does the declaration use the deprecated 'attribute' or 'varying'
2784 const bool uses_deprecated_qualifier
= qual
->flags
.q
.attribute
2785 || qual
->flags
.q
.varying
;
2788 /* Validate auxiliary storage qualifiers */
2790 /* From section 4.3.4 of the GLSL 1.30 spec:
2791 * "It is an error to use centroid in in a vertex shader."
2793 * From section 4.3.4 of the GLSL ES 3.00 spec:
2794 * "It is an error to use centroid in or interpolation qualifiers in
2795 * a vertex shader input."
2798 /* Section 4.3.6 of the GLSL 1.30 specification states:
2799 * "It is an error to use centroid out in a fragment shader."
2801 * The GL_ARB_shading_language_420pack extension specification states:
2802 * "It is an error to use auxiliary storage qualifiers or interpolation
2803 * qualifiers on an output in a fragment shader."
2805 if (qual
->flags
.q
.sample
&& (!is_varying_var(var
, state
->stage
) || uses_deprecated_qualifier
)) {
2806 _mesa_glsl_error(loc
, state
,
2807 "sample qualifier may only be used on `in` or `out` "
2808 "variables between shader stages");
2810 if (qual
->flags
.q
.centroid
&& !is_varying_var(var
, state
->stage
)) {
2811 _mesa_glsl_error(loc
, state
,
2812 "centroid qualifier may only be used with `in', "
2813 "`out' or `varying' variables between shader stages");
2817 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
2818 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
2819 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
2820 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
2821 * These extensions and all following extensions that add the 'layout'
2822 * keyword have been modified to require the use of 'in' or 'out'.
2824 * The following extension do not allow the deprecated keywords:
2826 * GL_AMD_conservative_depth
2827 * GL_ARB_conservative_depth
2828 * GL_ARB_gpu_shader5
2829 * GL_ARB_separate_shader_objects
2830 * GL_ARB_tessellation_shader
2831 * GL_ARB_transform_feedback3
2832 * GL_ARB_uniform_buffer_object
2834 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
2835 * allow layout with the deprecated keywords.
2837 const bool relaxed_layout_qualifier_checking
=
2838 state
->ARB_fragment_coord_conventions_enable
;
2840 if (qual
->has_layout() && uses_deprecated_qualifier
) {
2841 if (relaxed_layout_qualifier_checking
) {
2842 _mesa_glsl_warning(loc
, state
,
2843 "`layout' qualifier may not be used with "
2844 "`attribute' or `varying'");
2846 _mesa_glsl_error(loc
, state
,
2847 "`layout' qualifier may not be used with "
2848 "`attribute' or `varying'");
2852 /* Layout qualifiers for gl_FragDepth, which are enabled by extension
2853 * AMD_conservative_depth.
2855 int depth_layout_count
= qual
->flags
.q
.depth_any
2856 + qual
->flags
.q
.depth_greater
2857 + qual
->flags
.q
.depth_less
2858 + qual
->flags
.q
.depth_unchanged
;
2859 if (depth_layout_count
> 0
2860 && !state
->AMD_conservative_depth_enable
2861 && !state
->ARB_conservative_depth_enable
) {
2862 _mesa_glsl_error(loc
, state
,
2863 "extension GL_AMD_conservative_depth or "
2864 "GL_ARB_conservative_depth must be enabled "
2865 "to use depth layout qualifiers");
2866 } else if (depth_layout_count
> 0
2867 && strcmp(var
->name
, "gl_FragDepth") != 0) {
2868 _mesa_glsl_error(loc
, state
,
2869 "depth layout qualifiers can be applied only to "
2871 } else if (depth_layout_count
> 1
2872 && strcmp(var
->name
, "gl_FragDepth") == 0) {
2873 _mesa_glsl_error(loc
, state
,
2874 "at most one depth layout qualifier can be applied to "
2877 if (qual
->flags
.q
.depth_any
)
2878 var
->data
.depth_layout
= ir_depth_layout_any
;
2879 else if (qual
->flags
.q
.depth_greater
)
2880 var
->data
.depth_layout
= ir_depth_layout_greater
;
2881 else if (qual
->flags
.q
.depth_less
)
2882 var
->data
.depth_layout
= ir_depth_layout_less
;
2883 else if (qual
->flags
.q
.depth_unchanged
)
2884 var
->data
.depth_layout
= ir_depth_layout_unchanged
;
2886 var
->data
.depth_layout
= ir_depth_layout_none
;
2888 if (qual
->flags
.q
.std140
||
2889 qual
->flags
.q
.packed
||
2890 qual
->flags
.q
.shared
) {
2891 _mesa_glsl_error(loc
, state
,
2892 "uniform block layout qualifiers std140, packed, and "
2893 "shared can only be applied to uniform blocks, not "
2897 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
2898 validate_matrix_layout_for_type(state
, loc
, var
->type
, var
);
2901 apply_image_qualifier_to_variable(qual
, var
, state
, loc
);
2903 /* From section 4.4.1.3 of the GLSL 4.50 specification (Fragment Shader
2906 * "Fragment shaders also allow the following layout qualifier on in only
2907 * (not with variable declarations)
2908 * layout-qualifier-id
2909 * early_fragment_tests
2912 if (qual
->flags
.q
.early_fragment_tests
) {
2913 _mesa_glsl_error(loc
, state
, "early_fragment_tests layout qualifier only "
2914 "valid in fragment shader input layout declaration.");
2919 * Get the variable that is being redeclared by this declaration
2921 * Semantic checks to verify the validity of the redeclaration are also
2922 * performed. If semantic checks fail, compilation error will be emitted via
2923 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
2926 * A pointer to an existing variable in the current scope if the declaration
2927 * is a redeclaration, \c NULL otherwise.
2929 static ir_variable
*
2930 get_variable_being_redeclared(ir_variable
*var
, YYLTYPE loc
,
2931 struct _mesa_glsl_parse_state
*state
,
2932 bool allow_all_redeclarations
)
2934 /* Check if this declaration is actually a re-declaration, either to
2935 * resize an array or add qualifiers to an existing variable.
2937 * This is allowed for variables in the current scope, or when at
2938 * global scope (for built-ins in the implicit outer scope).
2940 ir_variable
*earlier
= state
->symbols
->get_variable(var
->name
);
2941 if (earlier
== NULL
||
2942 (state
->current_function
!= NULL
&&
2943 !state
->symbols
->name_declared_this_scope(var
->name
))) {
2948 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
2950 * "It is legal to declare an array without a size and then
2951 * later re-declare the same name as an array of the same
2952 * type and specify a size."
2954 if (earlier
->type
->is_unsized_array() && var
->type
->is_array()
2955 && (var
->type
->fields
.array
== earlier
->type
->fields
.array
)) {
2956 /* FINISHME: This doesn't match the qualifiers on the two
2957 * FINISHME: declarations. It's not 100% clear whether this is
2958 * FINISHME: required or not.
2961 const unsigned size
= unsigned(var
->type
->array_size());
2962 check_builtin_array_max_size(var
->name
, size
, loc
, state
);
2963 if ((size
> 0) && (size
<= earlier
->data
.max_array_access
)) {
2964 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
2966 earlier
->data
.max_array_access
);
2969 earlier
->type
= var
->type
;
2972 } else if ((state
->ARB_fragment_coord_conventions_enable
||
2973 state
->is_version(150, 0))
2974 && strcmp(var
->name
, "gl_FragCoord") == 0
2975 && earlier
->type
== var
->type
2976 && earlier
->data
.mode
== var
->data
.mode
) {
2977 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
2980 earlier
->data
.origin_upper_left
= var
->data
.origin_upper_left
;
2981 earlier
->data
.pixel_center_integer
= var
->data
.pixel_center_integer
;
2983 /* According to section 4.3.7 of the GLSL 1.30 spec,
2984 * the following built-in varaibles can be redeclared with an
2985 * interpolation qualifier:
2988 * * gl_FrontSecondaryColor
2989 * * gl_BackSecondaryColor
2991 * * gl_SecondaryColor
2993 } else if (state
->is_version(130, 0)
2994 && (strcmp(var
->name
, "gl_FrontColor") == 0
2995 || strcmp(var
->name
, "gl_BackColor") == 0
2996 || strcmp(var
->name
, "gl_FrontSecondaryColor") == 0
2997 || strcmp(var
->name
, "gl_BackSecondaryColor") == 0
2998 || strcmp(var
->name
, "gl_Color") == 0
2999 || strcmp(var
->name
, "gl_SecondaryColor") == 0)
3000 && earlier
->type
== var
->type
3001 && earlier
->data
.mode
== var
->data
.mode
) {
3002 earlier
->data
.interpolation
= var
->data
.interpolation
;
3004 /* Layout qualifiers for gl_FragDepth. */
3005 } else if ((state
->AMD_conservative_depth_enable
||
3006 state
->ARB_conservative_depth_enable
)
3007 && strcmp(var
->name
, "gl_FragDepth") == 0
3008 && earlier
->type
== var
->type
3009 && earlier
->data
.mode
== var
->data
.mode
) {
3011 /** From the AMD_conservative_depth spec:
3012 * Within any shader, the first redeclarations of gl_FragDepth
3013 * must appear before any use of gl_FragDepth.
3015 if (earlier
->data
.used
) {
3016 _mesa_glsl_error(&loc
, state
,
3017 "the first redeclaration of gl_FragDepth "
3018 "must appear before any use of gl_FragDepth");
3021 /* Prevent inconsistent redeclaration of depth layout qualifier. */
3022 if (earlier
->data
.depth_layout
!= ir_depth_layout_none
3023 && earlier
->data
.depth_layout
!= var
->data
.depth_layout
) {
3024 _mesa_glsl_error(&loc
, state
,
3025 "gl_FragDepth: depth layout is declared here "
3026 "as '%s, but it was previously declared as "
3028 depth_layout_string(var
->data
.depth_layout
),
3029 depth_layout_string(earlier
->data
.depth_layout
));
3032 earlier
->data
.depth_layout
= var
->data
.depth_layout
;
3034 } else if (allow_all_redeclarations
) {
3035 if (earlier
->data
.mode
!= var
->data
.mode
) {
3036 _mesa_glsl_error(&loc
, state
,
3037 "redeclaration of `%s' with incorrect qualifiers",
3039 } else if (earlier
->type
!= var
->type
) {
3040 _mesa_glsl_error(&loc
, state
,
3041 "redeclaration of `%s' has incorrect type",
3045 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
3052 * Generate the IR for an initializer in a variable declaration
3055 process_initializer(ir_variable
*var
, ast_declaration
*decl
,
3056 ast_fully_specified_type
*type
,
3057 exec_list
*initializer_instructions
,
3058 struct _mesa_glsl_parse_state
*state
)
3060 ir_rvalue
*result
= NULL
;
3062 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
3064 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
3066 * "All uniform variables are read-only and are initialized either
3067 * directly by an application via API commands, or indirectly by
3070 if (var
->data
.mode
== ir_var_uniform
) {
3071 state
->check_version(120, 0, &initializer_loc
,
3072 "cannot initialize uniforms");
3075 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
3077 * "Buffer variables cannot have initializers."
3079 if (var
->data
.mode
== ir_var_shader_storage
) {
3080 _mesa_glsl_error(& initializer_loc
, state
,
3081 "SSBO variables cannot have initializers");
3084 /* From section 4.1.7 of the GLSL 4.40 spec:
3086 * "Opaque variables [...] are initialized only through the
3087 * OpenGL API; they cannot be declared with an initializer in a
3090 if (var
->type
->contains_opaque()) {
3091 _mesa_glsl_error(& initializer_loc
, state
,
3092 "cannot initialize opaque variable");
3095 if ((var
->data
.mode
== ir_var_shader_in
) && (state
->current_function
== NULL
)) {
3096 _mesa_glsl_error(& initializer_loc
, state
,
3097 "cannot initialize %s shader input / %s",
3098 _mesa_shader_stage_to_string(state
->stage
),
3099 (state
->stage
== MESA_SHADER_VERTEX
)
3100 ? "attribute" : "varying");
3103 /* If the initializer is an ast_aggregate_initializer, recursively store
3104 * type information from the LHS into it, so that its hir() function can do
3107 if (decl
->initializer
->oper
== ast_aggregate
)
3108 _mesa_ast_set_aggregate_type(var
->type
, decl
->initializer
);
3110 ir_dereference
*const lhs
= new(state
) ir_dereference_variable(var
);
3111 ir_rvalue
*rhs
= decl
->initializer
->hir(initializer_instructions
, state
);
3113 /* Calculate the constant value if this is a const or uniform
3116 if (type
->qualifier
.flags
.q
.constant
3117 || type
->qualifier
.flags
.q
.uniform
) {
3118 ir_rvalue
*new_rhs
= validate_assignment(state
, initializer_loc
,
3120 if (new_rhs
!= NULL
) {
3123 ir_constant
*constant_value
= rhs
->constant_expression_value();
3124 if (!constant_value
) {
3125 /* If ARB_shading_language_420pack is enabled, initializers of
3126 * const-qualified local variables do not have to be constant
3127 * expressions. Const-qualified global variables must still be
3128 * initialized with constant expressions.
3130 if (!state
->ARB_shading_language_420pack_enable
3131 || state
->current_function
== NULL
) {
3132 _mesa_glsl_error(& initializer_loc
, state
,
3133 "initializer of %s variable `%s' must be a "
3134 "constant expression",
3135 (type
->qualifier
.flags
.q
.constant
)
3136 ? "const" : "uniform",
3138 if (var
->type
->is_numeric()) {
3139 /* Reduce cascading errors. */
3140 var
->constant_value
= ir_constant::zero(state
, var
->type
);
3144 rhs
= constant_value
;
3145 var
->constant_value
= constant_value
;
3148 if (var
->type
->is_numeric()) {
3149 /* Reduce cascading errors. */
3150 var
->constant_value
= ir_constant::zero(state
, var
->type
);
3155 if (rhs
&& !rhs
->type
->is_error()) {
3156 bool temp
= var
->data
.read_only
;
3157 if (type
->qualifier
.flags
.q
.constant
)
3158 var
->data
.read_only
= false;
3160 /* Never emit code to initialize a uniform.
3162 const glsl_type
*initializer_type
;
3163 if (!type
->qualifier
.flags
.q
.uniform
) {
3164 do_assignment(initializer_instructions
, state
,
3169 type
->get_location());
3170 initializer_type
= result
->type
;
3172 initializer_type
= rhs
->type
;
3174 var
->constant_initializer
= rhs
->constant_expression_value();
3175 var
->data
.has_initializer
= true;
3177 /* If the declared variable is an unsized array, it must inherrit
3178 * its full type from the initializer. A declaration such as
3180 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
3184 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
3186 * The assignment generated in the if-statement (below) will also
3187 * automatically handle this case for non-uniforms.
3189 * If the declared variable is not an array, the types must
3190 * already match exactly. As a result, the type assignment
3191 * here can be done unconditionally. For non-uniforms the call
3192 * to do_assignment can change the type of the initializer (via
3193 * the implicit conversion rules). For uniforms the initializer
3194 * must be a constant expression, and the type of that expression
3195 * was validated above.
3197 var
->type
= initializer_type
;
3199 var
->data
.read_only
= temp
;
3206 validate_layout_qualifier_vertex_count(struct _mesa_glsl_parse_state
*state
,
3207 YYLTYPE loc
, ir_variable
*var
,
3208 unsigned num_vertices
,
3210 const char *var_category
)
3212 if (var
->type
->is_unsized_array()) {
3213 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says:
3215 * All geometry shader input unsized array declarations will be
3216 * sized by an earlier input layout qualifier, when present, as per
3217 * the following table.
3219 * Followed by a table mapping each allowed input layout qualifier to
3220 * the corresponding input length.
3222 * Similarly for tessellation control shader outputs.
3224 if (num_vertices
!= 0)
3225 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
3228 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec
3229 * includes the following examples of compile-time errors:
3231 * // code sequence within one shader...
3232 * in vec4 Color1[]; // size unknown
3233 * ...Color1.length()...// illegal, length() unknown
3234 * in vec4 Color2[2]; // size is 2
3235 * ...Color1.length()...// illegal, Color1 still has no size
3236 * in vec4 Color3[3]; // illegal, input sizes are inconsistent
3237 * layout(lines) in; // legal, input size is 2, matching
3238 * in vec4 Color4[3]; // illegal, contradicts layout
3241 * To detect the case illustrated by Color3, we verify that the size of
3242 * an explicitly-sized array matches the size of any previously declared
3243 * explicitly-sized array. To detect the case illustrated by Color4, we
3244 * verify that the size of an explicitly-sized array is consistent with
3245 * any previously declared input layout.
3247 if (num_vertices
!= 0 && var
->type
->length
!= num_vertices
) {
3248 _mesa_glsl_error(&loc
, state
,
3249 "%s size contradicts previously declared layout "
3250 "(size is %u, but layout requires a size of %u)",
3251 var_category
, var
->type
->length
, num_vertices
);
3252 } else if (*size
!= 0 && var
->type
->length
!= *size
) {
3253 _mesa_glsl_error(&loc
, state
,
3254 "%s sizes are inconsistent (size is %u, but a "
3255 "previous declaration has size %u)",
3256 var_category
, var
->type
->length
, *size
);
3258 *size
= var
->type
->length
;
3264 handle_tess_ctrl_shader_output_decl(struct _mesa_glsl_parse_state
*state
,
3265 YYLTYPE loc
, ir_variable
*var
)
3267 unsigned num_vertices
= 0;
3269 if (state
->tcs_output_vertices_specified
) {
3270 num_vertices
= state
->out_qualifier
->vertices
;
3273 if (!var
->type
->is_array() && !var
->data
.patch
) {
3274 _mesa_glsl_error(&loc
, state
,
3275 "tessellation control shader outputs must be arrays");
3277 /* To avoid cascading failures, short circuit the checks below. */
3281 if (var
->data
.patch
)
3284 validate_layout_qualifier_vertex_count(state
, loc
, var
, num_vertices
,
3285 &state
->tcs_output_size
,
3286 "geometry shader input");
3290 * Do additional processing necessary for tessellation control/evaluation shader
3291 * input declarations. This covers both interface block arrays and bare input
3295 handle_tess_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
3296 YYLTYPE loc
, ir_variable
*var
)
3298 if (!var
->type
->is_array() && !var
->data
.patch
) {
3299 _mesa_glsl_error(&loc
, state
,
3300 "per-vertex tessellation shader inputs must be arrays");
3301 /* Avoid cascading failures. */
3305 if (var
->data
.patch
)
3308 /* Unsized arrays are implicitly sized to gl_MaxPatchVertices. */
3309 if (var
->type
->is_unsized_array()) {
3310 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
3311 state
->Const
.MaxPatchVertices
);
3317 * Do additional processing necessary for geometry shader input declarations
3318 * (this covers both interface blocks arrays and bare input variables).
3321 handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
3322 YYLTYPE loc
, ir_variable
*var
)
3324 unsigned num_vertices
= 0;
3326 if (state
->gs_input_prim_type_specified
) {
3327 num_vertices
= vertices_per_prim(state
->in_qualifier
->prim_type
);
3330 /* Geometry shader input variables must be arrays. Caller should have
3331 * reported an error for this.
3333 if (!var
->type
->is_array()) {
3334 assert(state
->error
);
3336 /* To avoid cascading failures, short circuit the checks below. */
3340 validate_layout_qualifier_vertex_count(state
, loc
, var
, num_vertices
,
3341 &state
->gs_input_size
,
3342 "geometry shader input");
3346 validate_identifier(const char *identifier
, YYLTYPE loc
,
3347 struct _mesa_glsl_parse_state
*state
)
3349 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
3351 * "Identifiers starting with "gl_" are reserved for use by
3352 * OpenGL, and may not be declared in a shader as either a
3353 * variable or a function."
3355 if (is_gl_identifier(identifier
)) {
3356 _mesa_glsl_error(&loc
, state
,
3357 "identifier `%s' uses reserved `gl_' prefix",
3359 } else if (strstr(identifier
, "__")) {
3360 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
3363 * "In addition, all identifiers containing two
3364 * consecutive underscores (__) are reserved as
3365 * possible future keywords."
3367 * The intention is that names containing __ are reserved for internal
3368 * use by the implementation, and names prefixed with GL_ are reserved
3369 * for use by Khronos. Names simply containing __ are dangerous to use,
3370 * but should be allowed.
3372 * A future version of the GLSL specification will clarify this.
3374 _mesa_glsl_warning(&loc
, state
,
3375 "identifier `%s' uses reserved `__' string",
3381 precision_qualifier_allowed(const glsl_type
*type
)
3383 /* Precision qualifiers apply to floating point, integer and sampler
3386 * Section 4.5.2 (Precision Qualifiers) of the GLSL 1.30 spec says:
3387 * "Any floating point or any integer declaration can have the type
3388 * preceded by one of these precision qualifiers [...] Literal
3389 * constants do not have precision qualifiers. Neither do Boolean
3392 * Section 4.5 (Precision and Precision Qualifiers) of the GLSL 1.30
3395 * "Precision qualifiers are added for code portability with OpenGL
3396 * ES, not for functionality. They have the same syntax as in OpenGL
3399 * Section 8 (Built-In Functions) of the GLSL ES 1.00 spec says:
3401 * "uniform lowp sampler2D sampler;
3404 * lowp vec4 col = texture2D (sampler, coord);
3405 * // texture2D returns lowp"
3407 * From this, we infer that GLSL 1.30 (and later) should allow precision
3408 * qualifiers on sampler types just like float and integer types.
3410 return type
->is_float()
3411 || type
->is_integer()
3412 || type
->is_record()
3413 || type
->is_sampler();
3417 ast_declarator_list::hir(exec_list
*instructions
,
3418 struct _mesa_glsl_parse_state
*state
)
3421 const struct glsl_type
*decl_type
;
3422 const char *type_name
= NULL
;
3423 ir_rvalue
*result
= NULL
;
3424 YYLTYPE loc
= this->get_location();
3426 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
3428 * "To ensure that a particular output variable is invariant, it is
3429 * necessary to use the invariant qualifier. It can either be used to
3430 * qualify a previously declared variable as being invariant
3432 * invariant gl_Position; // make existing gl_Position be invariant"
3434 * In these cases the parser will set the 'invariant' flag in the declarator
3435 * list, and the type will be NULL.
3437 if (this->invariant
) {
3438 assert(this->type
== NULL
);
3440 if (state
->current_function
!= NULL
) {
3441 _mesa_glsl_error(& loc
, state
,
3442 "all uses of `invariant' keyword must be at global "
3446 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
3447 assert(decl
->array_specifier
== NULL
);
3448 assert(decl
->initializer
== NULL
);
3450 ir_variable
*const earlier
=
3451 state
->symbols
->get_variable(decl
->identifier
);
3452 if (earlier
== NULL
) {
3453 _mesa_glsl_error(& loc
, state
,
3454 "undeclared variable `%s' cannot be marked "
3455 "invariant", decl
->identifier
);
3456 } else if (!is_varying_var(earlier
, state
->stage
)) {
3457 _mesa_glsl_error(&loc
, state
,
3458 "`%s' cannot be marked invariant; interfaces between "
3459 "shader stages only.", decl
->identifier
);
3460 } else if (earlier
->data
.used
) {
3461 _mesa_glsl_error(& loc
, state
,
3462 "variable `%s' may not be redeclared "
3463 "`invariant' after being used",
3466 earlier
->data
.invariant
= true;
3470 /* Invariant redeclarations do not have r-values.
3475 if (this->precise
) {
3476 assert(this->type
== NULL
);
3478 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
3479 assert(decl
->array_specifier
== NULL
);
3480 assert(decl
->initializer
== NULL
);
3482 ir_variable
*const earlier
=
3483 state
->symbols
->get_variable(decl
->identifier
);
3484 if (earlier
== NULL
) {
3485 _mesa_glsl_error(& loc
, state
,
3486 "undeclared variable `%s' cannot be marked "
3487 "precise", decl
->identifier
);
3488 } else if (state
->current_function
!= NULL
&&
3489 !state
->symbols
->name_declared_this_scope(decl
->identifier
)) {
3490 /* Note: we have to check if we're in a function, since
3491 * builtins are treated as having come from another scope.
3493 _mesa_glsl_error(& loc
, state
,
3494 "variable `%s' from an outer scope may not be "
3495 "redeclared `precise' in this scope",
3497 } else if (earlier
->data
.used
) {
3498 _mesa_glsl_error(& loc
, state
,
3499 "variable `%s' may not be redeclared "
3500 "`precise' after being used",
3503 earlier
->data
.precise
= true;
3507 /* Precise redeclarations do not have r-values either. */
3511 assert(this->type
!= NULL
);
3512 assert(!this->invariant
);
3513 assert(!this->precise
);
3515 /* The type specifier may contain a structure definition. Process that
3516 * before any of the variable declarations.
3518 (void) this->type
->specifier
->hir(instructions
, state
);
3520 decl_type
= this->type
->glsl_type(& type_name
, state
);
3522 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
3523 * "Buffer variables may only be declared inside interface blocks
3524 * (section 4.3.9 “Interface Blocks”), which are then referred to as
3525 * shader storage blocks. It is a compile-time error to declare buffer
3526 * variables at global scope (outside a block)."
3528 if (type
->qualifier
.flags
.q
.buffer
&& !decl_type
->is_interface()) {
3529 _mesa_glsl_error(&loc
, state
,
3530 "buffer variables cannot be declared outside "
3531 "interface blocks");
3534 /* An offset-qualified atomic counter declaration sets the default
3535 * offset for the next declaration within the same atomic counter
3538 if (decl_type
&& decl_type
->contains_atomic()) {
3539 if (type
->qualifier
.flags
.q
.explicit_binding
&&
3540 type
->qualifier
.flags
.q
.explicit_offset
)
3541 state
->atomic_counter_offsets
[type
->qualifier
.binding
] =
3542 type
->qualifier
.offset
;
3545 if (this->declarations
.is_empty()) {
3546 /* If there is no structure involved in the program text, there are two
3547 * possible scenarios:
3549 * - The program text contained something like 'vec4;'. This is an
3550 * empty declaration. It is valid but weird. Emit a warning.
3552 * - The program text contained something like 'S;' and 'S' is not the
3553 * name of a known structure type. This is both invalid and weird.
3556 * - The program text contained something like 'mediump float;'
3557 * when the programmer probably meant 'precision mediump
3558 * float;' Emit a warning with a description of what they
3559 * probably meant to do.
3561 * Note that if decl_type is NULL and there is a structure involved,
3562 * there must have been some sort of error with the structure. In this
3563 * case we assume that an error was already generated on this line of
3564 * code for the structure. There is no need to generate an additional,
3567 assert(this->type
->specifier
->structure
== NULL
|| decl_type
!= NULL
3570 if (decl_type
== NULL
) {
3571 _mesa_glsl_error(&loc
, state
,
3572 "invalid type `%s' in empty declaration",
3574 } else if (decl_type
->base_type
== GLSL_TYPE_ATOMIC_UINT
) {
3575 /* Empty atomic counter declarations are allowed and useful
3576 * to set the default offset qualifier.
3579 } else if (this->type
->qualifier
.precision
!= ast_precision_none
) {
3580 if (this->type
->specifier
->structure
!= NULL
) {
3581 _mesa_glsl_error(&loc
, state
,
3582 "precision qualifiers can't be applied "
3585 static const char *const precision_names
[] = {
3592 _mesa_glsl_warning(&loc
, state
,
3593 "empty declaration with precision qualifier, "
3594 "to set the default precision, use "
3595 "`precision %s %s;'",
3596 precision_names
[this->type
->qualifier
.precision
],
3599 } else if (this->type
->specifier
->structure
== NULL
) {
3600 _mesa_glsl_warning(&loc
, state
, "empty declaration");
3604 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
3605 const struct glsl_type
*var_type
;
3607 const char *identifier
= decl
->identifier
;
3608 /* FINISHME: Emit a warning if a variable declaration shadows a
3609 * FINISHME: declaration at a higher scope.
3612 if ((decl_type
== NULL
) || decl_type
->is_void()) {
3613 if (type_name
!= NULL
) {
3614 _mesa_glsl_error(& loc
, state
,
3615 "invalid type `%s' in declaration of `%s'",
3616 type_name
, decl
->identifier
);
3618 _mesa_glsl_error(& loc
, state
,
3619 "invalid type in declaration of `%s'",
3625 if (this->type
->qualifier
.flags
.q
.subroutine
) {
3629 t
= state
->symbols
->get_type(this->type
->specifier
->type_name
);
3631 _mesa_glsl_error(& loc
, state
,
3632 "invalid type in declaration of `%s'",
3634 name
= ralloc_asprintf(ctx
, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state
->stage
), decl
->identifier
);
3639 var_type
= process_array_type(&loc
, decl_type
, decl
->array_specifier
,
3642 var
= new(ctx
) ir_variable(var_type
, identifier
, ir_var_auto
);
3644 /* The 'varying in' and 'varying out' qualifiers can only be used with
3645 * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
3648 if (this->type
->qualifier
.flags
.q
.varying
) {
3649 if (this->type
->qualifier
.flags
.q
.in
) {
3650 _mesa_glsl_error(& loc
, state
,
3651 "`varying in' qualifier in declaration of "
3652 "`%s' only valid for geometry shaders using "
3653 "ARB_geometry_shader4 or EXT_geometry_shader4",
3655 } else if (this->type
->qualifier
.flags
.q
.out
) {
3656 _mesa_glsl_error(& loc
, state
,
3657 "`varying out' qualifier in declaration of "
3658 "`%s' only valid for geometry shaders using "
3659 "ARB_geometry_shader4 or EXT_geometry_shader4",
3664 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
3666 * "Global variables can only use the qualifiers const,
3667 * attribute, uniform, or varying. Only one may be
3670 * Local variables can only use the qualifier const."
3672 * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
3673 * any extension that adds the 'layout' keyword.
3675 if (!state
->is_version(130, 300)
3676 && !state
->has_explicit_attrib_location()
3677 && !state
->has_separate_shader_objects()
3678 && !state
->ARB_fragment_coord_conventions_enable
) {
3679 if (this->type
->qualifier
.flags
.q
.out
) {
3680 _mesa_glsl_error(& loc
, state
,
3681 "`out' qualifier in declaration of `%s' "
3682 "only valid for function parameters in %s",
3683 decl
->identifier
, state
->get_version_string());
3685 if (this->type
->qualifier
.flags
.q
.in
) {
3686 _mesa_glsl_error(& loc
, state
,
3687 "`in' qualifier in declaration of `%s' "
3688 "only valid for function parameters in %s",
3689 decl
->identifier
, state
->get_version_string());
3691 /* FINISHME: Test for other invalid qualifiers. */
3694 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
3697 if (this->type
->qualifier
.flags
.q
.invariant
) {
3698 if (!is_varying_var(var
, state
->stage
)) {
3699 _mesa_glsl_error(&loc
, state
,
3700 "`%s' cannot be marked invariant; interfaces between "
3701 "shader stages only", var
->name
);
3705 if (state
->current_function
!= NULL
) {
3706 const char *mode
= NULL
;
3707 const char *extra
= "";
3709 /* There is no need to check for 'inout' here because the parser will
3710 * only allow that in function parameter lists.
3712 if (this->type
->qualifier
.flags
.q
.attribute
) {
3714 } else if (this->type
->qualifier
.flags
.q
.subroutine
) {
3715 mode
= "subroutine uniform";
3716 } else if (this->type
->qualifier
.flags
.q
.uniform
) {
3718 } else if (this->type
->qualifier
.flags
.q
.varying
) {
3720 } else if (this->type
->qualifier
.flags
.q
.in
) {
3722 extra
= " or in function parameter list";
3723 } else if (this->type
->qualifier
.flags
.q
.out
) {
3725 extra
= " or in function parameter list";
3729 _mesa_glsl_error(& loc
, state
,
3730 "%s variable `%s' must be declared at "
3732 mode
, var
->name
, extra
);
3734 } else if (var
->data
.mode
== ir_var_shader_in
) {
3735 var
->data
.read_only
= true;
3737 if (state
->stage
== MESA_SHADER_VERTEX
) {
3738 bool error_emitted
= false;
3740 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
3742 * "Vertex shader inputs can only be float, floating-point
3743 * vectors, matrices, signed and unsigned integers and integer
3744 * vectors. Vertex shader inputs can also form arrays of these
3745 * types, but not structures."
3747 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
3749 * "Vertex shader inputs can only be float, floating-point
3750 * vectors, matrices, signed and unsigned integers and integer
3751 * vectors. They cannot be arrays or structures."
3753 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
3755 * "The attribute qualifier can be used only with float,
3756 * floating-point vectors, and matrices. Attribute variables
3757 * cannot be declared as arrays or structures."
3759 * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
3761 * "Vertex shader inputs can only be float, floating-point
3762 * vectors, matrices, signed and unsigned integers and integer
3763 * vectors. Vertex shader inputs cannot be arrays or
3766 const glsl_type
*check_type
= var
->type
->without_array();
3768 switch (check_type
->base_type
) {
3769 case GLSL_TYPE_FLOAT
:
3771 case GLSL_TYPE_UINT
:
3773 if (state
->is_version(120, 300))
3775 case GLSL_TYPE_DOUBLE
:
3776 if (check_type
->base_type
== GLSL_TYPE_DOUBLE
&& (state
->is_version(410, 0) || state
->ARB_vertex_attrib_64bit_enable
))
3780 _mesa_glsl_error(& loc
, state
,
3781 "vertex shader input / attribute cannot have "
3783 var
->type
->is_array() ? "array of " : "",
3785 error_emitted
= true;
3788 if (!error_emitted
&& var
->type
->is_array() &&
3789 !state
->check_version(150, 0, &loc
,
3790 "vertex shader input / attribute "
3791 "cannot have array type")) {
3792 error_emitted
= true;
3794 } else if (state
->stage
== MESA_SHADER_GEOMETRY
) {
3795 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
3797 * Geometry shader input variables get the per-vertex values
3798 * written out by vertex shader output variables of the same
3799 * names. Since a geometry shader operates on a set of
3800 * vertices, each input varying variable (or input block, see
3801 * interface blocks below) needs to be declared as an array.
3803 if (!var
->type
->is_array()) {
3804 _mesa_glsl_error(&loc
, state
,
3805 "geometry shader inputs must be arrays");
3808 handle_geometry_shader_input_decl(state
, loc
, var
);
3809 } else if (state
->stage
== MESA_SHADER_FRAGMENT
) {
3810 /* From section 4.3.4 (Input Variables) of the GLSL ES 3.10 spec:
3812 * It is a compile-time error to declare a fragment shader
3813 * input with, or that contains, any of the following types:
3817 * * An array of arrays
3818 * * An array of structures
3819 * * A structure containing an array
3820 * * A structure containing a structure
3822 if (state
->es_shader
) {
3823 const glsl_type
*check_type
= var
->type
->without_array();
3824 if (check_type
->is_boolean() ||
3825 check_type
->contains_opaque()) {
3826 _mesa_glsl_error(&loc
, state
,
3827 "fragment shader input cannot have type %s",
3830 if (var
->type
->is_array() &&
3831 var
->type
->fields
.array
->is_array()) {
3832 _mesa_glsl_error(&loc
, state
,
3834 "cannot have an array of arrays",
3835 _mesa_shader_stage_to_string(state
->stage
));
3837 if (var
->type
->is_array() &&
3838 var
->type
->fields
.array
->is_record()) {
3839 _mesa_glsl_error(&loc
, state
,
3840 "fragment shader input "
3841 "cannot have an array of structs");
3843 if (var
->type
->is_record()) {
3844 for (unsigned i
= 0; i
< var
->type
->length
; i
++) {
3845 if (var
->type
->fields
.structure
[i
].type
->is_array() ||
3846 var
->type
->fields
.structure
[i
].type
->is_record())
3847 _mesa_glsl_error(&loc
, state
,
3848 "fragement shader input cannot have "
3849 "a struct that contains an "
3854 } else if (state
->stage
== MESA_SHADER_TESS_CTRL
||
3855 state
->stage
== MESA_SHADER_TESS_EVAL
) {
3856 handle_tess_shader_input_decl(state
, loc
, var
);
3858 } else if (var
->data
.mode
== ir_var_shader_out
) {
3859 const glsl_type
*check_type
= var
->type
->without_array();
3861 /* From section 4.3.6 (Output variables) of the GLSL 4.40 spec:
3863 * It is a compile-time error to declare a vertex, tessellation
3864 * evaluation, tessellation control, or geometry shader output
3865 * that contains any of the following:
3867 * * A Boolean type (bool, bvec2 ...)
3870 if (check_type
->is_boolean() || check_type
->contains_opaque())
3871 _mesa_glsl_error(&loc
, state
,
3872 "%s shader output cannot have type %s",
3873 _mesa_shader_stage_to_string(state
->stage
),
3876 /* From section 4.3.6 (Output variables) of the GLSL 4.40 spec:
3878 * It is a compile-time error to declare a fragment shader output
3879 * that contains any of the following:
3881 * * A Boolean type (bool, bvec2 ...)
3882 * * A double-precision scalar or vector (double, dvec2 ...)
3887 if (state
->stage
== MESA_SHADER_FRAGMENT
) {
3888 if (check_type
->is_record() || check_type
->is_matrix())
3889 _mesa_glsl_error(&loc
, state
,
3890 "fragment shader output "
3891 "cannot have struct or matrix type");
3892 switch (check_type
->base_type
) {
3893 case GLSL_TYPE_UINT
:
3895 case GLSL_TYPE_FLOAT
:
3898 _mesa_glsl_error(&loc
, state
,
3899 "fragment shader output cannot have "
3900 "type %s", check_type
->name
);
3904 /* From section 4.3.6 (Output Variables) of the GLSL ES 3.10 spec:
3906 * It is a compile-time error to declare a vertex shader output
3907 * with, or that contains, any of the following types:
3911 * * An array of arrays
3912 * * An array of structures
3913 * * A structure containing an array
3914 * * A structure containing a structure
3916 * It is a compile-time error to declare a fragment shader output
3917 * with, or that contains, any of the following types:
3923 * * An array of array
3925 if (state
->es_shader
) {
3926 if (var
->type
->is_array() &&
3927 var
->type
->fields
.array
->is_array()) {
3928 _mesa_glsl_error(&loc
, state
,
3930 "cannot have an array of arrays",
3931 _mesa_shader_stage_to_string(state
->stage
));
3933 if (state
->stage
== MESA_SHADER_VERTEX
) {
3934 if (var
->type
->is_array() &&
3935 var
->type
->fields
.array
->is_record()) {
3936 _mesa_glsl_error(&loc
, state
,
3937 "vertex shader output "
3938 "cannot have an array of structs");
3940 if (var
->type
->is_record()) {
3941 for (unsigned i
= 0; i
< var
->type
->length
; i
++) {
3942 if (var
->type
->fields
.structure
[i
].type
->is_array() ||
3943 var
->type
->fields
.structure
[i
].type
->is_record())
3944 _mesa_glsl_error(&loc
, state
,
3945 "vertex shader output cannot have a "
3946 "struct that contains an "
3953 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
3954 handle_tess_ctrl_shader_output_decl(state
, loc
, var
);
3956 } else if (var
->type
->contains_subroutine()) {
3957 /* declare subroutine uniforms as hidden */
3958 var
->data
.how_declared
= ir_var_hidden
;
3961 /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
3962 * so must integer vertex outputs.
3964 * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
3965 * "Fragment shader inputs that are signed or unsigned integers or
3966 * integer vectors must be qualified with the interpolation qualifier
3969 * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
3970 * "Fragment shader inputs that are, or contain, signed or unsigned
3971 * integers or integer vectors must be qualified with the
3972 * interpolation qualifier flat."
3974 * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
3975 * "Vertex shader outputs that are, or contain, signed or unsigned
3976 * integers or integer vectors must be qualified with the
3977 * interpolation qualifier flat."
3979 * Note that prior to GLSL 1.50, this requirement applied to vertex
3980 * outputs rather than fragment inputs. That creates problems in the
3981 * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
3982 * desktop GL shaders. For GLSL ES shaders, we follow the spec and
3983 * apply the restriction to both vertex outputs and fragment inputs.
3985 * Note also that the desktop GLSL specs are missing the text "or
3986 * contain"; this is presumably an oversight, since there is no
3987 * reasonable way to interpolate a fragment shader input that contains
3990 if (state
->is_version(130, 300) &&
3991 var
->type
->contains_integer() &&
3992 var
->data
.interpolation
!= INTERP_QUALIFIER_FLAT
&&
3993 ((state
->stage
== MESA_SHADER_FRAGMENT
&& var
->data
.mode
== ir_var_shader_in
)
3994 || (state
->stage
== MESA_SHADER_VERTEX
&& var
->data
.mode
== ir_var_shader_out
3995 && state
->es_shader
))) {
3996 const char *var_type
= (state
->stage
== MESA_SHADER_VERTEX
) ?
3997 "vertex output" : "fragment input";
3998 _mesa_glsl_error(&loc
, state
, "if a %s is (or contains) "
3999 "an integer, then it must be qualified with 'flat'",
4003 /* Double fragment inputs must be qualified with 'flat'. */
4004 if (var
->type
->contains_double() &&
4005 var
->data
.interpolation
!= INTERP_QUALIFIER_FLAT
&&
4006 state
->stage
== MESA_SHADER_FRAGMENT
&&
4007 var
->data
.mode
== ir_var_shader_in
) {
4008 _mesa_glsl_error(&loc
, state
, "if a fragment input is (or contains) "
4009 "a double, then it must be qualified with 'flat'",
4013 /* Interpolation qualifiers cannot be applied to 'centroid' and
4014 * 'centroid varying'.
4016 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
4017 * "interpolation qualifiers may only precede the qualifiers in,
4018 * centroid in, out, or centroid out in a declaration. They do not apply
4019 * to the deprecated storage qualifiers varying or centroid varying."
4021 * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
4023 if (state
->is_version(130, 0)
4024 && this->type
->qualifier
.has_interpolation()
4025 && this->type
->qualifier
.flags
.q
.varying
) {
4027 const char *i
= this->type
->qualifier
.interpolation_string();
4030 if (this->type
->qualifier
.flags
.q
.centroid
)
4031 s
= "centroid varying";
4035 _mesa_glsl_error(&loc
, state
,
4036 "qualifier '%s' cannot be applied to the "
4037 "deprecated storage qualifier '%s'", i
, s
);
4041 /* Interpolation qualifiers can only apply to vertex shader outputs and
4042 * fragment shader inputs.
4044 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
4045 * "Outputs from a vertex shader (out) and inputs to a fragment
4046 * shader (in) can be further qualified with one or more of these
4047 * interpolation qualifiers"
4049 * From page 31 (page 37 of the PDF) of the GLSL ES 3.00 spec:
4050 * "These interpolation qualifiers may only precede the qualifiers
4051 * in, centroid in, out, or centroid out in a declaration. They do
4052 * not apply to inputs into a vertex shader or outputs from a
4055 if (state
->is_version(130, 300)
4056 && this->type
->qualifier
.has_interpolation()) {
4058 const char *i
= this->type
->qualifier
.interpolation_string();
4061 switch (state
->stage
) {
4062 case MESA_SHADER_VERTEX
:
4063 if (this->type
->qualifier
.flags
.q
.in
) {
4064 _mesa_glsl_error(&loc
, state
,
4065 "qualifier '%s' cannot be applied to vertex "
4066 "shader inputs", i
);
4069 case MESA_SHADER_FRAGMENT
:
4070 if (this->type
->qualifier
.flags
.q
.out
) {
4071 _mesa_glsl_error(&loc
, state
,
4072 "qualifier '%s' cannot be applied to fragment "
4073 "shader outputs", i
);
4082 /* From section 4.3.4 of the GLSL 4.00 spec:
4083 * "Input variables may not be declared using the patch in qualifier
4084 * in tessellation control or geometry shaders."
4086 * From section 4.3.6 of the GLSL 4.00 spec:
4087 * "It is an error to use patch out in a vertex, tessellation
4088 * evaluation, or geometry shader."
4090 * This doesn't explicitly forbid using them in a fragment shader, but
4091 * that's probably just an oversight.
4093 if (state
->stage
!= MESA_SHADER_TESS_EVAL
4094 && this->type
->qualifier
.flags
.q
.patch
4095 && this->type
->qualifier
.flags
.q
.in
) {
4097 _mesa_glsl_error(&loc
, state
, "'patch in' can only be used in a "
4098 "tessellation evaluation shader");
4101 if (state
->stage
!= MESA_SHADER_TESS_CTRL
4102 && this->type
->qualifier
.flags
.q
.patch
4103 && this->type
->qualifier
.flags
.q
.out
) {
4105 _mesa_glsl_error(&loc
, state
, "'patch out' can only be used in a "
4106 "tessellation control shader");
4109 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
4111 if (this->type
->qualifier
.precision
!= ast_precision_none
) {
4112 state
->check_precision_qualifiers_allowed(&loc
);
4116 /* If a precision qualifier is allowed on a type, it is allowed on
4117 * an array of that type.
4119 if (!(this->type
->qualifier
.precision
== ast_precision_none
4120 || precision_qualifier_allowed(var
->type
->without_array()))) {
4122 _mesa_glsl_error(&loc
, state
,
4123 "precision qualifiers apply only to floating point"
4124 ", integer and sampler types");
4127 /* From section 4.1.7 of the GLSL 4.40 spec:
4129 * "[Opaque types] can only be declared as function
4130 * parameters or uniform-qualified variables."
4132 if (var_type
->contains_opaque() &&
4133 !this->type
->qualifier
.flags
.q
.uniform
) {
4134 _mesa_glsl_error(&loc
, state
,
4135 "opaque variables must be declared uniform");
4138 /* Process the initializer and add its instructions to a temporary
4139 * list. This list will be added to the instruction stream (below) after
4140 * the declaration is added. This is done because in some cases (such as
4141 * redeclarations) the declaration may not actually be added to the
4142 * instruction stream.
4144 exec_list initializer_instructions
;
4146 /* Examine var name here since var may get deleted in the next call */
4147 bool var_is_gl_id
= is_gl_identifier(var
->name
);
4149 ir_variable
*earlier
=
4150 get_variable_being_redeclared(var
, decl
->get_location(), state
,
4151 false /* allow_all_redeclarations */);
4152 if (earlier
!= NULL
) {
4154 earlier
->data
.how_declared
== ir_var_declared_in_block
) {
4155 _mesa_glsl_error(&loc
, state
,
4156 "`%s' has already been redeclared using "
4157 "gl_PerVertex", earlier
->name
);
4159 earlier
->data
.how_declared
= ir_var_declared_normally
;
4162 if (decl
->initializer
!= NULL
) {
4163 result
= process_initializer((earlier
== NULL
) ? var
: earlier
,
4165 &initializer_instructions
, state
);
4168 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
4170 * "It is an error to write to a const variable outside of
4171 * its declaration, so they must be initialized when
4174 if (this->type
->qualifier
.flags
.q
.constant
&& decl
->initializer
== NULL
) {
4175 _mesa_glsl_error(& loc
, state
,
4176 "const declaration of `%s' must be initialized",
4180 if (state
->es_shader
) {
4181 const glsl_type
*const t
= (earlier
== NULL
)
4182 ? var
->type
: earlier
->type
;
4184 if (t
->is_unsized_array())
4185 /* Section 10.17 of the GLSL ES 1.00 specification states that
4186 * unsized array declarations have been removed from the language.
4187 * Arrays that are sized using an initializer are still explicitly
4188 * sized. However, GLSL ES 1.00 does not allow array
4189 * initializers. That is only allowed in GLSL ES 3.00.
4191 * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
4193 * "An array type can also be formed without specifying a size
4194 * if the definition includes an initializer:
4196 * float x[] = float[2] (1.0, 2.0); // declares an array of size 2
4197 * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
4202 _mesa_glsl_error(& loc
, state
,
4203 "unsized array declarations are not allowed in "
4207 /* If the declaration is not a redeclaration, there are a few additional
4208 * semantic checks that must be applied. In addition, variable that was
4209 * created for the declaration should be added to the IR stream.
4211 if (earlier
== NULL
) {
4212 validate_identifier(decl
->identifier
, loc
, state
);
4214 /* Add the variable to the symbol table. Note that the initializer's
4215 * IR was already processed earlier (though it hasn't been emitted
4216 * yet), without the variable in scope.
4218 * This differs from most C-like languages, but it follows the GLSL
4219 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
4222 * "Within a declaration, the scope of a name starts immediately
4223 * after the initializer if present or immediately after the name
4224 * being declared if not."
4226 if (!state
->symbols
->add_variable(var
)) {
4227 YYLTYPE loc
= this->get_location();
4228 _mesa_glsl_error(&loc
, state
, "name `%s' already taken in the "
4229 "current scope", decl
->identifier
);
4233 /* Push the variable declaration to the top. It means that all the
4234 * variable declarations will appear in a funny last-to-first order,
4235 * but otherwise we run into trouble if a function is prototyped, a
4236 * global var is decled, then the function is defined with usage of
4237 * the global var. See glslparsertest's CorrectModule.frag.
4239 instructions
->push_head(var
);
4242 instructions
->append_list(&initializer_instructions
);
4246 /* Generally, variable declarations do not have r-values. However,
4247 * one is used for the declaration in
4249 * while (bool b = some_condition()) {
4253 * so we return the rvalue from the last seen declaration here.
4260 ast_parameter_declarator::hir(exec_list
*instructions
,
4261 struct _mesa_glsl_parse_state
*state
)
4264 const struct glsl_type
*type
;
4265 const char *name
= NULL
;
4266 YYLTYPE loc
= this->get_location();
4268 type
= this->type
->glsl_type(& name
, state
);
4272 _mesa_glsl_error(& loc
, state
,
4273 "invalid type `%s' in declaration of `%s'",
4274 name
, this->identifier
);
4276 _mesa_glsl_error(& loc
, state
,
4277 "invalid type in declaration of `%s'",
4281 type
= glsl_type::error_type
;
4284 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
4286 * "Functions that accept no input arguments need not use void in the
4287 * argument list because prototypes (or definitions) are required and
4288 * therefore there is no ambiguity when an empty argument list "( )" is
4289 * declared. The idiom "(void)" as a parameter list is provided for
4292 * Placing this check here prevents a void parameter being set up
4293 * for a function, which avoids tripping up checks for main taking
4294 * parameters and lookups of an unnamed symbol.
4296 if (type
->is_void()) {
4297 if (this->identifier
!= NULL
)
4298 _mesa_glsl_error(& loc
, state
,
4299 "named parameter cannot have type `void'");
4305 if (formal_parameter
&& (this->identifier
== NULL
)) {
4306 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
4310 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
4311 * call already handled the "vec4[..] foo" case.
4313 type
= process_array_type(&loc
, type
, this->array_specifier
, state
);
4315 if (!type
->is_error() && type
->is_unsized_array()) {
4316 _mesa_glsl_error(&loc
, state
, "arrays passed as parameters must have "
4318 type
= glsl_type::error_type
;
4322 ir_variable
*var
= new(ctx
)
4323 ir_variable(type
, this->identifier
, ir_var_function_in
);
4325 /* Apply any specified qualifiers to the parameter declaration. Note that
4326 * for function parameters the default mode is 'in'.
4328 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
,
4331 /* From section 4.1.7 of the GLSL 4.40 spec:
4333 * "Opaque variables cannot be treated as l-values; hence cannot
4334 * be used as out or inout function parameters, nor can they be
4337 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
4338 && type
->contains_opaque()) {
4339 _mesa_glsl_error(&loc
, state
, "out and inout parameters cannot "
4340 "contain opaque variables");
4341 type
= glsl_type::error_type
;
4344 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
4346 * "When calling a function, expressions that do not evaluate to
4347 * l-values cannot be passed to parameters declared as out or inout."
4349 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
4351 * "Other binary or unary expressions, non-dereferenced arrays,
4352 * function names, swizzles with repeated fields, and constants
4353 * cannot be l-values."
4355 * So for GLSL 1.10, passing an array as an out or inout parameter is not
4356 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
4358 if ((var
->data
.mode
== ir_var_function_inout
|| var
->data
.mode
== ir_var_function_out
)
4360 && !state
->check_version(120, 100, &loc
,
4361 "arrays cannot be out or inout parameters")) {
4362 type
= glsl_type::error_type
;
4365 instructions
->push_tail(var
);
4367 /* Parameter declarations do not have r-values.
4374 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
4376 exec_list
*ir_parameters
,
4377 _mesa_glsl_parse_state
*state
)
4379 ast_parameter_declarator
*void_param
= NULL
;
4382 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
4383 param
->formal_parameter
= formal
;
4384 param
->hir(ir_parameters
, state
);
4392 if ((void_param
!= NULL
) && (count
> 1)) {
4393 YYLTYPE loc
= void_param
->get_location();
4395 _mesa_glsl_error(& loc
, state
,
4396 "`void' parameter must be only parameter");
4402 emit_function(_mesa_glsl_parse_state
*state
, ir_function
*f
)
4404 /* IR invariants disallow function declarations or definitions
4405 * nested within other function definitions. But there is no
4406 * requirement about the relative order of function declarations
4407 * and definitions with respect to one another. So simply insert
4408 * the new ir_function block at the end of the toplevel instruction
4411 state
->toplevel_ir
->push_tail(f
);
4416 ast_function::hir(exec_list
*instructions
,
4417 struct _mesa_glsl_parse_state
*state
)
4420 ir_function
*f
= NULL
;
4421 ir_function_signature
*sig
= NULL
;
4422 exec_list hir_parameters
;
4423 YYLTYPE loc
= this->get_location();
4425 const char *const name
= identifier
;
4427 /* New functions are always added to the top-level IR instruction stream,
4428 * so this instruction list pointer is ignored. See also emit_function
4431 (void) instructions
;
4433 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
4435 * "Function declarations (prototypes) cannot occur inside of functions;
4436 * they must be at global scope, or for the built-in functions, outside
4437 * the global scope."
4439 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
4441 * "User defined functions may only be defined within the global scope."
4443 * Note that this language does not appear in GLSL 1.10.
4445 if ((state
->current_function
!= NULL
) &&
4446 state
->is_version(120, 100)) {
4447 YYLTYPE loc
= this->get_location();
4448 _mesa_glsl_error(&loc
, state
,
4449 "declaration of function `%s' not allowed within "
4450 "function body", name
);
4453 validate_identifier(name
, this->get_location(), state
);
4455 /* Convert the list of function parameters to HIR now so that they can be
4456 * used below to compare this function's signature with previously seen
4457 * signatures for functions with the same name.
4459 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
4461 & hir_parameters
, state
);
4463 const char *return_type_name
;
4464 const glsl_type
*return_type
=
4465 this->return_type
->glsl_type(& return_type_name
, state
);
4468 YYLTYPE loc
= this->get_location();
4469 _mesa_glsl_error(&loc
, state
,
4470 "function `%s' has undeclared return type `%s'",
4471 name
, return_type_name
);
4472 return_type
= glsl_type::error_type
;
4475 /* ARB_shader_subroutine states:
4476 * "Subroutine declarations cannot be prototyped. It is an error to prepend
4477 * subroutine(...) to a function declaration."
4479 if (this->return_type
->qualifier
.flags
.q
.subroutine_def
&& !is_definition
) {
4480 YYLTYPE loc
= this->get_location();
4481 _mesa_glsl_error(&loc
, state
,
4482 "function declaration `%s' cannot have subroutine prepended",
4486 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
4487 * "No qualifier is allowed on the return type of a function."
4489 if (this->return_type
->has_qualifiers()) {
4490 YYLTYPE loc
= this->get_location();
4491 _mesa_glsl_error(& loc
, state
,
4492 "function `%s' return type has qualifiers", name
);
4495 /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
4497 * "Arrays are allowed as arguments and as the return type. In both
4498 * cases, the array must be explicitly sized."
4500 if (return_type
->is_unsized_array()) {
4501 YYLTYPE loc
= this->get_location();
4502 _mesa_glsl_error(& loc
, state
,
4503 "function `%s' return type array must be explicitly "
4507 /* From section 4.1.7 of the GLSL 4.40 spec:
4509 * "[Opaque types] can only be declared as function parameters
4510 * or uniform-qualified variables."
4512 if (return_type
->contains_opaque()) {
4513 YYLTYPE loc
= this->get_location();
4514 _mesa_glsl_error(&loc
, state
,
4515 "function `%s' return type can't contain an opaque type",
4519 /* Create an ir_function if one doesn't already exist. */
4520 f
= state
->symbols
->get_function(name
);
4522 f
= new(ctx
) ir_function(name
);
4523 if (!this->return_type
->qualifier
.flags
.q
.subroutine
) {
4524 if (!state
->symbols
->add_function(f
)) {
4525 /* This function name shadows a non-function use of the same name. */
4526 YYLTYPE loc
= this->get_location();
4527 _mesa_glsl_error(&loc
, state
, "function name `%s' conflicts with "
4528 "non-function", name
);
4532 emit_function(state
, f
);
4535 /* From GLSL ES 3.0 spec, chapter 6.1 "Function Definitions", page 71:
4537 * "A shader cannot redefine or overload built-in functions."
4539 * While in GLSL ES 1.0 specification, chapter 8 "Built-in Functions":
4541 * "User code can overload the built-in functions but cannot redefine
4544 if (state
->es_shader
&& state
->language_version
>= 300) {
4545 /* Local shader has no exact candidates; check the built-ins. */
4546 _mesa_glsl_initialize_builtin_functions();
4547 if (_mesa_glsl_find_builtin_function_by_name(state
, name
)) {
4548 YYLTYPE loc
= this->get_location();
4549 _mesa_glsl_error(& loc
, state
,
4550 "A shader cannot redefine or overload built-in "
4551 "function `%s' in GLSL ES 3.00", name
);
4556 /* Verify that this function's signature either doesn't match a previously
4557 * seen signature for a function with the same name, or, if a match is found,
4558 * that the previously seen signature does not have an associated definition.
4560 if (state
->es_shader
|| f
->has_user_signature()) {
4561 sig
= f
->exact_matching_signature(state
, &hir_parameters
);
4563 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
4564 if (badvar
!= NULL
) {
4565 YYLTYPE loc
= this->get_location();
4567 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
4568 "qualifiers don't match prototype", name
, badvar
);
4571 if (sig
->return_type
!= return_type
) {
4572 YYLTYPE loc
= this->get_location();
4574 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
4575 "match prototype", name
);
4578 if (sig
->is_defined
) {
4579 if (is_definition
) {
4580 YYLTYPE loc
= this->get_location();
4581 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
4583 /* We just encountered a prototype that exactly matches a
4584 * function that's already been defined. This is redundant,
4585 * and we should ignore it.
4593 /* Verify the return type of main() */
4594 if (strcmp(name
, "main") == 0) {
4595 if (! return_type
->is_void()) {
4596 YYLTYPE loc
= this->get_location();
4598 _mesa_glsl_error(& loc
, state
, "main() must return void");
4601 if (!hir_parameters
.is_empty()) {
4602 YYLTYPE loc
= this->get_location();
4604 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
4608 /* Finish storing the information about this new function in its signature.
4611 sig
= new(ctx
) ir_function_signature(return_type
);
4612 f
->add_signature(sig
);
4615 sig
->replace_parameters(&hir_parameters
);
4618 if (this->return_type
->qualifier
.flags
.q
.subroutine_def
) {
4621 f
->num_subroutine_types
= this->return_type
->qualifier
.subroutine_list
->declarations
.length();
4622 f
->subroutine_types
= ralloc_array(state
, const struct glsl_type
*,
4623 f
->num_subroutine_types
);
4625 foreach_list_typed(ast_declaration
, decl
, link
, &this->return_type
->qualifier
.subroutine_list
->declarations
) {
4626 const struct glsl_type
*type
;
4627 /* the subroutine type must be already declared */
4628 type
= state
->symbols
->get_type(decl
->identifier
);
4630 _mesa_glsl_error(& loc
, state
, "unknown type '%s' in subroutine function definition", decl
->identifier
);
4632 f
->subroutine_types
[idx
++] = type
;
4634 state
->subroutines
= (ir_function
**)reralloc(state
, state
->subroutines
,
4636 state
->num_subroutines
+ 1);
4637 state
->subroutines
[state
->num_subroutines
] = f
;
4638 state
->num_subroutines
++;
4642 if (this->return_type
->qualifier
.flags
.q
.subroutine
) {
4643 if (!state
->symbols
->add_type(this->identifier
, glsl_type::get_subroutine_instance(this->identifier
))) {
4644 _mesa_glsl_error(& loc
, state
, "type '%s' previously defined", this->identifier
);
4647 state
->subroutine_types
= (ir_function
**)reralloc(state
, state
->subroutine_types
,
4649 state
->num_subroutine_types
+ 1);
4650 state
->subroutine_types
[state
->num_subroutine_types
] = f
;
4651 state
->num_subroutine_types
++;
4653 f
->is_subroutine
= true;
4656 /* Function declarations (prototypes) do not have r-values.
4663 ast_function_definition::hir(exec_list
*instructions
,
4664 struct _mesa_glsl_parse_state
*state
)
4666 prototype
->is_definition
= true;
4667 prototype
->hir(instructions
, state
);
4669 ir_function_signature
*signature
= prototype
->signature
;
4670 if (signature
== NULL
)
4673 assert(state
->current_function
== NULL
);
4674 state
->current_function
= signature
;
4675 state
->found_return
= false;
4677 /* Duplicate parameters declared in the prototype as concrete variables.
4678 * Add these to the symbol table.
4680 state
->symbols
->push_scope();
4681 foreach_in_list(ir_variable
, var
, &signature
->parameters
) {
4682 assert(var
->as_variable() != NULL
);
4684 /* The only way a parameter would "exist" is if two parameters have
4687 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
4688 YYLTYPE loc
= this->get_location();
4690 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
4692 state
->symbols
->add_variable(var
);
4696 /* Convert the body of the function to HIR. */
4697 this->body
->hir(&signature
->body
, state
);
4698 signature
->is_defined
= true;
4700 state
->symbols
->pop_scope();
4702 assert(state
->current_function
== signature
);
4703 state
->current_function
= NULL
;
4705 if (!signature
->return_type
->is_void() && !state
->found_return
) {
4706 YYLTYPE loc
= this->get_location();
4707 _mesa_glsl_error(& loc
, state
, "function `%s' has non-void return type "
4708 "%s, but no return statement",
4709 signature
->function_name(),
4710 signature
->return_type
->name
);
4713 /* Function definitions do not have r-values.
4720 ast_jump_statement::hir(exec_list
*instructions
,
4721 struct _mesa_glsl_parse_state
*state
)
4728 assert(state
->current_function
);
4730 if (opt_return_value
) {
4731 ir_rvalue
*ret
= opt_return_value
->hir(instructions
, state
);
4733 /* The value of the return type can be NULL if the shader says
4734 * 'return foo();' and foo() is a function that returns void.
4736 * NOTE: The GLSL spec doesn't say that this is an error. The type
4737 * of the return value is void. If the return type of the function is
4738 * also void, then this should compile without error. Seriously.
4740 const glsl_type
*const ret_type
=
4741 (ret
== NULL
) ? glsl_type::void_type
: ret
->type
;
4743 /* Implicit conversions are not allowed for return values prior to
4744 * ARB_shading_language_420pack.
4746 if (state
->current_function
->return_type
!= ret_type
) {
4747 YYLTYPE loc
= this->get_location();
4749 if (state
->ARB_shading_language_420pack_enable
) {
4750 if (!apply_implicit_conversion(state
->current_function
->return_type
,
4752 _mesa_glsl_error(& loc
, state
,
4753 "could not implicitly convert return value "
4754 "to %s, in function `%s'",
4755 state
->current_function
->return_type
->name
,
4756 state
->current_function
->function_name());
4759 _mesa_glsl_error(& loc
, state
,
4760 "`return' with wrong type %s, in function `%s' "
4763 state
->current_function
->function_name(),
4764 state
->current_function
->return_type
->name
);
4766 } else if (state
->current_function
->return_type
->base_type
==
4768 YYLTYPE loc
= this->get_location();
4770 /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
4771 * specs add a clarification:
4773 * "A void function can only use return without a return argument, even if
4774 * the return argument has void type. Return statements only accept values:
4777 * void func2() { return func1(); } // illegal return statement"
4779 _mesa_glsl_error(& loc
, state
,
4780 "void functions can only use `return' without a "
4784 inst
= new(ctx
) ir_return(ret
);
4786 if (state
->current_function
->return_type
->base_type
!=
4788 YYLTYPE loc
= this->get_location();
4790 _mesa_glsl_error(& loc
, state
,
4791 "`return' with no value, in function %s returning "
4793 state
->current_function
->function_name());
4795 inst
= new(ctx
) ir_return
;
4798 state
->found_return
= true;
4799 instructions
->push_tail(inst
);
4804 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
4805 YYLTYPE loc
= this->get_location();
4807 _mesa_glsl_error(& loc
, state
,
4808 "`discard' may only appear in a fragment shader");
4810 instructions
->push_tail(new(ctx
) ir_discard
);
4815 if (mode
== ast_continue
&&
4816 state
->loop_nesting_ast
== NULL
) {
4817 YYLTYPE loc
= this->get_location();
4819 _mesa_glsl_error(& loc
, state
, "continue may only appear in a loop");
4820 } else if (mode
== ast_break
&&
4821 state
->loop_nesting_ast
== NULL
&&
4822 state
->switch_state
.switch_nesting_ast
== NULL
) {
4823 YYLTYPE loc
= this->get_location();
4825 _mesa_glsl_error(& loc
, state
,
4826 "break may only appear in a loop or a switch");
4828 /* For a loop, inline the for loop expression again, since we don't
4829 * know where near the end of the loop body the normal copy of it is
4830 * going to be placed. Same goes for the condition for a do-while
4833 if (state
->loop_nesting_ast
!= NULL
&&
4834 mode
== ast_continue
&& !state
->switch_state
.is_switch_innermost
) {
4835 if (state
->loop_nesting_ast
->rest_expression
) {
4836 state
->loop_nesting_ast
->rest_expression
->hir(instructions
,
4839 if (state
->loop_nesting_ast
->mode
==
4840 ast_iteration_statement::ast_do_while
) {
4841 state
->loop_nesting_ast
->condition_to_hir(instructions
, state
);
4845 if (state
->switch_state
.is_switch_innermost
&&
4846 mode
== ast_continue
) {
4847 /* Set 'continue_inside' to true. */
4848 ir_rvalue
*const true_val
= new (ctx
) ir_constant(true);
4849 ir_dereference_variable
*deref_continue_inside_var
=
4850 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
4851 instructions
->push_tail(new(ctx
) ir_assignment(deref_continue_inside_var
,
4854 /* Break out from the switch, continue for the loop will
4855 * be called right after switch. */
4856 ir_loop_jump
*const jump
=
4857 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
4858 instructions
->push_tail(jump
);
4860 } else if (state
->switch_state
.is_switch_innermost
&&
4861 mode
== ast_break
) {
4862 /* Force break out of switch by inserting a break. */
4863 ir_loop_jump
*const jump
=
4864 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
4865 instructions
->push_tail(jump
);
4867 ir_loop_jump
*const jump
=
4868 new(ctx
) ir_loop_jump((mode
== ast_break
)
4869 ? ir_loop_jump::jump_break
4870 : ir_loop_jump::jump_continue
);
4871 instructions
->push_tail(jump
);
4878 /* Jump instructions do not have r-values.
4885 ast_selection_statement::hir(exec_list
*instructions
,
4886 struct _mesa_glsl_parse_state
*state
)
4890 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
4892 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
4894 * "Any expression whose type evaluates to a Boolean can be used as the
4895 * conditional expression bool-expression. Vector types are not accepted
4896 * as the expression to if."
4898 * The checks are separated so that higher quality diagnostics can be
4899 * generated for cases where both rules are violated.
4901 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
4902 YYLTYPE loc
= this->condition
->get_location();
4904 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
4908 ir_if
*const stmt
= new(ctx
) ir_if(condition
);
4910 if (then_statement
!= NULL
) {
4911 state
->symbols
->push_scope();
4912 then_statement
->hir(& stmt
->then_instructions
, state
);
4913 state
->symbols
->pop_scope();
4916 if (else_statement
!= NULL
) {
4917 state
->symbols
->push_scope();
4918 else_statement
->hir(& stmt
->else_instructions
, state
);
4919 state
->symbols
->pop_scope();
4922 instructions
->push_tail(stmt
);
4924 /* if-statements do not have r-values.
4931 ast_switch_statement::hir(exec_list
*instructions
,
4932 struct _mesa_glsl_parse_state
*state
)
4936 ir_rvalue
*const test_expression
=
4937 this->test_expression
->hir(instructions
, state
);
4939 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
4941 * "The type of init-expression in a switch statement must be a
4944 if (!test_expression
->type
->is_scalar() ||
4945 !test_expression
->type
->is_integer()) {
4946 YYLTYPE loc
= this->test_expression
->get_location();
4948 _mesa_glsl_error(& loc
,
4950 "switch-statement expression must be scalar "
4954 /* Track the switch-statement nesting in a stack-like manner.
4956 struct glsl_switch_state saved
= state
->switch_state
;
4958 state
->switch_state
.is_switch_innermost
= true;
4959 state
->switch_state
.switch_nesting_ast
= this;
4960 state
->switch_state
.labels_ht
= hash_table_ctor(0, hash_table_pointer_hash
,
4961 hash_table_pointer_compare
);
4962 state
->switch_state
.previous_default
= NULL
;
4964 /* Initalize is_fallthru state to false.
4966 ir_rvalue
*const is_fallthru_val
= new (ctx
) ir_constant(false);
4967 state
->switch_state
.is_fallthru_var
=
4968 new(ctx
) ir_variable(glsl_type::bool_type
,
4969 "switch_is_fallthru_tmp",
4971 instructions
->push_tail(state
->switch_state
.is_fallthru_var
);
4973 ir_dereference_variable
*deref_is_fallthru_var
=
4974 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4975 instructions
->push_tail(new(ctx
) ir_assignment(deref_is_fallthru_var
,
4978 /* Initialize continue_inside state to false.
4980 state
->switch_state
.continue_inside
=
4981 new(ctx
) ir_variable(glsl_type::bool_type
,
4982 "continue_inside_tmp",
4984 instructions
->push_tail(state
->switch_state
.continue_inside
);
4986 ir_rvalue
*const false_val
= new (ctx
) ir_constant(false);
4987 ir_dereference_variable
*deref_continue_inside_var
=
4988 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
4989 instructions
->push_tail(new(ctx
) ir_assignment(deref_continue_inside_var
,
4992 state
->switch_state
.run_default
=
4993 new(ctx
) ir_variable(glsl_type::bool_type
,
4996 instructions
->push_tail(state
->switch_state
.run_default
);
4998 /* Loop around the switch is used for flow control. */
4999 ir_loop
* loop
= new(ctx
) ir_loop();
5000 instructions
->push_tail(loop
);
5002 /* Cache test expression.
5004 test_to_hir(&loop
->body_instructions
, state
);
5006 /* Emit code for body of switch stmt.
5008 body
->hir(&loop
->body_instructions
, state
);
5010 /* Insert a break at the end to exit loop. */
5011 ir_loop_jump
*jump
= new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
5012 loop
->body_instructions
.push_tail(jump
);
5014 /* If we are inside loop, check if continue got called inside switch. */
5015 if (state
->loop_nesting_ast
!= NULL
) {
5016 ir_dereference_variable
*deref_continue_inside
=
5017 new(ctx
) ir_dereference_variable(state
->switch_state
.continue_inside
);
5018 ir_if
*irif
= new(ctx
) ir_if(deref_continue_inside
);
5019 ir_loop_jump
*jump
= new(ctx
) ir_loop_jump(ir_loop_jump::jump_continue
);
5021 if (state
->loop_nesting_ast
!= NULL
) {
5022 if (state
->loop_nesting_ast
->rest_expression
) {
5023 state
->loop_nesting_ast
->rest_expression
->hir(&irif
->then_instructions
,
5026 if (state
->loop_nesting_ast
->mode
==
5027 ast_iteration_statement::ast_do_while
) {
5028 state
->loop_nesting_ast
->condition_to_hir(&irif
->then_instructions
, state
);
5031 irif
->then_instructions
.push_tail(jump
);
5032 instructions
->push_tail(irif
);
5035 hash_table_dtor(state
->switch_state
.labels_ht
);
5037 state
->switch_state
= saved
;
5039 /* Switch statements do not have r-values. */
5045 ast_switch_statement::test_to_hir(exec_list
*instructions
,
5046 struct _mesa_glsl_parse_state
*state
)
5050 /* Cache value of test expression. */
5051 ir_rvalue
*const test_val
=
5052 test_expression
->hir(instructions
,
5055 state
->switch_state
.test_var
= new(ctx
) ir_variable(test_val
->type
,
5058 ir_dereference_variable
*deref_test_var
=
5059 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
5061 instructions
->push_tail(state
->switch_state
.test_var
);
5062 instructions
->push_tail(new(ctx
) ir_assignment(deref_test_var
, test_val
));
5067 ast_switch_body::hir(exec_list
*instructions
,
5068 struct _mesa_glsl_parse_state
*state
)
5071 stmts
->hir(instructions
, state
);
5073 /* Switch bodies do not have r-values. */
5078 ast_case_statement_list::hir(exec_list
*instructions
,
5079 struct _mesa_glsl_parse_state
*state
)
5081 exec_list default_case
, after_default
, tmp
;
5083 foreach_list_typed (ast_case_statement
, case_stmt
, link
, & this->cases
) {
5084 case_stmt
->hir(&tmp
, state
);
5087 if (state
->switch_state
.previous_default
&& default_case
.is_empty()) {
5088 default_case
.append_list(&tmp
);
5092 /* If default case found, append 'after_default' list. */
5093 if (!default_case
.is_empty())
5094 after_default
.append_list(&tmp
);
5096 instructions
->append_list(&tmp
);
5099 /* Handle the default case. This is done here because default might not be
5100 * the last case. We need to add checks against following cases first to see
5101 * if default should be chosen or not.
5103 if (!default_case
.is_empty()) {
5105 ir_rvalue
*const true_val
= new (state
) ir_constant(true);
5106 ir_dereference_variable
*deref_run_default_var
=
5107 new(state
) ir_dereference_variable(state
->switch_state
.run_default
);
5109 /* Choose to run default case initially, following conditional
5110 * assignments might change this.
5112 ir_assignment
*const init_var
=
5113 new(state
) ir_assignment(deref_run_default_var
, true_val
);
5114 instructions
->push_tail(init_var
);
5116 /* Default case was the last one, no checks required. */
5117 if (after_default
.is_empty()) {
5118 instructions
->append_list(&default_case
);
5122 foreach_in_list(ir_instruction
, ir
, &after_default
) {
5123 ir_assignment
*assign
= ir
->as_assignment();
5128 /* Clone the check between case label and init expression. */
5129 ir_expression
*exp
= (ir_expression
*) assign
->condition
;
5130 ir_expression
*clone
= exp
->clone(state
, NULL
);
5132 ir_dereference_variable
*deref_var
=
5133 new(state
) ir_dereference_variable(state
->switch_state
.run_default
);
5134 ir_rvalue
*const false_val
= new (state
) ir_constant(false);
5136 ir_assignment
*const set_false
=
5137 new(state
) ir_assignment(deref_var
, false_val
, clone
);
5139 instructions
->push_tail(set_false
);
5142 /* Append default case and all cases after it. */
5143 instructions
->append_list(&default_case
);
5144 instructions
->append_list(&after_default
);
5147 /* Case statements do not have r-values. */
5152 ast_case_statement::hir(exec_list
*instructions
,
5153 struct _mesa_glsl_parse_state
*state
)
5155 labels
->hir(instructions
, state
);
5157 /* Guard case statements depending on fallthru state. */
5158 ir_dereference_variable
*const deref_fallthru_guard
=
5159 new(state
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
5160 ir_if
*const test_fallthru
= new(state
) ir_if(deref_fallthru_guard
);
5162 foreach_list_typed (ast_node
, stmt
, link
, & this->stmts
)
5163 stmt
->hir(& test_fallthru
->then_instructions
, state
);
5165 instructions
->push_tail(test_fallthru
);
5167 /* Case statements do not have r-values. */
5173 ast_case_label_list::hir(exec_list
*instructions
,
5174 struct _mesa_glsl_parse_state
*state
)
5176 foreach_list_typed (ast_case_label
, label
, link
, & this->labels
)
5177 label
->hir(instructions
, state
);
5179 /* Case labels do not have r-values. */
5184 ast_case_label::hir(exec_list
*instructions
,
5185 struct _mesa_glsl_parse_state
*state
)
5189 ir_dereference_variable
*deref_fallthru_var
=
5190 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
5192 ir_rvalue
*const true_val
= new(ctx
) ir_constant(true);
5194 /* If not default case, ... */
5195 if (this->test_value
!= NULL
) {
5196 /* Conditionally set fallthru state based on
5197 * comparison of cached test expression value to case label.
5199 ir_rvalue
*const label_rval
= this->test_value
->hir(instructions
, state
);
5200 ir_constant
*label_const
= label_rval
->constant_expression_value();
5203 YYLTYPE loc
= this->test_value
->get_location();
5205 _mesa_glsl_error(& loc
, state
,
5206 "switch statement case label must be a "
5207 "constant expression");
5209 /* Stuff a dummy value in to allow processing to continue. */
5210 label_const
= new(ctx
) ir_constant(0);
5212 ast_expression
*previous_label
= (ast_expression
*)
5213 hash_table_find(state
->switch_state
.labels_ht
,
5214 (void *)(uintptr_t)label_const
->value
.u
[0]);
5216 if (previous_label
) {
5217 YYLTYPE loc
= this->test_value
->get_location();
5218 _mesa_glsl_error(& loc
, state
, "duplicate case value");
5220 loc
= previous_label
->get_location();
5221 _mesa_glsl_error(& loc
, state
, "this is the previous case label");
5223 hash_table_insert(state
->switch_state
.labels_ht
,
5225 (void *)(uintptr_t)label_const
->value
.u
[0]);
5229 ir_dereference_variable
*deref_test_var
=
5230 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
5232 ir_expression
*test_cond
= new(ctx
) ir_expression(ir_binop_all_equal
,
5237 * From GLSL 4.40 specification section 6.2 ("Selection"):
5239 * "The type of the init-expression value in a switch statement must
5240 * be a scalar int or uint. The type of the constant-expression value
5241 * in a case label also must be a scalar int or uint. When any pair
5242 * of these values is tested for "equal value" and the types do not
5243 * match, an implicit conversion will be done to convert the int to a
5244 * uint (see section 4.1.10 “Implicit Conversions”) before the compare
5247 if (label_const
->type
!= state
->switch_state
.test_var
->type
) {
5248 YYLTYPE loc
= this->test_value
->get_location();
5250 const glsl_type
*type_a
= label_const
->type
;
5251 const glsl_type
*type_b
= state
->switch_state
.test_var
->type
;
5253 /* Check if int->uint implicit conversion is supported. */
5254 bool integer_conversion_supported
=
5255 glsl_type::int_type
->can_implicitly_convert_to(glsl_type::uint_type
,
5258 if ((!type_a
->is_integer() || !type_b
->is_integer()) ||
5259 !integer_conversion_supported
) {
5260 _mesa_glsl_error(&loc
, state
, "type mismatch with switch "
5261 "init-expression and case label (%s != %s)",
5262 type_a
->name
, type_b
->name
);
5264 /* Conversion of the case label. */
5265 if (type_a
->base_type
== GLSL_TYPE_INT
) {
5266 if (!apply_implicit_conversion(glsl_type::uint_type
,
5267 test_cond
->operands
[0], state
))
5268 _mesa_glsl_error(&loc
, state
, "implicit type conversion error");
5270 /* Conversion of the init-expression value. */
5271 if (!apply_implicit_conversion(glsl_type::uint_type
,
5272 test_cond
->operands
[1], state
))
5273 _mesa_glsl_error(&loc
, state
, "implicit type conversion error");
5278 ir_assignment
*set_fallthru_on_test
=
5279 new(ctx
) ir_assignment(deref_fallthru_var
, true_val
, test_cond
);
5281 instructions
->push_tail(set_fallthru_on_test
);
5282 } else { /* default case */
5283 if (state
->switch_state
.previous_default
) {
5284 YYLTYPE loc
= this->get_location();
5285 _mesa_glsl_error(& loc
, state
,
5286 "multiple default labels in one switch");
5288 loc
= state
->switch_state
.previous_default
->get_location();
5289 _mesa_glsl_error(& loc
, state
, "this is the first default label");
5291 state
->switch_state
.previous_default
= this;
5293 /* Set fallthru condition on 'run_default' bool. */
5294 ir_dereference_variable
*deref_run_default
=
5295 new(ctx
) ir_dereference_variable(state
->switch_state
.run_default
);
5296 ir_rvalue
*const cond_true
= new(ctx
) ir_constant(true);
5297 ir_expression
*test_cond
= new(ctx
) ir_expression(ir_binop_all_equal
,
5301 /* Set falltrhu state. */
5302 ir_assignment
*set_fallthru
=
5303 new(ctx
) ir_assignment(deref_fallthru_var
, true_val
, test_cond
);
5305 instructions
->push_tail(set_fallthru
);
5308 /* Case statements do not have r-values. */
5313 ast_iteration_statement::condition_to_hir(exec_list
*instructions
,
5314 struct _mesa_glsl_parse_state
*state
)
5318 if (condition
!= NULL
) {
5319 ir_rvalue
*const cond
=
5320 condition
->hir(instructions
, state
);
5323 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
5324 YYLTYPE loc
= condition
->get_location();
5326 _mesa_glsl_error(& loc
, state
,
5327 "loop condition must be scalar boolean");
5329 /* As the first code in the loop body, generate a block that looks
5330 * like 'if (!condition) break;' as the loop termination condition.
5332 ir_rvalue
*const not_cond
=
5333 new(ctx
) ir_expression(ir_unop_logic_not
, cond
);
5335 ir_if
*const if_stmt
= new(ctx
) ir_if(not_cond
);
5337 ir_jump
*const break_stmt
=
5338 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
5340 if_stmt
->then_instructions
.push_tail(break_stmt
);
5341 instructions
->push_tail(if_stmt
);
5348 ast_iteration_statement::hir(exec_list
*instructions
,
5349 struct _mesa_glsl_parse_state
*state
)
5353 /* For-loops and while-loops start a new scope, but do-while loops do not.
5355 if (mode
!= ast_do_while
)
5356 state
->symbols
->push_scope();
5358 if (init_statement
!= NULL
)
5359 init_statement
->hir(instructions
, state
);
5361 ir_loop
*const stmt
= new(ctx
) ir_loop();
5362 instructions
->push_tail(stmt
);
5364 /* Track the current loop nesting. */
5365 ast_iteration_statement
*nesting_ast
= state
->loop_nesting_ast
;
5367 state
->loop_nesting_ast
= this;
5369 /* Likewise, indicate that following code is closest to a loop,
5370 * NOT closest to a switch.
5372 bool saved_is_switch_innermost
= state
->switch_state
.is_switch_innermost
;
5373 state
->switch_state
.is_switch_innermost
= false;
5375 if (mode
!= ast_do_while
)
5376 condition_to_hir(&stmt
->body_instructions
, state
);
5379 body
->hir(& stmt
->body_instructions
, state
);
5381 if (rest_expression
!= NULL
)
5382 rest_expression
->hir(& stmt
->body_instructions
, state
);
5384 if (mode
== ast_do_while
)
5385 condition_to_hir(&stmt
->body_instructions
, state
);
5387 if (mode
!= ast_do_while
)
5388 state
->symbols
->pop_scope();
5390 /* Restore previous nesting before returning. */
5391 state
->loop_nesting_ast
= nesting_ast
;
5392 state
->switch_state
.is_switch_innermost
= saved_is_switch_innermost
;
5394 /* Loops do not have r-values.
5401 * Determine if the given type is valid for establishing a default precision
5404 * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
5406 * "The precision statement
5408 * precision precision-qualifier type;
5410 * can be used to establish a default precision qualifier. The type field
5411 * can be either int or float or any of the sampler types, and the
5412 * precision-qualifier can be lowp, mediump, or highp."
5414 * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
5415 * qualifiers on sampler types, but this seems like an oversight (since the
5416 * intention of including these in GLSL 1.30 is to allow compatibility with ES
5417 * shaders). So we allow int, float, and all sampler types regardless of GLSL
5421 is_valid_default_precision_type(const struct glsl_type
*const type
)
5426 switch (type
->base_type
) {
5428 case GLSL_TYPE_FLOAT
:
5429 /* "int" and "float" are valid, but vectors and matrices are not. */
5430 return type
->vector_elements
== 1 && type
->matrix_columns
== 1;
5431 case GLSL_TYPE_SAMPLER
:
5440 ast_type_specifier::hir(exec_list
*instructions
,
5441 struct _mesa_glsl_parse_state
*state
)
5443 if (this->default_precision
== ast_precision_none
&& this->structure
== NULL
)
5446 YYLTYPE loc
= this->get_location();
5448 /* If this is a precision statement, check that the type to which it is
5449 * applied is either float or int.
5451 * From section 4.5.3 of the GLSL 1.30 spec:
5452 * "The precision statement
5453 * precision precision-qualifier type;
5454 * can be used to establish a default precision qualifier. The type
5455 * field can be either int or float [...]. Any other types or
5456 * qualifiers will result in an error.
5458 if (this->default_precision
!= ast_precision_none
) {
5459 if (!state
->check_precision_qualifiers_allowed(&loc
))
5462 if (this->structure
!= NULL
) {
5463 _mesa_glsl_error(&loc
, state
,
5464 "precision qualifiers do not apply to structures");
5468 if (this->array_specifier
!= NULL
) {
5469 _mesa_glsl_error(&loc
, state
,
5470 "default precision statements do not apply to "
5475 const struct glsl_type
*const type
=
5476 state
->symbols
->get_type(this->type_name
);
5477 if (!is_valid_default_precision_type(type
)) {
5478 _mesa_glsl_error(&loc
, state
,
5479 "default precision statements apply only to "
5480 "float, int, and sampler types");
5484 if (type
->base_type
== GLSL_TYPE_FLOAT
5486 && state
->stage
== MESA_SHADER_FRAGMENT
) {
5487 /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
5490 * "The fragment language has no default precision qualifier for
5491 * floating point types."
5493 * As a result, we have to track whether or not default precision has
5494 * been specified for float in GLSL ES fragment shaders.
5496 * Earlier in that same section, the spec says:
5498 * "Non-precision qualified declarations will use the precision
5499 * qualifier specified in the most recent precision statement
5500 * that is still in scope. The precision statement has the same
5501 * scoping rules as variable declarations. If it is declared
5502 * inside a compound statement, its effect stops at the end of
5503 * the innermost statement it was declared in. Precision
5504 * statements in nested scopes override precision statements in
5505 * outer scopes. Multiple precision statements for the same basic
5506 * type can appear inside the same scope, with later statements
5507 * overriding earlier statements within that scope."
5509 * Default precision specifications follow the same scope rules as
5510 * variables. So, we can track the state of the default float
5511 * precision in the symbol table, and the rules will just work. This
5512 * is a slight abuse of the symbol table, but it has the semantics
5515 ir_variable
*const junk
=
5516 new(state
) ir_variable(type
, "#default precision",
5519 state
->symbols
->add_variable(junk
);
5522 /* FINISHME: Translate precision statements into IR. */
5526 /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
5527 * process_record_constructor() can do type-checking on C-style initializer
5528 * expressions of structs, but ast_struct_specifier should only be translated
5529 * to HIR if it is declaring the type of a structure.
5531 * The ->is_declaration field is false for initializers of variables
5532 * declared separately from the struct's type definition.
5534 * struct S { ... }; (is_declaration = true)
5535 * struct T { ... } t = { ... }; (is_declaration = true)
5536 * S s = { ... }; (is_declaration = false)
5538 if (this->structure
!= NULL
&& this->structure
->is_declaration
)
5539 return this->structure
->hir(instructions
, state
);
5546 * Process a structure or interface block tree into an array of structure fields
5548 * After parsing, where there are some syntax differnces, structures and
5549 * interface blocks are almost identical. They are similar enough that the
5550 * AST for each can be processed the same way into a set of
5551 * \c glsl_struct_field to describe the members.
5553 * If we're processing an interface block, var_mode should be the type of the
5554 * interface block (ir_var_shader_in, ir_var_shader_out, ir_var_uniform or
5555 * ir_var_shader_storage). If we're processing a structure, var_mode should be
5559 * The number of fields processed. A pointer to the array structure fields is
5560 * stored in \c *fields_ret.
5563 ast_process_structure_or_interface_block(exec_list
*instructions
,
5564 struct _mesa_glsl_parse_state
*state
,
5565 exec_list
*declarations
,
5567 glsl_struct_field
**fields_ret
,
5569 enum glsl_matrix_layout matrix_layout
,
5570 bool allow_reserved_names
,
5571 ir_variable_mode var_mode
)
5573 unsigned decl_count
= 0;
5575 /* Make an initial pass over the list of fields to determine how
5576 * many there are. Each element in this list is an ast_declarator_list.
5577 * This means that we actually need to count the number of elements in the
5578 * 'declarations' list in each of the elements.
5580 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
5581 decl_count
+= decl_list
->declarations
.length();
5584 /* Allocate storage for the fields and process the field
5585 * declarations. As the declarations are processed, try to also convert
5586 * the types to HIR. This ensures that structure definitions embedded in
5587 * other structure definitions or in interface blocks are processed.
5589 glsl_struct_field
*const fields
= ralloc_array(state
, glsl_struct_field
,
5593 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
5594 const char *type_name
;
5596 decl_list
->type
->specifier
->hir(instructions
, state
);
5598 /* Section 10.9 of the GLSL ES 1.00 specification states that
5599 * embedded structure definitions have been removed from the language.
5601 if (state
->es_shader
&& decl_list
->type
->specifier
->structure
!= NULL
) {
5602 _mesa_glsl_error(&loc
, state
, "embedded structure definitions are "
5603 "not allowed in GLSL ES 1.00");
5606 const glsl_type
*decl_type
=
5607 decl_list
->type
->glsl_type(& type_name
, state
);
5609 foreach_list_typed (ast_declaration
, decl
, link
,
5610 &decl_list
->declarations
) {
5611 if (!allow_reserved_names
)
5612 validate_identifier(decl
->identifier
, loc
, state
);
5614 /* From section 4.3.9 of the GLSL 4.40 spec:
5616 * "[In interface blocks] opaque types are not allowed."
5618 * It should be impossible for decl_type to be NULL here. Cases that
5619 * might naturally lead to decl_type being NULL, especially for the
5620 * is_interface case, will have resulted in compilation having
5621 * already halted due to a syntax error.
5623 const struct glsl_type
*field_type
=
5624 decl_type
!= NULL
? decl_type
: glsl_type::error_type
;
5626 if (is_interface
&& field_type
->contains_opaque()) {
5627 YYLTYPE loc
= decl_list
->get_location();
5628 _mesa_glsl_error(&loc
, state
,
5629 "uniform/buffer in non-default interface block contains "
5633 if (field_type
->contains_atomic()) {
5634 /* FINISHME: Add a spec quotation here once updated spec
5635 * FINISHME: language is available. See Khronos bug #10903
5636 * FINISHME: on whether atomic counters are allowed in
5637 * FINISHME: structures.
5639 YYLTYPE loc
= decl_list
->get_location();
5640 _mesa_glsl_error(&loc
, state
, "atomic counter in structure, "
5641 "shader storage block or uniform block");
5644 if (field_type
->contains_image()) {
5645 /* FINISHME: Same problem as with atomic counters.
5646 * FINISHME: Request clarification from Khronos and add
5647 * FINISHME: spec quotation here.
5649 YYLTYPE loc
= decl_list
->get_location();
5650 _mesa_glsl_error(&loc
, state
,
5651 "image in structure, shader storage block or "
5655 const struct ast_type_qualifier
*const qual
=
5656 & decl_list
->type
->qualifier
;
5657 if (qual
->flags
.q
.std140
||
5658 qual
->flags
.q
.packed
||
5659 qual
->flags
.q
.shared
) {
5660 _mesa_glsl_error(&loc
, state
,
5661 "uniform/shader storage block layout qualifiers "
5662 "std140, packed, and shared can only be applied "
5663 "to uniform/shader storage blocks, not members");
5666 if (qual
->flags
.q
.constant
) {
5667 YYLTYPE loc
= decl_list
->get_location();
5668 _mesa_glsl_error(&loc
, state
,
5669 "const storage qualifier cannot be applied "
5670 "to struct or interface block members");
5673 field_type
= process_array_type(&loc
, decl_type
,
5674 decl
->array_specifier
, state
);
5675 fields
[i
].type
= field_type
;
5676 fields
[i
].name
= decl
->identifier
;
5677 fields
[i
].location
= -1;
5678 fields
[i
].interpolation
=
5679 interpret_interpolation_qualifier(qual
, var_mode
, state
, &loc
);
5680 fields
[i
].centroid
= qual
->flags
.q
.centroid
? 1 : 0;
5681 fields
[i
].sample
= qual
->flags
.q
.sample
? 1 : 0;
5682 fields
[i
].patch
= qual
->flags
.q
.patch
? 1 : 0;
5684 /* Only save explicitly defined streams in block's field */
5685 fields
[i
].stream
= qual
->flags
.q
.explicit_stream
? qual
->stream
: -1;
5687 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
5688 if (!qual
->flags
.q
.uniform
&& !qual
->flags
.q
.buffer
) {
5689 _mesa_glsl_error(&loc
, state
,
5690 "row_major and column_major can only be "
5691 "applied to interface blocks");
5693 validate_matrix_layout_for_type(state
, &loc
, field_type
, NULL
);
5696 if (qual
->flags
.q
.uniform
&& qual
->has_interpolation()) {
5697 _mesa_glsl_error(&loc
, state
,
5698 "interpolation qualifiers cannot be used "
5699 "with uniform interface blocks");
5702 if ((qual
->flags
.q
.uniform
|| !is_interface
) &&
5703 qual
->has_auxiliary_storage()) {
5704 _mesa_glsl_error(&loc
, state
,
5705 "auxiliary storage qualifiers cannot be used "
5706 "in uniform blocks or structures.");
5709 /* Propogate row- / column-major information down the fields of the
5710 * structure or interface block. Structures need this data because
5711 * the structure may contain a structure that contains ... a matrix
5712 * that need the proper layout.
5714 if (field_type
->without_array()->is_matrix()
5715 || field_type
->without_array()->is_record()) {
5716 /* If no layout is specified for the field, inherit the layout
5719 fields
[i
].matrix_layout
= matrix_layout
;
5721 if (qual
->flags
.q
.row_major
)
5722 fields
[i
].matrix_layout
= GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
5723 else if (qual
->flags
.q
.column_major
)
5724 fields
[i
].matrix_layout
= GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
;
5726 /* If we're processing an interface block, the matrix layout must
5727 * be decided by this point.
5729 assert(!is_interface
5730 || fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
5731 || fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
);
5738 assert(i
== decl_count
);
5740 *fields_ret
= fields
;
5746 ast_struct_specifier::hir(exec_list
*instructions
,
5747 struct _mesa_glsl_parse_state
*state
)
5749 YYLTYPE loc
= this->get_location();
5751 /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
5753 * "Anonymous structures are not supported; so embedded structures must
5754 * have a declarator. A name given to an embedded struct is scoped at
5755 * the same level as the struct it is embedded in."
5757 * The same section of the GLSL 1.20 spec says:
5759 * "Anonymous structures are not supported. Embedded structures are not
5762 * struct S { float f; };
5764 * S; // Error: anonymous structures disallowed
5765 * struct { ... }; // Error: embedded structures disallowed
5766 * S s; // Okay: nested structures with name are allowed
5769 * The GLSL ES 1.00 and 3.00 specs have similar langauge and examples. So,
5770 * we allow embedded structures in 1.10 only.
5772 if (state
->language_version
!= 110 && state
->struct_specifier_depth
!= 0)
5773 _mesa_glsl_error(&loc
, state
,
5774 "embedded structure declarations are not allowed");
5776 state
->struct_specifier_depth
++;
5778 glsl_struct_field
*fields
;
5779 unsigned decl_count
=
5780 ast_process_structure_or_interface_block(instructions
,
5782 &this->declarations
,
5786 GLSL_MATRIX_LAYOUT_INHERITED
,
5787 false /* allow_reserved_names */,
5790 validate_identifier(this->name
, loc
, state
);
5792 const glsl_type
*t
=
5793 glsl_type::get_record_instance(fields
, decl_count
, this->name
);
5795 if (!state
->symbols
->add_type(name
, t
)) {
5796 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
5798 const glsl_type
**s
= reralloc(state
, state
->user_structures
,
5800 state
->num_user_structures
+ 1);
5802 s
[state
->num_user_structures
] = t
;
5803 state
->user_structures
= s
;
5804 state
->num_user_structures
++;
5808 state
->struct_specifier_depth
--;
5810 /* Structure type definitions do not have r-values.
5817 * Visitor class which detects whether a given interface block has been used.
5819 class interface_block_usage_visitor
: public ir_hierarchical_visitor
5822 interface_block_usage_visitor(ir_variable_mode mode
, const glsl_type
*block
)
5823 : mode(mode
), block(block
), found(false)
5827 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
5829 if (ir
->var
->data
.mode
== mode
&& ir
->var
->get_interface_type() == block
) {
5833 return visit_continue
;
5836 bool usage_found() const
5842 ir_variable_mode mode
;
5843 const glsl_type
*block
;
5849 ast_interface_block::hir(exec_list
*instructions
,
5850 struct _mesa_glsl_parse_state
*state
)
5852 YYLTYPE loc
= this->get_location();
5854 /* Interface blocks must be declared at global scope */
5855 if (state
->current_function
!= NULL
) {
5856 _mesa_glsl_error(&loc
, state
,
5857 "Interface block `%s' must be declared "
5862 /* The ast_interface_block has a list of ast_declarator_lists. We
5863 * need to turn those into ir_variables with an association
5864 * with this uniform block.
5866 enum glsl_interface_packing packing
;
5867 if (this->layout
.flags
.q
.shared
) {
5868 packing
= GLSL_INTERFACE_PACKING_SHARED
;
5869 } else if (this->layout
.flags
.q
.packed
) {
5870 packing
= GLSL_INTERFACE_PACKING_PACKED
;
5872 /* The default layout is std140.
5874 packing
= GLSL_INTERFACE_PACKING_STD140
;
5877 ir_variable_mode var_mode
;
5878 const char *iface_type_name
;
5879 if (this->layout
.flags
.q
.in
) {
5880 var_mode
= ir_var_shader_in
;
5881 iface_type_name
= "in";
5882 } else if (this->layout
.flags
.q
.out
) {
5883 var_mode
= ir_var_shader_out
;
5884 iface_type_name
= "out";
5885 } else if (this->layout
.flags
.q
.uniform
) {
5886 var_mode
= ir_var_uniform
;
5887 iface_type_name
= "uniform";
5888 } else if (this->layout
.flags
.q
.buffer
) {
5889 var_mode
= ir_var_shader_storage
;
5890 iface_type_name
= "buffer";
5892 var_mode
= ir_var_auto
;
5893 iface_type_name
= "UNKNOWN";
5894 assert(!"interface block layout qualifier not found!");
5897 enum glsl_matrix_layout matrix_layout
= GLSL_MATRIX_LAYOUT_INHERITED
;
5898 if (this->layout
.flags
.q
.row_major
)
5899 matrix_layout
= GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
5900 else if (this->layout
.flags
.q
.column_major
)
5901 matrix_layout
= GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
;
5903 bool redeclaring_per_vertex
= strcmp(this->block_name
, "gl_PerVertex") == 0;
5904 exec_list declared_variables
;
5905 glsl_struct_field
*fields
;
5907 /* Treat an interface block as one level of nesting, so that embedded struct
5908 * specifiers will be disallowed.
5910 state
->struct_specifier_depth
++;
5912 unsigned int num_variables
=
5913 ast_process_structure_or_interface_block(&declared_variables
,
5915 &this->declarations
,
5920 redeclaring_per_vertex
,
5923 state
->struct_specifier_depth
--;
5925 if (!redeclaring_per_vertex
) {
5926 validate_identifier(this->block_name
, loc
, state
);
5928 /* From section 4.3.9 ("Interface Blocks") of the GLSL 4.50 spec:
5930 * "Block names have no other use within a shader beyond interface
5931 * matching; it is a compile-time error to use a block name at global
5932 * scope for anything other than as a block name."
5934 ir_variable
*var
= state
->symbols
->get_variable(this->block_name
);
5935 if (var
&& !var
->type
->is_interface()) {
5936 _mesa_glsl_error(&loc
, state
, "Block name `%s' is "
5937 "already used in the scope.",
5942 const glsl_type
*earlier_per_vertex
= NULL
;
5943 if (redeclaring_per_vertex
) {
5944 /* Find the previous declaration of gl_PerVertex. If we're redeclaring
5945 * the named interface block gl_in, we can find it by looking at the
5946 * previous declaration of gl_in. Otherwise we can find it by looking
5947 * at the previous decalartion of any of the built-in outputs,
5950 * Also check that the instance name and array-ness of the redeclaration
5954 case ir_var_shader_in
:
5955 if (ir_variable
*earlier_gl_in
=
5956 state
->symbols
->get_variable("gl_in")) {
5957 earlier_per_vertex
= earlier_gl_in
->get_interface_type();
5959 _mesa_glsl_error(&loc
, state
,
5960 "redeclaration of gl_PerVertex input not allowed "
5962 _mesa_shader_stage_to_string(state
->stage
));
5964 if (this->instance_name
== NULL
||
5965 strcmp(this->instance_name
, "gl_in") != 0 || this->array_specifier
== NULL
) {
5966 _mesa_glsl_error(&loc
, state
,
5967 "gl_PerVertex input must be redeclared as "
5971 case ir_var_shader_out
:
5972 if (ir_variable
*earlier_gl_Position
=
5973 state
->symbols
->get_variable("gl_Position")) {
5974 earlier_per_vertex
= earlier_gl_Position
->get_interface_type();
5975 } else if (ir_variable
*earlier_gl_out
=
5976 state
->symbols
->get_variable("gl_out")) {
5977 earlier_per_vertex
= earlier_gl_out
->get_interface_type();
5979 _mesa_glsl_error(&loc
, state
,
5980 "redeclaration of gl_PerVertex output not "
5981 "allowed in the %s shader",
5982 _mesa_shader_stage_to_string(state
->stage
));
5984 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
5985 if (this->instance_name
== NULL
||
5986 strcmp(this->instance_name
, "gl_out") != 0 || this->array_specifier
== NULL
) {
5987 _mesa_glsl_error(&loc
, state
,
5988 "gl_PerVertex output must be redeclared as "
5992 if (this->instance_name
!= NULL
) {
5993 _mesa_glsl_error(&loc
, state
,
5994 "gl_PerVertex output may not be redeclared with "
5995 "an instance name");
6000 _mesa_glsl_error(&loc
, state
,
6001 "gl_PerVertex must be declared as an input or an "
6006 if (earlier_per_vertex
== NULL
) {
6007 /* An error has already been reported. Bail out to avoid null
6008 * dereferences later in this function.
6013 /* Copy locations from the old gl_PerVertex interface block. */
6014 for (unsigned i
= 0; i
< num_variables
; i
++) {
6015 int j
= earlier_per_vertex
->field_index(fields
[i
].name
);
6017 _mesa_glsl_error(&loc
, state
,
6018 "redeclaration of gl_PerVertex must be a subset "
6019 "of the built-in members of gl_PerVertex");
6021 fields
[i
].location
=
6022 earlier_per_vertex
->fields
.structure
[j
].location
;
6023 fields
[i
].interpolation
=
6024 earlier_per_vertex
->fields
.structure
[j
].interpolation
;
6025 fields
[i
].centroid
=
6026 earlier_per_vertex
->fields
.structure
[j
].centroid
;
6028 earlier_per_vertex
->fields
.structure
[j
].sample
;
6030 earlier_per_vertex
->fields
.structure
[j
].patch
;
6034 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10
6037 * If a built-in interface block is redeclared, it must appear in
6038 * the shader before any use of any member included in the built-in
6039 * declaration, or a compilation error will result.
6041 * This appears to be a clarification to the behaviour established for
6042 * gl_PerVertex by GLSL 1.50, therefore we implement this behaviour
6043 * regardless of GLSL version.
6045 interface_block_usage_visitor
v(var_mode
, earlier_per_vertex
);
6046 v
.run(instructions
);
6047 if (v
.usage_found()) {
6048 _mesa_glsl_error(&loc
, state
,
6049 "redeclaration of a built-in interface block must "
6050 "appear before any use of any member of the "
6055 const glsl_type
*block_type
=
6056 glsl_type::get_interface_instance(fields
,
6061 if (!state
->symbols
->add_interface(block_type
->name
, block_type
, var_mode
)) {
6062 YYLTYPE loc
= this->get_location();
6063 _mesa_glsl_error(&loc
, state
, "interface block `%s' with type `%s' "
6064 "already taken in the current scope",
6065 this->block_name
, iface_type_name
);
6068 /* Since interface blocks cannot contain statements, it should be
6069 * impossible for the block to generate any instructions.
6071 assert(declared_variables
.is_empty());
6073 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
6075 * Geometry shader input variables get the per-vertex values written
6076 * out by vertex shader output variables of the same names. Since a
6077 * geometry shader operates on a set of vertices, each input varying
6078 * variable (or input block, see interface blocks below) needs to be
6079 * declared as an array.
6081 if (state
->stage
== MESA_SHADER_GEOMETRY
&& this->array_specifier
== NULL
&&
6082 var_mode
== ir_var_shader_in
) {
6083 _mesa_glsl_error(&loc
, state
, "geometry shader inputs must be arrays");
6084 } else if ((state
->stage
== MESA_SHADER_TESS_CTRL
||
6085 state
->stage
== MESA_SHADER_TESS_EVAL
) &&
6086 this->array_specifier
== NULL
&&
6087 var_mode
== ir_var_shader_in
) {
6088 _mesa_glsl_error(&loc
, state
, "per-vertex tessellation shader inputs must be arrays");
6089 } else if (state
->stage
== MESA_SHADER_TESS_CTRL
&&
6090 this->array_specifier
== NULL
&&
6091 var_mode
== ir_var_shader_out
) {
6092 _mesa_glsl_error(&loc
, state
, "tessellation control shader outputs must be arrays");
6096 /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
6099 * "If an instance name (instance-name) is used, then it puts all the
6100 * members inside a scope within its own name space, accessed with the
6101 * field selector ( . ) operator (analogously to structures)."
6103 if (this->instance_name
) {
6104 if (redeclaring_per_vertex
) {
6105 /* When a built-in in an unnamed interface block is redeclared,
6106 * get_variable_being_redeclared() calls
6107 * check_builtin_array_max_size() to make sure that built-in array
6108 * variables aren't redeclared to illegal sizes. But we're looking
6109 * at a redeclaration of a named built-in interface block. So we
6110 * have to manually call check_builtin_array_max_size() for all parts
6111 * of the interface that are arrays.
6113 for (unsigned i
= 0; i
< num_variables
; i
++) {
6114 if (fields
[i
].type
->is_array()) {
6115 const unsigned size
= fields
[i
].type
->array_size();
6116 check_builtin_array_max_size(fields
[i
].name
, size
, loc
, state
);
6120 validate_identifier(this->instance_name
, loc
, state
);
6125 if (this->array_specifier
!= NULL
) {
6126 /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
6128 * For uniform blocks declared an array, each individual array
6129 * element corresponds to a separate buffer object backing one
6130 * instance of the block. As the array size indicates the number
6131 * of buffer objects needed, uniform block array declarations
6132 * must specify an array size.
6134 * And a few paragraphs later:
6136 * Geometry shader input blocks must be declared as arrays and
6137 * follow the array declaration and linking rules for all
6138 * geometry shader inputs. All other input and output block
6139 * arrays must specify an array size.
6141 * The same applies to tessellation shaders.
6143 * The upshot of this is that the only circumstance where an
6144 * interface array size *doesn't* need to be specified is on a
6145 * geometry shader input, tessellation control shader input,
6146 * tessellation control shader output, and tessellation evaluation
6149 if (this->array_specifier
->is_unsized_array
) {
6150 bool allow_inputs
= state
->stage
== MESA_SHADER_GEOMETRY
||
6151 state
->stage
== MESA_SHADER_TESS_CTRL
||
6152 state
->stage
== MESA_SHADER_TESS_EVAL
;
6153 bool allow_outputs
= state
->stage
== MESA_SHADER_TESS_CTRL
;
6155 if (this->layout
.flags
.q
.in
) {
6157 _mesa_glsl_error(&loc
, state
,
6158 "unsized input block arrays not allowed in "
6160 _mesa_shader_stage_to_string(state
->stage
));
6161 } else if (this->layout
.flags
.q
.out
) {
6163 _mesa_glsl_error(&loc
, state
,
6164 "unsized output block arrays not allowed in "
6166 _mesa_shader_stage_to_string(state
->stage
));
6168 /* by elimination, this is a uniform block array */
6169 _mesa_glsl_error(&loc
, state
,
6170 "unsized uniform block arrays not allowed in "
6172 _mesa_shader_stage_to_string(state
->stage
));
6176 const glsl_type
*block_array_type
=
6177 process_array_type(&loc
, block_type
, this->array_specifier
, state
);
6179 /* From section 4.3.9 (Interface Blocks) of the GLSL ES 3.10 spec:
6181 * * Arrays of arrays of blocks are not allowed
6183 if (state
->es_shader
&& block_array_type
->is_array() &&
6184 block_array_type
->fields
.array
->is_array()) {
6185 _mesa_glsl_error(&loc
, state
,
6186 "arrays of arrays interface blocks are "
6190 var
= new(state
) ir_variable(block_array_type
,
6191 this->instance_name
,
6194 var
= new(state
) ir_variable(block_type
,
6195 this->instance_name
,
6199 var
->data
.matrix_layout
= matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
6200 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
: matrix_layout
;
6202 if (var_mode
== ir_var_shader_in
|| var_mode
== ir_var_uniform
)
6203 var
->data
.read_only
= true;
6205 if (state
->stage
== MESA_SHADER_GEOMETRY
&& var_mode
== ir_var_shader_in
)
6206 handle_geometry_shader_input_decl(state
, loc
, var
);
6207 else if ((state
->stage
== MESA_SHADER_TESS_CTRL
||
6208 state
->stage
== MESA_SHADER_TESS_EVAL
) && var_mode
== ir_var_shader_in
)
6209 handle_tess_shader_input_decl(state
, loc
, var
);
6210 else if (state
->stage
== MESA_SHADER_TESS_CTRL
&& var_mode
== ir_var_shader_out
)
6211 handle_tess_ctrl_shader_output_decl(state
, loc
, var
);
6213 if (ir_variable
*earlier
=
6214 state
->symbols
->get_variable(this->instance_name
)) {
6215 if (!redeclaring_per_vertex
) {
6216 _mesa_glsl_error(&loc
, state
, "`%s' redeclared",
6217 this->instance_name
);
6219 earlier
->data
.how_declared
= ir_var_declared_normally
;
6220 earlier
->type
= var
->type
;
6221 earlier
->reinit_interface_type(block_type
);
6224 /* Propagate the "binding" keyword into this UBO's fields;
6225 * the UBO declaration itself doesn't get an ir_variable unless it
6226 * has an instance name. This is ugly.
6228 var
->data
.explicit_binding
= this->layout
.flags
.q
.explicit_binding
;
6229 var
->data
.binding
= this->layout
.binding
;
6231 state
->symbols
->add_variable(var
);
6232 instructions
->push_tail(var
);
6235 /* In order to have an array size, the block must also be declared with
6238 assert(this->array_specifier
== NULL
);
6240 for (unsigned i
= 0; i
< num_variables
; i
++) {
6242 new(state
) ir_variable(fields
[i
].type
,
6243 ralloc_strdup(state
, fields
[i
].name
),
6245 var
->data
.interpolation
= fields
[i
].interpolation
;
6246 var
->data
.centroid
= fields
[i
].centroid
;
6247 var
->data
.sample
= fields
[i
].sample
;
6248 var
->data
.patch
= fields
[i
].patch
;
6249 var
->init_interface_type(block_type
);
6251 if (var_mode
== ir_var_shader_in
|| var_mode
== ir_var_uniform
)
6252 var
->data
.read_only
= true;
6254 if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
) {
6255 var
->data
.matrix_layout
= matrix_layout
== GLSL_MATRIX_LAYOUT_INHERITED
6256 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
: matrix_layout
;
6258 var
->data
.matrix_layout
= fields
[i
].matrix_layout
;
6261 if (fields
[i
].stream
!= -1 &&
6262 ((unsigned)fields
[i
].stream
) != this->layout
.stream
) {
6263 _mesa_glsl_error(&loc
, state
,
6264 "stream layout qualifier on "
6265 "interface block member `%s' does not match "
6266 "the interface block (%d vs %d)",
6267 var
->name
, fields
[i
].stream
, this->layout
.stream
);
6270 var
->data
.stream
= this->layout
.stream
;
6272 /* Examine var name here since var may get deleted in the next call */
6273 bool var_is_gl_id
= is_gl_identifier(var
->name
);
6275 if (redeclaring_per_vertex
) {
6276 ir_variable
*earlier
=
6277 get_variable_being_redeclared(var
, loc
, state
,
6278 true /* allow_all_redeclarations */);
6279 if (!var_is_gl_id
|| earlier
== NULL
) {
6280 _mesa_glsl_error(&loc
, state
,
6281 "redeclaration of gl_PerVertex can only "
6282 "include built-in variables");
6283 } else if (earlier
->data
.how_declared
== ir_var_declared_normally
) {
6284 _mesa_glsl_error(&loc
, state
,
6285 "`%s' has already been redeclared",
6288 earlier
->data
.how_declared
= ir_var_declared_in_block
;
6289 earlier
->reinit_interface_type(block_type
);
6294 if (state
->symbols
->get_variable(var
->name
) != NULL
)
6295 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", var
->name
);
6297 /* Propagate the "binding" keyword into this UBO/SSBO's fields.
6298 * The UBO declaration itself doesn't get an ir_variable unless it
6299 * has an instance name. This is ugly.
6301 var
->data
.explicit_binding
= this->layout
.flags
.q
.explicit_binding
;
6302 var
->data
.binding
= this->layout
.binding
;
6304 state
->symbols
->add_variable(var
);
6305 instructions
->push_tail(var
);
6308 if (redeclaring_per_vertex
&& block_type
!= earlier_per_vertex
) {
6309 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec:
6311 * It is also a compilation error ... to redeclare a built-in
6312 * block and then use a member from that built-in block that was
6313 * not included in the redeclaration.
6315 * This appears to be a clarification to the behaviour established
6316 * for gl_PerVertex by GLSL 1.50, therefore we implement this
6317 * behaviour regardless of GLSL version.
6319 * To prevent the shader from using a member that was not included in
6320 * the redeclaration, we disable any ir_variables that are still
6321 * associated with the old declaration of gl_PerVertex (since we've
6322 * already updated all of the variables contained in the new
6323 * gl_PerVertex to point to it).
6325 * As a side effect this will prevent
6326 * validate_intrastage_interface_blocks() from getting confused and
6327 * thinking there are conflicting definitions of gl_PerVertex in the
6330 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
6331 ir_variable
*const var
= node
->as_variable();
6333 var
->get_interface_type() == earlier_per_vertex
&&
6334 var
->data
.mode
== var_mode
) {
6335 if (var
->data
.how_declared
== ir_var_declared_normally
) {
6336 _mesa_glsl_error(&loc
, state
,
6337 "redeclaration of gl_PerVertex cannot "
6338 "follow a redeclaration of `%s'",
6341 state
->symbols
->disable_variable(var
->name
);
6353 ast_tcs_output_layout::hir(exec_list
*instructions
,
6354 struct _mesa_glsl_parse_state
*state
)
6356 YYLTYPE loc
= this->get_location();
6358 /* If any tessellation control output layout declaration preceded this
6359 * one, make sure it was consistent with this one.
6361 if (state
->tcs_output_vertices_specified
&&
6362 state
->out_qualifier
->vertices
!= this->vertices
) {
6363 _mesa_glsl_error(&loc
, state
,
6364 "tessellation control shader output layout does not "
6365 "match previous declaration");
6369 /* If any shader outputs occurred before this declaration and specified an
6370 * array size, make sure the size they specified is consistent with the
6373 unsigned num_vertices
= this->vertices
;
6374 if (state
->tcs_output_size
!= 0 && state
->tcs_output_size
!= num_vertices
) {
6375 _mesa_glsl_error(&loc
, state
,
6376 "this tessellation control shader output layout "
6377 "specifies %u vertices, but a previous output "
6378 "is declared with size %u",
6379 num_vertices
, state
->tcs_output_size
);
6383 state
->tcs_output_vertices_specified
= true;
6385 /* If any shader outputs occurred before this declaration and did not
6386 * specify an array size, their size is determined now.
6388 foreach_in_list (ir_instruction
, node
, instructions
) {
6389 ir_variable
*var
= node
->as_variable();
6390 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_out
)
6393 /* Note: Not all tessellation control shader output are arrays. */
6394 if (!var
->type
->is_unsized_array() || var
->data
.patch
)
6397 if (var
->data
.max_array_access
>= num_vertices
) {
6398 _mesa_glsl_error(&loc
, state
,
6399 "this tessellation control shader output layout "
6400 "specifies %u vertices, but an access to element "
6401 "%u of output `%s' already exists", num_vertices
,
6402 var
->data
.max_array_access
, var
->name
);
6404 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
6414 ast_gs_input_layout::hir(exec_list
*instructions
,
6415 struct _mesa_glsl_parse_state
*state
)
6417 YYLTYPE loc
= this->get_location();
6419 /* If any geometry input layout declaration preceded this one, make sure it
6420 * was consistent with this one.
6422 if (state
->gs_input_prim_type_specified
&&
6423 state
->in_qualifier
->prim_type
!= this->prim_type
) {
6424 _mesa_glsl_error(&loc
, state
,
6425 "geometry shader input layout does not match"
6426 " previous declaration");
6430 /* If any shader inputs occurred before this declaration and specified an
6431 * array size, make sure the size they specified is consistent with the
6434 unsigned num_vertices
= vertices_per_prim(this->prim_type
);
6435 if (state
->gs_input_size
!= 0 && state
->gs_input_size
!= num_vertices
) {
6436 _mesa_glsl_error(&loc
, state
,
6437 "this geometry shader input layout implies %u vertices"
6438 " per primitive, but a previous input is declared"
6439 " with size %u", num_vertices
, state
->gs_input_size
);
6443 state
->gs_input_prim_type_specified
= true;
6445 /* If any shader inputs occurred before this declaration and did not
6446 * specify an array size, their size is determined now.
6448 foreach_in_list(ir_instruction
, node
, instructions
) {
6449 ir_variable
*var
= node
->as_variable();
6450 if (var
== NULL
|| var
->data
.mode
!= ir_var_shader_in
)
6453 /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
6457 if (var
->type
->is_unsized_array()) {
6458 if (var
->data
.max_array_access
>= num_vertices
) {
6459 _mesa_glsl_error(&loc
, state
,
6460 "this geometry shader input layout implies %u"
6461 " vertices, but an access to element %u of input"
6462 " `%s' already exists", num_vertices
,
6463 var
->data
.max_array_access
, var
->name
);
6465 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
6476 ast_cs_input_layout::hir(exec_list
*instructions
,
6477 struct _mesa_glsl_parse_state
*state
)
6479 YYLTYPE loc
= this->get_location();
6481 /* If any compute input layout declaration preceded this one, make sure it
6482 * was consistent with this one.
6484 if (state
->cs_input_local_size_specified
) {
6485 for (int i
= 0; i
< 3; i
++) {
6486 if (state
->cs_input_local_size
[i
] != this->local_size
[i
]) {
6487 _mesa_glsl_error(&loc
, state
,
6488 "compute shader input layout does not match"
6489 " previous declaration");
6495 /* From the ARB_compute_shader specification:
6497 * If the local size of the shader in any dimension is greater
6498 * than the maximum size supported by the implementation for that
6499 * dimension, a compile-time error results.
6501 * It is not clear from the spec how the error should be reported if
6502 * the total size of the work group exceeds
6503 * MAX_COMPUTE_WORK_GROUP_INVOCATIONS, but it seems reasonable to
6504 * report it at compile time as well.
6506 GLuint64 total_invocations
= 1;
6507 for (int i
= 0; i
< 3; i
++) {
6508 if (this->local_size
[i
] > state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]) {
6509 _mesa_glsl_error(&loc
, state
,
6510 "local_size_%c exceeds MAX_COMPUTE_WORK_GROUP_SIZE"
6512 state
->ctx
->Const
.MaxComputeWorkGroupSize
[i
]);
6515 total_invocations
*= this->local_size
[i
];
6516 if (total_invocations
>
6517 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
) {
6518 _mesa_glsl_error(&loc
, state
,
6519 "product of local_sizes exceeds "
6520 "MAX_COMPUTE_WORK_GROUP_INVOCATIONS (%d)",
6521 state
->ctx
->Const
.MaxComputeWorkGroupInvocations
);
6526 state
->cs_input_local_size_specified
= true;
6527 for (int i
= 0; i
< 3; i
++)
6528 state
->cs_input_local_size
[i
] = this->local_size
[i
];
6530 /* We may now declare the built-in constant gl_WorkGroupSize (see
6531 * builtin_variable_generator::generate_constants() for why we didn't
6532 * declare it earlier).
6534 ir_variable
*var
= new(state
->symbols
)
6535 ir_variable(glsl_type::uvec3_type
, "gl_WorkGroupSize", ir_var_auto
);
6536 var
->data
.how_declared
= ir_var_declared_implicitly
;
6537 var
->data
.read_only
= true;
6538 instructions
->push_tail(var
);
6539 state
->symbols
->add_variable(var
);
6540 ir_constant_data data
;
6541 memset(&data
, 0, sizeof(data
));
6542 for (int i
= 0; i
< 3; i
++)
6543 data
.u
[i
] = this->local_size
[i
];
6544 var
->constant_value
= new(var
) ir_constant(glsl_type::uvec3_type
, &data
);
6545 var
->constant_initializer
=
6546 new(var
) ir_constant(glsl_type::uvec3_type
, &data
);
6547 var
->data
.has_initializer
= true;
6554 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
6555 exec_list
*instructions
)
6557 bool gl_FragColor_assigned
= false;
6558 bool gl_FragData_assigned
= false;
6559 bool user_defined_fs_output_assigned
= false;
6560 ir_variable
*user_defined_fs_output
= NULL
;
6562 /* It would be nice to have proper location information. */
6564 memset(&loc
, 0, sizeof(loc
));
6566 foreach_in_list(ir_instruction
, node
, instructions
) {
6567 ir_variable
*var
= node
->as_variable();
6569 if (!var
|| !var
->data
.assigned
)
6572 if (strcmp(var
->name
, "gl_FragColor") == 0)
6573 gl_FragColor_assigned
= true;
6574 else if (strcmp(var
->name
, "gl_FragData") == 0)
6575 gl_FragData_assigned
= true;
6576 else if (!is_gl_identifier(var
->name
)) {
6577 if (state
->stage
== MESA_SHADER_FRAGMENT
&&
6578 var
->data
.mode
== ir_var_shader_out
) {
6579 user_defined_fs_output_assigned
= true;
6580 user_defined_fs_output
= var
;
6585 /* From the GLSL 1.30 spec:
6587 * "If a shader statically assigns a value to gl_FragColor, it
6588 * may not assign a value to any element of gl_FragData. If a
6589 * shader statically writes a value to any element of
6590 * gl_FragData, it may not assign a value to
6591 * gl_FragColor. That is, a shader may assign values to either
6592 * gl_FragColor or gl_FragData, but not both. Multiple shaders
6593 * linked together must also consistently write just one of
6594 * these variables. Similarly, if user declared output
6595 * variables are in use (statically assigned to), then the
6596 * built-in variables gl_FragColor and gl_FragData may not be
6597 * assigned to. These incorrect usages all generate compile
6600 if (gl_FragColor_assigned
&& gl_FragData_assigned
) {
6601 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
6602 "`gl_FragColor' and `gl_FragData'");
6603 } else if (gl_FragColor_assigned
&& user_defined_fs_output_assigned
) {
6604 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
6605 "`gl_FragColor' and `%s'",
6606 user_defined_fs_output
->name
);
6607 } else if (gl_FragData_assigned
&& user_defined_fs_output_assigned
) {
6608 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
6609 "`gl_FragData' and `%s'",
6610 user_defined_fs_output
->name
);
6616 remove_per_vertex_blocks(exec_list
*instructions
,
6617 _mesa_glsl_parse_state
*state
, ir_variable_mode mode
)
6619 /* Find the gl_PerVertex interface block of the appropriate (in/out) mode,
6620 * if it exists in this shader type.
6622 const glsl_type
*per_vertex
= NULL
;
6624 case ir_var_shader_in
:
6625 if (ir_variable
*gl_in
= state
->symbols
->get_variable("gl_in"))
6626 per_vertex
= gl_in
->get_interface_type();
6628 case ir_var_shader_out
:
6629 if (ir_variable
*gl_Position
=
6630 state
->symbols
->get_variable("gl_Position")) {
6631 per_vertex
= gl_Position
->get_interface_type();
6635 assert(!"Unexpected mode");
6639 /* If we didn't find a built-in gl_PerVertex interface block, then we don't
6640 * need to do anything.
6642 if (per_vertex
== NULL
)
6645 /* If the interface block is used by the shader, then we don't need to do
6648 interface_block_usage_visitor
v(mode
, per_vertex
);
6649 v
.run(instructions
);
6650 if (v
.usage_found())
6653 /* Remove any ir_variable declarations that refer to the interface block
6656 foreach_in_list_safe(ir_instruction
, node
, instructions
) {
6657 ir_variable
*const var
= node
->as_variable();
6658 if (var
!= NULL
&& var
->get_interface_type() == per_vertex
&&
6659 var
->data
.mode
== mode
) {
6660 state
->symbols
->disable_variable(var
->name
);