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