1 /*
2 * Copyright © 2010 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 /**
25 * \file ast_to_hir.c
26 * Convert abstract syntax to to high-level intermediate reprensentation (HIR).
27 *
28 * During the conversion to HIR, the majority of the symantic checking is
29 * preformed on the program. This includes:
30 *
31 * * Symbol table management
32 * * Type checking
33 * * Function binding
34 *
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.
43 *
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.
47 *
48 * As a result, my preference is to put as little C code as possible in the
49 * parser (and lexer) sources.
50 */
51
52 #include "glsl_symbol_table.h"
53 #include "glsl_parser_extras.h"
54 #include "ast.h"
55 #include "compiler/glsl_types.h"
56 #include "util/hash_table.h"
57 #include "main/consts_exts.h"
58 #include "main/macros.h"
59 #include "main/shaderobj.h"
60 #include "ir.h"
61 #include "ir_builder.h"
62 #include "linker_util.h"
63 #include "builtin_functions.h"
64
65 using namespace ir_builder;
66
67 static void
68 detect_conflicting_assignments(struct _mesa_glsl_parse_state *state,
69 exec_list *instructions);
70 static void
71 verify_subroutine_associated_funcs(struct _mesa_glsl_parse_state *state);
72
73 static void
74 remove_per_vertex_blocks(exec_list *instructions,
75 _mesa_glsl_parse_state *state, ir_variable_mode mode);
76
77 /**
78 * Visitor class that finds the first instance of any write-only variable that
79 * is ever read, if any
80 */
81 class read_from_write_only_variable_visitor : public ir_hierarchical_visitor
82 {
83 public:
read_from_write_only_variable_visitor()84 read_from_write_only_variable_visitor() : found(NULL)
85 {
86 }
87
visit(ir_dereference_variable * ir)88 virtual ir_visitor_status visit(ir_dereference_variable *ir)
89 {
90 if (this->in_assignee)
91 return visit_continue;
92
93 ir_variable *var = ir->variable_referenced();
94 /* We can have memory_write_only set on both images and buffer variables,
95 * but in the former there is a distinction between reads from
96 * the variable itself (write_only) and from the memory they point to
97 * (memory_write_only), while in the case of buffer variables there is
98 * no such distinction, that is why this check here is limited to
99 * buffer variables alone.
100 */
101 if (!var || var->data.mode != ir_var_shader_storage)
102 return visit_continue;
103
104 if (var->data.memory_write_only) {
105 found = var;
106 return visit_stop;
107 }
108
109 return visit_continue;
110 }
111
get_variable()112 ir_variable *get_variable() {
113 return found;
114 }
115
visit_enter(ir_expression * ir)116 virtual ir_visitor_status visit_enter(ir_expression *ir)
117 {
118 /* .length() doesn't actually read anything */
119 if (ir->operation == ir_unop_ssbo_unsized_array_length)
120 return visit_continue_with_parent;
121
122 return visit_continue;
123 }
124
125 private:
126 ir_variable *found;
127 };
128
129 void
_mesa_ast_to_hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)130 _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
131 {
132 _mesa_glsl_initialize_variables(instructions, state);
133
134 state->symbols->separate_function_namespace = state->language_version == 110;
135
136 state->current_function = NULL;
137
138 state->toplevel_ir = instructions;
139
140 state->gs_input_prim_type_specified = false;
141 state->tcs_output_vertices_specified = false;
142 state->cs_input_local_size_specified = false;
143
144 /* Section 4.2 of the GLSL 1.20 specification states:
145 * "The built-in functions are scoped in a scope outside the global scope
146 * users declare global variables in. That is, a shader's global scope,
147 * available for user-defined functions and global variables, is nested
148 * inside the scope containing the built-in functions."
149 *
150 * Since built-in functions like ftransform() access built-in variables,
151 * it follows that those must be in the outer scope as well.
152 *
153 * We push scope here to create this nesting effect...but don't pop.
154 * This way, a shader's globals are still in the symbol table for use
155 * by the linker.
156 */
157 state->symbols->push_scope();
158
159 foreach_list_typed (ast_node, ast, link, & state->translation_unit)
160 ast->hir(instructions, state);
161
162 verify_subroutine_associated_funcs(state);
163 detect_recursion_unlinked(state, instructions);
164 detect_conflicting_assignments(state, instructions);
165
166 state->toplevel_ir = NULL;
167
168 /* Move all of the variable declarations to the front of the IR list, and
169 * reverse the order. This has the (intended!) side effect that vertex
170 * shader inputs and fragment shader outputs will appear in the IR in the
171 * same order that they appeared in the shader code. This results in the
172 * locations being assigned in the declared order. Many (arguably buggy)
173 * applications depend on this behavior, and it matches what nearly all
174 * other drivers do.
175 */
176 foreach_in_list_safe(ir_instruction, node, instructions) {
177 ir_variable *const var = node->as_variable();
178
179 if (var == NULL)
180 continue;
181
182 var->remove();
183 instructions->push_head(var);
184 }
185
186 /* Figure out if gl_FragCoord is actually used in fragment shader */
187 ir_variable *const var = state->symbols->get_variable("gl_FragCoord");
188 if (var != NULL)
189 state->fs_uses_gl_fragcoord = var->data.used;
190
191 /* From section 7.1 (Built-In Language Variables) of the GLSL 4.10 spec:
192 *
193 * If multiple shaders using members of a built-in block belonging to
194 * the same interface are linked together in the same program, they
195 * must all redeclare the built-in block in the same way, as described
196 * in section 4.3.7 "Interface Blocks" for interface block matching, or
197 * a link error will result.
198 *
199 * The phrase "using members of a built-in block" implies that if two
200 * shaders are linked together and one of them *does not use* any members
201 * of the built-in block, then that shader does not need to have a matching
202 * redeclaration of the built-in block.
203 *
204 * This appears to be a clarification to the behaviour established for
205 * gl_PerVertex by GLSL 1.50, therefore implement it regardless of GLSL
206 * version.
207 *
208 * The definition of "interface" in section 4.3.7 that applies here is as
209 * follows:
210 *
211 * The boundary between adjacent programmable pipeline stages: This
212 * spans all the outputs in all compilation units of the first stage
213 * and all the inputs in all compilation units of the second stage.
214 *
215 * Therefore this rule applies to both inter- and intra-stage linking.
216 *
217 * The easiest way to implement this is to check whether the shader uses
218 * gl_PerVertex right after ast-to-ir conversion, and if it doesn't, simply
219 * remove all the relevant variable declaration from the IR, so that the
220 * linker won't see them and complain about mismatches.
221 */
222 remove_per_vertex_blocks(instructions, state, ir_var_shader_in);
223 remove_per_vertex_blocks(instructions, state, ir_var_shader_out);
224
225 /* Check that we don't have reads from write-only variables */
226 read_from_write_only_variable_visitor v;
227 v.run(instructions);
228 ir_variable *error_var = v.get_variable();
229 if (error_var) {
230 /* It would be nice to have proper location information, but for that
231 * we would need to check this as we process each kind of AST node
232 */
233 YYLTYPE loc;
234 memset(&loc, 0, sizeof(loc));
235 _mesa_glsl_error(&loc, state, "Read from write-only variable `%s'",
236 error_var->name);
237 }
238 }
239
240
241 static ir_expression_operation
get_implicit_conversion_operation(const glsl_type * to,const glsl_type * from,struct _mesa_glsl_parse_state * state)242 get_implicit_conversion_operation(const glsl_type *to, const glsl_type *from,
243 struct _mesa_glsl_parse_state *state)
244 {
245 switch (to->base_type) {
246 case GLSL_TYPE_FLOAT16:
247 switch (from->base_type) {
248 case GLSL_TYPE_INT: return ir_unop_i2f16;
249 case GLSL_TYPE_UINT: return ir_unop_u2f16;
250 default: return (ir_expression_operation)0;
251 }
252
253 case GLSL_TYPE_FLOAT:
254 switch (from->base_type) {
255 case GLSL_TYPE_INT: return ir_unop_i2f;
256 case GLSL_TYPE_UINT: return ir_unop_u2f;
257 case GLSL_TYPE_FLOAT16: return ir_unop_f162f;
258 default: return (ir_expression_operation)0;
259 }
260
261 case GLSL_TYPE_UINT:
262 if (!state->has_implicit_int_to_uint_conversion())
263 return (ir_expression_operation)0;
264 switch (from->base_type) {
265 case GLSL_TYPE_INT: return ir_unop_i2u;
266 default: return (ir_expression_operation)0;
267 }
268
269 case GLSL_TYPE_DOUBLE:
270 if (!state->has_double())
271 return (ir_expression_operation)0;
272 switch (from->base_type) {
273 case GLSL_TYPE_INT: return ir_unop_i2d;
274 case GLSL_TYPE_UINT: return ir_unop_u2d;
275 case GLSL_TYPE_FLOAT16: return ir_unop_f162d;
276 case GLSL_TYPE_FLOAT: return ir_unop_f2d;
277 case GLSL_TYPE_INT64: return ir_unop_i642d;
278 case GLSL_TYPE_UINT64: return ir_unop_u642d;
279 default: return (ir_expression_operation)0;
280 }
281
282 case GLSL_TYPE_UINT64:
283 if (!state->has_int64())
284 return (ir_expression_operation)0;
285 switch (from->base_type) {
286 case GLSL_TYPE_INT: return ir_unop_i2u64;
287 case GLSL_TYPE_UINT: return ir_unop_u2u64;
288 case GLSL_TYPE_INT64: return ir_unop_i642u64;
289 default: return (ir_expression_operation)0;
290 }
291
292 case GLSL_TYPE_INT64:
293 if (!state->has_int64())
294 return (ir_expression_operation)0;
295 switch (from->base_type) {
296 case GLSL_TYPE_INT: return ir_unop_i2i64;
297 default: return (ir_expression_operation)0;
298 }
299
300 default: return (ir_expression_operation)0;
301 }
302 }
303
304
305 /**
306 * If a conversion is available, convert one operand to a different type
307 *
308 * The \c from \c ir_rvalue is converted "in place".
309 *
310 * \param to Type that the operand it to be converted to
311 * \param from Operand that is being converted
312 * \param state GLSL compiler state
313 *
314 * \return
315 * If a conversion is possible (or unnecessary), \c true is returned.
316 * Otherwise \c false is returned.
317 */
318 static bool
apply_implicit_conversion(const glsl_type * to,ir_rvalue * & from,struct _mesa_glsl_parse_state * state)319 apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
320 struct _mesa_glsl_parse_state *state)
321 {
322 void *ctx = state;
323 if (to->base_type == from->type->base_type)
324 return true;
325
326 /* Prior to GLSL 1.20, there are no implicit conversions */
327 if (!state->has_implicit_conversions())
328 return false;
329
330 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
331 *
332 * "There are no implicit array or structure conversions. For
333 * example, an array of int cannot be implicitly converted to an
334 * array of float.
335 */
336 if (!glsl_type_is_numeric(to) || !glsl_type_is_numeric(from->type))
337 return false;
338
339 /* We don't actually want the specific type `to`, we want a type
340 * with the same base type as `to`, but the same vector width as
341 * `from`.
342 */
343 to = glsl_simple_type(to->base_type, from->type->vector_elements,
344 from->type->matrix_columns);
345
346 ir_expression_operation op = get_implicit_conversion_operation(to, from->type, state);
347 if (op) {
348 from = new(ctx) ir_expression(op, to, from, NULL);
349 return true;
350 } else {
351 return false;
352 }
353 }
354
355
356 static const struct glsl_type *
arithmetic_result_type(ir_rvalue * & value_a,ir_rvalue * & value_b,bool multiply,struct _mesa_glsl_parse_state * state,YYLTYPE * loc)357 arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
358 bool multiply,
359 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
360 {
361 const glsl_type *type_a = value_a->type;
362 const glsl_type *type_b = value_b->type;
363
364 /* From GLSL 1.50 spec, page 56:
365 *
366 * "The arithmetic binary operators add (+), subtract (-),
367 * multiply (*), and divide (/) operate on integer and
368 * floating-point scalars, vectors, and matrices."
369 */
370 if (!glsl_type_is_numeric(type_a) || !glsl_type_is_numeric(type_b)) {
371 _mesa_glsl_error(loc, state,
372 "operands to arithmetic operators must be numeric");
373 return &glsl_type_builtin_error;
374 }
375
376
377 /* "If one operand is floating-point based and the other is
378 * not, then the conversions from Section 4.1.10 "Implicit
379 * Conversions" are applied to the non-floating-point-based operand."
380 */
381 if (!apply_implicit_conversion(type_a, value_b, state)
382 && !apply_implicit_conversion(type_b, value_a, state)) {
383 _mesa_glsl_error(loc, state,
384 "could not implicitly convert operands to "
385 "arithmetic operator");
386 return &glsl_type_builtin_error;
387 }
388 type_a = value_a->type;
389 type_b = value_b->type;
390
391 /* "If the operands are integer types, they must both be signed or
392 * both be unsigned."
393 *
394 * From this rule and the preceeding conversion it can be inferred that
395 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
396 * The is_numeric check above already filtered out the case where either
397 * type is not one of these, so now the base types need only be tested for
398 * equality.
399 */
400 if (type_a->base_type != type_b->base_type) {
401 _mesa_glsl_error(loc, state,
402 "base type mismatch for arithmetic operator");
403 return &glsl_type_builtin_error;
404 }
405
406 /* "All arithmetic binary operators result in the same fundamental type
407 * (signed integer, unsigned integer, or floating-point) as the
408 * operands they operate on, after operand type conversion. After
409 * conversion, the following cases are valid
410 *
411 * * The two operands are scalars. In this case the operation is
412 * applied, resulting in a scalar."
413 */
414 if (glsl_type_is_scalar(type_a) && glsl_type_is_scalar(type_b))
415 return type_a;
416
417 /* "* One operand is a scalar, and the other is a vector or matrix.
418 * In this case, the scalar operation is applied independently to each
419 * component of the vector or matrix, resulting in the same size
420 * vector or matrix."
421 */
422 if (glsl_type_is_scalar(type_a)) {
423 if (!glsl_type_is_scalar(type_b))
424 return type_b;
425 } else if (glsl_type_is_scalar(type_b)) {
426 return type_a;
427 }
428
429 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
430 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
431 * handled.
432 */
433 assert(!glsl_type_is_scalar(type_a));
434 assert(!glsl_type_is_scalar(type_b));
435
436 /* "* The two operands are vectors of the same size. In this case, the
437 * operation is done component-wise resulting in the same size
438 * vector."
439 */
440 if (glsl_type_is_vector(type_a) && glsl_type_is_vector(type_b)) {
441 if (type_a == type_b) {
442 return type_a;
443 } else {
444 _mesa_glsl_error(loc, state,
445 "vector size mismatch for arithmetic operator");
446 return &glsl_type_builtin_error;
447 }
448 }
449
450 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
451 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
452 * <vector, vector> have been handled. At least one of the operands must
453 * be matrix. Further, since there are no integer matrix types, the base
454 * type of both operands must be float.
455 */
456 assert(glsl_type_is_matrix(type_a) || glsl_type_is_matrix(type_b));
457 assert(glsl_type_is_float(type_a) || glsl_type_is_double(type_a));
458 assert(glsl_type_is_float(type_b) || glsl_type_is_double(type_b));
459
460 /* "* The operator is add (+), subtract (-), or divide (/), and the
461 * operands are matrices with the same number of rows and the same
462 * number of columns. In this case, the operation is done component-
463 * wise resulting in the same size matrix."
464 * * The operator is multiply (*), where both operands are matrices or
465 * one operand is a vector and the other a matrix. A right vector
466 * operand is treated as a column vector and a left vector operand as a
467 * row vector. In all these cases, it is required that the number of
468 * columns of the left operand is equal to the number of rows of the
469 * right operand. Then, the multiply (*) operation does a linear
470 * algebraic multiply, yielding an object that has the same number of
471 * rows as the left operand and the same number of columns as the right
472 * operand. Section 5.10 "Vector and Matrix Operations" explains in
473 * more detail how vectors and matrices are operated on."
474 */
475 if (! multiply) {
476 if (type_a == type_b)
477 return type_a;
478 } else {
479 const glsl_type *type = glsl_get_mul_type(type_a, type_b);
480
481 if (type == &glsl_type_builtin_error) {
482 _mesa_glsl_error(loc, state,
483 "size mismatch for matrix multiplication");
484 }
485
486 return type;
487 }
488
489
490 /* "All other cases are illegal."
491 */
492 _mesa_glsl_error(loc, state, "type mismatch");
493 return &glsl_type_builtin_error;
494 }
495
496
497 static const struct glsl_type *
unary_arithmetic_result_type(const struct glsl_type * type,struct _mesa_glsl_parse_state * state,YYLTYPE * loc)498 unary_arithmetic_result_type(const struct glsl_type *type,
499 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
500 {
501 /* From GLSL 1.50 spec, page 57:
502 *
503 * "The arithmetic unary operators negate (-), post- and pre-increment
504 * and decrement (-- and ++) operate on integer or floating-point
505 * values (including vectors and matrices). All unary operators work
506 * component-wise on their operands. These result with the same type
507 * they operated on."
508 */
509 if (!glsl_type_is_numeric(type)) {
510 _mesa_glsl_error(loc, state,
511 "operands to arithmetic operators must be numeric");
512 return &glsl_type_builtin_error;
513 }
514
515 return type;
516 }
517
518 /**
519 * \brief Return the result type of a bit-logic operation.
520 *
521 * If the given types to the bit-logic operator are invalid, return
522 * &glsl_type_builtin_error.
523 *
524 * \param value_a LHS of bit-logic op
525 * \param value_b RHS of bit-logic op
526 */
527 static const struct glsl_type *
bit_logic_result_type(ir_rvalue * & value_a,ir_rvalue * & value_b,ast_operators op,struct _mesa_glsl_parse_state * state,YYLTYPE * loc)528 bit_logic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
529 ast_operators op,
530 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
531 {
532 const glsl_type *type_a = value_a->type;
533 const glsl_type *type_b = value_b->type;
534
535 if (!state->check_bitwise_operations_allowed(loc)) {
536 return &glsl_type_builtin_error;
537 }
538
539 /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
540 *
541 * "The bitwise operators and (&), exclusive-or (^), and inclusive-or
542 * (|). The operands must be of type signed or unsigned integers or
543 * integer vectors."
544 */
545 if (!glsl_type_is_integer_32_64(type_a)) {
546 _mesa_glsl_error(loc, state, "LHS of `%s' must be an integer",
547 ast_expression::operator_string(op));
548 return &glsl_type_builtin_error;
549 }
550 if (!glsl_type_is_integer_32_64(type_b)) {
551 _mesa_glsl_error(loc, state, "RHS of `%s' must be an integer",
552 ast_expression::operator_string(op));
553 return &glsl_type_builtin_error;
554 }
555
556 /* Prior to GLSL 4.0 / GL_ARB_gpu_shader5, implicit conversions didn't
557 * make sense for bitwise operations, as they don't operate on floats.
558 *
559 * GLSL 4.0 added implicit int -> uint conversions, which are relevant
560 * here. It wasn't clear whether or not we should apply them to bitwise
561 * operations. However, Khronos has decided that they should in future
562 * language revisions. Applications also rely on this behavior. We opt
563 * to apply them in general, but issue a portability warning.
564 *
565 * See https://www.khronos.org/bugzilla/show_bug.cgi?id=1405
566 */
567 if (type_a->base_type != type_b->base_type) {
568 if (!apply_implicit_conversion(type_a, value_b, state)
569 && !apply_implicit_conversion(type_b, value_a, state)) {
570 _mesa_glsl_error(loc, state,
571 "could not implicitly convert operands to "
572 "`%s` operator",
573 ast_expression::operator_string(op));
574 return &glsl_type_builtin_error;
575 } else {
576 _mesa_glsl_warning(loc, state,
577 "some implementations may not support implicit "
578 "int -> uint conversions for `%s' operators; "
579 "consider casting explicitly for portability",
580 ast_expression::operator_string(op));
581 }
582 type_a = value_a->type;
583 type_b = value_b->type;
584 }
585
586 /* "The fundamental types of the operands (signed or unsigned) must
587 * match,"
588 */
589 if (type_a->base_type != type_b->base_type) {
590 _mesa_glsl_error(loc, state, "operands of `%s' must have the same "
591 "base type", ast_expression::operator_string(op));
592 return &glsl_type_builtin_error;
593 }
594
595 /* "The operands cannot be vectors of differing size." */
596 if (glsl_type_is_vector(type_a) &&
597 glsl_type_is_vector(type_b) &&
598 type_a->vector_elements != type_b->vector_elements) {
599 _mesa_glsl_error(loc, state, "operands of `%s' cannot be vectors of "
600 "different sizes", ast_expression::operator_string(op));
601 return &glsl_type_builtin_error;
602 }
603
604 /* "If one operand is a scalar and the other a vector, the scalar is
605 * applied component-wise to the vector, resulting in the same type as
606 * the vector. The fundamental types of the operands [...] will be the
607 * resulting fundamental type."
608 */
609 if (glsl_type_is_scalar(type_a))
610 return type_b;
611 else
612 return type_a;
613 }
614
615 static const struct glsl_type *
modulus_result_type(ir_rvalue * & value_a,ir_rvalue * & value_b,struct _mesa_glsl_parse_state * state,YYLTYPE * loc)616 modulus_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
617 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
618 {
619 const glsl_type *type_a = value_a->type;
620 const glsl_type *type_b = value_b->type;
621
622 if (!state->EXT_gpu_shader4_enable &&
623 !state->check_version(130, 300, loc, "operator '%%' is reserved")) {
624 return &glsl_type_builtin_error;
625 }
626
627 /* Section 5.9 (Expressions) of the GLSL 4.00 specification says:
628 *
629 * "The operator modulus (%) operates on signed or unsigned integers or
630 * integer vectors."
631 */
632 if (!glsl_type_is_integer_32_64(type_a)) {
633 _mesa_glsl_error(loc, state, "LHS of operator %% must be an integer");
634 return &glsl_type_builtin_error;
635 }
636 if (!glsl_type_is_integer_32_64(type_b)) {
637 _mesa_glsl_error(loc, state, "RHS of operator %% must be an integer");
638 return &glsl_type_builtin_error;
639 }
640
641 /* "If the fundamental types in the operands do not match, then the
642 * conversions from section 4.1.10 "Implicit Conversions" are applied
643 * to create matching types."
644 *
645 * Note that GLSL 4.00 (and GL_ARB_gpu_shader5) introduced implicit
646 * int -> uint conversion rules. Prior to that, there were no implicit
647 * conversions. So it's harmless to apply them universally - no implicit
648 * conversions will exist. If the types don't match, we'll receive false,
649 * and raise an error, satisfying the GLSL 1.50 spec, page 56:
650 *
651 * "The operand types must both be signed or unsigned."
652 */
653 if (!apply_implicit_conversion(type_a, value_b, state) &&
654 !apply_implicit_conversion(type_b, value_a, state)) {
655 _mesa_glsl_error(loc, state,
656 "could not implicitly convert operands to "
657 "modulus (%%) operator");
658 return &glsl_type_builtin_error;
659 }
660 type_a = value_a->type;
661 type_b = value_b->type;
662
663 /* "The operands cannot be vectors of differing size. If one operand is
664 * a scalar and the other vector, then the scalar is applied component-
665 * wise to the vector, resulting in the same type as the vector. If both
666 * are vectors of the same size, the result is computed component-wise."
667 */
668 if (glsl_type_is_vector(type_a)) {
669 if (!glsl_type_is_vector(type_b)
670 || (type_a->vector_elements == type_b->vector_elements))
671 return type_a;
672 } else
673 return type_b;
674
675 /* "The operator modulus (%) is not defined for any other data types
676 * (non-integer types)."
677 */
678 _mesa_glsl_error(loc, state, "type mismatch");
679 return &glsl_type_builtin_error;
680 }
681
682
683 static const struct glsl_type *
relational_result_type(ir_rvalue * & value_a,ir_rvalue * & value_b,struct _mesa_glsl_parse_state * state,YYLTYPE * loc)684 relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
685 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
686 {
687 const glsl_type *type_a = value_a->type;
688 const glsl_type *type_b = value_b->type;
689
690 /* From GLSL 1.50 spec, page 56:
691 * "The relational operators greater than (>), less than (<), greater
692 * than or equal (>=), and less than or equal (<=) operate only on
693 * scalar integer and scalar floating-point expressions."
694 */
695 if (!glsl_type_is_numeric(type_a)
696 || !glsl_type_is_numeric(type_b)
697 || !glsl_type_is_scalar(type_a)
698 || !glsl_type_is_scalar(type_b)) {
699 _mesa_glsl_error(loc, state,
700 "operands to relational operators must be scalar and "
701 "numeric");
702 return &glsl_type_builtin_error;
703 }
704
705 /* "Either the operands' types must match, or the conversions from
706 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
707 * operand, after which the types must match."
708 */
709 if (!apply_implicit_conversion(type_a, value_b, state)
710 && !apply_implicit_conversion(type_b, value_a, state)) {
711 _mesa_glsl_error(loc, state,
712 "could not implicitly convert operands to "
713 "relational operator");
714 return &glsl_type_builtin_error;
715 }
716 type_a = value_a->type;
717 type_b = value_b->type;
718
719 if (type_a->base_type != type_b->base_type) {
720 _mesa_glsl_error(loc, state, "base type mismatch");
721 return &glsl_type_builtin_error;
722 }
723
724 /* "The result is scalar Boolean."
725 */
726 return &glsl_type_builtin_bool;
727 }
728
729 /**
730 * \brief Return the result type of a bit-shift operation.
731 *
732 * If the given types to the bit-shift operator are invalid, return
733 * &glsl_type_builtin_error.
734 *
735 * \param type_a Type of LHS of bit-shift op
736 * \param type_b Type of RHS of bit-shift op
737 */
738 static const struct glsl_type *
shift_result_type(const struct glsl_type * type_a,const struct glsl_type * type_b,ast_operators op,struct _mesa_glsl_parse_state * state,YYLTYPE * loc)739 shift_result_type(const struct glsl_type *type_a,
740 const struct glsl_type *type_b,
741 ast_operators op,
742 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
743 {
744 if (!state->check_bitwise_operations_allowed(loc)) {
745 return &glsl_type_builtin_error;
746 }
747
748 /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
749 *
750 * "The shift operators (<<) and (>>). For both operators, the operands
751 * must be signed or unsigned integers or integer vectors. One operand
752 * can be signed while the other is unsigned."
753 */
754 if (!glsl_type_is_integer_32_64(type_a)) {
755 _mesa_glsl_error(loc, state, "LHS of operator %s must be an integer or "
756 "integer vector", ast_expression::operator_string(op));
757 return &glsl_type_builtin_error;
758
759 }
760 if (!glsl_type_is_integer_32_64(type_b)) {
761 _mesa_glsl_error(loc, state, "RHS of operator %s must be an integer or "
762 "integer vector", ast_expression::operator_string(op));
763 return &glsl_type_builtin_error;
764 }
765
766 /* "If the first operand is a scalar, the second operand has to be
767 * a scalar as well."
768 */
769 if (glsl_type_is_scalar(type_a) && !glsl_type_is_scalar(type_b)) {
770 _mesa_glsl_error(loc, state, "if the first operand of %s is scalar, the "
771 "second must be scalar as well",
772 ast_expression::operator_string(op));
773 return &glsl_type_builtin_error;
774 }
775
776 /* If both operands are vectors, check that they have same number of
777 * elements.
778 */
779 if (glsl_type_is_vector(type_a) &&
780 glsl_type_is_vector(type_b) &&
781 type_a->vector_elements != type_b->vector_elements) {
782 _mesa_glsl_error(loc, state, "vector operands to operator %s must "
783 "have same number of elements",
784 ast_expression::operator_string(op));
785 return &glsl_type_builtin_error;
786 }
787
788 /* "In all cases, the resulting type will be the same type as the left
789 * operand."
790 */
791 return type_a;
792 }
793
794 /**
795 * Returns the innermost array index expression in an rvalue tree.
796 * This is the largest indexing level -- if an array of blocks, then
797 * it is the block index rather than an indexing expression for an
798 * array-typed member of an array of blocks.
799 */
800 static ir_rvalue *
find_innermost_array_index(ir_rvalue * rv)801 find_innermost_array_index(ir_rvalue *rv)
802 {
803 ir_dereference_array *last = NULL;
804 while (rv) {
805 if (rv->as_dereference_array()) {
806 last = rv->as_dereference_array();
807 rv = last->array;
808 } else if (rv->as_dereference_record())
809 rv = rv->as_dereference_record()->record;
810 else if (rv->as_swizzle())
811 rv = rv->as_swizzle()->val;
812 else
813 rv = NULL;
814 }
815
816 if (last)
817 return last->array_index;
818
819 return NULL;
820 }
821
822 /**
823 * Validates that a value can be assigned to a location with a specified type
824 *
825 * Validates that \c rhs can be assigned to some location. If the types are
826 * not an exact match but an automatic conversion is possible, \c rhs will be
827 * converted.
828 *
829 * \return
830 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
831 * Otherwise the actual RHS to be assigned will be returned. This may be
832 * \c rhs, or it may be \c rhs after some type conversion.
833 *
834 * \note
835 * In addition to being used for assignments, this function is used to
836 * type-check return values.
837 */
838 static ir_rvalue *
validate_assignment(struct _mesa_glsl_parse_state * state,YYLTYPE loc,ir_rvalue * lhs,ir_rvalue * rhs,bool is_initializer)839 validate_assignment(struct _mesa_glsl_parse_state *state,
840 YYLTYPE loc, ir_rvalue *lhs,
841 ir_rvalue *rhs, bool is_initializer)
842 {
843 /* If there is already some error in the RHS, just return it. Anything
844 * else will lead to an avalanche of error message back to the user.
845 */
846 if (glsl_type_is_error(rhs->type))
847 return rhs;
848
849 /* In the Tessellation Control Shader:
850 * If a per-vertex output variable is used as an l-value, it is an error
851 * if the expression indicating the vertex number is not the identifier
852 * `gl_InvocationID`.
853 */
854 if (state->stage == MESA_SHADER_TESS_CTRL && !glsl_type_is_error(lhs->type)) {
855 ir_variable *var = lhs->variable_referenced();
856 if (var && var->data.mode == ir_var_shader_out && !var->data.patch) {
857 ir_rvalue *index = find_innermost_array_index(lhs);
858 ir_variable *index_var = index ? index->variable_referenced() : NULL;
859 if (!index_var || strcmp(index_var->name, "gl_InvocationID") != 0) {
860 _mesa_glsl_error(&loc, state,
861 "Tessellation control shader outputs can only "
862 "be indexed by gl_InvocationID");
863 return NULL;
864 }
865 }
866 }
867
868 /* If the types are identical, the assignment can trivially proceed.
869 */
870 if (rhs->type == lhs->type)
871 return rhs;
872
873 /* If the array element types are the same and the LHS is unsized,
874 * the assignment is okay for initializers embedded in variable
875 * declarations.
876 *
877 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
878 * is handled by ir_dereference::is_lvalue.
879 */
880 const glsl_type *lhs_t = lhs->type;
881 const glsl_type *rhs_t = rhs->type;
882 bool unsized_array = false;
883 while(glsl_type_is_array(lhs_t)) {
884 if (rhs_t == lhs_t)
885 break; /* the rest of the inner arrays match so break out early */
886 if (!glsl_type_is_array(rhs_t)) {
887 unsized_array = false;
888 break; /* number of dimensions mismatch */
889 }
890 if (lhs_t->length == rhs_t->length) {
891 lhs_t = lhs_t->fields.array;
892 rhs_t = rhs_t->fields.array;
893 continue;
894 } else if (glsl_type_is_unsized_array(lhs_t)) {
895 unsized_array = true;
896 } else {
897 unsized_array = false;
898 break; /* sized array mismatch */
899 }
900 lhs_t = lhs_t->fields.array;
901 rhs_t = rhs_t->fields.array;
902 }
903 if (unsized_array) {
904 if (is_initializer) {
905 if (glsl_get_scalar_type(rhs->type) == glsl_get_scalar_type(lhs->type))
906 return rhs;
907 } else {
908 _mesa_glsl_error(&loc, state,
909 "implicitly sized arrays cannot be assigned");
910 return NULL;
911 }
912 }
913
914 /* Check for implicit conversion in GLSL 1.20 */
915 if (apply_implicit_conversion(lhs->type, rhs, state)) {
916 if (rhs->type == lhs->type)
917 return rhs;
918 }
919
920 _mesa_glsl_error(&loc, state,
921 "%s of type %s cannot be assigned to "
922 "variable of type %s",
923 is_initializer ? "initializer" : "value",
924 glsl_get_type_name(rhs->type), glsl_get_type_name(lhs->type));
925
926 return NULL;
927 }
928
929 static void
mark_whole_array_access(ir_rvalue * access)930 mark_whole_array_access(ir_rvalue *access)
931 {
932 ir_dereference_variable *deref = access->as_dereference_variable();
933
934 if (deref && deref->var) {
935 deref->var->data.max_array_access = deref->type->length - 1;
936 }
937 }
938
939 static bool
do_assignment(exec_list * instructions,struct _mesa_glsl_parse_state * state,const char * non_lvalue_description,ir_rvalue * lhs,ir_rvalue * rhs,ir_rvalue ** out_rvalue,bool needs_rvalue,bool is_initializer,YYLTYPE lhs_loc)940 do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
941 const char *non_lvalue_description,
942 ir_rvalue *lhs, ir_rvalue *rhs,
943 ir_rvalue **out_rvalue, bool needs_rvalue,
944 bool is_initializer,
945 YYLTYPE lhs_loc)
946 {
947 void *ctx = state;
948 bool error_emitted = (glsl_type_is_error(lhs->type) || glsl_type_is_error(rhs->type));
949
950 ir_variable *lhs_var = lhs->variable_referenced();
951 if (lhs_var)
952 lhs_var->data.assigned = true;
953
954 bool omit_assignment = false;
955 if (!error_emitted) {
956 if (non_lvalue_description != NULL) {
957 _mesa_glsl_error(&lhs_loc, state,
958 "assignment to %s",
959 non_lvalue_description);
960 error_emitted = true;
961 } else if (lhs_var != NULL && (lhs_var->data.read_only ||
962 (lhs_var->data.mode == ir_var_shader_storage &&
963 lhs_var->data.memory_read_only))) {
964 /* We can have memory_read_only set on both images and buffer variables,
965 * but in the former there is a distinction between assignments to
966 * the variable itself (read_only) and to the memory they point to
967 * (memory_read_only), while in the case of buffer variables there is
968 * no such distinction, that is why this check here is limited to
969 * buffer variables alone.
970 */
971
972 if (state->ignore_write_to_readonly_var)
973 omit_assignment = true;
974 else {
975 _mesa_glsl_error(&lhs_loc, state,
976 "assignment to read-only variable '%s'",
977 lhs_var->name);
978 error_emitted = true;
979 }
980 } else if (glsl_type_is_array(lhs->type) &&
981 !state->check_version(state->allow_glsl_120_subset_in_110 ? 110 : 120,
982 300, &lhs_loc,
983 "whole array assignment forbidden")) {
984 /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
985 *
986 * "Other binary or unary expressions, non-dereferenced
987 * arrays, function names, swizzles with repeated fields,
988 * and constants cannot be l-values."
989 *
990 * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00.
991 */
992 error_emitted = true;
993 } else if (!lhs->is_lvalue(state)) {
994 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
995 error_emitted = true;
996 }
997 }
998
999 ir_rvalue *new_rhs =
1000 validate_assignment(state, lhs_loc, lhs, rhs, is_initializer);
1001 if (new_rhs != NULL) {
1002 rhs = new_rhs;
1003
1004 /* If the LHS array was not declared with a size, it takes it size from
1005 * the RHS. If the LHS is an l-value and a whole array, it must be a
1006 * dereference of a variable. Any other case would require that the LHS
1007 * is either not an l-value or not a whole array.
1008 */
1009 if (glsl_type_is_unsized_array(lhs->type)) {
1010 ir_dereference *const d = lhs->as_dereference();
1011
1012 assert(d != NULL);
1013
1014 ir_variable *const var = d->variable_referenced();
1015
1016 assert(var != NULL);
1017
1018 if (var->data.max_array_access >= glsl_array_size(rhs->type)) {
1019 /* FINISHME: This should actually log the location of the RHS. */
1020 _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
1021 "previous access",
1022 var->data.max_array_access);
1023 }
1024
1025 var->type = glsl_array_type(lhs->type->fields.array,
1026 glsl_array_size(rhs->type), 0);
1027 d->type = var->type;
1028 }
1029 if (glsl_type_is_array(lhs->type)) {
1030 mark_whole_array_access(rhs);
1031 mark_whole_array_access(lhs);
1032 }
1033 } else {
1034 error_emitted = true;
1035 }
1036
1037 if (omit_assignment) {
1038 *out_rvalue = needs_rvalue ? ir_rvalue::error_value(ctx) : NULL;
1039 return error_emitted;
1040 }
1041
1042 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
1043 * but not post_inc) need the converted assigned value as an rvalue
1044 * to handle things like:
1045 *
1046 * i = j += 1;
1047 */
1048 if (needs_rvalue) {
1049 ir_rvalue *rvalue;
1050 if (!error_emitted) {
1051 ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp",
1052 ir_var_temporary);
1053 instructions->push_tail(var);
1054 instructions->push_tail(assign(var, rhs));
1055
1056 ir_dereference_variable *deref_var =
1057 new(ctx) ir_dereference_variable(var);
1058 instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var));
1059 rvalue = new(ctx) ir_dereference_variable(var);
1060 } else {
1061 rvalue = ir_rvalue::error_value(ctx);
1062 }
1063 *out_rvalue = rvalue;
1064 } else {
1065 if (!error_emitted)
1066 instructions->push_tail(new(ctx) ir_assignment(lhs, rhs));
1067 *out_rvalue = NULL;
1068 }
1069
1070 return error_emitted;
1071 }
1072
1073 static ir_rvalue *
get_lvalue_copy(exec_list * instructions,ir_rvalue * lvalue)1074 get_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
1075 {
1076 void *ctx = ralloc_parent(lvalue);
1077 ir_variable *var;
1078
1079 var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
1080 ir_var_temporary);
1081 instructions->push_tail(var);
1082
1083 instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
1084 lvalue));
1085
1086 return new(ctx) ir_dereference_variable(var);
1087 }
1088
1089
1090 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)1091 ast_node::hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
1092 {
1093 (void) instructions;
1094 (void) state;
1095
1096 return NULL;
1097 }
1098
1099 bool
has_sequence_subexpression() const1100 ast_node::has_sequence_subexpression() const
1101 {
1102 return false;
1103 }
1104
1105 void
set_is_lhs(bool)1106 ast_node::set_is_lhs(bool /* new_value */)
1107 {
1108 }
1109
1110 void
hir_no_rvalue(exec_list * instructions,struct _mesa_glsl_parse_state * state)1111 ast_function_expression::hir_no_rvalue(exec_list *instructions,
1112 struct _mesa_glsl_parse_state *state)
1113 {
1114 (void)hir(instructions, state);
1115 }
1116
1117 void
hir_no_rvalue(exec_list * instructions,struct _mesa_glsl_parse_state * state)1118 ast_aggregate_initializer::hir_no_rvalue(exec_list *instructions,
1119 struct _mesa_glsl_parse_state *state)
1120 {
1121 (void)hir(instructions, state);
1122 }
1123
1124 static ir_rvalue *
do_comparison(void * mem_ctx,int operation,ir_rvalue * op0,ir_rvalue * op1)1125 do_comparison(void *mem_ctx, int operation, ir_rvalue *op0, ir_rvalue *op1)
1126 {
1127 int join_op;
1128 ir_rvalue *cmp = NULL;
1129
1130 if (operation == ir_binop_all_equal)
1131 join_op = ir_binop_logic_and;
1132 else
1133 join_op = ir_binop_logic_or;
1134
1135 switch (op0->type->base_type) {
1136 case GLSL_TYPE_FLOAT:
1137 case GLSL_TYPE_FLOAT16:
1138 case GLSL_TYPE_UINT:
1139 case GLSL_TYPE_INT:
1140 case GLSL_TYPE_BOOL:
1141 case GLSL_TYPE_DOUBLE:
1142 case GLSL_TYPE_UINT64:
1143 case GLSL_TYPE_INT64:
1144 case GLSL_TYPE_UINT16:
1145 case GLSL_TYPE_INT16:
1146 case GLSL_TYPE_UINT8:
1147 case GLSL_TYPE_INT8:
1148 return new(mem_ctx) ir_expression(operation, op0, op1);
1149
1150 case GLSL_TYPE_ARRAY: {
1151 for (unsigned int i = 0; i < op0->type->length; i++) {
1152 ir_rvalue *e0, *e1, *result;
1153
1154 e0 = new(mem_ctx) ir_dereference_array(op0->clone(mem_ctx, NULL),
1155 new(mem_ctx) ir_constant(i));
1156 e1 = new(mem_ctx) ir_dereference_array(op1->clone(mem_ctx, NULL),
1157 new(mem_ctx) ir_constant(i));
1158 result = do_comparison(mem_ctx, operation, e0, e1);
1159
1160 if (cmp) {
1161 cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
1162 } else {
1163 cmp = result;
1164 }
1165 }
1166
1167 mark_whole_array_access(op0);
1168 mark_whole_array_access(op1);
1169 break;
1170 }
1171
1172 case GLSL_TYPE_STRUCT: {
1173 for (unsigned int i = 0; i < op0->type->length; i++) {
1174 ir_rvalue *e0, *e1, *result;
1175 const char *field_name = op0->type->fields.structure[i].name;
1176
1177 e0 = new(mem_ctx) ir_dereference_record(op0->clone(mem_ctx, NULL),
1178 field_name);
1179 e1 = new(mem_ctx) ir_dereference_record(op1->clone(mem_ctx, NULL),
1180 field_name);
1181 result = do_comparison(mem_ctx, operation, e0, e1);
1182
1183 if (cmp) {
1184 cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
1185 } else {
1186 cmp = result;
1187 }
1188 }
1189 break;
1190 }
1191
1192 case GLSL_TYPE_ERROR:
1193 case GLSL_TYPE_VOID:
1194 case GLSL_TYPE_SAMPLER:
1195 case GLSL_TYPE_TEXTURE:
1196 case GLSL_TYPE_IMAGE:
1197 case GLSL_TYPE_INTERFACE:
1198 case GLSL_TYPE_ATOMIC_UINT:
1199 case GLSL_TYPE_SUBROUTINE:
1200 /* I assume a comparison of a struct containing a sampler just
1201 * ignores the sampler present in the type.
1202 */
1203 break;
1204
1205 case GLSL_TYPE_COOPERATIVE_MATRIX:
1206 unreachable("unsupported base type cooperative matrix");
1207 }
1208
1209 if (cmp == NULL)
1210 cmp = new(mem_ctx) ir_constant(true);
1211
1212 return cmp;
1213 }
1214
1215 /* For logical operations, we want to ensure that the operands are
1216 * scalar booleans. If it isn't, emit an error and return a constant
1217 * boolean to avoid triggering cascading error messages.
1218 */
1219 static ir_rvalue *
get_scalar_boolean_operand(exec_list * instructions,struct _mesa_glsl_parse_state * state,ast_expression * parent_expr,int operand,const char * operand_name,bool * error_emitted)1220 get_scalar_boolean_operand(exec_list *instructions,
1221 struct _mesa_glsl_parse_state *state,
1222 ast_expression *parent_expr,
1223 int operand,
1224 const char *operand_name,
1225 bool *error_emitted)
1226 {
1227 ast_expression *expr = parent_expr->subexpressions[operand];
1228 void *ctx = state;
1229 ir_rvalue *val = expr->hir(instructions, state);
1230
1231 if (glsl_type_is_boolean(val->type) && glsl_type_is_scalar(val->type))
1232 return val;
1233
1234 if (!*error_emitted) {
1235 YYLTYPE loc = expr->get_location();
1236 _mesa_glsl_error(&loc, state, "%s of `%s' must be scalar boolean",
1237 operand_name,
1238 parent_expr->operator_string(parent_expr->oper));
1239 *error_emitted = true;
1240 }
1241
1242 return new(ctx) ir_constant(true);
1243 }
1244
1245 /**
1246 * If name refers to a builtin array whose maximum allowed size is less than
1247 * size, report an error and return true. Otherwise return false.
1248 */
1249 void
check_builtin_array_max_size(const char * name,unsigned size,YYLTYPE loc,struct _mesa_glsl_parse_state * state)1250 check_builtin_array_max_size(const char *name, unsigned size,
1251 YYLTYPE loc, struct _mesa_glsl_parse_state *state)
1252 {
1253 if ((strcmp("gl_TexCoord", name) == 0)
1254 && (size > state->Const.MaxTextureCoords)) {
1255 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1256 *
1257 * "The size [of gl_TexCoord] can be at most
1258 * gl_MaxTextureCoords."
1259 */
1260 _mesa_glsl_error(&loc, state, "`gl_TexCoord' array size cannot "
1261 "be larger than gl_MaxTextureCoords (%u)",
1262 state->Const.MaxTextureCoords);
1263 } else if (strcmp("gl_ClipDistance", name) == 0) {
1264 state->clip_dist_size = size;
1265 if (size + state->cull_dist_size > state->Const.MaxClipPlanes) {
1266 /* From section 7.1 (Vertex Shader Special Variables) of the
1267 * GLSL 1.30 spec:
1268 *
1269 * "The gl_ClipDistance array is predeclared as unsized and
1270 * must be sized by the shader either redeclaring it with a
1271 * size or indexing it only with integral constant
1272 * expressions. ... The size can be at most
1273 * gl_MaxClipDistances."
1274 */
1275 _mesa_glsl_error(&loc, state, "`gl_ClipDistance' array size cannot "
1276 "be larger than gl_MaxClipDistances (%u)",
1277 state->Const.MaxClipPlanes);
1278 }
1279 } else if (strcmp("gl_CullDistance", name) == 0) {
1280 state->cull_dist_size = size;
1281 if (size + state->clip_dist_size > state->Const.MaxClipPlanes) {
1282 /* From the ARB_cull_distance spec:
1283 *
1284 * "The gl_CullDistance array is predeclared as unsized and
1285 * must be sized by the shader either redeclaring it with
1286 * a size or indexing it only with integral constant
1287 * expressions. The size determines the number and set of
1288 * enabled cull distances and can be at most
1289 * gl_MaxCullDistances."
1290 */
1291 _mesa_glsl_error(&loc, state, "`gl_CullDistance' array size cannot "
1292 "be larger than gl_MaxCullDistances (%u)",
1293 state->Const.MaxClipPlanes);
1294 }
1295 }
1296 }
1297
1298 /**
1299 * Create the constant 1, of a which is appropriate for incrementing and
1300 * decrementing values of the given GLSL type. For example, if type is vec4,
1301 * this creates a constant value of 1.0 having type float.
1302 *
1303 * If the given type is invalid for increment and decrement operators, return
1304 * a floating point 1--the error will be detected later.
1305 */
1306 static ir_rvalue *
constant_one_for_inc_dec(void * ctx,const glsl_type * type)1307 constant_one_for_inc_dec(void *ctx, const glsl_type *type)
1308 {
1309 switch (type->base_type) {
1310 case GLSL_TYPE_UINT:
1311 return new(ctx) ir_constant((unsigned) 1);
1312 case GLSL_TYPE_INT:
1313 return new(ctx) ir_constant(1);
1314 case GLSL_TYPE_UINT64:
1315 return new(ctx) ir_constant((uint64_t) 1);
1316 case GLSL_TYPE_INT64:
1317 return new(ctx) ir_constant((int64_t) 1);
1318 default:
1319 case GLSL_TYPE_FLOAT:
1320 return new(ctx) ir_constant(1.0f);
1321 }
1322 }
1323
1324 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)1325 ast_expression::hir(exec_list *instructions,
1326 struct _mesa_glsl_parse_state *state)
1327 {
1328 return do_hir(instructions, state, true);
1329 }
1330
1331 void
hir_no_rvalue(exec_list * instructions,struct _mesa_glsl_parse_state * state)1332 ast_expression::hir_no_rvalue(exec_list *instructions,
1333 struct _mesa_glsl_parse_state *state)
1334 {
1335 do_hir(instructions, state, false);
1336 }
1337
1338 void
set_is_lhs(bool new_value)1339 ast_expression::set_is_lhs(bool new_value)
1340 {
1341 /* is_lhs is tracked only to print "variable used uninitialized" warnings,
1342 * if we lack an identifier we can just skip it.
1343 */
1344 if (this->primary_expression.identifier == NULL)
1345 return;
1346
1347 this->is_lhs = new_value;
1348
1349 /* We need to go through the subexpressions tree to cover cases like
1350 * ast_field_selection
1351 */
1352 if (this->subexpressions[0] != NULL)
1353 this->subexpressions[0]->set_is_lhs(new_value);
1354 }
1355
1356 ir_rvalue *
do_hir(exec_list * instructions,struct _mesa_glsl_parse_state * state,bool needs_rvalue)1357 ast_expression::do_hir(exec_list *instructions,
1358 struct _mesa_glsl_parse_state *state,
1359 bool needs_rvalue)
1360 {
1361 void *ctx = state;
1362 static const int operations[AST_NUM_OPERATORS] = {
1363 -1, /* ast_assign doesn't convert to ir_expression. */
1364 -1, /* ast_plus doesn't convert to ir_expression. */
1365 ir_unop_neg,
1366 ir_binop_add,
1367 ir_binop_sub,
1368 ir_binop_mul,
1369 ir_binop_div,
1370 ir_binop_mod,
1371 ir_binop_lshift,
1372 ir_binop_rshift,
1373 ir_binop_less,
1374 ir_binop_less, /* This is correct. See the ast_greater case below. */
1375 ir_binop_gequal, /* This is correct. See the ast_lequal case below. */
1376 ir_binop_gequal,
1377 ir_binop_all_equal,
1378 ir_binop_any_nequal,
1379 ir_binop_bit_and,
1380 ir_binop_bit_xor,
1381 ir_binop_bit_or,
1382 ir_unop_bit_not,
1383 ir_binop_logic_and,
1384 ir_binop_logic_xor,
1385 ir_binop_logic_or,
1386 ir_unop_logic_not,
1387
1388 /* Note: The following block of expression types actually convert
1389 * to multiple IR instructions.
1390 */
1391 ir_binop_mul, /* ast_mul_assign */
1392 ir_binop_div, /* ast_div_assign */
1393 ir_binop_mod, /* ast_mod_assign */
1394 ir_binop_add, /* ast_add_assign */
1395 ir_binop_sub, /* ast_sub_assign */
1396 ir_binop_lshift, /* ast_ls_assign */
1397 ir_binop_rshift, /* ast_rs_assign */
1398 ir_binop_bit_and, /* ast_and_assign */
1399 ir_binop_bit_xor, /* ast_xor_assign */
1400 ir_binop_bit_or, /* ast_or_assign */
1401
1402 -1, /* ast_conditional doesn't convert to ir_expression. */
1403 ir_binop_add, /* ast_pre_inc. */
1404 ir_binop_sub, /* ast_pre_dec. */
1405 ir_binop_add, /* ast_post_inc. */
1406 ir_binop_sub, /* ast_post_dec. */
1407 -1, /* ast_field_selection doesn't conv to ir_expression. */
1408 -1, /* ast_array_index doesn't convert to ir_expression. */
1409 -1, /* ast_function_call doesn't conv to ir_expression. */
1410 -1, /* ast_identifier doesn't convert to ir_expression. */
1411 -1, /* ast_int_constant doesn't convert to ir_expression. */
1412 -1, /* ast_uint_constant doesn't conv to ir_expression. */
1413 -1, /* ast_float_constant doesn't conv to ir_expression. */
1414 -1, /* ast_bool_constant doesn't conv to ir_expression. */
1415 -1, /* ast_sequence doesn't convert to ir_expression. */
1416 -1, /* ast_aggregate shouldn't ever even get here. */
1417 };
1418 ir_rvalue *result = NULL;
1419 ir_rvalue *op[3];
1420 const struct glsl_type *type, *orig_type;
1421 bool error_emitted = false;
1422 YYLTYPE loc;
1423
1424 loc = this->get_location();
1425
1426 switch (this->oper) {
1427 case ast_aggregate:
1428 unreachable("ast_aggregate: Should never get here.");
1429
1430 case ast_assign: {
1431 this->subexpressions[0]->set_is_lhs(true);
1432 op[0] = this->subexpressions[0]->hir(instructions, state);
1433 op[1] = this->subexpressions[1]->hir(instructions, state);
1434
1435 error_emitted =
1436 do_assignment(instructions, state,
1437 this->subexpressions[0]->non_lvalue_description,
1438 op[0], op[1], &result, needs_rvalue, false,
1439 this->subexpressions[0]->get_location());
1440 break;
1441 }
1442
1443 case ast_plus:
1444 op[0] = this->subexpressions[0]->hir(instructions, state);
1445
1446 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
1447
1448 error_emitted = glsl_type_is_error(type);
1449
1450 result = op[0];
1451 break;
1452
1453 case ast_neg:
1454 op[0] = this->subexpressions[0]->hir(instructions, state);
1455
1456 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
1457
1458 error_emitted = glsl_type_is_error(type);
1459
1460 result = new(ctx) ir_expression(operations[this->oper], type,
1461 op[0], NULL);
1462 break;
1463
1464 case ast_add:
1465 case ast_sub:
1466 case ast_mul:
1467 case ast_div:
1468 op[0] = this->subexpressions[0]->hir(instructions, state);
1469 op[1] = this->subexpressions[1]->hir(instructions, state);
1470
1471 type = arithmetic_result_type(op[0], op[1],
1472 (this->oper == ast_mul),
1473 state, & loc);
1474 error_emitted = glsl_type_is_error(type);
1475
1476 result = new(ctx) ir_expression(operations[this->oper], type,
1477 op[0], op[1]);
1478 break;
1479
1480 case ast_mod:
1481 op[0] = this->subexpressions[0]->hir(instructions, state);
1482 op[1] = this->subexpressions[1]->hir(instructions, state);
1483
1484 type = modulus_result_type(op[0], op[1], state, &loc);
1485
1486 assert(operations[this->oper] == ir_binop_mod);
1487
1488 result = new(ctx) ir_expression(operations[this->oper], type,
1489 op[0], op[1]);
1490 error_emitted = glsl_type_is_error(type);
1491 break;
1492
1493 case ast_lshift:
1494 case ast_rshift:
1495 if (!state->check_bitwise_operations_allowed(&loc)) {
1496 error_emitted = true;
1497 }
1498
1499 op[0] = this->subexpressions[0]->hir(instructions, state);
1500 op[1] = this->subexpressions[1]->hir(instructions, state);
1501 type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1502 &loc);
1503 result = new(ctx) ir_expression(operations[this->oper], type,
1504 op[0], op[1]);
1505 error_emitted = glsl_type_is_error(op[0]->type) || glsl_type_is_error(op[1]->type);
1506 break;
1507
1508 case ast_less:
1509 case ast_greater:
1510 case ast_lequal:
1511 case ast_gequal:
1512 op[0] = this->subexpressions[0]->hir(instructions, state);
1513 op[1] = this->subexpressions[1]->hir(instructions, state);
1514
1515 type = relational_result_type(op[0], op[1], state, & loc);
1516
1517 /* The relational operators must either generate an error or result
1518 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1519 */
1520 assert(glsl_type_is_error(type)
1521 || (glsl_type_is_boolean(type) && glsl_type_is_scalar(type)));
1522
1523 /* Like NIR, GLSL IR does not have opcodes for > or <=. Instead, swap
1524 * the arguments and use < or >=.
1525 */
1526 if (this->oper == ast_greater || this->oper == ast_lequal) {
1527 ir_rvalue *const tmp = op[0];
1528 op[0] = op[1];
1529 op[1] = tmp;
1530 }
1531
1532 result = new(ctx) ir_expression(operations[this->oper], type,
1533 op[0], op[1]);
1534 error_emitted = glsl_type_is_error(type);
1535 break;
1536
1537 case ast_nequal:
1538 case ast_equal:
1539 op[0] = this->subexpressions[0]->hir(instructions, state);
1540 op[1] = this->subexpressions[1]->hir(instructions, state);
1541
1542 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1543 *
1544 * "The equality operators equal (==), and not equal (!=)
1545 * operate on all types. They result in a scalar Boolean. If
1546 * the operand types do not match, then there must be a
1547 * conversion from Section 4.1.10 "Implicit Conversions"
1548 * applied to one operand that can make them match, in which
1549 * case this conversion is done."
1550 */
1551
1552 if (op[0]->type == &glsl_type_builtin_void || op[1]->type == &glsl_type_builtin_void) {
1553 _mesa_glsl_error(& loc, state, "wrong operand types: "
1554 "no operation `%s' exists that takes a left-hand "
1555 "operand of type 'void' or a right operand of type "
1556 "'void'", (this->oper == ast_equal) ? "==" : "!=");
1557 error_emitted = true;
1558 } else if ((!apply_implicit_conversion(op[0]->type, op[1], state)
1559 && !apply_implicit_conversion(op[1]->type, op[0], state))
1560 || (op[0]->type != op[1]->type)) {
1561 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
1562 "type", (this->oper == ast_equal) ? "==" : "!=");
1563 error_emitted = true;
1564 } else if ((glsl_type_is_array(op[0]->type) || glsl_type_is_array(op[1]->type)) &&
1565 !state->check_version(120, 300, &loc,
1566 "array comparisons forbidden")) {
1567 error_emitted = true;
1568 } else if ((glsl_contains_subroutine(op[0]->type) ||
1569 glsl_contains_subroutine(op[1]->type))) {
1570 _mesa_glsl_error(&loc, state, "subroutine comparisons forbidden");
1571 error_emitted = true;
1572 } else if ((glsl_contains_opaque(op[0]->type) ||
1573 glsl_contains_opaque(op[1]->type))) {
1574 _mesa_glsl_error(&loc, state, "opaque type comparisons forbidden");
1575 error_emitted = true;
1576 }
1577
1578 if (error_emitted) {
1579 result = new(ctx) ir_constant(false);
1580 } else {
1581 result = do_comparison(ctx, operations[this->oper], op[0], op[1]);
1582 assert(result->type == &glsl_type_builtin_bool);
1583 }
1584 break;
1585
1586 case ast_bit_and:
1587 case ast_bit_xor:
1588 case ast_bit_or:
1589 op[0] = this->subexpressions[0]->hir(instructions, state);
1590 op[1] = this->subexpressions[1]->hir(instructions, state);
1591 type = bit_logic_result_type(op[0], op[1], this->oper, state, &loc);
1592 result = new(ctx) ir_expression(operations[this->oper], type,
1593 op[0], op[1]);
1594 error_emitted = glsl_type_is_error(op[0]->type) || glsl_type_is_error(op[1]->type);
1595 break;
1596
1597 case ast_bit_not:
1598 op[0] = this->subexpressions[0]->hir(instructions, state);
1599
1600 if (!state->check_bitwise_operations_allowed(&loc)) {
1601 error_emitted = true;
1602 }
1603
1604 if (!glsl_type_is_integer_32_64(op[0]->type)) {
1605 _mesa_glsl_error(&loc, state, "operand of `~' must be an integer");
1606 error_emitted = true;
1607 }
1608
1609 type = error_emitted ? &glsl_type_builtin_error : op[0]->type;
1610 result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL);
1611 break;
1612
1613 case ast_logic_and: {
1614 exec_list rhs_instructions;
1615 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1616 "LHS", &error_emitted);
1617 op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
1618 "RHS", &error_emitted);
1619
1620 if (rhs_instructions.is_empty()) {
1621 result = new(ctx) ir_expression(ir_binop_logic_and, op[0], op[1]);
1622 } else {
1623 ir_variable *const tmp = new(ctx) ir_variable(&glsl_type_builtin_bool,
1624 "and_tmp",
1625 ir_var_temporary);
1626 instructions->push_tail(tmp);
1627
1628 ir_if *const stmt = new(ctx) ir_if(op[0]);
1629 instructions->push_tail(stmt);
1630
1631 stmt->then_instructions.append_list(&rhs_instructions);
1632 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
1633 ir_assignment *const then_assign =
1634 new(ctx) ir_assignment(then_deref, op[1]);
1635 stmt->then_instructions.push_tail(then_assign);
1636
1637 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
1638 ir_assignment *const else_assign =
1639 new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false));
1640 stmt->else_instructions.push_tail(else_assign);
1641
1642 result = new(ctx) ir_dereference_variable(tmp);
1643 }
1644 break;
1645 }
1646
1647 case ast_logic_or: {
1648 exec_list rhs_instructions;
1649 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1650 "LHS", &error_emitted);
1651 op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
1652 "RHS", &error_emitted);
1653
1654 if (rhs_instructions.is_empty()) {
1655 result = new(ctx) ir_expression(ir_binop_logic_or, op[0], op[1]);
1656 } else {
1657 ir_variable *const tmp = new(ctx) ir_variable(&glsl_type_builtin_bool,
1658 "or_tmp",
1659 ir_var_temporary);
1660 instructions->push_tail(tmp);
1661
1662 ir_if *const stmt = new(ctx) ir_if(op[0]);
1663 instructions->push_tail(stmt);
1664
1665 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
1666 ir_assignment *const then_assign =
1667 new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true));
1668 stmt->then_instructions.push_tail(then_assign);
1669
1670 stmt->else_instructions.append_list(&rhs_instructions);
1671 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
1672 ir_assignment *const else_assign =
1673 new(ctx) ir_assignment(else_deref, op[1]);
1674 stmt->else_instructions.push_tail(else_assign);
1675
1676 result = new(ctx) ir_dereference_variable(tmp);
1677 }
1678 break;
1679 }
1680
1681 case ast_logic_xor:
1682 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1683 *
1684 * "The logical binary operators and (&&), or ( | | ), and
1685 * exclusive or (^^). They operate only on two Boolean
1686 * expressions and result in a Boolean expression."
1687 */
1688 op[0] = get_scalar_boolean_operand(instructions, state, this, 0, "LHS",
1689 &error_emitted);
1690 op[1] = get_scalar_boolean_operand(instructions, state, this, 1, "RHS",
1691 &error_emitted);
1692
1693 result = new(ctx) ir_expression(operations[this->oper], &glsl_type_builtin_bool,
1694 op[0], op[1]);
1695 break;
1696
1697 case ast_logic_not:
1698 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1699 "operand", &error_emitted);
1700
1701 result = new(ctx) ir_expression(operations[this->oper], &glsl_type_builtin_bool,
1702 op[0], NULL);
1703 break;
1704
1705 case ast_mul_assign:
1706 case ast_div_assign:
1707 case ast_add_assign:
1708 case ast_sub_assign: {
1709 this->subexpressions[0]->set_is_lhs(true);
1710 op[0] = this->subexpressions[0]->hir(instructions, state);
1711 op[1] = this->subexpressions[1]->hir(instructions, state);
1712
1713 orig_type = op[0]->type;
1714
1715 /* Break out if operand types were not parsed successfully. */
1716 if ((op[0]->type == &glsl_type_builtin_error ||
1717 op[1]->type == &glsl_type_builtin_error)) {
1718 error_emitted = true;
1719 result = ir_rvalue::error_value(ctx);
1720 break;
1721 }
1722
1723 type = arithmetic_result_type(op[0], op[1],
1724 (this->oper == ast_mul_assign),
1725 state, & loc);
1726
1727 if (type != orig_type) {
1728 _mesa_glsl_error(& loc, state,
1729 "could not implicitly convert "
1730 "%s to %s", glsl_get_type_name(type), glsl_get_type_name(orig_type));
1731 type = &glsl_type_builtin_error;
1732 }
1733
1734 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1735 op[0], op[1]);
1736
1737 error_emitted =
1738 do_assignment(instructions, state,
1739 this->subexpressions[0]->non_lvalue_description,
1740 op[0]->clone(ctx, NULL), temp_rhs,
1741 &result, needs_rvalue, false,
1742 this->subexpressions[0]->get_location());
1743
1744 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1745 * explicitly test for this because none of the binary expression
1746 * operators allow array operands either.
1747 */
1748
1749 break;
1750 }
1751
1752 case ast_mod_assign: {
1753 this->subexpressions[0]->set_is_lhs(true);
1754 op[0] = this->subexpressions[0]->hir(instructions, state);
1755 op[1] = this->subexpressions[1]->hir(instructions, state);
1756
1757 /* Break out if operand types were not parsed successfully. */
1758 if ((op[0]->type == &glsl_type_builtin_error ||
1759 op[1]->type == &glsl_type_builtin_error)) {
1760 error_emitted = true;
1761 result = ir_rvalue::error_value(ctx);
1762 break;
1763 }
1764
1765 orig_type = op[0]->type;
1766 type = modulus_result_type(op[0], op[1], state, &loc);
1767
1768 if (type != orig_type) {
1769 _mesa_glsl_error(& loc, state,
1770 "could not implicitly convert "
1771 "%s to %s", glsl_get_type_name(type), glsl_get_type_name(orig_type));
1772 type = &glsl_type_builtin_error;
1773 }
1774
1775 assert(operations[this->oper] == ir_binop_mod);
1776
1777 ir_rvalue *temp_rhs;
1778 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1779 op[0], op[1]);
1780
1781 error_emitted =
1782 do_assignment(instructions, state,
1783 this->subexpressions[0]->non_lvalue_description,
1784 op[0]->clone(ctx, NULL), temp_rhs,
1785 &result, needs_rvalue, false,
1786 this->subexpressions[0]->get_location());
1787 break;
1788 }
1789
1790 case ast_ls_assign:
1791 case ast_rs_assign: {
1792 this->subexpressions[0]->set_is_lhs(true);
1793 op[0] = this->subexpressions[0]->hir(instructions, state);
1794 op[1] = this->subexpressions[1]->hir(instructions, state);
1795
1796 /* Break out if operand types were not parsed successfully. */
1797 if ((op[0]->type == &glsl_type_builtin_error ||
1798 op[1]->type == &glsl_type_builtin_error)) {
1799 error_emitted = true;
1800 result = ir_rvalue::error_value(ctx);
1801 break;
1802 }
1803
1804 type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1805 &loc);
1806 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1807 type, op[0], op[1]);
1808 error_emitted =
1809 do_assignment(instructions, state,
1810 this->subexpressions[0]->non_lvalue_description,
1811 op[0]->clone(ctx, NULL), temp_rhs,
1812 &result, needs_rvalue, false,
1813 this->subexpressions[0]->get_location());
1814 break;
1815 }
1816
1817 case ast_and_assign:
1818 case ast_xor_assign:
1819 case ast_or_assign: {
1820 this->subexpressions[0]->set_is_lhs(true);
1821 op[0] = this->subexpressions[0]->hir(instructions, state);
1822 op[1] = this->subexpressions[1]->hir(instructions, state);
1823
1824 /* Break out if operand types were not parsed successfully. */
1825 if ((op[0]->type == &glsl_type_builtin_error ||
1826 op[1]->type == &glsl_type_builtin_error)) {
1827 error_emitted = true;
1828 result = ir_rvalue::error_value(ctx);
1829 break;
1830 }
1831
1832 orig_type = op[0]->type;
1833 type = bit_logic_result_type(op[0], op[1], this->oper, state, &loc);
1834
1835 if (type != orig_type) {
1836 _mesa_glsl_error(& loc, state,
1837 "could not implicitly convert "
1838 "%s to %s", glsl_get_type_name(type), glsl_get_type_name(orig_type));
1839 type = &glsl_type_builtin_error;
1840 }
1841
1842 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1843 type, op[0], op[1]);
1844 error_emitted =
1845 do_assignment(instructions, state,
1846 this->subexpressions[0]->non_lvalue_description,
1847 op[0]->clone(ctx, NULL), temp_rhs,
1848 &result, needs_rvalue, false,
1849 this->subexpressions[0]->get_location());
1850 break;
1851 }
1852
1853 case ast_conditional: {
1854 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1855 *
1856 * "The ternary selection operator (?:). It operates on three
1857 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1858 * first expression, which must result in a scalar Boolean."
1859 */
1860 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1861 "condition", &error_emitted);
1862
1863 /* The :? operator is implemented by generating an anonymous temporary
1864 * followed by an if-statement. The last instruction in each branch of
1865 * the if-statement assigns a value to the anonymous temporary. This
1866 * temporary is the r-value of the expression.
1867 */
1868 exec_list then_instructions;
1869 exec_list else_instructions;
1870
1871 op[1] = this->subexpressions[1]->hir(&then_instructions, state);
1872 op[2] = this->subexpressions[2]->hir(&else_instructions, state);
1873
1874 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1875 *
1876 * "The second and third expressions can be any type, as
1877 * long their types match, or there is a conversion in
1878 * Section 4.1.10 "Implicit Conversions" that can be applied
1879 * to one of the expressions to make their types match. This
1880 * resulting matching type is the type of the entire
1881 * expression."
1882 */
1883 if ((!apply_implicit_conversion(op[1]->type, op[2], state)
1884 && !apply_implicit_conversion(op[2]->type, op[1], state))
1885 || (op[1]->type != op[2]->type)) {
1886 YYLTYPE loc = this->subexpressions[1]->get_location();
1887
1888 _mesa_glsl_error(& loc, state, "second and third operands of ?: "
1889 "operator must have matching types");
1890 error_emitted = true;
1891 type = &glsl_type_builtin_error;
1892 } else {
1893 type = op[1]->type;
1894 }
1895
1896 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1897 *
1898 * "The second and third expressions must be the same type, but can
1899 * be of any type other than an array."
1900 */
1901 if (glsl_type_is_array(type) &&
1902 !state->check_version(120, 300, &loc,
1903 "second and third operands of ?: operator "
1904 "cannot be arrays")) {
1905 error_emitted = true;
1906 }
1907
1908 /* From section 4.1.7 of the GLSL 4.50 spec (Opaque Types):
1909 *
1910 * "Except for array indexing, structure member selection, and
1911 * parentheses, opaque variables are not allowed to be operands in
1912 * expressions; such use results in a compile-time error."
1913 */
1914 if (glsl_contains_opaque(type)) {
1915 if (!(state->has_bindless() && (glsl_type_is_image(type) || glsl_type_is_sampler(type)))) {
1916 _mesa_glsl_error(&loc, state, "variables of type %s cannot be "
1917 "operands of the ?: operator", glsl_get_type_name(type));
1918 error_emitted = true;
1919 }
1920 }
1921
1922 ir_constant *cond_val = op[0]->constant_expression_value(ctx);
1923
1924 if (then_instructions.is_empty()
1925 && else_instructions.is_empty()
1926 && cond_val != NULL) {
1927 result = cond_val->value.b[0] ? op[1] : op[2];
1928 } else {
1929 /* The copy to conditional_tmp reads the whole array. */
1930 if (glsl_type_is_array(type)) {
1931 mark_whole_array_access(op[1]);
1932 mark_whole_array_access(op[2]);
1933 }
1934
1935 ir_variable *const tmp =
1936 new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
1937 instructions->push_tail(tmp);
1938
1939 ir_if *const stmt = new(ctx) ir_if(op[0]);
1940 instructions->push_tail(stmt);
1941
1942 then_instructions.move_nodes_to(& stmt->then_instructions);
1943 ir_dereference *const then_deref =
1944 new(ctx) ir_dereference_variable(tmp);
1945 ir_assignment *const then_assign =
1946 new(ctx) ir_assignment(then_deref, op[1]);
1947 stmt->then_instructions.push_tail(then_assign);
1948
1949 else_instructions.move_nodes_to(& stmt->else_instructions);
1950 ir_dereference *const else_deref =
1951 new(ctx) ir_dereference_variable(tmp);
1952 ir_assignment *const else_assign =
1953 new(ctx) ir_assignment(else_deref, op[2]);
1954 stmt->else_instructions.push_tail(else_assign);
1955
1956 result = new(ctx) ir_dereference_variable(tmp);
1957 }
1958 break;
1959 }
1960
1961 case ast_pre_inc:
1962 case ast_pre_dec: {
1963 this->non_lvalue_description = (this->oper == ast_pre_inc)
1964 ? "pre-increment operation" : "pre-decrement operation";
1965
1966 op[0] = this->subexpressions[0]->hir(instructions, state);
1967 op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
1968
1969 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1970
1971 ir_rvalue *temp_rhs;
1972 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1973 op[0], op[1]);
1974
1975 error_emitted =
1976 do_assignment(instructions, state,
1977 this->subexpressions[0]->non_lvalue_description,
1978 op[0]->clone(ctx, NULL), temp_rhs,
1979 &result, needs_rvalue, false,
1980 this->subexpressions[0]->get_location());
1981 break;
1982 }
1983
1984 case ast_post_inc:
1985 case ast_post_dec: {
1986 this->non_lvalue_description = (this->oper == ast_post_inc)
1987 ? "post-increment operation" : "post-decrement operation";
1988 op[0] = this->subexpressions[0]->hir(instructions, state);
1989 op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
1990
1991 error_emitted = glsl_type_is_error(op[0]->type) || glsl_type_is_error(op[1]->type);
1992
1993 if (error_emitted) {
1994 result = ir_rvalue::error_value(ctx);
1995 break;
1996 }
1997
1998 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1999
2000 ir_rvalue *temp_rhs;
2001 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
2002 op[0], op[1]);
2003
2004 /* Get a temporary of a copy of the lvalue before it's modified.
2005 * This may get thrown away later.
2006 */
2007 result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
2008
2009 ir_rvalue *junk_rvalue;
2010 error_emitted =
2011 do_assignment(instructions, state,
2012 this->subexpressions[0]->non_lvalue_description,
2013 op[0]->clone(ctx, NULL), temp_rhs,
2014 &junk_rvalue, false, false,
2015 this->subexpressions[0]->get_location());
2016
2017 break;
2018 }
2019
2020 case ast_field_selection:
2021 result = _mesa_ast_field_selection_to_hir(this, instructions, state);
2022 break;
2023
2024 case ast_array_index: {
2025 YYLTYPE index_loc = subexpressions[1]->get_location();
2026
2027 /* Getting if an array is being used uninitialized is beyond what we get
2028 * from ir_value.data.assigned. Setting is_lhs as true would force to
2029 * not raise a uninitialized warning when using an array
2030 */
2031 subexpressions[0]->set_is_lhs(true);
2032 op[0] = subexpressions[0]->hir(instructions, state);
2033 op[1] = subexpressions[1]->hir(instructions, state);
2034
2035 result = _mesa_ast_array_index_to_hir(ctx, state, op[0], op[1],
2036 loc, index_loc);
2037
2038 if (glsl_type_is_error(result->type))
2039 error_emitted = true;
2040
2041 break;
2042 }
2043
2044 case ast_unsized_array_dim:
2045 unreachable("ast_unsized_array_dim: Should never get here.");
2046
2047 case ast_function_call:
2048 /* Should *NEVER* get here. ast_function_call should always be handled
2049 * by ast_function_expression::hir.
2050 */
2051 unreachable("ast_function_call: handled elsewhere ");
2052
2053 case ast_identifier: {
2054 /* ast_identifier can appear several places in a full abstract syntax
2055 * tree. This particular use must be at location specified in the grammar
2056 * as 'variable_identifier'.
2057 */
2058 ir_variable *var =
2059 state->symbols->get_variable(this->primary_expression.identifier);
2060
2061 if (var == NULL) {
2062 /* the identifier might be a subroutine name */
2063 char *sub_name;
2064 sub_name = ralloc_asprintf(ctx, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state->stage), this->primary_expression.identifier);
2065 var = state->symbols->get_variable(sub_name);
2066 ralloc_free(sub_name);
2067 }
2068
2069 if (var != NULL) {
2070 var->data.used = true;
2071 result = new(ctx) ir_dereference_variable(var);
2072
2073 if ((var->data.mode == ir_var_auto || var->data.mode == ir_var_shader_out)
2074 && !this->is_lhs
2075 && result->variable_referenced()->data.assigned != true
2076 && !is_gl_identifier(var->name)) {
2077 _mesa_glsl_warning(&loc, state, "`%s' used uninitialized",
2078 this->primary_expression.identifier);
2079 }
2080
2081 if (var->is_fb_fetch_color_output()) {
2082 /* From the EXT_shader_framebuffer_fetch spec:
2083 *
2084 * "Unless the GL_EXT_shader_framebuffer_fetch extension has been
2085 * enabled in addition, it's an error to use gl_LastFragData if it
2086 * hasn't been explicitly redeclared with layout(noncoherent)."
2087 */
2088 if (var->data.memory_coherent && !state->EXT_shader_framebuffer_fetch_enable) {
2089 _mesa_glsl_error(&loc, state,
2090 "invalid use of framebuffer fetch output not "
2091 "qualified with layout(noncoherent)");
2092 }
2093 } else if (var->data.fb_fetch_output) {
2094 /* From the ARM_shader_framebuffer_fetch_depth_stencil spec:
2095 *
2096 * "It is not legal for a fragment shader to read from gl_LastFragDepthARM
2097 * and gl_LastFragStencilARM if the early_fragment_tests layout qualifier
2098 * is specified. This will result in a compile-time error."
2099 */
2100 if (state->fs_early_fragment_tests) {
2101 _mesa_glsl_error(&loc, state,
2102 "invalid use of depth or stencil fetch "
2103 "with early fragment tests enabled");
2104 }
2105 }
2106
2107 } else {
2108 _mesa_glsl_error(& loc, state, "`%s' undeclared",
2109 this->primary_expression.identifier);
2110
2111 result = ir_rvalue::error_value(ctx);
2112 error_emitted = true;
2113 }
2114 break;
2115 }
2116
2117 case ast_int_constant:
2118 result = new(ctx) ir_constant(this->primary_expression.int_constant);
2119 break;
2120
2121 case ast_uint_constant:
2122 result = new(ctx) ir_constant(this->primary_expression.uint_constant);
2123 break;
2124
2125 case ast_float16_constant:
2126 result = new(ctx) ir_constant(float16_t(this->primary_expression.float16_constant));
2127 break;
2128
2129 case ast_float_constant:
2130 result = new(ctx) ir_constant(this->primary_expression.float_constant);
2131 break;
2132
2133 case ast_bool_constant:
2134 result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
2135 break;
2136
2137 case ast_double_constant:
2138 result = new(ctx) ir_constant(this->primary_expression.double_constant);
2139 break;
2140
2141 case ast_uint64_constant:
2142 result = new(ctx) ir_constant(this->primary_expression.uint64_constant);
2143 break;
2144
2145 case ast_int64_constant:
2146 result = new(ctx) ir_constant(this->primary_expression.int64_constant);
2147 break;
2148
2149 case ast_sequence: {
2150 /* It should not be possible to generate a sequence in the AST without
2151 * any expressions in it.
2152 */
2153 assert(!this->expressions.is_empty());
2154
2155 /* The r-value of a sequence is the last expression in the sequence. If
2156 * the other expressions in the sequence do not have side-effects (and
2157 * therefore add instructions to the instruction list), they get dropped
2158 * on the floor.
2159 */
2160 exec_node *previous_tail = NULL;
2161 YYLTYPE previous_operand_loc = loc;
2162
2163 foreach_list_typed (ast_node, ast, link, &this->expressions) {
2164 /* If one of the operands of comma operator does not generate any
2165 * code, we want to emit a warning. At each pass through the loop
2166 * previous_tail will point to the last instruction in the stream
2167 * *before* processing the previous operand. Naturally,
2168 * instructions->get_tail_raw() will point to the last instruction in
2169 * the stream *after* processing the previous operand. If the two
2170 * pointers match, then the previous operand had no effect.
2171 *
2172 * The warning behavior here differs slightly from GCC. GCC will
2173 * only emit a warning if none of the left-hand operands have an
2174 * effect. However, it will emit a warning for each. I believe that
2175 * there are some cases in C (especially with GCC extensions) where
2176 * it is useful to have an intermediate step in a sequence have no
2177 * effect, but I don't think these cases exist in GLSL. Either way,
2178 * it would be a giant hassle to replicate that behavior.
2179 */
2180 if (previous_tail == instructions->get_tail_raw()) {
2181 _mesa_glsl_warning(&previous_operand_loc, state,
2182 "left-hand operand of comma expression has "
2183 "no effect");
2184 }
2185
2186 /* The tail is directly accessed instead of using the get_tail()
2187 * method for performance reasons. get_tail() has extra code to
2188 * return NULL when the list is empty. We don't care about that
2189 * here, so using get_tail_raw() is fine.
2190 */
2191 previous_tail = instructions->get_tail_raw();
2192 previous_operand_loc = ast->get_location();
2193
2194 result = ast->hir(instructions, state);
2195 }
2196
2197 /* Any errors should have already been emitted in the loop above.
2198 */
2199 error_emitted = true;
2200 break;
2201 }
2202 }
2203 type = NULL; /* use result->type, not type. */
2204 assert(error_emitted || (result != NULL || !needs_rvalue));
2205
2206 if (result && glsl_type_is_error(result->type) && !error_emitted)
2207 _mesa_glsl_error(& loc, state, "type mismatch");
2208
2209 return result;
2210 }
2211
2212 bool
has_sequence_subexpression() const2213 ast_expression::has_sequence_subexpression() const
2214 {
2215 switch (this->oper) {
2216 case ast_plus:
2217 case ast_neg:
2218 case ast_bit_not:
2219 case ast_logic_not:
2220 case ast_pre_inc:
2221 case ast_pre_dec:
2222 case ast_post_inc:
2223 case ast_post_dec:
2224 return this->subexpressions[0]->has_sequence_subexpression();
2225
2226 case ast_assign:
2227 case ast_add:
2228 case ast_sub:
2229 case ast_mul:
2230 case ast_div:
2231 case ast_mod:
2232 case ast_lshift:
2233 case ast_rshift:
2234 case ast_less:
2235 case ast_greater:
2236 case ast_lequal:
2237 case ast_gequal:
2238 case ast_nequal:
2239 case ast_equal:
2240 case ast_bit_and:
2241 case ast_bit_xor:
2242 case ast_bit_or:
2243 case ast_logic_and:
2244 case ast_logic_or:
2245 case ast_logic_xor:
2246 case ast_array_index:
2247 case ast_mul_assign:
2248 case ast_div_assign:
2249 case ast_add_assign:
2250 case ast_sub_assign:
2251 case ast_mod_assign:
2252 case ast_ls_assign:
2253 case ast_rs_assign:
2254 case ast_and_assign:
2255 case ast_xor_assign:
2256 case ast_or_assign:
2257 return this->subexpressions[0]->has_sequence_subexpression() ||
2258 this->subexpressions[1]->has_sequence_subexpression();
2259
2260 case ast_conditional:
2261 return this->subexpressions[0]->has_sequence_subexpression() ||
2262 this->subexpressions[1]->has_sequence_subexpression() ||
2263 this->subexpressions[2]->has_sequence_subexpression();
2264
2265 case ast_sequence:
2266 return true;
2267
2268 case ast_field_selection:
2269 case ast_identifier:
2270 case ast_int_constant:
2271 case ast_uint_constant:
2272 case ast_float16_constant:
2273 case ast_float_constant:
2274 case ast_bool_constant:
2275 case ast_double_constant:
2276 case ast_int64_constant:
2277 case ast_uint64_constant:
2278 return false;
2279
2280 case ast_aggregate:
2281 return false;
2282
2283 case ast_function_call:
2284 unreachable("should be handled by ast_function_expression::hir");
2285
2286 case ast_unsized_array_dim:
2287 unreachable("ast_unsized_array_dim: Should never get here.");
2288 }
2289
2290 return false;
2291 }
2292
2293 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)2294 ast_expression_statement::hir(exec_list *instructions,
2295 struct _mesa_glsl_parse_state *state)
2296 {
2297 /* It is possible to have expression statements that don't have an
2298 * expression. This is the solitary semicolon:
2299 *
2300 * for (i = 0; i < 5; i++)
2301 * ;
2302 *
2303 * In this case the expression will be NULL. Test for NULL and don't do
2304 * anything in that case.
2305 */
2306 if (expression != NULL)
2307 expression->hir_no_rvalue(instructions, state);
2308
2309 /* Statements do not have r-values.
2310 */
2311 return NULL;
2312 }
2313
2314
2315 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)2316 ast_compound_statement::hir(exec_list *instructions,
2317 struct _mesa_glsl_parse_state *state)
2318 {
2319 if (new_scope)
2320 state->symbols->push_scope();
2321
2322 foreach_list_typed (ast_node, ast, link, &this->statements)
2323 ast->hir(instructions, state);
2324
2325 if (new_scope)
2326 state->symbols->pop_scope();
2327
2328 /* Compound statements do not have r-values.
2329 */
2330 return NULL;
2331 }
2332
2333 /**
2334 * Evaluate the given exec_node (which should be an ast_node representing
2335 * a single array dimension) and return its integer value.
2336 */
2337 static unsigned
process_array_size(exec_node * node,struct _mesa_glsl_parse_state * state)2338 process_array_size(exec_node *node,
2339 struct _mesa_glsl_parse_state *state)
2340 {
2341 void *mem_ctx = state;
2342
2343 exec_list dummy_instructions;
2344
2345 ast_node *array_size = exec_node_data(ast_node, node, link);
2346
2347 /**
2348 * Dimensions other than the outermost dimension can by unsized if they
2349 * are immediately sized by a constructor or initializer.
2350 */
2351 if (((ast_expression*)array_size)->oper == ast_unsized_array_dim)
2352 return 0;
2353
2354 ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
2355 YYLTYPE loc = array_size->get_location();
2356
2357 if (ir == NULL) {
2358 _mesa_glsl_error(& loc, state,
2359 "array size could not be resolved");
2360 return 0;
2361 }
2362
2363 if (!glsl_type_is_integer_32(ir->type)) {
2364 _mesa_glsl_error(& loc, state,
2365 "array size must be integer type");
2366 return 0;
2367 }
2368
2369 if (!glsl_type_is_scalar(ir->type)) {
2370 _mesa_glsl_error(& loc, state,
2371 "array size must be scalar type");
2372 return 0;
2373 }
2374
2375 ir_constant *const size = ir->constant_expression_value(mem_ctx);
2376 if (size == NULL ||
2377 (state->is_version(120, 300) &&
2378 array_size->has_sequence_subexpression())) {
2379 _mesa_glsl_error(& loc, state, "array size must be a "
2380 "constant valued expression");
2381 return 0;
2382 }
2383
2384 if (size->value.i[0] <= 0) {
2385 _mesa_glsl_error(& loc, state, "array size must be > 0");
2386 return 0;
2387 }
2388
2389 assert(size->type == ir->type);
2390
2391 /* If the array size is const (and we've verified that
2392 * it is) then no instructions should have been emitted
2393 * when we converted it to HIR. If they were emitted,
2394 * then either the array size isn't const after all, or
2395 * we are emitting unnecessary instructions.
2396 */
2397 assert(dummy_instructions.is_empty());
2398
2399 return size->value.u[0];
2400 }
2401
2402 static const glsl_type *
process_array_type(YYLTYPE * loc,const glsl_type * base,ast_array_specifier * array_specifier,struct _mesa_glsl_parse_state * state)2403 process_array_type(YYLTYPE *loc, const glsl_type *base,
2404 ast_array_specifier *array_specifier,
2405 struct _mesa_glsl_parse_state *state)
2406 {
2407 const glsl_type *array_type = base;
2408
2409 if (array_specifier != NULL) {
2410 if (glsl_type_is_array(base)) {
2411
2412 /* From page 19 (page 25) of the GLSL 1.20 spec:
2413 *
2414 * "Only one-dimensional arrays may be declared."
2415 */
2416 if (!state->check_arrays_of_arrays_allowed(loc)) {
2417 return &glsl_type_builtin_error;
2418 }
2419 }
2420
2421 for (exec_node *node = array_specifier->array_dimensions.get_tail_raw();
2422 !node->is_head_sentinel(); node = node->prev) {
2423 unsigned array_size = process_array_size(node, state);
2424 array_type = glsl_array_type(array_type, array_size, 0);
2425 }
2426 }
2427
2428 return array_type;
2429 }
2430
2431 static bool
precision_qualifier_allowed(const glsl_type * type)2432 precision_qualifier_allowed(const glsl_type *type)
2433 {
2434 /* Precision qualifiers apply to floating point, integer and opaque
2435 * types.
2436 *
2437 * Section 4.5.2 (Precision Qualifiers) of the GLSL 1.30 spec says:
2438 * "Any floating point or any integer declaration can have the type
2439 * preceded by one of these precision qualifiers [...] Literal
2440 * constants do not have precision qualifiers. Neither do Boolean
2441 * variables.
2442 *
2443 * Section 4.5 (Precision and Precision Qualifiers) of the GLSL 1.30
2444 * spec also says:
2445 *
2446 * "Precision qualifiers are added for code portability with OpenGL
2447 * ES, not for functionality. They have the same syntax as in OpenGL
2448 * ES."
2449 *
2450 * Section 8 (Built-In Functions) of the GLSL ES 1.00 spec says:
2451 *
2452 * "uniform lowp sampler2D sampler;
2453 * highp vec2 coord;
2454 * ...
2455 * lowp vec4 col = texture2D (sampler, coord);
2456 * // texture2D returns lowp"
2457 *
2458 * From this, we infer that GLSL 1.30 (and later) should allow precision
2459 * qualifiers on sampler types just like float and integer types.
2460 */
2461 const glsl_type *const t = glsl_without_array(type);
2462
2463 return (glsl_type_is_float(t) || glsl_type_is_integer_32(t) || glsl_contains_opaque(t)) &&
2464 !glsl_type_is_struct(t);
2465 }
2466
2467 const glsl_type *
glsl_type(const char ** name,struct _mesa_glsl_parse_state * state) const2468 ast_type_specifier::glsl_type(const char **name,
2469 struct _mesa_glsl_parse_state *state) const
2470 {
2471 const struct glsl_type *type;
2472
2473 if (this->type != NULL)
2474 type = this->type;
2475 else if (structure)
2476 type = structure->type;
2477 else
2478 type = state->symbols->get_type(this->type_name);
2479 *name = this->type_name;
2480
2481 YYLTYPE loc = this->get_location();
2482 type = process_array_type(&loc, type, this->array_specifier, state);
2483
2484 return type;
2485 }
2486
2487 /**
2488 * From the OpenGL ES 3.0 spec, 4.5.4 Default Precision Qualifiers:
2489 *
2490 * "The precision statement
2491 *
2492 * precision precision-qualifier type;
2493 *
2494 * can be used to establish a default precision qualifier. The type field can
2495 * be either int or float or any of the sampler types, (...) If type is float,
2496 * the directive applies to non-precision-qualified floating point type
2497 * (scalar, vector, and matrix) declarations. If type is int, the directive
2498 * applies to all non-precision-qualified integer type (scalar, vector, signed,
2499 * and unsigned) declarations."
2500 *
2501 * We use the symbol table to keep the values of the default precisions for
2502 * each 'type' in each scope and we use the 'type' string from the precision
2503 * statement as key in the symbol table. When we want to retrieve the default
2504 * precision associated with a given glsl_type we need to know the type string
2505 * associated with it. This is what this function returns.
2506 */
2507 static const char *
get_type_name_for_precision_qualifier(const glsl_type * type)2508 get_type_name_for_precision_qualifier(const glsl_type *type)
2509 {
2510 switch (type->base_type) {
2511 case GLSL_TYPE_FLOAT:
2512 return "float";
2513 case GLSL_TYPE_UINT:
2514 case GLSL_TYPE_INT:
2515 return "int";
2516 case GLSL_TYPE_ATOMIC_UINT:
2517 return "atomic_uint";
2518 case GLSL_TYPE_IMAGE:
2519 FALLTHROUGH;
2520 case GLSL_TYPE_SAMPLER: {
2521 const unsigned type_idx =
2522 type->sampler_array + 2 * type->sampler_shadow;
2523 const unsigned offset = glsl_type_is_sampler(type) ? 0 : 4;
2524 assert(type_idx < 4);
2525 switch (type->sampled_type) {
2526 case GLSL_TYPE_FLOAT:
2527 switch (type->sampler_dimensionality) {
2528 case GLSL_SAMPLER_DIM_1D: {
2529 assert(glsl_type_is_sampler(type));
2530 static const char *const names[4] = {
2531 "sampler1D", "sampler1DArray",
2532 "sampler1DShadow", "sampler1DArrayShadow"
2533 };
2534 return names[type_idx];
2535 }
2536 case GLSL_SAMPLER_DIM_2D: {
2537 static const char *const names[8] = {
2538 "sampler2D", "sampler2DArray",
2539 "sampler2DShadow", "sampler2DArrayShadow",
2540 "image2D", "image2DArray", NULL, NULL
2541 };
2542 return names[offset + type_idx];
2543 }
2544 case GLSL_SAMPLER_DIM_3D: {
2545 static const char *const names[8] = {
2546 "sampler3D", NULL, NULL, NULL,
2547 "image3D", NULL, NULL, NULL
2548 };
2549 return names[offset + type_idx];
2550 }
2551 case GLSL_SAMPLER_DIM_CUBE: {
2552 static const char *const names[8] = {
2553 "samplerCube", "samplerCubeArray",
2554 "samplerCubeShadow", "samplerCubeArrayShadow",
2555 "imageCube", NULL, NULL, NULL
2556 };
2557 return names[offset + type_idx];
2558 }
2559 case GLSL_SAMPLER_DIM_MS: {
2560 assert(glsl_type_is_sampler(type));
2561 static const char *const names[4] = {
2562 "sampler2DMS", "sampler2DMSArray", NULL, NULL
2563 };
2564 return names[type_idx];
2565 }
2566 case GLSL_SAMPLER_DIM_RECT: {
2567 assert(glsl_type_is_sampler(type));
2568 static const char *const names[4] = {
2569 "samplerRect", NULL, "samplerRectShadow", NULL
2570 };
2571 return names[type_idx];
2572 }
2573 case GLSL_SAMPLER_DIM_BUF: {
2574 static const char *const names[8] = {
2575 "samplerBuffer", NULL, NULL, NULL,
2576 "imageBuffer", NULL, NULL, NULL
2577 };
2578 return names[offset + type_idx];
2579 }
2580 case GLSL_SAMPLER_DIM_EXTERNAL: {
2581 assert(glsl_type_is_sampler(type));
2582 static const char *const names[4] = {
2583 "samplerExternalOES", NULL, NULL, NULL
2584 };
2585 return names[type_idx];
2586 }
2587 default:
2588 unreachable("Unsupported sampler/image dimensionality");
2589 } /* sampler/image float dimensionality */
2590 break;
2591 case GLSL_TYPE_INT:
2592 switch (type->sampler_dimensionality) {
2593 case GLSL_SAMPLER_DIM_1D: {
2594 assert(glsl_type_is_sampler(type));
2595 static const char *const names[4] = {
2596 "isampler1D", "isampler1DArray", NULL, NULL
2597 };
2598 return names[type_idx];
2599 }
2600 case GLSL_SAMPLER_DIM_2D: {
2601 static const char *const names[8] = {
2602 "isampler2D", "isampler2DArray", NULL, NULL,
2603 "iimage2D", "iimage2DArray", NULL, NULL
2604 };
2605 return names[offset + type_idx];
2606 }
2607 case GLSL_SAMPLER_DIM_3D: {
2608 static const char *const names[8] = {
2609 "isampler3D", NULL, NULL, NULL,
2610 "iimage3D", NULL, NULL, NULL
2611 };
2612 return names[offset + type_idx];
2613 }
2614 case GLSL_SAMPLER_DIM_CUBE: {
2615 static const char *const names[8] = {
2616 "isamplerCube", "isamplerCubeArray", NULL, NULL,
2617 "iimageCube", NULL, NULL, NULL
2618 };
2619 return names[offset + type_idx];
2620 }
2621 case GLSL_SAMPLER_DIM_MS: {
2622 assert(glsl_type_is_sampler(type));
2623 static const char *const names[4] = {
2624 "isampler2DMS", "isampler2DMSArray", NULL, NULL
2625 };
2626 return names[type_idx];
2627 }
2628 case GLSL_SAMPLER_DIM_RECT: {
2629 assert(glsl_type_is_sampler(type));
2630 static const char *const names[4] = {
2631 "isamplerRect", NULL, "isamplerRectShadow", NULL
2632 };
2633 return names[type_idx];
2634 }
2635 case GLSL_SAMPLER_DIM_BUF: {
2636 static const char *const names[8] = {
2637 "isamplerBuffer", NULL, NULL, NULL,
2638 "iimageBuffer", NULL, NULL, NULL
2639 };
2640 return names[offset + type_idx];
2641 }
2642 default:
2643 unreachable("Unsupported isampler/iimage dimensionality");
2644 } /* sampler/image int dimensionality */
2645 break;
2646 case GLSL_TYPE_UINT:
2647 switch (type->sampler_dimensionality) {
2648 case GLSL_SAMPLER_DIM_1D: {
2649 assert(glsl_type_is_sampler(type));
2650 static const char *const names[4] = {
2651 "usampler1D", "usampler1DArray", NULL, NULL
2652 };
2653 return names[type_idx];
2654 }
2655 case GLSL_SAMPLER_DIM_2D: {
2656 static const char *const names[8] = {
2657 "usampler2D", "usampler2DArray", NULL, NULL,
2658 "uimage2D", "uimage2DArray", NULL, NULL
2659 };
2660 return names[offset + type_idx];
2661 }
2662 case GLSL_SAMPLER_DIM_3D: {
2663 static const char *const names[8] = {
2664 "usampler3D", NULL, NULL, NULL,
2665 "uimage3D", NULL, NULL, NULL
2666 };
2667 return names[offset + type_idx];
2668 }
2669 case GLSL_SAMPLER_DIM_CUBE: {
2670 static const char *const names[8] = {
2671 "usamplerCube", "usamplerCubeArray", NULL, NULL,
2672 "uimageCube", NULL, NULL, NULL
2673 };
2674 return names[offset + type_idx];
2675 }
2676 case GLSL_SAMPLER_DIM_MS: {
2677 assert(glsl_type_is_sampler(type));
2678 static const char *const names[4] = {
2679 "usampler2DMS", "usampler2DMSArray", NULL, NULL
2680 };
2681 return names[type_idx];
2682 }
2683 case GLSL_SAMPLER_DIM_RECT: {
2684 assert(glsl_type_is_sampler(type));
2685 static const char *const names[4] = {
2686 "usamplerRect", NULL, "usamplerRectShadow", NULL
2687 };
2688 return names[type_idx];
2689 }
2690 case GLSL_SAMPLER_DIM_BUF: {
2691 static const char *const names[8] = {
2692 "usamplerBuffer", NULL, NULL, NULL,
2693 "uimageBuffer", NULL, NULL, NULL
2694 };
2695 return names[offset + type_idx];
2696 }
2697 default:
2698 unreachable("Unsupported usampler/uimage dimensionality");
2699 } /* sampler/image uint dimensionality */
2700 break;
2701 default:
2702 unreachable("Unsupported sampler/image type");
2703 } /* sampler/image type */
2704 break;
2705 } /* GLSL_TYPE_SAMPLER/GLSL_TYPE_IMAGE */
2706 break;
2707 default:
2708 unreachable("Unsupported type");
2709 } /* base type */
2710 }
2711
2712 static unsigned
select_gles_precision(unsigned qual_precision,const glsl_type * type,struct _mesa_glsl_parse_state * state,YYLTYPE * loc)2713 select_gles_precision(unsigned qual_precision,
2714 const glsl_type *type,
2715 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
2716 {
2717 /* Precision qualifiers do not have any meaning in Desktop GLSL.
2718 * In GLES we take the precision from the type qualifier if present,
2719 * otherwise, if the type of the variable allows precision qualifiers at
2720 * all, we look for the default precision qualifier for that type in the
2721 * current scope.
2722 */
2723 assert(state->es_shader);
2724
2725 unsigned precision = GLSL_PRECISION_NONE;
2726 if (qual_precision) {
2727 precision = qual_precision;
2728 } else if (precision_qualifier_allowed(type)) {
2729 const char *type_name =
2730 get_type_name_for_precision_qualifier(glsl_without_array(type));
2731 assert(type_name != NULL);
2732
2733 precision =
2734 state->symbols->get_default_precision_qualifier(type_name);
2735 if (precision == ast_precision_none) {
2736 _mesa_glsl_error(loc, state,
2737 "No precision specified in this scope for type `%s'",
2738 glsl_get_type_name(type));
2739 }
2740 }
2741
2742
2743 /* Section 4.1.7.3 (Atomic Counters) of the GLSL ES 3.10 spec says:
2744 *
2745 * "The default precision of all atomic types is highp. It is an error to
2746 * declare an atomic type with a different precision or to specify the
2747 * default precision for an atomic type to be lowp or mediump."
2748 */
2749 if (glsl_type_is_atomic_uint(type) && precision != ast_precision_high) {
2750 _mesa_glsl_error(loc, state,
2751 "atomic_uint can only have highp precision qualifier");
2752 }
2753
2754 return precision;
2755 }
2756
2757 const glsl_type *
glsl_type(const char ** name,struct _mesa_glsl_parse_state * state) const2758 ast_fully_specified_type::glsl_type(const char **name,
2759 struct _mesa_glsl_parse_state *state) const
2760 {
2761 return this->specifier->glsl_type(name, state);
2762 }
2763
2764 /**
2765 * Determine whether a toplevel variable declaration declares a varying. This
2766 * function operates by examining the variable's mode and the shader target,
2767 * so it correctly identifies linkage variables regardless of whether they are
2768 * declared using the deprecated "varying" syntax or the new "in/out" syntax.
2769 *
2770 * Passing a non-toplevel variable declaration (e.g. a function parameter) to
2771 * this function will produce undefined results.
2772 */
2773 static bool
is_varying_var(ir_variable * var,gl_shader_stage target)2774 is_varying_var(ir_variable *var, gl_shader_stage target)
2775 {
2776 switch (target) {
2777 case MESA_SHADER_VERTEX:
2778 return var->data.mode == ir_var_shader_out;
2779 case MESA_SHADER_FRAGMENT:
2780 return var->data.mode == ir_var_shader_in ||
2781 (var->data.mode == ir_var_system_value &&
2782 var->data.location == SYSTEM_VALUE_FRAG_COORD);
2783 default:
2784 return var->data.mode == ir_var_shader_out || var->data.mode == ir_var_shader_in;
2785 }
2786 }
2787
2788 static bool
is_allowed_invariant(ir_variable * var,struct _mesa_glsl_parse_state * state)2789 is_allowed_invariant(ir_variable *var, struct _mesa_glsl_parse_state *state)
2790 {
2791 if (is_varying_var(var, state->stage))
2792 return true;
2793
2794 /* ES2 says:
2795 *
2796 * "For the built-in special variables, gl_FragCoord can only be declared
2797 * invariant if and only if gl_Position is declared invariant. Similarly
2798 * gl_PointCoord can only be declared invariant if and only if gl_PointSize
2799 * is declared invariant. It is an error to declare gl_FrontFacing as
2800 * invariant. The invariance of gl_FrontFacing is the same as the invariance
2801 * of gl_Position."
2802 *
2803 * ES3.1 says about invariance:
2804 *
2805 * "How does this rule apply to the built-in special variables?
2806 *
2807 * Option 1: It should be the same as for varyings. But gl_Position is used
2808 * internally by the rasterizer as well as for gl_FragCoord so there may be
2809 * cases where rasterization is required to be invariant but gl_FragCoord is
2810 * not.
2811 *
2812 * RESOLUTION: Option 1."
2813 *
2814 * and the ES3 spec has similar text but the "RESOLUTION" is missing.
2815 *
2816 * Any system values should be from built-in special variables.
2817 */
2818 if (var->data.mode == ir_var_system_value) {
2819 if (state->is_version(0, 300)) {
2820 return true;
2821 } else {
2822 /* Note: We don't actually have a check that the VS's PointSize is
2823 * invariant, even when it's treated as a varying.
2824 */
2825 if (var->data.location == SYSTEM_VALUE_POINT_COORD)
2826 return true;
2827 }
2828 }
2829
2830 /* From Section 4.6.1 ("The Invariant Qualifier") GLSL 1.20 spec:
2831 * "Only variables output from a vertex shader can be candidates
2832 * for invariance".
2833 */
2834 if (!state->is_version(130, 100))
2835 return false;
2836
2837 /*
2838 * Later specs remove this language - so allowed invariant
2839 * on fragment shader outputs as well.
2840 */
2841 if (state->stage == MESA_SHADER_FRAGMENT &&
2842 var->data.mode == ir_var_shader_out)
2843 return true;
2844 return false;
2845 }
2846
2847 static void
validate_component_layout_for_type(struct _mesa_glsl_parse_state * state,YYLTYPE * loc,const glsl_type * type,unsigned qual_component)2848 validate_component_layout_for_type(struct _mesa_glsl_parse_state *state,
2849 YYLTYPE *loc, const glsl_type *type,
2850 unsigned qual_component)
2851 {
2852 type = glsl_without_array(type);
2853 unsigned components = glsl_get_component_slots(type);
2854
2855 if (glsl_type_is_matrix(type) || glsl_type_is_struct(type)) {
2856 _mesa_glsl_error(loc, state, "component layout qualifier "
2857 "cannot be applied to a matrix, a structure, "
2858 "a block, or an array containing any of these.");
2859 } else if (components > 4 && glsl_type_is_64bit(type)) {
2860 _mesa_glsl_error(loc, state, "component layout qualifier "
2861 "cannot be applied to dvec%u.",
2862 components / 2);
2863 } else if (qual_component != 0 && (qual_component + components - 1) > 3) {
2864 _mesa_glsl_error(loc, state, "component overflow (%u > 3)",
2865 (qual_component + components - 1));
2866 } else if (qual_component == 1 && glsl_type_is_64bit(type)) {
2867 /* We don't bother checking for 3 as it should be caught by the
2868 * overflow check above.
2869 */
2870 _mesa_glsl_error(loc, state, "doubles cannot begin at component 1 or 3");
2871 }
2872 }
2873
2874 /**
2875 * Matrix layout qualifiers are only allowed on certain types
2876 */
2877 static void
validate_matrix_layout_for_type(struct _mesa_glsl_parse_state * state,YYLTYPE * loc,const glsl_type * type,ir_variable * var)2878 validate_matrix_layout_for_type(struct _mesa_glsl_parse_state *state,
2879 YYLTYPE *loc,
2880 const glsl_type *type,
2881 ir_variable *var)
2882 {
2883 if (var && !var->is_in_buffer_block()) {
2884 /* Layout qualifiers may only apply to interface blocks and fields in
2885 * them.
2886 */
2887 _mesa_glsl_error(loc, state,
2888 "uniform block layout qualifiers row_major and "
2889 "column_major may not be applied to variables "
2890 "outside of uniform blocks");
2891 } else if (!glsl_type_is_matrix(glsl_without_array(type))) {
2892 /* The OpenGL ES 3.0 conformance tests did not originally allow
2893 * matrix layout qualifiers on non-matrices. However, the OpenGL
2894 * 4.4 and OpenGL ES 3.0 (revision TBD) specifications were
2895 * amended to specifically allow these layouts on all types. Emit
2896 * a warning so that people know their code may not be portable.
2897 */
2898 _mesa_glsl_warning(loc, state,
2899 "uniform block layout qualifiers row_major and "
2900 "column_major applied to non-matrix types may "
2901 "be rejected by older compilers");
2902 }
2903 }
2904
2905 static bool
validate_xfb_buffer_qualifier(YYLTYPE * loc,struct _mesa_glsl_parse_state * state,unsigned xfb_buffer)2906 validate_xfb_buffer_qualifier(YYLTYPE *loc,
2907 struct _mesa_glsl_parse_state *state,
2908 unsigned xfb_buffer) {
2909 if (xfb_buffer >= state->Const.MaxTransformFeedbackBuffers) {
2910 _mesa_glsl_error(loc, state,
2911 "invalid xfb_buffer specified %d is larger than "
2912 "MAX_TRANSFORM_FEEDBACK_BUFFERS - 1 (%d).",
2913 xfb_buffer,
2914 state->Const.MaxTransformFeedbackBuffers - 1);
2915 return false;
2916 }
2917
2918 return true;
2919 }
2920
2921 /* From the ARB_enhanced_layouts spec:
2922 *
2923 * "Variables and block members qualified with *xfb_offset* can be
2924 * scalars, vectors, matrices, structures, and (sized) arrays of these.
2925 * The offset must be a multiple of the size of the first component of
2926 * the first qualified variable or block member, or a compile-time error
2927 * results. Further, if applied to an aggregate containing a double,
2928 * the offset must also be a multiple of 8, and the space taken in the
2929 * buffer will be a multiple of 8.
2930 */
2931 static bool
validate_xfb_offset_qualifier(YYLTYPE * loc,struct _mesa_glsl_parse_state * state,int xfb_offset,const glsl_type * type,unsigned component_size)2932 validate_xfb_offset_qualifier(YYLTYPE *loc,
2933 struct _mesa_glsl_parse_state *state,
2934 int xfb_offset, const glsl_type *type,
2935 unsigned component_size) {
2936 const glsl_type *t_without_array = glsl_without_array(type);
2937
2938 if (xfb_offset != -1 && glsl_type_is_unsized_array(type)) {
2939 _mesa_glsl_error(loc, state,
2940 "xfb_offset can't be used with unsized arrays.");
2941 return false;
2942 }
2943
2944 /* Make sure nested structs don't contain unsized arrays, and validate
2945 * any xfb_offsets on interface members.
2946 */
2947 if (glsl_type_is_struct(t_without_array) || glsl_type_is_interface(t_without_array))
2948 for (unsigned int i = 0; i < t_without_array->length; i++) {
2949 const glsl_type *member_t = t_without_array->fields.structure[i].type;
2950
2951 /* When the interface block doesn't have an xfb_offset qualifier then
2952 * we apply the component size rules at the member level.
2953 */
2954 if (xfb_offset == -1)
2955 component_size = glsl_contains_double(member_t) ? 8 : 4;
2956
2957 int xfb_offset = t_without_array->fields.structure[i].offset;
2958 validate_xfb_offset_qualifier(loc, state, xfb_offset, member_t,
2959 component_size);
2960 }
2961
2962 /* Nested structs or interface block without offset may not have had an
2963 * offset applied yet so return.
2964 */
2965 if (xfb_offset == -1) {
2966 return true;
2967 }
2968
2969 if (xfb_offset % component_size) {
2970 _mesa_glsl_error(loc, state,
2971 "invalid qualifier xfb_offset=%d must be a multiple "
2972 "of the first component size of the first qualified "
2973 "variable or block member. Or double if an aggregate "
2974 "that contains a double (%d).",
2975 xfb_offset, component_size);
2976 return false;
2977 }
2978
2979 return true;
2980 }
2981
2982 static bool
validate_stream_qualifier(YYLTYPE * loc,struct _mesa_glsl_parse_state * state,unsigned stream)2983 validate_stream_qualifier(YYLTYPE *loc, struct _mesa_glsl_parse_state *state,
2984 unsigned stream)
2985 {
2986 if (stream >= state->consts->MaxVertexStreams) {
2987 _mesa_glsl_error(loc, state,
2988 "invalid stream specified %d is larger than "
2989 "MAX_VERTEX_STREAMS - 1 (%d).",
2990 stream, state->consts->MaxVertexStreams - 1);
2991 return false;
2992 }
2993
2994 return true;
2995 }
2996
2997 static void
apply_explicit_binding(struct _mesa_glsl_parse_state * state,YYLTYPE * loc,ir_variable * var,const glsl_type * type,const ast_type_qualifier * qual)2998 apply_explicit_binding(struct _mesa_glsl_parse_state *state,
2999 YYLTYPE *loc,
3000 ir_variable *var,
3001 const glsl_type *type,
3002 const ast_type_qualifier *qual)
3003 {
3004 if (!qual->flags.q.uniform && !qual->flags.q.buffer) {
3005 _mesa_glsl_error(loc, state,
3006 "the \"binding\" qualifier only applies to uniforms and "
3007 "shader storage buffer objects");
3008 return;
3009 }
3010
3011 unsigned qual_binding;
3012 if (!process_qualifier_constant(state, loc, "binding", qual->binding,
3013 &qual_binding)) {
3014 return;
3015 }
3016
3017 const struct gl_constants *consts = state->consts;
3018 unsigned elements = glsl_type_is_array(type) ? glsl_get_aoa_size(type) : 1;
3019 unsigned max_index = qual_binding + elements - 1;
3020 const glsl_type *base_type = glsl_without_array(type);
3021
3022 if (glsl_type_is_interface(base_type)) {
3023 /* UBOs. From page 60 of the GLSL 4.20 specification:
3024 * "If the binding point for any uniform block instance is less than zero,
3025 * or greater than or equal to the implementation-dependent maximum
3026 * number of uniform buffer bindings, a compilation error will occur.
3027 * When the binding identifier is used with a uniform block instanced as
3028 * an array of size N, all elements of the array from binding through
3029 * binding + N – 1 must be within this range."
3030 *
3031 * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
3032 */
3033 if (qual->flags.q.uniform &&
3034 max_index >= consts->MaxUniformBufferBindings) {
3035 _mesa_glsl_error(loc, state, "layout(binding = %u) for %d UBOs exceeds "
3036 "the maximum number of UBO binding points (%d)",
3037 qual_binding, elements,
3038 consts->MaxUniformBufferBindings);
3039 return;
3040 }
3041
3042 /* SSBOs. From page 67 of the GLSL 4.30 specification:
3043 * "If the binding point for any uniform or shader storage block instance
3044 * is less than zero, or greater than or equal to the
3045 * implementation-dependent maximum number of uniform buffer bindings, a
3046 * compile-time error will occur. When the binding identifier is used
3047 * with a uniform or shader storage block instanced as an array of size
3048 * N, all elements of the array from binding through binding + N – 1 must
3049 * be within this range."
3050 */
3051 if (qual->flags.q.buffer &&
3052 max_index >= consts->MaxShaderStorageBufferBindings) {
3053 _mesa_glsl_error(loc, state, "layout(binding = %u) for %d SSBOs exceeds "
3054 "the maximum number of SSBO binding points (%d)",
3055 qual_binding, elements,
3056 consts->MaxShaderStorageBufferBindings);
3057 return;
3058 }
3059 } else if (glsl_type_is_sampler(base_type)) {
3060 /* Samplers. From page 63 of the GLSL 4.20 specification:
3061 * "If the binding is less than zero, or greater than or equal to the
3062 * implementation-dependent maximum supported number of units, a
3063 * compilation error will occur. When the binding identifier is used
3064 * with an array of size N, all elements of the array from binding
3065 * through binding + N - 1 must be within this range."
3066 */
3067 unsigned limit = consts->MaxCombinedTextureImageUnits;
3068
3069 if (max_index >= limit) {
3070 _mesa_glsl_error(loc, state, "layout(binding = %d) for %d samplers "
3071 "exceeds the maximum number of texture image units "
3072 "(%u)", qual_binding, elements, limit);
3073
3074 return;
3075 }
3076 } else if (glsl_contains_atomic(base_type)) {
3077 assert(consts->MaxAtomicBufferBindings <= MAX_COMBINED_ATOMIC_BUFFERS);
3078 if (qual_binding >= consts->MaxAtomicBufferBindings) {
3079 _mesa_glsl_error(loc, state, "layout(binding = %d) exceeds the "
3080 "maximum number of atomic counter buffer bindings "
3081 "(%u)", qual_binding,
3082 consts->MaxAtomicBufferBindings);
3083
3084 return;
3085 }
3086 } else if ((state->is_version(420, 310) ||
3087 state->ARB_shading_language_420pack_enable) &&
3088 glsl_type_is_image(base_type)) {
3089 assert(consts->MaxImageUnits <= MAX_IMAGE_UNITS);
3090 if (max_index >= consts->MaxImageUnits) {
3091 _mesa_glsl_error(loc, state, "Image binding %d exceeds the "
3092 "maximum number of image units (%d)", max_index,
3093 consts->MaxImageUnits);
3094 return;
3095 }
3096
3097 } else {
3098 _mesa_glsl_error(loc, state,
3099 "the \"binding\" qualifier only applies to uniform "
3100 "blocks, storage blocks, opaque variables, or arrays "
3101 "thereof");
3102 return;
3103 }
3104
3105 var->data.explicit_binding = true;
3106 var->data.binding = qual_binding;
3107
3108 return;
3109 }
3110
3111 static void
validate_fragment_flat_interpolation_input(struct _mesa_glsl_parse_state * state,YYLTYPE * loc,const glsl_interp_mode interpolation,const struct glsl_type * var_type,ir_variable_mode mode)3112 validate_fragment_flat_interpolation_input(struct _mesa_glsl_parse_state *state,
3113 YYLTYPE *loc,
3114 const glsl_interp_mode interpolation,
3115 const struct glsl_type *var_type,
3116 ir_variable_mode mode)
3117 {
3118 if (state->stage != MESA_SHADER_FRAGMENT ||
3119 interpolation == INTERP_MODE_FLAT ||
3120 mode != ir_var_shader_in)
3121 return;
3122
3123 /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
3124 * so must integer vertex outputs.
3125 *
3126 * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
3127 * "Fragment shader inputs that are signed or unsigned integers or
3128 * integer vectors must be qualified with the interpolation qualifier
3129 * flat."
3130 *
3131 * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
3132 * "Fragment shader inputs that are, or contain, signed or unsigned
3133 * integers or integer vectors must be qualified with the
3134 * interpolation qualifier flat."
3135 *
3136 * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
3137 * "Vertex shader outputs that are, or contain, signed or unsigned
3138 * integers or integer vectors must be qualified with the
3139 * interpolation qualifier flat."
3140 *
3141 * Note that prior to GLSL 1.50, this requirement applied to vertex
3142 * outputs rather than fragment inputs. That creates problems in the
3143 * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
3144 * desktop GL shaders. For GLSL ES shaders, we follow the spec and
3145 * apply the restriction to both vertex outputs and fragment inputs.
3146 *
3147 * Note also that the desktop GLSL specs are missing the text "or
3148 * contain"; this is presumably an oversight, since there is no
3149 * reasonable way to interpolate a fragment shader input that contains
3150 * an integer. See Khronos bug #15671.
3151 */
3152 if ((state->is_version(130, 300) || state->EXT_gpu_shader4_enable)
3153 && glsl_contains_integer(var_type)) {
3154 _mesa_glsl_error(loc, state, "if a fragment input is (or contains) "
3155 "an integer, then it must be qualified with 'flat'");
3156 }
3157
3158 /* Double fragment inputs must be qualified with 'flat'.
3159 *
3160 * From the "Overview" of the ARB_gpu_shader_fp64 extension spec:
3161 * "This extension does not support interpolation of double-precision
3162 * values; doubles used as fragment shader inputs must be qualified as
3163 * "flat"."
3164 *
3165 * From section 4.3.4 ("Inputs") of the GLSL 4.00 spec:
3166 * "Fragment shader inputs that are signed or unsigned integers, integer
3167 * vectors, or any double-precision floating-point type must be
3168 * qualified with the interpolation qualifier flat."
3169 *
3170 * Note that the GLSL specs are missing the text "or contain"; this is
3171 * presumably an oversight. See Khronos bug #15671.
3172 *
3173 * The 'double' type does not exist in GLSL ES so far.
3174 */
3175 if (state->has_double()
3176 && glsl_contains_double(var_type)) {
3177 _mesa_glsl_error(loc, state, "if a fragment input is (or contains) "
3178 "a double, then it must be qualified with 'flat'");
3179 }
3180
3181 /* Bindless sampler/image fragment inputs must be qualified with 'flat'.
3182 *
3183 * From section 4.3.4 of the ARB_bindless_texture spec:
3184 *
3185 * "(modify last paragraph, p. 35, allowing samplers and images as
3186 * fragment shader inputs) ... Fragment inputs can only be signed and
3187 * unsigned integers and integer vectors, floating point scalars,
3188 * floating-point vectors, matrices, sampler and image types, or arrays
3189 * or structures of these. Fragment shader inputs that are signed or
3190 * unsigned integers, integer vectors, or any double-precision floating-
3191 * point type, or any sampler or image type must be qualified with the
3192 * interpolation qualifier "flat"."
3193 */
3194 if (state->has_bindless()
3195 && (glsl_contains_sampler(var_type) || glsl_type_contains_image(var_type))) {
3196 _mesa_glsl_error(loc, state, "if a fragment input is (or contains) "
3197 "a bindless sampler (or image), then it must be "
3198 "qualified with 'flat'");
3199 }
3200 }
3201
3202 static void
validate_interpolation_qualifier(struct _mesa_glsl_parse_state * state,YYLTYPE * loc,const glsl_interp_mode interpolation,const struct ast_type_qualifier * qual,const struct glsl_type * var_type,ir_variable_mode mode)3203 validate_interpolation_qualifier(struct _mesa_glsl_parse_state *state,
3204 YYLTYPE *loc,
3205 const glsl_interp_mode interpolation,
3206 const struct ast_type_qualifier *qual,
3207 const struct glsl_type *var_type,
3208 ir_variable_mode mode)
3209 {
3210 /* Interpolation qualifiers can only apply to shader inputs or outputs, but
3211 * not to vertex shader inputs nor fragment shader outputs.
3212 *
3213 * From section 4.3 ("Storage Qualifiers") of the GLSL 1.30 spec:
3214 * "Outputs from a vertex shader (out) and inputs to a fragment
3215 * shader (in) can be further qualified with one or more of these
3216 * interpolation qualifiers"
3217 * ...
3218 * "These interpolation qualifiers may only precede the qualifiers in,
3219 * centroid in, out, or centroid out in a declaration. They do not apply
3220 * to the deprecated storage qualifiers varying or centroid
3221 * varying. They also do not apply to inputs into a vertex shader or
3222 * outputs from a fragment shader."
3223 *
3224 * From section 4.3 ("Storage Qualifiers") of the GLSL ES 3.00 spec:
3225 * "Outputs from a shader (out) and inputs to a shader (in) can be
3226 * further qualified with one of these interpolation qualifiers."
3227 * ...
3228 * "These interpolation qualifiers may only precede the qualifiers
3229 * in, centroid in, out, or centroid out in a declaration. They do
3230 * not apply to inputs into a vertex shader or outputs from a
3231 * fragment shader."
3232 */
3233 if ((state->is_version(130, 300) || state->EXT_gpu_shader4_enable)
3234 && interpolation != INTERP_MODE_NONE) {
3235 const char *i = interpolation_string(interpolation);
3236 if (mode != ir_var_shader_in && mode != ir_var_shader_out)
3237 _mesa_glsl_error(loc, state,
3238 "interpolation qualifier `%s' can only be applied to "
3239 "shader inputs or outputs.", i);
3240
3241 switch (state->stage) {
3242 case MESA_SHADER_VERTEX:
3243 if (mode == ir_var_shader_in) {
3244 _mesa_glsl_error(loc, state,
3245 "interpolation qualifier '%s' cannot be applied to "
3246 "vertex shader inputs", i);
3247 }
3248 break;
3249 case MESA_SHADER_FRAGMENT:
3250 if (mode == ir_var_shader_out) {
3251 _mesa_glsl_error(loc, state,
3252 "interpolation qualifier '%s' cannot be applied to "
3253 "fragment shader outputs", i);
3254 }
3255 break;
3256 default:
3257 break;
3258 }
3259 }
3260
3261 /* Interpolation qualifiers cannot be applied to 'centroid' and
3262 * 'centroid varying'.
3263 *
3264 * From section 4.3 ("Storage Qualifiers") of the GLSL 1.30 spec:
3265 * "interpolation qualifiers may only precede the qualifiers in,
3266 * centroid in, out, or centroid out in a declaration. They do not apply
3267 * to the deprecated storage qualifiers varying or centroid varying."
3268 *
3269 * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
3270 *
3271 * GL_EXT_gpu_shader4 allows this.
3272 */
3273 if (state->is_version(130, 0) && !state->EXT_gpu_shader4_enable
3274 && interpolation != INTERP_MODE_NONE
3275 && qual->flags.q.varying) {
3276
3277 const char *i = interpolation_string(interpolation);
3278 const char *s;
3279 if (qual->flags.q.centroid)
3280 s = "centroid varying";
3281 else
3282 s = "varying";
3283
3284 _mesa_glsl_error(loc, state,
3285 "qualifier '%s' cannot be applied to the "
3286 "deprecated storage qualifier '%s'", i, s);
3287 }
3288
3289 validate_fragment_flat_interpolation_input(state, loc, interpolation,
3290 var_type, mode);
3291 }
3292
3293 static glsl_interp_mode
interpret_interpolation_qualifier(const struct ast_type_qualifier * qual,const struct glsl_type * var_type,ir_variable_mode mode,struct _mesa_glsl_parse_state * state,YYLTYPE * loc)3294 interpret_interpolation_qualifier(const struct ast_type_qualifier *qual,
3295 const struct glsl_type *var_type,
3296 ir_variable_mode mode,
3297 struct _mesa_glsl_parse_state *state,
3298 YYLTYPE *loc)
3299 {
3300 glsl_interp_mode interpolation;
3301 if (qual->flags.q.flat)
3302 interpolation = INTERP_MODE_FLAT;
3303 else if (qual->flags.q.noperspective)
3304 interpolation = INTERP_MODE_NOPERSPECTIVE;
3305 else if (qual->flags.q.smooth)
3306 interpolation = INTERP_MODE_SMOOTH;
3307 else
3308 interpolation = INTERP_MODE_NONE;
3309
3310 validate_interpolation_qualifier(state, loc,
3311 interpolation,
3312 qual, var_type, mode);
3313
3314 return interpolation;
3315 }
3316
3317
3318 static void
apply_explicit_location(const struct ast_type_qualifier * qual,ir_variable * var,struct _mesa_glsl_parse_state * state,YYLTYPE * loc)3319 apply_explicit_location(const struct ast_type_qualifier *qual,
3320 ir_variable *var,
3321 struct _mesa_glsl_parse_state *state,
3322 YYLTYPE *loc)
3323 {
3324 bool fail = false;
3325
3326 unsigned qual_location;
3327 if (!process_qualifier_constant(state, loc, "location", qual->location,
3328 &qual_location)) {
3329 return;
3330 }
3331
3332 /* Checks for GL_ARB_explicit_uniform_location. */
3333 if (qual->flags.q.uniform) {
3334 if (!state->check_explicit_uniform_location_allowed(loc, var))
3335 return;
3336
3337 const struct gl_constants *consts = state->consts;
3338 unsigned max_loc = qual_location + glsl_type_uniform_locations(var->type) - 1;
3339
3340 if (max_loc >= consts->MaxUserAssignableUniformLocations) {
3341 _mesa_glsl_error(loc, state, "location(s) consumed by uniform %s "
3342 ">= MAX_UNIFORM_LOCATIONS (%u)", var->name,
3343 consts->MaxUserAssignableUniformLocations);
3344 return;
3345 }
3346
3347 var->data.explicit_location = true;
3348 var->data.location = qual_location;
3349 return;
3350 }
3351
3352 /* Between GL_ARB_explicit_attrib_location an
3353 * GL_ARB_separate_shader_objects, the inputs and outputs of any shader
3354 * stage can be assigned explicit locations. The checking here associates
3355 * the correct extension with the correct stage's input / output:
3356 *
3357 * input output
3358 * ----- ------
3359 * vertex explicit_loc sso
3360 * tess control sso sso
3361 * tess eval sso sso
3362 * geometry sso sso
3363 * fragment sso explicit_loc
3364 */
3365 switch (state->stage) {
3366 case MESA_SHADER_VERTEX:
3367 if (var->data.mode == ir_var_shader_in) {
3368 if (!state->check_explicit_attrib_location_allowed(loc, var))
3369 return;
3370
3371 break;
3372 }
3373
3374 if (var->data.mode == ir_var_shader_out) {
3375 if (!state->check_separate_shader_objects_allowed(loc, var))
3376 return;
3377
3378 break;
3379 }
3380
3381 fail = true;
3382 break;
3383
3384 case MESA_SHADER_TESS_CTRL:
3385 case MESA_SHADER_TESS_EVAL:
3386 case MESA_SHADER_GEOMETRY:
3387 if (var->data.mode == ir_var_shader_in || var->data.mode == ir_var_shader_out) {
3388 if (!state->check_separate_shader_objects_allowed(loc, var))
3389 return;
3390
3391 break;
3392 }
3393
3394 fail = true;
3395 break;
3396
3397 case MESA_SHADER_FRAGMENT:
3398 if (var->data.mode == ir_var_shader_in) {
3399 if (!state->check_separate_shader_objects_allowed(loc, var))
3400 return;
3401
3402 break;
3403 }
3404
3405 if (var->data.mode == ir_var_shader_out) {
3406 if (!state->check_explicit_attrib_location_allowed(loc, var))
3407 return;
3408
3409 break;
3410 }
3411
3412 fail = true;
3413 break;
3414
3415 case MESA_SHADER_COMPUTE:
3416 _mesa_glsl_error(loc, state,
3417 "compute shader variables cannot be given "
3418 "explicit locations");
3419 return;
3420 default:
3421 fail = true;
3422 break;
3423 };
3424
3425 if (fail) {
3426 _mesa_glsl_error(loc, state,
3427 "%s cannot be given an explicit location in %s shader",
3428 mode_string(var),
3429 _mesa_shader_stage_to_string(state->stage));
3430 } else {
3431 var->data.explicit_location = true;
3432
3433 switch (state->stage) {
3434 case MESA_SHADER_VERTEX:
3435 var->data.location = (var->data.mode == ir_var_shader_in)
3436 ? (qual_location + VERT_ATTRIB_GENERIC0)
3437 : (qual_location + VARYING_SLOT_VAR0);
3438 break;
3439
3440 case MESA_SHADER_TESS_CTRL:
3441 case MESA_SHADER_TESS_EVAL:
3442 case MESA_SHADER_GEOMETRY:
3443 if (var->data.patch)
3444 var->data.location = qual_location + VARYING_SLOT_PATCH0;
3445 else
3446 var->data.location = qual_location + VARYING_SLOT_VAR0;
3447 break;
3448
3449 case MESA_SHADER_FRAGMENT:
3450 var->data.location = (var->data.mode == ir_var_shader_out)
3451 ? (qual_location + FRAG_RESULT_DATA0)
3452 : (qual_location + VARYING_SLOT_VAR0);
3453 break;
3454 default:
3455 assert(!"Unexpected shader type");
3456 break;
3457 }
3458
3459 /* Check if index was set for the uniform instead of the function */
3460 if (qual->flags.q.explicit_index && qual->is_subroutine_decl()) {
3461 _mesa_glsl_error(loc, state, "an index qualifier can only be "
3462 "used with subroutine functions");
3463 return;
3464 }
3465
3466 unsigned qual_index;
3467 if (qual->flags.q.explicit_index &&
3468 process_qualifier_constant(state, loc, "index", qual->index,
3469 &qual_index)) {
3470 /* From the GLSL 4.30 specification, section 4.4.2 (Output
3471 * Layout Qualifiers):
3472 *
3473 * "It is also a compile-time error if a fragment shader
3474 * sets a layout index to less than 0 or greater than 1."
3475 *
3476 * Older specifications don't mandate a behavior; we take
3477 * this as a clarification and always generate the error.
3478 */
3479 if (qual_index > 1) {
3480 _mesa_glsl_error(loc, state,
3481 "explicit index may only be 0 or 1");
3482 } else {
3483 var->data.explicit_index = true;
3484 var->data.index = qual_index;
3485 }
3486 }
3487 }
3488 }
3489
3490 static bool
validate_storage_for_sampler_image_types(ir_variable * var,struct _mesa_glsl_parse_state * state,YYLTYPE * loc)3491 validate_storage_for_sampler_image_types(ir_variable *var,
3492 struct _mesa_glsl_parse_state *state,
3493 YYLTYPE *loc)
3494 {
3495 /* From section 4.1.7 of the GLSL 4.40 spec:
3496 *
3497 * "[Opaque types] can only be declared as function
3498 * parameters or uniform-qualified variables."
3499 *
3500 * From section 4.1.7 of the ARB_bindless_texture spec:
3501 *
3502 * "Samplers may be declared as shader inputs and outputs, as uniform
3503 * variables, as temporary variables, and as function parameters."
3504 *
3505 * From section 4.1.X of the ARB_bindless_texture spec:
3506 *
3507 * "Images may be declared as shader inputs and outputs, as uniform
3508 * variables, as temporary variables, and as function parameters."
3509 */
3510 if (state->has_bindless()) {
3511 if (var->data.mode != ir_var_auto &&
3512 var->data.mode != ir_var_uniform &&
3513 var->data.mode != ir_var_shader_in &&
3514 var->data.mode != ir_var_shader_out &&
3515 var->data.mode != ir_var_function_in &&
3516 var->data.mode != ir_var_function_out &&
3517 var->data.mode != ir_var_function_inout) {
3518 _mesa_glsl_error(loc, state, "bindless image/sampler variables may "
3519 "only be declared as shader inputs and outputs, as "
3520 "uniform variables, as temporary variables and as "
3521 "function parameters");
3522 return false;
3523 }
3524 } else {
3525 if (var->data.mode != ir_var_uniform &&
3526 var->data.mode != ir_var_function_in) {
3527 _mesa_glsl_error(loc, state, "image/sampler variables may only be "
3528 "declared as function parameters or "
3529 "uniform-qualified global variables");
3530 return false;
3531 }
3532 }
3533 return true;
3534 }
3535
3536 static bool
validate_memory_qualifier_for_type(struct _mesa_glsl_parse_state * state,YYLTYPE * loc,const struct ast_type_qualifier * qual,const glsl_type * type)3537 validate_memory_qualifier_for_type(struct _mesa_glsl_parse_state *state,
3538 YYLTYPE *loc,
3539 const struct ast_type_qualifier *qual,
3540 const glsl_type *type)
3541 {
3542 /* From Section 4.10 (Memory Qualifiers) of the GLSL 4.50 spec:
3543 *
3544 * "Memory qualifiers are only supported in the declarations of image
3545 * variables, buffer variables, and shader storage blocks; it is an error
3546 * to use such qualifiers in any other declarations.
3547 */
3548 if (!glsl_type_is_image(type) && !qual->flags.q.buffer) {
3549 if (qual->flags.q.read_only ||
3550 qual->flags.q.write_only ||
3551 qual->flags.q.coherent ||
3552 qual->flags.q._volatile ||
3553 qual->flags.q.restrict_flag) {
3554 _mesa_glsl_error(loc, state, "memory qualifiers may only be applied "
3555 "in the declarations of image variables, buffer "
3556 "variables, and shader storage blocks");
3557 return false;
3558 }
3559 }
3560 return true;
3561 }
3562
3563 static bool
validate_image_format_qualifier_for_type(struct _mesa_glsl_parse_state * state,YYLTYPE * loc,const struct ast_type_qualifier * qual,const glsl_type * type)3564 validate_image_format_qualifier_for_type(struct _mesa_glsl_parse_state *state,
3565 YYLTYPE *loc,
3566 const struct ast_type_qualifier *qual,
3567 const glsl_type *type)
3568 {
3569 /* From section 4.4.6.2 (Format Layout Qualifiers) of the GLSL 4.50 spec:
3570 *
3571 * "Format layout qualifiers can be used on image variable declarations
3572 * (those declared with a basic type having “image ” in its keyword)."
3573 */
3574 if (!glsl_type_is_image(type) && qual->flags.q.explicit_image_format) {
3575 _mesa_glsl_error(loc, state, "format layout qualifiers may only be "
3576 "applied to images");
3577 return false;
3578 }
3579 return true;
3580 }
3581
3582 static void
apply_image_qualifier_to_variable(const struct ast_type_qualifier * qual,ir_variable * var,struct _mesa_glsl_parse_state * state,YYLTYPE * loc)3583 apply_image_qualifier_to_variable(const struct ast_type_qualifier *qual,
3584 ir_variable *var,
3585 struct _mesa_glsl_parse_state *state,
3586 YYLTYPE *loc)
3587 {
3588 const glsl_type *base_type = glsl_without_array(var->type);
3589
3590 if (!validate_image_format_qualifier_for_type(state, loc, qual, base_type) ||
3591 !validate_memory_qualifier_for_type(state, loc, qual, base_type))
3592 return;
3593
3594 if (!glsl_type_is_image(base_type))
3595 return;
3596
3597 if (!validate_storage_for_sampler_image_types(var, state, loc))
3598 return;
3599
3600 var->data.memory_read_only |= qual->flags.q.read_only;
3601 var->data.memory_write_only |= qual->flags.q.write_only;
3602 var->data.memory_coherent |= qual->flags.q.coherent;
3603 var->data.memory_volatile |= qual->flags.q._volatile;
3604 var->data.memory_restrict |= qual->flags.q.restrict_flag;
3605
3606 if (qual->flags.q.explicit_image_format) {
3607 if (var->data.mode == ir_var_function_in) {
3608 _mesa_glsl_error(loc, state, "format qualifiers cannot be used on "
3609 "image function parameters");
3610 }
3611
3612 if (qual->image_base_type != base_type->sampled_type) {
3613 _mesa_glsl_error(loc, state, "format qualifier doesn't match the base "
3614 "data type of the image");
3615 }
3616
3617 var->data.image_format = qual->image_format;
3618 } else if (state->has_image_load_formatted()) {
3619 if (var->data.mode == ir_var_uniform &&
3620 state->EXT_shader_image_load_formatted_warn) {
3621 _mesa_glsl_warning(loc, state, "GL_EXT_image_load_formatted used");
3622 }
3623 } else {
3624 if (var->data.mode == ir_var_uniform) {
3625 if (state->es_shader ||
3626 !(state->is_version(420, 310) || state->ARB_shader_image_load_store_enable)) {
3627 _mesa_glsl_error(loc, state, "all image uniforms must have a "
3628 "format layout qualifier");
3629 } else if (!qual->flags.q.write_only) {
3630 _mesa_glsl_error(loc, state, "image uniforms not qualified with "
3631 "`writeonly' must have a format layout qualifier");
3632 }
3633 }
3634 var->data.image_format = PIPE_FORMAT_NONE;
3635 }
3636
3637 /* From page 70 of the GLSL ES 3.1 specification:
3638 *
3639 * "Except for image variables qualified with the format qualifiers r32f,
3640 * r32i, and r32ui, image variables must specify either memory qualifier
3641 * readonly or the memory qualifier writeonly."
3642 */
3643 if (state->es_shader &&
3644 var->data.image_format != PIPE_FORMAT_R32_FLOAT &&
3645 var->data.image_format != PIPE_FORMAT_R32_SINT &&
3646 var->data.image_format != PIPE_FORMAT_R32_UINT &&
3647 !var->data.memory_read_only &&
3648 !var->data.memory_write_only) {
3649 _mesa_glsl_error(loc, state, "image variables of format other than r32f, "
3650 "r32i or r32ui must be qualified `readonly' or "
3651 "`writeonly'");
3652 }
3653 }
3654
3655 static inline const char*
get_layout_qualifier_string(bool origin_upper_left,bool pixel_center_integer)3656 get_layout_qualifier_string(bool origin_upper_left, bool pixel_center_integer)
3657 {
3658 if (origin_upper_left && pixel_center_integer)
3659 return "origin_upper_left, pixel_center_integer";
3660 else if (origin_upper_left)
3661 return "origin_upper_left";
3662 else if (pixel_center_integer)
3663 return "pixel_center_integer";
3664 else
3665 return " ";
3666 }
3667
3668 static inline bool
is_conflicting_fragcoord_redeclaration(struct _mesa_glsl_parse_state * state,const struct ast_type_qualifier * qual)3669 is_conflicting_fragcoord_redeclaration(struct _mesa_glsl_parse_state *state,
3670 const struct ast_type_qualifier *qual)
3671 {
3672 /* If gl_FragCoord was previously declared, and the qualifiers were
3673 * different in any way, return true.
3674 */
3675 if (state->fs_redeclares_gl_fragcoord) {
3676 return (state->fs_pixel_center_integer != qual->flags.q.pixel_center_integer
3677 || state->fs_origin_upper_left != qual->flags.q.origin_upper_left);
3678 }
3679
3680 return false;
3681 }
3682
3683 static inline bool
is_conflicting_layer_redeclaration(struct _mesa_glsl_parse_state * state,const struct ast_type_qualifier * qual)3684 is_conflicting_layer_redeclaration(struct _mesa_glsl_parse_state *state,
3685 const struct ast_type_qualifier *qual)
3686 {
3687 if (state->redeclares_gl_layer) {
3688 return state->layer_viewport_relative != qual->flags.q.viewport_relative;
3689 }
3690 return false;
3691 }
3692
3693 static inline void
validate_array_dimensions(const glsl_type * t,struct _mesa_glsl_parse_state * state,YYLTYPE * loc)3694 validate_array_dimensions(const glsl_type *t,
3695 struct _mesa_glsl_parse_state *state,
3696 YYLTYPE *loc) {
3697 const glsl_type *top = t;
3698 if (glsl_type_is_array(t)) {
3699 t = t->fields.array;
3700 while (glsl_type_is_array(t)) {
3701 if (glsl_type_is_unsized_array(t)) {
3702 _mesa_glsl_error(loc, state,
3703 "only the outermost array dimension can "
3704 "be unsized, but got %s",
3705 glsl_get_type_name(top));
3706 break;
3707 }
3708 t = t->fields.array;
3709 }
3710 }
3711 }
3712
3713 static void
apply_bindless_qualifier_to_variable(const struct ast_type_qualifier * qual,ir_variable * var,struct _mesa_glsl_parse_state * state,YYLTYPE * loc)3714 apply_bindless_qualifier_to_variable(const struct ast_type_qualifier *qual,
3715 ir_variable *var,
3716 struct _mesa_glsl_parse_state *state,
3717 YYLTYPE *loc)
3718 {
3719 bool has_local_qualifiers = qual->flags.q.bindless_sampler ||
3720 qual->flags.q.bindless_image ||
3721 qual->flags.q.bound_sampler ||
3722 qual->flags.q.bound_image;
3723
3724 /* The ARB_bindless_texture spec says:
3725 *
3726 * "Modify Section 4.4.6 Opaque-Uniform Layout Qualifiers of the GLSL 4.30
3727 * spec"
3728 *
3729 * "If these layout qualifiers are applied to other types of default block
3730 * uniforms, or variables with non-uniform storage, a compile-time error
3731 * will be generated."
3732 */
3733 if (has_local_qualifiers && !qual->flags.q.uniform) {
3734 _mesa_glsl_error(loc, state, "ARB_bindless_texture layout qualifiers "
3735 "can only be applied to default block uniforms or "
3736 "variables with uniform storage");
3737 return;
3738 }
3739
3740 /* The ARB_bindless_texture spec doesn't state anything in this situation,
3741 * but it makes sense to only allow bindless_sampler/bound_sampler for
3742 * sampler types, and respectively bindless_image/bound_image for image
3743 * types.
3744 */
3745 if ((qual->flags.q.bindless_sampler || qual->flags.q.bound_sampler) &&
3746 !glsl_contains_sampler(var->type)) {
3747 _mesa_glsl_error(loc, state, "bindless_sampler or bound_sampler can only "
3748 "be applied to sampler types");
3749 return;
3750 }
3751
3752 if ((qual->flags.q.bindless_image || qual->flags.q.bound_image) &&
3753 !glsl_type_contains_image(var->type)) {
3754 _mesa_glsl_error(loc, state, "bindless_image or bound_image can only be "
3755 "applied to image types");
3756 return;
3757 }
3758
3759 /* The bindless_sampler/bindless_image (and respectively
3760 * bound_sampler/bound_image) layout qualifiers can be set at global and at
3761 * local scope.
3762 */
3763 if (glsl_contains_sampler(var->type) || glsl_type_contains_image(var->type)) {
3764 var->data.bindless = qual->flags.q.bindless_sampler ||
3765 qual->flags.q.bindless_image ||
3766 state->bindless_sampler_specified ||
3767 state->bindless_image_specified;
3768
3769 var->data.bound = qual->flags.q.bound_sampler ||
3770 qual->flags.q.bound_image ||
3771 state->bound_sampler_specified ||
3772 state->bound_image_specified;
3773 }
3774
3775 /* ARB_bindless_texture spec says:
3776 *
3777 * "When used as shader inputs, outputs, uniform block members,
3778 * or temporaries, the value of the sampler is a 64-bit unsigned
3779 * integer handle and never refers to a texture image unit."
3780 *
3781 * The spec doesn't reference images defined inside structs but it was
3782 * clarified with the authors that bindless images are allowed in structs.
3783 * So we treat these images as implicitly bindless just like the types
3784 * in the spec quote above.
3785 */
3786 if (!var->data.bindless && glsl_type_is_struct(var->type) &&
3787 glsl_type_contains_image(var->type)) {
3788 var->data.bindless = true;
3789 }
3790 }
3791
3792 static void
apply_layout_qualifier_to_variable(const struct ast_type_qualifier * qual,ir_variable * var,struct _mesa_glsl_parse_state * state,YYLTYPE * loc)3793 apply_layout_qualifier_to_variable(const struct ast_type_qualifier *qual,
3794 ir_variable *var,
3795 struct _mesa_glsl_parse_state *state,
3796 YYLTYPE *loc)
3797 {
3798 if (var->name != NULL && strcmp(var->name, "gl_FragCoord") == 0) {
3799
3800 /* Section 4.3.8.1, page 39 of GLSL 1.50 spec says:
3801 *
3802 * "Within any shader, the first redeclarations of gl_FragCoord
3803 * must appear before any use of gl_FragCoord."
3804 *
3805 * Generate a compiler error if above condition is not met by the
3806 * fragment shader.
3807 */
3808 ir_variable *earlier = state->symbols->get_variable("gl_FragCoord");
3809 if (earlier != NULL &&
3810 earlier->data.used &&
3811 !state->fs_redeclares_gl_fragcoord) {
3812 _mesa_glsl_error(loc, state,
3813 "gl_FragCoord used before its first redeclaration "
3814 "in fragment shader");
3815 }
3816
3817 /* Make sure all gl_FragCoord redeclarations specify the same layout
3818 * qualifiers.
3819 */
3820 if (is_conflicting_fragcoord_redeclaration(state, qual)) {
3821 const char *const qual_string =
3822 get_layout_qualifier_string(qual->flags.q.origin_upper_left,
3823 qual->flags.q.pixel_center_integer);
3824
3825 const char *const state_string =
3826 get_layout_qualifier_string(state->fs_origin_upper_left,
3827 state->fs_pixel_center_integer);
3828
3829 _mesa_glsl_error(loc, state,
3830 "gl_FragCoord redeclared with different layout "
3831 "qualifiers (%s) and (%s) ",
3832 state_string,
3833 qual_string);
3834 }
3835 state->fs_origin_upper_left = qual->flags.q.origin_upper_left;
3836 state->fs_pixel_center_integer = qual->flags.q.pixel_center_integer;
3837 state->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers =
3838 !qual->flags.q.origin_upper_left && !qual->flags.q.pixel_center_integer;
3839 state->fs_redeclares_gl_fragcoord =
3840 state->fs_origin_upper_left ||
3841 state->fs_pixel_center_integer ||
3842 state->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers;
3843 }
3844
3845 if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer)
3846 && (strcmp(var->name, "gl_FragCoord") != 0)) {
3847 const char *const qual_string = (qual->flags.q.origin_upper_left)
3848 ? "origin_upper_left" : "pixel_center_integer";
3849
3850 _mesa_glsl_error(loc, state,
3851 "layout qualifier `%s' can only be applied to "
3852 "fragment shader input `gl_FragCoord'",
3853 qual_string);
3854 }
3855
3856 if (qual->flags.q.explicit_location) {
3857 apply_explicit_location(qual, var, state, loc);
3858
3859 if (qual->flags.q.explicit_component) {
3860 unsigned qual_component;
3861 if (process_qualifier_constant(state, loc, "component",
3862 qual->component, &qual_component)) {
3863 validate_component_layout_for_type(state, loc, var->type,
3864 qual_component);
3865 var->data.explicit_component = true;
3866 var->data.location_frac = qual_component;
3867 }
3868 }
3869 } else if (qual->flags.q.explicit_index) {
3870 if (!qual->subroutine_list)
3871 _mesa_glsl_error(loc, state,
3872 "explicit index requires explicit location");
3873 } else if (qual->flags.q.explicit_component) {
3874 _mesa_glsl_error(loc, state,
3875 "explicit component requires explicit location");
3876 }
3877
3878 if (qual->flags.q.explicit_binding) {
3879 apply_explicit_binding(state, loc, var, var->type, qual);
3880 }
3881
3882 if (state->stage == MESA_SHADER_GEOMETRY &&
3883 qual->flags.q.out && qual->flags.q.stream) {
3884 unsigned qual_stream;
3885 if (process_qualifier_constant(state, loc, "stream", qual->stream,
3886 &qual_stream) &&
3887 validate_stream_qualifier(loc, state, qual_stream)) {
3888 var->data.stream = qual_stream;
3889 }
3890 }
3891
3892 if (qual->flags.q.out && qual->flags.q.xfb_buffer) {
3893 unsigned qual_xfb_buffer;
3894 if (process_qualifier_constant(state, loc, "xfb_buffer",
3895 qual->xfb_buffer, &qual_xfb_buffer) &&
3896 validate_xfb_buffer_qualifier(loc, state, qual_xfb_buffer)) {
3897 var->data.xfb_buffer = qual_xfb_buffer;
3898 if (qual->flags.q.explicit_xfb_buffer)
3899 var->data.explicit_xfb_buffer = true;
3900 }
3901 }
3902
3903 if (qual->flags.q.explicit_xfb_offset) {
3904 unsigned qual_xfb_offset;
3905 unsigned component_size = glsl_contains_double(var->type) ? 8 : 4;
3906
3907 if (process_qualifier_constant(state, loc, "xfb_offset",
3908 qual->offset, &qual_xfb_offset) &&
3909 validate_xfb_offset_qualifier(loc, state, (int) qual_xfb_offset,
3910 var->type, component_size)) {
3911 var->data.offset = qual_xfb_offset;
3912 var->data.explicit_xfb_offset = true;
3913 }
3914 }
3915
3916 if (qual->flags.q.explicit_xfb_stride) {
3917 unsigned qual_xfb_stride;
3918 if (process_qualifier_constant(state, loc, "xfb_stride",
3919 qual->xfb_stride, &qual_xfb_stride)) {
3920 var->data.xfb_stride = qual_xfb_stride;
3921 var->data.explicit_xfb_stride = true;
3922 }
3923 }
3924
3925 if (glsl_contains_atomic(var->type)) {
3926 if (var->data.mode == ir_var_uniform) {
3927 if (var->data.explicit_binding) {
3928 unsigned *offset =
3929 &state->atomic_counter_offsets[var->data.binding];
3930
3931 if (*offset % ATOMIC_COUNTER_SIZE)
3932 _mesa_glsl_error(loc, state,
3933 "misaligned atomic counter offset");
3934
3935 if (*offset >= state->Const.MaxAtomicCounterBufferSize)
3936 _mesa_glsl_error(loc, state,
3937 "offset > max atomic counter buffer size");
3938
3939 var->data.offset = *offset;
3940 *offset += glsl_atomic_size(var->type);
3941
3942 } else {
3943 _mesa_glsl_error(loc, state,
3944 "atomic counters require explicit binding point");
3945 }
3946 } else if (var->data.mode != ir_var_function_in) {
3947 _mesa_glsl_error(loc, state, "atomic counters may only be declared as "
3948 "function parameters or uniform-qualified "
3949 "global variables");
3950 }
3951 }
3952
3953 if (glsl_contains_sampler(var->type) &&
3954 !validate_storage_for_sampler_image_types(var, state, loc))
3955 return;
3956
3957 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
3958 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
3959 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
3960 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
3961 * These extensions and all following extensions that add the 'layout'
3962 * keyword have been modified to require the use of 'in' or 'out'.
3963 *
3964 * The following extension do not allow the deprecated keywords:
3965 *
3966 * GL_AMD_conservative_depth
3967 * GL_ARB_conservative_depth
3968 * GL_ARB_gpu_shader5
3969 * GL_ARB_separate_shader_objects
3970 * GL_ARB_tessellation_shader
3971 * GL_ARB_transform_feedback3
3972 * GL_ARB_uniform_buffer_object
3973 *
3974 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
3975 * allow layout with the deprecated keywords.
3976 */
3977 const bool relaxed_layout_qualifier_checking =
3978 state->ARB_fragment_coord_conventions_enable;
3979
3980 const bool uses_deprecated_qualifier = qual->flags.q.attribute
3981 || qual->flags.q.varying;
3982 if (qual->has_layout() && uses_deprecated_qualifier) {
3983 if (relaxed_layout_qualifier_checking) {
3984 _mesa_glsl_warning(loc, state,
3985 "`layout' qualifier may not be used with "
3986 "`attribute' or `varying'");
3987 } else {
3988 _mesa_glsl_error(loc, state,
3989 "`layout' qualifier may not be used with "
3990 "`attribute' or `varying'");
3991 }
3992 }
3993
3994 /* Layout qualifiers for gl_FragDepth, which are enabled by extension
3995 * AMD_conservative_depth.
3996 */
3997 if (qual->flags.q.depth_type
3998 && !state->is_version(420, 0)
3999 && !state->AMD_conservative_depth_enable
4000 && !state->ARB_conservative_depth_enable) {
4001 _mesa_glsl_error(loc, state,
4002 "extension GL_AMD_conservative_depth or "
4003 "GL_ARB_conservative_depth must be enabled "
4004 "to use depth layout qualifiers");
4005 } else if (qual->flags.q.depth_type
4006 && strcmp(var->name, "gl_FragDepth") != 0) {
4007 _mesa_glsl_error(loc, state,
4008 "depth layout qualifiers can be applied only to "
4009 "gl_FragDepth");
4010 }
4011
4012 switch (qual->depth_type) {
4013 case ast_depth_any:
4014 var->data.depth_layout = ir_depth_layout_any;
4015 break;
4016 case ast_depth_greater:
4017 var->data.depth_layout = ir_depth_layout_greater;
4018 break;
4019 case ast_depth_less:
4020 var->data.depth_layout = ir_depth_layout_less;
4021 break;
4022 case ast_depth_unchanged:
4023 var->data.depth_layout = ir_depth_layout_unchanged;
4024 break;
4025 default:
4026 var->data.depth_layout = ir_depth_layout_none;
4027 break;
4028 }
4029
4030 if (qual->flags.q.std140 ||
4031 qual->flags.q.std430 ||
4032 qual->flags.q.packed ||
4033 qual->flags.q.shared) {
4034 _mesa_glsl_error(loc, state,
4035 "uniform and shader storage block layout qualifiers "
4036 "std140, std430, packed, and shared can only be "
4037 "applied to uniform or shader storage blocks, not "
4038 "members");
4039 }
4040
4041 if (qual->flags.q.row_major || qual->flags.q.column_major) {
4042 validate_matrix_layout_for_type(state, loc, var->type, var);
4043 }
4044
4045 /* From section 4.4.1.3 of the GLSL 4.50 specification (Fragment Shader
4046 * Inputs):
4047 *
4048 * "Fragment shaders also allow the following layout qualifier on in only
4049 * (not with variable declarations)
4050 * layout-qualifier-id
4051 * early_fragment_tests
4052 * [...]"
4053 */
4054 if (qual->flags.q.early_fragment_tests) {
4055 _mesa_glsl_error(loc, state, "early_fragment_tests layout qualifier only "
4056 "valid in fragment shader input layout declaration.");
4057 }
4058
4059 if (qual->flags.q.inner_coverage) {
4060 _mesa_glsl_error(loc, state, "inner_coverage layout qualifier only "
4061 "valid in fragment shader input layout declaration.");
4062 }
4063
4064 if (qual->flags.q.post_depth_coverage) {
4065 _mesa_glsl_error(loc, state, "post_depth_coverage layout qualifier only "
4066 "valid in fragment shader input layout declaration.");
4067 }
4068
4069 if (state->has_bindless())
4070 apply_bindless_qualifier_to_variable(qual, var, state, loc);
4071
4072 if (qual->flags.q.pixel_interlock_ordered ||
4073 qual->flags.q.pixel_interlock_unordered ||
4074 qual->flags.q.sample_interlock_ordered ||
4075 qual->flags.q.sample_interlock_unordered) {
4076 _mesa_glsl_error(loc, state, "interlock layout qualifiers: "
4077 "pixel_interlock_ordered, pixel_interlock_unordered, "
4078 "sample_interlock_ordered and sample_interlock_unordered, "
4079 "only valid in fragment shader input layout declaration.");
4080 }
4081
4082 if (var->name != NULL && strcmp(var->name, "gl_Layer") == 0) {
4083 if (is_conflicting_layer_redeclaration(state, qual)) {
4084 _mesa_glsl_error(loc, state, "gl_Layer redeclaration with "
4085 "different viewport_relative setting than earlier");
4086 }
4087 state->redeclares_gl_layer = true;
4088 if (qual->flags.q.viewport_relative) {
4089 state->layer_viewport_relative = true;
4090 }
4091 } else if (qual->flags.q.viewport_relative) {
4092 _mesa_glsl_error(loc, state,
4093 "viewport_relative qualifier "
4094 "can only be applied to gl_Layer.");
4095 }
4096 }
4097
4098 static void
apply_type_qualifier_to_variable(const struct ast_type_qualifier * qual,ir_variable * var,struct _mesa_glsl_parse_state * state,YYLTYPE * loc,bool is_parameter)4099 apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
4100 ir_variable *var,
4101 struct _mesa_glsl_parse_state *state,
4102 YYLTYPE *loc,
4103 bool is_parameter)
4104 {
4105 STATIC_ASSERT(sizeof(qual->flags.q) <= sizeof(qual->flags.i));
4106
4107 if (qual->flags.q.invariant) {
4108 if (var->data.used) {
4109 _mesa_glsl_error(loc, state,
4110 "variable `%s' may not be redeclared "
4111 "`invariant' after being used",
4112 var->name);
4113 } else {
4114 var->data.explicit_invariant = true;
4115 var->data.invariant = true;
4116 }
4117 }
4118
4119 if (qual->flags.q.precise) {
4120 if (var->data.used) {
4121 _mesa_glsl_error(loc, state,
4122 "variable `%s' may not be redeclared "
4123 "`precise' after being used",
4124 var->name);
4125 } else {
4126 var->data.precise = 1;
4127 }
4128 }
4129
4130 if (qual->is_subroutine_decl() && !qual->flags.q.uniform) {
4131 _mesa_glsl_error(loc, state,
4132 "`subroutine' may only be applied to uniforms, "
4133 "subroutine type declarations, or function definitions");
4134 }
4135
4136 if (qual->flags.q.constant || qual->flags.q.attribute
4137 || qual->flags.q.uniform
4138 || (qual->flags.q.varying && (state->stage == MESA_SHADER_FRAGMENT)))
4139 var->data.read_only = 1;
4140
4141 if (qual->flags.q.centroid)
4142 var->data.centroid = 1;
4143
4144 if (qual->flags.q.sample)
4145 var->data.sample = 1;
4146
4147 /* Precision qualifiers do not hold any meaning in Desktop GLSL */
4148 if (state->es_shader) {
4149 var->data.precision =
4150 select_gles_precision(qual->precision, var->type, state, loc);
4151 }
4152
4153 if (qual->flags.q.patch)
4154 var->data.patch = 1;
4155
4156 if (qual->flags.q.attribute && state->stage != MESA_SHADER_VERTEX) {
4157 var->type = &glsl_type_builtin_error;
4158 _mesa_glsl_error(loc, state,
4159 "`attribute' variables may not be declared in the "
4160 "%s shader",
4161 _mesa_shader_stage_to_string(state->stage));
4162 }
4163
4164 /* Disallow layout qualifiers which may only appear on layout declarations. */
4165 if (qual->flags.q.prim_type) {
4166 _mesa_glsl_error(loc, state,
4167 "Primitive type may only be specified on GS input or output "
4168 "layout declaration, not on variables.");
4169 }
4170
4171 /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
4172 *
4173 * "However, the const qualifier cannot be used with out or inout."
4174 *
4175 * The same section of the GLSL 4.40 spec further clarifies this saying:
4176 *
4177 * "The const qualifier cannot be used with out or inout, or a
4178 * compile-time error results."
4179 */
4180 if (is_parameter && qual->flags.q.constant && qual->flags.q.out) {
4181 _mesa_glsl_error(loc, state,
4182 "`const' may not be applied to `out' or `inout' "
4183 "function parameters");
4184 }
4185
4186 /* If there is no qualifier that changes the mode of the variable, leave
4187 * the setting alone.
4188 */
4189 assert(var->data.mode != ir_var_temporary);
4190 if (qual->flags.q.in && qual->flags.q.out)
4191 var->data.mode = is_parameter ? ir_var_function_inout : ir_var_shader_out;
4192 else if (qual->flags.q.in)
4193 var->data.mode = is_parameter ? ir_var_function_in : ir_var_shader_in;
4194 else if (qual->flags.q.attribute
4195 || (qual->flags.q.varying && (state->stage == MESA_SHADER_FRAGMENT)))
4196 var->data.mode = ir_var_shader_in;
4197 else if (qual->flags.q.out)
4198 var->data.mode = is_parameter ? ir_var_function_out : ir_var_shader_out;
4199 else if (qual->flags.q.varying && (state->stage == MESA_SHADER_VERTEX))
4200 var->data.mode = ir_var_shader_out;
4201 else if (qual->flags.q.uniform)
4202 var->data.mode = ir_var_uniform;
4203 else if (qual->flags.q.buffer)
4204 var->data.mode = ir_var_shader_storage;
4205 else if (qual->flags.q.shared_storage)
4206 var->data.mode = ir_var_shader_shared;
4207
4208 if (!is_parameter && state->stage == MESA_SHADER_FRAGMENT) {
4209 if (state->has_framebuffer_fetch()) {
4210 if (state->is_version(130, 300))
4211 var->data.fb_fetch_output = qual->flags.q.in && qual->flags.q.out;
4212 else
4213 var->data.fb_fetch_output = (strcmp(var->name, "gl_LastFragData") == 0);
4214 }
4215
4216 if (state->has_framebuffer_fetch_zs() &&
4217 (strcmp(var->name, "gl_LastFragDepthARM") == 0 ||
4218 strcmp(var->name, "gl_LastFragStencilARM") == 0)) {
4219 var->data.fb_fetch_output = 1;
4220 }
4221 }
4222
4223 if (var->data.fb_fetch_output)
4224 var->data.assigned = true;
4225
4226 if (var->is_fb_fetch_color_output()) {
4227 var->data.memory_coherent = !qual->flags.q.non_coherent;
4228
4229 /* From the EXT_shader_framebuffer_fetch spec:
4230 *
4231 * "It is an error to declare an inout fragment output not qualified
4232 * with layout(noncoherent) if the GL_EXT_shader_framebuffer_fetch
4233 * extension hasn't been enabled."
4234 */
4235 if (var->data.memory_coherent &&
4236 !state->EXT_shader_framebuffer_fetch_enable)
4237 _mesa_glsl_error(loc, state,
4238 "invalid declaration of framebuffer fetch output not "
4239 "qualified with layout(noncoherent)");
4240
4241 } else {
4242 /* From the EXT_shader_framebuffer_fetch spec:
4243 *
4244 * "Fragment outputs declared inout may specify the following layout
4245 * qualifier: [...] noncoherent"
4246 */
4247 if (qual->flags.q.non_coherent)
4248 _mesa_glsl_error(loc, state,
4249 "invalid layout(noncoherent) qualifier not part of "
4250 "framebuffer fetch output declaration");
4251 }
4252
4253 if (!is_parameter && is_varying_var(var, state->stage)) {
4254 /* User-defined ins/outs are not permitted in compute shaders. */
4255 if (state->stage == MESA_SHADER_COMPUTE) {
4256 _mesa_glsl_error(loc, state,
4257 "user-defined input and output variables are not "
4258 "permitted in compute shaders");
4259 }
4260
4261 /* This variable is being used to link data between shader stages (in
4262 * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
4263 * that is allowed for such purposes.
4264 *
4265 * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
4266 *
4267 * "The varying qualifier can be used only with the data types
4268 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
4269 * these."
4270 *
4271 * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
4272 * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
4273 *
4274 * "Fragment inputs can only be signed and unsigned integers and
4275 * integer vectors, float, floating-point vectors, matrices, or
4276 * arrays of these. Structures cannot be input.
4277 *
4278 * Similar text exists in the section on vertex shader outputs.
4279 *
4280 * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
4281 * 3.00 spec allows structs as well. Varying structs are also allowed
4282 * in GLSL 1.50.
4283 *
4284 * From section 4.3.4 of the ARB_bindless_texture spec:
4285 *
4286 * "(modify third paragraph of the section to allow sampler and image
4287 * types) ... Vertex shader inputs can only be float,
4288 * single-precision floating-point scalars, single-precision
4289 * floating-point vectors, matrices, signed and unsigned integers
4290 * and integer vectors, sampler and image types."
4291 *
4292 * From section 4.3.6 of the ARB_bindless_texture spec:
4293 *
4294 * "Output variables can only be floating-point scalars,
4295 * floating-point vectors, matrices, signed or unsigned integers or
4296 * integer vectors, sampler or image types, or arrays or structures
4297 * of any these."
4298 */
4299 switch (glsl_without_array(var->type)->base_type) {
4300 case GLSL_TYPE_FLOAT:
4301 /* Ok in all GLSL versions */
4302 break;
4303 case GLSL_TYPE_FLOAT16:
4304 if (state->AMD_gpu_shader_half_float_enable)
4305 break;
4306 _mesa_glsl_error(loc, state, "illegal type for a varying variable");
4307 break;
4308 case GLSL_TYPE_UINT:
4309 case GLSL_TYPE_INT:
4310 if (state->is_version(130, 300) || state->EXT_gpu_shader4_enable)
4311 break;
4312 _mesa_glsl_error(loc, state,
4313 "varying variables must be of base type float in %s",
4314 state->get_version_string());
4315 break;
4316 case GLSL_TYPE_STRUCT:
4317 if (state->is_version(150, 300))
4318 break;
4319 _mesa_glsl_error(loc, state,
4320 "varying variables may not be of type struct");
4321 break;
4322 case GLSL_TYPE_DOUBLE:
4323 case GLSL_TYPE_UINT64:
4324 case GLSL_TYPE_INT64:
4325 break;
4326 case GLSL_TYPE_SAMPLER:
4327 case GLSL_TYPE_TEXTURE:
4328 case GLSL_TYPE_IMAGE:
4329 if (state->has_bindless())
4330 break;
4331 FALLTHROUGH;
4332 default:
4333 _mesa_glsl_error(loc, state, "illegal type for a varying variable");
4334 break;
4335 }
4336 }
4337
4338 if (state->all_invariant && var->data.mode == ir_var_shader_out) {
4339 var->data.explicit_invariant = true;
4340 var->data.invariant = true;
4341 }
4342
4343 var->data.interpolation =
4344 interpret_interpolation_qualifier(qual, var->type,
4345 (ir_variable_mode) var->data.mode,
4346 state, loc);
4347
4348 /* Does the declaration use the deprecated 'attribute' or 'varying'
4349 * keywords?
4350 */
4351 const bool uses_deprecated_qualifier = qual->flags.q.attribute
4352 || qual->flags.q.varying;
4353
4354
4355 /* Validate auxiliary storage qualifiers */
4356
4357 /* From section 4.3.4 of the GLSL 1.30 spec:
4358 * "It is an error to use centroid in in a vertex shader."
4359 *
4360 * From section 4.3.4 of the GLSL ES 3.00 spec:
4361 * "It is an error to use centroid in or interpolation qualifiers in
4362 * a vertex shader input."
4363 */
4364
4365 /* Section 4.3.6 of the GLSL 1.30 specification states:
4366 * "It is an error to use centroid out in a fragment shader."
4367 *
4368 * The GL_ARB_shading_language_420pack extension specification states:
4369 * "It is an error to use auxiliary storage qualifiers or interpolation
4370 * qualifiers on an output in a fragment shader."
4371 */
4372 if (qual->flags.q.sample && (!is_varying_var(var, state->stage) || uses_deprecated_qualifier)) {
4373 _mesa_glsl_error(loc, state,
4374 "sample qualifier may only be used on `in` or `out` "
4375 "variables between shader stages");
4376 }
4377 if (qual->flags.q.centroid && !is_varying_var(var, state->stage)) {
4378 _mesa_glsl_error(loc, state,
4379 "centroid qualifier may only be used with `in', "
4380 "`out' or `varying' variables between shader stages");
4381 }
4382
4383 if (qual->flags.q.shared_storage && state->stage != MESA_SHADER_COMPUTE) {
4384 _mesa_glsl_error(loc, state,
4385 "the shared storage qualifiers can only be used with "
4386 "compute shaders");
4387 }
4388
4389 apply_image_qualifier_to_variable(qual, var, state, loc);
4390 }
4391
4392 /**
4393 * Get the variable that is being redeclared by this declaration or if it
4394 * does not exist, the current declared variable.
4395 *
4396 * Semantic checks to verify the validity of the redeclaration are also
4397 * performed. If semantic checks fail, compilation error will be emitted via
4398 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
4399 *
4400 * \returns
4401 * A pointer to an existing variable in the current scope if the declaration
4402 * is a redeclaration, current variable otherwise. \c is_declared boolean
4403 * will return \c true if the declaration is a redeclaration, \c false
4404 * otherwise.
4405 */
4406 static ir_variable *
get_variable_being_redeclared(ir_variable ** var_ptr,YYLTYPE loc,struct _mesa_glsl_parse_state * state,bool allow_all_redeclarations,bool * is_redeclaration)4407 get_variable_being_redeclared(ir_variable **var_ptr, YYLTYPE loc,
4408 struct _mesa_glsl_parse_state *state,
4409 bool allow_all_redeclarations,
4410 bool *is_redeclaration)
4411 {
4412 ir_variable *var = *var_ptr;
4413
4414 /* Check if this declaration is actually a re-declaration, either to
4415 * resize an array or add qualifiers to an existing variable.
4416 *
4417 * This is allowed for variables in the current scope, or when at
4418 * global scope (for built-ins in the implicit outer scope).
4419 */
4420 ir_variable *earlier = state->symbols->get_variable(var->name);
4421 if (earlier == NULL ||
4422 (state->current_function != NULL &&
4423 !state->symbols->name_declared_this_scope(var->name))) {
4424 *is_redeclaration = false;
4425 return var;
4426 }
4427
4428 *is_redeclaration = true;
4429
4430 if (earlier->data.how_declared == ir_var_declared_implicitly) {
4431 /* Verify that the redeclaration of a built-in does not change the
4432 * storage qualifier. There are a couple special cases.
4433 *
4434 * 1. Some built-in variables that are defined as 'in' in the
4435 * specification are implemented as system values. Allow
4436 * ir_var_system_value -> ir_var_shader_in.
4437 *
4438 * 2. gl_LastFragData is implemented as a ir_var_shader_out, but the
4439 * specification requires that redeclarations omit any qualifier.
4440 * Allow ir_var_shader_out -> ir_var_auto for this one variable.
4441 */
4442 if (earlier->data.mode != var->data.mode &&
4443 !(earlier->data.mode == ir_var_system_value &&
4444 var->data.mode == ir_var_shader_in) &&
4445 !(strcmp(var->name, "gl_LastFragData") == 0 &&
4446 var->data.mode == ir_var_auto)) {
4447 _mesa_glsl_error(&loc, state,
4448 "redeclaration cannot change qualification of `%s'",
4449 var->name);
4450 }
4451 }
4452
4453 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
4454 *
4455 * "It is legal to declare an array without a size and then
4456 * later re-declare the same name as an array of the same
4457 * type and specify a size."
4458 */
4459 if (glsl_type_is_unsized_array(earlier->type) && glsl_type_is_array(var->type)
4460 && (var->type->fields.array == earlier->type->fields.array)) {
4461 const int size = glsl_array_size(var->type);
4462 check_builtin_array_max_size(var->name, size, loc, state);
4463 if ((size > 0) && (size <= earlier->data.max_array_access)) {
4464 _mesa_glsl_error(& loc, state, "array size must be > %u due to "
4465 "previous access",
4466 earlier->data.max_array_access);
4467 }
4468
4469 earlier->type = var->type;
4470 delete var;
4471 var = NULL;
4472 *var_ptr = NULL;
4473 } else if (earlier->type != var->type) {
4474 _mesa_glsl_error(&loc, state,
4475 "redeclaration of `%s' has incorrect type",
4476 var->name);
4477 } else if ((state->ARB_fragment_coord_conventions_enable ||
4478 state->is_version(150, 0))
4479 && strcmp(var->name, "gl_FragCoord") == 0) {
4480 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
4481 * qualifiers.
4482 *
4483 * We don't really need to do anything here, just allow the
4484 * redeclaration. Any error on the gl_FragCoord is handled on the ast
4485 * level at apply_layout_qualifier_to_variable using the
4486 * ast_type_qualifier and _mesa_glsl_parse_state, or later at
4487 * linker.cpp.
4488 */
4489 /* According to section 4.3.7 of the GLSL 1.30 spec,
4490 * the following built-in varaibles can be redeclared with an
4491 * interpolation qualifier:
4492 * * gl_FrontColor
4493 * * gl_BackColor
4494 * * gl_FrontSecondaryColor
4495 * * gl_BackSecondaryColor
4496 * * gl_Color
4497 * * gl_SecondaryColor
4498 */
4499 } else if (state->is_version(130, 0)
4500 && (strcmp(var->name, "gl_FrontColor") == 0
4501 || strcmp(var->name, "gl_BackColor") == 0
4502 || strcmp(var->name, "gl_FrontSecondaryColor") == 0
4503 || strcmp(var->name, "gl_BackSecondaryColor") == 0
4504 || strcmp(var->name, "gl_Color") == 0
4505 || strcmp(var->name, "gl_SecondaryColor") == 0)) {
4506 earlier->data.interpolation = var->data.interpolation;
4507
4508 /* Layout qualifiers for gl_FragDepth. */
4509 } else if ((state->is_version(420, 0) ||
4510 state->AMD_conservative_depth_enable ||
4511 state->ARB_conservative_depth_enable)
4512 && strcmp(var->name, "gl_FragDepth") == 0) {
4513
4514 /** From the AMD_conservative_depth spec:
4515 * Within any shader, the first redeclarations of gl_FragDepth
4516 * must appear before any use of gl_FragDepth.
4517 */
4518 if (earlier->data.used) {
4519 _mesa_glsl_error(&loc, state,
4520 "the first redeclaration of gl_FragDepth "
4521 "must appear before any use of gl_FragDepth");
4522 }
4523
4524 /* Prevent inconsistent redeclaration of depth layout qualifier. */
4525 if (earlier->data.depth_layout != ir_depth_layout_none
4526 && earlier->data.depth_layout != var->data.depth_layout) {
4527 _mesa_glsl_error(&loc, state,
4528 "gl_FragDepth: depth layout is declared here "
4529 "as '%s, but it was previously declared as "
4530 "'%s'",
4531 depth_layout_string((ir_depth_layout)var->data.depth_layout),
4532 depth_layout_string((ir_depth_layout)earlier->data.depth_layout));
4533 }
4534
4535 earlier->data.depth_layout = var->data.depth_layout;
4536
4537 } else if (state->has_framebuffer_fetch() &&
4538 strcmp(var->name, "gl_LastFragData") == 0 &&
4539 var->data.mode == ir_var_auto) {
4540 /* According to the EXT_shader_framebuffer_fetch spec:
4541 *
4542 * "By default, gl_LastFragData is declared with the mediump precision
4543 * qualifier. This can be changed by redeclaring the corresponding
4544 * variables with the desired precision qualifier."
4545 *
4546 * "Fragment shaders may specify the following layout qualifier only for
4547 * redeclaring the built-in gl_LastFragData array [...]: noncoherent"
4548 */
4549 earlier->data.precision = var->data.precision;
4550 earlier->data.memory_coherent = var->data.memory_coherent;
4551
4552 } else if (state->NV_viewport_array2_enable &&
4553 strcmp(var->name, "gl_Layer") == 0 &&
4554 earlier->data.how_declared == ir_var_declared_implicitly) {
4555 /* No need to do anything, just allow it. Qualifier is stored in state */
4556
4557 } else if (state->is_version(0, 300) &&
4558 state->has_separate_shader_objects() &&
4559 (strcmp(var->name, "gl_Position") == 0 ||
4560 strcmp(var->name, "gl_PointSize") == 0)) {
4561
4562 /* EXT_separate_shader_objects spec says:
4563 *
4564 * "The following vertex shader outputs may be redeclared
4565 * at global scope to specify a built-in output interface,
4566 * with or without special qualifiers:
4567 *
4568 * gl_Position
4569 * gl_PointSize
4570 *
4571 * When compiling shaders using either of the above variables,
4572 * both such variables must be redeclared prior to use."
4573 */
4574 if (earlier->data.used) {
4575 _mesa_glsl_error(&loc, state, "the first redeclaration of "
4576 "%s must appear before any use", var->name);
4577 }
4578 } else if ((earlier->data.how_declared == ir_var_declared_implicitly &&
4579 state->allow_builtin_variable_redeclaration) ||
4580 allow_all_redeclarations) {
4581 /* Allow verbatim redeclarations of built-in variables. Not explicitly
4582 * valid, but some applications do it.
4583 */
4584 } else {
4585 _mesa_glsl_error(&loc, state, "`%s' redeclared", var->name);
4586 }
4587
4588 return earlier;
4589 }
4590
4591 /**
4592 * Generate the IR for an initializer in a variable declaration
4593 */
4594 static ir_rvalue *
process_initializer(ir_variable * var,ast_declaration * decl,ast_fully_specified_type * type,exec_list * initializer_instructions,struct _mesa_glsl_parse_state * state)4595 process_initializer(ir_variable *var, ast_declaration *decl,
4596 ast_fully_specified_type *type,
4597 exec_list *initializer_instructions,
4598 struct _mesa_glsl_parse_state *state)
4599 {
4600 void *mem_ctx = state;
4601 ir_rvalue *result = NULL;
4602
4603 YYLTYPE initializer_loc = decl->initializer->get_location();
4604
4605 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
4606 *
4607 * "All uniform variables are read-only and are initialized either
4608 * directly by an application via API commands, or indirectly by
4609 * OpenGL."
4610 */
4611 if (var->data.mode == ir_var_uniform) {
4612 state->check_version(120, 0, &initializer_loc,
4613 "cannot initialize uniform %s",
4614 var->name);
4615 }
4616
4617 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
4618 *
4619 * "Buffer variables cannot have initializers."
4620 */
4621 if (var->data.mode == ir_var_shader_storage) {
4622 _mesa_glsl_error(&initializer_loc, state,
4623 "cannot initialize buffer variable %s",
4624 var->name);
4625 }
4626
4627 /* From section 4.1.7 of the GLSL 4.40 spec:
4628 *
4629 * "Opaque variables [...] are initialized only through the
4630 * OpenGL API; they cannot be declared with an initializer in a
4631 * shader."
4632 *
4633 * From section 4.1.7 of the ARB_bindless_texture spec:
4634 *
4635 * "Samplers may be declared as shader inputs and outputs, as uniform
4636 * variables, as temporary variables, and as function parameters."
4637 *
4638 * From section 4.1.X of the ARB_bindless_texture spec:
4639 *
4640 * "Images may be declared as shader inputs and outputs, as uniform
4641 * variables, as temporary variables, and as function parameters."
4642 */
4643 if (glsl_contains_atomic(var->type) ||
4644 (!state->has_bindless() && glsl_contains_opaque(var->type))) {
4645 _mesa_glsl_error(&initializer_loc, state,
4646 "cannot initialize %s variable %s",
4647 var->name, state->has_bindless() ? "atomic" : "opaque");
4648 }
4649
4650 if ((var->data.mode == ir_var_shader_in) && (state->current_function == NULL)) {
4651 _mesa_glsl_error(&initializer_loc, state,
4652 "cannot initialize %s shader input / %s %s",
4653 _mesa_shader_stage_to_string(state->stage),
4654 (state->stage == MESA_SHADER_VERTEX)
4655 ? "attribute" : "varying",
4656 var->name);
4657 }
4658
4659 if (var->data.mode == ir_var_shader_out && state->current_function == NULL) {
4660 _mesa_glsl_error(&initializer_loc, state,
4661 "cannot initialize %s shader output %s",
4662 _mesa_shader_stage_to_string(state->stage),
4663 var->name);
4664 }
4665
4666 /* If the initializer is an ast_aggregate_initializer, recursively store
4667 * type information from the LHS into it, so that its hir() function can do
4668 * type checking.
4669 */
4670 if (decl->initializer->oper == ast_aggregate)
4671 _mesa_ast_set_aggregate_type(var->type, decl->initializer);
4672
4673 ir_dereference *const lhs = new(state) ir_dereference_variable(var);
4674 ir_rvalue *rhs = decl->initializer->hir(initializer_instructions, state);
4675
4676 /* Calculate the constant value if this is a const or uniform
4677 * declaration.
4678 *
4679 * Section 4.3 (Storage Qualifiers) of the GLSL ES 1.00.17 spec says:
4680 *
4681 * "Declarations of globals without a storage qualifier, or with
4682 * just the const qualifier, may include initializers, in which case
4683 * they will be initialized before the first line of main() is
4684 * executed. Such initializers must be a constant expression."
4685 *
4686 * The same section of the GLSL ES 3.00.4 spec has similar language.
4687 */
4688 if (type->qualifier.flags.q.constant
4689 || type->qualifier.flags.q.uniform
4690 || (state->es_shader && state->current_function == NULL)) {
4691 ir_rvalue *new_rhs = validate_assignment(state, initializer_loc,
4692 lhs, rhs, true);
4693 if (new_rhs != NULL) {
4694 rhs = new_rhs;
4695
4696 /* Section 4.3.3 (Constant Expressions) of the GLSL ES 3.00.4 spec
4697 * says:
4698 *
4699 * "A constant expression is one of
4700 *
4701 * ...
4702 *
4703 * - an expression formed by an operator on operands that are
4704 * all constant expressions, including getting an element of
4705 * a constant array, or a field of a constant structure, or
4706 * components of a constant vector. However, the sequence
4707 * operator ( , ) and the assignment operators ( =, +=, ...)
4708 * are not included in the operators that can create a
4709 * constant expression."
4710 *
4711 * Section 12.43 (Sequence operator and constant expressions) says:
4712 *
4713 * "Should the following construct be allowed?
4714 *
4715 * float a[2,3];
4716 *
4717 * The expression within the brackets uses the sequence operator
4718 * (',') and returns the integer 3 so the construct is declaring
4719 * a single-dimensional array of size 3. In some languages, the
4720 * construct declares a two-dimensional array. It would be
4721 * preferable to make this construct illegal to avoid confusion.
4722 *
4723 * One possibility is to change the definition of the sequence
4724 * operator so that it does not return a constant-expression and
4725 * hence cannot be used to declare an array size.
4726 *
4727 * RESOLUTION: The result of a sequence operator is not a
4728 * constant-expression."
4729 *
4730 * Section 4.3.3 (Constant Expressions) of the GLSL 4.30.9 spec
4731 * contains language almost identical to the section 4.3.3 in the
4732 * GLSL ES 3.00.4 spec. This is a new limitation for these GLSL
4733 * versions.
4734 */
4735 ir_constant *constant_value =
4736 rhs->constant_expression_value(mem_ctx);
4737
4738 if (!constant_value ||
4739 (state->is_version(430, 300) &&
4740 decl->initializer->has_sequence_subexpression())) {
4741 const char *const variable_mode =
4742 (type->qualifier.flags.q.constant)
4743 ? "const"
4744 : ((type->qualifier.flags.q.uniform) ? "uniform" : "global");
4745
4746 /* If ARB_shading_language_420pack is enabled, initializers of
4747 * const-qualified local variables do not have to be constant
4748 * expressions. Const-qualified global variables must still be
4749 * initialized with constant expressions.
4750 */
4751 if (!state->has_420pack()
4752 || state->current_function == NULL) {
4753 _mesa_glsl_error(& initializer_loc, state,
4754 "initializer of %s variable `%s' must be a "
4755 "constant expression",
4756 variable_mode,
4757 decl->identifier);
4758 if (glsl_type_is_numeric(var->type)) {
4759 /* Reduce cascading errors. */
4760 var->constant_value = type->qualifier.flags.q.constant
4761 ? ir_constant::zero(state, var->type) : NULL;
4762 }
4763 }
4764 } else {
4765 rhs = constant_value;
4766 var->constant_value = type->qualifier.flags.q.constant
4767 ? constant_value : NULL;
4768 }
4769 } else {
4770 if (glsl_type_is_numeric(var->type)) {
4771 /* Reduce cascading errors. */
4772 rhs = var->constant_value = type->qualifier.flags.q.constant
4773 ? ir_constant::zero(state, var->type) : NULL;
4774 }
4775 }
4776 }
4777
4778 if (rhs && !glsl_type_is_error(rhs->type)) {
4779 bool temp = var->data.read_only;
4780 if (type->qualifier.flags.q.constant)
4781 var->data.read_only = false;
4782
4783 /* Never emit code to initialize a uniform.
4784 */
4785 const glsl_type *initializer_type;
4786 bool error_emitted = false;
4787 if (!type->qualifier.flags.q.uniform) {
4788 error_emitted =
4789 do_assignment(initializer_instructions, state,
4790 NULL, lhs, rhs,
4791 &result, true, true,
4792 type->get_location());
4793 initializer_type = result->type;
4794 } else
4795 initializer_type = rhs->type;
4796
4797 if (!error_emitted) {
4798 var->constant_initializer = rhs->constant_expression_value(mem_ctx);
4799 var->data.has_initializer = true;
4800 var->data.is_implicit_initializer = false;
4801
4802 /* If the declared variable is an unsized array, it must inherrit
4803 * its full type from the initializer. A declaration such as
4804 *
4805 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
4806 *
4807 * becomes
4808 *
4809 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
4810 *
4811 * The assignment generated in the if-statement (below) will also
4812 * automatically handle this case for non-uniforms.
4813 *
4814 * If the declared variable is not an array, the types must
4815 * already match exactly. As a result, the type assignment
4816 * here can be done unconditionally. For non-uniforms the call
4817 * to do_assignment can change the type of the initializer (via
4818 * the implicit conversion rules). For uniforms the initializer
4819 * must be a constant expression, and the type of that expression
4820 * was validated above.
4821 */
4822 var->type = initializer_type;
4823 }
4824
4825 var->data.read_only = temp;
4826 }
4827
4828 return result;
4829 }
4830
4831 static void
validate_layout_qualifier_vertex_count(struct _mesa_glsl_parse_state * state,YYLTYPE loc,ir_variable * var,unsigned num_vertices,unsigned * size,const char * var_category)4832 validate_layout_qualifier_vertex_count(struct _mesa_glsl_parse_state *state,
4833 YYLTYPE loc, ir_variable *var,
4834 unsigned num_vertices,
4835 unsigned *size,
4836 const char *var_category)
4837 {
4838 if (glsl_type_is_unsized_array(var->type)) {
4839 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says:
4840 *
4841 * All geometry shader input unsized array declarations will be
4842 * sized by an earlier input layout qualifier, when present, as per
4843 * the following table.
4844 *
4845 * Followed by a table mapping each allowed input layout qualifier to
4846 * the corresponding input length.
4847 *
4848 * Similarly for tessellation control shader outputs.
4849 */
4850 if (num_vertices != 0)
4851 var->type = glsl_array_type(var->type->fields.array,
4852 num_vertices, 0);
4853 } else {
4854 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec
4855 * includes the following examples of compile-time errors:
4856 *
4857 * // code sequence within one shader...
4858 * in vec4 Color1[]; // size unknown
4859 * ...Color1.length()...// illegal, length() unknown
4860 * in vec4 Color2[2]; // size is 2
4861 * ...Color1.length()...// illegal, Color1 still has no size
4862 * in vec4 Color3[3]; // illegal, input sizes are inconsistent
4863 * layout(lines) in; // legal, input size is 2, matching
4864 * in vec4 Color4[3]; // illegal, contradicts layout
4865 * ...
4866 *
4867 * To detect the case illustrated by Color3, we verify that the size of
4868 * an explicitly-sized array matches the size of any previously declared
4869 * explicitly-sized array. To detect the case illustrated by Color4, we
4870 * verify that the size of an explicitly-sized array is consistent with
4871 * any previously declared input layout.
4872 */
4873 if (num_vertices != 0 && var->type->length != num_vertices) {
4874 _mesa_glsl_error(&loc, state,
4875 "%s size contradicts previously declared layout "
4876 "(size is %u, but layout requires a size of %u)",
4877 var_category, var->type->length, num_vertices);
4878 } else if (*size != 0 && var->type->length != *size) {
4879 _mesa_glsl_error(&loc, state,
4880 "%s sizes are inconsistent (size is %u, but a "
4881 "previous declaration has size %u)",
4882 var_category, var->type->length, *size);
4883 } else {
4884 *size = var->type->length;
4885 }
4886 }
4887 }
4888
4889 static void
handle_tess_ctrl_shader_output_decl(struct _mesa_glsl_parse_state * state,YYLTYPE loc,ir_variable * var)4890 handle_tess_ctrl_shader_output_decl(struct _mesa_glsl_parse_state *state,
4891 YYLTYPE loc, ir_variable *var)
4892 {
4893 unsigned num_vertices = 0;
4894
4895 if (state->tcs_output_vertices_specified) {
4896 if (!state->out_qualifier->vertices->
4897 process_qualifier_constant(state, "vertices",
4898 &num_vertices, false)) {
4899 return;
4900 }
4901
4902 if (num_vertices > state->Const.MaxPatchVertices) {
4903 _mesa_glsl_error(&loc, state, "vertices (%d) exceeds "
4904 "GL_MAX_PATCH_VERTICES", num_vertices);
4905 return;
4906 }
4907 }
4908
4909 if (!glsl_type_is_array(var->type) && !var->data.patch) {
4910 _mesa_glsl_error(&loc, state,
4911 "tessellation control shader outputs must be arrays");
4912
4913 /* To avoid cascading failures, short circuit the checks below. */
4914 return;
4915 }
4916
4917 if (var->data.patch)
4918 return;
4919
4920 validate_layout_qualifier_vertex_count(state, loc, var, num_vertices,
4921 &state->tcs_output_size,
4922 "tessellation control shader output");
4923 }
4924
4925 /**
4926 * Do additional processing necessary for tessellation control/evaluation shader
4927 * input declarations. This covers both interface block arrays and bare input
4928 * variables.
4929 */
4930 static void
handle_tess_shader_input_decl(struct _mesa_glsl_parse_state * state,YYLTYPE loc,ir_variable * var)4931 handle_tess_shader_input_decl(struct _mesa_glsl_parse_state *state,
4932 YYLTYPE loc, ir_variable *var)
4933 {
4934 if (!glsl_type_is_array(var->type) && !var->data.patch) {
4935 _mesa_glsl_error(&loc, state,
4936 "per-vertex tessellation shader inputs must be arrays");
4937 /* Avoid cascading failures. */
4938 return;
4939 }
4940
4941 if (var->data.patch)
4942 return;
4943
4944 /* The ARB_tessellation_shader spec says:
4945 *
4946 * "Declaring an array size is optional. If no size is specified, it
4947 * will be taken from the implementation-dependent maximum patch size
4948 * (gl_MaxPatchVertices). If a size is specified, it must match the
4949 * maximum patch size; otherwise, a compile or link error will occur."
4950 *
4951 * This text appears twice, once for TCS inputs, and again for TES inputs.
4952 */
4953 if (glsl_type_is_unsized_array(var->type)) {
4954 var->type = glsl_array_type(var->type->fields.array,
4955 state->Const.MaxPatchVertices, 0);
4956 } else if (var->type->length != state->Const.MaxPatchVertices) {
4957 _mesa_glsl_error(&loc, state,
4958 "per-vertex tessellation shader input arrays must be "
4959 "sized to gl_MaxPatchVertices (%d).",
4960 state->Const.MaxPatchVertices);
4961 }
4962 }
4963
4964
4965 /**
4966 * Do additional processing necessary for geometry shader input declarations
4967 * (this covers both interface blocks arrays and bare input variables).
4968 */
4969 static void
handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state * state,YYLTYPE loc,ir_variable * var)4970 handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state *state,
4971 YYLTYPE loc, ir_variable *var)
4972 {
4973 unsigned num_vertices = 0;
4974
4975 if (state->gs_input_prim_type_specified) {
4976 GLenum in_prim_type = state->in_qualifier->prim_type;
4977 num_vertices = mesa_vertices_per_prim(gl_to_mesa_prim(in_prim_type));
4978 }
4979
4980 /* Geometry shader input variables must be arrays. Caller should have
4981 * reported an error for this.
4982 */
4983 if (!glsl_type_is_array(var->type)) {
4984 assert(state->error);
4985
4986 /* To avoid cascading failures, short circuit the checks below. */
4987 return;
4988 }
4989
4990 validate_layout_qualifier_vertex_count(state, loc, var, num_vertices,
4991 &state->gs_input_size,
4992 "geometry shader input");
4993 }
4994
4995 static void
validate_identifier(const char * identifier,YYLTYPE loc,struct _mesa_glsl_parse_state * state)4996 validate_identifier(const char *identifier, YYLTYPE loc,
4997 struct _mesa_glsl_parse_state *state)
4998 {
4999 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
5000 *
5001 * "Identifiers starting with "gl_" are reserved for use by
5002 * OpenGL, and may not be declared in a shader as either a
5003 * variable or a function."
5004 */
5005 if (is_gl_identifier(identifier)) {
5006 _mesa_glsl_error(&loc, state,
5007 "identifier `%s' uses reserved `gl_' prefix",
5008 identifier);
5009 } else if (strstr(identifier, "__")) {
5010 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
5011 * spec:
5012 *
5013 * "In addition, all identifiers containing two
5014 * consecutive underscores (__) are reserved as
5015 * possible future keywords."
5016 *
5017 * The intention is that names containing __ are reserved for internal
5018 * use by the implementation, and names prefixed with GL_ are reserved
5019 * for use by Khronos. Names simply containing __ are dangerous to use,
5020 * but should be allowed.
5021 *
5022 * A future version of the GLSL specification will clarify this.
5023 */
5024 _mesa_glsl_warning(&loc, state,
5025 "identifier `%s' uses reserved `__' string",
5026 identifier);
5027 }
5028 }
5029
5030 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)5031 ast_declarator_list::hir(exec_list *instructions,
5032 struct _mesa_glsl_parse_state *state)
5033 {
5034 void *ctx = state;
5035 const struct glsl_type *decl_type;
5036 const char *type_name = NULL;
5037 ir_rvalue *result = NULL;
5038 YYLTYPE loc = this->get_location();
5039
5040 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
5041 *
5042 * "To ensure that a particular output variable is invariant, it is
5043 * necessary to use the invariant qualifier. It can either be used to
5044 * qualify a previously declared variable as being invariant
5045 *
5046 * invariant gl_Position; // make existing gl_Position be invariant"
5047 *
5048 * In these cases the parser will set the 'invariant' flag in the declarator
5049 * list, and the type will be NULL.
5050 */
5051 if (this->invariant) {
5052 assert(this->type == NULL);
5053
5054 if (state->current_function != NULL) {
5055 _mesa_glsl_error(& loc, state,
5056 "all uses of `invariant' keyword must be at global "
5057 "scope");
5058 }
5059
5060 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
5061 assert(decl->array_specifier == NULL);
5062 assert(decl->initializer == NULL);
5063
5064 ir_variable *const earlier =
5065 state->symbols->get_variable(decl->identifier);
5066 if (earlier == NULL) {
5067 _mesa_glsl_error(& loc, state,
5068 "undeclared variable `%s' cannot be marked "
5069 "invariant", decl->identifier);
5070 } else if (!is_allowed_invariant(earlier, state)) {
5071 _mesa_glsl_error(&loc, state,
5072 "`%s' cannot be marked invariant; interfaces between "
5073 "shader stages only.", decl->identifier);
5074 } else if (earlier->data.used) {
5075 _mesa_glsl_error(& loc, state,
5076 "variable `%s' may not be redeclared "
5077 "`invariant' after being used",
5078 earlier->name);
5079 } else {
5080 earlier->data.explicit_invariant = true;
5081 earlier->data.invariant = true;
5082 }
5083 }
5084
5085 /* Invariant redeclarations do not have r-values.
5086 */
5087 return NULL;
5088 }
5089
5090 if (this->precise) {
5091 assert(this->type == NULL);
5092
5093 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
5094 assert(decl->array_specifier == NULL);
5095 assert(decl->initializer == NULL);
5096
5097 ir_variable *const earlier =
5098 state->symbols->get_variable(decl->identifier);
5099 if (earlier == NULL) {
5100 _mesa_glsl_error(& loc, state,
5101 "undeclared variable `%s' cannot be marked "
5102 "precise", decl->identifier);
5103 } else if (state->current_function != NULL &&
5104 !state->symbols->name_declared_this_scope(decl->identifier)) {
5105 /* Note: we have to check if we're in a function, since
5106 * builtins are treated as having come from another scope.
5107 */
5108 _mesa_glsl_error(& loc, state,
5109 "variable `%s' from an outer scope may not be "
5110 "redeclared `precise' in this scope",
5111 earlier->name);
5112 } else if (earlier->data.used) {
5113 _mesa_glsl_error(& loc, state,
5114 "variable `%s' may not be redeclared "
5115 "`precise' after being used",
5116 earlier->name);
5117 } else {
5118 earlier->data.precise = true;
5119 }
5120 }
5121
5122 /* Precise redeclarations do not have r-values either. */
5123 return NULL;
5124 }
5125
5126 assert(this->type != NULL);
5127 assert(!this->invariant);
5128 assert(!this->precise);
5129
5130 /* GL_EXT_shader_image_load_store base type uses GLSL_TYPE_VOID as a special value to
5131 * indicate that it needs to be updated later (see glsl_parser.yy).
5132 * This is done here, based on the layout qualifier and the type of the image var
5133 */
5134 if (this->type->qualifier.flags.q.explicit_image_format &&
5135 glsl_type_is_image(this->type->specifier->type) &&
5136 this->type->qualifier.image_base_type == GLSL_TYPE_VOID) {
5137 /* "The ARB_shader_image_load_store says:
5138 * If both extensions are enabled in the shading language, the "size*" layout
5139 * qualifiers are treated as format qualifiers, and are mapped to equivalent
5140 * format qualifiers in the table below, according to the type of image
5141 * variable.
5142 * image* iimage* uimage*
5143 * -------- -------- --------
5144 * size1x8 n/a r8i r8ui
5145 * size1x16 r16f r16i r16ui
5146 * size1x32 r32f r32i r32ui
5147 * size2x32 rg32f rg32i rg32ui
5148 * size4x32 rgba32f rgba32i rgba32ui"
5149 */
5150 if (strncmp(this->type->specifier->type_name, "image", strlen("image")) == 0) {
5151 switch (this->type->qualifier.image_format) {
5152 case PIPE_FORMAT_R8_SINT:
5153 /* The GL_EXT_shader_image_load_store spec says:
5154 * A layout of "size1x8" is illegal for image variables associated
5155 * with floating-point data types.
5156 */
5157 _mesa_glsl_error(& loc, state,
5158 "size1x8 is illegal for image variables "
5159 "with floating-point data types.");
5160 return NULL;
5161 case PIPE_FORMAT_R16_SINT:
5162 this->type->qualifier.image_format = PIPE_FORMAT_R16_FLOAT;
5163 break;
5164 case PIPE_FORMAT_R32_SINT:
5165 this->type->qualifier.image_format = PIPE_FORMAT_R32_FLOAT;
5166 break;
5167 case PIPE_FORMAT_R32G32_SINT:
5168 this->type->qualifier.image_format = PIPE_FORMAT_R32G32_FLOAT;
5169 break;
5170 case PIPE_FORMAT_R32G32B32A32_SINT:
5171 this->type->qualifier.image_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
5172 break;
5173 default:
5174 unreachable("Unknown image format");
5175 }
5176 this->type->qualifier.image_base_type = GLSL_TYPE_FLOAT;
5177 } else if (strncmp(this->type->specifier->type_name, "uimage", strlen("uimage")) == 0) {
5178 switch (this->type->qualifier.image_format) {
5179 case PIPE_FORMAT_R8_SINT:
5180 this->type->qualifier.image_format = PIPE_FORMAT_R8_UINT;
5181 break;
5182 case PIPE_FORMAT_R16_SINT:
5183 this->type->qualifier.image_format = PIPE_FORMAT_R16_UINT;
5184 break;
5185 case PIPE_FORMAT_R32_SINT:
5186 this->type->qualifier.image_format = PIPE_FORMAT_R32_UINT;
5187 break;
5188 case PIPE_FORMAT_R32G32_SINT:
5189 this->type->qualifier.image_format = PIPE_FORMAT_R32G32_UINT;
5190 break;
5191 case PIPE_FORMAT_R32G32B32A32_SINT:
5192 this->type->qualifier.image_format = PIPE_FORMAT_R32G32B32A32_UINT;
5193 break;
5194 default:
5195 unreachable("Unknown image format");
5196 }
5197 this->type->qualifier.image_base_type = GLSL_TYPE_UINT;
5198 } else if (strncmp(this->type->specifier->type_name, "iimage", strlen("iimage")) == 0) {
5199 this->type->qualifier.image_base_type = GLSL_TYPE_INT;
5200 } else {
5201 assert(false);
5202 }
5203 }
5204
5205 /* The type specifier may contain a structure definition. Process that
5206 * before any of the variable declarations.
5207 */
5208 (void) this->type->specifier->hir(instructions, state);
5209
5210 decl_type = this->type->glsl_type(& type_name, state);
5211
5212 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
5213 * "Buffer variables may only be declared inside interface blocks
5214 * (section 4.3.9 “Interface Blocks”), which are then referred to as
5215 * shader storage blocks. It is a compile-time error to declare buffer
5216 * variables at global scope (outside a block)."
5217 */
5218 if (type->qualifier.flags.q.buffer && !glsl_type_is_interface(decl_type)) {
5219 _mesa_glsl_error(&loc, state,
5220 "buffer variables cannot be declared outside "
5221 "interface blocks");
5222 }
5223
5224 /* An offset-qualified atomic counter declaration sets the default
5225 * offset for the next declaration within the same atomic counter
5226 * buffer.
5227 */
5228 if (decl_type && glsl_contains_atomic(decl_type)) {
5229 if (type->qualifier.flags.q.explicit_binding &&
5230 type->qualifier.flags.q.explicit_offset) {
5231 unsigned qual_binding;
5232 unsigned qual_offset;
5233 if (process_qualifier_constant(state, &loc, "binding",
5234 type->qualifier.binding,
5235 &qual_binding)
5236 && process_qualifier_constant(state, &loc, "offset",
5237 type->qualifier.offset,
5238 &qual_offset)) {
5239 if (qual_binding < ARRAY_SIZE(state->atomic_counter_offsets))
5240 state->atomic_counter_offsets[qual_binding] = qual_offset;
5241 }
5242 }
5243
5244 ast_type_qualifier allowed_atomic_qual_mask;
5245 allowed_atomic_qual_mask.flags.i = 0;
5246 allowed_atomic_qual_mask.flags.q.explicit_binding = 1;
5247 allowed_atomic_qual_mask.flags.q.explicit_offset = 1;
5248 allowed_atomic_qual_mask.flags.q.uniform = 1;
5249
5250 type->qualifier.validate_flags(&loc, state, allowed_atomic_qual_mask,
5251 "invalid layout qualifier for",
5252 "atomic_uint");
5253 }
5254
5255 if (this->declarations.is_empty()) {
5256 /* If there is no structure involved in the program text, there are two
5257 * possible scenarios:
5258 *
5259 * - The program text contained something like 'vec4;'. This is an
5260 * empty declaration. It is valid but weird. Emit a warning.
5261 *
5262 * - The program text contained something like 'S;' and 'S' is not the
5263 * name of a known structure type. This is both invalid and weird.
5264 * Emit an error.
5265 *
5266 * - The program text contained something like 'mediump float;'
5267 * when the programmer probably meant 'precision mediump
5268 * float;' Emit a warning with a description of what they
5269 * probably meant to do.
5270 *
5271 * Note that if decl_type is NULL and there is a structure involved,
5272 * there must have been some sort of error with the structure. In this
5273 * case we assume that an error was already generated on this line of
5274 * code for the structure. There is no need to generate an additional,
5275 * confusing error.
5276 */
5277 assert(this->type->specifier->structure == NULL || decl_type != NULL
5278 || state->error);
5279
5280 if (decl_type == NULL) {
5281 _mesa_glsl_error(&loc, state,
5282 "invalid type `%s' in empty declaration",
5283 type_name);
5284 } else {
5285 if (glsl_type_is_array(decl_type)) {
5286 /* From Section 13.22 (Array Declarations) of the GLSL ES 3.2
5287 * spec:
5288 *
5289 * "... any declaration that leaves the size undefined is
5290 * disallowed as this would add complexity and there are no
5291 * use-cases."
5292 */
5293 if (state->es_shader && glsl_type_is_unsized_array(decl_type)) {
5294 _mesa_glsl_error(&loc, state, "array size must be explicitly "
5295 "or implicitly defined");
5296 }
5297
5298 /* From Section 4.12 (Empty Declarations) of the GLSL 4.5 spec:
5299 *
5300 * "The combinations of types and qualifiers that cause
5301 * compile-time or link-time errors are the same whether or not
5302 * the declaration is empty."
5303 */
5304 validate_array_dimensions(decl_type, state, &loc);
5305 }
5306
5307 if (glsl_type_is_atomic_uint(decl_type)) {
5308 /* Empty atomic counter declarations are allowed and useful
5309 * to set the default offset qualifier.
5310 */
5311 return NULL;
5312 } else if (this->type->qualifier.precision != ast_precision_none) {
5313 if (this->type->specifier->structure != NULL) {
5314 _mesa_glsl_error(&loc, state,
5315 "precision qualifiers can't be applied "
5316 "to structures");
5317 } else {
5318 static const char *const precision_names[] = {
5319 "highp",
5320 "highp",
5321 "mediump",
5322 "lowp"
5323 };
5324
5325 _mesa_glsl_warning(&loc, state,
5326 "empty declaration with precision "
5327 "qualifier, to set the default precision, "
5328 "use `precision %s %s;'",
5329 precision_names[this->type->
5330 qualifier.precision],
5331 type_name);
5332 }
5333 } else if (this->type->specifier->structure == NULL) {
5334 _mesa_glsl_warning(&loc, state, "empty declaration");
5335 }
5336 }
5337 }
5338
5339 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
5340 const struct glsl_type *var_type;
5341 ir_variable *var;
5342 const char *identifier = decl->identifier;
5343 /* FINISHME: Emit a warning if a variable declaration shadows a
5344 * FINISHME: declaration at a higher scope.
5345 */
5346
5347 if ((decl_type == NULL) || glsl_type_is_void(decl_type)) {
5348 if (type_name != NULL) {
5349 _mesa_glsl_error(& loc, state,
5350 "invalid type `%s' in declaration of `%s'",
5351 type_name, decl->identifier);
5352 } else {
5353 _mesa_glsl_error(& loc, state,
5354 "invalid type in declaration of `%s'",
5355 decl->identifier);
5356 }
5357 continue;
5358 }
5359
5360 if (this->type->qualifier.is_subroutine_decl()) {
5361 const glsl_type *t;
5362 const char *name;
5363
5364 t = state->symbols->get_type(this->type->specifier->type_name);
5365 if (!t)
5366 _mesa_glsl_error(& loc, state,
5367 "invalid type in declaration of `%s'",
5368 decl->identifier);
5369 name = ralloc_asprintf(ctx, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state->stage), decl->identifier);
5370
5371 identifier = name;
5372
5373 }
5374 var_type = process_array_type(&loc, decl_type, decl->array_specifier,
5375 state);
5376
5377 var = new(ctx) ir_variable(var_type, identifier, ir_var_auto);
5378
5379 /* The 'varying in' and 'varying out' qualifiers can only be used with
5380 * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
5381 * yet.
5382 */
5383 if (this->type->qualifier.flags.q.varying) {
5384 if (this->type->qualifier.flags.q.in) {
5385 _mesa_glsl_error(& loc, state,
5386 "`varying in' qualifier in declaration of "
5387 "`%s' only valid for geometry shaders using "
5388 "ARB_geometry_shader4 or EXT_geometry_shader4",
5389 decl->identifier);
5390 } else if (this->type->qualifier.flags.q.out) {
5391 _mesa_glsl_error(& loc, state,
5392 "`varying out' qualifier in declaration of "
5393 "`%s' only valid for geometry shaders using "
5394 "ARB_geometry_shader4 or EXT_geometry_shader4",
5395 decl->identifier);
5396 }
5397 }
5398
5399 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
5400 *
5401 * "Global variables can only use the qualifiers const,
5402 * attribute, uniform, or varying. Only one may be
5403 * specified.
5404 *
5405 * Local variables can only use the qualifier const."
5406 *
5407 * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
5408 * any extension that adds the 'layout' keyword.
5409 */
5410 if (!state->is_version(130, 300)
5411 && !state->has_explicit_attrib_location()
5412 && !state->has_separate_shader_objects()
5413 && !state->ARB_fragment_coord_conventions_enable) {
5414 /* GL_EXT_gpu_shader4 only allows "varying out" on fragment shader
5415 * outputs. (the varying flag is not set by the parser)
5416 */
5417 if (this->type->qualifier.flags.q.out &&
5418 (!state->EXT_gpu_shader4_enable ||
5419 state->stage != MESA_SHADER_FRAGMENT)) {
5420 _mesa_glsl_error(& loc, state,
5421 "`out' qualifier in declaration of `%s' "
5422 "only valid for function parameters in %s",
5423 decl->identifier, state->get_version_string());
5424 }
5425 if (this->type->qualifier.flags.q.in) {
5426 _mesa_glsl_error(& loc, state,
5427 "`in' qualifier in declaration of `%s' "
5428 "only valid for function parameters in %s",
5429 decl->identifier, state->get_version_string());
5430 }
5431 /* FINISHME: Test for other invalid qualifiers. */
5432 }
5433
5434 apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
5435 & loc, false);
5436 apply_layout_qualifier_to_variable(&this->type->qualifier, var, state,
5437 &loc);
5438
5439 if ((state->zero_init & (1u << var->data.mode)) &&
5440 (glsl_type_is_numeric(var->type) || glsl_type_is_boolean(var->type))) {
5441 const ir_constant_data data = { { 0 } };
5442 var->data.has_initializer = true;
5443 var->data.is_implicit_initializer = true;
5444 var->constant_initializer = new(var) ir_constant(var->type, &data);
5445 }
5446
5447 if (this->type->qualifier.flags.q.invariant) {
5448 if (!is_allowed_invariant(var, state)) {
5449 _mesa_glsl_error(&loc, state,
5450 "`%s' cannot be marked invariant; interfaces between "
5451 "shader stages only", var->name);
5452 }
5453 }
5454
5455 if (state->current_function != NULL) {
5456 const char *mode = NULL;
5457 const char *extra = "";
5458
5459 /* There is no need to check for 'inout' here because the parser will
5460 * only allow that in function parameter lists.
5461 */
5462 if (this->type->qualifier.flags.q.attribute) {
5463 mode = "attribute";
5464 } else if (this->type->qualifier.is_subroutine_decl()) {
5465 mode = "subroutine uniform";
5466 } else if (this->type->qualifier.flags.q.uniform) {
5467 mode = "uniform";
5468 } else if (this->type->qualifier.flags.q.varying) {
5469 mode = "varying";
5470 } else if (this->type->qualifier.flags.q.in) {
5471 mode = "in";
5472 extra = " or in function parameter list";
5473 } else if (this->type->qualifier.flags.q.out) {
5474 mode = "out";
5475 extra = " or in function parameter list";
5476 }
5477
5478 if (mode) {
5479 _mesa_glsl_error(& loc, state,
5480 "%s variable `%s' must be declared at "
5481 "global scope%s",
5482 mode, var->name, extra);
5483 }
5484 } else if (var->data.mode == ir_var_shader_in) {
5485 var->data.read_only = true;
5486
5487 if (state->stage == MESA_SHADER_VERTEX) {
5488 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
5489 *
5490 * "Vertex shader inputs can only be float, floating-point
5491 * vectors, matrices, signed and unsigned integers and integer
5492 * vectors. Vertex shader inputs can also form arrays of these
5493 * types, but not structures."
5494 *
5495 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
5496 *
5497 * "Vertex shader inputs can only be float, floating-point
5498 * vectors, matrices, signed and unsigned integers and integer
5499 * vectors. They cannot be arrays or structures."
5500 *
5501 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
5502 *
5503 * "The attribute qualifier can be used only with float,
5504 * floating-point vectors, and matrices. Attribute variables
5505 * cannot be declared as arrays or structures."
5506 *
5507 * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
5508 *
5509 * "Vertex shader inputs can only be float, floating-point
5510 * vectors, matrices, signed and unsigned integers and integer
5511 * vectors. Vertex shader inputs cannot be arrays or
5512 * structures."
5513 *
5514 * From section 4.3.4 of the ARB_bindless_texture spec:
5515 *
5516 * "(modify third paragraph of the section to allow sampler and
5517 * image types) ... Vertex shader inputs can only be float,
5518 * single-precision floating-point scalars, single-precision
5519 * floating-point vectors, matrices, signed and unsigned
5520 * integers and integer vectors, sampler and image types."
5521 */
5522 const glsl_type *check_type = glsl_without_array(var->type);
5523
5524 bool error = false;
5525 switch (check_type->base_type) {
5526 case GLSL_TYPE_FLOAT:
5527 break;
5528 case GLSL_TYPE_UINT64:
5529 case GLSL_TYPE_INT64:
5530 break;
5531 case GLSL_TYPE_UINT:
5532 case GLSL_TYPE_INT:
5533 error = !state->is_version(120, 300) && !state->EXT_gpu_shader4_enable;
5534 break;
5535 case GLSL_TYPE_DOUBLE:
5536 error = !state->is_version(410, 0) && !state->ARB_vertex_attrib_64bit_enable;
5537 break;
5538 case GLSL_TYPE_SAMPLER:
5539 case GLSL_TYPE_TEXTURE:
5540 case GLSL_TYPE_IMAGE:
5541 error = !state->has_bindless();
5542 break;
5543 default:
5544 error = true;
5545 }
5546
5547 if (error) {
5548 _mesa_glsl_error(& loc, state,
5549 "vertex shader input / attribute cannot have "
5550 "type %s`%s'",
5551 glsl_type_is_array(var->type) ? "array of " : "",
5552 glsl_get_type_name(check_type));
5553 } else if (glsl_type_is_array(var->type) &&
5554 !state->check_version(150, 0, &loc,
5555 "vertex shader input / attribute "
5556 "cannot have array type")) {
5557 }
5558 } else if (state->stage == MESA_SHADER_GEOMETRY) {
5559 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
5560 *
5561 * Geometry shader input variables get the per-vertex values
5562 * written out by vertex shader output variables of the same
5563 * names. Since a geometry shader operates on a set of
5564 * vertices, each input varying variable (or input block, see
5565 * interface blocks below) needs to be declared as an array.
5566 */
5567 if (!glsl_type_is_array(var->type)) {
5568 _mesa_glsl_error(&loc, state,
5569 "geometry shader inputs must be arrays");
5570 }
5571
5572 handle_geometry_shader_input_decl(state, loc, var);
5573 } else if (state->stage == MESA_SHADER_FRAGMENT) {
5574 /* From section 4.3.4 (Input Variables) of the GLSL ES 3.10 spec:
5575 *
5576 * It is a compile-time error to declare a fragment shader
5577 * input with, or that contains, any of the following types:
5578 *
5579 * * A boolean type
5580 * * An opaque type
5581 * * An array of arrays
5582 * * An array of structures
5583 * * A structure containing an array
5584 * * A structure containing a structure
5585 */
5586 if (state->es_shader) {
5587 const glsl_type *check_type = glsl_without_array(var->type);
5588 if (glsl_type_is_boolean(check_type) ||
5589 glsl_contains_opaque(check_type)) {
5590 _mesa_glsl_error(&loc, state,
5591 "fragment shader input cannot have type %s",
5592 glsl_get_type_name(check_type));
5593 }
5594 if (glsl_type_is_array(var->type) &&
5595 glsl_type_is_array(var->type->fields.array)) {
5596 _mesa_glsl_error(&loc, state,
5597 "%s shader output "
5598 "cannot have an array of arrays",
5599 _mesa_shader_stage_to_string(state->stage));
5600 }
5601 if (glsl_type_is_array(var->type) &&
5602 glsl_type_is_struct(var->type->fields.array)) {
5603 _mesa_glsl_error(&loc, state,
5604 "fragment shader input "
5605 "cannot have an array of structs");
5606 }
5607 if (glsl_type_is_struct(var->type)) {
5608 for (unsigned i = 0; i < var->type->length; i++) {
5609 if (glsl_type_is_array(var->type->fields.structure[i].type) ||
5610 glsl_type_is_struct(var->type->fields.structure[i].type))
5611 _mesa_glsl_error(&loc, state,
5612 "fragment shader input cannot have "
5613 "a struct that contains an "
5614 "array or struct");
5615 }
5616 }
5617 }
5618 } else if (state->stage == MESA_SHADER_TESS_CTRL ||
5619 state->stage == MESA_SHADER_TESS_EVAL) {
5620 handle_tess_shader_input_decl(state, loc, var);
5621 }
5622 } else if (var->data.mode == ir_var_shader_out) {
5623 const glsl_type *check_type = glsl_without_array(var->type);
5624
5625 /* From section 4.3.6 (Output variables) of the GLSL 4.40 spec:
5626 *
5627 * It is a compile-time error to declare a fragment shader output
5628 * that contains any of the following:
5629 *
5630 * * A Boolean type (bool, bvec2 ...)
5631 * * A double-precision scalar or vector (double, dvec2 ...)
5632 * * An opaque type
5633 * * Any matrix type
5634 * * A structure
5635 */
5636 if (state->stage == MESA_SHADER_FRAGMENT) {
5637 if (glsl_type_is_struct(check_type) || glsl_type_is_matrix(check_type))
5638 _mesa_glsl_error(&loc, state,
5639 "fragment shader output "
5640 "cannot have struct or matrix type");
5641 switch (check_type->base_type) {
5642 case GLSL_TYPE_UINT:
5643 case GLSL_TYPE_INT:
5644 case GLSL_TYPE_FLOAT:
5645 break;
5646 default:
5647 _mesa_glsl_error(&loc, state,
5648 "fragment shader output cannot have "
5649 "type %s", glsl_get_type_name(check_type));
5650 }
5651 }
5652
5653 /* From section 4.3.6 (Output Variables) of the GLSL ES 3.10 spec:
5654 *
5655 * It is a compile-time error to declare a vertex shader output
5656 * with, or that contains, any of the following types:
5657 *
5658 * * A boolean type
5659 * * An opaque type
5660 * * An array of arrays
5661 * * An array of structures
5662 * * A structure containing an array
5663 * * A structure containing a structure
5664 *
5665 * It is a compile-time error to declare a fragment shader output
5666 * with, or that contains, any of the following types:
5667 *
5668 * * A boolean type
5669 * * An opaque type
5670 * * A matrix
5671 * * A structure
5672 * * An array of array
5673 *
5674 * ES 3.20 updates this to apply to tessellation and geometry shaders
5675 * as well. Because there are per-vertex arrays in the new stages,
5676 * it strikes the "array of..." rules and replaces them with these:
5677 *
5678 * * For per-vertex-arrayed variables (applies to tessellation
5679 * control, tessellation evaluation and geometry shaders):
5680 *
5681 * * Per-vertex-arrayed arrays of arrays
5682 * * Per-vertex-arrayed arrays of structures
5683 *
5684 * * For non-per-vertex-arrayed variables:
5685 *
5686 * * An array of arrays
5687 * * An array of structures
5688 *
5689 * which basically says to unwrap the per-vertex aspect and apply
5690 * the old rules.
5691 */
5692 if (state->es_shader) {
5693 if (glsl_type_is_array(var->type) &&
5694 glsl_type_is_array(var->type->fields.array)) {
5695 _mesa_glsl_error(&loc, state,
5696 "%s shader output "
5697 "cannot have an array of arrays",
5698 _mesa_shader_stage_to_string(state->stage));
5699 }
5700 if (state->stage <= MESA_SHADER_GEOMETRY) {
5701 const glsl_type *type = var->type;
5702
5703 if (state->stage == MESA_SHADER_TESS_CTRL &&
5704 !var->data.patch && glsl_type_is_array(var->type)) {
5705 type = var->type->fields.array;
5706 }
5707
5708 if (glsl_type_is_array(type) && glsl_type_is_struct(type->fields.array)) {
5709 _mesa_glsl_error(&loc, state,
5710 "%s shader output cannot have "
5711 "an array of structs",
5712 _mesa_shader_stage_to_string(state->stage));
5713 }
5714 if (glsl_type_is_struct(type)) {
5715 for (unsigned i = 0; i < type->length; i++) {
5716 if (glsl_type_is_array(type->fields.structure[i].type) ||
5717 glsl_type_is_struct(type->fields.structure[i].type))
5718 _mesa_glsl_error(&loc, state,
5719 "%s shader output cannot have a "
5720 "struct that contains an "
5721 "array or struct",
5722 _mesa_shader_stage_to_string(state->stage));
5723 }
5724 }
5725 }
5726 }
5727
5728 if (state->stage == MESA_SHADER_TESS_CTRL) {
5729 handle_tess_ctrl_shader_output_decl(state, loc, var);
5730 }
5731 } else if (glsl_contains_subroutine(var->type)) {
5732 /* declare subroutine uniforms as hidden */
5733 var->data.how_declared = ir_var_hidden;
5734 }
5735
5736 /* From section 4.3.4 of the GLSL 4.00 spec:
5737 * "Input variables may not be declared using the patch in qualifier
5738 * in tessellation control or geometry shaders."
5739 *
5740 * From section 4.3.6 of the GLSL 4.00 spec:
5741 * "It is an error to use patch out in a vertex, tessellation
5742 * evaluation, or geometry shader."
5743 *
5744 * This doesn't explicitly forbid using them in a fragment shader, but
5745 * that's probably just an oversight.
5746 */
5747 if (state->stage != MESA_SHADER_TESS_EVAL
5748 && this->type->qualifier.flags.q.patch
5749 && this->type->qualifier.flags.q.in) {
5750
5751 _mesa_glsl_error(&loc, state, "'patch in' can only be used in a "
5752 "tessellation evaluation shader");
5753 }
5754
5755 if (state->stage != MESA_SHADER_TESS_CTRL
5756 && this->type->qualifier.flags.q.patch
5757 && this->type->qualifier.flags.q.out) {
5758
5759 _mesa_glsl_error(&loc, state, "'patch out' can only be used in a "
5760 "tessellation control shader");
5761 }
5762
5763 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
5764 */
5765 if (this->type->qualifier.precision != ast_precision_none) {
5766 state->check_precision_qualifiers_allowed(&loc);
5767 }
5768
5769 if (this->type->qualifier.precision != ast_precision_none &&
5770 !precision_qualifier_allowed(var->type)) {
5771 _mesa_glsl_error(&loc, state,
5772 "precision qualifiers apply only to floating point"
5773 ", integer and opaque types");
5774 }
5775
5776 /* From section 4.1.7 of the GLSL 4.40 spec:
5777 *
5778 * "[Opaque types] can only be declared as function
5779 * parameters or uniform-qualified variables."
5780 *
5781 * From section 4.1.7 of the ARB_bindless_texture spec:
5782 *
5783 * "Samplers may be declared as shader inputs and outputs, as uniform
5784 * variables, as temporary variables, and as function parameters."
5785 *
5786 * From section 4.1.X of the ARB_bindless_texture spec:
5787 *
5788 * "Images may be declared as shader inputs and outputs, as uniform
5789 * variables, as temporary variables, and as function parameters."
5790 */
5791 if (!this->type->qualifier.flags.q.uniform &&
5792 (glsl_contains_atomic(var_type) ||
5793 (!state->has_bindless() && glsl_contains_opaque(var_type)))) {
5794 _mesa_glsl_error(&loc, state,
5795 "%s variables must be declared uniform",
5796 state->has_bindless() ? "atomic" : "opaque");
5797 }
5798
5799 /* Process the initializer and add its instructions to a temporary
5800 * list. This list will be added to the instruction stream (below) after
5801 * the declaration is added. This is done because in some cases (such as
5802 * redeclarations) the declaration may not actually be added to the
5803 * instruction stream.
5804 */
5805 exec_list initializer_instructions;
5806
5807 /* Examine var name here since var may get deleted in the next call */
5808 bool var_is_gl_id = is_gl_identifier(var->name);
5809
5810 bool is_redeclaration;
5811 var = get_variable_being_redeclared(&var, decl->get_location(), state,
5812 false /* allow_all_redeclarations */,
5813 &is_redeclaration);
5814 if (is_redeclaration) {
5815 if (var_is_gl_id &&
5816 var->data.how_declared == ir_var_declared_in_block) {
5817 _mesa_glsl_error(&loc, state,
5818 "`%s' has already been redeclared using "
5819 "gl_PerVertex", var->name);
5820 }
5821 var->data.how_declared = ir_var_declared_normally;
5822 }
5823
5824 if (decl->initializer != NULL) {
5825 result = process_initializer(var,
5826 decl, this->type,
5827 &initializer_instructions, state);
5828 } else {
5829 validate_array_dimensions(var_type, state, &loc);
5830 }
5831
5832 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
5833 *
5834 * "It is an error to write to a const variable outside of
5835 * its declaration, so they must be initialized when
5836 * declared."
5837 */
5838 if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) {
5839 _mesa_glsl_error(& loc, state,
5840 "const declaration of `%s' must be initialized",
5841 decl->identifier);
5842 }
5843
5844 if (state->es_shader) {
5845 const glsl_type *const t = var->type;
5846
5847 /* Skip the unsized array check for TCS/TES/GS inputs & TCS outputs.
5848 *
5849 * The GL_OES_tessellation_shader spec says about inputs:
5850 *
5851 * "Declaring an array size is optional. If no size is specified,
5852 * it will be taken from the implementation-dependent maximum
5853 * patch size (gl_MaxPatchVertices)."
5854 *
5855 * and about TCS outputs:
5856 *
5857 * "If no size is specified, it will be taken from output patch
5858 * size declared in the shader."
5859 *
5860 * The GL_OES_geometry_shader spec says:
5861 *
5862 * "All geometry shader input unsized array declarations will be
5863 * sized by an earlier input primitive layout qualifier, when
5864 * present, as per the following table."
5865 */
5866 const bool implicitly_sized =
5867 (var->data.mode == ir_var_shader_in &&
5868 state->stage >= MESA_SHADER_TESS_CTRL &&
5869 state->stage <= MESA_SHADER_GEOMETRY) ||
5870 (var->data.mode == ir_var_shader_out &&
5871 state->stage == MESA_SHADER_TESS_CTRL);
5872
5873 if (glsl_type_is_unsized_array(t) && !implicitly_sized)
5874 /* Section 10.17 of the GLSL ES 1.00 specification states that
5875 * unsized array declarations have been removed from the language.
5876 * Arrays that are sized using an initializer are still explicitly
5877 * sized. However, GLSL ES 1.00 does not allow array
5878 * initializers. That is only allowed in GLSL ES 3.00.
5879 *
5880 * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
5881 *
5882 * "An array type can also be formed without specifying a size
5883 * if the definition includes an initializer:
5884 *
5885 * float x[] = float[2] (1.0, 2.0); // declares an array of size 2
5886 * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
5887 *
5888 * float a[5];
5889 * float b[] = a;"
5890 */
5891 _mesa_glsl_error(& loc, state,
5892 "unsized array declarations are not allowed in "
5893 "GLSL ES");
5894 }
5895
5896 /* Section 4.4.6.1 Atomic Counter Layout Qualifiers of the GLSL 4.60 spec:
5897 *
5898 * "It is a compile-time error to declare an unsized array of
5899 * atomic_uint"
5900 */
5901 if (glsl_type_is_unsized_array(var->type) &&
5902 glsl_without_array(var->type)->base_type == GLSL_TYPE_ATOMIC_UINT) {
5903 _mesa_glsl_error(& loc, state,
5904 "Unsized array of atomic_uint is not allowed");
5905 }
5906
5907 /* If the declaration is not a redeclaration, there are a few additional
5908 * semantic checks that must be applied. In addition, variable that was
5909 * created for the declaration should be added to the IR stream.
5910 */
5911 if (!is_redeclaration) {
5912 validate_identifier(decl->identifier, loc, state);
5913
5914 /* Add the variable to the symbol table. Note that the initializer's
5915 * IR was already processed earlier (though it hasn't been emitted
5916 * yet), without the variable in scope.
5917 *
5918 * This differs from most C-like languages, but it follows the GLSL
5919 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
5920 * spec:
5921 *
5922 * "Within a declaration, the scope of a name starts immediately
5923 * after the initializer if present or immediately after the name
5924 * being declared if not."
5925 */
5926 if (!state->symbols->add_variable(var)) {
5927 YYLTYPE loc = this->get_location();
5928 _mesa_glsl_error(&loc, state, "name `%s' already taken in the "
5929 "current scope", decl->identifier);
5930 continue;
5931 }
5932
5933 /* Push the variable declaration to the top. It means that all the
5934 * variable declarations will appear in a funny last-to-first order,
5935 * but otherwise we run into trouble if a function is prototyped, a
5936 * global var is decled, then the function is defined with usage of
5937 * the global var. See glslparsertest's CorrectModule.frag.
5938 */
5939 instructions->push_head(var);
5940 }
5941
5942 instructions->append_list(&initializer_instructions);
5943 }
5944
5945
5946 /* Generally, variable declarations do not have r-values. However,
5947 * one is used for the declaration in
5948 *
5949 * while (bool b = some_condition()) {
5950 * ...
5951 * }
5952 *
5953 * so we return the rvalue from the last seen declaration here.
5954 */
5955 return result;
5956 }
5957
5958
5959 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)5960 ast_parameter_declarator::hir(exec_list *instructions,
5961 struct _mesa_glsl_parse_state *state)
5962 {
5963 void *ctx = state;
5964 const struct glsl_type *type;
5965 const char *name = NULL;
5966 YYLTYPE loc = this->get_location();
5967
5968 type = this->type->glsl_type(& name, state);
5969
5970 if (type == NULL) {
5971 if (name != NULL) {
5972 _mesa_glsl_error(& loc, state,
5973 "invalid type `%s' in declaration of `%s'",
5974 name, this->identifier);
5975 } else {
5976 _mesa_glsl_error(& loc, state,
5977 "invalid type in declaration of `%s'",
5978 this->identifier);
5979 }
5980
5981 type = &glsl_type_builtin_error;
5982 }
5983
5984 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
5985 *
5986 * "Functions that accept no input arguments need not use void in the
5987 * argument list because prototypes (or definitions) are required and
5988 * therefore there is no ambiguity when an empty argument list "( )" is
5989 * declared. The idiom "(void)" as a parameter list is provided for
5990 * convenience."
5991 *
5992 * Placing this check here prevents a void parameter being set up
5993 * for a function, which avoids tripping up checks for main taking
5994 * parameters and lookups of an unnamed symbol.
5995 */
5996 if (glsl_type_is_void(type)) {
5997 if (this->identifier != NULL)
5998 _mesa_glsl_error(& loc, state,
5999 "named parameter cannot have type `void'");
6000
6001 is_void = true;
6002 return NULL;
6003 }
6004
6005 if (formal_parameter && (this->identifier == NULL)) {
6006 _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
6007 return NULL;
6008 }
6009
6010 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
6011 * call already handled the "vec4[..] foo" case.
6012 */
6013 type = process_array_type(&loc, type, this->array_specifier, state);
6014
6015 if (!glsl_type_is_error(type) && glsl_type_is_unsized_array(type)) {
6016 _mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
6017 "a declared size");
6018 type = &glsl_type_builtin_error;
6019 }
6020
6021 is_void = false;
6022 ir_variable *var = new(ctx)
6023 ir_variable(type, this->identifier, ir_var_function_in);
6024
6025 /* Apply any specified qualifiers to the parameter declaration. Note that
6026 * for function parameters the default mode is 'in'.
6027 */
6028 apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc,
6029 true);
6030
6031 if (((1u << var->data.mode) & state->zero_init) &&
6032 (glsl_type_is_numeric(var->type) || glsl_type_is_boolean(var->type))) {
6033 const ir_constant_data data = { { 0 } };
6034 var->data.has_initializer = true;
6035 var->data.is_implicit_initializer = true;
6036 var->constant_initializer = new(var) ir_constant(var->type, &data);
6037 }
6038
6039 /* From section 4.1.7 of the GLSL 4.40 spec:
6040 *
6041 * "Opaque variables cannot be treated as l-values; hence cannot
6042 * be used as out or inout function parameters, nor can they be
6043 * assigned into."
6044 *
6045 * From section 4.1.7 of the ARB_bindless_texture spec:
6046 *
6047 * "Samplers can be used as l-values, so can be assigned into and used
6048 * as "out" and "inout" function parameters."
6049 *
6050 * From section 4.1.X of the ARB_bindless_texture spec:
6051 *
6052 * "Images can be used as l-values, so can be assigned into and used as
6053 * "out" and "inout" function parameters."
6054 */
6055 if ((var->data.mode == ir_var_function_inout || var->data.mode == ir_var_function_out)
6056 && (glsl_contains_atomic(type) ||
6057 (!state->has_bindless() && glsl_contains_opaque(type)))) {
6058 _mesa_glsl_error(&loc, state, "out and inout parameters cannot "
6059 "contain %s variables",
6060 state->has_bindless() ? "atomic" : "opaque");
6061 type = &glsl_type_builtin_error;
6062 }
6063
6064 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
6065 *
6066 * "When calling a function, expressions that do not evaluate to
6067 * l-values cannot be passed to parameters declared as out or inout."
6068 *
6069 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
6070 *
6071 * "Other binary or unary expressions, non-dereferenced arrays,
6072 * function names, swizzles with repeated fields, and constants
6073 * cannot be l-values."
6074 *
6075 * So for GLSL 1.10, passing an array as an out or inout parameter is not
6076 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
6077 */
6078 if ((var->data.mode == ir_var_function_inout || var->data.mode == ir_var_function_out)
6079 && glsl_type_is_array(type)
6080 && !state->check_version(120, 100, &loc,
6081 "arrays cannot be out or inout parameters")) {
6082 type = &glsl_type_builtin_error;
6083 }
6084
6085 instructions->push_tail(var);
6086
6087 /* Parameter declarations do not have r-values.
6088 */
6089 return NULL;
6090 }
6091
6092
6093 void
parameters_to_hir(exec_list * ast_parameters,bool formal,exec_list * ir_parameters,_mesa_glsl_parse_state * state)6094 ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
6095 bool formal,
6096 exec_list *ir_parameters,
6097 _mesa_glsl_parse_state *state)
6098 {
6099 ast_parameter_declarator *void_param = NULL;
6100 unsigned count = 0;
6101
6102 foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
6103 param->formal_parameter = formal;
6104 param->hir(ir_parameters, state);
6105
6106 if (param->is_void)
6107 void_param = param;
6108
6109 count++;
6110 }
6111
6112 if ((void_param != NULL) && (count > 1)) {
6113 YYLTYPE loc = void_param->get_location();
6114
6115 _mesa_glsl_error(& loc, state,
6116 "`void' parameter must be only parameter");
6117 }
6118 }
6119
6120
6121 void
emit_function(_mesa_glsl_parse_state * state,ir_function * f)6122 emit_function(_mesa_glsl_parse_state *state, ir_function *f)
6123 {
6124 /* IR invariants disallow function declarations or definitions
6125 * nested within other function definitions. But there is no
6126 * requirement about the relative order of function declarations
6127 * and definitions with respect to one another. So simply insert
6128 * the new ir_function block at the end of the toplevel instruction
6129 * list.
6130 */
6131 state->toplevel_ir->push_tail(f);
6132 }
6133
6134
6135 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)6136 ast_function::hir(exec_list *instructions,
6137 struct _mesa_glsl_parse_state *state)
6138 {
6139 void *ctx = state;
6140 ir_function *f = NULL;
6141 ir_function_signature *sig = NULL;
6142 exec_list hir_parameters;
6143 YYLTYPE loc = this->get_location();
6144
6145 const char *const name = identifier;
6146
6147 /* New functions are always added to the top-level IR instruction stream,
6148 * so this instruction list pointer is ignored. See also emit_function
6149 * (called below).
6150 */
6151 (void) instructions;
6152
6153 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
6154 *
6155 * "Function declarations (prototypes) cannot occur inside of functions;
6156 * they must be at global scope, or for the built-in functions, outside
6157 * the global scope."
6158 *
6159 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
6160 *
6161 * "User defined functions may only be defined within the global scope."
6162 *
6163 * Note that this language does not appear in GLSL 1.10.
6164 */
6165 if ((state->current_function != NULL) &&
6166 state->is_version(120, 100)) {
6167 YYLTYPE loc = this->get_location();
6168 _mesa_glsl_error(&loc, state,
6169 "declaration of function `%s' not allowed within "
6170 "function body", name);
6171 }
6172
6173 validate_identifier(name, this->get_location(), state);
6174
6175 /* Convert the list of function parameters to HIR now so that they can be
6176 * used below to compare this function's signature with previously seen
6177 * signatures for functions with the same name.
6178 */
6179 ast_parameter_declarator::parameters_to_hir(& this->parameters,
6180 is_definition,
6181 & hir_parameters, state);
6182
6183 const char *return_type_name;
6184 const glsl_type *return_type =
6185 this->return_type->glsl_type(& return_type_name, state);
6186
6187 if (!return_type) {
6188 YYLTYPE loc = this->get_location();
6189 _mesa_glsl_error(&loc, state,
6190 "function `%s' has undeclared return type `%s'",
6191 name, return_type_name);
6192 return_type = &glsl_type_builtin_error;
6193 }
6194
6195 /* ARB_shader_subroutine states:
6196 * "Subroutine declarations cannot be prototyped. It is an error to prepend
6197 * subroutine(...) to a function declaration."
6198 */
6199 if (this->return_type->qualifier.subroutine_list && !is_definition) {
6200 YYLTYPE loc = this->get_location();
6201 _mesa_glsl_error(&loc, state,
6202 "function declaration `%s' cannot have subroutine prepended",
6203 name);
6204 }
6205
6206 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
6207 * "No qualifier is allowed on the return type of a function."
6208 */
6209 if (this->return_type->has_qualifiers(state)) {
6210 YYLTYPE loc = this->get_location();
6211 _mesa_glsl_error(& loc, state,
6212 "function `%s' return type has qualifiers", name);
6213 }
6214
6215 /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
6216 *
6217 * "Arrays are allowed as arguments and as the return type. In both
6218 * cases, the array must be explicitly sized."
6219 */
6220 if (glsl_type_is_unsized_array(return_type)) {
6221 YYLTYPE loc = this->get_location();
6222 _mesa_glsl_error(& loc, state,
6223 "function `%s' return type array must be explicitly "
6224 "sized", name);
6225 }
6226
6227 /* From Section 6.1 (Function Definitions) of the GLSL 1.00 spec:
6228 *
6229 * "Arrays are allowed as arguments, but not as the return type. [...]
6230 * The return type can also be a structure if the structure does not
6231 * contain an array."
6232 */
6233 if (state->language_version == 100 && glsl_contains_array(return_type)) {
6234 YYLTYPE loc = this->get_location();
6235 _mesa_glsl_error(& loc, state,
6236 "function `%s' return type contains an array", name);
6237 }
6238
6239 /* From section 4.1.7 of the GLSL 4.40 spec:
6240 *
6241 * "[Opaque types] can only be declared as function parameters
6242 * or uniform-qualified variables."
6243 *
6244 * The ARB_bindless_texture spec doesn't clearly state this, but as it says
6245 * "Replace Section 4.1.7 (Samplers), p. 25" and, "Replace Section 4.1.X,
6246 * (Images)", this should be allowed.
6247 */
6248 if (glsl_contains_atomic(return_type) ||
6249 (!state->has_bindless() && glsl_contains_opaque(return_type))) {
6250 YYLTYPE loc = this->get_location();
6251 _mesa_glsl_error(&loc, state,
6252 "function `%s' return type can't contain an %s type",
6253 name, state->has_bindless() ? "atomic" : "opaque");
6254 }
6255
6256 /**/
6257 if (glsl_type_is_subroutine(return_type)) {
6258 YYLTYPE loc = this->get_location();
6259 _mesa_glsl_error(&loc, state,
6260 "function `%s' return type can't be a subroutine type",
6261 name);
6262 }
6263
6264 /* Get the precision for the return type */
6265 unsigned return_precision;
6266
6267 if (state->es_shader) {
6268 YYLTYPE loc = this->get_location();
6269 return_precision =
6270 select_gles_precision(this->return_type->qualifier.precision,
6271 return_type,
6272 state,
6273 &loc);
6274 } else {
6275 return_precision = GLSL_PRECISION_NONE;
6276 }
6277
6278 /* Create an ir_function if one doesn't already exist. */
6279 f = state->symbols->get_function(name);
6280 if (f == NULL) {
6281 f = new(ctx) ir_function(name);
6282 if (!this->return_type->qualifier.is_subroutine_decl()) {
6283 if (!state->symbols->add_function(f)) {
6284 /* This function name shadows a non-function use of the same name. */
6285 YYLTYPE loc = this->get_location();
6286 _mesa_glsl_error(&loc, state, "function name `%s' conflicts with "
6287 "non-function", name);
6288 return NULL;
6289 }
6290 }
6291 emit_function(state, f);
6292 }
6293
6294 /* From GLSL ES 3.0 spec, chapter 6.1 "Function Definitions", page 71:
6295 *
6296 * "A shader cannot redefine or overload built-in functions."
6297 *
6298 * While in GLSL ES 1.0 specification, chapter 8 "Built-in Functions":
6299 *
6300 * "User code can overload the built-in functions but cannot redefine
6301 * them."
6302 */
6303 if (state->es_shader) {
6304 /* Local shader has no exact candidates; check the built-ins. */
6305 if (state->language_version >= 300 &&
6306 _mesa_glsl_has_builtin_function(state, name)) {
6307 YYLTYPE loc = this->get_location();
6308 _mesa_glsl_error(& loc, state,
6309 "A shader cannot redefine or overload built-in "
6310 "function `%s' in GLSL ES 3.00", name);
6311 return NULL;
6312 }
6313
6314 if (state->language_version == 100) {
6315 ir_function_signature *sig =
6316 _mesa_glsl_find_builtin_function(state, name, &hir_parameters);
6317 if (sig && sig->is_builtin()) {
6318 _mesa_glsl_error(& loc, state,
6319 "A shader cannot redefine built-in "
6320 "function `%s' in GLSL ES 1.00", name);
6321 }
6322 }
6323 }
6324
6325 /* Verify that this function's signature either doesn't match a previously
6326 * seen signature for a function with the same name, or, if a match is found,
6327 * that the previously seen signature does not have an associated definition.
6328 */
6329 if (state->es_shader || f->has_user_signature()) {
6330 sig = f->exact_matching_signature(state, &hir_parameters);
6331 if (sig != NULL) {
6332 const char *badvar = sig->qualifiers_match(&hir_parameters);
6333 if (badvar != NULL) {
6334 YYLTYPE loc = this->get_location();
6335
6336 _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
6337 "qualifiers don't match prototype", name, badvar);
6338 }
6339
6340 if (sig->return_type != return_type) {
6341 YYLTYPE loc = this->get_location();
6342
6343 _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
6344 "match prototype", name);
6345 }
6346
6347 if (sig->return_precision != return_precision) {
6348 YYLTYPE loc = this->get_location();
6349
6350 _mesa_glsl_error(&loc, state, "function `%s' return type precision "
6351 "doesn't match prototype", name);
6352 }
6353
6354 if (sig->is_defined) {
6355 if (is_definition) {
6356 YYLTYPE loc = this->get_location();
6357 _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
6358 } else {
6359 /* We just encountered a prototype that exactly matches a
6360 * function that's already been defined. This is redundant,
6361 * and we should ignore it.
6362 */
6363 return NULL;
6364 }
6365 } else if (state->language_version == 100 && !is_definition) {
6366 /* From the GLSL 1.00 spec, section 4.2.7:
6367 *
6368 * "A particular variable, structure or function declaration
6369 * may occur at most once within a scope with the exception
6370 * that a single function prototype plus the corresponding
6371 * function definition are allowed."
6372 */
6373 YYLTYPE loc = this->get_location();
6374 _mesa_glsl_error(&loc, state, "function `%s' redeclared", name);
6375 }
6376 }
6377 }
6378
6379 /* Verify the return type of main() */
6380 if (strcmp(name, "main") == 0) {
6381 if (! glsl_type_is_void(return_type)) {
6382 YYLTYPE loc = this->get_location();
6383
6384 _mesa_glsl_error(& loc, state, "main() must return void");
6385 }
6386
6387 if (!hir_parameters.is_empty()) {
6388 YYLTYPE loc = this->get_location();
6389
6390 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
6391 }
6392 }
6393
6394 /* Finish storing the information about this new function in its signature.
6395 */
6396 if (sig == NULL) {
6397 sig = new(ctx) ir_function_signature(return_type);
6398 sig->return_precision = return_precision;
6399 f->add_signature(sig);
6400 }
6401
6402 sig->replace_parameters(&hir_parameters);
6403 signature = sig;
6404
6405 if (this->return_type->qualifier.subroutine_list) {
6406 int idx;
6407
6408 if (this->return_type->qualifier.flags.q.explicit_index) {
6409 unsigned qual_index;
6410 if (process_qualifier_constant(state, &loc, "index",
6411 this->return_type->qualifier.index,
6412 &qual_index)) {
6413 if (!state->has_explicit_uniform_location()) {
6414 _mesa_glsl_error(&loc, state, "subroutine index requires "
6415 "GL_ARB_explicit_uniform_location or "
6416 "GLSL 4.30");
6417 } else if (qual_index >= MAX_SUBROUTINES) {
6418 _mesa_glsl_error(&loc, state,
6419 "invalid subroutine index (%d) index must "
6420 "be a number between 0 and "
6421 "GL_MAX_SUBROUTINES - 1 (%d)", qual_index,
6422 MAX_SUBROUTINES - 1);
6423 } else {
6424 f->subroutine_index = qual_index;
6425 }
6426 }
6427 }
6428
6429 f->num_subroutine_types = this->return_type->qualifier.subroutine_list->declarations.length();
6430 f->subroutine_types = ralloc_array(state, const struct glsl_type *,
6431 f->num_subroutine_types);
6432 idx = 0;
6433 foreach_list_typed(ast_declaration, decl, link, &this->return_type->qualifier.subroutine_list->declarations) {
6434 const struct glsl_type *type;
6435 /* the subroutine type must be already declared */
6436 type = state->symbols->get_type(decl->identifier);
6437 if (!type) {
6438 _mesa_glsl_error(& loc, state, "unknown type '%s' in subroutine function definition", decl->identifier);
6439 }
6440
6441 for (int i = 0; i < state->num_subroutine_types; i++) {
6442 ir_function *fn = state->subroutine_types[i];
6443 ir_function_signature *tsig = NULL;
6444
6445 if (strcmp(fn->name, decl->identifier))
6446 continue;
6447
6448 tsig = fn->matching_signature(state, &sig->parameters,
6449 false);
6450 if (!tsig) {
6451 _mesa_glsl_error(& loc, state, "subroutine type mismatch '%s' - signatures do not match\n", decl->identifier);
6452 } else {
6453 if (tsig->return_type != sig->return_type) {
6454 _mesa_glsl_error(& loc, state, "subroutine type mismatch '%s' - return types do not match\n", decl->identifier);
6455 }
6456 }
6457 }
6458 f->subroutine_types[idx++] = type;
6459 }
6460 state->subroutines = (ir_function **)reralloc(state, state->subroutines,
6461 ir_function *,
6462 state->num_subroutines + 1);
6463 state->subroutines[state->num_subroutines] = f;
6464 state->num_subroutines++;
6465
6466 }
6467
6468 if (this->return_type->qualifier.is_subroutine_decl()) {
6469 if (!state->symbols->add_type(this->identifier, glsl_subroutine_type(this->identifier))) {
6470 _mesa_glsl_error(& loc, state, "type '%s' previously defined", this->identifier);
6471 return NULL;
6472 }
6473 state->subroutine_types = (ir_function **)reralloc(state, state->subroutine_types,
6474 ir_function *,
6475 state->num_subroutine_types + 1);
6476 state->subroutine_types[state->num_subroutine_types] = f;
6477 state->num_subroutine_types++;
6478
6479 f->is_subroutine = true;
6480 }
6481
6482 /* Function declarations (prototypes) do not have r-values.
6483 */
6484 return NULL;
6485 }
6486
6487
6488 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)6489 ast_function_definition::hir(exec_list *instructions,
6490 struct _mesa_glsl_parse_state *state)
6491 {
6492 prototype->is_definition = true;
6493 prototype->hir(instructions, state);
6494
6495 ir_function_signature *signature = prototype->signature;
6496 if (signature == NULL)
6497 return NULL;
6498
6499 assert(state->current_function == NULL);
6500 state->current_function = signature;
6501 state->found_return = false;
6502 state->found_begin_interlock = false;
6503 state->found_end_interlock = false;
6504
6505 /* Duplicate parameters declared in the prototype as concrete variables.
6506 * Add these to the symbol table.
6507 */
6508 state->symbols->push_scope();
6509 foreach_in_list(ir_variable, var, &signature->parameters) {
6510 assert(var->as_variable() != NULL);
6511
6512 /* The only way a parameter would "exist" is if two parameters have
6513 * the same name.
6514 */
6515 if (state->symbols->name_declared_this_scope(var->name)) {
6516 YYLTYPE loc = this->get_location();
6517
6518 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
6519 } else {
6520 state->symbols->add_variable(var);
6521 }
6522 }
6523
6524 /* Convert the body of the function to HIR. */
6525 this->body->hir(&signature->body, state);
6526 signature->is_defined = true;
6527
6528 state->symbols->pop_scope();
6529
6530 assert(state->current_function == signature);
6531 state->current_function = NULL;
6532
6533 if (!glsl_type_is_void(signature->return_type) && !state->found_return) {
6534 YYLTYPE loc = this->get_location();
6535 _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
6536 "%s, but no return statement",
6537 signature->function_name(),
6538 glsl_get_type_name(signature->return_type));
6539 }
6540
6541 /* Function definitions do not have r-values.
6542 */
6543 return NULL;
6544 }
6545
6546
6547 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)6548 ast_jump_statement::hir(exec_list *instructions,
6549 struct _mesa_glsl_parse_state *state)
6550 {
6551 void *ctx = state;
6552
6553 switch (mode) {
6554 case ast_return: {
6555 ir_return *inst;
6556 assert(state->current_function);
6557
6558 if (opt_return_value) {
6559 ir_rvalue *ret = opt_return_value->hir(instructions, state);
6560
6561 /* The value of the return type can be NULL if the shader says
6562 * 'return foo();' and foo() is a function that returns void.
6563 *
6564 * NOTE: The GLSL spec doesn't say that this is an error. The type
6565 * of the return value is void. If the return type of the function is
6566 * also void, then this should compile without error. Seriously.
6567 */
6568 const glsl_type *const ret_type =
6569 (ret == NULL) ? &glsl_type_builtin_void : ret->type;
6570
6571 /* Implicit conversions are not allowed for return values prior to
6572 * ARB_shading_language_420pack.
6573 */
6574 if (state->current_function->return_type != ret_type) {
6575 YYLTYPE loc = this->get_location();
6576
6577 if (state->has_420pack()) {
6578 if (!apply_implicit_conversion(state->current_function->return_type,
6579 ret, state)
6580 || (ret->type != state->current_function->return_type)) {
6581 _mesa_glsl_error(& loc, state,
6582 "could not implicitly convert return value "
6583 "to %s, in function `%s'",
6584 glsl_get_type_name(state->current_function->return_type),
6585 state->current_function->function_name());
6586 }
6587 } else {
6588 _mesa_glsl_error(& loc, state,
6589 "`return' with wrong type %s, in function `%s' "
6590 "returning %s",
6591 glsl_get_type_name(ret_type),
6592 state->current_function->function_name(),
6593 glsl_get_type_name(state->current_function->return_type));
6594 }
6595 } else if (state->current_function->return_type->base_type ==
6596 GLSL_TYPE_VOID) {
6597 YYLTYPE loc = this->get_location();
6598
6599 /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
6600 * specs add a clarification:
6601 *
6602 * "A void function can only use return without a return argument, even if
6603 * the return argument has void type. Return statements only accept values:
6604 *
6605 * void func1() { }
6606 * void func2() { return func1(); } // illegal return statement"
6607 */
6608 _mesa_glsl_error(& loc, state,
6609 "void functions can only use `return' without a "
6610 "return argument");
6611 }
6612
6613 inst = new(ctx) ir_return(ret);
6614 } else {
6615 if (state->current_function->return_type->base_type !=
6616 GLSL_TYPE_VOID) {
6617 YYLTYPE loc = this->get_location();
6618
6619 _mesa_glsl_error(& loc, state,
6620 "`return' with no value, in function %s returning "
6621 "non-void",
6622 state->current_function->function_name());
6623 }
6624 inst = new(ctx) ir_return;
6625 }
6626
6627 state->found_return = true;
6628 instructions->push_tail(inst);
6629 break;
6630 }
6631
6632 case ast_discard:
6633 if (state->stage != MESA_SHADER_FRAGMENT) {
6634 YYLTYPE loc = this->get_location();
6635
6636 _mesa_glsl_error(& loc, state,
6637 "`discard' may only appear in a fragment shader");
6638 }
6639 instructions->push_tail(new(ctx) ir_discard);
6640 break;
6641
6642 case ast_break:
6643 case ast_continue:
6644 if (mode == ast_continue &&
6645 state->loop_nesting_ast == NULL) {
6646 YYLTYPE loc = this->get_location();
6647
6648 _mesa_glsl_error(& loc, state, "continue may only appear in a loop");
6649 } else if (mode == ast_break &&
6650 state->loop_nesting_ast == NULL &&
6651 state->switch_state.switch_nesting_ast == NULL) {
6652 YYLTYPE loc = this->get_location();
6653
6654 _mesa_glsl_error(& loc, state,
6655 "break may only appear in a loop or a switch");
6656 } else {
6657 /* For a loop, inline the for loop expression again, since we don't
6658 * know where near the end of the loop body the normal copy of it is
6659 * going to be placed. Same goes for the condition for a do-while
6660 * loop.
6661 */
6662 if (state->loop_nesting_ast != NULL &&
6663 mode == ast_continue && !state->switch_state.is_switch_innermost) {
6664 if (state->loop_nesting_ast->rest_expression) {
6665 clone_ir_list(ctx, instructions,
6666 &state->loop_nesting_ast->rest_instructions);
6667 }
6668 if (state->loop_nesting_ast->mode ==
6669 ast_iteration_statement::ast_do_while) {
6670 state->loop_nesting_ast->condition_to_hir(instructions, state);
6671 }
6672 }
6673
6674 if (state->switch_state.is_switch_innermost &&
6675 mode == ast_continue) {
6676 /* Set 'continue_inside' to true. */
6677 ir_rvalue *const true_val = new (ctx) ir_constant(true);
6678 ir_dereference_variable *deref_continue_inside_var =
6679 new(ctx) ir_dereference_variable(state->switch_state.continue_inside);
6680 instructions->push_tail(new(ctx) ir_assignment(deref_continue_inside_var,
6681 true_val));
6682
6683 /* Break out from the switch, continue for the loop will
6684 * be called right after switch. */
6685 ir_loop_jump *const jump =
6686 new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
6687 instructions->push_tail(jump);
6688
6689 } else if (state->switch_state.is_switch_innermost &&
6690 mode == ast_break) {
6691 /* Force break out of switch by inserting a break. */
6692 ir_loop_jump *const jump =
6693 new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
6694 instructions->push_tail(jump);
6695 } else {
6696 ir_loop_jump *const jump =
6697 new(ctx) ir_loop_jump((mode == ast_break)
6698 ? ir_loop_jump::jump_break
6699 : ir_loop_jump::jump_continue);
6700 instructions->push_tail(jump);
6701 }
6702 }
6703
6704 break;
6705 }
6706
6707 /* Jump instructions do not have r-values.
6708 */
6709 return NULL;
6710 }
6711
6712
6713 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)6714 ast_demote_statement::hir(exec_list *instructions,
6715 struct _mesa_glsl_parse_state *state)
6716 {
6717 void *ctx = state;
6718
6719 if (state->stage != MESA_SHADER_FRAGMENT) {
6720 YYLTYPE loc = this->get_location();
6721
6722 _mesa_glsl_error(& loc, state,
6723 "`demote' may only appear in a fragment shader");
6724 }
6725
6726 instructions->push_tail(new(ctx) ir_demote);
6727
6728 return NULL;
6729 }
6730
6731
6732 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)6733 ast_selection_statement::hir(exec_list *instructions,
6734 struct _mesa_glsl_parse_state *state)
6735 {
6736 void *ctx = state;
6737
6738 ir_rvalue *const condition = this->condition->hir(instructions, state);
6739
6740 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
6741 *
6742 * "Any expression whose type evaluates to a Boolean can be used as the
6743 * conditional expression bool-expression. Vector types are not accepted
6744 * as the expression to if."
6745 *
6746 * The checks are separated so that higher quality diagnostics can be
6747 * generated for cases where both rules are violated.
6748 */
6749 if (!glsl_type_is_boolean(condition->type) || !glsl_type_is_scalar(condition->type)) {
6750 YYLTYPE loc = this->condition->get_location();
6751
6752 _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
6753 "boolean");
6754 }
6755
6756 ir_if *const stmt = new(ctx) ir_if(condition);
6757
6758 if (then_statement != NULL) {
6759 state->symbols->push_scope();
6760 then_statement->hir(& stmt->then_instructions, state);
6761 state->symbols->pop_scope();
6762 }
6763
6764 if (else_statement != NULL) {
6765 state->symbols->push_scope();
6766 else_statement->hir(& stmt->else_instructions, state);
6767 state->symbols->pop_scope();
6768 }
6769
6770 instructions->push_tail(stmt);
6771
6772 /* if-statements do not have r-values.
6773 */
6774 return NULL;
6775 }
6776
6777
6778 struct case_label {
6779 /** Value of the case label. */
6780 unsigned value;
6781
6782 /** Does this label occur after the default? */
6783 bool after_default;
6784
6785 /**
6786 * AST for the case label.
6787 *
6788 * This is only used to generate error messages for duplicate labels.
6789 */
6790 ast_expression *ast;
6791 };
6792
6793 /* Used for detection of duplicate case values, compare
6794 * given contents directly.
6795 */
6796 static bool
compare_case_value(const void * a,const void * b)6797 compare_case_value(const void *a, const void *b)
6798 {
6799 return ((struct case_label *) a)->value == ((struct case_label *) b)->value;
6800 }
6801
6802
6803 /* Used for detection of duplicate case values, just
6804 * returns key contents as is.
6805 */
6806 static unsigned
key_contents(const void * key)6807 key_contents(const void *key)
6808 {
6809 return ((struct case_label *) key)->value;
6810 }
6811
6812 void
eval_test_expression(exec_list * instructions,struct _mesa_glsl_parse_state * state)6813 ast_switch_statement::eval_test_expression(exec_list *instructions,
6814 struct _mesa_glsl_parse_state *state)
6815 {
6816 if (test_val == NULL)
6817 test_val = this->test_expression->hir(instructions, state);
6818 }
6819
6820 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)6821 ast_switch_statement::hir(exec_list *instructions,
6822 struct _mesa_glsl_parse_state *state)
6823 {
6824 void *ctx = state;
6825
6826 this->eval_test_expression(instructions, state);
6827
6828 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
6829 *
6830 * "The type of init-expression in a switch statement must be a
6831 * scalar integer."
6832 */
6833 if (!glsl_type_is_scalar(test_val->type) ||
6834 !glsl_type_is_integer_32(test_val->type)) {
6835 YYLTYPE loc = this->test_expression->get_location();
6836
6837 _mesa_glsl_error(& loc,
6838 state,
6839 "switch-statement expression must be scalar "
6840 "integer");
6841 return NULL;
6842 }
6843
6844 /* Track the switch-statement nesting in a stack-like manner.
6845 */
6846 struct glsl_switch_state saved = state->switch_state;
6847
6848 state->switch_state.is_switch_innermost = true;
6849 state->switch_state.switch_nesting_ast = this;
6850 state->switch_state.labels_ht =
6851 _mesa_hash_table_create(NULL, key_contents,
6852 compare_case_value);
6853 state->switch_state.previous_default = NULL;
6854
6855 /* Initalize is_fallthru state to false.
6856 */
6857 ir_rvalue *const is_fallthru_val = new (ctx) ir_constant(false);
6858 state->switch_state.is_fallthru_var =
6859 new(ctx) ir_variable(&glsl_type_builtin_bool,
6860 "switch_is_fallthru_tmp",
6861 ir_var_temporary);
6862 instructions->push_tail(state->switch_state.is_fallthru_var);
6863
6864 ir_dereference_variable *deref_is_fallthru_var =
6865 new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var);
6866 instructions->push_tail(new(ctx) ir_assignment(deref_is_fallthru_var,
6867 is_fallthru_val));
6868
6869 /* Initialize continue_inside state to false.
6870 */
6871 state->switch_state.continue_inside =
6872 new(ctx) ir_variable(&glsl_type_builtin_bool,
6873 "continue_inside_tmp",
6874 ir_var_temporary);
6875 instructions->push_tail(state->switch_state.continue_inside);
6876
6877 ir_rvalue *const false_val = new (ctx) ir_constant(false);
6878 ir_dereference_variable *deref_continue_inside_var =
6879 new(ctx) ir_dereference_variable(state->switch_state.continue_inside);
6880 instructions->push_tail(new(ctx) ir_assignment(deref_continue_inside_var,
6881 false_val));
6882
6883 state->switch_state.run_default =
6884 new(ctx) ir_variable(&glsl_type_builtin_bool,
6885 "run_default_tmp",
6886 ir_var_temporary);
6887 instructions->push_tail(state->switch_state.run_default);
6888
6889 /* Loop around the switch is used for flow control. */
6890 ir_loop * loop = new(ctx) ir_loop();
6891 instructions->push_tail(loop);
6892
6893 /* Cache test expression.
6894 */
6895 test_to_hir(&loop->body_instructions, state);
6896
6897 /* Emit code for body of switch stmt.
6898 */
6899 body->hir(&loop->body_instructions, state);
6900
6901 /* Insert a break at the end to exit loop. */
6902 ir_loop_jump *jump = new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
6903 loop->body_instructions.push_tail(jump);
6904
6905 /* If we are inside loop, check if continue got called inside switch. */
6906 if (state->loop_nesting_ast != NULL) {
6907 ir_dereference_variable *deref_continue_inside =
6908 new(ctx) ir_dereference_variable(state->switch_state.continue_inside);
6909 ir_if *irif = new(ctx) ir_if(deref_continue_inside);
6910 ir_loop_jump *jump = new(ctx) ir_loop_jump(ir_loop_jump::jump_continue);
6911
6912 if (state->loop_nesting_ast != NULL) {
6913 if (state->loop_nesting_ast->rest_expression) {
6914 clone_ir_list(ctx, &irif->then_instructions,
6915 &state->loop_nesting_ast->rest_instructions);
6916 }
6917 if (state->loop_nesting_ast->mode ==
6918 ast_iteration_statement::ast_do_while) {
6919 state->loop_nesting_ast->condition_to_hir(&irif->then_instructions, state);
6920 }
6921 }
6922 irif->then_instructions.push_tail(jump);
6923 instructions->push_tail(irif);
6924 }
6925
6926 _mesa_hash_table_destroy(state->switch_state.labels_ht, NULL);
6927
6928 state->switch_state = saved;
6929
6930 /* Switch statements do not have r-values. */
6931 return NULL;
6932 }
6933
6934
6935 void
test_to_hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)6936 ast_switch_statement::test_to_hir(exec_list *instructions,
6937 struct _mesa_glsl_parse_state *state)
6938 {
6939 void *ctx = state;
6940
6941 /* set to true to avoid a duplicate "use of uninitialized variable" warning
6942 * on the switch test case. The first one would be already raised when
6943 * getting the test_expression at ast_switch_statement::hir
6944 */
6945 test_expression->set_is_lhs(true);
6946 /* Cache value of test expression. */
6947 this->eval_test_expression(instructions, state);
6948
6949 state->switch_state.test_var = new(ctx) ir_variable(test_val->type,
6950 "switch_test_tmp",
6951 ir_var_temporary);
6952 ir_dereference_variable *deref_test_var =
6953 new(ctx) ir_dereference_variable(state->switch_state.test_var);
6954
6955 instructions->push_tail(state->switch_state.test_var);
6956 instructions->push_tail(new(ctx) ir_assignment(deref_test_var, test_val));
6957 }
6958
6959
6960 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)6961 ast_switch_body::hir(exec_list *instructions,
6962 struct _mesa_glsl_parse_state *state)
6963 {
6964 if (stmts != NULL) {
6965 state->symbols->push_scope();
6966 stmts->hir(instructions, state);
6967 state->symbols->pop_scope();
6968 }
6969
6970 /* Switch bodies do not have r-values. */
6971 return NULL;
6972 }
6973
6974 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)6975 ast_case_statement_list::hir(exec_list *instructions,
6976 struct _mesa_glsl_parse_state *state)
6977 {
6978 exec_list default_case, after_default, tmp;
6979
6980 foreach_list_typed (ast_case_statement, case_stmt, link, & this->cases) {
6981 case_stmt->hir(&tmp, state);
6982
6983 /* Default case. */
6984 if (state->switch_state.previous_default && default_case.is_empty()) {
6985 default_case.append_list(&tmp);
6986 continue;
6987 }
6988
6989 /* If default case found, append 'after_default' list. */
6990 if (!default_case.is_empty())
6991 after_default.append_list(&tmp);
6992 else
6993 instructions->append_list(&tmp);
6994 }
6995
6996 /* Handle the default case. This is done here because default might not be
6997 * the last case. We need to add checks against following cases first to see
6998 * if default should be chosen or not.
6999 */
7000 if (!default_case.is_empty()) {
7001 ir_factory body(instructions, state);
7002
7003 ir_expression *cmp = NULL;
7004
7005 hash_table_foreach(state->switch_state.labels_ht, entry) {
7006 const struct case_label *const l = (struct case_label *) entry->data;
7007
7008 /* If the switch init-value is the value of one of the labels that
7009 * occurs after the default case, disable execution of the default
7010 * case.
7011 */
7012 if (l->after_default) {
7013 ir_constant *const cnst =
7014 state->switch_state.test_var->type->base_type == GLSL_TYPE_UINT
7015 ? body.constant(unsigned(l->value))
7016 : body.constant(int(l->value));
7017
7018 cmp = cmp == NULL
7019 ? equal(cnst, state->switch_state.test_var)
7020 : logic_or(cmp, equal(cnst, state->switch_state.test_var));
7021 }
7022 }
7023
7024 if (cmp != NULL)
7025 body.emit(assign(state->switch_state.run_default, logic_not(cmp)));
7026 else
7027 body.emit(assign(state->switch_state.run_default, body.constant(true)));
7028
7029 /* Append default case and all cases after it. */
7030 instructions->append_list(&default_case);
7031 instructions->append_list(&after_default);
7032 }
7033
7034 /* Case statements do not have r-values. */
7035 return NULL;
7036 }
7037
7038 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)7039 ast_case_statement::hir(exec_list *instructions,
7040 struct _mesa_glsl_parse_state *state)
7041 {
7042 labels->hir(instructions, state);
7043
7044 /* Guard case statements depending on fallthru state. */
7045 ir_dereference_variable *const deref_fallthru_guard =
7046 new(state) ir_dereference_variable(state->switch_state.is_fallthru_var);
7047 ir_if *const test_fallthru = new(state) ir_if(deref_fallthru_guard);
7048
7049 foreach_list_typed (ast_node, stmt, link, & this->stmts)
7050 stmt->hir(& test_fallthru->then_instructions, state);
7051
7052 instructions->push_tail(test_fallthru);
7053
7054 /* Case statements do not have r-values. */
7055 return NULL;
7056 }
7057
7058
7059 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)7060 ast_case_label_list::hir(exec_list *instructions,
7061 struct _mesa_glsl_parse_state *state)
7062 {
7063 foreach_list_typed (ast_case_label, label, link, & this->labels)
7064 label->hir(instructions, state);
7065
7066 /* Case labels do not have r-values. */
7067 return NULL;
7068 }
7069
7070 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)7071 ast_case_label::hir(exec_list *instructions,
7072 struct _mesa_glsl_parse_state *state)
7073 {
7074 ir_factory body(instructions, state);
7075
7076 ir_variable *const fallthru_var = state->switch_state.is_fallthru_var;
7077
7078 /* If not default case, ... */
7079 if (this->test_value != NULL) {
7080 /* Conditionally set fallthru state based on
7081 * comparison of cached test expression value to case label.
7082 */
7083 ir_rvalue *const label_rval = this->test_value->hir(instructions, state);
7084 ir_constant *label_const =
7085 label_rval->constant_expression_value(body.mem_ctx);
7086
7087 if (!label_const) {
7088 YYLTYPE loc = this->test_value->get_location();
7089
7090 _mesa_glsl_error(& loc, state,
7091 "switch statement case label must be a "
7092 "constant expression");
7093
7094 /* Stuff a dummy value in to allow processing to continue. */
7095 label_const = body.constant(0);
7096 } else {
7097 hash_entry *entry =
7098 _mesa_hash_table_search(state->switch_state.labels_ht,
7099 &label_const->value.u[0]);
7100
7101 if (entry) {
7102 const struct case_label *const l =
7103 (struct case_label *) entry->data;
7104 const ast_expression *const previous_label = l->ast;
7105 YYLTYPE loc = this->test_value->get_location();
7106
7107 _mesa_glsl_error(& loc, state, "duplicate case value");
7108
7109 loc = previous_label->get_location();
7110 _mesa_glsl_error(& loc, state, "this is the previous case label");
7111 } else {
7112 struct case_label *l = ralloc(state->switch_state.labels_ht,
7113 struct case_label);
7114
7115 l->value = label_const->value.u[0];
7116 l->after_default = state->switch_state.previous_default != NULL;
7117 l->ast = this->test_value;
7118
7119 _mesa_hash_table_insert(state->switch_state.labels_ht,
7120 &label_const->value.u[0],
7121 l);
7122 }
7123 }
7124
7125 /* Create an r-value version of the ir_constant label here (after we may
7126 * have created a fake one in error cases) that can be passed to
7127 * apply_implicit_conversion below.
7128 */
7129 ir_rvalue *label = label_const;
7130
7131 ir_rvalue *deref_test_var =
7132 new(body.mem_ctx) ir_dereference_variable(state->switch_state.test_var);
7133
7134 /*
7135 * From GLSL 4.40 specification section 6.2 ("Selection"):
7136 *
7137 * "The type of the init-expression value in a switch statement must
7138 * be a scalar int or uint. The type of the constant-expression value
7139 * in a case label also must be a scalar int or uint. When any pair
7140 * of these values is tested for "equal value" and the types do not
7141 * match, an implicit conversion will be done to convert the int to a
7142 * uint (see section 4.1.10 “Implicit Conversions”) before the compare
7143 * is done."
7144 */
7145 if (label->type != state->switch_state.test_var->type) {
7146 YYLTYPE loc = this->test_value->get_location();
7147
7148 const glsl_type *type_a = label->type;
7149 const glsl_type *type_b = state->switch_state.test_var->type;
7150
7151 /* Check if int->uint implicit conversion is supported. */
7152 bool integer_conversion_supported =
7153 _mesa_glsl_can_implicitly_convert(&glsl_type_builtin_int, &glsl_type_builtin_uint, state);
7154
7155 if ((!glsl_type_is_integer_32(type_a) || !glsl_type_is_integer_32(type_b)) ||
7156 !integer_conversion_supported) {
7157 _mesa_glsl_error(&loc, state, "type mismatch with switch "
7158 "init-expression and case label (%s != %s)",
7159 glsl_get_type_name(type_a), glsl_get_type_name(type_b));
7160 } else {
7161 /* Conversion of the case label. */
7162 if (type_a->base_type == GLSL_TYPE_INT) {
7163 if (!apply_implicit_conversion(&glsl_type_builtin_uint,
7164 label, state))
7165 _mesa_glsl_error(&loc, state, "implicit type conversion error");
7166 } else {
7167 /* Conversion of the init-expression value. */
7168 if (!apply_implicit_conversion(&glsl_type_builtin_uint,
7169 deref_test_var, state))
7170 _mesa_glsl_error(&loc, state, "implicit type conversion error");
7171 }
7172 }
7173
7174 /* If the implicit conversion was allowed, the types will already be
7175 * the same. If the implicit conversion wasn't allowed, smash the
7176 * type of the label anyway. This will prevent the expression
7177 * constructor (below) from failing an assertion.
7178 */
7179 label->type = deref_test_var->type;
7180 }
7181
7182 body.emit(assign(fallthru_var,
7183 logic_or(fallthru_var, equal(label, deref_test_var))));
7184 } else { /* default case */
7185 if (state->switch_state.previous_default) {
7186 YYLTYPE loc = this->get_location();
7187 _mesa_glsl_error(& loc, state,
7188 "multiple default labels in one switch");
7189
7190 loc = state->switch_state.previous_default->get_location();
7191 _mesa_glsl_error(& loc, state, "this is the first default label");
7192 }
7193 state->switch_state.previous_default = this;
7194
7195 /* Set fallthru condition on 'run_default' bool. */
7196 body.emit(assign(fallthru_var,
7197 logic_or(fallthru_var,
7198 state->switch_state.run_default)));
7199 }
7200
7201 /* Case statements do not have r-values. */
7202 return NULL;
7203 }
7204
7205 void
condition_to_hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)7206 ast_iteration_statement::condition_to_hir(exec_list *instructions,
7207 struct _mesa_glsl_parse_state *state)
7208 {
7209 void *ctx = state;
7210
7211 if (condition != NULL) {
7212 ir_rvalue *const cond =
7213 condition->hir(instructions, state);
7214
7215 if ((cond == NULL)
7216 || !glsl_type_is_boolean(cond->type) || !glsl_type_is_scalar(cond->type)) {
7217 YYLTYPE loc = condition->get_location();
7218
7219 _mesa_glsl_error(& loc, state,
7220 "loop condition must be scalar boolean");
7221 } else {
7222 /* As the first code in the loop body, generate a block that looks
7223 * like 'if (!condition) break;' as the loop termination condition.
7224 */
7225 ir_rvalue *const not_cond =
7226 new(ctx) ir_expression(ir_unop_logic_not, cond);
7227
7228 ir_if *const if_stmt = new(ctx) ir_if(not_cond);
7229
7230 ir_jump *const break_stmt =
7231 new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
7232
7233 if_stmt->then_instructions.push_tail(break_stmt);
7234 instructions->push_tail(if_stmt);
7235 }
7236 }
7237 }
7238
7239
7240 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)7241 ast_iteration_statement::hir(exec_list *instructions,
7242 struct _mesa_glsl_parse_state *state)
7243 {
7244 void *ctx = state;
7245
7246 /* For-loops and while-loops start a new scope, but do-while loops do not.
7247 */
7248 if (mode != ast_do_while)
7249 state->symbols->push_scope();
7250
7251 if (init_statement != NULL)
7252 init_statement->hir(instructions, state);
7253
7254 ir_loop *const stmt = new(ctx) ir_loop();
7255 instructions->push_tail(stmt);
7256
7257 /* Track the current loop nesting. */
7258 ast_iteration_statement *nesting_ast = state->loop_nesting_ast;
7259
7260 state->loop_nesting_ast = this;
7261
7262 /* Likewise, indicate that following code is closest to a loop,
7263 * NOT closest to a switch.
7264 */
7265 bool saved_is_switch_innermost = state->switch_state.is_switch_innermost;
7266 state->switch_state.is_switch_innermost = false;
7267
7268 if (mode != ast_do_while)
7269 condition_to_hir(&stmt->body_instructions, state);
7270
7271 if (rest_expression != NULL)
7272 rest_expression->hir(&rest_instructions, state);
7273
7274 if (body != NULL) {
7275 if (mode == ast_do_while)
7276 state->symbols->push_scope();
7277
7278 body->hir(& stmt->body_instructions, state);
7279
7280 if (mode == ast_do_while)
7281 state->symbols->pop_scope();
7282 }
7283
7284 if (rest_expression != NULL)
7285 stmt->body_instructions.append_list(&rest_instructions);
7286
7287 if (mode == ast_do_while)
7288 condition_to_hir(&stmt->body_instructions, state);
7289
7290 if (mode != ast_do_while)
7291 state->symbols->pop_scope();
7292
7293 /* Restore previous nesting before returning. */
7294 state->loop_nesting_ast = nesting_ast;
7295 state->switch_state.is_switch_innermost = saved_is_switch_innermost;
7296
7297 /* Loops do not have r-values.
7298 */
7299 return NULL;
7300 }
7301
7302
7303 /**
7304 * Determine if the given type is valid for establishing a default precision
7305 * qualifier.
7306 *
7307 * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
7308 *
7309 * "The precision statement
7310 *
7311 * precision precision-qualifier type;
7312 *
7313 * can be used to establish a default precision qualifier. The type field
7314 * can be either int or float or any of the sampler types, and the
7315 * precision-qualifier can be lowp, mediump, or highp."
7316 *
7317 * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
7318 * qualifiers on sampler types, but this seems like an oversight (since the
7319 * intention of including these in GLSL 1.30 is to allow compatibility with ES
7320 * shaders). So we allow int, float, and all sampler types regardless of GLSL
7321 * version.
7322 */
7323 static bool
is_valid_default_precision_type(const struct glsl_type * const type)7324 is_valid_default_precision_type(const struct glsl_type *const type)
7325 {
7326 if (type == NULL)
7327 return false;
7328
7329 switch (type->base_type) {
7330 case GLSL_TYPE_INT:
7331 case GLSL_TYPE_FLOAT:
7332 /* "int" and "float" are valid, but vectors and matrices are not. */
7333 return type->vector_elements == 1 && type->matrix_columns == 1;
7334 case GLSL_TYPE_SAMPLER:
7335 case GLSL_TYPE_TEXTURE:
7336 case GLSL_TYPE_IMAGE:
7337 case GLSL_TYPE_ATOMIC_UINT:
7338 return true;
7339 default:
7340 return false;
7341 }
7342 }
7343
7344
7345 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)7346 ast_type_specifier::hir(exec_list *instructions,
7347 struct _mesa_glsl_parse_state *state)
7348 {
7349 if (this->default_precision == ast_precision_none && this->structure == NULL)
7350 return NULL;
7351
7352 YYLTYPE loc = this->get_location();
7353
7354 /* If this is a precision statement, check that the type to which it is
7355 * applied is either float or int.
7356 *
7357 * From section 4.5.3 of the GLSL 1.30 spec:
7358 * "The precision statement
7359 * precision precision-qualifier type;
7360 * can be used to establish a default precision qualifier. The type
7361 * field can be either int or float [...]. Any other types or
7362 * qualifiers will result in an error.
7363 */
7364 if (this->default_precision != ast_precision_none) {
7365 if (!state->check_precision_qualifiers_allowed(&loc))
7366 return NULL;
7367
7368 if (this->structure != NULL) {
7369 _mesa_glsl_error(&loc, state,
7370 "precision qualifiers do not apply to structures");
7371 return NULL;
7372 }
7373
7374 if (this->array_specifier != NULL) {
7375 _mesa_glsl_error(&loc, state,
7376 "default precision statements do not apply to "
7377 "arrays");
7378 return NULL;
7379 }
7380
7381 const struct glsl_type *const type =
7382 state->symbols->get_type(this->type_name);
7383 if (!is_valid_default_precision_type(type)) {
7384 _mesa_glsl_error(&loc, state,
7385 "default precision statements apply only to "
7386 "float, int, and opaque types");
7387 return NULL;
7388 }
7389
7390 if (state->es_shader) {
7391 /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
7392 * spec says:
7393 *
7394 * "Non-precision qualified declarations will use the precision
7395 * qualifier specified in the most recent precision statement
7396 * that is still in scope. The precision statement has the same
7397 * scoping rules as variable declarations. If it is declared
7398 * inside a compound statement, its effect stops at the end of
7399 * the innermost statement it was declared in. Precision
7400 * statements in nested scopes override precision statements in
7401 * outer scopes. Multiple precision statements for the same basic
7402 * type can appear inside the same scope, with later statements
7403 * overriding earlier statements within that scope."
7404 *
7405 * Default precision specifications follow the same scope rules as
7406 * variables. So, we can track the state of the default precision
7407 * qualifiers in the symbol table, and the rules will just work. This
7408 * is a slight abuse of the symbol table, but it has the semantics
7409 * that we want.
7410 */
7411 state->symbols->add_default_precision_qualifier(this->type_name,
7412 this->default_precision);
7413 }
7414
7415 /* FINISHME: Translate precision statements into IR. */
7416 return NULL;
7417 }
7418
7419 /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
7420 * process_record_constructor() can do type-checking on C-style initializer
7421 * expressions of structs, but ast_struct_specifier should only be translated
7422 * to HIR if it is declaring the type of a structure.
7423 *
7424 * The ->is_declaration field is false for initializers of variables
7425 * declared separately from the struct's type definition.
7426 *
7427 * struct S { ... }; (is_declaration = true)
7428 * struct T { ... } t = { ... }; (is_declaration = true)
7429 * S s = { ... }; (is_declaration = false)
7430 */
7431 if (this->structure != NULL && this->structure->is_declaration)
7432 return this->structure->hir(instructions, state);
7433
7434 return NULL;
7435 }
7436
7437
7438 /**
7439 * Process a structure or interface block tree into an array of structure fields
7440 *
7441 * After parsing, where there are some syntax differnces, structures and
7442 * interface blocks are almost identical. They are similar enough that the
7443 * AST for each can be processed the same way into a set of
7444 * \c glsl_struct_field to describe the members.
7445 *
7446 * If we're processing an interface block, var_mode should be the type of the
7447 * interface block (ir_var_shader_in, ir_var_shader_out, ir_var_uniform or
7448 * ir_var_shader_storage). If we're processing a structure, var_mode should be
7449 * ir_var_auto.
7450 *
7451 * \return
7452 * The number of fields processed. A pointer to the array structure fields is
7453 * stored in \c *fields_ret.
7454 */
7455 static unsigned
ast_process_struct_or_iface_block_members(exec_list * instructions,struct _mesa_glsl_parse_state * state,exec_list * declarations,glsl_struct_field ** fields_ret,bool is_interface,enum glsl_matrix_layout matrix_layout,bool allow_reserved_names,ir_variable_mode var_mode,ast_type_qualifier * layout,unsigned block_stream,unsigned block_xfb_buffer,unsigned block_xfb_offset,unsigned expl_location,unsigned expl_align)7456 ast_process_struct_or_iface_block_members(exec_list *instructions,
7457 struct _mesa_glsl_parse_state *state,
7458 exec_list *declarations,
7459 glsl_struct_field **fields_ret,
7460 bool is_interface,
7461 enum glsl_matrix_layout matrix_layout,
7462 bool allow_reserved_names,
7463 ir_variable_mode var_mode,
7464 ast_type_qualifier *layout,
7465 unsigned block_stream,
7466 unsigned block_xfb_buffer,
7467 unsigned block_xfb_offset,
7468 unsigned expl_location,
7469 unsigned expl_align)
7470 {
7471 unsigned decl_count = 0;
7472 unsigned next_offset = 0;
7473
7474 /* Make an initial pass over the list of fields to determine how
7475 * many there are. Each element in this list is an ast_declarator_list.
7476 * This means that we actually need to count the number of elements in the
7477 * 'declarations' list in each of the elements.
7478 */
7479 foreach_list_typed (ast_declarator_list, decl_list, link, declarations) {
7480 decl_count += decl_list->declarations.length();
7481 }
7482
7483 /* Allocate storage for the fields and process the field
7484 * declarations. As the declarations are processed, try to also convert
7485 * the types to HIR. This ensures that structure definitions embedded in
7486 * other structure definitions or in interface blocks are processed.
7487 */
7488 glsl_struct_field *const fields = rzalloc_array(state, glsl_struct_field,
7489 decl_count);
7490
7491 bool first_member = true;
7492 bool first_member_has_explicit_location = false;
7493
7494 unsigned i = 0;
7495 foreach_list_typed (ast_declarator_list, decl_list, link, declarations) {
7496 const char *type_name;
7497 YYLTYPE loc = decl_list->get_location();
7498
7499 decl_list->type->specifier->hir(instructions, state);
7500
7501 /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
7502 *
7503 * "Anonymous structures are not supported; so embedded structures
7504 * must have a declarator. A name given to an embedded struct is
7505 * scoped at the same level as the struct it is embedded in."
7506 *
7507 * The same section of the GLSL 1.20 spec says:
7508 *
7509 * "Anonymous structures are not supported. Embedded structures are
7510 * not supported."
7511 *
7512 * The GLSL ES 1.00 and 3.00 specs have similar langauge. So, we allow
7513 * embedded structures in 1.10 only.
7514 */
7515 if (state->language_version != 110 &&
7516 decl_list->type->specifier->structure != NULL)
7517 _mesa_glsl_error(&loc, state,
7518 "embedded structure declarations are not allowed");
7519
7520 const glsl_type *decl_type =
7521 decl_list->type->glsl_type(& type_name, state);
7522
7523 const struct ast_type_qualifier *const qual =
7524 &decl_list->type->qualifier;
7525
7526 /* From section 4.3.9 of the GLSL 4.40 spec:
7527 *
7528 * "[In interface blocks] opaque types are not allowed."
7529 *
7530 * It should be impossible for decl_type to be NULL here. Cases that
7531 * might naturally lead to decl_type being NULL, especially for the
7532 * is_interface case, will have resulted in compilation having
7533 * already halted due to a syntax error.
7534 */
7535 assert(decl_type);
7536
7537 if (is_interface) {
7538 /* From section 4.3.7 of the ARB_bindless_texture spec:
7539 *
7540 * "(remove the following bullet from the last list on p. 39,
7541 * thereby permitting sampler types in interface blocks; image
7542 * types are also permitted in blocks by this extension)"
7543 *
7544 * * sampler types are not allowed
7545 */
7546 if (glsl_contains_atomic(decl_type) ||
7547 (!state->has_bindless() && glsl_contains_opaque(decl_type))) {
7548 _mesa_glsl_error(&loc, state, "uniform/buffer in non-default "
7549 "interface block contains %s variable",
7550 state->has_bindless() ? "atomic" : "opaque");
7551 }
7552 } else {
7553 if (glsl_contains_atomic(decl_type)) {
7554 /* From section 4.1.7.3 of the GLSL 4.40 spec:
7555 *
7556 * "Members of structures cannot be declared as atomic counter
7557 * types."
7558 */
7559 _mesa_glsl_error(&loc, state, "atomic counter in structure");
7560 }
7561
7562 if (!state->has_bindless() && glsl_type_contains_image(decl_type)) {
7563 /* FINISHME: Same problem as with atomic counters.
7564 * FINISHME: Request clarification from Khronos and add
7565 * FINISHME: spec quotation here.
7566 */
7567 _mesa_glsl_error(&loc, state, "image in structure");
7568 }
7569 }
7570
7571 if (qual->flags.q.explicit_binding) {
7572 _mesa_glsl_error(&loc, state,
7573 "binding layout qualifier cannot be applied "
7574 "to struct or interface block members");
7575 }
7576
7577 if (is_interface) {
7578 if (!first_member) {
7579 if (!layout->flags.q.explicit_location &&
7580 ((first_member_has_explicit_location &&
7581 !qual->flags.q.explicit_location) ||
7582 (!first_member_has_explicit_location &&
7583 qual->flags.q.explicit_location))) {
7584 _mesa_glsl_error(&loc, state,
7585 "when block-level location layout qualifier "
7586 "is not supplied either all members must "
7587 "have a location layout qualifier or all "
7588 "members must not have a location layout "
7589 "qualifier");
7590 }
7591 } else {
7592 first_member = false;
7593 first_member_has_explicit_location =
7594 qual->flags.q.explicit_location;
7595 }
7596 }
7597
7598 if (qual->flags.q.std140 ||
7599 qual->flags.q.std430 ||
7600 qual->flags.q.packed ||
7601 qual->flags.q.shared) {
7602 _mesa_glsl_error(&loc, state,
7603 "uniform/shader storage block layout qualifiers "
7604 "std140, std430, packed, and shared can only be "
7605 "applied to uniform/shader storage blocks, not "
7606 "members");
7607 }
7608
7609 if (qual->flags.q.constant) {
7610 _mesa_glsl_error(&loc, state,
7611 "const storage qualifier cannot be applied "
7612 "to struct or interface block members");
7613 }
7614
7615 validate_memory_qualifier_for_type(state, &loc, qual, decl_type);
7616 validate_image_format_qualifier_for_type(state, &loc, qual, decl_type);
7617
7618 /* From Section 4.4.2.3 (Geometry Outputs) of the GLSL 4.50 spec:
7619 *
7620 * "A block member may be declared with a stream identifier, but
7621 * the specified stream must match the stream associated with the
7622 * containing block."
7623 */
7624 if (qual->flags.q.explicit_stream) {
7625 unsigned qual_stream;
7626 if (process_qualifier_constant(state, &loc, "stream",
7627 qual->stream, &qual_stream) &&
7628 qual_stream != block_stream) {
7629 _mesa_glsl_error(&loc, state, "stream layout qualifier on "
7630 "interface block member does not match "
7631 "the interface block (%u vs %u)", qual_stream,
7632 block_stream);
7633 }
7634 }
7635
7636 int xfb_buffer;
7637 unsigned explicit_xfb_buffer = 0;
7638 if (qual->flags.q.explicit_xfb_buffer) {
7639 unsigned qual_xfb_buffer;
7640 if (process_qualifier_constant(state, &loc, "xfb_buffer",
7641 qual->xfb_buffer, &qual_xfb_buffer)) {
7642 explicit_xfb_buffer = 1;
7643 if (qual_xfb_buffer != block_xfb_buffer)
7644 _mesa_glsl_error(&loc, state, "xfb_buffer layout qualifier on "
7645 "interface block member does not match "
7646 "the interface block (%u vs %u)",
7647 qual_xfb_buffer, block_xfb_buffer);
7648 }
7649 xfb_buffer = (int) qual_xfb_buffer;
7650 } else {
7651 if (layout)
7652 explicit_xfb_buffer = layout->flags.q.explicit_xfb_buffer;
7653 xfb_buffer = (int) block_xfb_buffer;
7654 }
7655
7656 int xfb_stride = -1;
7657 if (qual->flags.q.explicit_xfb_stride) {
7658 unsigned qual_xfb_stride;
7659 if (process_qualifier_constant(state, &loc, "xfb_stride",
7660 qual->xfb_stride, &qual_xfb_stride)) {
7661 xfb_stride = (int) qual_xfb_stride;
7662 }
7663 }
7664
7665 if (qual->flags.q.uniform && qual->has_interpolation()) {
7666 _mesa_glsl_error(&loc, state,
7667 "interpolation qualifiers cannot be used "
7668 "with uniform interface blocks");
7669 }
7670
7671 if ((qual->flags.q.uniform || !is_interface) &&
7672 qual->has_auxiliary_storage()) {
7673 _mesa_glsl_error(&loc, state,
7674 "auxiliary storage qualifiers cannot be used "
7675 "in uniform blocks or structures.");
7676 }
7677
7678 if (qual->flags.q.row_major || qual->flags.q.column_major) {
7679 if (!qual->flags.q.uniform && !qual->flags.q.buffer) {
7680 _mesa_glsl_error(&loc, state,
7681 "row_major and column_major can only be "
7682 "applied to interface blocks");
7683 } else
7684 validate_matrix_layout_for_type(state, &loc, decl_type, NULL);
7685 }
7686
7687 foreach_list_typed (ast_declaration, decl, link,
7688 &decl_list->declarations) {
7689 YYLTYPE loc = decl->get_location();
7690
7691 if (!allow_reserved_names)
7692 validate_identifier(decl->identifier, loc, state);
7693
7694 const struct glsl_type *field_type =
7695 process_array_type(&loc, decl_type, decl->array_specifier, state);
7696 validate_array_dimensions(field_type, state, &loc);
7697 fields[i].type = field_type;
7698 fields[i].name = decl->identifier;
7699 fields[i].interpolation =
7700 interpret_interpolation_qualifier(qual, field_type,
7701 var_mode, state, &loc);
7702 fields[i].centroid = qual->flags.q.centroid ? 1 : 0;
7703 fields[i].sample = qual->flags.q.sample ? 1 : 0;
7704 fields[i].patch = qual->flags.q.patch ? 1 : 0;
7705 fields[i].offset = -1;
7706 fields[i].explicit_xfb_buffer = explicit_xfb_buffer;
7707 fields[i].xfb_buffer = xfb_buffer;
7708 fields[i].xfb_stride = xfb_stride;
7709
7710 if (qual->flags.q.explicit_location) {
7711 unsigned qual_location;
7712 if (process_qualifier_constant(state, &loc, "location",
7713 qual->location, &qual_location)) {
7714 fields[i].location = qual_location +
7715 (fields[i].patch ? VARYING_SLOT_PATCH0 : VARYING_SLOT_VAR0);
7716 expl_location = fields[i].location +
7717 glsl_count_attribute_slots(fields[i].type, false);
7718 }
7719 } else {
7720 if (layout && layout->flags.q.explicit_location) {
7721 fields[i].location = expl_location;
7722 expl_location += glsl_count_attribute_slots(fields[i].type, false);
7723 } else {
7724 fields[i].location = -1;
7725 }
7726 }
7727
7728 if (qual->flags.q.explicit_component) {
7729 unsigned qual_component;
7730 if (process_qualifier_constant(state, &loc, "component",
7731 qual->component, &qual_component)) {
7732 validate_component_layout_for_type(state, &loc, fields[i].type,
7733 qual_component);
7734 fields[i].component = qual_component;
7735 }
7736 } else {
7737 fields[i].component = -1;
7738 }
7739
7740 /* Offset can only be used with std430 and std140 layouts an initial
7741 * value of 0 is used for error detection.
7742 */
7743 unsigned base_alignment = 0;
7744 unsigned size = 0;
7745 if (layout) {
7746 bool row_major;
7747 if (qual->flags.q.row_major ||
7748 matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
7749 row_major = true;
7750 } else {
7751 row_major = false;
7752 }
7753
7754 if(layout->flags.q.std140) {
7755 base_alignment = glsl_get_std140_base_alignment(field_type, row_major);
7756 size = glsl_get_std140_size(field_type, row_major);
7757 } else if (layout->flags.q.std430) {
7758 base_alignment = glsl_get_std430_base_alignment(field_type, row_major);
7759 size = glsl_get_std430_size(field_type, row_major);
7760 }
7761 }
7762
7763 if (qual->flags.q.explicit_offset) {
7764 unsigned qual_offset;
7765 if (process_qualifier_constant(state, &loc, "offset",
7766 qual->offset, &qual_offset)) {
7767 if (base_alignment != 0 && size != 0) {
7768 if (next_offset > qual_offset)
7769 _mesa_glsl_error(&loc, state, "layout qualifier "
7770 "offset overlaps previous member");
7771
7772 if (qual_offset % base_alignment) {
7773 _mesa_glsl_error(&loc, state, "layout qualifier offset "
7774 "must be a multiple of the base "
7775 "alignment of %s", glsl_get_type_name(field_type));
7776 }
7777 fields[i].offset = qual_offset;
7778 next_offset = qual_offset + size;
7779 } else {
7780 _mesa_glsl_error(&loc, state, "offset can only be used "
7781 "with std430 and std140 layouts");
7782 }
7783 }
7784 }
7785
7786 if (qual->flags.q.explicit_align || expl_align != 0) {
7787 unsigned offset = fields[i].offset != -1 ? fields[i].offset :
7788 next_offset;
7789 if (base_alignment == 0 || size == 0) {
7790 _mesa_glsl_error(&loc, state, "align can only be used with "
7791 "std430 and std140 layouts");
7792 } else if (qual->flags.q.explicit_align) {
7793 unsigned member_align;
7794 if (process_qualifier_constant(state, &loc, "align",
7795 qual->align, &member_align)) {
7796 if (member_align == 0 ||
7797 member_align & (member_align - 1)) {
7798 _mesa_glsl_error(&loc, state, "align layout qualifier "
7799 "is not a power of 2");
7800 } else {
7801 fields[i].offset = align(offset, member_align);
7802 next_offset = fields[i].offset + size;
7803 }
7804 }
7805 } else {
7806 fields[i].offset = align(offset, expl_align);
7807 next_offset = fields[i].offset + size;
7808 }
7809 } else if (!qual->flags.q.explicit_offset) {
7810 if (base_alignment != 0 && size != 0)
7811 next_offset = align(next_offset, base_alignment) + size;
7812 }
7813
7814 /* From the ARB_enhanced_layouts spec:
7815 *
7816 * "The given offset applies to the first component of the first
7817 * member of the qualified entity. Then, within the qualified
7818 * entity, subsequent components are each assigned, in order, to
7819 * the next available offset aligned to a multiple of that
7820 * component's size. Aggregate types are flattened down to the
7821 * component level to get this sequence of components."
7822 */
7823 if (qual->flags.q.explicit_xfb_offset) {
7824 unsigned xfb_offset;
7825 if (process_qualifier_constant(state, &loc, "xfb_offset",
7826 qual->offset, &xfb_offset)) {
7827 fields[i].offset = xfb_offset;
7828 block_xfb_offset = fields[i].offset +
7829 4 * glsl_get_component_slots(field_type);
7830 }
7831 } else {
7832 if (layout && layout->flags.q.explicit_xfb_offset) {
7833 unsigned base_alignment = glsl_type_is_64bit(field_type) ? 8 : 4;
7834 fields[i].offset = align(block_xfb_offset, base_alignment);
7835 block_xfb_offset += 4 * glsl_get_component_slots(field_type);
7836 }
7837 }
7838
7839 /* Propogate row- / column-major information down the fields of the
7840 * structure or interface block. Structures need this data because
7841 * the structure may contain a structure that contains ... a matrix
7842 * that need the proper layout.
7843 */
7844 if (is_interface && layout &&
7845 (layout->flags.q.uniform || layout->flags.q.buffer) &&
7846 (glsl_type_is_matrix(glsl_without_array(field_type))
7847 || glsl_type_is_struct(glsl_without_array(field_type)))) {
7848 /* If no layout is specified for the field, inherit the layout
7849 * from the block.
7850 */
7851 fields[i].matrix_layout = matrix_layout;
7852
7853 if (qual->flags.q.row_major)
7854 fields[i].matrix_layout = GLSL_MATRIX_LAYOUT_ROW_MAJOR;
7855 else if (qual->flags.q.column_major)
7856 fields[i].matrix_layout = GLSL_MATRIX_LAYOUT_COLUMN_MAJOR;
7857
7858 /* If we're processing an uniform or buffer block, the matrix
7859 * layout must be decided by this point.
7860 */
7861 assert(fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR
7862 || fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR);
7863 }
7864
7865 /* Memory qualifiers are allowed on buffer and image variables, while
7866 * the format qualifier is only accepted for images.
7867 */
7868 if (var_mode == ir_var_shader_storage ||
7869 glsl_type_is_image(glsl_without_array(field_type))) {
7870 /* For readonly and writeonly qualifiers the field definition,
7871 * if set, overwrites the layout qualifier.
7872 */
7873 if (qual->flags.q.read_only || qual->flags.q.write_only) {
7874 fields[i].memory_read_only = qual->flags.q.read_only;
7875 fields[i].memory_write_only = qual->flags.q.write_only;
7876 } else {
7877 fields[i].memory_read_only =
7878 layout ? layout->flags.q.read_only : 0;
7879 fields[i].memory_write_only =
7880 layout ? layout->flags.q.write_only : 0;
7881 }
7882
7883 /* For other qualifiers, we set the flag if either the layout
7884 * qualifier or the field qualifier are set
7885 */
7886 fields[i].memory_coherent = qual->flags.q.coherent ||
7887 (layout && layout->flags.q.coherent);
7888 fields[i].memory_volatile = qual->flags.q._volatile ||
7889 (layout && layout->flags.q._volatile);
7890 fields[i].memory_restrict = qual->flags.q.restrict_flag ||
7891 (layout && layout->flags.q.restrict_flag);
7892
7893 if (glsl_type_is_image(glsl_without_array(field_type))) {
7894 if (qual->flags.q.explicit_image_format) {
7895 if (qual->image_base_type !=
7896 glsl_without_array(field_type)->sampled_type) {
7897 _mesa_glsl_error(&loc, state, "format qualifier doesn't "
7898 "match the base data type of the image");
7899 }
7900
7901 fields[i].image_format = qual->image_format;
7902 } else {
7903 if (!qual->flags.q.write_only) {
7904 _mesa_glsl_error(&loc, state, "image not qualified with "
7905 "`writeonly' must have a format layout "
7906 "qualifier");
7907 }
7908
7909 fields[i].image_format = PIPE_FORMAT_NONE;
7910 }
7911 }
7912 }
7913
7914 /* Precision qualifiers do not hold any meaning in Desktop GLSL */
7915 if (state->es_shader) {
7916 fields[i].precision = select_gles_precision(qual->precision,
7917 field_type,
7918 state,
7919 &loc);
7920 } else {
7921 fields[i].precision = qual->precision;
7922 }
7923
7924 i++;
7925 }
7926 }
7927
7928 assert(i == decl_count);
7929
7930 *fields_ret = fields;
7931 return decl_count;
7932 }
7933
7934 static bool
is_anonymous(const glsl_type * t)7935 is_anonymous(const glsl_type *t)
7936 {
7937 /* See handling for struct_specifier in glsl_parser.yy. */
7938 return !strncmp(glsl_get_type_name(t), "#anon", 5);
7939 }
7940
7941 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)7942 ast_struct_specifier::hir(exec_list *instructions,
7943 struct _mesa_glsl_parse_state *state)
7944 {
7945 YYLTYPE loc = this->get_location();
7946
7947 unsigned expl_location = 0;
7948 if (layout && layout->flags.q.explicit_location) {
7949 if (!process_qualifier_constant(state, &loc, "location",
7950 layout->location, &expl_location)) {
7951 return NULL;
7952 } else {
7953 expl_location = VARYING_SLOT_VAR0 + expl_location;
7954 }
7955 }
7956
7957 glsl_struct_field *fields;
7958 unsigned decl_count =
7959 ast_process_struct_or_iface_block_members(instructions,
7960 state,
7961 &this->declarations,
7962 &fields,
7963 false,
7964 GLSL_MATRIX_LAYOUT_INHERITED,
7965 false /* allow_reserved_names */,
7966 ir_var_auto,
7967 layout,
7968 0, /* for interface only */
7969 0, /* for interface only */
7970 0, /* for interface only */
7971 expl_location,
7972 0 /* for interface only */);
7973
7974 validate_identifier(this->name, loc, state);
7975
7976 type = glsl_struct_type(fields, decl_count, this->name, false /* packed */);
7977
7978 if (!is_anonymous(type) && !state->symbols->add_type(name, type)) {
7979 const glsl_type *match = state->symbols->get_type(name);
7980 /* allow struct matching for desktop GL - older UE4 does this */
7981 if (match != NULL && state->is_version(130, 0) && glsl_record_compare(match, type, true, false, true))
7982 _mesa_glsl_warning(& loc, state, "struct `%s' previously defined", name);
7983 else
7984 _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
7985 } else {
7986 const glsl_type **s = reralloc(state, state->user_structures,
7987 const glsl_type *,
7988 state->num_user_structures + 1);
7989 if (s != NULL) {
7990 s[state->num_user_structures] = type;
7991 state->user_structures = s;
7992 state->num_user_structures++;
7993 }
7994 }
7995
7996 /* Structure type definitions do not have r-values.
7997 */
7998 return NULL;
7999 }
8000
8001
8002 /**
8003 * Visitor class which detects whether a given interface block has been used.
8004 */
8005 class interface_block_usage_visitor : public ir_hierarchical_visitor
8006 {
8007 public:
interface_block_usage_visitor(ir_variable_mode mode,const glsl_type * block)8008 interface_block_usage_visitor(ir_variable_mode mode, const glsl_type *block)
8009 : mode(mode), block(block), found(false)
8010 {
8011 }
8012
visit(ir_dereference_variable * ir)8013 virtual ir_visitor_status visit(ir_dereference_variable *ir)
8014 {
8015 if (ir->var->data.mode == mode && ir->var->get_interface_type() == block) {
8016 found = true;
8017 return visit_stop;
8018 }
8019 return visit_continue;
8020 }
8021
usage_found() const8022 bool usage_found() const
8023 {
8024 return this->found;
8025 }
8026
8027 private:
8028 ir_variable_mode mode;
8029 const glsl_type *block;
8030 bool found;
8031 };
8032
8033 static bool
is_unsized_array_last_element(ir_variable * v)8034 is_unsized_array_last_element(ir_variable *v)
8035 {
8036 const glsl_type *interface_type = v->get_interface_type();
8037 int length = interface_type->length;
8038
8039 assert(glsl_type_is_unsized_array(v->type));
8040
8041 /* Check if it is the last element of the interface */
8042 if (strcmp(interface_type->fields.structure[length-1].name, v->name) == 0)
8043 return true;
8044 return false;
8045 }
8046
8047 static void
apply_memory_qualifiers(ir_variable * var,glsl_struct_field field)8048 apply_memory_qualifiers(ir_variable *var, glsl_struct_field field)
8049 {
8050 var->data.memory_read_only = field.memory_read_only;
8051 var->data.memory_write_only = field.memory_write_only;
8052 var->data.memory_coherent = field.memory_coherent;
8053 var->data.memory_volatile = field.memory_volatile;
8054 var->data.memory_restrict = field.memory_restrict;
8055 }
8056
8057 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)8058 ast_interface_block::hir(exec_list *instructions,
8059 struct _mesa_glsl_parse_state *state)
8060 {
8061 YYLTYPE loc = this->get_location();
8062
8063 /* Interface blocks must be declared at global scope */
8064 if (state->current_function != NULL) {
8065 _mesa_glsl_error(&loc, state,
8066 "Interface block `%s' must be declared "
8067 "at global scope",
8068 this->block_name);
8069 }
8070
8071 /* Validate qualifiers:
8072 *
8073 * - Layout Qualifiers as per the table in Section 4.4
8074 * ("Layout Qualifiers") of the GLSL 4.50 spec.
8075 *
8076 * - Memory Qualifiers as per Section 4.10 ("Memory Qualifiers") of the
8077 * GLSL 4.50 spec:
8078 *
8079 * "Additionally, memory qualifiers may also be used in the declaration
8080 * of shader storage blocks"
8081 *
8082 * Note the table in Section 4.4 says std430 is allowed on both uniform and
8083 * buffer blocks however Section 4.4.5 (Uniform and Shader Storage Block
8084 * Layout Qualifiers) of the GLSL 4.50 spec says:
8085 *
8086 * "The std430 qualifier is supported only for shader storage blocks;
8087 * using std430 on a uniform block will result in a compile-time error."
8088 */
8089 ast_type_qualifier allowed_blk_qualifiers;
8090 allowed_blk_qualifiers.flags.i = 0;
8091 if (this->layout.flags.q.buffer || this->layout.flags.q.uniform) {
8092 allowed_blk_qualifiers.flags.q.shared = 1;
8093 allowed_blk_qualifiers.flags.q.packed = 1;
8094 allowed_blk_qualifiers.flags.q.std140 = 1;
8095 allowed_blk_qualifiers.flags.q.row_major = 1;
8096 allowed_blk_qualifiers.flags.q.column_major = 1;
8097 allowed_blk_qualifiers.flags.q.explicit_align = 1;
8098 allowed_blk_qualifiers.flags.q.explicit_binding = 1;
8099 if (this->layout.flags.q.buffer) {
8100 allowed_blk_qualifiers.flags.q.buffer = 1;
8101 allowed_blk_qualifiers.flags.q.std430 = 1;
8102 allowed_blk_qualifiers.flags.q.coherent = 1;
8103 allowed_blk_qualifiers.flags.q._volatile = 1;
8104 allowed_blk_qualifiers.flags.q.restrict_flag = 1;
8105 allowed_blk_qualifiers.flags.q.read_only = 1;
8106 allowed_blk_qualifiers.flags.q.write_only = 1;
8107 } else {
8108 allowed_blk_qualifiers.flags.q.uniform = 1;
8109 }
8110 } else {
8111 /* Interface block */
8112 assert(this->layout.flags.q.in || this->layout.flags.q.out);
8113
8114 allowed_blk_qualifiers.flags.q.explicit_location = 1;
8115 if (this->layout.flags.q.out) {
8116 allowed_blk_qualifiers.flags.q.out = 1;
8117 if (state->stage == MESA_SHADER_GEOMETRY ||
8118 state->stage == MESA_SHADER_TESS_CTRL ||
8119 state->stage == MESA_SHADER_TESS_EVAL ||
8120 state->stage == MESA_SHADER_VERTEX ) {
8121 allowed_blk_qualifiers.flags.q.explicit_xfb_offset = 1;
8122 allowed_blk_qualifiers.flags.q.explicit_xfb_buffer = 1;
8123 allowed_blk_qualifiers.flags.q.xfb_buffer = 1;
8124 allowed_blk_qualifiers.flags.q.explicit_xfb_stride = 1;
8125 allowed_blk_qualifiers.flags.q.xfb_stride = 1;
8126 }
8127 if (state->stage == MESA_SHADER_GEOMETRY) {
8128 allowed_blk_qualifiers.flags.q.stream = 1;
8129 allowed_blk_qualifiers.flags.q.explicit_stream = 1;
8130 }
8131 if (state->stage == MESA_SHADER_TESS_CTRL) {
8132 allowed_blk_qualifiers.flags.q.patch = 1;
8133 }
8134 } else {
8135 allowed_blk_qualifiers.flags.q.in = 1;
8136 if (state->stage == MESA_SHADER_TESS_EVAL) {
8137 allowed_blk_qualifiers.flags.q.patch = 1;
8138 }
8139 }
8140 }
8141
8142 this->layout.validate_flags(&loc, state, allowed_blk_qualifiers,
8143 "invalid qualifier for block",
8144 this->block_name);
8145
8146 enum glsl_interface_packing packing;
8147 if (this->layout.flags.q.std140) {
8148 packing = GLSL_INTERFACE_PACKING_STD140;
8149 } else if (this->layout.flags.q.packed) {
8150 packing = GLSL_INTERFACE_PACKING_PACKED;
8151 } else if (this->layout.flags.q.std430) {
8152 packing = GLSL_INTERFACE_PACKING_STD430;
8153 } else {
8154 /* The default layout is shared.
8155 */
8156 packing = GLSL_INTERFACE_PACKING_SHARED;
8157 }
8158
8159 ir_variable_mode var_mode;
8160 const char *iface_type_name;
8161 if (this->layout.flags.q.in) {
8162 var_mode = ir_var_shader_in;
8163 iface_type_name = "in";
8164 } else if (this->layout.flags.q.out) {
8165 var_mode = ir_var_shader_out;
8166 iface_type_name = "out";
8167 } else if (this->layout.flags.q.uniform) {
8168 var_mode = ir_var_uniform;
8169 iface_type_name = "uniform";
8170 } else if (this->layout.flags.q.buffer) {
8171 var_mode = ir_var_shader_storage;
8172 iface_type_name = "buffer";
8173 } else {
8174 var_mode = ir_var_auto;
8175 iface_type_name = "UNKNOWN";
8176 assert(!"interface block layout qualifier not found!");
8177 }
8178
8179 enum glsl_matrix_layout matrix_layout = GLSL_MATRIX_LAYOUT_INHERITED;
8180 if (this->layout.flags.q.row_major)
8181 matrix_layout = GLSL_MATRIX_LAYOUT_ROW_MAJOR;
8182 else if (this->layout.flags.q.column_major)
8183 matrix_layout = GLSL_MATRIX_LAYOUT_COLUMN_MAJOR;
8184
8185 bool redeclaring_per_vertex = strcmp(this->block_name, "gl_PerVertex") == 0;
8186 exec_list declared_variables;
8187 glsl_struct_field *fields;
8188
8189 /* For blocks that accept memory qualifiers (i.e. shader storage), verify
8190 * that we don't have incompatible qualifiers
8191 */
8192 if (this->layout.flags.q.read_only && this->layout.flags.q.write_only) {
8193 _mesa_glsl_error(&loc, state,
8194 "Interface block sets both readonly and writeonly");
8195 }
8196
8197 unsigned qual_stream;
8198 if (!process_qualifier_constant(state, &loc, "stream", this->layout.stream,
8199 &qual_stream) ||
8200 !validate_stream_qualifier(&loc, state, qual_stream)) {
8201 /* If the stream qualifier is invalid it doesn't make sense to continue
8202 * on and try to compare stream layouts on member variables against it
8203 * so just return early.
8204 */
8205 return NULL;
8206 }
8207
8208 unsigned qual_xfb_buffer = 0;
8209 if (layout.flags.q.xfb_buffer) {
8210 if (!process_qualifier_constant(state, &loc, "xfb_buffer",
8211 layout.xfb_buffer, &qual_xfb_buffer) ||
8212 !validate_xfb_buffer_qualifier(&loc, state, qual_xfb_buffer)) {
8213 return NULL;
8214 }
8215 }
8216
8217 unsigned qual_xfb_offset = 0;
8218 if (layout.flags.q.explicit_xfb_offset) {
8219 if (!process_qualifier_constant(state, &loc, "xfb_offset",
8220 layout.offset, &qual_xfb_offset)) {
8221 return NULL;
8222 }
8223 }
8224
8225 unsigned qual_xfb_stride = 0;
8226 if (layout.flags.q.explicit_xfb_stride) {
8227 if (!process_qualifier_constant(state, &loc, "xfb_stride",
8228 layout.xfb_stride, &qual_xfb_stride)) {
8229 return NULL;
8230 }
8231 }
8232
8233 unsigned expl_location = 0;
8234 if (layout.flags.q.explicit_location) {
8235 if (!process_qualifier_constant(state, &loc, "location",
8236 layout.location, &expl_location)) {
8237 return NULL;
8238 } else {
8239 expl_location += this->layout.flags.q.patch ? VARYING_SLOT_PATCH0
8240 : VARYING_SLOT_VAR0;
8241 }
8242 }
8243
8244 unsigned expl_align = 0;
8245 if (layout.flags.q.explicit_align) {
8246 if (!process_qualifier_constant(state, &loc, "align",
8247 layout.align, &expl_align)) {
8248 return NULL;
8249 } else {
8250 if (expl_align == 0 || expl_align & (expl_align - 1)) {
8251 _mesa_glsl_error(&loc, state, "align layout qualifier is not a "
8252 "power of 2.");
8253 return NULL;
8254 }
8255 }
8256 }
8257
8258 unsigned int num_variables =
8259 ast_process_struct_or_iface_block_members(&declared_variables,
8260 state,
8261 &this->declarations,
8262 &fields,
8263 true,
8264 matrix_layout,
8265 redeclaring_per_vertex,
8266 var_mode,
8267 &this->layout,
8268 qual_stream,
8269 qual_xfb_buffer,
8270 qual_xfb_offset,
8271 expl_location,
8272 expl_align);
8273
8274 if (!redeclaring_per_vertex) {
8275 validate_identifier(this->block_name, loc, state);
8276
8277 /* From section 4.3.9 ("Interface Blocks") of the GLSL 4.50 spec:
8278 *
8279 * "Block names have no other use within a shader beyond interface
8280 * matching; it is a compile-time error to use a block name at global
8281 * scope for anything other than as a block name."
8282 */
8283 ir_variable *var = state->symbols->get_variable(this->block_name);
8284 if (var && !glsl_type_is_interface(var->type)) {
8285 _mesa_glsl_error(&loc, state, "Block name `%s' is "
8286 "already used in the scope.",
8287 this->block_name);
8288 }
8289 }
8290
8291 const glsl_type *earlier_per_vertex = NULL;
8292 if (redeclaring_per_vertex) {
8293 /* Find the previous declaration of gl_PerVertex. If we're redeclaring
8294 * the named interface block gl_in, we can find it by looking at the
8295 * previous declaration of gl_in. Otherwise we can find it by looking
8296 * at the previous decalartion of any of the built-in outputs,
8297 * e.g. gl_Position.
8298 *
8299 * Also check that the instance name and array-ness of the redeclaration
8300 * are correct.
8301 */
8302 switch (var_mode) {
8303 case ir_var_shader_in:
8304 if (ir_variable *earlier_gl_in =
8305 state->symbols->get_variable("gl_in")) {
8306 earlier_per_vertex = earlier_gl_in->get_interface_type();
8307 } else {
8308 _mesa_glsl_error(&loc, state,
8309 "redeclaration of gl_PerVertex input not allowed "
8310 "in the %s shader",
8311 _mesa_shader_stage_to_string(state->stage));
8312 }
8313 if (this->instance_name == NULL ||
8314 strcmp(this->instance_name, "gl_in") != 0 || this->array_specifier == NULL ||
8315 !this->array_specifier->is_single_dimension()) {
8316 _mesa_glsl_error(&loc, state,
8317 "gl_PerVertex input must be redeclared as "
8318 "gl_in[]");
8319 }
8320 break;
8321 case ir_var_shader_out:
8322 if (ir_variable *earlier_gl_Position =
8323 state->symbols->get_variable("gl_Position")) {
8324 earlier_per_vertex = earlier_gl_Position->get_interface_type();
8325 } else if (ir_variable *earlier_gl_out =
8326 state->symbols->get_variable("gl_out")) {
8327 earlier_per_vertex = earlier_gl_out->get_interface_type();
8328 } else {
8329 _mesa_glsl_error(&loc, state,
8330 "redeclaration of gl_PerVertex output not "
8331 "allowed in the %s shader",
8332 _mesa_shader_stage_to_string(state->stage));
8333 }
8334 if (state->stage == MESA_SHADER_TESS_CTRL) {
8335 if (this->instance_name == NULL ||
8336 strcmp(this->instance_name, "gl_out") != 0 || this->array_specifier == NULL) {
8337 _mesa_glsl_error(&loc, state,
8338 "gl_PerVertex output must be redeclared as "
8339 "gl_out[]");
8340 }
8341 } else {
8342 if (this->instance_name != NULL) {
8343 _mesa_glsl_error(&loc, state,
8344 "gl_PerVertex output may not be redeclared with "
8345 "an instance name");
8346 }
8347 }
8348 break;
8349 default:
8350 _mesa_glsl_error(&loc, state,
8351 "gl_PerVertex must be declared as an input or an "
8352 "output");
8353 break;
8354 }
8355
8356 if (earlier_per_vertex == NULL) {
8357 /* An error has already been reported. Bail out to avoid null
8358 * dereferences later in this function.
8359 */
8360 return NULL;
8361 }
8362
8363 /* Copy locations from the old gl_PerVertex interface block. */
8364 for (unsigned i = 0; i < num_variables; i++) {
8365 int j = glsl_get_field_index(earlier_per_vertex, fields[i].name);
8366 if (j == -1) {
8367 _mesa_glsl_error(&loc, state,
8368 "redeclaration of gl_PerVertex must be a subset "
8369 "of the built-in members of gl_PerVertex");
8370 } else {
8371 fields[i].location =
8372 earlier_per_vertex->fields.structure[j].location;
8373 fields[i].offset =
8374 earlier_per_vertex->fields.structure[j].offset;
8375 fields[i].interpolation =
8376 earlier_per_vertex->fields.structure[j].interpolation;
8377 fields[i].centroid =
8378 earlier_per_vertex->fields.structure[j].centroid;
8379 fields[i].sample =
8380 earlier_per_vertex->fields.structure[j].sample;
8381 fields[i].patch =
8382 earlier_per_vertex->fields.structure[j].patch;
8383 fields[i].precision =
8384 earlier_per_vertex->fields.structure[j].precision;
8385 fields[i].explicit_xfb_buffer =
8386 earlier_per_vertex->fields.structure[j].explicit_xfb_buffer;
8387 fields[i].xfb_buffer =
8388 earlier_per_vertex->fields.structure[j].xfb_buffer;
8389 fields[i].xfb_stride =
8390 earlier_per_vertex->fields.structure[j].xfb_stride;
8391 }
8392 }
8393
8394 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10
8395 * spec:
8396 *
8397 * If a built-in interface block is redeclared, it must appear in
8398 * the shader before any use of any member included in the built-in
8399 * declaration, or a compilation error will result.
8400 *
8401 * This appears to be a clarification to the behaviour established for
8402 * gl_PerVertex by GLSL 1.50, therefore we implement this behaviour
8403 * regardless of GLSL version.
8404 */
8405 interface_block_usage_visitor v(var_mode, earlier_per_vertex);
8406 v.run(instructions);
8407 if (v.usage_found()) {
8408 _mesa_glsl_error(&loc, state,
8409 "redeclaration of a built-in interface block must "
8410 "appear before any use of any member of the "
8411 "interface block");
8412 }
8413 }
8414
8415 const glsl_type *block_type =
8416 glsl_interface_type(fields,
8417 num_variables,
8418 packing,
8419 matrix_layout ==
8420 GLSL_MATRIX_LAYOUT_ROW_MAJOR,
8421 this->block_name);
8422
8423 unsigned component_size = glsl_contains_double(block_type) ? 8 : 4;
8424 int xfb_offset =
8425 layout.flags.q.explicit_xfb_offset ? (int) qual_xfb_offset : -1;
8426 validate_xfb_offset_qualifier(&loc, state, xfb_offset, block_type,
8427 component_size);
8428
8429 if (!state->symbols->add_interface(glsl_get_type_name(block_type), block_type, var_mode)) {
8430 YYLTYPE loc = this->get_location();
8431 _mesa_glsl_error(&loc, state, "interface block `%s' with type `%s' "
8432 "already taken in the current scope",
8433 this->block_name, iface_type_name);
8434 }
8435
8436 /* Since interface blocks cannot contain statements, it should be
8437 * impossible for the block to generate any instructions.
8438 */
8439 assert(declared_variables.is_empty());
8440
8441 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
8442 *
8443 * Geometry shader input variables get the per-vertex values written
8444 * out by vertex shader output variables of the same names. Since a
8445 * geometry shader operates on a set of vertices, each input varying
8446 * variable (or input block, see interface blocks below) needs to be
8447 * declared as an array.
8448 */
8449 if (state->stage == MESA_SHADER_GEOMETRY && this->array_specifier == NULL &&
8450 var_mode == ir_var_shader_in) {
8451 _mesa_glsl_error(&loc, state, "geometry shader inputs must be arrays");
8452 } else if ((state->stage == MESA_SHADER_TESS_CTRL ||
8453 state->stage == MESA_SHADER_TESS_EVAL) &&
8454 !this->layout.flags.q.patch &&
8455 this->array_specifier == NULL &&
8456 var_mode == ir_var_shader_in) {
8457 _mesa_glsl_error(&loc, state, "per-vertex tessellation shader inputs must be arrays");
8458 } else if (state->stage == MESA_SHADER_TESS_CTRL &&
8459 !this->layout.flags.q.patch &&
8460 this->array_specifier == NULL &&
8461 var_mode == ir_var_shader_out) {
8462 _mesa_glsl_error(&loc, state, "tessellation control shader outputs must be arrays");
8463 }
8464
8465
8466 /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
8467 * says:
8468 *
8469 * "If an instance name (instance-name) is used, then it puts all the
8470 * members inside a scope within its own name space, accessed with the
8471 * field selector ( . ) operator (analogously to structures)."
8472 */
8473 if (this->instance_name) {
8474 if (redeclaring_per_vertex) {
8475 /* When a built-in in an unnamed interface block is redeclared,
8476 * get_variable_being_redeclared() calls
8477 * check_builtin_array_max_size() to make sure that built-in array
8478 * variables aren't redeclared to illegal sizes. But we're looking
8479 * at a redeclaration of a named built-in interface block. So we
8480 * have to manually call check_builtin_array_max_size() for all parts
8481 * of the interface that are arrays.
8482 */
8483 for (unsigned i = 0; i < num_variables; i++) {
8484 if (glsl_type_is_array(fields[i].type)) {
8485 const unsigned size = glsl_array_size(fields[i].type);
8486 check_builtin_array_max_size(fields[i].name, size, loc, state);
8487 }
8488 }
8489 } else {
8490 validate_identifier(this->instance_name, loc, state);
8491 }
8492
8493 ir_variable *var;
8494
8495 if (this->array_specifier != NULL) {
8496 const glsl_type *block_array_type =
8497 process_array_type(&loc, block_type, this->array_specifier, state);
8498
8499 /* From Section 4.4.1 (Input Layout Qualifiers) of the GLSL 4.50 spec:
8500 *
8501 * "For some blocks declared as arrays, the location can only be applied
8502 * at the block level: When a block is declared as an array where
8503 * additional locations are needed for each member for each block array
8504 * element, it is a compile-time error to specify locations on the block
8505 * members. That is, when locations would be under specified by applying
8506 * them on block members, they are not allowed on block members. For
8507 * arrayed interfaces (those generally having an extra level of
8508 * arrayness due to interface expansion), the outer array is stripped
8509 * before applying this rule"
8510 *
8511 * From 4.4.1 (Input Layout Qualifiers) and
8512 * 4.4.2 (Output Layout Qualifiers) of GLSL ES 3.20
8513 *
8514 * "If an input is declared as an array of blocks, excluding
8515 * per-vertex-arrays as required for tessellation, it is an error
8516 * to declare a member of the block with a location qualifier."
8517 *
8518 * "If an output is declared as an array of blocks, excluding
8519 * per-vertex-arrays as required for tessellation, it is an error
8520 * to declare a member of the block with a location qualifier."
8521 */
8522 if (!redeclaring_per_vertex &&
8523 (state->has_enhanced_layouts() || state->has_shader_io_blocks())) {
8524 bool allow_location;
8525 switch (state->stage)
8526 {
8527 case MESA_SHADER_TESS_CTRL:
8528 allow_location = this->array_specifier->is_single_dimension();
8529 break;
8530 case MESA_SHADER_TESS_EVAL:
8531 case MESA_SHADER_GEOMETRY:
8532 allow_location = (this->array_specifier->is_single_dimension()
8533 && var_mode == ir_var_shader_in);
8534 break;
8535 default:
8536 allow_location = false;
8537 break;
8538 }
8539
8540 if (!allow_location) {
8541 for (unsigned i = 0; i < num_variables; i++) {
8542 if (fields[i].location != -1) {
8543 _mesa_glsl_error(&loc, state,
8544 "explicit member locations are not allowed in "
8545 "blocks declared as arrays %s shader",
8546 _mesa_shader_stage_to_string(state->stage));
8547 }
8548 }
8549 }
8550 }
8551
8552 /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
8553 *
8554 * For uniform blocks declared an array, each individual array
8555 * element corresponds to a separate buffer object backing one
8556 * instance of the block. As the array size indicates the number
8557 * of buffer objects needed, uniform block array declarations
8558 * must specify an array size.
8559 *
8560 * And a few paragraphs later:
8561 *
8562 * Geometry shader input blocks must be declared as arrays and
8563 * follow the array declaration and linking rules for all
8564 * geometry shader inputs. All other input and output block
8565 * arrays must specify an array size.
8566 *
8567 * The same applies to tessellation shaders.
8568 *
8569 * The upshot of this is that the only circumstance where an
8570 * interface array size *doesn't* need to be specified is on a
8571 * geometry shader input, tessellation control shader input,
8572 * tessellation control shader output, and tessellation evaluation
8573 * shader input.
8574 */
8575 if (glsl_type_is_unsized_array(block_array_type)) {
8576 bool allow_inputs = state->stage == MESA_SHADER_GEOMETRY ||
8577 state->stage == MESA_SHADER_TESS_CTRL ||
8578 state->stage == MESA_SHADER_TESS_EVAL;
8579 bool allow_outputs = state->stage == MESA_SHADER_TESS_CTRL;
8580
8581 if (this->layout.flags.q.in) {
8582 if (!allow_inputs)
8583 _mesa_glsl_error(&loc, state,
8584 "unsized input block arrays not allowed in "
8585 "%s shader",
8586 _mesa_shader_stage_to_string(state->stage));
8587 } else if (this->layout.flags.q.out) {
8588 if (!allow_outputs)
8589 _mesa_glsl_error(&loc, state,
8590 "unsized output block arrays not allowed in "
8591 "%s shader",
8592 _mesa_shader_stage_to_string(state->stage));
8593 } else {
8594 /* by elimination, this is a uniform block array */
8595 _mesa_glsl_error(&loc, state,
8596 "unsized uniform block arrays not allowed in "
8597 "%s shader",
8598 _mesa_shader_stage_to_string(state->stage));
8599 }
8600 }
8601
8602 /* From section 4.3.9 (Interface Blocks) of the GLSL ES 3.10 spec:
8603 *
8604 * * Arrays of arrays of blocks are not allowed
8605 */
8606 if (state->es_shader && glsl_type_is_array(block_array_type) &&
8607 glsl_type_is_array(block_array_type->fields.array)) {
8608 _mesa_glsl_error(&loc, state,
8609 "arrays of arrays interface blocks are "
8610 "not allowed");
8611 }
8612
8613 var = new(state) ir_variable(block_array_type,
8614 this->instance_name,
8615 var_mode);
8616 } else {
8617 var = new(state) ir_variable(block_type,
8618 this->instance_name,
8619 var_mode);
8620 }
8621
8622 var->data.matrix_layout = matrix_layout == GLSL_MATRIX_LAYOUT_INHERITED
8623 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR : matrix_layout;
8624
8625 if (var_mode == ir_var_shader_in || var_mode == ir_var_uniform)
8626 var->data.read_only = true;
8627
8628 var->data.patch = this->layout.flags.q.patch;
8629
8630 if (state->stage == MESA_SHADER_GEOMETRY && var_mode == ir_var_shader_in)
8631 handle_geometry_shader_input_decl(state, loc, var);
8632 else if ((state->stage == MESA_SHADER_TESS_CTRL ||
8633 state->stage == MESA_SHADER_TESS_EVAL) && var_mode == ir_var_shader_in)
8634 handle_tess_shader_input_decl(state, loc, var);
8635 else if (state->stage == MESA_SHADER_TESS_CTRL && var_mode == ir_var_shader_out)
8636 handle_tess_ctrl_shader_output_decl(state, loc, var);
8637
8638 for (unsigned i = 0; i < num_variables; i++) {
8639 if (var->data.mode == ir_var_shader_storage)
8640 apply_memory_qualifiers(var, fields[i]);
8641 }
8642
8643 if (ir_variable *earlier =
8644 state->symbols->get_variable(this->instance_name)) {
8645 if (!redeclaring_per_vertex) {
8646 _mesa_glsl_error(&loc, state, "`%s' redeclared",
8647 this->instance_name);
8648 }
8649 earlier->data.how_declared = ir_var_declared_normally;
8650 earlier->type = var->type;
8651 earlier->reinit_interface_type(block_type);
8652 delete var;
8653 } else {
8654 if (this->layout.flags.q.explicit_binding) {
8655 apply_explicit_binding(state, &loc, var, var->type,
8656 &this->layout);
8657 }
8658
8659 var->data.stream = qual_stream;
8660 if (layout.flags.q.explicit_location) {
8661 var->data.location = expl_location;
8662 var->data.explicit_location = true;
8663 }
8664
8665 state->symbols->add_variable(var);
8666 instructions->push_tail(var);
8667 }
8668 } else {
8669 /* In order to have an array size, the block must also be declared with
8670 * an instance name.
8671 */
8672 assert(this->array_specifier == NULL);
8673
8674 for (unsigned i = 0; i < num_variables; i++) {
8675 ir_variable *var =
8676 new(state) ir_variable(fields[i].type,
8677 ralloc_strdup(state, fields[i].name),
8678 var_mode);
8679 var->data.interpolation = fields[i].interpolation;
8680 var->data.centroid = fields[i].centroid;
8681 var->data.sample = fields[i].sample;
8682 var->data.patch = fields[i].patch;
8683 var->data.stream = qual_stream;
8684 var->data.location = fields[i].location;
8685
8686 if (fields[i].location != -1)
8687 var->data.explicit_location = true;
8688
8689 var->data.explicit_xfb_buffer = fields[i].explicit_xfb_buffer;
8690 var->data.xfb_buffer = fields[i].xfb_buffer;
8691
8692 if (fields[i].offset != -1)
8693 var->data.explicit_xfb_offset = true;
8694 var->data.offset = fields[i].offset;
8695
8696 var->init_interface_type(block_type);
8697
8698 if (var_mode == ir_var_shader_in || var_mode == ir_var_uniform)
8699 var->data.read_only = true;
8700
8701 /* Precision qualifiers do not have any meaning in Desktop GLSL */
8702 if (state->es_shader) {
8703 var->data.precision =
8704 select_gles_precision(fields[i].precision, fields[i].type,
8705 state, &loc);
8706 }
8707
8708 if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_INHERITED) {
8709 var->data.matrix_layout = matrix_layout == GLSL_MATRIX_LAYOUT_INHERITED
8710 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR : matrix_layout;
8711 } else {
8712 var->data.matrix_layout = fields[i].matrix_layout;
8713 }
8714
8715 if (var->data.mode == ir_var_shader_storage)
8716 apply_memory_qualifiers(var, fields[i]);
8717
8718 /* Examine var name here since var may get deleted in the next call */
8719 bool var_is_gl_id = is_gl_identifier(var->name);
8720
8721 if (redeclaring_per_vertex) {
8722 bool is_redeclaration;
8723 var =
8724 get_variable_being_redeclared(&var, loc, state,
8725 true /* allow_all_redeclarations */,
8726 &is_redeclaration);
8727 if (!var_is_gl_id || !is_redeclaration) {
8728 _mesa_glsl_error(&loc, state,
8729 "redeclaration of gl_PerVertex can only "
8730 "include built-in variables");
8731 } else if (var->data.how_declared == ir_var_declared_normally) {
8732 _mesa_glsl_error(&loc, state,
8733 "`%s' has already been redeclared",
8734 var->name);
8735 } else {
8736 var->data.how_declared = ir_var_declared_in_block;
8737 var->reinit_interface_type(block_type);
8738 }
8739 continue;
8740 }
8741
8742 if (state->symbols->get_variable(var->name) != NULL)
8743 _mesa_glsl_error(&loc, state, "`%s' redeclared", var->name);
8744
8745 /* Propagate the "binding" keyword into this UBO/SSBO's fields.
8746 * The UBO declaration itself doesn't get an ir_variable unless it
8747 * has an instance name. This is ugly.
8748 */
8749 if (this->layout.flags.q.explicit_binding) {
8750 apply_explicit_binding(state, &loc, var,
8751 var->get_interface_type(), &this->layout);
8752 }
8753
8754 if (glsl_type_is_unsized_array(var->type)) {
8755 if (var->is_in_shader_storage_block() &&
8756 is_unsized_array_last_element(var)) {
8757 var->data.from_ssbo_unsized_array = true;
8758 } else {
8759 /* From GLSL ES 3.10 spec, section 4.1.9 "Arrays":
8760 *
8761 * "If an array is declared as the last member of a shader storage
8762 * block and the size is not specified at compile-time, it is
8763 * sized at run-time. In all other cases, arrays are sized only
8764 * at compile-time."
8765 *
8766 * In desktop GLSL it is allowed to have unsized-arrays that are
8767 * not last, as long as we can determine that they are implicitly
8768 * sized.
8769 */
8770 if (state->es_shader) {
8771 _mesa_glsl_error(&loc, state, "unsized array `%s' "
8772 "definition: only last member of a shader "
8773 "storage block can be defined as unsized "
8774 "array", fields[i].name);
8775 }
8776 }
8777 }
8778
8779 state->symbols->add_variable(var);
8780 instructions->push_tail(var);
8781 }
8782
8783 if (redeclaring_per_vertex && block_type != earlier_per_vertex) {
8784 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec:
8785 *
8786 * It is also a compilation error ... to redeclare a built-in
8787 * block and then use a member from that built-in block that was
8788 * not included in the redeclaration.
8789 *
8790 * This appears to be a clarification to the behaviour established
8791 * for gl_PerVertex by GLSL 1.50, therefore we implement this
8792 * behaviour regardless of GLSL version.
8793 *
8794 * To prevent the shader from using a member that was not included in
8795 * the redeclaration, we disable any ir_variables that are still
8796 * associated with the old declaration of gl_PerVertex (since we've
8797 * already updated all of the variables contained in the new
8798 * gl_PerVertex to point to it).
8799 *
8800 * As a side effect this will prevent
8801 * validate_intrastage_interface_blocks() from getting confused and
8802 * thinking there are conflicting definitions of gl_PerVertex in the
8803 * shader.
8804 */
8805 foreach_in_list_safe(ir_instruction, node, instructions) {
8806 ir_variable *const var = node->as_variable();
8807 if (var != NULL &&
8808 var->get_interface_type() == earlier_per_vertex &&
8809 var->data.mode == var_mode) {
8810 if (var->data.how_declared == ir_var_declared_normally) {
8811 _mesa_glsl_error(&loc, state,
8812 "redeclaration of gl_PerVertex cannot "
8813 "follow a redeclaration of `%s'",
8814 var->name);
8815 }
8816 state->symbols->disable_variable(var->name);
8817 var->remove();
8818 }
8819 }
8820 }
8821 }
8822
8823 return NULL;
8824 }
8825
8826
8827 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)8828 ast_tcs_output_layout::hir(exec_list *instructions,
8829 struct _mesa_glsl_parse_state *state)
8830 {
8831 YYLTYPE loc = this->get_location();
8832
8833 unsigned num_vertices;
8834 if (!state->out_qualifier->vertices->
8835 process_qualifier_constant(state, "vertices", &num_vertices,
8836 false)) {
8837 /* return here to stop cascading incorrect error messages */
8838 return NULL;
8839 }
8840
8841 /* If any shader outputs occurred before this declaration and specified an
8842 * array size, make sure the size they specified is consistent with the
8843 * primitive type.
8844 */
8845 if (state->tcs_output_size != 0 && state->tcs_output_size != num_vertices) {
8846 _mesa_glsl_error(&loc, state,
8847 "this tessellation control shader output layout "
8848 "specifies %u vertices, but a previous output "
8849 "is declared with size %u",
8850 num_vertices, state->tcs_output_size);
8851 return NULL;
8852 }
8853
8854 state->tcs_output_vertices_specified = true;
8855
8856 /* If any shader outputs occurred before this declaration and did not
8857 * specify an array size, their size is determined now.
8858 */
8859 foreach_in_list (ir_instruction, node, instructions) {
8860 ir_variable *var = node->as_variable();
8861 if (var == NULL || var->data.mode != ir_var_shader_out)
8862 continue;
8863
8864 /* Note: Not all tessellation control shader output are arrays. */
8865 if (!glsl_type_is_unsized_array(var->type) || var->data.patch)
8866 continue;
8867
8868 if (var->data.max_array_access >= (int)num_vertices) {
8869 _mesa_glsl_error(&loc, state,
8870 "this tessellation control shader output layout "
8871 "specifies %u vertices, but an access to element "
8872 "%u of output `%s' already exists", num_vertices,
8873 var->data.max_array_access, var->name);
8874 } else {
8875 var->type = glsl_array_type(var->type->fields.array,
8876 num_vertices, 0);
8877 }
8878 }
8879
8880 return NULL;
8881 }
8882
8883
8884 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)8885 ast_gs_input_layout::hir(exec_list *instructions,
8886 struct _mesa_glsl_parse_state *state)
8887 {
8888 YYLTYPE loc = this->get_location();
8889
8890 /* Should have been prevented by the parser. */
8891 assert(!state->gs_input_prim_type_specified
8892 || state->in_qualifier->prim_type == this->prim_type);
8893
8894 /* If any shader inputs occurred before this declaration and specified an
8895 * array size, make sure the size they specified is consistent with the
8896 * primitive type.
8897 */
8898 unsigned num_vertices =
8899 mesa_vertices_per_prim(gl_to_mesa_prim(this->prim_type));
8900 if (state->gs_input_size != 0 && state->gs_input_size != num_vertices) {
8901 _mesa_glsl_error(&loc, state,
8902 "this geometry shader input layout implies %u vertices"
8903 " per primitive, but a previous input is declared"
8904 " with size %u", num_vertices, state->gs_input_size);
8905 return NULL;
8906 }
8907
8908 state->gs_input_prim_type_specified = true;
8909
8910 /* If any shader inputs occurred before this declaration and did not
8911 * specify an array size, their size is determined now.
8912 */
8913 foreach_in_list(ir_instruction, node, instructions) {
8914 ir_variable *var = node->as_variable();
8915 if (var == NULL || var->data.mode != ir_var_shader_in)
8916 continue;
8917
8918 /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
8919 * array; skip it.
8920 */
8921
8922 if (glsl_type_is_unsized_array(var->type)) {
8923 if (var->data.max_array_access >= (int)num_vertices) {
8924 _mesa_glsl_error(&loc, state,
8925 "this geometry shader input layout implies %u"
8926 " vertices, but an access to element %u of input"
8927 " `%s' already exists", num_vertices,
8928 var->data.max_array_access, var->name);
8929 } else {
8930 var->type = glsl_array_type(var->type->fields.array,
8931 num_vertices, 0);
8932 }
8933 }
8934 }
8935
8936 return NULL;
8937 }
8938
8939
8940 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)8941 ast_cs_input_layout::hir(exec_list *instructions,
8942 struct _mesa_glsl_parse_state *state)
8943 {
8944 YYLTYPE loc = this->get_location();
8945
8946 /* From the ARB_compute_shader specification:
8947 *
8948 * If the local size of the shader in any dimension is greater
8949 * than the maximum size supported by the implementation for that
8950 * dimension, a compile-time error results.
8951 *
8952 * It is not clear from the spec how the error should be reported if
8953 * the total size of the work group exceeds
8954 * MAX_COMPUTE_WORK_GROUP_INVOCATIONS, but it seems reasonable to
8955 * report it at compile time as well.
8956 */
8957 GLuint64 total_invocations = 1;
8958 unsigned qual_local_size[3];
8959 for (int i = 0; i < 3; i++) {
8960
8961 char *local_size_str = ralloc_asprintf(NULL, "invalid local_size_%c",
8962 'x' + i);
8963 /* Infer a local_size of 1 for unspecified dimensions */
8964 if (this->local_size[i] == NULL) {
8965 qual_local_size[i] = 1;
8966 } else if (!this->local_size[i]->
8967 process_qualifier_constant(state, local_size_str,
8968 &qual_local_size[i], false)) {
8969 ralloc_free(local_size_str);
8970 return NULL;
8971 }
8972 ralloc_free(local_size_str);
8973
8974 if (qual_local_size[i] > state->consts->MaxComputeWorkGroupSize[i]) {
8975 _mesa_glsl_error(&loc, state,
8976 "local_size_%c exceeds MAX_COMPUTE_WORK_GROUP_SIZE"
8977 " (%d)", 'x' + i,
8978 state->consts->MaxComputeWorkGroupSize[i]);
8979 break;
8980 }
8981 total_invocations *= qual_local_size[i];
8982 if (total_invocations >
8983 state->consts->MaxComputeWorkGroupInvocations) {
8984 _mesa_glsl_error(&loc, state,
8985 "product of local_sizes exceeds "
8986 "MAX_COMPUTE_WORK_GROUP_INVOCATIONS (%d)",
8987 state->consts->MaxComputeWorkGroupInvocations);
8988 break;
8989 }
8990 }
8991
8992 /* If any compute input layout declaration preceded this one, make sure it
8993 * was consistent with this one.
8994 */
8995 if (state->cs_input_local_size_specified) {
8996 for (int i = 0; i < 3; i++) {
8997 if (state->cs_input_local_size[i] != qual_local_size[i]) {
8998 _mesa_glsl_error(&loc, state,
8999 "compute shader input layout does not match"
9000 " previous declaration");
9001 return NULL;
9002 }
9003 }
9004 }
9005
9006 /* The ARB_compute_variable_group_size spec says:
9007 *
9008 * If a compute shader including a *local_size_variable* qualifier also
9009 * declares a fixed local group size using the *local_size_x*,
9010 * *local_size_y*, or *local_size_z* qualifiers, a compile-time error
9011 * results
9012 */
9013 if (state->cs_input_local_size_variable_specified) {
9014 _mesa_glsl_error(&loc, state,
9015 "compute shader can't include both a variable and a "
9016 "fixed local group size");
9017 return NULL;
9018 }
9019
9020 state->cs_input_local_size_specified = true;
9021 for (int i = 0; i < 3; i++)
9022 state->cs_input_local_size[i] = qual_local_size[i];
9023
9024 /* We may now declare the built-in constant gl_WorkGroupSize (see
9025 * builtin_variable_generator::generate_constants() for why we didn't
9026 * declare it earlier).
9027 */
9028 ir_variable *var = new(state->symbols)
9029 ir_variable(&glsl_type_builtin_uvec3, "gl_WorkGroupSize", ir_var_auto);
9030 var->data.how_declared = ir_var_declared_implicitly;
9031 var->data.read_only = true;
9032 instructions->push_tail(var);
9033 state->symbols->add_variable(var);
9034 ir_constant_data data;
9035 memset(&data, 0, sizeof(data));
9036 for (int i = 0; i < 3; i++)
9037 data.u[i] = qual_local_size[i];
9038 var->constant_value = new(var) ir_constant(&glsl_type_builtin_uvec3, &data);
9039 var->constant_initializer =
9040 new(var) ir_constant(&glsl_type_builtin_uvec3, &data);
9041 var->data.has_initializer = true;
9042 var->data.is_implicit_initializer = false;
9043
9044 return NULL;
9045 }
9046
9047
9048 static void
detect_conflicting_assignments(struct _mesa_glsl_parse_state * state,exec_list * instructions)9049 detect_conflicting_assignments(struct _mesa_glsl_parse_state *state,
9050 exec_list *instructions)
9051 {
9052 bool gl_FragColor_assigned = false;
9053 bool gl_FragData_assigned = false;
9054 bool gl_FragSecondaryColor_assigned = false;
9055 bool gl_FragSecondaryData_assigned = false;
9056 bool user_defined_fs_output_assigned = false;
9057 ir_variable *user_defined_fs_output = NULL;
9058
9059 /* It would be nice to have proper location information. */
9060 YYLTYPE loc;
9061 memset(&loc, 0, sizeof(loc));
9062
9063 foreach_in_list(ir_instruction, node, instructions) {
9064 ir_variable *var = node->as_variable();
9065
9066 if (!var || !var->data.assigned)
9067 continue;
9068
9069 if (strcmp(var->name, "gl_FragColor") == 0) {
9070 gl_FragColor_assigned = true;
9071 if (!var->constant_initializer && state->zero_init) {
9072 const ir_constant_data data = { { 0 } };
9073 var->data.has_initializer = true;
9074 var->data.is_implicit_initializer = true;
9075 var->constant_initializer = new(var) ir_constant(var->type, &data);
9076 }
9077 }
9078 else if (strcmp(var->name, "gl_FragData") == 0)
9079 gl_FragData_assigned = true;
9080 else if (strcmp(var->name, "gl_SecondaryFragColorEXT") == 0)
9081 gl_FragSecondaryColor_assigned = true;
9082 else if (strcmp(var->name, "gl_SecondaryFragDataEXT") == 0)
9083 gl_FragSecondaryData_assigned = true;
9084 else if (!is_gl_identifier(var->name)) {
9085 if (state->stage == MESA_SHADER_FRAGMENT &&
9086 var->data.mode == ir_var_shader_out) {
9087 user_defined_fs_output_assigned = true;
9088 user_defined_fs_output = var;
9089 }
9090 }
9091 }
9092
9093 /* From the GLSL 1.30 spec:
9094 *
9095 * "If a shader statically assigns a value to gl_FragColor, it
9096 * may not assign a value to any element of gl_FragData. If a
9097 * shader statically writes a value to any element of
9098 * gl_FragData, it may not assign a value to
9099 * gl_FragColor. That is, a shader may assign values to either
9100 * gl_FragColor or gl_FragData, but not both. Multiple shaders
9101 * linked together must also consistently write just one of
9102 * these variables. Similarly, if user declared output
9103 * variables are in use (statically assigned to), then the
9104 * built-in variables gl_FragColor and gl_FragData may not be
9105 * assigned to. These incorrect usages all generate compile
9106 * time errors."
9107 */
9108 if (gl_FragColor_assigned && gl_FragData_assigned) {
9109 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
9110 "`gl_FragColor' and `gl_FragData'");
9111 } else if (gl_FragColor_assigned && user_defined_fs_output_assigned) {
9112 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
9113 "`gl_FragColor' and `%s'",
9114 user_defined_fs_output->name);
9115 } else if (gl_FragSecondaryColor_assigned && gl_FragSecondaryData_assigned) {
9116 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
9117 "`gl_FragSecondaryColorEXT' and"
9118 " `gl_FragSecondaryDataEXT'");
9119 } else if (gl_FragColor_assigned && gl_FragSecondaryData_assigned) {
9120 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
9121 "`gl_FragColor' and"
9122 " `gl_FragSecondaryDataEXT'");
9123 } else if (gl_FragData_assigned && gl_FragSecondaryColor_assigned) {
9124 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
9125 "`gl_FragData' and"
9126 " `gl_FragSecondaryColorEXT'");
9127 } else if (gl_FragData_assigned && user_defined_fs_output_assigned) {
9128 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
9129 "`gl_FragData' and `%s'",
9130 user_defined_fs_output->name);
9131 }
9132
9133 if ((gl_FragSecondaryColor_assigned || gl_FragSecondaryData_assigned) &&
9134 !state->EXT_blend_func_extended_enable) {
9135 _mesa_glsl_error(&loc, state,
9136 "Dual source blending requires EXT_blend_func_extended");
9137 }
9138 }
9139
9140 static void
verify_subroutine_associated_funcs(struct _mesa_glsl_parse_state * state)9141 verify_subroutine_associated_funcs(struct _mesa_glsl_parse_state *state)
9142 {
9143 YYLTYPE loc;
9144 memset(&loc, 0, sizeof(loc));
9145
9146 /* Section 6.1.2 (Subroutines) of the GLSL 4.00 spec says:
9147 *
9148 * "A program will fail to compile or link if any shader
9149 * or stage contains two or more functions with the same
9150 * name if the name is associated with a subroutine type."
9151 */
9152
9153 for (int i = 0; i < state->num_subroutines; i++) {
9154 unsigned definitions = 0;
9155 ir_function *fn = state->subroutines[i];
9156 /* Calculate number of function definitions with the same name */
9157 foreach_in_list(ir_function_signature, sig, &fn->signatures) {
9158 if (sig->is_defined) {
9159 if (++definitions > 1) {
9160 _mesa_glsl_error(&loc, state,
9161 "%s shader contains two or more function "
9162 "definitions with name `%s', which is "
9163 "associated with a subroutine type.\n",
9164 _mesa_shader_stage_to_string(state->stage),
9165 fn->name);
9166 return;
9167 }
9168 }
9169 }
9170 }
9171 }
9172
9173 static void
remove_per_vertex_blocks(exec_list * instructions,_mesa_glsl_parse_state * state,ir_variable_mode mode)9174 remove_per_vertex_blocks(exec_list *instructions,
9175 _mesa_glsl_parse_state *state, ir_variable_mode mode)
9176 {
9177 /* Find the gl_PerVertex interface block of the appropriate (in/out) mode,
9178 * if it exists in this shader type.
9179 */
9180 const glsl_type *per_vertex = NULL;
9181 switch (mode) {
9182 case ir_var_shader_in:
9183 if (ir_variable *gl_in = state->symbols->get_variable("gl_in"))
9184 per_vertex = gl_in->get_interface_type();
9185 break;
9186 case ir_var_shader_out:
9187 if (ir_variable *gl_Position =
9188 state->symbols->get_variable("gl_Position")) {
9189 per_vertex = gl_Position->get_interface_type();
9190 }
9191 break;
9192 default:
9193 assert(!"Unexpected mode");
9194 break;
9195 }
9196
9197 /* If we didn't find a built-in gl_PerVertex interface block, then we don't
9198 * need to do anything.
9199 */
9200 if (per_vertex == NULL)
9201 return;
9202
9203 /* If the interface block is used by the shader, then we don't need to do
9204 * anything.
9205 */
9206 interface_block_usage_visitor v(mode, per_vertex);
9207 v.run(instructions);
9208 if (v.usage_found())
9209 return;
9210
9211 /* Remove any ir_variable declarations that refer to the interface block
9212 * we're removing.
9213 */
9214 foreach_in_list_safe(ir_instruction, node, instructions) {
9215 ir_variable *const var = node->as_variable();
9216 if (var != NULL && var->get_interface_type() == per_vertex &&
9217 var->data.mode == mode) {
9218 state->symbols->disable_variable(var->name);
9219 var->remove();
9220 }
9221 }
9222 }
9223
9224 ir_rvalue *
hir(exec_list *,struct _mesa_glsl_parse_state * state)9225 ast_warnings_toggle::hir(exec_list *,
9226 struct _mesa_glsl_parse_state *state)
9227 {
9228 state->warnings_enabled = enable;
9229 return NULL;
9230 }
9231