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