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