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 _mesa_glsl_error(loc, state,
4019 "extension GL_AMD_conservative_depth or "
4020 "GL_ARB_conservative_depth must be enabled "
4021 "to use depth layout qualifiers");
4022 } else if (qual->flags.q.depth_type
4023 && strcmp(var->name, "gl_FragDepth") != 0) {
4024 _mesa_glsl_error(loc, state,
4025 "depth layout qualifiers can be applied only to "
4026 "gl_FragDepth");
4027 }
4028
4029 switch (qual->depth_type) {
4030 case ast_depth_any:
4031 var->data.depth_layout = ir_depth_layout_any;
4032 break;
4033 case ast_depth_greater:
4034 var->data.depth_layout = ir_depth_layout_greater;
4035 break;
4036 case ast_depth_less:
4037 var->data.depth_layout = ir_depth_layout_less;
4038 break;
4039 case ast_depth_unchanged:
4040 var->data.depth_layout = ir_depth_layout_unchanged;
4041 break;
4042 default:
4043 var->data.depth_layout = ir_depth_layout_none;
4044 break;
4045 }
4046
4047 if (qual->flags.q.std140 ||
4048 qual->flags.q.std430 ||
4049 qual->flags.q.packed ||
4050 qual->flags.q.shared) {
4051 _mesa_glsl_error(loc, state,
4052 "uniform and shader storage block layout qualifiers "
4053 "std140, std430, packed, and shared can only be "
4054 "applied to uniform or shader storage blocks, not "
4055 "members");
4056 }
4057
4058 if (qual->flags.q.row_major || qual->flags.q.column_major) {
4059 validate_matrix_layout_for_type(state, loc, var->type, var);
4060 }
4061
4062 /* From section 4.4.1.3 of the GLSL 4.50 specification (Fragment Shader
4063 * Inputs):
4064 *
4065 * "Fragment shaders also allow the following layout qualifier on in only
4066 * (not with variable declarations)
4067 * layout-qualifier-id
4068 * early_fragment_tests
4069 * [...]"
4070 */
4071 if (qual->flags.q.early_fragment_tests) {
4072 _mesa_glsl_error(loc, state, "early_fragment_tests layout qualifier only "
4073 "valid in fragment shader input layout declaration.");
4074 }
4075
4076 if (qual->flags.q.inner_coverage) {
4077 _mesa_glsl_error(loc, state, "inner_coverage layout qualifier only "
4078 "valid in fragment shader input layout declaration.");
4079 }
4080
4081 if (qual->flags.q.post_depth_coverage) {
4082 _mesa_glsl_error(loc, state, "post_depth_coverage layout qualifier only "
4083 "valid in fragment shader input layout declaration.");
4084 }
4085
4086 if (state->has_bindless())
4087 apply_bindless_qualifier_to_variable(qual, var, state, loc);
4088
4089 if (qual->flags.q.pixel_interlock_ordered ||
4090 qual->flags.q.pixel_interlock_unordered ||
4091 qual->flags.q.sample_interlock_ordered ||
4092 qual->flags.q.sample_interlock_unordered) {
4093 _mesa_glsl_error(loc, state, "interlock layout qualifiers: "
4094 "pixel_interlock_ordered, pixel_interlock_unordered, "
4095 "sample_interlock_ordered and sample_interlock_unordered, "
4096 "only valid in fragment shader input layout declaration.");
4097 }
4098
4099 if (var->name != NULL && strcmp(var->name, "gl_Layer") == 0) {
4100 if (is_conflicting_layer_redeclaration(state, qual)) {
4101 _mesa_glsl_error(loc, state, "gl_Layer redeclaration with "
4102 "different viewport_relative setting than earlier");
4103 }
4104 state->redeclares_gl_layer = true;
4105 if (qual->flags.q.viewport_relative) {
4106 state->layer_viewport_relative = true;
4107 }
4108 } else if (qual->flags.q.viewport_relative) {
4109 _mesa_glsl_error(loc, state,
4110 "viewport_relative qualifier "
4111 "can only be applied to gl_Layer.");
4112 }
4113 }
4114
4115 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)4116 apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
4117 ir_variable *var,
4118 struct _mesa_glsl_parse_state *state,
4119 YYLTYPE *loc,
4120 bool is_parameter)
4121 {
4122 STATIC_ASSERT(sizeof(qual->flags.q) <= sizeof(qual->flags.i));
4123
4124 if (qual->flags.q.invariant) {
4125 if (var->data.used) {
4126 _mesa_glsl_error(loc, state,
4127 "variable `%s' may not be redeclared "
4128 "`invariant' after being used",
4129 var->name);
4130 } else {
4131 var->data.explicit_invariant = true;
4132 var->data.invariant = true;
4133 }
4134 }
4135
4136 if (qual->flags.q.precise) {
4137 if (var->data.used) {
4138 _mesa_glsl_error(loc, state,
4139 "variable `%s' may not be redeclared "
4140 "`precise' after being used",
4141 var->name);
4142 } else {
4143 var->data.precise = 1;
4144 }
4145 }
4146
4147 if (qual->is_subroutine_decl() && !qual->flags.q.uniform) {
4148 _mesa_glsl_error(loc, state,
4149 "`subroutine' may only be applied to uniforms, "
4150 "subroutine type declarations, or function definitions");
4151 }
4152
4153 if (qual->flags.q.constant || qual->flags.q.attribute
4154 || qual->flags.q.uniform
4155 || (qual->flags.q.varying && (state->stage == MESA_SHADER_FRAGMENT)))
4156 var->data.read_only = 1;
4157
4158 if (qual->flags.q.centroid)
4159 var->data.centroid = 1;
4160
4161 if (qual->flags.q.sample)
4162 var->data.sample = 1;
4163
4164 /* Precision qualifiers do not hold any meaning in Desktop GLSL */
4165 if (state->es_shader) {
4166 var->data.precision =
4167 select_gles_precision(qual->precision, var->type, state, loc);
4168 }
4169
4170 if (qual->flags.q.patch)
4171 var->data.patch = 1;
4172
4173 if (qual->flags.q.attribute && state->stage != MESA_SHADER_VERTEX) {
4174 var->type = &glsl_type_builtin_error;
4175 _mesa_glsl_error(loc, state,
4176 "`attribute' variables may not be declared in the "
4177 "%s shader",
4178 _mesa_shader_stage_to_string(state->stage));
4179 }
4180
4181 /* Disallow layout qualifiers which may only appear on layout declarations. */
4182 if (qual->flags.q.prim_type) {
4183 _mesa_glsl_error(loc, state,
4184 "Primitive type may only be specified on GS input or output "
4185 "layout declaration, not on variables.");
4186 }
4187
4188 /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
4189 *
4190 * "However, the const qualifier cannot be used with out or inout."
4191 *
4192 * The same section of the GLSL 4.40 spec further clarifies this saying:
4193 *
4194 * "The const qualifier cannot be used with out or inout, or a
4195 * compile-time error results."
4196 */
4197 if (is_parameter && qual->flags.q.constant && qual->flags.q.out) {
4198 _mesa_glsl_error(loc, state,
4199 "`const' may not be applied to `out' or `inout' "
4200 "function parameters");
4201 }
4202
4203 /* If there is no qualifier that changes the mode of the variable, leave
4204 * the setting alone.
4205 */
4206 assert(var->data.mode != ir_var_temporary);
4207 if (qual->flags.q.in && qual->flags.q.out)
4208 var->data.mode = is_parameter ? ir_var_function_inout : ir_var_shader_out;
4209 else if (qual->flags.q.in)
4210 var->data.mode = is_parameter ? ir_var_function_in : ir_var_shader_in;
4211 else if (qual->flags.q.attribute
4212 || (qual->flags.q.varying && (state->stage == MESA_SHADER_FRAGMENT)))
4213 var->data.mode = ir_var_shader_in;
4214 else if (qual->flags.q.out)
4215 var->data.mode = is_parameter ? ir_var_function_out : ir_var_shader_out;
4216 else if (qual->flags.q.varying && (state->stage == MESA_SHADER_VERTEX))
4217 var->data.mode = ir_var_shader_out;
4218 else if (qual->flags.q.uniform)
4219 var->data.mode = ir_var_uniform;
4220 else if (qual->flags.q.buffer)
4221 var->data.mode = ir_var_shader_storage;
4222 else if (qual->flags.q.shared_storage)
4223 var->data.mode = ir_var_shader_shared;
4224
4225 if (!is_parameter && state->stage == MESA_SHADER_FRAGMENT) {
4226 if (state->has_framebuffer_fetch()) {
4227 if (state->is_version(130, 300))
4228 var->data.fb_fetch_output = qual->flags.q.in && qual->flags.q.out;
4229 else
4230 var->data.fb_fetch_output = (strcmp(var->name, "gl_LastFragData") == 0);
4231 }
4232
4233 if (state->has_framebuffer_fetch_zs() &&
4234 (strcmp(var->name, "gl_LastFragDepthARM") == 0 ||
4235 strcmp(var->name, "gl_LastFragStencilARM") == 0)) {
4236 var->data.fb_fetch_output = 1;
4237 }
4238 }
4239
4240 if (var->data.fb_fetch_output)
4241 var->data.assigned = true;
4242
4243 if (var->is_fb_fetch_color_output()) {
4244 var->data.memory_coherent = !qual->flags.q.non_coherent;
4245
4246 /* From the EXT_shader_framebuffer_fetch spec:
4247 *
4248 * "It is an error to declare an inout fragment output not qualified
4249 * with layout(noncoherent) if the GL_EXT_shader_framebuffer_fetch
4250 * extension hasn't been enabled."
4251 */
4252 if (var->data.memory_coherent &&
4253 !state->EXT_shader_framebuffer_fetch_enable)
4254 _mesa_glsl_error(loc, state,
4255 "invalid declaration of framebuffer fetch output not "
4256 "qualified with layout(noncoherent)");
4257
4258 } else {
4259 /* From the EXT_shader_framebuffer_fetch spec:
4260 *
4261 * "Fragment outputs declared inout may specify the following layout
4262 * qualifier: [...] noncoherent"
4263 */
4264 if (qual->flags.q.non_coherent)
4265 _mesa_glsl_error(loc, state,
4266 "invalid layout(noncoherent) qualifier not part of "
4267 "framebuffer fetch output declaration");
4268 }
4269
4270 if (!is_parameter && is_varying_var(var, state->stage)) {
4271 /* User-defined ins/outs are not permitted in compute shaders. */
4272 if (state->stage == MESA_SHADER_COMPUTE) {
4273 _mesa_glsl_error(loc, state,
4274 "user-defined input and output variables are not "
4275 "permitted in compute shaders");
4276 }
4277
4278 /* This variable is being used to link data between shader stages (in
4279 * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
4280 * that is allowed for such purposes.
4281 *
4282 * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
4283 *
4284 * "The varying qualifier can be used only with the data types
4285 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
4286 * these."
4287 *
4288 * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
4289 * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
4290 *
4291 * "Fragment inputs can only be signed and unsigned integers and
4292 * integer vectors, float, floating-point vectors, matrices, or
4293 * arrays of these. Structures cannot be input.
4294 *
4295 * Similar text exists in the section on vertex shader outputs.
4296 *
4297 * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
4298 * 3.00 spec allows structs as well. Varying structs are also allowed
4299 * in GLSL 1.50.
4300 *
4301 * From section 4.3.4 of the ARB_bindless_texture spec:
4302 *
4303 * "(modify third paragraph of the section to allow sampler and image
4304 * types) ... Vertex shader inputs can only be float,
4305 * single-precision floating-point scalars, single-precision
4306 * floating-point vectors, matrices, signed and unsigned integers
4307 * and integer vectors, sampler and image types."
4308 *
4309 * From section 4.3.6 of the ARB_bindless_texture spec:
4310 *
4311 * "Output variables can only be floating-point scalars,
4312 * floating-point vectors, matrices, signed or unsigned integers or
4313 * integer vectors, sampler or image types, or arrays or structures
4314 * of any these."
4315 */
4316 switch (glsl_without_array(var->type)->base_type) {
4317 case GLSL_TYPE_FLOAT:
4318 /* Ok in all GLSL versions */
4319 break;
4320 case GLSL_TYPE_FLOAT16:
4321 if (state->AMD_gpu_shader_half_float_enable)
4322 break;
4323 _mesa_glsl_error(loc, state, "illegal type for a varying variable");
4324 break;
4325 case GLSL_TYPE_UINT:
4326 case GLSL_TYPE_INT:
4327 if (state->is_version(130, 300) || state->EXT_gpu_shader4_enable)
4328 break;
4329 _mesa_glsl_error(loc, state,
4330 "varying variables must be of base type float in %s",
4331 state->get_version_string());
4332 break;
4333 case GLSL_TYPE_STRUCT:
4334 if (state->is_version(150, 300))
4335 break;
4336 _mesa_glsl_error(loc, state,
4337 "varying variables may not be of type struct");
4338 break;
4339 case GLSL_TYPE_DOUBLE:
4340 case GLSL_TYPE_UINT64:
4341 case GLSL_TYPE_INT64:
4342 break;
4343 case GLSL_TYPE_SAMPLER:
4344 case GLSL_TYPE_TEXTURE:
4345 case GLSL_TYPE_IMAGE:
4346 if (state->has_bindless())
4347 break;
4348 FALLTHROUGH;
4349 default:
4350 _mesa_glsl_error(loc, state, "illegal type for a varying variable");
4351 break;
4352 }
4353 }
4354
4355 if (state->all_invariant && var->data.mode == ir_var_shader_out) {
4356 var->data.explicit_invariant = true;
4357 var->data.invariant = true;
4358 }
4359
4360 var->data.interpolation =
4361 interpret_interpolation_qualifier(qual, var->type,
4362 (ir_variable_mode) var->data.mode,
4363 state, loc);
4364
4365 /* Does the declaration use the deprecated 'attribute' or 'varying'
4366 * keywords?
4367 */
4368 const bool uses_deprecated_qualifier = qual->flags.q.attribute
4369 || qual->flags.q.varying;
4370
4371
4372 /* Validate auxiliary storage qualifiers */
4373
4374 /* From section 4.3.4 of the GLSL 1.30 spec:
4375 * "It is an error to use centroid in in a vertex shader."
4376 *
4377 * From section 4.3.4 of the GLSL ES 3.00 spec:
4378 * "It is an error to use centroid in or interpolation qualifiers in
4379 * a vertex shader input."
4380 */
4381
4382 /* Section 4.3.6 of the GLSL 1.30 specification states:
4383 * "It is an error to use centroid out in a fragment shader."
4384 *
4385 * The GL_ARB_shading_language_420pack extension specification states:
4386 * "It is an error to use auxiliary storage qualifiers or interpolation
4387 * qualifiers on an output in a fragment shader."
4388 */
4389 if (qual->flags.q.sample && (!is_varying_var(var, state->stage) || uses_deprecated_qualifier)) {
4390 _mesa_glsl_error(loc, state,
4391 "sample qualifier may only be used on `in` or `out` "
4392 "variables between shader stages");
4393 }
4394 if (qual->flags.q.centroid && !is_varying_var(var, state->stage)) {
4395 _mesa_glsl_error(loc, state,
4396 "centroid qualifier may only be used with `in', "
4397 "`out' or `varying' variables between shader stages");
4398 }
4399
4400 if (qual->flags.q.shared_storage && state->stage != MESA_SHADER_COMPUTE) {
4401 _mesa_glsl_error(loc, state,
4402 "the shared storage qualifiers can only be used with "
4403 "compute shaders");
4404 }
4405
4406 apply_image_qualifier_to_variable(qual, var, state, loc);
4407 }
4408
4409 /**
4410 * Get the variable that is being redeclared by this declaration or if it
4411 * does not exist, the current declared variable.
4412 *
4413 * Semantic checks to verify the validity of the redeclaration are also
4414 * performed. If semantic checks fail, compilation error will be emitted via
4415 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
4416 *
4417 * \returns
4418 * A pointer to an existing variable in the current scope if the declaration
4419 * is a redeclaration, current variable otherwise. \c is_declared boolean
4420 * will return \c true if the declaration is a redeclaration, \c false
4421 * otherwise.
4422 */
4423 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)4424 get_variable_being_redeclared(ir_variable **var_ptr, YYLTYPE loc,
4425 struct _mesa_glsl_parse_state *state,
4426 bool allow_all_redeclarations,
4427 bool *is_redeclaration)
4428 {
4429 ir_variable *var = *var_ptr;
4430
4431 /* Check if this declaration is actually a re-declaration, either to
4432 * resize an array or add qualifiers to an existing variable.
4433 *
4434 * This is allowed for variables in the current scope, or when at
4435 * global scope (for built-ins in the implicit outer scope).
4436 */
4437 ir_variable *earlier = state->symbols->get_variable(var->name);
4438 if (earlier == NULL ||
4439 (state->current_function != NULL &&
4440 !state->symbols->name_declared_this_scope(var->name))) {
4441 *is_redeclaration = false;
4442 return var;
4443 }
4444
4445 *is_redeclaration = true;
4446
4447 if (earlier->data.how_declared == ir_var_declared_implicitly) {
4448 /* Verify that the redeclaration of a built-in does not change the
4449 * storage qualifier. There are a couple special cases.
4450 *
4451 * 1. Some built-in variables that are defined as 'in' in the
4452 * specification are implemented as system values. Allow
4453 * ir_var_system_value -> ir_var_shader_in.
4454 *
4455 * 2. gl_LastFragData is implemented as a ir_var_shader_out, but the
4456 * specification requires that redeclarations omit any qualifier.
4457 * Allow ir_var_shader_out -> ir_var_auto for this one variable.
4458 */
4459 if (earlier->data.mode != var->data.mode &&
4460 !(earlier->data.mode == ir_var_system_value &&
4461 var->data.mode == ir_var_shader_in) &&
4462 !(strcmp(var->name, "gl_LastFragData") == 0 &&
4463 var->data.mode == ir_var_auto)) {
4464 _mesa_glsl_error(&loc, state,
4465 "redeclaration cannot change qualification of `%s'",
4466 var->name);
4467 }
4468 }
4469
4470 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
4471 *
4472 * "It is legal to declare an array without a size and then
4473 * later re-declare the same name as an array of the same
4474 * type and specify a size."
4475 */
4476 if (glsl_type_is_unsized_array(earlier->type) && glsl_type_is_array(var->type)
4477 && (var->type->fields.array == earlier->type->fields.array)) {
4478 const int size = glsl_array_size(var->type);
4479 check_builtin_array_max_size(var->name, size, loc, state);
4480 if ((size > 0) && (size <= earlier->data.max_array_access)) {
4481 _mesa_glsl_error(& loc, state, "array size must be > %u due to "
4482 "previous access",
4483 earlier->data.max_array_access);
4484 }
4485
4486 earlier->type = var->type;
4487 delete var;
4488 var = NULL;
4489 *var_ptr = NULL;
4490 } else if (earlier->type != var->type) {
4491 _mesa_glsl_error(&loc, state,
4492 "redeclaration of `%s' has incorrect type",
4493 var->name);
4494 } else if ((state->ARB_fragment_coord_conventions_enable ||
4495 state->is_version(150, 0))
4496 && strcmp(var->name, "gl_FragCoord") == 0) {
4497 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
4498 * qualifiers.
4499 *
4500 * We don't really need to do anything here, just allow the
4501 * redeclaration. Any error on the gl_FragCoord is handled on the ast
4502 * level at apply_layout_qualifier_to_variable using the
4503 * ast_type_qualifier and _mesa_glsl_parse_state, or later in the
4504 * linker.
4505 */
4506 /* According to section 4.3.7 of the GLSL 1.30 spec,
4507 * the following built-in varaibles can be redeclared with an
4508 * interpolation qualifier:
4509 * * gl_FrontColor
4510 * * gl_BackColor
4511 * * gl_FrontSecondaryColor
4512 * * gl_BackSecondaryColor
4513 * * gl_Color
4514 * * gl_SecondaryColor
4515 */
4516 } else if (state->is_version(130, 0)
4517 && (strcmp(var->name, "gl_FrontColor") == 0
4518 || strcmp(var->name, "gl_BackColor") == 0
4519 || strcmp(var->name, "gl_FrontSecondaryColor") == 0
4520 || strcmp(var->name, "gl_BackSecondaryColor") == 0
4521 || strcmp(var->name, "gl_Color") == 0
4522 || strcmp(var->name, "gl_SecondaryColor") == 0)) {
4523 earlier->data.interpolation = var->data.interpolation;
4524
4525 /* Layout qualifiers for gl_FragDepth. */
4526 } else if ((state->is_version(420, 0) ||
4527 state->AMD_conservative_depth_enable ||
4528 state->ARB_conservative_depth_enable)
4529 && strcmp(var->name, "gl_FragDepth") == 0) {
4530
4531 /** From the AMD_conservative_depth spec:
4532 * Within any shader, the first redeclarations of gl_FragDepth
4533 * must appear before any use of gl_FragDepth.
4534 */
4535 if (earlier->data.used) {
4536 _mesa_glsl_error(&loc, state,
4537 "the first redeclaration of gl_FragDepth "
4538 "must appear before any use of gl_FragDepth");
4539 }
4540
4541 /* Prevent inconsistent redeclaration of depth layout qualifier. */
4542 if (earlier->data.depth_layout != ir_depth_layout_none
4543 && earlier->data.depth_layout != var->data.depth_layout) {
4544 _mesa_glsl_error(&loc, state,
4545 "gl_FragDepth: depth layout is declared here "
4546 "as '%s, but it was previously declared as "
4547 "'%s'",
4548 depth_layout_string((ir_depth_layout)var->data.depth_layout),
4549 depth_layout_string((ir_depth_layout)earlier->data.depth_layout));
4550 }
4551
4552 earlier->data.depth_layout = var->data.depth_layout;
4553
4554 } else if (state->has_framebuffer_fetch() &&
4555 strcmp(var->name, "gl_LastFragData") == 0 &&
4556 var->data.mode == ir_var_auto) {
4557 /* According to the EXT_shader_framebuffer_fetch spec:
4558 *
4559 * "By default, gl_LastFragData is declared with the mediump precision
4560 * qualifier. This can be changed by redeclaring the corresponding
4561 * variables with the desired precision qualifier."
4562 *
4563 * "Fragment shaders may specify the following layout qualifier only for
4564 * redeclaring the built-in gl_LastFragData array [...]: noncoherent"
4565 */
4566 earlier->data.precision = var->data.precision;
4567 earlier->data.memory_coherent = var->data.memory_coherent;
4568
4569 } else if (state->NV_viewport_array2_enable &&
4570 strcmp(var->name, "gl_Layer") == 0 &&
4571 earlier->data.how_declared == ir_var_declared_implicitly) {
4572 /* No need to do anything, just allow it. Qualifier is stored in state */
4573
4574 } else if (state->is_version(0, 300) &&
4575 state->has_separate_shader_objects() &&
4576 (strcmp(var->name, "gl_Position") == 0 ||
4577 strcmp(var->name, "gl_PointSize") == 0)) {
4578
4579 /* EXT_separate_shader_objects spec says:
4580 *
4581 * "The following vertex shader outputs may be redeclared
4582 * at global scope to specify a built-in output interface,
4583 * with or without special qualifiers:
4584 *
4585 * gl_Position
4586 * gl_PointSize
4587 *
4588 * When compiling shaders using either of the above variables,
4589 * both such variables must be redeclared prior to use."
4590 */
4591 if (earlier->data.used) {
4592 _mesa_glsl_error(&loc, state, "the first redeclaration of "
4593 "%s must appear before any use", var->name);
4594 }
4595 } else if ((earlier->data.how_declared == ir_var_declared_implicitly &&
4596 state->allow_builtin_variable_redeclaration) ||
4597 allow_all_redeclarations) {
4598 /* Allow verbatim redeclarations of built-in variables. Not explicitly
4599 * valid, but some applications do it.
4600 */
4601 } else {
4602 _mesa_glsl_error(&loc, state, "`%s' redeclared", var->name);
4603 }
4604
4605 return earlier;
4606 }
4607
4608 /**
4609 * Generate the IR for an initializer in a variable declaration
4610 */
4611 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)4612 process_initializer(ir_variable *var, ast_declaration *decl,
4613 ast_fully_specified_type *type,
4614 exec_list *initializer_instructions,
4615 struct _mesa_glsl_parse_state *state)
4616 {
4617 void *mem_ctx = state;
4618 ir_rvalue *result = NULL;
4619
4620 YYLTYPE initializer_loc = decl->initializer->get_location();
4621
4622 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
4623 *
4624 * "All uniform variables are read-only and are initialized either
4625 * directly by an application via API commands, or indirectly by
4626 * OpenGL."
4627 */
4628 if (var->data.mode == ir_var_uniform) {
4629 state->check_version(120, 0, &initializer_loc,
4630 "cannot initialize uniform %s",
4631 var->name);
4632 }
4633
4634 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
4635 *
4636 * "Buffer variables cannot have initializers."
4637 */
4638 if (var->data.mode == ir_var_shader_storage) {
4639 _mesa_glsl_error(&initializer_loc, state,
4640 "cannot initialize buffer variable %s",
4641 var->name);
4642 }
4643
4644 /* From section 4.1.7 of the GLSL 4.40 spec:
4645 *
4646 * "Opaque variables [...] are initialized only through the
4647 * OpenGL API; they cannot be declared with an initializer in a
4648 * shader."
4649 *
4650 * From section 4.1.7 of the ARB_bindless_texture spec:
4651 *
4652 * "Samplers may be declared as shader inputs and outputs, as uniform
4653 * variables, as temporary variables, and as function parameters."
4654 *
4655 * From section 4.1.X of the ARB_bindless_texture spec:
4656 *
4657 * "Images may be declared as shader inputs and outputs, as uniform
4658 * variables, as temporary variables, and as function parameters."
4659 */
4660 if (glsl_contains_atomic(var->type) ||
4661 (!state->has_bindless() && glsl_contains_opaque(var->type))) {
4662 _mesa_glsl_error(&initializer_loc, state,
4663 "cannot initialize %s variable %s",
4664 var->name, state->has_bindless() ? "atomic" : "opaque");
4665 }
4666
4667 if ((var->data.mode == ir_var_shader_in) && (state->current_function == NULL)) {
4668 _mesa_glsl_error(&initializer_loc, state,
4669 "cannot initialize %s shader input / %s %s",
4670 _mesa_shader_stage_to_string(state->stage),
4671 (state->stage == MESA_SHADER_VERTEX)
4672 ? "attribute" : "varying",
4673 var->name);
4674 }
4675
4676 if (var->data.mode == ir_var_shader_out && state->current_function == NULL) {
4677 _mesa_glsl_error(&initializer_loc, state,
4678 "cannot initialize %s shader output %s",
4679 _mesa_shader_stage_to_string(state->stage),
4680 var->name);
4681 }
4682
4683 /* If the initializer is an ast_aggregate_initializer, recursively store
4684 * type information from the LHS into it, so that its hir() function can do
4685 * type checking.
4686 */
4687 if (decl->initializer->oper == ast_aggregate)
4688 _mesa_ast_set_aggregate_type(var->type, decl->initializer);
4689
4690 ir_dereference *const lhs = new(state) ir_dereference_variable(var);
4691 ir_rvalue *rhs = decl->initializer->hir(initializer_instructions, state);
4692
4693 /* Calculate the constant value if this is a const or uniform
4694 * declaration.
4695 *
4696 * Section 4.3 (Storage Qualifiers) of the GLSL ES 1.00.17 spec says:
4697 *
4698 * "Declarations of globals without a storage qualifier, or with
4699 * just the const qualifier, may include initializers, in which case
4700 * they will be initialized before the first line of main() is
4701 * executed. Such initializers must be a constant expression."
4702 *
4703 * The same section of the GLSL ES 3.00.4 spec has similar language.
4704 */
4705 if (type->qualifier.flags.q.constant
4706 || type->qualifier.flags.q.uniform
4707 || (state->es_shader && state->current_function == NULL)) {
4708 ir_rvalue *new_rhs = validate_assignment(state, initializer_loc,
4709 lhs, rhs, true);
4710 if (new_rhs != NULL) {
4711 rhs = new_rhs;
4712
4713 /* Section 4.3.3 (Constant Expressions) of the GLSL ES 3.00.4 spec
4714 * says:
4715 *
4716 * "A constant expression is one of
4717 *
4718 * ...
4719 *
4720 * - an expression formed by an operator on operands that are
4721 * all constant expressions, including getting an element of
4722 * a constant array, or a field of a constant structure, or
4723 * components of a constant vector. However, the sequence
4724 * operator ( , ) and the assignment operators ( =, +=, ...)
4725 * are not included in the operators that can create a
4726 * constant expression."
4727 *
4728 * Section 12.43 (Sequence operator and constant expressions) says:
4729 *
4730 * "Should the following construct be allowed?
4731 *
4732 * float a[2,3];
4733 *
4734 * The expression within the brackets uses the sequence operator
4735 * (',') and returns the integer 3 so the construct is declaring
4736 * a single-dimensional array of size 3. In some languages, the
4737 * construct declares a two-dimensional array. It would be
4738 * preferable to make this construct illegal to avoid confusion.
4739 *
4740 * One possibility is to change the definition of the sequence
4741 * operator so that it does not return a constant-expression and
4742 * hence cannot be used to declare an array size.
4743 *
4744 * RESOLUTION: The result of a sequence operator is not a
4745 * constant-expression."
4746 *
4747 * Section 4.3.3 (Constant Expressions) of the GLSL 4.30.9 spec
4748 * contains language almost identical to the section 4.3.3 in the
4749 * GLSL ES 3.00.4 spec. This is a new limitation for these GLSL
4750 * versions.
4751 */
4752 ir_constant *constant_value =
4753 rhs->constant_expression_value(mem_ctx);
4754
4755 if (!constant_value ||
4756 (state->is_version(430, 300) &&
4757 decl->initializer->has_sequence_subexpression())) {
4758 const char *const variable_mode =
4759 (type->qualifier.flags.q.constant)
4760 ? "const"
4761 : ((type->qualifier.flags.q.uniform) ? "uniform" : "global");
4762
4763 /* If ARB_shading_language_420pack is enabled, initializers of
4764 * const-qualified local variables do not have to be constant
4765 * expressions. Const-qualified global variables must still be
4766 * initialized with constant expressions.
4767 */
4768 if (!state->has_420pack()
4769 || state->current_function == NULL) {
4770 _mesa_glsl_error(& initializer_loc, state,
4771 "initializer of %s variable `%s' must be a "
4772 "constant expression",
4773 variable_mode,
4774 decl->identifier);
4775 if (glsl_type_is_numeric(var->type)) {
4776 /* Reduce cascading errors. */
4777 var->constant_value = type->qualifier.flags.q.constant
4778 ? ir_constant::zero(state, var->type) : NULL;
4779 }
4780 }
4781 } else {
4782 rhs = constant_value;
4783 var->constant_value = type->qualifier.flags.q.constant
4784 ? constant_value : NULL;
4785 }
4786 } else {
4787 if (glsl_type_is_numeric(var->type)) {
4788 /* Reduce cascading errors. */
4789 rhs = var->constant_value = type->qualifier.flags.q.constant
4790 ? ir_constant::zero(state, var->type) : NULL;
4791 }
4792 }
4793 }
4794
4795 if (rhs && !glsl_type_is_error(rhs->type)) {
4796 bool temp = var->data.read_only;
4797 if (type->qualifier.flags.q.constant)
4798 var->data.read_only = false;
4799
4800 /* Never emit code to initialize a uniform.
4801 */
4802 const glsl_type *initializer_type;
4803 bool error_emitted = false;
4804 if (!type->qualifier.flags.q.uniform) {
4805 error_emitted =
4806 do_assignment(initializer_instructions, state,
4807 NULL, lhs, rhs,
4808 &result, true, true,
4809 type->get_location());
4810 initializer_type = result->type;
4811 } else
4812 initializer_type = rhs->type;
4813
4814 if (!error_emitted) {
4815 var->constant_initializer = rhs->constant_expression_value(mem_ctx);
4816 var->data.has_initializer = true;
4817 var->data.is_implicit_initializer = false;
4818
4819 /* If the declared variable is an unsized array, it must inherrit
4820 * its full type from the initializer. A declaration such as
4821 *
4822 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
4823 *
4824 * becomes
4825 *
4826 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
4827 *
4828 * The assignment generated in the if-statement (below) will also
4829 * automatically handle this case for non-uniforms.
4830 *
4831 * If the declared variable is not an array, the types must
4832 * already match exactly. As a result, the type assignment
4833 * here can be done unconditionally. For non-uniforms the call
4834 * to do_assignment can change the type of the initializer (via
4835 * the implicit conversion rules). For uniforms the initializer
4836 * must be a constant expression, and the type of that expression
4837 * was validated above.
4838 */
4839 var->type = initializer_type;
4840 }
4841
4842 var->data.read_only = temp;
4843 }
4844
4845 return result;
4846 }
4847
4848 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)4849 validate_layout_qualifier_vertex_count(struct _mesa_glsl_parse_state *state,
4850 YYLTYPE loc, ir_variable *var,
4851 unsigned num_vertices,
4852 unsigned *size,
4853 const char *var_category)
4854 {
4855 if (glsl_type_is_unsized_array(var->type)) {
4856 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says:
4857 *
4858 * All geometry shader input unsized array declarations will be
4859 * sized by an earlier input layout qualifier, when present, as per
4860 * the following table.
4861 *
4862 * Followed by a table mapping each allowed input layout qualifier to
4863 * the corresponding input length.
4864 *
4865 * Similarly for tessellation control shader outputs.
4866 */
4867 if (num_vertices != 0)
4868 var->type = glsl_array_type(var->type->fields.array,
4869 num_vertices, 0);
4870 } else {
4871 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec
4872 * includes the following examples of compile-time errors:
4873 *
4874 * // code sequence within one shader...
4875 * in vec4 Color1[]; // size unknown
4876 * ...Color1.length()...// illegal, length() unknown
4877 * in vec4 Color2[2]; // size is 2
4878 * ...Color1.length()...// illegal, Color1 still has no size
4879 * in vec4 Color3[3]; // illegal, input sizes are inconsistent
4880 * layout(lines) in; // legal, input size is 2, matching
4881 * in vec4 Color4[3]; // illegal, contradicts layout
4882 * ...
4883 *
4884 * To detect the case illustrated by Color3, we verify that the size of
4885 * an explicitly-sized array matches the size of any previously declared
4886 * explicitly-sized array. To detect the case illustrated by Color4, we
4887 * verify that the size of an explicitly-sized array is consistent with
4888 * any previously declared input layout.
4889 */
4890 if (num_vertices != 0 && var->type->length != num_vertices) {
4891 _mesa_glsl_error(&loc, state,
4892 "%s size contradicts previously declared layout "
4893 "(size is %u, but layout requires a size of %u)",
4894 var_category, var->type->length, num_vertices);
4895 } else if (*size != 0 && var->type->length != *size) {
4896 _mesa_glsl_error(&loc, state,
4897 "%s sizes are inconsistent (size is %u, but a "
4898 "previous declaration has size %u)",
4899 var_category, var->type->length, *size);
4900 } else {
4901 *size = var->type->length;
4902 }
4903 }
4904 }
4905
4906 static void
handle_tess_ctrl_shader_output_decl(struct _mesa_glsl_parse_state * state,YYLTYPE loc,ir_variable * var)4907 handle_tess_ctrl_shader_output_decl(struct _mesa_glsl_parse_state *state,
4908 YYLTYPE loc, ir_variable *var)
4909 {
4910 unsigned num_vertices = 0;
4911
4912 if (state->tcs_output_vertices_specified) {
4913 if (!state->out_qualifier->vertices->
4914 process_qualifier_constant(state, "vertices",
4915 &num_vertices, false)) {
4916 return;
4917 }
4918
4919 if (num_vertices > state->Const.MaxPatchVertices) {
4920 _mesa_glsl_error(&loc, state, "vertices (%d) exceeds "
4921 "GL_MAX_PATCH_VERTICES", num_vertices);
4922 return;
4923 }
4924 }
4925
4926 if (!glsl_type_is_array(var->type) && !var->data.patch) {
4927 _mesa_glsl_error(&loc, state,
4928 "tessellation control shader outputs must be arrays");
4929
4930 /* To avoid cascading failures, short circuit the checks below. */
4931 return;
4932 }
4933
4934 if (var->data.patch)
4935 return;
4936
4937 validate_layout_qualifier_vertex_count(state, loc, var, num_vertices,
4938 &state->tcs_output_size,
4939 "tessellation control shader output");
4940 }
4941
4942 /**
4943 * Do additional processing necessary for tessellation control/evaluation shader
4944 * input declarations. This covers both interface block arrays and bare input
4945 * variables.
4946 */
4947 static void
handle_tess_shader_input_decl(struct _mesa_glsl_parse_state * state,YYLTYPE loc,ir_variable * var)4948 handle_tess_shader_input_decl(struct _mesa_glsl_parse_state *state,
4949 YYLTYPE loc, ir_variable *var)
4950 {
4951 if (!glsl_type_is_array(var->type) && !var->data.patch) {
4952 _mesa_glsl_error(&loc, state,
4953 "per-vertex tessellation shader inputs must be arrays");
4954 /* Avoid cascading failures. */
4955 return;
4956 }
4957
4958 if (var->data.patch)
4959 return;
4960
4961 /* The ARB_tessellation_shader spec says:
4962 *
4963 * "Declaring an array size is optional. If no size is specified, it
4964 * will be taken from the implementation-dependent maximum patch size
4965 * (gl_MaxPatchVertices). If a size is specified, it must match the
4966 * maximum patch size; otherwise, a compile or link error will occur."
4967 *
4968 * This text appears twice, once for TCS inputs, and again for TES inputs.
4969 */
4970 if (glsl_type_is_unsized_array(var->type)) {
4971 var->type = glsl_array_type(var->type->fields.array,
4972 state->Const.MaxPatchVertices, 0);
4973 } else if (var->type->length != state->Const.MaxPatchVertices) {
4974 _mesa_glsl_error(&loc, state,
4975 "per-vertex tessellation shader input arrays must be "
4976 "sized to gl_MaxPatchVertices (%d).",
4977 state->Const.MaxPatchVertices);
4978 }
4979 }
4980
4981
4982 /**
4983 * Do additional processing necessary for geometry shader input declarations
4984 * (this covers both interface blocks arrays and bare input variables).
4985 */
4986 static void
handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state * state,YYLTYPE loc,ir_variable * var)4987 handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state *state,
4988 YYLTYPE loc, ir_variable *var)
4989 {
4990 unsigned num_vertices = 0;
4991
4992 if (state->gs_input_prim_type_specified) {
4993 GLenum in_prim_type = state->in_qualifier->prim_type;
4994 num_vertices = mesa_vertices_per_prim(gl_to_mesa_prim(in_prim_type));
4995 }
4996
4997 /* Geometry shader input variables must be arrays. Caller should have
4998 * reported an error for this.
4999 */
5000 if (!glsl_type_is_array(var->type)) {
5001 assert(state->error);
5002
5003 /* To avoid cascading failures, short circuit the checks below. */
5004 return;
5005 }
5006
5007 validate_layout_qualifier_vertex_count(state, loc, var, num_vertices,
5008 &state->gs_input_size,
5009 "geometry shader input");
5010 }
5011
5012 static void
validate_identifier(const char * identifier,YYLTYPE loc,struct _mesa_glsl_parse_state * state)5013 validate_identifier(const char *identifier, YYLTYPE loc,
5014 struct _mesa_glsl_parse_state *state)
5015 {
5016 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
5017 *
5018 * "Identifiers starting with "gl_" are reserved for use by
5019 * OpenGL, and may not be declared in a shader as either a
5020 * variable or a function."
5021 */
5022 if (is_gl_identifier(identifier)) {
5023 _mesa_glsl_error(&loc, state,
5024 "identifier `%s' uses reserved `gl_' prefix",
5025 identifier);
5026 } else if (strstr(identifier, "__")) {
5027 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
5028 * spec:
5029 *
5030 * "In addition, all identifiers containing two
5031 * consecutive underscores (__) are reserved as
5032 * possible future keywords."
5033 *
5034 * The intention is that names containing __ are reserved for internal
5035 * use by the implementation, and names prefixed with GL_ are reserved
5036 * for use by Khronos. Names simply containing __ are dangerous to use,
5037 * but should be allowed.
5038 *
5039 * A future version of the GLSL specification will clarify this.
5040 */
5041 _mesa_glsl_warning(&loc, state,
5042 "identifier `%s' uses reserved `__' string",
5043 identifier);
5044 }
5045 }
5046
5047 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)5048 ast_declarator_list::hir(exec_list *instructions,
5049 struct _mesa_glsl_parse_state *state)
5050 {
5051 void *ctx = state;
5052 const struct glsl_type *decl_type;
5053 const char *type_name = NULL;
5054 ir_rvalue *result = NULL;
5055 YYLTYPE loc = this->get_location();
5056
5057 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
5058 *
5059 * "To ensure that a particular output variable is invariant, it is
5060 * necessary to use the invariant qualifier. It can either be used to
5061 * qualify a previously declared variable as being invariant
5062 *
5063 * invariant gl_Position; // make existing gl_Position be invariant"
5064 *
5065 * In these cases the parser will set the 'invariant' flag in the declarator
5066 * list, and the type will be NULL.
5067 */
5068 if (this->invariant) {
5069 assert(this->type == NULL);
5070
5071 if (state->current_function != NULL) {
5072 _mesa_glsl_error(& loc, state,
5073 "all uses of `invariant' keyword must be at global "
5074 "scope");
5075 }
5076
5077 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
5078 assert(decl->array_specifier == NULL);
5079 assert(decl->initializer == NULL);
5080
5081 ir_variable *const earlier =
5082 state->symbols->get_variable(decl->identifier);
5083 if (earlier == NULL) {
5084 _mesa_glsl_error(& loc, state,
5085 "undeclared variable `%s' cannot be marked "
5086 "invariant", decl->identifier);
5087 } else if (!is_allowed_invariant(earlier, state)) {
5088 _mesa_glsl_error(&loc, state,
5089 "`%s' cannot be marked invariant; interfaces between "
5090 "shader stages only.", decl->identifier);
5091 } else if (earlier->data.used) {
5092 _mesa_glsl_error(& loc, state,
5093 "variable `%s' may not be redeclared "
5094 "`invariant' after being used",
5095 earlier->name);
5096 } else {
5097 earlier->data.explicit_invariant = true;
5098 earlier->data.invariant = true;
5099 }
5100 }
5101
5102 /* Invariant redeclarations do not have r-values.
5103 */
5104 return NULL;
5105 }
5106
5107 if (this->precise) {
5108 assert(this->type == NULL);
5109
5110 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
5111 assert(decl->array_specifier == NULL);
5112 assert(decl->initializer == NULL);
5113
5114 ir_variable *const earlier =
5115 state->symbols->get_variable(decl->identifier);
5116 if (earlier == NULL) {
5117 _mesa_glsl_error(& loc, state,
5118 "undeclared variable `%s' cannot be marked "
5119 "precise", decl->identifier);
5120 } else if (state->current_function != NULL &&
5121 !state->symbols->name_declared_this_scope(decl->identifier)) {
5122 /* Note: we have to check if we're in a function, since
5123 * builtins are treated as having come from another scope.
5124 */
5125 _mesa_glsl_error(& loc, state,
5126 "variable `%s' from an outer scope may not be "
5127 "redeclared `precise' in this scope",
5128 earlier->name);
5129 } else if (earlier->data.used) {
5130 _mesa_glsl_error(& loc, state,
5131 "variable `%s' may not be redeclared "
5132 "`precise' after being used",
5133 earlier->name);
5134 } else {
5135 earlier->data.precise = true;
5136 }
5137 }
5138
5139 /* Precise redeclarations do not have r-values either. */
5140 return NULL;
5141 }
5142
5143 assert(this->type != NULL);
5144 assert(!this->invariant);
5145 assert(!this->precise);
5146
5147 /* GL_EXT_shader_image_load_store base type uses GLSL_TYPE_VOID as a special value to
5148 * indicate that it needs to be updated later (see glsl_parser.yy).
5149 * This is done here, based on the layout qualifier and the type of the image var
5150 */
5151 if (this->type->qualifier.flags.q.explicit_image_format &&
5152 glsl_type_is_image(this->type->specifier->type) &&
5153 this->type->qualifier.image_base_type == GLSL_TYPE_VOID) {
5154 /* "The ARB_shader_image_load_store says:
5155 * If both extensions are enabled in the shading language, the "size*" layout
5156 * qualifiers are treated as format qualifiers, and are mapped to equivalent
5157 * format qualifiers in the table below, according to the type of image
5158 * variable.
5159 * image* iimage* uimage*
5160 * -------- -------- --------
5161 * size1x8 n/a r8i r8ui
5162 * size1x16 r16f r16i r16ui
5163 * size1x32 r32f r32i r32ui
5164 * size2x32 rg32f rg32i rg32ui
5165 * size4x32 rgba32f rgba32i rgba32ui"
5166 */
5167 if (strncmp(this->type->specifier->type_name, "image", strlen("image")) == 0) {
5168 switch (this->type->qualifier.image_format) {
5169 case PIPE_FORMAT_R8_SINT:
5170 /* The GL_EXT_shader_image_load_store spec says:
5171 * A layout of "size1x8" is illegal for image variables associated
5172 * with floating-point data types.
5173 */
5174 _mesa_glsl_error(& loc, state,
5175 "size1x8 is illegal for image variables "
5176 "with floating-point data types.");
5177 return NULL;
5178 case PIPE_FORMAT_R16_SINT:
5179 this->type->qualifier.image_format = PIPE_FORMAT_R16_FLOAT;
5180 break;
5181 case PIPE_FORMAT_R32_SINT:
5182 this->type->qualifier.image_format = PIPE_FORMAT_R32_FLOAT;
5183 break;
5184 case PIPE_FORMAT_R32G32_SINT:
5185 this->type->qualifier.image_format = PIPE_FORMAT_R32G32_FLOAT;
5186 break;
5187 case PIPE_FORMAT_R32G32B32A32_SINT:
5188 this->type->qualifier.image_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
5189 break;
5190 default:
5191 unreachable("Unknown image format");
5192 }
5193 this->type->qualifier.image_base_type = GLSL_TYPE_FLOAT;
5194 } else if (strncmp(this->type->specifier->type_name, "uimage", strlen("uimage")) == 0) {
5195 switch (this->type->qualifier.image_format) {
5196 case PIPE_FORMAT_R8_SINT:
5197 this->type->qualifier.image_format = PIPE_FORMAT_R8_UINT;
5198 break;
5199 case PIPE_FORMAT_R16_SINT:
5200 this->type->qualifier.image_format = PIPE_FORMAT_R16_UINT;
5201 break;
5202 case PIPE_FORMAT_R32_SINT:
5203 this->type->qualifier.image_format = PIPE_FORMAT_R32_UINT;
5204 break;
5205 case PIPE_FORMAT_R32G32_SINT:
5206 this->type->qualifier.image_format = PIPE_FORMAT_R32G32_UINT;
5207 break;
5208 case PIPE_FORMAT_R32G32B32A32_SINT:
5209 this->type->qualifier.image_format = PIPE_FORMAT_R32G32B32A32_UINT;
5210 break;
5211 default:
5212 unreachable("Unknown image format");
5213 }
5214 this->type->qualifier.image_base_type = GLSL_TYPE_UINT;
5215 } else if (strncmp(this->type->specifier->type_name, "iimage", strlen("iimage")) == 0) {
5216 this->type->qualifier.image_base_type = GLSL_TYPE_INT;
5217 } else {
5218 assert(false);
5219 }
5220 }
5221
5222 /* The type specifier may contain a structure definition. Process that
5223 * before any of the variable declarations.
5224 */
5225 (void) this->type->specifier->hir(instructions, state);
5226
5227 decl_type = this->type->glsl_type(& type_name, state);
5228
5229 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
5230 * "Buffer variables may only be declared inside interface blocks
5231 * (section 4.3.9 “Interface Blocks”), which are then referred to as
5232 * shader storage blocks. It is a compile-time error to declare buffer
5233 * variables at global scope (outside a block)."
5234 */
5235 if (type->qualifier.flags.q.buffer && !glsl_type_is_interface(decl_type)) {
5236 _mesa_glsl_error(&loc, state,
5237 "buffer variables cannot be declared outside "
5238 "interface blocks");
5239 }
5240
5241 /* An offset-qualified atomic counter declaration sets the default
5242 * offset for the next declaration within the same atomic counter
5243 * buffer.
5244 */
5245 if (decl_type && glsl_contains_atomic(decl_type)) {
5246 if (type->qualifier.flags.q.explicit_binding &&
5247 type->qualifier.flags.q.explicit_offset) {
5248 unsigned qual_binding;
5249 unsigned qual_offset;
5250 if (process_qualifier_constant(state, &loc, "binding",
5251 type->qualifier.binding,
5252 &qual_binding)
5253 && process_qualifier_constant(state, &loc, "offset",
5254 type->qualifier.offset,
5255 &qual_offset)) {
5256 if (qual_binding < ARRAY_SIZE(state->atomic_counter_offsets))
5257 state->atomic_counter_offsets[qual_binding] = qual_offset;
5258 }
5259 }
5260
5261 ast_type_qualifier allowed_atomic_qual_mask;
5262 allowed_atomic_qual_mask.flags.i = 0;
5263 allowed_atomic_qual_mask.flags.q.explicit_binding = 1;
5264 allowed_atomic_qual_mask.flags.q.explicit_offset = 1;
5265 allowed_atomic_qual_mask.flags.q.uniform = 1;
5266
5267 type->qualifier.validate_flags(&loc, state, allowed_atomic_qual_mask,
5268 "invalid layout qualifier for",
5269 "atomic_uint");
5270 }
5271
5272 if (this->declarations.is_empty()) {
5273 /* If there is no structure involved in the program text, there are two
5274 * possible scenarios:
5275 *
5276 * - The program text contained something like 'vec4;'. This is an
5277 * empty declaration. It is valid but weird. Emit a warning.
5278 *
5279 * - The program text contained something like 'S;' and 'S' is not the
5280 * name of a known structure type. This is both invalid and weird.
5281 * Emit an error.
5282 *
5283 * - The program text contained something like 'mediump float;'
5284 * when the programmer probably meant 'precision mediump
5285 * float;' Emit a warning with a description of what they
5286 * probably meant to do.
5287 *
5288 * Note that if decl_type is NULL and there is a structure involved,
5289 * there must have been some sort of error with the structure. In this
5290 * case we assume that an error was already generated on this line of
5291 * code for the structure. There is no need to generate an additional,
5292 * confusing error.
5293 */
5294 assert(this->type->specifier->structure == NULL || decl_type != NULL
5295 || state->error);
5296
5297 if (decl_type == NULL) {
5298 _mesa_glsl_error(&loc, state,
5299 "invalid type `%s' in empty declaration",
5300 type_name);
5301 } else {
5302 if (glsl_type_is_array(decl_type)) {
5303 /* From Section 13.22 (Array Declarations) of the GLSL ES 3.2
5304 * spec:
5305 *
5306 * "... any declaration that leaves the size undefined is
5307 * disallowed as this would add complexity and there are no
5308 * use-cases."
5309 */
5310 if (state->es_shader && glsl_type_is_unsized_array(decl_type)) {
5311 _mesa_glsl_error(&loc, state, "array size must be explicitly "
5312 "or implicitly defined");
5313 }
5314
5315 /* From Section 4.12 (Empty Declarations) of the GLSL 4.5 spec:
5316 *
5317 * "The combinations of types and qualifiers that cause
5318 * compile-time or link-time errors are the same whether or not
5319 * the declaration is empty."
5320 */
5321 validate_array_dimensions(decl_type, state, &loc);
5322 }
5323
5324 if (glsl_type_is_atomic_uint(decl_type)) {
5325 /* Empty atomic counter declarations are allowed and useful
5326 * to set the default offset qualifier.
5327 */
5328 return NULL;
5329 } else if (this->type->qualifier.precision != ast_precision_none) {
5330 if (this->type->specifier->structure != NULL) {
5331 _mesa_glsl_error(&loc, state,
5332 "precision qualifiers can't be applied "
5333 "to structures");
5334 } else {
5335 static const char *const precision_names[] = {
5336 "highp",
5337 "highp",
5338 "mediump",
5339 "lowp"
5340 };
5341
5342 _mesa_glsl_warning(&loc, state,
5343 "empty declaration with precision "
5344 "qualifier, to set the default precision, "
5345 "use `precision %s %s;'",
5346 precision_names[this->type->
5347 qualifier.precision],
5348 type_name);
5349 }
5350 } else if (this->type->specifier->structure == NULL) {
5351 _mesa_glsl_warning(&loc, state, "empty declaration");
5352 }
5353 }
5354 }
5355
5356 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
5357 const struct glsl_type *var_type;
5358 ir_variable *var;
5359 const char *identifier = decl->identifier;
5360 /* FINISHME: Emit a warning if a variable declaration shadows a
5361 * FINISHME: declaration at a higher scope.
5362 */
5363
5364 if ((decl_type == NULL) || glsl_type_is_void(decl_type)) {
5365 if (type_name != NULL) {
5366 _mesa_glsl_error(& loc, state,
5367 "invalid type `%s' in declaration of `%s'",
5368 type_name, decl->identifier);
5369 } else {
5370 _mesa_glsl_error(& loc, state,
5371 "invalid type in declaration of `%s'",
5372 decl->identifier);
5373 }
5374 continue;
5375 }
5376
5377 if (this->type->qualifier.is_subroutine_decl()) {
5378 const glsl_type *t;
5379 const char *name;
5380
5381 t = state->symbols->get_type(this->type->specifier->type_name);
5382 if (!t)
5383 _mesa_glsl_error(& loc, state,
5384 "invalid type in declaration of `%s'",
5385 decl->identifier);
5386 name = ralloc_asprintf(ctx, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state->stage), decl->identifier);
5387
5388 identifier = name;
5389
5390 }
5391 var_type = process_array_type(&loc, decl_type, decl->array_specifier,
5392 state);
5393
5394 var = new(ctx) ir_variable(var_type, identifier, ir_var_auto);
5395
5396 /* The 'varying in' and 'varying out' qualifiers can only be used with
5397 * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
5398 * yet.
5399 */
5400 if (this->type->qualifier.flags.q.varying) {
5401 if (this->type->qualifier.flags.q.in) {
5402 _mesa_glsl_error(& loc, state,
5403 "`varying in' qualifier in declaration of "
5404 "`%s' only valid for geometry shaders using "
5405 "ARB_geometry_shader4 or EXT_geometry_shader4",
5406 decl->identifier);
5407 } else if (this->type->qualifier.flags.q.out) {
5408 _mesa_glsl_error(& loc, state,
5409 "`varying out' qualifier in declaration of "
5410 "`%s' only valid for geometry shaders using "
5411 "ARB_geometry_shader4 or EXT_geometry_shader4",
5412 decl->identifier);
5413 }
5414 }
5415
5416 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
5417 *
5418 * "Global variables can only use the qualifiers const,
5419 * attribute, uniform, or varying. Only one may be
5420 * specified.
5421 *
5422 * Local variables can only use the qualifier const."
5423 *
5424 * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
5425 * any extension that adds the 'layout' keyword.
5426 */
5427 if (!state->is_version(130, 300)
5428 && !state->has_explicit_attrib_location()
5429 && !state->has_separate_shader_objects()
5430 && !state->ARB_fragment_coord_conventions_enable) {
5431 /* GL_EXT_gpu_shader4 only allows "varying out" on fragment shader
5432 * outputs. (the varying flag is not set by the parser)
5433 */
5434 if (this->type->qualifier.flags.q.out &&
5435 (!state->EXT_gpu_shader4_enable ||
5436 state->stage != MESA_SHADER_FRAGMENT)) {
5437 _mesa_glsl_error(& loc, state,
5438 "`out' qualifier in declaration of `%s' "
5439 "only valid for function parameters in %s",
5440 decl->identifier, state->get_version_string());
5441 }
5442 if (this->type->qualifier.flags.q.in) {
5443 _mesa_glsl_error(& loc, state,
5444 "`in' qualifier in declaration of `%s' "
5445 "only valid for function parameters in %s",
5446 decl->identifier, state->get_version_string());
5447 }
5448 /* FINISHME: Test for other invalid qualifiers. */
5449 }
5450
5451 apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
5452 & loc, false);
5453 apply_layout_qualifier_to_variable(&this->type->qualifier, var, state,
5454 &loc);
5455
5456 if ((state->zero_init & (1u << var->data.mode)) &&
5457 (glsl_type_is_numeric(var->type) || glsl_type_is_boolean(var->type))) {
5458 const ir_constant_data data = { { 0 } };
5459 var->data.has_initializer = true;
5460 var->data.is_implicit_initializer = true;
5461 var->constant_initializer = new(var) ir_constant(var->type, &data);
5462 }
5463
5464 if (this->type->qualifier.flags.q.invariant) {
5465 if (!is_allowed_invariant(var, state)) {
5466 _mesa_glsl_error(&loc, state,
5467 "`%s' cannot be marked invariant; interfaces between "
5468 "shader stages only", var->name);
5469 }
5470 }
5471
5472 if (state->current_function != NULL) {
5473 const char *mode = NULL;
5474 const char *extra = "";
5475
5476 /* There is no need to check for 'inout' here because the parser will
5477 * only allow that in function parameter lists.
5478 */
5479 if (this->type->qualifier.flags.q.attribute) {
5480 mode = "attribute";
5481 } else if (this->type->qualifier.is_subroutine_decl()) {
5482 mode = "subroutine uniform";
5483 } else if (this->type->qualifier.flags.q.uniform) {
5484 mode = "uniform";
5485 } else if (this->type->qualifier.flags.q.varying) {
5486 mode = "varying";
5487 } else if (this->type->qualifier.flags.q.in) {
5488 mode = "in";
5489 extra = " or in function parameter list";
5490 } else if (this->type->qualifier.flags.q.out) {
5491 mode = "out";
5492 extra = " or in function parameter list";
5493 }
5494
5495 if (mode) {
5496 _mesa_glsl_error(& loc, state,
5497 "%s variable `%s' must be declared at "
5498 "global scope%s",
5499 mode, var->name, extra);
5500 }
5501 } else if (var->data.mode == ir_var_shader_in) {
5502 var->data.read_only = true;
5503
5504 if (state->stage == MESA_SHADER_VERTEX) {
5505 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
5506 *
5507 * "Vertex shader inputs can only be float, floating-point
5508 * vectors, matrices, signed and unsigned integers and integer
5509 * vectors. Vertex shader inputs can also form arrays of these
5510 * types, but not structures."
5511 *
5512 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
5513 *
5514 * "Vertex shader inputs can only be float, floating-point
5515 * vectors, matrices, signed and unsigned integers and integer
5516 * vectors. They cannot be arrays or structures."
5517 *
5518 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
5519 *
5520 * "The attribute qualifier can be used only with float,
5521 * floating-point vectors, and matrices. Attribute variables
5522 * cannot be declared as arrays or structures."
5523 *
5524 * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
5525 *
5526 * "Vertex shader inputs can only be float, floating-point
5527 * vectors, matrices, signed and unsigned integers and integer
5528 * vectors. Vertex shader inputs cannot be arrays or
5529 * structures."
5530 *
5531 * From section 4.3.4 of the ARB_bindless_texture spec:
5532 *
5533 * "(modify third paragraph of the section to allow sampler and
5534 * image types) ... Vertex shader inputs can only be float,
5535 * single-precision floating-point scalars, single-precision
5536 * floating-point vectors, matrices, signed and unsigned
5537 * integers and integer vectors, sampler and image types."
5538 */
5539 const glsl_type *check_type = glsl_without_array(var->type);
5540
5541 bool error = false;
5542 switch (check_type->base_type) {
5543 case GLSL_TYPE_FLOAT:
5544 break;
5545 case GLSL_TYPE_UINT64:
5546 case GLSL_TYPE_INT64:
5547 break;
5548 case GLSL_TYPE_UINT:
5549 case GLSL_TYPE_INT:
5550 error = !state->is_version(120, 300) && !state->EXT_gpu_shader4_enable;
5551 break;
5552 case GLSL_TYPE_DOUBLE:
5553 error = !state->is_version(410, 0) && !state->ARB_vertex_attrib_64bit_enable;
5554 break;
5555 case GLSL_TYPE_SAMPLER:
5556 case GLSL_TYPE_TEXTURE:
5557 case GLSL_TYPE_IMAGE:
5558 error = !state->has_bindless();
5559 break;
5560 default:
5561 error = true;
5562 }
5563
5564 if (error) {
5565 _mesa_glsl_error(& loc, state,
5566 "vertex shader input / attribute cannot have "
5567 "type %s`%s'",
5568 glsl_type_is_array(var->type) ? "array of " : "",
5569 glsl_get_type_name(check_type));
5570 } else if (glsl_type_is_array(var->type) &&
5571 !state->check_version(150, 0, &loc,
5572 "vertex shader input / attribute "
5573 "cannot have array type")) {
5574 }
5575 } else if (state->stage == MESA_SHADER_GEOMETRY) {
5576 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
5577 *
5578 * Geometry shader input variables get the per-vertex values
5579 * written out by vertex shader output variables of the same
5580 * names. Since a geometry shader operates on a set of
5581 * vertices, each input varying variable (or input block, see
5582 * interface blocks below) needs to be declared as an array.
5583 */
5584 if (!glsl_type_is_array(var->type)) {
5585 _mesa_glsl_error(&loc, state,
5586 "geometry shader inputs must be arrays");
5587 }
5588
5589 handle_geometry_shader_input_decl(state, loc, var);
5590 } else if (state->stage == MESA_SHADER_FRAGMENT) {
5591 /* From section 4.3.4 (Input Variables) of the GLSL ES 3.10 spec:
5592 *
5593 * It is a compile-time error to declare a fragment shader
5594 * input with, or that contains, any of the following types:
5595 *
5596 * * A boolean type
5597 * * An opaque type
5598 * * An array of arrays
5599 * * An array of structures
5600 * * A structure containing an array
5601 * * A structure containing a structure
5602 */
5603 if (state->es_shader) {
5604 const glsl_type *check_type = glsl_without_array(var->type);
5605 if (glsl_type_is_boolean(check_type) ||
5606 glsl_contains_opaque(check_type)) {
5607 _mesa_glsl_error(&loc, state,
5608 "fragment shader input cannot have type %s",
5609 glsl_get_type_name(check_type));
5610 }
5611 if (glsl_type_is_array(var->type) &&
5612 glsl_type_is_array(var->type->fields.array)) {
5613 _mesa_glsl_error(&loc, state,
5614 "%s shader output "
5615 "cannot have an array of arrays",
5616 _mesa_shader_stage_to_string(state->stage));
5617 }
5618 if (glsl_type_is_array(var->type) &&
5619 glsl_type_is_struct(var->type->fields.array)) {
5620 _mesa_glsl_error(&loc, state,
5621 "fragment shader input "
5622 "cannot have an array of structs");
5623 }
5624 if (glsl_type_is_struct(var->type)) {
5625 for (unsigned i = 0; i < var->type->length; i++) {
5626 if (glsl_type_is_array(var->type->fields.structure[i].type) ||
5627 glsl_type_is_struct(var->type->fields.structure[i].type))
5628 _mesa_glsl_error(&loc, state,
5629 "fragment shader input cannot have "
5630 "a struct that contains an "
5631 "array or struct");
5632 }
5633 }
5634 }
5635 } else if (state->stage == MESA_SHADER_TESS_CTRL ||
5636 state->stage == MESA_SHADER_TESS_EVAL) {
5637 handle_tess_shader_input_decl(state, loc, var);
5638 }
5639 } else if (var->data.mode == ir_var_shader_out) {
5640 const glsl_type *check_type = glsl_without_array(var->type);
5641
5642 /* From section 4.3.6 (Output variables) of the GLSL 4.40 spec:
5643 *
5644 * It is a compile-time error to declare a fragment shader output
5645 * that contains any of the following:
5646 *
5647 * * A Boolean type (bool, bvec2 ...)
5648 * * A double-precision scalar or vector (double, dvec2 ...)
5649 * * An opaque type
5650 * * Any matrix type
5651 * * A structure
5652 */
5653 if (state->stage == MESA_SHADER_FRAGMENT) {
5654 if (glsl_type_is_struct(check_type) || glsl_type_is_matrix(check_type))
5655 _mesa_glsl_error(&loc, state,
5656 "fragment shader output "
5657 "cannot have struct or matrix type");
5658 switch (check_type->base_type) {
5659 case GLSL_TYPE_UINT:
5660 case GLSL_TYPE_INT:
5661 case GLSL_TYPE_FLOAT:
5662 break;
5663 default:
5664 _mesa_glsl_error(&loc, state,
5665 "fragment shader output cannot have "
5666 "type %s", glsl_get_type_name(check_type));
5667 }
5668 }
5669
5670 /* From section 4.3.6 (Output Variables) of the GLSL ES 3.10 spec:
5671 *
5672 * It is a compile-time error to declare a vertex shader output
5673 * with, or that contains, any of the following types:
5674 *
5675 * * A boolean type
5676 * * An opaque type
5677 * * An array of arrays
5678 * * An array of structures
5679 * * A structure containing an array
5680 * * A structure containing a structure
5681 *
5682 * It is a compile-time error to declare a fragment shader output
5683 * with, or that contains, any of the following types:
5684 *
5685 * * A boolean type
5686 * * An opaque type
5687 * * A matrix
5688 * * A structure
5689 * * An array of array
5690 *
5691 * ES 3.20 updates this to apply to tessellation and geometry shaders
5692 * as well. Because there are per-vertex arrays in the new stages,
5693 * it strikes the "array of..." rules and replaces them with these:
5694 *
5695 * * For per-vertex-arrayed variables (applies to tessellation
5696 * control, tessellation evaluation and geometry shaders):
5697 *
5698 * * Per-vertex-arrayed arrays of arrays
5699 * * Per-vertex-arrayed arrays of structures
5700 *
5701 * * For non-per-vertex-arrayed variables:
5702 *
5703 * * An array of arrays
5704 * * An array of structures
5705 *
5706 * which basically says to unwrap the per-vertex aspect and apply
5707 * the old rules.
5708 */
5709 if (state->es_shader) {
5710 if (glsl_type_is_array(var->type) &&
5711 glsl_type_is_array(var->type->fields.array)) {
5712 _mesa_glsl_error(&loc, state,
5713 "%s shader output "
5714 "cannot have an array of arrays",
5715 _mesa_shader_stage_to_string(state->stage));
5716 }
5717 if (state->stage <= MESA_SHADER_GEOMETRY) {
5718 const glsl_type *type = var->type;
5719
5720 if (state->stage == MESA_SHADER_TESS_CTRL &&
5721 !var->data.patch && glsl_type_is_array(var->type)) {
5722 type = var->type->fields.array;
5723 }
5724
5725 if (glsl_type_is_array(type) && glsl_type_is_struct(type->fields.array)) {
5726 _mesa_glsl_error(&loc, state,
5727 "%s shader output cannot have "
5728 "an array of structs",
5729 _mesa_shader_stage_to_string(state->stage));
5730 }
5731 if (glsl_type_is_struct(type)) {
5732 for (unsigned i = 0; i < type->length; i++) {
5733 if (glsl_type_is_array(type->fields.structure[i].type) ||
5734 glsl_type_is_struct(type->fields.structure[i].type))
5735 _mesa_glsl_error(&loc, state,
5736 "%s shader output cannot have a "
5737 "struct that contains an "
5738 "array or struct",
5739 _mesa_shader_stage_to_string(state->stage));
5740 }
5741 }
5742 }
5743 }
5744
5745 if (state->stage == MESA_SHADER_TESS_CTRL) {
5746 handle_tess_ctrl_shader_output_decl(state, loc, var);
5747 }
5748 } else if (glsl_contains_subroutine(var->type)) {
5749 /* declare subroutine uniforms as hidden */
5750 var->data.how_declared = ir_var_hidden;
5751 }
5752
5753 /* From section 4.3.4 of the GLSL 4.00 spec:
5754 * "Input variables may not be declared using the patch in qualifier
5755 * in tessellation control or geometry shaders."
5756 *
5757 * From section 4.3.6 of the GLSL 4.00 spec:
5758 * "It is an error to use patch out in a vertex, tessellation
5759 * evaluation, or geometry shader."
5760 *
5761 * This doesn't explicitly forbid using them in a fragment shader, but
5762 * that's probably just an oversight.
5763 */
5764 if (state->stage != MESA_SHADER_TESS_EVAL
5765 && this->type->qualifier.flags.q.patch
5766 && this->type->qualifier.flags.q.in) {
5767
5768 _mesa_glsl_error(&loc, state, "'patch in' can only be used in a "
5769 "tessellation evaluation shader");
5770 }
5771
5772 if (state->stage != MESA_SHADER_TESS_CTRL
5773 && this->type->qualifier.flags.q.patch
5774 && this->type->qualifier.flags.q.out) {
5775
5776 _mesa_glsl_error(&loc, state, "'patch out' can only be used in a "
5777 "tessellation control shader");
5778 }
5779
5780 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
5781 */
5782 if (this->type->qualifier.precision != ast_precision_none) {
5783 state->check_precision_qualifiers_allowed(&loc);
5784 }
5785
5786 if (this->type->qualifier.precision != ast_precision_none &&
5787 !precision_qualifier_allowed(var->type)) {
5788 _mesa_glsl_error(&loc, state,
5789 "precision qualifiers apply only to floating point"
5790 ", integer and opaque types");
5791 }
5792
5793 /* From section 4.1.7 of the GLSL 4.40 spec:
5794 *
5795 * "[Opaque types] can only be declared as function
5796 * parameters or uniform-qualified variables."
5797 *
5798 * From section 4.1.7 of the ARB_bindless_texture spec:
5799 *
5800 * "Samplers may be declared as shader inputs and outputs, as uniform
5801 * variables, as temporary variables, and as function parameters."
5802 *
5803 * From section 4.1.X of the ARB_bindless_texture spec:
5804 *
5805 * "Images may be declared as shader inputs and outputs, as uniform
5806 * variables, as temporary variables, and as function parameters."
5807 */
5808 if (!this->type->qualifier.flags.q.uniform &&
5809 (glsl_contains_atomic(var_type) ||
5810 (!state->has_bindless() && glsl_contains_opaque(var_type)))) {
5811 _mesa_glsl_error(&loc, state,
5812 "%s variables must be declared uniform",
5813 state->has_bindless() ? "atomic" : "opaque");
5814 }
5815
5816 /* Process the initializer and add its instructions to a temporary
5817 * list. This list will be added to the instruction stream (below) after
5818 * the declaration is added. This is done because in some cases (such as
5819 * redeclarations) the declaration may not actually be added to the
5820 * instruction stream.
5821 */
5822 exec_list initializer_instructions;
5823
5824 /* Examine var name here since var may get deleted in the next call */
5825 bool var_is_gl_id = is_gl_identifier(var->name);
5826
5827 bool is_redeclaration;
5828 var = get_variable_being_redeclared(&var, decl->get_location(), state,
5829 false /* allow_all_redeclarations */,
5830 &is_redeclaration);
5831 if (is_redeclaration) {
5832 if (var_is_gl_id &&
5833 var->data.how_declared == ir_var_declared_in_block) {
5834 _mesa_glsl_error(&loc, state,
5835 "`%s' has already been redeclared using "
5836 "gl_PerVertex", var->name);
5837 }
5838 var->data.how_declared = ir_var_declared_normally;
5839 }
5840
5841 if (decl->initializer != NULL) {
5842 result = process_initializer(var,
5843 decl, this->type,
5844 &initializer_instructions, state);
5845 } else {
5846 validate_array_dimensions(var_type, state, &loc);
5847 }
5848
5849 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
5850 *
5851 * "It is an error to write to a const variable outside of
5852 * its declaration, so they must be initialized when
5853 * declared."
5854 */
5855 if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) {
5856 _mesa_glsl_error(& loc, state,
5857 "const declaration of `%s' must be initialized",
5858 decl->identifier);
5859 }
5860
5861 if (state->es_shader) {
5862 const glsl_type *const t = var->type;
5863
5864 /* Skip the unsized array check for TCS/TES/GS inputs & TCS outputs.
5865 *
5866 * The GL_OES_tessellation_shader spec says about inputs:
5867 *
5868 * "Declaring an array size is optional. If no size is specified,
5869 * it will be taken from the implementation-dependent maximum
5870 * patch size (gl_MaxPatchVertices)."
5871 *
5872 * and about TCS outputs:
5873 *
5874 * "If no size is specified, it will be taken from output patch
5875 * size declared in the shader."
5876 *
5877 * The GL_OES_geometry_shader spec says:
5878 *
5879 * "All geometry shader input unsized array declarations will be
5880 * sized by an earlier input primitive layout qualifier, when
5881 * present, as per the following table."
5882 */
5883 const bool implicitly_sized =
5884 (var->data.mode == ir_var_shader_in &&
5885 state->stage >= MESA_SHADER_TESS_CTRL &&
5886 state->stage <= MESA_SHADER_GEOMETRY) ||
5887 (var->data.mode == ir_var_shader_out &&
5888 state->stage == MESA_SHADER_TESS_CTRL);
5889
5890 if (glsl_type_is_unsized_array(t) && !implicitly_sized)
5891 /* Section 10.17 of the GLSL ES 1.00 specification states that
5892 * unsized array declarations have been removed from the language.
5893 * Arrays that are sized using an initializer are still explicitly
5894 * sized. However, GLSL ES 1.00 does not allow array
5895 * initializers. That is only allowed in GLSL ES 3.00.
5896 *
5897 * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
5898 *
5899 * "An array type can also be formed without specifying a size
5900 * if the definition includes an initializer:
5901 *
5902 * float x[] = float[2] (1.0, 2.0); // declares an array of size 2
5903 * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
5904 *
5905 * float a[5];
5906 * float b[] = a;"
5907 */
5908 _mesa_glsl_error(& loc, state,
5909 "unsized array declarations are not allowed in "
5910 "GLSL ES");
5911 }
5912
5913 /* Section 4.4.6.1 Atomic Counter Layout Qualifiers of the GLSL 4.60 spec:
5914 *
5915 * "It is a compile-time error to declare an unsized array of
5916 * atomic_uint"
5917 */
5918 if (glsl_type_is_unsized_array(var->type) &&
5919 glsl_without_array(var->type)->base_type == GLSL_TYPE_ATOMIC_UINT) {
5920 _mesa_glsl_error(& loc, state,
5921 "Unsized array of atomic_uint is not allowed");
5922 }
5923
5924 /* If the declaration is not a redeclaration, there are a few additional
5925 * semantic checks that must be applied. In addition, variable that was
5926 * created for the declaration should be added to the IR stream.
5927 */
5928 if (!is_redeclaration) {
5929 validate_identifier(decl->identifier, loc, state);
5930
5931 /* Add the variable to the symbol table. Note that the initializer's
5932 * IR was already processed earlier (though it hasn't been emitted
5933 * yet), without the variable in scope.
5934 *
5935 * This differs from most C-like languages, but it follows the GLSL
5936 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
5937 * spec:
5938 *
5939 * "Within a declaration, the scope of a name starts immediately
5940 * after the initializer if present or immediately after the name
5941 * being declared if not."
5942 */
5943 if (!state->symbols->add_variable(var)) {
5944 YYLTYPE loc = this->get_location();
5945 _mesa_glsl_error(&loc, state, "name `%s' already taken in the "
5946 "current scope", decl->identifier);
5947 continue;
5948 }
5949
5950 /* Push the variable declaration to the top. It means that all the
5951 * variable declarations will appear in a funny last-to-first order,
5952 * but otherwise we run into trouble if a function is prototyped, a
5953 * global var is decled, then the function is defined with usage of
5954 * the global var. See glslparsertest's CorrectModule.frag.
5955 */
5956 instructions->push_head(var);
5957 }
5958
5959 instructions->append_list(&initializer_instructions);
5960 }
5961
5962
5963 /* Generally, variable declarations do not have r-values. However,
5964 * one is used for the declaration in
5965 *
5966 * while (bool b = some_condition()) {
5967 * ...
5968 * }
5969 *
5970 * so we return the rvalue from the last seen declaration here.
5971 */
5972 return result;
5973 }
5974
5975
5976 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)5977 ast_parameter_declarator::hir(exec_list *instructions,
5978 struct _mesa_glsl_parse_state *state)
5979 {
5980 void *ctx = state;
5981 const struct glsl_type *type;
5982 const char *name = NULL;
5983 YYLTYPE loc = this->get_location();
5984
5985 type = this->type->glsl_type(& name, state);
5986
5987 if (type == NULL) {
5988 if (name != NULL) {
5989 _mesa_glsl_error(& loc, state,
5990 "invalid type `%s' in declaration of `%s'",
5991 name, this->identifier);
5992 } else {
5993 _mesa_glsl_error(& loc, state,
5994 "invalid type in declaration of `%s'",
5995 this->identifier);
5996 }
5997
5998 type = &glsl_type_builtin_error;
5999 }
6000
6001 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
6002 *
6003 * "Functions that accept no input arguments need not use void in the
6004 * argument list because prototypes (or definitions) are required and
6005 * therefore there is no ambiguity when an empty argument list "( )" is
6006 * declared. The idiom "(void)" as a parameter list is provided for
6007 * convenience."
6008 *
6009 * Placing this check here prevents a void parameter being set up
6010 * for a function, which avoids tripping up checks for main taking
6011 * parameters and lookups of an unnamed symbol.
6012 */
6013 if (glsl_type_is_void(type)) {
6014 if (this->identifier != NULL)
6015 _mesa_glsl_error(& loc, state,
6016 "named parameter cannot have type `void'");
6017
6018 is_void = true;
6019 return NULL;
6020 }
6021
6022 if (formal_parameter && (this->identifier == NULL)) {
6023 _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
6024 return NULL;
6025 }
6026
6027 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
6028 * call already handled the "vec4[..] foo" case.
6029 */
6030 type = process_array_type(&loc, type, this->array_specifier, state);
6031
6032 if (!glsl_type_is_error(type) && glsl_type_is_unsized_array(type)) {
6033 _mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
6034 "a declared size");
6035 type = &glsl_type_builtin_error;
6036 }
6037
6038 is_void = false;
6039 ir_variable *var = new(ctx)
6040 ir_variable(type, this->identifier, ir_var_function_in);
6041
6042 /* Apply any specified qualifiers to the parameter declaration. Note that
6043 * for function parameters the default mode is 'in'.
6044 */
6045 apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc,
6046 true);
6047
6048 if (((1u << var->data.mode) & state->zero_init) &&
6049 (glsl_type_is_numeric(var->type) || glsl_type_is_boolean(var->type))) {
6050 const ir_constant_data data = { { 0 } };
6051 var->data.has_initializer = true;
6052 var->data.is_implicit_initializer = true;
6053 var->constant_initializer = new(var) ir_constant(var->type, &data);
6054 }
6055
6056 /* From section 4.1.7 of the GLSL 4.40 spec:
6057 *
6058 * "Opaque variables cannot be treated as l-values; hence cannot
6059 * be used as out or inout function parameters, nor can they be
6060 * assigned into."
6061 *
6062 * From section 4.1.7 of the ARB_bindless_texture spec:
6063 *
6064 * "Samplers can be used as l-values, so can be assigned into and used
6065 * as "out" and "inout" function parameters."
6066 *
6067 * From section 4.1.X of the ARB_bindless_texture spec:
6068 *
6069 * "Images can be used as l-values, so can be assigned into and used as
6070 * "out" and "inout" function parameters."
6071 */
6072 if ((var->data.mode == ir_var_function_inout || var->data.mode == ir_var_function_out)
6073 && (glsl_contains_atomic(type) ||
6074 (!state->has_bindless() && glsl_contains_opaque(type)))) {
6075 _mesa_glsl_error(&loc, state, "out and inout parameters cannot "
6076 "contain %s variables",
6077 state->has_bindless() ? "atomic" : "opaque");
6078 type = &glsl_type_builtin_error;
6079 }
6080
6081 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
6082 *
6083 * "When calling a function, expressions that do not evaluate to
6084 * l-values cannot be passed to parameters declared as out or inout."
6085 *
6086 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
6087 *
6088 * "Other binary or unary expressions, non-dereferenced arrays,
6089 * function names, swizzles with repeated fields, and constants
6090 * cannot be l-values."
6091 *
6092 * So for GLSL 1.10, passing an array as an out or inout parameter is not
6093 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
6094 */
6095 if ((var->data.mode == ir_var_function_inout || var->data.mode == ir_var_function_out)
6096 && glsl_type_is_array(type)
6097 && !state->check_version(state->allow_glsl_120_subset_in_110 ? 110 : 120, 100, &loc,
6098 "arrays cannot be out or inout parameters")) {
6099 type = &glsl_type_builtin_error;
6100 }
6101
6102 instructions->push_tail(var);
6103
6104 /* Parameter declarations do not have r-values.
6105 */
6106 return NULL;
6107 }
6108
6109
6110 void
parameters_to_hir(exec_list * ast_parameters,bool formal,exec_list * ir_parameters,_mesa_glsl_parse_state * state)6111 ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
6112 bool formal,
6113 exec_list *ir_parameters,
6114 _mesa_glsl_parse_state *state)
6115 {
6116 ast_parameter_declarator *void_param = NULL;
6117 unsigned count = 0;
6118
6119 foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
6120 param->formal_parameter = formal;
6121 param->hir(ir_parameters, state);
6122
6123 if (param->is_void)
6124 void_param = param;
6125
6126 count++;
6127 }
6128
6129 if ((void_param != NULL) && (count > 1)) {
6130 YYLTYPE loc = void_param->get_location();
6131
6132 _mesa_glsl_error(& loc, state,
6133 "`void' parameter must be only parameter");
6134 }
6135 }
6136
6137
6138 void
emit_function(_mesa_glsl_parse_state * state,ir_function * f)6139 emit_function(_mesa_glsl_parse_state *state, ir_function *f)
6140 {
6141 /* IR invariants disallow function declarations or definitions
6142 * nested within other function definitions. But there is no
6143 * requirement about the relative order of function declarations
6144 * and definitions with respect to one another. So simply insert
6145 * the new ir_function block at the end of the toplevel instruction
6146 * list.
6147 */
6148 state->toplevel_ir->push_tail(f);
6149 }
6150
6151
6152 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)6153 ast_function::hir(exec_list *instructions,
6154 struct _mesa_glsl_parse_state *state)
6155 {
6156 void *ctx = state;
6157 ir_function *f = NULL;
6158 ir_function_signature *sig = NULL;
6159 exec_list hir_parameters;
6160 YYLTYPE loc = this->get_location();
6161
6162 const char *const name = identifier;
6163
6164 /* New functions are always added to the top-level IR instruction stream,
6165 * so this instruction list pointer is ignored. See also emit_function
6166 * (called below).
6167 */
6168 (void) instructions;
6169
6170 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
6171 *
6172 * "Function declarations (prototypes) cannot occur inside of functions;
6173 * they must be at global scope, or for the built-in functions, outside
6174 * the global scope."
6175 *
6176 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
6177 *
6178 * "User defined functions may only be defined within the global scope."
6179 *
6180 * Note that this language does not appear in GLSL 1.10.
6181 */
6182 if ((state->current_function != NULL) &&
6183 state->is_version(120, 100)) {
6184 YYLTYPE loc = this->get_location();
6185 _mesa_glsl_error(&loc, state,
6186 "declaration of function `%s' not allowed within "
6187 "function body", name);
6188 }
6189
6190 validate_identifier(name, this->get_location(), state);
6191
6192 /* Convert the list of function parameters to HIR now so that they can be
6193 * used below to compare this function's signature with previously seen
6194 * signatures for functions with the same name.
6195 */
6196 ast_parameter_declarator::parameters_to_hir(& this->parameters,
6197 is_definition,
6198 & hir_parameters, state);
6199
6200 const char *return_type_name;
6201 const glsl_type *return_type =
6202 this->return_type->glsl_type(& return_type_name, state);
6203
6204 if (!return_type) {
6205 YYLTYPE loc = this->get_location();
6206 _mesa_glsl_error(&loc, state,
6207 "function `%s' has undeclared return type `%s'",
6208 name, return_type_name);
6209 return_type = &glsl_type_builtin_error;
6210 }
6211
6212 /* ARB_shader_subroutine states:
6213 * "Subroutine declarations cannot be prototyped. It is an error to prepend
6214 * subroutine(...) to a function declaration."
6215 */
6216 if (this->return_type->qualifier.subroutine_list && !is_definition) {
6217 YYLTYPE loc = this->get_location();
6218 _mesa_glsl_error(&loc, state,
6219 "function declaration `%s' cannot have subroutine prepended",
6220 name);
6221 }
6222
6223 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
6224 * "No qualifier is allowed on the return type of a function."
6225 */
6226 if (this->return_type->has_qualifiers(state)) {
6227 YYLTYPE loc = this->get_location();
6228 _mesa_glsl_error(& loc, state,
6229 "function `%s' return type has qualifiers", name);
6230 }
6231
6232 /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
6233 *
6234 * "Arrays are allowed as arguments and as the return type. In both
6235 * cases, the array must be explicitly sized."
6236 */
6237 if (glsl_type_is_unsized_array(return_type)) {
6238 YYLTYPE loc = this->get_location();
6239 _mesa_glsl_error(& loc, state,
6240 "function `%s' return type array must be explicitly "
6241 "sized", name);
6242 }
6243
6244 /* From Section 6.1 (Function Definitions) of the GLSL 1.00 spec:
6245 *
6246 * "Arrays are allowed as arguments, but not as the return type. [...]
6247 * The return type can also be a structure if the structure does not
6248 * contain an array."
6249 */
6250 if (state->language_version == 100 && glsl_contains_array(return_type)) {
6251 YYLTYPE loc = this->get_location();
6252 _mesa_glsl_error(& loc, state,
6253 "function `%s' return type contains an array", name);
6254 }
6255
6256 /* From section 4.1.7 of the GLSL 4.40 spec:
6257 *
6258 * "[Opaque types] can only be declared as function parameters
6259 * or uniform-qualified variables."
6260 *
6261 * The ARB_bindless_texture spec doesn't clearly state this, but as it says
6262 * "Replace Section 4.1.7 (Samplers), p. 25" and, "Replace Section 4.1.X,
6263 * (Images)", this should be allowed.
6264 */
6265 if (glsl_contains_atomic(return_type) ||
6266 (!state->has_bindless() && glsl_contains_opaque(return_type))) {
6267 YYLTYPE loc = this->get_location();
6268 _mesa_glsl_error(&loc, state,
6269 "function `%s' return type can't contain an %s type",
6270 name, state->has_bindless() ? "atomic" : "opaque");
6271 }
6272
6273 /**/
6274 if (glsl_type_is_subroutine(return_type)) {
6275 YYLTYPE loc = this->get_location();
6276 _mesa_glsl_error(&loc, state,
6277 "function `%s' return type can't be a subroutine type",
6278 name);
6279 }
6280
6281 /* Get the precision for the return type */
6282 unsigned return_precision;
6283
6284 if (state->es_shader) {
6285 YYLTYPE loc = this->get_location();
6286 return_precision =
6287 select_gles_precision(this->return_type->qualifier.precision,
6288 return_type,
6289 state,
6290 &loc);
6291 } else {
6292 return_precision = GLSL_PRECISION_NONE;
6293 }
6294
6295 /* Create an ir_function if one doesn't already exist. */
6296 f = state->symbols->get_function(name);
6297 if (f == NULL) {
6298 f = new(ctx) ir_function(name);
6299 if (!this->return_type->qualifier.is_subroutine_decl()) {
6300 if (!state->symbols->add_function(f)) {
6301 /* This function name shadows a non-function use of the same name. */
6302 YYLTYPE loc = this->get_location();
6303 _mesa_glsl_error(&loc, state, "function name `%s' conflicts with "
6304 "non-function", name);
6305 return NULL;
6306 }
6307 }
6308 emit_function(state, f);
6309 }
6310
6311 /* From GLSL ES 3.0 spec, chapter 6.1 "Function Definitions", page 71:
6312 *
6313 * "A shader cannot redefine or overload built-in functions."
6314 *
6315 * While in GLSL ES 1.0 specification, chapter 8 "Built-in Functions":
6316 *
6317 * "User code can overload the built-in functions but cannot redefine
6318 * them."
6319 */
6320 if (state->es_shader) {
6321 /* Local shader has no exact candidates; check the built-ins. */
6322 if (state->language_version >= 300 &&
6323 _mesa_glsl_has_builtin_function(state, name)) {
6324 YYLTYPE loc = this->get_location();
6325 _mesa_glsl_error(& loc, state,
6326 "A shader cannot redefine or overload built-in "
6327 "function `%s' in GLSL ES 3.00", name);
6328 return NULL;
6329 }
6330
6331 if (state->language_version == 100) {
6332 ir_function_signature *sig =
6333 _mesa_glsl_find_builtin_function(state, name, &hir_parameters);
6334 if (sig && sig->is_builtin()) {
6335 _mesa_glsl_error(& loc, state,
6336 "A shader cannot redefine built-in "
6337 "function `%s' in GLSL ES 1.00", name);
6338 }
6339 }
6340 }
6341
6342 /* Verify that this function's signature either doesn't match a previously
6343 * seen signature for a function with the same name, or, if a match is found,
6344 * that the previously seen signature does not have an associated definition.
6345 */
6346 if (state->es_shader || f->has_user_signature()) {
6347 sig = f->exact_matching_signature(state, &hir_parameters);
6348 if (sig != NULL) {
6349 const char *badvar = sig->qualifiers_match(&hir_parameters);
6350 if (badvar != NULL) {
6351 YYLTYPE loc = this->get_location();
6352
6353 _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
6354 "qualifiers don't match prototype", name, badvar);
6355 }
6356
6357 if (sig->return_type != return_type) {
6358 YYLTYPE loc = this->get_location();
6359
6360 _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
6361 "match prototype", name);
6362 }
6363
6364 if (sig->return_precision != return_precision) {
6365 YYLTYPE loc = this->get_location();
6366
6367 _mesa_glsl_error(&loc, state, "function `%s' return type precision "
6368 "doesn't match prototype", name);
6369 }
6370
6371 if (sig->is_defined) {
6372 if (is_definition) {
6373 YYLTYPE loc = this->get_location();
6374 _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
6375 } else {
6376 /* We just encountered a prototype that exactly matches a
6377 * function that's already been defined. This is redundant,
6378 * and we should ignore it.
6379 */
6380 return NULL;
6381 }
6382 } else if (state->language_version == 100 && !is_definition) {
6383 /* From the GLSL 1.00 spec, section 4.2.7:
6384 *
6385 * "A particular variable, structure or function declaration
6386 * may occur at most once within a scope with the exception
6387 * that a single function prototype plus the corresponding
6388 * function definition are allowed."
6389 */
6390 YYLTYPE loc = this->get_location();
6391 _mesa_glsl_error(&loc, state, "function `%s' redeclared", name);
6392 }
6393 }
6394 }
6395
6396 /* Verify the return type of main() */
6397 if (strcmp(name, "main") == 0) {
6398 if (! glsl_type_is_void(return_type)) {
6399 YYLTYPE loc = this->get_location();
6400
6401 _mesa_glsl_error(& loc, state, "main() must return void");
6402 }
6403
6404 if (!hir_parameters.is_empty()) {
6405 YYLTYPE loc = this->get_location();
6406
6407 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
6408 }
6409 }
6410
6411 /* Finish storing the information about this new function in its signature.
6412 */
6413 if (sig == NULL) {
6414 sig = new(ctx) ir_function_signature(return_type);
6415 sig->return_precision = return_precision;
6416 f->add_signature(sig);
6417 }
6418
6419 sig->replace_parameters(&hir_parameters);
6420 signature = sig;
6421
6422 if (this->return_type->qualifier.subroutine_list) {
6423 int idx;
6424
6425 if (this->return_type->qualifier.flags.q.explicit_index) {
6426 unsigned qual_index;
6427 if (process_qualifier_constant(state, &loc, "index",
6428 this->return_type->qualifier.index,
6429 &qual_index)) {
6430 if (!state->has_explicit_uniform_location()) {
6431 _mesa_glsl_error(&loc, state, "subroutine index requires "
6432 "GL_ARB_explicit_uniform_location or "
6433 "GLSL 4.30");
6434 } else if (qual_index >= MAX_SUBROUTINES) {
6435 _mesa_glsl_error(&loc, state,
6436 "invalid subroutine index (%d) index must "
6437 "be a number between 0 and "
6438 "GL_MAX_SUBROUTINES - 1 (%d)", qual_index,
6439 MAX_SUBROUTINES - 1);
6440 } else {
6441 f->subroutine_index = qual_index;
6442 }
6443 }
6444 }
6445
6446 f->num_subroutine_types = this->return_type->qualifier.subroutine_list->declarations.length();
6447 f->subroutine_types = ralloc_array(state, const struct glsl_type *,
6448 f->num_subroutine_types);
6449 idx = 0;
6450 foreach_list_typed(ast_declaration, decl, link, &this->return_type->qualifier.subroutine_list->declarations) {
6451 const struct glsl_type *type;
6452 /* the subroutine type must be already declared */
6453 type = state->symbols->get_type(decl->identifier);
6454 if (!type) {
6455 _mesa_glsl_error(& loc, state, "unknown type '%s' in subroutine function definition", decl->identifier);
6456 }
6457
6458 for (int i = 0; i < state->num_subroutine_types; i++) {
6459 ir_function *fn = state->subroutine_types[i];
6460 ir_function_signature *tsig = NULL;
6461
6462 if (strcmp(fn->name, decl->identifier))
6463 continue;
6464
6465 tsig = fn->matching_signature(state, &sig->parameters,
6466 state->has_implicit_conversions(),
6467 state->has_implicit_int_to_uint_conversion(),
6468 false);
6469 if (!tsig) {
6470 _mesa_glsl_error(& loc, state, "subroutine type mismatch '%s' - signatures do not match\n", decl->identifier);
6471 } else {
6472 if (tsig->return_type != sig->return_type) {
6473 _mesa_glsl_error(& loc, state, "subroutine type mismatch '%s' - return types do not match\n", decl->identifier);
6474 }
6475 }
6476 }
6477 f->subroutine_types[idx++] = type;
6478 }
6479 state->subroutines = (ir_function **)reralloc(state, state->subroutines,
6480 ir_function *,
6481 state->num_subroutines + 1);
6482 state->subroutines[state->num_subroutines] = f;
6483 state->num_subroutines++;
6484
6485 }
6486
6487 if (this->return_type->qualifier.is_subroutine_decl()) {
6488 if (!state->symbols->add_type(this->identifier, glsl_subroutine_type(this->identifier))) {
6489 _mesa_glsl_error(& loc, state, "type '%s' previously defined", this->identifier);
6490 return NULL;
6491 }
6492 state->subroutine_types = (ir_function **)reralloc(state, state->subroutine_types,
6493 ir_function *,
6494 state->num_subroutine_types + 1);
6495 state->subroutine_types[state->num_subroutine_types] = f;
6496 state->num_subroutine_types++;
6497
6498 f->is_subroutine = true;
6499 }
6500
6501 /* Function declarations (prototypes) do not have r-values.
6502 */
6503 return NULL;
6504 }
6505
6506
6507 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)6508 ast_function_definition::hir(exec_list *instructions,
6509 struct _mesa_glsl_parse_state *state)
6510 {
6511 prototype->is_definition = true;
6512 prototype->hir(instructions, state);
6513
6514 ir_function_signature *signature = prototype->signature;
6515 if (signature == NULL)
6516 return NULL;
6517
6518 assert(state->current_function == NULL);
6519 state->current_function = signature;
6520 state->found_return = false;
6521 state->found_begin_interlock = false;
6522 state->found_end_interlock = false;
6523
6524 /* Duplicate parameters declared in the prototype as concrete variables.
6525 * Add these to the symbol table.
6526 */
6527 state->symbols->push_scope();
6528 foreach_in_list(ir_variable, var, &signature->parameters) {
6529 assert(var->as_variable() != NULL);
6530
6531 /* The only way a parameter would "exist" is if two parameters have
6532 * the same name.
6533 */
6534 if (state->symbols->name_declared_this_scope(var->name)) {
6535 YYLTYPE loc = this->get_location();
6536
6537 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
6538 } else {
6539 state->symbols->add_variable(var);
6540 }
6541 }
6542
6543 /* Convert the body of the function to HIR. */
6544 this->body->hir(&signature->body, state);
6545 signature->is_defined = true;
6546
6547 state->symbols->pop_scope();
6548
6549 assert(state->current_function == signature);
6550 state->current_function = NULL;
6551
6552 if (!glsl_type_is_void(signature->return_type) && !state->found_return) {
6553 YYLTYPE loc = this->get_location();
6554 _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
6555 "%s, but no return statement",
6556 signature->function_name(),
6557 glsl_get_type_name(signature->return_type));
6558 }
6559
6560 /* Function definitions do not have r-values.
6561 */
6562 return NULL;
6563 }
6564
6565
6566 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)6567 ast_jump_statement::hir(exec_list *instructions,
6568 struct _mesa_glsl_parse_state *state)
6569 {
6570 void *ctx = state;
6571
6572 switch (mode) {
6573 case ast_return: {
6574 ir_return *inst;
6575 assert(state->current_function);
6576
6577 if (opt_return_value) {
6578 ir_rvalue *ret = opt_return_value->hir(instructions, state);
6579
6580 /* The value of the return type can be NULL if the shader says
6581 * 'return foo();' and foo() is a function that returns void.
6582 *
6583 * NOTE: The GLSL spec doesn't say that this is an error. The type
6584 * of the return value is void. If the return type of the function is
6585 * also void, then this should compile without error. Seriously.
6586 */
6587 const glsl_type *const ret_type =
6588 (ret == NULL) ? &glsl_type_builtin_void : ret->type;
6589
6590 /* Implicit conversions are not allowed for return values prior to
6591 * ARB_shading_language_420pack.
6592 */
6593 if (state->current_function->return_type != ret_type) {
6594 YYLTYPE loc = this->get_location();
6595
6596 if (state->has_420pack()) {
6597 if (!apply_implicit_conversion(state->current_function->return_type,
6598 ret, state)
6599 || (ret->type != state->current_function->return_type)) {
6600 _mesa_glsl_error(& loc, state,
6601 "could not implicitly convert return value "
6602 "to %s, in function `%s'",
6603 glsl_get_type_name(state->current_function->return_type),
6604 state->current_function->function_name());
6605 }
6606 } else {
6607 _mesa_glsl_error(& loc, state,
6608 "`return' with wrong type %s, in function `%s' "
6609 "returning %s",
6610 glsl_get_type_name(ret_type),
6611 state->current_function->function_name(),
6612 glsl_get_type_name(state->current_function->return_type));
6613 }
6614 } else if (state->current_function->return_type->base_type ==
6615 GLSL_TYPE_VOID) {
6616 YYLTYPE loc = this->get_location();
6617
6618 /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
6619 * specs add a clarification:
6620 *
6621 * "A void function can only use return without a return argument, even if
6622 * the return argument has void type. Return statements only accept values:
6623 *
6624 * void func1() { }
6625 * void func2() { return func1(); } // illegal return statement"
6626 */
6627 _mesa_glsl_error(& loc, state,
6628 "void functions can only use `return' without a "
6629 "return argument");
6630 }
6631
6632 inst = new(ctx) ir_return(ret);
6633 } else {
6634 if (state->current_function->return_type->base_type !=
6635 GLSL_TYPE_VOID) {
6636 YYLTYPE loc = this->get_location();
6637
6638 _mesa_glsl_error(& loc, state,
6639 "`return' with no value, in function %s returning "
6640 "non-void",
6641 state->current_function->function_name());
6642 }
6643 inst = new(ctx) ir_return;
6644 }
6645
6646 state->found_return = true;
6647 instructions->push_tail(inst);
6648 break;
6649 }
6650
6651 case ast_discard:
6652 if (state->stage != MESA_SHADER_FRAGMENT) {
6653 YYLTYPE loc = this->get_location();
6654
6655 _mesa_glsl_error(& loc, state,
6656 "`discard' may only appear in a fragment shader");
6657 }
6658 instructions->push_tail(new(ctx) ir_discard);
6659 break;
6660
6661 case ast_break:
6662 case ast_continue:
6663 if (mode == ast_continue &&
6664 state->loop_nesting_ast == NULL) {
6665 YYLTYPE loc = this->get_location();
6666
6667 _mesa_glsl_error(& loc, state, "continue may only appear in a loop");
6668 } else if (mode == ast_break &&
6669 state->loop_nesting_ast == NULL &&
6670 state->switch_state.switch_nesting_ast == NULL) {
6671 YYLTYPE loc = this->get_location();
6672
6673 _mesa_glsl_error(& loc, state,
6674 "break may only appear in a loop or a switch");
6675 } else {
6676 /* For a loop, inline the for loop expression again, since we don't
6677 * know where near the end of the loop body the normal copy of it is
6678 * going to be placed. Same goes for the condition for a do-while
6679 * loop.
6680 */
6681 if (state->loop_nesting_ast != NULL &&
6682 mode == ast_continue && !state->switch_state.is_switch_innermost) {
6683 if (state->loop_nesting_ast->rest_expression) {
6684 clone_ir_list(ctx, instructions,
6685 &state->loop_nesting_ast->rest_instructions);
6686 }
6687 if (state->loop_nesting_ast->mode ==
6688 ast_iteration_statement::ast_do_while) {
6689 state->loop_nesting_ast->condition_to_hir(instructions, state);
6690 }
6691 }
6692
6693 if (state->switch_state.is_switch_innermost &&
6694 mode == ast_continue) {
6695 /* Set 'continue_inside' to true. */
6696 ir_rvalue *const true_val = new (ctx) ir_constant(true);
6697 ir_dereference_variable *deref_continue_inside_var =
6698 new(ctx) ir_dereference_variable(state->switch_state.continue_inside);
6699 instructions->push_tail(new(ctx) ir_assignment(deref_continue_inside_var,
6700 true_val));
6701
6702 /* Break out from the switch, continue for the loop will
6703 * be called right after switch. */
6704 ir_loop_jump *const jump =
6705 new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
6706 instructions->push_tail(jump);
6707
6708 } else if (state->switch_state.is_switch_innermost &&
6709 mode == ast_break) {
6710 /* Force break out of switch by inserting a break. */
6711 ir_loop_jump *const jump =
6712 new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
6713 instructions->push_tail(jump);
6714 } else {
6715 ir_loop_jump *const jump =
6716 new(ctx) ir_loop_jump((mode == ast_break)
6717 ? ir_loop_jump::jump_break
6718 : ir_loop_jump::jump_continue);
6719 instructions->push_tail(jump);
6720 }
6721 }
6722
6723 break;
6724 }
6725
6726 /* Jump instructions do not have r-values.
6727 */
6728 return NULL;
6729 }
6730
6731
6732 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)6733 ast_demote_statement::hir(exec_list *instructions,
6734 struct _mesa_glsl_parse_state *state)
6735 {
6736 void *ctx = state;
6737
6738 if (state->stage != MESA_SHADER_FRAGMENT) {
6739 YYLTYPE loc = this->get_location();
6740
6741 _mesa_glsl_error(& loc, state,
6742 "`demote' may only appear in a fragment shader");
6743 }
6744
6745 instructions->push_tail(new(ctx) ir_demote);
6746
6747 return NULL;
6748 }
6749
6750
6751 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)6752 ast_selection_statement::hir(exec_list *instructions,
6753 struct _mesa_glsl_parse_state *state)
6754 {
6755 void *ctx = state;
6756
6757 ir_rvalue *const condition = this->condition->hir(instructions, state);
6758
6759 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
6760 *
6761 * "Any expression whose type evaluates to a Boolean can be used as the
6762 * conditional expression bool-expression. Vector types are not accepted
6763 * as the expression to if."
6764 *
6765 * The checks are separated so that higher quality diagnostics can be
6766 * generated for cases where both rules are violated.
6767 */
6768 if (!glsl_type_is_boolean(condition->type) || !glsl_type_is_scalar(condition->type)) {
6769 YYLTYPE loc = this->condition->get_location();
6770
6771 _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
6772 "boolean");
6773 }
6774
6775 ir_if *const stmt = new(ctx) ir_if(condition);
6776
6777 if (then_statement != NULL) {
6778 state->symbols->push_scope();
6779 then_statement->hir(& stmt->then_instructions, state);
6780 state->symbols->pop_scope();
6781 }
6782
6783 if (else_statement != NULL) {
6784 state->symbols->push_scope();
6785 else_statement->hir(& stmt->else_instructions, state);
6786 state->symbols->pop_scope();
6787 }
6788
6789 instructions->push_tail(stmt);
6790
6791 /* if-statements do not have r-values.
6792 */
6793 return NULL;
6794 }
6795
6796
6797 struct case_label {
6798 /** Value of the case label. */
6799 unsigned value;
6800
6801 /** Does this label occur after the default? */
6802 bool after_default;
6803
6804 /**
6805 * AST for the case label.
6806 *
6807 * This is only used to generate error messages for duplicate labels.
6808 */
6809 ast_expression *ast;
6810 };
6811
6812 /* Used for detection of duplicate case values, compare
6813 * given contents directly.
6814 */
6815 static bool
compare_case_value(const void * a,const void * b)6816 compare_case_value(const void *a, const void *b)
6817 {
6818 return ((struct case_label *) a)->value == ((struct case_label *) b)->value;
6819 }
6820
6821
6822 /* Used for detection of duplicate case values, just
6823 * returns key contents as is.
6824 */
6825 static unsigned
key_contents(const void * key)6826 key_contents(const void *key)
6827 {
6828 return ((struct case_label *) key)->value;
6829 }
6830
6831 void
eval_test_expression(exec_list * instructions,struct _mesa_glsl_parse_state * state)6832 ast_switch_statement::eval_test_expression(exec_list *instructions,
6833 struct _mesa_glsl_parse_state *state)
6834 {
6835 if (test_val == NULL)
6836 test_val = this->test_expression->hir(instructions, state);
6837 }
6838
6839 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)6840 ast_switch_statement::hir(exec_list *instructions,
6841 struct _mesa_glsl_parse_state *state)
6842 {
6843 void *ctx = state;
6844
6845 this->eval_test_expression(instructions, state);
6846
6847 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
6848 *
6849 * "The type of init-expression in a switch statement must be a
6850 * scalar integer."
6851 */
6852 if (!glsl_type_is_scalar(test_val->type) ||
6853 !glsl_type_is_integer_32(test_val->type)) {
6854 YYLTYPE loc = this->test_expression->get_location();
6855
6856 _mesa_glsl_error(& loc,
6857 state,
6858 "switch-statement expression must be scalar "
6859 "integer");
6860 return NULL;
6861 }
6862
6863 /* Track the switch-statement nesting in a stack-like manner.
6864 */
6865 struct glsl_switch_state saved = state->switch_state;
6866
6867 state->switch_state.is_switch_innermost = true;
6868 state->switch_state.switch_nesting_ast = this;
6869 state->switch_state.labels_ht =
6870 _mesa_hash_table_create(NULL, key_contents,
6871 compare_case_value);
6872 state->switch_state.previous_default = NULL;
6873
6874 /* Initalize is_fallthru state to false.
6875 */
6876 ir_rvalue *const is_fallthru_val = new (ctx) ir_constant(false);
6877 state->switch_state.is_fallthru_var =
6878 new(ctx) ir_variable(&glsl_type_builtin_bool,
6879 "switch_is_fallthru_tmp",
6880 ir_var_temporary);
6881 instructions->push_tail(state->switch_state.is_fallthru_var);
6882
6883 ir_dereference_variable *deref_is_fallthru_var =
6884 new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var);
6885 instructions->push_tail(new(ctx) ir_assignment(deref_is_fallthru_var,
6886 is_fallthru_val));
6887
6888 /* Initialize continue_inside state to false.
6889 */
6890 state->switch_state.continue_inside =
6891 new(ctx) ir_variable(&glsl_type_builtin_bool,
6892 "continue_inside_tmp",
6893 ir_var_temporary);
6894 instructions->push_tail(state->switch_state.continue_inside);
6895
6896 ir_rvalue *const false_val = new (ctx) ir_constant(false);
6897 ir_dereference_variable *deref_continue_inside_var =
6898 new(ctx) ir_dereference_variable(state->switch_state.continue_inside);
6899 instructions->push_tail(new(ctx) ir_assignment(deref_continue_inside_var,
6900 false_val));
6901
6902 state->switch_state.run_default =
6903 new(ctx) ir_variable(&glsl_type_builtin_bool,
6904 "run_default_tmp",
6905 ir_var_temporary);
6906 instructions->push_tail(state->switch_state.run_default);
6907
6908 /* Loop around the switch is used for flow control. */
6909 ir_loop * loop = new(ctx) ir_loop();
6910 instructions->push_tail(loop);
6911
6912 /* Cache test expression.
6913 */
6914 test_to_hir(&loop->body_instructions, state);
6915
6916 /* Emit code for body of switch stmt.
6917 */
6918 body->hir(&loop->body_instructions, state);
6919
6920 /* Insert a break at the end to exit loop. */
6921 ir_loop_jump *jump = new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
6922 loop->body_instructions.push_tail(jump);
6923
6924 /* If we are inside loop, check if continue got called inside switch. */
6925 if (state->loop_nesting_ast != NULL) {
6926 ir_dereference_variable *deref_continue_inside =
6927 new(ctx) ir_dereference_variable(state->switch_state.continue_inside);
6928 ir_if *irif = new(ctx) ir_if(deref_continue_inside);
6929 ir_loop_jump *jump = new(ctx) ir_loop_jump(ir_loop_jump::jump_continue);
6930
6931 if (state->loop_nesting_ast != NULL) {
6932 if (state->loop_nesting_ast->rest_expression) {
6933 clone_ir_list(ctx, &irif->then_instructions,
6934 &state->loop_nesting_ast->rest_instructions);
6935 }
6936 if (state->loop_nesting_ast->mode ==
6937 ast_iteration_statement::ast_do_while) {
6938 state->loop_nesting_ast->condition_to_hir(&irif->then_instructions, state);
6939 }
6940 }
6941 irif->then_instructions.push_tail(jump);
6942 instructions->push_tail(irif);
6943 }
6944
6945 _mesa_hash_table_destroy(state->switch_state.labels_ht, NULL);
6946
6947 state->switch_state = saved;
6948
6949 /* Switch statements do not have r-values. */
6950 return NULL;
6951 }
6952
6953
6954 void
test_to_hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)6955 ast_switch_statement::test_to_hir(exec_list *instructions,
6956 struct _mesa_glsl_parse_state *state)
6957 {
6958 void *ctx = state;
6959
6960 /* set to true to avoid a duplicate "use of uninitialized variable" warning
6961 * on the switch test case. The first one would be already raised when
6962 * getting the test_expression at ast_switch_statement::hir
6963 */
6964 test_expression->set_is_lhs(true);
6965 /* Cache value of test expression. */
6966 this->eval_test_expression(instructions, state);
6967
6968 state->switch_state.test_var = new(ctx) ir_variable(test_val->type,
6969 "switch_test_tmp",
6970 ir_var_temporary);
6971 ir_dereference_variable *deref_test_var =
6972 new(ctx) ir_dereference_variable(state->switch_state.test_var);
6973
6974 instructions->push_tail(state->switch_state.test_var);
6975 instructions->push_tail(new(ctx) ir_assignment(deref_test_var, test_val));
6976 }
6977
6978
6979 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)6980 ast_switch_body::hir(exec_list *instructions,
6981 struct _mesa_glsl_parse_state *state)
6982 {
6983 if (stmts != NULL) {
6984 state->symbols->push_scope();
6985 stmts->hir(instructions, state);
6986 state->symbols->pop_scope();
6987 }
6988
6989 /* Switch bodies do not have r-values. */
6990 return NULL;
6991 }
6992
6993 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)6994 ast_case_statement_list::hir(exec_list *instructions,
6995 struct _mesa_glsl_parse_state *state)
6996 {
6997 exec_list default_case, after_default, tmp;
6998
6999 foreach_list_typed (ast_case_statement, case_stmt, link, & this->cases) {
7000 case_stmt->hir(&tmp, state);
7001
7002 /* Default case. */
7003 if (state->switch_state.previous_default && default_case.is_empty()) {
7004 default_case.append_list(&tmp);
7005 continue;
7006 }
7007
7008 /* If default case found, append 'after_default' list. */
7009 if (!default_case.is_empty())
7010 after_default.append_list(&tmp);
7011 else
7012 instructions->append_list(&tmp);
7013 }
7014
7015 /* Handle the default case. This is done here because default might not be
7016 * the last case. We need to add checks against following cases first to see
7017 * if default should be chosen or not.
7018 */
7019 if (!default_case.is_empty()) {
7020 ir_factory body(instructions, state);
7021
7022 ir_expression *cmp = NULL;
7023
7024 hash_table_foreach(state->switch_state.labels_ht, entry) {
7025 const struct case_label *const l = (struct case_label *) entry->data;
7026
7027 /* If the switch init-value is the value of one of the labels that
7028 * occurs after the default case, disable execution of the default
7029 * case.
7030 */
7031 if (l->after_default) {
7032 ir_constant *const cnst =
7033 state->switch_state.test_var->type->base_type == GLSL_TYPE_UINT
7034 ? body.constant(unsigned(l->value))
7035 : body.constant(int(l->value));
7036
7037 cmp = cmp == NULL
7038 ? equal(cnst, state->switch_state.test_var)
7039 : logic_or(cmp, equal(cnst, state->switch_state.test_var));
7040 }
7041 }
7042
7043 if (cmp != NULL)
7044 body.emit(assign(state->switch_state.run_default, logic_not(cmp)));
7045 else
7046 body.emit(assign(state->switch_state.run_default, body.constant(true)));
7047
7048 /* Append default case and all cases after it. */
7049 instructions->append_list(&default_case);
7050 instructions->append_list(&after_default);
7051 }
7052
7053 /* Case statements do not have r-values. */
7054 return NULL;
7055 }
7056
7057 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)7058 ast_case_statement::hir(exec_list *instructions,
7059 struct _mesa_glsl_parse_state *state)
7060 {
7061 labels->hir(instructions, state);
7062
7063 /* Guard case statements depending on fallthru state. */
7064 ir_dereference_variable *const deref_fallthru_guard =
7065 new(state) ir_dereference_variable(state->switch_state.is_fallthru_var);
7066 ir_if *const test_fallthru = new(state) ir_if(deref_fallthru_guard);
7067
7068 foreach_list_typed (ast_node, stmt, link, & this->stmts)
7069 stmt->hir(& test_fallthru->then_instructions, state);
7070
7071 instructions->push_tail(test_fallthru);
7072
7073 /* Case statements do not have r-values. */
7074 return NULL;
7075 }
7076
7077
7078 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)7079 ast_case_label_list::hir(exec_list *instructions,
7080 struct _mesa_glsl_parse_state *state)
7081 {
7082 foreach_list_typed (ast_case_label, label, link, & this->labels)
7083 label->hir(instructions, state);
7084
7085 /* Case labels do not have r-values. */
7086 return NULL;
7087 }
7088
7089 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)7090 ast_case_label::hir(exec_list *instructions,
7091 struct _mesa_glsl_parse_state *state)
7092 {
7093 ir_factory body(instructions, state);
7094
7095 ir_variable *const fallthru_var = state->switch_state.is_fallthru_var;
7096
7097 /* If not default case, ... */
7098 if (this->test_value != NULL) {
7099 /* Conditionally set fallthru state based on
7100 * comparison of cached test expression value to case label.
7101 */
7102 ir_rvalue *const label_rval = this->test_value->hir(instructions, state);
7103 ir_constant *label_const =
7104 label_rval->constant_expression_value(body.mem_ctx);
7105
7106 if (!label_const) {
7107 YYLTYPE loc = this->test_value->get_location();
7108
7109 _mesa_glsl_error(& loc, state,
7110 "switch statement case label must be a "
7111 "constant expression");
7112
7113 /* Stuff a dummy value in to allow processing to continue. */
7114 label_const = body.constant(0);
7115 } else {
7116 hash_entry *entry =
7117 _mesa_hash_table_search(state->switch_state.labels_ht,
7118 &label_const->value.u[0]);
7119
7120 if (entry) {
7121 const struct case_label *const l =
7122 (struct case_label *) entry->data;
7123 const ast_expression *const previous_label = l->ast;
7124 YYLTYPE loc = this->test_value->get_location();
7125
7126 _mesa_glsl_error(& loc, state, "duplicate case value");
7127
7128 loc = previous_label->get_location();
7129 _mesa_glsl_error(& loc, state, "this is the previous case label");
7130 } else {
7131 struct case_label *l = ralloc(state->switch_state.labels_ht,
7132 struct case_label);
7133
7134 l->value = label_const->value.u[0];
7135 l->after_default = state->switch_state.previous_default != NULL;
7136 l->ast = this->test_value;
7137
7138 _mesa_hash_table_insert(state->switch_state.labels_ht,
7139 &label_const->value.u[0],
7140 l);
7141 }
7142 }
7143
7144 /* Create an r-value version of the ir_constant label here (after we may
7145 * have created a fake one in error cases) that can be passed to
7146 * apply_implicit_conversion below.
7147 */
7148 ir_rvalue *label = label_const;
7149
7150 ir_rvalue *deref_test_var =
7151 new(body.mem_ctx) ir_dereference_variable(state->switch_state.test_var);
7152
7153 /*
7154 * From GLSL 4.40 specification section 6.2 ("Selection"):
7155 *
7156 * "The type of the init-expression value in a switch statement must
7157 * be a scalar int or uint. The type of the constant-expression value
7158 * in a case label also must be a scalar int or uint. When any pair
7159 * of these values is tested for "equal value" and the types do not
7160 * match, an implicit conversion will be done to convert the int to a
7161 * uint (see section 4.1.10 “Implicit Conversions”) before the compare
7162 * is done."
7163 */
7164 if (label->type != state->switch_state.test_var->type) {
7165 YYLTYPE loc = this->test_value->get_location();
7166
7167 const glsl_type *type_a = label->type;
7168 const glsl_type *type_b = state->switch_state.test_var->type;
7169
7170 /* Check if int->uint implicit conversion is supported. */
7171 bool integer_conversion_supported =
7172 _mesa_glsl_can_implicitly_convert(&glsl_type_builtin_int, &glsl_type_builtin_uint,
7173 state->has_implicit_conversions(),
7174 state->has_implicit_int_to_uint_conversion());
7175
7176 if ((!glsl_type_is_integer_32(type_a) || !glsl_type_is_integer_32(type_b)) ||
7177 !integer_conversion_supported) {
7178 _mesa_glsl_error(&loc, state, "type mismatch with switch "
7179 "init-expression and case label (%s != %s)",
7180 glsl_get_type_name(type_a), glsl_get_type_name(type_b));
7181 } else {
7182 /* Conversion of the case label. */
7183 if (type_a->base_type == GLSL_TYPE_INT) {
7184 if (!apply_implicit_conversion(&glsl_type_builtin_uint,
7185 label, state))
7186 _mesa_glsl_error(&loc, state, "implicit type conversion error");
7187 } else {
7188 /* Conversion of the init-expression value. */
7189 if (!apply_implicit_conversion(&glsl_type_builtin_uint,
7190 deref_test_var, state))
7191 _mesa_glsl_error(&loc, state, "implicit type conversion error");
7192 }
7193 }
7194
7195 /* If the implicit conversion was allowed, the types will already be
7196 * the same. If the implicit conversion wasn't allowed, smash the
7197 * type of the label anyway. This will prevent the expression
7198 * constructor (below) from failing an assertion.
7199 */
7200 label->type = deref_test_var->type;
7201 }
7202
7203 body.emit(assign(fallthru_var,
7204 logic_or(fallthru_var, equal(label, deref_test_var))));
7205 } else { /* default case */
7206 if (state->switch_state.previous_default) {
7207 YYLTYPE loc = this->get_location();
7208 _mesa_glsl_error(& loc, state,
7209 "multiple default labels in one switch");
7210
7211 loc = state->switch_state.previous_default->get_location();
7212 _mesa_glsl_error(& loc, state, "this is the first default label");
7213 }
7214 state->switch_state.previous_default = this;
7215
7216 /* Set fallthru condition on 'run_default' bool. */
7217 body.emit(assign(fallthru_var,
7218 logic_or(fallthru_var,
7219 state->switch_state.run_default)));
7220 }
7221
7222 /* Case statements do not have r-values. */
7223 return NULL;
7224 }
7225
7226 void
condition_to_hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)7227 ast_iteration_statement::condition_to_hir(exec_list *instructions,
7228 struct _mesa_glsl_parse_state *state)
7229 {
7230 void *ctx = state;
7231
7232 if (condition != NULL) {
7233 ir_rvalue *const cond =
7234 condition->hir(instructions, state);
7235
7236 if ((cond == NULL)
7237 || !glsl_type_is_boolean(cond->type) || !glsl_type_is_scalar(cond->type)) {
7238 YYLTYPE loc = condition->get_location();
7239
7240 _mesa_glsl_error(& loc, state,
7241 "loop condition must be scalar boolean");
7242 } else {
7243 /* As the first code in the loop body, generate a block that looks
7244 * like 'if (!condition) break;' as the loop termination condition.
7245 */
7246 ir_rvalue *const not_cond =
7247 new(ctx) ir_expression(ir_unop_logic_not, cond);
7248
7249 ir_if *const if_stmt = new(ctx) ir_if(not_cond);
7250
7251 ir_jump *const break_stmt =
7252 new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
7253
7254 if_stmt->then_instructions.push_tail(break_stmt);
7255 instructions->push_tail(if_stmt);
7256 }
7257 }
7258 }
7259
7260
7261 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)7262 ast_iteration_statement::hir(exec_list *instructions,
7263 struct _mesa_glsl_parse_state *state)
7264 {
7265 void *ctx = state;
7266
7267 /* For-loops and while-loops start a new scope, but do-while loops do not.
7268 */
7269 if (mode != ast_do_while)
7270 state->symbols->push_scope();
7271
7272 if (init_statement != NULL)
7273 init_statement->hir(instructions, state);
7274
7275 ir_loop *const stmt = new(ctx) ir_loop();
7276 instructions->push_tail(stmt);
7277
7278 /* Track the current loop nesting. */
7279 ast_iteration_statement *nesting_ast = state->loop_nesting_ast;
7280
7281 state->loop_nesting_ast = this;
7282
7283 /* Likewise, indicate that following code is closest to a loop,
7284 * NOT closest to a switch.
7285 */
7286 bool saved_is_switch_innermost = state->switch_state.is_switch_innermost;
7287 state->switch_state.is_switch_innermost = false;
7288
7289 if (mode != ast_do_while)
7290 condition_to_hir(&stmt->body_instructions, state);
7291
7292 if (rest_expression != NULL)
7293 rest_expression->hir(&rest_instructions, state);
7294
7295 if (body != NULL) {
7296 if (mode == ast_do_while)
7297 state->symbols->push_scope();
7298
7299 body->hir(& stmt->body_instructions, state);
7300
7301 if (mode == ast_do_while)
7302 state->symbols->pop_scope();
7303 }
7304
7305 if (rest_expression != NULL)
7306 stmt->body_instructions.append_list(&rest_instructions);
7307
7308 if (mode == ast_do_while)
7309 condition_to_hir(&stmt->body_instructions, state);
7310
7311 if (mode != ast_do_while)
7312 state->symbols->pop_scope();
7313
7314 /* Restore previous nesting before returning. */
7315 state->loop_nesting_ast = nesting_ast;
7316 state->switch_state.is_switch_innermost = saved_is_switch_innermost;
7317
7318 /* Loops do not have r-values.
7319 */
7320 return NULL;
7321 }
7322
7323
7324 /**
7325 * Determine if the given type is valid for establishing a default precision
7326 * qualifier.
7327 *
7328 * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
7329 *
7330 * "The precision statement
7331 *
7332 * precision precision-qualifier type;
7333 *
7334 * can be used to establish a default precision qualifier. The type field
7335 * can be either int or float or any of the sampler types, and the
7336 * precision-qualifier can be lowp, mediump, or highp."
7337 *
7338 * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
7339 * qualifiers on sampler types, but this seems like an oversight (since the
7340 * intention of including these in GLSL 1.30 is to allow compatibility with ES
7341 * shaders). So we allow int, float, and all sampler types regardless of GLSL
7342 * version.
7343 */
7344 static bool
is_valid_default_precision_type(const struct glsl_type * const type)7345 is_valid_default_precision_type(const struct glsl_type *const type)
7346 {
7347 if (type == NULL)
7348 return false;
7349
7350 switch (type->base_type) {
7351 case GLSL_TYPE_INT:
7352 case GLSL_TYPE_FLOAT:
7353 /* "int" and "float" are valid, but vectors and matrices are not. */
7354 return type->vector_elements == 1 && type->matrix_columns == 1;
7355 case GLSL_TYPE_SAMPLER:
7356 case GLSL_TYPE_TEXTURE:
7357 case GLSL_TYPE_IMAGE:
7358 case GLSL_TYPE_ATOMIC_UINT:
7359 return true;
7360 default:
7361 return false;
7362 }
7363 }
7364
7365
7366 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)7367 ast_type_specifier::hir(exec_list *instructions,
7368 struct _mesa_glsl_parse_state *state)
7369 {
7370 if (this->default_precision == ast_precision_none && this->structure == NULL)
7371 return NULL;
7372
7373 YYLTYPE loc = this->get_location();
7374
7375 /* If this is a precision statement, check that the type to which it is
7376 * applied is either float or int.
7377 *
7378 * From section 4.5.3 of the GLSL 1.30 spec:
7379 * "The precision statement
7380 * precision precision-qualifier type;
7381 * can be used to establish a default precision qualifier. The type
7382 * field can be either int or float [...]. Any other types or
7383 * qualifiers will result in an error.
7384 */
7385 if (this->default_precision != ast_precision_none) {
7386 if (!state->check_precision_qualifiers_allowed(&loc))
7387 return NULL;
7388
7389 if (this->structure != NULL) {
7390 _mesa_glsl_error(&loc, state,
7391 "precision qualifiers do not apply to structures");
7392 return NULL;
7393 }
7394
7395 if (this->array_specifier != NULL) {
7396 _mesa_glsl_error(&loc, state,
7397 "default precision statements do not apply to "
7398 "arrays");
7399 return NULL;
7400 }
7401
7402 const struct glsl_type *const type =
7403 state->symbols->get_type(this->type_name);
7404 if (!is_valid_default_precision_type(type)) {
7405 _mesa_glsl_error(&loc, state,
7406 "default precision statements apply only to "
7407 "float, int, and opaque types");
7408 return NULL;
7409 }
7410
7411 if (state->es_shader) {
7412 /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
7413 * spec says:
7414 *
7415 * "Non-precision qualified declarations will use the precision
7416 * qualifier specified in the most recent precision statement
7417 * that is still in scope. The precision statement has the same
7418 * scoping rules as variable declarations. If it is declared
7419 * inside a compound statement, its effect stops at the end of
7420 * the innermost statement it was declared in. Precision
7421 * statements in nested scopes override precision statements in
7422 * outer scopes. Multiple precision statements for the same basic
7423 * type can appear inside the same scope, with later statements
7424 * overriding earlier statements within that scope."
7425 *
7426 * Default precision specifications follow the same scope rules as
7427 * variables. So, we can track the state of the default precision
7428 * qualifiers in the symbol table, and the rules will just work. This
7429 * is a slight abuse of the symbol table, but it has the semantics
7430 * that we want.
7431 */
7432 state->symbols->add_default_precision_qualifier(this->type_name,
7433 this->default_precision);
7434 }
7435
7436 /* FINISHME: Translate precision statements into IR. */
7437 return NULL;
7438 }
7439
7440 /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
7441 * process_record_constructor() can do type-checking on C-style initializer
7442 * expressions of structs, but ast_struct_specifier should only be translated
7443 * to HIR if it is declaring the type of a structure.
7444 *
7445 * The ->is_declaration field is false for initializers of variables
7446 * declared separately from the struct's type definition.
7447 *
7448 * struct S { ... }; (is_declaration = true)
7449 * struct T { ... } t = { ... }; (is_declaration = true)
7450 * S s = { ... }; (is_declaration = false)
7451 */
7452 if (this->structure != NULL && this->structure->is_declaration)
7453 return this->structure->hir(instructions, state);
7454
7455 return NULL;
7456 }
7457
7458
7459 /**
7460 * Process a structure or interface block tree into an array of structure fields
7461 *
7462 * After parsing, where there are some syntax differnces, structures and
7463 * interface blocks are almost identical. They are similar enough that the
7464 * AST for each can be processed the same way into a set of
7465 * \c glsl_struct_field to describe the members.
7466 *
7467 * If we're processing an interface block, var_mode should be the type of the
7468 * interface block (ir_var_shader_in, ir_var_shader_out, ir_var_uniform or
7469 * ir_var_shader_storage). If we're processing a structure, var_mode should be
7470 * ir_var_auto.
7471 *
7472 * \return
7473 * The number of fields processed. A pointer to the array structure fields is
7474 * stored in \c *fields_ret.
7475 */
7476 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)7477 ast_process_struct_or_iface_block_members(exec_list *instructions,
7478 struct _mesa_glsl_parse_state *state,
7479 exec_list *declarations,
7480 glsl_struct_field **fields_ret,
7481 bool is_interface,
7482 enum glsl_matrix_layout matrix_layout,
7483 bool allow_reserved_names,
7484 ir_variable_mode var_mode,
7485 ast_type_qualifier *layout,
7486 unsigned block_stream,
7487 unsigned block_xfb_buffer,
7488 unsigned block_xfb_offset,
7489 unsigned expl_location,
7490 unsigned expl_align)
7491 {
7492 unsigned decl_count = 0;
7493 unsigned next_offset = 0;
7494
7495 /* Make an initial pass over the list of fields to determine how
7496 * many there are. Each element in this list is an ast_declarator_list.
7497 * This means that we actually need to count the number of elements in the
7498 * 'declarations' list in each of the elements.
7499 */
7500 foreach_list_typed (ast_declarator_list, decl_list, link, declarations) {
7501 decl_count += decl_list->declarations.length();
7502 }
7503
7504 /* Allocate storage for the fields and process the field
7505 * declarations. As the declarations are processed, try to also convert
7506 * the types to HIR. This ensures that structure definitions embedded in
7507 * other structure definitions or in interface blocks are processed.
7508 */
7509 glsl_struct_field *const fields = rzalloc_array(state, glsl_struct_field,
7510 decl_count);
7511
7512 bool first_member = true;
7513 bool first_member_has_explicit_location = false;
7514
7515 unsigned i = 0;
7516 foreach_list_typed (ast_declarator_list, decl_list, link, declarations) {
7517 const char *type_name;
7518 YYLTYPE loc = decl_list->get_location();
7519
7520 decl_list->type->specifier->hir(instructions, state);
7521
7522 /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
7523 *
7524 * "Anonymous structures are not supported; so embedded structures
7525 * must have a declarator. A name given to an embedded struct is
7526 * scoped at the same level as the struct it is embedded in."
7527 *
7528 * The same section of the GLSL 1.20 spec says:
7529 *
7530 * "Anonymous structures are not supported. Embedded structures are
7531 * not supported."
7532 *
7533 * The GLSL ES 1.00 and 3.00 specs have similar langauge. So, we allow
7534 * embedded structures in 1.10 only.
7535 */
7536 if (state->language_version != 110 &&
7537 decl_list->type->specifier->structure != NULL)
7538 _mesa_glsl_error(&loc, state,
7539 "embedded structure declarations are not allowed");
7540
7541 const glsl_type *decl_type =
7542 decl_list->type->glsl_type(& type_name, state);
7543
7544 const struct ast_type_qualifier *const qual =
7545 &decl_list->type->qualifier;
7546
7547 /* From section 4.3.9 of the GLSL 4.40 spec:
7548 *
7549 * "[In interface blocks] opaque types are not allowed."
7550 *
7551 * It should be impossible for decl_type to be NULL here. Cases that
7552 * might naturally lead to decl_type being NULL, especially for the
7553 * is_interface case, will have resulted in compilation having
7554 * already halted due to a syntax error.
7555 */
7556 assert(decl_type);
7557
7558 if (is_interface) {
7559 /* From section 4.3.7 of the ARB_bindless_texture spec:
7560 *
7561 * "(remove the following bullet from the last list on p. 39,
7562 * thereby permitting sampler types in interface blocks; image
7563 * types are also permitted in blocks by this extension)"
7564 *
7565 * * sampler types are not allowed
7566 */
7567 if (glsl_contains_atomic(decl_type) ||
7568 (!state->has_bindless() && glsl_contains_opaque(decl_type))) {
7569 _mesa_glsl_error(&loc, state, "uniform/buffer in non-default "
7570 "interface block contains %s variable",
7571 state->has_bindless() ? "atomic" : "opaque");
7572 }
7573 } else {
7574 if (glsl_contains_atomic(decl_type)) {
7575 /* From section 4.1.7.3 of the GLSL 4.40 spec:
7576 *
7577 * "Members of structures cannot be declared as atomic counter
7578 * types."
7579 */
7580 _mesa_glsl_error(&loc, state, "atomic counter in structure");
7581 }
7582
7583 if (!state->has_bindless() && glsl_type_contains_image(decl_type)) {
7584 /* FINISHME: Same problem as with atomic counters.
7585 * FINISHME: Request clarification from Khronos and add
7586 * FINISHME: spec quotation here.
7587 */
7588 _mesa_glsl_error(&loc, state, "image in structure");
7589 }
7590 }
7591
7592 if (qual->flags.q.explicit_binding) {
7593 _mesa_glsl_error(&loc, state,
7594 "binding layout qualifier cannot be applied "
7595 "to struct or interface block members");
7596 }
7597
7598 if (is_interface) {
7599 if (!first_member) {
7600 if (!layout->flags.q.explicit_location &&
7601 ((first_member_has_explicit_location &&
7602 !qual->flags.q.explicit_location) ||
7603 (!first_member_has_explicit_location &&
7604 qual->flags.q.explicit_location))) {
7605 _mesa_glsl_error(&loc, state,
7606 "when block-level location layout qualifier "
7607 "is not supplied either all members must "
7608 "have a location layout qualifier or all "
7609 "members must not have a location layout "
7610 "qualifier");
7611 }
7612 } else {
7613 first_member = false;
7614 first_member_has_explicit_location =
7615 qual->flags.q.explicit_location;
7616 }
7617 }
7618
7619 if (qual->flags.q.std140 ||
7620 qual->flags.q.std430 ||
7621 qual->flags.q.packed ||
7622 qual->flags.q.shared) {
7623 _mesa_glsl_error(&loc, state,
7624 "uniform/shader storage block layout qualifiers "
7625 "std140, std430, packed, and shared can only be "
7626 "applied to uniform/shader storage blocks, not "
7627 "members");
7628 }
7629
7630 if (qual->flags.q.constant) {
7631 _mesa_glsl_error(&loc, state,
7632 "const storage qualifier cannot be applied "
7633 "to struct or interface block members");
7634 }
7635
7636 validate_memory_qualifier_for_type(state, &loc, qual, decl_type);
7637 validate_image_format_qualifier_for_type(state, &loc, qual, decl_type);
7638
7639 /* From Section 4.4.2.3 (Geometry Outputs) of the GLSL 4.50 spec:
7640 *
7641 * "A block member may be declared with a stream identifier, but
7642 * the specified stream must match the stream associated with the
7643 * containing block."
7644 */
7645 if (qual->flags.q.explicit_stream) {
7646 unsigned qual_stream;
7647 if (process_qualifier_constant(state, &loc, "stream",
7648 qual->stream, &qual_stream) &&
7649 qual_stream != block_stream) {
7650 _mesa_glsl_error(&loc, state, "stream layout qualifier on "
7651 "interface block member does not match "
7652 "the interface block (%u vs %u)", qual_stream,
7653 block_stream);
7654 }
7655 }
7656
7657 int xfb_buffer;
7658 unsigned explicit_xfb_buffer = 0;
7659 if (qual->flags.q.explicit_xfb_buffer) {
7660 unsigned qual_xfb_buffer;
7661 if (process_qualifier_constant(state, &loc, "xfb_buffer",
7662 qual->xfb_buffer, &qual_xfb_buffer)) {
7663 explicit_xfb_buffer = 1;
7664 if (qual_xfb_buffer != block_xfb_buffer)
7665 _mesa_glsl_error(&loc, state, "xfb_buffer layout qualifier on "
7666 "interface block member does not match "
7667 "the interface block (%u vs %u)",
7668 qual_xfb_buffer, block_xfb_buffer);
7669 }
7670 xfb_buffer = (int) qual_xfb_buffer;
7671 } else {
7672 if (layout)
7673 explicit_xfb_buffer = layout->flags.q.explicit_xfb_buffer;
7674 xfb_buffer = (int) block_xfb_buffer;
7675 }
7676
7677 int xfb_stride = -1;
7678 if (qual->flags.q.explicit_xfb_stride) {
7679 unsigned qual_xfb_stride;
7680 if (process_qualifier_constant(state, &loc, "xfb_stride",
7681 qual->xfb_stride, &qual_xfb_stride)) {
7682 xfb_stride = (int) qual_xfb_stride;
7683 }
7684 }
7685
7686 if (qual->flags.q.uniform && qual->has_interpolation()) {
7687 _mesa_glsl_error(&loc, state,
7688 "interpolation qualifiers cannot be used "
7689 "with uniform interface blocks");
7690 }
7691
7692 if ((qual->flags.q.uniform || !is_interface) &&
7693 qual->has_auxiliary_storage()) {
7694 _mesa_glsl_error(&loc, state,
7695 "auxiliary storage qualifiers cannot be used "
7696 "in uniform blocks or structures.");
7697 }
7698
7699 if (qual->flags.q.row_major || qual->flags.q.column_major) {
7700 if (!qual->flags.q.uniform && !qual->flags.q.buffer) {
7701 _mesa_glsl_error(&loc, state,
7702 "row_major and column_major can only be "
7703 "applied to interface blocks");
7704 } else
7705 validate_matrix_layout_for_type(state, &loc, decl_type, NULL);
7706 }
7707
7708 foreach_list_typed (ast_declaration, decl, link,
7709 &decl_list->declarations) {
7710 YYLTYPE loc = decl->get_location();
7711
7712 if (!allow_reserved_names)
7713 validate_identifier(decl->identifier, loc, state);
7714
7715 const struct glsl_type *field_type =
7716 process_array_type(&loc, decl_type, decl->array_specifier, state);
7717 validate_array_dimensions(field_type, state, &loc);
7718 fields[i].type = field_type;
7719 fields[i].name = decl->identifier;
7720 fields[i].interpolation =
7721 interpret_interpolation_qualifier(qual, field_type,
7722 var_mode, state, &loc);
7723 fields[i].centroid = qual->flags.q.centroid ? 1 : 0;
7724 fields[i].sample = qual->flags.q.sample ? 1 : 0;
7725 fields[i].patch = qual->flags.q.patch ? 1 : 0;
7726 fields[i].offset = -1;
7727 fields[i].explicit_xfb_buffer = explicit_xfb_buffer;
7728 fields[i].xfb_buffer = xfb_buffer;
7729 fields[i].xfb_stride = xfb_stride;
7730
7731 if (qual->flags.q.explicit_location) {
7732 unsigned qual_location;
7733 if (process_qualifier_constant(state, &loc, "location",
7734 qual->location, &qual_location)) {
7735 fields[i].location = qual_location +
7736 (fields[i].patch ? VARYING_SLOT_PATCH0 : VARYING_SLOT_VAR0);
7737 expl_location = fields[i].location +
7738 glsl_count_attribute_slots(fields[i].type, false);
7739 }
7740 } else {
7741 if (layout && layout->flags.q.explicit_location) {
7742 fields[i].location = expl_location;
7743 expl_location += glsl_count_attribute_slots(fields[i].type, false);
7744 } else {
7745 fields[i].location = -1;
7746 }
7747 }
7748
7749 if (qual->flags.q.explicit_component) {
7750 unsigned qual_component;
7751 if (process_qualifier_constant(state, &loc, "component",
7752 qual->component, &qual_component)) {
7753 validate_component_layout_for_type(state, &loc, fields[i].type,
7754 qual_component);
7755 fields[i].component = qual_component;
7756 }
7757 } else {
7758 fields[i].component = -1;
7759 }
7760
7761 /* Offset can only be used with std430 and std140 layouts an initial
7762 * value of 0 is used for error detection.
7763 */
7764 unsigned base_alignment = 0;
7765 unsigned size = 0;
7766 if (layout) {
7767 bool row_major;
7768 if (qual->flags.q.row_major ||
7769 matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
7770 row_major = true;
7771 } else {
7772 row_major = false;
7773 }
7774
7775 if(layout->flags.q.std140) {
7776 base_alignment = glsl_get_std140_base_alignment(field_type, row_major);
7777 size = glsl_get_std140_size(field_type, row_major);
7778 } else if (layout->flags.q.std430) {
7779 base_alignment = glsl_get_std430_base_alignment(field_type, row_major);
7780 size = glsl_get_std430_size(field_type, row_major);
7781 }
7782 }
7783
7784 if (qual->flags.q.explicit_offset) {
7785 unsigned qual_offset;
7786 if (process_qualifier_constant(state, &loc, "offset",
7787 qual->offset, &qual_offset)) {
7788 if (base_alignment != 0 && size != 0) {
7789 if (next_offset > qual_offset)
7790 _mesa_glsl_error(&loc, state, "layout qualifier "
7791 "offset overlaps previous member");
7792
7793 if (qual_offset % base_alignment) {
7794 _mesa_glsl_error(&loc, state, "layout qualifier offset "
7795 "must be a multiple of the base "
7796 "alignment of %s", glsl_get_type_name(field_type));
7797 }
7798 fields[i].offset = qual_offset;
7799 next_offset = qual_offset + size;
7800 } else {
7801 _mesa_glsl_error(&loc, state, "offset can only be used "
7802 "with std430 and std140 layouts");
7803 }
7804 }
7805 }
7806
7807 if (qual->flags.q.explicit_align || expl_align != 0) {
7808 unsigned offset = fields[i].offset != -1 ? fields[i].offset :
7809 next_offset;
7810 if (base_alignment == 0 || size == 0) {
7811 _mesa_glsl_error(&loc, state, "align can only be used with "
7812 "std430 and std140 layouts");
7813 } else if (qual->flags.q.explicit_align) {
7814 unsigned member_align;
7815 if (process_qualifier_constant(state, &loc, "align",
7816 qual->align, &member_align)) {
7817 if (member_align == 0 ||
7818 member_align & (member_align - 1)) {
7819 _mesa_glsl_error(&loc, state, "align layout qualifier "
7820 "is not a power of 2");
7821 } else {
7822 fields[i].offset = align(offset, member_align);
7823 next_offset = fields[i].offset + size;
7824 }
7825 }
7826 } else {
7827 fields[i].offset = align(offset, expl_align);
7828 next_offset = fields[i].offset + size;
7829 }
7830 } else if (!qual->flags.q.explicit_offset) {
7831 if (base_alignment != 0 && size != 0)
7832 next_offset = align(next_offset, base_alignment) + size;
7833 }
7834
7835 /* From the ARB_enhanced_layouts spec:
7836 *
7837 * "The given offset applies to the first component of the first
7838 * member of the qualified entity. Then, within the qualified
7839 * entity, subsequent components are each assigned, in order, to
7840 * the next available offset aligned to a multiple of that
7841 * component's size. Aggregate types are flattened down to the
7842 * component level to get this sequence of components."
7843 */
7844 if (qual->flags.q.explicit_xfb_offset) {
7845 unsigned xfb_offset;
7846 if (process_qualifier_constant(state, &loc, "xfb_offset",
7847 qual->offset, &xfb_offset)) {
7848 fields[i].offset = xfb_offset;
7849 block_xfb_offset = fields[i].offset +
7850 4 * glsl_get_component_slots(field_type);
7851 }
7852 } else {
7853 if (layout && layout->flags.q.explicit_xfb_offset) {
7854 unsigned base_alignment = glsl_type_is_64bit(field_type) ? 8 : 4;
7855 fields[i].offset = align(block_xfb_offset, base_alignment);
7856 block_xfb_offset += 4 * glsl_get_component_slots(field_type);
7857 }
7858 }
7859
7860 /* Propogate row- / column-major information down the fields of the
7861 * structure or interface block. Structures need this data because
7862 * the structure may contain a structure that contains ... a matrix
7863 * that need the proper layout.
7864 */
7865 if (is_interface && layout &&
7866 (layout->flags.q.uniform || layout->flags.q.buffer) &&
7867 (glsl_type_is_matrix(glsl_without_array(field_type))
7868 || glsl_type_is_struct(glsl_without_array(field_type)))) {
7869 /* If no layout is specified for the field, inherit the layout
7870 * from the block.
7871 */
7872 fields[i].matrix_layout = matrix_layout;
7873
7874 if (qual->flags.q.row_major)
7875 fields[i].matrix_layout = GLSL_MATRIX_LAYOUT_ROW_MAJOR;
7876 else if (qual->flags.q.column_major)
7877 fields[i].matrix_layout = GLSL_MATRIX_LAYOUT_COLUMN_MAJOR;
7878
7879 /* If we're processing an uniform or buffer block, the matrix
7880 * layout must be decided by this point.
7881 */
7882 assert(fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR
7883 || fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR);
7884 }
7885
7886 /* Memory qualifiers are allowed on buffer and image variables, while
7887 * the format qualifier is only accepted for images.
7888 */
7889 if (var_mode == ir_var_shader_storage ||
7890 glsl_type_is_image(glsl_without_array(field_type))) {
7891 /* For readonly and writeonly qualifiers the field definition,
7892 * if set, overwrites the layout qualifier.
7893 */
7894 if (qual->flags.q.read_only || qual->flags.q.write_only) {
7895 fields[i].memory_read_only = qual->flags.q.read_only;
7896 fields[i].memory_write_only = qual->flags.q.write_only;
7897 } else {
7898 fields[i].memory_read_only =
7899 layout ? layout->flags.q.read_only : 0;
7900 fields[i].memory_write_only =
7901 layout ? layout->flags.q.write_only : 0;
7902 }
7903
7904 /* For other qualifiers, we set the flag if either the layout
7905 * qualifier or the field qualifier are set
7906 */
7907 fields[i].memory_coherent = qual->flags.q.coherent ||
7908 (layout && layout->flags.q.coherent);
7909 fields[i].memory_volatile = qual->flags.q._volatile ||
7910 (layout && layout->flags.q._volatile);
7911 fields[i].memory_restrict = qual->flags.q.restrict_flag ||
7912 (layout && layout->flags.q.restrict_flag);
7913
7914 if (glsl_type_is_image(glsl_without_array(field_type))) {
7915 if (qual->flags.q.explicit_image_format) {
7916 if (qual->image_base_type !=
7917 glsl_without_array(field_type)->sampled_type) {
7918 _mesa_glsl_error(&loc, state, "format qualifier doesn't "
7919 "match the base data type of the image");
7920 }
7921
7922 fields[i].image_format = qual->image_format;
7923 } else {
7924 if (state->has_image_load_formatted()) {
7925 if (state->EXT_shader_image_load_formatted_warn) {
7926 _mesa_glsl_warning(&loc, state, "GL_EXT_image_load_formatted used");
7927 }
7928 } else if (!qual->flags.q.write_only) {
7929 _mesa_glsl_error(&loc, state, "image not qualified with "
7930 "`writeonly' must have a format layout "
7931 "qualifier");
7932 }
7933
7934 fields[i].image_format = PIPE_FORMAT_NONE;
7935 }
7936 }
7937 }
7938
7939 /* Precision qualifiers do not hold any meaning in Desktop GLSL */
7940 if (state->es_shader) {
7941 fields[i].precision = select_gles_precision(qual->precision,
7942 field_type,
7943 state,
7944 &loc);
7945 } else {
7946 fields[i].precision = qual->precision;
7947 }
7948
7949 i++;
7950 }
7951 }
7952
7953 assert(i == decl_count);
7954
7955 *fields_ret = fields;
7956 return decl_count;
7957 }
7958
7959 static bool
is_anonymous(const glsl_type * t)7960 is_anonymous(const glsl_type *t)
7961 {
7962 /* See handling for struct_specifier in glsl_parser.yy. */
7963 return !strncmp(glsl_get_type_name(t), "#anon", 5);
7964 }
7965
7966 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)7967 ast_struct_specifier::hir(exec_list *instructions,
7968 struct _mesa_glsl_parse_state *state)
7969 {
7970 YYLTYPE loc = this->get_location();
7971
7972 unsigned expl_location = 0;
7973 if (layout && layout->flags.q.explicit_location) {
7974 if (!process_qualifier_constant(state, &loc, "location",
7975 layout->location, &expl_location)) {
7976 return NULL;
7977 } else {
7978 expl_location = VARYING_SLOT_VAR0 + expl_location;
7979 }
7980 }
7981
7982 glsl_struct_field *fields;
7983 unsigned decl_count =
7984 ast_process_struct_or_iface_block_members(instructions,
7985 state,
7986 &this->declarations,
7987 &fields,
7988 false,
7989 GLSL_MATRIX_LAYOUT_INHERITED,
7990 false /* allow_reserved_names */,
7991 ir_var_auto,
7992 layout,
7993 0, /* for interface only */
7994 0, /* for interface only */
7995 0, /* for interface only */
7996 expl_location,
7997 0 /* for interface only */);
7998
7999 validate_identifier(this->name, loc, state);
8000
8001 type = glsl_struct_type(fields, decl_count, this->name, false /* packed */);
8002
8003 if (!is_anonymous(type) && !state->symbols->add_type(name, type)) {
8004 const glsl_type *match = state->symbols->get_type(name);
8005 /* allow struct matching for desktop GL - older UE4 does this */
8006 if (match != NULL && state->is_version(130, 0) && glsl_record_compare(match, type, true, false, true))
8007 _mesa_glsl_warning(& loc, state, "struct `%s' previously defined", name);
8008 else
8009 _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
8010 } else {
8011 const glsl_type **s = reralloc(state, state->user_structures,
8012 const glsl_type *,
8013 state->num_user_structures + 1);
8014 if (s != NULL) {
8015 s[state->num_user_structures] = type;
8016 state->user_structures = s;
8017 state->num_user_structures++;
8018 }
8019 }
8020
8021 /* Structure type definitions do not have r-values.
8022 */
8023 return NULL;
8024 }
8025
8026
8027 /**
8028 * Visitor class which detects whether a given interface block has been used.
8029 */
8030 class interface_block_usage_visitor : public ir_hierarchical_visitor
8031 {
8032 public:
interface_block_usage_visitor(ir_variable_mode mode,const glsl_type * block)8033 interface_block_usage_visitor(ir_variable_mode mode, const glsl_type *block)
8034 : mode(mode), block(block), found(false)
8035 {
8036 }
8037
visit(ir_dereference_variable * ir)8038 virtual ir_visitor_status visit(ir_dereference_variable *ir)
8039 {
8040 if (ir->var->data.mode == mode && ir->var->get_interface_type() == block) {
8041 found = true;
8042 return visit_stop;
8043 }
8044 return visit_continue;
8045 }
8046
usage_found() const8047 bool usage_found() const
8048 {
8049 return this->found;
8050 }
8051
8052 private:
8053 ir_variable_mode mode;
8054 const glsl_type *block;
8055 bool found;
8056 };
8057
8058 static bool
is_unsized_array_last_element(ir_variable * v)8059 is_unsized_array_last_element(ir_variable *v)
8060 {
8061 const glsl_type *interface_type = v->get_interface_type();
8062 int length = interface_type->length;
8063
8064 assert(glsl_type_is_unsized_array(v->type));
8065
8066 /* Check if it is the last element of the interface */
8067 if (strcmp(interface_type->fields.structure[length-1].name, v->name) == 0)
8068 return true;
8069 return false;
8070 }
8071
8072 static void
apply_memory_qualifiers(ir_variable * var,glsl_struct_field field)8073 apply_memory_qualifiers(ir_variable *var, glsl_struct_field field)
8074 {
8075 var->data.memory_read_only = field.memory_read_only;
8076 var->data.memory_write_only = field.memory_write_only;
8077 var->data.memory_coherent = field.memory_coherent;
8078 var->data.memory_volatile = field.memory_volatile;
8079 var->data.memory_restrict = field.memory_restrict;
8080 }
8081
8082 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)8083 ast_interface_block::hir(exec_list *instructions,
8084 struct _mesa_glsl_parse_state *state)
8085 {
8086 YYLTYPE loc = this->get_location();
8087
8088 /* Interface blocks must be declared at global scope */
8089 if (state->current_function != NULL) {
8090 _mesa_glsl_error(&loc, state,
8091 "Interface block `%s' must be declared "
8092 "at global scope",
8093 this->block_name);
8094 }
8095
8096 /* Validate qualifiers:
8097 *
8098 * - Layout Qualifiers as per the table in Section 4.4
8099 * ("Layout Qualifiers") of the GLSL 4.50 spec.
8100 *
8101 * - Memory Qualifiers as per Section 4.10 ("Memory Qualifiers") of the
8102 * GLSL 4.50 spec:
8103 *
8104 * "Additionally, memory qualifiers may also be used in the declaration
8105 * of shader storage blocks"
8106 *
8107 * Note the table in Section 4.4 says std430 is allowed on both uniform and
8108 * buffer blocks however Section 4.4.5 (Uniform and Shader Storage Block
8109 * Layout Qualifiers) of the GLSL 4.50 spec says:
8110 *
8111 * "The std430 qualifier is supported only for shader storage blocks;
8112 * using std430 on a uniform block will result in a compile-time error."
8113 */
8114 ast_type_qualifier allowed_blk_qualifiers;
8115 allowed_blk_qualifiers.flags.i = 0;
8116 if (this->layout.flags.q.buffer || this->layout.flags.q.uniform) {
8117 allowed_blk_qualifiers.flags.q.shared = 1;
8118 allowed_blk_qualifiers.flags.q.packed = 1;
8119 allowed_blk_qualifiers.flags.q.std140 = 1;
8120 allowed_blk_qualifiers.flags.q.row_major = 1;
8121 allowed_blk_qualifiers.flags.q.column_major = 1;
8122 allowed_blk_qualifiers.flags.q.explicit_align = 1;
8123 allowed_blk_qualifiers.flags.q.explicit_binding = 1;
8124 if (this->layout.flags.q.buffer) {
8125 allowed_blk_qualifiers.flags.q.buffer = 1;
8126 allowed_blk_qualifiers.flags.q.std430 = 1;
8127 allowed_blk_qualifiers.flags.q.coherent = 1;
8128 allowed_blk_qualifiers.flags.q._volatile = 1;
8129 allowed_blk_qualifiers.flags.q.restrict_flag = 1;
8130 allowed_blk_qualifiers.flags.q.read_only = 1;
8131 allowed_blk_qualifiers.flags.q.write_only = 1;
8132 } else {
8133 allowed_blk_qualifiers.flags.q.uniform = 1;
8134 }
8135 } else {
8136 /* Interface block */
8137 assert(this->layout.flags.q.in || this->layout.flags.q.out);
8138
8139 allowed_blk_qualifiers.flags.q.explicit_location = 1;
8140 if (this->layout.flags.q.out) {
8141 allowed_blk_qualifiers.flags.q.out = 1;
8142 if (state->stage == MESA_SHADER_GEOMETRY ||
8143 state->stage == MESA_SHADER_TESS_CTRL ||
8144 state->stage == MESA_SHADER_TESS_EVAL ||
8145 state->stage == MESA_SHADER_VERTEX ) {
8146 allowed_blk_qualifiers.flags.q.explicit_xfb_offset = 1;
8147 allowed_blk_qualifiers.flags.q.explicit_xfb_buffer = 1;
8148 allowed_blk_qualifiers.flags.q.xfb_buffer = 1;
8149 allowed_blk_qualifiers.flags.q.explicit_xfb_stride = 1;
8150 allowed_blk_qualifiers.flags.q.xfb_stride = 1;
8151 }
8152 if (state->stage == MESA_SHADER_GEOMETRY) {
8153 allowed_blk_qualifiers.flags.q.stream = 1;
8154 allowed_blk_qualifiers.flags.q.explicit_stream = 1;
8155 }
8156 if (state->stage == MESA_SHADER_TESS_CTRL) {
8157 allowed_blk_qualifiers.flags.q.patch = 1;
8158 }
8159 } else {
8160 allowed_blk_qualifiers.flags.q.in = 1;
8161 if (state->stage == MESA_SHADER_TESS_EVAL) {
8162 allowed_blk_qualifiers.flags.q.patch = 1;
8163 }
8164 }
8165 }
8166
8167 this->layout.validate_flags(&loc, state, allowed_blk_qualifiers,
8168 "invalid qualifier for block",
8169 this->block_name);
8170
8171 enum glsl_interface_packing packing;
8172 if (this->layout.flags.q.std140) {
8173 packing = GLSL_INTERFACE_PACKING_STD140;
8174 } else if (this->layout.flags.q.packed) {
8175 packing = GLSL_INTERFACE_PACKING_PACKED;
8176 } else if (this->layout.flags.q.std430) {
8177 packing = GLSL_INTERFACE_PACKING_STD430;
8178 } else {
8179 /* The default layout is shared.
8180 */
8181 packing = GLSL_INTERFACE_PACKING_SHARED;
8182 }
8183
8184 ir_variable_mode var_mode;
8185 const char *iface_type_name;
8186 if (this->layout.flags.q.in) {
8187 var_mode = ir_var_shader_in;
8188 iface_type_name = "in";
8189 } else if (this->layout.flags.q.out) {
8190 var_mode = ir_var_shader_out;
8191 iface_type_name = "out";
8192 } else if (this->layout.flags.q.uniform) {
8193 var_mode = ir_var_uniform;
8194 iface_type_name = "uniform";
8195 } else if (this->layout.flags.q.buffer) {
8196 var_mode = ir_var_shader_storage;
8197 iface_type_name = "buffer";
8198 } else {
8199 var_mode = ir_var_auto;
8200 iface_type_name = "UNKNOWN";
8201 assert(!"interface block layout qualifier not found!");
8202 }
8203
8204 enum glsl_matrix_layout matrix_layout = GLSL_MATRIX_LAYOUT_INHERITED;
8205 if (this->layout.flags.q.row_major)
8206 matrix_layout = GLSL_MATRIX_LAYOUT_ROW_MAJOR;
8207 else if (this->layout.flags.q.column_major)
8208 matrix_layout = GLSL_MATRIX_LAYOUT_COLUMN_MAJOR;
8209
8210 bool redeclaring_per_vertex = strcmp(this->block_name, "gl_PerVertex") == 0;
8211 exec_list declared_variables;
8212 glsl_struct_field *fields;
8213
8214 /* For blocks that accept memory qualifiers (i.e. shader storage), verify
8215 * that we don't have incompatible qualifiers
8216 */
8217 if (this->layout.flags.q.read_only && this->layout.flags.q.write_only) {
8218 _mesa_glsl_error(&loc, state,
8219 "Interface block sets both readonly and writeonly");
8220 }
8221
8222 unsigned qual_stream;
8223 if (!process_qualifier_constant(state, &loc, "stream", this->layout.stream,
8224 &qual_stream) ||
8225 !validate_stream_qualifier(&loc, state, qual_stream)) {
8226 /* If the stream qualifier is invalid it doesn't make sense to continue
8227 * on and try to compare stream layouts on member variables against it
8228 * so just return early.
8229 */
8230 return NULL;
8231 }
8232
8233 unsigned qual_xfb_buffer = 0;
8234 if (layout.flags.q.xfb_buffer) {
8235 if (!process_qualifier_constant(state, &loc, "xfb_buffer",
8236 layout.xfb_buffer, &qual_xfb_buffer) ||
8237 !validate_xfb_buffer_qualifier(&loc, state, qual_xfb_buffer)) {
8238 return NULL;
8239 }
8240 }
8241
8242 unsigned qual_xfb_offset = 0;
8243 if (layout.flags.q.explicit_xfb_offset) {
8244 if (!process_qualifier_constant(state, &loc, "xfb_offset",
8245 layout.offset, &qual_xfb_offset)) {
8246 return NULL;
8247 }
8248 }
8249
8250 unsigned qual_xfb_stride = 0;
8251 if (layout.flags.q.explicit_xfb_stride) {
8252 if (!process_qualifier_constant(state, &loc, "xfb_stride",
8253 layout.xfb_stride, &qual_xfb_stride)) {
8254 return NULL;
8255 }
8256 }
8257
8258 unsigned expl_location = 0;
8259 if (layout.flags.q.explicit_location) {
8260 if (!process_qualifier_constant(state, &loc, "location",
8261 layout.location, &expl_location)) {
8262 return NULL;
8263 } else {
8264 expl_location += this->layout.flags.q.patch ? VARYING_SLOT_PATCH0
8265 : VARYING_SLOT_VAR0;
8266 }
8267 }
8268
8269 unsigned expl_align = 0;
8270 if (layout.flags.q.explicit_align) {
8271 if (!process_qualifier_constant(state, &loc, "align",
8272 layout.align, &expl_align)) {
8273 return NULL;
8274 } else {
8275 if (expl_align == 0 || expl_align & (expl_align - 1)) {
8276 _mesa_glsl_error(&loc, state, "align layout qualifier is not a "
8277 "power of 2.");
8278 return NULL;
8279 }
8280 }
8281 }
8282
8283 unsigned int num_variables =
8284 ast_process_struct_or_iface_block_members(&declared_variables,
8285 state,
8286 &this->declarations,
8287 &fields,
8288 true,
8289 matrix_layout,
8290 redeclaring_per_vertex,
8291 var_mode,
8292 &this->layout,
8293 qual_stream,
8294 qual_xfb_buffer,
8295 qual_xfb_offset,
8296 expl_location,
8297 expl_align);
8298
8299 if (!redeclaring_per_vertex) {
8300 validate_identifier(this->block_name, loc, state);
8301
8302 /* From section 4.3.9 ("Interface Blocks") of the GLSL 4.50 spec:
8303 *
8304 * "Block names have no other use within a shader beyond interface
8305 * matching; it is a compile-time error to use a block name at global
8306 * scope for anything other than as a block name."
8307 */
8308 ir_variable *var = state->symbols->get_variable(this->block_name);
8309 if (var && !glsl_type_is_interface(var->type)) {
8310 _mesa_glsl_error(&loc, state, "Block name `%s' is "
8311 "already used in the scope.",
8312 this->block_name);
8313 }
8314 }
8315
8316 const glsl_type *earlier_per_vertex = NULL;
8317 if (redeclaring_per_vertex) {
8318 /* Find the previous declaration of gl_PerVertex. If we're redeclaring
8319 * the named interface block gl_in, we can find it by looking at the
8320 * previous declaration of gl_in. Otherwise we can find it by looking
8321 * at the previous decalartion of any of the built-in outputs,
8322 * e.g. gl_Position.
8323 *
8324 * Also check that the instance name and array-ness of the redeclaration
8325 * are correct.
8326 */
8327 switch (var_mode) {
8328 case ir_var_shader_in:
8329 if (ir_variable *earlier_gl_in =
8330 state->symbols->get_variable("gl_in")) {
8331 earlier_per_vertex = earlier_gl_in->get_interface_type();
8332 } else {
8333 _mesa_glsl_error(&loc, state,
8334 "redeclaration of gl_PerVertex input not allowed "
8335 "in the %s shader",
8336 _mesa_shader_stage_to_string(state->stage));
8337 }
8338 if (this->instance_name == NULL ||
8339 strcmp(this->instance_name, "gl_in") != 0 || this->array_specifier == NULL ||
8340 !this->array_specifier->is_single_dimension()) {
8341 _mesa_glsl_error(&loc, state,
8342 "gl_PerVertex input must be redeclared as "
8343 "gl_in[]");
8344 }
8345 break;
8346 case ir_var_shader_out:
8347 if (ir_variable *earlier_gl_Position =
8348 state->symbols->get_variable("gl_Position")) {
8349 earlier_per_vertex = earlier_gl_Position->get_interface_type();
8350 } else if (ir_variable *earlier_gl_out =
8351 state->symbols->get_variable("gl_out")) {
8352 earlier_per_vertex = earlier_gl_out->get_interface_type();
8353 } else {
8354 _mesa_glsl_error(&loc, state,
8355 "redeclaration of gl_PerVertex output not "
8356 "allowed in the %s shader",
8357 _mesa_shader_stage_to_string(state->stage));
8358 }
8359 if (state->stage == MESA_SHADER_TESS_CTRL) {
8360 if (this->instance_name == NULL ||
8361 strcmp(this->instance_name, "gl_out") != 0 || this->array_specifier == NULL) {
8362 _mesa_glsl_error(&loc, state,
8363 "gl_PerVertex output must be redeclared as "
8364 "gl_out[]");
8365 }
8366 } else {
8367 if (this->instance_name != NULL) {
8368 _mesa_glsl_error(&loc, state,
8369 "gl_PerVertex output may not be redeclared with "
8370 "an instance name");
8371 }
8372 }
8373 break;
8374 default:
8375 _mesa_glsl_error(&loc, state,
8376 "gl_PerVertex must be declared as an input or an "
8377 "output");
8378 break;
8379 }
8380
8381 if (earlier_per_vertex == NULL) {
8382 /* An error has already been reported. Bail out to avoid null
8383 * dereferences later in this function.
8384 */
8385 return NULL;
8386 }
8387
8388 /* Copy locations from the old gl_PerVertex interface block. */
8389 for (unsigned i = 0; i < num_variables; i++) {
8390 int j = glsl_get_field_index(earlier_per_vertex, fields[i].name);
8391 if (j == -1) {
8392 _mesa_glsl_error(&loc, state,
8393 "redeclaration of gl_PerVertex must be a subset "
8394 "of the built-in members of gl_PerVertex");
8395 } else {
8396 fields[i].location =
8397 earlier_per_vertex->fields.structure[j].location;
8398 fields[i].offset =
8399 earlier_per_vertex->fields.structure[j].offset;
8400 fields[i].interpolation =
8401 earlier_per_vertex->fields.structure[j].interpolation;
8402 fields[i].centroid =
8403 earlier_per_vertex->fields.structure[j].centroid;
8404 fields[i].sample =
8405 earlier_per_vertex->fields.structure[j].sample;
8406 fields[i].patch =
8407 earlier_per_vertex->fields.structure[j].patch;
8408 fields[i].precision =
8409 earlier_per_vertex->fields.structure[j].precision;
8410 fields[i].explicit_xfb_buffer =
8411 earlier_per_vertex->fields.structure[j].explicit_xfb_buffer;
8412 fields[i].xfb_buffer =
8413 earlier_per_vertex->fields.structure[j].xfb_buffer;
8414 fields[i].xfb_stride =
8415 earlier_per_vertex->fields.structure[j].xfb_stride;
8416 }
8417 }
8418
8419 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10
8420 * spec:
8421 *
8422 * If a built-in interface block is redeclared, it must appear in
8423 * the shader before any use of any member included in the built-in
8424 * declaration, or a compilation error will result.
8425 *
8426 * This appears to be a clarification to the behaviour established for
8427 * gl_PerVertex by GLSL 1.50, therefore we implement this behaviour
8428 * regardless of GLSL version.
8429 */
8430 interface_block_usage_visitor v(var_mode, earlier_per_vertex);
8431 v.run(instructions);
8432 if (v.usage_found()) {
8433 _mesa_glsl_error(&loc, state,
8434 "redeclaration of a built-in interface block must "
8435 "appear before any use of any member of the "
8436 "interface block");
8437 }
8438 }
8439
8440 const glsl_type *block_type =
8441 glsl_interface_type(fields,
8442 num_variables,
8443 packing,
8444 matrix_layout ==
8445 GLSL_MATRIX_LAYOUT_ROW_MAJOR,
8446 this->block_name);
8447
8448 unsigned component_size = glsl_contains_double(block_type) ? 8 : 4;
8449 int xfb_offset =
8450 layout.flags.q.explicit_xfb_offset ? (int) qual_xfb_offset : -1;
8451 validate_xfb_offset_qualifier(&loc, state, xfb_offset, block_type,
8452 component_size);
8453
8454 if (!state->symbols->add_interface(glsl_get_type_name(block_type), block_type, var_mode)) {
8455 YYLTYPE loc = this->get_location();
8456 _mesa_glsl_error(&loc, state, "interface block `%s' with type `%s' "
8457 "already taken in the current scope",
8458 this->block_name, iface_type_name);
8459 }
8460
8461 /* Since interface blocks cannot contain statements, it should be
8462 * impossible for the block to generate any instructions.
8463 */
8464 assert(declared_variables.is_empty());
8465
8466 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
8467 *
8468 * Geometry shader input variables get the per-vertex values written
8469 * out by vertex shader output variables of the same names. Since a
8470 * geometry shader operates on a set of vertices, each input varying
8471 * variable (or input block, see interface blocks below) needs to be
8472 * declared as an array.
8473 */
8474 if (state->stage == MESA_SHADER_GEOMETRY && this->array_specifier == NULL &&
8475 var_mode == ir_var_shader_in) {
8476 _mesa_glsl_error(&loc, state, "geometry shader inputs must be arrays");
8477 } else if ((state->stage == MESA_SHADER_TESS_CTRL ||
8478 state->stage == MESA_SHADER_TESS_EVAL) &&
8479 !this->layout.flags.q.patch &&
8480 this->array_specifier == NULL &&
8481 var_mode == ir_var_shader_in) {
8482 _mesa_glsl_error(&loc, state, "per-vertex tessellation shader inputs must be arrays");
8483 } else if (state->stage == MESA_SHADER_TESS_CTRL &&
8484 !this->layout.flags.q.patch &&
8485 this->array_specifier == NULL &&
8486 var_mode == ir_var_shader_out) {
8487 _mesa_glsl_error(&loc, state, "tessellation control shader outputs must be arrays");
8488 }
8489
8490
8491 /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
8492 * says:
8493 *
8494 * "If an instance name (instance-name) is used, then it puts all the
8495 * members inside a scope within its own name space, accessed with the
8496 * field selector ( . ) operator (analogously to structures)."
8497 */
8498 if (this->instance_name) {
8499 if (redeclaring_per_vertex) {
8500 /* When a built-in in an unnamed interface block is redeclared,
8501 * get_variable_being_redeclared() calls
8502 * check_builtin_array_max_size() to make sure that built-in array
8503 * variables aren't redeclared to illegal sizes. But we're looking
8504 * at a redeclaration of a named built-in interface block. So we
8505 * have to manually call check_builtin_array_max_size() for all parts
8506 * of the interface that are arrays.
8507 */
8508 for (unsigned i = 0; i < num_variables; i++) {
8509 if (glsl_type_is_array(fields[i].type)) {
8510 const unsigned size = glsl_array_size(fields[i].type);
8511 check_builtin_array_max_size(fields[i].name, size, loc, state);
8512 }
8513 }
8514 } else {
8515 validate_identifier(this->instance_name, loc, state);
8516 }
8517
8518 ir_variable *var;
8519
8520 if (this->array_specifier != NULL) {
8521 const glsl_type *block_array_type =
8522 process_array_type(&loc, block_type, this->array_specifier, state);
8523
8524 /* From Section 4.4.1 (Input Layout Qualifiers) of the GLSL 4.50 spec:
8525 *
8526 * "For some blocks declared as arrays, the location can only be applied
8527 * at the block level: When a block is declared as an array where
8528 * additional locations are needed for each member for each block array
8529 * element, it is a compile-time error to specify locations on the block
8530 * members. That is, when locations would be under specified by applying
8531 * them on block members, they are not allowed on block members. For
8532 * arrayed interfaces (those generally having an extra level of
8533 * arrayness due to interface expansion), the outer array is stripped
8534 * before applying this rule"
8535 *
8536 * From 4.4.1 (Input Layout Qualifiers) and
8537 * 4.4.2 (Output Layout Qualifiers) of GLSL ES 3.20
8538 *
8539 * "If an input is declared as an array of blocks, excluding
8540 * per-vertex-arrays as required for tessellation, it is an error
8541 * to declare a member of the block with a location qualifier."
8542 *
8543 * "If an output is declared as an array of blocks, excluding
8544 * per-vertex-arrays as required for tessellation, it is an error
8545 * to declare a member of the block with a location qualifier."
8546 */
8547 if (!redeclaring_per_vertex &&
8548 (state->has_enhanced_layouts() || state->has_shader_io_blocks())) {
8549 bool allow_location;
8550 switch (state->stage)
8551 {
8552 case MESA_SHADER_TESS_CTRL:
8553 allow_location = this->array_specifier->is_single_dimension();
8554 break;
8555 case MESA_SHADER_TESS_EVAL:
8556 case MESA_SHADER_GEOMETRY:
8557 allow_location = (this->array_specifier->is_single_dimension()
8558 && var_mode == ir_var_shader_in);
8559 break;
8560 default:
8561 allow_location = false;
8562 break;
8563 }
8564
8565 if (!allow_location) {
8566 for (unsigned i = 0; i < num_variables; i++) {
8567 if (fields[i].location != -1) {
8568 _mesa_glsl_error(&loc, state,
8569 "explicit member locations are not allowed in "
8570 "blocks declared as arrays %s shader",
8571 _mesa_shader_stage_to_string(state->stage));
8572 }
8573 }
8574 }
8575 }
8576
8577 /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
8578 *
8579 * For uniform blocks declared an array, each individual array
8580 * element corresponds to a separate buffer object backing one
8581 * instance of the block. As the array size indicates the number
8582 * of buffer objects needed, uniform block array declarations
8583 * must specify an array size.
8584 *
8585 * And a few paragraphs later:
8586 *
8587 * Geometry shader input blocks must be declared as arrays and
8588 * follow the array declaration and linking rules for all
8589 * geometry shader inputs. All other input and output block
8590 * arrays must specify an array size.
8591 *
8592 * The same applies to tessellation shaders.
8593 *
8594 * The upshot of this is that the only circumstance where an
8595 * interface array size *doesn't* need to be specified is on a
8596 * geometry shader input, tessellation control shader input,
8597 * tessellation control shader output, and tessellation evaluation
8598 * shader input.
8599 */
8600 if (glsl_type_is_unsized_array(block_array_type)) {
8601 bool allow_inputs = state->stage == MESA_SHADER_GEOMETRY ||
8602 state->stage == MESA_SHADER_TESS_CTRL ||
8603 state->stage == MESA_SHADER_TESS_EVAL;
8604 bool allow_outputs = state->stage == MESA_SHADER_TESS_CTRL;
8605
8606 if (this->layout.flags.q.in) {
8607 if (!allow_inputs)
8608 _mesa_glsl_error(&loc, state,
8609 "unsized input block arrays not allowed in "
8610 "%s shader",
8611 _mesa_shader_stage_to_string(state->stage));
8612 } else if (this->layout.flags.q.out) {
8613 if (!allow_outputs)
8614 _mesa_glsl_error(&loc, state,
8615 "unsized output block arrays not allowed in "
8616 "%s shader",
8617 _mesa_shader_stage_to_string(state->stage));
8618 } else {
8619 /* by elimination, this is a uniform block array */
8620 _mesa_glsl_error(&loc, state,
8621 "unsized uniform block arrays not allowed in "
8622 "%s shader",
8623 _mesa_shader_stage_to_string(state->stage));
8624 }
8625 }
8626
8627 /* From section 4.3.9 (Interface Blocks) of the GLSL ES 3.10 spec:
8628 *
8629 * * Arrays of arrays of blocks are not allowed
8630 */
8631 if (state->es_shader && glsl_type_is_array(block_array_type) &&
8632 glsl_type_is_array(block_array_type->fields.array)) {
8633 _mesa_glsl_error(&loc, state,
8634 "arrays of arrays interface blocks are "
8635 "not allowed");
8636 }
8637
8638 var = new(state) ir_variable(block_array_type,
8639 this->instance_name,
8640 var_mode);
8641 } else {
8642 var = new(state) ir_variable(block_type,
8643 this->instance_name,
8644 var_mode);
8645 }
8646
8647 var->data.matrix_layout = matrix_layout == GLSL_MATRIX_LAYOUT_INHERITED
8648 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR : matrix_layout;
8649
8650 if (var_mode == ir_var_shader_in || var_mode == ir_var_uniform)
8651 var->data.read_only = true;
8652
8653 var->data.patch = this->layout.flags.q.patch;
8654
8655 if (state->stage == MESA_SHADER_GEOMETRY && var_mode == ir_var_shader_in)
8656 handle_geometry_shader_input_decl(state, loc, var);
8657 else if ((state->stage == MESA_SHADER_TESS_CTRL ||
8658 state->stage == MESA_SHADER_TESS_EVAL) && var_mode == ir_var_shader_in)
8659 handle_tess_shader_input_decl(state, loc, var);
8660 else if (state->stage == MESA_SHADER_TESS_CTRL && var_mode == ir_var_shader_out)
8661 handle_tess_ctrl_shader_output_decl(state, loc, var);
8662
8663 for (unsigned i = 0; i < num_variables; i++) {
8664 if (var->data.mode == ir_var_shader_storage)
8665 apply_memory_qualifiers(var, fields[i]);
8666 }
8667
8668 if (ir_variable *earlier =
8669 state->symbols->get_variable(this->instance_name)) {
8670 if (!redeclaring_per_vertex) {
8671 _mesa_glsl_error(&loc, state, "`%s' redeclared",
8672 this->instance_name);
8673 }
8674 earlier->data.how_declared = ir_var_declared_normally;
8675 earlier->type = var->type;
8676 earlier->reinit_interface_type(block_type);
8677 delete var;
8678 } else {
8679 if (this->layout.flags.q.explicit_binding) {
8680 apply_explicit_binding(state, &loc, var, var->type,
8681 &this->layout);
8682 }
8683
8684 var->data.stream = qual_stream;
8685 if (layout.flags.q.explicit_location) {
8686 var->data.location = expl_location;
8687 var->data.explicit_location = true;
8688 }
8689
8690 state->symbols->add_variable(var);
8691 instructions->push_tail(var);
8692 }
8693 } else {
8694 /* In order to have an array size, the block must also be declared with
8695 * an instance name.
8696 */
8697 assert(this->array_specifier == NULL);
8698
8699 for (unsigned i = 0; i < num_variables; i++) {
8700 ir_variable *var =
8701 new(state) ir_variable(fields[i].type,
8702 ralloc_strdup(state, fields[i].name),
8703 var_mode);
8704 var->data.interpolation = fields[i].interpolation;
8705 var->data.centroid = fields[i].centroid;
8706 var->data.sample = fields[i].sample;
8707 var->data.patch = fields[i].patch;
8708 var->data.stream = qual_stream;
8709 var->data.location = fields[i].location;
8710
8711 if (fields[i].location != -1)
8712 var->data.explicit_location = true;
8713
8714 var->data.explicit_xfb_buffer = fields[i].explicit_xfb_buffer;
8715 var->data.xfb_buffer = fields[i].xfb_buffer;
8716
8717 if (fields[i].offset != -1)
8718 var->data.explicit_xfb_offset = true;
8719 var->data.offset = fields[i].offset;
8720
8721 var->init_interface_type(block_type);
8722
8723 if (var_mode == ir_var_shader_in || var_mode == ir_var_uniform)
8724 var->data.read_only = true;
8725
8726 /* Precision qualifiers do not have any meaning in Desktop GLSL */
8727 if (state->es_shader) {
8728 var->data.precision =
8729 select_gles_precision(fields[i].precision, fields[i].type,
8730 state, &loc);
8731 }
8732
8733 if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_INHERITED) {
8734 var->data.matrix_layout = matrix_layout == GLSL_MATRIX_LAYOUT_INHERITED
8735 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR : matrix_layout;
8736 } else {
8737 var->data.matrix_layout = fields[i].matrix_layout;
8738 }
8739
8740 if (var->data.mode == ir_var_shader_storage)
8741 apply_memory_qualifiers(var, fields[i]);
8742
8743 /* Examine var name here since var may get deleted in the next call */
8744 bool var_is_gl_id = is_gl_identifier(var->name);
8745
8746 if (redeclaring_per_vertex) {
8747 bool is_redeclaration;
8748 var =
8749 get_variable_being_redeclared(&var, loc, state,
8750 true /* allow_all_redeclarations */,
8751 &is_redeclaration);
8752 if (!var_is_gl_id || !is_redeclaration) {
8753 _mesa_glsl_error(&loc, state,
8754 "redeclaration of gl_PerVertex can only "
8755 "include built-in variables");
8756 } else if (var->data.how_declared == ir_var_declared_normally) {
8757 _mesa_glsl_error(&loc, state,
8758 "`%s' has already been redeclared",
8759 var->name);
8760 } else {
8761 var->data.how_declared = ir_var_declared_in_block;
8762 var->reinit_interface_type(block_type);
8763 }
8764 continue;
8765 }
8766
8767 if (state->symbols->get_variable(var->name) != NULL)
8768 _mesa_glsl_error(&loc, state, "`%s' redeclared", var->name);
8769
8770 /* Propagate the "binding" keyword into this UBO/SSBO's fields.
8771 * The UBO declaration itself doesn't get an ir_variable unless it
8772 * has an instance name. This is ugly.
8773 */
8774 if (this->layout.flags.q.explicit_binding) {
8775 apply_explicit_binding(state, &loc, var,
8776 var->get_interface_type(), &this->layout);
8777 }
8778
8779 if (glsl_type_is_unsized_array(var->type)) {
8780 if (var->is_in_shader_storage_block() &&
8781 is_unsized_array_last_element(var)) {
8782 var->data.from_ssbo_unsized_array = true;
8783 } else {
8784 /* From GLSL ES 3.10 spec, section 4.1.9 "Arrays":
8785 *
8786 * "If an array is declared as the last member of a shader storage
8787 * block and the size is not specified at compile-time, it is
8788 * sized at run-time. In all other cases, arrays are sized only
8789 * at compile-time."
8790 *
8791 * In desktop GLSL it is allowed to have unsized-arrays that are
8792 * not last, as long as we can determine that they are implicitly
8793 * sized.
8794 */
8795 if (state->es_shader) {
8796 _mesa_glsl_error(&loc, state, "unsized array `%s' "
8797 "definition: only last member of a shader "
8798 "storage block can be defined as unsized "
8799 "array", fields[i].name);
8800 }
8801 }
8802 }
8803
8804 state->symbols->add_variable(var);
8805 instructions->push_tail(var);
8806 }
8807
8808 if (redeclaring_per_vertex && block_type != earlier_per_vertex) {
8809 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec:
8810 *
8811 * It is also a compilation error ... to redeclare a built-in
8812 * block and then use a member from that built-in block that was
8813 * not included in the redeclaration.
8814 *
8815 * This appears to be a clarification to the behaviour established
8816 * for gl_PerVertex by GLSL 1.50, therefore we implement this
8817 * behaviour regardless of GLSL version.
8818 *
8819 * To prevent the shader from using a member that was not included in
8820 * the redeclaration, we disable any ir_variables that are still
8821 * associated with the old declaration of gl_PerVertex (since we've
8822 * already updated all of the variables contained in the new
8823 * gl_PerVertex to point to it).
8824 *
8825 * As a side effect this will prevent
8826 * validate_intrastage_interface_blocks() from getting confused and
8827 * thinking there are conflicting definitions of gl_PerVertex in the
8828 * shader.
8829 */
8830 foreach_in_list_safe(ir_instruction, node, instructions) {
8831 ir_variable *const var = node->as_variable();
8832 if (var != NULL &&
8833 var->get_interface_type() == earlier_per_vertex &&
8834 var->data.mode == var_mode) {
8835 if (var->data.how_declared == ir_var_declared_normally) {
8836 _mesa_glsl_error(&loc, state,
8837 "redeclaration of gl_PerVertex cannot "
8838 "follow a redeclaration of `%s'",
8839 var->name);
8840 }
8841 state->symbols->disable_variable(var->name);
8842 var->remove();
8843 }
8844 }
8845 }
8846 }
8847
8848 return NULL;
8849 }
8850
8851
8852 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)8853 ast_tcs_output_layout::hir(exec_list *instructions,
8854 struct _mesa_glsl_parse_state *state)
8855 {
8856 YYLTYPE loc = this->get_location();
8857
8858 unsigned num_vertices;
8859 if (!state->out_qualifier->vertices->
8860 process_qualifier_constant(state, "vertices", &num_vertices,
8861 false)) {
8862 /* return here to stop cascading incorrect error messages */
8863 return NULL;
8864 }
8865
8866 /* If any shader outputs occurred before this declaration and specified an
8867 * array size, make sure the size they specified is consistent with the
8868 * primitive type.
8869 */
8870 if (state->tcs_output_size != 0 && state->tcs_output_size != num_vertices) {
8871 _mesa_glsl_error(&loc, state,
8872 "this tessellation control shader output layout "
8873 "specifies %u vertices, but a previous output "
8874 "is declared with size %u",
8875 num_vertices, state->tcs_output_size);
8876 return NULL;
8877 }
8878
8879 state->tcs_output_vertices_specified = true;
8880
8881 /* If any shader outputs occurred before this declaration and did not
8882 * specify an array size, their size is determined now.
8883 */
8884 foreach_in_list (ir_instruction, node, instructions) {
8885 ir_variable *var = node->as_variable();
8886 if (var == NULL || var->data.mode != ir_var_shader_out)
8887 continue;
8888
8889 /* Note: Not all tessellation control shader output are arrays. */
8890 if (!glsl_type_is_unsized_array(var->type) || var->data.patch)
8891 continue;
8892
8893 if (var->data.max_array_access >= (int)num_vertices) {
8894 _mesa_glsl_error(&loc, state,
8895 "this tessellation control shader output layout "
8896 "specifies %u vertices, but an access to element "
8897 "%u of output `%s' already exists", num_vertices,
8898 var->data.max_array_access, var->name);
8899 } else {
8900 var->type = glsl_array_type(var->type->fields.array,
8901 num_vertices, 0);
8902 }
8903 }
8904
8905 return NULL;
8906 }
8907
8908
8909 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)8910 ast_gs_input_layout::hir(exec_list *instructions,
8911 struct _mesa_glsl_parse_state *state)
8912 {
8913 YYLTYPE loc = this->get_location();
8914
8915 /* Should have been prevented by the parser. */
8916 assert(!state->gs_input_prim_type_specified
8917 || state->in_qualifier->prim_type == this->prim_type);
8918
8919 /* If any shader inputs occurred before this declaration and specified an
8920 * array size, make sure the size they specified is consistent with the
8921 * primitive type.
8922 */
8923 unsigned num_vertices =
8924 mesa_vertices_per_prim(gl_to_mesa_prim(this->prim_type));
8925 if (state->gs_input_size != 0 && state->gs_input_size != num_vertices) {
8926 _mesa_glsl_error(&loc, state,
8927 "this geometry shader input layout implies %u vertices"
8928 " per primitive, but a previous input is declared"
8929 " with size %u", num_vertices, state->gs_input_size);
8930 return NULL;
8931 }
8932
8933 state->gs_input_prim_type_specified = true;
8934
8935 /* If any shader inputs occurred before this declaration and did not
8936 * specify an array size, their size is determined now.
8937 */
8938 foreach_in_list(ir_instruction, node, instructions) {
8939 ir_variable *var = node->as_variable();
8940 if (var == NULL || var->data.mode != ir_var_shader_in)
8941 continue;
8942
8943 /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
8944 * array; skip it.
8945 */
8946
8947 if (glsl_type_is_unsized_array(var->type)) {
8948 if (var->data.max_array_access >= (int)num_vertices) {
8949 _mesa_glsl_error(&loc, state,
8950 "this geometry shader input layout implies %u"
8951 " vertices, but an access to element %u of input"
8952 " `%s' already exists", num_vertices,
8953 var->data.max_array_access, var->name);
8954 } else {
8955 var->type = glsl_array_type(var->type->fields.array,
8956 num_vertices, 0);
8957 }
8958 }
8959 }
8960
8961 return NULL;
8962 }
8963
8964
8965 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)8966 ast_cs_input_layout::hir(exec_list *instructions,
8967 struct _mesa_glsl_parse_state *state)
8968 {
8969 YYLTYPE loc = this->get_location();
8970
8971 /* From the ARB_compute_shader specification:
8972 *
8973 * If the local size of the shader in any dimension is greater
8974 * than the maximum size supported by the implementation for that
8975 * dimension, a compile-time error results.
8976 *
8977 * It is not clear from the spec how the error should be reported if
8978 * the total size of the work group exceeds
8979 * MAX_COMPUTE_WORK_GROUP_INVOCATIONS, but it seems reasonable to
8980 * report it at compile time as well.
8981 */
8982 GLuint64 total_invocations = 1;
8983 unsigned qual_local_size[3];
8984 for (int i = 0; i < 3; i++) {
8985
8986 char *local_size_str = ralloc_asprintf(NULL, "invalid local_size_%c",
8987 'x' + i);
8988 /* Infer a local_size of 1 for unspecified dimensions */
8989 if (this->local_size[i] == NULL) {
8990 qual_local_size[i] = 1;
8991 } else if (!this->local_size[i]->
8992 process_qualifier_constant(state, local_size_str,
8993 &qual_local_size[i], false)) {
8994 ralloc_free(local_size_str);
8995 return NULL;
8996 }
8997 ralloc_free(local_size_str);
8998
8999 if (qual_local_size[i] > state->consts->MaxComputeWorkGroupSize[i]) {
9000 _mesa_glsl_error(&loc, state,
9001 "local_size_%c exceeds MAX_COMPUTE_WORK_GROUP_SIZE"
9002 " (%d)", 'x' + i,
9003 state->consts->MaxComputeWorkGroupSize[i]);
9004 break;
9005 }
9006 total_invocations *= qual_local_size[i];
9007 if (total_invocations >
9008 state->consts->MaxComputeWorkGroupInvocations) {
9009 _mesa_glsl_error(&loc, state,
9010 "product of local_sizes exceeds "
9011 "MAX_COMPUTE_WORK_GROUP_INVOCATIONS (%d)",
9012 state->consts->MaxComputeWorkGroupInvocations);
9013 break;
9014 }
9015 }
9016
9017 /* If any compute input layout declaration preceded this one, make sure it
9018 * was consistent with this one.
9019 */
9020 if (state->cs_input_local_size_specified) {
9021 for (int i = 0; i < 3; i++) {
9022 if (state->cs_input_local_size[i] != qual_local_size[i]) {
9023 _mesa_glsl_error(&loc, state,
9024 "compute shader input layout does not match"
9025 " previous declaration");
9026 return NULL;
9027 }
9028 }
9029 }
9030
9031 /* The ARB_compute_variable_group_size spec says:
9032 *
9033 * If a compute shader including a *local_size_variable* qualifier also
9034 * declares a fixed local group size using the *local_size_x*,
9035 * *local_size_y*, or *local_size_z* qualifiers, a compile-time error
9036 * results
9037 */
9038 if (state->cs_input_local_size_variable_specified) {
9039 _mesa_glsl_error(&loc, state,
9040 "compute shader can't include both a variable and a "
9041 "fixed local group size");
9042 return NULL;
9043 }
9044
9045 state->cs_input_local_size_specified = true;
9046 for (int i = 0; i < 3; i++)
9047 state->cs_input_local_size[i] = qual_local_size[i];
9048
9049 /* We may now declare the built-in constant gl_WorkGroupSize (see
9050 * builtin_variable_generator::generate_constants() for why we didn't
9051 * declare it earlier).
9052 */
9053 ir_variable *var = new(state->symbols)
9054 ir_variable(&glsl_type_builtin_uvec3, "gl_WorkGroupSize", ir_var_auto);
9055 var->data.how_declared = ir_var_declared_implicitly;
9056 var->data.read_only = true;
9057 instructions->push_tail(var);
9058 state->symbols->add_variable(var);
9059 ir_constant_data data;
9060 memset(&data, 0, sizeof(data));
9061 for (int i = 0; i < 3; i++)
9062 data.u[i] = qual_local_size[i];
9063 var->constant_value = new(var) ir_constant(&glsl_type_builtin_uvec3, &data);
9064 var->constant_initializer =
9065 new(var) ir_constant(&glsl_type_builtin_uvec3, &data);
9066 var->data.has_initializer = true;
9067 var->data.is_implicit_initializer = false;
9068
9069 return NULL;
9070 }
9071
9072
9073 static void
detect_conflicting_assignments(struct _mesa_glsl_parse_state * state,exec_list * instructions)9074 detect_conflicting_assignments(struct _mesa_glsl_parse_state *state,
9075 exec_list *instructions)
9076 {
9077 bool gl_FragColor_assigned = false;
9078 bool gl_FragData_assigned = false;
9079 bool gl_FragSecondaryColor_assigned = false;
9080 bool gl_FragSecondaryData_assigned = false;
9081 bool user_defined_fs_output_assigned = false;
9082 ir_variable *user_defined_fs_output = NULL;
9083
9084 /* It would be nice to have proper location information. */
9085 YYLTYPE loc;
9086 memset(&loc, 0, sizeof(loc));
9087
9088 foreach_in_list(ir_instruction, node, instructions) {
9089 ir_variable *var = node->as_variable();
9090
9091 if (!var || !var->data.assigned)
9092 continue;
9093
9094 if (strcmp(var->name, "gl_FragColor") == 0) {
9095 gl_FragColor_assigned = true;
9096 if (!var->constant_initializer && state->zero_init) {
9097 const ir_constant_data data = { { 0 } };
9098 var->data.has_initializer = true;
9099 var->data.is_implicit_initializer = true;
9100 var->constant_initializer = new(var) ir_constant(var->type, &data);
9101 }
9102 }
9103 else if (strcmp(var->name, "gl_FragData") == 0)
9104 gl_FragData_assigned = true;
9105 else if (strcmp(var->name, "gl_SecondaryFragColorEXT") == 0)
9106 gl_FragSecondaryColor_assigned = true;
9107 else if (strcmp(var->name, "gl_SecondaryFragDataEXT") == 0)
9108 gl_FragSecondaryData_assigned = true;
9109 else if (!is_gl_identifier(var->name)) {
9110 if (state->stage == MESA_SHADER_FRAGMENT &&
9111 var->data.mode == ir_var_shader_out) {
9112 user_defined_fs_output_assigned = true;
9113 user_defined_fs_output = var;
9114 }
9115 }
9116 }
9117
9118 /* From the GLSL 1.30 spec:
9119 *
9120 * "If a shader statically assigns a value to gl_FragColor, it
9121 * may not assign a value to any element of gl_FragData. If a
9122 * shader statically writes a value to any element of
9123 * gl_FragData, it may not assign a value to
9124 * gl_FragColor. That is, a shader may assign values to either
9125 * gl_FragColor or gl_FragData, but not both. Multiple shaders
9126 * linked together must also consistently write just one of
9127 * these variables. Similarly, if user declared output
9128 * variables are in use (statically assigned to), then the
9129 * built-in variables gl_FragColor and gl_FragData may not be
9130 * assigned to. These incorrect usages all generate compile
9131 * time errors."
9132 */
9133 if (gl_FragColor_assigned && gl_FragData_assigned) {
9134 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
9135 "`gl_FragColor' and `gl_FragData'");
9136 } else if (gl_FragColor_assigned && user_defined_fs_output_assigned) {
9137 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
9138 "`gl_FragColor' and `%s'",
9139 user_defined_fs_output->name);
9140 } else if (gl_FragSecondaryColor_assigned && gl_FragSecondaryData_assigned) {
9141 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
9142 "`gl_FragSecondaryColorEXT' and"
9143 " `gl_FragSecondaryDataEXT'");
9144 } else if (gl_FragColor_assigned && gl_FragSecondaryData_assigned) {
9145 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
9146 "`gl_FragColor' and"
9147 " `gl_FragSecondaryDataEXT'");
9148 } else if (gl_FragData_assigned && gl_FragSecondaryColor_assigned) {
9149 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
9150 "`gl_FragData' and"
9151 " `gl_FragSecondaryColorEXT'");
9152 } else if (gl_FragData_assigned && user_defined_fs_output_assigned) {
9153 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
9154 "`gl_FragData' and `%s'",
9155 user_defined_fs_output->name);
9156 }
9157
9158 if ((gl_FragSecondaryColor_assigned || gl_FragSecondaryData_assigned) &&
9159 !state->EXT_blend_func_extended_enable) {
9160 _mesa_glsl_error(&loc, state,
9161 "Dual source blending requires EXT_blend_func_extended");
9162 }
9163 }
9164
9165 static void
verify_subroutine_associated_funcs(struct _mesa_glsl_parse_state * state)9166 verify_subroutine_associated_funcs(struct _mesa_glsl_parse_state *state)
9167 {
9168 YYLTYPE loc;
9169 memset(&loc, 0, sizeof(loc));
9170
9171 /* Section 6.1.2 (Subroutines) of the GLSL 4.00 spec says:
9172 *
9173 * "A program will fail to compile or link if any shader
9174 * or stage contains two or more functions with the same
9175 * name if the name is associated with a subroutine type."
9176 */
9177
9178 for (int i = 0; i < state->num_subroutines; i++) {
9179 unsigned definitions = 0;
9180 ir_function *fn = state->subroutines[i];
9181 /* Calculate number of function definitions with the same name */
9182 foreach_in_list(ir_function_signature, sig, &fn->signatures) {
9183 if (sig->is_defined) {
9184 if (++definitions > 1) {
9185 _mesa_glsl_error(&loc, state,
9186 "%s shader contains two or more function "
9187 "definitions with name `%s', which is "
9188 "associated with a subroutine type.\n",
9189 _mesa_shader_stage_to_string(state->stage),
9190 fn->name);
9191 return;
9192 }
9193 }
9194 }
9195 }
9196 }
9197
9198 static void
remove_per_vertex_blocks(exec_list * instructions,_mesa_glsl_parse_state * state,ir_variable_mode mode)9199 remove_per_vertex_blocks(exec_list *instructions,
9200 _mesa_glsl_parse_state *state, ir_variable_mode mode)
9201 {
9202 /* Find the gl_PerVertex interface block of the appropriate (in/out) mode,
9203 * if it exists in this shader type.
9204 */
9205 const glsl_type *per_vertex = NULL;
9206 switch (mode) {
9207 case ir_var_shader_in:
9208 if (ir_variable *gl_in = state->symbols->get_variable("gl_in"))
9209 per_vertex = gl_in->get_interface_type();
9210 break;
9211 case ir_var_shader_out:
9212 if (ir_variable *gl_Position =
9213 state->symbols->get_variable("gl_Position")) {
9214 per_vertex = gl_Position->get_interface_type();
9215 }
9216 break;
9217 default:
9218 assert(!"Unexpected mode");
9219 break;
9220 }
9221
9222 /* If we didn't find a built-in gl_PerVertex interface block, then we don't
9223 * need to do anything.
9224 */
9225 if (per_vertex == NULL)
9226 return;
9227
9228 /* If the interface block is used by the shader, then we don't need to do
9229 * anything.
9230 */
9231 interface_block_usage_visitor v(mode, per_vertex);
9232 v.run(instructions);
9233 if (v.usage_found())
9234 return;
9235
9236 /* Remove any ir_variable declarations that refer to the interface block
9237 * we're removing.
9238 */
9239 foreach_in_list_safe(ir_instruction, node, instructions) {
9240 ir_variable *const var = node->as_variable();
9241 if (var != NULL && var->get_interface_type() == per_vertex &&
9242 var->data.mode == mode &&
9243 var->data.how_declared == ir_var_declared_implicitly) {
9244 state->symbols->disable_variable(var->name);
9245 var->remove();
9246 }
9247 }
9248 }
9249
9250 ir_rvalue *
hir(exec_list *,struct _mesa_glsl_parse_state * state)9251 ast_warnings_toggle::hir(exec_list *,
9252 struct _mesa_glsl_parse_state *state)
9253 {
9254 state->warnings_enabled = enable;
9255 return NULL;
9256 }
9257