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