101e04c3fSmrg/* 201e04c3fSmrg * Copyright © 2010 Intel Corporation 301e04c3fSmrg * 401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a 501e04c3fSmrg * copy of this software and associated documentation files (the "Software"), 601e04c3fSmrg * to deal in the Software without restriction, including without limitation 701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the 901e04c3fSmrg * Software is furnished to do so, subject to the following conditions: 1001e04c3fSmrg * 1101e04c3fSmrg * The above copyright notice and this permission notice (including the next 1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the 1301e04c3fSmrg * Software. 1401e04c3fSmrg * 1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 2101e04c3fSmrg * DEALINGS IN THE SOFTWARE. 2201e04c3fSmrg */ 2301e04c3fSmrg 2401e04c3fSmrg/** 2501e04c3fSmrg * \file ast_to_hir.c 2601e04c3fSmrg * Convert abstract syntax to to high-level intermediate reprensentation (HIR). 2701e04c3fSmrg * 2801e04c3fSmrg * During the conversion to HIR, the majority of the symantic checking is 2901e04c3fSmrg * preformed on the program. This includes: 3001e04c3fSmrg * 3101e04c3fSmrg * * Symbol table management 3201e04c3fSmrg * * Type checking 3301e04c3fSmrg * * Function binding 3401e04c3fSmrg * 3501e04c3fSmrg * The majority of this work could be done during parsing, and the parser could 3601e04c3fSmrg * probably generate HIR directly. However, this results in frequent changes 3701e04c3fSmrg * to the parser code. Since we do not assume that every system this complier 3801e04c3fSmrg * is built on will have Flex and Bison installed, we have to store the code 3901e04c3fSmrg * generated by these tools in our version control system. In other parts of 4001e04c3fSmrg * the system we've seen problems where a parser was changed but the generated 4101e04c3fSmrg * code was not committed, merge conflicts where created because two developers 4201e04c3fSmrg * had slightly different versions of Bison installed, etc. 4301e04c3fSmrg * 4401e04c3fSmrg * I have also noticed that running Bison generated parsers in GDB is very 4501e04c3fSmrg * irritating. When you get a segfault on '$$ = $1->foo', you can't very 4601e04c3fSmrg * well 'print $1' in GDB. 4701e04c3fSmrg * 4801e04c3fSmrg * As a result, my preference is to put as little C code as possible in the 4901e04c3fSmrg * parser (and lexer) sources. 5001e04c3fSmrg */ 5101e04c3fSmrg 5201e04c3fSmrg#include "glsl_symbol_table.h" 5301e04c3fSmrg#include "glsl_parser_extras.h" 5401e04c3fSmrg#include "ast.h" 5501e04c3fSmrg#include "compiler/glsl_types.h" 5601e04c3fSmrg#include "util/hash_table.h" 5701e04c3fSmrg#include "main/mtypes.h" 5801e04c3fSmrg#include "main/macros.h" 5901e04c3fSmrg#include "main/shaderobj.h" 6001e04c3fSmrg#include "ir.h" 6101e04c3fSmrg#include "ir_builder.h" 6201e04c3fSmrg#include "builtin_functions.h" 6301e04c3fSmrg 6401e04c3fSmrgusing namespace ir_builder; 6501e04c3fSmrg 6601e04c3fSmrgstatic void 6701e04c3fSmrgdetect_conflicting_assignments(struct _mesa_glsl_parse_state *state, 6801e04c3fSmrg exec_list *instructions); 6901e04c3fSmrgstatic void 7001e04c3fSmrgverify_subroutine_associated_funcs(struct _mesa_glsl_parse_state *state); 7101e04c3fSmrg 7201e04c3fSmrgstatic void 7301e04c3fSmrgremove_per_vertex_blocks(exec_list *instructions, 7401e04c3fSmrg _mesa_glsl_parse_state *state, ir_variable_mode mode); 7501e04c3fSmrg 7601e04c3fSmrg/** 7701e04c3fSmrg * Visitor class that finds the first instance of any write-only variable that 7801e04c3fSmrg * is ever read, if any 7901e04c3fSmrg */ 8001e04c3fSmrgclass read_from_write_only_variable_visitor : public ir_hierarchical_visitor 8101e04c3fSmrg{ 8201e04c3fSmrgpublic: 8301e04c3fSmrg read_from_write_only_variable_visitor() : found(NULL) 8401e04c3fSmrg { 8501e04c3fSmrg } 8601e04c3fSmrg 8701e04c3fSmrg virtual ir_visitor_status visit(ir_dereference_variable *ir) 8801e04c3fSmrg { 8901e04c3fSmrg if (this->in_assignee) 9001e04c3fSmrg return visit_continue; 9101e04c3fSmrg 9201e04c3fSmrg ir_variable *var = ir->variable_referenced(); 9301e04c3fSmrg /* We can have memory_write_only set on both images and buffer variables, 9401e04c3fSmrg * but in the former there is a distinction between reads from 9501e04c3fSmrg * the variable itself (write_only) and from the memory they point to 9601e04c3fSmrg * (memory_write_only), while in the case of buffer variables there is 9701e04c3fSmrg * no such distinction, that is why this check here is limited to 9801e04c3fSmrg * buffer variables alone. 9901e04c3fSmrg */ 10001e04c3fSmrg if (!var || var->data.mode != ir_var_shader_storage) 10101e04c3fSmrg return visit_continue; 10201e04c3fSmrg 10301e04c3fSmrg if (var->data.memory_write_only) { 10401e04c3fSmrg found = var; 10501e04c3fSmrg return visit_stop; 10601e04c3fSmrg } 10701e04c3fSmrg 10801e04c3fSmrg return visit_continue; 10901e04c3fSmrg } 11001e04c3fSmrg 11101e04c3fSmrg ir_variable *get_variable() { 11201e04c3fSmrg return found; 11301e04c3fSmrg } 11401e04c3fSmrg 11501e04c3fSmrg virtual ir_visitor_status visit_enter(ir_expression *ir) 11601e04c3fSmrg { 11701e04c3fSmrg /* .length() doesn't actually read anything */ 11801e04c3fSmrg if (ir->operation == ir_unop_ssbo_unsized_array_length) 11901e04c3fSmrg return visit_continue_with_parent; 12001e04c3fSmrg 12101e04c3fSmrg return visit_continue; 12201e04c3fSmrg } 12301e04c3fSmrg 12401e04c3fSmrgprivate: 12501e04c3fSmrg ir_variable *found; 12601e04c3fSmrg}; 12701e04c3fSmrg 12801e04c3fSmrgvoid 12901e04c3fSmrg_mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state) 13001e04c3fSmrg{ 13101e04c3fSmrg _mesa_glsl_initialize_variables(instructions, state); 13201e04c3fSmrg 13301e04c3fSmrg state->symbols->separate_function_namespace = state->language_version == 110; 13401e04c3fSmrg 13501e04c3fSmrg state->current_function = NULL; 13601e04c3fSmrg 13701e04c3fSmrg state->toplevel_ir = instructions; 13801e04c3fSmrg 13901e04c3fSmrg state->gs_input_prim_type_specified = false; 14001e04c3fSmrg state->tcs_output_vertices_specified = false; 14101e04c3fSmrg state->cs_input_local_size_specified = false; 14201e04c3fSmrg 14301e04c3fSmrg /* Section 4.2 of the GLSL 1.20 specification states: 14401e04c3fSmrg * "The built-in functions are scoped in a scope outside the global scope 14501e04c3fSmrg * users declare global variables in. That is, a shader's global scope, 14601e04c3fSmrg * available for user-defined functions and global variables, is nested 14701e04c3fSmrg * inside the scope containing the built-in functions." 14801e04c3fSmrg * 14901e04c3fSmrg * Since built-in functions like ftransform() access built-in variables, 15001e04c3fSmrg * it follows that those must be in the outer scope as well. 15101e04c3fSmrg * 15201e04c3fSmrg * We push scope here to create this nesting effect...but don't pop. 15301e04c3fSmrg * This way, a shader's globals are still in the symbol table for use 15401e04c3fSmrg * by the linker. 15501e04c3fSmrg */ 15601e04c3fSmrg state->symbols->push_scope(); 15701e04c3fSmrg 15801e04c3fSmrg foreach_list_typed (ast_node, ast, link, & state->translation_unit) 15901e04c3fSmrg ast->hir(instructions, state); 16001e04c3fSmrg 16101e04c3fSmrg verify_subroutine_associated_funcs(state); 16201e04c3fSmrg detect_recursion_unlinked(state, instructions); 16301e04c3fSmrg detect_conflicting_assignments(state, instructions); 16401e04c3fSmrg 16501e04c3fSmrg state->toplevel_ir = NULL; 16601e04c3fSmrg 16701e04c3fSmrg /* Move all of the variable declarations to the front of the IR list, and 16801e04c3fSmrg * reverse the order. This has the (intended!) side effect that vertex 16901e04c3fSmrg * shader inputs and fragment shader outputs will appear in the IR in the 17001e04c3fSmrg * same order that they appeared in the shader code. This results in the 17101e04c3fSmrg * locations being assigned in the declared order. Many (arguably buggy) 17201e04c3fSmrg * applications depend on this behavior, and it matches what nearly all 17301e04c3fSmrg * other drivers do. 17401e04c3fSmrg */ 17501e04c3fSmrg foreach_in_list_safe(ir_instruction, node, instructions) { 17601e04c3fSmrg ir_variable *const var = node->as_variable(); 17701e04c3fSmrg 17801e04c3fSmrg if (var == NULL) 17901e04c3fSmrg continue; 18001e04c3fSmrg 18101e04c3fSmrg var->remove(); 18201e04c3fSmrg instructions->push_head(var); 18301e04c3fSmrg } 18401e04c3fSmrg 18501e04c3fSmrg /* Figure out if gl_FragCoord is actually used in fragment shader */ 18601e04c3fSmrg ir_variable *const var = state->symbols->get_variable("gl_FragCoord"); 18701e04c3fSmrg if (var != NULL) 18801e04c3fSmrg state->fs_uses_gl_fragcoord = var->data.used; 18901e04c3fSmrg 19001e04c3fSmrg /* From section 7.1 (Built-In Language Variables) of the GLSL 4.10 spec: 19101e04c3fSmrg * 19201e04c3fSmrg * If multiple shaders using members of a built-in block belonging to 19301e04c3fSmrg * the same interface are linked together in the same program, they 19401e04c3fSmrg * must all redeclare the built-in block in the same way, as described 19501e04c3fSmrg * in section 4.3.7 "Interface Blocks" for interface block matching, or 19601e04c3fSmrg * a link error will result. 19701e04c3fSmrg * 19801e04c3fSmrg * The phrase "using members of a built-in block" implies that if two 19901e04c3fSmrg * shaders are linked together and one of them *does not use* any members 20001e04c3fSmrg * of the built-in block, then that shader does not need to have a matching 20101e04c3fSmrg * redeclaration of the built-in block. 20201e04c3fSmrg * 20301e04c3fSmrg * This appears to be a clarification to the behaviour established for 20401e04c3fSmrg * gl_PerVertex by GLSL 1.50, therefore implement it regardless of GLSL 20501e04c3fSmrg * version. 20601e04c3fSmrg * 20701e04c3fSmrg * The definition of "interface" in section 4.3.7 that applies here is as 20801e04c3fSmrg * follows: 20901e04c3fSmrg * 21001e04c3fSmrg * The boundary between adjacent programmable pipeline stages: This 21101e04c3fSmrg * spans all the outputs in all compilation units of the first stage 21201e04c3fSmrg * and all the inputs in all compilation units of the second stage. 21301e04c3fSmrg * 21401e04c3fSmrg * Therefore this rule applies to both inter- and intra-stage linking. 21501e04c3fSmrg * 21601e04c3fSmrg * The easiest way to implement this is to check whether the shader uses 21701e04c3fSmrg * gl_PerVertex right after ast-to-ir conversion, and if it doesn't, simply 21801e04c3fSmrg * remove all the relevant variable declaration from the IR, so that the 21901e04c3fSmrg * linker won't see them and complain about mismatches. 22001e04c3fSmrg */ 22101e04c3fSmrg remove_per_vertex_blocks(instructions, state, ir_var_shader_in); 22201e04c3fSmrg remove_per_vertex_blocks(instructions, state, ir_var_shader_out); 22301e04c3fSmrg 22401e04c3fSmrg /* Check that we don't have reads from write-only variables */ 22501e04c3fSmrg read_from_write_only_variable_visitor v; 22601e04c3fSmrg v.run(instructions); 22701e04c3fSmrg ir_variable *error_var = v.get_variable(); 22801e04c3fSmrg if (error_var) { 22901e04c3fSmrg /* It would be nice to have proper location information, but for that 23001e04c3fSmrg * we would need to check this as we process each kind of AST node 23101e04c3fSmrg */ 23201e04c3fSmrg YYLTYPE loc; 23301e04c3fSmrg memset(&loc, 0, sizeof(loc)); 23401e04c3fSmrg _mesa_glsl_error(&loc, state, "Read from write-only variable `%s'", 23501e04c3fSmrg error_var->name); 23601e04c3fSmrg } 23701e04c3fSmrg} 23801e04c3fSmrg 23901e04c3fSmrg 24001e04c3fSmrgstatic ir_expression_operation 24101e04c3fSmrgget_implicit_conversion_operation(const glsl_type *to, const glsl_type *from, 24201e04c3fSmrg struct _mesa_glsl_parse_state *state) 24301e04c3fSmrg{ 24401e04c3fSmrg switch (to->base_type) { 24501e04c3fSmrg case GLSL_TYPE_FLOAT: 24601e04c3fSmrg switch (from->base_type) { 24701e04c3fSmrg case GLSL_TYPE_INT: return ir_unop_i2f; 24801e04c3fSmrg case GLSL_TYPE_UINT: return ir_unop_u2f; 24901e04c3fSmrg default: return (ir_expression_operation)0; 25001e04c3fSmrg } 25101e04c3fSmrg 25201e04c3fSmrg case GLSL_TYPE_UINT: 2537ec681f3Smrg if (!state->has_implicit_int_to_uint_conversion()) 25401e04c3fSmrg return (ir_expression_operation)0; 25501e04c3fSmrg switch (from->base_type) { 25601e04c3fSmrg case GLSL_TYPE_INT: return ir_unop_i2u; 25701e04c3fSmrg default: return (ir_expression_operation)0; 25801e04c3fSmrg } 25901e04c3fSmrg 26001e04c3fSmrg case GLSL_TYPE_DOUBLE: 26101e04c3fSmrg if (!state->has_double()) 26201e04c3fSmrg return (ir_expression_operation)0; 26301e04c3fSmrg switch (from->base_type) { 26401e04c3fSmrg case GLSL_TYPE_INT: return ir_unop_i2d; 26501e04c3fSmrg case GLSL_TYPE_UINT: return ir_unop_u2d; 26601e04c3fSmrg case GLSL_TYPE_FLOAT: return ir_unop_f2d; 26701e04c3fSmrg case GLSL_TYPE_INT64: return ir_unop_i642d; 26801e04c3fSmrg case GLSL_TYPE_UINT64: return ir_unop_u642d; 26901e04c3fSmrg default: return (ir_expression_operation)0; 27001e04c3fSmrg } 27101e04c3fSmrg 27201e04c3fSmrg case GLSL_TYPE_UINT64: 27301e04c3fSmrg if (!state->has_int64()) 27401e04c3fSmrg return (ir_expression_operation)0; 27501e04c3fSmrg switch (from->base_type) { 27601e04c3fSmrg case GLSL_TYPE_INT: return ir_unop_i2u64; 27701e04c3fSmrg case GLSL_TYPE_UINT: return ir_unop_u2u64; 27801e04c3fSmrg case GLSL_TYPE_INT64: return ir_unop_i642u64; 27901e04c3fSmrg default: return (ir_expression_operation)0; 28001e04c3fSmrg } 28101e04c3fSmrg 28201e04c3fSmrg case GLSL_TYPE_INT64: 28301e04c3fSmrg if (!state->has_int64()) 28401e04c3fSmrg return (ir_expression_operation)0; 28501e04c3fSmrg switch (from->base_type) { 28601e04c3fSmrg case GLSL_TYPE_INT: return ir_unop_i2i64; 28701e04c3fSmrg default: return (ir_expression_operation)0; 28801e04c3fSmrg } 28901e04c3fSmrg 29001e04c3fSmrg default: return (ir_expression_operation)0; 29101e04c3fSmrg } 29201e04c3fSmrg} 29301e04c3fSmrg 29401e04c3fSmrg 29501e04c3fSmrg/** 29601e04c3fSmrg * If a conversion is available, convert one operand to a different type 29701e04c3fSmrg * 29801e04c3fSmrg * The \c from \c ir_rvalue is converted "in place". 29901e04c3fSmrg * 30001e04c3fSmrg * \param to Type that the operand it to be converted to 30101e04c3fSmrg * \param from Operand that is being converted 30201e04c3fSmrg * \param state GLSL compiler state 30301e04c3fSmrg * 30401e04c3fSmrg * \return 30501e04c3fSmrg * If a conversion is possible (or unnecessary), \c true is returned. 30601e04c3fSmrg * Otherwise \c false is returned. 30701e04c3fSmrg */ 30801e04c3fSmrgstatic bool 30901e04c3fSmrgapply_implicit_conversion(const glsl_type *to, ir_rvalue * &from, 31001e04c3fSmrg struct _mesa_glsl_parse_state *state) 31101e04c3fSmrg{ 31201e04c3fSmrg void *ctx = state; 31301e04c3fSmrg if (to->base_type == from->type->base_type) 31401e04c3fSmrg return true; 31501e04c3fSmrg 31601e04c3fSmrg /* Prior to GLSL 1.20, there are no implicit conversions */ 317ed98bd31Smaya if (!state->has_implicit_conversions()) 31801e04c3fSmrg return false; 31901e04c3fSmrg 32001e04c3fSmrg /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec: 32101e04c3fSmrg * 32201e04c3fSmrg * "There are no implicit array or structure conversions. For 32301e04c3fSmrg * example, an array of int cannot be implicitly converted to an 32401e04c3fSmrg * array of float. 32501e04c3fSmrg */ 32601e04c3fSmrg if (!to->is_numeric() || !from->type->is_numeric()) 32701e04c3fSmrg return false; 32801e04c3fSmrg 32901e04c3fSmrg /* We don't actually want the specific type `to`, we want a type 33001e04c3fSmrg * with the same base type as `to`, but the same vector width as 33101e04c3fSmrg * `from`. 33201e04c3fSmrg */ 33301e04c3fSmrg to = glsl_type::get_instance(to->base_type, from->type->vector_elements, 33401e04c3fSmrg from->type->matrix_columns); 33501e04c3fSmrg 33601e04c3fSmrg ir_expression_operation op = get_implicit_conversion_operation(to, from->type, state); 33701e04c3fSmrg if (op) { 33801e04c3fSmrg from = new(ctx) ir_expression(op, to, from, NULL); 33901e04c3fSmrg return true; 34001e04c3fSmrg } else { 34101e04c3fSmrg return false; 34201e04c3fSmrg } 34301e04c3fSmrg} 34401e04c3fSmrg 34501e04c3fSmrg 34601e04c3fSmrgstatic const struct glsl_type * 34701e04c3fSmrgarithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b, 34801e04c3fSmrg bool multiply, 34901e04c3fSmrg struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 35001e04c3fSmrg{ 35101e04c3fSmrg const glsl_type *type_a = value_a->type; 35201e04c3fSmrg const glsl_type *type_b = value_b->type; 35301e04c3fSmrg 35401e04c3fSmrg /* From GLSL 1.50 spec, page 56: 35501e04c3fSmrg * 35601e04c3fSmrg * "The arithmetic binary operators add (+), subtract (-), 35701e04c3fSmrg * multiply (*), and divide (/) operate on integer and 35801e04c3fSmrg * floating-point scalars, vectors, and matrices." 35901e04c3fSmrg */ 36001e04c3fSmrg if (!type_a->is_numeric() || !type_b->is_numeric()) { 36101e04c3fSmrg _mesa_glsl_error(loc, state, 36201e04c3fSmrg "operands to arithmetic operators must be numeric"); 36301e04c3fSmrg return glsl_type::error_type; 36401e04c3fSmrg } 36501e04c3fSmrg 36601e04c3fSmrg 36701e04c3fSmrg /* "If one operand is floating-point based and the other is 36801e04c3fSmrg * not, then the conversions from Section 4.1.10 "Implicit 36901e04c3fSmrg * Conversions" are applied to the non-floating-point-based operand." 37001e04c3fSmrg */ 37101e04c3fSmrg if (!apply_implicit_conversion(type_a, value_b, state) 37201e04c3fSmrg && !apply_implicit_conversion(type_b, value_a, state)) { 37301e04c3fSmrg _mesa_glsl_error(loc, state, 37401e04c3fSmrg "could not implicitly convert operands to " 37501e04c3fSmrg "arithmetic operator"); 37601e04c3fSmrg return glsl_type::error_type; 37701e04c3fSmrg } 37801e04c3fSmrg type_a = value_a->type; 37901e04c3fSmrg type_b = value_b->type; 38001e04c3fSmrg 38101e04c3fSmrg /* "If the operands are integer types, they must both be signed or 38201e04c3fSmrg * both be unsigned." 38301e04c3fSmrg * 38401e04c3fSmrg * From this rule and the preceeding conversion it can be inferred that 38501e04c3fSmrg * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT. 38601e04c3fSmrg * The is_numeric check above already filtered out the case where either 38701e04c3fSmrg * type is not one of these, so now the base types need only be tested for 38801e04c3fSmrg * equality. 38901e04c3fSmrg */ 39001e04c3fSmrg if (type_a->base_type != type_b->base_type) { 39101e04c3fSmrg _mesa_glsl_error(loc, state, 39201e04c3fSmrg "base type mismatch for arithmetic operator"); 39301e04c3fSmrg return glsl_type::error_type; 39401e04c3fSmrg } 39501e04c3fSmrg 39601e04c3fSmrg /* "All arithmetic binary operators result in the same fundamental type 39701e04c3fSmrg * (signed integer, unsigned integer, or floating-point) as the 39801e04c3fSmrg * operands they operate on, after operand type conversion. After 39901e04c3fSmrg * conversion, the following cases are valid 40001e04c3fSmrg * 40101e04c3fSmrg * * The two operands are scalars. In this case the operation is 40201e04c3fSmrg * applied, resulting in a scalar." 40301e04c3fSmrg */ 40401e04c3fSmrg if (type_a->is_scalar() && type_b->is_scalar()) 40501e04c3fSmrg return type_a; 40601e04c3fSmrg 40701e04c3fSmrg /* "* One operand is a scalar, and the other is a vector or matrix. 40801e04c3fSmrg * In this case, the scalar operation is applied independently to each 40901e04c3fSmrg * component of the vector or matrix, resulting in the same size 41001e04c3fSmrg * vector or matrix." 41101e04c3fSmrg */ 41201e04c3fSmrg if (type_a->is_scalar()) { 41301e04c3fSmrg if (!type_b->is_scalar()) 41401e04c3fSmrg return type_b; 41501e04c3fSmrg } else if (type_b->is_scalar()) { 41601e04c3fSmrg return type_a; 41701e04c3fSmrg } 41801e04c3fSmrg 41901e04c3fSmrg /* All of the combinations of <scalar, scalar>, <vector, scalar>, 42001e04c3fSmrg * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been 42101e04c3fSmrg * handled. 42201e04c3fSmrg */ 42301e04c3fSmrg assert(!type_a->is_scalar()); 42401e04c3fSmrg assert(!type_b->is_scalar()); 42501e04c3fSmrg 42601e04c3fSmrg /* "* The two operands are vectors of the same size. In this case, the 42701e04c3fSmrg * operation is done component-wise resulting in the same size 42801e04c3fSmrg * vector." 42901e04c3fSmrg */ 43001e04c3fSmrg if (type_a->is_vector() && type_b->is_vector()) { 43101e04c3fSmrg if (type_a == type_b) { 43201e04c3fSmrg return type_a; 43301e04c3fSmrg } else { 43401e04c3fSmrg _mesa_glsl_error(loc, state, 43501e04c3fSmrg "vector size mismatch for arithmetic operator"); 43601e04c3fSmrg return glsl_type::error_type; 43701e04c3fSmrg } 43801e04c3fSmrg } 43901e04c3fSmrg 44001e04c3fSmrg /* All of the combinations of <scalar, scalar>, <vector, scalar>, 44101e04c3fSmrg * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and 44201e04c3fSmrg * <vector, vector> have been handled. At least one of the operands must 44301e04c3fSmrg * be matrix. Further, since there are no integer matrix types, the base 44401e04c3fSmrg * type of both operands must be float. 44501e04c3fSmrg */ 44601e04c3fSmrg assert(type_a->is_matrix() || type_b->is_matrix()); 44701e04c3fSmrg assert(type_a->is_float() || type_a->is_double()); 44801e04c3fSmrg assert(type_b->is_float() || type_b->is_double()); 44901e04c3fSmrg 45001e04c3fSmrg /* "* The operator is add (+), subtract (-), or divide (/), and the 45101e04c3fSmrg * operands are matrices with the same number of rows and the same 45201e04c3fSmrg * number of columns. In this case, the operation is done component- 45301e04c3fSmrg * wise resulting in the same size matrix." 45401e04c3fSmrg * * The operator is multiply (*), where both operands are matrices or 45501e04c3fSmrg * one operand is a vector and the other a matrix. A right vector 45601e04c3fSmrg * operand is treated as a column vector and a left vector operand as a 45701e04c3fSmrg * row vector. In all these cases, it is required that the number of 45801e04c3fSmrg * columns of the left operand is equal to the number of rows of the 45901e04c3fSmrg * right operand. Then, the multiply (*) operation does a linear 46001e04c3fSmrg * algebraic multiply, yielding an object that has the same number of 46101e04c3fSmrg * rows as the left operand and the same number of columns as the right 46201e04c3fSmrg * operand. Section 5.10 "Vector and Matrix Operations" explains in 46301e04c3fSmrg * more detail how vectors and matrices are operated on." 46401e04c3fSmrg */ 46501e04c3fSmrg if (! multiply) { 46601e04c3fSmrg if (type_a == type_b) 46701e04c3fSmrg return type_a; 46801e04c3fSmrg } else { 46901e04c3fSmrg const glsl_type *type = glsl_type::get_mul_type(type_a, type_b); 47001e04c3fSmrg 47101e04c3fSmrg if (type == glsl_type::error_type) { 47201e04c3fSmrg _mesa_glsl_error(loc, state, 47301e04c3fSmrg "size mismatch for matrix multiplication"); 47401e04c3fSmrg } 47501e04c3fSmrg 47601e04c3fSmrg return type; 47701e04c3fSmrg } 47801e04c3fSmrg 47901e04c3fSmrg 48001e04c3fSmrg /* "All other cases are illegal." 48101e04c3fSmrg */ 48201e04c3fSmrg _mesa_glsl_error(loc, state, "type mismatch"); 48301e04c3fSmrg return glsl_type::error_type; 48401e04c3fSmrg} 48501e04c3fSmrg 48601e04c3fSmrg 48701e04c3fSmrgstatic const struct glsl_type * 48801e04c3fSmrgunary_arithmetic_result_type(const struct glsl_type *type, 48901e04c3fSmrg struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 49001e04c3fSmrg{ 49101e04c3fSmrg /* From GLSL 1.50 spec, page 57: 49201e04c3fSmrg * 49301e04c3fSmrg * "The arithmetic unary operators negate (-), post- and pre-increment 49401e04c3fSmrg * and decrement (-- and ++) operate on integer or floating-point 49501e04c3fSmrg * values (including vectors and matrices). All unary operators work 49601e04c3fSmrg * component-wise on their operands. These result with the same type 49701e04c3fSmrg * they operated on." 49801e04c3fSmrg */ 49901e04c3fSmrg if (!type->is_numeric()) { 50001e04c3fSmrg _mesa_glsl_error(loc, state, 50101e04c3fSmrg "operands to arithmetic operators must be numeric"); 50201e04c3fSmrg return glsl_type::error_type; 50301e04c3fSmrg } 50401e04c3fSmrg 50501e04c3fSmrg return type; 50601e04c3fSmrg} 50701e04c3fSmrg 50801e04c3fSmrg/** 50901e04c3fSmrg * \brief Return the result type of a bit-logic operation. 51001e04c3fSmrg * 51101e04c3fSmrg * If the given types to the bit-logic operator are invalid, return 51201e04c3fSmrg * glsl_type::error_type. 51301e04c3fSmrg * 51401e04c3fSmrg * \param value_a LHS of bit-logic op 51501e04c3fSmrg * \param value_b RHS of bit-logic op 51601e04c3fSmrg */ 51701e04c3fSmrgstatic const struct glsl_type * 51801e04c3fSmrgbit_logic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b, 51901e04c3fSmrg ast_operators op, 52001e04c3fSmrg struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 52101e04c3fSmrg{ 52201e04c3fSmrg const glsl_type *type_a = value_a->type; 52301e04c3fSmrg const glsl_type *type_b = value_b->type; 52401e04c3fSmrg 52501e04c3fSmrg if (!state->check_bitwise_operations_allowed(loc)) { 52601e04c3fSmrg return glsl_type::error_type; 52701e04c3fSmrg } 52801e04c3fSmrg 52901e04c3fSmrg /* From page 50 (page 56 of PDF) of GLSL 1.30 spec: 53001e04c3fSmrg * 53101e04c3fSmrg * "The bitwise operators and (&), exclusive-or (^), and inclusive-or 53201e04c3fSmrg * (|). The operands must be of type signed or unsigned integers or 53301e04c3fSmrg * integer vectors." 53401e04c3fSmrg */ 53501e04c3fSmrg if (!type_a->is_integer_32_64()) { 53601e04c3fSmrg _mesa_glsl_error(loc, state, "LHS of `%s' must be an integer", 53701e04c3fSmrg ast_expression::operator_string(op)); 53801e04c3fSmrg return glsl_type::error_type; 53901e04c3fSmrg } 54001e04c3fSmrg if (!type_b->is_integer_32_64()) { 54101e04c3fSmrg _mesa_glsl_error(loc, state, "RHS of `%s' must be an integer", 54201e04c3fSmrg ast_expression::operator_string(op)); 54301e04c3fSmrg return glsl_type::error_type; 54401e04c3fSmrg } 54501e04c3fSmrg 54601e04c3fSmrg /* Prior to GLSL 4.0 / GL_ARB_gpu_shader5, implicit conversions didn't 54701e04c3fSmrg * make sense for bitwise operations, as they don't operate on floats. 54801e04c3fSmrg * 54901e04c3fSmrg * GLSL 4.0 added implicit int -> uint conversions, which are relevant 55001e04c3fSmrg * here. It wasn't clear whether or not we should apply them to bitwise 55101e04c3fSmrg * operations. However, Khronos has decided that they should in future 55201e04c3fSmrg * language revisions. Applications also rely on this behavior. We opt 55301e04c3fSmrg * to apply them in general, but issue a portability warning. 55401e04c3fSmrg * 55501e04c3fSmrg * See https://www.khronos.org/bugzilla/show_bug.cgi?id=1405 55601e04c3fSmrg */ 55701e04c3fSmrg if (type_a->base_type != type_b->base_type) { 55801e04c3fSmrg if (!apply_implicit_conversion(type_a, value_b, state) 55901e04c3fSmrg && !apply_implicit_conversion(type_b, value_a, state)) { 56001e04c3fSmrg _mesa_glsl_error(loc, state, 56101e04c3fSmrg "could not implicitly convert operands to " 56201e04c3fSmrg "`%s` operator", 56301e04c3fSmrg ast_expression::operator_string(op)); 56401e04c3fSmrg return glsl_type::error_type; 56501e04c3fSmrg } else { 56601e04c3fSmrg _mesa_glsl_warning(loc, state, 56701e04c3fSmrg "some implementations may not support implicit " 56801e04c3fSmrg "int -> uint conversions for `%s' operators; " 56901e04c3fSmrg "consider casting explicitly for portability", 57001e04c3fSmrg ast_expression::operator_string(op)); 57101e04c3fSmrg } 57201e04c3fSmrg type_a = value_a->type; 57301e04c3fSmrg type_b = value_b->type; 57401e04c3fSmrg } 57501e04c3fSmrg 57601e04c3fSmrg /* "The fundamental types of the operands (signed or unsigned) must 57701e04c3fSmrg * match," 57801e04c3fSmrg */ 57901e04c3fSmrg if (type_a->base_type != type_b->base_type) { 58001e04c3fSmrg _mesa_glsl_error(loc, state, "operands of `%s' must have the same " 58101e04c3fSmrg "base type", ast_expression::operator_string(op)); 58201e04c3fSmrg return glsl_type::error_type; 58301e04c3fSmrg } 58401e04c3fSmrg 58501e04c3fSmrg /* "The operands cannot be vectors of differing size." */ 58601e04c3fSmrg if (type_a->is_vector() && 58701e04c3fSmrg type_b->is_vector() && 58801e04c3fSmrg type_a->vector_elements != type_b->vector_elements) { 58901e04c3fSmrg _mesa_glsl_error(loc, state, "operands of `%s' cannot be vectors of " 59001e04c3fSmrg "different sizes", ast_expression::operator_string(op)); 59101e04c3fSmrg return glsl_type::error_type; 59201e04c3fSmrg } 59301e04c3fSmrg 59401e04c3fSmrg /* "If one operand is a scalar and the other a vector, the scalar is 59501e04c3fSmrg * applied component-wise to the vector, resulting in the same type as 59601e04c3fSmrg * the vector. The fundamental types of the operands [...] will be the 59701e04c3fSmrg * resulting fundamental type." 59801e04c3fSmrg */ 59901e04c3fSmrg if (type_a->is_scalar()) 60001e04c3fSmrg return type_b; 60101e04c3fSmrg else 60201e04c3fSmrg return type_a; 60301e04c3fSmrg} 60401e04c3fSmrg 60501e04c3fSmrgstatic const struct glsl_type * 60601e04c3fSmrgmodulus_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b, 60701e04c3fSmrg struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 60801e04c3fSmrg{ 60901e04c3fSmrg const glsl_type *type_a = value_a->type; 61001e04c3fSmrg const glsl_type *type_b = value_b->type; 61101e04c3fSmrg 612ed98bd31Smaya if (!state->EXT_gpu_shader4_enable && 613ed98bd31Smaya !state->check_version(130, 300, loc, "operator '%%' is reserved")) { 61401e04c3fSmrg return glsl_type::error_type; 61501e04c3fSmrg } 61601e04c3fSmrg 61701e04c3fSmrg /* Section 5.9 (Expressions) of the GLSL 4.00 specification says: 61801e04c3fSmrg * 61901e04c3fSmrg * "The operator modulus (%) operates on signed or unsigned integers or 62001e04c3fSmrg * integer vectors." 62101e04c3fSmrg */ 62201e04c3fSmrg if (!type_a->is_integer_32_64()) { 62301e04c3fSmrg _mesa_glsl_error(loc, state, "LHS of operator %% must be an integer"); 62401e04c3fSmrg return glsl_type::error_type; 62501e04c3fSmrg } 62601e04c3fSmrg if (!type_b->is_integer_32_64()) { 62701e04c3fSmrg _mesa_glsl_error(loc, state, "RHS of operator %% must be an integer"); 62801e04c3fSmrg return glsl_type::error_type; 62901e04c3fSmrg } 63001e04c3fSmrg 63101e04c3fSmrg /* "If the fundamental types in the operands do not match, then the 63201e04c3fSmrg * conversions from section 4.1.10 "Implicit Conversions" are applied 63301e04c3fSmrg * to create matching types." 63401e04c3fSmrg * 63501e04c3fSmrg * Note that GLSL 4.00 (and GL_ARB_gpu_shader5) introduced implicit 63601e04c3fSmrg * int -> uint conversion rules. Prior to that, there were no implicit 63701e04c3fSmrg * conversions. So it's harmless to apply them universally - no implicit 63801e04c3fSmrg * conversions will exist. If the types don't match, we'll receive false, 63901e04c3fSmrg * and raise an error, satisfying the GLSL 1.50 spec, page 56: 64001e04c3fSmrg * 64101e04c3fSmrg * "The operand types must both be signed or unsigned." 64201e04c3fSmrg */ 64301e04c3fSmrg if (!apply_implicit_conversion(type_a, value_b, state) && 64401e04c3fSmrg !apply_implicit_conversion(type_b, value_a, state)) { 64501e04c3fSmrg _mesa_glsl_error(loc, state, 64601e04c3fSmrg "could not implicitly convert operands to " 64701e04c3fSmrg "modulus (%%) operator"); 64801e04c3fSmrg return glsl_type::error_type; 64901e04c3fSmrg } 65001e04c3fSmrg type_a = value_a->type; 65101e04c3fSmrg type_b = value_b->type; 65201e04c3fSmrg 65301e04c3fSmrg /* "The operands cannot be vectors of differing size. If one operand is 65401e04c3fSmrg * a scalar and the other vector, then the scalar is applied component- 65501e04c3fSmrg * wise to the vector, resulting in the same type as the vector. If both 65601e04c3fSmrg * are vectors of the same size, the result is computed component-wise." 65701e04c3fSmrg */ 65801e04c3fSmrg if (type_a->is_vector()) { 65901e04c3fSmrg if (!type_b->is_vector() 66001e04c3fSmrg || (type_a->vector_elements == type_b->vector_elements)) 66101e04c3fSmrg return type_a; 66201e04c3fSmrg } else 66301e04c3fSmrg return type_b; 66401e04c3fSmrg 66501e04c3fSmrg /* "The operator modulus (%) is not defined for any other data types 66601e04c3fSmrg * (non-integer types)." 66701e04c3fSmrg */ 66801e04c3fSmrg _mesa_glsl_error(loc, state, "type mismatch"); 66901e04c3fSmrg return glsl_type::error_type; 67001e04c3fSmrg} 67101e04c3fSmrg 67201e04c3fSmrg 67301e04c3fSmrgstatic const struct glsl_type * 67401e04c3fSmrgrelational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b, 67501e04c3fSmrg struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 67601e04c3fSmrg{ 67701e04c3fSmrg const glsl_type *type_a = value_a->type; 67801e04c3fSmrg const glsl_type *type_b = value_b->type; 67901e04c3fSmrg 68001e04c3fSmrg /* From GLSL 1.50 spec, page 56: 68101e04c3fSmrg * "The relational operators greater than (>), less than (<), greater 68201e04c3fSmrg * than or equal (>=), and less than or equal (<=) operate only on 68301e04c3fSmrg * scalar integer and scalar floating-point expressions." 68401e04c3fSmrg */ 68501e04c3fSmrg if (!type_a->is_numeric() 68601e04c3fSmrg || !type_b->is_numeric() 68701e04c3fSmrg || !type_a->is_scalar() 68801e04c3fSmrg || !type_b->is_scalar()) { 68901e04c3fSmrg _mesa_glsl_error(loc, state, 69001e04c3fSmrg "operands to relational operators must be scalar and " 69101e04c3fSmrg "numeric"); 69201e04c3fSmrg return glsl_type::error_type; 69301e04c3fSmrg } 69401e04c3fSmrg 69501e04c3fSmrg /* "Either the operands' types must match, or the conversions from 69601e04c3fSmrg * Section 4.1.10 "Implicit Conversions" will be applied to the integer 69701e04c3fSmrg * operand, after which the types must match." 69801e04c3fSmrg */ 69901e04c3fSmrg if (!apply_implicit_conversion(type_a, value_b, state) 70001e04c3fSmrg && !apply_implicit_conversion(type_b, value_a, state)) { 70101e04c3fSmrg _mesa_glsl_error(loc, state, 70201e04c3fSmrg "could not implicitly convert operands to " 70301e04c3fSmrg "relational operator"); 70401e04c3fSmrg return glsl_type::error_type; 70501e04c3fSmrg } 70601e04c3fSmrg type_a = value_a->type; 70701e04c3fSmrg type_b = value_b->type; 70801e04c3fSmrg 70901e04c3fSmrg if (type_a->base_type != type_b->base_type) { 71001e04c3fSmrg _mesa_glsl_error(loc, state, "base type mismatch"); 71101e04c3fSmrg return glsl_type::error_type; 71201e04c3fSmrg } 71301e04c3fSmrg 71401e04c3fSmrg /* "The result is scalar Boolean." 71501e04c3fSmrg */ 71601e04c3fSmrg return glsl_type::bool_type; 71701e04c3fSmrg} 71801e04c3fSmrg 71901e04c3fSmrg/** 72001e04c3fSmrg * \brief Return the result type of a bit-shift operation. 72101e04c3fSmrg * 72201e04c3fSmrg * If the given types to the bit-shift operator are invalid, return 72301e04c3fSmrg * glsl_type::error_type. 72401e04c3fSmrg * 72501e04c3fSmrg * \param type_a Type of LHS of bit-shift op 72601e04c3fSmrg * \param type_b Type of RHS of bit-shift op 72701e04c3fSmrg */ 72801e04c3fSmrgstatic const struct glsl_type * 72901e04c3fSmrgshift_result_type(const struct glsl_type *type_a, 73001e04c3fSmrg const struct glsl_type *type_b, 73101e04c3fSmrg ast_operators op, 73201e04c3fSmrg struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 73301e04c3fSmrg{ 73401e04c3fSmrg if (!state->check_bitwise_operations_allowed(loc)) { 73501e04c3fSmrg return glsl_type::error_type; 73601e04c3fSmrg } 73701e04c3fSmrg 73801e04c3fSmrg /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec: 73901e04c3fSmrg * 74001e04c3fSmrg * "The shift operators (<<) and (>>). For both operators, the operands 74101e04c3fSmrg * must be signed or unsigned integers or integer vectors. One operand 74201e04c3fSmrg * can be signed while the other is unsigned." 74301e04c3fSmrg */ 74401e04c3fSmrg if (!type_a->is_integer_32_64()) { 74501e04c3fSmrg _mesa_glsl_error(loc, state, "LHS of operator %s must be an integer or " 74601e04c3fSmrg "integer vector", ast_expression::operator_string(op)); 74701e04c3fSmrg return glsl_type::error_type; 74801e04c3fSmrg 74901e04c3fSmrg } 7507ec681f3Smrg if (!type_b->is_integer_32()) { 75101e04c3fSmrg _mesa_glsl_error(loc, state, "RHS of operator %s must be an integer or " 75201e04c3fSmrg "integer vector", ast_expression::operator_string(op)); 75301e04c3fSmrg return glsl_type::error_type; 75401e04c3fSmrg } 75501e04c3fSmrg 75601e04c3fSmrg /* "If the first operand is a scalar, the second operand has to be 75701e04c3fSmrg * a scalar as well." 75801e04c3fSmrg */ 75901e04c3fSmrg if (type_a->is_scalar() && !type_b->is_scalar()) { 76001e04c3fSmrg _mesa_glsl_error(loc, state, "if the first operand of %s is scalar, the " 76101e04c3fSmrg "second must be scalar as well", 76201e04c3fSmrg ast_expression::operator_string(op)); 76301e04c3fSmrg return glsl_type::error_type; 76401e04c3fSmrg } 76501e04c3fSmrg 76601e04c3fSmrg /* If both operands are vectors, check that they have same number of 76701e04c3fSmrg * elements. 76801e04c3fSmrg */ 76901e04c3fSmrg if (type_a->is_vector() && 77001e04c3fSmrg type_b->is_vector() && 77101e04c3fSmrg type_a->vector_elements != type_b->vector_elements) { 77201e04c3fSmrg _mesa_glsl_error(loc, state, "vector operands to operator %s must " 77301e04c3fSmrg "have same number of elements", 77401e04c3fSmrg ast_expression::operator_string(op)); 77501e04c3fSmrg return glsl_type::error_type; 77601e04c3fSmrg } 77701e04c3fSmrg 77801e04c3fSmrg /* "In all cases, the resulting type will be the same type as the left 77901e04c3fSmrg * operand." 78001e04c3fSmrg */ 78101e04c3fSmrg return type_a; 78201e04c3fSmrg} 78301e04c3fSmrg 78401e04c3fSmrg/** 78501e04c3fSmrg * Returns the innermost array index expression in an rvalue tree. 78601e04c3fSmrg * This is the largest indexing level -- if an array of blocks, then 78701e04c3fSmrg * it is the block index rather than an indexing expression for an 78801e04c3fSmrg * array-typed member of an array of blocks. 78901e04c3fSmrg */ 79001e04c3fSmrgstatic ir_rvalue * 79101e04c3fSmrgfind_innermost_array_index(ir_rvalue *rv) 79201e04c3fSmrg{ 79301e04c3fSmrg ir_dereference_array *last = NULL; 79401e04c3fSmrg while (rv) { 79501e04c3fSmrg if (rv->as_dereference_array()) { 79601e04c3fSmrg last = rv->as_dereference_array(); 79701e04c3fSmrg rv = last->array; 79801e04c3fSmrg } else if (rv->as_dereference_record()) 79901e04c3fSmrg rv = rv->as_dereference_record()->record; 80001e04c3fSmrg else if (rv->as_swizzle()) 80101e04c3fSmrg rv = rv->as_swizzle()->val; 80201e04c3fSmrg else 80301e04c3fSmrg rv = NULL; 80401e04c3fSmrg } 80501e04c3fSmrg 80601e04c3fSmrg if (last) 80701e04c3fSmrg return last->array_index; 80801e04c3fSmrg 80901e04c3fSmrg return NULL; 81001e04c3fSmrg} 81101e04c3fSmrg 81201e04c3fSmrg/** 81301e04c3fSmrg * Validates that a value can be assigned to a location with a specified type 81401e04c3fSmrg * 81501e04c3fSmrg * Validates that \c rhs can be assigned to some location. If the types are 81601e04c3fSmrg * not an exact match but an automatic conversion is possible, \c rhs will be 81701e04c3fSmrg * converted. 81801e04c3fSmrg * 81901e04c3fSmrg * \return 82001e04c3fSmrg * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type. 82101e04c3fSmrg * Otherwise the actual RHS to be assigned will be returned. This may be 82201e04c3fSmrg * \c rhs, or it may be \c rhs after some type conversion. 82301e04c3fSmrg * 82401e04c3fSmrg * \note 82501e04c3fSmrg * In addition to being used for assignments, this function is used to 82601e04c3fSmrg * type-check return values. 82701e04c3fSmrg */ 82801e04c3fSmrgstatic ir_rvalue * 82901e04c3fSmrgvalidate_assignment(struct _mesa_glsl_parse_state *state, 83001e04c3fSmrg YYLTYPE loc, ir_rvalue *lhs, 83101e04c3fSmrg ir_rvalue *rhs, bool is_initializer) 83201e04c3fSmrg{ 83301e04c3fSmrg /* If there is already some error in the RHS, just return it. Anything 83401e04c3fSmrg * else will lead to an avalanche of error message back to the user. 83501e04c3fSmrg */ 83601e04c3fSmrg if (rhs->type->is_error()) 83701e04c3fSmrg return rhs; 83801e04c3fSmrg 83901e04c3fSmrg /* In the Tessellation Control Shader: 84001e04c3fSmrg * If a per-vertex output variable is used as an l-value, it is an error 84101e04c3fSmrg * if the expression indicating the vertex number is not the identifier 84201e04c3fSmrg * `gl_InvocationID`. 84301e04c3fSmrg */ 84401e04c3fSmrg if (state->stage == MESA_SHADER_TESS_CTRL && !lhs->type->is_error()) { 84501e04c3fSmrg ir_variable *var = lhs->variable_referenced(); 84601e04c3fSmrg if (var && var->data.mode == ir_var_shader_out && !var->data.patch) { 84701e04c3fSmrg ir_rvalue *index = find_innermost_array_index(lhs); 84801e04c3fSmrg ir_variable *index_var = index ? index->variable_referenced() : NULL; 84901e04c3fSmrg if (!index_var || strcmp(index_var->name, "gl_InvocationID") != 0) { 85001e04c3fSmrg _mesa_glsl_error(&loc, state, 85101e04c3fSmrg "Tessellation control shader outputs can only " 85201e04c3fSmrg "be indexed by gl_InvocationID"); 85301e04c3fSmrg return NULL; 85401e04c3fSmrg } 85501e04c3fSmrg } 85601e04c3fSmrg } 85701e04c3fSmrg 85801e04c3fSmrg /* If the types are identical, the assignment can trivially proceed. 85901e04c3fSmrg */ 86001e04c3fSmrg if (rhs->type == lhs->type) 86101e04c3fSmrg return rhs; 86201e04c3fSmrg 86301e04c3fSmrg /* If the array element types are the same and the LHS is unsized, 86401e04c3fSmrg * the assignment is okay for initializers embedded in variable 86501e04c3fSmrg * declarations. 86601e04c3fSmrg * 86701e04c3fSmrg * Note: Whole-array assignments are not permitted in GLSL 1.10, but this 86801e04c3fSmrg * is handled by ir_dereference::is_lvalue. 86901e04c3fSmrg */ 87001e04c3fSmrg const glsl_type *lhs_t = lhs->type; 87101e04c3fSmrg const glsl_type *rhs_t = rhs->type; 87201e04c3fSmrg bool unsized_array = false; 87301e04c3fSmrg while(lhs_t->is_array()) { 87401e04c3fSmrg if (rhs_t == lhs_t) 87501e04c3fSmrg break; /* the rest of the inner arrays match so break out early */ 87601e04c3fSmrg if (!rhs_t->is_array()) { 87701e04c3fSmrg unsized_array = false; 87801e04c3fSmrg break; /* number of dimensions mismatch */ 87901e04c3fSmrg } 88001e04c3fSmrg if (lhs_t->length == rhs_t->length) { 88101e04c3fSmrg lhs_t = lhs_t->fields.array; 88201e04c3fSmrg rhs_t = rhs_t->fields.array; 88301e04c3fSmrg continue; 88401e04c3fSmrg } else if (lhs_t->is_unsized_array()) { 88501e04c3fSmrg unsized_array = true; 88601e04c3fSmrg } else { 88701e04c3fSmrg unsized_array = false; 88801e04c3fSmrg break; /* sized array mismatch */ 88901e04c3fSmrg } 89001e04c3fSmrg lhs_t = lhs_t->fields.array; 89101e04c3fSmrg rhs_t = rhs_t->fields.array; 89201e04c3fSmrg } 89301e04c3fSmrg if (unsized_array) { 89401e04c3fSmrg if (is_initializer) { 89501e04c3fSmrg if (rhs->type->get_scalar_type() == lhs->type->get_scalar_type()) 89601e04c3fSmrg return rhs; 89701e04c3fSmrg } else { 89801e04c3fSmrg _mesa_glsl_error(&loc, state, 89901e04c3fSmrg "implicitly sized arrays cannot be assigned"); 90001e04c3fSmrg return NULL; 90101e04c3fSmrg } 90201e04c3fSmrg } 90301e04c3fSmrg 90401e04c3fSmrg /* Check for implicit conversion in GLSL 1.20 */ 90501e04c3fSmrg if (apply_implicit_conversion(lhs->type, rhs, state)) { 90601e04c3fSmrg if (rhs->type == lhs->type) 90701e04c3fSmrg return rhs; 90801e04c3fSmrg } 90901e04c3fSmrg 91001e04c3fSmrg _mesa_glsl_error(&loc, state, 91101e04c3fSmrg "%s of type %s cannot be assigned to " 91201e04c3fSmrg "variable of type %s", 91301e04c3fSmrg is_initializer ? "initializer" : "value", 91401e04c3fSmrg rhs->type->name, lhs->type->name); 91501e04c3fSmrg 91601e04c3fSmrg return NULL; 91701e04c3fSmrg} 91801e04c3fSmrg 91901e04c3fSmrgstatic void 92001e04c3fSmrgmark_whole_array_access(ir_rvalue *access) 92101e04c3fSmrg{ 92201e04c3fSmrg ir_dereference_variable *deref = access->as_dereference_variable(); 92301e04c3fSmrg 92401e04c3fSmrg if (deref && deref->var) { 92501e04c3fSmrg deref->var->data.max_array_access = deref->type->length - 1; 92601e04c3fSmrg } 92701e04c3fSmrg} 92801e04c3fSmrg 92901e04c3fSmrgstatic bool 93001e04c3fSmrgdo_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state, 93101e04c3fSmrg const char *non_lvalue_description, 93201e04c3fSmrg ir_rvalue *lhs, ir_rvalue *rhs, 93301e04c3fSmrg ir_rvalue **out_rvalue, bool needs_rvalue, 93401e04c3fSmrg bool is_initializer, 93501e04c3fSmrg YYLTYPE lhs_loc) 93601e04c3fSmrg{ 93701e04c3fSmrg void *ctx = state; 93801e04c3fSmrg bool error_emitted = (lhs->type->is_error() || rhs->type->is_error()); 93901e04c3fSmrg 94001e04c3fSmrg ir_variable *lhs_var = lhs->variable_referenced(); 94101e04c3fSmrg if (lhs_var) 94201e04c3fSmrg lhs_var->data.assigned = true; 94301e04c3fSmrg 9447ec681f3Smrg bool omit_assignment = false; 94501e04c3fSmrg if (!error_emitted) { 94601e04c3fSmrg if (non_lvalue_description != NULL) { 94701e04c3fSmrg _mesa_glsl_error(&lhs_loc, state, 94801e04c3fSmrg "assignment to %s", 94901e04c3fSmrg non_lvalue_description); 95001e04c3fSmrg error_emitted = true; 95101e04c3fSmrg } else if (lhs_var != NULL && (lhs_var->data.read_only || 95201e04c3fSmrg (lhs_var->data.mode == ir_var_shader_storage && 95301e04c3fSmrg lhs_var->data.memory_read_only))) { 95401e04c3fSmrg /* We can have memory_read_only set on both images and buffer variables, 95501e04c3fSmrg * but in the former there is a distinction between assignments to 95601e04c3fSmrg * the variable itself (read_only) and to the memory they point to 95701e04c3fSmrg * (memory_read_only), while in the case of buffer variables there is 95801e04c3fSmrg * no such distinction, that is why this check here is limited to 95901e04c3fSmrg * buffer variables alone. 96001e04c3fSmrg */ 9617ec681f3Smrg 9627ec681f3Smrg if (state->ignore_write_to_readonly_var) 9637ec681f3Smrg omit_assignment = true; 9647ec681f3Smrg else { 9657ec681f3Smrg _mesa_glsl_error(&lhs_loc, state, 9667ec681f3Smrg "assignment to read-only variable '%s'", 9677ec681f3Smrg lhs_var->name); 9687ec681f3Smrg error_emitted = true; 9697ec681f3Smrg } 97001e04c3fSmrg } else if (lhs->type->is_array() && 9717ec681f3Smrg !state->check_version(state->allow_glsl_120_subset_in_110 ? 110 : 120, 9727ec681f3Smrg 300, &lhs_loc, 97301e04c3fSmrg "whole array assignment forbidden")) { 97401e04c3fSmrg /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec: 97501e04c3fSmrg * 97601e04c3fSmrg * "Other binary or unary expressions, non-dereferenced 97701e04c3fSmrg * arrays, function names, swizzles with repeated fields, 97801e04c3fSmrg * and constants cannot be l-values." 97901e04c3fSmrg * 98001e04c3fSmrg * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00. 98101e04c3fSmrg */ 98201e04c3fSmrg error_emitted = true; 98301e04c3fSmrg } else if (!lhs->is_lvalue(state)) { 98401e04c3fSmrg _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment"); 98501e04c3fSmrg error_emitted = true; 98601e04c3fSmrg } 98701e04c3fSmrg } 98801e04c3fSmrg 98901e04c3fSmrg ir_rvalue *new_rhs = 99001e04c3fSmrg validate_assignment(state, lhs_loc, lhs, rhs, is_initializer); 99101e04c3fSmrg if (new_rhs != NULL) { 99201e04c3fSmrg rhs = new_rhs; 99301e04c3fSmrg 99401e04c3fSmrg /* If the LHS array was not declared with a size, it takes it size from 99501e04c3fSmrg * the RHS. If the LHS is an l-value and a whole array, it must be a 99601e04c3fSmrg * dereference of a variable. Any other case would require that the LHS 99701e04c3fSmrg * is either not an l-value or not a whole array. 99801e04c3fSmrg */ 99901e04c3fSmrg if (lhs->type->is_unsized_array()) { 100001e04c3fSmrg ir_dereference *const d = lhs->as_dereference(); 100101e04c3fSmrg 100201e04c3fSmrg assert(d != NULL); 100301e04c3fSmrg 100401e04c3fSmrg ir_variable *const var = d->variable_referenced(); 100501e04c3fSmrg 100601e04c3fSmrg assert(var != NULL); 100701e04c3fSmrg 100801e04c3fSmrg if (var->data.max_array_access >= rhs->type->array_size()) { 100901e04c3fSmrg /* FINISHME: This should actually log the location of the RHS. */ 101001e04c3fSmrg _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to " 101101e04c3fSmrg "previous access", 101201e04c3fSmrg var->data.max_array_access); 101301e04c3fSmrg } 101401e04c3fSmrg 101501e04c3fSmrg var->type = glsl_type::get_array_instance(lhs->type->fields.array, 101601e04c3fSmrg rhs->type->array_size()); 101701e04c3fSmrg d->type = var->type; 101801e04c3fSmrg } 101901e04c3fSmrg if (lhs->type->is_array()) { 102001e04c3fSmrg mark_whole_array_access(rhs); 102101e04c3fSmrg mark_whole_array_access(lhs); 102201e04c3fSmrg } 102301e04c3fSmrg } else { 102401e04c3fSmrg error_emitted = true; 102501e04c3fSmrg } 102601e04c3fSmrg 10277ec681f3Smrg if (omit_assignment) { 10287ec681f3Smrg *out_rvalue = needs_rvalue ? ir_rvalue::error_value(ctx) : NULL; 10297ec681f3Smrg return error_emitted; 10307ec681f3Smrg } 10317ec681f3Smrg 103201e04c3fSmrg /* Most callers of do_assignment (assign, add_assign, pre_inc/dec, 103301e04c3fSmrg * but not post_inc) need the converted assigned value as an rvalue 103401e04c3fSmrg * to handle things like: 103501e04c3fSmrg * 103601e04c3fSmrg * i = j += 1; 103701e04c3fSmrg */ 103801e04c3fSmrg if (needs_rvalue) { 103901e04c3fSmrg ir_rvalue *rvalue; 104001e04c3fSmrg if (!error_emitted) { 104101e04c3fSmrg ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp", 104201e04c3fSmrg ir_var_temporary); 104301e04c3fSmrg instructions->push_tail(var); 104401e04c3fSmrg instructions->push_tail(assign(var, rhs)); 104501e04c3fSmrg 104601e04c3fSmrg ir_dereference_variable *deref_var = 104701e04c3fSmrg new(ctx) ir_dereference_variable(var); 104801e04c3fSmrg instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var)); 104901e04c3fSmrg rvalue = new(ctx) ir_dereference_variable(var); 105001e04c3fSmrg } else { 105101e04c3fSmrg rvalue = ir_rvalue::error_value(ctx); 105201e04c3fSmrg } 105301e04c3fSmrg *out_rvalue = rvalue; 105401e04c3fSmrg } else { 105501e04c3fSmrg if (!error_emitted) 105601e04c3fSmrg instructions->push_tail(new(ctx) ir_assignment(lhs, rhs)); 105701e04c3fSmrg *out_rvalue = NULL; 105801e04c3fSmrg } 105901e04c3fSmrg 106001e04c3fSmrg return error_emitted; 106101e04c3fSmrg} 106201e04c3fSmrg 106301e04c3fSmrgstatic ir_rvalue * 106401e04c3fSmrgget_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue) 106501e04c3fSmrg{ 106601e04c3fSmrg void *ctx = ralloc_parent(lvalue); 106701e04c3fSmrg ir_variable *var; 106801e04c3fSmrg 106901e04c3fSmrg var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp", 107001e04c3fSmrg ir_var_temporary); 107101e04c3fSmrg instructions->push_tail(var); 107201e04c3fSmrg 107301e04c3fSmrg instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var), 107401e04c3fSmrg lvalue)); 107501e04c3fSmrg 107601e04c3fSmrg return new(ctx) ir_dereference_variable(var); 107701e04c3fSmrg} 107801e04c3fSmrg 107901e04c3fSmrg 108001e04c3fSmrgir_rvalue * 108101e04c3fSmrgast_node::hir(exec_list *instructions, struct _mesa_glsl_parse_state *state) 108201e04c3fSmrg{ 108301e04c3fSmrg (void) instructions; 108401e04c3fSmrg (void) state; 108501e04c3fSmrg 108601e04c3fSmrg return NULL; 108701e04c3fSmrg} 108801e04c3fSmrg 108901e04c3fSmrgbool 109001e04c3fSmrgast_node::has_sequence_subexpression() const 109101e04c3fSmrg{ 109201e04c3fSmrg return false; 109301e04c3fSmrg} 109401e04c3fSmrg 109501e04c3fSmrgvoid 109601e04c3fSmrgast_node::set_is_lhs(bool /* new_value */) 109701e04c3fSmrg{ 109801e04c3fSmrg} 109901e04c3fSmrg 110001e04c3fSmrgvoid 110101e04c3fSmrgast_function_expression::hir_no_rvalue(exec_list *instructions, 110201e04c3fSmrg struct _mesa_glsl_parse_state *state) 110301e04c3fSmrg{ 110401e04c3fSmrg (void)hir(instructions, state); 110501e04c3fSmrg} 110601e04c3fSmrg 110701e04c3fSmrgvoid 110801e04c3fSmrgast_aggregate_initializer::hir_no_rvalue(exec_list *instructions, 110901e04c3fSmrg struct _mesa_glsl_parse_state *state) 111001e04c3fSmrg{ 111101e04c3fSmrg (void)hir(instructions, state); 111201e04c3fSmrg} 111301e04c3fSmrg 111401e04c3fSmrgstatic ir_rvalue * 111501e04c3fSmrgdo_comparison(void *mem_ctx, int operation, ir_rvalue *op0, ir_rvalue *op1) 111601e04c3fSmrg{ 111701e04c3fSmrg int join_op; 111801e04c3fSmrg ir_rvalue *cmp = NULL; 111901e04c3fSmrg 112001e04c3fSmrg if (operation == ir_binop_all_equal) 112101e04c3fSmrg join_op = ir_binop_logic_and; 112201e04c3fSmrg else 112301e04c3fSmrg join_op = ir_binop_logic_or; 112401e04c3fSmrg 112501e04c3fSmrg switch (op0->type->base_type) { 112601e04c3fSmrg case GLSL_TYPE_FLOAT: 112701e04c3fSmrg case GLSL_TYPE_FLOAT16: 112801e04c3fSmrg case GLSL_TYPE_UINT: 112901e04c3fSmrg case GLSL_TYPE_INT: 113001e04c3fSmrg case GLSL_TYPE_BOOL: 113101e04c3fSmrg case GLSL_TYPE_DOUBLE: 113201e04c3fSmrg case GLSL_TYPE_UINT64: 113301e04c3fSmrg case GLSL_TYPE_INT64: 113401e04c3fSmrg case GLSL_TYPE_UINT16: 113501e04c3fSmrg case GLSL_TYPE_INT16: 113601e04c3fSmrg case GLSL_TYPE_UINT8: 113701e04c3fSmrg case GLSL_TYPE_INT8: 113801e04c3fSmrg return new(mem_ctx) ir_expression(operation, op0, op1); 113901e04c3fSmrg 114001e04c3fSmrg case GLSL_TYPE_ARRAY: { 114101e04c3fSmrg for (unsigned int i = 0; i < op0->type->length; i++) { 114201e04c3fSmrg ir_rvalue *e0, *e1, *result; 114301e04c3fSmrg 114401e04c3fSmrg e0 = new(mem_ctx) ir_dereference_array(op0->clone(mem_ctx, NULL), 114501e04c3fSmrg new(mem_ctx) ir_constant(i)); 114601e04c3fSmrg e1 = new(mem_ctx) ir_dereference_array(op1->clone(mem_ctx, NULL), 114701e04c3fSmrg new(mem_ctx) ir_constant(i)); 114801e04c3fSmrg result = do_comparison(mem_ctx, operation, e0, e1); 114901e04c3fSmrg 115001e04c3fSmrg if (cmp) { 115101e04c3fSmrg cmp = new(mem_ctx) ir_expression(join_op, cmp, result); 115201e04c3fSmrg } else { 115301e04c3fSmrg cmp = result; 115401e04c3fSmrg } 115501e04c3fSmrg } 115601e04c3fSmrg 115701e04c3fSmrg mark_whole_array_access(op0); 115801e04c3fSmrg mark_whole_array_access(op1); 115901e04c3fSmrg break; 116001e04c3fSmrg } 116101e04c3fSmrg 116201e04c3fSmrg case GLSL_TYPE_STRUCT: { 116301e04c3fSmrg for (unsigned int i = 0; i < op0->type->length; i++) { 116401e04c3fSmrg ir_rvalue *e0, *e1, *result; 116501e04c3fSmrg const char *field_name = op0->type->fields.structure[i].name; 116601e04c3fSmrg 116701e04c3fSmrg e0 = new(mem_ctx) ir_dereference_record(op0->clone(mem_ctx, NULL), 116801e04c3fSmrg field_name); 116901e04c3fSmrg e1 = new(mem_ctx) ir_dereference_record(op1->clone(mem_ctx, NULL), 117001e04c3fSmrg field_name); 117101e04c3fSmrg result = do_comparison(mem_ctx, operation, e0, e1); 117201e04c3fSmrg 117301e04c3fSmrg if (cmp) { 117401e04c3fSmrg cmp = new(mem_ctx) ir_expression(join_op, cmp, result); 117501e04c3fSmrg } else { 117601e04c3fSmrg cmp = result; 117701e04c3fSmrg } 117801e04c3fSmrg } 117901e04c3fSmrg break; 118001e04c3fSmrg } 118101e04c3fSmrg 118201e04c3fSmrg case GLSL_TYPE_ERROR: 118301e04c3fSmrg case GLSL_TYPE_VOID: 118401e04c3fSmrg case GLSL_TYPE_SAMPLER: 118501e04c3fSmrg case GLSL_TYPE_IMAGE: 118601e04c3fSmrg case GLSL_TYPE_INTERFACE: 118701e04c3fSmrg case GLSL_TYPE_ATOMIC_UINT: 118801e04c3fSmrg case GLSL_TYPE_SUBROUTINE: 118901e04c3fSmrg case GLSL_TYPE_FUNCTION: 119001e04c3fSmrg /* I assume a comparison of a struct containing a sampler just 119101e04c3fSmrg * ignores the sampler present in the type. 119201e04c3fSmrg */ 119301e04c3fSmrg break; 119401e04c3fSmrg } 119501e04c3fSmrg 119601e04c3fSmrg if (cmp == NULL) 119701e04c3fSmrg cmp = new(mem_ctx) ir_constant(true); 119801e04c3fSmrg 119901e04c3fSmrg return cmp; 120001e04c3fSmrg} 120101e04c3fSmrg 120201e04c3fSmrg/* For logical operations, we want to ensure that the operands are 120301e04c3fSmrg * scalar booleans. If it isn't, emit an error and return a constant 120401e04c3fSmrg * boolean to avoid triggering cascading error messages. 120501e04c3fSmrg */ 120601e04c3fSmrgstatic ir_rvalue * 120701e04c3fSmrgget_scalar_boolean_operand(exec_list *instructions, 120801e04c3fSmrg struct _mesa_glsl_parse_state *state, 120901e04c3fSmrg ast_expression *parent_expr, 121001e04c3fSmrg int operand, 121101e04c3fSmrg const char *operand_name, 121201e04c3fSmrg bool *error_emitted) 121301e04c3fSmrg{ 121401e04c3fSmrg ast_expression *expr = parent_expr->subexpressions[operand]; 121501e04c3fSmrg void *ctx = state; 121601e04c3fSmrg ir_rvalue *val = expr->hir(instructions, state); 121701e04c3fSmrg 121801e04c3fSmrg if (val->type->is_boolean() && val->type->is_scalar()) 121901e04c3fSmrg return val; 122001e04c3fSmrg 122101e04c3fSmrg if (!*error_emitted) { 122201e04c3fSmrg YYLTYPE loc = expr->get_location(); 122301e04c3fSmrg _mesa_glsl_error(&loc, state, "%s of `%s' must be scalar boolean", 122401e04c3fSmrg operand_name, 122501e04c3fSmrg parent_expr->operator_string(parent_expr->oper)); 122601e04c3fSmrg *error_emitted = true; 122701e04c3fSmrg } 122801e04c3fSmrg 122901e04c3fSmrg return new(ctx) ir_constant(true); 123001e04c3fSmrg} 123101e04c3fSmrg 123201e04c3fSmrg/** 123301e04c3fSmrg * If name refers to a builtin array whose maximum allowed size is less than 123401e04c3fSmrg * size, report an error and return true. Otherwise return false. 123501e04c3fSmrg */ 123601e04c3fSmrgvoid 123701e04c3fSmrgcheck_builtin_array_max_size(const char *name, unsigned size, 123801e04c3fSmrg YYLTYPE loc, struct _mesa_glsl_parse_state *state) 123901e04c3fSmrg{ 124001e04c3fSmrg if ((strcmp("gl_TexCoord", name) == 0) 124101e04c3fSmrg && (size > state->Const.MaxTextureCoords)) { 124201e04c3fSmrg /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec: 124301e04c3fSmrg * 124401e04c3fSmrg * "The size [of gl_TexCoord] can be at most 124501e04c3fSmrg * gl_MaxTextureCoords." 124601e04c3fSmrg */ 124701e04c3fSmrg _mesa_glsl_error(&loc, state, "`gl_TexCoord' array size cannot " 124801e04c3fSmrg "be larger than gl_MaxTextureCoords (%u)", 124901e04c3fSmrg state->Const.MaxTextureCoords); 125001e04c3fSmrg } else if (strcmp("gl_ClipDistance", name) == 0) { 125101e04c3fSmrg state->clip_dist_size = size; 125201e04c3fSmrg if (size + state->cull_dist_size > state->Const.MaxClipPlanes) { 125301e04c3fSmrg /* From section 7.1 (Vertex Shader Special Variables) of the 125401e04c3fSmrg * GLSL 1.30 spec: 125501e04c3fSmrg * 125601e04c3fSmrg * "The gl_ClipDistance array is predeclared as unsized and 125701e04c3fSmrg * must be sized by the shader either redeclaring it with a 125801e04c3fSmrg * size or indexing it only with integral constant 125901e04c3fSmrg * expressions. ... The size can be at most 126001e04c3fSmrg * gl_MaxClipDistances." 126101e04c3fSmrg */ 126201e04c3fSmrg _mesa_glsl_error(&loc, state, "`gl_ClipDistance' array size cannot " 126301e04c3fSmrg "be larger than gl_MaxClipDistances (%u)", 126401e04c3fSmrg state->Const.MaxClipPlanes); 126501e04c3fSmrg } 126601e04c3fSmrg } else if (strcmp("gl_CullDistance", name) == 0) { 126701e04c3fSmrg state->cull_dist_size = size; 126801e04c3fSmrg if (size + state->clip_dist_size > state->Const.MaxClipPlanes) { 126901e04c3fSmrg /* From the ARB_cull_distance spec: 127001e04c3fSmrg * 127101e04c3fSmrg * "The gl_CullDistance array is predeclared as unsized and 127201e04c3fSmrg * must be sized by the shader either redeclaring it with 127301e04c3fSmrg * a size or indexing it only with integral constant 127401e04c3fSmrg * expressions. The size determines the number and set of 127501e04c3fSmrg * enabled cull distances and can be at most 127601e04c3fSmrg * gl_MaxCullDistances." 127701e04c3fSmrg */ 127801e04c3fSmrg _mesa_glsl_error(&loc, state, "`gl_CullDistance' array size cannot " 127901e04c3fSmrg "be larger than gl_MaxCullDistances (%u)", 128001e04c3fSmrg state->Const.MaxClipPlanes); 128101e04c3fSmrg } 128201e04c3fSmrg } 128301e04c3fSmrg} 128401e04c3fSmrg 128501e04c3fSmrg/** 128601e04c3fSmrg * Create the constant 1, of a which is appropriate for incrementing and 128701e04c3fSmrg * decrementing values of the given GLSL type. For example, if type is vec4, 128801e04c3fSmrg * this creates a constant value of 1.0 having type float. 128901e04c3fSmrg * 129001e04c3fSmrg * If the given type is invalid for increment and decrement operators, return 129101e04c3fSmrg * a floating point 1--the error will be detected later. 129201e04c3fSmrg */ 129301e04c3fSmrgstatic ir_rvalue * 129401e04c3fSmrgconstant_one_for_inc_dec(void *ctx, const glsl_type *type) 129501e04c3fSmrg{ 129601e04c3fSmrg switch (type->base_type) { 129701e04c3fSmrg case GLSL_TYPE_UINT: 129801e04c3fSmrg return new(ctx) ir_constant((unsigned) 1); 129901e04c3fSmrg case GLSL_TYPE_INT: 130001e04c3fSmrg return new(ctx) ir_constant(1); 130101e04c3fSmrg case GLSL_TYPE_UINT64: 130201e04c3fSmrg return new(ctx) ir_constant((uint64_t) 1); 130301e04c3fSmrg case GLSL_TYPE_INT64: 130401e04c3fSmrg return new(ctx) ir_constant((int64_t) 1); 130501e04c3fSmrg default: 130601e04c3fSmrg case GLSL_TYPE_FLOAT: 130701e04c3fSmrg return new(ctx) ir_constant(1.0f); 130801e04c3fSmrg } 130901e04c3fSmrg} 131001e04c3fSmrg 131101e04c3fSmrgir_rvalue * 131201e04c3fSmrgast_expression::hir(exec_list *instructions, 131301e04c3fSmrg struct _mesa_glsl_parse_state *state) 131401e04c3fSmrg{ 131501e04c3fSmrg return do_hir(instructions, state, true); 131601e04c3fSmrg} 131701e04c3fSmrg 131801e04c3fSmrgvoid 131901e04c3fSmrgast_expression::hir_no_rvalue(exec_list *instructions, 132001e04c3fSmrg struct _mesa_glsl_parse_state *state) 132101e04c3fSmrg{ 132201e04c3fSmrg do_hir(instructions, state, false); 132301e04c3fSmrg} 132401e04c3fSmrg 132501e04c3fSmrgvoid 132601e04c3fSmrgast_expression::set_is_lhs(bool new_value) 132701e04c3fSmrg{ 132801e04c3fSmrg /* is_lhs is tracked only to print "variable used uninitialized" warnings, 132901e04c3fSmrg * if we lack an identifier we can just skip it. 133001e04c3fSmrg */ 133101e04c3fSmrg if (this->primary_expression.identifier == NULL) 133201e04c3fSmrg return; 133301e04c3fSmrg 133401e04c3fSmrg this->is_lhs = new_value; 133501e04c3fSmrg 133601e04c3fSmrg /* We need to go through the subexpressions tree to cover cases like 133701e04c3fSmrg * ast_field_selection 133801e04c3fSmrg */ 133901e04c3fSmrg if (this->subexpressions[0] != NULL) 134001e04c3fSmrg this->subexpressions[0]->set_is_lhs(new_value); 134101e04c3fSmrg} 134201e04c3fSmrg 134301e04c3fSmrgir_rvalue * 134401e04c3fSmrgast_expression::do_hir(exec_list *instructions, 134501e04c3fSmrg struct _mesa_glsl_parse_state *state, 134601e04c3fSmrg bool needs_rvalue) 134701e04c3fSmrg{ 134801e04c3fSmrg void *ctx = state; 134901e04c3fSmrg static const int operations[AST_NUM_OPERATORS] = { 135001e04c3fSmrg -1, /* ast_assign doesn't convert to ir_expression. */ 135101e04c3fSmrg -1, /* ast_plus doesn't convert to ir_expression. */ 135201e04c3fSmrg ir_unop_neg, 135301e04c3fSmrg ir_binop_add, 135401e04c3fSmrg ir_binop_sub, 135501e04c3fSmrg ir_binop_mul, 135601e04c3fSmrg ir_binop_div, 135701e04c3fSmrg ir_binop_mod, 135801e04c3fSmrg ir_binop_lshift, 135901e04c3fSmrg ir_binop_rshift, 136001e04c3fSmrg ir_binop_less, 136101e04c3fSmrg ir_binop_less, /* This is correct. See the ast_greater case below. */ 136201e04c3fSmrg ir_binop_gequal, /* This is correct. See the ast_lequal case below. */ 136301e04c3fSmrg ir_binop_gequal, 136401e04c3fSmrg ir_binop_all_equal, 136501e04c3fSmrg ir_binop_any_nequal, 136601e04c3fSmrg ir_binop_bit_and, 136701e04c3fSmrg ir_binop_bit_xor, 136801e04c3fSmrg ir_binop_bit_or, 136901e04c3fSmrg ir_unop_bit_not, 137001e04c3fSmrg ir_binop_logic_and, 137101e04c3fSmrg ir_binop_logic_xor, 137201e04c3fSmrg ir_binop_logic_or, 137301e04c3fSmrg ir_unop_logic_not, 137401e04c3fSmrg 137501e04c3fSmrg /* Note: The following block of expression types actually convert 137601e04c3fSmrg * to multiple IR instructions. 137701e04c3fSmrg */ 137801e04c3fSmrg ir_binop_mul, /* ast_mul_assign */ 137901e04c3fSmrg ir_binop_div, /* ast_div_assign */ 138001e04c3fSmrg ir_binop_mod, /* ast_mod_assign */ 138101e04c3fSmrg ir_binop_add, /* ast_add_assign */ 138201e04c3fSmrg ir_binop_sub, /* ast_sub_assign */ 138301e04c3fSmrg ir_binop_lshift, /* ast_ls_assign */ 138401e04c3fSmrg ir_binop_rshift, /* ast_rs_assign */ 138501e04c3fSmrg ir_binop_bit_and, /* ast_and_assign */ 138601e04c3fSmrg ir_binop_bit_xor, /* ast_xor_assign */ 138701e04c3fSmrg ir_binop_bit_or, /* ast_or_assign */ 138801e04c3fSmrg 138901e04c3fSmrg -1, /* ast_conditional doesn't convert to ir_expression. */ 139001e04c3fSmrg ir_binop_add, /* ast_pre_inc. */ 139101e04c3fSmrg ir_binop_sub, /* ast_pre_dec. */ 139201e04c3fSmrg ir_binop_add, /* ast_post_inc. */ 139301e04c3fSmrg ir_binop_sub, /* ast_post_dec. */ 139401e04c3fSmrg -1, /* ast_field_selection doesn't conv to ir_expression. */ 139501e04c3fSmrg -1, /* ast_array_index doesn't convert to ir_expression. */ 139601e04c3fSmrg -1, /* ast_function_call doesn't conv to ir_expression. */ 139701e04c3fSmrg -1, /* ast_identifier doesn't convert to ir_expression. */ 139801e04c3fSmrg -1, /* ast_int_constant doesn't convert to ir_expression. */ 139901e04c3fSmrg -1, /* ast_uint_constant doesn't conv to ir_expression. */ 140001e04c3fSmrg -1, /* ast_float_constant doesn't conv to ir_expression. */ 140101e04c3fSmrg -1, /* ast_bool_constant doesn't conv to ir_expression. */ 140201e04c3fSmrg -1, /* ast_sequence doesn't convert to ir_expression. */ 140301e04c3fSmrg -1, /* ast_aggregate shouldn't ever even get here. */ 140401e04c3fSmrg }; 140501e04c3fSmrg ir_rvalue *result = NULL; 140601e04c3fSmrg ir_rvalue *op[3]; 140701e04c3fSmrg const struct glsl_type *type, *orig_type; 140801e04c3fSmrg bool error_emitted = false; 140901e04c3fSmrg YYLTYPE loc; 141001e04c3fSmrg 141101e04c3fSmrg loc = this->get_location(); 141201e04c3fSmrg 141301e04c3fSmrg switch (this->oper) { 141401e04c3fSmrg case ast_aggregate: 141501e04c3fSmrg unreachable("ast_aggregate: Should never get here."); 141601e04c3fSmrg 141701e04c3fSmrg case ast_assign: { 141801e04c3fSmrg this->subexpressions[0]->set_is_lhs(true); 141901e04c3fSmrg op[0] = this->subexpressions[0]->hir(instructions, state); 142001e04c3fSmrg op[1] = this->subexpressions[1]->hir(instructions, state); 142101e04c3fSmrg 142201e04c3fSmrg error_emitted = 142301e04c3fSmrg do_assignment(instructions, state, 142401e04c3fSmrg this->subexpressions[0]->non_lvalue_description, 142501e04c3fSmrg op[0], op[1], &result, needs_rvalue, false, 142601e04c3fSmrg this->subexpressions[0]->get_location()); 142701e04c3fSmrg break; 142801e04c3fSmrg } 142901e04c3fSmrg 143001e04c3fSmrg case ast_plus: 143101e04c3fSmrg op[0] = this->subexpressions[0]->hir(instructions, state); 143201e04c3fSmrg 143301e04c3fSmrg type = unary_arithmetic_result_type(op[0]->type, state, & loc); 143401e04c3fSmrg 143501e04c3fSmrg error_emitted = type->is_error(); 143601e04c3fSmrg 143701e04c3fSmrg result = op[0]; 143801e04c3fSmrg break; 143901e04c3fSmrg 144001e04c3fSmrg case ast_neg: 144101e04c3fSmrg op[0] = this->subexpressions[0]->hir(instructions, state); 144201e04c3fSmrg 144301e04c3fSmrg type = unary_arithmetic_result_type(op[0]->type, state, & loc); 144401e04c3fSmrg 144501e04c3fSmrg error_emitted = type->is_error(); 144601e04c3fSmrg 144701e04c3fSmrg result = new(ctx) ir_expression(operations[this->oper], type, 144801e04c3fSmrg op[0], NULL); 144901e04c3fSmrg break; 145001e04c3fSmrg 145101e04c3fSmrg case ast_add: 145201e04c3fSmrg case ast_sub: 145301e04c3fSmrg case ast_mul: 145401e04c3fSmrg case ast_div: 145501e04c3fSmrg op[0] = this->subexpressions[0]->hir(instructions, state); 145601e04c3fSmrg op[1] = this->subexpressions[1]->hir(instructions, state); 145701e04c3fSmrg 145801e04c3fSmrg type = arithmetic_result_type(op[0], op[1], 145901e04c3fSmrg (this->oper == ast_mul), 146001e04c3fSmrg state, & loc); 146101e04c3fSmrg error_emitted = type->is_error(); 146201e04c3fSmrg 146301e04c3fSmrg result = new(ctx) ir_expression(operations[this->oper], type, 146401e04c3fSmrg op[0], op[1]); 146501e04c3fSmrg break; 146601e04c3fSmrg 146701e04c3fSmrg case ast_mod: 146801e04c3fSmrg op[0] = this->subexpressions[0]->hir(instructions, state); 146901e04c3fSmrg op[1] = this->subexpressions[1]->hir(instructions, state); 147001e04c3fSmrg 147101e04c3fSmrg type = modulus_result_type(op[0], op[1], state, &loc); 147201e04c3fSmrg 147301e04c3fSmrg assert(operations[this->oper] == ir_binop_mod); 147401e04c3fSmrg 147501e04c3fSmrg result = new(ctx) ir_expression(operations[this->oper], type, 147601e04c3fSmrg op[0], op[1]); 147701e04c3fSmrg error_emitted = type->is_error(); 147801e04c3fSmrg break; 147901e04c3fSmrg 148001e04c3fSmrg case ast_lshift: 148101e04c3fSmrg case ast_rshift: 148201e04c3fSmrg if (!state->check_bitwise_operations_allowed(&loc)) { 148301e04c3fSmrg error_emitted = true; 148401e04c3fSmrg } 148501e04c3fSmrg 148601e04c3fSmrg op[0] = this->subexpressions[0]->hir(instructions, state); 148701e04c3fSmrg op[1] = this->subexpressions[1]->hir(instructions, state); 148801e04c3fSmrg type = shift_result_type(op[0]->type, op[1]->type, this->oper, state, 148901e04c3fSmrg &loc); 149001e04c3fSmrg result = new(ctx) ir_expression(operations[this->oper], type, 149101e04c3fSmrg op[0], op[1]); 149201e04c3fSmrg error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); 149301e04c3fSmrg break; 149401e04c3fSmrg 149501e04c3fSmrg case ast_less: 149601e04c3fSmrg case ast_greater: 149701e04c3fSmrg case ast_lequal: 149801e04c3fSmrg case ast_gequal: 149901e04c3fSmrg op[0] = this->subexpressions[0]->hir(instructions, state); 150001e04c3fSmrg op[1] = this->subexpressions[1]->hir(instructions, state); 150101e04c3fSmrg 150201e04c3fSmrg type = relational_result_type(op[0], op[1], state, & loc); 150301e04c3fSmrg 150401e04c3fSmrg /* The relational operators must either generate an error or result 150501e04c3fSmrg * in a scalar boolean. See page 57 of the GLSL 1.50 spec. 150601e04c3fSmrg */ 150701e04c3fSmrg assert(type->is_error() 150801e04c3fSmrg || (type->is_boolean() && type->is_scalar())); 150901e04c3fSmrg 151001e04c3fSmrg /* Like NIR, GLSL IR does not have opcodes for > or <=. Instead, swap 151101e04c3fSmrg * the arguments and use < or >=. 151201e04c3fSmrg */ 151301e04c3fSmrg if (this->oper == ast_greater || this->oper == ast_lequal) { 151401e04c3fSmrg ir_rvalue *const tmp = op[0]; 151501e04c3fSmrg op[0] = op[1]; 151601e04c3fSmrg op[1] = tmp; 151701e04c3fSmrg } 151801e04c3fSmrg 151901e04c3fSmrg result = new(ctx) ir_expression(operations[this->oper], type, 152001e04c3fSmrg op[0], op[1]); 152101e04c3fSmrg error_emitted = type->is_error(); 152201e04c3fSmrg break; 152301e04c3fSmrg 152401e04c3fSmrg case ast_nequal: 152501e04c3fSmrg case ast_equal: 152601e04c3fSmrg op[0] = this->subexpressions[0]->hir(instructions, state); 152701e04c3fSmrg op[1] = this->subexpressions[1]->hir(instructions, state); 152801e04c3fSmrg 152901e04c3fSmrg /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec: 153001e04c3fSmrg * 153101e04c3fSmrg * "The equality operators equal (==), and not equal (!=) 153201e04c3fSmrg * operate on all types. They result in a scalar Boolean. If 153301e04c3fSmrg * the operand types do not match, then there must be a 153401e04c3fSmrg * conversion from Section 4.1.10 "Implicit Conversions" 153501e04c3fSmrg * applied to one operand that can make them match, in which 153601e04c3fSmrg * case this conversion is done." 153701e04c3fSmrg */ 153801e04c3fSmrg 153901e04c3fSmrg if (op[0]->type == glsl_type::void_type || op[1]->type == glsl_type::void_type) { 154001e04c3fSmrg _mesa_glsl_error(& loc, state, "`%s': wrong operand types: " 154101e04c3fSmrg "no operation `%1$s' exists that takes a left-hand " 154201e04c3fSmrg "operand of type 'void' or a right operand of type " 154301e04c3fSmrg "'void'", (this->oper == ast_equal) ? "==" : "!="); 154401e04c3fSmrg error_emitted = true; 154501e04c3fSmrg } else if ((!apply_implicit_conversion(op[0]->type, op[1], state) 154601e04c3fSmrg && !apply_implicit_conversion(op[1]->type, op[0], state)) 154701e04c3fSmrg || (op[0]->type != op[1]->type)) { 154801e04c3fSmrg _mesa_glsl_error(& loc, state, "operands of `%s' must have the same " 154901e04c3fSmrg "type", (this->oper == ast_equal) ? "==" : "!="); 155001e04c3fSmrg error_emitted = true; 155101e04c3fSmrg } else if ((op[0]->type->is_array() || op[1]->type->is_array()) && 155201e04c3fSmrg !state->check_version(120, 300, &loc, 155301e04c3fSmrg "array comparisons forbidden")) { 155401e04c3fSmrg error_emitted = true; 155501e04c3fSmrg } else if ((op[0]->type->contains_subroutine() || 155601e04c3fSmrg op[1]->type->contains_subroutine())) { 155701e04c3fSmrg _mesa_glsl_error(&loc, state, "subroutine comparisons forbidden"); 155801e04c3fSmrg error_emitted = true; 155901e04c3fSmrg } else if ((op[0]->type->contains_opaque() || 156001e04c3fSmrg op[1]->type->contains_opaque())) { 156101e04c3fSmrg _mesa_glsl_error(&loc, state, "opaque type comparisons forbidden"); 156201e04c3fSmrg error_emitted = true; 156301e04c3fSmrg } 156401e04c3fSmrg 156501e04c3fSmrg if (error_emitted) { 156601e04c3fSmrg result = new(ctx) ir_constant(false); 156701e04c3fSmrg } else { 156801e04c3fSmrg result = do_comparison(ctx, operations[this->oper], op[0], op[1]); 156901e04c3fSmrg assert(result->type == glsl_type::bool_type); 157001e04c3fSmrg } 157101e04c3fSmrg break; 157201e04c3fSmrg 157301e04c3fSmrg case ast_bit_and: 157401e04c3fSmrg case ast_bit_xor: 157501e04c3fSmrg case ast_bit_or: 157601e04c3fSmrg op[0] = this->subexpressions[0]->hir(instructions, state); 157701e04c3fSmrg op[1] = this->subexpressions[1]->hir(instructions, state); 157801e04c3fSmrg type = bit_logic_result_type(op[0], op[1], this->oper, state, &loc); 157901e04c3fSmrg result = new(ctx) ir_expression(operations[this->oper], type, 158001e04c3fSmrg op[0], op[1]); 158101e04c3fSmrg error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); 158201e04c3fSmrg break; 158301e04c3fSmrg 158401e04c3fSmrg case ast_bit_not: 158501e04c3fSmrg op[0] = this->subexpressions[0]->hir(instructions, state); 158601e04c3fSmrg 158701e04c3fSmrg if (!state->check_bitwise_operations_allowed(&loc)) { 158801e04c3fSmrg error_emitted = true; 158901e04c3fSmrg } 159001e04c3fSmrg 159101e04c3fSmrg if (!op[0]->type->is_integer_32_64()) { 159201e04c3fSmrg _mesa_glsl_error(&loc, state, "operand of `~' must be an integer"); 159301e04c3fSmrg error_emitted = true; 159401e04c3fSmrg } 159501e04c3fSmrg 159601e04c3fSmrg type = error_emitted ? glsl_type::error_type : op[0]->type; 159701e04c3fSmrg result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL); 159801e04c3fSmrg break; 159901e04c3fSmrg 160001e04c3fSmrg case ast_logic_and: { 160101e04c3fSmrg exec_list rhs_instructions; 160201e04c3fSmrg op[0] = get_scalar_boolean_operand(instructions, state, this, 0, 160301e04c3fSmrg "LHS", &error_emitted); 160401e04c3fSmrg op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1, 160501e04c3fSmrg "RHS", &error_emitted); 160601e04c3fSmrg 160701e04c3fSmrg if (rhs_instructions.is_empty()) { 160801e04c3fSmrg result = new(ctx) ir_expression(ir_binop_logic_and, op[0], op[1]); 160901e04c3fSmrg } else { 161001e04c3fSmrg ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type, 161101e04c3fSmrg "and_tmp", 161201e04c3fSmrg ir_var_temporary); 161301e04c3fSmrg instructions->push_tail(tmp); 161401e04c3fSmrg 161501e04c3fSmrg ir_if *const stmt = new(ctx) ir_if(op[0]); 161601e04c3fSmrg instructions->push_tail(stmt); 161701e04c3fSmrg 161801e04c3fSmrg stmt->then_instructions.append_list(&rhs_instructions); 161901e04c3fSmrg ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp); 162001e04c3fSmrg ir_assignment *const then_assign = 162101e04c3fSmrg new(ctx) ir_assignment(then_deref, op[1]); 162201e04c3fSmrg stmt->then_instructions.push_tail(then_assign); 162301e04c3fSmrg 162401e04c3fSmrg ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp); 162501e04c3fSmrg ir_assignment *const else_assign = 162601e04c3fSmrg new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false)); 162701e04c3fSmrg stmt->else_instructions.push_tail(else_assign); 162801e04c3fSmrg 162901e04c3fSmrg result = new(ctx) ir_dereference_variable(tmp); 163001e04c3fSmrg } 163101e04c3fSmrg break; 163201e04c3fSmrg } 163301e04c3fSmrg 163401e04c3fSmrg case ast_logic_or: { 163501e04c3fSmrg exec_list rhs_instructions; 163601e04c3fSmrg op[0] = get_scalar_boolean_operand(instructions, state, this, 0, 163701e04c3fSmrg "LHS", &error_emitted); 163801e04c3fSmrg op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1, 163901e04c3fSmrg "RHS", &error_emitted); 164001e04c3fSmrg 164101e04c3fSmrg if (rhs_instructions.is_empty()) { 164201e04c3fSmrg result = new(ctx) ir_expression(ir_binop_logic_or, op[0], op[1]); 164301e04c3fSmrg } else { 164401e04c3fSmrg ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type, 164501e04c3fSmrg "or_tmp", 164601e04c3fSmrg ir_var_temporary); 164701e04c3fSmrg instructions->push_tail(tmp); 164801e04c3fSmrg 164901e04c3fSmrg ir_if *const stmt = new(ctx) ir_if(op[0]); 165001e04c3fSmrg instructions->push_tail(stmt); 165101e04c3fSmrg 165201e04c3fSmrg ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp); 165301e04c3fSmrg ir_assignment *const then_assign = 165401e04c3fSmrg new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true)); 165501e04c3fSmrg stmt->then_instructions.push_tail(then_assign); 165601e04c3fSmrg 165701e04c3fSmrg stmt->else_instructions.append_list(&rhs_instructions); 165801e04c3fSmrg ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp); 165901e04c3fSmrg ir_assignment *const else_assign = 166001e04c3fSmrg new(ctx) ir_assignment(else_deref, op[1]); 166101e04c3fSmrg stmt->else_instructions.push_tail(else_assign); 166201e04c3fSmrg 166301e04c3fSmrg result = new(ctx) ir_dereference_variable(tmp); 166401e04c3fSmrg } 166501e04c3fSmrg break; 166601e04c3fSmrg } 166701e04c3fSmrg 166801e04c3fSmrg case ast_logic_xor: 166901e04c3fSmrg /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec: 167001e04c3fSmrg * 167101e04c3fSmrg * "The logical binary operators and (&&), or ( | | ), and 167201e04c3fSmrg * exclusive or (^^). They operate only on two Boolean 167301e04c3fSmrg * expressions and result in a Boolean expression." 167401e04c3fSmrg */ 167501e04c3fSmrg op[0] = get_scalar_boolean_operand(instructions, state, this, 0, "LHS", 167601e04c3fSmrg &error_emitted); 167701e04c3fSmrg op[1] = get_scalar_boolean_operand(instructions, state, this, 1, "RHS", 167801e04c3fSmrg &error_emitted); 167901e04c3fSmrg 168001e04c3fSmrg result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type, 168101e04c3fSmrg op[0], op[1]); 168201e04c3fSmrg break; 168301e04c3fSmrg 168401e04c3fSmrg case ast_logic_not: 168501e04c3fSmrg op[0] = get_scalar_boolean_operand(instructions, state, this, 0, 168601e04c3fSmrg "operand", &error_emitted); 168701e04c3fSmrg 168801e04c3fSmrg result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type, 168901e04c3fSmrg op[0], NULL); 169001e04c3fSmrg break; 169101e04c3fSmrg 169201e04c3fSmrg case ast_mul_assign: 169301e04c3fSmrg case ast_div_assign: 169401e04c3fSmrg case ast_add_assign: 169501e04c3fSmrg case ast_sub_assign: { 169601e04c3fSmrg this->subexpressions[0]->set_is_lhs(true); 169701e04c3fSmrg op[0] = this->subexpressions[0]->hir(instructions, state); 169801e04c3fSmrg op[1] = this->subexpressions[1]->hir(instructions, state); 169901e04c3fSmrg 170001e04c3fSmrg orig_type = op[0]->type; 170101e04c3fSmrg 170201e04c3fSmrg /* Break out if operand types were not parsed successfully. */ 170301e04c3fSmrg if ((op[0]->type == glsl_type::error_type || 17047ec681f3Smrg op[1]->type == glsl_type::error_type)) { 17057ec681f3Smrg error_emitted = true; 17067ec681f3Smrg result = ir_rvalue::error_value(ctx); 170701e04c3fSmrg break; 17087ec681f3Smrg } 170901e04c3fSmrg 171001e04c3fSmrg type = arithmetic_result_type(op[0], op[1], 171101e04c3fSmrg (this->oper == ast_mul_assign), 171201e04c3fSmrg state, & loc); 171301e04c3fSmrg 171401e04c3fSmrg if (type != orig_type) { 171501e04c3fSmrg _mesa_glsl_error(& loc, state, 171601e04c3fSmrg "could not implicitly convert " 171701e04c3fSmrg "%s to %s", type->name, orig_type->name); 171801e04c3fSmrg type = glsl_type::error_type; 171901e04c3fSmrg } 172001e04c3fSmrg 172101e04c3fSmrg ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type, 172201e04c3fSmrg op[0], op[1]); 172301e04c3fSmrg 172401e04c3fSmrg error_emitted = 172501e04c3fSmrg do_assignment(instructions, state, 172601e04c3fSmrg this->subexpressions[0]->non_lvalue_description, 172701e04c3fSmrg op[0]->clone(ctx, NULL), temp_rhs, 172801e04c3fSmrg &result, needs_rvalue, false, 172901e04c3fSmrg this->subexpressions[0]->get_location()); 173001e04c3fSmrg 173101e04c3fSmrg /* GLSL 1.10 does not allow array assignment. However, we don't have to 173201e04c3fSmrg * explicitly test for this because none of the binary expression 173301e04c3fSmrg * operators allow array operands either. 173401e04c3fSmrg */ 173501e04c3fSmrg 173601e04c3fSmrg break; 173701e04c3fSmrg } 173801e04c3fSmrg 173901e04c3fSmrg case ast_mod_assign: { 174001e04c3fSmrg this->subexpressions[0]->set_is_lhs(true); 174101e04c3fSmrg op[0] = this->subexpressions[0]->hir(instructions, state); 174201e04c3fSmrg op[1] = this->subexpressions[1]->hir(instructions, state); 174301e04c3fSmrg 17447ec681f3Smrg /* Break out if operand types were not parsed successfully. */ 17457ec681f3Smrg if ((op[0]->type == glsl_type::error_type || 17467ec681f3Smrg op[1]->type == glsl_type::error_type)) { 17477ec681f3Smrg error_emitted = true; 17487ec681f3Smrg result = ir_rvalue::error_value(ctx); 17497ec681f3Smrg break; 17507ec681f3Smrg } 17517ec681f3Smrg 175201e04c3fSmrg orig_type = op[0]->type; 175301e04c3fSmrg type = modulus_result_type(op[0], op[1], state, &loc); 175401e04c3fSmrg 175501e04c3fSmrg if (type != orig_type) { 175601e04c3fSmrg _mesa_glsl_error(& loc, state, 175701e04c3fSmrg "could not implicitly convert " 175801e04c3fSmrg "%s to %s", type->name, orig_type->name); 175901e04c3fSmrg type = glsl_type::error_type; 176001e04c3fSmrg } 176101e04c3fSmrg 176201e04c3fSmrg assert(operations[this->oper] == ir_binop_mod); 176301e04c3fSmrg 176401e04c3fSmrg ir_rvalue *temp_rhs; 176501e04c3fSmrg temp_rhs = new(ctx) ir_expression(operations[this->oper], type, 176601e04c3fSmrg op[0], op[1]); 176701e04c3fSmrg 176801e04c3fSmrg error_emitted = 176901e04c3fSmrg do_assignment(instructions, state, 177001e04c3fSmrg this->subexpressions[0]->non_lvalue_description, 177101e04c3fSmrg op[0]->clone(ctx, NULL), temp_rhs, 177201e04c3fSmrg &result, needs_rvalue, false, 177301e04c3fSmrg this->subexpressions[0]->get_location()); 177401e04c3fSmrg break; 177501e04c3fSmrg } 177601e04c3fSmrg 177701e04c3fSmrg case ast_ls_assign: 177801e04c3fSmrg case ast_rs_assign: { 177901e04c3fSmrg this->subexpressions[0]->set_is_lhs(true); 178001e04c3fSmrg op[0] = this->subexpressions[0]->hir(instructions, state); 178101e04c3fSmrg op[1] = this->subexpressions[1]->hir(instructions, state); 17827ec681f3Smrg 17837ec681f3Smrg /* Break out if operand types were not parsed successfully. */ 17847ec681f3Smrg if ((op[0]->type == glsl_type::error_type || 17857ec681f3Smrg op[1]->type == glsl_type::error_type)) { 17867ec681f3Smrg error_emitted = true; 17877ec681f3Smrg result = ir_rvalue::error_value(ctx); 17887ec681f3Smrg break; 17897ec681f3Smrg } 17907ec681f3Smrg 179101e04c3fSmrg type = shift_result_type(op[0]->type, op[1]->type, this->oper, state, 179201e04c3fSmrg &loc); 179301e04c3fSmrg ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], 179401e04c3fSmrg type, op[0], op[1]); 179501e04c3fSmrg error_emitted = 179601e04c3fSmrg do_assignment(instructions, state, 179701e04c3fSmrg this->subexpressions[0]->non_lvalue_description, 179801e04c3fSmrg op[0]->clone(ctx, NULL), temp_rhs, 179901e04c3fSmrg &result, needs_rvalue, false, 180001e04c3fSmrg this->subexpressions[0]->get_location()); 180101e04c3fSmrg break; 180201e04c3fSmrg } 180301e04c3fSmrg 180401e04c3fSmrg case ast_and_assign: 180501e04c3fSmrg case ast_xor_assign: 180601e04c3fSmrg case ast_or_assign: { 180701e04c3fSmrg this->subexpressions[0]->set_is_lhs(true); 180801e04c3fSmrg op[0] = this->subexpressions[0]->hir(instructions, state); 180901e04c3fSmrg op[1] = this->subexpressions[1]->hir(instructions, state); 181001e04c3fSmrg 18117ec681f3Smrg /* Break out if operand types were not parsed successfully. */ 18127ec681f3Smrg if ((op[0]->type == glsl_type::error_type || 18137ec681f3Smrg op[1]->type == glsl_type::error_type)) { 18147ec681f3Smrg error_emitted = true; 18157ec681f3Smrg result = ir_rvalue::error_value(ctx); 18167ec681f3Smrg break; 18177ec681f3Smrg } 18187ec681f3Smrg 181901e04c3fSmrg orig_type = op[0]->type; 182001e04c3fSmrg type = bit_logic_result_type(op[0], op[1], this->oper, state, &loc); 182101e04c3fSmrg 182201e04c3fSmrg if (type != orig_type) { 182301e04c3fSmrg _mesa_glsl_error(& loc, state, 182401e04c3fSmrg "could not implicitly convert " 182501e04c3fSmrg "%s to %s", type->name, orig_type->name); 182601e04c3fSmrg type = glsl_type::error_type; 182701e04c3fSmrg } 182801e04c3fSmrg 182901e04c3fSmrg ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], 183001e04c3fSmrg type, op[0], op[1]); 183101e04c3fSmrg error_emitted = 183201e04c3fSmrg do_assignment(instructions, state, 183301e04c3fSmrg this->subexpressions[0]->non_lvalue_description, 183401e04c3fSmrg op[0]->clone(ctx, NULL), temp_rhs, 183501e04c3fSmrg &result, needs_rvalue, false, 183601e04c3fSmrg this->subexpressions[0]->get_location()); 183701e04c3fSmrg break; 183801e04c3fSmrg } 183901e04c3fSmrg 184001e04c3fSmrg case ast_conditional: { 184101e04c3fSmrg /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec: 184201e04c3fSmrg * 184301e04c3fSmrg * "The ternary selection operator (?:). It operates on three 184401e04c3fSmrg * expressions (exp1 ? exp2 : exp3). This operator evaluates the 184501e04c3fSmrg * first expression, which must result in a scalar Boolean." 184601e04c3fSmrg */ 184701e04c3fSmrg op[0] = get_scalar_boolean_operand(instructions, state, this, 0, 184801e04c3fSmrg "condition", &error_emitted); 184901e04c3fSmrg 185001e04c3fSmrg /* The :? operator is implemented by generating an anonymous temporary 185101e04c3fSmrg * followed by an if-statement. The last instruction in each branch of 185201e04c3fSmrg * the if-statement assigns a value to the anonymous temporary. This 185301e04c3fSmrg * temporary is the r-value of the expression. 185401e04c3fSmrg */ 185501e04c3fSmrg exec_list then_instructions; 185601e04c3fSmrg exec_list else_instructions; 185701e04c3fSmrg 185801e04c3fSmrg op[1] = this->subexpressions[1]->hir(&then_instructions, state); 185901e04c3fSmrg op[2] = this->subexpressions[2]->hir(&else_instructions, state); 186001e04c3fSmrg 186101e04c3fSmrg /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec: 186201e04c3fSmrg * 186301e04c3fSmrg * "The second and third expressions can be any type, as 186401e04c3fSmrg * long their types match, or there is a conversion in 186501e04c3fSmrg * Section 4.1.10 "Implicit Conversions" that can be applied 186601e04c3fSmrg * to one of the expressions to make their types match. This 186701e04c3fSmrg * resulting matching type is the type of the entire 186801e04c3fSmrg * expression." 186901e04c3fSmrg */ 187001e04c3fSmrg if ((!apply_implicit_conversion(op[1]->type, op[2], state) 187101e04c3fSmrg && !apply_implicit_conversion(op[2]->type, op[1], state)) 187201e04c3fSmrg || (op[1]->type != op[2]->type)) { 187301e04c3fSmrg YYLTYPE loc = this->subexpressions[1]->get_location(); 187401e04c3fSmrg 187501e04c3fSmrg _mesa_glsl_error(& loc, state, "second and third operands of ?: " 187601e04c3fSmrg "operator must have matching types"); 187701e04c3fSmrg error_emitted = true; 187801e04c3fSmrg type = glsl_type::error_type; 187901e04c3fSmrg } else { 188001e04c3fSmrg type = op[1]->type; 188101e04c3fSmrg } 188201e04c3fSmrg 188301e04c3fSmrg /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec: 188401e04c3fSmrg * 188501e04c3fSmrg * "The second and third expressions must be the same type, but can 188601e04c3fSmrg * be of any type other than an array." 188701e04c3fSmrg */ 188801e04c3fSmrg if (type->is_array() && 188901e04c3fSmrg !state->check_version(120, 300, &loc, 189001e04c3fSmrg "second and third operands of ?: operator " 189101e04c3fSmrg "cannot be arrays")) { 189201e04c3fSmrg error_emitted = true; 189301e04c3fSmrg } 189401e04c3fSmrg 189501e04c3fSmrg /* From section 4.1.7 of the GLSL 4.50 spec (Opaque Types): 189601e04c3fSmrg * 189701e04c3fSmrg * "Except for array indexing, structure member selection, and 189801e04c3fSmrg * parentheses, opaque variables are not allowed to be operands in 189901e04c3fSmrg * expressions; such use results in a compile-time error." 190001e04c3fSmrg */ 190101e04c3fSmrg if (type->contains_opaque()) { 190201e04c3fSmrg if (!(state->has_bindless() && (type->is_image() || type->is_sampler()))) { 190301e04c3fSmrg _mesa_glsl_error(&loc, state, "variables of type %s cannot be " 190401e04c3fSmrg "operands of the ?: operator", type->name); 190501e04c3fSmrg error_emitted = true; 190601e04c3fSmrg } 190701e04c3fSmrg } 190801e04c3fSmrg 190901e04c3fSmrg ir_constant *cond_val = op[0]->constant_expression_value(ctx); 191001e04c3fSmrg 191101e04c3fSmrg if (then_instructions.is_empty() 191201e04c3fSmrg && else_instructions.is_empty() 191301e04c3fSmrg && cond_val != NULL) { 191401e04c3fSmrg result = cond_val->value.b[0] ? op[1] : op[2]; 191501e04c3fSmrg } else { 191601e04c3fSmrg /* The copy to conditional_tmp reads the whole array. */ 191701e04c3fSmrg if (type->is_array()) { 191801e04c3fSmrg mark_whole_array_access(op[1]); 191901e04c3fSmrg mark_whole_array_access(op[2]); 192001e04c3fSmrg } 192101e04c3fSmrg 192201e04c3fSmrg ir_variable *const tmp = 192301e04c3fSmrg new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary); 192401e04c3fSmrg instructions->push_tail(tmp); 192501e04c3fSmrg 192601e04c3fSmrg ir_if *const stmt = new(ctx) ir_if(op[0]); 192701e04c3fSmrg instructions->push_tail(stmt); 192801e04c3fSmrg 192901e04c3fSmrg then_instructions.move_nodes_to(& stmt->then_instructions); 193001e04c3fSmrg ir_dereference *const then_deref = 193101e04c3fSmrg new(ctx) ir_dereference_variable(tmp); 193201e04c3fSmrg ir_assignment *const then_assign = 193301e04c3fSmrg new(ctx) ir_assignment(then_deref, op[1]); 193401e04c3fSmrg stmt->then_instructions.push_tail(then_assign); 193501e04c3fSmrg 193601e04c3fSmrg else_instructions.move_nodes_to(& stmt->else_instructions); 193701e04c3fSmrg ir_dereference *const else_deref = 193801e04c3fSmrg new(ctx) ir_dereference_variable(tmp); 193901e04c3fSmrg ir_assignment *const else_assign = 194001e04c3fSmrg new(ctx) ir_assignment(else_deref, op[2]); 194101e04c3fSmrg stmt->else_instructions.push_tail(else_assign); 194201e04c3fSmrg 194301e04c3fSmrg result = new(ctx) ir_dereference_variable(tmp); 194401e04c3fSmrg } 194501e04c3fSmrg break; 194601e04c3fSmrg } 194701e04c3fSmrg 194801e04c3fSmrg case ast_pre_inc: 194901e04c3fSmrg case ast_pre_dec: { 195001e04c3fSmrg this->non_lvalue_description = (this->oper == ast_pre_inc) 195101e04c3fSmrg ? "pre-increment operation" : "pre-decrement operation"; 195201e04c3fSmrg 195301e04c3fSmrg op[0] = this->subexpressions[0]->hir(instructions, state); 195401e04c3fSmrg op[1] = constant_one_for_inc_dec(ctx, op[0]->type); 195501e04c3fSmrg 195601e04c3fSmrg type = arithmetic_result_type(op[0], op[1], false, state, & loc); 195701e04c3fSmrg 195801e04c3fSmrg ir_rvalue *temp_rhs; 195901e04c3fSmrg temp_rhs = new(ctx) ir_expression(operations[this->oper], type, 196001e04c3fSmrg op[0], op[1]); 196101e04c3fSmrg 196201e04c3fSmrg error_emitted = 196301e04c3fSmrg do_assignment(instructions, state, 196401e04c3fSmrg this->subexpressions[0]->non_lvalue_description, 196501e04c3fSmrg op[0]->clone(ctx, NULL), temp_rhs, 196601e04c3fSmrg &result, needs_rvalue, false, 196701e04c3fSmrg this->subexpressions[0]->get_location()); 196801e04c3fSmrg break; 196901e04c3fSmrg } 197001e04c3fSmrg 197101e04c3fSmrg case ast_post_inc: 197201e04c3fSmrg case ast_post_dec: { 197301e04c3fSmrg this->non_lvalue_description = (this->oper == ast_post_inc) 197401e04c3fSmrg ? "post-increment operation" : "post-decrement operation"; 197501e04c3fSmrg op[0] = this->subexpressions[0]->hir(instructions, state); 197601e04c3fSmrg op[1] = constant_one_for_inc_dec(ctx, op[0]->type); 197701e04c3fSmrg 197801e04c3fSmrg error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); 197901e04c3fSmrg 198001e04c3fSmrg if (error_emitted) { 198101e04c3fSmrg result = ir_rvalue::error_value(ctx); 198201e04c3fSmrg break; 198301e04c3fSmrg } 198401e04c3fSmrg 198501e04c3fSmrg type = arithmetic_result_type(op[0], op[1], false, state, & loc); 198601e04c3fSmrg 198701e04c3fSmrg ir_rvalue *temp_rhs; 198801e04c3fSmrg temp_rhs = new(ctx) ir_expression(operations[this->oper], type, 198901e04c3fSmrg op[0], op[1]); 199001e04c3fSmrg 199101e04c3fSmrg /* Get a temporary of a copy of the lvalue before it's modified. 199201e04c3fSmrg * This may get thrown away later. 199301e04c3fSmrg */ 199401e04c3fSmrg result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL)); 199501e04c3fSmrg 199601e04c3fSmrg ir_rvalue *junk_rvalue; 199701e04c3fSmrg error_emitted = 199801e04c3fSmrg do_assignment(instructions, state, 199901e04c3fSmrg this->subexpressions[0]->non_lvalue_description, 200001e04c3fSmrg op[0]->clone(ctx, NULL), temp_rhs, 200101e04c3fSmrg &junk_rvalue, false, false, 200201e04c3fSmrg this->subexpressions[0]->get_location()); 200301e04c3fSmrg 200401e04c3fSmrg break; 200501e04c3fSmrg } 200601e04c3fSmrg 200701e04c3fSmrg case ast_field_selection: 200801e04c3fSmrg result = _mesa_ast_field_selection_to_hir(this, instructions, state); 200901e04c3fSmrg break; 201001e04c3fSmrg 201101e04c3fSmrg case ast_array_index: { 201201e04c3fSmrg YYLTYPE index_loc = subexpressions[1]->get_location(); 201301e04c3fSmrg 201401e04c3fSmrg /* Getting if an array is being used uninitialized is beyond what we get 201501e04c3fSmrg * from ir_value.data.assigned. Setting is_lhs as true would force to 201601e04c3fSmrg * not raise a uninitialized warning when using an array 201701e04c3fSmrg */ 201801e04c3fSmrg subexpressions[0]->set_is_lhs(true); 201901e04c3fSmrg op[0] = subexpressions[0]->hir(instructions, state); 202001e04c3fSmrg op[1] = subexpressions[1]->hir(instructions, state); 202101e04c3fSmrg 202201e04c3fSmrg result = _mesa_ast_array_index_to_hir(ctx, state, op[0], op[1], 202301e04c3fSmrg loc, index_loc); 202401e04c3fSmrg 202501e04c3fSmrg if (result->type->is_error()) 202601e04c3fSmrg error_emitted = true; 202701e04c3fSmrg 202801e04c3fSmrg break; 202901e04c3fSmrg } 203001e04c3fSmrg 203101e04c3fSmrg case ast_unsized_array_dim: 203201e04c3fSmrg unreachable("ast_unsized_array_dim: Should never get here."); 203301e04c3fSmrg 203401e04c3fSmrg case ast_function_call: 203501e04c3fSmrg /* Should *NEVER* get here. ast_function_call should always be handled 203601e04c3fSmrg * by ast_function_expression::hir. 203701e04c3fSmrg */ 203801e04c3fSmrg unreachable("ast_function_call: handled elsewhere "); 203901e04c3fSmrg 204001e04c3fSmrg case ast_identifier: { 204101e04c3fSmrg /* ast_identifier can appear several places in a full abstract syntax 204201e04c3fSmrg * tree. This particular use must be at location specified in the grammar 204301e04c3fSmrg * as 'variable_identifier'. 204401e04c3fSmrg */ 204501e04c3fSmrg ir_variable *var = 204601e04c3fSmrg state->symbols->get_variable(this->primary_expression.identifier); 204701e04c3fSmrg 204801e04c3fSmrg if (var == NULL) { 204901e04c3fSmrg /* the identifier might be a subroutine name */ 205001e04c3fSmrg char *sub_name; 205101e04c3fSmrg sub_name = ralloc_asprintf(ctx, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state->stage), this->primary_expression.identifier); 205201e04c3fSmrg var = state->symbols->get_variable(sub_name); 205301e04c3fSmrg ralloc_free(sub_name); 205401e04c3fSmrg } 205501e04c3fSmrg 205601e04c3fSmrg if (var != NULL) { 205701e04c3fSmrg var->data.used = true; 205801e04c3fSmrg result = new(ctx) ir_dereference_variable(var); 205901e04c3fSmrg 206001e04c3fSmrg if ((var->data.mode == ir_var_auto || var->data.mode == ir_var_shader_out) 206101e04c3fSmrg && !this->is_lhs 206201e04c3fSmrg && result->variable_referenced()->data.assigned != true 206301e04c3fSmrg && !is_gl_identifier(var->name)) { 206401e04c3fSmrg _mesa_glsl_warning(&loc, state, "`%s' used uninitialized", 206501e04c3fSmrg this->primary_expression.identifier); 206601e04c3fSmrg } 206701e04c3fSmrg 206801e04c3fSmrg /* From the EXT_shader_framebuffer_fetch spec: 206901e04c3fSmrg * 207001e04c3fSmrg * "Unless the GL_EXT_shader_framebuffer_fetch extension has been 207101e04c3fSmrg * enabled in addition, it's an error to use gl_LastFragData if it 207201e04c3fSmrg * hasn't been explicitly redeclared with layout(noncoherent)." 207301e04c3fSmrg */ 207401e04c3fSmrg if (var->data.fb_fetch_output && var->data.memory_coherent && 207501e04c3fSmrg !state->EXT_shader_framebuffer_fetch_enable) { 207601e04c3fSmrg _mesa_glsl_error(&loc, state, 207701e04c3fSmrg "invalid use of framebuffer fetch output not " 207801e04c3fSmrg "qualified with layout(noncoherent)"); 207901e04c3fSmrg } 208001e04c3fSmrg 208101e04c3fSmrg } else { 208201e04c3fSmrg _mesa_glsl_error(& loc, state, "`%s' undeclared", 208301e04c3fSmrg this->primary_expression.identifier); 208401e04c3fSmrg 208501e04c3fSmrg result = ir_rvalue::error_value(ctx); 208601e04c3fSmrg error_emitted = true; 208701e04c3fSmrg } 208801e04c3fSmrg break; 208901e04c3fSmrg } 209001e04c3fSmrg 209101e04c3fSmrg case ast_int_constant: 209201e04c3fSmrg result = new(ctx) ir_constant(this->primary_expression.int_constant); 209301e04c3fSmrg break; 209401e04c3fSmrg 209501e04c3fSmrg case ast_uint_constant: 209601e04c3fSmrg result = new(ctx) ir_constant(this->primary_expression.uint_constant); 209701e04c3fSmrg break; 209801e04c3fSmrg 209901e04c3fSmrg case ast_float_constant: 210001e04c3fSmrg result = new(ctx) ir_constant(this->primary_expression.float_constant); 210101e04c3fSmrg break; 210201e04c3fSmrg 210301e04c3fSmrg case ast_bool_constant: 210401e04c3fSmrg result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant)); 210501e04c3fSmrg break; 210601e04c3fSmrg 210701e04c3fSmrg case ast_double_constant: 210801e04c3fSmrg result = new(ctx) ir_constant(this->primary_expression.double_constant); 210901e04c3fSmrg break; 211001e04c3fSmrg 211101e04c3fSmrg case ast_uint64_constant: 211201e04c3fSmrg result = new(ctx) ir_constant(this->primary_expression.uint64_constant); 211301e04c3fSmrg break; 211401e04c3fSmrg 211501e04c3fSmrg case ast_int64_constant: 211601e04c3fSmrg result = new(ctx) ir_constant(this->primary_expression.int64_constant); 211701e04c3fSmrg break; 211801e04c3fSmrg 211901e04c3fSmrg case ast_sequence: { 212001e04c3fSmrg /* It should not be possible to generate a sequence in the AST without 212101e04c3fSmrg * any expressions in it. 212201e04c3fSmrg */ 212301e04c3fSmrg assert(!this->expressions.is_empty()); 212401e04c3fSmrg 212501e04c3fSmrg /* The r-value of a sequence is the last expression in the sequence. If 212601e04c3fSmrg * the other expressions in the sequence do not have side-effects (and 212701e04c3fSmrg * therefore add instructions to the instruction list), they get dropped 212801e04c3fSmrg * on the floor. 212901e04c3fSmrg */ 213001e04c3fSmrg exec_node *previous_tail = NULL; 213101e04c3fSmrg YYLTYPE previous_operand_loc = loc; 213201e04c3fSmrg 213301e04c3fSmrg foreach_list_typed (ast_node, ast, link, &this->expressions) { 213401e04c3fSmrg /* If one of the operands of comma operator does not generate any 213501e04c3fSmrg * code, we want to emit a warning. At each pass through the loop 213601e04c3fSmrg * previous_tail will point to the last instruction in the stream 213701e04c3fSmrg * *before* processing the previous operand. Naturally, 213801e04c3fSmrg * instructions->get_tail_raw() will point to the last instruction in 213901e04c3fSmrg * the stream *after* processing the previous operand. If the two 214001e04c3fSmrg * pointers match, then the previous operand had no effect. 214101e04c3fSmrg * 214201e04c3fSmrg * The warning behavior here differs slightly from GCC. GCC will 214301e04c3fSmrg * only emit a warning if none of the left-hand operands have an 214401e04c3fSmrg * effect. However, it will emit a warning for each. I believe that 214501e04c3fSmrg * there are some cases in C (especially with GCC extensions) where 214601e04c3fSmrg * it is useful to have an intermediate step in a sequence have no 214701e04c3fSmrg * effect, but I don't think these cases exist in GLSL. Either way, 214801e04c3fSmrg * it would be a giant hassle to replicate that behavior. 214901e04c3fSmrg */ 215001e04c3fSmrg if (previous_tail == instructions->get_tail_raw()) { 215101e04c3fSmrg _mesa_glsl_warning(&previous_operand_loc, state, 215201e04c3fSmrg "left-hand operand of comma expression has " 215301e04c3fSmrg "no effect"); 215401e04c3fSmrg } 215501e04c3fSmrg 215601e04c3fSmrg /* The tail is directly accessed instead of using the get_tail() 215701e04c3fSmrg * method for performance reasons. get_tail() has extra code to 215801e04c3fSmrg * return NULL when the list is empty. We don't care about that 215901e04c3fSmrg * here, so using get_tail_raw() is fine. 216001e04c3fSmrg */ 216101e04c3fSmrg previous_tail = instructions->get_tail_raw(); 216201e04c3fSmrg previous_operand_loc = ast->get_location(); 216301e04c3fSmrg 216401e04c3fSmrg result = ast->hir(instructions, state); 216501e04c3fSmrg } 216601e04c3fSmrg 216701e04c3fSmrg /* Any errors should have already been emitted in the loop above. 216801e04c3fSmrg */ 216901e04c3fSmrg error_emitted = true; 217001e04c3fSmrg break; 217101e04c3fSmrg } 217201e04c3fSmrg } 217301e04c3fSmrg type = NULL; /* use result->type, not type. */ 21747ec681f3Smrg assert(error_emitted || (result != NULL || !needs_rvalue)); 217501e04c3fSmrg 217601e04c3fSmrg if (result && result->type->is_error() && !error_emitted) 217701e04c3fSmrg _mesa_glsl_error(& loc, state, "type mismatch"); 217801e04c3fSmrg 217901e04c3fSmrg return result; 218001e04c3fSmrg} 218101e04c3fSmrg 218201e04c3fSmrgbool 218301e04c3fSmrgast_expression::has_sequence_subexpression() const 218401e04c3fSmrg{ 218501e04c3fSmrg switch (this->oper) { 218601e04c3fSmrg case ast_plus: 218701e04c3fSmrg case ast_neg: 218801e04c3fSmrg case ast_bit_not: 218901e04c3fSmrg case ast_logic_not: 219001e04c3fSmrg case ast_pre_inc: 219101e04c3fSmrg case ast_pre_dec: 219201e04c3fSmrg case ast_post_inc: 219301e04c3fSmrg case ast_post_dec: 219401e04c3fSmrg return this->subexpressions[0]->has_sequence_subexpression(); 219501e04c3fSmrg 219601e04c3fSmrg case ast_assign: 219701e04c3fSmrg case ast_add: 219801e04c3fSmrg case ast_sub: 219901e04c3fSmrg case ast_mul: 220001e04c3fSmrg case ast_div: 220101e04c3fSmrg case ast_mod: 220201e04c3fSmrg case ast_lshift: 220301e04c3fSmrg case ast_rshift: 220401e04c3fSmrg case ast_less: 220501e04c3fSmrg case ast_greater: 220601e04c3fSmrg case ast_lequal: 220701e04c3fSmrg case ast_gequal: 220801e04c3fSmrg case ast_nequal: 220901e04c3fSmrg case ast_equal: 221001e04c3fSmrg case ast_bit_and: 221101e04c3fSmrg case ast_bit_xor: 221201e04c3fSmrg case ast_bit_or: 221301e04c3fSmrg case ast_logic_and: 221401e04c3fSmrg case ast_logic_or: 221501e04c3fSmrg case ast_logic_xor: 221601e04c3fSmrg case ast_array_index: 221701e04c3fSmrg case ast_mul_assign: 221801e04c3fSmrg case ast_div_assign: 221901e04c3fSmrg case ast_add_assign: 222001e04c3fSmrg case ast_sub_assign: 222101e04c3fSmrg case ast_mod_assign: 222201e04c3fSmrg case ast_ls_assign: 222301e04c3fSmrg case ast_rs_assign: 222401e04c3fSmrg case ast_and_assign: 222501e04c3fSmrg case ast_xor_assign: 222601e04c3fSmrg case ast_or_assign: 222701e04c3fSmrg return this->subexpressions[0]->has_sequence_subexpression() || 222801e04c3fSmrg this->subexpressions[1]->has_sequence_subexpression(); 222901e04c3fSmrg 223001e04c3fSmrg case ast_conditional: 223101e04c3fSmrg return this->subexpressions[0]->has_sequence_subexpression() || 223201e04c3fSmrg this->subexpressions[1]->has_sequence_subexpression() || 223301e04c3fSmrg this->subexpressions[2]->has_sequence_subexpression(); 223401e04c3fSmrg 223501e04c3fSmrg case ast_sequence: 223601e04c3fSmrg return true; 223701e04c3fSmrg 223801e04c3fSmrg case ast_field_selection: 223901e04c3fSmrg case ast_identifier: 224001e04c3fSmrg case ast_int_constant: 224101e04c3fSmrg case ast_uint_constant: 224201e04c3fSmrg case ast_float_constant: 224301e04c3fSmrg case ast_bool_constant: 224401e04c3fSmrg case ast_double_constant: 224501e04c3fSmrg case ast_int64_constant: 224601e04c3fSmrg case ast_uint64_constant: 224701e04c3fSmrg return false; 224801e04c3fSmrg 224901e04c3fSmrg case ast_aggregate: 225001e04c3fSmrg return false; 225101e04c3fSmrg 225201e04c3fSmrg case ast_function_call: 225301e04c3fSmrg unreachable("should be handled by ast_function_expression::hir"); 225401e04c3fSmrg 225501e04c3fSmrg case ast_unsized_array_dim: 225601e04c3fSmrg unreachable("ast_unsized_array_dim: Should never get here."); 225701e04c3fSmrg } 225801e04c3fSmrg 225901e04c3fSmrg return false; 226001e04c3fSmrg} 226101e04c3fSmrg 226201e04c3fSmrgir_rvalue * 226301e04c3fSmrgast_expression_statement::hir(exec_list *instructions, 226401e04c3fSmrg struct _mesa_glsl_parse_state *state) 226501e04c3fSmrg{ 226601e04c3fSmrg /* It is possible to have expression statements that don't have an 226701e04c3fSmrg * expression. This is the solitary semicolon: 226801e04c3fSmrg * 226901e04c3fSmrg * for (i = 0; i < 5; i++) 227001e04c3fSmrg * ; 227101e04c3fSmrg * 227201e04c3fSmrg * In this case the expression will be NULL. Test for NULL and don't do 227301e04c3fSmrg * anything in that case. 227401e04c3fSmrg */ 227501e04c3fSmrg if (expression != NULL) 227601e04c3fSmrg expression->hir_no_rvalue(instructions, state); 227701e04c3fSmrg 227801e04c3fSmrg /* Statements do not have r-values. 227901e04c3fSmrg */ 228001e04c3fSmrg return NULL; 228101e04c3fSmrg} 228201e04c3fSmrg 228301e04c3fSmrg 228401e04c3fSmrgir_rvalue * 228501e04c3fSmrgast_compound_statement::hir(exec_list *instructions, 228601e04c3fSmrg struct _mesa_glsl_parse_state *state) 228701e04c3fSmrg{ 228801e04c3fSmrg if (new_scope) 228901e04c3fSmrg state->symbols->push_scope(); 229001e04c3fSmrg 229101e04c3fSmrg foreach_list_typed (ast_node, ast, link, &this->statements) 229201e04c3fSmrg ast->hir(instructions, state); 229301e04c3fSmrg 229401e04c3fSmrg if (new_scope) 229501e04c3fSmrg state->symbols->pop_scope(); 229601e04c3fSmrg 229701e04c3fSmrg /* Compound statements do not have r-values. 229801e04c3fSmrg */ 229901e04c3fSmrg return NULL; 230001e04c3fSmrg} 230101e04c3fSmrg 230201e04c3fSmrg/** 230301e04c3fSmrg * Evaluate the given exec_node (which should be an ast_node representing 230401e04c3fSmrg * a single array dimension) and return its integer value. 230501e04c3fSmrg */ 230601e04c3fSmrgstatic unsigned 230701e04c3fSmrgprocess_array_size(exec_node *node, 230801e04c3fSmrg struct _mesa_glsl_parse_state *state) 230901e04c3fSmrg{ 231001e04c3fSmrg void *mem_ctx = state; 231101e04c3fSmrg 231201e04c3fSmrg exec_list dummy_instructions; 231301e04c3fSmrg 231401e04c3fSmrg ast_node *array_size = exec_node_data(ast_node, node, link); 231501e04c3fSmrg 231601e04c3fSmrg /** 231701e04c3fSmrg * Dimensions other than the outermost dimension can by unsized if they 231801e04c3fSmrg * are immediately sized by a constructor or initializer. 231901e04c3fSmrg */ 232001e04c3fSmrg if (((ast_expression*)array_size)->oper == ast_unsized_array_dim) 232101e04c3fSmrg return 0; 232201e04c3fSmrg 232301e04c3fSmrg ir_rvalue *const ir = array_size->hir(& dummy_instructions, state); 232401e04c3fSmrg YYLTYPE loc = array_size->get_location(); 232501e04c3fSmrg 232601e04c3fSmrg if (ir == NULL) { 232701e04c3fSmrg _mesa_glsl_error(& loc, state, 232801e04c3fSmrg "array size could not be resolved"); 232901e04c3fSmrg return 0; 233001e04c3fSmrg } 233101e04c3fSmrg 23327ec681f3Smrg if (!ir->type->is_integer_32()) { 233301e04c3fSmrg _mesa_glsl_error(& loc, state, 233401e04c3fSmrg "array size must be integer type"); 233501e04c3fSmrg return 0; 233601e04c3fSmrg } 233701e04c3fSmrg 233801e04c3fSmrg if (!ir->type->is_scalar()) { 233901e04c3fSmrg _mesa_glsl_error(& loc, state, 234001e04c3fSmrg "array size must be scalar type"); 234101e04c3fSmrg return 0; 234201e04c3fSmrg } 234301e04c3fSmrg 234401e04c3fSmrg ir_constant *const size = ir->constant_expression_value(mem_ctx); 234501e04c3fSmrg if (size == NULL || 234601e04c3fSmrg (state->is_version(120, 300) && 234701e04c3fSmrg array_size->has_sequence_subexpression())) { 234801e04c3fSmrg _mesa_glsl_error(& loc, state, "array size must be a " 234901e04c3fSmrg "constant valued expression"); 235001e04c3fSmrg return 0; 235101e04c3fSmrg } 235201e04c3fSmrg 235301e04c3fSmrg if (size->value.i[0] <= 0) { 235401e04c3fSmrg _mesa_glsl_error(& loc, state, "array size must be > 0"); 235501e04c3fSmrg return 0; 235601e04c3fSmrg } 235701e04c3fSmrg 235801e04c3fSmrg assert(size->type == ir->type); 235901e04c3fSmrg 236001e04c3fSmrg /* If the array size is const (and we've verified that 236101e04c3fSmrg * it is) then no instructions should have been emitted 236201e04c3fSmrg * when we converted it to HIR. If they were emitted, 236301e04c3fSmrg * then either the array size isn't const after all, or 236401e04c3fSmrg * we are emitting unnecessary instructions. 236501e04c3fSmrg */ 236601e04c3fSmrg assert(dummy_instructions.is_empty()); 236701e04c3fSmrg 236801e04c3fSmrg return size->value.u[0]; 236901e04c3fSmrg} 237001e04c3fSmrg 237101e04c3fSmrgstatic const glsl_type * 237201e04c3fSmrgprocess_array_type(YYLTYPE *loc, const glsl_type *base, 237301e04c3fSmrg ast_array_specifier *array_specifier, 237401e04c3fSmrg struct _mesa_glsl_parse_state *state) 237501e04c3fSmrg{ 237601e04c3fSmrg const glsl_type *array_type = base; 237701e04c3fSmrg 237801e04c3fSmrg if (array_specifier != NULL) { 237901e04c3fSmrg if (base->is_array()) { 238001e04c3fSmrg 238101e04c3fSmrg /* From page 19 (page 25) of the GLSL 1.20 spec: 238201e04c3fSmrg * 238301e04c3fSmrg * "Only one-dimensional arrays may be declared." 238401e04c3fSmrg */ 238501e04c3fSmrg if (!state->check_arrays_of_arrays_allowed(loc)) { 238601e04c3fSmrg return glsl_type::error_type; 238701e04c3fSmrg } 238801e04c3fSmrg } 238901e04c3fSmrg 239001e04c3fSmrg for (exec_node *node = array_specifier->array_dimensions.get_tail_raw(); 239101e04c3fSmrg !node->is_head_sentinel(); node = node->prev) { 239201e04c3fSmrg unsigned array_size = process_array_size(node, state); 239301e04c3fSmrg array_type = glsl_type::get_array_instance(array_type, array_size); 239401e04c3fSmrg } 239501e04c3fSmrg } 239601e04c3fSmrg 239701e04c3fSmrg return array_type; 239801e04c3fSmrg} 239901e04c3fSmrg 240001e04c3fSmrgstatic bool 240101e04c3fSmrgprecision_qualifier_allowed(const glsl_type *type) 240201e04c3fSmrg{ 240301e04c3fSmrg /* Precision qualifiers apply to floating point, integer and opaque 240401e04c3fSmrg * types. 240501e04c3fSmrg * 240601e04c3fSmrg * Section 4.5.2 (Precision Qualifiers) of the GLSL 1.30 spec says: 240701e04c3fSmrg * "Any floating point or any integer declaration can have the type 240801e04c3fSmrg * preceded by one of these precision qualifiers [...] Literal 240901e04c3fSmrg * constants do not have precision qualifiers. Neither do Boolean 241001e04c3fSmrg * variables. 241101e04c3fSmrg * 241201e04c3fSmrg * Section 4.5 (Precision and Precision Qualifiers) of the GLSL 1.30 241301e04c3fSmrg * spec also says: 241401e04c3fSmrg * 241501e04c3fSmrg * "Precision qualifiers are added for code portability with OpenGL 241601e04c3fSmrg * ES, not for functionality. They have the same syntax as in OpenGL 241701e04c3fSmrg * ES." 241801e04c3fSmrg * 241901e04c3fSmrg * Section 8 (Built-In Functions) of the GLSL ES 1.00 spec says: 242001e04c3fSmrg * 242101e04c3fSmrg * "uniform lowp sampler2D sampler; 242201e04c3fSmrg * highp vec2 coord; 242301e04c3fSmrg * ... 242401e04c3fSmrg * lowp vec4 col = texture2D (sampler, coord); 242501e04c3fSmrg * // texture2D returns lowp" 242601e04c3fSmrg * 242701e04c3fSmrg * From this, we infer that GLSL 1.30 (and later) should allow precision 242801e04c3fSmrg * qualifiers on sampler types just like float and integer types. 242901e04c3fSmrg */ 243001e04c3fSmrg const glsl_type *const t = type->without_array(); 243101e04c3fSmrg 24327ec681f3Smrg return (t->is_float() || t->is_integer_32() || t->contains_opaque()) && 2433ed98bd31Smaya !t->is_struct(); 243401e04c3fSmrg} 243501e04c3fSmrg 243601e04c3fSmrgconst glsl_type * 243701e04c3fSmrgast_type_specifier::glsl_type(const char **name, 243801e04c3fSmrg struct _mesa_glsl_parse_state *state) const 243901e04c3fSmrg{ 244001e04c3fSmrg const struct glsl_type *type; 244101e04c3fSmrg 244201e04c3fSmrg if (this->type != NULL) 244301e04c3fSmrg type = this->type; 244401e04c3fSmrg else if (structure) 244501e04c3fSmrg type = structure->type; 244601e04c3fSmrg else 244701e04c3fSmrg type = state->symbols->get_type(this->type_name); 244801e04c3fSmrg *name = this->type_name; 244901e04c3fSmrg 245001e04c3fSmrg YYLTYPE loc = this->get_location(); 245101e04c3fSmrg type = process_array_type(&loc, type, this->array_specifier, state); 245201e04c3fSmrg 245301e04c3fSmrg return type; 245401e04c3fSmrg} 245501e04c3fSmrg 245601e04c3fSmrg/** 245701e04c3fSmrg * From the OpenGL ES 3.0 spec, 4.5.4 Default Precision Qualifiers: 245801e04c3fSmrg * 245901e04c3fSmrg * "The precision statement 246001e04c3fSmrg * 246101e04c3fSmrg * precision precision-qualifier type; 246201e04c3fSmrg * 246301e04c3fSmrg * can be used to establish a default precision qualifier. The type field can 246401e04c3fSmrg * be either int or float or any of the sampler types, (...) If type is float, 246501e04c3fSmrg * the directive applies to non-precision-qualified floating point type 246601e04c3fSmrg * (scalar, vector, and matrix) declarations. If type is int, the directive 246701e04c3fSmrg * applies to all non-precision-qualified integer type (scalar, vector, signed, 246801e04c3fSmrg * and unsigned) declarations." 246901e04c3fSmrg * 247001e04c3fSmrg * We use the symbol table to keep the values of the default precisions for 247101e04c3fSmrg * each 'type' in each scope and we use the 'type' string from the precision 247201e04c3fSmrg * statement as key in the symbol table. When we want to retrieve the default 247301e04c3fSmrg * precision associated with a given glsl_type we need to know the type string 247401e04c3fSmrg * associated with it. This is what this function returns. 247501e04c3fSmrg */ 247601e04c3fSmrgstatic const char * 247701e04c3fSmrgget_type_name_for_precision_qualifier(const glsl_type *type) 247801e04c3fSmrg{ 247901e04c3fSmrg switch (type->base_type) { 248001e04c3fSmrg case GLSL_TYPE_FLOAT: 248101e04c3fSmrg return "float"; 248201e04c3fSmrg case GLSL_TYPE_UINT: 248301e04c3fSmrg case GLSL_TYPE_INT: 248401e04c3fSmrg return "int"; 248501e04c3fSmrg case GLSL_TYPE_ATOMIC_UINT: 248601e04c3fSmrg return "atomic_uint"; 248701e04c3fSmrg case GLSL_TYPE_IMAGE: 24887ec681f3Smrg FALLTHROUGH; 248901e04c3fSmrg case GLSL_TYPE_SAMPLER: { 249001e04c3fSmrg const unsigned type_idx = 249101e04c3fSmrg type->sampler_array + 2 * type->sampler_shadow; 249201e04c3fSmrg const unsigned offset = type->is_sampler() ? 0 : 4; 249301e04c3fSmrg assert(type_idx < 4); 249401e04c3fSmrg switch (type->sampled_type) { 249501e04c3fSmrg case GLSL_TYPE_FLOAT: 249601e04c3fSmrg switch (type->sampler_dimensionality) { 249701e04c3fSmrg case GLSL_SAMPLER_DIM_1D: { 249801e04c3fSmrg assert(type->is_sampler()); 249901e04c3fSmrg static const char *const names[4] = { 250001e04c3fSmrg "sampler1D", "sampler1DArray", 250101e04c3fSmrg "sampler1DShadow", "sampler1DArrayShadow" 250201e04c3fSmrg }; 250301e04c3fSmrg return names[type_idx]; 250401e04c3fSmrg } 250501e04c3fSmrg case GLSL_SAMPLER_DIM_2D: { 250601e04c3fSmrg static const char *const names[8] = { 250701e04c3fSmrg "sampler2D", "sampler2DArray", 250801e04c3fSmrg "sampler2DShadow", "sampler2DArrayShadow", 250901e04c3fSmrg "image2D", "image2DArray", NULL, NULL 251001e04c3fSmrg }; 251101e04c3fSmrg return names[offset + type_idx]; 251201e04c3fSmrg } 251301e04c3fSmrg case GLSL_SAMPLER_DIM_3D: { 251401e04c3fSmrg static const char *const names[8] = { 251501e04c3fSmrg "sampler3D", NULL, NULL, NULL, 251601e04c3fSmrg "image3D", NULL, NULL, NULL 251701e04c3fSmrg }; 251801e04c3fSmrg return names[offset + type_idx]; 251901e04c3fSmrg } 252001e04c3fSmrg case GLSL_SAMPLER_DIM_CUBE: { 252101e04c3fSmrg static const char *const names[8] = { 252201e04c3fSmrg "samplerCube", "samplerCubeArray", 252301e04c3fSmrg "samplerCubeShadow", "samplerCubeArrayShadow", 252401e04c3fSmrg "imageCube", NULL, NULL, NULL 252501e04c3fSmrg }; 252601e04c3fSmrg return names[offset + type_idx]; 252701e04c3fSmrg } 252801e04c3fSmrg case GLSL_SAMPLER_DIM_MS: { 252901e04c3fSmrg assert(type->is_sampler()); 253001e04c3fSmrg static const char *const names[4] = { 253101e04c3fSmrg "sampler2DMS", "sampler2DMSArray", NULL, NULL 253201e04c3fSmrg }; 253301e04c3fSmrg return names[type_idx]; 253401e04c3fSmrg } 253501e04c3fSmrg case GLSL_SAMPLER_DIM_RECT: { 253601e04c3fSmrg assert(type->is_sampler()); 253701e04c3fSmrg static const char *const names[4] = { 253801e04c3fSmrg "samplerRect", NULL, "samplerRectShadow", NULL 253901e04c3fSmrg }; 254001e04c3fSmrg return names[type_idx]; 254101e04c3fSmrg } 254201e04c3fSmrg case GLSL_SAMPLER_DIM_BUF: { 254301e04c3fSmrg static const char *const names[8] = { 254401e04c3fSmrg "samplerBuffer", NULL, NULL, NULL, 254501e04c3fSmrg "imageBuffer", NULL, NULL, NULL 254601e04c3fSmrg }; 254701e04c3fSmrg return names[offset + type_idx]; 254801e04c3fSmrg } 254901e04c3fSmrg case GLSL_SAMPLER_DIM_EXTERNAL: { 255001e04c3fSmrg assert(type->is_sampler()); 255101e04c3fSmrg static const char *const names[4] = { 255201e04c3fSmrg "samplerExternalOES", NULL, NULL, NULL 255301e04c3fSmrg }; 255401e04c3fSmrg return names[type_idx]; 255501e04c3fSmrg } 255601e04c3fSmrg default: 255701e04c3fSmrg unreachable("Unsupported sampler/image dimensionality"); 255801e04c3fSmrg } /* sampler/image float dimensionality */ 255901e04c3fSmrg break; 256001e04c3fSmrg case GLSL_TYPE_INT: 256101e04c3fSmrg switch (type->sampler_dimensionality) { 256201e04c3fSmrg case GLSL_SAMPLER_DIM_1D: { 256301e04c3fSmrg assert(type->is_sampler()); 256401e04c3fSmrg static const char *const names[4] = { 256501e04c3fSmrg "isampler1D", "isampler1DArray", NULL, NULL 256601e04c3fSmrg }; 256701e04c3fSmrg return names[type_idx]; 256801e04c3fSmrg } 256901e04c3fSmrg case GLSL_SAMPLER_DIM_2D: { 257001e04c3fSmrg static const char *const names[8] = { 257101e04c3fSmrg "isampler2D", "isampler2DArray", NULL, NULL, 257201e04c3fSmrg "iimage2D", "iimage2DArray", NULL, NULL 257301e04c3fSmrg }; 257401e04c3fSmrg return names[offset + type_idx]; 257501e04c3fSmrg } 257601e04c3fSmrg case GLSL_SAMPLER_DIM_3D: { 257701e04c3fSmrg static const char *const names[8] = { 257801e04c3fSmrg "isampler3D", NULL, NULL, NULL, 257901e04c3fSmrg "iimage3D", NULL, NULL, NULL 258001e04c3fSmrg }; 258101e04c3fSmrg return names[offset + type_idx]; 258201e04c3fSmrg } 258301e04c3fSmrg case GLSL_SAMPLER_DIM_CUBE: { 258401e04c3fSmrg static const char *const names[8] = { 258501e04c3fSmrg "isamplerCube", "isamplerCubeArray", NULL, NULL, 258601e04c3fSmrg "iimageCube", NULL, NULL, NULL 258701e04c3fSmrg }; 258801e04c3fSmrg return names[offset + type_idx]; 258901e04c3fSmrg } 259001e04c3fSmrg case GLSL_SAMPLER_DIM_MS: { 259101e04c3fSmrg assert(type->is_sampler()); 259201e04c3fSmrg static const char *const names[4] = { 259301e04c3fSmrg "isampler2DMS", "isampler2DMSArray", NULL, NULL 259401e04c3fSmrg }; 259501e04c3fSmrg return names[type_idx]; 259601e04c3fSmrg } 259701e04c3fSmrg case GLSL_SAMPLER_DIM_RECT: { 259801e04c3fSmrg assert(type->is_sampler()); 259901e04c3fSmrg static const char *const names[4] = { 260001e04c3fSmrg "isamplerRect", NULL, "isamplerRectShadow", NULL 260101e04c3fSmrg }; 260201e04c3fSmrg return names[type_idx]; 260301e04c3fSmrg } 260401e04c3fSmrg case GLSL_SAMPLER_DIM_BUF: { 260501e04c3fSmrg static const char *const names[8] = { 260601e04c3fSmrg "isamplerBuffer", NULL, NULL, NULL, 260701e04c3fSmrg "iimageBuffer", NULL, NULL, NULL 260801e04c3fSmrg }; 260901e04c3fSmrg return names[offset + type_idx]; 261001e04c3fSmrg } 261101e04c3fSmrg default: 261201e04c3fSmrg unreachable("Unsupported isampler/iimage dimensionality"); 261301e04c3fSmrg } /* sampler/image int dimensionality */ 261401e04c3fSmrg break; 261501e04c3fSmrg case GLSL_TYPE_UINT: 261601e04c3fSmrg switch (type->sampler_dimensionality) { 261701e04c3fSmrg case GLSL_SAMPLER_DIM_1D: { 261801e04c3fSmrg assert(type->is_sampler()); 261901e04c3fSmrg static const char *const names[4] = { 262001e04c3fSmrg "usampler1D", "usampler1DArray", NULL, NULL 262101e04c3fSmrg }; 262201e04c3fSmrg return names[type_idx]; 262301e04c3fSmrg } 262401e04c3fSmrg case GLSL_SAMPLER_DIM_2D: { 262501e04c3fSmrg static const char *const names[8] = { 262601e04c3fSmrg "usampler2D", "usampler2DArray", NULL, NULL, 262701e04c3fSmrg "uimage2D", "uimage2DArray", NULL, NULL 262801e04c3fSmrg }; 262901e04c3fSmrg return names[offset + type_idx]; 263001e04c3fSmrg } 263101e04c3fSmrg case GLSL_SAMPLER_DIM_3D: { 263201e04c3fSmrg static const char *const names[8] = { 263301e04c3fSmrg "usampler3D", NULL, NULL, NULL, 263401e04c3fSmrg "uimage3D", NULL, NULL, NULL 263501e04c3fSmrg }; 263601e04c3fSmrg return names[offset + type_idx]; 263701e04c3fSmrg } 263801e04c3fSmrg case GLSL_SAMPLER_DIM_CUBE: { 263901e04c3fSmrg static const char *const names[8] = { 264001e04c3fSmrg "usamplerCube", "usamplerCubeArray", NULL, NULL, 264101e04c3fSmrg "uimageCube", NULL, NULL, NULL 264201e04c3fSmrg }; 264301e04c3fSmrg return names[offset + type_idx]; 264401e04c3fSmrg } 264501e04c3fSmrg case GLSL_SAMPLER_DIM_MS: { 264601e04c3fSmrg assert(type->is_sampler()); 264701e04c3fSmrg static const char *const names[4] = { 264801e04c3fSmrg "usampler2DMS", "usampler2DMSArray", NULL, NULL 264901e04c3fSmrg }; 265001e04c3fSmrg return names[type_idx]; 265101e04c3fSmrg } 265201e04c3fSmrg case GLSL_SAMPLER_DIM_RECT: { 265301e04c3fSmrg assert(type->is_sampler()); 265401e04c3fSmrg static const char *const names[4] = { 265501e04c3fSmrg "usamplerRect", NULL, "usamplerRectShadow", NULL 265601e04c3fSmrg }; 265701e04c3fSmrg return names[type_idx]; 265801e04c3fSmrg } 265901e04c3fSmrg case GLSL_SAMPLER_DIM_BUF: { 266001e04c3fSmrg static const char *const names[8] = { 266101e04c3fSmrg "usamplerBuffer", NULL, NULL, NULL, 266201e04c3fSmrg "uimageBuffer", NULL, NULL, NULL 266301e04c3fSmrg }; 266401e04c3fSmrg return names[offset + type_idx]; 266501e04c3fSmrg } 266601e04c3fSmrg default: 266701e04c3fSmrg unreachable("Unsupported usampler/uimage dimensionality"); 266801e04c3fSmrg } /* sampler/image uint dimensionality */ 266901e04c3fSmrg break; 267001e04c3fSmrg default: 267101e04c3fSmrg unreachable("Unsupported sampler/image type"); 267201e04c3fSmrg } /* sampler/image type */ 267301e04c3fSmrg break; 267401e04c3fSmrg } /* GLSL_TYPE_SAMPLER/GLSL_TYPE_IMAGE */ 267501e04c3fSmrg break; 267601e04c3fSmrg default: 267701e04c3fSmrg unreachable("Unsupported type"); 267801e04c3fSmrg } /* base type */ 267901e04c3fSmrg} 268001e04c3fSmrg 268101e04c3fSmrgstatic unsigned 268201e04c3fSmrgselect_gles_precision(unsigned qual_precision, 268301e04c3fSmrg const glsl_type *type, 268401e04c3fSmrg struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 268501e04c3fSmrg{ 268601e04c3fSmrg /* Precision qualifiers do not have any meaning in Desktop GLSL. 268701e04c3fSmrg * In GLES we take the precision from the type qualifier if present, 268801e04c3fSmrg * otherwise, if the type of the variable allows precision qualifiers at 268901e04c3fSmrg * all, we look for the default precision qualifier for that type in the 269001e04c3fSmrg * current scope. 269101e04c3fSmrg */ 269201e04c3fSmrg assert(state->es_shader); 269301e04c3fSmrg 269401e04c3fSmrg unsigned precision = GLSL_PRECISION_NONE; 269501e04c3fSmrg if (qual_precision) { 269601e04c3fSmrg precision = qual_precision; 269701e04c3fSmrg } else if (precision_qualifier_allowed(type)) { 269801e04c3fSmrg const char *type_name = 269901e04c3fSmrg get_type_name_for_precision_qualifier(type->without_array()); 270001e04c3fSmrg assert(type_name != NULL); 270101e04c3fSmrg 270201e04c3fSmrg precision = 270301e04c3fSmrg state->symbols->get_default_precision_qualifier(type_name); 270401e04c3fSmrg if (precision == ast_precision_none) { 270501e04c3fSmrg _mesa_glsl_error(loc, state, 270601e04c3fSmrg "No precision specified in this scope for type `%s'", 270701e04c3fSmrg type->name); 270801e04c3fSmrg } 270901e04c3fSmrg } 271001e04c3fSmrg 271101e04c3fSmrg 271201e04c3fSmrg /* Section 4.1.7.3 (Atomic Counters) of the GLSL ES 3.10 spec says: 271301e04c3fSmrg * 271401e04c3fSmrg * "The default precision of all atomic types is highp. It is an error to 271501e04c3fSmrg * declare an atomic type with a different precision or to specify the 271601e04c3fSmrg * default precision for an atomic type to be lowp or mediump." 271701e04c3fSmrg */ 271801e04c3fSmrg if (type->is_atomic_uint() && precision != ast_precision_high) { 271901e04c3fSmrg _mesa_glsl_error(loc, state, 272001e04c3fSmrg "atomic_uint can only have highp precision qualifier"); 272101e04c3fSmrg } 272201e04c3fSmrg 272301e04c3fSmrg return precision; 272401e04c3fSmrg} 272501e04c3fSmrg 272601e04c3fSmrgconst glsl_type * 272701e04c3fSmrgast_fully_specified_type::glsl_type(const char **name, 272801e04c3fSmrg struct _mesa_glsl_parse_state *state) const 272901e04c3fSmrg{ 273001e04c3fSmrg return this->specifier->glsl_type(name, state); 273101e04c3fSmrg} 273201e04c3fSmrg 273301e04c3fSmrg/** 273401e04c3fSmrg * Determine whether a toplevel variable declaration declares a varying. This 273501e04c3fSmrg * function operates by examining the variable's mode and the shader target, 273601e04c3fSmrg * so it correctly identifies linkage variables regardless of whether they are 273701e04c3fSmrg * declared using the deprecated "varying" syntax or the new "in/out" syntax. 273801e04c3fSmrg * 273901e04c3fSmrg * Passing a non-toplevel variable declaration (e.g. a function parameter) to 274001e04c3fSmrg * this function will produce undefined results. 274101e04c3fSmrg */ 274201e04c3fSmrgstatic bool 274301e04c3fSmrgis_varying_var(ir_variable *var, gl_shader_stage target) 274401e04c3fSmrg{ 274501e04c3fSmrg switch (target) { 274601e04c3fSmrg case MESA_SHADER_VERTEX: 274701e04c3fSmrg return var->data.mode == ir_var_shader_out; 274801e04c3fSmrg case MESA_SHADER_FRAGMENT: 27497ec681f3Smrg return var->data.mode == ir_var_shader_in || 27507ec681f3Smrg (var->data.mode == ir_var_system_value && 27517ec681f3Smrg var->data.location == SYSTEM_VALUE_FRAG_COORD); 275201e04c3fSmrg default: 275301e04c3fSmrg return var->data.mode == ir_var_shader_out || var->data.mode == ir_var_shader_in; 275401e04c3fSmrg } 275501e04c3fSmrg} 275601e04c3fSmrg 275701e04c3fSmrgstatic bool 275801e04c3fSmrgis_allowed_invariant(ir_variable *var, struct _mesa_glsl_parse_state *state) 275901e04c3fSmrg{ 276001e04c3fSmrg if (is_varying_var(var, state->stage)) 276101e04c3fSmrg return true; 276201e04c3fSmrg 276301e04c3fSmrg /* From Section 4.6.1 ("The Invariant Qualifier") GLSL 1.20 spec: 276401e04c3fSmrg * "Only variables output from a vertex shader can be candidates 276501e04c3fSmrg * for invariance". 276601e04c3fSmrg */ 2767ed98bd31Smaya if (!state->is_version(130, 100)) 276801e04c3fSmrg return false; 276901e04c3fSmrg 277001e04c3fSmrg /* 277101e04c3fSmrg * Later specs remove this language - so allowed invariant 277201e04c3fSmrg * on fragment shader outputs as well. 277301e04c3fSmrg */ 277401e04c3fSmrg if (state->stage == MESA_SHADER_FRAGMENT && 277501e04c3fSmrg var->data.mode == ir_var_shader_out) 277601e04c3fSmrg return true; 277701e04c3fSmrg return false; 277801e04c3fSmrg} 277901e04c3fSmrg 27807ec681f3Smrgstatic void 27817ec681f3Smrgvalidate_component_layout_for_type(struct _mesa_glsl_parse_state *state, 27827ec681f3Smrg YYLTYPE *loc, const glsl_type *type, 27837ec681f3Smrg unsigned qual_component) 27847ec681f3Smrg{ 27857ec681f3Smrg type = type->without_array(); 27867ec681f3Smrg unsigned components = type->component_slots(); 27877ec681f3Smrg 27887ec681f3Smrg if (type->is_matrix() || type->is_struct()) { 27897ec681f3Smrg _mesa_glsl_error(loc, state, "component layout qualifier " 27907ec681f3Smrg "cannot be applied to a matrix, a structure, " 27917ec681f3Smrg "a block, or an array containing any of these."); 27927ec681f3Smrg } else if (components > 4 && type->is_64bit()) { 27937ec681f3Smrg _mesa_glsl_error(loc, state, "component layout qualifier " 27947ec681f3Smrg "cannot be applied to dvec%u.", 27957ec681f3Smrg components / 2); 27967ec681f3Smrg } else if (qual_component != 0 && (qual_component + components - 1) > 3) { 27977ec681f3Smrg _mesa_glsl_error(loc, state, "component overflow (%u > 3)", 27987ec681f3Smrg (qual_component + components - 1)); 27997ec681f3Smrg } else if (qual_component == 1 && type->is_64bit()) { 28007ec681f3Smrg /* We don't bother checking for 3 as it should be caught by the 28017ec681f3Smrg * overflow check above. 28027ec681f3Smrg */ 28037ec681f3Smrg _mesa_glsl_error(loc, state, "doubles cannot begin at component 1 or 3"); 28047ec681f3Smrg } 28057ec681f3Smrg} 28067ec681f3Smrg 280701e04c3fSmrg/** 280801e04c3fSmrg * Matrix layout qualifiers are only allowed on certain types 280901e04c3fSmrg */ 281001e04c3fSmrgstatic void 281101e04c3fSmrgvalidate_matrix_layout_for_type(struct _mesa_glsl_parse_state *state, 281201e04c3fSmrg YYLTYPE *loc, 281301e04c3fSmrg const glsl_type *type, 281401e04c3fSmrg ir_variable *var) 281501e04c3fSmrg{ 281601e04c3fSmrg if (var && !var->is_in_buffer_block()) { 281701e04c3fSmrg /* Layout qualifiers may only apply to interface blocks and fields in 281801e04c3fSmrg * them. 281901e04c3fSmrg */ 282001e04c3fSmrg _mesa_glsl_error(loc, state, 282101e04c3fSmrg "uniform block layout qualifiers row_major and " 282201e04c3fSmrg "column_major may not be applied to variables " 282301e04c3fSmrg "outside of uniform blocks"); 282401e04c3fSmrg } else if (!type->without_array()->is_matrix()) { 282501e04c3fSmrg /* The OpenGL ES 3.0 conformance tests did not originally allow 282601e04c3fSmrg * matrix layout qualifiers on non-matrices. However, the OpenGL 282701e04c3fSmrg * 4.4 and OpenGL ES 3.0 (revision TBD) specifications were 282801e04c3fSmrg * amended to specifically allow these layouts on all types. Emit 282901e04c3fSmrg * a warning so that people know their code may not be portable. 283001e04c3fSmrg */ 283101e04c3fSmrg _mesa_glsl_warning(loc, state, 283201e04c3fSmrg "uniform block layout qualifiers row_major and " 283301e04c3fSmrg "column_major applied to non-matrix types may " 283401e04c3fSmrg "be rejected by older compilers"); 283501e04c3fSmrg } 283601e04c3fSmrg} 283701e04c3fSmrg 283801e04c3fSmrgstatic bool 283901e04c3fSmrgvalidate_xfb_buffer_qualifier(YYLTYPE *loc, 284001e04c3fSmrg struct _mesa_glsl_parse_state *state, 284101e04c3fSmrg unsigned xfb_buffer) { 284201e04c3fSmrg if (xfb_buffer >= state->Const.MaxTransformFeedbackBuffers) { 284301e04c3fSmrg _mesa_glsl_error(loc, state, 284401e04c3fSmrg "invalid xfb_buffer specified %d is larger than " 284501e04c3fSmrg "MAX_TRANSFORM_FEEDBACK_BUFFERS - 1 (%d).", 284601e04c3fSmrg xfb_buffer, 284701e04c3fSmrg state->Const.MaxTransformFeedbackBuffers - 1); 284801e04c3fSmrg return false; 284901e04c3fSmrg } 285001e04c3fSmrg 285101e04c3fSmrg return true; 285201e04c3fSmrg} 285301e04c3fSmrg 285401e04c3fSmrg/* From the ARB_enhanced_layouts spec: 285501e04c3fSmrg * 285601e04c3fSmrg * "Variables and block members qualified with *xfb_offset* can be 285701e04c3fSmrg * scalars, vectors, matrices, structures, and (sized) arrays of these. 285801e04c3fSmrg * The offset must be a multiple of the size of the first component of 285901e04c3fSmrg * the first qualified variable or block member, or a compile-time error 286001e04c3fSmrg * results. Further, if applied to an aggregate containing a double, 286101e04c3fSmrg * the offset must also be a multiple of 8, and the space taken in the 286201e04c3fSmrg * buffer will be a multiple of 8. 286301e04c3fSmrg */ 286401e04c3fSmrgstatic bool 286501e04c3fSmrgvalidate_xfb_offset_qualifier(YYLTYPE *loc, 286601e04c3fSmrg struct _mesa_glsl_parse_state *state, 286701e04c3fSmrg int xfb_offset, const glsl_type *type, 286801e04c3fSmrg unsigned component_size) { 286901e04c3fSmrg const glsl_type *t_without_array = type->without_array(); 287001e04c3fSmrg 287101e04c3fSmrg if (xfb_offset != -1 && type->is_unsized_array()) { 287201e04c3fSmrg _mesa_glsl_error(loc, state, 287301e04c3fSmrg "xfb_offset can't be used with unsized arrays."); 287401e04c3fSmrg return false; 287501e04c3fSmrg } 287601e04c3fSmrg 287701e04c3fSmrg /* Make sure nested structs don't contain unsized arrays, and validate 287801e04c3fSmrg * any xfb_offsets on interface members. 287901e04c3fSmrg */ 2880ed98bd31Smaya if (t_without_array->is_struct() || t_without_array->is_interface()) 288101e04c3fSmrg for (unsigned int i = 0; i < t_without_array->length; i++) { 288201e04c3fSmrg const glsl_type *member_t = t_without_array->fields.structure[i].type; 288301e04c3fSmrg 288401e04c3fSmrg /* When the interface block doesn't have an xfb_offset qualifier then 288501e04c3fSmrg * we apply the component size rules at the member level. 288601e04c3fSmrg */ 288701e04c3fSmrg if (xfb_offset == -1) 288801e04c3fSmrg component_size = member_t->contains_double() ? 8 : 4; 288901e04c3fSmrg 289001e04c3fSmrg int xfb_offset = t_without_array->fields.structure[i].offset; 289101e04c3fSmrg validate_xfb_offset_qualifier(loc, state, xfb_offset, member_t, 289201e04c3fSmrg component_size); 289301e04c3fSmrg } 289401e04c3fSmrg 289501e04c3fSmrg /* Nested structs or interface block without offset may not have had an 289601e04c3fSmrg * offset applied yet so return. 289701e04c3fSmrg */ 289801e04c3fSmrg if (xfb_offset == -1) { 289901e04c3fSmrg return true; 290001e04c3fSmrg } 290101e04c3fSmrg 290201e04c3fSmrg if (xfb_offset % component_size) { 290301e04c3fSmrg _mesa_glsl_error(loc, state, 290401e04c3fSmrg "invalid qualifier xfb_offset=%d must be a multiple " 290501e04c3fSmrg "of the first component size of the first qualified " 290601e04c3fSmrg "variable or block member. Or double if an aggregate " 290701e04c3fSmrg "that contains a double (%d).", 290801e04c3fSmrg xfb_offset, component_size); 290901e04c3fSmrg return false; 291001e04c3fSmrg } 291101e04c3fSmrg 291201e04c3fSmrg return true; 291301e04c3fSmrg} 291401e04c3fSmrg 291501e04c3fSmrgstatic bool 291601e04c3fSmrgvalidate_stream_qualifier(YYLTYPE *loc, struct _mesa_glsl_parse_state *state, 291701e04c3fSmrg unsigned stream) 291801e04c3fSmrg{ 291901e04c3fSmrg if (stream >= state->ctx->Const.MaxVertexStreams) { 292001e04c3fSmrg _mesa_glsl_error(loc, state, 292101e04c3fSmrg "invalid stream specified %d is larger than " 292201e04c3fSmrg "MAX_VERTEX_STREAMS - 1 (%d).", 292301e04c3fSmrg stream, state->ctx->Const.MaxVertexStreams - 1); 292401e04c3fSmrg return false; 292501e04c3fSmrg } 292601e04c3fSmrg 292701e04c3fSmrg return true; 292801e04c3fSmrg} 292901e04c3fSmrg 293001e04c3fSmrgstatic void 293101e04c3fSmrgapply_explicit_binding(struct _mesa_glsl_parse_state *state, 293201e04c3fSmrg YYLTYPE *loc, 293301e04c3fSmrg ir_variable *var, 293401e04c3fSmrg const glsl_type *type, 293501e04c3fSmrg const ast_type_qualifier *qual) 293601e04c3fSmrg{ 293701e04c3fSmrg if (!qual->flags.q.uniform && !qual->flags.q.buffer) { 293801e04c3fSmrg _mesa_glsl_error(loc, state, 293901e04c3fSmrg "the \"binding\" qualifier only applies to uniforms and " 294001e04c3fSmrg "shader storage buffer objects"); 294101e04c3fSmrg return; 294201e04c3fSmrg } 294301e04c3fSmrg 294401e04c3fSmrg unsigned qual_binding; 294501e04c3fSmrg if (!process_qualifier_constant(state, loc, "binding", qual->binding, 294601e04c3fSmrg &qual_binding)) { 294701e04c3fSmrg return; 294801e04c3fSmrg } 294901e04c3fSmrg 295001e04c3fSmrg const struct gl_context *const ctx = state->ctx; 295101e04c3fSmrg unsigned elements = type->is_array() ? type->arrays_of_arrays_size() : 1; 295201e04c3fSmrg unsigned max_index = qual_binding + elements - 1; 295301e04c3fSmrg const glsl_type *base_type = type->without_array(); 295401e04c3fSmrg 295501e04c3fSmrg if (base_type->is_interface()) { 295601e04c3fSmrg /* UBOs. From page 60 of the GLSL 4.20 specification: 295701e04c3fSmrg * "If the binding point for any uniform block instance is less than zero, 295801e04c3fSmrg * or greater than or equal to the implementation-dependent maximum 295901e04c3fSmrg * number of uniform buffer bindings, a compilation error will occur. 296001e04c3fSmrg * When the binding identifier is used with a uniform block instanced as 296101e04c3fSmrg * an array of size N, all elements of the array from binding through 296201e04c3fSmrg * binding + N – 1 must be within this range." 296301e04c3fSmrg * 296401e04c3fSmrg * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS. 296501e04c3fSmrg */ 296601e04c3fSmrg if (qual->flags.q.uniform && 296701e04c3fSmrg max_index >= ctx->Const.MaxUniformBufferBindings) { 296801e04c3fSmrg _mesa_glsl_error(loc, state, "layout(binding = %u) for %d UBOs exceeds " 296901e04c3fSmrg "the maximum number of UBO binding points (%d)", 297001e04c3fSmrg qual_binding, elements, 297101e04c3fSmrg ctx->Const.MaxUniformBufferBindings); 297201e04c3fSmrg return; 297301e04c3fSmrg } 297401e04c3fSmrg 297501e04c3fSmrg /* SSBOs. From page 67 of the GLSL 4.30 specification: 297601e04c3fSmrg * "If the binding point for any uniform or shader storage block instance 297701e04c3fSmrg * is less than zero, or greater than or equal to the 297801e04c3fSmrg * implementation-dependent maximum number of uniform buffer bindings, a 297901e04c3fSmrg * compile-time error will occur. When the binding identifier is used 298001e04c3fSmrg * with a uniform or shader storage block instanced as an array of size 298101e04c3fSmrg * N, all elements of the array from binding through binding + N – 1 must 298201e04c3fSmrg * be within this range." 298301e04c3fSmrg */ 298401e04c3fSmrg if (qual->flags.q.buffer && 298501e04c3fSmrg max_index >= ctx->Const.MaxShaderStorageBufferBindings) { 298601e04c3fSmrg _mesa_glsl_error(loc, state, "layout(binding = %u) for %d SSBOs exceeds " 298701e04c3fSmrg "the maximum number of SSBO binding points (%d)", 298801e04c3fSmrg qual_binding, elements, 298901e04c3fSmrg ctx->Const.MaxShaderStorageBufferBindings); 299001e04c3fSmrg return; 299101e04c3fSmrg } 299201e04c3fSmrg } else if (base_type->is_sampler()) { 299301e04c3fSmrg /* Samplers. From page 63 of the GLSL 4.20 specification: 299401e04c3fSmrg * "If the binding is less than zero, or greater than or equal to the 299501e04c3fSmrg * implementation-dependent maximum supported number of units, a 299601e04c3fSmrg * compilation error will occur. When the binding identifier is used 299701e04c3fSmrg * with an array of size N, all elements of the array from binding 299801e04c3fSmrg * through binding + N - 1 must be within this range." 299901e04c3fSmrg */ 300001e04c3fSmrg unsigned limit = ctx->Const.MaxCombinedTextureImageUnits; 300101e04c3fSmrg 300201e04c3fSmrg if (max_index >= limit) { 300301e04c3fSmrg _mesa_glsl_error(loc, state, "layout(binding = %d) for %d samplers " 300401e04c3fSmrg "exceeds the maximum number of texture image units " 300501e04c3fSmrg "(%u)", qual_binding, elements, limit); 300601e04c3fSmrg 300701e04c3fSmrg return; 300801e04c3fSmrg } 300901e04c3fSmrg } else if (base_type->contains_atomic()) { 301001e04c3fSmrg assert(ctx->Const.MaxAtomicBufferBindings <= MAX_COMBINED_ATOMIC_BUFFERS); 301101e04c3fSmrg if (qual_binding >= ctx->Const.MaxAtomicBufferBindings) { 301201e04c3fSmrg _mesa_glsl_error(loc, state, "layout(binding = %d) exceeds the " 301301e04c3fSmrg "maximum number of atomic counter buffer bindings " 301401e04c3fSmrg "(%u)", qual_binding, 301501e04c3fSmrg ctx->Const.MaxAtomicBufferBindings); 301601e04c3fSmrg 301701e04c3fSmrg return; 301801e04c3fSmrg } 301901e04c3fSmrg } else if ((state->is_version(420, 310) || 302001e04c3fSmrg state->ARB_shading_language_420pack_enable) && 302101e04c3fSmrg base_type->is_image()) { 302201e04c3fSmrg assert(ctx->Const.MaxImageUnits <= MAX_IMAGE_UNITS); 302301e04c3fSmrg if (max_index >= ctx->Const.MaxImageUnits) { 302401e04c3fSmrg _mesa_glsl_error(loc, state, "Image binding %d exceeds the " 302501e04c3fSmrg "maximum number of image units (%d)", max_index, 302601e04c3fSmrg ctx->Const.MaxImageUnits); 302701e04c3fSmrg return; 302801e04c3fSmrg } 302901e04c3fSmrg 303001e04c3fSmrg } else { 303101e04c3fSmrg _mesa_glsl_error(loc, state, 303201e04c3fSmrg "the \"binding\" qualifier only applies to uniform " 303301e04c3fSmrg "blocks, storage blocks, opaque variables, or arrays " 303401e04c3fSmrg "thereof"); 303501e04c3fSmrg return; 303601e04c3fSmrg } 303701e04c3fSmrg 303801e04c3fSmrg var->data.explicit_binding = true; 303901e04c3fSmrg var->data.binding = qual_binding; 304001e04c3fSmrg 304101e04c3fSmrg return; 304201e04c3fSmrg} 304301e04c3fSmrg 304401e04c3fSmrgstatic void 304501e04c3fSmrgvalidate_fragment_flat_interpolation_input(struct _mesa_glsl_parse_state *state, 304601e04c3fSmrg YYLTYPE *loc, 304701e04c3fSmrg const glsl_interp_mode interpolation, 304801e04c3fSmrg const struct glsl_type *var_type, 304901e04c3fSmrg ir_variable_mode mode) 305001e04c3fSmrg{ 305101e04c3fSmrg if (state->stage != MESA_SHADER_FRAGMENT || 305201e04c3fSmrg interpolation == INTERP_MODE_FLAT || 305301e04c3fSmrg mode != ir_var_shader_in) 305401e04c3fSmrg return; 305501e04c3fSmrg 305601e04c3fSmrg /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES, 305701e04c3fSmrg * so must integer vertex outputs. 305801e04c3fSmrg * 305901e04c3fSmrg * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec: 306001e04c3fSmrg * "Fragment shader inputs that are signed or unsigned integers or 306101e04c3fSmrg * integer vectors must be qualified with the interpolation qualifier 306201e04c3fSmrg * flat." 306301e04c3fSmrg * 306401e04c3fSmrg * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec: 306501e04c3fSmrg * "Fragment shader inputs that are, or contain, signed or unsigned 306601e04c3fSmrg * integers or integer vectors must be qualified with the 306701e04c3fSmrg * interpolation qualifier flat." 306801e04c3fSmrg * 306901e04c3fSmrg * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec: 307001e04c3fSmrg * "Vertex shader outputs that are, or contain, signed or unsigned 307101e04c3fSmrg * integers or integer vectors must be qualified with the 307201e04c3fSmrg * interpolation qualifier flat." 307301e04c3fSmrg * 307401e04c3fSmrg * Note that prior to GLSL 1.50, this requirement applied to vertex 307501e04c3fSmrg * outputs rather than fragment inputs. That creates problems in the 307601e04c3fSmrg * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all 307701e04c3fSmrg * desktop GL shaders. For GLSL ES shaders, we follow the spec and 307801e04c3fSmrg * apply the restriction to both vertex outputs and fragment inputs. 307901e04c3fSmrg * 308001e04c3fSmrg * Note also that the desktop GLSL specs are missing the text "or 308101e04c3fSmrg * contain"; this is presumably an oversight, since there is no 308201e04c3fSmrg * reasonable way to interpolate a fragment shader input that contains 308301e04c3fSmrg * an integer. See Khronos bug #15671. 308401e04c3fSmrg */ 3085ed98bd31Smaya if ((state->is_version(130, 300) || state->EXT_gpu_shader4_enable) 308601e04c3fSmrg && var_type->contains_integer()) { 308701e04c3fSmrg _mesa_glsl_error(loc, state, "if a fragment input is (or contains) " 308801e04c3fSmrg "an integer, then it must be qualified with 'flat'"); 308901e04c3fSmrg } 309001e04c3fSmrg 309101e04c3fSmrg /* Double fragment inputs must be qualified with 'flat'. 309201e04c3fSmrg * 309301e04c3fSmrg * From the "Overview" of the ARB_gpu_shader_fp64 extension spec: 309401e04c3fSmrg * "This extension does not support interpolation of double-precision 309501e04c3fSmrg * values; doubles used as fragment shader inputs must be qualified as 309601e04c3fSmrg * "flat"." 309701e04c3fSmrg * 309801e04c3fSmrg * From section 4.3.4 ("Inputs") of the GLSL 4.00 spec: 309901e04c3fSmrg * "Fragment shader inputs that are signed or unsigned integers, integer 310001e04c3fSmrg * vectors, or any double-precision floating-point type must be 310101e04c3fSmrg * qualified with the interpolation qualifier flat." 310201e04c3fSmrg * 310301e04c3fSmrg * Note that the GLSL specs are missing the text "or contain"; this is 310401e04c3fSmrg * presumably an oversight. See Khronos bug #15671. 310501e04c3fSmrg * 310601e04c3fSmrg * The 'double' type does not exist in GLSL ES so far. 310701e04c3fSmrg */ 310801e04c3fSmrg if (state->has_double() 310901e04c3fSmrg && var_type->contains_double()) { 311001e04c3fSmrg _mesa_glsl_error(loc, state, "if a fragment input is (or contains) " 311101e04c3fSmrg "a double, then it must be qualified with 'flat'"); 311201e04c3fSmrg } 311301e04c3fSmrg 311401e04c3fSmrg /* Bindless sampler/image fragment inputs must be qualified with 'flat'. 311501e04c3fSmrg * 311601e04c3fSmrg * From section 4.3.4 of the ARB_bindless_texture spec: 311701e04c3fSmrg * 311801e04c3fSmrg * "(modify last paragraph, p. 35, allowing samplers and images as 311901e04c3fSmrg * fragment shader inputs) ... Fragment inputs can only be signed and 312001e04c3fSmrg * unsigned integers and integer vectors, floating point scalars, 312101e04c3fSmrg * floating-point vectors, matrices, sampler and image types, or arrays 312201e04c3fSmrg * or structures of these. Fragment shader inputs that are signed or 312301e04c3fSmrg * unsigned integers, integer vectors, or any double-precision floating- 312401e04c3fSmrg * point type, or any sampler or image type must be qualified with the 312501e04c3fSmrg * interpolation qualifier "flat"." 312601e04c3fSmrg */ 312701e04c3fSmrg if (state->has_bindless() 312801e04c3fSmrg && (var_type->contains_sampler() || var_type->contains_image())) { 312901e04c3fSmrg _mesa_glsl_error(loc, state, "if a fragment input is (or contains) " 313001e04c3fSmrg "a bindless sampler (or image), then it must be " 313101e04c3fSmrg "qualified with 'flat'"); 313201e04c3fSmrg } 313301e04c3fSmrg} 313401e04c3fSmrg 313501e04c3fSmrgstatic void 313601e04c3fSmrgvalidate_interpolation_qualifier(struct _mesa_glsl_parse_state *state, 313701e04c3fSmrg YYLTYPE *loc, 313801e04c3fSmrg const glsl_interp_mode interpolation, 313901e04c3fSmrg const struct ast_type_qualifier *qual, 314001e04c3fSmrg const struct glsl_type *var_type, 314101e04c3fSmrg ir_variable_mode mode) 314201e04c3fSmrg{ 314301e04c3fSmrg /* Interpolation qualifiers can only apply to shader inputs or outputs, but 314401e04c3fSmrg * not to vertex shader inputs nor fragment shader outputs. 314501e04c3fSmrg * 314601e04c3fSmrg * From section 4.3 ("Storage Qualifiers") of the GLSL 1.30 spec: 314701e04c3fSmrg * "Outputs from a vertex shader (out) and inputs to a fragment 314801e04c3fSmrg * shader (in) can be further qualified with one or more of these 314901e04c3fSmrg * interpolation qualifiers" 315001e04c3fSmrg * ... 315101e04c3fSmrg * "These interpolation qualifiers may only precede the qualifiers in, 315201e04c3fSmrg * centroid in, out, or centroid out in a declaration. They do not apply 315301e04c3fSmrg * to the deprecated storage qualifiers varying or centroid 315401e04c3fSmrg * varying. They also do not apply to inputs into a vertex shader or 315501e04c3fSmrg * outputs from a fragment shader." 315601e04c3fSmrg * 315701e04c3fSmrg * From section 4.3 ("Storage Qualifiers") of the GLSL ES 3.00 spec: 315801e04c3fSmrg * "Outputs from a shader (out) and inputs to a shader (in) can be 315901e04c3fSmrg * further qualified with one of these interpolation qualifiers." 316001e04c3fSmrg * ... 316101e04c3fSmrg * "These interpolation qualifiers may only precede the qualifiers 316201e04c3fSmrg * in, centroid in, out, or centroid out in a declaration. They do 316301e04c3fSmrg * not apply to inputs into a vertex shader or outputs from a 316401e04c3fSmrg * fragment shader." 316501e04c3fSmrg */ 3166ed98bd31Smaya if ((state->is_version(130, 300) || state->EXT_gpu_shader4_enable) 316701e04c3fSmrg && interpolation != INTERP_MODE_NONE) { 316801e04c3fSmrg const char *i = interpolation_string(interpolation); 316901e04c3fSmrg if (mode != ir_var_shader_in && mode != ir_var_shader_out) 317001e04c3fSmrg _mesa_glsl_error(loc, state, 317101e04c3fSmrg "interpolation qualifier `%s' can only be applied to " 317201e04c3fSmrg "shader inputs or outputs.", i); 317301e04c3fSmrg 317401e04c3fSmrg switch (state->stage) { 317501e04c3fSmrg case MESA_SHADER_VERTEX: 317601e04c3fSmrg if (mode == ir_var_shader_in) { 317701e04c3fSmrg _mesa_glsl_error(loc, state, 317801e04c3fSmrg "interpolation qualifier '%s' cannot be applied to " 317901e04c3fSmrg "vertex shader inputs", i); 318001e04c3fSmrg } 318101e04c3fSmrg break; 318201e04c3fSmrg case MESA_SHADER_FRAGMENT: 318301e04c3fSmrg if (mode == ir_var_shader_out) { 318401e04c3fSmrg _mesa_glsl_error(loc, state, 318501e04c3fSmrg "interpolation qualifier '%s' cannot be applied to " 318601e04c3fSmrg "fragment shader outputs", i); 318701e04c3fSmrg } 318801e04c3fSmrg break; 318901e04c3fSmrg default: 319001e04c3fSmrg break; 319101e04c3fSmrg } 319201e04c3fSmrg } 319301e04c3fSmrg 319401e04c3fSmrg /* Interpolation qualifiers cannot be applied to 'centroid' and 319501e04c3fSmrg * 'centroid varying'. 319601e04c3fSmrg * 319701e04c3fSmrg * From section 4.3 ("Storage Qualifiers") of the GLSL 1.30 spec: 319801e04c3fSmrg * "interpolation qualifiers may only precede the qualifiers in, 319901e04c3fSmrg * centroid in, out, or centroid out in a declaration. They do not apply 320001e04c3fSmrg * to the deprecated storage qualifiers varying or centroid varying." 320101e04c3fSmrg * 320201e04c3fSmrg * These deprecated storage qualifiers do not exist in GLSL ES 3.00. 3203ed98bd31Smaya * 3204ed98bd31Smaya * GL_EXT_gpu_shader4 allows this. 320501e04c3fSmrg */ 3206ed98bd31Smaya if (state->is_version(130, 0) && !state->EXT_gpu_shader4_enable 320701e04c3fSmrg && interpolation != INTERP_MODE_NONE 320801e04c3fSmrg && qual->flags.q.varying) { 320901e04c3fSmrg 321001e04c3fSmrg const char *i = interpolation_string(interpolation); 321101e04c3fSmrg const char *s; 321201e04c3fSmrg if (qual->flags.q.centroid) 321301e04c3fSmrg s = "centroid varying"; 321401e04c3fSmrg else 321501e04c3fSmrg s = "varying"; 321601e04c3fSmrg 321701e04c3fSmrg _mesa_glsl_error(loc, state, 321801e04c3fSmrg "qualifier '%s' cannot be applied to the " 321901e04c3fSmrg "deprecated storage qualifier '%s'", i, s); 322001e04c3fSmrg } 322101e04c3fSmrg 322201e04c3fSmrg validate_fragment_flat_interpolation_input(state, loc, interpolation, 322301e04c3fSmrg var_type, mode); 322401e04c3fSmrg} 322501e04c3fSmrg 322601e04c3fSmrgstatic glsl_interp_mode 322701e04c3fSmrginterpret_interpolation_qualifier(const struct ast_type_qualifier *qual, 322801e04c3fSmrg const struct glsl_type *var_type, 322901e04c3fSmrg ir_variable_mode mode, 323001e04c3fSmrg struct _mesa_glsl_parse_state *state, 323101e04c3fSmrg YYLTYPE *loc) 323201e04c3fSmrg{ 323301e04c3fSmrg glsl_interp_mode interpolation; 323401e04c3fSmrg if (qual->flags.q.flat) 323501e04c3fSmrg interpolation = INTERP_MODE_FLAT; 323601e04c3fSmrg else if (qual->flags.q.noperspective) 323701e04c3fSmrg interpolation = INTERP_MODE_NOPERSPECTIVE; 323801e04c3fSmrg else if (qual->flags.q.smooth) 323901e04c3fSmrg interpolation = INTERP_MODE_SMOOTH; 324001e04c3fSmrg else 324101e04c3fSmrg interpolation = INTERP_MODE_NONE; 324201e04c3fSmrg 324301e04c3fSmrg validate_interpolation_qualifier(state, loc, 324401e04c3fSmrg interpolation, 324501e04c3fSmrg qual, var_type, mode); 324601e04c3fSmrg 324701e04c3fSmrg return interpolation; 324801e04c3fSmrg} 324901e04c3fSmrg 325001e04c3fSmrg 325101e04c3fSmrgstatic void 325201e04c3fSmrgapply_explicit_location(const struct ast_type_qualifier *qual, 325301e04c3fSmrg ir_variable *var, 325401e04c3fSmrg struct _mesa_glsl_parse_state *state, 325501e04c3fSmrg YYLTYPE *loc) 325601e04c3fSmrg{ 325701e04c3fSmrg bool fail = false; 325801e04c3fSmrg 325901e04c3fSmrg unsigned qual_location; 326001e04c3fSmrg if (!process_qualifier_constant(state, loc, "location", qual->location, 326101e04c3fSmrg &qual_location)) { 326201e04c3fSmrg return; 326301e04c3fSmrg } 326401e04c3fSmrg 326501e04c3fSmrg /* Checks for GL_ARB_explicit_uniform_location. */ 326601e04c3fSmrg if (qual->flags.q.uniform) { 326701e04c3fSmrg if (!state->check_explicit_uniform_location_allowed(loc, var)) 326801e04c3fSmrg return; 326901e04c3fSmrg 327001e04c3fSmrg const struct gl_context *const ctx = state->ctx; 327101e04c3fSmrg unsigned max_loc = qual_location + var->type->uniform_locations() - 1; 327201e04c3fSmrg 327301e04c3fSmrg if (max_loc >= ctx->Const.MaxUserAssignableUniformLocations) { 327401e04c3fSmrg _mesa_glsl_error(loc, state, "location(s) consumed by uniform %s " 327501e04c3fSmrg ">= MAX_UNIFORM_LOCATIONS (%u)", var->name, 327601e04c3fSmrg ctx->Const.MaxUserAssignableUniformLocations); 327701e04c3fSmrg return; 327801e04c3fSmrg } 327901e04c3fSmrg 328001e04c3fSmrg var->data.explicit_location = true; 328101e04c3fSmrg var->data.location = qual_location; 328201e04c3fSmrg return; 328301e04c3fSmrg } 328401e04c3fSmrg 328501e04c3fSmrg /* Between GL_ARB_explicit_attrib_location an 328601e04c3fSmrg * GL_ARB_separate_shader_objects, the inputs and outputs of any shader 328701e04c3fSmrg * stage can be assigned explicit locations. The checking here associates 328801e04c3fSmrg * the correct extension with the correct stage's input / output: 328901e04c3fSmrg * 329001e04c3fSmrg * input output 329101e04c3fSmrg * ----- ------ 329201e04c3fSmrg * vertex explicit_loc sso 329301e04c3fSmrg * tess control sso sso 329401e04c3fSmrg * tess eval sso sso 329501e04c3fSmrg * geometry sso sso 329601e04c3fSmrg * fragment sso explicit_loc 329701e04c3fSmrg */ 329801e04c3fSmrg switch (state->stage) { 329901e04c3fSmrg case MESA_SHADER_VERTEX: 330001e04c3fSmrg if (var->data.mode == ir_var_shader_in) { 330101e04c3fSmrg if (!state->check_explicit_attrib_location_allowed(loc, var)) 330201e04c3fSmrg return; 330301e04c3fSmrg 330401e04c3fSmrg break; 330501e04c3fSmrg } 330601e04c3fSmrg 330701e04c3fSmrg if (var->data.mode == ir_var_shader_out) { 330801e04c3fSmrg if (!state->check_separate_shader_objects_allowed(loc, var)) 330901e04c3fSmrg return; 331001e04c3fSmrg 331101e04c3fSmrg break; 331201e04c3fSmrg } 331301e04c3fSmrg 331401e04c3fSmrg fail = true; 331501e04c3fSmrg break; 331601e04c3fSmrg 331701e04c3fSmrg case MESA_SHADER_TESS_CTRL: 331801e04c3fSmrg case MESA_SHADER_TESS_EVAL: 331901e04c3fSmrg case MESA_SHADER_GEOMETRY: 332001e04c3fSmrg if (var->data.mode == ir_var_shader_in || var->data.mode == ir_var_shader_out) { 332101e04c3fSmrg if (!state->check_separate_shader_objects_allowed(loc, var)) 332201e04c3fSmrg return; 332301e04c3fSmrg 332401e04c3fSmrg break; 332501e04c3fSmrg } 332601e04c3fSmrg 332701e04c3fSmrg fail = true; 332801e04c3fSmrg break; 332901e04c3fSmrg 333001e04c3fSmrg case MESA_SHADER_FRAGMENT: 333101e04c3fSmrg if (var->data.mode == ir_var_shader_in) { 333201e04c3fSmrg if (!state->check_separate_shader_objects_allowed(loc, var)) 333301e04c3fSmrg return; 333401e04c3fSmrg 333501e04c3fSmrg break; 333601e04c3fSmrg } 333701e04c3fSmrg 333801e04c3fSmrg if (var->data.mode == ir_var_shader_out) { 333901e04c3fSmrg if (!state->check_explicit_attrib_location_allowed(loc, var)) 334001e04c3fSmrg return; 334101e04c3fSmrg 334201e04c3fSmrg break; 334301e04c3fSmrg } 334401e04c3fSmrg 334501e04c3fSmrg fail = true; 334601e04c3fSmrg break; 334701e04c3fSmrg 334801e04c3fSmrg case MESA_SHADER_COMPUTE: 334901e04c3fSmrg _mesa_glsl_error(loc, state, 335001e04c3fSmrg "compute shader variables cannot be given " 335101e04c3fSmrg "explicit locations"); 335201e04c3fSmrg return; 335301e04c3fSmrg default: 335401e04c3fSmrg fail = true; 335501e04c3fSmrg break; 335601e04c3fSmrg }; 335701e04c3fSmrg 335801e04c3fSmrg if (fail) { 335901e04c3fSmrg _mesa_glsl_error(loc, state, 336001e04c3fSmrg "%s cannot be given an explicit location in %s shader", 336101e04c3fSmrg mode_string(var), 336201e04c3fSmrg _mesa_shader_stage_to_string(state->stage)); 336301e04c3fSmrg } else { 336401e04c3fSmrg var->data.explicit_location = true; 336501e04c3fSmrg 336601e04c3fSmrg switch (state->stage) { 336701e04c3fSmrg case MESA_SHADER_VERTEX: 336801e04c3fSmrg var->data.location = (var->data.mode == ir_var_shader_in) 336901e04c3fSmrg ? (qual_location + VERT_ATTRIB_GENERIC0) 337001e04c3fSmrg : (qual_location + VARYING_SLOT_VAR0); 337101e04c3fSmrg break; 337201e04c3fSmrg 337301e04c3fSmrg case MESA_SHADER_TESS_CTRL: 337401e04c3fSmrg case MESA_SHADER_TESS_EVAL: 337501e04c3fSmrg case MESA_SHADER_GEOMETRY: 337601e04c3fSmrg if (var->data.patch) 337701e04c3fSmrg var->data.location = qual_location + VARYING_SLOT_PATCH0; 337801e04c3fSmrg else 337901e04c3fSmrg var->data.location = qual_location + VARYING_SLOT_VAR0; 338001e04c3fSmrg break; 338101e04c3fSmrg 338201e04c3fSmrg case MESA_SHADER_FRAGMENT: 338301e04c3fSmrg var->data.location = (var->data.mode == ir_var_shader_out) 338401e04c3fSmrg ? (qual_location + FRAG_RESULT_DATA0) 338501e04c3fSmrg : (qual_location + VARYING_SLOT_VAR0); 338601e04c3fSmrg break; 338701e04c3fSmrg default: 338801e04c3fSmrg assert(!"Unexpected shader type"); 338901e04c3fSmrg break; 339001e04c3fSmrg } 339101e04c3fSmrg 339201e04c3fSmrg /* Check if index was set for the uniform instead of the function */ 339301e04c3fSmrg if (qual->flags.q.explicit_index && qual->is_subroutine_decl()) { 339401e04c3fSmrg _mesa_glsl_error(loc, state, "an index qualifier can only be " 339501e04c3fSmrg "used with subroutine functions"); 339601e04c3fSmrg return; 339701e04c3fSmrg } 339801e04c3fSmrg 339901e04c3fSmrg unsigned qual_index; 340001e04c3fSmrg if (qual->flags.q.explicit_index && 340101e04c3fSmrg process_qualifier_constant(state, loc, "index", qual->index, 340201e04c3fSmrg &qual_index)) { 340301e04c3fSmrg /* From the GLSL 4.30 specification, section 4.4.2 (Output 340401e04c3fSmrg * Layout Qualifiers): 340501e04c3fSmrg * 340601e04c3fSmrg * "It is also a compile-time error if a fragment shader 340701e04c3fSmrg * sets a layout index to less than 0 or greater than 1." 340801e04c3fSmrg * 340901e04c3fSmrg * Older specifications don't mandate a behavior; we take 341001e04c3fSmrg * this as a clarification and always generate the error. 341101e04c3fSmrg */ 341201e04c3fSmrg if (qual_index > 1) { 341301e04c3fSmrg _mesa_glsl_error(loc, state, 341401e04c3fSmrg "explicit index may only be 0 or 1"); 341501e04c3fSmrg } else { 341601e04c3fSmrg var->data.explicit_index = true; 341701e04c3fSmrg var->data.index = qual_index; 341801e04c3fSmrg } 341901e04c3fSmrg } 342001e04c3fSmrg } 342101e04c3fSmrg} 342201e04c3fSmrg 342301e04c3fSmrgstatic bool 342401e04c3fSmrgvalidate_storage_for_sampler_image_types(ir_variable *var, 342501e04c3fSmrg struct _mesa_glsl_parse_state *state, 342601e04c3fSmrg YYLTYPE *loc) 342701e04c3fSmrg{ 342801e04c3fSmrg /* From section 4.1.7 of the GLSL 4.40 spec: 342901e04c3fSmrg * 343001e04c3fSmrg * "[Opaque types] can only be declared as function 343101e04c3fSmrg * parameters or uniform-qualified variables." 343201e04c3fSmrg * 343301e04c3fSmrg * From section 4.1.7 of the ARB_bindless_texture spec: 343401e04c3fSmrg * 343501e04c3fSmrg * "Samplers may be declared as shader inputs and outputs, as uniform 343601e04c3fSmrg * variables, as temporary variables, and as function parameters." 343701e04c3fSmrg * 343801e04c3fSmrg * From section 4.1.X of the ARB_bindless_texture spec: 343901e04c3fSmrg * 344001e04c3fSmrg * "Images may be declared as shader inputs and outputs, as uniform 344101e04c3fSmrg * variables, as temporary variables, and as function parameters." 344201e04c3fSmrg */ 344301e04c3fSmrg if (state->has_bindless()) { 344401e04c3fSmrg if (var->data.mode != ir_var_auto && 344501e04c3fSmrg var->data.mode != ir_var_uniform && 344601e04c3fSmrg var->data.mode != ir_var_shader_in && 344701e04c3fSmrg var->data.mode != ir_var_shader_out && 344801e04c3fSmrg var->data.mode != ir_var_function_in && 344901e04c3fSmrg var->data.mode != ir_var_function_out && 345001e04c3fSmrg var->data.mode != ir_var_function_inout) { 345101e04c3fSmrg _mesa_glsl_error(loc, state, "bindless image/sampler variables may " 345201e04c3fSmrg "only be declared as shader inputs and outputs, as " 345301e04c3fSmrg "uniform variables, as temporary variables and as " 345401e04c3fSmrg "function parameters"); 345501e04c3fSmrg return false; 345601e04c3fSmrg } 345701e04c3fSmrg } else { 345801e04c3fSmrg if (var->data.mode != ir_var_uniform && 345901e04c3fSmrg var->data.mode != ir_var_function_in) { 346001e04c3fSmrg _mesa_glsl_error(loc, state, "image/sampler variables may only be " 346101e04c3fSmrg "declared as function parameters or " 346201e04c3fSmrg "uniform-qualified global variables"); 346301e04c3fSmrg return false; 346401e04c3fSmrg } 346501e04c3fSmrg } 346601e04c3fSmrg return true; 346701e04c3fSmrg} 346801e04c3fSmrg 346901e04c3fSmrgstatic bool 347001e04c3fSmrgvalidate_memory_qualifier_for_type(struct _mesa_glsl_parse_state *state, 347101e04c3fSmrg YYLTYPE *loc, 347201e04c3fSmrg const struct ast_type_qualifier *qual, 347301e04c3fSmrg const glsl_type *type) 347401e04c3fSmrg{ 347501e04c3fSmrg /* From Section 4.10 (Memory Qualifiers) of the GLSL 4.50 spec: 347601e04c3fSmrg * 347701e04c3fSmrg * "Memory qualifiers are only supported in the declarations of image 347801e04c3fSmrg * variables, buffer variables, and shader storage blocks; it is an error 347901e04c3fSmrg * to use such qualifiers in any other declarations. 348001e04c3fSmrg */ 348101e04c3fSmrg if (!type->is_image() && !qual->flags.q.buffer) { 348201e04c3fSmrg if (qual->flags.q.read_only || 348301e04c3fSmrg qual->flags.q.write_only || 348401e04c3fSmrg qual->flags.q.coherent || 348501e04c3fSmrg qual->flags.q._volatile || 348601e04c3fSmrg qual->flags.q.restrict_flag) { 348701e04c3fSmrg _mesa_glsl_error(loc, state, "memory qualifiers may only be applied " 348801e04c3fSmrg "in the declarations of image variables, buffer " 348901e04c3fSmrg "variables, and shader storage blocks"); 349001e04c3fSmrg return false; 349101e04c3fSmrg } 349201e04c3fSmrg } 349301e04c3fSmrg return true; 349401e04c3fSmrg} 349501e04c3fSmrg 349601e04c3fSmrgstatic bool 349701e04c3fSmrgvalidate_image_format_qualifier_for_type(struct _mesa_glsl_parse_state *state, 349801e04c3fSmrg YYLTYPE *loc, 349901e04c3fSmrg const struct ast_type_qualifier *qual, 350001e04c3fSmrg const glsl_type *type) 350101e04c3fSmrg{ 350201e04c3fSmrg /* From section 4.4.6.2 (Format Layout Qualifiers) of the GLSL 4.50 spec: 350301e04c3fSmrg * 350401e04c3fSmrg * "Format layout qualifiers can be used on image variable declarations 350501e04c3fSmrg * (those declared with a basic type having “image ” in its keyword)." 350601e04c3fSmrg */ 350701e04c3fSmrg if (!type->is_image() && qual->flags.q.explicit_image_format) { 350801e04c3fSmrg _mesa_glsl_error(loc, state, "format layout qualifiers may only be " 350901e04c3fSmrg "applied to images"); 351001e04c3fSmrg return false; 351101e04c3fSmrg } 351201e04c3fSmrg return true; 351301e04c3fSmrg} 351401e04c3fSmrg 351501e04c3fSmrgstatic void 351601e04c3fSmrgapply_image_qualifier_to_variable(const struct ast_type_qualifier *qual, 351701e04c3fSmrg ir_variable *var, 351801e04c3fSmrg struct _mesa_glsl_parse_state *state, 351901e04c3fSmrg YYLTYPE *loc) 352001e04c3fSmrg{ 352101e04c3fSmrg const glsl_type *base_type = var->type->without_array(); 352201e04c3fSmrg 352301e04c3fSmrg if (!validate_image_format_qualifier_for_type(state, loc, qual, base_type) || 352401e04c3fSmrg !validate_memory_qualifier_for_type(state, loc, qual, base_type)) 352501e04c3fSmrg return; 352601e04c3fSmrg 352701e04c3fSmrg if (!base_type->is_image()) 352801e04c3fSmrg return; 352901e04c3fSmrg 353001e04c3fSmrg if (!validate_storage_for_sampler_image_types(var, state, loc)) 353101e04c3fSmrg return; 353201e04c3fSmrg 353301e04c3fSmrg var->data.memory_read_only |= qual->flags.q.read_only; 353401e04c3fSmrg var->data.memory_write_only |= qual->flags.q.write_only; 353501e04c3fSmrg var->data.memory_coherent |= qual->flags.q.coherent; 353601e04c3fSmrg var->data.memory_volatile |= qual->flags.q._volatile; 353701e04c3fSmrg var->data.memory_restrict |= qual->flags.q.restrict_flag; 353801e04c3fSmrg 353901e04c3fSmrg if (qual->flags.q.explicit_image_format) { 354001e04c3fSmrg if (var->data.mode == ir_var_function_in) { 354101e04c3fSmrg _mesa_glsl_error(loc, state, "format qualifiers cannot be used on " 354201e04c3fSmrg "image function parameters"); 354301e04c3fSmrg } 354401e04c3fSmrg 354501e04c3fSmrg if (qual->image_base_type != base_type->sampled_type) { 354601e04c3fSmrg _mesa_glsl_error(loc, state, "format qualifier doesn't match the base " 354701e04c3fSmrg "data type of the image"); 354801e04c3fSmrg } 354901e04c3fSmrg 355001e04c3fSmrg var->data.image_format = qual->image_format; 3551ed98bd31Smaya } else if (state->has_image_load_formatted()) { 3552ed98bd31Smaya if (var->data.mode == ir_var_uniform && 3553ed98bd31Smaya state->EXT_shader_image_load_formatted_warn) { 3554ed98bd31Smaya _mesa_glsl_warning(loc, state, "GL_EXT_image_load_formatted used"); 3555ed98bd31Smaya } 355601e04c3fSmrg } else { 355701e04c3fSmrg if (var->data.mode == ir_var_uniform) { 35587ec681f3Smrg if (state->es_shader || 35597ec681f3Smrg !(state->is_version(420, 310) || state->ARB_shader_image_load_store_enable)) { 356001e04c3fSmrg _mesa_glsl_error(loc, state, "all image uniforms must have a " 356101e04c3fSmrg "format layout qualifier"); 356201e04c3fSmrg } else if (!qual->flags.q.write_only) { 356301e04c3fSmrg _mesa_glsl_error(loc, state, "image uniforms not qualified with " 356401e04c3fSmrg "`writeonly' must have a format layout qualifier"); 356501e04c3fSmrg } 356601e04c3fSmrg } 35677ec681f3Smrg var->data.image_format = PIPE_FORMAT_NONE; 356801e04c3fSmrg } 356901e04c3fSmrg 357001e04c3fSmrg /* From page 70 of the GLSL ES 3.1 specification: 357101e04c3fSmrg * 357201e04c3fSmrg * "Except for image variables qualified with the format qualifiers r32f, 357301e04c3fSmrg * r32i, and r32ui, image variables must specify either memory qualifier 357401e04c3fSmrg * readonly or the memory qualifier writeonly." 357501e04c3fSmrg */ 357601e04c3fSmrg if (state->es_shader && 35777ec681f3Smrg var->data.image_format != PIPE_FORMAT_R32_FLOAT && 35787ec681f3Smrg var->data.image_format != PIPE_FORMAT_R32_SINT && 35797ec681f3Smrg var->data.image_format != PIPE_FORMAT_R32_UINT && 358001e04c3fSmrg !var->data.memory_read_only && 358101e04c3fSmrg !var->data.memory_write_only) { 358201e04c3fSmrg _mesa_glsl_error(loc, state, "image variables of format other than r32f, " 358301e04c3fSmrg "r32i or r32ui must be qualified `readonly' or " 358401e04c3fSmrg "`writeonly'"); 358501e04c3fSmrg } 358601e04c3fSmrg} 358701e04c3fSmrg 358801e04c3fSmrgstatic inline const char* 358901e04c3fSmrgget_layout_qualifier_string(bool origin_upper_left, bool pixel_center_integer) 359001e04c3fSmrg{ 359101e04c3fSmrg if (origin_upper_left && pixel_center_integer) 359201e04c3fSmrg return "origin_upper_left, pixel_center_integer"; 359301e04c3fSmrg else if (origin_upper_left) 359401e04c3fSmrg return "origin_upper_left"; 359501e04c3fSmrg else if (pixel_center_integer) 359601e04c3fSmrg return "pixel_center_integer"; 359701e04c3fSmrg else 359801e04c3fSmrg return " "; 359901e04c3fSmrg} 360001e04c3fSmrg 360101e04c3fSmrgstatic inline bool 360201e04c3fSmrgis_conflicting_fragcoord_redeclaration(struct _mesa_glsl_parse_state *state, 360301e04c3fSmrg const struct ast_type_qualifier *qual) 360401e04c3fSmrg{ 360501e04c3fSmrg /* If gl_FragCoord was previously declared, and the qualifiers were 360601e04c3fSmrg * different in any way, return true. 360701e04c3fSmrg */ 360801e04c3fSmrg if (state->fs_redeclares_gl_fragcoord) { 360901e04c3fSmrg return (state->fs_pixel_center_integer != qual->flags.q.pixel_center_integer 361001e04c3fSmrg || state->fs_origin_upper_left != qual->flags.q.origin_upper_left); 361101e04c3fSmrg } 361201e04c3fSmrg 361301e04c3fSmrg return false; 361401e04c3fSmrg} 361501e04c3fSmrg 36167ec681f3Smrgstatic inline bool 36177ec681f3Smrgis_conflicting_layer_redeclaration(struct _mesa_glsl_parse_state *state, 36187ec681f3Smrg const struct ast_type_qualifier *qual) 36197ec681f3Smrg{ 36207ec681f3Smrg if (state->redeclares_gl_layer) { 36217ec681f3Smrg return state->layer_viewport_relative != qual->flags.q.viewport_relative; 36227ec681f3Smrg } 36237ec681f3Smrg return false; 36247ec681f3Smrg} 36257ec681f3Smrg 362601e04c3fSmrgstatic inline void 362701e04c3fSmrgvalidate_array_dimensions(const glsl_type *t, 362801e04c3fSmrg struct _mesa_glsl_parse_state *state, 362901e04c3fSmrg YYLTYPE *loc) { 363001e04c3fSmrg if (t->is_array()) { 363101e04c3fSmrg t = t->fields.array; 363201e04c3fSmrg while (t->is_array()) { 363301e04c3fSmrg if (t->is_unsized_array()) { 363401e04c3fSmrg _mesa_glsl_error(loc, state, 363501e04c3fSmrg "only the outermost array dimension can " 363601e04c3fSmrg "be unsized", 363701e04c3fSmrg t->name); 363801e04c3fSmrg break; 363901e04c3fSmrg } 364001e04c3fSmrg t = t->fields.array; 364101e04c3fSmrg } 364201e04c3fSmrg } 364301e04c3fSmrg} 364401e04c3fSmrg 364501e04c3fSmrgstatic void 364601e04c3fSmrgapply_bindless_qualifier_to_variable(const struct ast_type_qualifier *qual, 364701e04c3fSmrg ir_variable *var, 364801e04c3fSmrg struct _mesa_glsl_parse_state *state, 364901e04c3fSmrg YYLTYPE *loc) 365001e04c3fSmrg{ 365101e04c3fSmrg bool has_local_qualifiers = qual->flags.q.bindless_sampler || 365201e04c3fSmrg qual->flags.q.bindless_image || 365301e04c3fSmrg qual->flags.q.bound_sampler || 365401e04c3fSmrg qual->flags.q.bound_image; 365501e04c3fSmrg 365601e04c3fSmrg /* The ARB_bindless_texture spec says: 365701e04c3fSmrg * 365801e04c3fSmrg * "Modify Section 4.4.6 Opaque-Uniform Layout Qualifiers of the GLSL 4.30 365901e04c3fSmrg * spec" 366001e04c3fSmrg * 366101e04c3fSmrg * "If these layout qualifiers are applied to other types of default block 366201e04c3fSmrg * uniforms, or variables with non-uniform storage, a compile-time error 366301e04c3fSmrg * will be generated." 366401e04c3fSmrg */ 366501e04c3fSmrg if (has_local_qualifiers && !qual->flags.q.uniform) { 366601e04c3fSmrg _mesa_glsl_error(loc, state, "ARB_bindless_texture layout qualifiers " 366701e04c3fSmrg "can only be applied to default block uniforms or " 366801e04c3fSmrg "variables with uniform storage"); 366901e04c3fSmrg return; 367001e04c3fSmrg } 367101e04c3fSmrg 367201e04c3fSmrg /* The ARB_bindless_texture spec doesn't state anything in this situation, 367301e04c3fSmrg * but it makes sense to only allow bindless_sampler/bound_sampler for 367401e04c3fSmrg * sampler types, and respectively bindless_image/bound_image for image 367501e04c3fSmrg * types. 367601e04c3fSmrg */ 367701e04c3fSmrg if ((qual->flags.q.bindless_sampler || qual->flags.q.bound_sampler) && 367801e04c3fSmrg !var->type->contains_sampler()) { 367901e04c3fSmrg _mesa_glsl_error(loc, state, "bindless_sampler or bound_sampler can only " 368001e04c3fSmrg "be applied to sampler types"); 368101e04c3fSmrg return; 368201e04c3fSmrg } 368301e04c3fSmrg 368401e04c3fSmrg if ((qual->flags.q.bindless_image || qual->flags.q.bound_image) && 368501e04c3fSmrg !var->type->contains_image()) { 368601e04c3fSmrg _mesa_glsl_error(loc, state, "bindless_image or bound_image can only be " 368701e04c3fSmrg "applied to image types"); 368801e04c3fSmrg return; 368901e04c3fSmrg } 369001e04c3fSmrg 369101e04c3fSmrg /* The bindless_sampler/bindless_image (and respectively 369201e04c3fSmrg * bound_sampler/bound_image) layout qualifiers can be set at global and at 369301e04c3fSmrg * local scope. 369401e04c3fSmrg */ 369501e04c3fSmrg if (var->type->contains_sampler() || var->type->contains_image()) { 369601e04c3fSmrg var->data.bindless = qual->flags.q.bindless_sampler || 369701e04c3fSmrg qual->flags.q.bindless_image || 369801e04c3fSmrg state->bindless_sampler_specified || 369901e04c3fSmrg state->bindless_image_specified; 370001e04c3fSmrg 370101e04c3fSmrg var->data.bound = qual->flags.q.bound_sampler || 370201e04c3fSmrg qual->flags.q.bound_image || 370301e04c3fSmrg state->bound_sampler_specified || 370401e04c3fSmrg state->bound_image_specified; 370501e04c3fSmrg } 370601e04c3fSmrg} 370701e04c3fSmrg 370801e04c3fSmrgstatic void 370901e04c3fSmrgapply_layout_qualifier_to_variable(const struct ast_type_qualifier *qual, 371001e04c3fSmrg ir_variable *var, 371101e04c3fSmrg struct _mesa_glsl_parse_state *state, 371201e04c3fSmrg YYLTYPE *loc) 371301e04c3fSmrg{ 371401e04c3fSmrg if (var->name != NULL && strcmp(var->name, "gl_FragCoord") == 0) { 371501e04c3fSmrg 371601e04c3fSmrg /* Section 4.3.8.1, page 39 of GLSL 1.50 spec says: 371701e04c3fSmrg * 371801e04c3fSmrg * "Within any shader, the first redeclarations of gl_FragCoord 371901e04c3fSmrg * must appear before any use of gl_FragCoord." 372001e04c3fSmrg * 372101e04c3fSmrg * Generate a compiler error if above condition is not met by the 372201e04c3fSmrg * fragment shader. 372301e04c3fSmrg */ 372401e04c3fSmrg ir_variable *earlier = state->symbols->get_variable("gl_FragCoord"); 372501e04c3fSmrg if (earlier != NULL && 372601e04c3fSmrg earlier->data.used && 372701e04c3fSmrg !state->fs_redeclares_gl_fragcoord) { 372801e04c3fSmrg _mesa_glsl_error(loc, state, 372901e04c3fSmrg "gl_FragCoord used before its first redeclaration " 373001e04c3fSmrg "in fragment shader"); 373101e04c3fSmrg } 373201e04c3fSmrg 373301e04c3fSmrg /* Make sure all gl_FragCoord redeclarations specify the same layout 373401e04c3fSmrg * qualifiers. 373501e04c3fSmrg */ 373601e04c3fSmrg if (is_conflicting_fragcoord_redeclaration(state, qual)) { 373701e04c3fSmrg const char *const qual_string = 373801e04c3fSmrg get_layout_qualifier_string(qual->flags.q.origin_upper_left, 373901e04c3fSmrg qual->flags.q.pixel_center_integer); 374001e04c3fSmrg 374101e04c3fSmrg const char *const state_string = 374201e04c3fSmrg get_layout_qualifier_string(state->fs_origin_upper_left, 374301e04c3fSmrg state->fs_pixel_center_integer); 374401e04c3fSmrg 374501e04c3fSmrg _mesa_glsl_error(loc, state, 374601e04c3fSmrg "gl_FragCoord redeclared with different layout " 374701e04c3fSmrg "qualifiers (%s) and (%s) ", 374801e04c3fSmrg state_string, 374901e04c3fSmrg qual_string); 375001e04c3fSmrg } 375101e04c3fSmrg state->fs_origin_upper_left = qual->flags.q.origin_upper_left; 375201e04c3fSmrg state->fs_pixel_center_integer = qual->flags.q.pixel_center_integer; 375301e04c3fSmrg state->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers = 375401e04c3fSmrg !qual->flags.q.origin_upper_left && !qual->flags.q.pixel_center_integer; 375501e04c3fSmrg state->fs_redeclares_gl_fragcoord = 375601e04c3fSmrg state->fs_origin_upper_left || 375701e04c3fSmrg state->fs_pixel_center_integer || 375801e04c3fSmrg state->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers; 375901e04c3fSmrg } 376001e04c3fSmrg 376101e04c3fSmrg if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer) 376201e04c3fSmrg && (strcmp(var->name, "gl_FragCoord") != 0)) { 376301e04c3fSmrg const char *const qual_string = (qual->flags.q.origin_upper_left) 376401e04c3fSmrg ? "origin_upper_left" : "pixel_center_integer"; 376501e04c3fSmrg 376601e04c3fSmrg _mesa_glsl_error(loc, state, 376701e04c3fSmrg "layout qualifier `%s' can only be applied to " 376801e04c3fSmrg "fragment shader input `gl_FragCoord'", 376901e04c3fSmrg qual_string); 377001e04c3fSmrg } 377101e04c3fSmrg 377201e04c3fSmrg if (qual->flags.q.explicit_location) { 377301e04c3fSmrg apply_explicit_location(qual, var, state, loc); 377401e04c3fSmrg 377501e04c3fSmrg if (qual->flags.q.explicit_component) { 377601e04c3fSmrg unsigned qual_component; 377701e04c3fSmrg if (process_qualifier_constant(state, loc, "component", 377801e04c3fSmrg qual->component, &qual_component)) { 37797ec681f3Smrg validate_component_layout_for_type(state, loc, var->type, 37807ec681f3Smrg qual_component); 37817ec681f3Smrg var->data.explicit_component = true; 37827ec681f3Smrg var->data.location_frac = qual_component; 378301e04c3fSmrg } 378401e04c3fSmrg } 378501e04c3fSmrg } else if (qual->flags.q.explicit_index) { 378601e04c3fSmrg if (!qual->subroutine_list) 378701e04c3fSmrg _mesa_glsl_error(loc, state, 378801e04c3fSmrg "explicit index requires explicit location"); 378901e04c3fSmrg } else if (qual->flags.q.explicit_component) { 379001e04c3fSmrg _mesa_glsl_error(loc, state, 379101e04c3fSmrg "explicit component requires explicit location"); 379201e04c3fSmrg } 379301e04c3fSmrg 379401e04c3fSmrg if (qual->flags.q.explicit_binding) { 379501e04c3fSmrg apply_explicit_binding(state, loc, var, var->type, qual); 379601e04c3fSmrg } 379701e04c3fSmrg 379801e04c3fSmrg if (state->stage == MESA_SHADER_GEOMETRY && 379901e04c3fSmrg qual->flags.q.out && qual->flags.q.stream) { 380001e04c3fSmrg unsigned qual_stream; 380101e04c3fSmrg if (process_qualifier_constant(state, loc, "stream", qual->stream, 380201e04c3fSmrg &qual_stream) && 380301e04c3fSmrg validate_stream_qualifier(loc, state, qual_stream)) { 380401e04c3fSmrg var->data.stream = qual_stream; 380501e04c3fSmrg } 380601e04c3fSmrg } 380701e04c3fSmrg 380801e04c3fSmrg if (qual->flags.q.out && qual->flags.q.xfb_buffer) { 380901e04c3fSmrg unsigned qual_xfb_buffer; 381001e04c3fSmrg if (process_qualifier_constant(state, loc, "xfb_buffer", 381101e04c3fSmrg qual->xfb_buffer, &qual_xfb_buffer) && 381201e04c3fSmrg validate_xfb_buffer_qualifier(loc, state, qual_xfb_buffer)) { 381301e04c3fSmrg var->data.xfb_buffer = qual_xfb_buffer; 381401e04c3fSmrg if (qual->flags.q.explicit_xfb_buffer) 381501e04c3fSmrg var->data.explicit_xfb_buffer = true; 381601e04c3fSmrg } 381701e04c3fSmrg } 381801e04c3fSmrg 381901e04c3fSmrg if (qual->flags.q.explicit_xfb_offset) { 382001e04c3fSmrg unsigned qual_xfb_offset; 382101e04c3fSmrg unsigned component_size = var->type->contains_double() ? 8 : 4; 382201e04c3fSmrg 382301e04c3fSmrg if (process_qualifier_constant(state, loc, "xfb_offset", 382401e04c3fSmrg qual->offset, &qual_xfb_offset) && 382501e04c3fSmrg validate_xfb_offset_qualifier(loc, state, (int) qual_xfb_offset, 382601e04c3fSmrg var->type, component_size)) { 382701e04c3fSmrg var->data.offset = qual_xfb_offset; 382801e04c3fSmrg var->data.explicit_xfb_offset = true; 382901e04c3fSmrg } 383001e04c3fSmrg } 383101e04c3fSmrg 383201e04c3fSmrg if (qual->flags.q.explicit_xfb_stride) { 383301e04c3fSmrg unsigned qual_xfb_stride; 383401e04c3fSmrg if (process_qualifier_constant(state, loc, "xfb_stride", 383501e04c3fSmrg qual->xfb_stride, &qual_xfb_stride)) { 383601e04c3fSmrg var->data.xfb_stride = qual_xfb_stride; 383701e04c3fSmrg var->data.explicit_xfb_stride = true; 383801e04c3fSmrg } 383901e04c3fSmrg } 384001e04c3fSmrg 384101e04c3fSmrg if (var->type->contains_atomic()) { 384201e04c3fSmrg if (var->data.mode == ir_var_uniform) { 384301e04c3fSmrg if (var->data.explicit_binding) { 384401e04c3fSmrg unsigned *offset = 384501e04c3fSmrg &state->atomic_counter_offsets[var->data.binding]; 384601e04c3fSmrg 384701e04c3fSmrg if (*offset % ATOMIC_COUNTER_SIZE) 384801e04c3fSmrg _mesa_glsl_error(loc, state, 384901e04c3fSmrg "misaligned atomic counter offset"); 385001e04c3fSmrg 385101e04c3fSmrg var->data.offset = *offset; 385201e04c3fSmrg *offset += var->type->atomic_size(); 385301e04c3fSmrg 385401e04c3fSmrg } else { 385501e04c3fSmrg _mesa_glsl_error(loc, state, 385601e04c3fSmrg "atomic counters require explicit binding point"); 385701e04c3fSmrg } 385801e04c3fSmrg } else if (var->data.mode != ir_var_function_in) { 385901e04c3fSmrg _mesa_glsl_error(loc, state, "atomic counters may only be declared as " 386001e04c3fSmrg "function parameters or uniform-qualified " 386101e04c3fSmrg "global variables"); 386201e04c3fSmrg } 386301e04c3fSmrg } 386401e04c3fSmrg 386501e04c3fSmrg if (var->type->contains_sampler() && 386601e04c3fSmrg !validate_storage_for_sampler_image_types(var, state, loc)) 386701e04c3fSmrg return; 386801e04c3fSmrg 386901e04c3fSmrg /* Is the 'layout' keyword used with parameters that allow relaxed checking. 387001e04c3fSmrg * Many implementations of GL_ARB_fragment_coord_conventions_enable and some 387101e04c3fSmrg * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable 387201e04c3fSmrg * allowed the layout qualifier to be used with 'varying' and 'attribute'. 387301e04c3fSmrg * These extensions and all following extensions that add the 'layout' 387401e04c3fSmrg * keyword have been modified to require the use of 'in' or 'out'. 387501e04c3fSmrg * 387601e04c3fSmrg * The following extension do not allow the deprecated keywords: 387701e04c3fSmrg * 387801e04c3fSmrg * GL_AMD_conservative_depth 387901e04c3fSmrg * GL_ARB_conservative_depth 388001e04c3fSmrg * GL_ARB_gpu_shader5 388101e04c3fSmrg * GL_ARB_separate_shader_objects 388201e04c3fSmrg * GL_ARB_tessellation_shader 388301e04c3fSmrg * GL_ARB_transform_feedback3 388401e04c3fSmrg * GL_ARB_uniform_buffer_object 388501e04c3fSmrg * 388601e04c3fSmrg * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5 388701e04c3fSmrg * allow layout with the deprecated keywords. 388801e04c3fSmrg */ 388901e04c3fSmrg const bool relaxed_layout_qualifier_checking = 389001e04c3fSmrg state->ARB_fragment_coord_conventions_enable; 389101e04c3fSmrg 389201e04c3fSmrg const bool uses_deprecated_qualifier = qual->flags.q.attribute 389301e04c3fSmrg || qual->flags.q.varying; 389401e04c3fSmrg if (qual->has_layout() && uses_deprecated_qualifier) { 389501e04c3fSmrg if (relaxed_layout_qualifier_checking) { 389601e04c3fSmrg _mesa_glsl_warning(loc, state, 389701e04c3fSmrg "`layout' qualifier may not be used with " 389801e04c3fSmrg "`attribute' or `varying'"); 389901e04c3fSmrg } else { 390001e04c3fSmrg _mesa_glsl_error(loc, state, 390101e04c3fSmrg "`layout' qualifier may not be used with " 390201e04c3fSmrg "`attribute' or `varying'"); 390301e04c3fSmrg } 390401e04c3fSmrg } 390501e04c3fSmrg 390601e04c3fSmrg /* Layout qualifiers for gl_FragDepth, which are enabled by extension 390701e04c3fSmrg * AMD_conservative_depth. 390801e04c3fSmrg */ 390901e04c3fSmrg if (qual->flags.q.depth_type 391001e04c3fSmrg && !state->is_version(420, 0) 391101e04c3fSmrg && !state->AMD_conservative_depth_enable 391201e04c3fSmrg && !state->ARB_conservative_depth_enable) { 391301e04c3fSmrg _mesa_glsl_error(loc, state, 391401e04c3fSmrg "extension GL_AMD_conservative_depth or " 391501e04c3fSmrg "GL_ARB_conservative_depth must be enabled " 391601e04c3fSmrg "to use depth layout qualifiers"); 391701e04c3fSmrg } else if (qual->flags.q.depth_type 391801e04c3fSmrg && strcmp(var->name, "gl_FragDepth") != 0) { 391901e04c3fSmrg _mesa_glsl_error(loc, state, 392001e04c3fSmrg "depth layout qualifiers can be applied only to " 392101e04c3fSmrg "gl_FragDepth"); 392201e04c3fSmrg } 392301e04c3fSmrg 392401e04c3fSmrg switch (qual->depth_type) { 392501e04c3fSmrg case ast_depth_any: 392601e04c3fSmrg var->data.depth_layout = ir_depth_layout_any; 392701e04c3fSmrg break; 392801e04c3fSmrg case ast_depth_greater: 392901e04c3fSmrg var->data.depth_layout = ir_depth_layout_greater; 393001e04c3fSmrg break; 393101e04c3fSmrg case ast_depth_less: 393201e04c3fSmrg var->data.depth_layout = ir_depth_layout_less; 393301e04c3fSmrg break; 393401e04c3fSmrg case ast_depth_unchanged: 393501e04c3fSmrg var->data.depth_layout = ir_depth_layout_unchanged; 393601e04c3fSmrg break; 393701e04c3fSmrg default: 393801e04c3fSmrg var->data.depth_layout = ir_depth_layout_none; 393901e04c3fSmrg break; 394001e04c3fSmrg } 394101e04c3fSmrg 394201e04c3fSmrg if (qual->flags.q.std140 || 394301e04c3fSmrg qual->flags.q.std430 || 394401e04c3fSmrg qual->flags.q.packed || 394501e04c3fSmrg qual->flags.q.shared) { 394601e04c3fSmrg _mesa_glsl_error(loc, state, 394701e04c3fSmrg "uniform and shader storage block layout qualifiers " 394801e04c3fSmrg "std140, std430, packed, and shared can only be " 394901e04c3fSmrg "applied to uniform or shader storage blocks, not " 395001e04c3fSmrg "members"); 395101e04c3fSmrg } 395201e04c3fSmrg 395301e04c3fSmrg if (qual->flags.q.row_major || qual->flags.q.column_major) { 395401e04c3fSmrg validate_matrix_layout_for_type(state, loc, var->type, var); 395501e04c3fSmrg } 395601e04c3fSmrg 395701e04c3fSmrg /* From section 4.4.1.3 of the GLSL 4.50 specification (Fragment Shader 395801e04c3fSmrg * Inputs): 395901e04c3fSmrg * 396001e04c3fSmrg * "Fragment shaders also allow the following layout qualifier on in only 396101e04c3fSmrg * (not with variable declarations) 396201e04c3fSmrg * layout-qualifier-id 396301e04c3fSmrg * early_fragment_tests 396401e04c3fSmrg * [...]" 396501e04c3fSmrg */ 396601e04c3fSmrg if (qual->flags.q.early_fragment_tests) { 396701e04c3fSmrg _mesa_glsl_error(loc, state, "early_fragment_tests layout qualifier only " 396801e04c3fSmrg "valid in fragment shader input layout declaration."); 396901e04c3fSmrg } 397001e04c3fSmrg 397101e04c3fSmrg if (qual->flags.q.inner_coverage) { 397201e04c3fSmrg _mesa_glsl_error(loc, state, "inner_coverage layout qualifier only " 397301e04c3fSmrg "valid in fragment shader input layout declaration."); 397401e04c3fSmrg } 397501e04c3fSmrg 397601e04c3fSmrg if (qual->flags.q.post_depth_coverage) { 397701e04c3fSmrg _mesa_glsl_error(loc, state, "post_depth_coverage layout qualifier only " 397801e04c3fSmrg "valid in fragment shader input layout declaration."); 397901e04c3fSmrg } 398001e04c3fSmrg 398101e04c3fSmrg if (state->has_bindless()) 398201e04c3fSmrg apply_bindless_qualifier_to_variable(qual, var, state, loc); 398301e04c3fSmrg 398401e04c3fSmrg if (qual->flags.q.pixel_interlock_ordered || 398501e04c3fSmrg qual->flags.q.pixel_interlock_unordered || 398601e04c3fSmrg qual->flags.q.sample_interlock_ordered || 398701e04c3fSmrg qual->flags.q.sample_interlock_unordered) { 398801e04c3fSmrg _mesa_glsl_error(loc, state, "interlock layout qualifiers: " 398901e04c3fSmrg "pixel_interlock_ordered, pixel_interlock_unordered, " 399001e04c3fSmrg "sample_interlock_ordered and sample_interlock_unordered, " 399101e04c3fSmrg "only valid in fragment shader input layout declaration."); 399201e04c3fSmrg } 39937ec681f3Smrg 39947ec681f3Smrg if (var->name != NULL && strcmp(var->name, "gl_Layer") == 0) { 39957ec681f3Smrg if (is_conflicting_layer_redeclaration(state, qual)) { 39967ec681f3Smrg _mesa_glsl_error(loc, state, "gl_Layer redeclaration with " 39977ec681f3Smrg "different viewport_relative setting than earlier"); 39987ec681f3Smrg } 39997ec681f3Smrg state->redeclares_gl_layer = true; 40007ec681f3Smrg if (qual->flags.q.viewport_relative) { 40017ec681f3Smrg state->layer_viewport_relative = true; 40027ec681f3Smrg } 40037ec681f3Smrg } else if (qual->flags.q.viewport_relative) { 40047ec681f3Smrg _mesa_glsl_error(loc, state, 40057ec681f3Smrg "viewport_relative qualifier " 40067ec681f3Smrg "can only be applied to gl_Layer."); 40077ec681f3Smrg } 400801e04c3fSmrg} 400901e04c3fSmrg 401001e04c3fSmrgstatic void 401101e04c3fSmrgapply_type_qualifier_to_variable(const struct ast_type_qualifier *qual, 401201e04c3fSmrg ir_variable *var, 401301e04c3fSmrg struct _mesa_glsl_parse_state *state, 401401e04c3fSmrg YYLTYPE *loc, 401501e04c3fSmrg bool is_parameter) 401601e04c3fSmrg{ 401701e04c3fSmrg STATIC_ASSERT(sizeof(qual->flags.q) <= sizeof(qual->flags.i)); 401801e04c3fSmrg 401901e04c3fSmrg if (qual->flags.q.invariant) { 402001e04c3fSmrg if (var->data.used) { 402101e04c3fSmrg _mesa_glsl_error(loc, state, 402201e04c3fSmrg "variable `%s' may not be redeclared " 402301e04c3fSmrg "`invariant' after being used", 402401e04c3fSmrg var->name); 402501e04c3fSmrg } else { 4026993e1d59Smrg var->data.explicit_invariant = true; 4027993e1d59Smrg var->data.invariant = true; 402801e04c3fSmrg } 402901e04c3fSmrg } 403001e04c3fSmrg 403101e04c3fSmrg if (qual->flags.q.precise) { 403201e04c3fSmrg if (var->data.used) { 403301e04c3fSmrg _mesa_glsl_error(loc, state, 403401e04c3fSmrg "variable `%s' may not be redeclared " 403501e04c3fSmrg "`precise' after being used", 403601e04c3fSmrg var->name); 403701e04c3fSmrg } else { 403801e04c3fSmrg var->data.precise = 1; 403901e04c3fSmrg } 404001e04c3fSmrg } 404101e04c3fSmrg 404201e04c3fSmrg if (qual->is_subroutine_decl() && !qual->flags.q.uniform) { 404301e04c3fSmrg _mesa_glsl_error(loc, state, 404401e04c3fSmrg "`subroutine' may only be applied to uniforms, " 404501e04c3fSmrg "subroutine type declarations, or function definitions"); 404601e04c3fSmrg } 404701e04c3fSmrg 404801e04c3fSmrg if (qual->flags.q.constant || qual->flags.q.attribute 404901e04c3fSmrg || qual->flags.q.uniform 405001e04c3fSmrg || (qual->flags.q.varying && (state->stage == MESA_SHADER_FRAGMENT))) 405101e04c3fSmrg var->data.read_only = 1; 405201e04c3fSmrg 405301e04c3fSmrg if (qual->flags.q.centroid) 405401e04c3fSmrg var->data.centroid = 1; 405501e04c3fSmrg 405601e04c3fSmrg if (qual->flags.q.sample) 405701e04c3fSmrg var->data.sample = 1; 405801e04c3fSmrg 405901e04c3fSmrg /* Precision qualifiers do not hold any meaning in Desktop GLSL */ 406001e04c3fSmrg if (state->es_shader) { 406101e04c3fSmrg var->data.precision = 406201e04c3fSmrg select_gles_precision(qual->precision, var->type, state, loc); 406301e04c3fSmrg } 406401e04c3fSmrg 406501e04c3fSmrg if (qual->flags.q.patch) 406601e04c3fSmrg var->data.patch = 1; 406701e04c3fSmrg 406801e04c3fSmrg if (qual->flags.q.attribute && state->stage != MESA_SHADER_VERTEX) { 406901e04c3fSmrg var->type = glsl_type::error_type; 407001e04c3fSmrg _mesa_glsl_error(loc, state, 407101e04c3fSmrg "`attribute' variables may not be declared in the " 407201e04c3fSmrg "%s shader", 407301e04c3fSmrg _mesa_shader_stage_to_string(state->stage)); 407401e04c3fSmrg } 407501e04c3fSmrg 407601e04c3fSmrg /* Disallow layout qualifiers which may only appear on layout declarations. */ 407701e04c3fSmrg if (qual->flags.q.prim_type) { 407801e04c3fSmrg _mesa_glsl_error(loc, state, 407901e04c3fSmrg "Primitive type may only be specified on GS input or output " 408001e04c3fSmrg "layout declaration, not on variables."); 408101e04c3fSmrg } 408201e04c3fSmrg 408301e04c3fSmrg /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says: 408401e04c3fSmrg * 408501e04c3fSmrg * "However, the const qualifier cannot be used with out or inout." 408601e04c3fSmrg * 408701e04c3fSmrg * The same section of the GLSL 4.40 spec further clarifies this saying: 408801e04c3fSmrg * 408901e04c3fSmrg * "The const qualifier cannot be used with out or inout, or a 409001e04c3fSmrg * compile-time error results." 409101e04c3fSmrg */ 409201e04c3fSmrg if (is_parameter && qual->flags.q.constant && qual->flags.q.out) { 409301e04c3fSmrg _mesa_glsl_error(loc, state, 409401e04c3fSmrg "`const' may not be applied to `out' or `inout' " 409501e04c3fSmrg "function parameters"); 409601e04c3fSmrg } 409701e04c3fSmrg 409801e04c3fSmrg /* If there is no qualifier that changes the mode of the variable, leave 409901e04c3fSmrg * the setting alone. 410001e04c3fSmrg */ 410101e04c3fSmrg assert(var->data.mode != ir_var_temporary); 410201e04c3fSmrg if (qual->flags.q.in && qual->flags.q.out) 410301e04c3fSmrg var->data.mode = is_parameter ? ir_var_function_inout : ir_var_shader_out; 410401e04c3fSmrg else if (qual->flags.q.in) 410501e04c3fSmrg var->data.mode = is_parameter ? ir_var_function_in : ir_var_shader_in; 410601e04c3fSmrg else if (qual->flags.q.attribute 410701e04c3fSmrg || (qual->flags.q.varying && (state->stage == MESA_SHADER_FRAGMENT))) 410801e04c3fSmrg var->data.mode = ir_var_shader_in; 410901e04c3fSmrg else if (qual->flags.q.out) 411001e04c3fSmrg var->data.mode = is_parameter ? ir_var_function_out : ir_var_shader_out; 411101e04c3fSmrg else if (qual->flags.q.varying && (state->stage == MESA_SHADER_VERTEX)) 411201e04c3fSmrg var->data.mode = ir_var_shader_out; 411301e04c3fSmrg else if (qual->flags.q.uniform) 411401e04c3fSmrg var->data.mode = ir_var_uniform; 411501e04c3fSmrg else if (qual->flags.q.buffer) 411601e04c3fSmrg var->data.mode = ir_var_shader_storage; 411701e04c3fSmrg else if (qual->flags.q.shared_storage) 411801e04c3fSmrg var->data.mode = ir_var_shader_shared; 411901e04c3fSmrg 412001e04c3fSmrg if (!is_parameter && state->has_framebuffer_fetch() && 412101e04c3fSmrg state->stage == MESA_SHADER_FRAGMENT) { 412201e04c3fSmrg if (state->is_version(130, 300)) 412301e04c3fSmrg var->data.fb_fetch_output = qual->flags.q.in && qual->flags.q.out; 412401e04c3fSmrg else 412501e04c3fSmrg var->data.fb_fetch_output = (strcmp(var->name, "gl_LastFragData") == 0); 412601e04c3fSmrg } 412701e04c3fSmrg 412801e04c3fSmrg if (var->data.fb_fetch_output) { 412901e04c3fSmrg var->data.assigned = true; 413001e04c3fSmrg var->data.memory_coherent = !qual->flags.q.non_coherent; 413101e04c3fSmrg 413201e04c3fSmrg /* From the EXT_shader_framebuffer_fetch spec: 413301e04c3fSmrg * 413401e04c3fSmrg * "It is an error to declare an inout fragment output not qualified 413501e04c3fSmrg * with layout(noncoherent) if the GL_EXT_shader_framebuffer_fetch 413601e04c3fSmrg * extension hasn't been enabled." 413701e04c3fSmrg */ 413801e04c3fSmrg if (var->data.memory_coherent && 413901e04c3fSmrg !state->EXT_shader_framebuffer_fetch_enable) 414001e04c3fSmrg _mesa_glsl_error(loc, state, 414101e04c3fSmrg "invalid declaration of framebuffer fetch output not " 414201e04c3fSmrg "qualified with layout(noncoherent)"); 414301e04c3fSmrg 414401e04c3fSmrg } else { 414501e04c3fSmrg /* From the EXT_shader_framebuffer_fetch spec: 414601e04c3fSmrg * 414701e04c3fSmrg * "Fragment outputs declared inout may specify the following layout 414801e04c3fSmrg * qualifier: [...] noncoherent" 414901e04c3fSmrg */ 415001e04c3fSmrg if (qual->flags.q.non_coherent) 415101e04c3fSmrg _mesa_glsl_error(loc, state, 415201e04c3fSmrg "invalid layout(noncoherent) qualifier not part of " 415301e04c3fSmrg "framebuffer fetch output declaration"); 415401e04c3fSmrg } 415501e04c3fSmrg 415601e04c3fSmrg if (!is_parameter && is_varying_var(var, state->stage)) { 415701e04c3fSmrg /* User-defined ins/outs are not permitted in compute shaders. */ 415801e04c3fSmrg if (state->stage == MESA_SHADER_COMPUTE) { 415901e04c3fSmrg _mesa_glsl_error(loc, state, 416001e04c3fSmrg "user-defined input and output variables are not " 416101e04c3fSmrg "permitted in compute shaders"); 416201e04c3fSmrg } 416301e04c3fSmrg 416401e04c3fSmrg /* This variable is being used to link data between shader stages (in 416501e04c3fSmrg * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type 416601e04c3fSmrg * that is allowed for such purposes. 416701e04c3fSmrg * 416801e04c3fSmrg * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec: 416901e04c3fSmrg * 417001e04c3fSmrg * "The varying qualifier can be used only with the data types 417101e04c3fSmrg * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of 417201e04c3fSmrg * these." 417301e04c3fSmrg * 417401e04c3fSmrg * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From 417501e04c3fSmrg * page 31 (page 37 of the PDF) of the GLSL 1.30 spec: 417601e04c3fSmrg * 417701e04c3fSmrg * "Fragment inputs can only be signed and unsigned integers and 417801e04c3fSmrg * integer vectors, float, floating-point vectors, matrices, or 417901e04c3fSmrg * arrays of these. Structures cannot be input. 418001e04c3fSmrg * 418101e04c3fSmrg * Similar text exists in the section on vertex shader outputs. 418201e04c3fSmrg * 418301e04c3fSmrg * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES 418401e04c3fSmrg * 3.00 spec allows structs as well. Varying structs are also allowed 418501e04c3fSmrg * in GLSL 1.50. 418601e04c3fSmrg * 418701e04c3fSmrg * From section 4.3.4 of the ARB_bindless_texture spec: 418801e04c3fSmrg * 418901e04c3fSmrg * "(modify third paragraph of the section to allow sampler and image 419001e04c3fSmrg * types) ... Vertex shader inputs can only be float, 419101e04c3fSmrg * single-precision floating-point scalars, single-precision 419201e04c3fSmrg * floating-point vectors, matrices, signed and unsigned integers 419301e04c3fSmrg * and integer vectors, sampler and image types." 419401e04c3fSmrg * 419501e04c3fSmrg * From section 4.3.6 of the ARB_bindless_texture spec: 419601e04c3fSmrg * 419701e04c3fSmrg * "Output variables can only be floating-point scalars, 419801e04c3fSmrg * floating-point vectors, matrices, signed or unsigned integers or 419901e04c3fSmrg * integer vectors, sampler or image types, or arrays or structures 420001e04c3fSmrg * of any these." 420101e04c3fSmrg */ 420201e04c3fSmrg switch (var->type->without_array()->base_type) { 420301e04c3fSmrg case GLSL_TYPE_FLOAT: 420401e04c3fSmrg /* Ok in all GLSL versions */ 420501e04c3fSmrg break; 420601e04c3fSmrg case GLSL_TYPE_UINT: 420701e04c3fSmrg case GLSL_TYPE_INT: 4208ed98bd31Smaya if (state->is_version(130, 300) || state->EXT_gpu_shader4_enable) 420901e04c3fSmrg break; 421001e04c3fSmrg _mesa_glsl_error(loc, state, 421101e04c3fSmrg "varying variables must be of base type float in %s", 421201e04c3fSmrg state->get_version_string()); 421301e04c3fSmrg break; 421401e04c3fSmrg case GLSL_TYPE_STRUCT: 421501e04c3fSmrg if (state->is_version(150, 300)) 421601e04c3fSmrg break; 421701e04c3fSmrg _mesa_glsl_error(loc, state, 421801e04c3fSmrg "varying variables may not be of type struct"); 421901e04c3fSmrg break; 422001e04c3fSmrg case GLSL_TYPE_DOUBLE: 422101e04c3fSmrg case GLSL_TYPE_UINT64: 422201e04c3fSmrg case GLSL_TYPE_INT64: 422301e04c3fSmrg break; 422401e04c3fSmrg case GLSL_TYPE_SAMPLER: 422501e04c3fSmrg case GLSL_TYPE_IMAGE: 422601e04c3fSmrg if (state->has_bindless()) 422701e04c3fSmrg break; 42287ec681f3Smrg FALLTHROUGH; 422901e04c3fSmrg default: 423001e04c3fSmrg _mesa_glsl_error(loc, state, "illegal type for a varying variable"); 423101e04c3fSmrg break; 423201e04c3fSmrg } 423301e04c3fSmrg } 423401e04c3fSmrg 4235993e1d59Smrg if (state->all_invariant && var->data.mode == ir_var_shader_out) { 4236993e1d59Smrg var->data.explicit_invariant = true; 423701e04c3fSmrg var->data.invariant = true; 4238993e1d59Smrg } 423901e04c3fSmrg 424001e04c3fSmrg var->data.interpolation = 424101e04c3fSmrg interpret_interpolation_qualifier(qual, var->type, 424201e04c3fSmrg (ir_variable_mode) var->data.mode, 424301e04c3fSmrg state, loc); 424401e04c3fSmrg 424501e04c3fSmrg /* Does the declaration use the deprecated 'attribute' or 'varying' 424601e04c3fSmrg * keywords? 424701e04c3fSmrg */ 424801e04c3fSmrg const bool uses_deprecated_qualifier = qual->flags.q.attribute 424901e04c3fSmrg || qual->flags.q.varying; 425001e04c3fSmrg 425101e04c3fSmrg 425201e04c3fSmrg /* Validate auxiliary storage qualifiers */ 425301e04c3fSmrg 425401e04c3fSmrg /* From section 4.3.4 of the GLSL 1.30 spec: 425501e04c3fSmrg * "It is an error to use centroid in in a vertex shader." 425601e04c3fSmrg * 425701e04c3fSmrg * From section 4.3.4 of the GLSL ES 3.00 spec: 425801e04c3fSmrg * "It is an error to use centroid in or interpolation qualifiers in 425901e04c3fSmrg * a vertex shader input." 426001e04c3fSmrg */ 426101e04c3fSmrg 426201e04c3fSmrg /* Section 4.3.6 of the GLSL 1.30 specification states: 426301e04c3fSmrg * "It is an error to use centroid out in a fragment shader." 426401e04c3fSmrg * 426501e04c3fSmrg * The GL_ARB_shading_language_420pack extension specification states: 426601e04c3fSmrg * "It is an error to use auxiliary storage qualifiers or interpolation 426701e04c3fSmrg * qualifiers on an output in a fragment shader." 426801e04c3fSmrg */ 426901e04c3fSmrg if (qual->flags.q.sample && (!is_varying_var(var, state->stage) || uses_deprecated_qualifier)) { 427001e04c3fSmrg _mesa_glsl_error(loc, state, 427101e04c3fSmrg "sample qualifier may only be used on `in` or `out` " 427201e04c3fSmrg "variables between shader stages"); 427301e04c3fSmrg } 427401e04c3fSmrg if (qual->flags.q.centroid && !is_varying_var(var, state->stage)) { 427501e04c3fSmrg _mesa_glsl_error(loc, state, 427601e04c3fSmrg "centroid qualifier may only be used with `in', " 427701e04c3fSmrg "`out' or `varying' variables between shader stages"); 427801e04c3fSmrg } 427901e04c3fSmrg 428001e04c3fSmrg if (qual->flags.q.shared_storage && state->stage != MESA_SHADER_COMPUTE) { 428101e04c3fSmrg _mesa_glsl_error(loc, state, 428201e04c3fSmrg "the shared storage qualifiers can only be used with " 428301e04c3fSmrg "compute shaders"); 428401e04c3fSmrg } 428501e04c3fSmrg 428601e04c3fSmrg apply_image_qualifier_to_variable(qual, var, state, loc); 428701e04c3fSmrg} 428801e04c3fSmrg 428901e04c3fSmrg/** 429001e04c3fSmrg * Get the variable that is being redeclared by this declaration or if it 429101e04c3fSmrg * does not exist, the current declared variable. 429201e04c3fSmrg * 429301e04c3fSmrg * Semantic checks to verify the validity of the redeclaration are also 429401e04c3fSmrg * performed. If semantic checks fail, compilation error will be emitted via 429501e04c3fSmrg * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned. 429601e04c3fSmrg * 429701e04c3fSmrg * \returns 429801e04c3fSmrg * A pointer to an existing variable in the current scope if the declaration 429901e04c3fSmrg * is a redeclaration, current variable otherwise. \c is_declared boolean 430001e04c3fSmrg * will return \c true if the declaration is a redeclaration, \c false 430101e04c3fSmrg * otherwise. 430201e04c3fSmrg */ 430301e04c3fSmrgstatic ir_variable * 430401e04c3fSmrgget_variable_being_redeclared(ir_variable **var_ptr, YYLTYPE loc, 430501e04c3fSmrg struct _mesa_glsl_parse_state *state, 430601e04c3fSmrg bool allow_all_redeclarations, 430701e04c3fSmrg bool *is_redeclaration) 430801e04c3fSmrg{ 430901e04c3fSmrg ir_variable *var = *var_ptr; 431001e04c3fSmrg 431101e04c3fSmrg /* Check if this declaration is actually a re-declaration, either to 431201e04c3fSmrg * resize an array or add qualifiers to an existing variable. 431301e04c3fSmrg * 431401e04c3fSmrg * This is allowed for variables in the current scope, or when at 431501e04c3fSmrg * global scope (for built-ins in the implicit outer scope). 431601e04c3fSmrg */ 431701e04c3fSmrg ir_variable *earlier = state->symbols->get_variable(var->name); 431801e04c3fSmrg if (earlier == NULL || 431901e04c3fSmrg (state->current_function != NULL && 432001e04c3fSmrg !state->symbols->name_declared_this_scope(var->name))) { 432101e04c3fSmrg *is_redeclaration = false; 432201e04c3fSmrg return var; 432301e04c3fSmrg } 432401e04c3fSmrg 432501e04c3fSmrg *is_redeclaration = true; 432601e04c3fSmrg 4327ed98bd31Smaya if (earlier->data.how_declared == ir_var_declared_implicitly) { 4328ed98bd31Smaya /* Verify that the redeclaration of a built-in does not change the 4329ed98bd31Smaya * storage qualifier. There are a couple special cases. 4330ed98bd31Smaya * 4331ed98bd31Smaya * 1. Some built-in variables that are defined as 'in' in the 4332ed98bd31Smaya * specification are implemented as system values. Allow 4333ed98bd31Smaya * ir_var_system_value -> ir_var_shader_in. 4334ed98bd31Smaya * 4335ed98bd31Smaya * 2. gl_LastFragData is implemented as a ir_var_shader_out, but the 4336ed98bd31Smaya * specification requires that redeclarations omit any qualifier. 4337ed98bd31Smaya * Allow ir_var_shader_out -> ir_var_auto for this one variable. 4338ed98bd31Smaya */ 4339ed98bd31Smaya if (earlier->data.mode != var->data.mode && 4340ed98bd31Smaya !(earlier->data.mode == ir_var_system_value && 4341ed98bd31Smaya var->data.mode == ir_var_shader_in) && 4342ed98bd31Smaya !(strcmp(var->name, "gl_LastFragData") == 0 && 4343ed98bd31Smaya var->data.mode == ir_var_auto)) { 4344ed98bd31Smaya _mesa_glsl_error(&loc, state, 4345ed98bd31Smaya "redeclaration cannot change qualification of `%s'", 4346ed98bd31Smaya var->name); 4347ed98bd31Smaya } 4348ed98bd31Smaya } 4349ed98bd31Smaya 435001e04c3fSmrg /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec, 435101e04c3fSmrg * 435201e04c3fSmrg * "It is legal to declare an array without a size and then 435301e04c3fSmrg * later re-declare the same name as an array of the same 435401e04c3fSmrg * type and specify a size." 435501e04c3fSmrg */ 435601e04c3fSmrg if (earlier->type->is_unsized_array() && var->type->is_array() 435701e04c3fSmrg && (var->type->fields.array == earlier->type->fields.array)) { 435801e04c3fSmrg const int size = var->type->array_size(); 435901e04c3fSmrg check_builtin_array_max_size(var->name, size, loc, state); 436001e04c3fSmrg if ((size > 0) && (size <= earlier->data.max_array_access)) { 436101e04c3fSmrg _mesa_glsl_error(& loc, state, "array size must be > %u due to " 436201e04c3fSmrg "previous access", 436301e04c3fSmrg earlier->data.max_array_access); 436401e04c3fSmrg } 436501e04c3fSmrg 436601e04c3fSmrg earlier->type = var->type; 436701e04c3fSmrg delete var; 436801e04c3fSmrg var = NULL; 436901e04c3fSmrg *var_ptr = NULL; 4370ed98bd31Smaya } else if (earlier->type != var->type) { 4371ed98bd31Smaya _mesa_glsl_error(&loc, state, 4372ed98bd31Smaya "redeclaration of `%s' has incorrect type", 4373ed98bd31Smaya var->name); 437401e04c3fSmrg } else if ((state->ARB_fragment_coord_conventions_enable || 437501e04c3fSmrg state->is_version(150, 0)) 4376ed98bd31Smaya && strcmp(var->name, "gl_FragCoord") == 0) { 437701e04c3fSmrg /* Allow redeclaration of gl_FragCoord for ARB_fcc layout 437801e04c3fSmrg * qualifiers. 4379ed98bd31Smaya * 4380ed98bd31Smaya * We don't really need to do anything here, just allow the 4381ed98bd31Smaya * redeclaration. Any error on the gl_FragCoord is handled on the ast 4382ed98bd31Smaya * level at apply_layout_qualifier_to_variable using the 4383ed98bd31Smaya * ast_type_qualifier and _mesa_glsl_parse_state, or later at 4384ed98bd31Smaya * linker.cpp. 438501e04c3fSmrg */ 438601e04c3fSmrg /* According to section 4.3.7 of the GLSL 1.30 spec, 438701e04c3fSmrg * the following built-in varaibles can be redeclared with an 438801e04c3fSmrg * interpolation qualifier: 438901e04c3fSmrg * * gl_FrontColor 439001e04c3fSmrg * * gl_BackColor 439101e04c3fSmrg * * gl_FrontSecondaryColor 439201e04c3fSmrg * * gl_BackSecondaryColor 439301e04c3fSmrg * * gl_Color 439401e04c3fSmrg * * gl_SecondaryColor 439501e04c3fSmrg */ 439601e04c3fSmrg } else if (state->is_version(130, 0) 439701e04c3fSmrg && (strcmp(var->name, "gl_FrontColor") == 0 439801e04c3fSmrg || strcmp(var->name, "gl_BackColor") == 0 439901e04c3fSmrg || strcmp(var->name, "gl_FrontSecondaryColor") == 0 440001e04c3fSmrg || strcmp(var->name, "gl_BackSecondaryColor") == 0 440101e04c3fSmrg || strcmp(var->name, "gl_Color") == 0 4402ed98bd31Smaya || strcmp(var->name, "gl_SecondaryColor") == 0)) { 440301e04c3fSmrg earlier->data.interpolation = var->data.interpolation; 440401e04c3fSmrg 440501e04c3fSmrg /* Layout qualifiers for gl_FragDepth. */ 440601e04c3fSmrg } else if ((state->is_version(420, 0) || 440701e04c3fSmrg state->AMD_conservative_depth_enable || 440801e04c3fSmrg state->ARB_conservative_depth_enable) 4409ed98bd31Smaya && strcmp(var->name, "gl_FragDepth") == 0) { 441001e04c3fSmrg 441101e04c3fSmrg /** From the AMD_conservative_depth spec: 441201e04c3fSmrg * Within any shader, the first redeclarations of gl_FragDepth 441301e04c3fSmrg * must appear before any use of gl_FragDepth. 441401e04c3fSmrg */ 441501e04c3fSmrg if (earlier->data.used) { 441601e04c3fSmrg _mesa_glsl_error(&loc, state, 441701e04c3fSmrg "the first redeclaration of gl_FragDepth " 441801e04c3fSmrg "must appear before any use of gl_FragDepth"); 441901e04c3fSmrg } 442001e04c3fSmrg 442101e04c3fSmrg /* Prevent inconsistent redeclaration of depth layout qualifier. */ 442201e04c3fSmrg if (earlier->data.depth_layout != ir_depth_layout_none 442301e04c3fSmrg && earlier->data.depth_layout != var->data.depth_layout) { 442401e04c3fSmrg _mesa_glsl_error(&loc, state, 442501e04c3fSmrg "gl_FragDepth: depth layout is declared here " 442601e04c3fSmrg "as '%s, but it was previously declared as " 442701e04c3fSmrg "'%s'", 442801e04c3fSmrg depth_layout_string(var->data.depth_layout), 442901e04c3fSmrg depth_layout_string(earlier->data.depth_layout)); 443001e04c3fSmrg } 443101e04c3fSmrg 443201e04c3fSmrg earlier->data.depth_layout = var->data.depth_layout; 443301e04c3fSmrg 443401e04c3fSmrg } else if (state->has_framebuffer_fetch() && 443501e04c3fSmrg strcmp(var->name, "gl_LastFragData") == 0 && 443601e04c3fSmrg var->data.mode == ir_var_auto) { 443701e04c3fSmrg /* According to the EXT_shader_framebuffer_fetch spec: 443801e04c3fSmrg * 443901e04c3fSmrg * "By default, gl_LastFragData is declared with the mediump precision 444001e04c3fSmrg * qualifier. This can be changed by redeclaring the corresponding 444101e04c3fSmrg * variables with the desired precision qualifier." 444201e04c3fSmrg * 444301e04c3fSmrg * "Fragment shaders may specify the following layout qualifier only for 444401e04c3fSmrg * redeclaring the built-in gl_LastFragData array [...]: noncoherent" 444501e04c3fSmrg */ 444601e04c3fSmrg earlier->data.precision = var->data.precision; 444701e04c3fSmrg earlier->data.memory_coherent = var->data.memory_coherent; 444801e04c3fSmrg 44497ec681f3Smrg } else if (state->NV_viewport_array2_enable && 44507ec681f3Smrg strcmp(var->name, "gl_Layer") == 0 && 44517ec681f3Smrg earlier->data.how_declared == ir_var_declared_implicitly) { 44527ec681f3Smrg /* No need to do anything, just allow it. Qualifier is stored in state */ 44537ec681f3Smrg 44547ec681f3Smrg } else if (state->is_version(0, 300) && 44557ec681f3Smrg state->has_separate_shader_objects() && 44567ec681f3Smrg (strcmp(var->name, "gl_Position") == 0 || 44577ec681f3Smrg strcmp(var->name, "gl_PointSize") == 0)) { 44587ec681f3Smrg 44597ec681f3Smrg /* EXT_separate_shader_objects spec says: 44607ec681f3Smrg * 44617ec681f3Smrg * "The following vertex shader outputs may be redeclared 44627ec681f3Smrg * at global scope to specify a built-in output interface, 44637ec681f3Smrg * with or without special qualifiers: 44647ec681f3Smrg * 44657ec681f3Smrg * gl_Position 44667ec681f3Smrg * gl_PointSize 44677ec681f3Smrg * 44687ec681f3Smrg * When compiling shaders using either of the above variables, 44697ec681f3Smrg * both such variables must be redeclared prior to use." 44707ec681f3Smrg */ 44717ec681f3Smrg if (earlier->data.used) { 44727ec681f3Smrg _mesa_glsl_error(&loc, state, "the first redeclaration of " 44737ec681f3Smrg "%s must appear before any use", var->name); 44747ec681f3Smrg } 4475ed98bd31Smaya } else if ((earlier->data.how_declared == ir_var_declared_implicitly && 4476ed98bd31Smaya state->allow_builtin_variable_redeclaration) || 4477ed98bd31Smaya allow_all_redeclarations) { 447801e04c3fSmrg /* Allow verbatim redeclarations of built-in variables. Not explicitly 447901e04c3fSmrg * valid, but some applications do it. 448001e04c3fSmrg */ 448101e04c3fSmrg } else { 448201e04c3fSmrg _mesa_glsl_error(&loc, state, "`%s' redeclared", var->name); 448301e04c3fSmrg } 448401e04c3fSmrg 448501e04c3fSmrg return earlier; 448601e04c3fSmrg} 448701e04c3fSmrg 448801e04c3fSmrg/** 448901e04c3fSmrg * Generate the IR for an initializer in a variable declaration 449001e04c3fSmrg */ 449101e04c3fSmrgstatic ir_rvalue * 449201e04c3fSmrgprocess_initializer(ir_variable *var, ast_declaration *decl, 449301e04c3fSmrg ast_fully_specified_type *type, 449401e04c3fSmrg exec_list *initializer_instructions, 449501e04c3fSmrg struct _mesa_glsl_parse_state *state) 449601e04c3fSmrg{ 449701e04c3fSmrg void *mem_ctx = state; 449801e04c3fSmrg ir_rvalue *result = NULL; 449901e04c3fSmrg 450001e04c3fSmrg YYLTYPE initializer_loc = decl->initializer->get_location(); 450101e04c3fSmrg 450201e04c3fSmrg /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec: 450301e04c3fSmrg * 450401e04c3fSmrg * "All uniform variables are read-only and are initialized either 450501e04c3fSmrg * directly by an application via API commands, or indirectly by 450601e04c3fSmrg * OpenGL." 450701e04c3fSmrg */ 450801e04c3fSmrg if (var->data.mode == ir_var_uniform) { 450901e04c3fSmrg state->check_version(120, 0, &initializer_loc, 451001e04c3fSmrg "cannot initialize uniform %s", 451101e04c3fSmrg var->name); 451201e04c3fSmrg } 451301e04c3fSmrg 451401e04c3fSmrg /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec: 451501e04c3fSmrg * 451601e04c3fSmrg * "Buffer variables cannot have initializers." 451701e04c3fSmrg */ 451801e04c3fSmrg if (var->data.mode == ir_var_shader_storage) { 451901e04c3fSmrg _mesa_glsl_error(&initializer_loc, state, 452001e04c3fSmrg "cannot initialize buffer variable %s", 452101e04c3fSmrg var->name); 452201e04c3fSmrg } 452301e04c3fSmrg 452401e04c3fSmrg /* From section 4.1.7 of the GLSL 4.40 spec: 452501e04c3fSmrg * 452601e04c3fSmrg * "Opaque variables [...] are initialized only through the 452701e04c3fSmrg * OpenGL API; they cannot be declared with an initializer in a 452801e04c3fSmrg * shader." 452901e04c3fSmrg * 453001e04c3fSmrg * From section 4.1.7 of the ARB_bindless_texture spec: 453101e04c3fSmrg * 453201e04c3fSmrg * "Samplers may be declared as shader inputs and outputs, as uniform 453301e04c3fSmrg * variables, as temporary variables, and as function parameters." 453401e04c3fSmrg * 453501e04c3fSmrg * From section 4.1.X of the ARB_bindless_texture spec: 453601e04c3fSmrg * 453701e04c3fSmrg * "Images may be declared as shader inputs and outputs, as uniform 453801e04c3fSmrg * variables, as temporary variables, and as function parameters." 453901e04c3fSmrg */ 454001e04c3fSmrg if (var->type->contains_atomic() || 454101e04c3fSmrg (!state->has_bindless() && var->type->contains_opaque())) { 454201e04c3fSmrg _mesa_glsl_error(&initializer_loc, state, 454301e04c3fSmrg "cannot initialize %s variable %s", 454401e04c3fSmrg var->name, state->has_bindless() ? "atomic" : "opaque"); 454501e04c3fSmrg } 454601e04c3fSmrg 454701e04c3fSmrg if ((var->data.mode == ir_var_shader_in) && (state->current_function == NULL)) { 454801e04c3fSmrg _mesa_glsl_error(&initializer_loc, state, 454901e04c3fSmrg "cannot initialize %s shader input / %s %s", 455001e04c3fSmrg _mesa_shader_stage_to_string(state->stage), 455101e04c3fSmrg (state->stage == MESA_SHADER_VERTEX) 455201e04c3fSmrg ? "attribute" : "varying", 455301e04c3fSmrg var->name); 455401e04c3fSmrg } 455501e04c3fSmrg 455601e04c3fSmrg if (var->data.mode == ir_var_shader_out && state->current_function == NULL) { 455701e04c3fSmrg _mesa_glsl_error(&initializer_loc, state, 455801e04c3fSmrg "cannot initialize %s shader output %s", 455901e04c3fSmrg _mesa_shader_stage_to_string(state->stage), 456001e04c3fSmrg var->name); 456101e04c3fSmrg } 456201e04c3fSmrg 456301e04c3fSmrg /* If the initializer is an ast_aggregate_initializer, recursively store 456401e04c3fSmrg * type information from the LHS into it, so that its hir() function can do 456501e04c3fSmrg * type checking. 456601e04c3fSmrg */ 456701e04c3fSmrg if (decl->initializer->oper == ast_aggregate) 456801e04c3fSmrg _mesa_ast_set_aggregate_type(var->type, decl->initializer); 456901e04c3fSmrg 457001e04c3fSmrg ir_dereference *const lhs = new(state) ir_dereference_variable(var); 457101e04c3fSmrg ir_rvalue *rhs = decl->initializer->hir(initializer_instructions, state); 457201e04c3fSmrg 457301e04c3fSmrg /* Calculate the constant value if this is a const or uniform 457401e04c3fSmrg * declaration. 457501e04c3fSmrg * 457601e04c3fSmrg * Section 4.3 (Storage Qualifiers) of the GLSL ES 1.00.17 spec says: 457701e04c3fSmrg * 457801e04c3fSmrg * "Declarations of globals without a storage qualifier, or with 457901e04c3fSmrg * just the const qualifier, may include initializers, in which case 458001e04c3fSmrg * they will be initialized before the first line of main() is 458101e04c3fSmrg * executed. Such initializers must be a constant expression." 458201e04c3fSmrg * 458301e04c3fSmrg * The same section of the GLSL ES 3.00.4 spec has similar language. 458401e04c3fSmrg */ 458501e04c3fSmrg if (type->qualifier.flags.q.constant 458601e04c3fSmrg || type->qualifier.flags.q.uniform 458701e04c3fSmrg || (state->es_shader && state->current_function == NULL)) { 458801e04c3fSmrg ir_rvalue *new_rhs = validate_assignment(state, initializer_loc, 458901e04c3fSmrg lhs, rhs, true); 459001e04c3fSmrg if (new_rhs != NULL) { 459101e04c3fSmrg rhs = new_rhs; 459201e04c3fSmrg 459301e04c3fSmrg /* Section 4.3.3 (Constant Expressions) of the GLSL ES 3.00.4 spec 459401e04c3fSmrg * says: 459501e04c3fSmrg * 459601e04c3fSmrg * "A constant expression is one of 459701e04c3fSmrg * 459801e04c3fSmrg * ... 459901e04c3fSmrg * 460001e04c3fSmrg * - an expression formed by an operator on operands that are 460101e04c3fSmrg * all constant expressions, including getting an element of 460201e04c3fSmrg * a constant array, or a field of a constant structure, or 460301e04c3fSmrg * components of a constant vector. However, the sequence 460401e04c3fSmrg * operator ( , ) and the assignment operators ( =, +=, ...) 460501e04c3fSmrg * are not included in the operators that can create a 460601e04c3fSmrg * constant expression." 460701e04c3fSmrg * 460801e04c3fSmrg * Section 12.43 (Sequence operator and constant expressions) says: 460901e04c3fSmrg * 461001e04c3fSmrg * "Should the following construct be allowed? 461101e04c3fSmrg * 461201e04c3fSmrg * float a[2,3]; 461301e04c3fSmrg * 461401e04c3fSmrg * The expression within the brackets uses the sequence operator 461501e04c3fSmrg * (',') and returns the integer 3 so the construct is declaring 461601e04c3fSmrg * a single-dimensional array of size 3. In some languages, the 461701e04c3fSmrg * construct declares a two-dimensional array. It would be 461801e04c3fSmrg * preferable to make this construct illegal to avoid confusion. 461901e04c3fSmrg * 462001e04c3fSmrg * One possibility is to change the definition of the sequence 462101e04c3fSmrg * operator so that it does not return a constant-expression and 462201e04c3fSmrg * hence cannot be used to declare an array size. 462301e04c3fSmrg * 462401e04c3fSmrg * RESOLUTION: The result of a sequence operator is not a 462501e04c3fSmrg * constant-expression." 462601e04c3fSmrg * 462701e04c3fSmrg * Section 4.3.3 (Constant Expressions) of the GLSL 4.30.9 spec 462801e04c3fSmrg * contains language almost identical to the section 4.3.3 in the 462901e04c3fSmrg * GLSL ES 3.00.4 spec. This is a new limitation for these GLSL 463001e04c3fSmrg * versions. 463101e04c3fSmrg */ 463201e04c3fSmrg ir_constant *constant_value = 463301e04c3fSmrg rhs->constant_expression_value(mem_ctx); 463401e04c3fSmrg 463501e04c3fSmrg if (!constant_value || 463601e04c3fSmrg (state->is_version(430, 300) && 463701e04c3fSmrg decl->initializer->has_sequence_subexpression())) { 463801e04c3fSmrg const char *const variable_mode = 463901e04c3fSmrg (type->qualifier.flags.q.constant) 464001e04c3fSmrg ? "const" 464101e04c3fSmrg : ((type->qualifier.flags.q.uniform) ? "uniform" : "global"); 464201e04c3fSmrg 464301e04c3fSmrg /* If ARB_shading_language_420pack is enabled, initializers of 464401e04c3fSmrg * const-qualified local variables do not have to be constant 464501e04c3fSmrg * expressions. Const-qualified global variables must still be 464601e04c3fSmrg * initialized with constant expressions. 464701e04c3fSmrg */ 464801e04c3fSmrg if (!state->has_420pack() 464901e04c3fSmrg || state->current_function == NULL) { 465001e04c3fSmrg _mesa_glsl_error(& initializer_loc, state, 465101e04c3fSmrg "initializer of %s variable `%s' must be a " 465201e04c3fSmrg "constant expression", 465301e04c3fSmrg variable_mode, 465401e04c3fSmrg decl->identifier); 465501e04c3fSmrg if (var->type->is_numeric()) { 465601e04c3fSmrg /* Reduce cascading errors. */ 465701e04c3fSmrg var->constant_value = type->qualifier.flags.q.constant 465801e04c3fSmrg ? ir_constant::zero(state, var->type) : NULL; 465901e04c3fSmrg } 466001e04c3fSmrg } 466101e04c3fSmrg } else { 466201e04c3fSmrg rhs = constant_value; 466301e04c3fSmrg var->constant_value = type->qualifier.flags.q.constant 466401e04c3fSmrg ? constant_value : NULL; 466501e04c3fSmrg } 466601e04c3fSmrg } else { 466701e04c3fSmrg if (var->type->is_numeric()) { 466801e04c3fSmrg /* Reduce cascading errors. */ 466901e04c3fSmrg rhs = var->constant_value = type->qualifier.flags.q.constant 467001e04c3fSmrg ? ir_constant::zero(state, var->type) : NULL; 467101e04c3fSmrg } 467201e04c3fSmrg } 467301e04c3fSmrg } 467401e04c3fSmrg 467501e04c3fSmrg if (rhs && !rhs->type->is_error()) { 467601e04c3fSmrg bool temp = var->data.read_only; 467701e04c3fSmrg if (type->qualifier.flags.q.constant) 467801e04c3fSmrg var->data.read_only = false; 467901e04c3fSmrg 468001e04c3fSmrg /* Never emit code to initialize a uniform. 468101e04c3fSmrg */ 468201e04c3fSmrg const glsl_type *initializer_type; 468301e04c3fSmrg bool error_emitted = false; 468401e04c3fSmrg if (!type->qualifier.flags.q.uniform) { 468501e04c3fSmrg error_emitted = 468601e04c3fSmrg do_assignment(initializer_instructions, state, 468701e04c3fSmrg NULL, lhs, rhs, 468801e04c3fSmrg &result, true, true, 468901e04c3fSmrg type->get_location()); 469001e04c3fSmrg initializer_type = result->type; 469101e04c3fSmrg } else 469201e04c3fSmrg initializer_type = rhs->type; 469301e04c3fSmrg 469401e04c3fSmrg if (!error_emitted) { 469501e04c3fSmrg var->constant_initializer = rhs->constant_expression_value(mem_ctx); 469601e04c3fSmrg var->data.has_initializer = true; 46977ec681f3Smrg var->data.is_implicit_initializer = false; 469801e04c3fSmrg 469901e04c3fSmrg /* If the declared variable is an unsized array, it must inherrit 470001e04c3fSmrg * its full type from the initializer. A declaration such as 470101e04c3fSmrg * 470201e04c3fSmrg * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0); 470301e04c3fSmrg * 470401e04c3fSmrg * becomes 470501e04c3fSmrg * 470601e04c3fSmrg * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0); 470701e04c3fSmrg * 470801e04c3fSmrg * The assignment generated in the if-statement (below) will also 470901e04c3fSmrg * automatically handle this case for non-uniforms. 471001e04c3fSmrg * 471101e04c3fSmrg * If the declared variable is not an array, the types must 471201e04c3fSmrg * already match exactly. As a result, the type assignment 471301e04c3fSmrg * here can be done unconditionally. For non-uniforms the call 471401e04c3fSmrg * to do_assignment can change the type of the initializer (via 471501e04c3fSmrg * the implicit conversion rules). For uniforms the initializer 471601e04c3fSmrg * must be a constant expression, and the type of that expression 471701e04c3fSmrg * was validated above. 471801e04c3fSmrg */ 471901e04c3fSmrg var->type = initializer_type; 472001e04c3fSmrg } 472101e04c3fSmrg 472201e04c3fSmrg var->data.read_only = temp; 472301e04c3fSmrg } 472401e04c3fSmrg 472501e04c3fSmrg return result; 472601e04c3fSmrg} 472701e04c3fSmrg 472801e04c3fSmrgstatic void 472901e04c3fSmrgvalidate_layout_qualifier_vertex_count(struct _mesa_glsl_parse_state *state, 473001e04c3fSmrg YYLTYPE loc, ir_variable *var, 473101e04c3fSmrg unsigned num_vertices, 473201e04c3fSmrg unsigned *size, 473301e04c3fSmrg const char *var_category) 473401e04c3fSmrg{ 473501e04c3fSmrg if (var->type->is_unsized_array()) { 473601e04c3fSmrg /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says: 473701e04c3fSmrg * 473801e04c3fSmrg * All geometry shader input unsized array declarations will be 473901e04c3fSmrg * sized by an earlier input layout qualifier, when present, as per 474001e04c3fSmrg * the following table. 474101e04c3fSmrg * 474201e04c3fSmrg * Followed by a table mapping each allowed input layout qualifier to 474301e04c3fSmrg * the corresponding input length. 474401e04c3fSmrg * 474501e04c3fSmrg * Similarly for tessellation control shader outputs. 474601e04c3fSmrg */ 474701e04c3fSmrg if (num_vertices != 0) 474801e04c3fSmrg var->type = glsl_type::get_array_instance(var->type->fields.array, 474901e04c3fSmrg num_vertices); 475001e04c3fSmrg } else { 475101e04c3fSmrg /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec 475201e04c3fSmrg * includes the following examples of compile-time errors: 475301e04c3fSmrg * 475401e04c3fSmrg * // code sequence within one shader... 475501e04c3fSmrg * in vec4 Color1[]; // size unknown 475601e04c3fSmrg * ...Color1.length()...// illegal, length() unknown 475701e04c3fSmrg * in vec4 Color2[2]; // size is 2 475801e04c3fSmrg * ...Color1.length()...// illegal, Color1 still has no size 475901e04c3fSmrg * in vec4 Color3[3]; // illegal, input sizes are inconsistent 476001e04c3fSmrg * layout(lines) in; // legal, input size is 2, matching 476101e04c3fSmrg * in vec4 Color4[3]; // illegal, contradicts layout 476201e04c3fSmrg * ... 476301e04c3fSmrg * 476401e04c3fSmrg * To detect the case illustrated by Color3, we verify that the size of 476501e04c3fSmrg * an explicitly-sized array matches the size of any previously declared 476601e04c3fSmrg * explicitly-sized array. To detect the case illustrated by Color4, we 476701e04c3fSmrg * verify that the size of an explicitly-sized array is consistent with 476801e04c3fSmrg * any previously declared input layout. 476901e04c3fSmrg */ 477001e04c3fSmrg if (num_vertices != 0 && var->type->length != num_vertices) { 477101e04c3fSmrg _mesa_glsl_error(&loc, state, 477201e04c3fSmrg "%s size contradicts previously declared layout " 477301e04c3fSmrg "(size is %u, but layout requires a size of %u)", 477401e04c3fSmrg var_category, var->type->length, num_vertices); 477501e04c3fSmrg } else if (*size != 0 && var->type->length != *size) { 477601e04c3fSmrg _mesa_glsl_error(&loc, state, 477701e04c3fSmrg "%s sizes are inconsistent (size is %u, but a " 477801e04c3fSmrg "previous declaration has size %u)", 477901e04c3fSmrg var_category, var->type->length, *size); 478001e04c3fSmrg } else { 478101e04c3fSmrg *size = var->type->length; 478201e04c3fSmrg } 478301e04c3fSmrg } 478401e04c3fSmrg} 478501e04c3fSmrg 478601e04c3fSmrgstatic void 478701e04c3fSmrghandle_tess_ctrl_shader_output_decl(struct _mesa_glsl_parse_state *state, 478801e04c3fSmrg YYLTYPE loc, ir_variable *var) 478901e04c3fSmrg{ 479001e04c3fSmrg unsigned num_vertices = 0; 479101e04c3fSmrg 479201e04c3fSmrg if (state->tcs_output_vertices_specified) { 479301e04c3fSmrg if (!state->out_qualifier->vertices-> 479401e04c3fSmrg process_qualifier_constant(state, "vertices", 479501e04c3fSmrg &num_vertices, false)) { 479601e04c3fSmrg return; 479701e04c3fSmrg } 479801e04c3fSmrg 479901e04c3fSmrg if (num_vertices > state->Const.MaxPatchVertices) { 480001e04c3fSmrg _mesa_glsl_error(&loc, state, "vertices (%d) exceeds " 480101e04c3fSmrg "GL_MAX_PATCH_VERTICES", num_vertices); 480201e04c3fSmrg return; 480301e04c3fSmrg } 480401e04c3fSmrg } 480501e04c3fSmrg 480601e04c3fSmrg if (!var->type->is_array() && !var->data.patch) { 480701e04c3fSmrg _mesa_glsl_error(&loc, state, 480801e04c3fSmrg "tessellation control shader outputs must be arrays"); 480901e04c3fSmrg 481001e04c3fSmrg /* To avoid cascading failures, short circuit the checks below. */ 481101e04c3fSmrg return; 481201e04c3fSmrg } 481301e04c3fSmrg 481401e04c3fSmrg if (var->data.patch) 481501e04c3fSmrg return; 481601e04c3fSmrg 481701e04c3fSmrg validate_layout_qualifier_vertex_count(state, loc, var, num_vertices, 481801e04c3fSmrg &state->tcs_output_size, 481901e04c3fSmrg "tessellation control shader output"); 482001e04c3fSmrg} 482101e04c3fSmrg 482201e04c3fSmrg/** 482301e04c3fSmrg * Do additional processing necessary for tessellation control/evaluation shader 482401e04c3fSmrg * input declarations. This covers both interface block arrays and bare input 482501e04c3fSmrg * variables. 482601e04c3fSmrg */ 482701e04c3fSmrgstatic void 482801e04c3fSmrghandle_tess_shader_input_decl(struct _mesa_glsl_parse_state *state, 482901e04c3fSmrg YYLTYPE loc, ir_variable *var) 483001e04c3fSmrg{ 483101e04c3fSmrg if (!var->type->is_array() && !var->data.patch) { 483201e04c3fSmrg _mesa_glsl_error(&loc, state, 483301e04c3fSmrg "per-vertex tessellation shader inputs must be arrays"); 483401e04c3fSmrg /* Avoid cascading failures. */ 483501e04c3fSmrg return; 483601e04c3fSmrg } 483701e04c3fSmrg 483801e04c3fSmrg if (var->data.patch) 483901e04c3fSmrg return; 484001e04c3fSmrg 484101e04c3fSmrg /* The ARB_tessellation_shader spec says: 484201e04c3fSmrg * 484301e04c3fSmrg * "Declaring an array size is optional. If no size is specified, it 484401e04c3fSmrg * will be taken from the implementation-dependent maximum patch size 484501e04c3fSmrg * (gl_MaxPatchVertices). If a size is specified, it must match the 484601e04c3fSmrg * maximum patch size; otherwise, a compile or link error will occur." 484701e04c3fSmrg * 484801e04c3fSmrg * This text appears twice, once for TCS inputs, and again for TES inputs. 484901e04c3fSmrg */ 485001e04c3fSmrg if (var->type->is_unsized_array()) { 485101e04c3fSmrg var->type = glsl_type::get_array_instance(var->type->fields.array, 485201e04c3fSmrg state->Const.MaxPatchVertices); 485301e04c3fSmrg } else if (var->type->length != state->Const.MaxPatchVertices) { 485401e04c3fSmrg _mesa_glsl_error(&loc, state, 485501e04c3fSmrg "per-vertex tessellation shader input arrays must be " 485601e04c3fSmrg "sized to gl_MaxPatchVertices (%d).", 485701e04c3fSmrg state->Const.MaxPatchVertices); 485801e04c3fSmrg } 485901e04c3fSmrg} 486001e04c3fSmrg 486101e04c3fSmrg 486201e04c3fSmrg/** 486301e04c3fSmrg * Do additional processing necessary for geometry shader input declarations 486401e04c3fSmrg * (this covers both interface blocks arrays and bare input variables). 486501e04c3fSmrg */ 486601e04c3fSmrgstatic void 486701e04c3fSmrghandle_geometry_shader_input_decl(struct _mesa_glsl_parse_state *state, 486801e04c3fSmrg YYLTYPE loc, ir_variable *var) 486901e04c3fSmrg{ 487001e04c3fSmrg unsigned num_vertices = 0; 487101e04c3fSmrg 487201e04c3fSmrg if (state->gs_input_prim_type_specified) { 487301e04c3fSmrg num_vertices = vertices_per_prim(state->in_qualifier->prim_type); 487401e04c3fSmrg } 487501e04c3fSmrg 487601e04c3fSmrg /* Geometry shader input variables must be arrays. Caller should have 487701e04c3fSmrg * reported an error for this. 487801e04c3fSmrg */ 487901e04c3fSmrg if (!var->type->is_array()) { 488001e04c3fSmrg assert(state->error); 488101e04c3fSmrg 488201e04c3fSmrg /* To avoid cascading failures, short circuit the checks below. */ 488301e04c3fSmrg return; 488401e04c3fSmrg } 488501e04c3fSmrg 488601e04c3fSmrg validate_layout_qualifier_vertex_count(state, loc, var, num_vertices, 488701e04c3fSmrg &state->gs_input_size, 488801e04c3fSmrg "geometry shader input"); 488901e04c3fSmrg} 489001e04c3fSmrg 489101e04c3fSmrgstatic void 489201e04c3fSmrgvalidate_identifier(const char *identifier, YYLTYPE loc, 489301e04c3fSmrg struct _mesa_glsl_parse_state *state) 489401e04c3fSmrg{ 489501e04c3fSmrg /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec, 489601e04c3fSmrg * 489701e04c3fSmrg * "Identifiers starting with "gl_" are reserved for use by 489801e04c3fSmrg * OpenGL, and may not be declared in a shader as either a 489901e04c3fSmrg * variable or a function." 490001e04c3fSmrg */ 490101e04c3fSmrg if (is_gl_identifier(identifier)) { 490201e04c3fSmrg _mesa_glsl_error(&loc, state, 490301e04c3fSmrg "identifier `%s' uses reserved `gl_' prefix", 490401e04c3fSmrg identifier); 490501e04c3fSmrg } else if (strstr(identifier, "__")) { 490601e04c3fSmrg /* From page 14 (page 20 of the PDF) of the GLSL 1.10 490701e04c3fSmrg * spec: 490801e04c3fSmrg * 490901e04c3fSmrg * "In addition, all identifiers containing two 491001e04c3fSmrg * consecutive underscores (__) are reserved as 491101e04c3fSmrg * possible future keywords." 491201e04c3fSmrg * 491301e04c3fSmrg * The intention is that names containing __ are reserved for internal 491401e04c3fSmrg * use by the implementation, and names prefixed with GL_ are reserved 491501e04c3fSmrg * for use by Khronos. Names simply containing __ are dangerous to use, 491601e04c3fSmrg * but should be allowed. 491701e04c3fSmrg * 491801e04c3fSmrg * A future version of the GLSL specification will clarify this. 491901e04c3fSmrg */ 492001e04c3fSmrg _mesa_glsl_warning(&loc, state, 492101e04c3fSmrg "identifier `%s' uses reserved `__' string", 492201e04c3fSmrg identifier); 492301e04c3fSmrg } 492401e04c3fSmrg} 492501e04c3fSmrg 492601e04c3fSmrgir_rvalue * 492701e04c3fSmrgast_declarator_list::hir(exec_list *instructions, 492801e04c3fSmrg struct _mesa_glsl_parse_state *state) 492901e04c3fSmrg{ 493001e04c3fSmrg void *ctx = state; 493101e04c3fSmrg const struct glsl_type *decl_type; 493201e04c3fSmrg const char *type_name = NULL; 493301e04c3fSmrg ir_rvalue *result = NULL; 493401e04c3fSmrg YYLTYPE loc = this->get_location(); 493501e04c3fSmrg 493601e04c3fSmrg /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec: 493701e04c3fSmrg * 493801e04c3fSmrg * "To ensure that a particular output variable is invariant, it is 493901e04c3fSmrg * necessary to use the invariant qualifier. It can either be used to 494001e04c3fSmrg * qualify a previously declared variable as being invariant 494101e04c3fSmrg * 494201e04c3fSmrg * invariant gl_Position; // make existing gl_Position be invariant" 494301e04c3fSmrg * 494401e04c3fSmrg * In these cases the parser will set the 'invariant' flag in the declarator 494501e04c3fSmrg * list, and the type will be NULL. 494601e04c3fSmrg */ 494701e04c3fSmrg if (this->invariant) { 494801e04c3fSmrg assert(this->type == NULL); 494901e04c3fSmrg 495001e04c3fSmrg if (state->current_function != NULL) { 495101e04c3fSmrg _mesa_glsl_error(& loc, state, 495201e04c3fSmrg "all uses of `invariant' keyword must be at global " 495301e04c3fSmrg "scope"); 495401e04c3fSmrg } 495501e04c3fSmrg 495601e04c3fSmrg foreach_list_typed (ast_declaration, decl, link, &this->declarations) { 495701e04c3fSmrg assert(decl->array_specifier == NULL); 495801e04c3fSmrg assert(decl->initializer == NULL); 495901e04c3fSmrg 496001e04c3fSmrg ir_variable *const earlier = 496101e04c3fSmrg state->symbols->get_variable(decl->identifier); 496201e04c3fSmrg if (earlier == NULL) { 496301e04c3fSmrg _mesa_glsl_error(& loc, state, 496401e04c3fSmrg "undeclared variable `%s' cannot be marked " 496501e04c3fSmrg "invariant", decl->identifier); 496601e04c3fSmrg } else if (!is_allowed_invariant(earlier, state)) { 496701e04c3fSmrg _mesa_glsl_error(&loc, state, 496801e04c3fSmrg "`%s' cannot be marked invariant; interfaces between " 496901e04c3fSmrg "shader stages only.", decl->identifier); 497001e04c3fSmrg } else if (earlier->data.used) { 497101e04c3fSmrg _mesa_glsl_error(& loc, state, 497201e04c3fSmrg "variable `%s' may not be redeclared " 497301e04c3fSmrg "`invariant' after being used", 497401e04c3fSmrg earlier->name); 497501e04c3fSmrg } else { 4976993e1d59Smrg earlier->data.explicit_invariant = true; 497701e04c3fSmrg earlier->data.invariant = true; 497801e04c3fSmrg } 497901e04c3fSmrg } 498001e04c3fSmrg 498101e04c3fSmrg /* Invariant redeclarations do not have r-values. 498201e04c3fSmrg */ 498301e04c3fSmrg return NULL; 498401e04c3fSmrg } 498501e04c3fSmrg 498601e04c3fSmrg if (this->precise) { 498701e04c3fSmrg assert(this->type == NULL); 498801e04c3fSmrg 498901e04c3fSmrg foreach_list_typed (ast_declaration, decl, link, &this->declarations) { 499001e04c3fSmrg assert(decl->array_specifier == NULL); 499101e04c3fSmrg assert(decl->initializer == NULL); 499201e04c3fSmrg 499301e04c3fSmrg ir_variable *const earlier = 499401e04c3fSmrg state->symbols->get_variable(decl->identifier); 499501e04c3fSmrg if (earlier == NULL) { 499601e04c3fSmrg _mesa_glsl_error(& loc, state, 499701e04c3fSmrg "undeclared variable `%s' cannot be marked " 499801e04c3fSmrg "precise", decl->identifier); 499901e04c3fSmrg } else if (state->current_function != NULL && 500001e04c3fSmrg !state->symbols->name_declared_this_scope(decl->identifier)) { 500101e04c3fSmrg /* Note: we have to check if we're in a function, since 500201e04c3fSmrg * builtins are treated as having come from another scope. 500301e04c3fSmrg */ 500401e04c3fSmrg _mesa_glsl_error(& loc, state, 500501e04c3fSmrg "variable `%s' from an outer scope may not be " 500601e04c3fSmrg "redeclared `precise' in this scope", 500701e04c3fSmrg earlier->name); 500801e04c3fSmrg } else if (earlier->data.used) { 500901e04c3fSmrg _mesa_glsl_error(& loc, state, 501001e04c3fSmrg "variable `%s' may not be redeclared " 501101e04c3fSmrg "`precise' after being used", 501201e04c3fSmrg earlier->name); 501301e04c3fSmrg } else { 501401e04c3fSmrg earlier->data.precise = true; 501501e04c3fSmrg } 501601e04c3fSmrg } 501701e04c3fSmrg 501801e04c3fSmrg /* Precise redeclarations do not have r-values either. */ 501901e04c3fSmrg return NULL; 502001e04c3fSmrg } 502101e04c3fSmrg 502201e04c3fSmrg assert(this->type != NULL); 502301e04c3fSmrg assert(!this->invariant); 502401e04c3fSmrg assert(!this->precise); 502501e04c3fSmrg 50267ec681f3Smrg /* GL_EXT_shader_image_load_store base type uses GLSL_TYPE_VOID as a special value to 50277ec681f3Smrg * indicate that it needs to be updated later (see glsl_parser.yy). 50287ec681f3Smrg * This is done here, based on the layout qualifier and the type of the image var 50297ec681f3Smrg */ 50307ec681f3Smrg if (this->type->qualifier.flags.q.explicit_image_format && 50317ec681f3Smrg this->type->specifier->type->is_image() && 50327ec681f3Smrg this->type->qualifier.image_base_type == GLSL_TYPE_VOID) { 50337ec681f3Smrg /* "The ARB_shader_image_load_store says: 50347ec681f3Smrg * If both extensions are enabled in the shading language, the "size*" layout 50357ec681f3Smrg * qualifiers are treated as format qualifiers, and are mapped to equivalent 50367ec681f3Smrg * format qualifiers in the table below, according to the type of image 50377ec681f3Smrg * variable. 50387ec681f3Smrg * image* iimage* uimage* 50397ec681f3Smrg * -------- -------- -------- 50407ec681f3Smrg * size1x8 n/a r8i r8ui 50417ec681f3Smrg * size1x16 r16f r16i r16ui 50427ec681f3Smrg * size1x32 r32f r32i r32ui 50437ec681f3Smrg * size2x32 rg32f rg32i rg32ui 50447ec681f3Smrg * size4x32 rgba32f rgba32i rgba32ui" 50457ec681f3Smrg */ 50467ec681f3Smrg if (strncmp(this->type->specifier->type_name, "image", strlen("image")) == 0) { 50477ec681f3Smrg switch (this->type->qualifier.image_format) { 50487ec681f3Smrg case PIPE_FORMAT_R8_SINT: 50497ec681f3Smrg /* The GL_EXT_shader_image_load_store spec says: 50507ec681f3Smrg * A layout of "size1x8" is illegal for image variables associated 50517ec681f3Smrg * with floating-point data types. 50527ec681f3Smrg */ 50537ec681f3Smrg _mesa_glsl_error(& loc, state, 50547ec681f3Smrg "size1x8 is illegal for image variables " 50557ec681f3Smrg "with floating-point data types."); 50567ec681f3Smrg return NULL; 50577ec681f3Smrg case PIPE_FORMAT_R16_SINT: 50587ec681f3Smrg this->type->qualifier.image_format = PIPE_FORMAT_R16_FLOAT; 50597ec681f3Smrg break; 50607ec681f3Smrg case PIPE_FORMAT_R32_SINT: 50617ec681f3Smrg this->type->qualifier.image_format = PIPE_FORMAT_R32_FLOAT; 50627ec681f3Smrg break; 50637ec681f3Smrg case PIPE_FORMAT_R32G32_SINT: 50647ec681f3Smrg this->type->qualifier.image_format = PIPE_FORMAT_R32G32_FLOAT; 50657ec681f3Smrg break; 50667ec681f3Smrg case PIPE_FORMAT_R32G32B32A32_SINT: 50677ec681f3Smrg this->type->qualifier.image_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 50687ec681f3Smrg break; 50697ec681f3Smrg default: 50707ec681f3Smrg unreachable("Unknown image format"); 50717ec681f3Smrg } 50727ec681f3Smrg this->type->qualifier.image_base_type = GLSL_TYPE_FLOAT; 50737ec681f3Smrg } else if (strncmp(this->type->specifier->type_name, "uimage", strlen("uimage")) == 0) { 50747ec681f3Smrg switch (this->type->qualifier.image_format) { 50757ec681f3Smrg case PIPE_FORMAT_R8_SINT: 50767ec681f3Smrg this->type->qualifier.image_format = PIPE_FORMAT_R8_UINT; 50777ec681f3Smrg break; 50787ec681f3Smrg case PIPE_FORMAT_R16_SINT: 50797ec681f3Smrg this->type->qualifier.image_format = PIPE_FORMAT_R16_UINT; 50807ec681f3Smrg break; 50817ec681f3Smrg case PIPE_FORMAT_R32_SINT: 50827ec681f3Smrg this->type->qualifier.image_format = PIPE_FORMAT_R32_UINT; 50837ec681f3Smrg break; 50847ec681f3Smrg case PIPE_FORMAT_R32G32_SINT: 50857ec681f3Smrg this->type->qualifier.image_format = PIPE_FORMAT_R32G32_UINT; 50867ec681f3Smrg break; 50877ec681f3Smrg case PIPE_FORMAT_R32G32B32A32_SINT: 50887ec681f3Smrg this->type->qualifier.image_format = PIPE_FORMAT_R32G32B32A32_UINT; 50897ec681f3Smrg break; 50907ec681f3Smrg default: 50917ec681f3Smrg unreachable("Unknown image format"); 50927ec681f3Smrg } 50937ec681f3Smrg this->type->qualifier.image_base_type = GLSL_TYPE_UINT; 50947ec681f3Smrg } else if (strncmp(this->type->specifier->type_name, "iimage", strlen("iimage")) == 0) { 50957ec681f3Smrg this->type->qualifier.image_base_type = GLSL_TYPE_INT; 50967ec681f3Smrg } else { 50977ec681f3Smrg assert(false); 50987ec681f3Smrg } 50997ec681f3Smrg } 51007ec681f3Smrg 510101e04c3fSmrg /* The type specifier may contain a structure definition. Process that 510201e04c3fSmrg * before any of the variable declarations. 510301e04c3fSmrg */ 510401e04c3fSmrg (void) this->type->specifier->hir(instructions, state); 510501e04c3fSmrg 510601e04c3fSmrg decl_type = this->type->glsl_type(& type_name, state); 510701e04c3fSmrg 510801e04c3fSmrg /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec: 510901e04c3fSmrg * "Buffer variables may only be declared inside interface blocks 511001e04c3fSmrg * (section 4.3.9 “Interface Blocks”), which are then referred to as 511101e04c3fSmrg * shader storage blocks. It is a compile-time error to declare buffer 511201e04c3fSmrg * variables at global scope (outside a block)." 511301e04c3fSmrg */ 511401e04c3fSmrg if (type->qualifier.flags.q.buffer && !decl_type->is_interface()) { 511501e04c3fSmrg _mesa_glsl_error(&loc, state, 511601e04c3fSmrg "buffer variables cannot be declared outside " 511701e04c3fSmrg "interface blocks"); 511801e04c3fSmrg } 511901e04c3fSmrg 512001e04c3fSmrg /* An offset-qualified atomic counter declaration sets the default 512101e04c3fSmrg * offset for the next declaration within the same atomic counter 512201e04c3fSmrg * buffer. 512301e04c3fSmrg */ 512401e04c3fSmrg if (decl_type && decl_type->contains_atomic()) { 512501e04c3fSmrg if (type->qualifier.flags.q.explicit_binding && 512601e04c3fSmrg type->qualifier.flags.q.explicit_offset) { 512701e04c3fSmrg unsigned qual_binding; 512801e04c3fSmrg unsigned qual_offset; 512901e04c3fSmrg if (process_qualifier_constant(state, &loc, "binding", 513001e04c3fSmrg type->qualifier.binding, 513101e04c3fSmrg &qual_binding) 513201e04c3fSmrg && process_qualifier_constant(state, &loc, "offset", 513301e04c3fSmrg type->qualifier.offset, 513401e04c3fSmrg &qual_offset)) { 5135ed98bd31Smaya if (qual_binding < ARRAY_SIZE(state->atomic_counter_offsets)) 5136ed98bd31Smaya state->atomic_counter_offsets[qual_binding] = qual_offset; 513701e04c3fSmrg } 513801e04c3fSmrg } 513901e04c3fSmrg 514001e04c3fSmrg ast_type_qualifier allowed_atomic_qual_mask; 514101e04c3fSmrg allowed_atomic_qual_mask.flags.i = 0; 514201e04c3fSmrg allowed_atomic_qual_mask.flags.q.explicit_binding = 1; 514301e04c3fSmrg allowed_atomic_qual_mask.flags.q.explicit_offset = 1; 514401e04c3fSmrg allowed_atomic_qual_mask.flags.q.uniform = 1; 514501e04c3fSmrg 514601e04c3fSmrg type->qualifier.validate_flags(&loc, state, allowed_atomic_qual_mask, 514701e04c3fSmrg "invalid layout qualifier for", 514801e04c3fSmrg "atomic_uint"); 514901e04c3fSmrg } 515001e04c3fSmrg 515101e04c3fSmrg if (this->declarations.is_empty()) { 515201e04c3fSmrg /* If there is no structure involved in the program text, there are two 515301e04c3fSmrg * possible scenarios: 515401e04c3fSmrg * 515501e04c3fSmrg * - The program text contained something like 'vec4;'. This is an 515601e04c3fSmrg * empty declaration. It is valid but weird. Emit a warning. 515701e04c3fSmrg * 515801e04c3fSmrg * - The program text contained something like 'S;' and 'S' is not the 515901e04c3fSmrg * name of a known structure type. This is both invalid and weird. 516001e04c3fSmrg * Emit an error. 516101e04c3fSmrg * 516201e04c3fSmrg * - The program text contained something like 'mediump float;' 516301e04c3fSmrg * when the programmer probably meant 'precision mediump 516401e04c3fSmrg * float;' Emit a warning with a description of what they 516501e04c3fSmrg * probably meant to do. 516601e04c3fSmrg * 516701e04c3fSmrg * Note that if decl_type is NULL and there is a structure involved, 516801e04c3fSmrg * there must have been some sort of error with the structure. In this 516901e04c3fSmrg * case we assume that an error was already generated on this line of 517001e04c3fSmrg * code for the structure. There is no need to generate an additional, 517101e04c3fSmrg * confusing error. 517201e04c3fSmrg */ 517301e04c3fSmrg assert(this->type->specifier->structure == NULL || decl_type != NULL 517401e04c3fSmrg || state->error); 517501e04c3fSmrg 517601e04c3fSmrg if (decl_type == NULL) { 517701e04c3fSmrg _mesa_glsl_error(&loc, state, 517801e04c3fSmrg "invalid type `%s' in empty declaration", 517901e04c3fSmrg type_name); 518001e04c3fSmrg } else { 518101e04c3fSmrg if (decl_type->is_array()) { 518201e04c3fSmrg /* From Section 13.22 (Array Declarations) of the GLSL ES 3.2 518301e04c3fSmrg * spec: 518401e04c3fSmrg * 518501e04c3fSmrg * "... any declaration that leaves the size undefined is 518601e04c3fSmrg * disallowed as this would add complexity and there are no 518701e04c3fSmrg * use-cases." 518801e04c3fSmrg */ 518901e04c3fSmrg if (state->es_shader && decl_type->is_unsized_array()) { 519001e04c3fSmrg _mesa_glsl_error(&loc, state, "array size must be explicitly " 519101e04c3fSmrg "or implicitly defined"); 519201e04c3fSmrg } 519301e04c3fSmrg 519401e04c3fSmrg /* From Section 4.12 (Empty Declarations) of the GLSL 4.5 spec: 519501e04c3fSmrg * 519601e04c3fSmrg * "The combinations of types and qualifiers that cause 519701e04c3fSmrg * compile-time or link-time errors are the same whether or not 519801e04c3fSmrg * the declaration is empty." 519901e04c3fSmrg */ 520001e04c3fSmrg validate_array_dimensions(decl_type, state, &loc); 520101e04c3fSmrg } 520201e04c3fSmrg 520301e04c3fSmrg if (decl_type->is_atomic_uint()) { 520401e04c3fSmrg /* Empty atomic counter declarations are allowed and useful 520501e04c3fSmrg * to set the default offset qualifier. 520601e04c3fSmrg */ 520701e04c3fSmrg return NULL; 520801e04c3fSmrg } else if (this->type->qualifier.precision != ast_precision_none) { 520901e04c3fSmrg if (this->type->specifier->structure != NULL) { 521001e04c3fSmrg _mesa_glsl_error(&loc, state, 521101e04c3fSmrg "precision qualifiers can't be applied " 521201e04c3fSmrg "to structures"); 521301e04c3fSmrg } else { 521401e04c3fSmrg static const char *const precision_names[] = { 521501e04c3fSmrg "highp", 521601e04c3fSmrg "highp", 521701e04c3fSmrg "mediump", 521801e04c3fSmrg "lowp" 521901e04c3fSmrg }; 522001e04c3fSmrg 522101e04c3fSmrg _mesa_glsl_warning(&loc, state, 522201e04c3fSmrg "empty declaration with precision " 522301e04c3fSmrg "qualifier, to set the default precision, " 522401e04c3fSmrg "use `precision %s %s;'", 522501e04c3fSmrg precision_names[this->type-> 522601e04c3fSmrg qualifier.precision], 522701e04c3fSmrg type_name); 522801e04c3fSmrg } 522901e04c3fSmrg } else if (this->type->specifier->structure == NULL) { 523001e04c3fSmrg _mesa_glsl_warning(&loc, state, "empty declaration"); 523101e04c3fSmrg } 523201e04c3fSmrg } 523301e04c3fSmrg } 523401e04c3fSmrg 523501e04c3fSmrg foreach_list_typed (ast_declaration, decl, link, &this->declarations) { 523601e04c3fSmrg const struct glsl_type *var_type; 523701e04c3fSmrg ir_variable *var; 523801e04c3fSmrg const char *identifier = decl->identifier; 523901e04c3fSmrg /* FINISHME: Emit a warning if a variable declaration shadows a 524001e04c3fSmrg * FINISHME: declaration at a higher scope. 524101e04c3fSmrg */ 524201e04c3fSmrg 524301e04c3fSmrg if ((decl_type == NULL) || decl_type->is_void()) { 524401e04c3fSmrg if (type_name != NULL) { 524501e04c3fSmrg _mesa_glsl_error(& loc, state, 524601e04c3fSmrg "invalid type `%s' in declaration of `%s'", 524701e04c3fSmrg type_name, decl->identifier); 524801e04c3fSmrg } else { 524901e04c3fSmrg _mesa_glsl_error(& loc, state, 525001e04c3fSmrg "invalid type in declaration of `%s'", 525101e04c3fSmrg decl->identifier); 525201e04c3fSmrg } 525301e04c3fSmrg continue; 525401e04c3fSmrg } 525501e04c3fSmrg 525601e04c3fSmrg if (this->type->qualifier.is_subroutine_decl()) { 525701e04c3fSmrg const glsl_type *t; 525801e04c3fSmrg const char *name; 525901e04c3fSmrg 526001e04c3fSmrg t = state->symbols->get_type(this->type->specifier->type_name); 526101e04c3fSmrg if (!t) 526201e04c3fSmrg _mesa_glsl_error(& loc, state, 526301e04c3fSmrg "invalid type in declaration of `%s'", 526401e04c3fSmrg decl->identifier); 526501e04c3fSmrg name = ralloc_asprintf(ctx, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state->stage), decl->identifier); 526601e04c3fSmrg 526701e04c3fSmrg identifier = name; 526801e04c3fSmrg 526901e04c3fSmrg } 527001e04c3fSmrg var_type = process_array_type(&loc, decl_type, decl->array_specifier, 527101e04c3fSmrg state); 527201e04c3fSmrg 527301e04c3fSmrg var = new(ctx) ir_variable(var_type, identifier, ir_var_auto); 527401e04c3fSmrg 527501e04c3fSmrg /* The 'varying in' and 'varying out' qualifiers can only be used with 527601e04c3fSmrg * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support 527701e04c3fSmrg * yet. 527801e04c3fSmrg */ 527901e04c3fSmrg if (this->type->qualifier.flags.q.varying) { 528001e04c3fSmrg if (this->type->qualifier.flags.q.in) { 528101e04c3fSmrg _mesa_glsl_error(& loc, state, 528201e04c3fSmrg "`varying in' qualifier in declaration of " 528301e04c3fSmrg "`%s' only valid for geometry shaders using " 528401e04c3fSmrg "ARB_geometry_shader4 or EXT_geometry_shader4", 528501e04c3fSmrg decl->identifier); 528601e04c3fSmrg } else if (this->type->qualifier.flags.q.out) { 528701e04c3fSmrg _mesa_glsl_error(& loc, state, 528801e04c3fSmrg "`varying out' qualifier in declaration of " 528901e04c3fSmrg "`%s' only valid for geometry shaders using " 529001e04c3fSmrg "ARB_geometry_shader4 or EXT_geometry_shader4", 529101e04c3fSmrg decl->identifier); 529201e04c3fSmrg } 529301e04c3fSmrg } 529401e04c3fSmrg 529501e04c3fSmrg /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification; 529601e04c3fSmrg * 529701e04c3fSmrg * "Global variables can only use the qualifiers const, 529801e04c3fSmrg * attribute, uniform, or varying. Only one may be 529901e04c3fSmrg * specified. 530001e04c3fSmrg * 530101e04c3fSmrg * Local variables can only use the qualifier const." 530201e04c3fSmrg * 530301e04c3fSmrg * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by 530401e04c3fSmrg * any extension that adds the 'layout' keyword. 530501e04c3fSmrg */ 530601e04c3fSmrg if (!state->is_version(130, 300) 530701e04c3fSmrg && !state->has_explicit_attrib_location() 530801e04c3fSmrg && !state->has_separate_shader_objects() 530901e04c3fSmrg && !state->ARB_fragment_coord_conventions_enable) { 5310ed98bd31Smaya /* GL_EXT_gpu_shader4 only allows "varying out" on fragment shader 5311ed98bd31Smaya * outputs. (the varying flag is not set by the parser) 5312ed98bd31Smaya */ 5313ed98bd31Smaya if (this->type->qualifier.flags.q.out && 5314ed98bd31Smaya (!state->EXT_gpu_shader4_enable || 5315ed98bd31Smaya state->stage != MESA_SHADER_FRAGMENT)) { 531601e04c3fSmrg _mesa_glsl_error(& loc, state, 531701e04c3fSmrg "`out' qualifier in declaration of `%s' " 531801e04c3fSmrg "only valid for function parameters in %s", 531901e04c3fSmrg decl->identifier, state->get_version_string()); 532001e04c3fSmrg } 532101e04c3fSmrg if (this->type->qualifier.flags.q.in) { 532201e04c3fSmrg _mesa_glsl_error(& loc, state, 532301e04c3fSmrg "`in' qualifier in declaration of `%s' " 532401e04c3fSmrg "only valid for function parameters in %s", 532501e04c3fSmrg decl->identifier, state->get_version_string()); 532601e04c3fSmrg } 532701e04c3fSmrg /* FINISHME: Test for other invalid qualifiers. */ 532801e04c3fSmrg } 532901e04c3fSmrg 533001e04c3fSmrg apply_type_qualifier_to_variable(& this->type->qualifier, var, state, 533101e04c3fSmrg & loc, false); 533201e04c3fSmrg apply_layout_qualifier_to_variable(&this->type->qualifier, var, state, 533301e04c3fSmrg &loc); 533401e04c3fSmrg 53357ec681f3Smrg if ((state->zero_init & (1u << var->data.mode)) && 53367ec681f3Smrg (var->type->is_numeric() || var->type->is_boolean())) { 533701e04c3fSmrg const ir_constant_data data = { { 0 } }; 533801e04c3fSmrg var->data.has_initializer = true; 53397ec681f3Smrg var->data.is_implicit_initializer = true; 534001e04c3fSmrg var->constant_initializer = new(var) ir_constant(var->type, &data); 534101e04c3fSmrg } 534201e04c3fSmrg 534301e04c3fSmrg if (this->type->qualifier.flags.q.invariant) { 534401e04c3fSmrg if (!is_allowed_invariant(var, state)) { 534501e04c3fSmrg _mesa_glsl_error(&loc, state, 534601e04c3fSmrg "`%s' cannot be marked invariant; interfaces between " 534701e04c3fSmrg "shader stages only", var->name); 534801e04c3fSmrg } 534901e04c3fSmrg } 535001e04c3fSmrg 535101e04c3fSmrg if (state->current_function != NULL) { 535201e04c3fSmrg const char *mode = NULL; 535301e04c3fSmrg const char *extra = ""; 535401e04c3fSmrg 535501e04c3fSmrg /* There is no need to check for 'inout' here because the parser will 535601e04c3fSmrg * only allow that in function parameter lists. 535701e04c3fSmrg */ 535801e04c3fSmrg if (this->type->qualifier.flags.q.attribute) { 535901e04c3fSmrg mode = "attribute"; 536001e04c3fSmrg } else if (this->type->qualifier.is_subroutine_decl()) { 536101e04c3fSmrg mode = "subroutine uniform"; 536201e04c3fSmrg } else if (this->type->qualifier.flags.q.uniform) { 536301e04c3fSmrg mode = "uniform"; 536401e04c3fSmrg } else if (this->type->qualifier.flags.q.varying) { 536501e04c3fSmrg mode = "varying"; 536601e04c3fSmrg } else if (this->type->qualifier.flags.q.in) { 536701e04c3fSmrg mode = "in"; 536801e04c3fSmrg extra = " or in function parameter list"; 536901e04c3fSmrg } else if (this->type->qualifier.flags.q.out) { 537001e04c3fSmrg mode = "out"; 537101e04c3fSmrg extra = " or in function parameter list"; 537201e04c3fSmrg } 537301e04c3fSmrg 537401e04c3fSmrg if (mode) { 537501e04c3fSmrg _mesa_glsl_error(& loc, state, 537601e04c3fSmrg "%s variable `%s' must be declared at " 537701e04c3fSmrg "global scope%s", 537801e04c3fSmrg mode, var->name, extra); 537901e04c3fSmrg } 538001e04c3fSmrg } else if (var->data.mode == ir_var_shader_in) { 538101e04c3fSmrg var->data.read_only = true; 538201e04c3fSmrg 538301e04c3fSmrg if (state->stage == MESA_SHADER_VERTEX) { 538401e04c3fSmrg /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec: 538501e04c3fSmrg * 538601e04c3fSmrg * "Vertex shader inputs can only be float, floating-point 538701e04c3fSmrg * vectors, matrices, signed and unsigned integers and integer 538801e04c3fSmrg * vectors. Vertex shader inputs can also form arrays of these 538901e04c3fSmrg * types, but not structures." 539001e04c3fSmrg * 539101e04c3fSmrg * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec: 539201e04c3fSmrg * 539301e04c3fSmrg * "Vertex shader inputs can only be float, floating-point 539401e04c3fSmrg * vectors, matrices, signed and unsigned integers and integer 539501e04c3fSmrg * vectors. They cannot be arrays or structures." 539601e04c3fSmrg * 539701e04c3fSmrg * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec: 539801e04c3fSmrg * 539901e04c3fSmrg * "The attribute qualifier can be used only with float, 540001e04c3fSmrg * floating-point vectors, and matrices. Attribute variables 540101e04c3fSmrg * cannot be declared as arrays or structures." 540201e04c3fSmrg * 540301e04c3fSmrg * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec: 540401e04c3fSmrg * 540501e04c3fSmrg * "Vertex shader inputs can only be float, floating-point 540601e04c3fSmrg * vectors, matrices, signed and unsigned integers and integer 540701e04c3fSmrg * vectors. Vertex shader inputs cannot be arrays or 540801e04c3fSmrg * structures." 540901e04c3fSmrg * 541001e04c3fSmrg * From section 4.3.4 of the ARB_bindless_texture spec: 541101e04c3fSmrg * 541201e04c3fSmrg * "(modify third paragraph of the section to allow sampler and 541301e04c3fSmrg * image types) ... Vertex shader inputs can only be float, 541401e04c3fSmrg * single-precision floating-point scalars, single-precision 541501e04c3fSmrg * floating-point vectors, matrices, signed and unsigned 541601e04c3fSmrg * integers and integer vectors, sampler and image types." 541701e04c3fSmrg */ 541801e04c3fSmrg const glsl_type *check_type = var->type->without_array(); 541901e04c3fSmrg 54207ec681f3Smrg bool error = false; 542101e04c3fSmrg switch (check_type->base_type) { 542201e04c3fSmrg case GLSL_TYPE_FLOAT: 54237ec681f3Smrg break; 542401e04c3fSmrg case GLSL_TYPE_UINT64: 542501e04c3fSmrg case GLSL_TYPE_INT64: 542601e04c3fSmrg break; 542701e04c3fSmrg case GLSL_TYPE_UINT: 542801e04c3fSmrg case GLSL_TYPE_INT: 54297ec681f3Smrg error = !state->is_version(120, 300) && !state->EXT_gpu_shader4_enable; 54307ec681f3Smrg break; 543101e04c3fSmrg case GLSL_TYPE_DOUBLE: 54327ec681f3Smrg error = !state->is_version(410, 0) && !state->ARB_vertex_attrib_64bit_enable; 54337ec681f3Smrg break; 543401e04c3fSmrg case GLSL_TYPE_SAMPLER: 543501e04c3fSmrg case GLSL_TYPE_IMAGE: 54367ec681f3Smrg error = !state->has_bindless(); 54377ec681f3Smrg break; 543801e04c3fSmrg default: 54397ec681f3Smrg error = true; 54407ec681f3Smrg } 54417ec681f3Smrg 54427ec681f3Smrg if (error) { 544301e04c3fSmrg _mesa_glsl_error(& loc, state, 544401e04c3fSmrg "vertex shader input / attribute cannot have " 544501e04c3fSmrg "type %s`%s'", 544601e04c3fSmrg var->type->is_array() ? "array of " : "", 544701e04c3fSmrg check_type->name); 54487ec681f3Smrg } else if (var->type->is_array() && 544901e04c3fSmrg !state->check_version(150, 0, &loc, 545001e04c3fSmrg "vertex shader input / attribute " 545101e04c3fSmrg "cannot have array type")) { 545201e04c3fSmrg } 545301e04c3fSmrg } else if (state->stage == MESA_SHADER_GEOMETRY) { 545401e04c3fSmrg /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec: 545501e04c3fSmrg * 545601e04c3fSmrg * Geometry shader input variables get the per-vertex values 545701e04c3fSmrg * written out by vertex shader output variables of the same 545801e04c3fSmrg * names. Since a geometry shader operates on a set of 545901e04c3fSmrg * vertices, each input varying variable (or input block, see 546001e04c3fSmrg * interface blocks below) needs to be declared as an array. 546101e04c3fSmrg */ 546201e04c3fSmrg if (!var->type->is_array()) { 546301e04c3fSmrg _mesa_glsl_error(&loc, state, 546401e04c3fSmrg "geometry shader inputs must be arrays"); 546501e04c3fSmrg } 546601e04c3fSmrg 546701e04c3fSmrg handle_geometry_shader_input_decl(state, loc, var); 546801e04c3fSmrg } else if (state->stage == MESA_SHADER_FRAGMENT) { 546901e04c3fSmrg /* From section 4.3.4 (Input Variables) of the GLSL ES 3.10 spec: 547001e04c3fSmrg * 547101e04c3fSmrg * It is a compile-time error to declare a fragment shader 547201e04c3fSmrg * input with, or that contains, any of the following types: 547301e04c3fSmrg * 547401e04c3fSmrg * * A boolean type 547501e04c3fSmrg * * An opaque type 547601e04c3fSmrg * * An array of arrays 547701e04c3fSmrg * * An array of structures 547801e04c3fSmrg * * A structure containing an array 547901e04c3fSmrg * * A structure containing a structure 548001e04c3fSmrg */ 548101e04c3fSmrg if (state->es_shader) { 548201e04c3fSmrg const glsl_type *check_type = var->type->without_array(); 548301e04c3fSmrg if (check_type->is_boolean() || 548401e04c3fSmrg check_type->contains_opaque()) { 548501e04c3fSmrg _mesa_glsl_error(&loc, state, 548601e04c3fSmrg "fragment shader input cannot have type %s", 548701e04c3fSmrg check_type->name); 548801e04c3fSmrg } 548901e04c3fSmrg if (var->type->is_array() && 549001e04c3fSmrg var->type->fields.array->is_array()) { 549101e04c3fSmrg _mesa_glsl_error(&loc, state, 549201e04c3fSmrg "%s shader output " 549301e04c3fSmrg "cannot have an array of arrays", 549401e04c3fSmrg _mesa_shader_stage_to_string(state->stage)); 549501e04c3fSmrg } 549601e04c3fSmrg if (var->type->is_array() && 5497ed98bd31Smaya var->type->fields.array->is_struct()) { 549801e04c3fSmrg _mesa_glsl_error(&loc, state, 549901e04c3fSmrg "fragment shader input " 550001e04c3fSmrg "cannot have an array of structs"); 550101e04c3fSmrg } 5502ed98bd31Smaya if (var->type->is_struct()) { 550301e04c3fSmrg for (unsigned i = 0; i < var->type->length; i++) { 550401e04c3fSmrg if (var->type->fields.structure[i].type->is_array() || 5505ed98bd31Smaya var->type->fields.structure[i].type->is_struct()) 550601e04c3fSmrg _mesa_glsl_error(&loc, state, 550701e04c3fSmrg "fragment shader input cannot have " 550801e04c3fSmrg "a struct that contains an " 550901e04c3fSmrg "array or struct"); 551001e04c3fSmrg } 551101e04c3fSmrg } 551201e04c3fSmrg } 551301e04c3fSmrg } else if (state->stage == MESA_SHADER_TESS_CTRL || 551401e04c3fSmrg state->stage == MESA_SHADER_TESS_EVAL) { 551501e04c3fSmrg handle_tess_shader_input_decl(state, loc, var); 551601e04c3fSmrg } 551701e04c3fSmrg } else if (var->data.mode == ir_var_shader_out) { 551801e04c3fSmrg const glsl_type *check_type = var->type->without_array(); 551901e04c3fSmrg 552001e04c3fSmrg /* From section 4.3.6 (Output variables) of the GLSL 4.40 spec: 552101e04c3fSmrg * 552201e04c3fSmrg * It is a compile-time error to declare a fragment shader output 552301e04c3fSmrg * that contains any of the following: 552401e04c3fSmrg * 552501e04c3fSmrg * * A Boolean type (bool, bvec2 ...) 552601e04c3fSmrg * * A double-precision scalar or vector (double, dvec2 ...) 552701e04c3fSmrg * * An opaque type 552801e04c3fSmrg * * Any matrix type 552901e04c3fSmrg * * A structure 553001e04c3fSmrg */ 553101e04c3fSmrg if (state->stage == MESA_SHADER_FRAGMENT) { 5532ed98bd31Smaya if (check_type->is_struct() || check_type->is_matrix()) 553301e04c3fSmrg _mesa_glsl_error(&loc, state, 553401e04c3fSmrg "fragment shader output " 553501e04c3fSmrg "cannot have struct or matrix type"); 553601e04c3fSmrg switch (check_type->base_type) { 553701e04c3fSmrg case GLSL_TYPE_UINT: 553801e04c3fSmrg case GLSL_TYPE_INT: 553901e04c3fSmrg case GLSL_TYPE_FLOAT: 554001e04c3fSmrg break; 554101e04c3fSmrg default: 554201e04c3fSmrg _mesa_glsl_error(&loc, state, 554301e04c3fSmrg "fragment shader output cannot have " 554401e04c3fSmrg "type %s", check_type->name); 554501e04c3fSmrg } 554601e04c3fSmrg } 554701e04c3fSmrg 554801e04c3fSmrg /* From section 4.3.6 (Output Variables) of the GLSL ES 3.10 spec: 554901e04c3fSmrg * 555001e04c3fSmrg * It is a compile-time error to declare a vertex shader output 555101e04c3fSmrg * with, or that contains, any of the following types: 555201e04c3fSmrg * 555301e04c3fSmrg * * A boolean type 555401e04c3fSmrg * * An opaque type 555501e04c3fSmrg * * An array of arrays 555601e04c3fSmrg * * An array of structures 555701e04c3fSmrg * * A structure containing an array 555801e04c3fSmrg * * A structure containing a structure 555901e04c3fSmrg * 556001e04c3fSmrg * It is a compile-time error to declare a fragment shader output 556101e04c3fSmrg * with, or that contains, any of the following types: 556201e04c3fSmrg * 556301e04c3fSmrg * * A boolean type 556401e04c3fSmrg * * An opaque type 556501e04c3fSmrg * * A matrix 556601e04c3fSmrg * * A structure 556701e04c3fSmrg * * An array of array 556801e04c3fSmrg * 556901e04c3fSmrg * ES 3.20 updates this to apply to tessellation and geometry shaders 557001e04c3fSmrg * as well. Because there are per-vertex arrays in the new stages, 557101e04c3fSmrg * it strikes the "array of..." rules and replaces them with these: 557201e04c3fSmrg * 557301e04c3fSmrg * * For per-vertex-arrayed variables (applies to tessellation 557401e04c3fSmrg * control, tessellation evaluation and geometry shaders): 557501e04c3fSmrg * 557601e04c3fSmrg * * Per-vertex-arrayed arrays of arrays 557701e04c3fSmrg * * Per-vertex-arrayed arrays of structures 557801e04c3fSmrg * 557901e04c3fSmrg * * For non-per-vertex-arrayed variables: 558001e04c3fSmrg * 558101e04c3fSmrg * * An array of arrays 558201e04c3fSmrg * * An array of structures 558301e04c3fSmrg * 558401e04c3fSmrg * which basically says to unwrap the per-vertex aspect and apply 558501e04c3fSmrg * the old rules. 558601e04c3fSmrg */ 558701e04c3fSmrg if (state->es_shader) { 558801e04c3fSmrg if (var->type->is_array() && 558901e04c3fSmrg var->type->fields.array->is_array()) { 559001e04c3fSmrg _mesa_glsl_error(&loc, state, 559101e04c3fSmrg "%s shader output " 559201e04c3fSmrg "cannot have an array of arrays", 559301e04c3fSmrg _mesa_shader_stage_to_string(state->stage)); 559401e04c3fSmrg } 559501e04c3fSmrg if (state->stage <= MESA_SHADER_GEOMETRY) { 559601e04c3fSmrg const glsl_type *type = var->type; 559701e04c3fSmrg 559801e04c3fSmrg if (state->stage == MESA_SHADER_TESS_CTRL && 559901e04c3fSmrg !var->data.patch && var->type->is_array()) { 560001e04c3fSmrg type = var->type->fields.array; 560101e04c3fSmrg } 560201e04c3fSmrg 5603ed98bd31Smaya if (type->is_array() && type->fields.array->is_struct()) { 560401e04c3fSmrg _mesa_glsl_error(&loc, state, 560501e04c3fSmrg "%s shader output cannot have " 560601e04c3fSmrg "an array of structs", 560701e04c3fSmrg _mesa_shader_stage_to_string(state->stage)); 560801e04c3fSmrg } 5609ed98bd31Smaya if (type->is_struct()) { 561001e04c3fSmrg for (unsigned i = 0; i < type->length; i++) { 561101e04c3fSmrg if (type->fields.structure[i].type->is_array() || 5612ed98bd31Smaya type->fields.structure[i].type->is_struct()) 561301e04c3fSmrg _mesa_glsl_error(&loc, state, 561401e04c3fSmrg "%s shader output cannot have a " 561501e04c3fSmrg "struct that contains an " 561601e04c3fSmrg "array or struct", 561701e04c3fSmrg _mesa_shader_stage_to_string(state->stage)); 561801e04c3fSmrg } 561901e04c3fSmrg } 562001e04c3fSmrg } 562101e04c3fSmrg } 562201e04c3fSmrg 562301e04c3fSmrg if (state->stage == MESA_SHADER_TESS_CTRL) { 562401e04c3fSmrg handle_tess_ctrl_shader_output_decl(state, loc, var); 562501e04c3fSmrg } 562601e04c3fSmrg } else if (var->type->contains_subroutine()) { 562701e04c3fSmrg /* declare subroutine uniforms as hidden */ 562801e04c3fSmrg var->data.how_declared = ir_var_hidden; 562901e04c3fSmrg } 563001e04c3fSmrg 563101e04c3fSmrg /* From section 4.3.4 of the GLSL 4.00 spec: 563201e04c3fSmrg * "Input variables may not be declared using the patch in qualifier 563301e04c3fSmrg * in tessellation control or geometry shaders." 563401e04c3fSmrg * 563501e04c3fSmrg * From section 4.3.6 of the GLSL 4.00 spec: 563601e04c3fSmrg * "It is an error to use patch out in a vertex, tessellation 563701e04c3fSmrg * evaluation, or geometry shader." 563801e04c3fSmrg * 563901e04c3fSmrg * This doesn't explicitly forbid using them in a fragment shader, but 564001e04c3fSmrg * that's probably just an oversight. 564101e04c3fSmrg */ 564201e04c3fSmrg if (state->stage != MESA_SHADER_TESS_EVAL 564301e04c3fSmrg && this->type->qualifier.flags.q.patch 564401e04c3fSmrg && this->type->qualifier.flags.q.in) { 564501e04c3fSmrg 564601e04c3fSmrg _mesa_glsl_error(&loc, state, "'patch in' can only be used in a " 564701e04c3fSmrg "tessellation evaluation shader"); 564801e04c3fSmrg } 564901e04c3fSmrg 565001e04c3fSmrg if (state->stage != MESA_SHADER_TESS_CTRL 565101e04c3fSmrg && this->type->qualifier.flags.q.patch 565201e04c3fSmrg && this->type->qualifier.flags.q.out) { 565301e04c3fSmrg 565401e04c3fSmrg _mesa_glsl_error(&loc, state, "'patch out' can only be used in a " 565501e04c3fSmrg "tessellation control shader"); 565601e04c3fSmrg } 565701e04c3fSmrg 565801e04c3fSmrg /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30. 565901e04c3fSmrg */ 566001e04c3fSmrg if (this->type->qualifier.precision != ast_precision_none) { 566101e04c3fSmrg state->check_precision_qualifiers_allowed(&loc); 566201e04c3fSmrg } 566301e04c3fSmrg 566401e04c3fSmrg if (this->type->qualifier.precision != ast_precision_none && 566501e04c3fSmrg !precision_qualifier_allowed(var->type)) { 566601e04c3fSmrg _mesa_glsl_error(&loc, state, 566701e04c3fSmrg "precision qualifiers apply only to floating point" 566801e04c3fSmrg ", integer and opaque types"); 566901e04c3fSmrg } 567001e04c3fSmrg 567101e04c3fSmrg /* From section 4.1.7 of the GLSL 4.40 spec: 567201e04c3fSmrg * 567301e04c3fSmrg * "[Opaque types] can only be declared as function 567401e04c3fSmrg * parameters or uniform-qualified variables." 567501e04c3fSmrg * 567601e04c3fSmrg * From section 4.1.7 of the ARB_bindless_texture spec: 567701e04c3fSmrg * 567801e04c3fSmrg * "Samplers may be declared as shader inputs and outputs, as uniform 567901e04c3fSmrg * variables, as temporary variables, and as function parameters." 568001e04c3fSmrg * 568101e04c3fSmrg * From section 4.1.X of the ARB_bindless_texture spec: 568201e04c3fSmrg * 568301e04c3fSmrg * "Images may be declared as shader inputs and outputs, as uniform 568401e04c3fSmrg * variables, as temporary variables, and as function parameters." 568501e04c3fSmrg */ 568601e04c3fSmrg if (!this->type->qualifier.flags.q.uniform && 568701e04c3fSmrg (var_type->contains_atomic() || 568801e04c3fSmrg (!state->has_bindless() && var_type->contains_opaque()))) { 568901e04c3fSmrg _mesa_glsl_error(&loc, state, 569001e04c3fSmrg "%s variables must be declared uniform", 569101e04c3fSmrg state->has_bindless() ? "atomic" : "opaque"); 569201e04c3fSmrg } 569301e04c3fSmrg 569401e04c3fSmrg /* Process the initializer and add its instructions to a temporary 569501e04c3fSmrg * list. This list will be added to the instruction stream (below) after 569601e04c3fSmrg * the declaration is added. This is done because in some cases (such as 569701e04c3fSmrg * redeclarations) the declaration may not actually be added to the 569801e04c3fSmrg * instruction stream. 569901e04c3fSmrg */ 570001e04c3fSmrg exec_list initializer_instructions; 570101e04c3fSmrg 570201e04c3fSmrg /* Examine var name here since var may get deleted in the next call */ 570301e04c3fSmrg bool var_is_gl_id = is_gl_identifier(var->name); 570401e04c3fSmrg 570501e04c3fSmrg bool is_redeclaration; 570601e04c3fSmrg var = get_variable_being_redeclared(&var, decl->get_location(), state, 570701e04c3fSmrg false /* allow_all_redeclarations */, 570801e04c3fSmrg &is_redeclaration); 570901e04c3fSmrg if (is_redeclaration) { 571001e04c3fSmrg if (var_is_gl_id && 571101e04c3fSmrg var->data.how_declared == ir_var_declared_in_block) { 571201e04c3fSmrg _mesa_glsl_error(&loc, state, 571301e04c3fSmrg "`%s' has already been redeclared using " 571401e04c3fSmrg "gl_PerVertex", var->name); 571501e04c3fSmrg } 571601e04c3fSmrg var->data.how_declared = ir_var_declared_normally; 571701e04c3fSmrg } 571801e04c3fSmrg 571901e04c3fSmrg if (decl->initializer != NULL) { 572001e04c3fSmrg result = process_initializer(var, 572101e04c3fSmrg decl, this->type, 572201e04c3fSmrg &initializer_instructions, state); 572301e04c3fSmrg } else { 572401e04c3fSmrg validate_array_dimensions(var_type, state, &loc); 572501e04c3fSmrg } 572601e04c3fSmrg 572701e04c3fSmrg /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec: 572801e04c3fSmrg * 572901e04c3fSmrg * "It is an error to write to a const variable outside of 573001e04c3fSmrg * its declaration, so they must be initialized when 573101e04c3fSmrg * declared." 573201e04c3fSmrg */ 573301e04c3fSmrg if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) { 573401e04c3fSmrg _mesa_glsl_error(& loc, state, 573501e04c3fSmrg "const declaration of `%s' must be initialized", 573601e04c3fSmrg decl->identifier); 573701e04c3fSmrg } 573801e04c3fSmrg 573901e04c3fSmrg if (state->es_shader) { 574001e04c3fSmrg const glsl_type *const t = var->type; 574101e04c3fSmrg 574201e04c3fSmrg /* Skip the unsized array check for TCS/TES/GS inputs & TCS outputs. 574301e04c3fSmrg * 574401e04c3fSmrg * The GL_OES_tessellation_shader spec says about inputs: 574501e04c3fSmrg * 574601e04c3fSmrg * "Declaring an array size is optional. If no size is specified, 574701e04c3fSmrg * it will be taken from the implementation-dependent maximum 574801e04c3fSmrg * patch size (gl_MaxPatchVertices)." 574901e04c3fSmrg * 575001e04c3fSmrg * and about TCS outputs: 575101e04c3fSmrg * 575201e04c3fSmrg * "If no size is specified, it will be taken from output patch 575301e04c3fSmrg * size declared in the shader." 575401e04c3fSmrg * 575501e04c3fSmrg * The GL_OES_geometry_shader spec says: 575601e04c3fSmrg * 575701e04c3fSmrg * "All geometry shader input unsized array declarations will be 575801e04c3fSmrg * sized by an earlier input primitive layout qualifier, when 575901e04c3fSmrg * present, as per the following table." 576001e04c3fSmrg */ 576101e04c3fSmrg const bool implicitly_sized = 576201e04c3fSmrg (var->data.mode == ir_var_shader_in && 576301e04c3fSmrg state->stage >= MESA_SHADER_TESS_CTRL && 576401e04c3fSmrg state->stage <= MESA_SHADER_GEOMETRY) || 576501e04c3fSmrg (var->data.mode == ir_var_shader_out && 576601e04c3fSmrg state->stage == MESA_SHADER_TESS_CTRL); 576701e04c3fSmrg 576801e04c3fSmrg if (t->is_unsized_array() && !implicitly_sized) 576901e04c3fSmrg /* Section 10.17 of the GLSL ES 1.00 specification states that 577001e04c3fSmrg * unsized array declarations have been removed from the language. 577101e04c3fSmrg * Arrays that are sized using an initializer are still explicitly 577201e04c3fSmrg * sized. However, GLSL ES 1.00 does not allow array 577301e04c3fSmrg * initializers. That is only allowed in GLSL ES 3.00. 577401e04c3fSmrg * 577501e04c3fSmrg * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says: 577601e04c3fSmrg * 577701e04c3fSmrg * "An array type can also be formed without specifying a size 577801e04c3fSmrg * if the definition includes an initializer: 577901e04c3fSmrg * 578001e04c3fSmrg * float x[] = float[2] (1.0, 2.0); // declares an array of size 2 578101e04c3fSmrg * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3 578201e04c3fSmrg * 578301e04c3fSmrg * float a[5]; 578401e04c3fSmrg * float b[] = a;" 578501e04c3fSmrg */ 578601e04c3fSmrg _mesa_glsl_error(& loc, state, 578701e04c3fSmrg "unsized array declarations are not allowed in " 578801e04c3fSmrg "GLSL ES"); 578901e04c3fSmrg } 579001e04c3fSmrg 579101e04c3fSmrg /* Section 4.4.6.1 Atomic Counter Layout Qualifiers of the GLSL 4.60 spec: 579201e04c3fSmrg * 579301e04c3fSmrg * "It is a compile-time error to declare an unsized array of 579401e04c3fSmrg * atomic_uint" 579501e04c3fSmrg */ 579601e04c3fSmrg if (var->type->is_unsized_array() && 579701e04c3fSmrg var->type->without_array()->base_type == GLSL_TYPE_ATOMIC_UINT) { 579801e04c3fSmrg _mesa_glsl_error(& loc, state, 579901e04c3fSmrg "Unsized array of atomic_uint is not allowed"); 580001e04c3fSmrg } 580101e04c3fSmrg 580201e04c3fSmrg /* If the declaration is not a redeclaration, there are a few additional 580301e04c3fSmrg * semantic checks that must be applied. In addition, variable that was 580401e04c3fSmrg * created for the declaration should be added to the IR stream. 580501e04c3fSmrg */ 580601e04c3fSmrg if (!is_redeclaration) { 580701e04c3fSmrg validate_identifier(decl->identifier, loc, state); 580801e04c3fSmrg 580901e04c3fSmrg /* Add the variable to the symbol table. Note that the initializer's 581001e04c3fSmrg * IR was already processed earlier (though it hasn't been emitted 581101e04c3fSmrg * yet), without the variable in scope. 581201e04c3fSmrg * 581301e04c3fSmrg * This differs from most C-like languages, but it follows the GLSL 581401e04c3fSmrg * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50 581501e04c3fSmrg * spec: 581601e04c3fSmrg * 581701e04c3fSmrg * "Within a declaration, the scope of a name starts immediately 581801e04c3fSmrg * after the initializer if present or immediately after the name 581901e04c3fSmrg * being declared if not." 582001e04c3fSmrg */ 582101e04c3fSmrg if (!state->symbols->add_variable(var)) { 582201e04c3fSmrg YYLTYPE loc = this->get_location(); 582301e04c3fSmrg _mesa_glsl_error(&loc, state, "name `%s' already taken in the " 582401e04c3fSmrg "current scope", decl->identifier); 582501e04c3fSmrg continue; 582601e04c3fSmrg } 582701e04c3fSmrg 582801e04c3fSmrg /* Push the variable declaration to the top. It means that all the 582901e04c3fSmrg * variable declarations will appear in a funny last-to-first order, 583001e04c3fSmrg * but otherwise we run into trouble if a function is prototyped, a 583101e04c3fSmrg * global var is decled, then the function is defined with usage of 583201e04c3fSmrg * the global var. See glslparsertest's CorrectModule.frag. 583301e04c3fSmrg */ 583401e04c3fSmrg instructions->push_head(var); 583501e04c3fSmrg } 583601e04c3fSmrg 583701e04c3fSmrg instructions->append_list(&initializer_instructions); 583801e04c3fSmrg } 583901e04c3fSmrg 584001e04c3fSmrg 584101e04c3fSmrg /* Generally, variable declarations do not have r-values. However, 584201e04c3fSmrg * one is used for the declaration in 584301e04c3fSmrg * 584401e04c3fSmrg * while (bool b = some_condition()) { 584501e04c3fSmrg * ... 584601e04c3fSmrg * } 584701e04c3fSmrg * 584801e04c3fSmrg * so we return the rvalue from the last seen declaration here. 584901e04c3fSmrg */ 585001e04c3fSmrg return result; 585101e04c3fSmrg} 585201e04c3fSmrg 585301e04c3fSmrg 585401e04c3fSmrgir_rvalue * 585501e04c3fSmrgast_parameter_declarator::hir(exec_list *instructions, 585601e04c3fSmrg struct _mesa_glsl_parse_state *state) 585701e04c3fSmrg{ 585801e04c3fSmrg void *ctx = state; 585901e04c3fSmrg const struct glsl_type *type; 586001e04c3fSmrg const char *name = NULL; 586101e04c3fSmrg YYLTYPE loc = this->get_location(); 586201e04c3fSmrg 586301e04c3fSmrg type = this->type->glsl_type(& name, state); 586401e04c3fSmrg 586501e04c3fSmrg if (type == NULL) { 586601e04c3fSmrg if (name != NULL) { 586701e04c3fSmrg _mesa_glsl_error(& loc, state, 586801e04c3fSmrg "invalid type `%s' in declaration of `%s'", 586901e04c3fSmrg name, this->identifier); 587001e04c3fSmrg } else { 587101e04c3fSmrg _mesa_glsl_error(& loc, state, 587201e04c3fSmrg "invalid type in declaration of `%s'", 587301e04c3fSmrg this->identifier); 587401e04c3fSmrg } 587501e04c3fSmrg 587601e04c3fSmrg type = glsl_type::error_type; 587701e04c3fSmrg } 587801e04c3fSmrg 587901e04c3fSmrg /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec: 588001e04c3fSmrg * 588101e04c3fSmrg * "Functions that accept no input arguments need not use void in the 588201e04c3fSmrg * argument list because prototypes (or definitions) are required and 588301e04c3fSmrg * therefore there is no ambiguity when an empty argument list "( )" is 588401e04c3fSmrg * declared. The idiom "(void)" as a parameter list is provided for 588501e04c3fSmrg * convenience." 588601e04c3fSmrg * 588701e04c3fSmrg * Placing this check here prevents a void parameter being set up 588801e04c3fSmrg * for a function, which avoids tripping up checks for main taking 588901e04c3fSmrg * parameters and lookups of an unnamed symbol. 589001e04c3fSmrg */ 589101e04c3fSmrg if (type->is_void()) { 589201e04c3fSmrg if (this->identifier != NULL) 589301e04c3fSmrg _mesa_glsl_error(& loc, state, 589401e04c3fSmrg "named parameter cannot have type `void'"); 589501e04c3fSmrg 589601e04c3fSmrg is_void = true; 589701e04c3fSmrg return NULL; 589801e04c3fSmrg } 589901e04c3fSmrg 590001e04c3fSmrg if (formal_parameter && (this->identifier == NULL)) { 590101e04c3fSmrg _mesa_glsl_error(& loc, state, "formal parameter lacks a name"); 590201e04c3fSmrg return NULL; 590301e04c3fSmrg } 590401e04c3fSmrg 590501e04c3fSmrg /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...) 590601e04c3fSmrg * call already handled the "vec4[..] foo" case. 590701e04c3fSmrg */ 590801e04c3fSmrg type = process_array_type(&loc, type, this->array_specifier, state); 590901e04c3fSmrg 591001e04c3fSmrg if (!type->is_error() && type->is_unsized_array()) { 591101e04c3fSmrg _mesa_glsl_error(&loc, state, "arrays passed as parameters must have " 591201e04c3fSmrg "a declared size"); 591301e04c3fSmrg type = glsl_type::error_type; 591401e04c3fSmrg } 591501e04c3fSmrg 591601e04c3fSmrg is_void = false; 591701e04c3fSmrg ir_variable *var = new(ctx) 591801e04c3fSmrg ir_variable(type, this->identifier, ir_var_function_in); 591901e04c3fSmrg 592001e04c3fSmrg /* Apply any specified qualifiers to the parameter declaration. Note that 592101e04c3fSmrg * for function parameters the default mode is 'in'. 592201e04c3fSmrg */ 592301e04c3fSmrg apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc, 592401e04c3fSmrg true); 592501e04c3fSmrg 59267ec681f3Smrg if (((1u << var->data.mode) & state->zero_init) && 59277ec681f3Smrg (var->type->is_numeric() || var->type->is_boolean())) { 59287ec681f3Smrg const ir_constant_data data = { { 0 } }; 59297ec681f3Smrg var->data.has_initializer = true; 59307ec681f3Smrg var->data.is_implicit_initializer = true; 59317ec681f3Smrg var->constant_initializer = new(var) ir_constant(var->type, &data); 59327ec681f3Smrg } 59337ec681f3Smrg 593401e04c3fSmrg /* From section 4.1.7 of the GLSL 4.40 spec: 593501e04c3fSmrg * 593601e04c3fSmrg * "Opaque variables cannot be treated as l-values; hence cannot 593701e04c3fSmrg * be used as out or inout function parameters, nor can they be 593801e04c3fSmrg * assigned into." 593901e04c3fSmrg * 594001e04c3fSmrg * From section 4.1.7 of the ARB_bindless_texture spec: 594101e04c3fSmrg * 594201e04c3fSmrg * "Samplers can be used as l-values, so can be assigned into and used 594301e04c3fSmrg * as "out" and "inout" function parameters." 594401e04c3fSmrg * 594501e04c3fSmrg * From section 4.1.X of the ARB_bindless_texture spec: 594601e04c3fSmrg * 594701e04c3fSmrg * "Images can be used as l-values, so can be assigned into and used as 594801e04c3fSmrg * "out" and "inout" function parameters." 594901e04c3fSmrg */ 595001e04c3fSmrg if ((var->data.mode == ir_var_function_inout || var->data.mode == ir_var_function_out) 595101e04c3fSmrg && (type->contains_atomic() || 595201e04c3fSmrg (!state->has_bindless() && type->contains_opaque()))) { 595301e04c3fSmrg _mesa_glsl_error(&loc, state, "out and inout parameters cannot " 595401e04c3fSmrg "contain %s variables", 595501e04c3fSmrg state->has_bindless() ? "atomic" : "opaque"); 595601e04c3fSmrg type = glsl_type::error_type; 595701e04c3fSmrg } 595801e04c3fSmrg 595901e04c3fSmrg /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec: 596001e04c3fSmrg * 596101e04c3fSmrg * "When calling a function, expressions that do not evaluate to 596201e04c3fSmrg * l-values cannot be passed to parameters declared as out or inout." 596301e04c3fSmrg * 596401e04c3fSmrg * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec: 596501e04c3fSmrg * 596601e04c3fSmrg * "Other binary or unary expressions, non-dereferenced arrays, 596701e04c3fSmrg * function names, swizzles with repeated fields, and constants 596801e04c3fSmrg * cannot be l-values." 596901e04c3fSmrg * 597001e04c3fSmrg * So for GLSL 1.10, passing an array as an out or inout parameter is not 597101e04c3fSmrg * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES. 597201e04c3fSmrg */ 597301e04c3fSmrg if ((var->data.mode == ir_var_function_inout || var->data.mode == ir_var_function_out) 597401e04c3fSmrg && type->is_array() 597501e04c3fSmrg && !state->check_version(120, 100, &loc, 597601e04c3fSmrg "arrays cannot be out or inout parameters")) { 597701e04c3fSmrg type = glsl_type::error_type; 597801e04c3fSmrg } 597901e04c3fSmrg 598001e04c3fSmrg instructions->push_tail(var); 598101e04c3fSmrg 598201e04c3fSmrg /* Parameter declarations do not have r-values. 598301e04c3fSmrg */ 598401e04c3fSmrg return NULL; 598501e04c3fSmrg} 598601e04c3fSmrg 598701e04c3fSmrg 598801e04c3fSmrgvoid 598901e04c3fSmrgast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters, 599001e04c3fSmrg bool formal, 599101e04c3fSmrg exec_list *ir_parameters, 599201e04c3fSmrg _mesa_glsl_parse_state *state) 599301e04c3fSmrg{ 599401e04c3fSmrg ast_parameter_declarator *void_param = NULL; 599501e04c3fSmrg unsigned count = 0; 599601e04c3fSmrg 599701e04c3fSmrg foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) { 599801e04c3fSmrg param->formal_parameter = formal; 599901e04c3fSmrg param->hir(ir_parameters, state); 600001e04c3fSmrg 600101e04c3fSmrg if (param->is_void) 600201e04c3fSmrg void_param = param; 600301e04c3fSmrg 600401e04c3fSmrg count++; 600501e04c3fSmrg } 600601e04c3fSmrg 600701e04c3fSmrg if ((void_param != NULL) && (count > 1)) { 600801e04c3fSmrg YYLTYPE loc = void_param->get_location(); 600901e04c3fSmrg 601001e04c3fSmrg _mesa_glsl_error(& loc, state, 601101e04c3fSmrg "`void' parameter must be only parameter"); 601201e04c3fSmrg } 601301e04c3fSmrg} 601401e04c3fSmrg 601501e04c3fSmrg 601601e04c3fSmrgvoid 601701e04c3fSmrgemit_function(_mesa_glsl_parse_state *state, ir_function *f) 601801e04c3fSmrg{ 601901e04c3fSmrg /* IR invariants disallow function declarations or definitions 602001e04c3fSmrg * nested within other function definitions. But there is no 602101e04c3fSmrg * requirement about the relative order of function declarations 602201e04c3fSmrg * and definitions with respect to one another. So simply insert 602301e04c3fSmrg * the new ir_function block at the end of the toplevel instruction 602401e04c3fSmrg * list. 602501e04c3fSmrg */ 602601e04c3fSmrg state->toplevel_ir->push_tail(f); 602701e04c3fSmrg} 602801e04c3fSmrg 602901e04c3fSmrg 603001e04c3fSmrgir_rvalue * 603101e04c3fSmrgast_function::hir(exec_list *instructions, 603201e04c3fSmrg struct _mesa_glsl_parse_state *state) 603301e04c3fSmrg{ 603401e04c3fSmrg void *ctx = state; 603501e04c3fSmrg ir_function *f = NULL; 603601e04c3fSmrg ir_function_signature *sig = NULL; 603701e04c3fSmrg exec_list hir_parameters; 603801e04c3fSmrg YYLTYPE loc = this->get_location(); 603901e04c3fSmrg 604001e04c3fSmrg const char *const name = identifier; 604101e04c3fSmrg 604201e04c3fSmrg /* New functions are always added to the top-level IR instruction stream, 604301e04c3fSmrg * so this instruction list pointer is ignored. See also emit_function 604401e04c3fSmrg * (called below). 604501e04c3fSmrg */ 604601e04c3fSmrg (void) instructions; 604701e04c3fSmrg 604801e04c3fSmrg /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec, 604901e04c3fSmrg * 605001e04c3fSmrg * "Function declarations (prototypes) cannot occur inside of functions; 605101e04c3fSmrg * they must be at global scope, or for the built-in functions, outside 605201e04c3fSmrg * the global scope." 605301e04c3fSmrg * 605401e04c3fSmrg * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec, 605501e04c3fSmrg * 605601e04c3fSmrg * "User defined functions may only be defined within the global scope." 605701e04c3fSmrg * 605801e04c3fSmrg * Note that this language does not appear in GLSL 1.10. 605901e04c3fSmrg */ 606001e04c3fSmrg if ((state->current_function != NULL) && 606101e04c3fSmrg state->is_version(120, 100)) { 606201e04c3fSmrg YYLTYPE loc = this->get_location(); 606301e04c3fSmrg _mesa_glsl_error(&loc, state, 606401e04c3fSmrg "declaration of function `%s' not allowed within " 606501e04c3fSmrg "function body", name); 606601e04c3fSmrg } 606701e04c3fSmrg 606801e04c3fSmrg validate_identifier(name, this->get_location(), state); 606901e04c3fSmrg 607001e04c3fSmrg /* Convert the list of function parameters to HIR now so that they can be 607101e04c3fSmrg * used below to compare this function's signature with previously seen 607201e04c3fSmrg * signatures for functions with the same name. 607301e04c3fSmrg */ 607401e04c3fSmrg ast_parameter_declarator::parameters_to_hir(& this->parameters, 607501e04c3fSmrg is_definition, 607601e04c3fSmrg & hir_parameters, state); 607701e04c3fSmrg 607801e04c3fSmrg const char *return_type_name; 607901e04c3fSmrg const glsl_type *return_type = 608001e04c3fSmrg this->return_type->glsl_type(& return_type_name, state); 608101e04c3fSmrg 608201e04c3fSmrg if (!return_type) { 608301e04c3fSmrg YYLTYPE loc = this->get_location(); 608401e04c3fSmrg _mesa_glsl_error(&loc, state, 608501e04c3fSmrg "function `%s' has undeclared return type `%s'", 608601e04c3fSmrg name, return_type_name); 608701e04c3fSmrg return_type = glsl_type::error_type; 608801e04c3fSmrg } 608901e04c3fSmrg 609001e04c3fSmrg /* ARB_shader_subroutine states: 609101e04c3fSmrg * "Subroutine declarations cannot be prototyped. It is an error to prepend 609201e04c3fSmrg * subroutine(...) to a function declaration." 609301e04c3fSmrg */ 609401e04c3fSmrg if (this->return_type->qualifier.subroutine_list && !is_definition) { 609501e04c3fSmrg YYLTYPE loc = this->get_location(); 609601e04c3fSmrg _mesa_glsl_error(&loc, state, 609701e04c3fSmrg "function declaration `%s' cannot have subroutine prepended", 609801e04c3fSmrg name); 609901e04c3fSmrg } 610001e04c3fSmrg 610101e04c3fSmrg /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec: 610201e04c3fSmrg * "No qualifier is allowed on the return type of a function." 610301e04c3fSmrg */ 610401e04c3fSmrg if (this->return_type->has_qualifiers(state)) { 610501e04c3fSmrg YYLTYPE loc = this->get_location(); 610601e04c3fSmrg _mesa_glsl_error(& loc, state, 610701e04c3fSmrg "function `%s' return type has qualifiers", name); 610801e04c3fSmrg } 610901e04c3fSmrg 611001e04c3fSmrg /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says: 611101e04c3fSmrg * 611201e04c3fSmrg * "Arrays are allowed as arguments and as the return type. In both 611301e04c3fSmrg * cases, the array must be explicitly sized." 611401e04c3fSmrg */ 611501e04c3fSmrg if (return_type->is_unsized_array()) { 611601e04c3fSmrg YYLTYPE loc = this->get_location(); 611701e04c3fSmrg _mesa_glsl_error(& loc, state, 611801e04c3fSmrg "function `%s' return type array must be explicitly " 611901e04c3fSmrg "sized", name); 612001e04c3fSmrg } 612101e04c3fSmrg 612201e04c3fSmrg /* From Section 6.1 (Function Definitions) of the GLSL 1.00 spec: 612301e04c3fSmrg * 612401e04c3fSmrg * "Arrays are allowed as arguments, but not as the return type. [...] 612501e04c3fSmrg * The return type can also be a structure if the structure does not 612601e04c3fSmrg * contain an array." 612701e04c3fSmrg */ 612801e04c3fSmrg if (state->language_version == 100 && return_type->contains_array()) { 612901e04c3fSmrg YYLTYPE loc = this->get_location(); 613001e04c3fSmrg _mesa_glsl_error(& loc, state, 613101e04c3fSmrg "function `%s' return type contains an array", name); 613201e04c3fSmrg } 613301e04c3fSmrg 613401e04c3fSmrg /* From section 4.1.7 of the GLSL 4.40 spec: 613501e04c3fSmrg * 613601e04c3fSmrg * "[Opaque types] can only be declared as function parameters 613701e04c3fSmrg * or uniform-qualified variables." 613801e04c3fSmrg * 613901e04c3fSmrg * The ARB_bindless_texture spec doesn't clearly state this, but as it says 614001e04c3fSmrg * "Replace Section 4.1.7 (Samplers), p. 25" and, "Replace Section 4.1.X, 614101e04c3fSmrg * (Images)", this should be allowed. 614201e04c3fSmrg */ 614301e04c3fSmrg if (return_type->contains_atomic() || 614401e04c3fSmrg (!state->has_bindless() && return_type->contains_opaque())) { 614501e04c3fSmrg YYLTYPE loc = this->get_location(); 614601e04c3fSmrg _mesa_glsl_error(&loc, state, 614701e04c3fSmrg "function `%s' return type can't contain an %s type", 614801e04c3fSmrg name, state->has_bindless() ? "atomic" : "opaque"); 614901e04c3fSmrg } 615001e04c3fSmrg 615101e04c3fSmrg /**/ 615201e04c3fSmrg if (return_type->is_subroutine()) { 615301e04c3fSmrg YYLTYPE loc = this->get_location(); 615401e04c3fSmrg _mesa_glsl_error(&loc, state, 615501e04c3fSmrg "function `%s' return type can't be a subroutine type", 615601e04c3fSmrg name); 615701e04c3fSmrg } 615801e04c3fSmrg 61597ec681f3Smrg /* Get the precision for the return type */ 61607ec681f3Smrg unsigned return_precision; 61617ec681f3Smrg 61627ec681f3Smrg if (state->es_shader) { 61637ec681f3Smrg YYLTYPE loc = this->get_location(); 61647ec681f3Smrg return_precision = 61657ec681f3Smrg select_gles_precision(this->return_type->qualifier.precision, 61667ec681f3Smrg return_type, 61677ec681f3Smrg state, 61687ec681f3Smrg &loc); 61697ec681f3Smrg } else { 61707ec681f3Smrg return_precision = GLSL_PRECISION_NONE; 61717ec681f3Smrg } 617201e04c3fSmrg 617301e04c3fSmrg /* Create an ir_function if one doesn't already exist. */ 617401e04c3fSmrg f = state->symbols->get_function(name); 617501e04c3fSmrg if (f == NULL) { 617601e04c3fSmrg f = new(ctx) ir_function(name); 617701e04c3fSmrg if (!this->return_type->qualifier.is_subroutine_decl()) { 617801e04c3fSmrg if (!state->symbols->add_function(f)) { 617901e04c3fSmrg /* This function name shadows a non-function use of the same name. */ 618001e04c3fSmrg YYLTYPE loc = this->get_location(); 618101e04c3fSmrg _mesa_glsl_error(&loc, state, "function name `%s' conflicts with " 618201e04c3fSmrg "non-function", name); 618301e04c3fSmrg return NULL; 618401e04c3fSmrg } 618501e04c3fSmrg } 618601e04c3fSmrg emit_function(state, f); 618701e04c3fSmrg } 618801e04c3fSmrg 618901e04c3fSmrg /* From GLSL ES 3.0 spec, chapter 6.1 "Function Definitions", page 71: 619001e04c3fSmrg * 619101e04c3fSmrg * "A shader cannot redefine or overload built-in functions." 619201e04c3fSmrg * 619301e04c3fSmrg * While in GLSL ES 1.0 specification, chapter 8 "Built-in Functions": 619401e04c3fSmrg * 619501e04c3fSmrg * "User code can overload the built-in functions but cannot redefine 619601e04c3fSmrg * them." 619701e04c3fSmrg */ 619801e04c3fSmrg if (state->es_shader) { 619901e04c3fSmrg /* Local shader has no exact candidates; check the built-ins. */ 620001e04c3fSmrg if (state->language_version >= 300 && 620101e04c3fSmrg _mesa_glsl_has_builtin_function(state, name)) { 620201e04c3fSmrg YYLTYPE loc = this->get_location(); 620301e04c3fSmrg _mesa_glsl_error(& loc, state, 620401e04c3fSmrg "A shader cannot redefine or overload built-in " 620501e04c3fSmrg "function `%s' in GLSL ES 3.00", name); 620601e04c3fSmrg return NULL; 620701e04c3fSmrg } 620801e04c3fSmrg 620901e04c3fSmrg if (state->language_version == 100) { 621001e04c3fSmrg ir_function_signature *sig = 621101e04c3fSmrg _mesa_glsl_find_builtin_function(state, name, &hir_parameters); 621201e04c3fSmrg if (sig && sig->is_builtin()) { 621301e04c3fSmrg _mesa_glsl_error(& loc, state, 621401e04c3fSmrg "A shader cannot redefine built-in " 621501e04c3fSmrg "function `%s' in GLSL ES 1.00", name); 621601e04c3fSmrg } 621701e04c3fSmrg } 621801e04c3fSmrg } 621901e04c3fSmrg 622001e04c3fSmrg /* Verify that this function's signature either doesn't match a previously 622101e04c3fSmrg * seen signature for a function with the same name, or, if a match is found, 622201e04c3fSmrg * that the previously seen signature does not have an associated definition. 622301e04c3fSmrg */ 622401e04c3fSmrg if (state->es_shader || f->has_user_signature()) { 622501e04c3fSmrg sig = f->exact_matching_signature(state, &hir_parameters); 622601e04c3fSmrg if (sig != NULL) { 622701e04c3fSmrg const char *badvar = sig->qualifiers_match(&hir_parameters); 622801e04c3fSmrg if (badvar != NULL) { 622901e04c3fSmrg YYLTYPE loc = this->get_location(); 623001e04c3fSmrg 623101e04c3fSmrg _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' " 623201e04c3fSmrg "qualifiers don't match prototype", name, badvar); 623301e04c3fSmrg } 623401e04c3fSmrg 623501e04c3fSmrg if (sig->return_type != return_type) { 623601e04c3fSmrg YYLTYPE loc = this->get_location(); 623701e04c3fSmrg 623801e04c3fSmrg _mesa_glsl_error(&loc, state, "function `%s' return type doesn't " 623901e04c3fSmrg "match prototype", name); 624001e04c3fSmrg } 624101e04c3fSmrg 62427ec681f3Smrg if (sig->return_precision != return_precision) { 62437ec681f3Smrg YYLTYPE loc = this->get_location(); 62447ec681f3Smrg 62457ec681f3Smrg _mesa_glsl_error(&loc, state, "function `%s' return type precision " 62467ec681f3Smrg "doesn't match prototype", name); 62477ec681f3Smrg } 62487ec681f3Smrg 624901e04c3fSmrg if (sig->is_defined) { 625001e04c3fSmrg if (is_definition) { 625101e04c3fSmrg YYLTYPE loc = this->get_location(); 625201e04c3fSmrg _mesa_glsl_error(& loc, state, "function `%s' redefined", name); 625301e04c3fSmrg } else { 625401e04c3fSmrg /* We just encountered a prototype that exactly matches a 625501e04c3fSmrg * function that's already been defined. This is redundant, 625601e04c3fSmrg * and we should ignore it. 625701e04c3fSmrg */ 625801e04c3fSmrg return NULL; 625901e04c3fSmrg } 626001e04c3fSmrg } else if (state->language_version == 100 && !is_definition) { 626101e04c3fSmrg /* From the GLSL 1.00 spec, section 4.2.7: 626201e04c3fSmrg * 626301e04c3fSmrg * "A particular variable, structure or function declaration 626401e04c3fSmrg * may occur at most once within a scope with the exception 626501e04c3fSmrg * that a single function prototype plus the corresponding 626601e04c3fSmrg * function definition are allowed." 626701e04c3fSmrg */ 626801e04c3fSmrg YYLTYPE loc = this->get_location(); 626901e04c3fSmrg _mesa_glsl_error(&loc, state, "function `%s' redeclared", name); 627001e04c3fSmrg } 627101e04c3fSmrg } 627201e04c3fSmrg } 627301e04c3fSmrg 627401e04c3fSmrg /* Verify the return type of main() */ 627501e04c3fSmrg if (strcmp(name, "main") == 0) { 627601e04c3fSmrg if (! return_type->is_void()) { 627701e04c3fSmrg YYLTYPE loc = this->get_location(); 627801e04c3fSmrg 627901e04c3fSmrg _mesa_glsl_error(& loc, state, "main() must return void"); 628001e04c3fSmrg } 628101e04c3fSmrg 628201e04c3fSmrg if (!hir_parameters.is_empty()) { 628301e04c3fSmrg YYLTYPE loc = this->get_location(); 628401e04c3fSmrg 628501e04c3fSmrg _mesa_glsl_error(& loc, state, "main() must not take any parameters"); 628601e04c3fSmrg } 628701e04c3fSmrg } 628801e04c3fSmrg 628901e04c3fSmrg /* Finish storing the information about this new function in its signature. 629001e04c3fSmrg */ 629101e04c3fSmrg if (sig == NULL) { 629201e04c3fSmrg sig = new(ctx) ir_function_signature(return_type); 62937ec681f3Smrg sig->return_precision = return_precision; 629401e04c3fSmrg f->add_signature(sig); 629501e04c3fSmrg } 629601e04c3fSmrg 629701e04c3fSmrg sig->replace_parameters(&hir_parameters); 629801e04c3fSmrg signature = sig; 629901e04c3fSmrg 630001e04c3fSmrg if (this->return_type->qualifier.subroutine_list) { 630101e04c3fSmrg int idx; 630201e04c3fSmrg 630301e04c3fSmrg if (this->return_type->qualifier.flags.q.explicit_index) { 630401e04c3fSmrg unsigned qual_index; 630501e04c3fSmrg if (process_qualifier_constant(state, &loc, "index", 630601e04c3fSmrg this->return_type->qualifier.index, 630701e04c3fSmrg &qual_index)) { 630801e04c3fSmrg if (!state->has_explicit_uniform_location()) { 630901e04c3fSmrg _mesa_glsl_error(&loc, state, "subroutine index requires " 631001e04c3fSmrg "GL_ARB_explicit_uniform_location or " 631101e04c3fSmrg "GLSL 4.30"); 631201e04c3fSmrg } else if (qual_index >= MAX_SUBROUTINES) { 631301e04c3fSmrg _mesa_glsl_error(&loc, state, 631401e04c3fSmrg "invalid subroutine index (%d) index must " 631501e04c3fSmrg "be a number between 0 and " 631601e04c3fSmrg "GL_MAX_SUBROUTINES - 1 (%d)", qual_index, 631701e04c3fSmrg MAX_SUBROUTINES - 1); 631801e04c3fSmrg } else { 631901e04c3fSmrg f->subroutine_index = qual_index; 632001e04c3fSmrg } 632101e04c3fSmrg } 632201e04c3fSmrg } 632301e04c3fSmrg 632401e04c3fSmrg f->num_subroutine_types = this->return_type->qualifier.subroutine_list->declarations.length(); 632501e04c3fSmrg f->subroutine_types = ralloc_array(state, const struct glsl_type *, 632601e04c3fSmrg f->num_subroutine_types); 632701e04c3fSmrg idx = 0; 632801e04c3fSmrg foreach_list_typed(ast_declaration, decl, link, &this->return_type->qualifier.subroutine_list->declarations) { 632901e04c3fSmrg const struct glsl_type *type; 633001e04c3fSmrg /* the subroutine type must be already declared */ 633101e04c3fSmrg type = state->symbols->get_type(decl->identifier); 633201e04c3fSmrg if (!type) { 633301e04c3fSmrg _mesa_glsl_error(& loc, state, "unknown type '%s' in subroutine function definition", decl->identifier); 633401e04c3fSmrg } 633501e04c3fSmrg 633601e04c3fSmrg for (int i = 0; i < state->num_subroutine_types; i++) { 633701e04c3fSmrg ir_function *fn = state->subroutine_types[i]; 633801e04c3fSmrg ir_function_signature *tsig = NULL; 633901e04c3fSmrg 634001e04c3fSmrg if (strcmp(fn->name, decl->identifier)) 634101e04c3fSmrg continue; 634201e04c3fSmrg 634301e04c3fSmrg tsig = fn->matching_signature(state, &sig->parameters, 634401e04c3fSmrg false); 634501e04c3fSmrg if (!tsig) { 634601e04c3fSmrg _mesa_glsl_error(& loc, state, "subroutine type mismatch '%s' - signatures do not match\n", decl->identifier); 634701e04c3fSmrg } else { 634801e04c3fSmrg if (tsig->return_type != sig->return_type) { 634901e04c3fSmrg _mesa_glsl_error(& loc, state, "subroutine type mismatch '%s' - return types do not match\n", decl->identifier); 635001e04c3fSmrg } 635101e04c3fSmrg } 635201e04c3fSmrg } 635301e04c3fSmrg f->subroutine_types[idx++] = type; 635401e04c3fSmrg } 635501e04c3fSmrg state->subroutines = (ir_function **)reralloc(state, state->subroutines, 635601e04c3fSmrg ir_function *, 635701e04c3fSmrg state->num_subroutines + 1); 635801e04c3fSmrg state->subroutines[state->num_subroutines] = f; 635901e04c3fSmrg state->num_subroutines++; 636001e04c3fSmrg 636101e04c3fSmrg } 636201e04c3fSmrg 636301e04c3fSmrg if (this->return_type->qualifier.is_subroutine_decl()) { 636401e04c3fSmrg if (!state->symbols->add_type(this->identifier, glsl_type::get_subroutine_instance(this->identifier))) { 636501e04c3fSmrg _mesa_glsl_error(& loc, state, "type '%s' previously defined", this->identifier); 636601e04c3fSmrg return NULL; 636701e04c3fSmrg } 636801e04c3fSmrg state->subroutine_types = (ir_function **)reralloc(state, state->subroutine_types, 636901e04c3fSmrg ir_function *, 637001e04c3fSmrg state->num_subroutine_types + 1); 637101e04c3fSmrg state->subroutine_types[state->num_subroutine_types] = f; 637201e04c3fSmrg state->num_subroutine_types++; 637301e04c3fSmrg 637401e04c3fSmrg f->is_subroutine = true; 637501e04c3fSmrg } 637601e04c3fSmrg 637701e04c3fSmrg /* Function declarations (prototypes) do not have r-values. 637801e04c3fSmrg */ 637901e04c3fSmrg return NULL; 638001e04c3fSmrg} 638101e04c3fSmrg 638201e04c3fSmrg 638301e04c3fSmrgir_rvalue * 638401e04c3fSmrgast_function_definition::hir(exec_list *instructions, 638501e04c3fSmrg struct _mesa_glsl_parse_state *state) 638601e04c3fSmrg{ 638701e04c3fSmrg prototype->is_definition = true; 638801e04c3fSmrg prototype->hir(instructions, state); 638901e04c3fSmrg 639001e04c3fSmrg ir_function_signature *signature = prototype->signature; 639101e04c3fSmrg if (signature == NULL) 639201e04c3fSmrg return NULL; 639301e04c3fSmrg 639401e04c3fSmrg assert(state->current_function == NULL); 639501e04c3fSmrg state->current_function = signature; 639601e04c3fSmrg state->found_return = false; 63977ec681f3Smrg state->found_begin_interlock = false; 63987ec681f3Smrg state->found_end_interlock = false; 639901e04c3fSmrg 640001e04c3fSmrg /* Duplicate parameters declared in the prototype as concrete variables. 640101e04c3fSmrg * Add these to the symbol table. 640201e04c3fSmrg */ 640301e04c3fSmrg state->symbols->push_scope(); 640401e04c3fSmrg foreach_in_list(ir_variable, var, &signature->parameters) { 640501e04c3fSmrg assert(var->as_variable() != NULL); 640601e04c3fSmrg 640701e04c3fSmrg /* The only way a parameter would "exist" is if two parameters have 640801e04c3fSmrg * the same name. 640901e04c3fSmrg */ 641001e04c3fSmrg if (state->symbols->name_declared_this_scope(var->name)) { 641101e04c3fSmrg YYLTYPE loc = this->get_location(); 641201e04c3fSmrg 641301e04c3fSmrg _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name); 641401e04c3fSmrg } else { 641501e04c3fSmrg state->symbols->add_variable(var); 641601e04c3fSmrg } 641701e04c3fSmrg } 641801e04c3fSmrg 641901e04c3fSmrg /* Convert the body of the function to HIR. */ 642001e04c3fSmrg this->body->hir(&signature->body, state); 642101e04c3fSmrg signature->is_defined = true; 642201e04c3fSmrg 642301e04c3fSmrg state->symbols->pop_scope(); 642401e04c3fSmrg 642501e04c3fSmrg assert(state->current_function == signature); 642601e04c3fSmrg state->current_function = NULL; 642701e04c3fSmrg 642801e04c3fSmrg if (!signature->return_type->is_void() && !state->found_return) { 642901e04c3fSmrg YYLTYPE loc = this->get_location(); 643001e04c3fSmrg _mesa_glsl_error(& loc, state, "function `%s' has non-void return type " 643101e04c3fSmrg "%s, but no return statement", 643201e04c3fSmrg signature->function_name(), 643301e04c3fSmrg signature->return_type->name); 643401e04c3fSmrg } 643501e04c3fSmrg 643601e04c3fSmrg /* Function definitions do not have r-values. 643701e04c3fSmrg */ 643801e04c3fSmrg return NULL; 643901e04c3fSmrg} 644001e04c3fSmrg 644101e04c3fSmrg 644201e04c3fSmrgir_rvalue * 644301e04c3fSmrgast_jump_statement::hir(exec_list *instructions, 644401e04c3fSmrg struct _mesa_glsl_parse_state *state) 644501e04c3fSmrg{ 644601e04c3fSmrg void *ctx = state; 644701e04c3fSmrg 644801e04c3fSmrg switch (mode) { 644901e04c3fSmrg case ast_return: { 645001e04c3fSmrg ir_return *inst; 645101e04c3fSmrg assert(state->current_function); 645201e04c3fSmrg 645301e04c3fSmrg if (opt_return_value) { 645401e04c3fSmrg ir_rvalue *ret = opt_return_value->hir(instructions, state); 645501e04c3fSmrg 645601e04c3fSmrg /* The value of the return type can be NULL if the shader says 645701e04c3fSmrg * 'return foo();' and foo() is a function that returns void. 645801e04c3fSmrg * 645901e04c3fSmrg * NOTE: The GLSL spec doesn't say that this is an error. The type 646001e04c3fSmrg * of the return value is void. If the return type of the function is 646101e04c3fSmrg * also void, then this should compile without error. Seriously. 646201e04c3fSmrg */ 646301e04c3fSmrg const glsl_type *const ret_type = 646401e04c3fSmrg (ret == NULL) ? glsl_type::void_type : ret->type; 646501e04c3fSmrg 646601e04c3fSmrg /* Implicit conversions are not allowed for return values prior to 646701e04c3fSmrg * ARB_shading_language_420pack. 646801e04c3fSmrg */ 646901e04c3fSmrg if (state->current_function->return_type != ret_type) { 647001e04c3fSmrg YYLTYPE loc = this->get_location(); 647101e04c3fSmrg 647201e04c3fSmrg if (state->has_420pack()) { 647301e04c3fSmrg if (!apply_implicit_conversion(state->current_function->return_type, 6474ed98bd31Smaya ret, state) 6475ed98bd31Smaya || (ret->type != state->current_function->return_type)) { 647601e04c3fSmrg _mesa_glsl_error(& loc, state, 647701e04c3fSmrg "could not implicitly convert return value " 647801e04c3fSmrg "to %s, in function `%s'", 647901e04c3fSmrg state->current_function->return_type->name, 648001e04c3fSmrg state->current_function->function_name()); 648101e04c3fSmrg } 648201e04c3fSmrg } else { 648301e04c3fSmrg _mesa_glsl_error(& loc, state, 648401e04c3fSmrg "`return' with wrong type %s, in function `%s' " 648501e04c3fSmrg "returning %s", 648601e04c3fSmrg ret_type->name, 648701e04c3fSmrg state->current_function->function_name(), 648801e04c3fSmrg state->current_function->return_type->name); 648901e04c3fSmrg } 649001e04c3fSmrg } else if (state->current_function->return_type->base_type == 649101e04c3fSmrg GLSL_TYPE_VOID) { 649201e04c3fSmrg YYLTYPE loc = this->get_location(); 649301e04c3fSmrg 649401e04c3fSmrg /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20 649501e04c3fSmrg * specs add a clarification: 649601e04c3fSmrg * 649701e04c3fSmrg * "A void function can only use return without a return argument, even if 649801e04c3fSmrg * the return argument has void type. Return statements only accept values: 649901e04c3fSmrg * 650001e04c3fSmrg * void func1() { } 650101e04c3fSmrg * void func2() { return func1(); } // illegal return statement" 650201e04c3fSmrg */ 650301e04c3fSmrg _mesa_glsl_error(& loc, state, 650401e04c3fSmrg "void functions can only use `return' without a " 650501e04c3fSmrg "return argument"); 650601e04c3fSmrg } 650701e04c3fSmrg 650801e04c3fSmrg inst = new(ctx) ir_return(ret); 650901e04c3fSmrg } else { 651001e04c3fSmrg if (state->current_function->return_type->base_type != 651101e04c3fSmrg GLSL_TYPE_VOID) { 651201e04c3fSmrg YYLTYPE loc = this->get_location(); 651301e04c3fSmrg 651401e04c3fSmrg _mesa_glsl_error(& loc, state, 651501e04c3fSmrg "`return' with no value, in function %s returning " 651601e04c3fSmrg "non-void", 651701e04c3fSmrg state->current_function->function_name()); 651801e04c3fSmrg } 651901e04c3fSmrg inst = new(ctx) ir_return; 652001e04c3fSmrg } 652101e04c3fSmrg 652201e04c3fSmrg state->found_return = true; 652301e04c3fSmrg instructions->push_tail(inst); 652401e04c3fSmrg break; 652501e04c3fSmrg } 652601e04c3fSmrg 652701e04c3fSmrg case ast_discard: 652801e04c3fSmrg if (state->stage != MESA_SHADER_FRAGMENT) { 652901e04c3fSmrg YYLTYPE loc = this->get_location(); 653001e04c3fSmrg 653101e04c3fSmrg _mesa_glsl_error(& loc, state, 653201e04c3fSmrg "`discard' may only appear in a fragment shader"); 653301e04c3fSmrg } 653401e04c3fSmrg instructions->push_tail(new(ctx) ir_discard); 653501e04c3fSmrg break; 653601e04c3fSmrg 653701e04c3fSmrg case ast_break: 653801e04c3fSmrg case ast_continue: 653901e04c3fSmrg if (mode == ast_continue && 654001e04c3fSmrg state->loop_nesting_ast == NULL) { 654101e04c3fSmrg YYLTYPE loc = this->get_location(); 654201e04c3fSmrg 654301e04c3fSmrg _mesa_glsl_error(& loc, state, "continue may only appear in a loop"); 654401e04c3fSmrg } else if (mode == ast_break && 654501e04c3fSmrg state->loop_nesting_ast == NULL && 654601e04c3fSmrg state->switch_state.switch_nesting_ast == NULL) { 654701e04c3fSmrg YYLTYPE loc = this->get_location(); 654801e04c3fSmrg 654901e04c3fSmrg _mesa_glsl_error(& loc, state, 655001e04c3fSmrg "break may only appear in a loop or a switch"); 655101e04c3fSmrg } else { 655201e04c3fSmrg /* For a loop, inline the for loop expression again, since we don't 655301e04c3fSmrg * know where near the end of the loop body the normal copy of it is 655401e04c3fSmrg * going to be placed. Same goes for the condition for a do-while 655501e04c3fSmrg * loop. 655601e04c3fSmrg */ 655701e04c3fSmrg if (state->loop_nesting_ast != NULL && 655801e04c3fSmrg mode == ast_continue && !state->switch_state.is_switch_innermost) { 655901e04c3fSmrg if (state->loop_nesting_ast->rest_expression) { 65607ec681f3Smrg clone_ir_list(ctx, instructions, 65617ec681f3Smrg &state->loop_nesting_ast->rest_instructions); 656201e04c3fSmrg } 656301e04c3fSmrg if (state->loop_nesting_ast->mode == 656401e04c3fSmrg ast_iteration_statement::ast_do_while) { 656501e04c3fSmrg state->loop_nesting_ast->condition_to_hir(instructions, state); 656601e04c3fSmrg } 656701e04c3fSmrg } 656801e04c3fSmrg 656901e04c3fSmrg if (state->switch_state.is_switch_innermost && 657001e04c3fSmrg mode == ast_continue) { 657101e04c3fSmrg /* Set 'continue_inside' to true. */ 657201e04c3fSmrg ir_rvalue *const true_val = new (ctx) ir_constant(true); 657301e04c3fSmrg ir_dereference_variable *deref_continue_inside_var = 657401e04c3fSmrg new(ctx) ir_dereference_variable(state->switch_state.continue_inside); 657501e04c3fSmrg instructions->push_tail(new(ctx) ir_assignment(deref_continue_inside_var, 657601e04c3fSmrg true_val)); 657701e04c3fSmrg 657801e04c3fSmrg /* Break out from the switch, continue for the loop will 657901e04c3fSmrg * be called right after switch. */ 658001e04c3fSmrg ir_loop_jump *const jump = 658101e04c3fSmrg new(ctx) ir_loop_jump(ir_loop_jump::jump_break); 658201e04c3fSmrg instructions->push_tail(jump); 658301e04c3fSmrg 658401e04c3fSmrg } else if (state->switch_state.is_switch_innermost && 658501e04c3fSmrg mode == ast_break) { 658601e04c3fSmrg /* Force break out of switch by inserting a break. */ 658701e04c3fSmrg ir_loop_jump *const jump = 658801e04c3fSmrg new(ctx) ir_loop_jump(ir_loop_jump::jump_break); 658901e04c3fSmrg instructions->push_tail(jump); 659001e04c3fSmrg } else { 659101e04c3fSmrg ir_loop_jump *const jump = 659201e04c3fSmrg new(ctx) ir_loop_jump((mode == ast_break) 659301e04c3fSmrg ? ir_loop_jump::jump_break 659401e04c3fSmrg : ir_loop_jump::jump_continue); 659501e04c3fSmrg instructions->push_tail(jump); 659601e04c3fSmrg } 659701e04c3fSmrg } 659801e04c3fSmrg 659901e04c3fSmrg break; 660001e04c3fSmrg } 660101e04c3fSmrg 660201e04c3fSmrg /* Jump instructions do not have r-values. 660301e04c3fSmrg */ 660401e04c3fSmrg return NULL; 660501e04c3fSmrg} 660601e04c3fSmrg 660701e04c3fSmrg 66087ec681f3Smrgir_rvalue * 66097ec681f3Smrgast_demote_statement::hir(exec_list *instructions, 66107ec681f3Smrg struct _mesa_glsl_parse_state *state) 66117ec681f3Smrg{ 66127ec681f3Smrg void *ctx = state; 66137ec681f3Smrg 66147ec681f3Smrg if (state->stage != MESA_SHADER_FRAGMENT) { 66157ec681f3Smrg YYLTYPE loc = this->get_location(); 66167ec681f3Smrg 66177ec681f3Smrg _mesa_glsl_error(& loc, state, 66187ec681f3Smrg "`demote' may only appear in a fragment shader"); 66197ec681f3Smrg } 66207ec681f3Smrg 66217ec681f3Smrg instructions->push_tail(new(ctx) ir_demote); 66227ec681f3Smrg 66237ec681f3Smrg return NULL; 66247ec681f3Smrg} 66257ec681f3Smrg 66267ec681f3Smrg 662701e04c3fSmrgir_rvalue * 662801e04c3fSmrgast_selection_statement::hir(exec_list *instructions, 662901e04c3fSmrg struct _mesa_glsl_parse_state *state) 663001e04c3fSmrg{ 663101e04c3fSmrg void *ctx = state; 663201e04c3fSmrg 663301e04c3fSmrg ir_rvalue *const condition = this->condition->hir(instructions, state); 663401e04c3fSmrg 663501e04c3fSmrg /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec: 663601e04c3fSmrg * 663701e04c3fSmrg * "Any expression whose type evaluates to a Boolean can be used as the 663801e04c3fSmrg * conditional expression bool-expression. Vector types are not accepted 663901e04c3fSmrg * as the expression to if." 664001e04c3fSmrg * 664101e04c3fSmrg * The checks are separated so that higher quality diagnostics can be 664201e04c3fSmrg * generated for cases where both rules are violated. 664301e04c3fSmrg */ 664401e04c3fSmrg if (!condition->type->is_boolean() || !condition->type->is_scalar()) { 664501e04c3fSmrg YYLTYPE loc = this->condition->get_location(); 664601e04c3fSmrg 664701e04c3fSmrg _mesa_glsl_error(& loc, state, "if-statement condition must be scalar " 664801e04c3fSmrg "boolean"); 664901e04c3fSmrg } 665001e04c3fSmrg 665101e04c3fSmrg ir_if *const stmt = new(ctx) ir_if(condition); 665201e04c3fSmrg 665301e04c3fSmrg if (then_statement != NULL) { 665401e04c3fSmrg state->symbols->push_scope(); 665501e04c3fSmrg then_statement->hir(& stmt->then_instructions, state); 665601e04c3fSmrg state->symbols->pop_scope(); 665701e04c3fSmrg } 665801e04c3fSmrg 665901e04c3fSmrg if (else_statement != NULL) { 666001e04c3fSmrg state->symbols->push_scope(); 666101e04c3fSmrg else_statement->hir(& stmt->else_instructions, state); 666201e04c3fSmrg state->symbols->pop_scope(); 666301e04c3fSmrg } 666401e04c3fSmrg 666501e04c3fSmrg instructions->push_tail(stmt); 666601e04c3fSmrg 666701e04c3fSmrg /* if-statements do not have r-values. 666801e04c3fSmrg */ 666901e04c3fSmrg return NULL; 667001e04c3fSmrg} 667101e04c3fSmrg 667201e04c3fSmrg 667301e04c3fSmrgstruct case_label { 667401e04c3fSmrg /** Value of the case label. */ 667501e04c3fSmrg unsigned value; 667601e04c3fSmrg 667701e04c3fSmrg /** Does this label occur after the default? */ 667801e04c3fSmrg bool after_default; 667901e04c3fSmrg 668001e04c3fSmrg /** 668101e04c3fSmrg * AST for the case label. 668201e04c3fSmrg * 668301e04c3fSmrg * This is only used to generate error messages for duplicate labels. 668401e04c3fSmrg */ 668501e04c3fSmrg ast_expression *ast; 668601e04c3fSmrg}; 668701e04c3fSmrg 668801e04c3fSmrg/* Used for detection of duplicate case values, compare 668901e04c3fSmrg * given contents directly. 669001e04c3fSmrg */ 669101e04c3fSmrgstatic bool 669201e04c3fSmrgcompare_case_value(const void *a, const void *b) 669301e04c3fSmrg{ 669401e04c3fSmrg return ((struct case_label *) a)->value == ((struct case_label *) b)->value; 669501e04c3fSmrg} 669601e04c3fSmrg 669701e04c3fSmrg 669801e04c3fSmrg/* Used for detection of duplicate case values, just 669901e04c3fSmrg * returns key contents as is. 670001e04c3fSmrg */ 670101e04c3fSmrgstatic unsigned 670201e04c3fSmrgkey_contents(const void *key) 670301e04c3fSmrg{ 670401e04c3fSmrg return ((struct case_label *) key)->value; 670501e04c3fSmrg} 670601e04c3fSmrg 67077ec681f3Smrgvoid 67087ec681f3Smrgast_switch_statement::eval_test_expression(exec_list *instructions, 67097ec681f3Smrg struct _mesa_glsl_parse_state *state) 67107ec681f3Smrg{ 67117ec681f3Smrg if (test_val == NULL) 67127ec681f3Smrg test_val = this->test_expression->hir(instructions, state); 67137ec681f3Smrg} 671401e04c3fSmrg 671501e04c3fSmrgir_rvalue * 671601e04c3fSmrgast_switch_statement::hir(exec_list *instructions, 671701e04c3fSmrg struct _mesa_glsl_parse_state *state) 671801e04c3fSmrg{ 671901e04c3fSmrg void *ctx = state; 672001e04c3fSmrg 67217ec681f3Smrg this->eval_test_expression(instructions, state); 672201e04c3fSmrg 672301e04c3fSmrg /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec: 672401e04c3fSmrg * 672501e04c3fSmrg * "The type of init-expression in a switch statement must be a 672601e04c3fSmrg * scalar integer." 672701e04c3fSmrg */ 67287ec681f3Smrg if (!test_val->type->is_scalar() || 67297ec681f3Smrg !test_val->type->is_integer_32()) { 673001e04c3fSmrg YYLTYPE loc = this->test_expression->get_location(); 673101e04c3fSmrg 673201e04c3fSmrg _mesa_glsl_error(& loc, 673301e04c3fSmrg state, 673401e04c3fSmrg "switch-statement expression must be scalar " 673501e04c3fSmrg "integer"); 673601e04c3fSmrg return NULL; 673701e04c3fSmrg } 673801e04c3fSmrg 673901e04c3fSmrg /* Track the switch-statement nesting in a stack-like manner. 674001e04c3fSmrg */ 674101e04c3fSmrg struct glsl_switch_state saved = state->switch_state; 674201e04c3fSmrg 674301e04c3fSmrg state->switch_state.is_switch_innermost = true; 674401e04c3fSmrg state->switch_state.switch_nesting_ast = this; 674501e04c3fSmrg state->switch_state.labels_ht = 674601e04c3fSmrg _mesa_hash_table_create(NULL, key_contents, 674701e04c3fSmrg compare_case_value); 674801e04c3fSmrg state->switch_state.previous_default = NULL; 674901e04c3fSmrg 675001e04c3fSmrg /* Initalize is_fallthru state to false. 675101e04c3fSmrg */ 675201e04c3fSmrg ir_rvalue *const is_fallthru_val = new (ctx) ir_constant(false); 675301e04c3fSmrg state->switch_state.is_fallthru_var = 675401e04c3fSmrg new(ctx) ir_variable(glsl_type::bool_type, 675501e04c3fSmrg "switch_is_fallthru_tmp", 675601e04c3fSmrg ir_var_temporary); 675701e04c3fSmrg instructions->push_tail(state->switch_state.is_fallthru_var); 675801e04c3fSmrg 675901e04c3fSmrg ir_dereference_variable *deref_is_fallthru_var = 676001e04c3fSmrg new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var); 676101e04c3fSmrg instructions->push_tail(new(ctx) ir_assignment(deref_is_fallthru_var, 676201e04c3fSmrg is_fallthru_val)); 676301e04c3fSmrg 676401e04c3fSmrg /* Initialize continue_inside state to false. 676501e04c3fSmrg */ 676601e04c3fSmrg state->switch_state.continue_inside = 676701e04c3fSmrg new(ctx) ir_variable(glsl_type::bool_type, 676801e04c3fSmrg "continue_inside_tmp", 676901e04c3fSmrg ir_var_temporary); 677001e04c3fSmrg instructions->push_tail(state->switch_state.continue_inside); 677101e04c3fSmrg 677201e04c3fSmrg ir_rvalue *const false_val = new (ctx) ir_constant(false); 677301e04c3fSmrg ir_dereference_variable *deref_continue_inside_var = 677401e04c3fSmrg new(ctx) ir_dereference_variable(state->switch_state.continue_inside); 677501e04c3fSmrg instructions->push_tail(new(ctx) ir_assignment(deref_continue_inside_var, 677601e04c3fSmrg false_val)); 677701e04c3fSmrg 677801e04c3fSmrg state->switch_state.run_default = 677901e04c3fSmrg new(ctx) ir_variable(glsl_type::bool_type, 678001e04c3fSmrg "run_default_tmp", 678101e04c3fSmrg ir_var_temporary); 678201e04c3fSmrg instructions->push_tail(state->switch_state.run_default); 678301e04c3fSmrg 678401e04c3fSmrg /* Loop around the switch is used for flow control. */ 678501e04c3fSmrg ir_loop * loop = new(ctx) ir_loop(); 678601e04c3fSmrg instructions->push_tail(loop); 678701e04c3fSmrg 678801e04c3fSmrg /* Cache test expression. 678901e04c3fSmrg */ 679001e04c3fSmrg test_to_hir(&loop->body_instructions, state); 679101e04c3fSmrg 679201e04c3fSmrg /* Emit code for body of switch stmt. 679301e04c3fSmrg */ 679401e04c3fSmrg body->hir(&loop->body_instructions, state); 679501e04c3fSmrg 679601e04c3fSmrg /* Insert a break at the end to exit loop. */ 679701e04c3fSmrg ir_loop_jump *jump = new(ctx) ir_loop_jump(ir_loop_jump::jump_break); 679801e04c3fSmrg loop->body_instructions.push_tail(jump); 679901e04c3fSmrg 680001e04c3fSmrg /* If we are inside loop, check if continue got called inside switch. */ 680101e04c3fSmrg if (state->loop_nesting_ast != NULL) { 680201e04c3fSmrg ir_dereference_variable *deref_continue_inside = 680301e04c3fSmrg new(ctx) ir_dereference_variable(state->switch_state.continue_inside); 680401e04c3fSmrg ir_if *irif = new(ctx) ir_if(deref_continue_inside); 680501e04c3fSmrg ir_loop_jump *jump = new(ctx) ir_loop_jump(ir_loop_jump::jump_continue); 680601e04c3fSmrg 680701e04c3fSmrg if (state->loop_nesting_ast != NULL) { 680801e04c3fSmrg if (state->loop_nesting_ast->rest_expression) { 68097ec681f3Smrg clone_ir_list(ctx, &irif->then_instructions, 68107ec681f3Smrg &state->loop_nesting_ast->rest_instructions); 681101e04c3fSmrg } 681201e04c3fSmrg if (state->loop_nesting_ast->mode == 681301e04c3fSmrg ast_iteration_statement::ast_do_while) { 681401e04c3fSmrg state->loop_nesting_ast->condition_to_hir(&irif->then_instructions, state); 681501e04c3fSmrg } 681601e04c3fSmrg } 681701e04c3fSmrg irif->then_instructions.push_tail(jump); 681801e04c3fSmrg instructions->push_tail(irif); 681901e04c3fSmrg } 682001e04c3fSmrg 682101e04c3fSmrg _mesa_hash_table_destroy(state->switch_state.labels_ht, NULL); 682201e04c3fSmrg 682301e04c3fSmrg state->switch_state = saved; 682401e04c3fSmrg 682501e04c3fSmrg /* Switch statements do not have r-values. */ 682601e04c3fSmrg return NULL; 682701e04c3fSmrg} 682801e04c3fSmrg 682901e04c3fSmrg 683001e04c3fSmrgvoid 683101e04c3fSmrgast_switch_statement::test_to_hir(exec_list *instructions, 683201e04c3fSmrg struct _mesa_glsl_parse_state *state) 683301e04c3fSmrg{ 683401e04c3fSmrg void *ctx = state; 683501e04c3fSmrg 683601e04c3fSmrg /* set to true to avoid a duplicate "use of uninitialized variable" warning 683701e04c3fSmrg * on the switch test case. The first one would be already raised when 683801e04c3fSmrg * getting the test_expression at ast_switch_statement::hir 683901e04c3fSmrg */ 684001e04c3fSmrg test_expression->set_is_lhs(true); 684101e04c3fSmrg /* Cache value of test expression. */ 68427ec681f3Smrg this->eval_test_expression(instructions, state); 684301e04c3fSmrg 684401e04c3fSmrg state->switch_state.test_var = new(ctx) ir_variable(test_val->type, 684501e04c3fSmrg "switch_test_tmp", 684601e04c3fSmrg ir_var_temporary); 684701e04c3fSmrg ir_dereference_variable *deref_test_var = 684801e04c3fSmrg new(ctx) ir_dereference_variable(state->switch_state.test_var); 684901e04c3fSmrg 685001e04c3fSmrg instructions->push_tail(state->switch_state.test_var); 685101e04c3fSmrg instructions->push_tail(new(ctx) ir_assignment(deref_test_var, test_val)); 685201e04c3fSmrg} 685301e04c3fSmrg 685401e04c3fSmrg 685501e04c3fSmrgir_rvalue * 685601e04c3fSmrgast_switch_body::hir(exec_list *instructions, 685701e04c3fSmrg struct _mesa_glsl_parse_state *state) 685801e04c3fSmrg{ 68597ec681f3Smrg if (stmts != NULL) { 68607ec681f3Smrg state->symbols->push_scope(); 686101e04c3fSmrg stmts->hir(instructions, state); 68627ec681f3Smrg state->symbols->pop_scope(); 68637ec681f3Smrg } 686401e04c3fSmrg 686501e04c3fSmrg /* Switch bodies do not have r-values. */ 686601e04c3fSmrg return NULL; 686701e04c3fSmrg} 686801e04c3fSmrg 686901e04c3fSmrgir_rvalue * 687001e04c3fSmrgast_case_statement_list::hir(exec_list *instructions, 687101e04c3fSmrg struct _mesa_glsl_parse_state *state) 687201e04c3fSmrg{ 687301e04c3fSmrg exec_list default_case, after_default, tmp; 687401e04c3fSmrg 687501e04c3fSmrg foreach_list_typed (ast_case_statement, case_stmt, link, & this->cases) { 687601e04c3fSmrg case_stmt->hir(&tmp, state); 687701e04c3fSmrg 687801e04c3fSmrg /* Default case. */ 687901e04c3fSmrg if (state->switch_state.previous_default && default_case.is_empty()) { 688001e04c3fSmrg default_case.append_list(&tmp); 688101e04c3fSmrg continue; 688201e04c3fSmrg } 688301e04c3fSmrg 688401e04c3fSmrg /* If default case found, append 'after_default' list. */ 688501e04c3fSmrg if (!default_case.is_empty()) 688601e04c3fSmrg after_default.append_list(&tmp); 688701e04c3fSmrg else 688801e04c3fSmrg instructions->append_list(&tmp); 688901e04c3fSmrg } 689001e04c3fSmrg 689101e04c3fSmrg /* Handle the default case. This is done here because default might not be 689201e04c3fSmrg * the last case. We need to add checks against following cases first to see 689301e04c3fSmrg * if default should be chosen or not. 689401e04c3fSmrg */ 689501e04c3fSmrg if (!default_case.is_empty()) { 689601e04c3fSmrg ir_factory body(instructions, state); 689701e04c3fSmrg 689801e04c3fSmrg ir_expression *cmp = NULL; 689901e04c3fSmrg 690001e04c3fSmrg hash_table_foreach(state->switch_state.labels_ht, entry) { 690101e04c3fSmrg const struct case_label *const l = (struct case_label *) entry->data; 690201e04c3fSmrg 690301e04c3fSmrg /* If the switch init-value is the value of one of the labels that 690401e04c3fSmrg * occurs after the default case, disable execution of the default 690501e04c3fSmrg * case. 690601e04c3fSmrg */ 690701e04c3fSmrg if (l->after_default) { 690801e04c3fSmrg ir_constant *const cnst = 690901e04c3fSmrg state->switch_state.test_var->type->base_type == GLSL_TYPE_UINT 691001e04c3fSmrg ? body.constant(unsigned(l->value)) 691101e04c3fSmrg : body.constant(int(l->value)); 691201e04c3fSmrg 691301e04c3fSmrg cmp = cmp == NULL 691401e04c3fSmrg ? equal(cnst, state->switch_state.test_var) 691501e04c3fSmrg : logic_or(cmp, equal(cnst, state->switch_state.test_var)); 691601e04c3fSmrg } 691701e04c3fSmrg } 691801e04c3fSmrg 691901e04c3fSmrg if (cmp != NULL) 692001e04c3fSmrg body.emit(assign(state->switch_state.run_default, logic_not(cmp))); 692101e04c3fSmrg else 692201e04c3fSmrg body.emit(assign(state->switch_state.run_default, body.constant(true))); 692301e04c3fSmrg 692401e04c3fSmrg /* Append default case and all cases after it. */ 692501e04c3fSmrg instructions->append_list(&default_case); 692601e04c3fSmrg instructions->append_list(&after_default); 692701e04c3fSmrg } 692801e04c3fSmrg 692901e04c3fSmrg /* Case statements do not have r-values. */ 693001e04c3fSmrg return NULL; 693101e04c3fSmrg} 693201e04c3fSmrg 693301e04c3fSmrgir_rvalue * 693401e04c3fSmrgast_case_statement::hir(exec_list *instructions, 693501e04c3fSmrg struct _mesa_glsl_parse_state *state) 693601e04c3fSmrg{ 693701e04c3fSmrg labels->hir(instructions, state); 693801e04c3fSmrg 693901e04c3fSmrg /* Guard case statements depending on fallthru state. */ 694001e04c3fSmrg ir_dereference_variable *const deref_fallthru_guard = 694101e04c3fSmrg new(state) ir_dereference_variable(state->switch_state.is_fallthru_var); 694201e04c3fSmrg ir_if *const test_fallthru = new(state) ir_if(deref_fallthru_guard); 694301e04c3fSmrg 694401e04c3fSmrg foreach_list_typed (ast_node, stmt, link, & this->stmts) 694501e04c3fSmrg stmt->hir(& test_fallthru->then_instructions, state); 694601e04c3fSmrg 694701e04c3fSmrg instructions->push_tail(test_fallthru); 694801e04c3fSmrg 694901e04c3fSmrg /* Case statements do not have r-values. */ 695001e04c3fSmrg return NULL; 695101e04c3fSmrg} 695201e04c3fSmrg 695301e04c3fSmrg 695401e04c3fSmrgir_rvalue * 695501e04c3fSmrgast_case_label_list::hir(exec_list *instructions, 695601e04c3fSmrg struct _mesa_glsl_parse_state *state) 695701e04c3fSmrg{ 695801e04c3fSmrg foreach_list_typed (ast_case_label, label, link, & this->labels) 695901e04c3fSmrg label->hir(instructions, state); 696001e04c3fSmrg 696101e04c3fSmrg /* Case labels do not have r-values. */ 696201e04c3fSmrg return NULL; 696301e04c3fSmrg} 696401e04c3fSmrg 696501e04c3fSmrgir_rvalue * 696601e04c3fSmrgast_case_label::hir(exec_list *instructions, 696701e04c3fSmrg struct _mesa_glsl_parse_state *state) 696801e04c3fSmrg{ 696901e04c3fSmrg ir_factory body(instructions, state); 697001e04c3fSmrg 697101e04c3fSmrg ir_variable *const fallthru_var = state->switch_state.is_fallthru_var; 697201e04c3fSmrg 697301e04c3fSmrg /* If not default case, ... */ 697401e04c3fSmrg if (this->test_value != NULL) { 697501e04c3fSmrg /* Conditionally set fallthru state based on 697601e04c3fSmrg * comparison of cached test expression value to case label. 697701e04c3fSmrg */ 697801e04c3fSmrg ir_rvalue *const label_rval = this->test_value->hir(instructions, state); 697901e04c3fSmrg ir_constant *label_const = 698001e04c3fSmrg label_rval->constant_expression_value(body.mem_ctx); 698101e04c3fSmrg 698201e04c3fSmrg if (!label_const) { 698301e04c3fSmrg YYLTYPE loc = this->test_value->get_location(); 698401e04c3fSmrg 698501e04c3fSmrg _mesa_glsl_error(& loc, state, 698601e04c3fSmrg "switch statement case label must be a " 698701e04c3fSmrg "constant expression"); 698801e04c3fSmrg 698901e04c3fSmrg /* Stuff a dummy value in to allow processing to continue. */ 699001e04c3fSmrg label_const = body.constant(0); 699101e04c3fSmrg } else { 699201e04c3fSmrg hash_entry *entry = 699301e04c3fSmrg _mesa_hash_table_search(state->switch_state.labels_ht, 699401e04c3fSmrg &label_const->value.u[0]); 699501e04c3fSmrg 699601e04c3fSmrg if (entry) { 699701e04c3fSmrg const struct case_label *const l = 699801e04c3fSmrg (struct case_label *) entry->data; 699901e04c3fSmrg const ast_expression *const previous_label = l->ast; 700001e04c3fSmrg YYLTYPE loc = this->test_value->get_location(); 700101e04c3fSmrg 700201e04c3fSmrg _mesa_glsl_error(& loc, state, "duplicate case value"); 700301e04c3fSmrg 700401e04c3fSmrg loc = previous_label->get_location(); 700501e04c3fSmrg _mesa_glsl_error(& loc, state, "this is the previous case label"); 700601e04c3fSmrg } else { 700701e04c3fSmrg struct case_label *l = ralloc(state->switch_state.labels_ht, 700801e04c3fSmrg struct case_label); 700901e04c3fSmrg 701001e04c3fSmrg l->value = label_const->value.u[0]; 701101e04c3fSmrg l->after_default = state->switch_state.previous_default != NULL; 701201e04c3fSmrg l->ast = this->test_value; 701301e04c3fSmrg 701401e04c3fSmrg _mesa_hash_table_insert(state->switch_state.labels_ht, 701501e04c3fSmrg &label_const->value.u[0], 701601e04c3fSmrg l); 701701e04c3fSmrg } 701801e04c3fSmrg } 701901e04c3fSmrg 702001e04c3fSmrg /* Create an r-value version of the ir_constant label here (after we may 702101e04c3fSmrg * have created a fake one in error cases) that can be passed to 702201e04c3fSmrg * apply_implicit_conversion below. 702301e04c3fSmrg */ 702401e04c3fSmrg ir_rvalue *label = label_const; 702501e04c3fSmrg 702601e04c3fSmrg ir_rvalue *deref_test_var = 702701e04c3fSmrg new(body.mem_ctx) ir_dereference_variable(state->switch_state.test_var); 702801e04c3fSmrg 702901e04c3fSmrg /* 703001e04c3fSmrg * From GLSL 4.40 specification section 6.2 ("Selection"): 703101e04c3fSmrg * 703201e04c3fSmrg * "The type of the init-expression value in a switch statement must 703301e04c3fSmrg * be a scalar int or uint. The type of the constant-expression value 703401e04c3fSmrg * in a case label also must be a scalar int or uint. When any pair 703501e04c3fSmrg * of these values is tested for "equal value" and the types do not 703601e04c3fSmrg * match, an implicit conversion will be done to convert the int to a 703701e04c3fSmrg * uint (see section 4.1.10 “Implicit Conversions”) before the compare 703801e04c3fSmrg * is done." 703901e04c3fSmrg */ 704001e04c3fSmrg if (label->type != state->switch_state.test_var->type) { 704101e04c3fSmrg YYLTYPE loc = this->test_value->get_location(); 704201e04c3fSmrg 704301e04c3fSmrg const glsl_type *type_a = label->type; 704401e04c3fSmrg const glsl_type *type_b = state->switch_state.test_var->type; 704501e04c3fSmrg 704601e04c3fSmrg /* Check if int->uint implicit conversion is supported. */ 704701e04c3fSmrg bool integer_conversion_supported = 704801e04c3fSmrg glsl_type::int_type->can_implicitly_convert_to(glsl_type::uint_type, 704901e04c3fSmrg state); 705001e04c3fSmrg 70517ec681f3Smrg if ((!type_a->is_integer_32() || !type_b->is_integer_32()) || 705201e04c3fSmrg !integer_conversion_supported) { 705301e04c3fSmrg _mesa_glsl_error(&loc, state, "type mismatch with switch " 705401e04c3fSmrg "init-expression and case label (%s != %s)", 705501e04c3fSmrg type_a->name, type_b->name); 705601e04c3fSmrg } else { 705701e04c3fSmrg /* Conversion of the case label. */ 705801e04c3fSmrg if (type_a->base_type == GLSL_TYPE_INT) { 705901e04c3fSmrg if (!apply_implicit_conversion(glsl_type::uint_type, 706001e04c3fSmrg label, state)) 706101e04c3fSmrg _mesa_glsl_error(&loc, state, "implicit type conversion error"); 706201e04c3fSmrg } else { 706301e04c3fSmrg /* Conversion of the init-expression value. */ 706401e04c3fSmrg if (!apply_implicit_conversion(glsl_type::uint_type, 706501e04c3fSmrg deref_test_var, state)) 706601e04c3fSmrg _mesa_glsl_error(&loc, state, "implicit type conversion error"); 706701e04c3fSmrg } 706801e04c3fSmrg } 706901e04c3fSmrg 707001e04c3fSmrg /* If the implicit conversion was allowed, the types will already be 707101e04c3fSmrg * the same. If the implicit conversion wasn't allowed, smash the 707201e04c3fSmrg * type of the label anyway. This will prevent the expression 707301e04c3fSmrg * constructor (below) from failing an assertion. 707401e04c3fSmrg */ 707501e04c3fSmrg label->type = deref_test_var->type; 707601e04c3fSmrg } 707701e04c3fSmrg 707801e04c3fSmrg body.emit(assign(fallthru_var, 707901e04c3fSmrg logic_or(fallthru_var, equal(label, deref_test_var)))); 708001e04c3fSmrg } else { /* default case */ 708101e04c3fSmrg if (state->switch_state.previous_default) { 708201e04c3fSmrg YYLTYPE loc = this->get_location(); 708301e04c3fSmrg _mesa_glsl_error(& loc, state, 708401e04c3fSmrg "multiple default labels in one switch"); 708501e04c3fSmrg 708601e04c3fSmrg loc = state->switch_state.previous_default->get_location(); 708701e04c3fSmrg _mesa_glsl_error(& loc, state, "this is the first default label"); 708801e04c3fSmrg } 708901e04c3fSmrg state->switch_state.previous_default = this; 709001e04c3fSmrg 709101e04c3fSmrg /* Set fallthru condition on 'run_default' bool. */ 709201e04c3fSmrg body.emit(assign(fallthru_var, 709301e04c3fSmrg logic_or(fallthru_var, 709401e04c3fSmrg state->switch_state.run_default))); 709501e04c3fSmrg } 709601e04c3fSmrg 709701e04c3fSmrg /* Case statements do not have r-values. */ 709801e04c3fSmrg return NULL; 709901e04c3fSmrg} 710001e04c3fSmrg 710101e04c3fSmrgvoid 710201e04c3fSmrgast_iteration_statement::condition_to_hir(exec_list *instructions, 710301e04c3fSmrg struct _mesa_glsl_parse_state *state) 710401e04c3fSmrg{ 710501e04c3fSmrg void *ctx = state; 710601e04c3fSmrg 710701e04c3fSmrg if (condition != NULL) { 710801e04c3fSmrg ir_rvalue *const cond = 710901e04c3fSmrg condition->hir(instructions, state); 711001e04c3fSmrg 711101e04c3fSmrg if ((cond == NULL) 711201e04c3fSmrg || !cond->type->is_boolean() || !cond->type->is_scalar()) { 711301e04c3fSmrg YYLTYPE loc = condition->get_location(); 711401e04c3fSmrg 711501e04c3fSmrg _mesa_glsl_error(& loc, state, 711601e04c3fSmrg "loop condition must be scalar boolean"); 711701e04c3fSmrg } else { 711801e04c3fSmrg /* As the first code in the loop body, generate a block that looks 711901e04c3fSmrg * like 'if (!condition) break;' as the loop termination condition. 712001e04c3fSmrg */ 712101e04c3fSmrg ir_rvalue *const not_cond = 712201e04c3fSmrg new(ctx) ir_expression(ir_unop_logic_not, cond); 712301e04c3fSmrg 712401e04c3fSmrg ir_if *const if_stmt = new(ctx) ir_if(not_cond); 712501e04c3fSmrg 712601e04c3fSmrg ir_jump *const break_stmt = 712701e04c3fSmrg new(ctx) ir_loop_jump(ir_loop_jump::jump_break); 712801e04c3fSmrg 712901e04c3fSmrg if_stmt->then_instructions.push_tail(break_stmt); 713001e04c3fSmrg instructions->push_tail(if_stmt); 713101e04c3fSmrg } 713201e04c3fSmrg } 713301e04c3fSmrg} 713401e04c3fSmrg 713501e04c3fSmrg 713601e04c3fSmrgir_rvalue * 713701e04c3fSmrgast_iteration_statement::hir(exec_list *instructions, 713801e04c3fSmrg struct _mesa_glsl_parse_state *state) 713901e04c3fSmrg{ 714001e04c3fSmrg void *ctx = state; 714101e04c3fSmrg 714201e04c3fSmrg /* For-loops and while-loops start a new scope, but do-while loops do not. 714301e04c3fSmrg */ 714401e04c3fSmrg if (mode != ast_do_while) 714501e04c3fSmrg state->symbols->push_scope(); 714601e04c3fSmrg 714701e04c3fSmrg if (init_statement != NULL) 714801e04c3fSmrg init_statement->hir(instructions, state); 714901e04c3fSmrg 715001e04c3fSmrg ir_loop *const stmt = new(ctx) ir_loop(); 715101e04c3fSmrg instructions->push_tail(stmt); 715201e04c3fSmrg 715301e04c3fSmrg /* Track the current loop nesting. */ 715401e04c3fSmrg ast_iteration_statement *nesting_ast = state->loop_nesting_ast; 715501e04c3fSmrg 715601e04c3fSmrg state->loop_nesting_ast = this; 715701e04c3fSmrg 715801e04c3fSmrg /* Likewise, indicate that following code is closest to a loop, 715901e04c3fSmrg * NOT closest to a switch. 716001e04c3fSmrg */ 716101e04c3fSmrg bool saved_is_switch_innermost = state->switch_state.is_switch_innermost; 716201e04c3fSmrg state->switch_state.is_switch_innermost = false; 716301e04c3fSmrg 716401e04c3fSmrg if (mode != ast_do_while) 716501e04c3fSmrg condition_to_hir(&stmt->body_instructions, state); 716601e04c3fSmrg 71677ec681f3Smrg if (rest_expression != NULL) 71687ec681f3Smrg rest_expression->hir(&rest_instructions, state); 71697ec681f3Smrg 71707ec681f3Smrg if (body != NULL) { 71717ec681f3Smrg if (mode == ast_do_while) 71727ec681f3Smrg state->symbols->push_scope(); 71737ec681f3Smrg 717401e04c3fSmrg body->hir(& stmt->body_instructions, state); 717501e04c3fSmrg 71767ec681f3Smrg if (mode == ast_do_while) 71777ec681f3Smrg state->symbols->pop_scope(); 71787ec681f3Smrg } 71797ec681f3Smrg 718001e04c3fSmrg if (rest_expression != NULL) 71817ec681f3Smrg stmt->body_instructions.append_list(&rest_instructions); 718201e04c3fSmrg 718301e04c3fSmrg if (mode == ast_do_while) 718401e04c3fSmrg condition_to_hir(&stmt->body_instructions, state); 718501e04c3fSmrg 718601e04c3fSmrg if (mode != ast_do_while) 718701e04c3fSmrg state->symbols->pop_scope(); 718801e04c3fSmrg 718901e04c3fSmrg /* Restore previous nesting before returning. */ 719001e04c3fSmrg state->loop_nesting_ast = nesting_ast; 719101e04c3fSmrg state->switch_state.is_switch_innermost = saved_is_switch_innermost; 719201e04c3fSmrg 719301e04c3fSmrg /* Loops do not have r-values. 719401e04c3fSmrg */ 719501e04c3fSmrg return NULL; 719601e04c3fSmrg} 719701e04c3fSmrg 719801e04c3fSmrg 719901e04c3fSmrg/** 720001e04c3fSmrg * Determine if the given type is valid for establishing a default precision 720101e04c3fSmrg * qualifier. 720201e04c3fSmrg * 720301e04c3fSmrg * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"): 720401e04c3fSmrg * 720501e04c3fSmrg * "The precision statement 720601e04c3fSmrg * 720701e04c3fSmrg * precision precision-qualifier type; 720801e04c3fSmrg * 720901e04c3fSmrg * can be used to establish a default precision qualifier. The type field 721001e04c3fSmrg * can be either int or float or any of the sampler types, and the 721101e04c3fSmrg * precision-qualifier can be lowp, mediump, or highp." 721201e04c3fSmrg * 721301e04c3fSmrg * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision 721401e04c3fSmrg * qualifiers on sampler types, but this seems like an oversight (since the 721501e04c3fSmrg * intention of including these in GLSL 1.30 is to allow compatibility with ES 721601e04c3fSmrg * shaders). So we allow int, float, and all sampler types regardless of GLSL 721701e04c3fSmrg * version. 721801e04c3fSmrg */ 721901e04c3fSmrgstatic bool 722001e04c3fSmrgis_valid_default_precision_type(const struct glsl_type *const type) 722101e04c3fSmrg{ 722201e04c3fSmrg if (type == NULL) 722301e04c3fSmrg return false; 722401e04c3fSmrg 722501e04c3fSmrg switch (type->base_type) { 722601e04c3fSmrg case GLSL_TYPE_INT: 722701e04c3fSmrg case GLSL_TYPE_FLOAT: 722801e04c3fSmrg /* "int" and "float" are valid, but vectors and matrices are not. */ 722901e04c3fSmrg return type->vector_elements == 1 && type->matrix_columns == 1; 723001e04c3fSmrg case GLSL_TYPE_SAMPLER: 723101e04c3fSmrg case GLSL_TYPE_IMAGE: 723201e04c3fSmrg case GLSL_TYPE_ATOMIC_UINT: 723301e04c3fSmrg return true; 723401e04c3fSmrg default: 723501e04c3fSmrg return false; 723601e04c3fSmrg } 723701e04c3fSmrg} 723801e04c3fSmrg 723901e04c3fSmrg 724001e04c3fSmrgir_rvalue * 724101e04c3fSmrgast_type_specifier::hir(exec_list *instructions, 724201e04c3fSmrg struct _mesa_glsl_parse_state *state) 724301e04c3fSmrg{ 724401e04c3fSmrg if (this->default_precision == ast_precision_none && this->structure == NULL) 724501e04c3fSmrg return NULL; 724601e04c3fSmrg 724701e04c3fSmrg YYLTYPE loc = this->get_location(); 724801e04c3fSmrg 724901e04c3fSmrg /* If this is a precision statement, check that the type to which it is 725001e04c3fSmrg * applied is either float or int. 725101e04c3fSmrg * 725201e04c3fSmrg * From section 4.5.3 of the GLSL 1.30 spec: 725301e04c3fSmrg * "The precision statement 725401e04c3fSmrg * precision precision-qualifier type; 725501e04c3fSmrg * can be used to establish a default precision qualifier. The type 725601e04c3fSmrg * field can be either int or float [...]. Any other types or 725701e04c3fSmrg * qualifiers will result in an error. 725801e04c3fSmrg */ 725901e04c3fSmrg if (this->default_precision != ast_precision_none) { 726001e04c3fSmrg if (!state->check_precision_qualifiers_allowed(&loc)) 726101e04c3fSmrg return NULL; 726201e04c3fSmrg 726301e04c3fSmrg if (this->structure != NULL) { 726401e04c3fSmrg _mesa_glsl_error(&loc, state, 726501e04c3fSmrg "precision qualifiers do not apply to structures"); 726601e04c3fSmrg return NULL; 726701e04c3fSmrg } 726801e04c3fSmrg 726901e04c3fSmrg if (this->array_specifier != NULL) { 727001e04c3fSmrg _mesa_glsl_error(&loc, state, 727101e04c3fSmrg "default precision statements do not apply to " 727201e04c3fSmrg "arrays"); 727301e04c3fSmrg return NULL; 727401e04c3fSmrg } 727501e04c3fSmrg 727601e04c3fSmrg const struct glsl_type *const type = 727701e04c3fSmrg state->symbols->get_type(this->type_name); 727801e04c3fSmrg if (!is_valid_default_precision_type(type)) { 727901e04c3fSmrg _mesa_glsl_error(&loc, state, 728001e04c3fSmrg "default precision statements apply only to " 728101e04c3fSmrg "float, int, and opaque types"); 728201e04c3fSmrg return NULL; 728301e04c3fSmrg } 728401e04c3fSmrg 728501e04c3fSmrg if (state->es_shader) { 728601e04c3fSmrg /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00 728701e04c3fSmrg * spec says: 728801e04c3fSmrg * 728901e04c3fSmrg * "Non-precision qualified declarations will use the precision 729001e04c3fSmrg * qualifier specified in the most recent precision statement 729101e04c3fSmrg * that is still in scope. The precision statement has the same 729201e04c3fSmrg * scoping rules as variable declarations. If it is declared 729301e04c3fSmrg * inside a compound statement, its effect stops at the end of 729401e04c3fSmrg * the innermost statement it was declared in. Precision 729501e04c3fSmrg * statements in nested scopes override precision statements in 729601e04c3fSmrg * outer scopes. Multiple precision statements for the same basic 729701e04c3fSmrg * type can appear inside the same scope, with later statements 729801e04c3fSmrg * overriding earlier statements within that scope." 729901e04c3fSmrg * 730001e04c3fSmrg * Default precision specifications follow the same scope rules as 730101e04c3fSmrg * variables. So, we can track the state of the default precision 730201e04c3fSmrg * qualifiers in the symbol table, and the rules will just work. This 730301e04c3fSmrg * is a slight abuse of the symbol table, but it has the semantics 730401e04c3fSmrg * that we want. 730501e04c3fSmrg */ 730601e04c3fSmrg state->symbols->add_default_precision_qualifier(this->type_name, 730701e04c3fSmrg this->default_precision); 730801e04c3fSmrg } 730901e04c3fSmrg 731001e04c3fSmrg /* FINISHME: Translate precision statements into IR. */ 731101e04c3fSmrg return NULL; 731201e04c3fSmrg } 731301e04c3fSmrg 731401e04c3fSmrg /* _mesa_ast_set_aggregate_type() sets the <structure> field so that 731501e04c3fSmrg * process_record_constructor() can do type-checking on C-style initializer 731601e04c3fSmrg * expressions of structs, but ast_struct_specifier should only be translated 731701e04c3fSmrg * to HIR if it is declaring the type of a structure. 731801e04c3fSmrg * 731901e04c3fSmrg * The ->is_declaration field is false for initializers of variables 732001e04c3fSmrg * declared separately from the struct's type definition. 732101e04c3fSmrg * 732201e04c3fSmrg * struct S { ... }; (is_declaration = true) 732301e04c3fSmrg * struct T { ... } t = { ... }; (is_declaration = true) 732401e04c3fSmrg * S s = { ... }; (is_declaration = false) 732501e04c3fSmrg */ 732601e04c3fSmrg if (this->structure != NULL && this->structure->is_declaration) 732701e04c3fSmrg return this->structure->hir(instructions, state); 732801e04c3fSmrg 732901e04c3fSmrg return NULL; 733001e04c3fSmrg} 733101e04c3fSmrg 733201e04c3fSmrg 733301e04c3fSmrg/** 733401e04c3fSmrg * Process a structure or interface block tree into an array of structure fields 733501e04c3fSmrg * 733601e04c3fSmrg * After parsing, where there are some syntax differnces, structures and 733701e04c3fSmrg * interface blocks are almost identical. They are similar enough that the 733801e04c3fSmrg * AST for each can be processed the same way into a set of 733901e04c3fSmrg * \c glsl_struct_field to describe the members. 734001e04c3fSmrg * 734101e04c3fSmrg * If we're processing an interface block, var_mode should be the type of the 734201e04c3fSmrg * interface block (ir_var_shader_in, ir_var_shader_out, ir_var_uniform or 734301e04c3fSmrg * ir_var_shader_storage). If we're processing a structure, var_mode should be 734401e04c3fSmrg * ir_var_auto. 734501e04c3fSmrg * 734601e04c3fSmrg * \return 734701e04c3fSmrg * The number of fields processed. A pointer to the array structure fields is 734801e04c3fSmrg * stored in \c *fields_ret. 734901e04c3fSmrg */ 735001e04c3fSmrgstatic unsigned 735101e04c3fSmrgast_process_struct_or_iface_block_members(exec_list *instructions, 735201e04c3fSmrg struct _mesa_glsl_parse_state *state, 735301e04c3fSmrg exec_list *declarations, 735401e04c3fSmrg glsl_struct_field **fields_ret, 735501e04c3fSmrg bool is_interface, 735601e04c3fSmrg enum glsl_matrix_layout matrix_layout, 735701e04c3fSmrg bool allow_reserved_names, 735801e04c3fSmrg ir_variable_mode var_mode, 735901e04c3fSmrg ast_type_qualifier *layout, 736001e04c3fSmrg unsigned block_stream, 736101e04c3fSmrg unsigned block_xfb_buffer, 736201e04c3fSmrg unsigned block_xfb_offset, 736301e04c3fSmrg unsigned expl_location, 736401e04c3fSmrg unsigned expl_align) 736501e04c3fSmrg{ 736601e04c3fSmrg unsigned decl_count = 0; 736701e04c3fSmrg unsigned next_offset = 0; 736801e04c3fSmrg 736901e04c3fSmrg /* Make an initial pass over the list of fields to determine how 737001e04c3fSmrg * many there are. Each element in this list is an ast_declarator_list. 737101e04c3fSmrg * This means that we actually need to count the number of elements in the 737201e04c3fSmrg * 'declarations' list in each of the elements. 737301e04c3fSmrg */ 737401e04c3fSmrg foreach_list_typed (ast_declarator_list, decl_list, link, declarations) { 737501e04c3fSmrg decl_count += decl_list->declarations.length(); 737601e04c3fSmrg } 737701e04c3fSmrg 737801e04c3fSmrg /* Allocate storage for the fields and process the field 737901e04c3fSmrg * declarations. As the declarations are processed, try to also convert 738001e04c3fSmrg * the types to HIR. This ensures that structure definitions embedded in 738101e04c3fSmrg * other structure definitions or in interface blocks are processed. 738201e04c3fSmrg */ 738301e04c3fSmrg glsl_struct_field *const fields = rzalloc_array(state, glsl_struct_field, 738401e04c3fSmrg decl_count); 738501e04c3fSmrg 738601e04c3fSmrg bool first_member = true; 738701e04c3fSmrg bool first_member_has_explicit_location = false; 738801e04c3fSmrg 738901e04c3fSmrg unsigned i = 0; 739001e04c3fSmrg foreach_list_typed (ast_declarator_list, decl_list, link, declarations) { 739101e04c3fSmrg const char *type_name; 739201e04c3fSmrg YYLTYPE loc = decl_list->get_location(); 739301e04c3fSmrg 739401e04c3fSmrg decl_list->type->specifier->hir(instructions, state); 739501e04c3fSmrg 739601e04c3fSmrg /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says: 739701e04c3fSmrg * 739801e04c3fSmrg * "Anonymous structures are not supported; so embedded structures 739901e04c3fSmrg * must have a declarator. A name given to an embedded struct is 740001e04c3fSmrg * scoped at the same level as the struct it is embedded in." 740101e04c3fSmrg * 740201e04c3fSmrg * The same section of the GLSL 1.20 spec says: 740301e04c3fSmrg * 740401e04c3fSmrg * "Anonymous structures are not supported. Embedded structures are 740501e04c3fSmrg * not supported." 740601e04c3fSmrg * 740701e04c3fSmrg * The GLSL ES 1.00 and 3.00 specs have similar langauge. So, we allow 740801e04c3fSmrg * embedded structures in 1.10 only. 740901e04c3fSmrg */ 741001e04c3fSmrg if (state->language_version != 110 && 741101e04c3fSmrg decl_list->type->specifier->structure != NULL) 741201e04c3fSmrg _mesa_glsl_error(&loc, state, 741301e04c3fSmrg "embedded structure declarations are not allowed"); 741401e04c3fSmrg 741501e04c3fSmrg const glsl_type *decl_type = 741601e04c3fSmrg decl_list->type->glsl_type(& type_name, state); 741701e04c3fSmrg 741801e04c3fSmrg const struct ast_type_qualifier *const qual = 741901e04c3fSmrg &decl_list->type->qualifier; 742001e04c3fSmrg 742101e04c3fSmrg /* From section 4.3.9 of the GLSL 4.40 spec: 742201e04c3fSmrg * 742301e04c3fSmrg * "[In interface blocks] opaque types are not allowed." 742401e04c3fSmrg * 742501e04c3fSmrg * It should be impossible for decl_type to be NULL here. Cases that 742601e04c3fSmrg * might naturally lead to decl_type being NULL, especially for the 742701e04c3fSmrg * is_interface case, will have resulted in compilation having 742801e04c3fSmrg * already halted due to a syntax error. 742901e04c3fSmrg */ 743001e04c3fSmrg assert(decl_type); 743101e04c3fSmrg 743201e04c3fSmrg if (is_interface) { 743301e04c3fSmrg /* From section 4.3.7 of the ARB_bindless_texture spec: 743401e04c3fSmrg * 743501e04c3fSmrg * "(remove the following bullet from the last list on p. 39, 743601e04c3fSmrg * thereby permitting sampler types in interface blocks; image 743701e04c3fSmrg * types are also permitted in blocks by this extension)" 743801e04c3fSmrg * 743901e04c3fSmrg * * sampler types are not allowed 744001e04c3fSmrg */ 744101e04c3fSmrg if (decl_type->contains_atomic() || 744201e04c3fSmrg (!state->has_bindless() && decl_type->contains_opaque())) { 744301e04c3fSmrg _mesa_glsl_error(&loc, state, "uniform/buffer in non-default " 744401e04c3fSmrg "interface block contains %s variable", 744501e04c3fSmrg state->has_bindless() ? "atomic" : "opaque"); 744601e04c3fSmrg } 744701e04c3fSmrg } else { 744801e04c3fSmrg if (decl_type->contains_atomic()) { 744901e04c3fSmrg /* From section 4.1.7.3 of the GLSL 4.40 spec: 745001e04c3fSmrg * 745101e04c3fSmrg * "Members of structures cannot be declared as atomic counter 745201e04c3fSmrg * types." 745301e04c3fSmrg */ 745401e04c3fSmrg _mesa_glsl_error(&loc, state, "atomic counter in structure"); 745501e04c3fSmrg } 745601e04c3fSmrg 745701e04c3fSmrg if (!state->has_bindless() && decl_type->contains_image()) { 745801e04c3fSmrg /* FINISHME: Same problem as with atomic counters. 745901e04c3fSmrg * FINISHME: Request clarification from Khronos and add 746001e04c3fSmrg * FINISHME: spec quotation here. 746101e04c3fSmrg */ 746201e04c3fSmrg _mesa_glsl_error(&loc, state, "image in structure"); 746301e04c3fSmrg } 746401e04c3fSmrg } 746501e04c3fSmrg 746601e04c3fSmrg if (qual->flags.q.explicit_binding) { 746701e04c3fSmrg _mesa_glsl_error(&loc, state, 746801e04c3fSmrg "binding layout qualifier cannot be applied " 746901e04c3fSmrg "to struct or interface block members"); 747001e04c3fSmrg } 747101e04c3fSmrg 747201e04c3fSmrg if (is_interface) { 747301e04c3fSmrg if (!first_member) { 747401e04c3fSmrg if (!layout->flags.q.explicit_location && 747501e04c3fSmrg ((first_member_has_explicit_location && 747601e04c3fSmrg !qual->flags.q.explicit_location) || 747701e04c3fSmrg (!first_member_has_explicit_location && 747801e04c3fSmrg qual->flags.q.explicit_location))) { 747901e04c3fSmrg _mesa_glsl_error(&loc, state, 748001e04c3fSmrg "when block-level location layout qualifier " 748101e04c3fSmrg "is not supplied either all members must " 748201e04c3fSmrg "have a location layout qualifier or all " 748301e04c3fSmrg "members must not have a location layout " 748401e04c3fSmrg "qualifier"); 748501e04c3fSmrg } 748601e04c3fSmrg } else { 748701e04c3fSmrg first_member = false; 748801e04c3fSmrg first_member_has_explicit_location = 748901e04c3fSmrg qual->flags.q.explicit_location; 749001e04c3fSmrg } 749101e04c3fSmrg } 749201e04c3fSmrg 749301e04c3fSmrg if (qual->flags.q.std140 || 749401e04c3fSmrg qual->flags.q.std430 || 749501e04c3fSmrg qual->flags.q.packed || 749601e04c3fSmrg qual->flags.q.shared) { 749701e04c3fSmrg _mesa_glsl_error(&loc, state, 749801e04c3fSmrg "uniform/shader storage block layout qualifiers " 749901e04c3fSmrg "std140, std430, packed, and shared can only be " 750001e04c3fSmrg "applied to uniform/shader storage blocks, not " 750101e04c3fSmrg "members"); 750201e04c3fSmrg } 750301e04c3fSmrg 750401e04c3fSmrg if (qual->flags.q.constant) { 750501e04c3fSmrg _mesa_glsl_error(&loc, state, 750601e04c3fSmrg "const storage qualifier cannot be applied " 750701e04c3fSmrg "to struct or interface block members"); 750801e04c3fSmrg } 750901e04c3fSmrg 751001e04c3fSmrg validate_memory_qualifier_for_type(state, &loc, qual, decl_type); 751101e04c3fSmrg validate_image_format_qualifier_for_type(state, &loc, qual, decl_type); 751201e04c3fSmrg 751301e04c3fSmrg /* From Section 4.4.2.3 (Geometry Outputs) of the GLSL 4.50 spec: 751401e04c3fSmrg * 751501e04c3fSmrg * "A block member may be declared with a stream identifier, but 751601e04c3fSmrg * the specified stream must match the stream associated with the 751701e04c3fSmrg * containing block." 751801e04c3fSmrg */ 751901e04c3fSmrg if (qual->flags.q.explicit_stream) { 752001e04c3fSmrg unsigned qual_stream; 752101e04c3fSmrg if (process_qualifier_constant(state, &loc, "stream", 752201e04c3fSmrg qual->stream, &qual_stream) && 752301e04c3fSmrg qual_stream != block_stream) { 752401e04c3fSmrg _mesa_glsl_error(&loc, state, "stream layout qualifier on " 752501e04c3fSmrg "interface block member does not match " 752601e04c3fSmrg "the interface block (%u vs %u)", qual_stream, 752701e04c3fSmrg block_stream); 752801e04c3fSmrg } 752901e04c3fSmrg } 753001e04c3fSmrg 753101e04c3fSmrg int xfb_buffer; 753201e04c3fSmrg unsigned explicit_xfb_buffer = 0; 753301e04c3fSmrg if (qual->flags.q.explicit_xfb_buffer) { 753401e04c3fSmrg unsigned qual_xfb_buffer; 753501e04c3fSmrg if (process_qualifier_constant(state, &loc, "xfb_buffer", 753601e04c3fSmrg qual->xfb_buffer, &qual_xfb_buffer)) { 753701e04c3fSmrg explicit_xfb_buffer = 1; 753801e04c3fSmrg if (qual_xfb_buffer != block_xfb_buffer) 753901e04c3fSmrg _mesa_glsl_error(&loc, state, "xfb_buffer layout qualifier on " 754001e04c3fSmrg "interface block member does not match " 754101e04c3fSmrg "the interface block (%u vs %u)", 754201e04c3fSmrg qual_xfb_buffer, block_xfb_buffer); 754301e04c3fSmrg } 754401e04c3fSmrg xfb_buffer = (int) qual_xfb_buffer; 754501e04c3fSmrg } else { 754601e04c3fSmrg if (layout) 754701e04c3fSmrg explicit_xfb_buffer = layout->flags.q.explicit_xfb_buffer; 754801e04c3fSmrg xfb_buffer = (int) block_xfb_buffer; 754901e04c3fSmrg } 755001e04c3fSmrg 755101e04c3fSmrg int xfb_stride = -1; 755201e04c3fSmrg if (qual->flags.q.explicit_xfb_stride) { 755301e04c3fSmrg unsigned qual_xfb_stride; 755401e04c3fSmrg if (process_qualifier_constant(state, &loc, "xfb_stride", 755501e04c3fSmrg qual->xfb_stride, &qual_xfb_stride)) { 755601e04c3fSmrg xfb_stride = (int) qual_xfb_stride; 755701e04c3fSmrg } 755801e04c3fSmrg } 755901e04c3fSmrg 756001e04c3fSmrg if (qual->flags.q.uniform && qual->has_interpolation()) { 756101e04c3fSmrg _mesa_glsl_error(&loc, state, 756201e04c3fSmrg "interpolation qualifiers cannot be used " 756301e04c3fSmrg "with uniform interface blocks"); 756401e04c3fSmrg } 756501e04c3fSmrg 756601e04c3fSmrg if ((qual->flags.q.uniform || !is_interface) && 756701e04c3fSmrg qual->has_auxiliary_storage()) { 756801e04c3fSmrg _mesa_glsl_error(&loc, state, 756901e04c3fSmrg "auxiliary storage qualifiers cannot be used " 757001e04c3fSmrg "in uniform blocks or structures."); 757101e04c3fSmrg } 757201e04c3fSmrg 757301e04c3fSmrg if (qual->flags.q.row_major || qual->flags.q.column_major) { 757401e04c3fSmrg if (!qual->flags.q.uniform && !qual->flags.q.buffer) { 757501e04c3fSmrg _mesa_glsl_error(&loc, state, 757601e04c3fSmrg "row_major and column_major can only be " 757701e04c3fSmrg "applied to interface blocks"); 757801e04c3fSmrg } else 757901e04c3fSmrg validate_matrix_layout_for_type(state, &loc, decl_type, NULL); 758001e04c3fSmrg } 758101e04c3fSmrg 758201e04c3fSmrg foreach_list_typed (ast_declaration, decl, link, 758301e04c3fSmrg &decl_list->declarations) { 758401e04c3fSmrg YYLTYPE loc = decl->get_location(); 758501e04c3fSmrg 758601e04c3fSmrg if (!allow_reserved_names) 758701e04c3fSmrg validate_identifier(decl->identifier, loc, state); 758801e04c3fSmrg 758901e04c3fSmrg const struct glsl_type *field_type = 759001e04c3fSmrg process_array_type(&loc, decl_type, decl->array_specifier, state); 759101e04c3fSmrg validate_array_dimensions(field_type, state, &loc); 759201e04c3fSmrg fields[i].type = field_type; 759301e04c3fSmrg fields[i].name = decl->identifier; 759401e04c3fSmrg fields[i].interpolation = 759501e04c3fSmrg interpret_interpolation_qualifier(qual, field_type, 759601e04c3fSmrg var_mode, state, &loc); 759701e04c3fSmrg fields[i].centroid = qual->flags.q.centroid ? 1 : 0; 759801e04c3fSmrg fields[i].sample = qual->flags.q.sample ? 1 : 0; 759901e04c3fSmrg fields[i].patch = qual->flags.q.patch ? 1 : 0; 760001e04c3fSmrg fields[i].offset = -1; 760101e04c3fSmrg fields[i].explicit_xfb_buffer = explicit_xfb_buffer; 760201e04c3fSmrg fields[i].xfb_buffer = xfb_buffer; 760301e04c3fSmrg fields[i].xfb_stride = xfb_stride; 760401e04c3fSmrg 760501e04c3fSmrg if (qual->flags.q.explicit_location) { 760601e04c3fSmrg unsigned qual_location; 760701e04c3fSmrg if (process_qualifier_constant(state, &loc, "location", 760801e04c3fSmrg qual->location, &qual_location)) { 760901e04c3fSmrg fields[i].location = qual_location + 761001e04c3fSmrg (fields[i].patch ? VARYING_SLOT_PATCH0 : VARYING_SLOT_VAR0); 761101e04c3fSmrg expl_location = fields[i].location + 761201e04c3fSmrg fields[i].type->count_attribute_slots(false); 761301e04c3fSmrg } 761401e04c3fSmrg } else { 761501e04c3fSmrg if (layout && layout->flags.q.explicit_location) { 761601e04c3fSmrg fields[i].location = expl_location; 761701e04c3fSmrg expl_location += fields[i].type->count_attribute_slots(false); 761801e04c3fSmrg } else { 761901e04c3fSmrg fields[i].location = -1; 762001e04c3fSmrg } 762101e04c3fSmrg } 762201e04c3fSmrg 76237ec681f3Smrg if (qual->flags.q.explicit_component) { 76247ec681f3Smrg unsigned qual_component; 76257ec681f3Smrg if (process_qualifier_constant(state, &loc, "component", 76267ec681f3Smrg qual->component, &qual_component)) { 76277ec681f3Smrg validate_component_layout_for_type(state, &loc, fields[i].type, 76287ec681f3Smrg qual_component); 76297ec681f3Smrg fields[i].component = qual_component; 76307ec681f3Smrg } 76317ec681f3Smrg } else { 76327ec681f3Smrg fields[i].component = -1; 76337ec681f3Smrg } 76347ec681f3Smrg 763501e04c3fSmrg /* Offset can only be used with std430 and std140 layouts an initial 763601e04c3fSmrg * value of 0 is used for error detection. 763701e04c3fSmrg */ 763801e04c3fSmrg unsigned align = 0; 763901e04c3fSmrg unsigned size = 0; 764001e04c3fSmrg if (layout) { 764101e04c3fSmrg bool row_major; 764201e04c3fSmrg if (qual->flags.q.row_major || 764301e04c3fSmrg matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) { 764401e04c3fSmrg row_major = true; 764501e04c3fSmrg } else { 764601e04c3fSmrg row_major = false; 764701e04c3fSmrg } 764801e04c3fSmrg 764901e04c3fSmrg if(layout->flags.q.std140) { 765001e04c3fSmrg align = field_type->std140_base_alignment(row_major); 765101e04c3fSmrg size = field_type->std140_size(row_major); 765201e04c3fSmrg } else if (layout->flags.q.std430) { 765301e04c3fSmrg align = field_type->std430_base_alignment(row_major); 765401e04c3fSmrg size = field_type->std430_size(row_major); 765501e04c3fSmrg } 765601e04c3fSmrg } 765701e04c3fSmrg 765801e04c3fSmrg if (qual->flags.q.explicit_offset) { 765901e04c3fSmrg unsigned qual_offset; 766001e04c3fSmrg if (process_qualifier_constant(state, &loc, "offset", 766101e04c3fSmrg qual->offset, &qual_offset)) { 766201e04c3fSmrg if (align != 0 && size != 0) { 766301e04c3fSmrg if (next_offset > qual_offset) 766401e04c3fSmrg _mesa_glsl_error(&loc, state, "layout qualifier " 766501e04c3fSmrg "offset overlaps previous member"); 766601e04c3fSmrg 766701e04c3fSmrg if (qual_offset % align) { 766801e04c3fSmrg _mesa_glsl_error(&loc, state, "layout qualifier offset " 766901e04c3fSmrg "must be a multiple of the base " 767001e04c3fSmrg "alignment of %s", field_type->name); 767101e04c3fSmrg } 767201e04c3fSmrg fields[i].offset = qual_offset; 7673ed98bd31Smaya next_offset = qual_offset + size; 767401e04c3fSmrg } else { 767501e04c3fSmrg _mesa_glsl_error(&loc, state, "offset can only be used " 767601e04c3fSmrg "with std430 and std140 layouts"); 767701e04c3fSmrg } 767801e04c3fSmrg } 767901e04c3fSmrg } 768001e04c3fSmrg 768101e04c3fSmrg if (qual->flags.q.explicit_align || expl_align != 0) { 768201e04c3fSmrg unsigned offset = fields[i].offset != -1 ? fields[i].offset : 768301e04c3fSmrg next_offset; 768401e04c3fSmrg if (align == 0 || size == 0) { 768501e04c3fSmrg _mesa_glsl_error(&loc, state, "align can only be used with " 768601e04c3fSmrg "std430 and std140 layouts"); 768701e04c3fSmrg } else if (qual->flags.q.explicit_align) { 768801e04c3fSmrg unsigned member_align; 768901e04c3fSmrg if (process_qualifier_constant(state, &loc, "align", 769001e04c3fSmrg qual->align, &member_align)) { 769101e04c3fSmrg if (member_align == 0 || 769201e04c3fSmrg member_align & (member_align - 1)) { 769301e04c3fSmrg _mesa_glsl_error(&loc, state, "align layout qualifier " 769401e04c3fSmrg "is not a power of 2"); 769501e04c3fSmrg } else { 769601e04c3fSmrg fields[i].offset = glsl_align(offset, member_align); 7697ed98bd31Smaya next_offset = fields[i].offset + size; 769801e04c3fSmrg } 769901e04c3fSmrg } 770001e04c3fSmrg } else { 770101e04c3fSmrg fields[i].offset = glsl_align(offset, expl_align); 7702ed98bd31Smaya next_offset = fields[i].offset + size; 770301e04c3fSmrg } 770401e04c3fSmrg } else if (!qual->flags.q.explicit_offset) { 770501e04c3fSmrg if (align != 0 && size != 0) 7706ed98bd31Smaya next_offset = glsl_align(next_offset, align) + size; 770701e04c3fSmrg } 770801e04c3fSmrg 770901e04c3fSmrg /* From the ARB_enhanced_layouts spec: 771001e04c3fSmrg * 771101e04c3fSmrg * "The given offset applies to the first component of the first 771201e04c3fSmrg * member of the qualified entity. Then, within the qualified 771301e04c3fSmrg * entity, subsequent components are each assigned, in order, to 771401e04c3fSmrg * the next available offset aligned to a multiple of that 771501e04c3fSmrg * component's size. Aggregate types are flattened down to the 771601e04c3fSmrg * component level to get this sequence of components." 771701e04c3fSmrg */ 771801e04c3fSmrg if (qual->flags.q.explicit_xfb_offset) { 771901e04c3fSmrg unsigned xfb_offset; 772001e04c3fSmrg if (process_qualifier_constant(state, &loc, "xfb_offset", 772101e04c3fSmrg qual->offset, &xfb_offset)) { 772201e04c3fSmrg fields[i].offset = xfb_offset; 772301e04c3fSmrg block_xfb_offset = fields[i].offset + 772401e04c3fSmrg 4 * field_type->component_slots(); 772501e04c3fSmrg } 772601e04c3fSmrg } else { 772701e04c3fSmrg if (layout && layout->flags.q.explicit_xfb_offset) { 772801e04c3fSmrg unsigned align = field_type->is_64bit() ? 8 : 4; 772901e04c3fSmrg fields[i].offset = glsl_align(block_xfb_offset, align); 773001e04c3fSmrg block_xfb_offset += 4 * field_type->component_slots(); 773101e04c3fSmrg } 773201e04c3fSmrg } 773301e04c3fSmrg 773401e04c3fSmrg /* Propogate row- / column-major information down the fields of the 773501e04c3fSmrg * structure or interface block. Structures need this data because 773601e04c3fSmrg * the structure may contain a structure that contains ... a matrix 773701e04c3fSmrg * that need the proper layout. 773801e04c3fSmrg */ 773901e04c3fSmrg if (is_interface && layout && 774001e04c3fSmrg (layout->flags.q.uniform || layout->flags.q.buffer) && 774101e04c3fSmrg (field_type->without_array()->is_matrix() 7742ed98bd31Smaya || field_type->without_array()->is_struct())) { 774301e04c3fSmrg /* If no layout is specified for the field, inherit the layout 774401e04c3fSmrg * from the block. 774501e04c3fSmrg */ 774601e04c3fSmrg fields[i].matrix_layout = matrix_layout; 774701e04c3fSmrg 774801e04c3fSmrg if (qual->flags.q.row_major) 774901e04c3fSmrg fields[i].matrix_layout = GLSL_MATRIX_LAYOUT_ROW_MAJOR; 775001e04c3fSmrg else if (qual->flags.q.column_major) 775101e04c3fSmrg fields[i].matrix_layout = GLSL_MATRIX_LAYOUT_COLUMN_MAJOR; 775201e04c3fSmrg 775301e04c3fSmrg /* If we're processing an uniform or buffer block, the matrix 775401e04c3fSmrg * layout must be decided by this point. 775501e04c3fSmrg */ 775601e04c3fSmrg assert(fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR 775701e04c3fSmrg || fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR); 775801e04c3fSmrg } 775901e04c3fSmrg 776001e04c3fSmrg /* Memory qualifiers are allowed on buffer and image variables, while 776101e04c3fSmrg * the format qualifier is only accepted for images. 776201e04c3fSmrg */ 776301e04c3fSmrg if (var_mode == ir_var_shader_storage || 776401e04c3fSmrg field_type->without_array()->is_image()) { 776501e04c3fSmrg /* For readonly and writeonly qualifiers the field definition, 776601e04c3fSmrg * if set, overwrites the layout qualifier. 776701e04c3fSmrg */ 776801e04c3fSmrg if (qual->flags.q.read_only || qual->flags.q.write_only) { 776901e04c3fSmrg fields[i].memory_read_only = qual->flags.q.read_only; 777001e04c3fSmrg fields[i].memory_write_only = qual->flags.q.write_only; 777101e04c3fSmrg } else { 777201e04c3fSmrg fields[i].memory_read_only = 777301e04c3fSmrg layout ? layout->flags.q.read_only : 0; 777401e04c3fSmrg fields[i].memory_write_only = 777501e04c3fSmrg layout ? layout->flags.q.write_only : 0; 777601e04c3fSmrg } 777701e04c3fSmrg 777801e04c3fSmrg /* For other qualifiers, we set the flag if either the layout 777901e04c3fSmrg * qualifier or the field qualifier are set 778001e04c3fSmrg */ 778101e04c3fSmrg fields[i].memory_coherent = qual->flags.q.coherent || 778201e04c3fSmrg (layout && layout->flags.q.coherent); 778301e04c3fSmrg fields[i].memory_volatile = qual->flags.q._volatile || 778401e04c3fSmrg (layout && layout->flags.q._volatile); 778501e04c3fSmrg fields[i].memory_restrict = qual->flags.q.restrict_flag || 778601e04c3fSmrg (layout && layout->flags.q.restrict_flag); 778701e04c3fSmrg 778801e04c3fSmrg if (field_type->without_array()->is_image()) { 778901e04c3fSmrg if (qual->flags.q.explicit_image_format) { 779001e04c3fSmrg if (qual->image_base_type != 779101e04c3fSmrg field_type->without_array()->sampled_type) { 779201e04c3fSmrg _mesa_glsl_error(&loc, state, "format qualifier doesn't " 779301e04c3fSmrg "match the base data type of the image"); 779401e04c3fSmrg } 779501e04c3fSmrg 779601e04c3fSmrg fields[i].image_format = qual->image_format; 779701e04c3fSmrg } else { 779801e04c3fSmrg if (!qual->flags.q.write_only) { 779901e04c3fSmrg _mesa_glsl_error(&loc, state, "image not qualified with " 780001e04c3fSmrg "`writeonly' must have a format layout " 780101e04c3fSmrg "qualifier"); 780201e04c3fSmrg } 780301e04c3fSmrg 78047ec681f3Smrg fields[i].image_format = PIPE_FORMAT_NONE; 780501e04c3fSmrg } 780601e04c3fSmrg } 780701e04c3fSmrg } 780801e04c3fSmrg 78097ec681f3Smrg /* Precision qualifiers do not hold any meaning in Desktop GLSL */ 78107ec681f3Smrg if (state->es_shader) { 78117ec681f3Smrg fields[i].precision = select_gles_precision(qual->precision, 78127ec681f3Smrg field_type, 78137ec681f3Smrg state, 78147ec681f3Smrg &loc); 78157ec681f3Smrg } else { 78167ec681f3Smrg fields[i].precision = qual->precision; 78177ec681f3Smrg } 78187ec681f3Smrg 781901e04c3fSmrg i++; 782001e04c3fSmrg } 782101e04c3fSmrg } 782201e04c3fSmrg 782301e04c3fSmrg assert(i == decl_count); 782401e04c3fSmrg 782501e04c3fSmrg *fields_ret = fields; 782601e04c3fSmrg return decl_count; 782701e04c3fSmrg} 782801e04c3fSmrg 782901e04c3fSmrg 783001e04c3fSmrgir_rvalue * 783101e04c3fSmrgast_struct_specifier::hir(exec_list *instructions, 783201e04c3fSmrg struct _mesa_glsl_parse_state *state) 783301e04c3fSmrg{ 783401e04c3fSmrg YYLTYPE loc = this->get_location(); 783501e04c3fSmrg 783601e04c3fSmrg unsigned expl_location = 0; 783701e04c3fSmrg if (layout && layout->flags.q.explicit_location) { 783801e04c3fSmrg if (!process_qualifier_constant(state, &loc, "location", 783901e04c3fSmrg layout->location, &expl_location)) { 784001e04c3fSmrg return NULL; 784101e04c3fSmrg } else { 784201e04c3fSmrg expl_location = VARYING_SLOT_VAR0 + expl_location; 784301e04c3fSmrg } 784401e04c3fSmrg } 784501e04c3fSmrg 784601e04c3fSmrg glsl_struct_field *fields; 784701e04c3fSmrg unsigned decl_count = 784801e04c3fSmrg ast_process_struct_or_iface_block_members(instructions, 784901e04c3fSmrg state, 785001e04c3fSmrg &this->declarations, 785101e04c3fSmrg &fields, 785201e04c3fSmrg false, 785301e04c3fSmrg GLSL_MATRIX_LAYOUT_INHERITED, 785401e04c3fSmrg false /* allow_reserved_names */, 785501e04c3fSmrg ir_var_auto, 785601e04c3fSmrg layout, 785701e04c3fSmrg 0, /* for interface only */ 785801e04c3fSmrg 0, /* for interface only */ 785901e04c3fSmrg 0, /* for interface only */ 786001e04c3fSmrg expl_location, 786101e04c3fSmrg 0 /* for interface only */); 786201e04c3fSmrg 786301e04c3fSmrg validate_identifier(this->name, loc, state); 786401e04c3fSmrg 7865ed98bd31Smaya type = glsl_type::get_struct_instance(fields, decl_count, this->name); 786601e04c3fSmrg 786701e04c3fSmrg if (!type->is_anonymous() && !state->symbols->add_type(name, type)) { 786801e04c3fSmrg const glsl_type *match = state->symbols->get_type(name); 786901e04c3fSmrg /* allow struct matching for desktop GL - older UE4 does this */ 7870ed98bd31Smaya if (match != NULL && state->is_version(130, 0) && match->record_compare(type, true, false)) 787101e04c3fSmrg _mesa_glsl_warning(& loc, state, "struct `%s' previously defined", name); 787201e04c3fSmrg else 787301e04c3fSmrg _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name); 787401e04c3fSmrg } else { 787501e04c3fSmrg const glsl_type **s = reralloc(state, state->user_structures, 787601e04c3fSmrg const glsl_type *, 787701e04c3fSmrg state->num_user_structures + 1); 787801e04c3fSmrg if (s != NULL) { 787901e04c3fSmrg s[state->num_user_structures] = type; 788001e04c3fSmrg state->user_structures = s; 788101e04c3fSmrg state->num_user_structures++; 788201e04c3fSmrg } 788301e04c3fSmrg } 788401e04c3fSmrg 788501e04c3fSmrg /* Structure type definitions do not have r-values. 788601e04c3fSmrg */ 788701e04c3fSmrg return NULL; 788801e04c3fSmrg} 788901e04c3fSmrg 789001e04c3fSmrg 789101e04c3fSmrg/** 789201e04c3fSmrg * Visitor class which detects whether a given interface block has been used. 789301e04c3fSmrg */ 789401e04c3fSmrgclass interface_block_usage_visitor : public ir_hierarchical_visitor 789501e04c3fSmrg{ 789601e04c3fSmrgpublic: 789701e04c3fSmrg interface_block_usage_visitor(ir_variable_mode mode, const glsl_type *block) 789801e04c3fSmrg : mode(mode), block(block), found(false) 789901e04c3fSmrg { 790001e04c3fSmrg } 790101e04c3fSmrg 790201e04c3fSmrg virtual ir_visitor_status visit(ir_dereference_variable *ir) 790301e04c3fSmrg { 790401e04c3fSmrg if (ir->var->data.mode == mode && ir->var->get_interface_type() == block) { 790501e04c3fSmrg found = true; 790601e04c3fSmrg return visit_stop; 790701e04c3fSmrg } 790801e04c3fSmrg return visit_continue; 790901e04c3fSmrg } 791001e04c3fSmrg 791101e04c3fSmrg bool usage_found() const 791201e04c3fSmrg { 791301e04c3fSmrg return this->found; 791401e04c3fSmrg } 791501e04c3fSmrg 791601e04c3fSmrgprivate: 791701e04c3fSmrg ir_variable_mode mode; 791801e04c3fSmrg const glsl_type *block; 791901e04c3fSmrg bool found; 792001e04c3fSmrg}; 792101e04c3fSmrg 792201e04c3fSmrgstatic bool 792301e04c3fSmrgis_unsized_array_last_element(ir_variable *v) 792401e04c3fSmrg{ 792501e04c3fSmrg const glsl_type *interface_type = v->get_interface_type(); 792601e04c3fSmrg int length = interface_type->length; 792701e04c3fSmrg 792801e04c3fSmrg assert(v->type->is_unsized_array()); 792901e04c3fSmrg 793001e04c3fSmrg /* Check if it is the last element of the interface */ 793101e04c3fSmrg if (strcmp(interface_type->fields.structure[length-1].name, v->name) == 0) 793201e04c3fSmrg return true; 793301e04c3fSmrg return false; 793401e04c3fSmrg} 793501e04c3fSmrg 793601e04c3fSmrgstatic void 793701e04c3fSmrgapply_memory_qualifiers(ir_variable *var, glsl_struct_field field) 793801e04c3fSmrg{ 793901e04c3fSmrg var->data.memory_read_only = field.memory_read_only; 794001e04c3fSmrg var->data.memory_write_only = field.memory_write_only; 794101e04c3fSmrg var->data.memory_coherent = field.memory_coherent; 794201e04c3fSmrg var->data.memory_volatile = field.memory_volatile; 794301e04c3fSmrg var->data.memory_restrict = field.memory_restrict; 794401e04c3fSmrg} 794501e04c3fSmrg 794601e04c3fSmrgir_rvalue * 794701e04c3fSmrgast_interface_block::hir(exec_list *instructions, 794801e04c3fSmrg struct _mesa_glsl_parse_state *state) 794901e04c3fSmrg{ 795001e04c3fSmrg YYLTYPE loc = this->get_location(); 795101e04c3fSmrg 795201e04c3fSmrg /* Interface blocks must be declared at global scope */ 795301e04c3fSmrg if (state->current_function != NULL) { 795401e04c3fSmrg _mesa_glsl_error(&loc, state, 795501e04c3fSmrg "Interface block `%s' must be declared " 795601e04c3fSmrg "at global scope", 795701e04c3fSmrg this->block_name); 795801e04c3fSmrg } 795901e04c3fSmrg 796001e04c3fSmrg /* Validate qualifiers: 796101e04c3fSmrg * 796201e04c3fSmrg * - Layout Qualifiers as per the table in Section 4.4 796301e04c3fSmrg * ("Layout Qualifiers") of the GLSL 4.50 spec. 796401e04c3fSmrg * 796501e04c3fSmrg * - Memory Qualifiers as per Section 4.10 ("Memory Qualifiers") of the 796601e04c3fSmrg * GLSL 4.50 spec: 796701e04c3fSmrg * 796801e04c3fSmrg * "Additionally, memory qualifiers may also be used in the declaration 796901e04c3fSmrg * of shader storage blocks" 797001e04c3fSmrg * 797101e04c3fSmrg * Note the table in Section 4.4 says std430 is allowed on both uniform and 797201e04c3fSmrg * buffer blocks however Section 4.4.5 (Uniform and Shader Storage Block 797301e04c3fSmrg * Layout Qualifiers) of the GLSL 4.50 spec says: 797401e04c3fSmrg * 797501e04c3fSmrg * "The std430 qualifier is supported only for shader storage blocks; 797601e04c3fSmrg * using std430 on a uniform block will result in a compile-time error." 797701e04c3fSmrg */ 797801e04c3fSmrg ast_type_qualifier allowed_blk_qualifiers; 797901e04c3fSmrg allowed_blk_qualifiers.flags.i = 0; 798001e04c3fSmrg if (this->layout.flags.q.buffer || this->layout.flags.q.uniform) { 798101e04c3fSmrg allowed_blk_qualifiers.flags.q.shared = 1; 798201e04c3fSmrg allowed_blk_qualifiers.flags.q.packed = 1; 798301e04c3fSmrg allowed_blk_qualifiers.flags.q.std140 = 1; 798401e04c3fSmrg allowed_blk_qualifiers.flags.q.row_major = 1; 798501e04c3fSmrg allowed_blk_qualifiers.flags.q.column_major = 1; 798601e04c3fSmrg allowed_blk_qualifiers.flags.q.explicit_align = 1; 798701e04c3fSmrg allowed_blk_qualifiers.flags.q.explicit_binding = 1; 798801e04c3fSmrg if (this->layout.flags.q.buffer) { 798901e04c3fSmrg allowed_blk_qualifiers.flags.q.buffer = 1; 799001e04c3fSmrg allowed_blk_qualifiers.flags.q.std430 = 1; 799101e04c3fSmrg allowed_blk_qualifiers.flags.q.coherent = 1; 799201e04c3fSmrg allowed_blk_qualifiers.flags.q._volatile = 1; 799301e04c3fSmrg allowed_blk_qualifiers.flags.q.restrict_flag = 1; 799401e04c3fSmrg allowed_blk_qualifiers.flags.q.read_only = 1; 799501e04c3fSmrg allowed_blk_qualifiers.flags.q.write_only = 1; 799601e04c3fSmrg } else { 799701e04c3fSmrg allowed_blk_qualifiers.flags.q.uniform = 1; 799801e04c3fSmrg } 799901e04c3fSmrg } else { 800001e04c3fSmrg /* Interface block */ 800101e04c3fSmrg assert(this->layout.flags.q.in || this->layout.flags.q.out); 800201e04c3fSmrg 800301e04c3fSmrg allowed_blk_qualifiers.flags.q.explicit_location = 1; 800401e04c3fSmrg if (this->layout.flags.q.out) { 800501e04c3fSmrg allowed_blk_qualifiers.flags.q.out = 1; 800601e04c3fSmrg if (state->stage == MESA_SHADER_GEOMETRY || 800701e04c3fSmrg state->stage == MESA_SHADER_TESS_CTRL || 800801e04c3fSmrg state->stage == MESA_SHADER_TESS_EVAL || 800901e04c3fSmrg state->stage == MESA_SHADER_VERTEX ) { 801001e04c3fSmrg allowed_blk_qualifiers.flags.q.explicit_xfb_offset = 1; 801101e04c3fSmrg allowed_blk_qualifiers.flags.q.explicit_xfb_buffer = 1; 801201e04c3fSmrg allowed_blk_qualifiers.flags.q.xfb_buffer = 1; 801301e04c3fSmrg allowed_blk_qualifiers.flags.q.explicit_xfb_stride = 1; 801401e04c3fSmrg allowed_blk_qualifiers.flags.q.xfb_stride = 1; 801501e04c3fSmrg if (state->stage == MESA_SHADER_GEOMETRY) { 801601e04c3fSmrg allowed_blk_qualifiers.flags.q.stream = 1; 801701e04c3fSmrg allowed_blk_qualifiers.flags.q.explicit_stream = 1; 801801e04c3fSmrg } 801901e04c3fSmrg if (state->stage == MESA_SHADER_TESS_CTRL) { 802001e04c3fSmrg allowed_blk_qualifiers.flags.q.patch = 1; 802101e04c3fSmrg } 802201e04c3fSmrg } 802301e04c3fSmrg } else { 802401e04c3fSmrg allowed_blk_qualifiers.flags.q.in = 1; 802501e04c3fSmrg if (state->stage == MESA_SHADER_TESS_EVAL) { 802601e04c3fSmrg allowed_blk_qualifiers.flags.q.patch = 1; 802701e04c3fSmrg } 802801e04c3fSmrg } 802901e04c3fSmrg } 803001e04c3fSmrg 803101e04c3fSmrg this->layout.validate_flags(&loc, state, allowed_blk_qualifiers, 803201e04c3fSmrg "invalid qualifier for block", 803301e04c3fSmrg this->block_name); 803401e04c3fSmrg 803501e04c3fSmrg enum glsl_interface_packing packing; 803601e04c3fSmrg if (this->layout.flags.q.std140) { 803701e04c3fSmrg packing = GLSL_INTERFACE_PACKING_STD140; 803801e04c3fSmrg } else if (this->layout.flags.q.packed) { 803901e04c3fSmrg packing = GLSL_INTERFACE_PACKING_PACKED; 804001e04c3fSmrg } else if (this->layout.flags.q.std430) { 804101e04c3fSmrg packing = GLSL_INTERFACE_PACKING_STD430; 804201e04c3fSmrg } else { 804301e04c3fSmrg /* The default layout is shared. 804401e04c3fSmrg */ 804501e04c3fSmrg packing = GLSL_INTERFACE_PACKING_SHARED; 804601e04c3fSmrg } 804701e04c3fSmrg 804801e04c3fSmrg ir_variable_mode var_mode; 804901e04c3fSmrg const char *iface_type_name; 805001e04c3fSmrg if (this->layout.flags.q.in) { 805101e04c3fSmrg var_mode = ir_var_shader_in; 805201e04c3fSmrg iface_type_name = "in"; 805301e04c3fSmrg } else if (this->layout.flags.q.out) { 805401e04c3fSmrg var_mode = ir_var_shader_out; 805501e04c3fSmrg iface_type_name = "out"; 805601e04c3fSmrg } else if (this->layout.flags.q.uniform) { 805701e04c3fSmrg var_mode = ir_var_uniform; 805801e04c3fSmrg iface_type_name = "uniform"; 805901e04c3fSmrg } else if (this->layout.flags.q.buffer) { 806001e04c3fSmrg var_mode = ir_var_shader_storage; 806101e04c3fSmrg iface_type_name = "buffer"; 806201e04c3fSmrg } else { 806301e04c3fSmrg var_mode = ir_var_auto; 806401e04c3fSmrg iface_type_name = "UNKNOWN"; 806501e04c3fSmrg assert(!"interface block layout qualifier not found!"); 806601e04c3fSmrg } 806701e04c3fSmrg 806801e04c3fSmrg enum glsl_matrix_layout matrix_layout = GLSL_MATRIX_LAYOUT_INHERITED; 806901e04c3fSmrg if (this->layout.flags.q.row_major) 807001e04c3fSmrg matrix_layout = GLSL_MATRIX_LAYOUT_ROW_MAJOR; 807101e04c3fSmrg else if (this->layout.flags.q.column_major) 807201e04c3fSmrg matrix_layout = GLSL_MATRIX_LAYOUT_COLUMN_MAJOR; 807301e04c3fSmrg 807401e04c3fSmrg bool redeclaring_per_vertex = strcmp(this->block_name, "gl_PerVertex") == 0; 807501e04c3fSmrg exec_list declared_variables; 807601e04c3fSmrg glsl_struct_field *fields; 807701e04c3fSmrg 807801e04c3fSmrg /* For blocks that accept memory qualifiers (i.e. shader storage), verify 807901e04c3fSmrg * that we don't have incompatible qualifiers 808001e04c3fSmrg */ 808101e04c3fSmrg if (this->layout.flags.q.read_only && this->layout.flags.q.write_only) { 808201e04c3fSmrg _mesa_glsl_error(&loc, state, 808301e04c3fSmrg "Interface block sets both readonly and writeonly"); 808401e04c3fSmrg } 808501e04c3fSmrg 808601e04c3fSmrg unsigned qual_stream; 808701e04c3fSmrg if (!process_qualifier_constant(state, &loc, "stream", this->layout.stream, 808801e04c3fSmrg &qual_stream) || 808901e04c3fSmrg !validate_stream_qualifier(&loc, state, qual_stream)) { 809001e04c3fSmrg /* If the stream qualifier is invalid it doesn't make sense to continue 809101e04c3fSmrg * on and try to compare stream layouts on member variables against it 809201e04c3fSmrg * so just return early. 809301e04c3fSmrg */ 809401e04c3fSmrg return NULL; 809501e04c3fSmrg } 809601e04c3fSmrg 809701e04c3fSmrg unsigned qual_xfb_buffer; 809801e04c3fSmrg if (!process_qualifier_constant(state, &loc, "xfb_buffer", 809901e04c3fSmrg layout.xfb_buffer, &qual_xfb_buffer) || 810001e04c3fSmrg !validate_xfb_buffer_qualifier(&loc, state, qual_xfb_buffer)) { 810101e04c3fSmrg return NULL; 810201e04c3fSmrg } 810301e04c3fSmrg 81047ec681f3Smrg unsigned qual_xfb_offset = 0; 810501e04c3fSmrg if (layout.flags.q.explicit_xfb_offset) { 810601e04c3fSmrg if (!process_qualifier_constant(state, &loc, "xfb_offset", 810701e04c3fSmrg layout.offset, &qual_xfb_offset)) { 810801e04c3fSmrg return NULL; 810901e04c3fSmrg } 811001e04c3fSmrg } 811101e04c3fSmrg 81127ec681f3Smrg unsigned qual_xfb_stride = 0; 811301e04c3fSmrg if (layout.flags.q.explicit_xfb_stride) { 811401e04c3fSmrg if (!process_qualifier_constant(state, &loc, "xfb_stride", 811501e04c3fSmrg layout.xfb_stride, &qual_xfb_stride)) { 811601e04c3fSmrg return NULL; 811701e04c3fSmrg } 811801e04c3fSmrg } 811901e04c3fSmrg 812001e04c3fSmrg unsigned expl_location = 0; 812101e04c3fSmrg if (layout.flags.q.explicit_location) { 812201e04c3fSmrg if (!process_qualifier_constant(state, &loc, "location", 812301e04c3fSmrg layout.location, &expl_location)) { 812401e04c3fSmrg return NULL; 812501e04c3fSmrg } else { 812601e04c3fSmrg expl_location += this->layout.flags.q.patch ? VARYING_SLOT_PATCH0 812701e04c3fSmrg : VARYING_SLOT_VAR0; 812801e04c3fSmrg } 812901e04c3fSmrg } 813001e04c3fSmrg 813101e04c3fSmrg unsigned expl_align = 0; 813201e04c3fSmrg if (layout.flags.q.explicit_align) { 813301e04c3fSmrg if (!process_qualifier_constant(state, &loc, "align", 813401e04c3fSmrg layout.align, &expl_align)) { 813501e04c3fSmrg return NULL; 813601e04c3fSmrg } else { 813701e04c3fSmrg if (expl_align == 0 || expl_align & (expl_align - 1)) { 813801e04c3fSmrg _mesa_glsl_error(&loc, state, "align layout qualifier is not a " 813901e04c3fSmrg "power of 2."); 814001e04c3fSmrg return NULL; 814101e04c3fSmrg } 814201e04c3fSmrg } 814301e04c3fSmrg } 814401e04c3fSmrg 814501e04c3fSmrg unsigned int num_variables = 814601e04c3fSmrg ast_process_struct_or_iface_block_members(&declared_variables, 814701e04c3fSmrg state, 814801e04c3fSmrg &this->declarations, 814901e04c3fSmrg &fields, 815001e04c3fSmrg true, 815101e04c3fSmrg matrix_layout, 815201e04c3fSmrg redeclaring_per_vertex, 815301e04c3fSmrg var_mode, 815401e04c3fSmrg &this->layout, 815501e04c3fSmrg qual_stream, 815601e04c3fSmrg qual_xfb_buffer, 815701e04c3fSmrg qual_xfb_offset, 815801e04c3fSmrg expl_location, 815901e04c3fSmrg expl_align); 816001e04c3fSmrg 816101e04c3fSmrg if (!redeclaring_per_vertex) { 816201e04c3fSmrg validate_identifier(this->block_name, loc, state); 816301e04c3fSmrg 816401e04c3fSmrg /* From section 4.3.9 ("Interface Blocks") of the GLSL 4.50 spec: 816501e04c3fSmrg * 816601e04c3fSmrg * "Block names have no other use within a shader beyond interface 816701e04c3fSmrg * matching; it is a compile-time error to use a block name at global 816801e04c3fSmrg * scope for anything other than as a block name." 816901e04c3fSmrg */ 817001e04c3fSmrg ir_variable *var = state->symbols->get_variable(this->block_name); 817101e04c3fSmrg if (var && !var->type->is_interface()) { 817201e04c3fSmrg _mesa_glsl_error(&loc, state, "Block name `%s' is " 817301e04c3fSmrg "already used in the scope.", 817401e04c3fSmrg this->block_name); 817501e04c3fSmrg } 817601e04c3fSmrg } 817701e04c3fSmrg 817801e04c3fSmrg const glsl_type *earlier_per_vertex = NULL; 817901e04c3fSmrg if (redeclaring_per_vertex) { 818001e04c3fSmrg /* Find the previous declaration of gl_PerVertex. If we're redeclaring 818101e04c3fSmrg * the named interface block gl_in, we can find it by looking at the 818201e04c3fSmrg * previous declaration of gl_in. Otherwise we can find it by looking 818301e04c3fSmrg * at the previous decalartion of any of the built-in outputs, 818401e04c3fSmrg * e.g. gl_Position. 818501e04c3fSmrg * 818601e04c3fSmrg * Also check that the instance name and array-ness of the redeclaration 818701e04c3fSmrg * are correct. 818801e04c3fSmrg */ 818901e04c3fSmrg switch (var_mode) { 819001e04c3fSmrg case ir_var_shader_in: 819101e04c3fSmrg if (ir_variable *earlier_gl_in = 819201e04c3fSmrg state->symbols->get_variable("gl_in")) { 819301e04c3fSmrg earlier_per_vertex = earlier_gl_in->get_interface_type(); 819401e04c3fSmrg } else { 819501e04c3fSmrg _mesa_glsl_error(&loc, state, 819601e04c3fSmrg "redeclaration of gl_PerVertex input not allowed " 819701e04c3fSmrg "in the %s shader", 819801e04c3fSmrg _mesa_shader_stage_to_string(state->stage)); 819901e04c3fSmrg } 820001e04c3fSmrg if (this->instance_name == NULL || 820101e04c3fSmrg strcmp(this->instance_name, "gl_in") != 0 || this->array_specifier == NULL || 820201e04c3fSmrg !this->array_specifier->is_single_dimension()) { 820301e04c3fSmrg _mesa_glsl_error(&loc, state, 820401e04c3fSmrg "gl_PerVertex input must be redeclared as " 820501e04c3fSmrg "gl_in[]"); 820601e04c3fSmrg } 820701e04c3fSmrg break; 820801e04c3fSmrg case ir_var_shader_out: 820901e04c3fSmrg if (ir_variable *earlier_gl_Position = 821001e04c3fSmrg state->symbols->get_variable("gl_Position")) { 821101e04c3fSmrg earlier_per_vertex = earlier_gl_Position->get_interface_type(); 821201e04c3fSmrg } else if (ir_variable *earlier_gl_out = 821301e04c3fSmrg state->symbols->get_variable("gl_out")) { 821401e04c3fSmrg earlier_per_vertex = earlier_gl_out->get_interface_type(); 821501e04c3fSmrg } else { 821601e04c3fSmrg _mesa_glsl_error(&loc, state, 821701e04c3fSmrg "redeclaration of gl_PerVertex output not " 821801e04c3fSmrg "allowed in the %s shader", 821901e04c3fSmrg _mesa_shader_stage_to_string(state->stage)); 822001e04c3fSmrg } 822101e04c3fSmrg if (state->stage == MESA_SHADER_TESS_CTRL) { 822201e04c3fSmrg if (this->instance_name == NULL || 822301e04c3fSmrg strcmp(this->instance_name, "gl_out") != 0 || this->array_specifier == NULL) { 822401e04c3fSmrg _mesa_glsl_error(&loc, state, 822501e04c3fSmrg "gl_PerVertex output must be redeclared as " 822601e04c3fSmrg "gl_out[]"); 822701e04c3fSmrg } 822801e04c3fSmrg } else { 822901e04c3fSmrg if (this->instance_name != NULL) { 823001e04c3fSmrg _mesa_glsl_error(&loc, state, 823101e04c3fSmrg "gl_PerVertex output may not be redeclared with " 823201e04c3fSmrg "an instance name"); 823301e04c3fSmrg } 823401e04c3fSmrg } 823501e04c3fSmrg break; 823601e04c3fSmrg default: 823701e04c3fSmrg _mesa_glsl_error(&loc, state, 823801e04c3fSmrg "gl_PerVertex must be declared as an input or an " 823901e04c3fSmrg "output"); 824001e04c3fSmrg break; 824101e04c3fSmrg } 824201e04c3fSmrg 824301e04c3fSmrg if (earlier_per_vertex == NULL) { 824401e04c3fSmrg /* An error has already been reported. Bail out to avoid null 824501e04c3fSmrg * dereferences later in this function. 824601e04c3fSmrg */ 824701e04c3fSmrg return NULL; 824801e04c3fSmrg } 824901e04c3fSmrg 825001e04c3fSmrg /* Copy locations from the old gl_PerVertex interface block. */ 825101e04c3fSmrg for (unsigned i = 0; i < num_variables; i++) { 825201e04c3fSmrg int j = earlier_per_vertex->field_index(fields[i].name); 825301e04c3fSmrg if (j == -1) { 825401e04c3fSmrg _mesa_glsl_error(&loc, state, 825501e04c3fSmrg "redeclaration of gl_PerVertex must be a subset " 825601e04c3fSmrg "of the built-in members of gl_PerVertex"); 825701e04c3fSmrg } else { 825801e04c3fSmrg fields[i].location = 825901e04c3fSmrg earlier_per_vertex->fields.structure[j].location; 826001e04c3fSmrg fields[i].offset = 826101e04c3fSmrg earlier_per_vertex->fields.structure[j].offset; 826201e04c3fSmrg fields[i].interpolation = 826301e04c3fSmrg earlier_per_vertex->fields.structure[j].interpolation; 826401e04c3fSmrg fields[i].centroid = 826501e04c3fSmrg earlier_per_vertex->fields.structure[j].centroid; 826601e04c3fSmrg fields[i].sample = 826701e04c3fSmrg earlier_per_vertex->fields.structure[j].sample; 826801e04c3fSmrg fields[i].patch = 826901e04c3fSmrg earlier_per_vertex->fields.structure[j].patch; 827001e04c3fSmrg fields[i].precision = 827101e04c3fSmrg earlier_per_vertex->fields.structure[j].precision; 827201e04c3fSmrg fields[i].explicit_xfb_buffer = 827301e04c3fSmrg earlier_per_vertex->fields.structure[j].explicit_xfb_buffer; 827401e04c3fSmrg fields[i].xfb_buffer = 827501e04c3fSmrg earlier_per_vertex->fields.structure[j].xfb_buffer; 827601e04c3fSmrg fields[i].xfb_stride = 827701e04c3fSmrg earlier_per_vertex->fields.structure[j].xfb_stride; 827801e04c3fSmrg } 827901e04c3fSmrg } 828001e04c3fSmrg 828101e04c3fSmrg /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 828201e04c3fSmrg * spec: 828301e04c3fSmrg * 828401e04c3fSmrg * If a built-in interface block is redeclared, it must appear in 828501e04c3fSmrg * the shader before any use of any member included in the built-in 828601e04c3fSmrg * declaration, or a compilation error will result. 828701e04c3fSmrg * 828801e04c3fSmrg * This appears to be a clarification to the behaviour established for 828901e04c3fSmrg * gl_PerVertex by GLSL 1.50, therefore we implement this behaviour 829001e04c3fSmrg * regardless of GLSL version. 829101e04c3fSmrg */ 829201e04c3fSmrg interface_block_usage_visitor v(var_mode, earlier_per_vertex); 829301e04c3fSmrg v.run(instructions); 829401e04c3fSmrg if (v.usage_found()) { 829501e04c3fSmrg _mesa_glsl_error(&loc, state, 829601e04c3fSmrg "redeclaration of a built-in interface block must " 829701e04c3fSmrg "appear before any use of any member of the " 829801e04c3fSmrg "interface block"); 829901e04c3fSmrg } 830001e04c3fSmrg } 830101e04c3fSmrg 830201e04c3fSmrg const glsl_type *block_type = 830301e04c3fSmrg glsl_type::get_interface_instance(fields, 830401e04c3fSmrg num_variables, 830501e04c3fSmrg packing, 830601e04c3fSmrg matrix_layout == 830701e04c3fSmrg GLSL_MATRIX_LAYOUT_ROW_MAJOR, 830801e04c3fSmrg this->block_name); 830901e04c3fSmrg 831001e04c3fSmrg unsigned component_size = block_type->contains_double() ? 8 : 4; 831101e04c3fSmrg int xfb_offset = 831201e04c3fSmrg layout.flags.q.explicit_xfb_offset ? (int) qual_xfb_offset : -1; 831301e04c3fSmrg validate_xfb_offset_qualifier(&loc, state, xfb_offset, block_type, 831401e04c3fSmrg component_size); 831501e04c3fSmrg 831601e04c3fSmrg if (!state->symbols->add_interface(block_type->name, block_type, var_mode)) { 831701e04c3fSmrg YYLTYPE loc = this->get_location(); 831801e04c3fSmrg _mesa_glsl_error(&loc, state, "interface block `%s' with type `%s' " 831901e04c3fSmrg "already taken in the current scope", 832001e04c3fSmrg this->block_name, iface_type_name); 832101e04c3fSmrg } 832201e04c3fSmrg 832301e04c3fSmrg /* Since interface blocks cannot contain statements, it should be 832401e04c3fSmrg * impossible for the block to generate any instructions. 832501e04c3fSmrg */ 832601e04c3fSmrg assert(declared_variables.is_empty()); 832701e04c3fSmrg 832801e04c3fSmrg /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec: 832901e04c3fSmrg * 833001e04c3fSmrg * Geometry shader input variables get the per-vertex values written 833101e04c3fSmrg * out by vertex shader output variables of the same names. Since a 833201e04c3fSmrg * geometry shader operates on a set of vertices, each input varying 833301e04c3fSmrg * variable (or input block, see interface blocks below) needs to be 833401e04c3fSmrg * declared as an array. 833501e04c3fSmrg */ 833601e04c3fSmrg if (state->stage == MESA_SHADER_GEOMETRY && this->array_specifier == NULL && 833701e04c3fSmrg var_mode == ir_var_shader_in) { 833801e04c3fSmrg _mesa_glsl_error(&loc, state, "geometry shader inputs must be arrays"); 833901e04c3fSmrg } else if ((state->stage == MESA_SHADER_TESS_CTRL || 834001e04c3fSmrg state->stage == MESA_SHADER_TESS_EVAL) && 834101e04c3fSmrg !this->layout.flags.q.patch && 834201e04c3fSmrg this->array_specifier == NULL && 834301e04c3fSmrg var_mode == ir_var_shader_in) { 834401e04c3fSmrg _mesa_glsl_error(&loc, state, "per-vertex tessellation shader inputs must be arrays"); 834501e04c3fSmrg } else if (state->stage == MESA_SHADER_TESS_CTRL && 834601e04c3fSmrg !this->layout.flags.q.patch && 834701e04c3fSmrg this->array_specifier == NULL && 834801e04c3fSmrg var_mode == ir_var_shader_out) { 834901e04c3fSmrg _mesa_glsl_error(&loc, state, "tessellation control shader outputs must be arrays"); 835001e04c3fSmrg } 835101e04c3fSmrg 835201e04c3fSmrg 835301e04c3fSmrg /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec 835401e04c3fSmrg * says: 835501e04c3fSmrg * 835601e04c3fSmrg * "If an instance name (instance-name) is used, then it puts all the 835701e04c3fSmrg * members inside a scope within its own name space, accessed with the 835801e04c3fSmrg * field selector ( . ) operator (analogously to structures)." 835901e04c3fSmrg */ 836001e04c3fSmrg if (this->instance_name) { 836101e04c3fSmrg if (redeclaring_per_vertex) { 836201e04c3fSmrg /* When a built-in in an unnamed interface block is redeclared, 836301e04c3fSmrg * get_variable_being_redeclared() calls 836401e04c3fSmrg * check_builtin_array_max_size() to make sure that built-in array 836501e04c3fSmrg * variables aren't redeclared to illegal sizes. But we're looking 836601e04c3fSmrg * at a redeclaration of a named built-in interface block. So we 836701e04c3fSmrg * have to manually call check_builtin_array_max_size() for all parts 836801e04c3fSmrg * of the interface that are arrays. 836901e04c3fSmrg */ 837001e04c3fSmrg for (unsigned i = 0; i < num_variables; i++) { 837101e04c3fSmrg if (fields[i].type->is_array()) { 837201e04c3fSmrg const unsigned size = fields[i].type->array_size(); 837301e04c3fSmrg check_builtin_array_max_size(fields[i].name, size, loc, state); 837401e04c3fSmrg } 837501e04c3fSmrg } 837601e04c3fSmrg } else { 837701e04c3fSmrg validate_identifier(this->instance_name, loc, state); 837801e04c3fSmrg } 837901e04c3fSmrg 838001e04c3fSmrg ir_variable *var; 838101e04c3fSmrg 838201e04c3fSmrg if (this->array_specifier != NULL) { 838301e04c3fSmrg const glsl_type *block_array_type = 838401e04c3fSmrg process_array_type(&loc, block_type, this->array_specifier, state); 838501e04c3fSmrg 838601e04c3fSmrg /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says: 838701e04c3fSmrg * 838801e04c3fSmrg * For uniform blocks declared an array, each individual array 838901e04c3fSmrg * element corresponds to a separate buffer object backing one 839001e04c3fSmrg * instance of the block. As the array size indicates the number 839101e04c3fSmrg * of buffer objects needed, uniform block array declarations 839201e04c3fSmrg * must specify an array size. 839301e04c3fSmrg * 839401e04c3fSmrg * And a few paragraphs later: 839501e04c3fSmrg * 839601e04c3fSmrg * Geometry shader input blocks must be declared as arrays and 839701e04c3fSmrg * follow the array declaration and linking rules for all 839801e04c3fSmrg * geometry shader inputs. All other input and output block 839901e04c3fSmrg * arrays must specify an array size. 840001e04c3fSmrg * 840101e04c3fSmrg * The same applies to tessellation shaders. 840201e04c3fSmrg * 840301e04c3fSmrg * The upshot of this is that the only circumstance where an 840401e04c3fSmrg * interface array size *doesn't* need to be specified is on a 840501e04c3fSmrg * geometry shader input, tessellation control shader input, 840601e04c3fSmrg * tessellation control shader output, and tessellation evaluation 840701e04c3fSmrg * shader input. 840801e04c3fSmrg */ 840901e04c3fSmrg if (block_array_type->is_unsized_array()) { 841001e04c3fSmrg bool allow_inputs = state->stage == MESA_SHADER_GEOMETRY || 841101e04c3fSmrg state->stage == MESA_SHADER_TESS_CTRL || 841201e04c3fSmrg state->stage == MESA_SHADER_TESS_EVAL; 841301e04c3fSmrg bool allow_outputs = state->stage == MESA_SHADER_TESS_CTRL; 841401e04c3fSmrg 841501e04c3fSmrg if (this->layout.flags.q.in) { 841601e04c3fSmrg if (!allow_inputs) 841701e04c3fSmrg _mesa_glsl_error(&loc, state, 841801e04c3fSmrg "unsized input block arrays not allowed in " 841901e04c3fSmrg "%s shader", 842001e04c3fSmrg _mesa_shader_stage_to_string(state->stage)); 842101e04c3fSmrg } else if (this->layout.flags.q.out) { 842201e04c3fSmrg if (!allow_outputs) 842301e04c3fSmrg _mesa_glsl_error(&loc, state, 842401e04c3fSmrg "unsized output block arrays not allowed in " 842501e04c3fSmrg "%s shader", 842601e04c3fSmrg _mesa_shader_stage_to_string(state->stage)); 842701e04c3fSmrg } else { 842801e04c3fSmrg /* by elimination, this is a uniform block array */ 842901e04c3fSmrg _mesa_glsl_error(&loc, state, 843001e04c3fSmrg "unsized uniform block arrays not allowed in " 843101e04c3fSmrg "%s shader", 843201e04c3fSmrg _mesa_shader_stage_to_string(state->stage)); 843301e04c3fSmrg } 843401e04c3fSmrg } 843501e04c3fSmrg 843601e04c3fSmrg /* From section 4.3.9 (Interface Blocks) of the GLSL ES 3.10 spec: 843701e04c3fSmrg * 843801e04c3fSmrg * * Arrays of arrays of blocks are not allowed 843901e04c3fSmrg */ 844001e04c3fSmrg if (state->es_shader && block_array_type->is_array() && 844101e04c3fSmrg block_array_type->fields.array->is_array()) { 844201e04c3fSmrg _mesa_glsl_error(&loc, state, 844301e04c3fSmrg "arrays of arrays interface blocks are " 844401e04c3fSmrg "not allowed"); 844501e04c3fSmrg } 844601e04c3fSmrg 844701e04c3fSmrg var = new(state) ir_variable(block_array_type, 844801e04c3fSmrg this->instance_name, 844901e04c3fSmrg var_mode); 845001e04c3fSmrg } else { 845101e04c3fSmrg var = new(state) ir_variable(block_type, 845201e04c3fSmrg this->instance_name, 845301e04c3fSmrg var_mode); 845401e04c3fSmrg } 845501e04c3fSmrg 845601e04c3fSmrg var->data.matrix_layout = matrix_layout == GLSL_MATRIX_LAYOUT_INHERITED 845701e04c3fSmrg ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR : matrix_layout; 845801e04c3fSmrg 845901e04c3fSmrg if (var_mode == ir_var_shader_in || var_mode == ir_var_uniform) 846001e04c3fSmrg var->data.read_only = true; 846101e04c3fSmrg 846201e04c3fSmrg var->data.patch = this->layout.flags.q.patch; 846301e04c3fSmrg 846401e04c3fSmrg if (state->stage == MESA_SHADER_GEOMETRY && var_mode == ir_var_shader_in) 846501e04c3fSmrg handle_geometry_shader_input_decl(state, loc, var); 846601e04c3fSmrg else if ((state->stage == MESA_SHADER_TESS_CTRL || 846701e04c3fSmrg state->stage == MESA_SHADER_TESS_EVAL) && var_mode == ir_var_shader_in) 846801e04c3fSmrg handle_tess_shader_input_decl(state, loc, var); 846901e04c3fSmrg else if (state->stage == MESA_SHADER_TESS_CTRL && var_mode == ir_var_shader_out) 847001e04c3fSmrg handle_tess_ctrl_shader_output_decl(state, loc, var); 847101e04c3fSmrg 847201e04c3fSmrg for (unsigned i = 0; i < num_variables; i++) { 847301e04c3fSmrg if (var->data.mode == ir_var_shader_storage) 847401e04c3fSmrg apply_memory_qualifiers(var, fields[i]); 847501e04c3fSmrg } 847601e04c3fSmrg 847701e04c3fSmrg if (ir_variable *earlier = 847801e04c3fSmrg state->symbols->get_variable(this->instance_name)) { 847901e04c3fSmrg if (!redeclaring_per_vertex) { 848001e04c3fSmrg _mesa_glsl_error(&loc, state, "`%s' redeclared", 848101e04c3fSmrg this->instance_name); 848201e04c3fSmrg } 848301e04c3fSmrg earlier->data.how_declared = ir_var_declared_normally; 848401e04c3fSmrg earlier->type = var->type; 848501e04c3fSmrg earlier->reinit_interface_type(block_type); 848601e04c3fSmrg delete var; 848701e04c3fSmrg } else { 848801e04c3fSmrg if (this->layout.flags.q.explicit_binding) { 848901e04c3fSmrg apply_explicit_binding(state, &loc, var, var->type, 849001e04c3fSmrg &this->layout); 849101e04c3fSmrg } 849201e04c3fSmrg 849301e04c3fSmrg var->data.stream = qual_stream; 849401e04c3fSmrg if (layout.flags.q.explicit_location) { 849501e04c3fSmrg var->data.location = expl_location; 849601e04c3fSmrg var->data.explicit_location = true; 849701e04c3fSmrg } 849801e04c3fSmrg 849901e04c3fSmrg state->symbols->add_variable(var); 850001e04c3fSmrg instructions->push_tail(var); 850101e04c3fSmrg } 850201e04c3fSmrg } else { 850301e04c3fSmrg /* In order to have an array size, the block must also be declared with 850401e04c3fSmrg * an instance name. 850501e04c3fSmrg */ 850601e04c3fSmrg assert(this->array_specifier == NULL); 850701e04c3fSmrg 850801e04c3fSmrg for (unsigned i = 0; i < num_variables; i++) { 850901e04c3fSmrg ir_variable *var = 851001e04c3fSmrg new(state) ir_variable(fields[i].type, 851101e04c3fSmrg ralloc_strdup(state, fields[i].name), 851201e04c3fSmrg var_mode); 851301e04c3fSmrg var->data.interpolation = fields[i].interpolation; 851401e04c3fSmrg var->data.centroid = fields[i].centroid; 851501e04c3fSmrg var->data.sample = fields[i].sample; 851601e04c3fSmrg var->data.patch = fields[i].patch; 851701e04c3fSmrg var->data.stream = qual_stream; 851801e04c3fSmrg var->data.location = fields[i].location; 851901e04c3fSmrg 852001e04c3fSmrg if (fields[i].location != -1) 852101e04c3fSmrg var->data.explicit_location = true; 852201e04c3fSmrg 852301e04c3fSmrg var->data.explicit_xfb_buffer = fields[i].explicit_xfb_buffer; 852401e04c3fSmrg var->data.xfb_buffer = fields[i].xfb_buffer; 852501e04c3fSmrg 852601e04c3fSmrg if (fields[i].offset != -1) 852701e04c3fSmrg var->data.explicit_xfb_offset = true; 852801e04c3fSmrg var->data.offset = fields[i].offset; 852901e04c3fSmrg 853001e04c3fSmrg var->init_interface_type(block_type); 853101e04c3fSmrg 853201e04c3fSmrg if (var_mode == ir_var_shader_in || var_mode == ir_var_uniform) 853301e04c3fSmrg var->data.read_only = true; 853401e04c3fSmrg 853501e04c3fSmrg /* Precision qualifiers do not have any meaning in Desktop GLSL */ 853601e04c3fSmrg if (state->es_shader) { 853701e04c3fSmrg var->data.precision = 853801e04c3fSmrg select_gles_precision(fields[i].precision, fields[i].type, 853901e04c3fSmrg state, &loc); 854001e04c3fSmrg } 854101e04c3fSmrg 854201e04c3fSmrg if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_INHERITED) { 854301e04c3fSmrg var->data.matrix_layout = matrix_layout == GLSL_MATRIX_LAYOUT_INHERITED 854401e04c3fSmrg ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR : matrix_layout; 854501e04c3fSmrg } else { 854601e04c3fSmrg var->data.matrix_layout = fields[i].matrix_layout; 854701e04c3fSmrg } 854801e04c3fSmrg 854901e04c3fSmrg if (var->data.mode == ir_var_shader_storage) 855001e04c3fSmrg apply_memory_qualifiers(var, fields[i]); 855101e04c3fSmrg 855201e04c3fSmrg /* Examine var name here since var may get deleted in the next call */ 855301e04c3fSmrg bool var_is_gl_id = is_gl_identifier(var->name); 855401e04c3fSmrg 855501e04c3fSmrg if (redeclaring_per_vertex) { 855601e04c3fSmrg bool is_redeclaration; 855701e04c3fSmrg var = 855801e04c3fSmrg get_variable_being_redeclared(&var, loc, state, 855901e04c3fSmrg true /* allow_all_redeclarations */, 856001e04c3fSmrg &is_redeclaration); 856101e04c3fSmrg if (!var_is_gl_id || !is_redeclaration) { 856201e04c3fSmrg _mesa_glsl_error(&loc, state, 856301e04c3fSmrg "redeclaration of gl_PerVertex can only " 856401e04c3fSmrg "include built-in variables"); 856501e04c3fSmrg } else if (var->data.how_declared == ir_var_declared_normally) { 856601e04c3fSmrg _mesa_glsl_error(&loc, state, 856701e04c3fSmrg "`%s' has already been redeclared", 856801e04c3fSmrg var->name); 856901e04c3fSmrg } else { 857001e04c3fSmrg var->data.how_declared = ir_var_declared_in_block; 857101e04c3fSmrg var->reinit_interface_type(block_type); 857201e04c3fSmrg } 857301e04c3fSmrg continue; 857401e04c3fSmrg } 857501e04c3fSmrg 857601e04c3fSmrg if (state->symbols->get_variable(var->name) != NULL) 857701e04c3fSmrg _mesa_glsl_error(&loc, state, "`%s' redeclared", var->name); 857801e04c3fSmrg 857901e04c3fSmrg /* Propagate the "binding" keyword into this UBO/SSBO's fields. 858001e04c3fSmrg * The UBO declaration itself doesn't get an ir_variable unless it 858101e04c3fSmrg * has an instance name. This is ugly. 858201e04c3fSmrg */ 858301e04c3fSmrg if (this->layout.flags.q.explicit_binding) { 858401e04c3fSmrg apply_explicit_binding(state, &loc, var, 858501e04c3fSmrg var->get_interface_type(), &this->layout); 858601e04c3fSmrg } 858701e04c3fSmrg 858801e04c3fSmrg if (var->type->is_unsized_array()) { 858901e04c3fSmrg if (var->is_in_shader_storage_block() && 859001e04c3fSmrg is_unsized_array_last_element(var)) { 859101e04c3fSmrg var->data.from_ssbo_unsized_array = true; 859201e04c3fSmrg } else { 859301e04c3fSmrg /* From GLSL ES 3.10 spec, section 4.1.9 "Arrays": 859401e04c3fSmrg * 859501e04c3fSmrg * "If an array is declared as the last member of a shader storage 859601e04c3fSmrg * block and the size is not specified at compile-time, it is 859701e04c3fSmrg * sized at run-time. In all other cases, arrays are sized only 859801e04c3fSmrg * at compile-time." 859901e04c3fSmrg * 860001e04c3fSmrg * In desktop GLSL it is allowed to have unsized-arrays that are 860101e04c3fSmrg * not last, as long as we can determine that they are implicitly 860201e04c3fSmrg * sized. 860301e04c3fSmrg */ 860401e04c3fSmrg if (state->es_shader) { 860501e04c3fSmrg _mesa_glsl_error(&loc, state, "unsized array `%s' " 860601e04c3fSmrg "definition: only last member of a shader " 860701e04c3fSmrg "storage block can be defined as unsized " 860801e04c3fSmrg "array", fields[i].name); 860901e04c3fSmrg } 861001e04c3fSmrg } 861101e04c3fSmrg } 861201e04c3fSmrg 861301e04c3fSmrg state->symbols->add_variable(var); 861401e04c3fSmrg instructions->push_tail(var); 861501e04c3fSmrg } 861601e04c3fSmrg 861701e04c3fSmrg if (redeclaring_per_vertex && block_type != earlier_per_vertex) { 861801e04c3fSmrg /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec: 861901e04c3fSmrg * 862001e04c3fSmrg * It is also a compilation error ... to redeclare a built-in 862101e04c3fSmrg * block and then use a member from that built-in block that was 862201e04c3fSmrg * not included in the redeclaration. 862301e04c3fSmrg * 862401e04c3fSmrg * This appears to be a clarification to the behaviour established 862501e04c3fSmrg * for gl_PerVertex by GLSL 1.50, therefore we implement this 862601e04c3fSmrg * behaviour regardless of GLSL version. 862701e04c3fSmrg * 862801e04c3fSmrg * To prevent the shader from using a member that was not included in 862901e04c3fSmrg * the redeclaration, we disable any ir_variables that are still 863001e04c3fSmrg * associated with the old declaration of gl_PerVertex (since we've 863101e04c3fSmrg * already updated all of the variables contained in the new 863201e04c3fSmrg * gl_PerVertex to point to it). 863301e04c3fSmrg * 863401e04c3fSmrg * As a side effect this will prevent 863501e04c3fSmrg * validate_intrastage_interface_blocks() from getting confused and 863601e04c3fSmrg * thinking there are conflicting definitions of gl_PerVertex in the 863701e04c3fSmrg * shader. 863801e04c3fSmrg */ 863901e04c3fSmrg foreach_in_list_safe(ir_instruction, node, instructions) { 864001e04c3fSmrg ir_variable *const var = node->as_variable(); 864101e04c3fSmrg if (var != NULL && 864201e04c3fSmrg var->get_interface_type() == earlier_per_vertex && 864301e04c3fSmrg var->data.mode == var_mode) { 864401e04c3fSmrg if (var->data.how_declared == ir_var_declared_normally) { 864501e04c3fSmrg _mesa_glsl_error(&loc, state, 864601e04c3fSmrg "redeclaration of gl_PerVertex cannot " 864701e04c3fSmrg "follow a redeclaration of `%s'", 864801e04c3fSmrg var->name); 864901e04c3fSmrg } 865001e04c3fSmrg state->symbols->disable_variable(var->name); 865101e04c3fSmrg var->remove(); 865201e04c3fSmrg } 865301e04c3fSmrg } 865401e04c3fSmrg } 865501e04c3fSmrg } 865601e04c3fSmrg 865701e04c3fSmrg return NULL; 865801e04c3fSmrg} 865901e04c3fSmrg 866001e04c3fSmrg 866101e04c3fSmrgir_rvalue * 866201e04c3fSmrgast_tcs_output_layout::hir(exec_list *instructions, 866301e04c3fSmrg struct _mesa_glsl_parse_state *state) 866401e04c3fSmrg{ 866501e04c3fSmrg YYLTYPE loc = this->get_location(); 866601e04c3fSmrg 866701e04c3fSmrg unsigned num_vertices; 866801e04c3fSmrg if (!state->out_qualifier->vertices-> 866901e04c3fSmrg process_qualifier_constant(state, "vertices", &num_vertices, 867001e04c3fSmrg false)) { 867101e04c3fSmrg /* return here to stop cascading incorrect error messages */ 867201e04c3fSmrg return NULL; 867301e04c3fSmrg } 867401e04c3fSmrg 867501e04c3fSmrg /* If any shader outputs occurred before this declaration and specified an 867601e04c3fSmrg * array size, make sure the size they specified is consistent with the 867701e04c3fSmrg * primitive type. 867801e04c3fSmrg */ 867901e04c3fSmrg if (state->tcs_output_size != 0 && state->tcs_output_size != num_vertices) { 868001e04c3fSmrg _mesa_glsl_error(&loc, state, 868101e04c3fSmrg "this tessellation control shader output layout " 868201e04c3fSmrg "specifies %u vertices, but a previous output " 868301e04c3fSmrg "is declared with size %u", 868401e04c3fSmrg num_vertices, state->tcs_output_size); 868501e04c3fSmrg return NULL; 868601e04c3fSmrg } 868701e04c3fSmrg 868801e04c3fSmrg state->tcs_output_vertices_specified = true; 868901e04c3fSmrg 869001e04c3fSmrg /* If any shader outputs occurred before this declaration and did not 869101e04c3fSmrg * specify an array size, their size is determined now. 869201e04c3fSmrg */ 869301e04c3fSmrg foreach_in_list (ir_instruction, node, instructions) { 869401e04c3fSmrg ir_variable *var = node->as_variable(); 869501e04c3fSmrg if (var == NULL || var->data.mode != ir_var_shader_out) 869601e04c3fSmrg continue; 869701e04c3fSmrg 869801e04c3fSmrg /* Note: Not all tessellation control shader output are arrays. */ 869901e04c3fSmrg if (!var->type->is_unsized_array() || var->data.patch) 870001e04c3fSmrg continue; 870101e04c3fSmrg 870201e04c3fSmrg if (var->data.max_array_access >= (int)num_vertices) { 870301e04c3fSmrg _mesa_glsl_error(&loc, state, 870401e04c3fSmrg "this tessellation control shader output layout " 870501e04c3fSmrg "specifies %u vertices, but an access to element " 870601e04c3fSmrg "%u of output `%s' already exists", num_vertices, 870701e04c3fSmrg var->data.max_array_access, var->name); 870801e04c3fSmrg } else { 870901e04c3fSmrg var->type = glsl_type::get_array_instance(var->type->fields.array, 871001e04c3fSmrg num_vertices); 871101e04c3fSmrg } 871201e04c3fSmrg } 871301e04c3fSmrg 871401e04c3fSmrg return NULL; 871501e04c3fSmrg} 871601e04c3fSmrg 871701e04c3fSmrg 871801e04c3fSmrgir_rvalue * 871901e04c3fSmrgast_gs_input_layout::hir(exec_list *instructions, 872001e04c3fSmrg struct _mesa_glsl_parse_state *state) 872101e04c3fSmrg{ 872201e04c3fSmrg YYLTYPE loc = this->get_location(); 872301e04c3fSmrg 872401e04c3fSmrg /* Should have been prevented by the parser. */ 872501e04c3fSmrg assert(!state->gs_input_prim_type_specified 872601e04c3fSmrg || state->in_qualifier->prim_type == this->prim_type); 872701e04c3fSmrg 872801e04c3fSmrg /* If any shader inputs occurred before this declaration and specified an 872901e04c3fSmrg * array size, make sure the size they specified is consistent with the 873001e04c3fSmrg * primitive type. 873101e04c3fSmrg */ 873201e04c3fSmrg unsigned num_vertices = vertices_per_prim(this->prim_type); 873301e04c3fSmrg if (state->gs_input_size != 0 && state->gs_input_size != num_vertices) { 873401e04c3fSmrg _mesa_glsl_error(&loc, state, 873501e04c3fSmrg "this geometry shader input layout implies %u vertices" 873601e04c3fSmrg " per primitive, but a previous input is declared" 873701e04c3fSmrg " with size %u", num_vertices, state->gs_input_size); 873801e04c3fSmrg return NULL; 873901e04c3fSmrg } 874001e04c3fSmrg 874101e04c3fSmrg state->gs_input_prim_type_specified = true; 874201e04c3fSmrg 874301e04c3fSmrg /* If any shader inputs occurred before this declaration and did not 874401e04c3fSmrg * specify an array size, their size is determined now. 874501e04c3fSmrg */ 874601e04c3fSmrg foreach_in_list(ir_instruction, node, instructions) { 874701e04c3fSmrg ir_variable *var = node->as_variable(); 874801e04c3fSmrg if (var == NULL || var->data.mode != ir_var_shader_in) 874901e04c3fSmrg continue; 875001e04c3fSmrg 875101e04c3fSmrg /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an 875201e04c3fSmrg * array; skip it. 875301e04c3fSmrg */ 875401e04c3fSmrg 875501e04c3fSmrg if (var->type->is_unsized_array()) { 875601e04c3fSmrg if (var->data.max_array_access >= (int)num_vertices) { 875701e04c3fSmrg _mesa_glsl_error(&loc, state, 875801e04c3fSmrg "this geometry shader input layout implies %u" 875901e04c3fSmrg " vertices, but an access to element %u of input" 876001e04c3fSmrg " `%s' already exists", num_vertices, 876101e04c3fSmrg var->data.max_array_access, var->name); 876201e04c3fSmrg } else { 876301e04c3fSmrg var->type = glsl_type::get_array_instance(var->type->fields.array, 876401e04c3fSmrg num_vertices); 876501e04c3fSmrg } 876601e04c3fSmrg } 876701e04c3fSmrg } 876801e04c3fSmrg 876901e04c3fSmrg return NULL; 877001e04c3fSmrg} 877101e04c3fSmrg 877201e04c3fSmrg 877301e04c3fSmrgir_rvalue * 877401e04c3fSmrgast_cs_input_layout::hir(exec_list *instructions, 877501e04c3fSmrg struct _mesa_glsl_parse_state *state) 877601e04c3fSmrg{ 877701e04c3fSmrg YYLTYPE loc = this->get_location(); 877801e04c3fSmrg 877901e04c3fSmrg /* From the ARB_compute_shader specification: 878001e04c3fSmrg * 878101e04c3fSmrg * If the local size of the shader in any dimension is greater 878201e04c3fSmrg * than the maximum size supported by the implementation for that 878301e04c3fSmrg * dimension, a compile-time error results. 878401e04c3fSmrg * 878501e04c3fSmrg * It is not clear from the spec how the error should be reported if 878601e04c3fSmrg * the total size of the work group exceeds 878701e04c3fSmrg * MAX_COMPUTE_WORK_GROUP_INVOCATIONS, but it seems reasonable to 878801e04c3fSmrg * report it at compile time as well. 878901e04c3fSmrg */ 879001e04c3fSmrg GLuint64 total_invocations = 1; 879101e04c3fSmrg unsigned qual_local_size[3]; 879201e04c3fSmrg for (int i = 0; i < 3; i++) { 879301e04c3fSmrg 879401e04c3fSmrg char *local_size_str = ralloc_asprintf(NULL, "invalid local_size_%c", 879501e04c3fSmrg 'x' + i); 879601e04c3fSmrg /* Infer a local_size of 1 for unspecified dimensions */ 879701e04c3fSmrg if (this->local_size[i] == NULL) { 879801e04c3fSmrg qual_local_size[i] = 1; 879901e04c3fSmrg } else if (!this->local_size[i]-> 880001e04c3fSmrg process_qualifier_constant(state, local_size_str, 880101e04c3fSmrg &qual_local_size[i], false)) { 880201e04c3fSmrg ralloc_free(local_size_str); 880301e04c3fSmrg return NULL; 880401e04c3fSmrg } 880501e04c3fSmrg ralloc_free(local_size_str); 880601e04c3fSmrg 880701e04c3fSmrg if (qual_local_size[i] > state->ctx->Const.MaxComputeWorkGroupSize[i]) { 880801e04c3fSmrg _mesa_glsl_error(&loc, state, 880901e04c3fSmrg "local_size_%c exceeds MAX_COMPUTE_WORK_GROUP_SIZE" 881001e04c3fSmrg " (%d)", 'x' + i, 881101e04c3fSmrg state->ctx->Const.MaxComputeWorkGroupSize[i]); 881201e04c3fSmrg break; 881301e04c3fSmrg } 881401e04c3fSmrg total_invocations *= qual_local_size[i]; 881501e04c3fSmrg if (total_invocations > 881601e04c3fSmrg state->ctx->Const.MaxComputeWorkGroupInvocations) { 881701e04c3fSmrg _mesa_glsl_error(&loc, state, 881801e04c3fSmrg "product of local_sizes exceeds " 881901e04c3fSmrg "MAX_COMPUTE_WORK_GROUP_INVOCATIONS (%d)", 882001e04c3fSmrg state->ctx->Const.MaxComputeWorkGroupInvocations); 882101e04c3fSmrg break; 882201e04c3fSmrg } 882301e04c3fSmrg } 882401e04c3fSmrg 882501e04c3fSmrg /* If any compute input layout declaration preceded this one, make sure it 882601e04c3fSmrg * was consistent with this one. 882701e04c3fSmrg */ 882801e04c3fSmrg if (state->cs_input_local_size_specified) { 882901e04c3fSmrg for (int i = 0; i < 3; i++) { 883001e04c3fSmrg if (state->cs_input_local_size[i] != qual_local_size[i]) { 883101e04c3fSmrg _mesa_glsl_error(&loc, state, 883201e04c3fSmrg "compute shader input layout does not match" 883301e04c3fSmrg " previous declaration"); 883401e04c3fSmrg return NULL; 883501e04c3fSmrg } 883601e04c3fSmrg } 883701e04c3fSmrg } 883801e04c3fSmrg 883901e04c3fSmrg /* The ARB_compute_variable_group_size spec says: 884001e04c3fSmrg * 884101e04c3fSmrg * If a compute shader including a *local_size_variable* qualifier also 884201e04c3fSmrg * declares a fixed local group size using the *local_size_x*, 884301e04c3fSmrg * *local_size_y*, or *local_size_z* qualifiers, a compile-time error 884401e04c3fSmrg * results 884501e04c3fSmrg */ 884601e04c3fSmrg if (state->cs_input_local_size_variable_specified) { 884701e04c3fSmrg _mesa_glsl_error(&loc, state, 884801e04c3fSmrg "compute shader can't include both a variable and a " 884901e04c3fSmrg "fixed local group size"); 885001e04c3fSmrg return NULL; 885101e04c3fSmrg } 885201e04c3fSmrg 885301e04c3fSmrg state->cs_input_local_size_specified = true; 885401e04c3fSmrg for (int i = 0; i < 3; i++) 885501e04c3fSmrg state->cs_input_local_size[i] = qual_local_size[i]; 885601e04c3fSmrg 885701e04c3fSmrg /* We may now declare the built-in constant gl_WorkGroupSize (see 885801e04c3fSmrg * builtin_variable_generator::generate_constants() for why we didn't 885901e04c3fSmrg * declare it earlier). 886001e04c3fSmrg */ 886101e04c3fSmrg ir_variable *var = new(state->symbols) 886201e04c3fSmrg ir_variable(glsl_type::uvec3_type, "gl_WorkGroupSize", ir_var_auto); 886301e04c3fSmrg var->data.how_declared = ir_var_declared_implicitly; 886401e04c3fSmrg var->data.read_only = true; 886501e04c3fSmrg instructions->push_tail(var); 886601e04c3fSmrg state->symbols->add_variable(var); 886701e04c3fSmrg ir_constant_data data; 886801e04c3fSmrg memset(&data, 0, sizeof(data)); 886901e04c3fSmrg for (int i = 0; i < 3; i++) 887001e04c3fSmrg data.u[i] = qual_local_size[i]; 887101e04c3fSmrg var->constant_value = new(var) ir_constant(glsl_type::uvec3_type, &data); 887201e04c3fSmrg var->constant_initializer = 887301e04c3fSmrg new(var) ir_constant(glsl_type::uvec3_type, &data); 887401e04c3fSmrg var->data.has_initializer = true; 88757ec681f3Smrg var->data.is_implicit_initializer = false; 887601e04c3fSmrg 887701e04c3fSmrg return NULL; 887801e04c3fSmrg} 887901e04c3fSmrg 888001e04c3fSmrg 888101e04c3fSmrgstatic void 888201e04c3fSmrgdetect_conflicting_assignments(struct _mesa_glsl_parse_state *state, 888301e04c3fSmrg exec_list *instructions) 888401e04c3fSmrg{ 888501e04c3fSmrg bool gl_FragColor_assigned = false; 888601e04c3fSmrg bool gl_FragData_assigned = false; 888701e04c3fSmrg bool gl_FragSecondaryColor_assigned = false; 888801e04c3fSmrg bool gl_FragSecondaryData_assigned = false; 888901e04c3fSmrg bool user_defined_fs_output_assigned = false; 889001e04c3fSmrg ir_variable *user_defined_fs_output = NULL; 889101e04c3fSmrg 889201e04c3fSmrg /* It would be nice to have proper location information. */ 889301e04c3fSmrg YYLTYPE loc; 889401e04c3fSmrg memset(&loc, 0, sizeof(loc)); 889501e04c3fSmrg 889601e04c3fSmrg foreach_in_list(ir_instruction, node, instructions) { 889701e04c3fSmrg ir_variable *var = node->as_variable(); 889801e04c3fSmrg 889901e04c3fSmrg if (!var || !var->data.assigned) 890001e04c3fSmrg continue; 890101e04c3fSmrg 89027ec681f3Smrg if (strcmp(var->name, "gl_FragColor") == 0) { 890301e04c3fSmrg gl_FragColor_assigned = true; 89047ec681f3Smrg if (!var->constant_initializer && state->zero_init) { 89057ec681f3Smrg const ir_constant_data data = { { 0 } }; 89067ec681f3Smrg var->data.has_initializer = true; 89077ec681f3Smrg var->data.is_implicit_initializer = true; 89087ec681f3Smrg var->constant_initializer = new(var) ir_constant(var->type, &data); 89097ec681f3Smrg } 89107ec681f3Smrg } 891101e04c3fSmrg else if (strcmp(var->name, "gl_FragData") == 0) 891201e04c3fSmrg gl_FragData_assigned = true; 891301e04c3fSmrg else if (strcmp(var->name, "gl_SecondaryFragColorEXT") == 0) 891401e04c3fSmrg gl_FragSecondaryColor_assigned = true; 891501e04c3fSmrg else if (strcmp(var->name, "gl_SecondaryFragDataEXT") == 0) 891601e04c3fSmrg gl_FragSecondaryData_assigned = true; 891701e04c3fSmrg else if (!is_gl_identifier(var->name)) { 891801e04c3fSmrg if (state->stage == MESA_SHADER_FRAGMENT && 891901e04c3fSmrg var->data.mode == ir_var_shader_out) { 892001e04c3fSmrg user_defined_fs_output_assigned = true; 892101e04c3fSmrg user_defined_fs_output = var; 892201e04c3fSmrg } 892301e04c3fSmrg } 892401e04c3fSmrg } 892501e04c3fSmrg 892601e04c3fSmrg /* From the GLSL 1.30 spec: 892701e04c3fSmrg * 892801e04c3fSmrg * "If a shader statically assigns a value to gl_FragColor, it 892901e04c3fSmrg * may not assign a value to any element of gl_FragData. If a 893001e04c3fSmrg * shader statically writes a value to any element of 893101e04c3fSmrg * gl_FragData, it may not assign a value to 893201e04c3fSmrg * gl_FragColor. That is, a shader may assign values to either 893301e04c3fSmrg * gl_FragColor or gl_FragData, but not both. Multiple shaders 893401e04c3fSmrg * linked together must also consistently write just one of 893501e04c3fSmrg * these variables. Similarly, if user declared output 893601e04c3fSmrg * variables are in use (statically assigned to), then the 893701e04c3fSmrg * built-in variables gl_FragColor and gl_FragData may not be 893801e04c3fSmrg * assigned to. These incorrect usages all generate compile 893901e04c3fSmrg * time errors." 894001e04c3fSmrg */ 894101e04c3fSmrg if (gl_FragColor_assigned && gl_FragData_assigned) { 894201e04c3fSmrg _mesa_glsl_error(&loc, state, "fragment shader writes to both " 894301e04c3fSmrg "`gl_FragColor' and `gl_FragData'"); 894401e04c3fSmrg } else if (gl_FragColor_assigned && user_defined_fs_output_assigned) { 894501e04c3fSmrg _mesa_glsl_error(&loc, state, "fragment shader writes to both " 894601e04c3fSmrg "`gl_FragColor' and `%s'", 894701e04c3fSmrg user_defined_fs_output->name); 894801e04c3fSmrg } else if (gl_FragSecondaryColor_assigned && gl_FragSecondaryData_assigned) { 894901e04c3fSmrg _mesa_glsl_error(&loc, state, "fragment shader writes to both " 895001e04c3fSmrg "`gl_FragSecondaryColorEXT' and" 895101e04c3fSmrg " `gl_FragSecondaryDataEXT'"); 895201e04c3fSmrg } else if (gl_FragColor_assigned && gl_FragSecondaryData_assigned) { 895301e04c3fSmrg _mesa_glsl_error(&loc, state, "fragment shader writes to both " 895401e04c3fSmrg "`gl_FragColor' and" 895501e04c3fSmrg " `gl_FragSecondaryDataEXT'"); 895601e04c3fSmrg } else if (gl_FragData_assigned && gl_FragSecondaryColor_assigned) { 895701e04c3fSmrg _mesa_glsl_error(&loc, state, "fragment shader writes to both " 895801e04c3fSmrg "`gl_FragData' and" 895901e04c3fSmrg " `gl_FragSecondaryColorEXT'"); 896001e04c3fSmrg } else if (gl_FragData_assigned && user_defined_fs_output_assigned) { 896101e04c3fSmrg _mesa_glsl_error(&loc, state, "fragment shader writes to both " 896201e04c3fSmrg "`gl_FragData' and `%s'", 896301e04c3fSmrg user_defined_fs_output->name); 896401e04c3fSmrg } 896501e04c3fSmrg 896601e04c3fSmrg if ((gl_FragSecondaryColor_assigned || gl_FragSecondaryData_assigned) && 896701e04c3fSmrg !state->EXT_blend_func_extended_enable) { 896801e04c3fSmrg _mesa_glsl_error(&loc, state, 896901e04c3fSmrg "Dual source blending requires EXT_blend_func_extended"); 897001e04c3fSmrg } 897101e04c3fSmrg} 897201e04c3fSmrg 897301e04c3fSmrgstatic void 897401e04c3fSmrgverify_subroutine_associated_funcs(struct _mesa_glsl_parse_state *state) 897501e04c3fSmrg{ 897601e04c3fSmrg YYLTYPE loc; 897701e04c3fSmrg memset(&loc, 0, sizeof(loc)); 897801e04c3fSmrg 897901e04c3fSmrg /* Section 6.1.2 (Subroutines) of the GLSL 4.00 spec says: 898001e04c3fSmrg * 898101e04c3fSmrg * "A program will fail to compile or link if any shader 898201e04c3fSmrg * or stage contains two or more functions with the same 898301e04c3fSmrg * name if the name is associated with a subroutine type." 898401e04c3fSmrg */ 898501e04c3fSmrg 898601e04c3fSmrg for (int i = 0; i < state->num_subroutines; i++) { 898701e04c3fSmrg unsigned definitions = 0; 898801e04c3fSmrg ir_function *fn = state->subroutines[i]; 898901e04c3fSmrg /* Calculate number of function definitions with the same name */ 899001e04c3fSmrg foreach_in_list(ir_function_signature, sig, &fn->signatures) { 899101e04c3fSmrg if (sig->is_defined) { 899201e04c3fSmrg if (++definitions > 1) { 899301e04c3fSmrg _mesa_glsl_error(&loc, state, 899401e04c3fSmrg "%s shader contains two or more function " 899501e04c3fSmrg "definitions with name `%s', which is " 899601e04c3fSmrg "associated with a subroutine type.\n", 899701e04c3fSmrg _mesa_shader_stage_to_string(state->stage), 899801e04c3fSmrg fn->name); 899901e04c3fSmrg return; 900001e04c3fSmrg } 900101e04c3fSmrg } 900201e04c3fSmrg } 900301e04c3fSmrg } 900401e04c3fSmrg} 900501e04c3fSmrg 900601e04c3fSmrgstatic void 900701e04c3fSmrgremove_per_vertex_blocks(exec_list *instructions, 900801e04c3fSmrg _mesa_glsl_parse_state *state, ir_variable_mode mode) 900901e04c3fSmrg{ 901001e04c3fSmrg /* Find the gl_PerVertex interface block of the appropriate (in/out) mode, 901101e04c3fSmrg * if it exists in this shader type. 901201e04c3fSmrg */ 901301e04c3fSmrg const glsl_type *per_vertex = NULL; 901401e04c3fSmrg switch (mode) { 901501e04c3fSmrg case ir_var_shader_in: 901601e04c3fSmrg if (ir_variable *gl_in = state->symbols->get_variable("gl_in")) 901701e04c3fSmrg per_vertex = gl_in->get_interface_type(); 901801e04c3fSmrg break; 901901e04c3fSmrg case ir_var_shader_out: 902001e04c3fSmrg if (ir_variable *gl_Position = 902101e04c3fSmrg state->symbols->get_variable("gl_Position")) { 902201e04c3fSmrg per_vertex = gl_Position->get_interface_type(); 902301e04c3fSmrg } 902401e04c3fSmrg break; 902501e04c3fSmrg default: 902601e04c3fSmrg assert(!"Unexpected mode"); 902701e04c3fSmrg break; 902801e04c3fSmrg } 902901e04c3fSmrg 903001e04c3fSmrg /* If we didn't find a built-in gl_PerVertex interface block, then we don't 903101e04c3fSmrg * need to do anything. 903201e04c3fSmrg */ 903301e04c3fSmrg if (per_vertex == NULL) 903401e04c3fSmrg return; 903501e04c3fSmrg 903601e04c3fSmrg /* If the interface block is used by the shader, then we don't need to do 903701e04c3fSmrg * anything. 903801e04c3fSmrg */ 903901e04c3fSmrg interface_block_usage_visitor v(mode, per_vertex); 904001e04c3fSmrg v.run(instructions); 904101e04c3fSmrg if (v.usage_found()) 904201e04c3fSmrg return; 904301e04c3fSmrg 904401e04c3fSmrg /* Remove any ir_variable declarations that refer to the interface block 904501e04c3fSmrg * we're removing. 904601e04c3fSmrg */ 904701e04c3fSmrg foreach_in_list_safe(ir_instruction, node, instructions) { 904801e04c3fSmrg ir_variable *const var = node->as_variable(); 904901e04c3fSmrg if (var != NULL && var->get_interface_type() == per_vertex && 905001e04c3fSmrg var->data.mode == mode) { 905101e04c3fSmrg state->symbols->disable_variable(var->name); 905201e04c3fSmrg var->remove(); 905301e04c3fSmrg } 905401e04c3fSmrg } 905501e04c3fSmrg} 9056ed98bd31Smaya 9057ed98bd31Smayair_rvalue * 9058ed98bd31Smayaast_warnings_toggle::hir(exec_list *, 9059ed98bd31Smaya struct _mesa_glsl_parse_state *state) 9060ed98bd31Smaya{ 9061ed98bd31Smaya state->warnings_enabled = enable; 9062ed98bd31Smaya return NULL; 9063ed98bd31Smaya} 9064